Skip to content

Commit 8b9ca31

Browse files
heicarstgreghackmann
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 <heiko.carstens@de.ibm.com> Tested-by: David Rientjes <rientjes@google.com> Cc: Ian Kent <raven@themaw.net> Cc: Hendrik Brueckner <brueckner@linux.vnet.ibm.com> Cc: Thorsten Diehl <thorsten.diehl@de.ibm.com> Cc: Andrea Righi <andrea@betterlinux.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Stefan Bader <stefan.bader@canonical.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Conflicts: fs/seq_file.c Change-Id: I009080dd017b020ffd5e812e5b472bdb8349217a
1 parent 40ef955 commit 8b9ca31

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

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,8 +147,8 @@ static int traverse(struct seq_file *m, loff_t offset)
135147

136148
Eoverflow:
137149
m->op->stop(m, p);
138-
kfree(m->buf);
139-
m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
150+
kvfree(m->buf);
151+
m->buf = seq_buf_alloc(m->size <<= 1);
140152
return !m->buf ? -ENOMEM : -EAGAIN;
141153
}
142154

@@ -191,7 +203,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
191203

192204
/* grab buffer if we didn't have one */
193205
if (!m->buf) {
194-
m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
206+
m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
195207
if (!m->buf)
196208
goto Enomem;
197209
}
@@ -231,8 +243,8 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
231243
if (m->count < m->size)
232244
goto Fill;
233245
m->op->stop(m, p);
234-
kfree(m->buf);
235-
m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
246+
kvfree(m->buf);
247+
m->buf = seq_buf_alloc(m->size <<= 1);
236248
if (!m->buf)
237249
goto Enomem;
238250
m->count = 0;
@@ -347,7 +359,7 @@ EXPORT_SYMBOL(seq_lseek);
347359
int seq_release(struct inode *inode, struct file *file)
348360
{
349361
struct seq_file *m = file->private_data;
350-
kfree(m->buf);
362+
kvfree(m->buf);
351363
kfree(m);
352364
return 0;
353365
}
@@ -602,13 +614,13 @@ EXPORT_SYMBOL(single_open);
602614
int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
603615
void *data, size_t size)
604616
{
605-
char *buf = kmalloc(size, GFP_KERNEL);
617+
char *buf = seq_buf_alloc(size);
606618
int ret;
607619
if (!buf)
608620
return -ENOMEM;
609621
ret = single_open(file, show, data);
610622
if (ret) {
611-
kfree(buf);
623+
kvfree(buf);
612624
return ret;
613625
}
614626
((struct seq_file *)file->private_data)->buf = buf;

0 commit comments

Comments
 (0)