Skip to content

Commit 6164281

Browse files
xemultorvalds
authored andcommitted
user_ns: improve the user_ns on-the-slab packaging
Currently on 64-bit arch the user_namespace is 2096 and when being kmalloc-ed it resides on a 4k slab wasting 2003 bytes. If we allocate a separate cache for it and reduce the hash size from 128 to 64 chains the packaging becomes *much* better - the struct is 1072 bytes and the hole between is 98 bytes. [akpm@linux-foundation.org: s/__initcall/module_init/] Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Acked-by: Serge E. Hallyn <serge@hallyn.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e020e74 commit 6164281

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎include/linux/user_namespace.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/sched.h>
77
#include <linux/err.h>
88

9-
#define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 8)
9+
#define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 7)
1010
#define UIDHASH_SZ (1 << UIDHASH_BITS)
1111

1212
struct user_namespace {

‎kernel/user_namespace.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <linux/highuid.h>
1313
#include <linux/cred.h>
1414

15+
static struct kmem_cache *user_ns_cachep __read_mostly;
16+
1517
/*
1618
* Create a new user namespace, deriving the creator from the user in the
1719
* passed credentials, and replacing that user with the new root user for the
@@ -26,7 +28,7 @@ int create_user_ns(struct cred *new)
2628
struct user_struct *root_user;
2729
int n;
2830

29-
ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL);
31+
ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
3032
if (!ns)
3133
return -ENOMEM;
3234

@@ -38,7 +40,7 @@ int create_user_ns(struct cred *new)
3840
/* Alloc new root user. */
3941
root_user = alloc_uid(ns, 0);
4042
if (!root_user) {
41-
kfree(ns);
43+
kmem_cache_free(user_ns_cachep, ns);
4244
return -ENOMEM;
4345
}
4446

@@ -71,7 +73,7 @@ static void free_user_ns_work(struct work_struct *work)
7173
struct user_namespace *ns =
7274
container_of(work, struct user_namespace, destroyer);
7375
free_uid(ns->creator);
74-
kfree(ns);
76+
kmem_cache_free(user_ns_cachep, ns);
7577
}
7678

7779
void free_user_ns(struct kref *kref)
@@ -126,3 +128,10 @@ gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t
126128
/* No useful relationship so no mapping */
127129
return overflowgid;
128130
}
131+
132+
static __init int user_namespaces_init(void)
133+
{
134+
user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
135+
return 0;
136+
}
137+
module_init(user_namespaces_init);

0 commit comments

Comments
 (0)