add support for drop_caches for individual filesystem
Subsystems (4)
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.
Ye Bin (3):
vfs: introduce shrink_icache_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 | 27 ++++++++++++++++ fs/drop_caches.c | 43 +++++++++++++++++++++++++ fs/inode.c | 17 ++++++++++ fs/internal.h | 1 + include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 ++++++ 6 files changed, 99 insertions(+)
Signature · 1 line
2.31.1
PATCH 1/3 vfs: introduce shrink_icache_sb() helper
From: Ye Bin <[email protected]>
This patch is prepare for support drop_caches for specify file system. shrink_icache_sb() helper walk the superblock inode LRU for freeable inodes and attempt to free them.
--- fs/inode.c | 17 +++++++++++++++++ fs/internal.h | 1 + 2 files changed, 18 insertions(+)
fs/inode.c +17 -0
@@ -1045,6 +1045,23 @@ 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(). +*/ +void shrink_icache_sb (struct super_block *sb) +{ +do { +LIST_HEAD (dispose); + +list_lru_walk (&sb->s_inode_lru, inode_lru_isolate, + &dispose,1024 ); +dispose_list (&dispose); + }while (list_lru_count (&sb->s_inode_lru) >0 ); +} +EXPORT_SYMBOL (shrink_icache_sb); +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
@@ -199,6 +199,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 shrink_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);
Signature · 1 line
2.31.1
Jan Kara
reviewerRe: [PATCH 1/3] vfs: introduce shrink_icache_sb() helper
Quoted 26 lines · Ye Bin
From: Ye Bin <[email protected]>
This patch is prepare for support
drop_cachesfor specify file system.shrink_icache_sb()helper walk the superblock inode LRU for freeable inodes and attempt to free them.Signed-off-by Ye Bin <[email protected]>--- fs/inode.c | 17 +++++++++++++++++ fs/internal.h | 1 + 2 files changed, 18 insertions(+)fs/inode.c +6
-0@@ -1045,6 +1045,23 @@ 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(). +*/ +void shrink_icache_sb (struct super_block *sb)
fs/inode.c long prune_icache_sb(struct super_block *sb, struct shrink_control *sc) · line 1063
Quoted 10 lines
fs/inode.c +10
-0+{ +do { +LIST_HEAD (dispose); + +list_lru_walk (&sb->s_inode_lru, inode_lru_isolate, + &dispose,1024 ); +dispose_list (&dispose); + }while (list_lru_count (&sb->s_inode_lru) >0 ); +} +EXPORT_SYMBOL (shrink_icache_sb);
Hum, but this will livelock if we cannot remove all the inodes? Now I guess inode_lru_isolate() usually removes busy inodes from the LRU so this should not happen in practice but such behavior is not guaranteed (we can LRU_SKIP inodes if i_lock is busy or LRU_RETRY if inode has page cache pages). So I think we need some safety net here...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH 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 | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)
fs/drop_caches.c +43 -0
@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{ +unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ; + +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ; + +if (ctl < *((int *)table->extra1) || ctl > *((int *)table->extra2)) +return -EINVAL ; + + sb =user_get_super (MKDEV (major, minor), false); +if (!sb) +return -EINVAL ; + +if (ctl &1 ) { +lru_add_drain_all (); +drop_pagecache_sb (sb,NULL ); +count_vm_event (DROP_PAGECACHE ); + } + +if (ctl &2 ) { +shrink_dcache_sb (sb); +shrink_icache_sb (sb); +count_vm_event (DROP_SLAB ); + } + +drop_super (sb); + +if (!stfu) +pr_info ("%s (%d): drop_fs_caches: %u:%u:%d\n" , current->comm, +task_pid_nr (current), major, minor, ctl); + stfu |= ctl &4 ; + +return 0 ; +}
include/linux/mm.h +2 -0
@@ -3788,6 +3788,8 @@ extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);extern int sysctl_drop_caches;int drop_caches_sysctl_handler (const struct ctl_table *,int ,void *,size_t *,loff_t *); +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos);#endif void drop_slab (void );
kernel/sysctl.c +9 -0
@@ -2101,6 +2101,15 @@ static struct ctl_table vm_table[] = { .extra1 = SYSCTL_ONE, .extra2 = SYSCTL_FOUR, }, + { + .procname = "drop_fs_caches", + .data = NULL, + .maxlen = 256, + .mode = 0200, + .proc_handler = drop_fs_caches_sysctl_handler, + .extra1 = SYSCTL_ONE, + .extra2 = SYSCTL_FOUR, + }, { .procname = "page_lock_unfairness", .data = &sysctl_page_lock_unfairness,
Signature · 1 line
2.31.1
Jan Kara
reviewerRe: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
Quoted 4 lines · Ye Bin
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
I don't quite understand the use case you mention here. Can you explain it a bit more (that being said I've needed dropping caches for a particular sb myself a few times for debugging purposes so I generally agree it is a useful feature).
Quoted 27 lines
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 <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +4
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 93
Quoted 10 lines
fs/drop_caches.c +10
-0+unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ; + +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ;
I think specifying bdev major & minor number is not a great interface these days. In particular for filesystems which are not bdev based such as NFS. I think specifying path to some file/dir in the filesystem is nicer and you can easily resolve that to sb here as well.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
Quoted 9 lines · Jan Kara
Quoted 4 lines · Ye Bin
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
I don't quite understand the use case you mention here. Can you explain it a bit more (that being said I've needed dropping caches for a particular sb myself a few times for debugging purposes so I generally agree it is a useful feature).
Well, I'm analyzing what files are still open and the file system can't be unmounted. The process occupied by the opened file cannot be found through the fuser. That is, the file may be occupied by the kernel mode. You can insert a module or use kprobe to obtain all cached files of the corresponding file system. But there can be a lot of files, so I want to clean up irrelevant files first.
Quoted 42 lines
Quoted 27 lines
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 <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +4
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 93
Quoted 10 lines
fs/drop_caches.c +10
-0+unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ; + +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ;I think specifying bdev major & minor number is not a great interface these days. In particular for filesystems which are not bdev based such as NFS. I think specifying path to some file/dir in the filesystem is nicer and you can easily resolve that to sb here as well.
Quoted 1 line
Honza
That's a really good idea. I think by specifying bdev "major & minor", you can reclaim the file system pagecache that is not unmounted due to "umount -l" mode. In this case, the sb of the corresponding file system cannot be found in the specified path. So I think we can support both ways. I look forward to your opinion.
Re: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
In reply to Jan KaraQuoted 45 lines · Jan Kara
Quoted 4 lines · Ye Bin
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
I don't quite understand the use case you mention here. Can you explain it a bit more (that being said I've needed dropping caches for a particular sb myself a few times for debugging purposes so I generally agree it is a useful feature).
Quoted 34 lines
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 <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +11
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{ +unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ;
Quoted 8 lines
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 93
Quoted 3 lines
fs/drop_caches.c +3
-0+ +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ;I think specifying bdev major & minor number is not a great interface these days. In particular for filesystems which are not bdev based such as NFS. I think specifying path to some file/dir in the filesystem is nicer and you can easily resolve that to sb here as well.
Slight disagreement here since NFS uses set_anon_super() and major:minor will work fine with it. I'd prefer it actually since it avoids this interface having to do a pathwalk and make decisions about what's mounted where and in what namespace.
Ben
Jan Kara
reviewerRe: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
Quoted 56 lines · Benjamin Coddington
Quoted 45 lines · Jan Kara
Quoted 4 lines · Ye Bin
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
I don't quite understand the use case you mention here. Can you explain it a bit more (that being said I've needed dropping caches for a particular sb myself a few times for debugging purposes so I generally agree it is a useful feature).
Quoted 34 lines
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 <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +11
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{ +unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ;Quoted 8 lines
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 93
Quoted 3 lines
fs/drop_caches.c +3
-0+ +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ;I think specifying bdev major & minor number is not a great interface these days. In particular for filesystems which are not bdev based such as NFS. I think specifying path to some file/dir in the filesystem is nicer and you can easily resolve that to sb here as well.
Quoted 2 lines
Slight disagreement here since NFS uses
set_anon_super()and major:minor will work fine with it.
OK, fair point, anon bdev numbers can be used. But filesystems using get_tree_nodev() would still be problematic.
Quoted 3 lines
I'd prefer it actually since it avoids this
interface having to do a pathwalk and make decisions about what's mounted where and in what namespace.
I don't understand the problem here. We'd do user_path_at(AT_FDCWD, ..., &path) and then take path.mnt->mnt_sb. That doesn't look terribly complicated to me. Plus it naturally deals with issues like namespacing etc. although they are not a huge issue here because the functionality should be restricted to CAP_SYS_ADMIN anyway.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
Quoted 68 lines · Jan Kara
Quoted 56 lines · Benjamin Coddington
Quoted 45 lines · Jan Kara
Quoted 4 lines · Ye Bin
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
I don't quite understand the use case you mention here. Can you explain it a bit more (that being said I've needed dropping caches for a particular sb myself a few times for debugging purposes so I generally agree it is a useful feature).
Quoted 34 lines
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 <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +11
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{ +unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ;Quoted 8 lines
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 93
Quoted 3 lines
fs/drop_caches.c +3
-0+ +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ;I think specifying bdev major & minor number is not a great interface these days. In particular for filesystems which are not bdev based such as NFS. I think specifying path to some file/dir in the filesystem is nicer and you can easily resolve that to sb here as well.
Quoted 2 lines
Slight disagreement here since NFS uses
set_anon_super()and major:minor will work fine with it.OK, fair point, anon bdev numbers can be used. But filesystems using
get_tree_nodev()would still be problematic.Quoted 3 lines
I'd prefer it actually since it avoids this
interface having to do a pathwalk and make decisions about what's mounted where and in what namespace.
Quoted 6 lines
I don't understand the problem here. We'd do
user_path_at(AT_FDCWD, ..., &path)and then takepath.mnt->mnt_sb. That doesn't look terribly complicated to me. Plus it naturally deals with issues like namespacing etc. although they are not a huge issue here because the functionality should be restricted toCAP_SYS_ADMINanyway.
Both looking up bdev and looking up path from write() can make syzbot and lockdep very upset:
https://lore.kernel.org/linux-fsdevel/[email protected]/
I thought Christian had a proposal for dropping cache per-sb API via fadvise() or something?
Why use sysfs API for this and not fd to reference an sb?
Thanks,
Amir.
Jan Kara
reviewerRe: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
Quoted 76 lines · Amir Goldstein
Quoted 68 lines · Jan Kara
Quoted 56 lines · Benjamin Coddington
Quoted 45 lines · Jan Kara
Quoted 4 lines · Ye Bin
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
I don't quite understand the use case you mention here. Can you explain it a bit more (that being said I've needed dropping caches for a particular sb myself a few times for debugging purposes so I generally agree it is a useful feature).
Quoted 34 lines
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 <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +11
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{ +unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ;Quoted 8 lines
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 93
Quoted 3 lines
fs/drop_caches.c +3
-0+ +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ;I think specifying bdev major & minor number is not a great interface these days. In particular for filesystems which are not bdev based such as NFS. I think specifying path to some file/dir in the filesystem is nicer and you can easily resolve that to sb here as well.
Quoted 2 lines
Slight disagreement here since NFS uses
set_anon_super()and major:minor will work fine with it.OK, fair point, anon bdev numbers can be used. But filesystems using
get_tree_nodev()would still be problematic.Quoted 3 lines
I'd prefer it actually since it avoids this
interface having to do a pathwalk and make decisions about what's mounted where and in what namespace.Quoted 6 lines
I don't understand the problem here. We'd do
user_path_at(AT_FDCWD, ..., &path)and then takepath.mnt->mnt_sb. That doesn't look terribly complicated to me. Plus it naturally deals with issues like namespacing etc. although they are not a huge issue here because the functionality should be restricted toCAP_SYS_ADMINanyway.
Quoted 3 lines
Both looking up bdev and looking up path from
write()can make syzbot and lockdep very upset:
https://lore.kernel.org/linux-fsdevel/[email protected]/
OK, thanks for the reference.
Quoted 4 lines
I thought Christian had a proposal for dropping cache per-sb API via
fadvise()or something?Why use sysfs API for this and not fd to reference an sb?
I guess because the original drop_caches is in the sysfs. But yes, in principle we could use fd pointing to the filesystem for this. I'm just not sure fadvise(2) is really the right syscall for this because it is currently all about page cache of a file and this call should shrink also the dcache / icache. But ioctl() (not sure if this debug-mostly functionality is worth a syscall) implemented in VFS would certainly be possible and perhaps nicer than sysfs interface.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
In reply to Ye BinQuoted 40 lines · Ye Bin
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 patch supports the recycling of pagecache/entry for individual file systems.Signed-off-by Ye Bin <[email protected]>--- fs/drop_caches.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ kernel/sysctl.c | 9 +++++++++ 3 files changed, 54 insertions(+)fs/drop_caches.c +13
-0@@ -77,3 +77,46 @@ int drop_caches_sysctl_handler(const struct ctl_table *table, int write, }return 0 ; } + +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos) +{ +unsigned int major, minor; +unsigned int ctl; +struct super_block *sb; +static int stfu; + +if (!write) +return 0 ; + +if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 )
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 102
Quoted 10 lines
fs/drop_caches.c +10
-0+return -EINVAL ; + +if (ctl < *((int *)table->extra1) || ctl > *((int *)table->extra2)) +return -EINVAL ; + + sb =user_get_super (MKDEV (major, minor), false); +if (!sb) +return -EINVAL ; + +if (ctl &1 ) {
BIT(0)
Quoted 7 lines
fs/drop_caches.c +7
-0+lru_add_drain_all (); +drop_pagecache_sb (sb,NULL ); +count_vm_event (DROP_PAGECACHE ); + } + +if (ctl &2 ) { +shrink_dcache_sb (sb);
fs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 119
Quoted 10 lines
fs/drop_caches.c +10
-0+shrink_icache_sb (sb); +count_vm_event (DROP_SLAB ); + } + +drop_super (sb); + +if (!stfu) +pr_info ("%s (%d): drop_fs_caches: %u:%u:%d\n" , current->comm, +task_pid_nr (current), major, minor, ctl); + stfu |= ctl &4 ;
This looks very weird. I guess it's already in the original drop_caches_sysctl_handler().
kernel/sysctl.c static struct ctl_table vm_table[] = {
Quoted 21 lines
fs/drop_caches.c +3
-0+ +return 0 ; +}include/linux/mm.h +2
-0@@ -3788,6 +3788,8 @@ extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);extern int sysctl_drop_caches;int drop_caches_sysctl_handler (const struct ctl_table *,int ,void *,size_t *,loff_t *); +int drop_fs_caches_sysctl_handler (const struct ctl_table *table,int write, +void *buffer,size_t *length,loff_t *ppos);#endif void drop_slab (void );kernel/sysctl.c +0
-0@@ -2101,6 +2101,15 @@ static struct ctl_table vm_table[] = {
Sooner or later this table should move out of kernel/sysctl.c and into a subsystem-specific file.
This also means the handler doesn't need to be exported.
kernel/sysctl.c static struct ctl_table vm_table[] = { · line 2106
Quoted 6 lines
kernel/sysctl.c +3
-0.extra1 = SYSCTL_ONE, .extra2 = SYSCTL_FOUR, }, + { + .procname = "drop_fs_caches", + .data = NULL,
NULL is already the default.
kernel/sysctl.c static struct ctl_table vm_table[] = { · line 2107
Quoted 1 line
kernel/sysctl.c +1
-0+ .maxlen = 256,
The maxlen field refers to the data field.
As there is no data, there should be no maxlen.
kernel/sysctl.c static struct ctl_table vm_table[] = { · line 2111
Quoted 4 lines
kernel/sysctl.c +4
-0+ .mode = 0200, + .proc_handler = drop_fs_caches_sysctl_handler, + .extra1 = SYSCTL_ONE, + .extra2 = SYSCTL_FOUR,
These extras are meant as parameters for generic handlers. Inlining the limits into your hander makes it much clearer.
Quoted 7 lines
kernel/sysctl.c +1
-0+ }, { .procname = "page_lock_unfairness", .data = &sysctl_page_lock_unfairness,Signature · 1 line
2.31.1
Al Viro
maintainerRe: [PATCH 2/3] sysctl: add support for drop_caches for individual filesystem
In reply to Ye Binfs/drop_caches.c int drop_caches_sysctl_handler(const struct ctl_table *table, int write, · line 100
Quoted 9 lines · Ye Bin
fs/drop_caches.c +9
-0+if (sscanf (buffer,"%u:%u:%u" , &major, &minor, &ctl) !=3 ) +return -EINVAL ; + +if (ctl < *((int *)table->extra1) || ctl > *((int *)table->extra2)) +return -EINVAL ; + + sb =user_get_super (MKDEV (major, minor), false); +if (!sb) +return -EINVAL ;
Odd user interface aside, you do realize that you've just grabbed ->s_umount from inside a ->write() instance? Considering how much can be grabbed under ->s_umount... Ow.
IOW, I very much doubt that doing that kind of stuff from sysctl is a good idea - if nothing else, we'll end up with syzbot screaming its head off about many and varied potential deadlocks, as soon as it discovers that one. And I wouldn't swear that all of those would be false positives.
PATCH 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 | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
Documentation/admin-guide/sysctl/vm.rst +27 -0
@@ -36,6 +36,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
@@ -268,6 +269,32 @@ 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 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 "MAJOR:MINOR:1" > /proc/sys/vm/drop_caches + +To free reclaimable slab objects (includes dentries and inodes):: + + echo "MAJOR:MINOR:2" > /proc/sys/vm/drop_caches + +To free slab objects and pagecache:: + + echo "MAJOR:MINOR:3" > /proc/sys/vm/drop_caches + +You may see informational messages in your kernel log when this file is +used:: + + echo (1234): drop_fs_caches: MAJOR:MINOR:3 + enable_soft_offline =================== Correctable memory errors are very common on servers. Soft-offline is kernel's
Signature · 1 line
2.31.1