Skip to content

Commit baf8f0f

Browse files
xemulLinus Torvalds
authored andcommitted
pid namespaces: dynamic kmem cache allocator for pid namespaces
Add kmem_cache to pid_namespace to allocate pids from. Since both implementations expand the struct pid to carry more numerical values each namespace should have separate cache to store pids of different sizes. Each kmem cache is name "pid_<NR>", where <NR> is the number of numerical ids on the pid. Different namespaces with same level of nesting will have same caches. This patch has two FIXMEs that are to be fixed after we reach the consensus about the struct pid itself. The first one is that the namespace to free the pid from in free_pid() must be taken from pid. Now the init_pid_ns is used. The second FIXME is about the cache allocation. When we do know how long the object will be then we'll have to calculate this size in create_pid_cachep. Right now the sizeof(struct pid) value is used. [akpm@linux-foundation.org: coding-style repair] Signed-off-by: Pavel Emelianov <xemul@openvz.org> Acked-by: Cedric Le Goater <clg@fr.ibm.com> Acked-by: Sukadev Bhattiprolu <sukadev@us.ibm.com> Cc: Kirill Korotaev <dev@openvz.org> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Herbert Poetzl <herbert@13thfloor.at> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a05f7b1 commit baf8f0f

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎include/linux/pid_namespace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct pid_namespace {
2020
struct pidmap pidmap[PIDMAP_ENTRIES];
2121
int last_pid;
2222
struct task_struct *child_reaper;
23+
struct kmem_cache *pid_cachep;
2324
};
2425

2526
extern struct pid_namespace init_pid_ns;

‎kernel/pid.c‎

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
3333
static struct hlist_head *pid_hash;
3434
static int pidhash_shift;
35-
static struct kmem_cache *pid_cachep;
3635
struct pid init_struct_pid = INIT_STRUCT_PID;
3736

3837
int pid_max = PID_MAX_DEFAULT;
@@ -176,11 +175,16 @@ static int next_pidmap(struct pid_namespace *pid_ns, int last)
176175

177176
fastcall void put_pid(struct pid *pid)
178177
{
178+
struct pid_namespace *ns;
179+
179180
if (!pid)
180181
return;
182+
183+
/* FIXME - this must be the namespace this pid lives in */
184+
ns = &init_pid_ns;
181185
if ((atomic_read(&pid->count) == 1) ||
182186
atomic_dec_and_test(&pid->count))
183-
kmem_cache_free(pid_cachep, pid);
187+
kmem_cache_free(ns->pid_cachep, pid);
184188
}
185189
EXPORT_SYMBOL_GPL(put_pid);
186190

@@ -208,12 +212,14 @@ struct pid *alloc_pid(void)
208212
struct pid *pid;
209213
enum pid_type type;
210214
int nr = -1;
215+
struct pid_namespace *ns;
211216

212-
pid = kmem_cache_alloc(pid_cachep, GFP_KERNEL);
217+
ns = current->nsproxy->pid_ns;
218+
pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
213219
if (!pid)
214220
goto out;
215221

216-
nr = alloc_pidmap(current->nsproxy->pid_ns);
222+
nr = alloc_pidmap(ns);
217223
if (nr < 0)
218224
goto out_free;
219225

@@ -230,7 +236,7 @@ struct pid *alloc_pid(void)
230236
return pid;
231237

232238
out_free:
233-
kmem_cache_free(pid_cachep, pid);
239+
kmem_cache_free(ns->pid_cachep, pid);
234240
pid = NULL;
235241
goto out;
236242
}
@@ -365,6 +371,56 @@ struct pid *find_ge_pid(int nr)
365371
}
366372
EXPORT_SYMBOL_GPL(find_get_pid);
367373

374+
struct pid_cache {
375+
int nr_ids;
376+
char name[16];
377+
struct kmem_cache *cachep;
378+
struct list_head list;
379+
};
380+
381+
static LIST_HEAD(pid_caches_lh);
382+
static DEFINE_MUTEX(pid_caches_mutex);
383+
384+
/*
385+
* creates the kmem cache to allocate pids from.
386+
* @nr_ids: the number of numerical ids this pid will have to carry
387+
*/
388+
389+
static struct kmem_cache *create_pid_cachep(int nr_ids)
390+
{
391+
struct pid_cache *pcache;
392+
struct kmem_cache *cachep;
393+
394+
mutex_lock(&pid_caches_mutex);
395+
list_for_each_entry (pcache, &pid_caches_lh, list)
396+
if (pcache->nr_ids == nr_ids)
397+
goto out;
398+
399+
pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
400+
if (pcache == NULL)
401+
goto err_alloc;
402+
403+
snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
404+
cachep = kmem_cache_create(pcache->name,
405+
/* FIXME add numerical ids here */
406+
sizeof(struct pid), 0, SLAB_HWCACHE_ALIGN, NULL);
407+
if (cachep == NULL)
408+
goto err_cachep;
409+
410+
pcache->nr_ids = nr_ids;
411+
pcache->cachep = cachep;
412+
list_add(&pcache->list, &pid_caches_lh);
413+
out:
414+
mutex_unlock(&pid_caches_mutex);
415+
return pcache->cachep;
416+
417+
err_cachep:
418+
kfree(pcache);
419+
err_alloc:
420+
mutex_unlock(&pid_caches_mutex);
421+
return NULL;
422+
}
423+
368424
struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
369425
{
370426
BUG_ON(!old_ns);
@@ -412,5 +468,7 @@ void __init pidmap_init(void)
412468
set_bit(0, init_pid_ns.pidmap[0].page);
413469
atomic_dec(&init_pid_ns.pidmap[0].nr_free);
414470

415-
pid_cachep = KMEM_CACHE(pid, SLAB_PANIC);
471+
init_pid_ns.pid_cachep = create_pid_cachep(1);
472+
if (init_pid_ns.pid_cachep == NULL)
473+
panic("Can't create pid_1 cachep\n");
416474
}

0 commit comments

Comments
 (0)