* [PATCH v2 1/8] writeback: Fix missed lazytime flush
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
@ 2026-11-24 11:58 ` Jan Kara
2026-11-24 11:58 ` [PATCH v2 2/8] fs: Avoid inode dirtying on last iput Jan Kara
` (6 subsequent siblings)
7 siblings, 0 replies; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
move_expired_inodes() moves to b_io list inodes which were dirtied at
current_time - dirtytime_expire_interval or before.
__writeback_single_inode() however calls sync_lazytime() only if the
inode was dirtied strictly before current_time -
dirtytime_expire_interval. It can thus happen that inode with dirty
timestamps gets queued to b_io list, then fails the check in
__writeback_single_inode() and as a result is just placed back to
b_dirty_time list with a refreshed timestamp and so timestamps aren't
written when they should have been.
This is a very difficult race to hit in practice but changes to how
dirty time expiry is handled on last iput() will make this much easier
to hit.
Fixes: 5fcd57505c00 ("writeback: Drop I_DIRTY_TIME_EXPIRE")
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/fs-writeback.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index de77b6bc1dc4..65b1af2879e8 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -1910,8 +1910,8 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
*/
if ((inode_state_read_once(inode) & I_DIRTY_TIME) &&
(wbc->sync_mode == WB_SYNC_ALL ||
- time_after(jiffies, inode->dirtied_time_when +
- dirtytime_expire_interval * HZ)))
+ time_after_eq(jiffies, inode->dirtied_time_when +
+ dirtytime_expire_interval * HZ)))
sync_lazytime(inode);
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 2/8] fs: Avoid inode dirtying on last iput
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 ` Jan Kara
2026-11-25 12:00 ` sashiko-bot
2026-11-24 11:58 ` [PATCH v2 3/8] fs: Basic infrastructure for offloading inode reclaim Jan Kara
` (5 subsequent siblings)
7 siblings, 1 reply; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
When inode has dirtied timestamps, we currently call sync_lazytime() on
last iput. This is done because inode with any dirty bit set is not
inserted into LRU and dirty timestamps expire only after many (12 by
default) hours so these inodes would be sitting outside of LRU aging for
a really long time. However this can result in doing IO and consequently
GFP_NOFAIL allocations from dentry reclaim making MM complain. Sample
trace for ext4 is:
prune_dcache_sb
shrink_dentry_list
__dentry_kill
iput
sync_lazytime
__mark_inode_dirty
ext4_dirty_inode
__ext4_mark_inode_dirty
ext4_reserve_inode_write
ext4_get_inode_loc
bdev_getblk
__filemap_get_folio_mpol
Avoid this dirtying on last iput by reshuffling unused inodes to the
beginning of b_dirty_time list and clobbering dirtied_time_when instead
so that they get written during next periodic writeback.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/fs-writeback.c | 86 ++++++++++++++++++++++++++++++++++++++++++++---
fs/inode.c | 15 ++++-----
fs/internal.h | 1 +
3 files changed, 90 insertions(+), 12 deletions(-)
diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index 65b1af2879e8..0332a2edef93 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -1583,6 +1583,42 @@ static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
spin_unlock(&inode->i_lock);
}
+static void inode_add_dirtytime_head(struct inode *inode,
+ struct bdi_writeback *wb)
+{
+ unsigned long new_time;
+
+ new_time = jiffies - dirtytime_expire_interval * HZ;
+ if (!list_empty(&wb->b_dirty_time)) {
+ struct inode *first = wb_inode(wb->b_dirty_time.prev);
+ unsigned long first_time = READ_ONCE(first->dirtied_time_when);
+
+ if (time_before(first_time, new_time))
+ new_time = first_time;
+ }
+ inode->dirtied_when = new_time;
+ inode->dirtied_time_when = new_time;
+ list_move_tail(&inode->i_io_list, &wb->b_dirty_time);
+}
+
+static void move_to_dirtytime_locked(struct inode *inode,
+ struct bdi_writeback *wb)
+{
+ /* If the inode is used, move it to the tail of b_dirty_time list */
+ if (icount_read(inode)) {
+ inode->dirtied_when = jiffies;
+ inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
+ return;
+ }
+
+ /*
+ * Inode is not used. Expedite writeback by moving it to the head
+ * of b_dirty_time list so that it can enter LRU and be evicted.
+ */
+ inode_add_dirtytime_head(inode, wb);
+ wb_io_lists_depopulated(wb);
+}
+
/*
* requeue inode for re-scanning after bdi->b_io list is exhausted.
*/
@@ -1830,8 +1866,7 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
*/
redirty_tail_locked(inode, wb);
} else if (inode_state_read(inode) & I_DIRTY_TIME) {
- inode->dirtied_when = jiffies;
- inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
+ move_to_dirtytime_locked(inode, wb);
inode_state_clear(inode, I_SYNC_QUEUED);
} else {
/* The inode is clean. Remove from writeback lists. */
@@ -2072,8 +2107,7 @@ static int writeback_single_inode(struct inode *inode,
inode_cgwb_move_to_attached(inode, wb);
else if (!(istate & I_SYNC_QUEUED) && !(istate & I_DIRTY)) {
WARN_ON_ONCE(!(istate & I_DIRTY_TIME));
- inode->dirtied_when = jiffies;
- inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
+ move_to_dirtytime_locked(inode, wb);
}
}
@@ -2962,6 +2996,50 @@ void __mark_inode_dirty(struct inode *inode, int flags)
}
EXPORT_SYMBOL(__mark_inode_dirty);
+/*
+ * If inode has dirty timestamps to write out, make sure flush worker writes
+ * them out during its next periodic writeback writeout.
+ */
+void queue_dirtytime_writeback(struct inode *inode)
+{
+ struct bdi_writeback *wb;
+ unsigned int i_state;
+
+ lockdep_assert_held(&inode->i_lock);
+
+ wb = locked_inode_to_wb_and_lock_list(inode);
+ spin_lock(&inode->i_lock);
+ i_state = inode_state_read(inode);
+ /* Written back while we dropped i_lock? */
+ if (!(i_state & I_DIRTY_TIME))
+ goto out_wb_lock;
+
+ /*
+ * If the inode is dirty, next writeback will come soon so we don't
+ * need to do anything. If it is under writeback, we cannot touch dirty
+ * lists now so we handle moving the inode after writeback is complete.
+ */
+ if (i_state & (I_SYNC_QUEUED | I_SYNC | I_DIRTY)) {
+ /*
+ * Clobber dirtied_time_when so that timestamps get synced on
+ * next inode writeback.
+ */
+ inode->dirtied_time_when = jiffies -
+ dirtytime_expire_interval * HZ;
+ goto out_wb_lock;
+ }
+
+ /*
+ * Move inode to the beginning of dirty queue so that it gets written
+ * out during the next periodic writeback.
+ */
+ inode_add_dirtytime_head(inode, wb);
+ /* Wake up flush worker to ensure timely flush of the inode */
+ wb_wakeup_delayed(wb);
+out_wb_lock:
+ spin_unlock(&wb->list_lock);
+}
+
/*
* The @s_sync_lock is used to serialise concurrent sync operations
* to avoid lock contention problems with concurrent wait_sb_inodes() calls.
diff --git a/fs/inode.c b/fs/inode.c
index bf9403c32188..a9f757fba23b 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2051,7 +2051,6 @@ void iput(struct inode *inode)
if (unlikely(!inode))
return;
-retry:
lockdep_assert_not_held(&inode->i_lock);
VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), inode);
/*
@@ -2064,14 +2063,14 @@ void iput(struct inode *inode)
if (atomic_add_unless(&inode->i_count, -1, 1))
return;
- if (inode->i_nlink && sync_lazytime(inode))
- goto retry;
-
spin_lock(&inode->i_lock);
- if (unlikely((inode_state_read(inode) & I_DIRTY_TIME) && inode->i_nlink)) {
- spin_unlock(&inode->i_lock);
- goto retry;
- }
+ /*
+ * If inode has timestamp updates pending, queue flushing them now as
+ * otherwise the dirtiness could be preventing the inode from entering
+ * LRU for hours.
+ */
+ if (inode->i_nlink && inode_state_read(inode) & I_DIRTY_TIME)
+ queue_dirtytime_writeback(inode);
if (!atomic_dec_and_test(&inode->i_count)) {
spin_unlock(&inode->i_lock);
diff --git a/fs/internal.h b/fs/internal.h
index 9580afef293e..855a80f4b879 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -222,6 +222,7 @@ bool in_group_or_capable(const struct mnt_idmap *idmap,
*/
long get_nr_dirty_inodes(void);
bool sync_lazytime(struct inode *inode);
+void queue_dirtytime_writeback(struct inode *inode);
/*
* dcache.c
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 2/8] fs: Avoid inode dirtying on last iput
2026-11-24 11:58 ` [PATCH v2 2/8] fs: Avoid inode dirtying on last iput Jan Kara
@ 2026-11-25 12:00 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 12:00 UTC (permalink / raw)
To: Jan Kara, Christian Brauner
Cc: Christoph Hellwig, linux-mm, Mikhail Rudenko, Andreas Gruenbacher
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] writeback: missing delayed writeback wakeup for unused
I_DIRTY_TIME inodes
- [Medium] writeback: missing wb_io_lists_depopulated() in
inode_add_dirtytime_head()
- [Medium] writeback: clobbered dirtied_time_when not reset in
__mark_inode_dirty()
- [Medium] fs: lost lazytime updates and wasted queuing on immediate
eviction in iput()
--
commit ee4802f28b5dfd62a7f8c5bc17c60aed4d146987
Author: Jan Kara <jack@suse.cz>
fs: Avoid inode dirtying on last iput
This commit replaces synchronous sync_lazytime() on final iput() with
queue_dirtytime_writeback(), moving unused I_DIRTY_TIME inodes to the head
of wb->b_dirty_time with an expired dirtied_time_when timestamp so they are
flushed on the next periodic writeback pass.
> diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
> index 65b1af2879e87..0332a2edef938 100644
> --- a/fs/fs-writeback.c
> +++ b/fs/fs-writeback.c
> @@ -1583,6 +1583,42 @@ static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
> spin_unlock(&inode->i_lock);
> }
>
> +static void inode_add_dirtytime_head(struct inode *inode,
> + struct bdi_writeback *wb)
> +{
> + unsigned long new_time;
> +
> + new_time = jiffies - dirtytime_expire_interval * HZ;
> + if (!list_empty(&wb->b_dirty_time)) {
> + struct inode *first = wb_inode(wb->b_dirty_time.prev);
> + unsigned long first_time = READ_ONCE(first->dirtied_time_when);
> +
> + if (time_before(first_time, new_time))
> + new_time = first_time;
> + }
> + inode->dirtied_when = new_time;
> + inode->dirtied_time_when = new_time;
> + list_move_tail(&inode->i_io_list, &wb->b_dirty_time);
> +}
[Severity: Medium]
Should inode_add_dirtytime_head() call wb_io_lists_depopulated(wb) when
moving the inode onto wb->b_dirty_time?
move_to_dirtytime_locked() calls wb_io_lists_depopulated(wb) right after
inode_add_dirtytime_head(), but queue_dirtytime_writeback() does not. An
inode with only I_DIRTY_TIME set can be on wb->b_dirty if
inode_do_switch_wbs() placed it there (since it checks I_DIRTY_ALL) right
before process_inode_switch_wbs() drops its temporary reference with
iput(), or if redirty_tail_locked() moved an expired I_DIRTY_TIME inode
from b_io to b_dirty. When queue_dirtytime_writeback() moves the last
such inode from b_dirty to b_dirty_time, WB_has_dirty_io stays set on wb
and wb->avg_write_bandwidth remains added to wb->bdi->tot_write_bandwidth.
> +
> +static void move_to_dirtytime_locked(struct inode *inode,
> + struct bdi_writeback *wb)
> +{
> + /* If the inode is used, move it to the tail of b_dirty_time list */
> + if (icount_read(inode)) {
> + inode->dirtied_when = jiffies;
> + inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
> + return;
> + }
> +
> + /*
> + * Inode is not used. Expedite writeback by moving it to the head
> + * of b_dirty_time list so that it can enter LRU and be evicted.
> + */
> + inode_add_dirtytime_head(inode, wb);
> + wb_io_lists_depopulated(wb);
> +}
[Severity: Medium]
Can an unused I_DIRTY_TIME inode moved here miss periodic writeback and
stay off the inode LRU until dirtytime_expire_interval (12 hours) elapses?
If final iput() runs while I_SYNC or I_SYNC_QUEUED is set,
queue_dirtytime_writeback() clobbers dirtied_time_when and defers list
movement without calling wb_wakeup_delayed(wb). If the in-flight
__writeback_single_inode() already passed its I_DIRTY_TIME expiration
check, writeback finishes with I_DIRTY_TIME still set and calls
move_to_dirtytime_locked(). That moves the unused inode to
wb->b_dirty_time and clears WB_has_dirty_io via wb_io_lists_depopulated()
without scheduling wb->dwork.
The same stall can happen when queue_dirtytime_writeback() calls
wb_wakeup_delayed(wb) while wb->dwork is already queued for an earlier
deadline (or wb_wakeup() runs it immediately): if wb_workfn() runs before
wb->last_old_flush + dirty_writeback_interval, wb_check_old_data_flush()
returns 0 without checking b_dirty_time, and wb_workfn() only re-arms
wb_wakeup_delayed(wb) when wb_has_dirty_io(wb) is true.
[ ... ]
> @@ -2962,6 +2996,50 @@ void __mark_inode_dirty(struct inode *inode, int flags)
> }
> EXPORT_SYMBOL(__mark_inode_dirty);
>
> +/*
> + * If inode has dirty timestamps to write out, make sure flush worker writes
> + * them out during its next periodic writeback writeout.
> + */
> +void queue_dirtytime_writeback(struct inode *inode)
> +{
> + struct bdi_writeback *wb;
> + unsigned int i_state;
> +
> + lockdep_assert_held(&inode->i_lock);
> +
> + wb = locked_inode_to_wb_and_lock_list(inode);
> + spin_lock(&inode->i_lock);
> + i_state = inode_state_read(inode);
> + /* Written back while we dropped i_lock? */
> + if (!(i_state & I_DIRTY_TIME))
> + goto out_wb_lock;
> +
> + /*
> + * If the inode is dirty, next writeback will come soon so we don't
> + * need to do anything. If it is under writeback, we cannot touch dirty
> + * lists now so we handle moving the inode after writeback is complete.
> + */
> + if (i_state & (I_SYNC_QUEUED | I_SYNC | I_DIRTY)) {
> + /*
> + * Clobber dirtied_time_when so that timestamps get synced on
> + * next inode writeback.
> + */
> + inode->dirtied_time_when = jiffies -
> + dirtytime_expire_interval * HZ;
> + goto out_wb_lock;
> + }
[Severity: Medium]
Does backdating inode->dirtied_time_when leave a stale expired timestamp on
the cached inode after writeback clears I_DIRTY_TIME?
When __writeback_single_inode() later calls sync_lazytime(), I_DIRTY_TIME
is cleared while dirtied_time_when keeps the backdated value. If the
cached inode is later reopened, dirtied with I_DIRTY_PAGES (so was_dirty is
true) or queued with I_SYNC_QUEUED, and then marked I_DIRTY_TIME,
__mark_inode_dirty() skips updating dirtied_time_when:
fs/fs-writeback.c:__mark_inode_dirty() {
...
if (inode_state_read(inode) & I_SYNC_QUEUED)
goto out_unlock;
...
if (!was_dirty) {
...
inode->dirtied_when = jiffies;
if (dirtytime)
inode->dirtied_time_when = jiffies;
}
Because dirtied_time_when remains expired, the next data writeback on the
active inode immediately forces sync_lazytime(), and
move_to_dirtytime_locked() (with i_count > 0) can place the inode at the
tail of b_dirty_time with dirtied_when = jiffies but an already-expired
dirtied_time_when.
> diff --git a/fs/inode.c b/fs/inode.c
> index f2003ae753745..4b13125f0678c 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -2052,7 +2052,6 @@ void iput(struct inode *inode)
> if (unlikely(!inode))
> return;
>
> -retry:
> lockdep_assert_not_held(&inode->i_lock);
> VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), inode);
> /*
> @@ -2065,14 +2064,14 @@ void iput(struct inode *inode)
> if (atomic_add_unless(&inode->i_count, -1, 1))
> return;
>
> - if (inode->i_nlink && sync_lazytime(inode))
> - goto retry;
> -
> spin_lock(&inode->i_lock);
> - if (unlikely((inode_state_read(inode) & I_DIRTY_TIME) && inode->i_nlink)) {
> - spin_unlock(&inode->i_lock);
> - goto retry;
> - }
> + /*
> + * If inode has timestamp updates pending, queue flushing them now as
> + * otherwise the dirtiness could be preventing the inode from entering
> + * LRU for hours.
> + */
> + if (inode->i_nlink && inode_state_read(inode) & I_DIRTY_TIME)
> + queue_dirtytime_writeback(inode);
>
> if (!atomic_dec_and_test(&inode->i_count)) {
> spin_unlock(&inode->i_lock);
[Severity: Medium]
Can deferring lazytime writeout here lose pending timestamp updates when
iput_final() drops the inode immediately instead of retaining it?
When op->drop_inode(inode) returns 1 in iput_final(), it sets I_FREEING,
skips write_inode_now(), and calls evict(), which removes the inode from
wb->b_dirty_time via inode_io_list_del(). For filesystems where
->drop_inode() returns 1 on an inode that still exists on disk (such as
gfs2_drop_inode() when SDF_EVICTING is set during gfs2_evict_inodes() or on
iopen glock demotion), sync_lazytime() previously called ->dirty_inode()
before eviction so gfs2_dirty_inode() marked the glock GLF_DIRTY and
evict_linked_inode() wrote the timestamps out. Without sync_lazytime(),
neither GLF_DIRTY nor I_DIRTY is set and evict_linked_inode() skips the
writeout.
Also, whenever iput_final() evicts immediately (drop != 0, I_DONTCACHE, or
!SB_ACTIVE), calling queue_dirtytime_writeback() in iput() first drops
i_lock, locks wb->list_lock, queues the inode onto b_dirty_time (even if
unhashed), and arms wb_wakeup_delayed(), only for evict() to immediately
remove it from b_dirty_time.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124115850.3512-9-jack@suse.cz?part=2
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 3/8] fs: Basic infrastructure for offloading inode reclaim
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 ` Jan Kara
2026-11-25 12:00 ` sashiko-bot
2026-11-24 11:58 ` [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes Jan Kara
` (4 subsequent siblings)
7 siblings, 1 reply; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
Reclaim of some inodes is rather complex requiring running transactions
or doing other IO. Consequently filesystems end up doing GFP_NOFAIL
allocations from kswapd or even direct reclaim which is problematic
because forward progress of these allocations isn't guaranteed. Add
infrastructure for marking inodes whose reclaim is difficult and offload
reclaim of such inodes into a workqueue to not block kswapd with
difficult inode reclaim. The processing of the queued inodes happens in
parallel from several work items so that the reclaim has higher chances
of keeping up with the inflow of inodes if reclaim of some inodes
progresses slowly.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/inode.c | 215 ++++++++++++++++++++++++++++++-
fs/super.c | 2 +
include/linux/fs.h | 3 +
include/linux/fs/super_types.h | 3 +
include/trace/events/writeback.h | 3 +-
5 files changed, 222 insertions(+), 4 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index a9f757fba23b..e08c6f0b0336 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -74,6 +74,25 @@ static DEFINE_PER_CPU(unsigned long, nr_unused);
static struct kmem_cache *inode_cachep __ro_after_init;
+struct deferred_reclaim_queue {
+ struct list_head list;
+ spinlock_t lock;
+};
+
+/* Inodes for deferred reclaim */
+struct inode_deferred_reclaim {
+ int workers; /* Number of parallel workers */
+ struct workqueue_struct *wq; /* Workqueue for workers */
+ struct work_struct *work; /* Workers */
+ /*
+ * Queue of deferred inodes. The number of queues is the same number
+ * as the number of workers
+ */
+ struct deferred_reclaim_queue *queues;
+};
+
+static struct inode_deferred_reclaim deferred_reclaim;
+
static long get_nr_inodes(void)
{
int i;
@@ -965,7 +984,7 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
*/
if (icount_read(inode) ||
(inode_state_read(inode) &
- ~(I_REFERENCED | I_METADATA_WRITEBACK)) ||
+ ~(I_REFERENCED | I_METADATA_WRITEBACK | I_DEFER_RECLAIM)) ||
!mapping_shrinkable(&inode->i_data)) {
list_lru_isolate(lru, &inode->i_lru);
spin_unlock(&inode->i_lock);
@@ -1016,6 +1035,38 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
return LRU_REMOVED;
}
+/* Number of inodes to process in one reclaim batch */
+#define INODE_RECLAIM_BATCH_SIZE 16
+
+static int get_deferred_sb_id(struct super_block *sb)
+{
+ return hash_ptr(sb, 32) % deferred_reclaim.workers;
+}
+
+static void queue_deferred_reclaim(struct super_block *sb, int count,
+ struct list_head *list)
+{
+ int id = get_deferred_sb_id(sb);
+ struct deferred_reclaim_queue *queue =
+ &deferred_reclaim.queues[id];
+ int i, wake_count;
+
+ spin_lock(&queue->lock);
+ list_splice_tail(list, &queue->list);
+ atomic_add(count, &sb->s_deferred_reclaim_count);
+ spin_unlock(&queue->lock);
+
+ /* Queue works to process inodes we've added to the list */
+ wake_count = DIV_ROUND_UP(count, INODE_RECLAIM_BATCH_SIZE);
+ if (wake_count > deferred_reclaim.workers)
+ wake_count = deferred_reclaim.workers;
+ for (i = 0; i < wake_count; i++) {
+ queue_work(deferred_reclaim.wq,
+ &deferred_reclaim.work[id]);
+ id = (id + 1) % deferred_reclaim.workers;
+ }
+}
+
/*
* Walk the superblock inode LRU for freeable inodes and attempt to free them.
* This is called from the superblock shrinker function with a number of inodes
@@ -1025,14 +1076,169 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
{
LIST_HEAD(freeable);
- long freed;
+ long freed, deferred = 0;
+ struct inode *inode, *tmp;
freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
inode_lru_isolate, &freeable);
- dispose_list(&freeable);
+
+ list_for_each_entry_safe(inode, tmp, &freeable, i_lru) {
+ if (inode_state_read_once(inode) & I_DEFER_RECLAIM) {
+ deferred++;
+ continue;
+ }
+ list_del_init(&inode->i_lru);
+ evict(inode);
+ cond_resched();
+ }
+
+ if (!list_empty(&freeable))
+ queue_deferred_reclaim(sb, deferred, &freeable);
return freed;
}
+static void __get_inode_reclaim_batch(int *start, int end,
+ struct list_head *list, int *count)
+{
+ int i;
+
+ for (i = *start; i < end; i++) {
+ struct deferred_reclaim_queue *queue =
+ &deferred_reclaim.queues[i];
+ struct list_head *entry, tmp;
+
+ if (list_empty_careful(&queue->list))
+ continue;
+ spin_lock(&queue->lock);
+ list_for_each(entry, &queue->list) {
+ if (*count >= INODE_RECLAIM_BATCH_SIZE)
+ break;
+ (*count)++;
+ }
+ list_cut_before(&tmp, &queue->list, entry);
+ list_splice_tail(&tmp, list);
+ spin_unlock(&queue->lock);
+ if (*count >= INODE_RECLAIM_BATCH_SIZE)
+ break;
+ }
+ *start = i;
+}
+
+/* Move a batch of inodes from queued lists to our private list */
+static int get_inode_reclaim_batch(int *id, struct list_head *list)
+{
+ int count = 0;
+ int orig_id = *id;
+
+ __get_inode_reclaim_batch(id, deferred_reclaim.workers, list, &count);
+ if (count >= INODE_RECLAIM_BATCH_SIZE)
+ return count;
+ /* Wrap around */
+ if (orig_id > 0) {
+ *id = 0;
+ __get_inode_reclaim_batch(id, orig_id, list, &count);
+ }
+ return count;
+}
+
+
+static void inode_reclaim_deferred(struct work_struct *work)
+{
+ struct inode *inode;
+ LIST_HEAD(inode_batch);
+ /*
+ * We start with the list corresponding to the work but rolling a dice
+ * would work as well
+ */
+ int 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;
+
+ /*
+ * inode_batch list is private and I_FREEING flags
+ * protect us from anybody else trying to remove the
+ * inode from the LRU list. No locking needed.
+ */
+ inode = list_first_entry(&inode_batch, struct inode,
+ i_lru);
+ 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();
+ }
+ }
+}
+
+/* Maximum number of inode reclaim workers */
+#define MAX_RECLAIM_WORKERS 16
+
+static void __init deferred_reclaim_init(void)
+{
+ int workers = MAX_RECLAIM_WORKERS;
+ int i;
+
+ /* Scale down the number of workers for small systems */
+ if (workers > num_possible_cpus())
+ workers = num_possible_cpus();
+
+ deferred_reclaim.workers = workers;
+ deferred_reclaim.work = kmalloc_objs(struct work_struct, workers);
+ deferred_reclaim.queues =
+ kmalloc_objs(struct deferred_reclaim_queue, workers);
+ if (!deferred_reclaim.work || !deferred_reclaim.queues)
+ panic("Failed to allocate deferred inode reclaim queues");
+
+ deferred_reclaim.wq = alloc_workqueue("deferred-inode-reclaim",
+ WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_UNBOUND,
+ workers);
+ if (!deferred_reclaim.wq)
+ panic("Failed to allocate deferred inode reclaim workqueue");
+
+ for (i = 0; i < workers; i++) {
+ INIT_WORK(&deferred_reclaim.work[i], inode_reclaim_deferred);
+ spin_lock_init(&deferred_reclaim.queues[i].lock);
+ INIT_LIST_HEAD(&deferred_reclaim.queues[i].list);
+ }
+}
+
+/*
+ * 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
+ * in a workqueue. This is useful when inode reclaim is expected to take a
+ * long time (e.g. because it needs to do IO) so that it doesn't block kswapd
+ * or because it needs allocating memory (which cannot be guaranteed from
+ * reclaim path).
+ *
+ * Note that we currently don't provide a way to unmark inode for deferred
+ * reclaim. It could be done but the benefit is unclear. The rationale is that
+ * to make inode hard to reclaim, relatively expensive things like writing data
+ * to the file have to be done and in such case the additional CPU overhead and
+ * latency of offloading the inode reclaim is negligible in the overall load.
+ */
+void mark_inode_reclaim_deferred(struct inode *inode)
+{
+ if (inode_state_read_once(inode) & I_DEFER_RECLAIM)
+ return;
+
+ spin_lock(&inode->i_lock);
+ inode_state_set(inode, I_DEFER_RECLAIM);
+ spin_unlock(&inode->i_lock);
+}
+EXPORT_SYMBOL_GPL(mark_inode_reclaim_deferred);
+
static void __wait_on_freeing_inode(struct inode *inode, bool hash_locked, bool rcu_locked);
static bool igrab_from_hash(struct inode *inode);
@@ -2680,6 +2886,9 @@ void __init inode_init(void)
SLAB_ACCOUNT),
init_once);
+ /* Deferred inode reclaim infrastructure */
+ deferred_reclaim_init();
+
/* Hash may have been set up in inode_init_early */
if (!hashdist)
return;
diff --git a/fs/super.c b/fs/super.c
index 4e68e7cac342..fa345d5027bf 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -652,6 +652,8 @@ void deactivate_locked_super(struct super_block *s)
struct file_system_type *fs = s->s_type;
if (atomic_dec_and_test(&s->s_active)) {
shrinker_free(s->s_shrink);
+ wait_var_event(&s->s_deferred_reclaim_count,
+ !atomic_read(&s->s_deferred_reclaim_count));
fs->kill_sb(s);
kill_super_notify(s);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 92a2841184f3..fc1a2cc9eb21 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -744,6 +744,7 @@ enum inode_state_flags_enum {
I_SYNC_QUEUED = (1U << 17),
I_PINNING_NETFS_WB = (1U << 18),
I_METADATA_WRITEBACK = (1U << 19),
+ I_DEFER_RECLAIM = (1U << 20),
};
#define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
@@ -2229,6 +2230,8 @@ static inline void set_inode_metadata_writeback(struct inode *inode)
spin_unlock(&inode->i_lock);
}
+void mark_inode_reclaim_deferred(struct inode *inode);
+
/*
* returns the refcount on the inode. it can change arbitrarily.
*/
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index 4454e3574b2c..fc8f7f2261e2 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -239,6 +239,9 @@ struct super_block {
struct shrinker *s_shrink; /* per-sb shrinker handle */
+ /* Number of inodes queued for deferred reclaim */
+ atomic_t s_deferred_reclaim_count;
+
/* 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 b7dfc9f7bb61..c2a5b492a71a 100644
--- a/include/trace/events/writeback.h
+++ b/include/trace/events/writeback.h
@@ -28,7 +28,8 @@
{I_DONTCACHE, "I_DONTCACHE"}, \
{I_SYNC_QUEUED, "I_SYNC_QUEUED"}, \
{I_PINNING_NETFS_WB, "I_PINNING_NETFS_WB"}, \
- {I_LRU_ISOLATING, "I_LRU_ISOLATING"} \
+ {I_LRU_ISOLATING, "I_LRU_ISOLATING"}, \
+ {I_DEFER_RECLAIM, "I_DEFER_RECLAIM"} \
)
/* enums need to be exported to user space */
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 3/8] fs: Basic infrastructure for offloading inode reclaim
2026-11-24 11:58 ` [PATCH v2 3/8] fs: Basic infrastructure for offloading inode reclaim Jan Kara
@ 2026-11-25 12:00 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 12:00 UTC (permalink / raw)
To: Jan Kara, Christian Brauner
Cc: Christoph Hellwig, linux-mm, Mikhail Rudenko, Andreas Gruenbacher
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] fs: queue_deferred_reclaim() fails to wake sibling workers on
small batches
--
commit 8c1021dc7fb5b82263bdc1a9f11c91562cd680a6
Author: Jan Kara <jack@suse.cz>
fs: Basic infrastructure for offloading inode reclaim
This commit adds infrastructure for marking inodes with I_DEFER_RECLAIM
and offloading their eviction from the superblock shrinker to a multi-worker
workqueue.
> diff --git a/fs/inode.c b/fs/inode.c
> index 4b13125f0678c..8853eb6015dc0 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
[ ... ]
> @@ -1017,6 +1036,38 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
> return LRU_REMOVED;
> }
>
> +/* Number of inodes to process in one reclaim batch */
> +#define INODE_RECLAIM_BATCH_SIZE 16
> +
> +static int get_deferred_sb_id(struct super_block *sb)
> +{
> + return hash_ptr(sb, 32) % deferred_reclaim.workers;
> +}
> +
> +static void queue_deferred_reclaim(struct super_block *sb, int count,
> + struct list_head *list)
> +{
> + int id = get_deferred_sb_id(sb);
> + struct deferred_reclaim_queue *queue =
> + &deferred_reclaim.queues[id];
> + int i, wake_count;
> +
> + spin_lock(&queue->lock);
> + list_splice_tail(list, &queue->list);
> + atomic_add(count, &sb->s_deferred_reclaim_count);
> + spin_unlock(&queue->lock);
> +
> + /* Queue works to process inodes we've added to the list */
> + wake_count = DIV_ROUND_UP(count, INODE_RECLAIM_BATCH_SIZE);
> + if (wake_count > deferred_reclaim.workers)
> + wake_count = deferred_reclaim.workers;
> + for (i = 0; i < wake_count; i++) {
> + queue_work(deferred_reclaim.wq,
> + &deferred_reclaim.work[id]);
> + id = (id + 1) % deferred_reclaim.workers;
> + }
> +}
[Severity: Medium]
Can queue_deferred_reclaim() serialize deferred eviction for a superblock
onto a single worker when inodes are queued in batches of
INODE_RECLAIM_BATCH_SIZE (16) or fewer?
Because get_deferred_sb_id(sb) returns a fixed starting index for a given
superblock and wake_count is computed only from the current call's count,
any call with count <= 16 sets wake_count = 1 and only tries to queue
deferred_reclaim.work[id]. When multiple prune_icache_sb() scans (or
single-inode queuing from iput_final() in commit "fs: Defer reclaim of
unlinked inodes") each add <= 16 inodes while work[id] is busy and already
marked pending, queue_work() on work[id] returns false and the loop exits
without waking work[id + 1] or any of the other idle workers, even if a
large backlog has accumulated on queue->list.
Computing wake_count from the total queued count (or advancing to the next
worker when queue_work() returns false) would allow sibling workers to help
drain the queue via get_inode_reclaim_batch().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124115850.3512-9-jack@suse.cz?part=3
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
` (2 preceding siblings ...)
2026-11-24 11:58 ` [PATCH v2 3/8] fs: Basic infrastructure for offloading inode reclaim Jan Kara
@ 2026-11-24 11:58 ` Jan Kara
2026-11-25 12:00 ` sashiko-bot
2026-11-24 11:58 ` [PATCH v2 5/8] fs: Defer reclaim of inodes requiring metadata writeback Jan Kara
` (3 subsequent siblings)
7 siblings, 1 reply; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
Sometimes it can happen that reclaim of some slab object drops the last
reference of an inode that is already unlinked. The frequently reported
case of this is a reclaim of overlayfs inode dropping the last reference
of an inode on upper filesystem. Because the inode is unlinked, evict()
will delete it which generally requires writing to the filesystem and
memory allocations - not good from kswapd or even direct reclaim. Defer
inode reclaim of unlinked inodes to a workqueue when the last inode
reference is dropped from a reclaim path.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/inode.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/fs/inode.c b/fs/inode.c
index e08c6f0b0336..4ffafb101718 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2239,6 +2239,23 @@ static void iput_final(struct inode *inode)
inode_lru_list_del(inode);
spin_unlock(&inode->i_lock);
+ /*
+ * Are we going to delete this inode while in reclaim path? Likely
+ * reclaim of some object (e.g. overlayfs inode) ended up dropping the
+ * last reference to an already deleted inode. Offload the eviction as
+ * it's likely to require memory allocations and block on IO. Since
+ * this eviction was triggered by a reclaim of unrelated object, the
+ * inode is already outliving the operation that originally deleted it
+ * (unlink, file close...). A slightly longer delay thus should not be
+ * observable by userspace.
+ */
+ if (current->flags & PF_MEMALLOC && !inode->i_nlink) {
+ LIST_HEAD(defer);
+
+ list_add(&inode->i_lru, &defer);
+ queue_deferred_reclaim(sb, 1, &defer);
+ return;
+ }
evict(inode);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes
2026-11-24 11:58 ` [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes Jan Kara
@ 2026-11-25 12:00 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 12:00 UTC (permalink / raw)
To: Jan Kara, Christian Brauner
Cc: Christoph Hellwig, linux-mm, Mikhail Rudenko, Andreas Gruenbacher
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] fs: single-worker serialization in iput_final() via
queue_deferred_reclaim()
--
commit 8449483727b2bbcd575fed1bb97c8ea7882097a9
Author: Jan Kara <jack@suse.cz>
fs: Defer reclaim of unlinked inodes
This commit offloads the eviction of unlinked inodes to the deferred reclaim
workqueue when the last reference is dropped from a memory reclaim context.
> diff --git a/fs/inode.c b/fs/inode.c
> index 8853eb6015dc0..2977d5f1382b9 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -2240,6 +2240,23 @@ static void iput_final(struct inode *inode)
> inode_lru_list_del(inode);
> spin_unlock(&inode->i_lock);
>
> + /*
> + * Are we going to delete this inode while in reclaim path? Likely
> + * reclaim of some object (e.g. overlayfs inode) ended up dropping the
> + * last reference to an already deleted inode. Offload the eviction as
> + * it's likely to require memory allocations and block on IO. Since
> + * this eviction was triggered by a reclaim of unrelated object, the
> + * inode is already outliving the operation that originally deleted it
> + * (unlink, file close...). A slightly longer delay thus should not be
> + * observable by userspace.
> + */
> + if (current->flags & PF_MEMALLOC && !inode->i_nlink) {
> + LIST_HEAD(defer);
> +
> + list_add(&inode->i_lru, &defer);
> + queue_deferred_reclaim(sb, 1, &defer);
> + return;
> + }
[Severity: Medium]
Does passing count = 1 to queue_deferred_reclaim() here serialize all
deferred unlinked inode evictions for a superblock onto a single worker?
queue_deferred_reclaim() starts at id = get_deferred_sb_id(sb) and computes
how many workers to wake from the per-call count rather than the total queued
inodes:
fs/inode.c:queue_deferred_reclaim() {
...
wake_count = DIV_ROUND_UP(count, INODE_RECLAIM_BATCH_SIZE);
if (wake_count > deferred_reclaim.workers)
wake_count = deferred_reclaim.workers;
for (i = 0; i < wake_count; i++) {
queue_work(deferred_reclaim.wq,
&deferred_reclaim.work[id]);
id = (id + 1) % deferred_reclaim.workers;
}
}
Because iput_final() queues one inode at a time, wake_count is always 1 and
only deferred_reclaim.work[id] for that superblock's hash id is ever queued.
When reclaim (such as overlayfs dcache/icache shrinking) drops the last
reference to many unlinked upper inodes, the remaining workers in
deferred_reclaim.work are never woken, throttling background inode deletion to
one thread.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124115850.3512-9-jack@suse.cz?part=4
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 5/8] fs: Defer reclaim of inodes requiring metadata writeback
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
` (3 preceding siblings ...)
2026-11-24 11:58 ` [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes Jan Kara
@ 2026-11-24 11:58 ` Jan Kara
2026-11-24 11:58 ` [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim Jan Kara
` (2 subsequent siblings)
7 siblings, 0 replies; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
When we are reclaiming inodes which have I_METADATA_WRITEBACK set, we
have to callback into the filesystem to write out any dirty metadata
associated with the inode since we are going to loose track of it. This
will require IO and is problematic from memory reclaim paths. Defer
reclaim of inodes with I_METADATA_WRITEBACK set.
Reported-by: Christian Brauner (Amutable) <brauner@kernel.org>
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/inode.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/inode.c b/fs/inode.c
index 4ffafb101718..760c730f4a9f 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1083,7 +1083,8 @@ long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
inode_lru_isolate, &freeable);
list_for_each_entry_safe(inode, tmp, &freeable, i_lru) {
- if (inode_state_read_once(inode) & I_DEFER_RECLAIM) {
+ if (inode_state_read_once(inode) &
+ (I_DEFER_RECLAIM | I_METADATA_WRITEBACK)) {
deferred++;
continue;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
` (4 preceding siblings ...)
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
2026-11-25 12:00 ` sashiko-bot
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
7 siblings, 1 reply; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
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
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim
2026-11-24 11:58 ` [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim Jan Kara
@ 2026-11-25 12:00 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 12:00 UTC (permalink / raw)
To: Jan Kara, Christian Brauner
Cc: Christoph Hellwig, linux-mm, Mikhail Rudenko, Andreas Gruenbacher
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] fs: sub-jiffy delay truncation to 1-jiffy sleep in
deferred_delay_work()
- [Low] trace: writeback: non-standard
mark_inode_reclaim_deferred_throttle name
- [Low] fs: missing __GFP_NOWARN and direct GFP_NOFS in
throttle_inode_deferred_reclaim()
--
commit 1ea5687985f2e4f8753b554cdf12dd2d8a420e3c
Author: Jan Kara <jack@suse.cz>
fs: Add throttling to deferred inode reclaim
This commit adds a throttling mechanism that delays tasks on return to
userspace when the per-superblock deferred inode reclaim list exceeds
INODE_DEFERRED_RECLAIM_LIMIT, proportional to the moving-average per-inode
eviction duration.
> diff --git a/fs/inode.c b/fs/inode.c
> index dfba54288cbfd..fdab88f875454 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
[ ... ]
> @@ -1216,6 +1256,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)
> +{
[ ... ]
> + 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;
[Severity: Low]
Since allocation failure is intentionally ignored here, should this include
__GFP_NOWARN so a failed slab expansion under memory pressure does not trigger
warn_alloc() and slab_out_of_memory() splats in dmesg? Also, MM guidelines
prefer scoped memalloc_nofs_save()/memalloc_nofs_restore() (or
GFP_NOWAIT | __GFP_NOWARN) over passing GFP_NOFS directly.
> +
> + /*
> + * 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);
[Severity: Medium]
Does passing nsecs_to_jiffies(dd->delay) to schedule_timeout_killable() stall
tasks for a full jiffy whenever the scaled nanosecond delay is sub-jiffy or 0?
throttle_inode_deferred_reclaim() checks !delay and allocates dd before
scaling delay down by (len - 8192) / 8192, so a small s_deferred_reclaim_delay
near the limit scales to 0 ns and still queues deferred_delay_work().
Even when the scaled delay is non-zero, any sub-jiffy value is truncated to
0 jiffies by nsecs_to_jiffies(), and schedule_timeout(0) does not return
immediately: it arms a timer for jiffies + 1 and sleeps until the next timer
tick (1 to 10 ms depending on HZ), defeating the proportional ramp-up.
Scaling delay before checking !delay and allocating dd, and sleeping with
schedule_hrtimeout() in deferred_delay_work(), would avoid queuing 0-ns waits
and preserve sub-jiffy delays.
[ ... ]
> diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
> index c2a5b492a71a5..d371d1112843f 100644
> --- a/include/trace/events/writeback.h
> +++ b/include/trace/events/writeback.h
[ ... ]
> +TRACE_EVENT(mark_inode_reclaim_deferred_throttle,
> + TP_PROTO(struct inode *inode, unsigned int len, u64 delay),
> + TP_ARGS(inode, len, delay),
[Severity: Low]
This isn't a bug, but tracepoint names normally follow the subsystem_event
convention (such as inode_reclaim_throttle, matching inode_reclaim_update_stat
above) rather than starting with the verb mark_.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124115850.3512-9-jack@suse.cz?part=6
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 7/8] fs: Protect deferred inode reclaim from freezing
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
` (5 preceding siblings ...)
2026-11-24 11:58 ` [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim Jan Kara
@ 2026-11-24 11:58 ` Jan Kara
2026-11-25 12:00 ` sashiko-bot
2026-11-24 11:58 ` [PATCH v2 8/8] ext4: Defer inode reclaim if it has preallocations Jan Kara
7 siblings, 1 reply; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
Sometimes inode reclaim needs writing to the filesystem. One prominent
case is when inode is unlinked and we are deleting it, another case is
when XFS needs to trim its speculative block preallocation. When inode
reclaim runs on frozen filesystem, this will block the deferred inode
reclaim worker so eventually all deferred inode reclaim could be stalled
in the system because all workers are waiting for some filesystem to
thaw.
Avoid this problem by protecting running deferred inode reclaim from
filesystem freezing (the SB_FREEZE_INTWRITE level) and by just skipping
starting new deferred inode reclaim for inodes on frozen filesystem.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/inode.c | 89 ++++++++++++++++++++++++++++------
fs/super.c | 31 ++++++++++++
include/linux/fs/super.h | 1 +
include/linux/fs/super_types.h | 2 +
4 files changed, 107 insertions(+), 16 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index b6fa2965f4e8..25508c122e27 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1044,20 +1044,11 @@ static int get_deferred_sb_id(struct super_block *sb)
return hash_ptr(sb, 32) % deferred_reclaim.workers;
}
-static void queue_deferred_reclaim(struct super_block *sb, int count,
- struct list_head *list)
+void wake_deferred_reclaim_workers(struct super_block *sb, int count)
{
int id = get_deferred_sb_id(sb);
- struct deferred_reclaim_queue *queue =
- &deferred_reclaim.queues[id];
int i, wake_count;
- spin_lock(&queue->lock);
- list_splice_tail(list, &queue->list);
- atomic_add(count, &sb->s_deferred_reclaim_count);
- spin_unlock(&queue->lock);
-
- /* Queue works to process inodes we've added to the list */
wake_count = DIV_ROUND_UP(count, INODE_RECLAIM_BATCH_SIZE);
if (wake_count > deferred_reclaim.workers)
wake_count = deferred_reclaim.workers;
@@ -1068,6 +1059,33 @@ static void queue_deferred_reclaim(struct super_block *sb, int count,
}
}
+static void queue_deferred_reclaim(struct super_block *sb, int count,
+ struct list_head *list)
+{
+ int id = get_deferred_sb_id(sb);
+ struct deferred_reclaim_queue *queue =
+ &deferred_reclaim.queues[id];
+
+ spin_lock(&queue->lock);
+ list_splice_tail(list, &queue->list);
+ atomic_add(count, &sb->s_deferred_reclaim_count);
+ /*
+ * Make sure queued inodes are visible before we check whether deferred
+ * reclaim is blocked. Pairs with the barrier in thaw_super_locked().
+ */
+ smp_mb__after_atomic();
+ spin_unlock(&queue->lock);
+
+ /*
+ * Skip waking up workers for frozen filesystem. They'd skip the inodes
+ * anyway.
+ */
+ if (atomic_read(&sb->s_deferred_reclaim_running) < 0)
+ return;
+
+ wake_deferred_reclaim_workers(sb, count);
+}
+
/*
* Walk the superblock inode LRU for freeable inodes and attempt to free them.
* This is called from the superblock shrinker function with a number of inodes
@@ -1107,18 +1125,24 @@ static void __get_inode_reclaim_batch(int *start, int end,
for (i = *start; i < end; i++) {
struct deferred_reclaim_queue *queue =
&deferred_reclaim.queues[i];
- struct list_head *entry, tmp;
+ struct inode *inode, *next;
if (list_empty_careful(&queue->list))
continue;
spin_lock(&queue->lock);
- list_for_each(entry, &queue->list) {
- if (*count >= INODE_RECLAIM_BATCH_SIZE)
+ list_for_each_entry_safe(inode, next, &queue->list, i_lru) {
+ /*
+ * Skip inodes on frozen fs as we'd likely block. We
+ * always accept inodes from read-only fs as there
+ * eviction cannot require writing.
+ */
+ if (!sb_rdonly(inode->i_sb) &&
+ inode->i_sb->s_writers.frozen != SB_UNFROZEN)
+ continue;
+ list_move_tail(&inode->i_lru, list);
+ if (++(*count) >= INODE_RECLAIM_BATCH_SIZE)
break;
- (*count)++;
}
- list_cut_before(&tmp, &queue->list, entry);
- list_splice_tail(&tmp, list);
spin_unlock(&queue->lock);
if (*count >= INODE_RECLAIM_BATCH_SIZE)
break;
@@ -1160,8 +1184,34 @@ static void inode_reclaim_update_stat(struct super_block *sb, unsigned int n,
READ_ONCE(sb->s_deferred_reclaim_delay));
}
+/* Put inodes from sb back to reclaim queue */
+static void inode_reclaim_skip_inodes(struct list_head *list,
+ struct super_block *sb)
+{
+ struct inode *inode;
+ struct deferred_reclaim_queue *queue;
+ struct list_head tmp;
+
+ queue = &deferred_reclaim.queues[get_deferred_sb_id(sb)];
+ list_for_each_entry(inode, list, i_lru)
+ if (inode->i_sb != sb)
+ break;
+ list_cut_before(&tmp, list, &inode->i_lru);
+ spin_lock(&queue->lock);
+ list_splice_tail(&tmp, &queue->list);
+ spin_unlock(&queue->lock);
+}
+
+static bool sb_deferred_reclaim_get(struct super_block *sb)
+{
+ return atomic_inc_unless_negative(&sb->s_deferred_reclaim_running);
+}
+
static void sb_deferred_reclaim_put(struct super_block *sb, int n)
{
+ if (atomic_dec_and_test(&sb->s_deferred_reclaim_running))
+ wake_up_var(&sb->s_deferred_reclaim_running);
+
/*
* 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.
@@ -1209,6 +1259,13 @@ static void inode_reclaim_deferred(struct work_struct *work)
sb = inode->i_sb;
count = 0;
start = ktime_get_ns();
+ /* Protect us from filesystem freezing */
+ if (!sb_deferred_reclaim_get(sb)) {
+ inode_reclaim_skip_inodes(&inode_batch,
+ sb);
+ sb = NULL;
+ continue;
+ }
}
count++;
list_del_init(&inode->i_lru);
diff --git a/fs/super.c b/fs/super.c
index fa345d5027bf..77e6ab6383b8 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -2435,6 +2435,19 @@ static inline bool may_unfreeze(struct super_block *sb, enum freeze_holder who,
return false;
}
+/* Unblock deferred inode reclaim and wake workers */
+static void sb_unblock_deferred_reclaim(struct super_block *sb)
+{
+ atomic_set(&sb->s_deferred_reclaim_running, 0);
+ /*
+ * Make sure we see all queued inodes added while deferred reclaim was
+ * blocked. Pairs with the barrier in queue_deferred_reclaim()
+ */
+ smp_mb();
+ wake_deferred_reclaim_workers(sb,
+ atomic_read(&sb->s_deferred_reclaim_count));
+}
+
/**
* freeze_super - lock the filesystem and force it into a consistent state
* @sb: the super to lock
@@ -2561,10 +2574,26 @@ int freeze_super(struct super_block *sb, enum freeze_holder who, const void *fre
sb->s_writers.frozen = SB_UNFROZEN;
sb_freeze_unlock(sb, SB_FREEZE_PAGEFAULT);
sb_freeze_wake(sb);
+ /*
+ * Deferred reclaim of some inodes could have been skipped
+ * while sb->s_writers.frozen was set. Requeue work now.
+ */
+ wake_deferred_reclaim_workers(sb,
+ atomic_read(&sb->s_deferred_reclaim_count));
deactivate_locked_super(sb);
return ret;
}
+ /*
+ * Block deferred reclaim so that workers don't hang on frozen fs. We
+ * wait for outstanding deferred reclaim here. Because the filesystem
+ * is already partially frozen, workers will not start new deferred
+ * reclaim for this sb.
+ */
+ while (atomic_cmpxchg(&sb->s_deferred_reclaim_running, 0, -1))
+ wait_var_event(&sb->s_deferred_reclaim_running,
+ atomic_read(&sb->s_deferred_reclaim_running) == 0);
+
/* Now wait for internal filesystem counter */
sb->s_writers.frozen = SB_FREEZE_FS;
sb_wait_write(sb, SB_FREEZE_FS);
@@ -2577,6 +2606,7 @@ int freeze_super(struct super_block *sb, enum freeze_holder who, const void *fre
sb->s_writers.frozen = SB_UNFROZEN;
sb_freeze_unlock(sb, SB_FREEZE_FS);
sb_freeze_wake(sb);
+ sb_unblock_deferred_reclaim(sb);
deactivate_locked_super(sb);
return ret;
}
@@ -2648,6 +2678,7 @@ static int thaw_super_locked(struct super_block *sb, enum freeze_holder who,
sb->s_writers.freeze_owner = NULL;
sb_freeze_wake(sb);
sb_freeze_unlock(sb, SB_FREEZE_FS);
+ sb_unblock_deferred_reclaim(sb);
out_deactivate:
deactivate_locked_super(sb);
return 0;
diff --git a/include/linux/fs/super.h b/include/linux/fs/super.h
index 1f063769ff5c..ad102dc5a461 100644
--- a/include/linux/fs/super.h
+++ b/include/linux/fs/super.h
@@ -258,6 +258,7 @@ int freeze_super(struct super_block *super, enum freeze_holder who,
int thaw_super(struct super_block *super, enum freeze_holder who,
const void *freeze_owner);
+void wake_deferred_reclaim_workers(struct super_block *sb, int count);
int sb_init_dio_done_wq(struct super_block *sb);
struct file;
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index fe952ef02b96..ec0ff6a504d4 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;
+ /* Number of workers processing deferred reclaim on this sb */
+ atomic_t s_deferred_reclaim_running;
/* Average time to reclaim one deferred inode */
u64 s_deferred_reclaim_delay;
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 7/8] fs: Protect deferred inode reclaim from freezing
2026-11-24 11:58 ` [PATCH v2 7/8] fs: Protect deferred inode reclaim from freezing Jan Kara
@ 2026-11-25 12:00 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 12:00 UTC (permalink / raw)
To: Jan Kara, Christian Brauner
Cc: Christoph Hellwig, linux-mm, Mikhail Rudenko, Andreas Gruenbacher
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] fs: skipping I_FREEING inodes during freeze deadlocks inode
lookups
- [Medium] fs: state mismatch during thaw causes busy loop in
inode_reclaim_deferred()
- [Medium] fs: skipped frozen inodes at head of queue->list cause O(N)
rescans
- [Medium] fs: deferred reclaim of stacked inode can block on frozen
lower fs
- [Low] fs: queue_deferred_reclaim() wakes workers during freeze stages
--
commit 52eb2fd15de6155077a87f493e0148deba16ccc7
Author: Jan Kara <jack@suse.cz>
fs: Protect deferred inode reclaim from freezing
This commit protects running deferred inode reclaim against filesystem
freezing and skips starting new deferred reclaim for inodes on a frozen
filesystem.
> diff --git a/fs/inode.c b/fs/inode.c
> index fdab88f875454..c0460c0780e00 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -1045,20 +1045,11 @@ static int get_deferred_sb_id(struct super_block *sb)
> return hash_ptr(sb, 32) % deferred_reclaim.workers;
> }
>
> -static void queue_deferred_reclaim(struct super_block *sb, int count,
> - struct list_head *list)
> +void wake_deferred_reclaim_workers(struct super_block *sb, int count)
> {
> int id = get_deferred_sb_id(sb);
> - struct deferred_reclaim_queue *queue =
> - &deferred_reclaim.queues[id];
> int i, wake_count;
>
> - spin_lock(&queue->lock);
> - list_splice_tail(list, &queue->list);
> - atomic_add(count, &sb->s_deferred_reclaim_count);
> - spin_unlock(&queue->lock);
> -
> - /* Queue works to process inodes we've added to the list */
> wake_count = DIV_ROUND_UP(count, INODE_RECLAIM_BATCH_SIZE);
> if (wake_count > deferred_reclaim.workers)
> wake_count = deferred_reclaim.workers;
> @@ -1069,6 +1060,33 @@ static void queue_deferred_reclaim(struct super_block *sb, int count,
> }
> }
>
> +static void queue_deferred_reclaim(struct super_block *sb, int count,
> + struct list_head *list)
> +{
> + int id = get_deferred_sb_id(sb);
> + struct deferred_reclaim_queue *queue =
> + &deferred_reclaim.queues[id];
> +
> + spin_lock(&queue->lock);
> + list_splice_tail(list, &queue->list);
> + atomic_add(count, &sb->s_deferred_reclaim_count);
> + /*
> + * Make sure queued inodes are visible before we check whether deferred
> + * reclaim is blocked. Pairs with the barrier in thaw_super_locked().
> + */
> + smp_mb__after_atomic();
> + spin_unlock(&queue->lock);
> +
> + /*
> + * Skip waking up workers for frozen filesystem. They'd skip the inodes
> + * anyway.
> + */
> + if (atomic_read(&sb->s_deferred_reclaim_running) < 0)
> + return;
> +
> + wake_deferred_reclaim_workers(sb, count);
> +}
[Severity: Low]
Does this still wake workers unnecessarily during the earlier stages of
freeze_super()?
freeze_super() sets sb->s_writers.frozen to SB_FREEZE_WRITE and
SB_FREEZE_PAGEFAULT before sb_wait_write() and sync_filesystem(), while
s_deferred_reclaim_running is only set to -1 after sync_filesystem()
completes. During that window, queue_deferred_reclaim() wakes workers
even though __get_inode_reclaim_batch() will immediately skip all inodes
from sb because s_writers.frozen != SB_UNFROZEN.
> @@ -1108,18 +1126,24 @@ static void __get_inode_reclaim_batch(int *start, int end,
> for (i = *start; i < end; i++) {
> struct deferred_reclaim_queue *queue =
> &deferred_reclaim.queues[i];
> - struct list_head *entry, tmp;
> + struct inode *inode, *next;
>
> if (list_empty_careful(&queue->list))
> continue;
> spin_lock(&queue->lock);
> - list_for_each(entry, &queue->list) {
> - if (*count >= INODE_RECLAIM_BATCH_SIZE)
> + list_for_each_entry_safe(inode, next, &queue->list, i_lru) {
> + /*
> + * Skip inodes on frozen fs as we'd likely block. We
> + * always accept inodes from read-only fs as there
> + * eviction cannot require writing.
> + */
> + if (!sb_rdonly(inode->i_sb) &&
> + inode->i_sb->s_writers.frozen != SB_UNFROZEN)
> + continue;
[Severity: High]
Can skipping queued inodes here deadlock freeze_super() and stall inode
lookups?
Inodes queued for deferred reclaim via prune_icache_sb() or iput_final()
already have I_FREEING set, but remain hashed in inode_hashtable (and
filesystem lookup tables such as XFS per-AG radix trees) until evict()
runs in inode_reclaim_deferred(). Any lookup of such an inode blocks in
__wait_on_freeing_inode() waiting for evict() to finish.
CPU0 (freeze_super) CPU1 (in-flight writer) CPU2 (reclaim worker)
------------------- ----------------------- ---------------------
s_writers.frozen =
SB_FREEZE_WRITE
sb_start_write() held
iget_locked(ino)
__wait_on_freeing_inode()
__get_inode_reclaim_batch()
s_writers.frozen !=
SB_UNFROZEN
-> skips inode
sb_wait_write(
SB_FREEZE_WRITE)
<-- deadlocks
Also, while the filesystem is frozen, super_cache_scan() ->
prune_icache_sb() continues isolating inodes with I_DEFER_RECLAIM or
I_METADATA_WRITEBACK, setting I_FREEING and queuing them where they will
not be evicted until thaw, causing any reader looking up those inodes to
hang uninterruptibly in __wait_on_freeing_inode().
> + list_move_tail(&inode->i_lru, list);
> + if (++(*count) >= INODE_RECLAIM_BATCH_SIZE)
> break;
> - (*count)++;
> }
> - list_cut_before(&tmp, &queue->list, entry);
> - list_splice_tail(&tmp, list);
> spin_unlock(&queue->lock);
[Severity: Medium]
Does leaving skipped inodes from a frozen superblock at the head of
queue->list cause O(N) rescans under queue->lock?
Because deferred_reclaim.queues are shared across superblocks and
queue_deferred_reclaim() appends new batches at the tail, every batch
dequeued for an unfrozen superblock on the same queue must walk past all
frozen inodes at the front under queue->lock. Moreover, when any other
worker collects fewer than INODE_RECLAIM_BATCH_SIZE inodes from its own
queue, get_inode_reclaim_batch() wraps around across all queues, seeing
!list_empty_careful(&queue->list) and locking and scanning all N frozen
inodes on that queue only to skip them.
[ ... ]
> @@ -1210,6 +1260,13 @@ static void inode_reclaim_deferred(struct work_struct *work)
> sb = inode->i_sb;
> count = 0;
> start = ktime_get_ns();
> + /* Protect us from filesystem freezing */
> + if (!sb_deferred_reclaim_get(sb)) {
> + inode_reclaim_skip_inodes(&inode_batch,
> + sb);
> + sb = NULL;
> + continue;
> + }
> }
> count++;
> list_del_init(&inode->i_lru);
[Severity: Medium]
Can evicting a stacked filesystem inode (such as overlayfs or ecryptfs) in
inode_reclaim_deferred() still block the worker on a frozen lower
filesystem?
When iput_final() drops the last reference to an unlinked stacked inode
under PF_MEMALLOC, it queues the stacked inode to deferred_reclaim:
fs/inode.c:iput_final() {
...
if (current->flags & PF_MEMALLOC && !inode->i_nlink) {
LIST_HEAD(defer);
list_add(&inode->i_lru, &defer);
queue_deferred_reclaim(sb, 1, &defer);
return;
}
evict(inode);
}
__get_inode_reclaim_batch() and sb_deferred_reclaim_get() only check the
stacked superblock (inode->i_sb), which is unfrozen. When the worker runs
evict(inode), current->flags does not have PF_MEMALLOC set, so dropping
the underlying unlinked dentry/inode in ovl_destroy_inode() or
ecryptfs_evict_inode() enters iput_final() -> evict() synchronously on the
frozen lower writable filesystem and blocks on sb_start_intwrite().
> diff --git a/fs/super.c b/fs/super.c
> index fa345d5027bff..77e6ab6383b8b 100644
> --- a/fs/super.c
> +++ b/fs/super.c
[ ... ]
> @@ -2648,6 +2678,7 @@ static int thaw_super_locked(struct super_block *sb, enum freeze_holder who,
> sb->s_writers.frozen = SB_UNFROZEN;
> sb->s_writers.freeze_owner = NULL;
> sb_freeze_wake(sb);
> sb_freeze_unlock(sb, SB_FREEZE_FS);
> + sb_unblock_deferred_reclaim(sb);
> out_deactivate:
> deactivate_locked_super(sb);
> return 0;
[Severity: Medium]
Can setting sb->s_writers.frozen = SB_UNFROZEN before
sb_unblock_deferred_reclaim(sb) cause inode_reclaim_deferred() to spin in
a tight loop without calling cond_resched()?
In both thaw_super_locked() and the freeze_fs() error path of
freeze_super(), s_writers.frozen becomes SB_UNFROZEN before
s_deferred_reclaim_running is reset from -1 to 0 in
sb_unblock_deferred_reclaim(). During that window,
__get_inode_reclaim_batch() only checks s_writers.frozen != SB_UNFROZEN
and moves the inodes to inode_batch, after which
sb_deferred_reclaim_get(sb) fails, inode_reclaim_skip_inodes() splices
them back onto queue->list, and inode_reclaim_deferred() continues
without reaching cond_resched(), immediately re-dequeuing the same inodes
on the next iteration. Calling sb_unblock_deferred_reclaim(sb) before
setting s_writers.frozen = SB_UNFROZEN (or also checking
s_deferred_reclaim_running in __get_inode_reclaim_batch()) would avoid
this window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124115850.3512-9-jack@suse.cz?part=7
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 8/8] ext4: Defer inode reclaim if it has preallocations
2026-11-24 11:58 [PATCH 0/8 v4] fs: Deferred inode reclaim Jan Kara
` (6 preceding siblings ...)
2026-11-24 11:58 ` [PATCH v2 7/8] fs: Protect deferred inode reclaim from freezing Jan Kara
@ 2026-11-24 11:58 ` Jan Kara
7 siblings, 0 replies; 14+ messages in thread
From: Jan Kara @ 2026-11-24 11:58 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Christoph Hellwig, linux-mm, Mikhail Rudenko,
Andreas Gruenbacher, Jan Kara
When we have to free preallocations during inode eviction, we need to
load block bitmaps and run transaction to modify them. This takes time
and also requires GFP_NOFAIL allocations. Mark inodes with preallocated
blocks as needing offloading of inode reclaim to a workqueue so that we
don't block reclaim for long and potentially deadlock MM subsystem.
We do the marking in ext4_mb_regular_allocator() to reliably catch all
the cases adding preallocated blocks to the inode and additionally when
adding delay allocated blocks (which will very likely result in
preallocated blocks in the future) so that deferred reclaim throttling
has a chance of throttling tasks generating the work.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/ext4/inode.c | 9 ++++++++-
fs/ext4/mballoc.c | 7 +++++++
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index e3129353d4ed..efc2f8ebe7ba 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2113,8 +2113,15 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
map->m_flags |= EXT4_MAP_DELAYED | EXT4_MAP_NEW;
retval = ext4_insert_delayed_blocks(inode, map->m_lblk, map->m_len);
- if (!retval)
+ if (!retval) {
map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
+ /*
+ * Inode will likely have preallocations which are hard to
+ * reclaim. Mark is for deferred reclaim early so that deferred
+ * reclaim throttling hits the task causing the work.
+ */
+ mark_inode_reclaim_deferred(inode);
+ }
up_write(&EXT4_I(inode)->i_data_sem);
return retval;
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index a15285224cdf..d138c4b697b3 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -3113,6 +3113,13 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
if (ac->ac_prefetch_nr)
ext4_mb_prefetch_fini(sb, ac->ac_prefetch_grp, ac->ac_prefetch_nr);
+ /*
+ * Freeing preallocations requires loading bitmaps and running
+ * transactions. Defer inode reclaim to a workqueue.
+ */
+ if (!RB_EMPTY_ROOT(&EXT4_I(ac->ac_inode)->i_prealloc_node))
+ mark_inode_reclaim_deferred(ac->ac_inode);
+
return err;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread