[PATCH v4 06/11] fs/inode: convert evict_inodes() to sb_inode_iter

Huang Shijie huangsj at hygon.cn
Wed Nov 25 09:11:52 UTC 2026


Use sb_inode_iter with %INO_ITER_F_NOREF | %INO_ITER_F_NOIGET, which
hands out only zero-count inodes and takes no reference on them.

Signed-off-by: Huang Shijie <huangsj at hygon.cn>
---
 fs/inode.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index f63535ee3629..0830a11a37e9 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -865,6 +865,13 @@ static void dispose_list(struct list_head *head)
 	}
 }
 
+static bool evict_inode_eligible(struct inode *inode, void *data)
+{
+	inode_state_set(inode, I_FREEING);
+	inode_lru_list_del(inode);
+	return true;
+}
+
 /**
  * evict_inodes	- evict all evictable inodes for a superblock
  * @sb:		superblock to operate on
@@ -879,40 +886,20 @@ void evict_inodes(struct super_block *sb)
 	struct inode *inode;
 	LIST_HEAD(dispose);
 
-	spin_lock(&sb->s_inode_list_lock);
-	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
-		if (icount_read_once(inode))
-			continue;
-
-		spin_lock(&inode->i_lock);
-		if (icount_read(inode)) {
-			spin_unlock(&inode->i_lock);
-			continue;
-		}
-		if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE)) {
-			spin_unlock(&inode->i_lock);
-			continue;
-		}
-
-		inode_state_set(inode, I_FREEING);
-		inode_lru_list_del(inode);
-		spin_unlock(&inode->i_lock);
-
+	CLASS(sb_inode_iter, iter)(sb, evict_inode_eligible, &dispose,
+				   INO_ITER_F_NOREF | INO_ITER_F_NOIGET);
+	while ((inode = sb_inode_iter_next(&iter))) {
 		/*
 		 * Keep this inode out of dispose so it stays on s_inodes while
 		 * the list lock is dropped. I_FREEING prevents new references
 		 * and leaves eviction to us, so we can resume the walk from it.
 		 */
 		if (need_resched()) {
-			spin_unlock(&sb->s_inode_list_lock);
 			cond_resched();
 			dispose_list(&dispose);
-			spin_lock(&sb->s_inode_list_lock);
 		}
 		list_add(&inode->i_lru, &dispose);
 	}
-	spin_unlock(&sb->s_inode_list_lock);
-
 	dispose_list(&dispose);
 }
 EXPORT_SYMBOL_GPL(evict_inodes);
-- 
2.53.0





More information about the Linux-security-module-archive mailing list