Skip to content

Commit 058504e

Browse files
heicarsttorvalds
authored andcommitted
fs/seq_file: fallback to vmalloc allocation
There are a couple of seq_files which use the single_open() interface. This interface requires that the whole output must fit into a single buffer. E.g. for /proc/stat allocation failures have been observed because an order-4 memory allocation failed due to memory fragmentation. In such situations reading /proc/stat is not possible anymore. Therefore change the seq_file code to fallback to vmalloc allocations which will usually result in a couple of order-0 allocations and hence also work if memory is fragmented. For reference a call trace where reading from /proc/stat failed: sadc: page allocation failure: order:4, mode:0x1040d0 CPU: 1 PID: 192063 Comm: sadc Not tainted 3.10.0-123.el7.s390x #1 [...] Call Trace: show_stack+0x6c/0xe8 warn_alloc_failed+0xd6/0x138 __alloc_pages_nodemask+0x9da/0xb68 __get_free_pages+0x2e/0x58 kmalloc_order_trace+0x44/0xc0 stat_open+0x5a/0xd8 proc_reg_open+0x8a/0x140 do_dentry_open+0x1bc/0x2c8 finish_open+0x46/0x60 do_last+0x382/0x10d0 path_openat+0xc8/0x4f8 do_filp_open+0x46/0xa8 do_sys_open+0x114/0x1f0 sysc_tracego+0x14/0x1a Signed-off-by: Heiko Carstens <[email protected]> Tested-by: David Rientjes <[email protected]> Cc: Ian Kent <[email protected]> Cc: Hendrik Brueckner <[email protected]> Cc: Thorsten Diehl <[email protected]> Cc: Andrea Righi <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Al Viro <[email protected]> Cc: Stefan Bader <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent f74373a commit 058504e

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

fs/seq_file.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include <linux/fs.h>
99
#include <linux/export.h>
1010
#include <linux/seq_file.h>
11+
#include <linux/vmalloc.h>
1112
#include <linux/slab.h>
1213
#include <linux/cred.h>
14+
#include <linux/mm.h>
1315

1416
#include <asm/uaccess.h>
1517
#include <asm/page.h>
@@ -30,6 +32,16 @@ static void seq_set_overflow(struct seq_file *m)
3032
m->count = m->size;
3133
}
3234

35+
static void *seq_buf_alloc(unsigned long size)
36+
{
37+
void *buf;
38+
39+
buf = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
40+
if (!buf && size > PAGE_SIZE)
41+
buf = vmalloc(size);
42+
return buf;
43+
}
44+
3345
/**
3446
* seq_open - initialize sequential file
3547
* @file: file we initialize
@@ -96,7 +108,7 @@ static int traverse(struct seq_file *m, loff_t offset)
96108
return 0;
97109
}
98110
if (!m->buf) {
99-
m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
111+
m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
100112
if (!m->buf)
101113
return -ENOMEM;
102114
}
@@ -135,9 +147,9 @@ static int traverse(struct seq_file *m, loff_t offset)
135147

136148
Eoverflow:
137149
m->op->stop(m, p);
138-
kfree(m->buf);
150+
kvfree(m->buf);
139151
m->count = 0;
140-
m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
152+
m->buf = seq_buf_alloc(m->size <<= 1);
141153
return !m->buf ? -ENOMEM : -EAGAIN;
142154
}
143155

@@ -192,7 +204,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
192204

193205
/* grab buffer if we didn't have one */
194206
if (!m->buf) {
195-
m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
207+
m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
196208
if (!m->buf)
197209
goto Enomem;
198210
}
@@ -232,9 +244,9 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
232244
if (m->count < m->size)
233245
goto Fill;
234246
m->op->stop(m, p);
235-
kfree(m->buf);
247+
kvfree(m->buf);
236248
m->count = 0;
237-
m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
249+
m->buf = seq_buf_alloc(m->size <<= 1);
238250
if (!m->buf)
239251
goto Enomem;
240252
m->version = 0;
@@ -350,7 +362,7 @@ EXPORT_SYMBOL(seq_lseek);
350362
int seq_release(struct inode *inode, struct file *file)
351363
{
352364
struct seq_file *m = file->private_data;
353-
kfree(m->buf);
365+
kvfree(m->buf);
354366
kfree(m);
355367
return 0;
356368
}
@@ -605,13 +617,13 @@ EXPORT_SYMBOL(single_open);
605617
int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
606618
void *data, size_t size)
607619
{
608-
char *buf = kmalloc(size, GFP_KERNEL);
620+
char *buf = seq_buf_alloc(size);
609621
int ret;
610622
if (!buf)
611623
return -ENOMEM;
612624
ret = single_open(file, show, data);
613625
if (ret) {
614-
kfree(buf);
626+
kvfree(buf);
615627
return ret;
616628
}
617629
((struct seq_file *)file->private_data)->buf = buf;

0 commit comments

Comments
 (0)