* [PATCH v2 1/3] vfs: introduce reclaim_icache_sb() and reclaim_dcache_sb() helper
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
2025-11-17 11:27 ` [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem Ye Bin
2025-11-17 11:27 ` [PATCH v2 3/3] Documentation: add instructions for using 'drop_fs_caches sysctl' sysctl Ye Bin
2 siblings, 0 replies; 6+ messages in thread
From: Ye Bin @ 2025-11-17 11:27 UTC (permalink / raw)
To: viro, brauner, jack, linux-fsdevel; +Cc: linux-kernel, yebin10
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
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem
2025-11-17 11:27 [PATCH v2 0/3] add support for drop_caches for individual filesystem Ye Bin
2025-11-17 11:27 ` [PATCH v2 1/3] vfs: introduce reclaim_icache_sb() and reclaim_dcache_sb() helper Ye Bin
@ 2025-11-17 11:27 ` 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
2 siblings, 2 replies; 6+ messages in thread
From: Ye Bin @ 2025-11-17 11:27 UTC (permalink / raw)
To: viro, brauner, jack, linux-fsdevel; +Cc: linux-kernel, yebin10
From: Ye Bin <yebin10@huawei.com>
In order to better analyze the issue of file system uninstallation caused
by kernel module opening files, it is necessary to perform dentry recycling
on a single file system. But now, apart from global dentry recycling, it is
not supported to do dentry recycling on a single file system separately.
This feature has usage scenarios in problem localization scenarios.At the
same time, it also provides users with a slightly fine-grained
pagecache/entry recycling mechanism.
This patch supports the recycling of pagecache/entry for individual file
systems.
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
fs/drop_caches.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 127 insertions(+)
diff --git a/fs/drop_caches.c b/fs/drop_caches.c
index 49f56a598ecb..3c7e624129ec 100644
--- a/fs/drop_caches.c
+++ b/fs/drop_caches.c
@@ -11,6 +11,10 @@
#include <linux/sysctl.h>
#include <linux/gfp.h>
#include <linux/swap.h>
+#include <linux/ptrace.h>
+#include <asm/syscall.h>
+#include <linux/task_work.h>
+#include <linux/namei.h>
#include "internal.h"
/* A global variable is a bit ugly, but it keeps the code simple */
@@ -78,6 +82,124 @@ static int drop_caches_sysctl_handler(const struct ctl_table *table, int write,
return 0;
}
+struct drop_fs_caches_work {
+ struct callback_head task_work;
+ dev_t dev;
+ char *path;
+ unsigned int ctl;
+};
+
+static void drop_fs_caches(struct callback_head *twork)
+{
+ int ret;
+ struct super_block *sb;
+ static bool suppress;
+ struct drop_fs_caches_work *work = container_of(twork,
+ struct drop_fs_caches_work, task_work);
+ unsigned int ctl = work->ctl;
+ dev_t dev = work->dev;
+
+ if (work->path) {
+ struct path path;
+
+ ret = kern_path(work->path, LOOKUP_FOLLOW, &path);
+ if (ret) {
+ syscall_set_return_value(current,
+ current_pt_regs(),
+ 0, ret);
+ goto out;
+ }
+ dev = path.dentry->d_sb->s_dev;
+ /* Make this file's dentry and inode recyclable */
+ path_put(&path);
+ }
+
+ sb = user_get_super(dev, false);
+ if (!sb) {
+ syscall_set_return_value(current, current_pt_regs(), 0,
+ -EINVAL);
+ goto out;
+ }
+
+ if (ctl & BIT(0)) {
+ lru_add_drain_all();
+ drop_pagecache_sb(sb, NULL);
+ count_vm_event(DROP_PAGECACHE);
+ }
+
+ if (ctl & BIT(1)) {
+ reclaim_dcache_sb(sb);
+ reclaim_icache_sb(sb);
+ count_vm_event(DROP_SLAB);
+ }
+
+ if (!READ_ONCE(suppress)) {
+ pr_info("%s (%d): %s: %d %u:%u\n", current->comm,
+ task_pid_nr(current), __func__, ctl,
+ MAJOR(sb->s_dev), MINOR(sb->s_dev));
+
+ if (ctl & BIT(2))
+ WRITE_ONCE(suppress, true);
+ }
+
+ drop_super(sb);
+out:
+ kfree(work->path);
+ kfree(work);
+}
+
+static int drop_fs_caches_sysctl_handler(const struct ctl_table *table,
+ int write, void *buffer,
+ size_t *length, loff_t *ppos)
+{
+ struct drop_fs_caches_work *work = NULL;
+ unsigned int major, minor;
+ unsigned int ctl;
+ int ret;
+ char *path = NULL;
+
+ if (!write)
+ return 0;
+
+ if (sscanf(buffer, "%u %u:%u", &ctl, &major, &minor) != 3) {
+ path = kstrdup(buffer, GFP_NOFS);
+ if (!path) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ if (sscanf(buffer, "%u %s", &ctl, path) != 2) {
+ ret = -EINVAL;
+ goto out;
+ }
+ }
+
+ if (ctl < 1 || ctl > 7) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ work = kzalloc(sizeof(*work), GFP_KERNEL);
+ if (!work) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ init_task_work(&work->task_work, drop_fs_caches);
+ if (!path)
+ work->dev = MKDEV(major, minor);
+ work->path = path;
+ work->ctl = ctl;
+ ret = task_work_add(current, &work->task_work, TWA_RESUME);
+out:
+ if (ret) {
+ kfree(path);
+ kfree(work);
+ }
+
+ return ret;
+}
+
static const struct ctl_table drop_caches_table[] = {
{
.procname = "drop_caches",
@@ -88,6 +210,11 @@ static const struct ctl_table drop_caches_table[] = {
.extra1 = SYSCTL_ONE,
.extra2 = SYSCTL_FOUR,
},
+ {
+ .procname = "drop_fs_caches",
+ .mode = 0200,
+ .proc_handler = drop_fs_caches_sysctl_handler,
+ },
};
static int __init init_vm_drop_caches_sysctls(void)
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v2 3/3] Documentation: add instructions for using 'drop_fs_caches sysctl' sysctl
2025-11-17 11:27 [PATCH v2 0/3] add support for drop_caches for individual filesystem Ye Bin
2025-11-17 11:27 ` [PATCH v2 1/3] vfs: introduce reclaim_icache_sb() and reclaim_dcache_sb() helper Ye Bin
2025-11-17 11:27 ` [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem Ye Bin
@ 2025-11-17 11:27 ` Ye Bin
2 siblings, 0 replies; 6+ messages in thread
From: Ye Bin @ 2025-11-17 11:27 UTC (permalink / raw)
To: viro, brauner, jack, linux-fsdevel; +Cc: linux-kernel, yebin10
From: Ye Bin <yebin10@huawei.com>
Add instructions for 'drop_fs_caches sysctl' sysctl in 'vm.rst'.
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
Documentation/admin-guide/sysctl/vm.rst | 34 +++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst
index ace73480eb9d..c6c29c8cf92e 100644
--- a/Documentation/admin-guide/sysctl/vm.rst
+++ b/Documentation/admin-guide/sysctl/vm.rst
@@ -37,6 +37,7 @@ Currently, these files are in /proc/sys/vm:
- dirtytime_expire_seconds
- dirty_writeback_centisecs
- drop_caches
+- drop_fs_caches
- enable_soft_offline
- extfrag_threshold
- highmem_is_dirtyable
@@ -284,6 +285,39 @@ used::
These are informational only. They do not mean that anything is wrong
with your system. To disable them, echo 4 (bit 2) into drop_caches.
+drop_fs_caches
+==============
+
+Writing to this will cause the kernel to drop clean for a specific file system
+caches, as well as reclaimable slab objects like dentries and inodes. Once
+dropped, their memory becomes free. Except for specifying the device number
+or file path for a specific file system, everything else is consistent with
+drop_caches. The device number can be viewed through "cat /proc/self/montinfo"
+or 'lsblk'.
+
+To free pagecache::
+
+ echo "1 MAJOR:MINOR" > /proc/sys/vm/drop_fs_caches
+Or
+ echo "1 /mnt/XX" > /proc/sys/vm/drop_fs_caches
+
+To free reclaimable slab objects (includes dentries and inodes)::
+
+ echo "2 MAJOR:MINOR" > /proc/sys/vm/drop_fs_caches
+Or
+ echo "2 /mnt/XX" > /proc/sys/vm/drop_fs_caches
+
+To free slab objects and pagecache::
+
+ echo "3 MAJOR:MINOR" > /proc/sys/vm/drop_fs_caches
+Or
+ echo "3 /mnt/XX" > /proc/sys/vm/drop_fs_caches
+
+You may see informational messages in your kernel log when this file is
+used::
+
+ echo (1234): drop_fs_caches: 3 MAJOR:MINOR
+
enable_soft_offline
===================
Correctable memory errors are very common on servers. Soft-offline is kernel's
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread