FAILED: patch "[PATCH] fs: rotate inodes whose page cache could not be dropped" failed to apply to 6.18-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x 8ee09f597763acfd31c62a578ba88b792fc41b24
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 8ee09f597763acfd31c62a578ba88b792fc41b24 Mon Sep 17 00:00:00 2001
From: Nikolaos Karaolidis <[email protected]>
Date: Fri, 16 Oct 2026 10:25:15 +0000
Subject: [PATCH] fs: rotate inodes whose page cache could not be dropped
When inode_lru_isolate() drops the page cache of an unused inode, it has to unlock the list, so it returns LRU_RETRY and the walk restarts at the head of the list, where the inode still is. If invalidate_mapping_pages() could not empty the mapping, the restarted walk strips the same inode again, and so on until nr_to_walk runs out. Every walk of that list then ends on this inode, and nothing behind it is reclaimed for as long as its cache stays.
Before commit 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context") the walker pinned the inode with a reference, and dropping it in iput_final() set I_REFERENCED, so the restarted walk rotated the inode to the tail. Set I_REFERENCED again when the mapping is still populated after the invalidation, in the same i_lock section that clears I_LRU_ISOLATING. inode_unpin_lru_isolating() now expects i_lock to be held, like inode_pin_lru_isolating().
On CONFIG_HIGHMEM kernels this can happen to any unused inode: a folio is left behind if it is locked, under writeback, spliced into a pipe, or still in another CPU's LRU-add batch after being read. Without HIGHMEM it happens to DAX inodes whose only entry is at index 0, which mapping_shrinkable() lets onto the LRU and invalidate_mapping_pages() never removes.
--- fs/inode.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
fs/inode.c +7 -2
@@ -593,12 +593,11 @@ static void inode_pin_lru_isolating(struct inode *inode)static void inode_unpin_lru_isolating (struct inode *inode) { -spin_lock (&inode->i_lock); +lockdep_assert_held (&inode->i_lock);WARN_ON (!(inode_state_read (inode) &I_LRU_ISOLATING ));inode_state_clear (inode,I_LRU_ISOLATING );/* Called with inode->i_lock which ensures memory ordering. */ inode_wake_up_bit (inode, __I_LRU_ISOLATING); -spin_unlock (&inode->i_lock); }static void inode_wait_for_lru_isolating (struct inode *inode)
@@ -981,7 +980,13 @@ static enum lru_status inode_lru_isolate(struct list_head *item,else __count_vm_events (PGINODESTEAL , reap);mm_account_reclaimed_pages (reap); + +spin_lock (&inode->i_lock); +/* The walk restarts here: don't retry leftovers right away */ +if (!mapping_empty (&inode->i_data)) +inode_state_set (inode,I_REFERENCED );inode_unpin_lru_isolating (inode); +spin_unlock (&inode->i_lock);return LRU_RETRY ; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/6 fs: assert on ->i_count in iput_final()
From: Mateusz Guzik <[email protected]>
[ Upstream commit be97a4b63ca8c3ed8f5c3f5606a15a8379c78afb ]
Notably make sure the count is 0 after the return from ->drop_inode(), provided we are going to drop.
Inspired by suspicious games played by f2fs.
Stable-dep-of: 8ee09f597763 ("fs: rotate inodes whose page cache could not be dropped")
--- fs/inode.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/inode.c +7 -0
@@ -1875,6 +1875,7 @@ static void iput_final(struct inode *inode)int drop;WARN_ON (inode->i_state &I_NEW ); +VFS_BUG_ON_INODE (atomic_read (&inode->i_count) !=0 , inode);if (op->drop_inode) drop = op->drop_inode (inode);
@@ -1889,6 +1890,12 @@ static void iput_final(struct inode *inode)return ; } +/* +* Re-check ->i_count in case the ->drop_inode() hooks played games. +* Note we only execute this if the verdict was to drop the inode. +*/ +VFS_BUG_ON_INODE (atomic_read (&inode->i_count) !=0 , inode); + state = inode->i_state;if (!drop) {WRITE_ONCE (inode->i_state, state |I_WILL_FREE );
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/6 fs: add missing fences to I_NEW handling
From: Mateusz Guzik <[email protected]>
[ Upstream commit 31e332b911fca54df467d264d7e2a2ef9317f3ca ]
Suppose there are 2 CPUs racing inode hash lookup func (say ilookup5()) and unlock_new_inode().
In principle the latter can clear the I_NEW flag before prior stores into the inode were made visible.
The former can in turn observe I_NEW is cleared and proceed to use the inode, while possibly reading from not-yet-published areas.
Stable-dep-of: 8ee09f597763 ("fs: rotate inodes whose page cache could not be dropped")
--- fs/dcache.c | 4 ++++ fs/inode.c | 8 ++++++++ include/linux/writeback.h | 4 ++++ 3 files changed, 16 insertions(+)
fs/dcache.c +4 -0
@@ -1995,6 +1995,10 @@ void d_instantiate_new(struct dentry *entry, struct inode *inode)spin_lock (&inode->i_lock);__d_instantiate (entry, inode);WARN_ON (!(inode->i_state &I_NEW )); +/* +* Pairs with smp_rmb in wait_on_inode(). +*/ +smp_wmb (); inode->i_state &= ~I_NEW & ~I_CREATING ;/* * Pairs with the barrier in prepare_to_wait_event() to make sure
fs/inode.c +8 -0
@@ -1177,6 +1177,10 @@ void unlock_new_inode(struct inode *inode)lockdep_annotate_inode_mutex_key (inode);spin_lock (&inode->i_lock);WARN_ON (!(inode->i_state &I_NEW )); +/* +* Pairs with smp_rmb in wait_on_inode(). +*/ +smp_wmb (); inode->i_state &= ~I_NEW & ~I_CREATING ;/* * Pairs with the barrier in prepare_to_wait_event() to make sure
@@ -1194,6 +1198,10 @@ void discard_new_inode(struct inode *inode)lockdep_annotate_inode_mutex_key (inode);spin_lock (&inode->i_lock);WARN_ON (!(inode->i_state &I_NEW )); +/* +* Pairs with smp_rmb in wait_on_inode(). +*/ +smp_wmb (); inode->i_state &= ~I_NEW ;/* * Pairs with the barrier in prepare_to_wait_event() to make sure
include/linux/writeback.h +4 -0
@@ -194,6 +194,10 @@ static inline void wait_on_inode(struct inode *inode) {wait_var_event (inode_state_wait_address (inode, __I_NEW), !(READ_ONCE (inode->i_state) &I_NEW )); +/* +* Pairs with routines clearing I_NEW. +*/ +smp_rmb (); }#ifdef CONFIG_CGROUP_WRITEBACK
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/6 fs: spell out fenced ->i_state accesses with explicit smp_wmb/smp_rmb
In reply to Sasha LevinFrom: Mateusz Guzik <[email protected]>
[ Upstream commit cb5db358ab5769cbd3e8e864f14af321126cccdb ]
The incomming helpers don't ship with _release/_acquire variants, for the time being anyway.
Stable-dep-of: 8ee09f597763 ("fs: rotate inodes whose page cache could not be dropped")
--- fs/fs-writeback.c | 5 +++-- include/linux/backing-dev.h | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
fs/fs-writeback.c +3 -2
@@ -476,10 +476,11 @@ static bool inode_do_switch_wbs(struct inode *inode, switched = true; skip_switch:/* -* Paired with load_acquire in unlocked_inode_to_wb_begin() and +* Paired with an acquire fence in unlocked_inode_to_wb_begin() and * ensures that the new wb is visible if they see !I_WB_SWITCH. */ -smp_store_release (&inode->i_state, inode->i_state & ~I_WB_SWITCH ); +smp_wmb (); + inode->i_state &= ~I_WB_SWITCH ;xa_unlock_irq (&mapping->i_pages);spin_unlock (&inode->i_lock);
include/linux/backing-dev.h +3 -2
@@ -277,10 +277,11 @@ unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)rcu_read_lock ();/* -* Paired with store_release in inode_switch_wbs_work_fn() and +* Paired with a release fence in inode_do_switch_wbs() and * ensures that we see the new wb if we see cleared I_WB_SWITCH. */ - cookie->locked =smp_load_acquire (&inode->i_state) &I_WB_SWITCH ; + cookie->locked = inode->i_state &I_WB_SWITCH ; +smp_rmb ();if (unlikely (cookie->locked))xa_lock_irqsave (&inode->i_mapping->i_pages, cookie->flags);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/6 fs: provide accessors for ->i_state
In reply to Sasha LevinFrom: Mateusz Guzik <[email protected]>
[ Upstream commit d8753f788ab4916341d9fab81795be9f2f49c264 ]
Open-coded accesses prevent asserting they are done correctly. One obvious aspect is locking, but significantly more can checked. For example it can be detected when the code is clearing flags which are already missing, or is setting flags when it is illegal (e.g., I_FREEING when ->i_count > 0).
In order to keep things manageable this patchset merely gets the thing off the ground with only lockdep checks baked in.
Current consumers can be trivially converted.
Suppose flags I_A and I_B are to be handled.
If ->i_lock is held, then:
state = inode->i_state => state = inode_state_read(inode)inode->i_state |= (I_A | I_B) => inode_state_set(inode, I_A | I_B)inode->i_state &= ~(I_A | I_B) => inode_state_clear(inode, I_A | I_B)inode->i_state = I_A | I_B => inode_state_assign(inode, I_A | I_B)
If ->i_lock is not held or only held conditionally:
state = inode->i_state => state = inode_state_read_once(inode)inode->i_state |= (I_A | I_B) => inode_state_set_raw(inode, I_A | I_B)inode->i_state &= ~(I_A | I_B) => inode_state_clear_raw(inode, I_A | I_B)inode->i_state = I_A | I_B => inode_state_assign_raw(inode, I_A | I_B)
The "_once" vs "_raw" discrepancy stems from the read variant differing by READ_ONCE as opposed to just lockdep checks.
Finally, if you want to atomically clear flags and set new ones, the following:
state = inode->i_state;
state &= ~I_A;
state |= I_B;inode->i_state = state;
turns into:
inode_state_replace(inode, I_A, I_B);
Stable-dep-of: 8ee09f597763 ("fs: rotate inodes whose page cache could not be dropped")
--- include/linux/fs.h | 78 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-)
include/linux/fs.h +76 -2
@@ -759,7 +759,7 @@ enum inode_state_bits {/* reserved wait address bit 3 */ }; -enum inode_state_flags_t { +enum inode_state_flags_enum {I_NEW = (1U << __I_NEW),I_SYNC = (1U << __I_SYNC),I_LRU_ISOLATING = (1U << __I_LRU_ISOLATING),
@@ -843,7 +843,7 @@ struct inode {#endif /* Misc */ -enum inode_state_flags_t i_state; +enum inode_state_flags_enum i_state;/* 32-bit hole */ struct rw_semaphore i_rwsem;
@@ -904,6 +904,80 @@ struct inode {void *i_private;/* fs or device private pointer */ } __randomize_layout; +/* +* i_state handling +* +* We hide all of it behind helpers so that we can validate consumers. +*/ +static inline enum inode_state_flags_enum inode_state_read_once (struct inode *inode) +{ +return READ_ONCE (inode->i_state); +} + +static inline enum inode_state_flags_enum inode_state_read (struct inode *inode) +{ +lockdep_assert_held (&inode->i_lock); +return inode->i_state; +} + +static inline void inode_state_set_raw (struct inode *inode, +enum inode_state_flags_enum flags) +{ +WRITE_ONCE (inode->i_state, inode->i_state | flags); +} + +static inline void inode_state_set (struct inode *inode, +enum inode_state_flags_enum flags) +{ +lockdep_assert_held (&inode->i_lock); +inode_state_set_raw (inode, flags); +} + +static inline void inode_state_clear_raw (struct inode *inode, +enum inode_state_flags_enum flags) +{ +WRITE_ONCE (inode->i_state, inode->i_state & ~flags); +} + +static inline void inode_state_clear (struct inode *inode, +enum inode_state_flags_enum flags) +{ +lockdep_assert_held (&inode->i_lock); +inode_state_clear_raw (inode, flags); +} + +static inline void inode_state_assign_raw (struct inode *inode, +enum inode_state_flags_enum flags) +{ +WRITE_ONCE (inode->i_state, flags); +} + +static inline void inode_state_assign (struct inode *inode, +enum inode_state_flags_enum flags) +{ +lockdep_assert_held (&inode->i_lock); +inode_state_assign_raw (inode, flags); +} + +static inline void inode_state_replace_raw (struct inode *inode, +enum inode_state_flags_enum clearflags, +enum inode_state_flags_enum setflags) +{ +enum inode_state_flags_enum flags; + flags = inode->i_state; + flags &= ~clearflags; + flags |= setflags; +inode_state_assign_raw (inode, flags); +} + +static inline void inode_state_replace (struct inode *inode, +enum inode_state_flags_enum clearflags, +enum inode_state_flags_enum setflags) +{ +lockdep_assert_held (&inode->i_lock); +inode_state_replace_raw (inode, clearflags, setflags); +} +static inline void inode_set_cached_link (struct inode *inode,char *link,int linklen) {VFS_WARN_ON_INODE (strlen (link) != linklen, inode);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/6 fs: Ignore inode metadata buffers in inode_lru_isolate()
In reply to Sasha LevinFrom: Jan Kara <[email protected]>
[ Upstream commit 972b9dd4e4180fbb2352bf2f0e015b7b63f5cca0 ]
There are only a few filesystems that use generic tracking of inode metadata buffer heads. As such the logic to reclaim tracked metadata buffer heads in inode_lru_isolate() doesn't bring a benefit big enough to justify intertwining of inode reclaim and metadata buffer head tracking. Just treat tracked metadata buffer heads as any other metadata filesystem has to properly clean up on inode eviction and stop handling it in inode_lru_isolate(). As a result filesystems using generic tracking of metadata buffer heads may now see dirty metadata buffers in their .evict methods more often which can slow down inode reclaim but given these filesystems aren't used in performance demanding setups we should be fine.
Stable-dep-of: 8ee09f597763 ("fs: rotate inodes whose page cache could not be dropped")
--- fs/buffer.c | 29 ----------------------------- fs/inode.c | 21 +++++++++------------ include/linux/buffer_head.h | 3 --- 3 files changed, 9 insertions(+), 44 deletions(-)
fs/buffer.c +0 -29
@@ -880,35 +880,6 @@ void invalidate_inode_buffers(struct inode *inode) }EXPORT_SYMBOL (invalidate_inode_buffers); -/* -* Remove any clean buffers from the inode's buffer list. This is called -* when we're trying to free the inode itself. Those buffers can pin it. -* -* Returns true if all buffers were removed. -*/ -int remove_inode_buffers (struct inode *inode) -{ -int ret =1 ; - -if (inode_has_buffers (inode)) { -struct address_space *mapping = &inode->i_data; -struct list_head *list = &mapping->i_private_list; -struct address_space *buffer_mapping = mapping->i_private_data; - -spin_lock (&buffer_mapping->i_private_lock); -while (!list_empty (list)) { -struct buffer_head *bh =BH_ENTRY (list->next); -if (buffer_dirty (bh)) { - ret =0 ; -break ; - } -__remove_assoc_queue (bh); - } -spin_unlock (&buffer_mapping->i_private_lock); - } -return ret; -} -/* * Create the appropriate buffers when given a folio for data area and * the size of each buffer.. Use the bh->b_this_page linked list to
fs/inode.c +9 -12
@@ -16,7 +16,6 @@#include <linux/fsnotify.h> #include <linux/mount.h> #include <linux/posix_acl.h> -#include <linux/buffer_head.h> /* for inode_has_buffers */ #include <linux/ratelimit.h> #include <linux/list_lru.h> #include <linux/iversion.h>
@@ -366,7 +365,6 @@ struct inode *alloc_inode(struct super_block *sb)void __destroy_inode (struct inode *inode) { -BUG_ON (inode_has_buffers (inode));inode_detach_wb (inode);security_inode_free (inode);fsnotify_inode_delete (inode);
@@ -954,19 +952,18 @@ static enum lru_status inode_lru_isolate(struct list_head *item,* page cache in order to free up struct inodes: lowmem might * be under pressure before the cache inside the highmem zone. */ -if (inode_has_buffers (inode) || !mapping_empty (&inode->i_data)) { +if (!mapping_empty (&inode->i_data)) { +unsigned long reap; +inode_pin_lru_isolating (inode);spin_unlock (&inode->i_lock);spin_unlock (&lru->lock); -if (remove_inode_buffers (inode)) { -unsigned long reap; - reap =invalidate_mapping_pages (&inode->i_data,0 ,-1 ); -if (current_is_kswapd ()) -__count_vm_events (KSWAPD_INODESTEAL , reap); -else -__count_vm_events (PGINODESTEAL , reap); -mm_account_reclaimed_pages (reap); - } + reap =invalidate_mapping_pages (&inode->i_data,0 ,-1 ); +if (current_is_kswapd ()) +__count_vm_events (KSWAPD_INODESTEAL , reap); +else +__count_vm_events (PGINODESTEAL , reap); +mm_account_reclaimed_pages (reap);inode_unpin_lru_isolating (inode);return LRU_RETRY ; }
include/linux/buffer_head.h +0 -3
@@ -517,7 +517,6 @@ void buffer_init(void);bool try_to_free_buffers (struct folio *folio);int inode_has_buffers (struct inode *inode);void invalidate_inode_buffers (struct inode *inode); -int remove_inode_buffers (struct inode *inode);int sync_mapping_buffers (struct address_space *mapping);void invalidate_bh_lrus (void );void invalidate_bh_lrus_cpu (void );
@@ -528,9 +527,7 @@ extern int buffer_heads_over_limit;static inline void buffer_init (void ) {}static inline bool try_to_free_buffers (struct folio *folio) {return true; } -static inline int inode_has_buffers (struct inode *inode) {return 0 ; }static inline void invalidate_inode_buffers (struct inode *inode) {} -static inline int remove_inode_buffers (struct inode *inode) {return 1 ; }static inline int sync_mapping_buffers (struct address_space *mapping) {return 0 ; }static inline void invalidate_bh_lrus (void ) {}static inline void invalidate_bh_lrus_cpu (void ) {}
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 6/6 fs: rotate inodes whose page cache could not be dropped
In reply to Sasha LevinFrom: Nikolaos Karaolidis <[email protected]>
[ Upstream commit 8ee09f597763acfd31c62a578ba88b792fc41b24 ]
When inode_lru_isolate() drops the page cache of an unused inode, it has to unlock the list, so it returns LRU_RETRY and the walk restarts at the head of the list, where the inode still is. If invalidate_mapping_pages() could not empty the mapping, the restarted walk strips the same inode again, and so on until nr_to_walk runs out. Every walk of that list then ends on this inode, and nothing behind it is reclaimed for as long as its cache stays.
Before commit 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context") the walker pinned the inode with a reference, and dropping it in iput_final() set I_REFERENCED, so the restarted walk rotated the inode to the tail. Set I_REFERENCED again when the mapping is still populated after the invalidation, in the same i_lock section that clears I_LRU_ISOLATING. inode_unpin_lru_isolating() now expects i_lock to be held, like inode_pin_lru_isolating().
On CONFIG_HIGHMEM kernels this can happen to any unused inode: a folio is left behind if it is locked, under writeback, spliced into a pipe, or still in another CPU's LRU-add batch after being read. Without HIGHMEM it happens to DAX inodes whose only entry is at index 0, which mapping_shrinkable() lets onto the LRU and invalidate_mapping_pages() never removes.
--- fs/inode.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
fs/inode.c +7 -2
@@ -581,12 +581,11 @@ static void inode_pin_lru_isolating(struct inode *inode)static void inode_unpin_lru_isolating (struct inode *inode) { -spin_lock (&inode->i_lock); +lockdep_assert_held (&inode->i_lock);WARN_ON (!(inode->i_state &I_LRU_ISOLATING )); inode->i_state &= ~I_LRU_ISOLATING ;/* Called with inode->i_lock which ensures memory ordering. */ inode_wake_up_bit (inode, __I_LRU_ISOLATING); -spin_unlock (&inode->i_lock); }static void inode_wait_for_lru_isolating (struct inode *inode)
@@ -964,7 +963,13 @@ static enum lru_status inode_lru_isolate(struct list_head *item,else __count_vm_events (PGINODESTEAL , reap);mm_account_reclaimed_pages (reap); + +spin_lock (&inode->i_lock); +/* The walk restarts here: don't retry leftovers right away */ +if (!mapping_empty (&inode->i_data)) +inode_state_set (inode,I_REFERENCED );inode_unpin_lru_isolating (inode); +spin_unlock (&inode->i_lock);return LRU_RETRY ; }
Signature · 1 line
2.53.0