Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

pidns: simpler allocation of pid_* caches

Those pid_* caches are created on demand when a process advances to the new
level of pid namespace. Which means pointers are stable, write only and
thus can be packed into an array instead of spreading them over and using
lists(!) to find them.

Both first and subsequent clone/unshare(CLONE_NEWPID) become faster.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>

authored by

Alexey Dobriyan and committed by
Eric W. Biederman
dd206bec 91ab883e

+23 -42
+23 -42
kernel/pid_namespace.c
··· 23 23 #include <linux/sched/signal.h> 24 24 #include <linux/idr.h> 25 25 26 - struct pid_cache { 27 - int nr_ids; 28 - char name[16]; 29 - struct kmem_cache *cachep; 30 - struct list_head list; 31 - }; 32 - 33 - static LIST_HEAD(pid_caches_lh); 34 26 static DEFINE_MUTEX(pid_caches_mutex); 35 27 static struct kmem_cache *pid_ns_cachep; 28 + /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */ 29 + #define MAX_PID_NS_LEVEL 32 30 + /* Write once array, filled from the beginning. */ 31 + static struct kmem_cache *pid_cache[MAX_PID_NS_LEVEL]; 36 32 37 33 /* 38 34 * creates the kmem cache to allocate pids from. 39 - * @nr_ids: the number of numerical ids this pid will have to carry 35 + * @level: pid namespace level 40 36 */ 41 37 42 - static struct kmem_cache *create_pid_cachep(int nr_ids) 38 + static struct kmem_cache *create_pid_cachep(unsigned int level) 43 39 { 44 - struct pid_cache *pcache; 45 - struct kmem_cache *cachep; 40 + /* Level 0 is init_pid_ns.pid_cachep */ 41 + struct kmem_cache **pkc = &pid_cache[level - 1]; 42 + struct kmem_cache *kc; 43 + char name[4 + 10 + 1]; 44 + unsigned int len; 46 45 46 + kc = READ_ONCE(*pkc); 47 + if (kc) 48 + return kc; 49 + 50 + snprintf(name, sizeof(name), "pid_%u", level + 1); 51 + len = sizeof(struct pid) + level * sizeof(struct upid); 47 52 mutex_lock(&pid_caches_mutex); 48 - list_for_each_entry(pcache, &pid_caches_lh, list) 49 - if (pcache->nr_ids == nr_ids) 50 - goto out; 51 - 52 - pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL); 53 - if (pcache == NULL) 54 - goto err_alloc; 55 - 56 - snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids); 57 - cachep = kmem_cache_create(pcache->name, 58 - sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid), 59 - 0, SLAB_HWCACHE_ALIGN, NULL); 60 - if (cachep == NULL) 61 - goto err_cachep; 62 - 63 - pcache->nr_ids = nr_ids; 64 - pcache->cachep = cachep; 65 - list_add(&pcache->list, &pid_caches_lh); 66 - out: 53 + /* Name collision forces to do allocation under mutex. */ 54 + if (!*pkc) 55 + *pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0); 67 56 mutex_unlock(&pid_caches_mutex); 68 - return pcache->cachep; 69 - 70 - err_cachep: 71 - kfree(pcache); 72 - err_alloc: 73 - mutex_unlock(&pid_caches_mutex); 74 - return NULL; 57 + /* current can fail, but someone else can succeed. */ 58 + return READ_ONCE(*pkc); 75 59 } 76 60 77 61 static void proc_cleanup_work(struct work_struct *work) ··· 63 79 struct pid_namespace *ns = container_of(work, struct pid_namespace, proc_work); 64 80 pid_ns_release_proc(ns); 65 81 } 66 - 67 - /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */ 68 - #define MAX_PID_NS_LEVEL 32 69 82 70 83 static struct ucounts *inc_pid_namespaces(struct user_namespace *ns) 71 84 { ··· 100 119 101 120 idr_init(&ns->idr); 102 121 103 - ns->pid_cachep = create_pid_cachep(level + 1); 122 + ns->pid_cachep = create_pid_cachep(level); 104 123 if (ns->pid_cachep == NULL) 105 124 goto out_free_idr; 106 125