aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/pid.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/pid.c')
-rw-r--r--kernel/pid.c208
1 files changed, 18 insertions, 190 deletions
diff --git a/kernel/pid.c b/kernel/pid.c
index 3b30bccdfcdc..477691576b33 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -41,7 +41,6 @@
41static struct hlist_head *pid_hash; 41static struct hlist_head *pid_hash;
42static int pidhash_shift; 42static int pidhash_shift;
43struct pid init_struct_pid = INIT_STRUCT_PID; 43struct pid init_struct_pid = INIT_STRUCT_PID;
44static struct kmem_cache *pid_ns_cachep;
45 44
46int pid_max = PID_MAX_DEFAULT; 45int pid_max = PID_MAX_DEFAULT;
47 46
@@ -112,7 +111,7 @@ EXPORT_SYMBOL(is_container_init);
112 111
113static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock); 112static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
114 113
115static fastcall void free_pidmap(struct pid_namespace *pid_ns, int pid) 114static void free_pidmap(struct pid_namespace *pid_ns, int pid)
116{ 115{
117 struct pidmap *map = pid_ns->pidmap + pid / BITS_PER_PAGE; 116 struct pidmap *map = pid_ns->pidmap + pid / BITS_PER_PAGE;
118 int offset = pid & BITS_PER_PAGE_MASK; 117 int offset = pid & BITS_PER_PAGE_MASK;
@@ -181,7 +180,7 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)
181 return -1; 180 return -1;
182} 181}
183 182
184static int next_pidmap(struct pid_namespace *pid_ns, int last) 183int next_pidmap(struct pid_namespace *pid_ns, int last)
185{ 184{
186 int offset; 185 int offset;
187 struct pidmap *map, *end; 186 struct pidmap *map, *end;
@@ -199,7 +198,7 @@ static int next_pidmap(struct pid_namespace *pid_ns, int last)
199 return -1; 198 return -1;
200} 199}
201 200
202fastcall void put_pid(struct pid *pid) 201void put_pid(struct pid *pid)
203{ 202{
204 struct pid_namespace *ns; 203 struct pid_namespace *ns;
205 204
@@ -221,7 +220,7 @@ static void delayed_put_pid(struct rcu_head *rhp)
221 put_pid(pid); 220 put_pid(pid);
222} 221}
223 222
224fastcall void free_pid(struct pid *pid) 223void free_pid(struct pid *pid)
225{ 224{
226 /* We can be called with write_lock_irq(&tasklist_lock) held */ 225 /* We can be called with write_lock_irq(&tasklist_lock) held */
227 int i; 226 int i;
@@ -287,7 +286,7 @@ out_free:
287 goto out; 286 goto out;
288} 287}
289 288
290struct pid * fastcall find_pid_ns(int nr, struct pid_namespace *ns) 289struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
291{ 290{
292 struct hlist_node *elem; 291 struct hlist_node *elem;
293 struct upid *pnr; 292 struct upid *pnr;
@@ -317,7 +316,7 @@ EXPORT_SYMBOL_GPL(find_pid);
317/* 316/*
318 * attach_pid() must be called with the tasklist_lock write-held. 317 * attach_pid() must be called with the tasklist_lock write-held.
319 */ 318 */
320int fastcall attach_pid(struct task_struct *task, enum pid_type type, 319int attach_pid(struct task_struct *task, enum pid_type type,
321 struct pid *pid) 320 struct pid *pid)
322{ 321{
323 struct pid_link *link; 322 struct pid_link *link;
@@ -329,7 +328,7 @@ int fastcall attach_pid(struct task_struct *task, enum pid_type type,
329 return 0; 328 return 0;
330} 329}
331 330
332void fastcall detach_pid(struct task_struct *task, enum pid_type type) 331void detach_pid(struct task_struct *task, enum pid_type type)
333{ 332{
334 struct pid_link *link; 333 struct pid_link *link;
335 struct pid *pid; 334 struct pid *pid;
@@ -349,7 +348,7 @@ void fastcall detach_pid(struct task_struct *task, enum pid_type type)
349} 348}
350 349
351/* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */ 350/* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
352void fastcall transfer_pid(struct task_struct *old, struct task_struct *new, 351void transfer_pid(struct task_struct *old, struct task_struct *new,
353 enum pid_type type) 352 enum pid_type type)
354{ 353{
355 new->pids[type].pid = old->pids[type].pid; 354 new->pids[type].pid = old->pids[type].pid;
@@ -357,7 +356,7 @@ void fastcall transfer_pid(struct task_struct *old, struct task_struct *new,
357 old->pids[type].pid = NULL; 356 old->pids[type].pid = NULL;
358} 357}
359 358
360struct task_struct * fastcall pid_task(struct pid *pid, enum pid_type type) 359struct task_struct *pid_task(struct pid *pid, enum pid_type type)
361{ 360{
362 struct task_struct *result = NULL; 361 struct task_struct *result = NULL;
363 if (pid) { 362 if (pid) {
@@ -409,7 +408,7 @@ struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
409 return pid; 408 return pid;
410} 409}
411 410
412struct task_struct *fastcall get_pid_task(struct pid *pid, enum pid_type type) 411struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
413{ 412{
414 struct task_struct *result; 413 struct task_struct *result;
415 rcu_read_lock(); 414 rcu_read_lock();
@@ -444,6 +443,12 @@ pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
444 return nr; 443 return nr;
445} 444}
446 445
446pid_t pid_vnr(struct pid *pid)
447{
448 return pid_nr_ns(pid, current->nsproxy->pid_ns);
449}
450EXPORT_SYMBOL_GPL(pid_vnr);
451
447pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns) 452pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
448{ 453{
449 return pid_nr_ns(task_pid(tsk), ns); 454 return pid_nr_ns(task_pid(tsk), ns);
@@ -488,180 +493,6 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
488} 493}
489EXPORT_SYMBOL_GPL(find_get_pid); 494EXPORT_SYMBOL_GPL(find_get_pid);
490 495
491struct pid_cache {
492 int nr_ids;
493 char name[16];
494 struct kmem_cache *cachep;
495 struct list_head list;
496};
497
498static LIST_HEAD(pid_caches_lh);
499static DEFINE_MUTEX(pid_caches_mutex);
500
501/*
502 * creates the kmem cache to allocate pids from.
503 * @nr_ids: the number of numerical ids this pid will have to carry
504 */
505
506static struct kmem_cache *create_pid_cachep(int nr_ids)
507{
508 struct pid_cache *pcache;
509 struct kmem_cache *cachep;
510
511 mutex_lock(&pid_caches_mutex);
512 list_for_each_entry (pcache, &pid_caches_lh, list)
513 if (pcache->nr_ids == nr_ids)
514 goto out;
515
516 pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
517 if (pcache == NULL)
518 goto err_alloc;
519
520 snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
521 cachep = kmem_cache_create(pcache->name,
522 sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
523 0, SLAB_HWCACHE_ALIGN, NULL);
524 if (cachep == NULL)
525 goto err_cachep;
526
527 pcache->nr_ids = nr_ids;
528 pcache->cachep = cachep;
529 list_add(&pcache->list, &pid_caches_lh);
530out:
531 mutex_unlock(&pid_caches_mutex);
532 return pcache->cachep;
533
534err_cachep:
535 kfree(pcache);
536err_alloc:
537 mutex_unlock(&pid_caches_mutex);
538 return NULL;
539}
540
541#ifdef CONFIG_PID_NS
542static struct pid_namespace *create_pid_namespace(int level)
543{
544 struct pid_namespace *ns;
545 int i;
546
547 ns = kmem_cache_alloc(pid_ns_cachep, GFP_KERNEL);
548 if (ns == NULL)
549 goto out;
550
551 ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
552 if (!ns->pidmap[0].page)
553 goto out_free;
554
555 ns->pid_cachep = create_pid_cachep(level + 1);
556 if (ns->pid_cachep == NULL)
557 goto out_free_map;
558
559 kref_init(&ns->kref);
560 ns->last_pid = 0;
561 ns->child_reaper = NULL;
562 ns->level = level;
563
564 set_bit(0, ns->pidmap[0].page);
565 atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
566
567 for (i = 1; i < PIDMAP_ENTRIES; i++) {
568 ns->pidmap[i].page = 0;
569 atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
570 }
571
572 return ns;
573
574out_free_map:
575 kfree(ns->pidmap[0].page);
576out_free:
577 kmem_cache_free(pid_ns_cachep, ns);
578out:
579 return ERR_PTR(-ENOMEM);
580}
581
582static void destroy_pid_namespace(struct pid_namespace *ns)
583{
584 int i;
585
586 for (i = 0; i < PIDMAP_ENTRIES; i++)
587 kfree(ns->pidmap[i].page);
588 kmem_cache_free(pid_ns_cachep, ns);
589}
590
591struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
592{
593 struct pid_namespace *new_ns;
594
595 BUG_ON(!old_ns);
596 new_ns = get_pid_ns(old_ns);
597 if (!(flags & CLONE_NEWPID))
598 goto out;
599
600 new_ns = ERR_PTR(-EINVAL);
601 if (flags & CLONE_THREAD)
602 goto out_put;
603
604 new_ns = create_pid_namespace(old_ns->level + 1);
605 if (!IS_ERR(new_ns))
606 new_ns->parent = get_pid_ns(old_ns);
607
608out_put:
609 put_pid_ns(old_ns);
610out:
611 return new_ns;
612}
613
614void free_pid_ns(struct kref *kref)
615{
616 struct pid_namespace *ns, *parent;
617
618 ns = container_of(kref, struct pid_namespace, kref);
619
620 parent = ns->parent;
621 destroy_pid_namespace(ns);
622
623 if (parent != NULL)
624 put_pid_ns(parent);
625}
626#endif /* CONFIG_PID_NS */
627
628void zap_pid_ns_processes(struct pid_namespace *pid_ns)
629{
630 int nr;
631 int rc;
632
633 /*
634 * The last thread in the cgroup-init thread group is terminating.
635 * Find remaining pid_ts in the namespace, signal and wait for them
636 * to exit.
637 *
638 * Note: This signals each threads in the namespace - even those that
639 * belong to the same thread group, To avoid this, we would have
640 * to walk the entire tasklist looking a processes in this
641 * namespace, but that could be unnecessarily expensive if the
642 * pid namespace has just a few processes. Or we need to
643 * maintain a tasklist for each pid namespace.
644 *
645 */
646 read_lock(&tasklist_lock);
647 nr = next_pidmap(pid_ns, 1);
648 while (nr > 0) {
649 kill_proc_info(SIGKILL, SEND_SIG_PRIV, nr);
650 nr = next_pidmap(pid_ns, nr);
651 }
652 read_unlock(&tasklist_lock);
653
654 do {
655 clear_thread_flag(TIF_SIGPENDING);
656 rc = sys_wait4(-1, NULL, __WALL, NULL);
657 } while (rc != -ECHILD);
658
659
660 /* Child reaper for the pid namespace is going away */
661 pid_ns->child_reaper = NULL;
662 return;
663}
664
665/* 496/*
666 * The pid hash table is scaled according to the amount of memory in the 497 * The pid hash table is scaled according to the amount of memory in the
667 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or 498 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
@@ -694,9 +525,6 @@ void __init pidmap_init(void)
694 set_bit(0, init_pid_ns.pidmap[0].page); 525 set_bit(0, init_pid_ns.pidmap[0].page);
695 atomic_dec(&init_pid_ns.pidmap[0].nr_free); 526 atomic_dec(&init_pid_ns.pidmap[0].nr_free);
696 527
697 init_pid_ns.pid_cachep = create_pid_cachep(1); 528 init_pid_ns.pid_cachep = KMEM_CACHE(pid,
698 if (init_pid_ns.pid_cachep == NULL) 529 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
699 panic("Can't create pid_1 cachep\n");
700
701 pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
702} 530}