Skip to content

Commit 518de9b

Browse files
Eric Dumazettorvalds
authored andcommitted
fs: allow for more than 2^31 files
Robin Holt tried to boot a 16TB system and found af_unix was overflowing a 32bit value : <quote> We were seeing a failure which prevented boot. The kernel was incapable of creating either a named pipe or unix domain socket. This comes down to a common kernel function called unix_create1() which does: atomic_inc(&unix_nr_socks); if (atomic_read(&unix_nr_socks) > 2 * get_max_files()) goto out; The function get_max_files() is a simple return of files_stat.max_files. files_stat.max_files is a signed integer and is computed in fs/file_table.c's files_init(). n = (mempages * (PAGE_SIZE / 1024)) / 10; files_stat.max_files = n; In our case, mempages (total_ram_pages) is approx 3,758,096,384 (0xe0000000). That leaves max_files at approximately 1,503,238,553. This causes 2 * get_max_files() to integer overflow. </quote> Fix is to let /proc/sys/fs/file-nr & /proc/sys/fs/file-max use long integers, and change af_unix to use an atomic_long_t instead of atomic_t. get_max_files() is changed to return an unsigned long. get_nr_files() is changed to return a long. unix_nr_socks is changed from atomic_t to atomic_long_t, while not strictly needed to address Robin problem. Before patch (on a 64bit kernel) : # echo 2147483648 >/proc/sys/fs/file-max # cat /proc/sys/fs/file-max -18446744071562067968 After patch: # echo 2147483648 >/proc/sys/fs/file-max # cat /proc/sys/fs/file-max 2147483648 # cat /proc/sys/fs/file-nr 704 0 2147483648 Reported-by: Robin Holt <holt@sgi.com> Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Acked-by: David Miller <davem@davemloft.net> Reviewed-by: Robin Holt <holt@sgi.com> Tested-by: Robin Holt <holt@sgi.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Christoph Hellwig <hch@lst.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 571428b commit 518de9b

4 files changed

Lines changed: 21 additions & 24 deletions

File tree

‎fs/file_table.c‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ static inline void file_free(struct file *f)
6060
/*
6161
* Return the total number of open files in the system
6262
*/
63-
static int get_nr_files(void)
63+
static long get_nr_files(void)
6464
{
6565
return percpu_counter_read_positive(&nr_files);
6666
}
6767

6868
/*
6969
* Return the maximum number of open files in the system
7070
*/
71-
int get_max_files(void)
71+
unsigned long get_max_files(void)
7272
{
7373
return files_stat.max_files;
7474
}
@@ -82,7 +82,7 @@ int proc_nr_files(ctl_table *table, int write,
8282
void __user *buffer, size_t *lenp, loff_t *ppos)
8383
{
8484
files_stat.nr_files = get_nr_files();
85-
return proc_dointvec(table, write, buffer, lenp, ppos);
85+
return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
8686
}
8787
#else
8888
int proc_nr_files(ctl_table *table, int write,
@@ -105,7 +105,7 @@ int proc_nr_files(ctl_table *table, int write,
105105
struct file *get_empty_filp(void)
106106
{
107107
const struct cred *cred = current_cred();
108-
static int old_max;
108+
static long old_max;
109109
struct file * f;
110110

111111
/*
@@ -140,8 +140,7 @@ struct file *get_empty_filp(void)
140140
over:
141141
/* Ran out of filps - report that */
142142
if (get_nr_files() > old_max) {
143-
printk(KERN_INFO "VFS: file-max limit %d reached\n",
144-
get_max_files());
143+
pr_info("VFS: file-max limit %lu reached\n", get_max_files());
145144
old_max = get_nr_files();
146145
}
147146
goto fail;
@@ -487,7 +486,7 @@ void mark_files_ro(struct super_block *sb)
487486

488487
void __init files_init(unsigned long mempages)
489488
{
490-
int n;
489+
unsigned long n;
491490

492491
filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
493492
SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
@@ -498,9 +497,7 @@ void __init files_init(unsigned long mempages)
498497
*/
499498

500499
n = (mempages * (PAGE_SIZE / 1024)) / 10;
501-
files_stat.max_files = n;
502-
if (files_stat.max_files < NR_FILE)
503-
files_stat.max_files = NR_FILE;
500+
files_stat.max_files = max_t(unsigned long, n, NR_FILE);
504501
files_defer_init();
505502
lg_lock_init(files_lglock);
506503
percpu_counter_init(&nr_files, 0);

‎include/linux/fs.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434

3535
/* And dynamically-tunable limits and defaults: */
3636
struct files_stat_struct {
37-
int nr_files; /* read only */
38-
int nr_free_files; /* read only */
39-
int max_files; /* tunable */
37+
unsigned long nr_files; /* read only */
38+
unsigned long nr_free_files; /* read only */
39+
unsigned long max_files; /* tunable */
4040
};
4141

4242
struct inodes_stat_t {
@@ -400,7 +400,7 @@ extern void __init inode_init_early(void);
400400
extern void __init files_init(unsigned long);
401401

402402
extern struct files_stat_struct files_stat;
403-
extern int get_max_files(void);
403+
extern unsigned long get_max_files(void);
404404
extern int sysctl_nr_open;
405405
extern struct inodes_stat_t inodes_stat;
406406
extern int leases_enable, lease_break_time;

‎kernel/sysctl.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,16 +1352,16 @@ static struct ctl_table fs_table[] = {
13521352
{
13531353
.procname = "file-nr",
13541354
.data = &files_stat,
1355-
.maxlen = 3*sizeof(int),
1355+
.maxlen = sizeof(files_stat),
13561356
.mode = 0444,
13571357
.proc_handler = proc_nr_files,
13581358
},
13591359
{
13601360
.procname = "file-max",
13611361
.data = &files_stat.max_files,
1362-
.maxlen = sizeof(int),
1362+
.maxlen = sizeof(files_stat.max_files),
13631363
.mode = 0644,
1364-
.proc_handler = proc_dointvec,
1364+
.proc_handler = proc_doulongvec_minmax,
13651365
},
13661366
{
13671367
.procname = "nr_open",

‎net/unix/af_unix.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117

118118
static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
119119
static DEFINE_SPINLOCK(unix_table_lock);
120-
static atomic_t unix_nr_socks = ATOMIC_INIT(0);
120+
static atomic_long_t unix_nr_socks;
121121

122122
#define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
123123

@@ -360,13 +360,13 @@ static void unix_sock_destructor(struct sock *sk)
360360
if (u->addr)
361361
unix_release_addr(u->addr);
362362

363-
atomic_dec(&unix_nr_socks);
363+
atomic_long_dec(&unix_nr_socks);
364364
local_bh_disable();
365365
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
366366
local_bh_enable();
367367
#ifdef UNIX_REFCNT_DEBUG
368-
printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk,
369-
atomic_read(&unix_nr_socks));
368+
printk(KERN_DEBUG "UNIX %p is destroyed, %ld are still alive.\n", sk,
369+
atomic_long_read(&unix_nr_socks));
370370
#endif
371371
}
372372

@@ -606,8 +606,8 @@ static struct sock *unix_create1(struct net *net, struct socket *sock)
606606
struct sock *sk = NULL;
607607
struct unix_sock *u;
608608

609-
atomic_inc(&unix_nr_socks);
610-
if (atomic_read(&unix_nr_socks) > 2 * get_max_files())
609+
atomic_long_inc(&unix_nr_socks);
610+
if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
611611
goto out;
612612

613613
sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto);
@@ -632,7 +632,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock)
632632
unix_insert_socket(unix_sockets_unbound, sk);
633633
out:
634634
if (sk == NULL)
635-
atomic_dec(&unix_nr_socks);
635+
atomic_long_dec(&unix_nr_socks);
636636
else {
637637
local_bh_disable();
638638
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);

0 commit comments

Comments
 (0)