From: Jan Kara <jack@suse.cz>
To: Christian Brauner <brauner@kernel.org>
Cc: <linux-fsdevel@vger.kernel.org>,
Christoph Hellwig <hch@infradead.org>, <linux-mm@kvack.org>,
Mikhail Rudenko <xyzzy@yandex-team.ru>,
Andreas Gruenbacher <agruenba@redhat.com>,
Jan Kara <jack@suse.cz>
Subject: [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim
Date: Tue, 24 Nov 2026 12:58:50 +0100 [thread overview]
Message-ID: <20261124115850.3512-14-jack@suse.cz> (raw)
In-Reply-To: <20261124113450.3451-1-jack@suse.cz>
Deferring difficult inode reclaim from prune_icache_sb() to a workqueue
removes the natural feedback loop of blocking tasks in direct reclaim
until they make space for new allocations. This can result in the list
of deferred inodes to grow beyond any bounds and possibly push the
machine to a reclaim storm or OOM.
Add a throttling mechanism that makes tasks calling
mark_inode_reclaim_deferred() wait on exit to userspace if the number of
deferred inodes to reclaim for a superblock grows over limit. We measure
average time it takes to reclaim inode on deferred list for a superblock
and block tasks proportionally to that.
The mechanism to block tasks on exit to userspace is a bit simplistic
but I don't expect more than one inode to be deferred per syscall and I
expect throttling to be rare so it should be fine. If we wanted
something more advanced, we could store the delay in task_struct and
just wait for it when returning to userspace similarly as blkcg
throttling does it but so far I consider the space in task_struct a
waste for this purpose.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/inode.c | 150 ++++++++++++++++++++++++++++---
include/linux/fs/super_types.h | 2 +
include/trace/events/writeback.h | 51 +++++++++++
3 files changed, 189 insertions(+), 14 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index 760c730f4a9f..b6fa2965f4e8 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -23,6 +23,7 @@
#include <linux/rw_hint.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
+#include <linux/task_work.h>
#include <trace/events/writeback.h>
#define CREATE_TRACE_POINTS
#include <trace/events/timestamp.h>
@@ -1142,23 +1143,55 @@ static int get_inode_reclaim_batch(int *id, struct list_head *list)
return count;
}
+static void inode_reclaim_update_stat(struct super_block *sb, unsigned int n,
+ u64 start)
+{
+ u64 delay;
+
+ delay = div_u64(ktime_get_ns() - start, n);
+ /*
+ * Smooth delay updates with exponential moving average. Updates can
+ * get lost if workers race but we don't really care.
+ */
+ WRITE_ONCE(sb->s_deferred_reclaim_delay,
+ (63 * READ_ONCE(sb->s_deferred_reclaim_delay) + delay) / 64);
+
+ trace_inode_reclaim_update_stat(sb, n, delay,
+ READ_ONCE(sb->s_deferred_reclaim_delay));
+}
+
+static void sb_deferred_reclaim_put(struct super_block *sb, int n)
+{
+ /*
+ * The elevated s_deferred_reclaim_count keeps sb alive until we drop
+ * it. Use RCU to make sure sb isn't freed before the wakeup.
+ */
+ rcu_read_lock();
+ if (!atomic_sub_return(n, &sb->s_deferred_reclaim_count))
+ wake_up_var(&sb->s_deferred_reclaim_count);
+ rcu_read_unlock();
+}
static void inode_reclaim_deferred(struct work_struct *work)
{
struct inode *inode;
LIST_HEAD(inode_batch);
+ u64 start;
+ int id, count;
+ struct super_block *sb;
+
/*
* We start with the list corresponding to the work but rolling a dice
* would work as well
*/
- int id = work - deferred_reclaim.work;
+ id = work - deferred_reclaim.work;
while (1) {
if (!get_inode_reclaim_batch(&id, &inode_batch))
break;
- while (!list_empty(&inode_batch)) {
- struct super_block *sb;
+ sb = NULL;
+ while (!list_empty(&inode_batch)) {
/*
* inode_batch list is private and I_FREEING flags
* protect us from anybody else trying to remove the
@@ -1166,20 +1199,27 @@ static void inode_reclaim_deferred(struct work_struct *work)
*/
inode = list_first_entry(&inode_batch, struct inode,
i_lru);
+ if (inode->i_sb != sb) {
+ if (sb) {
+ inode_reclaim_update_stat(sb, count,
+ start);
+ sb_deferred_reclaim_put(sb, count);
+ }
+
+ sb = inode->i_sb;
+ count = 0;
+ start = ktime_get_ns();
+ }
+ count++;
list_del_init(&inode->i_lru);
- sb = inode->i_sb;
evict(inode);
- /*
- * The elevated s_deferred_reclaim_count keeps sb alive
- * until we drop it. Use RCU to keep sb alive until
- * the wakeup is done.
- */
- rcu_read_lock();
- if (atomic_dec_and_test(&sb->s_deferred_reclaim_count))
- wake_up_var(&sb->s_deferred_reclaim_count);
- rcu_read_unlock();
cond_resched();
}
+
+ if (sb) {
+ inode_reclaim_update_stat(sb, count, start);
+ sb_deferred_reclaim_put(sb, count);
+ }
}
}
@@ -1215,6 +1255,80 @@ static void __init deferred_reclaim_init(void)
}
}
+struct deferred_reclaim_delay {
+ struct callback_head task_work;
+ u64 delay;
+};
+
+static void deferred_delay_work(struct callback_head *work)
+{
+ struct deferred_reclaim_delay *dd =
+ container_of(work, struct deferred_reclaim_delay, task_work);
+
+ schedule_timeout_killable(nsecs_to_jiffies(dd->delay));
+ kfree(dd);
+}
+
+/*
+ * Size of deferred reclaim list from which we start throttling tasks creating
+ * inodes marked for deferred reclaim.
+ */
+#define INODE_DEFERRED_RECLAIM_LIMIT 8192
+
+static void throttle_inode_deferred_reclaim(struct inode *inode)
+{
+ struct super_block *sb = inode->i_sb;
+ unsigned int len;
+
+ /*
+ * Don't throttle kthreads. They are doing work for somebody else
+ * anyway so throttling is counterproductive.
+ */
+ if (current->flags & PF_KTHREAD)
+ return;
+
+ /*
+ * If inodes with deferred reclaim are accumulating too much, slow down
+ * tasks creating them. This doesn't provide any kind of guarantee on
+ * the length of the deferred list since lots of inodes with
+ * I_DEFER_RECLAIM can be already present in the inode cache and we
+ * have no control when they reach the deferred list. But if the
+ * pressure on the deferred list is sustained, the balance should
+ * eventually be established.
+ */
+ len = atomic_read(&sb->s_deferred_reclaim_count);
+ if (len > INODE_DEFERRED_RECLAIM_LIMIT) {
+ u64 delay = READ_ONCE(sb->s_deferred_reclaim_delay);
+ struct deferred_reclaim_delay *dd;
+
+ if (!delay)
+ return;
+
+ dd = kmalloc_obj(*dd, GFP_NOFS | __GFP_NORETRY);
+ /*
+ * If the allocation failed, don't bother. Throttling is
+ * approximate anyway and next time we'll throttle more.
+ */
+ if (!dd)
+ return;
+
+ /*
+ * Scale the delay based on how much we exceed the limit. Wait
+ * at most 4x as long as estimated time to reclaim the inode.
+ */
+ len = min(len, 5 * INODE_DEFERRED_RECLAIM_LIMIT);
+ delay = div_u64(delay * (len - INODE_DEFERRED_RECLAIM_LIMIT),
+ INODE_DEFERRED_RECLAIM_LIMIT);
+ trace_mark_inode_reclaim_deferred_throttle(inode, len, delay);
+
+ dd->delay = delay;
+ init_task_work(&dd->task_work, deferred_delay_work);
+ /* This shouldn't fail. If it does, just cleanly bail. */
+ if (task_work_add(current, &dd->task_work, TWA_RESUME) < 0)
+ kfree(dd);
+ }
+}
+
/*
* Mark inode for deferred reclaim. When slab reclaim path will be freeing the
* inode, it will not call evict() directly but rather offload it to a worker
@@ -1231,12 +1345,20 @@ static void __init deferred_reclaim_init(void)
*/
void mark_inode_reclaim_deferred(struct inode *inode)
{
+ bool throttle = false;
+
if (inode_state_read_once(inode) & I_DEFER_RECLAIM)
return;
spin_lock(&inode->i_lock);
- inode_state_set(inode, I_DEFER_RECLAIM);
+ if (!(inode_state_read(inode) & I_DEFER_RECLAIM)) {
+ inode_state_set(inode, I_DEFER_RECLAIM);
+ throttle = true;
+ }
spin_unlock(&inode->i_lock);
+
+ if (throttle)
+ throttle_inode_deferred_reclaim(inode);
}
EXPORT_SYMBOL_GPL(mark_inode_reclaim_deferred);
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index fc8f7f2261e2..fe952ef02b96 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -241,6 +241,8 @@ struct super_block {
/* Number of inodes queued for deferred reclaim */
atomic_t s_deferred_reclaim_count;
+ /* Average time to reclaim one deferred inode */
+ u64 s_deferred_reclaim_delay;
/* Number of inodes with nlink == 0 but still referenced */
atomic_long_t s_remove_count;
diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
index c2a5b492a71a..d371d1112843 100644
--- a/include/trace/events/writeback.h
+++ b/include/trace/events/writeback.h
@@ -885,6 +885,57 @@ DEFINE_EVENT(writeback_inode_template, sb_clear_inode_writeback,
TP_ARGS(inode)
);
+TRACE_EVENT(inode_reclaim_update_stat,
+ TP_PROTO(
+ struct super_block *sb,
+ unsigned int n,
+ u64 batch_delay,
+ u64 avg_delay
+ ),
+ TP_ARGS(sb, n, batch_delay, avg_delay),
+
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ __field(unsigned int, n)
+ __field(u64, batch_delay)
+ __field(u64, avg_delay)
+ ),
+
+ TP_fast_assign(
+ __entry->dev = sb->s_dev;
+ __entry->n = n;
+ __entry->batch_delay = batch_delay;
+ __entry->avg_delay = avg_delay;
+ ),
+
+ TP_printk("dev %d,%d batch size %u batch delay %llu ns avg delay %llu ns",
+ MAJOR(__entry->dev), MINOR(__entry->dev), __entry->n,
+ __entry->batch_delay, __entry->avg_delay)
+);
+
+TRACE_EVENT(mark_inode_reclaim_deferred_throttle,
+ TP_PROTO(struct inode *inode, unsigned int len, u64 delay),
+ TP_ARGS(inode, len, delay),
+
+ TP_STRUCT__entry(
+ __field(u64, ino)
+ __field(dev_t, dev)
+ __field(unsigned int, len)
+ __field(u64, delay)
+ ),
+
+ TP_fast_assign(
+ __entry->ino = inode->i_ino;
+ __entry->dev = inode->i_sb->s_dev;
+ __entry->len = len;
+ __entry->delay = delay;
+ ),
+
+ TP_printk("dev %d,%d ino %llu deferred list len %u delay %llu ns",
+ MAJOR(__entry->dev), MINOR(__entry->dev),
+ __entry->ino, __entry->len, __entry->delay)
+);
+
#endif /* _TRACE_WRITEBACK_H */
/* This part must be outside protection */
--
2.53.0
next prev parent reply other threads:[~2026-11-24 11:59 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
2026-11-24 11:58 ` [PATCH v2 1/8] writeback: Fix missed lazytime flush Jan Kara
2026-11-24 11:58 ` [PATCH v2 2/8] fs: Avoid inode dirtying on last iput Jan Kara
2026-11-24 11:58 ` [PATCH v2 3/8] fs: Basic infrastructure for offloading inode reclaim Jan Kara
2026-11-24 11:58 ` [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes Jan Kara
2026-11-24 11:58 ` [PATCH v2 5/8] fs: Defer reclaim of inodes requiring metadata writeback Jan Kara
2026-11-24 11:58 ` Jan Kara [this message]
2026-11-24 11:58 ` [PATCH v2 7/8] fs: Protect deferred inode reclaim from freezing Jan Kara
2026-11-24 11:58 ` [PATCH v2 8/8] ext4: Defer inode reclaim if it has preallocations Jan Kara
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124115850.3512-14-jack@suse.cz \
--to=jack@suse.cz \
--cc=agruenba@redhat.com \
--cc=brauner@kernel.org \
--cc=hch@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=xyzzy@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox