Fix file counting
Subsystems (4)
RFC PATCH 1/1 Fix file counting
Eric,
Going by your patch, I converted my nr-files patch to use percpu counters - except that I just used the existing percpu counter code. This patch is untested, just for comments. If you agree with the approach, we can go with it. I will get some benchmark numbers measured on a 16-CPU box.
Thanks
Dipankar
The way we do file struct accounting is not very suitable for batched freeing. For scalability reasons, file accounting was constructor/destructor based. This meant that nr_files was decremented only when
the object was removed from the slab cache. This is
susceptible to slab fragmentation. With RCU based file structure, consequent batched freeing and a test program like Serge's,
we just speed this up and end up with a very fragmented slab -
llm22:~ # cat /proc/sys/fs/file-nr
587730 0 758844
At the same time, I see only a 2000+ objects in filp cache. The following patch I fixes this problem.
This patch changes the file counting by removing the filp_count_lock. Instead we use a separate percpu counter, nr_files, for now and all accesses to it are through get_nr_files() api. In the sysctl handler for nr_files, we populate files_stat.nr_files before returning to user.
Counting files as an when they are created and destroyed (as opposed to inside slab) allows us to correctly count open files with RCU.
---
fs/dcache.c | 2 - fs/file_table.c | 80 +++++++++++++++++++++++++++++++-------------------- include/linux/file.h | 2 - include/linux/fs.h | 2 + kernel/sysctl.c | 5 ++- net/unix/af_unix.c | 2 - 6 files changed, 57 insertions(+), 36 deletions(-)
fs/dcache.c renamed from fs/dcache.c~fix-file-counting +1 -1
@@ -1736,7 +1736,7 @@ void __init vfs_caches_init(unsigned lon SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); filp_cachep =kmem_cache_create ("filp" ,sizeof (struct file ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC , filp_ctor, filp_dtor); +SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ,NULL );dcache_init (mempages);inode_init (mempages);
fs/file_table.c renamed from fs/file_table.c~fix-file-counting +49 -31
132 lines not on this page · raw message
include/linux/file.h renamed from include/linux/file.h~fix-file-counting +0 -2
@@ -60,8 +60,6 @@ extern void put_filp(struct file *);extern int get_unused_fd (void );extern void FASTCALL (put_unused_fd (unsigned int fd));struct kmem_cache ; -extern void filp_ctor (void * objp,struct kmem_cache *cachep,unsigned long cflags); -extern void filp_dtor (void * objp,struct kmem_cache *cachep,unsigned long dflags);extern struct file **alloc_fd_array (int );extern void free_fd_array (struct file **,int );
include/linux/fs.h renamed from include/linux/fs.h~fix-file-counting +2 -0
@@ -35,6 +35,8 @@ struct files_stat_struct {int max_files;/* tunable */ };extern struct files_stat_struct files_stat; +extern int get_nr_files (void ); +extern int get_max_files (void );struct inodes_stat_t {int nr_inodes;
kernel/sysctl.c renamed from kernel/sysctl.c~fix-file-counting +4 -1
@@ -52,6 +52,9 @@#include <linux/nfs_fs.h> #endif +extern int proc_nr_files (ctl_table *table,int write,struct file *filp, +void __user *buffer,size_t *lenp,loff_t *ppos); +#if defined(CONFIG_SYSCTL )/* External variables not in a header file. */
@@ -921,7 +924,7 @@ static ctl_table fs_table[] = { .data = &files_stat, .maxlen = 3*sizeof(int), .mode = 0444, - .proc_handler = &proc_dointvec, + .proc_handler = &proc_nr_files, }, { .ctl_name = FS_MAXFILE,
net/unix/af_unix.c renamed from net/unix/af_unix.c~fix-file-counting +1 -1
@@ -547,7 +547,7 @@ static struct sock * unix_create1(structstruct sock *sk =NULL ;struct unix_sock *u; -if (atomic_read (&unix_nr_socks) >=2 *files_stat.max_files) +if (atomic_read (&unix_nr_socks) >=2 *get_max_files ()) goto out; sk =sk_alloc (PF_UNIX ,GFP_KERNEL , &unix_proto,1 );
_
Quoted 42 lines · Dipankar Sarma
Eric,
Going by your patch, I converted my nr-files patch to use percpu counters - except that I just used the existing percpu counter code. This patch is untested, just for comments. If you agree with the approach, we can go with it. I will get some benchmark numbers measured on a 16-CPU box.
Thanks
DipankarThe way we do file struct accounting is not very suitable for batched freeing. For scalability reasons, file accounting was constructor/destructor based. This meant that
nr_fileswas decremented only when
the object was removed from the slab cache. This is
susceptible to slab fragmentation. With RCU based file structure, consequent batched freeing and a test program like Serge's,
we just speed this up and end up with a very fragmented slab -llm22:~ # cat
/proc/sys/fs/file-nr
587730 0 758844At the same time, I see only a 2000+ objects in filp cache. The following patch I fixes this problem.
This patch changes the file counting by removing the
filp_count_lock. Instead we use a separate percpu counter,nr_files, for now and all accesses to it are throughget_nr_files()api. In the sysctl handler fornr_files, we populate files_stat.nr_files before returning to user.Counting files as an when they are created and destroyed (as opposed to inside slab) allows us to correctly count open files with RCU.
Signed-off-by Dipankar Sarma <[email protected]>---
Quoted 8 lines
fs/dcache.c | 2 - fs/file_table.c | 80 +++++++++++++++++++++++++++++++-------------------- include/linux/file.h | 2 - include/linux/fs.h | 2 + kernel/sysctl.c | 5 ++- net/unix/af_unix.c | 2 - 6 files changed, 57 insertions(+), 36 deletions(-)
Hi Dipankar
I believe your patch is good.
However there is a bug in get_empty_filp() :
if security_file_alloc(f) returns TRUE : The goto fail_sec; is done and fail_sec: does a file_free(f)
This means an extra percpu_counter_mod(&nr_files, -1L); will be done.
So I think you must apply this patch too :
fail_sec:
- file_free(f);
+ kmem_cache_free(filp_cachep, f);
fail:
return NULL;
(Ie no need for a delayed free done via rcu here, just a direct kmem_cache_free() call)
Eric
Quoted 28 lines · Eric Dumazet
Quoted 9 lines · Dipankar Sarma
Eric,
Counting files as an when they are created and destroyed (as opposed to inside slab) allows us to correctly count open files with RCU.
Signed-off-by Dipankar Sarma <[email protected]>---
Hi Dipankar
I believe your patch is good.
However there is a bug in
get_empty_filp():if
security_file_alloc(f)returns TRUE : The gotofail_sec; is done andfail_sec: does afile_free(f)This means an extra
percpu_counter_mod(&nr_files, -1L); will be done.So I think you must apply this patch too :
fail_sec:(quoted diff) +1
-1- file_free(f); + kmem_cache_free(filp_cachep, f); fail:
Quoted 2 lines
(Ie no need for a delayed free done via rcu here, just a direct
kmem_cache_free()call)
Good catch! Either that or I can just increment the file count just after allocating the struct file. That way all freeing is done from file_free(). Sounds good ? I still need to test this stuff though.
thanks
Dipankar
The way we do file struct accounting is not very suitable for batched freeing. For scalability reasons, file accounting was constructor/destructor based. This meant that nr_files was decremented only when
the object was removed from the slab cache. This is
susceptible to slab fragmentation. With RCU based file structure, consequent batched freeing and a test program like Serge's,
we just speed this up and end up with a very fragmented slab -
llm22:~ # cat /proc/sys/fs/file-nr
587730 0 758844
At the same time, I see only a 2000+ objects in filp cache. The following patch I fixes this problem.
This patch changes the file counting by removing the filp_count_lock. Instead we use a separate percpu counter, nr_files, for now and all accesses to it are through get_nr_files() api. In the sysctl handler for nr_files, we populate files_stat.nr_files before returning to user.
Counting files as an when they are created and destroyed (as opposed to inside slab) allows us to correctly count open files with RCU.
---
fs/dcache.c | 2 - fs/file_table.c | 80 +++++++++++++++++++++++++++++++-------------------- include/linux/file.h | 2 - include/linux/fs.h | 2 + kernel/sysctl.c | 5 ++- net/unix/af_unix.c | 2 - 6 files changed, 57 insertions(+), 36 deletions(-)
fs/dcache.c renamed from fs/dcache.c~fix-file-counting +1 -1
@@ -1736,7 +1736,7 @@ void __init vfs_caches_init(unsigned lon SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); filp_cachep =kmem_cache_create ("filp" ,sizeof (struct file ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC , filp_ctor, filp_dtor); +SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ,NULL );dcache_init (mempages);inode_init (mempages);
fs/file_table.c renamed from fs/file_table.c~fix-file-counting +49 -31
134 lines not on this page · raw message
include/linux/file.h renamed from include/linux/file.h~fix-file-counting +0 -2
@@ -60,8 +60,6 @@ extern void put_filp(struct file *);extern int get_unused_fd (void );extern void FASTCALL (put_unused_fd (unsigned int fd));struct kmem_cache ; -extern void filp_ctor (void * objp,struct kmem_cache *cachep,unsigned long cflags); -extern void filp_dtor (void * objp,struct kmem_cache *cachep,unsigned long dflags);extern struct file **alloc_fd_array (int );extern void free_fd_array (struct file **,int );
include/linux/fs.h renamed from include/linux/fs.h~fix-file-counting +2 -0
@@ -35,6 +35,8 @@ struct files_stat_struct {int max_files;/* tunable */ };extern struct files_stat_struct files_stat; +extern int get_nr_files (void ); +extern int get_max_files (void );struct inodes_stat_t {int nr_inodes;
kernel/sysctl.c renamed from kernel/sysctl.c~fix-file-counting +4 -1
@@ -52,6 +52,9 @@#include <linux/nfs_fs.h> #endif +extern int proc_nr_files (ctl_table *table,int write,struct file *filp, +void __user *buffer,size_t *lenp,loff_t *ppos); +#if defined(CONFIG_SYSCTL )/* External variables not in a header file. */
@@ -921,7 +924,7 @@ static ctl_table fs_table[] = { .data = &files_stat, .maxlen = 3*sizeof(int), .mode = 0444, - .proc_handler = &proc_dointvec, + .proc_handler = &proc_nr_files, }, { .ctl_name = FS_MAXFILE,
net/unix/af_unix.c renamed from net/unix/af_unix.c~fix-file-counting +1 -1
@@ -547,7 +547,7 @@ static struct sock * unix_create1(structstruct sock *sk =NULL ;struct unix_sock *u; -if (atomic_read (&unix_nr_socks) >=2 *files_stat.max_files) +if (atomic_read (&unix_nr_socks) >=2 *get_max_files ()) goto out; sk =sk_alloc (PF_UNIX ,GFP_KERNEL , &unix_proto,1 );
_