[JFFS2] add write verify on dataflash.
[linux-2.6/verdex.git] / kernel / pid_namespace.c
blob6d792b66d854ff3536e30a1114e35a703be59695
1 /*
2 * Pid namespaces
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
9 */
11 #include <linux/pid.h>
12 #include <linux/pid_namespace.h>
13 #include <linux/syscalls.h>
14 #include <linux/err.h>
16 #define BITS_PER_PAGE (PAGE_SIZE*8)
18 struct pid_cache {
19 int nr_ids;
20 char name[16];
21 struct kmem_cache *cachep;
22 struct list_head list;
25 static LIST_HEAD(pid_caches_lh);
26 static DEFINE_MUTEX(pid_caches_mutex);
27 static struct kmem_cache *pid_ns_cachep;
30 * creates the kmem cache to allocate pids from.
31 * @nr_ids: the number of numerical ids this pid will have to carry
34 static struct kmem_cache *create_pid_cachep(int nr_ids)
36 struct pid_cache *pcache;
37 struct kmem_cache *cachep;
39 mutex_lock(&pid_caches_mutex);
40 list_for_each_entry(pcache, &pid_caches_lh, list)
41 if (pcache->nr_ids == nr_ids)
42 goto out;
44 pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
45 if (pcache == NULL)
46 goto err_alloc;
48 snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
49 cachep = kmem_cache_create(pcache->name,
50 sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
51 0, SLAB_HWCACHE_ALIGN, NULL);
52 if (cachep == NULL)
53 goto err_cachep;
55 pcache->nr_ids = nr_ids;
56 pcache->cachep = cachep;
57 list_add(&pcache->list, &pid_caches_lh);
58 out:
59 mutex_unlock(&pid_caches_mutex);
60 return pcache->cachep;
62 err_cachep:
63 kfree(pcache);
64 err_alloc:
65 mutex_unlock(&pid_caches_mutex);
66 return NULL;
69 static struct pid_namespace *create_pid_namespace(int level)
71 struct pid_namespace *ns;
72 int i;
74 ns = kmem_cache_alloc(pid_ns_cachep, GFP_KERNEL);
75 if (ns == NULL)
76 goto out;
78 ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
79 if (!ns->pidmap[0].page)
80 goto out_free;
82 ns->pid_cachep = create_pid_cachep(level + 1);
83 if (ns->pid_cachep == NULL)
84 goto out_free_map;
86 kref_init(&ns->kref);
87 ns->last_pid = 0;
88 ns->child_reaper = NULL;
89 ns->level = level;
91 set_bit(0, ns->pidmap[0].page);
92 atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
94 for (i = 1; i < PIDMAP_ENTRIES; i++) {
95 ns->pidmap[i].page = 0;
96 atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
99 return ns;
101 out_free_map:
102 kfree(ns->pidmap[0].page);
103 out_free:
104 kmem_cache_free(pid_ns_cachep, ns);
105 out:
106 return ERR_PTR(-ENOMEM);
109 static void destroy_pid_namespace(struct pid_namespace *ns)
111 int i;
113 for (i = 0; i < PIDMAP_ENTRIES; i++)
114 kfree(ns->pidmap[i].page);
115 kmem_cache_free(pid_ns_cachep, ns);
118 struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
120 struct pid_namespace *new_ns;
122 BUG_ON(!old_ns);
123 new_ns = get_pid_ns(old_ns);
124 if (!(flags & CLONE_NEWPID))
125 goto out;
127 new_ns = ERR_PTR(-EINVAL);
128 if (flags & CLONE_THREAD)
129 goto out_put;
131 new_ns = create_pid_namespace(old_ns->level + 1);
132 if (!IS_ERR(new_ns))
133 new_ns->parent = get_pid_ns(old_ns);
135 out_put:
136 put_pid_ns(old_ns);
137 out:
138 return new_ns;
141 void free_pid_ns(struct kref *kref)
143 struct pid_namespace *ns, *parent;
145 ns = container_of(kref, struct pid_namespace, kref);
147 parent = ns->parent;
148 destroy_pid_namespace(ns);
150 if (parent != NULL)
151 put_pid_ns(parent);
154 void zap_pid_ns_processes(struct pid_namespace *pid_ns)
156 int nr;
157 int rc;
160 * The last thread in the cgroup-init thread group is terminating.
161 * Find remaining pid_ts in the namespace, signal and wait for them
162 * to exit.
164 * Note: This signals each threads in the namespace - even those that
165 * belong to the same thread group, To avoid this, we would have
166 * to walk the entire tasklist looking a processes in this
167 * namespace, but that could be unnecessarily expensive if the
168 * pid namespace has just a few processes. Or we need to
169 * maintain a tasklist for each pid namespace.
172 read_lock(&tasklist_lock);
173 nr = next_pidmap(pid_ns, 1);
174 while (nr > 0) {
175 kill_proc_info(SIGKILL, SEND_SIG_PRIV, nr);
176 nr = next_pidmap(pid_ns, nr);
178 read_unlock(&tasklist_lock);
180 do {
181 clear_thread_flag(TIF_SIGPENDING);
182 rc = sys_wait4(-1, NULL, __WALL, NULL);
183 } while (rc != -ECHILD);
186 /* Child reaper for the pid namespace is going away */
187 pid_ns->child_reaper = NULL;
188 return;
191 static __init int pid_namespaces_init(void)
193 pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
194 return 0;
197 __initcall(pid_namespaces_init);