blob: fbc6fc8949b4e718be405f500e977bb10841cf64 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070034#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/rcupdate.h>
36#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070037#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070044#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070045#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070047#include <linux/hash.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070048
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <asm/atomic.h>
50
Paul Menage81a6a5c2007-10-18 23:39:38 -070051static DEFINE_MUTEX(cgroup_mutex);
52
Paul Menageddbcc7e2007-10-18 23:39:30 -070053/* Generate an array of cgroup subsystem pointers */
54#define SUBSYS(_x) &_x ## _subsys,
55
56static struct cgroup_subsys *subsys[] = {
57#include <linux/cgroup_subsys.h>
58};
59
60/*
61 * A cgroupfs_root represents the root of a cgroup hierarchy,
62 * and may be associated with a superblock to form an active
63 * hierarchy
64 */
65struct cgroupfs_root {
66 struct super_block *sb;
67
68 /*
69 * The bitmask of subsystems intended to be attached to this
70 * hierarchy
71 */
72 unsigned long subsys_bits;
73
74 /* The bitmask of subsystems currently attached to this hierarchy */
75 unsigned long actual_subsys_bits;
76
77 /* A list running through the attached subsystems */
78 struct list_head subsys_list;
79
80 /* The root cgroup for this hierarchy */
81 struct cgroup top_cgroup;
82
83 /* Tracks how many cgroups are currently defined in hierarchy.*/
84 int number_of_cgroups;
85
86 /* A list running through the mounted hierarchies */
87 struct list_head root_list;
88
89 /* Hierarchy-specific flags */
90 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070091
92 /* The path to use for release notifications. No locking
93 * between setting and use - so if userspace updates this
94 * while child cgroups exist, you could miss a
95 * notification. We ensure that it's always a valid
96 * NUL-terminated string */
97 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070098};
99
100
101/*
102 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
103 * subsystems that are otherwise unattached - it never has more than a
104 * single cgroup, and all tasks are part of that cgroup.
105 */
106static struct cgroupfs_root rootnode;
107
108/* The list of hierarchy roots */
109
110static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700111static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
113/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
114#define dummytop (&rootnode.top_cgroup)
115
116/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800117 * check for fork/exit handlers to call. This avoids us having to do
118 * extra work in the fork/exit path if none of the subsystems need to
119 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 */
121static int need_forkexit_callback;
Balbir Singhcf475ad2008-04-29 01:00:16 -0700122static int need_mm_owner_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700123
Paul Menageddbcc7e2007-10-18 23:39:30 -0700124/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700125inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700126{
Paul Menagebd89aab2007-10-18 23:40:44 -0700127 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128}
129
130/* bits in struct cgroupfs_root flags field */
131enum {
132 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
133};
134
Adrian Bunke9685a02008-02-07 00:13:46 -0800135static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700136{
137 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700138 (1 << CGRP_RELEASABLE) |
139 (1 << CGRP_NOTIFY_ON_RELEASE);
140 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700141}
142
Adrian Bunke9685a02008-02-07 00:13:46 -0800143static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700144{
Paul Menagebd89aab2007-10-18 23:40:44 -0700145 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700146}
147
Paul Menageddbcc7e2007-10-18 23:39:30 -0700148/*
149 * for_each_subsys() allows you to iterate on each subsystem attached to
150 * an active hierarchy
151 */
152#define for_each_subsys(_root, _ss) \
153list_for_each_entry(_ss, &_root->subsys_list, sibling)
154
155/* for_each_root() allows you to iterate across the active hierarchies */
156#define for_each_root(_root) \
157list_for_each_entry(_root, &roots, root_list)
158
Paul Menage81a6a5c2007-10-18 23:39:38 -0700159/* the list of cgroups eligible for automatic release. Protected by
160 * release_list_lock */
161static LIST_HEAD(release_list);
162static DEFINE_SPINLOCK(release_list_lock);
163static void cgroup_release_agent(struct work_struct *work);
164static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700165static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700166
Paul Menage817929e2007-10-18 23:39:36 -0700167/* Link structure for associating css_set objects with cgroups */
168struct cg_cgroup_link {
169 /*
170 * List running through cg_cgroup_links associated with a
171 * cgroup, anchored on cgroup->css_sets
172 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700173 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700174 /*
175 * List running through cg_cgroup_links pointing at a
176 * single css_set object, anchored on css_set->cg_links
177 */
178 struct list_head cg_link_list;
179 struct css_set *cg;
180};
181
182/* The default css_set - used by init and its children prior to any
183 * hierarchies being mounted. It contains a pointer to the root state
184 * for each subsystem. Also used to anchor the list of css_sets. Not
185 * reference-counted, to improve performance when child cgroups
186 * haven't been created.
187 */
188
189static struct css_set init_css_set;
190static struct cg_cgroup_link init_css_set_link;
191
192/* css_set_lock protects the list of css_set objects, and the
193 * chain of tasks off each css_set. Nests outside task->alloc_lock
194 * due to cgroup_iter_start() */
195static DEFINE_RWLOCK(css_set_lock);
196static int css_set_count;
197
Li Zefan472b1052008-04-29 01:00:11 -0700198/* hash table for cgroup groups. This improves the performance to
199 * find an existing css_set */
200#define CSS_SET_HASH_BITS 7
201#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
202static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
203
204static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
205{
206 int i;
207 int index;
208 unsigned long tmp = 0UL;
209
210 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
211 tmp += (unsigned long)css[i];
212 tmp = (tmp >> 16) ^ tmp;
213
214 index = hash_long(tmp, CSS_SET_HASH_BITS);
215
216 return &css_set_table[index];
217}
218
Paul Menage817929e2007-10-18 23:39:36 -0700219/* We don't maintain the lists running through each css_set to its
220 * task until after the first call to cgroup_iter_start(). This
221 * reduces the fork()/exit() overhead for people who have cgroups
222 * compiled into their kernel but not actually in use */
223static int use_task_css_set_links;
224
225/* When we create or destroy a css_set, the operation simply
226 * takes/releases a reference count on all the cgroups referenced
227 * by subsystems in this css_set. This can end up multiple-counting
228 * some cgroups, but that's OK - the ref-count is just a
229 * busy/not-busy indicator; ensuring that we only count each cgroup
230 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700231 * subsystems moved between hierarchies while we were doing so.
232 *
233 * Possible TODO: decide at boot time based on the number of
234 * registered subsystems and the number of CPUs or NUMA nodes whether
235 * it's better for performance to ref-count every subsystem, or to
236 * take a global lock and only add one ref count to each hierarchy.
237 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700238
Paul Menage817929e2007-10-18 23:39:36 -0700239/*
240 * unlink a css_set from the list and free it
241 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700242static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700243{
Paul Menage817929e2007-10-18 23:39:36 -0700244 write_lock(&css_set_lock);
Li Zefan472b1052008-04-29 01:00:11 -0700245 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700246 css_set_count--;
247 while (!list_empty(&cg->cg_links)) {
248 struct cg_cgroup_link *link;
249 link = list_entry(cg->cg_links.next,
250 struct cg_cgroup_link, cg_link_list);
251 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700252 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700253 kfree(link);
254 }
255 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256}
257
258static void __release_css_set(struct kref *k, int taskexit)
259{
260 int i;
261 struct css_set *cg = container_of(k, struct css_set, ref);
262
263 unlink_css_set(cg);
264
265 rcu_read_lock();
266 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700267 struct cgroup *cgrp = cg->subsys[i]->cgroup;
268 if (atomic_dec_and_test(&cgrp->count) &&
269 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700270 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700271 set_bit(CGRP_RELEASABLE, &cgrp->flags);
272 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700273 }
274 }
275 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700276 kfree(cg);
277}
278
Paul Menage81a6a5c2007-10-18 23:39:38 -0700279static void release_css_set(struct kref *k)
280{
281 __release_css_set(k, 0);
282}
283
284static void release_css_set_taskexit(struct kref *k)
285{
286 __release_css_set(k, 1);
287}
288
Paul Menage817929e2007-10-18 23:39:36 -0700289/*
290 * refcounted get/put for css_set objects
291 */
292static inline void get_css_set(struct css_set *cg)
293{
294 kref_get(&cg->ref);
295}
296
297static inline void put_css_set(struct css_set *cg)
298{
299 kref_put(&cg->ref, release_css_set);
300}
301
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302static inline void put_css_set_taskexit(struct css_set *cg)
303{
304 kref_put(&cg->ref, release_css_set_taskexit);
305}
306
Paul Menage817929e2007-10-18 23:39:36 -0700307/*
308 * find_existing_css_set() is a helper for
309 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700310 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700311 *
312 * oldcg: the cgroup group that we're using before the cgroup
313 * transition
314 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700315 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700316 *
317 * template: location in which to build the desired set of subsystem
318 * state objects for the new cgroup group
319 */
Paul Menage817929e2007-10-18 23:39:36 -0700320static struct css_set *find_existing_css_set(
321 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700322 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700323 struct cgroup_subsys_state *template[])
324{
325 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700326 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700327 struct hlist_head *hhead;
328 struct hlist_node *node;
329 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700330
331 /* Built the set of subsystem state objects that we want to
332 * see in the new css_set */
333 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800334 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700335 /* Subsystem is in this hierarchy. So we want
336 * the subsystem state from the new
337 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700338 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700339 } else {
340 /* Subsystem is not in this hierarchy, so we
341 * don't want to change the subsystem state */
342 template[i] = oldcg->subsys[i];
343 }
344 }
345
Li Zefan472b1052008-04-29 01:00:11 -0700346 hhead = css_set_hash(template);
347 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700348 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
349 /* All subsystems matched */
350 return cg;
351 }
Li Zefan472b1052008-04-29 01:00:11 -0700352 }
Paul Menage817929e2007-10-18 23:39:36 -0700353
354 /* No existing cgroup group matched */
355 return NULL;
356}
357
358/*
359 * allocate_cg_links() allocates "count" cg_cgroup_link structures
Paul Menagebd89aab2007-10-18 23:40:44 -0700360 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
Paul Menage817929e2007-10-18 23:39:36 -0700361 * success or a negative error
362 */
Paul Menage817929e2007-10-18 23:39:36 -0700363static int allocate_cg_links(int count, struct list_head *tmp)
364{
365 struct cg_cgroup_link *link;
366 int i;
367 INIT_LIST_HEAD(tmp);
368 for (i = 0; i < count; i++) {
369 link = kmalloc(sizeof(*link), GFP_KERNEL);
370 if (!link) {
371 while (!list_empty(tmp)) {
372 link = list_entry(tmp->next,
373 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700374 cgrp_link_list);
375 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700376 kfree(link);
377 }
378 return -ENOMEM;
379 }
Paul Menagebd89aab2007-10-18 23:40:44 -0700380 list_add(&link->cgrp_link_list, tmp);
Paul Menage817929e2007-10-18 23:39:36 -0700381 }
382 return 0;
383}
384
385static void free_cg_links(struct list_head *tmp)
386{
387 while (!list_empty(tmp)) {
388 struct cg_cgroup_link *link;
389 link = list_entry(tmp->next,
390 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700391 cgrp_link_list);
392 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700393 kfree(link);
394 }
395}
396
397/*
398 * find_css_set() takes an existing cgroup group and a
399 * cgroup object, and returns a css_set object that's
400 * equivalent to the old group, but with the given cgroup
401 * substituted into the appropriate hierarchy. Must be called with
402 * cgroup_mutex held
403 */
Paul Menage817929e2007-10-18 23:39:36 -0700404static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700405 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700406{
407 struct css_set *res;
408 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
409 int i;
410
411 struct list_head tmp_cg_links;
412 struct cg_cgroup_link *link;
413
Li Zefan472b1052008-04-29 01:00:11 -0700414 struct hlist_head *hhead;
415
Paul Menage817929e2007-10-18 23:39:36 -0700416 /* First see if we already have a cgroup group that matches
417 * the desired set */
418 write_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700419 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700420 if (res)
421 get_css_set(res);
422 write_unlock(&css_set_lock);
423
424 if (res)
425 return res;
426
427 res = kmalloc(sizeof(*res), GFP_KERNEL);
428 if (!res)
429 return NULL;
430
431 /* Allocate all the cg_cgroup_link objects that we'll need */
432 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
433 kfree(res);
434 return NULL;
435 }
436
437 kref_init(&res->ref);
438 INIT_LIST_HEAD(&res->cg_links);
439 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700440 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700441
442 /* Copy the set of subsystem state objects generated in
443 * find_existing_css_set() */
444 memcpy(res->subsys, template, sizeof(res->subsys));
445
446 write_lock(&css_set_lock);
447 /* Add reference counts and links from the new css_set. */
448 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700449 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700450 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700451 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700452 /*
453 * We want to add a link once per cgroup, so we
454 * only do it for the first subsystem in each
455 * hierarchy
456 */
457 if (ss->root->subsys_list.next == &ss->sibling) {
458 BUG_ON(list_empty(&tmp_cg_links));
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700461 cgrp_link_list);
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700464 link->cg = res;
465 list_add(&link->cg_link_list, &res->cg_links);
466 }
467 }
468 if (list_empty(&rootnode.subsys_list)) {
469 link = list_entry(tmp_cg_links.next,
470 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700471 cgrp_link_list);
472 list_del(&link->cgrp_link_list);
473 list_add(&link->cgrp_link_list, &dummytop->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700474 link->cg = res;
475 list_add(&link->cg_link_list, &res->cg_links);
476 }
477
478 BUG_ON(!list_empty(&tmp_cg_links));
479
Paul Menage817929e2007-10-18 23:39:36 -0700480 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700481
482 /* Add this cgroup group to the hash table */
483 hhead = css_set_hash(res->subsys);
484 hlist_add_head(&res->hlist, hhead);
485
Paul Menage817929e2007-10-18 23:39:36 -0700486 write_unlock(&css_set_lock);
487
488 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700489}
490
Paul Menageddbcc7e2007-10-18 23:39:30 -0700491/*
492 * There is one global cgroup mutex. We also require taking
493 * task_lock() when dereferencing a task's cgroup subsys pointers.
494 * See "The task_lock() exception", at the end of this comment.
495 *
496 * A task must hold cgroup_mutex to modify cgroups.
497 *
498 * Any task can increment and decrement the count field without lock.
499 * So in general, code holding cgroup_mutex can't rely on the count
500 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800501 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700502 * means that no tasks are currently attached, therefore there is no
503 * way a task attached to that cgroup can fork (the other way to
504 * increment the count). So code holding cgroup_mutex can safely
505 * assume that if the count is zero, it will stay zero. Similarly, if
506 * a task holds cgroup_mutex on a cgroup with zero count, it
507 * knows that the cgroup won't be removed, as cgroup_rmdir()
508 * needs that mutex.
509 *
510 * The cgroup_common_file_write handler for operations that modify
511 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
512 * single threading all such cgroup modifications across the system.
513 *
514 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
515 * (usually) take cgroup_mutex. These are the two most performance
516 * critical pieces of code here. The exception occurs on cgroup_exit(),
517 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
518 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800519 * to the release agent with the name of the cgroup (path relative to
520 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700521 *
522 * A cgroup can only be deleted if both its 'count' of using tasks
523 * is zero, and its list of 'children' cgroups is empty. Since all
524 * tasks in the system use _some_ cgroup, and since there is always at
525 * least one task in the system (init, pid == 1), therefore, top_cgroup
526 * always has either children cgroups and/or using tasks. So we don't
527 * need a special hack to ensure that top_cgroup cannot be deleted.
528 *
529 * The task_lock() exception
530 *
531 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800532 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800533 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700534 * several performance critical places that need to reference
535 * task->cgroup without the expense of grabbing a system global
536 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800537 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700538 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
539 * the task_struct routinely used for such matters.
540 *
541 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800542 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700543 */
544
Paul Menageddbcc7e2007-10-18 23:39:30 -0700545/**
546 * cgroup_lock - lock out any changes to cgroup structures
547 *
548 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700549void cgroup_lock(void)
550{
551 mutex_lock(&cgroup_mutex);
552}
553
554/**
555 * cgroup_unlock - release lock on cgroup changes
556 *
557 * Undo the lock taken in a previous cgroup_lock() call.
558 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700559void cgroup_unlock(void)
560{
561 mutex_unlock(&cgroup_mutex);
562}
563
564/*
565 * A couple of forward declarations required, due to cyclic reference loop:
566 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
567 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
568 * -> cgroup_mkdir.
569 */
570
571static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
572static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700573static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700574static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700575static struct file_operations proc_cgroupstats_operations;
576
577static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700578 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700579};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700580
581static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
582{
583 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700584
585 if (inode) {
586 inode->i_mode = mode;
587 inode->i_uid = current->fsuid;
588 inode->i_gid = current->fsgid;
589 inode->i_blocks = 0;
590 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
591 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
592 }
593 return inode;
594}
595
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800596/*
597 * Call subsys's pre_destroy handler.
598 * This is called before css refcnt check.
599 */
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800600static void cgroup_call_pre_destroy(struct cgroup *cgrp)
601{
602 struct cgroup_subsys *ss;
603 for_each_subsys(cgrp->root, ss)
604 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
605 ss->pre_destroy(ss, cgrp);
606 return;
607}
608
Paul Menageddbcc7e2007-10-18 23:39:30 -0700609static void cgroup_diput(struct dentry *dentry, struct inode *inode)
610{
611 /* is dentry a directory ? if so, kfree() associated cgroup */
612 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700613 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800614 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700615 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700616 /* It's possible for external users to be holding css
617 * reference counts on a cgroup; css_put() needs to
618 * be able to access the cgroup after decrementing
619 * the reference count in order to know if it needs to
620 * queue the cgroup to be handled by the release
621 * agent */
622 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800623
624 mutex_lock(&cgroup_mutex);
625 /*
626 * Release the subsystem state objects.
627 */
628 for_each_subsys(cgrp->root, ss) {
629 if (cgrp->subsys[ss->subsys_id])
630 ss->destroy(ss, cgrp);
631 }
632
633 cgrp->root->number_of_cgroups--;
634 mutex_unlock(&cgroup_mutex);
635
636 /* Drop the active superblock reference that we took when we
637 * created the cgroup */
638 deactivate_super(cgrp->root->sb);
639
Paul Menagebd89aab2007-10-18 23:40:44 -0700640 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700641 }
642 iput(inode);
643}
644
645static void remove_dir(struct dentry *d)
646{
647 struct dentry *parent = dget(d->d_parent);
648
649 d_delete(d);
650 simple_rmdir(parent->d_inode, d);
651 dput(parent);
652}
653
654static void cgroup_clear_directory(struct dentry *dentry)
655{
656 struct list_head *node;
657
658 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
659 spin_lock(&dcache_lock);
660 node = dentry->d_subdirs.next;
661 while (node != &dentry->d_subdirs) {
662 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
663 list_del_init(node);
664 if (d->d_inode) {
665 /* This should never be called on a cgroup
666 * directory with child cgroups */
667 BUG_ON(d->d_inode->i_mode & S_IFDIR);
668 d = dget_locked(d);
669 spin_unlock(&dcache_lock);
670 d_delete(d);
671 simple_unlink(dentry->d_inode, d);
672 dput(d);
673 spin_lock(&dcache_lock);
674 }
675 node = dentry->d_subdirs.next;
676 }
677 spin_unlock(&dcache_lock);
678}
679
680/*
681 * NOTE : the dentry must have been dget()'ed
682 */
683static void cgroup_d_remove_dir(struct dentry *dentry)
684{
685 cgroup_clear_directory(dentry);
686
687 spin_lock(&dcache_lock);
688 list_del_init(&dentry->d_u.d_child);
689 spin_unlock(&dcache_lock);
690 remove_dir(dentry);
691}
692
693static int rebind_subsystems(struct cgroupfs_root *root,
694 unsigned long final_bits)
695{
696 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700697 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700698 int i;
699
700 removed_bits = root->actual_subsys_bits & ~final_bits;
701 added_bits = final_bits & ~root->actual_subsys_bits;
702 /* Check that any added subsystems are currently free */
703 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800704 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700705 struct cgroup_subsys *ss = subsys[i];
706 if (!(bit & added_bits))
707 continue;
708 if (ss->root != &rootnode) {
709 /* Subsystem isn't free */
710 return -EBUSY;
711 }
712 }
713
714 /* Currently we don't handle adding/removing subsystems when
715 * any child cgroups exist. This is theoretically supportable
716 * but involves complex error handling, so it's being left until
717 * later */
Paul Menagebd89aab2007-10-18 23:40:44 -0700718 if (!list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700719 return -EBUSY;
720
721 /* Process each subsystem */
722 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
723 struct cgroup_subsys *ss = subsys[i];
724 unsigned long bit = 1UL << i;
725 if (bit & added_bits) {
726 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700727 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700728 BUG_ON(!dummytop->subsys[i]);
729 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -0700730 cgrp->subsys[i] = dummytop->subsys[i];
731 cgrp->subsys[i]->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700732 list_add(&ss->sibling, &root->subsys_list);
733 rcu_assign_pointer(ss->root, root);
734 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700735 ss->bind(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700736
737 } else if (bit & removed_bits) {
738 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700739 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
740 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700741 if (ss->bind)
742 ss->bind(ss, dummytop);
743 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700744 cgrp->subsys[i] = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700745 rcu_assign_pointer(subsys[i]->root, &rootnode);
746 list_del(&ss->sibling);
747 } else if (bit & final_bits) {
748 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700749 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 } else {
751 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700752 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753 }
754 }
755 root->subsys_bits = root->actual_subsys_bits = final_bits;
756 synchronize_rcu();
757
758 return 0;
759}
760
761static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
762{
763 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
764 struct cgroup_subsys *ss;
765
766 mutex_lock(&cgroup_mutex);
767 for_each_subsys(root, ss)
768 seq_printf(seq, ",%s", ss->name);
769 if (test_bit(ROOT_NOPREFIX, &root->flags))
770 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700771 if (strlen(root->release_agent_path))
772 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 mutex_unlock(&cgroup_mutex);
774 return 0;
775}
776
777struct cgroup_sb_opts {
778 unsigned long subsys_bits;
779 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700780 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781};
782
783/* Convert a hierarchy specifier into a bitmask of subsystems and
784 * flags. */
785static int parse_cgroupfs_options(char *data,
786 struct cgroup_sb_opts *opts)
787{
788 char *token, *o = data ?: "all";
789
790 opts->subsys_bits = 0;
791 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700792 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793
794 while ((token = strsep(&o, ",")) != NULL) {
795 if (!*token)
796 return -EINVAL;
797 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700798 /* Add all non-disabled subsystems */
799 int i;
800 opts->subsys_bits = 0;
801 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
802 struct cgroup_subsys *ss = subsys[i];
803 if (!ss->disabled)
804 opts->subsys_bits |= 1ul << i;
805 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 } else if (!strcmp(token, "noprefix")) {
807 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700808 } else if (!strncmp(token, "release_agent=", 14)) {
809 /* Specifying two release agents is forbidden */
810 if (opts->release_agent)
811 return -EINVAL;
812 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
813 if (!opts->release_agent)
814 return -ENOMEM;
815 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
816 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817 } else {
818 struct cgroup_subsys *ss;
819 int i;
820 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
821 ss = subsys[i];
822 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700823 if (!ss->disabled)
824 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825 break;
826 }
827 }
828 if (i == CGROUP_SUBSYS_COUNT)
829 return -ENOENT;
830 }
831 }
832
833 /* We can't have an empty hierarchy */
834 if (!opts->subsys_bits)
835 return -EINVAL;
836
837 return 0;
838}
839
840static int cgroup_remount(struct super_block *sb, int *flags, char *data)
841{
842 int ret = 0;
843 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700844 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845 struct cgroup_sb_opts opts;
846
Paul Menagebd89aab2007-10-18 23:40:44 -0700847 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 mutex_lock(&cgroup_mutex);
849
850 /* See what subsystems are wanted */
851 ret = parse_cgroupfs_options(data, &opts);
852 if (ret)
853 goto out_unlock;
854
855 /* Don't allow flags to change at remount */
856 if (opts.flags != root->flags) {
857 ret = -EINVAL;
858 goto out_unlock;
859 }
860
861 ret = rebind_subsystems(root, opts.subsys_bits);
862
863 /* (re)populate subsystem files */
864 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700865 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700866
Paul Menage81a6a5c2007-10-18 23:39:38 -0700867 if (opts.release_agent)
868 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700869 out_unlock:
Paul Menage81a6a5c2007-10-18 23:39:38 -0700870 if (opts.release_agent)
871 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700872 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700873 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700874 return ret;
875}
876
877static struct super_operations cgroup_ops = {
878 .statfs = simple_statfs,
879 .drop_inode = generic_delete_inode,
880 .show_options = cgroup_show_options,
881 .remount_fs = cgroup_remount,
882};
883
884static void init_cgroup_root(struct cgroupfs_root *root)
885{
Paul Menagebd89aab2007-10-18 23:40:44 -0700886 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700887 INIT_LIST_HEAD(&root->subsys_list);
888 INIT_LIST_HEAD(&root->root_list);
889 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700890 cgrp->root = root;
891 cgrp->top_cgroup = cgrp;
892 INIT_LIST_HEAD(&cgrp->sibling);
893 INIT_LIST_HEAD(&cgrp->children);
894 INIT_LIST_HEAD(&cgrp->css_sets);
895 INIT_LIST_HEAD(&cgrp->release_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896}
897
898static int cgroup_test_super(struct super_block *sb, void *data)
899{
900 struct cgroupfs_root *new = data;
901 struct cgroupfs_root *root = sb->s_fs_info;
902
903 /* First check subsystems */
904 if (new->subsys_bits != root->subsys_bits)
905 return 0;
906
907 /* Next check flags */
908 if (new->flags != root->flags)
909 return 0;
910
911 return 1;
912}
913
914static int cgroup_set_super(struct super_block *sb, void *data)
915{
916 int ret;
917 struct cgroupfs_root *root = data;
918
919 ret = set_anon_super(sb, NULL);
920 if (ret)
921 return ret;
922
923 sb->s_fs_info = root;
924 root->sb = sb;
925
926 sb->s_blocksize = PAGE_CACHE_SIZE;
927 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
928 sb->s_magic = CGROUP_SUPER_MAGIC;
929 sb->s_op = &cgroup_ops;
930
931 return 0;
932}
933
934static int cgroup_get_rootdir(struct super_block *sb)
935{
936 struct inode *inode =
937 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
938 struct dentry *dentry;
939
940 if (!inode)
941 return -ENOMEM;
942
Paul Menageddbcc7e2007-10-18 23:39:30 -0700943 inode->i_fop = &simple_dir_operations;
944 inode->i_op = &cgroup_dir_inode_operations;
945 /* directories start off with i_nlink == 2 (for "." entry) */
946 inc_nlink(inode);
947 dentry = d_alloc_root(inode);
948 if (!dentry) {
949 iput(inode);
950 return -ENOMEM;
951 }
952 sb->s_root = dentry;
953 return 0;
954}
955
956static int cgroup_get_sb(struct file_system_type *fs_type,
957 int flags, const char *unused_dev_name,
958 void *data, struct vfsmount *mnt)
959{
960 struct cgroup_sb_opts opts;
961 int ret = 0;
962 struct super_block *sb;
963 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -0700964 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700965 INIT_LIST_HEAD(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966
967 /* First find the desired set of subsystems */
968 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700969 if (ret) {
970 if (opts.release_agent)
971 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700973 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974
975 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -0800976 if (!root) {
977 if (opts.release_agent)
978 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -0800980 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981
982 init_cgroup_root(root);
983 root->subsys_bits = opts.subsys_bits;
984 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700985 if (opts.release_agent) {
986 strcpy(root->release_agent_path, opts.release_agent);
987 kfree(opts.release_agent);
988 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989
990 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
991
992 if (IS_ERR(sb)) {
993 kfree(root);
994 return PTR_ERR(sb);
995 }
996
997 if (sb->s_fs_info != root) {
998 /* Reusing an existing superblock */
999 BUG_ON(sb->s_root == NULL);
1000 kfree(root);
1001 root = NULL;
1002 } else {
1003 /* New superblock */
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 struct cgroup *cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001005 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001006 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007
1008 BUG_ON(sb->s_root != NULL);
1009
1010 ret = cgroup_get_rootdir(sb);
1011 if (ret)
1012 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001013 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014
Paul Menage817929e2007-10-18 23:39:36 -07001015 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 mutex_lock(&cgroup_mutex);
1017
Paul Menage817929e2007-10-18 23:39:36 -07001018 /*
1019 * We're accessing css_set_count without locking
1020 * css_set_lock here, but that's OK - it can only be
1021 * increased by someone holding cgroup_lock, and
1022 * that's us. The worst that can happen is that we
1023 * have some link structures left over
1024 */
1025 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1026 if (ret) {
1027 mutex_unlock(&cgroup_mutex);
1028 mutex_unlock(&inode->i_mutex);
1029 goto drop_new_super;
1030 }
1031
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 ret = rebind_subsystems(root, root->subsys_bits);
1033 if (ret == -EBUSY) {
1034 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001035 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 goto drop_new_super;
1037 }
1038
1039 /* EBUSY should be the only error here */
1040 BUG_ON(ret);
1041
1042 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001043 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044
1045 sb->s_root->d_fsdata = &root->top_cgroup;
1046 root->top_cgroup.dentry = sb->s_root;
1047
Paul Menage817929e2007-10-18 23:39:36 -07001048 /* Link the top cgroup in this hierarchy into all
1049 * the css_set objects */
1050 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001051 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1052 struct hlist_head *hhead = &css_set_table[i];
1053 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001054 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001055
1056 hlist_for_each_entry(cg, node, hhead, hlist) {
1057 struct cg_cgroup_link *link;
1058
1059 BUG_ON(list_empty(&tmp_cg_links));
1060 link = list_entry(tmp_cg_links.next,
1061 struct cg_cgroup_link,
1062 cgrp_link_list);
1063 list_del(&link->cgrp_link_list);
1064 link->cg = cg;
1065 list_add(&link->cgrp_link_list,
1066 &root->top_cgroup.css_sets);
1067 list_add(&link->cg_link_list, &cg->cg_links);
1068 }
1069 }
Paul Menage817929e2007-10-18 23:39:36 -07001070 write_unlock(&css_set_lock);
1071
1072 free_cg_links(&tmp_cg_links);
1073
Paul Menagebd89aab2007-10-18 23:40:44 -07001074 BUG_ON(!list_empty(&cgrp->sibling));
1075 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 BUG_ON(root->number_of_cgroups != 1);
1077
Paul Menagebd89aab2007-10-18 23:40:44 -07001078 cgroup_populate_dir(cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001079 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 mutex_unlock(&cgroup_mutex);
1081 }
1082
1083 return simple_set_mnt(mnt, sb);
1084
1085 drop_new_super:
1086 up_write(&sb->s_umount);
1087 deactivate_super(sb);
Paul Menage817929e2007-10-18 23:39:36 -07001088 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 return ret;
1090}
1091
1092static void cgroup_kill_sb(struct super_block *sb) {
1093 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001094 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 int ret;
1096
1097 BUG_ON(!root);
1098
1099 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001100 BUG_ON(!list_empty(&cgrp->children));
1101 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102
1103 mutex_lock(&cgroup_mutex);
1104
1105 /* Rebind all subsystems back to the default hierarchy */
1106 ret = rebind_subsystems(root, 0);
1107 /* Shouldn't be able to fail ... */
1108 BUG_ON(ret);
1109
Paul Menage817929e2007-10-18 23:39:36 -07001110 /*
1111 * Release all the links from css_sets to this hierarchy's
1112 * root cgroup
1113 */
1114 write_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001115 while (!list_empty(&cgrp->css_sets)) {
Paul Menage817929e2007-10-18 23:39:36 -07001116 struct cg_cgroup_link *link;
Paul Menagebd89aab2007-10-18 23:40:44 -07001117 link = list_entry(cgrp->css_sets.next,
1118 struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001119 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001120 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001121 kfree(link);
1122 }
1123 write_unlock(&css_set_lock);
1124
1125 if (!list_empty(&root->root_list)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126 list_del(&root->root_list);
Paul Menage817929e2007-10-18 23:39:36 -07001127 root_count--;
1128 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 mutex_unlock(&cgroup_mutex);
1130
1131 kfree(root);
1132 kill_litter_super(sb);
1133}
1134
1135static struct file_system_type cgroup_fs_type = {
1136 .name = "cgroup",
1137 .get_sb = cgroup_get_sb,
1138 .kill_sb = cgroup_kill_sb,
1139};
1140
Paul Menagebd89aab2007-10-18 23:40:44 -07001141static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142{
1143 return dentry->d_fsdata;
1144}
1145
1146static inline struct cftype *__d_cft(struct dentry *dentry)
1147{
1148 return dentry->d_fsdata;
1149}
1150
Li Zefana043e3b2008-02-23 15:24:09 -08001151/**
1152 * cgroup_path - generate the path of a cgroup
1153 * @cgrp: the cgroup in question
1154 * @buf: the buffer to write the path into
1155 * @buflen: the length of the buffer
1156 *
1157 * Called with cgroup_mutex held. Writes path of cgroup into buf.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158 * Returns 0 on success, -errno on error.
1159 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001160int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161{
1162 char *start;
1163
Paul Menagebd89aab2007-10-18 23:40:44 -07001164 if (cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001165 /*
1166 * Inactive subsystems have no dentry for their root
1167 * cgroup
1168 */
1169 strcpy(buf, "/");
1170 return 0;
1171 }
1172
1173 start = buf + buflen;
1174
1175 *--start = '\0';
1176 for (;;) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001177 int len = cgrp->dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001178 if ((start -= len) < buf)
1179 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001180 memcpy(start, cgrp->dentry->d_name.name, len);
1181 cgrp = cgrp->parent;
1182 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001183 break;
Paul Menagebd89aab2007-10-18 23:40:44 -07001184 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001185 continue;
1186 if (--start < buf)
1187 return -ENAMETOOLONG;
1188 *start = '/';
1189 }
1190 memmove(buf, start, buf + buflen - start);
1191 return 0;
1192}
1193
Paul Menagebbcb81d2007-10-18 23:39:32 -07001194/*
1195 * Return the first subsystem attached to a cgroup's hierarchy, and
1196 * its subsystem id.
1197 */
1198
Paul Menagebd89aab2007-10-18 23:40:44 -07001199static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001200 struct cgroup_subsys_state **css, int *subsys_id)
1201{
Paul Menagebd89aab2007-10-18 23:40:44 -07001202 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001203 const struct cgroup_subsys *test_ss;
1204 BUG_ON(list_empty(&root->subsys_list));
1205 test_ss = list_entry(root->subsys_list.next,
1206 struct cgroup_subsys, sibling);
1207 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001208 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001209 BUG_ON(!*css);
1210 }
1211 if (subsys_id)
1212 *subsys_id = test_ss->subsys_id;
1213}
1214
Li Zefana043e3b2008-02-23 15:24:09 -08001215/**
1216 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1217 * @cgrp: the cgroup the task is attaching to
1218 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001219 *
Li Zefana043e3b2008-02-23 15:24:09 -08001220 * Call holding cgroup_mutex. May take task_lock of
1221 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001222 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001223int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001224{
1225 int retval = 0;
1226 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001227 struct cgroup *oldcgrp;
Paul Menage817929e2007-10-18 23:39:36 -07001228 struct css_set *cg = tsk->cgroups;
1229 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001230 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001231 int subsys_id;
1232
Paul Menagebd89aab2007-10-18 23:40:44 -07001233 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001234
1235 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001236 oldcgrp = task_cgroup(tsk, subsys_id);
1237 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001238 return 0;
1239
1240 for_each_subsys(root, ss) {
1241 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001242 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001243 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001244 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001245 }
1246 }
1247
Paul Menage817929e2007-10-18 23:39:36 -07001248 /*
1249 * Locate or allocate a new css_set for this task,
1250 * based on its final set of cgroups
1251 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001252 newcg = find_css_set(cg, cgrp);
Paul Jacksone18f6312008-02-07 00:13:44 -08001253 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001254 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001255
Paul Menagebbcb81d2007-10-18 23:39:32 -07001256 task_lock(tsk);
1257 if (tsk->flags & PF_EXITING) {
1258 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001259 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001260 return -ESRCH;
1261 }
Paul Menage817929e2007-10-18 23:39:36 -07001262 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001263 task_unlock(tsk);
1264
Paul Menage817929e2007-10-18 23:39:36 -07001265 /* Update the css_set linked lists if we're using them */
1266 write_lock(&css_set_lock);
1267 if (!list_empty(&tsk->cg_list)) {
1268 list_del(&tsk->cg_list);
1269 list_add(&tsk->cg_list, &newcg->tasks);
1270 }
1271 write_unlock(&css_set_lock);
1272
Paul Menagebbcb81d2007-10-18 23:39:32 -07001273 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001274 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001275 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001276 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001277 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001278 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001279 put_css_set(cg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001280 return 0;
1281}
1282
1283/*
Paul Menagebd89aab2007-10-18 23:40:44 -07001284 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
Paul Menagebbcb81d2007-10-18 23:39:32 -07001285 * cgroup_mutex, may take task_lock of task
1286 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001287static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001288{
1289 pid_t pid;
1290 struct task_struct *tsk;
1291 int ret;
1292
1293 if (sscanf(pidbuf, "%d", &pid) != 1)
1294 return -EIO;
1295
1296 if (pid) {
1297 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001298 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001299 if (!tsk || tsk->flags & PF_EXITING) {
1300 rcu_read_unlock();
1301 return -ESRCH;
1302 }
1303 get_task_struct(tsk);
1304 rcu_read_unlock();
1305
1306 if ((current->euid) && (current->euid != tsk->uid)
1307 && (current->euid != tsk->suid)) {
1308 put_task_struct(tsk);
1309 return -EACCES;
1310 }
1311 } else {
1312 tsk = current;
1313 get_task_struct(tsk);
1314 }
1315
Cliff Wickman956db3c2008-02-07 00:14:43 -08001316 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001317 put_task_struct(tsk);
1318 return ret;
1319}
1320
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322enum cgroup_filetype {
1323 FILE_ROOT,
1324 FILE_DIR,
1325 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001326 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001327 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328};
1329
Paul Menagee73d2c62008-04-29 01:00:06 -07001330static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001331 struct file *file,
1332 const char __user *userbuf,
1333 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001334{
1335 char buffer[64];
1336 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001337 char *end;
1338
1339 if (!nbytes)
1340 return -EINVAL;
1341 if (nbytes >= sizeof(buffer))
1342 return -E2BIG;
1343 if (copy_from_user(buffer, userbuf, nbytes))
1344 return -EFAULT;
1345
1346 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001347 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001348 if (cft->write_u64) {
1349 u64 val = simple_strtoull(buffer, &end, 0);
1350 if (*end)
1351 return -EINVAL;
1352 retval = cft->write_u64(cgrp, cft, val);
1353 } else {
1354 s64 val = simple_strtoll(buffer, &end, 0);
1355 if (*end)
1356 return -EINVAL;
1357 retval = cft->write_s64(cgrp, cft, val);
1358 }
Paul Menage355e0c42007-10-18 23:39:33 -07001359 if (!retval)
1360 retval = nbytes;
1361 return retval;
1362}
1363
Paul Menagebd89aab2007-10-18 23:40:44 -07001364static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001365 struct cftype *cft,
1366 struct file *file,
1367 const char __user *userbuf,
1368 size_t nbytes, loff_t *unused_ppos)
1369{
1370 enum cgroup_filetype type = cft->private;
1371 char *buffer;
1372 int retval = 0;
1373
1374 if (nbytes >= PATH_MAX)
1375 return -E2BIG;
1376
1377 /* +1 for nul-terminator */
1378 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1379 if (buffer == NULL)
1380 return -ENOMEM;
1381
1382 if (copy_from_user(buffer, userbuf, nbytes)) {
1383 retval = -EFAULT;
1384 goto out1;
1385 }
1386 buffer[nbytes] = 0; /* nul-terminate */
Paul Jackson622d42c2008-02-07 00:13:44 -08001387 strstrip(buffer); /* strip -just- trailing whitespace */
Paul Menagebbcb81d2007-10-18 23:39:32 -07001388
1389 mutex_lock(&cgroup_mutex);
1390
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001391 /*
1392 * This was already checked for in cgroup_file_write(), but
1393 * check again now we're holding cgroup_mutex.
1394 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001395 if (cgroup_is_removed(cgrp)) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001396 retval = -ENODEV;
1397 goto out2;
1398 }
1399
1400 switch (type) {
1401 case FILE_TASKLIST:
Paul Menagebd89aab2007-10-18 23:40:44 -07001402 retval = attach_task_by_pid(cgrp, buffer);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001403 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001404 case FILE_NOTIFY_ON_RELEASE:
Paul Menagebd89aab2007-10-18 23:40:44 -07001405 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001406 if (simple_strtoul(buffer, NULL, 10) != 0)
Paul Menagebd89aab2007-10-18 23:40:44 -07001407 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001408 else
Paul Menagebd89aab2007-10-18 23:40:44 -07001409 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001410 break;
1411 case FILE_RELEASE_AGENT:
Paul Jackson622d42c2008-02-07 00:13:44 -08001412 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1413 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001414 break;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001415 default:
1416 retval = -EINVAL;
1417 goto out2;
1418 }
1419
1420 if (retval == 0)
1421 retval = nbytes;
1422out2:
1423 mutex_unlock(&cgroup_mutex);
1424out1:
1425 kfree(buffer);
1426 return retval;
1427}
1428
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1430 size_t nbytes, loff_t *ppos)
1431{
1432 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001433 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001435 if (!cft || cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001437 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001438 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001439 if (cft->write_u64 || cft->write_s64)
1440 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001441 if (cft->trigger) {
1442 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1443 return ret ? ret : nbytes;
1444 }
Paul Menage355e0c42007-10-18 23:39:33 -07001445 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446}
1447
Paul Menagef4c753b2008-04-29 00:59:56 -07001448static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1449 struct file *file,
1450 char __user *buf, size_t nbytes,
1451 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452{
1453 char tmp[64];
Paul Menagef4c753b2008-04-29 00:59:56 -07001454 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1456
1457 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1458}
1459
Paul Menagee73d2c62008-04-29 01:00:06 -07001460static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1461 struct file *file,
1462 char __user *buf, size_t nbytes,
1463 loff_t *ppos)
1464{
1465 char tmp[64];
1466 s64 val = cft->read_s64(cgrp, cft);
1467 int len = sprintf(tmp, "%lld\n", (long long) val);
1468
1469 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1470}
1471
Paul Menagebd89aab2007-10-18 23:40:44 -07001472static ssize_t cgroup_common_file_read(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001473 struct cftype *cft,
1474 struct file *file,
1475 char __user *buf,
1476 size_t nbytes, loff_t *ppos)
1477{
1478 enum cgroup_filetype type = cft->private;
1479 char *page;
1480 ssize_t retval = 0;
1481 char *s;
1482
1483 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1484 return -ENOMEM;
1485
1486 s = page;
1487
1488 switch (type) {
1489 case FILE_RELEASE_AGENT:
1490 {
1491 struct cgroupfs_root *root;
1492 size_t n;
1493 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001494 root = cgrp->root;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001495 n = strnlen(root->release_agent_path,
1496 sizeof(root->release_agent_path));
1497 n = min(n, (size_t) PAGE_SIZE);
1498 strncpy(s, root->release_agent_path, n);
1499 mutex_unlock(&cgroup_mutex);
1500 s += n;
1501 break;
1502 }
1503 default:
1504 retval = -EINVAL;
1505 goto out;
1506 }
1507 *s++ = '\n';
1508
1509 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1510out:
1511 free_page((unsigned long)page);
1512 return retval;
1513}
1514
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1516 size_t nbytes, loff_t *ppos)
1517{
1518 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001519 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001521 if (!cft || cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522 return -ENODEV;
1523
1524 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001525 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001526 if (cft->read_u64)
1527 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001528 if (cft->read_s64)
1529 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530 return -EINVAL;
1531}
1532
Paul Menage91796562008-04-29 01:00:01 -07001533/*
1534 * seqfile ops/methods for returning structured data. Currently just
1535 * supports string->u64 maps, but can be extended in future.
1536 */
1537
1538struct cgroup_seqfile_state {
1539 struct cftype *cft;
1540 struct cgroup *cgroup;
1541};
1542
1543static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1544{
1545 struct seq_file *sf = cb->state;
1546 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1547}
1548
1549static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1550{
1551 struct cgroup_seqfile_state *state = m->private;
1552 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001553 if (cft->read_map) {
1554 struct cgroup_map_cb cb = {
1555 .fill = cgroup_map_add,
1556 .state = m,
1557 };
1558 return cft->read_map(state->cgroup, cft, &cb);
1559 }
1560 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001561}
1562
1563int cgroup_seqfile_release(struct inode *inode, struct file *file)
1564{
1565 struct seq_file *seq = file->private_data;
1566 kfree(seq->private);
1567 return single_release(inode, file);
1568}
1569
1570static struct file_operations cgroup_seqfile_operations = {
1571 .read = seq_read,
1572 .llseek = seq_lseek,
1573 .release = cgroup_seqfile_release,
1574};
1575
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576static int cgroup_file_open(struct inode *inode, struct file *file)
1577{
1578 int err;
1579 struct cftype *cft;
1580
1581 err = generic_file_open(inode, file);
1582 if (err)
1583 return err;
1584
1585 cft = __d_cft(file->f_dentry);
1586 if (!cft)
1587 return -ENODEV;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001588 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001589 struct cgroup_seqfile_state *state =
1590 kzalloc(sizeof(*state), GFP_USER);
1591 if (!state)
1592 return -ENOMEM;
1593 state->cft = cft;
1594 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1595 file->f_op = &cgroup_seqfile_operations;
1596 err = single_open(file, cgroup_seqfile_show, state);
1597 if (err < 0)
1598 kfree(state);
1599 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 err = cft->open(inode, file);
1601 else
1602 err = 0;
1603
1604 return err;
1605}
1606
1607static int cgroup_file_release(struct inode *inode, struct file *file)
1608{
1609 struct cftype *cft = __d_cft(file->f_dentry);
1610 if (cft->release)
1611 return cft->release(inode, file);
1612 return 0;
1613}
1614
1615/*
1616 * cgroup_rename - Only allow simple rename of directories in place.
1617 */
1618static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1619 struct inode *new_dir, struct dentry *new_dentry)
1620{
1621 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1622 return -ENOTDIR;
1623 if (new_dentry->d_inode)
1624 return -EEXIST;
1625 if (old_dir != new_dir)
1626 return -EIO;
1627 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1628}
1629
1630static struct file_operations cgroup_file_operations = {
1631 .read = cgroup_file_read,
1632 .write = cgroup_file_write,
1633 .llseek = generic_file_llseek,
1634 .open = cgroup_file_open,
1635 .release = cgroup_file_release,
1636};
1637
1638static struct inode_operations cgroup_dir_inode_operations = {
1639 .lookup = simple_lookup,
1640 .mkdir = cgroup_mkdir,
1641 .rmdir = cgroup_rmdir,
1642 .rename = cgroup_rename,
1643};
1644
1645static int cgroup_create_file(struct dentry *dentry, int mode,
1646 struct super_block *sb)
1647{
1648 static struct dentry_operations cgroup_dops = {
1649 .d_iput = cgroup_diput,
1650 };
1651
1652 struct inode *inode;
1653
1654 if (!dentry)
1655 return -ENOENT;
1656 if (dentry->d_inode)
1657 return -EEXIST;
1658
1659 inode = cgroup_new_inode(mode, sb);
1660 if (!inode)
1661 return -ENOMEM;
1662
1663 if (S_ISDIR(mode)) {
1664 inode->i_op = &cgroup_dir_inode_operations;
1665 inode->i_fop = &simple_dir_operations;
1666
1667 /* start off with i_nlink == 2 (for "." entry) */
1668 inc_nlink(inode);
1669
1670 /* start with the directory inode held, so that we can
1671 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001672 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 } else if (S_ISREG(mode)) {
1674 inode->i_size = 0;
1675 inode->i_fop = &cgroup_file_operations;
1676 }
1677 dentry->d_op = &cgroup_dops;
1678 d_instantiate(dentry, inode);
1679 dget(dentry); /* Extra count - pin the dentry in core */
1680 return 0;
1681}
1682
1683/*
Li Zefana043e3b2008-02-23 15:24:09 -08001684 * cgroup_create_dir - create a directory for an object.
1685 * @cgrp: the cgroup we create the directory for. It must have a valid
1686 * ->parent field. And we are going to fill its ->dentry field.
1687 * @dentry: dentry of the new cgroup
1688 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001690static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691 int mode)
1692{
1693 struct dentry *parent;
1694 int error = 0;
1695
Paul Menagebd89aab2007-10-18 23:40:44 -07001696 parent = cgrp->parent->dentry;
1697 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001699 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 inc_nlink(parent->d_inode);
Paul Menagebd89aab2007-10-18 23:40:44 -07001701 cgrp->dentry = dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702 dget(dentry);
1703 }
1704 dput(dentry);
1705
1706 return error;
1707}
1708
Paul Menagebd89aab2007-10-18 23:40:44 -07001709int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 struct cgroup_subsys *subsys,
1711 const struct cftype *cft)
1712{
Paul Menagebd89aab2007-10-18 23:40:44 -07001713 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714 struct dentry *dentry;
1715 int error;
1716
1717 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001718 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719 strcpy(name, subsys->name);
1720 strcat(name, ".");
1721 }
1722 strcat(name, cft->name);
1723 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1724 dentry = lookup_one_len(name, dir, strlen(name));
1725 if (!IS_ERR(dentry)) {
1726 error = cgroup_create_file(dentry, 0644 | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001727 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 if (!error)
1729 dentry->d_fsdata = (void *)cft;
1730 dput(dentry);
1731 } else
1732 error = PTR_ERR(dentry);
1733 return error;
1734}
1735
Paul Menagebd89aab2007-10-18 23:40:44 -07001736int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 struct cgroup_subsys *subsys,
1738 const struct cftype cft[],
1739 int count)
1740{
1741 int i, err;
1742 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 if (err)
1745 return err;
1746 }
1747 return 0;
1748}
1749
Li Zefana043e3b2008-02-23 15:24:09 -08001750/**
1751 * cgroup_task_count - count the number of tasks in a cgroup.
1752 * @cgrp: the cgroup in question
1753 *
1754 * Return the number of tasks in the cgroup.
1755 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001756int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001757{
1758 int count = 0;
Paul Menage817929e2007-10-18 23:39:36 -07001759 struct list_head *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001760
Paul Menage817929e2007-10-18 23:39:36 -07001761 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001762 l = cgrp->css_sets.next;
1763 while (l != &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001764 struct cg_cgroup_link *link =
Paul Menagebd89aab2007-10-18 23:40:44 -07001765 list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001766 count += atomic_read(&link->cg->ref.refcount);
1767 l = l->next;
1768 }
1769 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001770 return count;
1771}
1772
1773/*
Paul Menage817929e2007-10-18 23:39:36 -07001774 * Advance a list_head iterator. The iterator should be positioned at
1775 * the start of a css_set
1776 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001777static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001778 struct cgroup_iter *it)
1779{
1780 struct list_head *l = it->cg_link;
1781 struct cg_cgroup_link *link;
1782 struct css_set *cg;
1783
1784 /* Advance to the next non-empty css_set */
1785 do {
1786 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001787 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001788 it->cg_link = NULL;
1789 return;
1790 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001791 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001792 cg = link->cg;
1793 } while (list_empty(&cg->tasks));
1794 it->cg_link = l;
1795 it->task = cg->tasks.next;
1796}
1797
Cliff Wickman31a7df02008-02-07 00:14:42 -08001798/*
1799 * To reduce the fork() overhead for systems that are not actually
1800 * using their cgroups capability, we don't maintain the lists running
1801 * through each css_set to its tasks until we see the list actually
1802 * used - in other words after the first call to cgroup_iter_start().
1803 *
1804 * The tasklist_lock is not held here, as do_each_thread() and
1805 * while_each_thread() are protected by RCU.
1806 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001807static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001808{
1809 struct task_struct *p, *g;
1810 write_lock(&css_set_lock);
1811 use_task_css_set_links = 1;
1812 do_each_thread(g, p) {
1813 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001814 /*
1815 * We should check if the process is exiting, otherwise
1816 * it will race with cgroup_exit() in that the list
1817 * entry won't be deleted though the process has exited.
1818 */
1819 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001820 list_add(&p->cg_list, &p->cgroups->tasks);
1821 task_unlock(p);
1822 } while_each_thread(g, p);
1823 write_unlock(&css_set_lock);
1824}
1825
Paul Menagebd89aab2007-10-18 23:40:44 -07001826void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001827{
1828 /*
1829 * The first time anyone tries to iterate across a cgroup,
1830 * we need to enable the list linking each css_set to its
1831 * tasks, and fix up all existing tasks.
1832 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001833 if (!use_task_css_set_links)
1834 cgroup_enable_task_cg_lists();
1835
Paul Menage817929e2007-10-18 23:39:36 -07001836 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001837 it->cg_link = &cgrp->css_sets;
1838 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001839}
1840
Paul Menagebd89aab2007-10-18 23:40:44 -07001841struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001842 struct cgroup_iter *it)
1843{
1844 struct task_struct *res;
1845 struct list_head *l = it->task;
1846
1847 /* If the iterator cg is NULL, we have no tasks */
1848 if (!it->cg_link)
1849 return NULL;
1850 res = list_entry(l, struct task_struct, cg_list);
1851 /* Advance iterator to find next entry */
1852 l = l->next;
1853 if (l == &res->cgroups->tasks) {
1854 /* We reached the end of this task list - move on to
1855 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001856 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001857 } else {
1858 it->task = l;
1859 }
1860 return res;
1861}
1862
Paul Menagebd89aab2007-10-18 23:40:44 -07001863void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001864{
1865 read_unlock(&css_set_lock);
1866}
1867
Cliff Wickman31a7df02008-02-07 00:14:42 -08001868static inline int started_after_time(struct task_struct *t1,
1869 struct timespec *time,
1870 struct task_struct *t2)
1871{
1872 int start_diff = timespec_compare(&t1->start_time, time);
1873 if (start_diff > 0) {
1874 return 1;
1875 } else if (start_diff < 0) {
1876 return 0;
1877 } else {
1878 /*
1879 * Arbitrarily, if two processes started at the same
1880 * time, we'll say that the lower pointer value
1881 * started first. Note that t2 may have exited by now
1882 * so this may not be a valid pointer any longer, but
1883 * that's fine - it still serves to distinguish
1884 * between two tasks started (effectively) simultaneously.
1885 */
1886 return t1 > t2;
1887 }
1888}
1889
1890/*
1891 * This function is a callback from heap_insert() and is used to order
1892 * the heap.
1893 * In this case we order the heap in descending task start time.
1894 */
1895static inline int started_after(void *p1, void *p2)
1896{
1897 struct task_struct *t1 = p1;
1898 struct task_struct *t2 = p2;
1899 return started_after_time(t1, &t2->start_time, t2);
1900}
1901
1902/**
1903 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1904 * @scan: struct cgroup_scanner containing arguments for the scan
1905 *
1906 * Arguments include pointers to callback functions test_task() and
1907 * process_task().
1908 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1909 * and if it returns true, call process_task() for it also.
1910 * The test_task pointer may be NULL, meaning always true (select all tasks).
1911 * Effectively duplicates cgroup_iter_{start,next,end}()
1912 * but does not lock css_set_lock for the call to process_task().
1913 * The struct cgroup_scanner may be embedded in any structure of the caller's
1914 * creation.
1915 * It is guaranteed that process_task() will act on every task that
1916 * is a member of the cgroup for the duration of this call. This
1917 * function may or may not call process_task() for tasks that exit
1918 * or move to a different cgroup during the call, or are forked or
1919 * move into the cgroup during the call.
1920 *
1921 * Note that test_task() may be called with locks held, and may in some
1922 * situations be called multiple times for the same task, so it should
1923 * be cheap.
1924 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1925 * pre-allocated and will be used for heap operations (and its "gt" member will
1926 * be overwritten), else a temporary heap will be used (allocation of which
1927 * may cause this function to fail).
1928 */
1929int cgroup_scan_tasks(struct cgroup_scanner *scan)
1930{
1931 int retval, i;
1932 struct cgroup_iter it;
1933 struct task_struct *p, *dropped;
1934 /* Never dereference latest_task, since it's not refcounted */
1935 struct task_struct *latest_task = NULL;
1936 struct ptr_heap tmp_heap;
1937 struct ptr_heap *heap;
1938 struct timespec latest_time = { 0, 0 };
1939
1940 if (scan->heap) {
1941 /* The caller supplied our heap and pre-allocated its memory */
1942 heap = scan->heap;
1943 heap->gt = &started_after;
1944 } else {
1945 /* We need to allocate our own heap memory */
1946 heap = &tmp_heap;
1947 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1948 if (retval)
1949 /* cannot allocate the heap */
1950 return retval;
1951 }
1952
1953 again:
1954 /*
1955 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1956 * to determine which are of interest, and using the scanner's
1957 * "process_task" callback to process any of them that need an update.
1958 * Since we don't want to hold any locks during the task updates,
1959 * gather tasks to be processed in a heap structure.
1960 * The heap is sorted by descending task start time.
1961 * If the statically-sized heap fills up, we overflow tasks that
1962 * started later, and in future iterations only consider tasks that
1963 * started after the latest task in the previous pass. This
1964 * guarantees forward progress and that we don't miss any tasks.
1965 */
1966 heap->size = 0;
1967 cgroup_iter_start(scan->cg, &it);
1968 while ((p = cgroup_iter_next(scan->cg, &it))) {
1969 /*
1970 * Only affect tasks that qualify per the caller's callback,
1971 * if he provided one
1972 */
1973 if (scan->test_task && !scan->test_task(p, scan))
1974 continue;
1975 /*
1976 * Only process tasks that started after the last task
1977 * we processed
1978 */
1979 if (!started_after_time(p, &latest_time, latest_task))
1980 continue;
1981 dropped = heap_insert(heap, p);
1982 if (dropped == NULL) {
1983 /*
1984 * The new task was inserted; the heap wasn't
1985 * previously full
1986 */
1987 get_task_struct(p);
1988 } else if (dropped != p) {
1989 /*
1990 * The new task was inserted, and pushed out a
1991 * different task
1992 */
1993 get_task_struct(p);
1994 put_task_struct(dropped);
1995 }
1996 /*
1997 * Else the new task was newer than anything already in
1998 * the heap and wasn't inserted
1999 */
2000 }
2001 cgroup_iter_end(scan->cg, &it);
2002
2003 if (heap->size) {
2004 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002005 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002006 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002007 latest_time = q->start_time;
2008 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002009 }
2010 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002011 scan->process_task(q, scan);
2012 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002013 }
2014 /*
2015 * If we had to process any tasks at all, scan again
2016 * in case some of them were in the middle of forking
2017 * children that didn't get processed.
2018 * Not the most efficient way to do it, but it avoids
2019 * having to take callback_mutex in the fork path
2020 */
2021 goto again;
2022 }
2023 if (heap == &tmp_heap)
2024 heap_free(&tmp_heap);
2025 return 0;
2026}
2027
Paul Menage817929e2007-10-18 23:39:36 -07002028/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002029 * Stuff for reading the 'tasks' file.
2030 *
2031 * Reading this file can return large amounts of data if a cgroup has
2032 * *lots* of attached tasks. So it may need several calls to read(),
2033 * but we cannot guarantee that the information we produce is correct
2034 * unless we produce it entirely atomically.
2035 *
2036 * Upon tasks file open(), a struct ctr_struct is allocated, that
2037 * will have a pointer to an array (also allocated here). The struct
2038 * ctr_struct * is stored in file->private_data. Its resources will
2039 * be freed by release() when the file is closed. The array is used
2040 * to sprintf the PIDs and then used by read().
2041 */
2042struct ctr_struct {
2043 char *buf;
2044 int bufsz;
2045};
2046
2047/*
2048 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002049 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002050 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2051 * read section, so the css_set can't go away, and is
2052 * immutable after creation.
2053 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002054static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002055{
2056 int n = 0;
Paul Menage817929e2007-10-18 23:39:36 -07002057 struct cgroup_iter it;
2058 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002059 cgroup_iter_start(cgrp, &it);
2060 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002061 if (unlikely(n == npids))
2062 break;
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002063 pidarray[n++] = task_pid_vnr(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002064 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002065 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002066 return n;
2067}
2068
Balbir Singh846c7bb2007-10-18 23:39:44 -07002069/**
Li Zefana043e3b2008-02-23 15:24:09 -08002070 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002071 * @stats: cgroupstats to fill information into
2072 * @dentry: A dentry entry belonging to the cgroup for which stats have
2073 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002074 *
2075 * Build and fill cgroupstats so that taskstats can export it to user
2076 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002077 */
2078int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2079{
2080 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002081 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002082 struct cgroup_iter it;
2083 struct task_struct *tsk;
2084 /*
2085 * Validate dentry by checking the superblock operations
2086 */
2087 if (dentry->d_sb->s_op != &cgroup_ops)
2088 goto err;
2089
2090 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002091 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002092 rcu_read_lock();
2093
Paul Menagebd89aab2007-10-18 23:40:44 -07002094 cgroup_iter_start(cgrp, &it);
2095 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002096 switch (tsk->state) {
2097 case TASK_RUNNING:
2098 stats->nr_running++;
2099 break;
2100 case TASK_INTERRUPTIBLE:
2101 stats->nr_sleeping++;
2102 break;
2103 case TASK_UNINTERRUPTIBLE:
2104 stats->nr_uninterruptible++;
2105 break;
2106 case TASK_STOPPED:
2107 stats->nr_stopped++;
2108 break;
2109 default:
2110 if (delayacct_is_task_waiting_on_io(tsk))
2111 stats->nr_io_wait++;
2112 break;
2113 }
2114 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002115 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002116
2117 rcu_read_unlock();
2118err:
2119 return ret;
2120}
2121
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122static int cmppid(const void *a, const void *b)
2123{
2124 return *(pid_t *)a - *(pid_t *)b;
2125}
2126
2127/*
2128 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2129 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2130 * count 'cnt' of how many chars would be written if buf were large enough.
2131 */
2132static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2133{
2134 int cnt = 0;
2135 int i;
2136
2137 for (i = 0; i < npids; i++)
2138 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2139 return cnt;
2140}
2141
2142/*
2143 * Handle an open on 'tasks' file. Prepare a buffer listing the
2144 * process id's of tasks currently attached to the cgroup being opened.
2145 *
2146 * Does not require any specific cgroup mutexes, and does not take any.
2147 */
2148static int cgroup_tasks_open(struct inode *unused, struct file *file)
2149{
Paul Menagebd89aab2007-10-18 23:40:44 -07002150 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002151 struct ctr_struct *ctr;
2152 pid_t *pidarray;
2153 int npids;
2154 char c;
2155
2156 if (!(file->f_mode & FMODE_READ))
2157 return 0;
2158
2159 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2160 if (!ctr)
2161 goto err0;
2162
2163 /*
2164 * If cgroup gets more users after we read count, we won't have
2165 * enough space - tough. This race is indistinguishable to the
2166 * caller from the case that the additional cgroup users didn't
2167 * show up until sometime later on.
2168 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002169 npids = cgroup_task_count(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 if (npids) {
2171 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2172 if (!pidarray)
2173 goto err1;
2174
Paul Menagebd89aab2007-10-18 23:40:44 -07002175 npids = pid_array_load(pidarray, npids, cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2177
2178 /* Call pid_array_to_buf() twice, first just to get bufsz */
2179 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2180 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2181 if (!ctr->buf)
2182 goto err2;
2183 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2184
2185 kfree(pidarray);
2186 } else {
Al Viro9dce07f2008-03-29 03:07:28 +00002187 ctr->buf = NULL;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 ctr->bufsz = 0;
2189 }
2190 file->private_data = ctr;
2191 return 0;
2192
2193err2:
2194 kfree(pidarray);
2195err1:
2196 kfree(ctr);
2197err0:
2198 return -ENOMEM;
2199}
2200
Paul Menagebd89aab2007-10-18 23:40:44 -07002201static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002202 struct cftype *cft,
2203 struct file *file, char __user *buf,
2204 size_t nbytes, loff_t *ppos)
2205{
2206 struct ctr_struct *ctr = file->private_data;
2207
2208 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2209}
2210
2211static int cgroup_tasks_release(struct inode *unused_inode,
2212 struct file *file)
2213{
2214 struct ctr_struct *ctr;
2215
2216 if (file->f_mode & FMODE_READ) {
2217 ctr = file->private_data;
2218 kfree(ctr->buf);
2219 kfree(ctr);
2220 }
2221 return 0;
2222}
2223
Paul Menagebd89aab2007-10-18 23:40:44 -07002224static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002225 struct cftype *cft)
2226{
Paul Menagebd89aab2007-10-18 23:40:44 -07002227 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002228}
2229
Paul Menagebbcb81d2007-10-18 23:39:32 -07002230/*
2231 * for the common functions, 'private' gives the type of file
2232 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002233static struct cftype files[] = {
2234 {
2235 .name = "tasks",
2236 .open = cgroup_tasks_open,
2237 .read = cgroup_tasks_read,
2238 .write = cgroup_common_file_write,
2239 .release = cgroup_tasks_release,
2240 .private = FILE_TASKLIST,
2241 },
2242
2243 {
2244 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002245 .read_u64 = cgroup_read_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002246 .write = cgroup_common_file_write,
2247 .private = FILE_NOTIFY_ON_RELEASE,
2248 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002249};
2250
2251static struct cftype cft_release_agent = {
2252 .name = "release_agent",
2253 .read = cgroup_common_file_read,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002254 .write = cgroup_common_file_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002255 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002256};
2257
Paul Menagebd89aab2007-10-18 23:40:44 -07002258static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002259{
2260 int err;
2261 struct cgroup_subsys *ss;
2262
2263 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002264 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002265
Paul Menagebd89aab2007-10-18 23:40:44 -07002266 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002267 if (err < 0)
2268 return err;
2269
Paul Menagebd89aab2007-10-18 23:40:44 -07002270 if (cgrp == cgrp->top_cgroup) {
2271 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002272 return err;
2273 }
2274
Paul Menagebd89aab2007-10-18 23:40:44 -07002275 for_each_subsys(cgrp->root, ss) {
2276 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002277 return err;
2278 }
2279
2280 return 0;
2281}
2282
2283static void init_cgroup_css(struct cgroup_subsys_state *css,
2284 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002285 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002286{
Paul Menagebd89aab2007-10-18 23:40:44 -07002287 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002288 atomic_set(&css->refcnt, 0);
2289 css->flags = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002290 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002291 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002292 BUG_ON(cgrp->subsys[ss->subsys_id]);
2293 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002294}
2295
2296/*
Li Zefana043e3b2008-02-23 15:24:09 -08002297 * cgroup_create - create a cgroup
2298 * @parent: cgroup that will be parent of the new cgroup
2299 * @dentry: dentry of the new cgroup
2300 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301 *
Li Zefana043e3b2008-02-23 15:24:09 -08002302 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002303 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002304static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2305 int mode)
2306{
Paul Menagebd89aab2007-10-18 23:40:44 -07002307 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002308 struct cgroupfs_root *root = parent->root;
2309 int err = 0;
2310 struct cgroup_subsys *ss;
2311 struct super_block *sb = root->sb;
2312
Paul Menagebd89aab2007-10-18 23:40:44 -07002313 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2314 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002315 return -ENOMEM;
2316
2317 /* Grab a reference on the superblock so the hierarchy doesn't
2318 * get deleted on unmount if there are child cgroups. This
2319 * can be done outside cgroup_mutex, since the sb can't
2320 * disappear while someone has an open control file on the
2321 * fs */
2322 atomic_inc(&sb->s_active);
2323
2324 mutex_lock(&cgroup_mutex);
2325
Paul Menagebd89aab2007-10-18 23:40:44 -07002326 INIT_LIST_HEAD(&cgrp->sibling);
2327 INIT_LIST_HEAD(&cgrp->children);
2328 INIT_LIST_HEAD(&cgrp->css_sets);
2329 INIT_LIST_HEAD(&cgrp->release_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002330
Paul Menagebd89aab2007-10-18 23:40:44 -07002331 cgrp->parent = parent;
2332 cgrp->root = parent->root;
2333 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002334
Li Zefanb6abdb02008-03-04 14:28:19 -08002335 if (notify_on_release(parent))
2336 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2337
Paul Menageddbcc7e2007-10-18 23:39:30 -07002338 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002339 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002340 if (IS_ERR(css)) {
2341 err = PTR_ERR(css);
2342 goto err_destroy;
2343 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002344 init_cgroup_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002345 }
2346
Paul Menagebd89aab2007-10-18 23:40:44 -07002347 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002348 root->number_of_cgroups++;
2349
Paul Menagebd89aab2007-10-18 23:40:44 -07002350 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351 if (err < 0)
2352 goto err_remove;
2353
2354 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002355 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002356
Paul Menagebd89aab2007-10-18 23:40:44 -07002357 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002358 /* If err < 0, we have a half-filled directory - oh well ;) */
2359
2360 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002361 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362
2363 return 0;
2364
2365 err_remove:
2366
Paul Menagebd89aab2007-10-18 23:40:44 -07002367 list_del(&cgrp->sibling);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002368 root->number_of_cgroups--;
2369
2370 err_destroy:
2371
2372 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002373 if (cgrp->subsys[ss->subsys_id])
2374 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002375 }
2376
2377 mutex_unlock(&cgroup_mutex);
2378
2379 /* Release the reference count that we took on the superblock */
2380 deactivate_super(sb);
2381
Paul Menagebd89aab2007-10-18 23:40:44 -07002382 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002383 return err;
2384}
2385
2386static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2387{
2388 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2389
2390 /* the vfs holds inode->i_mutex already */
2391 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2392}
2393
Paul Menagebd89aab2007-10-18 23:40:44 -07002394static inline int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002395{
2396 /* Check the reference count on each subsystem. Since we
2397 * already established that there are no tasks in the
2398 * cgroup, if the css refcount is also 0, then there should
2399 * be no outstanding references, so the subsystem is safe to
2400 * destroy. We scan across all subsystems rather than using
2401 * the per-hierarchy linked list of mounted subsystems since
2402 * we can be called via check_for_release() with no
2403 * synchronization other than RCU, and the subsystem linked
2404 * list isn't RCU-safe */
2405 int i;
2406 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2407 struct cgroup_subsys *ss = subsys[i];
2408 struct cgroup_subsys_state *css;
2409 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002410 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002411 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002412 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002413 /* When called from check_for_release() it's possible
2414 * that by this point the cgroup has been removed
2415 * and the css deleted. But a false-positive doesn't
2416 * matter, since it can only happen if the cgroup
2417 * has been deleted and hence no longer needs the
2418 * release agent to be called anyway. */
Paul Jacksone18f6312008-02-07 00:13:44 -08002419 if (css && atomic_read(&css->refcnt))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002420 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002421 }
2422 return 0;
2423}
2424
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2426{
Paul Menagebd89aab2007-10-18 23:40:44 -07002427 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428 struct dentry *d;
2429 struct cgroup *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 struct super_block *sb;
2431 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432
2433 /* the vfs holds both inode->i_mutex already */
2434
2435 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002436 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002437 mutex_unlock(&cgroup_mutex);
2438 return -EBUSY;
2439 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002440 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002441 mutex_unlock(&cgroup_mutex);
2442 return -EBUSY;
2443 }
2444
Paul Menagebd89aab2007-10-18 23:40:44 -07002445 parent = cgrp->parent;
2446 root = cgrp->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002447 sb = root->sb;
Li Zefana043e3b2008-02-23 15:24:09 -08002448
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002449 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002450 * Call pre_destroy handlers of subsys. Notify subsystems
2451 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002452 */
2453 cgroup_call_pre_destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002454
Paul Menagebd89aab2007-10-18 23:40:44 -07002455 if (cgroup_has_css_refs(cgrp)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002456 mutex_unlock(&cgroup_mutex);
2457 return -EBUSY;
2458 }
2459
Paul Menage81a6a5c2007-10-18 23:39:38 -07002460 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002461 set_bit(CGRP_REMOVED, &cgrp->flags);
2462 if (!list_empty(&cgrp->release_list))
2463 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002464 spin_unlock(&release_list_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002465 /* delete my sibling from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002466 list_del(&cgrp->sibling);
2467 spin_lock(&cgrp->dentry->d_lock);
2468 d = dget(cgrp->dentry);
2469 cgrp->dentry = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002470 spin_unlock(&d->d_lock);
2471
2472 cgroup_d_remove_dir(d);
2473 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002474
Paul Menagebd89aab2007-10-18 23:40:44 -07002475 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002476 check_for_release(parent);
2477
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002479 return 0;
2480}
2481
Li Zefan06a11922008-04-29 01:00:07 -07002482static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002485
2486 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002487
2488 /* Create the top cgroup state for this subsystem */
2489 ss->root = &rootnode;
2490 css = ss->create(ss, dummytop);
2491 /* We don't handle early failures gracefully */
2492 BUG_ON(IS_ERR(css));
2493 init_cgroup_css(css, ss, dummytop);
2494
Li Zefane8d55fd2008-04-29 01:00:13 -07002495 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002496 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002497 * newly registered, all tasks and hence the
2498 * init_css_set is in the subsystem's top cgroup. */
2499 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002500
2501 need_forkexit_callback |= ss->fork || ss->exit;
Balbir Singhcf475ad2008-04-29 01:00:16 -07002502 need_mm_owner_callback |= !!ss->mm_owner_changed;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002503
Li Zefane8d55fd2008-04-29 01:00:13 -07002504 /* At system boot, before all subsystems have been
2505 * registered, no tasks have been forked, so we don't
2506 * need to invoke fork callbacks here. */
2507 BUG_ON(!list_empty(&init_task.tasks));
2508
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509 ss->active = 1;
2510}
2511
2512/**
Li Zefana043e3b2008-02-23 15:24:09 -08002513 * cgroup_init_early - cgroup initialization at system boot
2514 *
2515 * Initialize cgroups at system boot, and initialize any
2516 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002517 */
2518int __init cgroup_init_early(void)
2519{
2520 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002521 kref_init(&init_css_set.ref);
2522 kref_get(&init_css_set.ref);
Paul Menage817929e2007-10-18 23:39:36 -07002523 INIT_LIST_HEAD(&init_css_set.cg_links);
2524 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002525 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002526 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002527 init_cgroup_root(&rootnode);
2528 list_add(&rootnode.root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07002529 root_count = 1;
2530 init_task.cgroups = &init_css_set;
2531
2532 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002533 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002534 &rootnode.top_cgroup.css_sets);
2535 list_add(&init_css_set_link.cg_link_list,
2536 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002537
Li Zefan472b1052008-04-29 01:00:11 -07002538 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2539 INIT_HLIST_HEAD(&css_set_table[i]);
2540
Paul Menageddbcc7e2007-10-18 23:39:30 -07002541 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2542 struct cgroup_subsys *ss = subsys[i];
2543
2544 BUG_ON(!ss->name);
2545 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2546 BUG_ON(!ss->create);
2547 BUG_ON(!ss->destroy);
2548 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002549 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 ss->name, ss->subsys_id);
2551 BUG();
2552 }
2553
2554 if (ss->early_init)
2555 cgroup_init_subsys(ss);
2556 }
2557 return 0;
2558}
2559
2560/**
Li Zefana043e3b2008-02-23 15:24:09 -08002561 * cgroup_init - cgroup initialization
2562 *
2563 * Register cgroup filesystem and /proc file, and initialize
2564 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002565 */
2566int __init cgroup_init(void)
2567{
2568 int err;
2569 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002570 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002571
2572 err = bdi_init(&cgroup_backing_dev_info);
2573 if (err)
2574 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002575
2576 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2577 struct cgroup_subsys *ss = subsys[i];
2578 if (!ss->early_init)
2579 cgroup_init_subsys(ss);
2580 }
2581
Li Zefan472b1052008-04-29 01:00:11 -07002582 /* Add init_css_set to the hash table */
2583 hhead = css_set_hash(init_css_set.subsys);
2584 hlist_add_head(&init_css_set.hlist, hhead);
2585
Paul Menageddbcc7e2007-10-18 23:39:30 -07002586 err = register_filesystem(&cgroup_fs_type);
2587 if (err < 0)
2588 goto out;
2589
Li Zefan46ae2202008-04-29 01:00:08 -07002590 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002591
Paul Menageddbcc7e2007-10-18 23:39:30 -07002592out:
Paul Menagea4243162007-10-18 23:39:35 -07002593 if (err)
2594 bdi_destroy(&cgroup_backing_dev_info);
2595
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 return err;
2597}
Paul Menageb4f48b62007-10-18 23:39:33 -07002598
Paul Menagea4243162007-10-18 23:39:35 -07002599/*
2600 * proc_cgroup_show()
2601 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2602 * - Used for /proc/<pid>/cgroup.
2603 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2604 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002605 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002606 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2607 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2608 * cgroup to top_cgroup.
2609 */
2610
2611/* TODO: Use a proper seq_file iterator */
2612static int proc_cgroup_show(struct seq_file *m, void *v)
2613{
2614 struct pid *pid;
2615 struct task_struct *tsk;
2616 char *buf;
2617 int retval;
2618 struct cgroupfs_root *root;
2619
2620 retval = -ENOMEM;
2621 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2622 if (!buf)
2623 goto out;
2624
2625 retval = -ESRCH;
2626 pid = m->private;
2627 tsk = get_pid_task(pid, PIDTYPE_PID);
2628 if (!tsk)
2629 goto out_free;
2630
2631 retval = 0;
2632
2633 mutex_lock(&cgroup_mutex);
2634
2635 for_each_root(root) {
2636 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002637 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002638 int subsys_id;
2639 int count = 0;
2640
2641 /* Skip this hierarchy if it has no active subsystems */
2642 if (!root->actual_subsys_bits)
2643 continue;
Paul Menageb6c30062008-04-10 21:29:16 -07002644 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002645 for_each_subsys(root, ss)
2646 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2647 seq_putc(m, ':');
2648 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002649 cgrp = task_cgroup(tsk, subsys_id);
2650 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002651 if (retval < 0)
2652 goto out_unlock;
2653 seq_puts(m, buf);
2654 seq_putc(m, '\n');
2655 }
2656
2657out_unlock:
2658 mutex_unlock(&cgroup_mutex);
2659 put_task_struct(tsk);
2660out_free:
2661 kfree(buf);
2662out:
2663 return retval;
2664}
2665
2666static int cgroup_open(struct inode *inode, struct file *file)
2667{
2668 struct pid *pid = PROC_I(inode)->pid;
2669 return single_open(file, proc_cgroup_show, pid);
2670}
2671
2672struct file_operations proc_cgroup_operations = {
2673 .open = cgroup_open,
2674 .read = seq_read,
2675 .llseek = seq_lseek,
2676 .release = single_release,
2677};
2678
2679/* Display information about each subsystem and each hierarchy */
2680static int proc_cgroupstats_show(struct seq_file *m, void *v)
2681{
2682 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002683
Paul Menage8bab8dd2008-04-04 14:29:57 -07002684 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002685 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002686 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2687 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002688 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002689 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002690 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002691 }
2692 mutex_unlock(&cgroup_mutex);
2693 return 0;
2694}
2695
2696static int cgroupstats_open(struct inode *inode, struct file *file)
2697{
Al Viro9dce07f2008-03-29 03:07:28 +00002698 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002699}
2700
2701static struct file_operations proc_cgroupstats_operations = {
2702 .open = cgroupstats_open,
2703 .read = seq_read,
2704 .llseek = seq_lseek,
2705 .release = single_release,
2706};
2707
Paul Menageb4f48b62007-10-18 23:39:33 -07002708/**
2709 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002710 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002711 *
2712 * Description: A task inherits its parent's cgroup at fork().
2713 *
2714 * A pointer to the shared css_set was automatically copied in
2715 * fork.c by dup_task_struct(). However, we ignore that copy, since
2716 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002717 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002718 * have already changed current->cgroups, allowing the previously
2719 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002720 *
2721 * At the point that cgroup_fork() is called, 'current' is the parent
2722 * task, and the passed argument 'child' points to the child task.
2723 */
2724void cgroup_fork(struct task_struct *child)
2725{
Paul Menage817929e2007-10-18 23:39:36 -07002726 task_lock(current);
2727 child->cgroups = current->cgroups;
2728 get_css_set(child->cgroups);
2729 task_unlock(current);
2730 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002731}
2732
2733/**
Li Zefana043e3b2008-02-23 15:24:09 -08002734 * cgroup_fork_callbacks - run fork callbacks
2735 * @child: the new task
2736 *
2737 * Called on a new task very soon before adding it to the
2738 * tasklist. No need to take any locks since no-one can
2739 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002740 */
2741void cgroup_fork_callbacks(struct task_struct *child)
2742{
2743 if (need_forkexit_callback) {
2744 int i;
2745 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2746 struct cgroup_subsys *ss = subsys[i];
2747 if (ss->fork)
2748 ss->fork(ss, child);
2749 }
2750 }
2751}
2752
Balbir Singhcf475ad2008-04-29 01:00:16 -07002753#ifdef CONFIG_MM_OWNER
2754/**
2755 * cgroup_mm_owner_callbacks - run callbacks when the mm->owner changes
2756 * @p: the new owner
2757 *
2758 * Called on every change to mm->owner. mm_init_owner() does not
2759 * invoke this routine, since it assigns the mm->owner the first time
2760 * and does not change it.
2761 */
2762void cgroup_mm_owner_callbacks(struct task_struct *old, struct task_struct *new)
2763{
2764 struct cgroup *oldcgrp, *newcgrp;
2765
2766 if (need_mm_owner_callback) {
2767 int i;
2768 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2769 struct cgroup_subsys *ss = subsys[i];
2770 oldcgrp = task_cgroup(old, ss->subsys_id);
2771 newcgrp = task_cgroup(new, ss->subsys_id);
2772 if (oldcgrp == newcgrp)
2773 continue;
2774 if (ss->mm_owner_changed)
2775 ss->mm_owner_changed(ss, oldcgrp, newcgrp);
2776 }
2777 }
2778}
2779#endif /* CONFIG_MM_OWNER */
2780
Paul Menageb4f48b62007-10-18 23:39:33 -07002781/**
Li Zefana043e3b2008-02-23 15:24:09 -08002782 * cgroup_post_fork - called on a new task after adding it to the task list
2783 * @child: the task in question
2784 *
2785 * Adds the task to the list running through its css_set if necessary.
2786 * Has to be after the task is visible on the task list in case we race
2787 * with the first call to cgroup_iter_start() - to guarantee that the
2788 * new task ends up on its list.
2789 */
Paul Menage817929e2007-10-18 23:39:36 -07002790void cgroup_post_fork(struct task_struct *child)
2791{
2792 if (use_task_css_set_links) {
2793 write_lock(&css_set_lock);
2794 if (list_empty(&child->cg_list))
2795 list_add(&child->cg_list, &child->cgroups->tasks);
2796 write_unlock(&css_set_lock);
2797 }
2798}
2799/**
Paul Menageb4f48b62007-10-18 23:39:33 -07002800 * cgroup_exit - detach cgroup from exiting task
2801 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08002802 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07002803 *
2804 * Description: Detach cgroup from @tsk and release it.
2805 *
2806 * Note that cgroups marked notify_on_release force every task in
2807 * them to take the global cgroup_mutex mutex when exiting.
2808 * This could impact scaling on very large systems. Be reluctant to
2809 * use notify_on_release cgroups where very high task exit scaling
2810 * is required on large systems.
2811 *
2812 * the_top_cgroup_hack:
2813 *
2814 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2815 *
2816 * We call cgroup_exit() while the task is still competent to
2817 * handle notify_on_release(), then leave the task attached to the
2818 * root cgroup in each hierarchy for the remainder of its exit.
2819 *
2820 * To do this properly, we would increment the reference count on
2821 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2822 * code we would add a second cgroup function call, to drop that
2823 * reference. This would just create an unnecessary hot spot on
2824 * the top_cgroup reference count, to no avail.
2825 *
2826 * Normally, holding a reference to a cgroup without bumping its
2827 * count is unsafe. The cgroup could go away, or someone could
2828 * attach us to a different cgroup, decrementing the count on
2829 * the first cgroup that we never incremented. But in this case,
2830 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002831 * which wards off any cgroup_attach_task() attempts, or task is a failed
2832 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002833 */
2834void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2835{
2836 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002837 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07002838
2839 if (run_callbacks && need_forkexit_callback) {
2840 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2841 struct cgroup_subsys *ss = subsys[i];
2842 if (ss->exit)
2843 ss->exit(ss, tsk);
2844 }
2845 }
Paul Menage817929e2007-10-18 23:39:36 -07002846
2847 /*
2848 * Unlink from the css_set task list if necessary.
2849 * Optimistically check cg_list before taking
2850 * css_set_lock
2851 */
2852 if (!list_empty(&tsk->cg_list)) {
2853 write_lock(&css_set_lock);
2854 if (!list_empty(&tsk->cg_list))
2855 list_del(&tsk->cg_list);
2856 write_unlock(&css_set_lock);
2857 }
2858
Paul Menageb4f48b62007-10-18 23:39:33 -07002859 /* Reassign the task to the init_css_set. */
2860 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002861 cg = tsk->cgroups;
2862 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07002863 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002864 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002865 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07002866}
Paul Menage697f4162007-10-18 23:39:34 -07002867
2868/**
Li Zefana043e3b2008-02-23 15:24:09 -08002869 * cgroup_clone - clone the cgroup the given subsystem is attached to
2870 * @tsk: the task to be moved
2871 * @subsys: the given subsystem
2872 *
2873 * Duplicate the current cgroup in the hierarchy that the given
2874 * subsystem is attached to, and move this task into the new
2875 * child.
Paul Menage697f4162007-10-18 23:39:34 -07002876 */
2877int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2878{
2879 struct dentry *dentry;
2880 int ret = 0;
2881 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2882 struct cgroup *parent, *child;
2883 struct inode *inode;
2884 struct css_set *cg;
2885 struct cgroupfs_root *root;
2886 struct cgroup_subsys *ss;
2887
2888 /* We shouldn't be called by an unregistered subsystem */
2889 BUG_ON(!subsys->active);
2890
2891 /* First figure out what hierarchy and cgroup we're dealing
2892 * with, and pin them so we can drop cgroup_mutex */
2893 mutex_lock(&cgroup_mutex);
2894 again:
2895 root = subsys->root;
2896 if (root == &rootnode) {
2897 printk(KERN_INFO
2898 "Not cloning cgroup for unused subsystem %s\n",
2899 subsys->name);
2900 mutex_unlock(&cgroup_mutex);
2901 return 0;
2902 }
Paul Menage817929e2007-10-18 23:39:36 -07002903 cg = tsk->cgroups;
Paul Menage697f4162007-10-18 23:39:34 -07002904 parent = task_cgroup(tsk, subsys->subsys_id);
2905
2906 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
2907
2908 /* Pin the hierarchy */
2909 atomic_inc(&parent->root->sb->s_active);
2910
Paul Menage817929e2007-10-18 23:39:36 -07002911 /* Keep the cgroup alive */
2912 get_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002913 mutex_unlock(&cgroup_mutex);
2914
2915 /* Now do the VFS work to create a cgroup */
2916 inode = parent->dentry->d_inode;
2917
2918 /* Hold the parent directory mutex across this operation to
2919 * stop anyone else deleting the new cgroup */
2920 mutex_lock(&inode->i_mutex);
2921 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2922 if (IS_ERR(dentry)) {
2923 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08002924 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07002925 PTR_ERR(dentry));
2926 ret = PTR_ERR(dentry);
2927 goto out_release;
2928 }
2929
2930 /* Create the cgroup directory, which also creates the cgroup */
2931 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07002932 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07002933 dput(dentry);
2934 if (ret) {
2935 printk(KERN_INFO
2936 "Failed to create cgroup %s: %d\n", nodename,
2937 ret);
2938 goto out_release;
2939 }
2940
2941 if (!child) {
2942 printk(KERN_INFO
2943 "Couldn't find new cgroup %s\n", nodename);
2944 ret = -ENOMEM;
2945 goto out_release;
2946 }
2947
2948 /* The cgroup now exists. Retake cgroup_mutex and check
2949 * that we're still in the same state that we thought we
2950 * were. */
2951 mutex_lock(&cgroup_mutex);
2952 if ((root != subsys->root) ||
2953 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2954 /* Aargh, we raced ... */
2955 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002956 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002957
2958 deactivate_super(parent->root->sb);
2959 /* The cgroup is still accessible in the VFS, but
2960 * we're not going to try to rmdir() it at this
2961 * point. */
2962 printk(KERN_INFO
2963 "Race in cgroup_clone() - leaking cgroup %s\n",
2964 nodename);
2965 goto again;
2966 }
2967
2968 /* do any required auto-setup */
2969 for_each_subsys(root, ss) {
2970 if (ss->post_clone)
2971 ss->post_clone(ss, child);
2972 }
2973
2974 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002975 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07002976 mutex_unlock(&cgroup_mutex);
2977
2978 out_release:
2979 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002980
2981 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002982 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002983 mutex_unlock(&cgroup_mutex);
Paul Menage697f4162007-10-18 23:39:34 -07002984 deactivate_super(parent->root->sb);
2985 return ret;
2986}
2987
Li Zefana043e3b2008-02-23 15:24:09 -08002988/**
2989 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2990 * @cgrp: the cgroup in question
2991 *
2992 * See if @cgrp is a descendant of the current task's cgroup in
2993 * the appropriate hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07002994 *
2995 * If we are sending in dummytop, then presumably we are creating
2996 * the top cgroup in the subsystem.
2997 *
2998 * Called only by the ns (nsproxy) cgroup.
2999 */
Paul Menagebd89aab2007-10-18 23:40:44 -07003000int cgroup_is_descendant(const struct cgroup *cgrp)
Paul Menage697f4162007-10-18 23:39:34 -07003001{
3002 int ret;
3003 struct cgroup *target;
3004 int subsys_id;
3005
Paul Menagebd89aab2007-10-18 23:40:44 -07003006 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003007 return 1;
3008
Paul Menagebd89aab2007-10-18 23:40:44 -07003009 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menage697f4162007-10-18 23:39:34 -07003010 target = task_cgroup(current, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003011 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3012 cgrp = cgrp->parent;
3013 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003014 return ret;
3015}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003016
Paul Menagebd89aab2007-10-18 23:40:44 -07003017static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003018{
3019 /* All of these checks rely on RCU to keep the cgroup
3020 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003021 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3022 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003023 /* Control Group is currently removeable. If it's not
3024 * already queued for a userspace notification, queue
3025 * it now */
3026 int need_schedule_work = 0;
3027 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003028 if (!cgroup_is_removed(cgrp) &&
3029 list_empty(&cgrp->release_list)) {
3030 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003031 need_schedule_work = 1;
3032 }
3033 spin_unlock(&release_list_lock);
3034 if (need_schedule_work)
3035 schedule_work(&release_agent_work);
3036 }
3037}
3038
3039void __css_put(struct cgroup_subsys_state *css)
3040{
Paul Menagebd89aab2007-10-18 23:40:44 -07003041 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003042 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -07003043 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3044 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3045 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003046 }
3047 rcu_read_unlock();
3048}
3049
3050/*
3051 * Notify userspace when a cgroup is released, by running the
3052 * configured release agent with the name of the cgroup (path
3053 * relative to the root of cgroup file system) as the argument.
3054 *
3055 * Most likely, this user command will try to rmdir this cgroup.
3056 *
3057 * This races with the possibility that some other task will be
3058 * attached to this cgroup before it is removed, or that some other
3059 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3060 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3061 * unused, and this cgroup will be reprieved from its death sentence,
3062 * to continue to serve a useful existence. Next time it's released,
3063 * we will get notified again, if it still has 'notify_on_release' set.
3064 *
3065 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3066 * means only wait until the task is successfully execve()'d. The
3067 * separate release agent task is forked by call_usermodehelper(),
3068 * then control in this thread returns here, without waiting for the
3069 * release agent task. We don't bother to wait because the caller of
3070 * this routine has no use for the exit status of the release agent
3071 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003072 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003073static void cgroup_release_agent(struct work_struct *work)
3074{
3075 BUG_ON(work != &release_agent_work);
3076 mutex_lock(&cgroup_mutex);
3077 spin_lock(&release_list_lock);
3078 while (!list_empty(&release_list)) {
3079 char *argv[3], *envp[3];
3080 int i;
3081 char *pathbuf;
Paul Menagebd89aab2007-10-18 23:40:44 -07003082 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003083 struct cgroup,
3084 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003085 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003086 spin_unlock(&release_list_lock);
3087 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3088 if (!pathbuf) {
3089 spin_lock(&release_list_lock);
3090 continue;
3091 }
3092
Paul Menagebd89aab2007-10-18 23:40:44 -07003093 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003094 kfree(pathbuf);
3095 spin_lock(&release_list_lock);
3096 continue;
3097 }
3098
3099 i = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003100 argv[i++] = cgrp->root->release_agent_path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003101 argv[i++] = (char *)pathbuf;
3102 argv[i] = NULL;
3103
3104 i = 0;
3105 /* minimal command environment */
3106 envp[i++] = "HOME=/";
3107 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3108 envp[i] = NULL;
3109
3110 /* Drop the lock while we invoke the usermode helper,
3111 * since the exec could involve hitting disk and hence
3112 * be a slow process */
3113 mutex_unlock(&cgroup_mutex);
3114 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3115 kfree(pathbuf);
3116 mutex_lock(&cgroup_mutex);
3117 spin_lock(&release_list_lock);
3118 }
3119 spin_unlock(&release_list_lock);
3120 mutex_unlock(&cgroup_mutex);
3121}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003122
3123static int __init cgroup_disable(char *str)
3124{
3125 int i;
3126 char *token;
3127
3128 while ((token = strsep(&str, ",")) != NULL) {
3129 if (!*token)
3130 continue;
3131
3132 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3133 struct cgroup_subsys *ss = subsys[i];
3134
3135 if (!strcmp(token, ss->name)) {
3136 ss->disabled = 1;
3137 printk(KERN_INFO "Disabling %s control group"
3138 " subsystem\n", ss->name);
3139 break;
3140 }
3141 }
3142 }
3143 return 1;
3144}
3145__setup("cgroup_disable=", cgroup_disable);