[PATCH v4 08/11] fs/drop_caches: convert drop_pagecache_sb() to sb_inode_iter

From: Huang Shijie

Date: Wed Nov 25 2026 - 04:12:58 EST


Convert drop_pagecache_sb() to sb_inode_iter.

No functional change.

Signed-off-by: Huang Shijie <huangsj@xxxxxxxx>
---
fs/drop_caches.c | 36 ++++++++++++------------------------
1 file changed, 12 insertions(+), 24 deletions(-)

diff --git a/fs/drop_caches.c b/fs/drop_caches.c
index 28c5fb9c7357..bfe9b8cbb45c 100644
--- a/fs/drop_caches.c
+++ b/fs/drop_caches.c
@@ -17,39 +17,27 @@
/* A global variable is a bit ugly, but it keeps the code simple */
static int sysctl_drop_caches;

+static bool invalidate_inode_eligible(struct inode *inode, void *priv)
+{
+ /*
+ * Skip inodes without pages, unless we need to reschedule: then take
+ * one anyway so that a long scan of empty inodes cannot stall.
+ */
+ return !mapping_empty(inode->i_mapping) || need_resched();
+}
+
static void drop_pagecache_sb(struct super_block *sb, void *unused)
{
- struct inode *inode, *toput_inode = NULL;
+ struct inode *inode;

if (sb->s_bdi == &noop_backing_dev_info)
return;

- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- /*
- * We must skip inodes in unusual state. We may also skip
- * inodes without pages but we deliberately won't in case
- * we need to reschedule to avoid softlockups.
- */
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) ||
- (mapping_empty(inode->i_mapping) && !need_resched())) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
+ CLASS(sb_inode_iter, iter)(sb, invalidate_inode_eligible, NULL, 0);
+ while ((inode = sb_inode_iter_next(&iter))) {
invalidate_mapping_pages(inode->i_mapping, 0, -1);
- iput(toput_inode);
- toput_inode = inode;
-
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
}
- spin_unlock(&sb->s_inode_list_lock);
- iput(toput_inode);
}

static int drop_caches_sysctl_handler(const struct ctl_table *table, int write,
--
2.53.0