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 v5 3/8] fs: Avoid inode dirtying on last iput
Date: Wed, 02 Dec 2026 14:36:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-245969@kernel.org> (raw)
In-Reply-To: <20261201143522.483168-11-jack@suse.cz>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] writeback: missed delayed wakeup for unused inodes on
  b_dirty_time
- [Medium] fs: queue_dirtytime_writeback() queues unhashed and evicted
  inodes
- [Medium] writeback: redirty_tail_locked() puts unused inodes at
  b_dirty_time head
- [Medium] writeback: clobbered dirtied_time_when persists across
  __mark_inode_dirty()
- [Medium] fs: lost lazytime timestamps in iput_final() when
  drop_inode() returns 1
--

commit 84464a49204ee2cc47ba9c4cc55f3f3c862e495d
Author: Jan Kara <jack@suse.cz>

fs: Avoid inode dirtying on last iput

This commit replaces the synchronous sync_lazytime() call in iput() with
queue_dirtytime_writeback(), moving unused inodes with dirty timestamps to
the head of wb->b_dirty_time with an expired dirtied_time_when timestamp so
that periodic writeback flushes them.

> diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
> index ec65ec6e48e7d..44f68a8c146f2 100644
> --- a/fs/fs-writeback.c
> +++ b/fs/fs-writeback.c
> @@ -1541,6 +1541,24 @@ void sb_clear_inode_writeback(struct inode *inode)
>  	}
>  }
>  
> +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)
>  {

[Severity: Medium]
When writeback skips an unused I_DIRTY_TIME inode and calls redirty_tail()
or redirty_tail_locked() (for example when super_trylock_shared() fails in
__writeback_inodes_wb() or when writeback_sb_inodes() filters for a
different superblock), does calling move_to_dirtytime_locked() ->
inode_add_dirtytime_head() put the skipped inode back at the oldest end of
wb->b_dirty_time (wb->b_dirty_time.prev) with an expired dirtied_when?

On the next iteration of wb_writeback(), queue_io() -> move_expired_inodes()
will immediately see the skipped inode as expired and move it back onto
wb->b_io, busy-retrying it instead of deferring it to the tail with
dirtied_when = jiffies.

[Severity: Medium]
Can clobbering inode->dirtied_time_when to jiffies -
dirtytime_expire_interval * HZ here (and in queue_dirtytime_writeback())
leave a permanently expired timestamp on the inode after I_DIRTY_TIME is
cleared?

Neither sync_lazytime() nor __mark_inode_dirty(inode, I_DIRTY_INODE) resets
dirtied_time_when, and __mark_inode_dirty(inode, I_DIRTY_TIME) only updates
dirtied_time_when when !was_dirty and !(i_state & I_SYNC_QUEUED):

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;
	...
}

If the cached inode is reused and has I_DIRTY_PAGES or I_SYNC_QUEUED set
when its timestamps are dirtied again, dirtied_time_when retains the
clobbered value from hours ago, causing __writeback_single_inode() to call
sync_lazytime() immediately on the next data writeback pass.

[ ... ]

> @@ -2986,6 +3002,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);
> +}

[Severity: Medium]
Can an unused inode placed at the head of wb->b_dirty_time miss periodic
writeback scheduling and stay off the LRU for up to
dirtytime_expire_interval?

If queue_dirtytime_writeback() hits the I_SYNC_QUEUED | I_SYNC | I_DIRTY
branch while __writeback_single_inode() has already passed its
dirtied_time_when check, writeback finishes with I_DIRTY_TIME still set and
calls move_to_dirtytime_locked().  For icount_read(inode) == 0,
move_to_dirtytime_locked() calls inode_add_dirtytime_head() and
wb_io_lists_depopulated() (which clears WB_has_dirty_io when b_dirty, b_io
and b_more_io are empty) without calling wb_wakeup_delayed(wb).

Similarly, even when queue_dirtytime_writeback() calls
wb_wakeup_delayed(wb), b_dirty_time does not set WB_has_dirty_io.  If
wb_workfn() runs before wb->last_old_flush +
msecs_to_jiffies(dirty_writeback_interval * 10), wb_check_old_data_flush()
returns 0 and wb_workfn() only re-arms wb->dwork if wb_has_dirty_io(wb) is
true, leaving wb->b_dirty_time unscheduled until wakeup_dirtytime_writeback()
runs hours later.

[Severity: Medium]
Can queue_dirtytime_writeback() place unhashed non-blockdev inodes onto
wb->b_dirty_time or clobber timestamps on inodes that are still in use or
about to be evicted?

__mark_inode_dirty() sets I_DIRTY_TIME in i_state before skipping dirty-list
insertion for (!S_ISBLK(inode->i_mode) && inode_unhashed(inode)).  Because
queue_dirtytime_writeback() does not check inode_unhashed(inode), and drops
inode->i_lock in locked_inode_to_wb_and_lock_list() without rechecking
icount_read(inode) == 1 or inode->i_nlink after re-acquiring i_lock, it will
link unhashed inodes onto wb->b_dirty_time and clobber timestamps even if
another thread grabbed a reference while i_lock was dropped.  It also queues
and wakes writeback for I_DONTCACHE or unhashed inodes that iput_final()
immediately evicts.

> 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 this lose pending lazytime timestamp updates when ->drop_inode() returns
1 on a linked inode?

Previously, iput() called sync_lazytime() before iput_final(), invoking
->dirty_inode(inode, I_DIRTY_SYNC | I_DIRTY_TIME).  Now I_DIRTY_TIME is only
queued on wb->b_dirty_time, and when op->drop_inode(inode) returns 1 in
iput_final(), the drop branch skips write_inode_now(inode, 1) and evicts the
inode directly:

fs/inode.c:iput_final() {
	...
	if (drop) {
		inode_state_set(inode, I_FREEING);
	} else {
		inode_state_set(inode, I_WILL_FREE);
		spin_unlock(&inode->i_lock);

		write_inode_now(inode, 1);
	...
}

For example, during unmount of a lazytime gfs2 filesystem, gfs2_kill_sb() ->
gfs2_evict_inodes() sets SDF_EVICTING so gfs2_drop_inode() returns 1 for all
linked inodes before kill_block_super() runs, and evict_linked_inode() skips
writing the inode because gfs2_dirty_inode() was never called to copy the
timestamps and set GLF_DIRTY.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201105733.18997-1-jack@suse.cz?part=3


  reply	other threads:[~2026-12-02 14:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:35 [PATCH v5 0/8] fs: Deferred inode reclaim Jan Kara
2026-12-01 14:35 ` [PATCH v5 1/8] writeback: Fix missed lazytime flush Jan Kara
2026-12-01 14:35 ` [PATCH v5 2/8] fs: Fix inode writeback list placement for switched and redirtied inodes Jan Kara
2026-12-02 14:36   ` sashiko-bot
2026-12-01 14:35 ` [PATCH v5 3/8] fs: Avoid inode dirtying on last iput Jan Kara
2026-12-02 14:36   ` sashiko-bot [this message]
2026-12-01 14:35 ` [PATCH v5 4/8] fs: Basic infrastructure for offloading inode reclaim Jan Kara
2026-12-02 14:36   ` sashiko-bot
2026-12-01 14:35 ` [PATCH v5 5/8] fs: Defer reclaim of unlinked inodes Jan Kara
2026-12-02 14:36   ` sashiko-bot
2026-12-01 14:35 ` [PATCH v5 6/8] fs: Defer reclaim of inodes requiring metadata writeback Jan Kara
2026-12-01 14:35 ` [PATCH v5 7/8] fs: Add throttling to deferred inode reclaim Jan Kara
2026-12-02 14:36   ` sashiko-bot
2026-12-01 14:35 ` [PATCH v5 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-245969@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.