From: Ye Bin <yebin@huaweicloud.com>
To: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, yebin10@huawei.com
Subject: [PATCH v2 1/3] vfs: introduce reclaim_icache_sb() and reclaim_dcache_sb() helper
Date: Mon, 17 Nov 2025 19:27:33 +0800 [thread overview]
Message-ID: <20251117112735.4170831-2-yebin@huaweicloud.com> (raw)
In-Reply-To: <20251117112735.4170831-1-yebin@huaweicloud.com>
From: Ye Bin <yebin10@huawei.com>
This patch is prepare for support drop_caches for specify file system.
reclaim_icache_sb()/reclaim_dcache_sb() helper walk the superblock
inode/dentry LRU for freeable inodes/dentrys and attempt to free them.
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
fs/dcache.c | 22 ++++++++++++++++++++++
fs/inode.c | 21 +++++++++++++++++++++
fs/internal.h | 1 +
include/linux/dcache.h | 1 +
4 files changed, 45 insertions(+)
diff --git a/fs/dcache.c b/fs/dcache.c
index de3e4e9777ea..d1b29b0f9062 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -1263,6 +1263,28 @@ void shrink_dcache_sb(struct super_block *sb)
}
EXPORT_SYMBOL(shrink_dcache_sb);
+/**
+ * reclaim_dcache_sb - reclaim dcache for a superblock
+ * @sb: superblock
+ *
+ * Reclaim the dcache for the specified super block. This is used to free
+ * the dcache via sysctl 'drop_fs_caches'.
+ */
+void reclaim_dcache_sb(struct super_block *sb)
+{
+ unsigned long count = list_lru_count(&sb->s_dentry_lru);
+
+ while (count > 0) {
+ LIST_HEAD(dispose);
+ unsigned long nr_to_walk = count >= 1024 ? 1024 : count;
+
+ count -= nr_to_walk;
+ list_lru_walk(&sb->s_dentry_lru, dentry_lru_isolate, &dispose,
+ nr_to_walk);
+ shrink_dentry_list(&dispose);
+ }
+}
+
/**
* enum d_walk_ret - action to talke during tree walk
* @D_WALK_CONTINUE: contrinue walk
diff --git a/fs/inode.c b/fs/inode.c
index 7901c2896d78..325de5a51955 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1073,6 +1073,27 @@ long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
return freed;
}
+/*
+ * Walk the superblock inode LRU for freeable inodes and attempt to free them.
+ * Inodes to be freed are moved to a temporary list and then are freed outside
+ * inode_lock by dispose_list(). This is used to free the icache via sysctl
+ * 'drop_fs_caches'.
+ */
+void reclaim_icache_sb(struct super_block *sb)
+{
+ unsigned long count = list_lru_count(&sb->s_inode_lru);
+
+ while (count > 0) {
+ LIST_HEAD(dispose);
+ unsigned long nr_to_walk = count >= 1024 ? 1024 : count;
+
+ count -= nr_to_walk;
+ list_lru_walk(&sb->s_inode_lru, inode_lru_isolate, &dispose,
+ nr_to_walk);
+ dispose_list(&dispose);
+ }
+}
+
static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked);
/*
* Called with the inode lock held.
diff --git a/fs/internal.h b/fs/internal.h
index 9b2b4d116880..8d3101232fb4 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -204,6 +204,7 @@ extern int vfs_open(const struct path *, struct file *);
* inode.c
*/
extern long prune_icache_sb(struct super_block *sb, struct shrink_control *sc);
+extern void reclaim_icache_sb(struct super_block *sb);
int dentry_needs_remove_privs(struct mnt_idmap *, struct dentry *dentry);
bool in_group_or_capable(struct mnt_idmap *idmap,
const struct inode *inode, vfsgid_t vfsgid);
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index c83e02b94389..fed46f694f54 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -257,6 +257,7 @@ extern struct dentry *d_find_any_alias(struct inode *inode);
extern struct dentry * d_obtain_alias(struct inode *);
extern struct dentry * d_obtain_root(struct inode *);
extern void shrink_dcache_sb(struct super_block *);
+extern void reclaim_dcache_sb(struct super_block *sb);
extern void shrink_dcache_parent(struct dentry *);
extern void d_invalidate(struct dentry *);
--
2.34.1
next prev parent reply other threads:[~2025-11-17 11:27 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 11:27 [PATCH v2 0/3] add support for drop_caches for individual filesystem Ye Bin
2025-11-17 11:27 ` Ye Bin [this message]
2025-11-17 11:27 ` [PATCH v2 2/3] sysctl: " Ye Bin
2025-11-17 13:57 ` kernel test robot
2025-11-17 14:07 ` kernel test robot
2025-11-17 11:27 ` [PATCH v2 3/3] Documentation: add instructions for using 'drop_fs_caches sysctl' sysctl Ye Bin
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=20251117112735.4170831-2-yebin@huaweicloud.com \
--to=yebin@huaweicloud.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=yebin10@huawei.com \
/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;
as well as URLs for NNTP newsgroup(s).