Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jan Kara" <jack@suse.cz>, "Christian Brauner" <brauner@kernel.org>
Cc: "Christoph Hellwig" <hch@infradead.org>,
	linux-mm@kvack.org, "Mikhail Rudenko" <xyzzy@yandex-team.ru>,
	"Andreas Gruenbacher" <agruenba@redhat.com>
Subject: Re: [PATCH v2 7/8] fs: Protect deferred inode reclaim from freezing
Date: Wed, 25 Nov 2026 12:00:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-233316@kernel.org> (raw)
In-Reply-To: <20261124115850.3512-15-jack@suse.cz>

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


  reply	other threads:[~2026-11-25 12:00 UTC|newest]

Thread overview: 14+ 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-25 12:00   ` sashiko-bot
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
2026-11-24 11:58 ` [PATCH v2 4/8] fs: Defer reclaim of unlinked inodes 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
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
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 [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=sashiko-outbox-233316@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agruenba@redhat.com \
    --cc=brauner@kernel.org \
    --cc=hch@infradead.org \
    --cc=jack@suse.cz \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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