Skip to contentSkip to conversation

fs: don't lose inodes that another LRU walker is isolating

PATCH Applied Nikolaos Karaolidis 4 participants 4 messages Oct 9 – Oct 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: don't lose inodes that another LRU walker is isolating

When inode_lru_isolate() finds an unused inode that still has page cache, it sets I_LRU_ISOLATING, drops i_lock and the list_lru lock, invalidates the page cache, clears the flag again and returns LRU_RETRY so that the restarted walk can free the inode.

Another walker of the same list can reach the inode in that window. It sees a non-zero i_state and takes the inode off the LRU, as it would an inode that is in use. Page cache deletion requeues the inode once the mapping is shrinkable, so this is harmless if it happens before the last deletion. If it happens after, nothing puts the inode
back: it stays in memory, unused, clean and without page cache, out of reach of the shrinker and of drop_caches, until it is looked up again or the filesystem is unmounted.

Before commit 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context") the pin was an i_count reference, and the final iput() put the inode back on the LRU.

Skip inodes that are being isolated and leave them to the walker that pinned them, which restarts its walk once it has unpinned them.

The window is reached by any unused inode with page cache on CONFIG_HIGHMEM kernels, and otherwise by inodes whose page cache is a single shadow entry at index 0. On an i386 HIGHMEM guest, eight concurrent "echo 2 > /proc/sys/vm/drop_caches" over 2000 freshly read one-page ext4 files leave roughly 400 of the inodes off the LRU per round; on x86_64, with the files' pages first evicted through memory.reclaim, 3 to 4 per round. With this patch, none in either case.

Fixes 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context")
Assisted-by LLM

---

Found while working on reclaim of inodes pinned by page cache in ZONE_MOVABLE.

Tested on master af32da41b032 (v7.3-rc6-232) in QEMU/KVM with a small stress program: mount an ext4 image, read 2000 one-page files, run 8 concurrent "echo 2 > /proc/sys/vm/drop_caches", drop caches again single-threaded (2, then 1, then 2), and count the filesystem's inodes still in memory via how many the umount frees:

i386 HIGHMEM: ~390 of 2000 lost per round without the patch
(3 runs x 20 rounds), 0 with it
x86_64, pages evicted through memory.reclaim first (single shadow
entry left): ~4 per round without, 0 with
x86_64 with KASAN, PROVE_LOCKING, DEBUG_VM, DEBUG_LIST:
6109 lost in 50 rounds without, 0 with, no splats

The reproducer is a single C file run as init; I can post it if useful.

I went with skipping the inode rather than requeueing it on unpin (which would be closer to what the final iput() did before 2a0629834cd8) because it avoids the remove/re-add and leaves the inode where the pinning walker finds it again. Happy to switch if preferred.

Jan, this applies cleanly before or after "fs: Basic infrastructure for offloading inode reclaim" (v2 3/5); happy to rebase onto that series if you'd rather take it there.

For stable: 6.18 and older need inode->i_state instead of inode_state_read(). I have only tested master.

An LLM was used to check the analysis against the code, write the reproducer and draft the changelog; I have reviewed all of it.

 fs/inode.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/inode.c +6 -0
index a9d37be390a1..74dfc379b377 100644
@@ -942,6 +942,12 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
 	if (!spin_trylock(&inode->i_lock))
 		return LRU_SKIP;
 
+	/* Another walker is dropping its page cache, leave it to that one */
+	if (inode_state_read(inode) & I_LRU_ISOLATING) {
+		spin_unlock(&inode->i_lock);
+		return LRU_SKIP;
+	}
+
 	/*
 	 * Inodes can get referenced, redirtied, or repopulated while
 	 * they're already on the LRU, and this can make them

base-commit: af32da41b0327b9c6a37856ba82b6760d6c8d10e --
2.55.0

Reviewed-by on v1 1/1
#1 lore raw
Quoted 62 lines · Nikolaos Karaolidis

When inode_lru_isolate() finds an unused inode that still has page cache, it sets I_LRU_ISOLATING, drops i_lock and the list_lru lock, invalidates the page cache, clears the flag again and returns LRU_RETRY so that the restarted walk can free the inode.

Another walker of the same list can reach the inode in that window. It sees a non-zero i_state and takes the inode off the LRU, as it would an inode that is in use. Page cache deletion requeues the inode once the mapping is shrinkable, so this is harmless if it happens before the last deletion. If it happens after, nothing puts the inode
back: it stays in memory, unused, clean and without page cache, out of reach of the shrinker and of drop_caches, until it is looked up again or the filesystem is unmounted.

Before commit 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context") the pin was an i_count reference, and the final iput() put the inode back on the LRU.

Skip inodes that are being isolated and leave them to the walker that pinned them, which restarts its walk once it has unpinned them.

The window is reached by any unused inode with page cache on CONFIG_HIGHMEM kernels, and otherwise by inodes whose page cache is a single shadow entry at index 0. On an i386 HIGHMEM guest, eight concurrent "echo 2 > /proc/sys/vm/drop_caches" over 2000 freshly read one-page ext4 files leave roughly 400 of the inodes off the LRU per round; on x86_64, with the files' pages first evicted through memory.reclaim, 3 to 4 per round. With this patch, none in either case.

Fixes 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context")
Assisted-by LLM

---

Found while working on reclaim of inodes pinned by page cache in ZONE_MOVABLE.

Tested on master af32da41b032 (v7.3-rc6-232) in QEMU/KVM with a small stress program: mount an ext4 image, read 2000 one-page files, run 8 concurrent "echo 2 > /proc/sys/vm/drop_caches", drop caches again single-threaded (2, then 1, then 2), and count the filesystem's inodes still in memory via how many the umount frees:

i386 HIGHMEM: ~390 of 2000 lost per round without the patch
(3 runs x 20 rounds), 0 with it
x86_64, pages evicted through memory.reclaim first (single shadow
entry left): ~4 per round without, 0 with
x86_64 with KASAN, PROVE_LOCKING, DEBUG_VM, DEBUG_LIST:
6109 lost in 50 rounds without, 0 with, no splats

The reproducer is a single C file run as init; I can post it if useful.

I went with skipping the inode rather than requeueing it on unpin (which would be closer to what the final iput() did before 2a0629834cd8) because it avoids the remove/re-add and leaves the inode where the pinning walker finds it again. Happy to switch if preferred.

Jan, this applies cleanly before or after "fs: Basic infrastructure for offloading inode reclaim" (v2 3/5); happy to rebase onto that series if you'd rather take it there.

Quoted 9 lines

For stable: 6.18 and older need inode->i_state instead of inode_state_read(). I have only tested master.

An LLM was used to check the analysis against the code, write the reproducer and draft the changelog; I have reviewed all of it.

  fs/inode.c | 6 ++++++
  1 file changed, 6 insertions(+)

Good catch.

Quoted 23 lines
fs/inode.c +6 -0
index a9d37be390a1..74dfc379b377 100644
@@ -942,6 +942,12 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
  	if (!spin_trylock(&inode->i_lock))
  		return LRU_SKIP;
 
+	/* Another walker is dropping its page cache, leave it to that one */
+	if (inode_state_read(inode) & I_LRU_ISOLATING) {
+		spin_unlock(&inode->i_lock);
+		return LRU_SKIP;
+	}
+
  	/*
  	 * Inodes can get referenced, redirtied, or repopulated while
  	 * they're already on the LRU, and this can make them

base-commit: af32da41b0327b9c6a37856ba82b6760d6c8d10e --
2.55.0
.

reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Nikolaos Karaolidis
Quoted 29 lines · Nikolaos Karaolidis

When inode_lru_isolate() finds an unused inode that still has page cache, it sets I_LRU_ISOLATING, drops i_lock and the list_lru lock, invalidates the page cache, clears the flag again and returns LRU_RETRY so that the restarted walk can free the inode.

Another walker of the same list can reach the inode in that window. It sees a non-zero i_state and takes the inode off the LRU, as it would an inode that is in use. Page cache deletion requeues the inode once the mapping is shrinkable, so this is harmless if it happens before the last deletion. If it happens after, nothing puts the inode
back: it stays in memory, unused, clean and without page cache, out of reach of the shrinker and of drop_caches, until it is looked up again or the filesystem is unmounted.

Before commit 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context") the pin was an i_count reference, and the final iput() put the inode back on the LRU.

Skip inodes that are being isolated and leave them to the walker that pinned them, which restarts its walk once it has unpinned them.

The window is reached by any unused inode with page cache on CONFIG_HIGHMEM kernels, and otherwise by inodes whose page cache is a single shadow entry at index 0. On an i386 HIGHMEM guest, eight concurrent "echo 2 > /proc/sys/vm/drop_caches" over 2000 freshly read one-page ext4 files leave roughly 400 of the inodes off the LRU per round; on x86_64, with the files' pages first evicted through memory.reclaim, 3 to 4 per round. With this patch, none in either case.

Quoted 4 lines
Fixes 2a0629834cd8 ("vfs: Don't evict inode under the inode lru traversing context")
Assisted-by LLM

Looks good. Feel free to add:

Honza

Quoted 59 lines

---

Found while working on reclaim of inodes pinned by page cache in ZONE_MOVABLE.

Tested on master af32da41b032 (v7.3-rc6-232) in QEMU/KVM with a small stress program: mount an ext4 image, read 2000 one-page files, run 8 concurrent "echo 2 > /proc/sys/vm/drop_caches", drop caches again single-threaded (2, then 1, then 2), and count the filesystem's inodes still in memory via how many the umount frees:

i386 HIGHMEM: ~390 of 2000 lost per round without the patch
(3 runs x 20 rounds), 0 with it
x86_64, pages evicted through memory.reclaim first (single shadow
entry left): ~4 per round without, 0 with
x86_64 with KASAN, PROVE_LOCKING, DEBUG_VM, DEBUG_LIST:
6109 lost in 50 rounds without, 0 with, no splats

The reproducer is a single C file run as init; I can post it if useful.

I went with skipping the inode rather than requeueing it on unpin (which would be closer to what the final iput() did before 2a0629834cd8) because it avoids the remove/re-add and leaves the inode where the pinning walker finds it again. Happy to switch if preferred.

Jan, this applies cleanly before or after "fs: Basic infrastructure for offloading inode reclaim" (v2 3/5); happy to rebase onto that series if you'd rather take it there.

For stable: 6.18 and older need inode->i_state instead of inode_state_read(). I have only tested master.

An LLM was used to check the analysis against the code, write the reproducer and draft the changelog; I have reviewed all of it.

 fs/inode.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/inode.c +6 -0
index a9d37be390a1..74dfc379b377 100644
@@ -942,6 +942,12 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
 	if (!spin_trylock(&inode->i_lock))
 		return LRU_SKIP;
 
+	/* Another walker is dropping its page cache, leave it to that one */
+	if (inode_state_read(inode) & I_LRU_ISOLATING) {
+		spin_unlock(&inode->i_lock);
+		return LRU_SKIP;
+	}
+
 	/*
 	 * Inodes can get referenced, redirtied, or repopulated while
 	 * they're already on the LRU, and this can make them

base-commit: af32da41b0327b9c6a37856ba82b6760d6c8d10e --
2.55.0

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#3 lore raw
In reply to Nikolaos Karaolidis
Quoted 1 line · Nikolaos Karaolidis

fs: don't lose inodes that another LRU walker is isolating

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] fs: don't lose inodes that another LRU walker is isolating

      https://git.kernel.org/vfs/vfs/c/3f54961ad92a