All of 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 2/8] fs: Avoid inode dirtying on last iput
Date: Wed, 25 Nov 2026 12:00:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-233311@kernel.org> (raw)
In-Reply-To: <20261124115850.3512-10-jack@suse.cz>

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


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

Thread overview: 20+ 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 [this message]
2026-11-27 17:08     ` Jan Kara
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-27 17:16     ` Jan Kara
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-27 17:19     ` 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-25 12:00   ` sashiko-bot
2026-11-27 17:44     ` Jan Kara
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
2026-11-27 20:03     ` Jan Kara
2026-12-01 10:05       ` Jan Kara
2026-11-24 11:58 ` [PATCH v2 8/8] ext4: Defer inode reclaim if it has preallocations Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233311@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.