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 7/8] fs: Protect deferred inode reclaim from freezing
Date: Tue, 24 Nov 2026 12:58:51 +0100 [thread overview]
Message-ID: <20261124115850.3512-15-jack@suse.cz> (raw)
In-Reply-To: <20261124113450.3451-1-jack@suse.cz>
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
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 ` [PATCH v2 6/8] fs: Add throttling to deferred inode reclaim Jan Kara
2026-11-24 11:58 ` Jan Kara [this message]
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-15-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