Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 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 | |
| 11 | #include <linux/pid.h> |
| 12 | #include <linux/pid_namespace.h> |
| 13 | #include <linux/syscalls.h> |
| 14 | #include <linux/err.h> |
Pavel Emelyanov | 0b6b030 | 2008-07-25 01:48:47 -0700 | [diff] [blame] | 15 | #include <linux/acct.h> |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 16 | |
| 17 | #define BITS_PER_PAGE (PAGE_SIZE*8) |
| 18 | |
| 19 | struct pid_cache { |
| 20 | int nr_ids; |
| 21 | char name[16]; |
| 22 | struct kmem_cache *cachep; |
| 23 | struct list_head list; |
| 24 | }; |
| 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 | /* |
| 31 | * creates the kmem cache to allocate pids from. |
| 32 | * @nr_ids: the number of numerical ids this pid will have to carry |
| 33 | */ |
| 34 | |
| 35 | static struct kmem_cache *create_pid_cachep(int nr_ids) |
| 36 | { |
| 37 | struct pid_cache *pcache; |
| 38 | struct kmem_cache *cachep; |
| 39 | |
| 40 | mutex_lock(&pid_caches_mutex); |
| 41 | list_for_each_entry(pcache, &pid_caches_lh, list) |
| 42 | if (pcache->nr_ids == nr_ids) |
| 43 | goto out; |
| 44 | |
| 45 | pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL); |
| 46 | if (pcache == NULL) |
| 47 | goto err_alloc; |
| 48 | |
| 49 | snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids); |
| 50 | cachep = kmem_cache_create(pcache->name, |
| 51 | sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid), |
| 52 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 53 | if (cachep == NULL) |
| 54 | goto err_cachep; |
| 55 | |
| 56 | pcache->nr_ids = nr_ids; |
| 57 | pcache->cachep = cachep; |
| 58 | list_add(&pcache->list, &pid_caches_lh); |
| 59 | out: |
| 60 | mutex_unlock(&pid_caches_mutex); |
| 61 | return pcache->cachep; |
| 62 | |
| 63 | err_cachep: |
| 64 | kfree(pcache); |
| 65 | err_alloc: |
| 66 | mutex_unlock(&pid_caches_mutex); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Alexey Dobriyan | ed469a6 | 2009-06-17 16:27:52 -0700 | [diff] [blame] | 70 | static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns) |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 71 | { |
| 72 | struct pid_namespace *ns; |
Alexey Dobriyan | ed469a6 | 2009-06-17 16:27:52 -0700 | [diff] [blame] | 73 | unsigned int level = parent_pid_ns->level + 1; |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 74 | int i; |
| 75 | |
Pavel Emelyanov | 84406c1 | 2008-07-25 01:48:42 -0700 | [diff] [blame] | 76 | ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL); |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 77 | if (ns == NULL) |
| 78 | goto out; |
| 79 | |
| 80 | ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 81 | if (!ns->pidmap[0].page) |
| 82 | goto out_free; |
| 83 | |
| 84 | ns->pid_cachep = create_pid_cachep(level + 1); |
| 85 | if (ns->pid_cachep == NULL) |
| 86 | goto out_free_map; |
| 87 | |
| 88 | kref_init(&ns->kref); |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 89 | ns->level = level; |
Alexey Dobriyan | ed469a6 | 2009-06-17 16:27:52 -0700 | [diff] [blame] | 90 | ns->parent = get_pid_ns(parent_pid_ns); |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 91 | |
| 92 | set_bit(0, ns->pidmap[0].page); |
| 93 | atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1); |
| 94 | |
Pavel Emelyanov | 84406c1 | 2008-07-25 01:48:42 -0700 | [diff] [blame] | 95 | for (i = 1; i < PIDMAP_ENTRIES; i++) |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 96 | atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE); |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 97 | |
| 98 | return ns; |
| 99 | |
| 100 | out_free_map: |
| 101 | kfree(ns->pidmap[0].page); |
| 102 | out_free: |
| 103 | kmem_cache_free(pid_ns_cachep, ns); |
| 104 | out: |
| 105 | return ERR_PTR(-ENOMEM); |
| 106 | } |
| 107 | |
| 108 | static void destroy_pid_namespace(struct pid_namespace *ns) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; i < PIDMAP_ENTRIES; i++) |
| 113 | kfree(ns->pidmap[i].page); |
| 114 | kmem_cache_free(pid_ns_cachep, ns); |
| 115 | } |
| 116 | |
| 117 | struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns) |
| 118 | { |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 119 | if (!(flags & CLONE_NEWPID)) |
Alexey Dobriyan | dca4a97 | 2009-06-17 16:27:53 -0700 | [diff] [blame] | 120 | return get_pid_ns(old_ns); |
Sukadev Bhattiprolu | e5a4738 | 2009-09-23 15:57:22 -0700 | [diff] [blame] | 121 | if (flags & (CLONE_THREAD|CLONE_PARENT)) |
Alexey Dobriyan | dca4a97 | 2009-06-17 16:27:53 -0700 | [diff] [blame] | 122 | return ERR_PTR(-EINVAL); |
| 123 | return create_pid_namespace(old_ns); |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void free_pid_ns(struct kref *kref) |
| 127 | { |
| 128 | struct pid_namespace *ns, *parent; |
| 129 | |
| 130 | ns = container_of(kref, struct pid_namespace, kref); |
| 131 | |
| 132 | parent = ns->parent; |
| 133 | destroy_pid_namespace(ns); |
| 134 | |
| 135 | if (parent != NULL) |
| 136 | put_pid_ns(parent); |
| 137 | } |
| 138 | |
| 139 | void zap_pid_ns_processes(struct pid_namespace *pid_ns) |
| 140 | { |
| 141 | int nr; |
| 142 | int rc; |
Sukadev Bhattiprolu | e4da026 | 2009-04-02 16:58:06 -0700 | [diff] [blame] | 143 | struct task_struct *task; |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 144 | |
| 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) { |
Sukadev Bhattiprolu | e4da026 | 2009-04-02 16:58:06 -0700 | [diff] [blame] | 161 | rcu_read_lock(); |
| 162 | |
| 163 | /* |
| 164 | * Use force_sig() since it clears SIGNAL_UNKILLABLE ensuring |
| 165 | * any nested-container's init processes don't ignore the |
| 166 | * signal |
| 167 | */ |
| 168 | task = pid_task(find_vpid(nr), PIDTYPE_PID); |
| 169 | if (task) |
| 170 | force_sig(SIGKILL, task); |
| 171 | |
| 172 | rcu_read_unlock(); |
| 173 | |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 174 | nr = next_pidmap(pid_ns, nr); |
| 175 | } |
| 176 | read_unlock(&tasklist_lock); |
| 177 | |
| 178 | do { |
| 179 | clear_thread_flag(TIF_SIGPENDING); |
| 180 | rc = sys_wait4(-1, NULL, __WALL, NULL); |
| 181 | } while (rc != -ECHILD); |
| 182 | |
Pavel Emelyanov | 0b6b030 | 2008-07-25 01:48:47 -0700 | [diff] [blame] | 183 | acct_exit_ns(pid_ns); |
Pavel Emelyanov | 74bd59b | 2008-02-08 04:18:24 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | static __init int pid_namespaces_init(void) |
| 188 | { |
| 189 | pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | __initcall(pid_namespaces_init); |