174bd59bbSPavel Emelyanov /* 274bd59bbSPavel Emelyanov * Pid namespaces 374bd59bbSPavel Emelyanov * 474bd59bbSPavel Emelyanov * Authors: 574bd59bbSPavel Emelyanov * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc. 674bd59bbSPavel Emelyanov * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM 774bd59bbSPavel Emelyanov * Many thanks to Oleg Nesterov for comments and help 874bd59bbSPavel Emelyanov * 974bd59bbSPavel Emelyanov */ 1074bd59bbSPavel Emelyanov 1174bd59bbSPavel Emelyanov #include <linux/pid.h> 1274bd59bbSPavel Emelyanov #include <linux/pid_namespace.h> 1374bd59bbSPavel Emelyanov #include <linux/syscalls.h> 1474bd59bbSPavel Emelyanov #include <linux/err.h> 150b6b030fSPavel Emelyanov #include <linux/acct.h> 1674bd59bbSPavel Emelyanov 1774bd59bbSPavel Emelyanov #define BITS_PER_PAGE (PAGE_SIZE*8) 1874bd59bbSPavel Emelyanov 1974bd59bbSPavel Emelyanov struct pid_cache { 2074bd59bbSPavel Emelyanov int nr_ids; 2174bd59bbSPavel Emelyanov char name[16]; 2274bd59bbSPavel Emelyanov struct kmem_cache *cachep; 2374bd59bbSPavel Emelyanov struct list_head list; 2474bd59bbSPavel Emelyanov }; 2574bd59bbSPavel Emelyanov 2674bd59bbSPavel Emelyanov static LIST_HEAD(pid_caches_lh); 2774bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex); 2874bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep; 2974bd59bbSPavel Emelyanov 3074bd59bbSPavel Emelyanov /* 3174bd59bbSPavel Emelyanov * creates the kmem cache to allocate pids from. 3274bd59bbSPavel Emelyanov * @nr_ids: the number of numerical ids this pid will have to carry 3374bd59bbSPavel Emelyanov */ 3474bd59bbSPavel Emelyanov 3574bd59bbSPavel Emelyanov static struct kmem_cache *create_pid_cachep(int nr_ids) 3674bd59bbSPavel Emelyanov { 3774bd59bbSPavel Emelyanov struct pid_cache *pcache; 3874bd59bbSPavel Emelyanov struct kmem_cache *cachep; 3974bd59bbSPavel Emelyanov 4074bd59bbSPavel Emelyanov mutex_lock(&pid_caches_mutex); 4174bd59bbSPavel Emelyanov list_for_each_entry(pcache, &pid_caches_lh, list) 4274bd59bbSPavel Emelyanov if (pcache->nr_ids == nr_ids) 4374bd59bbSPavel Emelyanov goto out; 4474bd59bbSPavel Emelyanov 4574bd59bbSPavel Emelyanov pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL); 4674bd59bbSPavel Emelyanov if (pcache == NULL) 4774bd59bbSPavel Emelyanov goto err_alloc; 4874bd59bbSPavel Emelyanov 4974bd59bbSPavel Emelyanov snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids); 5074bd59bbSPavel Emelyanov cachep = kmem_cache_create(pcache->name, 5174bd59bbSPavel Emelyanov sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid), 5274bd59bbSPavel Emelyanov 0, SLAB_HWCACHE_ALIGN, NULL); 5374bd59bbSPavel Emelyanov if (cachep == NULL) 5474bd59bbSPavel Emelyanov goto err_cachep; 5574bd59bbSPavel Emelyanov 5674bd59bbSPavel Emelyanov pcache->nr_ids = nr_ids; 5774bd59bbSPavel Emelyanov pcache->cachep = cachep; 5874bd59bbSPavel Emelyanov list_add(&pcache->list, &pid_caches_lh); 5974bd59bbSPavel Emelyanov out: 6074bd59bbSPavel Emelyanov mutex_unlock(&pid_caches_mutex); 6174bd59bbSPavel Emelyanov return pcache->cachep; 6274bd59bbSPavel Emelyanov 6374bd59bbSPavel Emelyanov err_cachep: 6474bd59bbSPavel Emelyanov kfree(pcache); 6574bd59bbSPavel Emelyanov err_alloc: 6674bd59bbSPavel Emelyanov mutex_unlock(&pid_caches_mutex); 6774bd59bbSPavel Emelyanov return NULL; 6874bd59bbSPavel Emelyanov } 6974bd59bbSPavel Emelyanov 70ed469a63SAlexey Dobriyan static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns) 7174bd59bbSPavel Emelyanov { 7274bd59bbSPavel Emelyanov struct pid_namespace *ns; 73ed469a63SAlexey Dobriyan unsigned int level = parent_pid_ns->level + 1; 7474bd59bbSPavel Emelyanov int i; 7574bd59bbSPavel Emelyanov 7684406c15SPavel Emelyanov ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL); 7774bd59bbSPavel Emelyanov if (ns == NULL) 7874bd59bbSPavel Emelyanov goto out; 7974bd59bbSPavel Emelyanov 8074bd59bbSPavel Emelyanov ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL); 8174bd59bbSPavel Emelyanov if (!ns->pidmap[0].page) 8274bd59bbSPavel Emelyanov goto out_free; 8374bd59bbSPavel Emelyanov 8474bd59bbSPavel Emelyanov ns->pid_cachep = create_pid_cachep(level + 1); 8574bd59bbSPavel Emelyanov if (ns->pid_cachep == NULL) 8674bd59bbSPavel Emelyanov goto out_free_map; 8774bd59bbSPavel Emelyanov 8874bd59bbSPavel Emelyanov kref_init(&ns->kref); 8974bd59bbSPavel Emelyanov ns->level = level; 90ed469a63SAlexey Dobriyan ns->parent = get_pid_ns(parent_pid_ns); 9174bd59bbSPavel Emelyanov 9274bd59bbSPavel Emelyanov set_bit(0, ns->pidmap[0].page); 9374bd59bbSPavel Emelyanov atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1); 9474bd59bbSPavel Emelyanov 9584406c15SPavel Emelyanov for (i = 1; i < PIDMAP_ENTRIES; i++) 9674bd59bbSPavel Emelyanov atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE); 9774bd59bbSPavel Emelyanov 9874bd59bbSPavel Emelyanov return ns; 9974bd59bbSPavel Emelyanov 10074bd59bbSPavel Emelyanov out_free_map: 10174bd59bbSPavel Emelyanov kfree(ns->pidmap[0].page); 10274bd59bbSPavel Emelyanov out_free: 10374bd59bbSPavel Emelyanov kmem_cache_free(pid_ns_cachep, ns); 10474bd59bbSPavel Emelyanov out: 10574bd59bbSPavel Emelyanov return ERR_PTR(-ENOMEM); 10674bd59bbSPavel Emelyanov } 10774bd59bbSPavel Emelyanov 10874bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns) 10974bd59bbSPavel Emelyanov { 11074bd59bbSPavel Emelyanov int i; 11174bd59bbSPavel Emelyanov 11274bd59bbSPavel Emelyanov for (i = 0; i < PIDMAP_ENTRIES; i++) 11374bd59bbSPavel Emelyanov kfree(ns->pidmap[i].page); 11474bd59bbSPavel Emelyanov kmem_cache_free(pid_ns_cachep, ns); 11574bd59bbSPavel Emelyanov } 11674bd59bbSPavel Emelyanov 11774bd59bbSPavel Emelyanov struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns) 11874bd59bbSPavel Emelyanov { 11974bd59bbSPavel Emelyanov if (!(flags & CLONE_NEWPID)) 120*dca4a979SAlexey Dobriyan return get_pid_ns(old_ns); 12174bd59bbSPavel Emelyanov if (flags & CLONE_THREAD) 122*dca4a979SAlexey Dobriyan return ERR_PTR(-EINVAL); 123*dca4a979SAlexey Dobriyan return create_pid_namespace(old_ns); 12474bd59bbSPavel Emelyanov } 12574bd59bbSPavel Emelyanov 12674bd59bbSPavel Emelyanov void free_pid_ns(struct kref *kref) 12774bd59bbSPavel Emelyanov { 12874bd59bbSPavel Emelyanov struct pid_namespace *ns, *parent; 12974bd59bbSPavel Emelyanov 13074bd59bbSPavel Emelyanov ns = container_of(kref, struct pid_namespace, kref); 13174bd59bbSPavel Emelyanov 13274bd59bbSPavel Emelyanov parent = ns->parent; 13374bd59bbSPavel Emelyanov destroy_pid_namespace(ns); 13474bd59bbSPavel Emelyanov 13574bd59bbSPavel Emelyanov if (parent != NULL) 13674bd59bbSPavel Emelyanov put_pid_ns(parent); 13774bd59bbSPavel Emelyanov } 13874bd59bbSPavel Emelyanov 13974bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns) 14074bd59bbSPavel Emelyanov { 14174bd59bbSPavel Emelyanov int nr; 14274bd59bbSPavel Emelyanov int rc; 143e4da026fSSukadev Bhattiprolu struct task_struct *task; 14474bd59bbSPavel Emelyanov 14574bd59bbSPavel Emelyanov /* 14674bd59bbSPavel Emelyanov * The last thread in the cgroup-init thread group is terminating. 14774bd59bbSPavel Emelyanov * Find remaining pid_ts in the namespace, signal and wait for them 14874bd59bbSPavel Emelyanov * to exit. 14974bd59bbSPavel Emelyanov * 15074bd59bbSPavel Emelyanov * Note: This signals each threads in the namespace - even those that 15174bd59bbSPavel Emelyanov * belong to the same thread group, To avoid this, we would have 15274bd59bbSPavel Emelyanov * to walk the entire tasklist looking a processes in this 15374bd59bbSPavel Emelyanov * namespace, but that could be unnecessarily expensive if the 15474bd59bbSPavel Emelyanov * pid namespace has just a few processes. Or we need to 15574bd59bbSPavel Emelyanov * maintain a tasklist for each pid namespace. 15674bd59bbSPavel Emelyanov * 15774bd59bbSPavel Emelyanov */ 15874bd59bbSPavel Emelyanov read_lock(&tasklist_lock); 15974bd59bbSPavel Emelyanov nr = next_pidmap(pid_ns, 1); 16074bd59bbSPavel Emelyanov while (nr > 0) { 161e4da026fSSukadev Bhattiprolu rcu_read_lock(); 162e4da026fSSukadev Bhattiprolu 163e4da026fSSukadev Bhattiprolu /* 164e4da026fSSukadev Bhattiprolu * Use force_sig() since it clears SIGNAL_UNKILLABLE ensuring 165e4da026fSSukadev Bhattiprolu * any nested-container's init processes don't ignore the 166e4da026fSSukadev Bhattiprolu * signal 167e4da026fSSukadev Bhattiprolu */ 168e4da026fSSukadev Bhattiprolu task = pid_task(find_vpid(nr), PIDTYPE_PID); 169e4da026fSSukadev Bhattiprolu if (task) 170e4da026fSSukadev Bhattiprolu force_sig(SIGKILL, task); 171e4da026fSSukadev Bhattiprolu 172e4da026fSSukadev Bhattiprolu rcu_read_unlock(); 173e4da026fSSukadev Bhattiprolu 17474bd59bbSPavel Emelyanov nr = next_pidmap(pid_ns, nr); 17574bd59bbSPavel Emelyanov } 17674bd59bbSPavel Emelyanov read_unlock(&tasklist_lock); 17774bd59bbSPavel Emelyanov 17874bd59bbSPavel Emelyanov do { 17974bd59bbSPavel Emelyanov clear_thread_flag(TIF_SIGPENDING); 18074bd59bbSPavel Emelyanov rc = sys_wait4(-1, NULL, __WALL, NULL); 18174bd59bbSPavel Emelyanov } while (rc != -ECHILD); 18274bd59bbSPavel Emelyanov 1830b6b030fSPavel Emelyanov acct_exit_ns(pid_ns); 18474bd59bbSPavel Emelyanov return; 18574bd59bbSPavel Emelyanov } 18674bd59bbSPavel Emelyanov 18774bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void) 18874bd59bbSPavel Emelyanov { 18974bd59bbSPavel Emelyanov pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC); 19074bd59bbSPavel Emelyanov return 0; 19174bd59bbSPavel Emelyanov } 19274bd59bbSPavel Emelyanov 19374bd59bbSPavel Emelyanov __initcall(pid_namespaces_init); 194