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 2/8] fs: Avoid inode dirtying on last iput
Date: Tue, 24 Nov 2026 12:58:46 +0100 [thread overview]
Message-ID: <20261124115850.3512-10-jack@suse.cz> (raw)
In-Reply-To: <20261124113450.3451-1-jack@suse.cz>
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
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 ` Jan Kara [this message]
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 ` [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
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-10-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