add support for drop_caches for individual filesystem
Subsystems (3)
v2 cover add support for drop_caches for individual filesystem
From: Ye Bin <[email protected]>
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 patchset supports the recycling of pagecache/entry for individual file systems.
Diff v2 vs v1:
1. Fix possible live lock for shrink_icache_sb().
2. Introduce reclaim_dcache_sb() for reclaim dentry.
3. Fix potential deadlocks as follows:
https://lore.kernel.org/linux-fsdevel/[email protected]/
After some consideration, it was decided that this feature would primarily be used for debugging purposes. Instead of adding a new IOCTL command, the task_work mechanism was employed to address potential deadlock issues.
Ye Bin (3):
vfs: introduce reclaim_icache_sb() and reclaim_dcache_sb() helper
sysctl: add support for drop_caches for individual filesystem
Documentation: add instructions for using 'drop_fs_caches sysctl'
sysctl
Documentation/admin-guide/sysctl/vm.rst | 34 +++++++ fs/dcache.c | 22 ++++ fs/drop_caches.c | 127 ++++++++++++++++++++++++ fs/inode.c | 21 ++++ fs/internal.h | 1 + include/linux/dcache.h | 1 + 6 files changed, 206 insertions(+)
Signature · 1 line
2.34.1
PATCH v2 1/3 vfs: introduce reclaim_icache_sb() and reclaim_dcache_sb() helper
From: Ye Bin <[email protected]>
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.
--- fs/dcache.c | 22 ++++++++++++++++++++++ fs/inode.c | 21 +++++++++++++++++++++ fs/internal.h | 1 + include/linux/dcache.h | 1 + 4 files changed, 45 insertions(+)
fs/dcache.c +22 -0
@@ -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
fs/inode.c +21 -0
@@ -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.
fs/internal.h +1 -0
@@ -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);
include/linux/dcache.h +1 -0
@@ -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 *);
Signature · 1 line
2.34.1
PATCH v2 2/3 sysctl: add support for drop_caches for individual filesystem
In reply to Ye BinFrom: Ye Bin <[email protected]>
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.
--- fs/drop_caches.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
fs/drop_caches.c +127 -0
@@ -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 )
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem
kernel test robot noticed the following build errors:
Hi Ye,
kernel test robot noticed the following build errors:
[auto build test ERROR on viro-vfs/for-next]
[also build test ERROR on linus/master brauner-vfs/vfs.all v6.18-rc6 next-20251117] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Ye-Bin/vfs-introduce-reclaim_icache_sb-and-reclaim_dcache_sb-helper/20251117-193502 base: https://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git for-next patch link: https://lore.kernel.org/r/20251117112735.4170831-3-yebin%40huaweicloud.com patch subject: [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20251117/[email protected]/config) compiler: alpha-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251117/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
fs/drop_caches.c: In function 'drop_fs_caches':
>> fs/drop_caches.c:107:25: error: implicit declaration of function 'syscall_set_return_value'; did you mean 'syscall_get_return_value'? [-Wimplicit-function-declaration]
107 | syscall_set_return_value(current,
| ^~~~~~~~~~~~~~~~~~~~~~~~
| syscall_get_return_valuevim +107 fs/drop_caches.c
91
92 static void drop_fs_caches(struct callback_head *twork)
93 {
94 int ret;
95 struct super_block *sb;
96 static bool suppress;
97 struct drop_fs_caches_work *work = container_of(twork,
98 struct drop_fs_caches_work, task_work);
99 unsigned int ctl = work->ctl;
100 dev_t dev = work->dev;
101
102 if (work->path) {
103 struct path path;
104
105 ret = kern_path(work->path, LOOKUP_FOLLOW, &path);
106 if (ret) {
> 107 syscall_set_return_value(current, 108 current_pt_regs(),
109 0, ret);
110 goto out;
111 }
112 dev = path.dentry->d_sb->s_dev;
113 /* Make this file's dentry and inode recyclable */
114 path_put(&path);
115 }
116
117 sb = user_get_super(dev, false);
118 if (!sb) {
119 syscall_set_return_value(current, current_pt_regs(), 0,
120 -EINVAL);
121 goto out;
122 }
123
124 if (ctl & BIT(0)) {
125 lru_add_drain_all();
126 drop_pagecache_sb(sb, NULL);
127 count_vm_event(DROP_PAGECACHE);
128 }
129
130 if (ctl & BIT(1)) {
131 reclaim_dcache_sb(sb);
132 reclaim_icache_sb(sb);
133 count_vm_event(DROP_SLAB);
134 }
135
136 if (!READ_ONCE(suppress)) {
137 pr_info("%s (%d): %s: %d %u:%u\n", current->comm,
138 task_pid_nr(current), __func__, ctl,
139 MAJOR(sb->s_dev), MINOR(sb->s_dev));
140
141 if (ctl & BIT(2))
142 WRITE_ONCE(suppress, true);
143 }
144
145 drop_super(sb);
146 out:
147 kfree(work->path);
148 kfree(work);
149 }
150
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem
In reply to Ye Binkernel test robot noticed the following build errors:
Hi Ye,
kernel test robot noticed the following build errors:
[auto build test ERROR on viro-vfs/for-next]
[also build test ERROR on linus/master brauner-vfs/vfs.all v6.18-rc6 next-20251117] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Ye-Bin/vfs-introduce-reclaim_icache_sb-and-reclaim_dcache_sb-helper/20251117-193502 base: https://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git for-next patch link: https://lore.kernel.org/r/20251117112735.4170831-3-yebin%40huaweicloud.com patch subject: [PATCH v2 2/3] sysctl: add support for drop_caches for individual filesystem config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20251117/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251117/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> fs/drop_caches.c:108:8: error: call to undeclared function 'task_stack_page'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
108 | current_pt_regs(),
| ^
include/linux/ptrace.h:389:27: note: expanded from macro 'current_pt_regs'
389 | #define current_pt_regs() task_pt_regs(current)
| ^
arch/x86/include/asm/processor.h:650:39: note: expanded from macro 'task_pt_regs'
650 | unsigned long __ptr = (unsigned long)task_stack_page(task); \
| ^
fs/drop_caches.c:119:37: error: call to undeclared function 'task_stack_page'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
119 | syscall_set_return_value(current, current_pt_regs(), 0,
| ^
include/linux/ptrace.h:389:27: note: expanded from macro 'current_pt_regs'
389 | #define current_pt_regs() task_pt_regs(current)
| ^
arch/x86/include/asm/processor.h:650:39: note: expanded from macro 'task_pt_regs'
650 | unsigned long __ptr = (unsigned long)task_stack_page(task); \
| ^2 errors generated.
vim +/task_stack_page +108 fs/drop_caches.c
91
92 static void drop_fs_caches(struct callback_head *twork)
93 {
94 int ret;
95 struct super_block *sb;
96 static bool suppress;
97 struct drop_fs_caches_work *work = container_of(twork,
98 struct drop_fs_caches_work, task_work);
99 unsigned int ctl = work->ctl;
100 dev_t dev = work->dev;
101
102 if (work->path) {
103 struct path path;
104
105 ret = kern_path(work->path, LOOKUP_FOLLOW, &path);
106 if (ret) {
107 syscall_set_return_value(current,
> 108 current_pt_regs(), 109 0, ret);
110 goto out;
111 }
112 dev = path.dentry->d_sb->s_dev;
113 /* Make this file's dentry and inode recyclable */
114 path_put(&path);
115 }
116
117 sb = user_get_super(dev, false);
118 if (!sb) {
119 syscall_set_return_value(current, current_pt_regs(), 0,
120 -EINVAL);
121 goto out;
122 }
123
124 if (ctl & BIT(0)) {
125 lru_add_drain_all();
126 drop_pagecache_sb(sb, NULL);
127 count_vm_event(DROP_PAGECACHE);
128 }
129
130 if (ctl & BIT(1)) {
131 reclaim_dcache_sb(sb);
132 reclaim_icache_sb(sb);
133 count_vm_event(DROP_SLAB);
134 }
135
136 if (!READ_ONCE(suppress)) {
137 pr_info("%s (%d): %s: %d %u:%u\n", current->comm,
138 task_pid_nr(current), __func__, ctl,
139 MAJOR(sb->s_dev), MINOR(sb->s_dev));
140
141 if (ctl & BIT(2))
142 WRITE_ONCE(suppress, true);
143 }
144
145 drop_super(sb);
146 out:
147 kfree(work->path);
148 kfree(work);
149 }
150
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 3/3 Documentation: add instructions for using 'drop_fs_caches sysctl' sysctl
In reply to Ye BinFrom: Ye Bin <[email protected]>
Add instructions for 'drop_fs_caches sysctl' sysctl in 'vm.rst'.
--- Documentation/admin-guide/sysctl/vm.rst | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
Documentation/admin-guide/sysctl/vm.rst +34 -0
@@ -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
Signature · 1 line
2.34.1