pid_namespace.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include <linux/acct.h>
  15. #include <linux/slab.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/reboot.h>
  18. #include <linux/export.h>
  19. #define BITS_PER_PAGE (PAGE_SIZE*8)
  20. struct pid_cache {
  21. int nr_ids;
  22. char name[16];
  23. struct kmem_cache *cachep;
  24. struct list_head list;
  25. };
  26. static LIST_HEAD(pid_caches_lh);
  27. static DEFINE_MUTEX(pid_caches_mutex);
  28. static struct kmem_cache *pid_ns_cachep;
  29. /*
  30. * creates the kmem cache to allocate pids from.
  31. * @nr_ids: the number of numerical ids this pid will have to carry
  32. */
  33. static struct kmem_cache *create_pid_cachep(int nr_ids)
  34. {
  35. struct pid_cache *pcache;
  36. struct kmem_cache *cachep;
  37. mutex_lock(&pid_caches_mutex);
  38. list_for_each_entry(pcache, &pid_caches_lh, list)
  39. if (pcache->nr_ids == nr_ids)
  40. goto out;
  41. pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
  42. if (pcache == NULL)
  43. goto err_alloc;
  44. snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
  45. cachep = kmem_cache_create(pcache->name,
  46. sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
  47. 0, SLAB_HWCACHE_ALIGN, NULL);
  48. if (cachep == NULL)
  49. goto err_cachep;
  50. pcache->nr_ids = nr_ids;
  51. pcache->cachep = cachep;
  52. list_add(&pcache->list, &pid_caches_lh);
  53. out:
  54. mutex_unlock(&pid_caches_mutex);
  55. return pcache->cachep;
  56. err_cachep:
  57. kfree(pcache);
  58. err_alloc:
  59. mutex_unlock(&pid_caches_mutex);
  60. return NULL;
  61. }
  62. /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
  63. #define MAX_PID_NS_LEVEL 32
  64. static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns)
  65. {
  66. struct pid_namespace *ns;
  67. unsigned int level = parent_pid_ns->level + 1;
  68. int i;
  69. int err;
  70. if (level > MAX_PID_NS_LEVEL) {
  71. err = -EINVAL;
  72. goto out;
  73. }
  74. err = -ENOMEM;
  75. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  76. if (ns == NULL)
  77. goto out;
  78. ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  79. if (!ns->pidmap[0].page)
  80. goto out_free;
  81. ns->pid_cachep = create_pid_cachep(level + 1);
  82. if (ns->pid_cachep == NULL)
  83. goto out_free_map;
  84. kref_init(&ns->kref);
  85. ns->level = level;
  86. ns->parent = get_pid_ns(parent_pid_ns);
  87. set_bit(0, ns->pidmap[0].page);
  88. atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
  89. for (i = 1; i < PIDMAP_ENTRIES; i++)
  90. atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
  91. err = pid_ns_prepare_proc(ns);
  92. if (err)
  93. goto out_put_parent_pid_ns;
  94. return ns;
  95. out_put_parent_pid_ns:
  96. put_pid_ns(parent_pid_ns);
  97. out_free_map:
  98. kfree(ns->pidmap[0].page);
  99. out_free:
  100. kmem_cache_free(pid_ns_cachep, ns);
  101. out:
  102. return ERR_PTR(err);
  103. }
  104. static void destroy_pid_namespace(struct pid_namespace *ns)
  105. {
  106. int i;
  107. for (i = 0; i < PIDMAP_ENTRIES; i++)
  108. kfree(ns->pidmap[i].page);
  109. kmem_cache_free(pid_ns_cachep, ns);
  110. }
  111. struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
  112. {
  113. if (!(flags & CLONE_NEWPID))
  114. return get_pid_ns(old_ns);
  115. if (flags & (CLONE_THREAD|CLONE_PARENT))
  116. return ERR_PTR(-EINVAL);
  117. return create_pid_namespace(old_ns);
  118. }
  119. static void free_pid_ns(struct kref *kref)
  120. {
  121. struct pid_namespace *ns;
  122. ns = container_of(kref, struct pid_namespace, kref);
  123. destroy_pid_namespace(ns);
  124. }
  125. void put_pid_ns(struct pid_namespace *ns)
  126. {
  127. struct pid_namespace *parent;
  128. while (ns != &init_pid_ns) {
  129. parent = ns->parent;
  130. if (!kref_put(&ns->kref, free_pid_ns))
  131. break;
  132. ns = parent;
  133. }
  134. }
  135. EXPORT_SYMBOL_GPL(put_pid_ns);
  136. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  137. {
  138. int nr;
  139. int rc;
  140. struct task_struct *task, *me = current;
  141. /* Ignore SIGCHLD causing any terminated children to autoreap */
  142. spin_lock_irq(&me->sighand->siglock);
  143. me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
  144. spin_unlock_irq(&me->sighand->siglock);
  145. /*
  146. * The last thread in the cgroup-init thread group is terminating.
  147. * Find remaining pid_ts in the namespace, signal and wait for them
  148. * to exit.
  149. *
  150. * Note: This signals each threads in the namespace - even those that
  151. * belong to the same thread group, To avoid this, we would have
  152. * to walk the entire tasklist looking a processes in this
  153. * namespace, but that could be unnecessarily expensive if the
  154. * pid namespace has just a few processes. Or we need to
  155. * maintain a tasklist for each pid namespace.
  156. *
  157. */
  158. read_lock(&tasklist_lock);
  159. nr = next_pidmap(pid_ns, 1);
  160. while (nr > 0) {
  161. rcu_read_lock();
  162. task = pid_task(find_vpid(nr), PIDTYPE_PID);
  163. if (task && !__fatal_signal_pending(task))
  164. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  165. rcu_read_unlock();
  166. nr = next_pidmap(pid_ns, nr);
  167. }
  168. read_unlock(&tasklist_lock);
  169. /* Firstly reap the EXIT_ZOMBIE children we may have. */
  170. do {
  171. clear_thread_flag(TIF_SIGPENDING);
  172. rc = sys_wait4(-1, NULL, __WALL, NULL);
  173. } while (rc != -ECHILD);
  174. /*
  175. * sys_wait4() above can't reap the TASK_DEAD children.
  176. * Make sure they all go away, see __unhash_process().
  177. */
  178. for (;;) {
  179. bool need_wait = false;
  180. read_lock(&tasklist_lock);
  181. if (!list_empty(&current->children)) {
  182. __set_current_state(TASK_UNINTERRUPTIBLE);
  183. need_wait = true;
  184. }
  185. read_unlock(&tasklist_lock);
  186. if (!need_wait)
  187. break;
  188. schedule();
  189. }
  190. if (pid_ns->reboot)
  191. current->signal->group_exit_code = pid_ns->reboot;
  192. acct_exit_ns(pid_ns);
  193. return;
  194. }
  195. #ifdef CONFIG_CHECKPOINT_RESTORE
  196. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  197. void __user *buffer, size_t *lenp, loff_t *ppos)
  198. {
  199. struct ctl_table tmp = *table;
  200. if (write && !capable(CAP_SYS_ADMIN))
  201. return -EPERM;
  202. /*
  203. * Writing directly to ns' last_pid field is OK, since this field
  204. * is volatile in a living namespace anyway and a code writing to
  205. * it should synchronize its usage with external means.
  206. */
  207. tmp.data = &current->nsproxy->pid_ns->last_pid;
  208. return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  209. }
  210. extern int pid_max;
  211. static int zero = 0;
  212. static struct ctl_table pid_ns_ctl_table[] = {
  213. {
  214. .procname = "ns_last_pid",
  215. .maxlen = sizeof(int),
  216. .mode = 0666, /* permissions are checked in the handler */
  217. .proc_handler = pid_ns_ctl_handler,
  218. .extra1 = &zero,
  219. .extra2 = &pid_max,
  220. },
  221. { }
  222. };
  223. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  224. #endif /* CONFIG_CHECKPOINT_RESTORE */
  225. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  226. {
  227. if (pid_ns == &init_pid_ns)
  228. return 0;
  229. switch (cmd) {
  230. case LINUX_REBOOT_CMD_RESTART2:
  231. case LINUX_REBOOT_CMD_RESTART:
  232. pid_ns->reboot = SIGHUP;
  233. break;
  234. case LINUX_REBOOT_CMD_POWER_OFF:
  235. case LINUX_REBOOT_CMD_HALT:
  236. pid_ns->reboot = SIGINT;
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. read_lock(&tasklist_lock);
  242. force_sig(SIGKILL, pid_ns->child_reaper);
  243. read_unlock(&tasklist_lock);
  244. do_exit(0);
  245. /* Not reached */
  246. return 0;
  247. }
  248. static __init int pid_namespaces_init(void)
  249. {
  250. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  251. #ifdef CONFIG_CHECKPOINT_RESTORE
  252. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  253. #endif
  254. return 0;
  255. }
  256. __initcall(pid_namespaces_init);