OSDN Git Service

seq_file: allocate seq_file from kmem_cache
authorAlexey Dobriyan <adobriyan@gmail.com>
Tue, 10 Apr 2018 23:34:45 +0000 (16:34 -0700)
committer0ranko0P <ranko0p@outlook.com>
Wed, 4 Dec 2019 17:16:45 +0000 (01:16 +0800)
For fine-grained debugging and usercopy protection.

Link: http://lkml.kernel.org/r/20180310085027.GA17121@avx2
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Glauber Costa <glommer@gmail.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/seq_file.c
include/linux/seq_file.h
init/main.c

index 6dc4296..81c2961 100644 (file)
@@ -5,6 +5,7 @@
  * initial implementation -- AV, Oct 2001.
  */
 
+#include <linux/cache.h>
 #include <linux/fs.h>
 #include <linux/export.h>
 #include <linux/seq_file.h>
@@ -18,6 +19,8 @@
 #include <asm/uaccess.h>
 #include <asm/page.h>
 
+static struct kmem_cache *seq_file_cache __ro_after_init;
+
 static void seq_set_overflow(struct seq_file *m)
 {
        m->count = m->size;
@@ -64,7 +67,7 @@ int seq_open(struct file *file, const struct seq_operations *op)
 
        WARN_ON(file->private_data);
 
-       p = kzalloc(sizeof(*p), GFP_KERNEL);
+       p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
@@ -369,7 +372,7 @@ int seq_release(struct inode *inode, struct file *file)
 {
        struct seq_file *m = file->private_data;
        kvfree(m->buf);
-       kfree(m);
+       kmem_cache_free(seq_file_cache, m);
        return 0;
 }
 EXPORT_SYMBOL(seq_release);
@@ -1012,3 +1015,8 @@ seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
        return NULL;
 }
 EXPORT_SYMBOL(seq_hlist_next_percpu);
+
+void __init seq_file_init(void)
+{
+       seq_file_cache = KMEM_CACHE(seq_file, SLAB_PANIC);
+}
index f3d45dd..2d86530 100644 (file)
@@ -220,4 +220,5 @@ extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *hea
 
 extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
 
+void seq_file_init(void);
 #endif
index 91b09de..509bb29 100644 (file)
@@ -658,6 +658,7 @@ asmlinkage __visible void __init start_kernel(void)
        signals_init();
        /* rootfs populating might need page-writeback */
        page_writeback_init();
+       seq_file_init();
        proc_root_init();
        nsfs_init();
        cpuset_init();