Commit 0c9e63fd authored by Eric Dumazet's avatar Eric Dumazet Committed by Linus Torvalds

[PATCH] Shrinks sizeof(files_struct) and better layout

1) Reduce the size of (struct fdtable) to exactly 64 bytes on 32bits
   platforms, lowering kmalloc() allocated space by 50%.

2) Reduce the size of (files_struct), using a special 32 bits (or
   64bits) embedded_fd_set, instead of a 1024 bits fd_set for the
   close_on_exec_init and open_fds_init fields.  This save some ram (248
   bytes per task) as most tasks dont open more than 32 files.  D-Cache
   footprint for such tasks is also reduced to the minimum.

3) Reduce size of allocated fdset.  Currently two full pages are
   allocated, that is 32768 bits on x86 for example, and way too much.  The
   minimum is now L1_CACHE_BYTES.

UP and SMP should benefit from this patch, because most tasks will touch
only one cache line when open()/close() stdin/stdout/stderr (0/1/2),
(next_fd, close_on_exec_init, open_fds_init, fd_array[0 ..  2] being in the
same cache line)
Signed-off-by: default avatarEric Dumazet <dada1@cosmosbay.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent d8733c29
...@@ -73,8 +73,8 @@ static int locate_fd(struct files_struct *files, ...@@ -73,8 +73,8 @@ static int locate_fd(struct files_struct *files,
* orig_start..fdt->next_fd * orig_start..fdt->next_fd
*/ */
start = orig_start; start = orig_start;
if (start < fdt->next_fd) if (start < files->next_fd)
start = fdt->next_fd; start = files->next_fd;
newfd = start; newfd = start;
if (start < fdt->max_fdset) { if (start < fdt->max_fdset) {
...@@ -102,9 +102,8 @@ static int locate_fd(struct files_struct *files, ...@@ -102,9 +102,8 @@ static int locate_fd(struct files_struct *files,
* we reacquire the fdtable pointer and use it while holding * we reacquire the fdtable pointer and use it while holding
* the lock, no one can free it during that time. * the lock, no one can free it during that time.
*/ */
fdt = files_fdtable(files); if (start <= files->next_fd)
if (start <= fdt->next_fd) files->next_fd = newfd + 1;
fdt->next_fd = newfd + 1;
error = newfd; error = newfd;
......
...@@ -125,7 +125,8 @@ static void free_fdtable_rcu(struct rcu_head *rcu) ...@@ -125,7 +125,8 @@ static void free_fdtable_rcu(struct rcu_head *rcu)
kmem_cache_free(files_cachep, fdt->free_files); kmem_cache_free(files_cachep, fdt->free_files);
return; return;
} }
if (fdt->max_fdset <= __FD_SETSIZE && fdt->max_fds <= NR_OPEN_DEFAULT) { if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
fdt->max_fds <= NR_OPEN_DEFAULT) {
/* /*
* The fdtable was embedded * The fdtable was embedded
*/ */
...@@ -155,7 +156,8 @@ static void free_fdtable_rcu(struct rcu_head *rcu) ...@@ -155,7 +156,8 @@ static void free_fdtable_rcu(struct rcu_head *rcu)
void free_fdtable(struct fdtable *fdt) void free_fdtable(struct fdtable *fdt)
{ {
if (fdt->free_files || fdt->max_fdset > __FD_SETSIZE || if (fdt->free_files ||
fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
fdt->max_fds > NR_OPEN_DEFAULT) fdt->max_fds > NR_OPEN_DEFAULT)
call_rcu(&fdt->rcu, free_fdtable_rcu); call_rcu(&fdt->rcu, free_fdtable_rcu);
} }
...@@ -199,7 +201,6 @@ static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt) ...@@ -199,7 +201,6 @@ static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
(nfdt->max_fds - fdt->max_fds) * (nfdt->max_fds - fdt->max_fds) *
sizeof(struct file *)); sizeof(struct file *));
} }
nfdt->next_fd = fdt->next_fd;
} }
/* /*
...@@ -220,11 +221,9 @@ fd_set * alloc_fdset(int num) ...@@ -220,11 +221,9 @@ fd_set * alloc_fdset(int num)
void free_fdset(fd_set *array, int num) void free_fdset(fd_set *array, int num)
{ {
int size = num / 8; if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */
if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
return; return;
else if (size <= PAGE_SIZE) else if (num <= 8 * PAGE_SIZE)
kfree(array); kfree(array);
else else
vfree(array); vfree(array);
...@@ -237,22 +236,17 @@ static struct fdtable *alloc_fdtable(int nr) ...@@ -237,22 +236,17 @@ static struct fdtable *alloc_fdtable(int nr)
fd_set *new_openset = NULL, *new_execset = NULL; fd_set *new_openset = NULL, *new_execset = NULL;
struct file **new_fds; struct file **new_fds;
fdt = kmalloc(sizeof(*fdt), GFP_KERNEL); fdt = kzalloc(sizeof(*fdt), GFP_KERNEL);
if (!fdt) if (!fdt)
goto out; goto out;
memset(fdt, 0, sizeof(*fdt));
nfds = __FD_SETSIZE; nfds = 8 * L1_CACHE_BYTES;
/* Expand to the max in easy steps */ /* Expand to the max in easy steps */
do { while (nfds <= nr) {
if (nfds < (PAGE_SIZE * 8))
nfds = PAGE_SIZE * 8;
else {
nfds = nfds * 2; nfds = nfds * 2;
if (nfds > NR_OPEN) if (nfds > NR_OPEN)
nfds = NR_OPEN; nfds = NR_OPEN;
} }
} while (nfds <= nr);
new_openset = alloc_fdset(nfds); new_openset = alloc_fdset(nfds);
new_execset = alloc_fdset(nfds); new_execset = alloc_fdset(nfds);
......
...@@ -973,7 +973,7 @@ int get_unused_fd(void) ...@@ -973,7 +973,7 @@ int get_unused_fd(void)
fdt = files_fdtable(files); fdt = files_fdtable(files);
fd = find_next_zero_bit(fdt->open_fds->fds_bits, fd = find_next_zero_bit(fdt->open_fds->fds_bits,
fdt->max_fdset, fdt->max_fdset,
fdt->next_fd); files->next_fd);
/* /*
* N.B. For clone tasks sharing a files structure, this test * N.B. For clone tasks sharing a files structure, this test
...@@ -998,7 +998,7 @@ int get_unused_fd(void) ...@@ -998,7 +998,7 @@ int get_unused_fd(void)
FD_SET(fd, fdt->open_fds); FD_SET(fd, fdt->open_fds);
FD_CLR(fd, fdt->close_on_exec); FD_CLR(fd, fdt->close_on_exec);
fdt->next_fd = fd + 1; files->next_fd = fd + 1;
#if 1 #if 1
/* Sanity check */ /* Sanity check */
if (fdt->fd[fd] != NULL) { if (fdt->fd[fd] != NULL) {
...@@ -1019,8 +1019,8 @@ static void __put_unused_fd(struct files_struct *files, unsigned int fd) ...@@ -1019,8 +1019,8 @@ static void __put_unused_fd(struct files_struct *files, unsigned int fd)
{ {
struct fdtable *fdt = files_fdtable(files); struct fdtable *fdt = files_fdtable(files);
__FD_CLR(fd, fdt->open_fds); __FD_CLR(fd, fdt->open_fds);
if (fd < fdt->next_fd) if (fd < files->next_fd)
fdt->next_fd = fd; files->next_fd = fd;
} }
void fastcall put_unused_fd(unsigned int fd) void fastcall put_unused_fd(unsigned int fd)
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <linux/compiler.h> #include <linux/compiler.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/rcupdate.h> #include <linux/rcupdate.h>
#include <linux/types.h>
/* /*
* The default fd array needs to be at least BITS_PER_LONG, * The default fd array needs to be at least BITS_PER_LONG,
...@@ -17,10 +18,22 @@ ...@@ -17,10 +18,22 @@
*/ */
#define NR_OPEN_DEFAULT BITS_PER_LONG #define NR_OPEN_DEFAULT BITS_PER_LONG
/*
* The embedded_fd_set is a small fd_set,
* suitable for most tasks (which open <= BITS_PER_LONG files)
*/
struct embedded_fd_set {
unsigned long fds_bits[1];
};
/*
* More than this number of fds: we use a separately allocated fd_set
*/
#define EMBEDDED_FD_SET_SIZE (BITS_PER_BYTE * sizeof(struct embedded_fd_set))
struct fdtable { struct fdtable {
unsigned int max_fds; unsigned int max_fds;
int max_fdset; int max_fdset;
int next_fd;
struct file ** fd; /* current fd array */ struct file ** fd; /* current fd array */
fd_set *close_on_exec; fd_set *close_on_exec;
fd_set *open_fds; fd_set *open_fds;
...@@ -33,13 +46,20 @@ struct fdtable { ...@@ -33,13 +46,20 @@ struct fdtable {
* Open file table structure * Open file table structure
*/ */
struct files_struct { struct files_struct {
/*
* read mostly part
*/
atomic_t count; atomic_t count;
struct fdtable *fdt; struct fdtable *fdt;
struct fdtable fdtab; struct fdtable fdtab;
fd_set close_on_exec_init; /*
fd_set open_fds_init; * written part on a separate cache line in SMP
*/
spinlock_t file_lock ____cacheline_aligned_in_smp;
int next_fd;
struct embedded_fd_set close_on_exec_init;
struct embedded_fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT]; struct file * fd_array[NR_OPEN_DEFAULT];
spinlock_t file_lock; /* Protects concurrent writers. Nests inside tsk->alloc_lock */
}; };
#define files_fdtable(files) (rcu_dereference((files)->fdt)) #define files_fdtable(files) (rcu_dereference((files)->fdt))
......
...@@ -7,11 +7,10 @@ ...@@ -7,11 +7,10 @@
#define INIT_FDTABLE \ #define INIT_FDTABLE \
{ \ { \
.max_fds = NR_OPEN_DEFAULT, \ .max_fds = NR_OPEN_DEFAULT, \
.max_fdset = __FD_SETSIZE, \ .max_fdset = EMBEDDED_FD_SET_SIZE, \
.next_fd = 0, \
.fd = &init_files.fd_array[0], \ .fd = &init_files.fd_array[0], \
.close_on_exec = &init_files.close_on_exec_init, \ .close_on_exec = (fd_set *)&init_files.close_on_exec_init, \
.open_fds = &init_files.open_fds_init, \ .open_fds = (fd_set *)&init_files.open_fds_init, \
.rcu = RCU_HEAD_INIT, \ .rcu = RCU_HEAD_INIT, \
.free_files = NULL, \ .free_files = NULL, \
.next = NULL, \ .next = NULL, \
...@@ -20,9 +19,10 @@ ...@@ -20,9 +19,10 @@
#define INIT_FILES \ #define INIT_FILES \
{ \ { \
.count = ATOMIC_INIT(1), \ .count = ATOMIC_INIT(1), \
.file_lock = SPIN_LOCK_UNLOCKED, \
.fdt = &init_files.fdtab, \ .fdt = &init_files.fdtab, \
.fdtab = INIT_FDTABLE, \ .fdtab = INIT_FDTABLE, \
.file_lock = SPIN_LOCK_UNLOCKED, \
.next_fd = 0, \
.close_on_exec_init = { { 0, } }, \ .close_on_exec_init = { { 0, } }, \
.open_fds_init = { { 0, } }, \ .open_fds_init = { { 0, } }, \
.fd_array = { NULL, } \ .fd_array = { NULL, } \
......
...@@ -607,12 +607,12 @@ static struct files_struct *alloc_files(void) ...@@ -607,12 +607,12 @@ static struct files_struct *alloc_files(void)
atomic_set(&newf->count, 1); atomic_set(&newf->count, 1);
spin_lock_init(&newf->file_lock); spin_lock_init(&newf->file_lock);
newf->next_fd = 0;
fdt = &newf->fdtab; fdt = &newf->fdtab;
fdt->next_fd = 0;
fdt->max_fds = NR_OPEN_DEFAULT; fdt->max_fds = NR_OPEN_DEFAULT;
fdt->max_fdset = __FD_SETSIZE; fdt->max_fdset = EMBEDDED_FD_SET_SIZE;
fdt->close_on_exec = &newf->close_on_exec_init; fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
fdt->open_fds = &newf->open_fds_init; fdt->open_fds = (fd_set *)&newf->open_fds_init;
fdt->fd = &newf->fd_array[0]; fdt->fd = &newf->fd_array[0];
INIT_RCU_HEAD(&fdt->rcu); INIT_RCU_HEAD(&fdt->rcu);
fdt->free_files = NULL; fdt->free_files = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment