Skip to content

Commit b4884f2

Browse files
Alexey Dobriyantorvalds
authored andcommitted
proc: move "struct proc_dir_entry" into kmem cache
"struct proc_dir_entry" is variable sized because of 0-length trailing array for name, however, because of SLAB padding allocations it is possible to make "struct proc_dir_entry" fixed sized and allocate same amount of memory. It buys fine-grained debugging with poisoning and usercopy protection which is not possible with kmalloc-* caches. Currently, on 32-bit 91+ byte allocations go into kmalloc-128 and on 64-bit 147+ byte allocations go to kmalloc-192 anyway. Additional memory is allocated only for 38/46+ byte long names which are rare or may not even exist in the wild. Link: http://lkml.kernel.org/r/20180223205504.GA17139@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c4219ed commit b4884f2

5 files changed

Lines changed: 52 additions & 23 deletions

File tree

‎fs/proc/generic.c‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Copyright (C) 1997 Theodore Ts'o
99
*/
1010

11+
#include <linux/cache.h>
1112
#include <linux/errno.h>
1213
#include <linux/time.h>
1314
#include <linux/proc_fs.h>
@@ -28,6 +29,17 @@
2829

2930
static DEFINE_RWLOCK(proc_subdir_lock);
3031

32+
struct kmem_cache *proc_dir_entry_cache __ro_after_init;
33+
34+
void pde_free(struct proc_dir_entry *pde)
35+
{
36+
if (S_ISLNK(pde->mode))
37+
kfree(pde->data);
38+
if (pde->name != pde->inline_name)
39+
kfree(pde->name);
40+
kmem_cache_free(proc_dir_entry_cache, pde);
41+
}
42+
3143
static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
3244
{
3345
if (len < de->namelen)
@@ -363,10 +375,20 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
363375
return NULL;
364376
}
365377

366-
ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
378+
ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
367379
if (!ent)
368380
goto out;
369381

382+
if (qstr.len + 1 <= sizeof(ent->inline_name)) {
383+
ent->name = ent->inline_name;
384+
} else {
385+
ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
386+
if (!ent->name) {
387+
pde_free(ent);
388+
return NULL;
389+
}
390+
}
391+
370392
memcpy(ent->name, fn, qstr.len + 1);
371393
ent->namelen = qstr.len;
372394
ent->mode = mode;
@@ -395,12 +417,11 @@ struct proc_dir_entry *proc_symlink(const char *name,
395417
strcpy((char*)ent->data,dest);
396418
ent->proc_iops = &proc_link_inode_operations;
397419
if (proc_register(parent, ent) < 0) {
398-
kfree(ent->data);
399-
kfree(ent);
420+
pde_free(ent);
400421
ent = NULL;
401422
}
402423
} else {
403-
kfree(ent);
424+
pde_free(ent);
404425
ent = NULL;
405426
}
406427
}
@@ -423,7 +444,7 @@ struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
423444
ent->proc_iops = &proc_dir_inode_operations;
424445
parent->nlink++;
425446
if (proc_register(parent, ent) < 0) {
426-
kfree(ent);
447+
pde_free(ent);
427448
parent->nlink--;
428449
ent = NULL;
429450
}
@@ -458,7 +479,7 @@ struct proc_dir_entry *proc_create_mount_point(const char *name)
458479
ent->proc_iops = NULL;
459480
parent->nlink++;
460481
if (proc_register(parent, ent) < 0) {
461-
kfree(ent);
482+
pde_free(ent);
462483
parent->nlink--;
463484
ent = NULL;
464485
}
@@ -495,7 +516,7 @@ struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
495516
goto out_free;
496517
return pde;
497518
out_free:
498-
kfree(pde);
519+
pde_free(pde);
499520
out:
500521
return NULL;
501522
}
@@ -522,19 +543,12 @@ void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
522543
}
523544
EXPORT_SYMBOL(proc_set_user);
524545

525-
static void free_proc_entry(struct proc_dir_entry *de)
526-
{
527-
proc_free_inum(de->low_ino);
528-
529-
if (S_ISLNK(de->mode))
530-
kfree(de->data);
531-
kfree(de);
532-
}
533-
534546
void pde_put(struct proc_dir_entry *pde)
535547
{
536-
if (atomic_dec_and_test(&pde->count))
537-
free_proc_entry(pde);
548+
if (atomic_dec_and_test(&pde->count)) {
549+
proc_free_inum(pde->low_ino);
550+
pde_free(pde);
551+
}
538552
}
539553

540554
/*

‎fs/proc/inode.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ void __init proc_init_kmemcache(void)
104104
pde_opener_cache =
105105
kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
106106
SLAB_ACCOUNT|SLAB_PANIC, NULL);
107+
proc_dir_entry_cache = kmem_cache_create_usercopy(
108+
"proc_dir_entry", sizeof(struct proc_dir_entry), 0, SLAB_PANIC,
109+
offsetof(struct proc_dir_entry, inline_name),
110+
sizeof_field(struct proc_dir_entry, inline_name), NULL);
107111
}
108112

109113
static int proc_show_options(struct seq_file *seq, struct dentry *root)

‎fs/proc/internal.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,20 @@ struct proc_dir_entry {
5252
struct proc_dir_entry *parent;
5353
struct rb_root_cached subdir;
5454
struct rb_node subdir_node;
55+
char *name;
5556
umode_t mode;
5657
u8 namelen;
57-
char name[];
58+
#ifdef CONFIG_64BIT
59+
#define SIZEOF_PDE_INLINE_NAME (192-147)
60+
#else
61+
#define SIZEOF_PDE_INLINE_NAME (128-91)
62+
#endif
63+
char inline_name[SIZEOF_PDE_INLINE_NAME];
5864
} __randomize_layout;
5965

66+
extern struct kmem_cache *proc_dir_entry_cache;
67+
void pde_free(struct proc_dir_entry *pde);
68+
6069
union proc_op {
6170
int (*proc_get_link)(struct dentry *, struct path *);
6271
int (*proc_show)(struct seq_file *m,

‎fs/proc/proc_net.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static __net_init int proc_net_ns_init(struct net *net)
192192
int err;
193193

194194
err = -ENOMEM;
195-
netd = kzalloc(sizeof(*netd) + 4, GFP_KERNEL);
195+
netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
196196
if (!netd)
197197
goto out;
198198

@@ -201,6 +201,7 @@ static __net_init int proc_net_ns_init(struct net *net)
201201
netd->nlink = 2;
202202
netd->namelen = 3;
203203
netd->parent = &proc_root;
204+
netd->name = netd->inline_name;
204205
memcpy(netd->name, "net", 4);
205206

206207
uid = make_kuid(net->user_ns, 0);
@@ -223,15 +224,15 @@ static __net_init int proc_net_ns_init(struct net *net)
223224
return 0;
224225

225226
free_net:
226-
kfree(netd);
227+
pde_free(netd);
227228
out:
228229
return err;
229230
}
230231

231232
static __net_exit void proc_net_ns_exit(struct net *net)
232233
{
233234
remove_proc_entry("stat", net->proc_net);
234-
kfree(net->proc_net);
235+
pde_free(net->proc_net);
235236
}
236237

237238
static struct pernet_operations __net_initdata proc_net_ns_ops = {

‎fs/proc/root.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ struct proc_dir_entry proc_root = {
208208
.proc_fops = &proc_root_operations,
209209
.parent = &proc_root,
210210
.subdir = RB_ROOT_CACHED,
211-
.name = "/proc",
211+
.name = proc_root.inline_name,
212+
.inline_name = "/proc",
212213
};
213214

214215
int pid_ns_prepare_proc(struct pid_namespace *ns)

0 commit comments

Comments
 (0)