pid_namespace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Pid namespaces
  3. *
  4. * Authors:
  5. * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
  6. * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
  7. * Many thanks to Oleg Nesterov for comments and help
  8. *
  9. */
  10. #include <linux/pid.h>
  11. #include <linux/pid_namespace.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/err.h>
  14. #define BITS_PER_PAGE (PAGE_SIZE*8)
  15. struct pid_cache {
  16. int nr_ids;
  17. char name[16];
  18. struct kmem_cache *cachep;
  19. struct list_head list;
  20. };
  21. static LIST_HEAD(pid_caches_lh);
  22. static DEFINE_MUTEX(pid_caches_mutex);
  23. static struct kmem_cache *pid_ns_cachep;
  24. /*
  25. * creates the kmem cache to allocate pids from.
  26. * @nr_ids: the number of numerical ids this pid will have to carry
  27. */
  28. static struct kmem_cache *create_pid_cachep(int nr_ids)
  29. {
  30. struct pid_cache *pcache;
  31. struct kmem_cache *cachep;
  32. mutex_lock(&pid_caches_mutex);
  33. list_for_each_entry(pcache, &pid_caches_lh, list)
  34. if (pcache->nr_ids == nr_ids)
  35. goto out;
  36. pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
  37. if (pcache == NULL)
  38. goto err_alloc;
  39. snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
  40. cachep = kmem_cache_create(pcache->name,
  41. sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
  42. 0, SLAB_HWCACHE_ALIGN, NULL);
  43. if (cachep == NULL)
  44. goto err_cachep;
  45. pcache->nr_ids = nr_ids;
  46. pcache->cachep = cachep;
  47. list_add(&pcache->list, &pid_caches_lh);
  48. out:
  49. mutex_unlock(&pid_caches_mutex);
  50. return pcache->cachep;
  51. err_cachep:
  52. kfree(pcache);
  53. err_alloc:
  54. mutex_unlock(&pid_caches_mutex);
  55. return NULL;
  56. }
  57. static struct pid_namespace *create_pid_namespace(unsigned int level)
  58. {
  59. struct pid_namespace *ns;
  60. int i;
  61. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  62. if (ns == NULL)
  63. goto out;
  64. ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  65. if (!ns->pidmap[0].page)
  66. goto out_free;
  67. ns->pid_cachep = create_pid_cachep(level + 1);
  68. if (ns->pid_cachep == NULL)
  69. goto out_free_map;
  70. kref_init(&ns->kref);
  71. ns->level = level;
  72. set_bit(0, ns->pidmap[0].page);
  73. atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
  74. for (i = 1; i < PIDMAP_ENTRIES; i++)
  75. atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
  76. return ns;
  77. out_free_map:
  78. kfree(ns->pidmap[0].page);
  79. out_free:
  80. kmem_cache_free(pid_ns_cachep, ns);
  81. out:
  82. return ERR_PTR(-ENOMEM);
  83. }
  84. static void destroy_pid_namespace(struct pid_namespace *ns)
  85. {
  86. int i;
  87. for (i = 0; i < PIDMAP_ENTRIES; i++)
  88. kfree(ns->pidmap[i].page);
  89. kmem_cache_free(pid_ns_cachep, ns);
  90. }
  91. struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
  92. {
  93. struct pid_namespace *new_ns;
  94. BUG_ON(!old_ns);
  95. new_ns = get_pid_ns(old_ns);
  96. if (!(flags & CLONE_NEWPID))
  97. goto out;
  98. new_ns = ERR_PTR(-EINVAL);
  99. if (flags & CLONE_THREAD)
  100. goto out_put;
  101. new_ns = create_pid_namespace(old_ns->level + 1);
  102. if (!IS_ERR(new_ns))
  103. new_ns->parent = get_pid_ns(old_ns);
  104. out_put:
  105. put_pid_ns(old_ns);
  106. out:
  107. return new_ns;
  108. }
  109. void free_pid_ns(struct kref *kref)
  110. {
  111. struct pid_namespace *ns, *parent;
  112. ns = container_of(kref, struct pid_namespace, kref);
  113. parent = ns->parent;
  114. destroy_pid_namespace(ns);
  115. if (parent != NULL)
  116. put_pid_ns(parent);
  117. }
  118. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  119. {
  120. int nr;
  121. int rc;
  122. /*
  123. * The last thread in the cgroup-init thread group is terminating.
  124. * Find remaining pid_ts in the namespace, signal and wait for them
  125. * to exit.
  126. *
  127. * Note: This signals each threads in the namespace - even those that
  128. * belong to the same thread group, To avoid this, we would have
  129. * to walk the entire tasklist looking a processes in this
  130. * namespace, but that could be unnecessarily expensive if the
  131. * pid namespace has just a few processes. Or we need to
  132. * maintain a tasklist for each pid namespace.
  133. *
  134. */
  135. read_lock(&tasklist_lock);
  136. nr = next_pidmap(pid_ns, 1);
  137. while (nr > 0) {
  138. kill_proc_info(SIGKILL, SEND_SIG_PRIV, nr);
  139. nr = next_pidmap(pid_ns, nr);
  140. }
  141. read_unlock(&tasklist_lock);
  142. do {
  143. clear_thread_flag(TIF_SIGPENDING);
  144. rc = sys_wait4(-1, NULL, __WALL, NULL);
  145. } while (rc != -ECHILD);
  146. /* Child reaper for the pid namespace is going away */
  147. pid_ns->child_reaper = NULL;
  148. return;
  149. }
  150. static __init int pid_namespaces_init(void)
  151. {
  152. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  153. return 0;
  154. }
  155. __initcall(pid_namespaces_init);