cgroups: add a back-pointer from struct cg_cgroup_link to struct cgroup
[linux-2.6-block.git] / kernel / cgroup.c
CommitLineData
ddbcc7e8 1/*
ddbcc7e8
PM
2 * 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>
c6d57f33 26#include <linux/ctype.h>
ddbcc7e8
PM
27#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/kernel.h>
30#include <linux/list.h>
31#include <linux/mm.h>
32#include <linux/mutex.h>
33#include <linux/mount.h>
34#include <linux/pagemap.h>
a424316c 35#include <linux/proc_fs.h>
ddbcc7e8
PM
36#include <linux/rcupdate.h>
37#include <linux/sched.h>
817929ec 38#include <linux/backing-dev.h>
ddbcc7e8
PM
39#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/magic.h>
42#include <linux/spinlock.h>
43#include <linux/string.h>
bbcb81d0 44#include <linux/sort.h>
81a6a5cd 45#include <linux/kmod.h>
846c7bb0
BS
46#include <linux/delayacct.h>
47#include <linux/cgroupstats.h>
472b1053 48#include <linux/hash.h>
3f8206d4 49#include <linux/namei.h>
337eb00a 50#include <linux/smp_lock.h>
096b7fe0 51#include <linux/pid_namespace.h>
846c7bb0 52
ddbcc7e8
PM
53#include <asm/atomic.h>
54
81a6a5cd
PM
55static DEFINE_MUTEX(cgroup_mutex);
56
ddbcc7e8
PM
57/* Generate an array of cgroup subsystem pointers */
58#define SUBSYS(_x) &_x ## _subsys,
59
60static struct cgroup_subsys *subsys[] = {
61#include <linux/cgroup_subsys.h>
62};
63
c6d57f33
PM
64#define MAX_CGROUP_ROOT_NAMELEN 64
65
ddbcc7e8
PM
66/*
67 * A cgroupfs_root represents the root of a cgroup hierarchy,
68 * and may be associated with a superblock to form an active
69 * hierarchy
70 */
71struct cgroupfs_root {
72 struct super_block *sb;
73
74 /*
75 * The bitmask of subsystems intended to be attached to this
76 * hierarchy
77 */
78 unsigned long subsys_bits;
79
80 /* The bitmask of subsystems currently attached to this hierarchy */
81 unsigned long actual_subsys_bits;
82
83 /* A list running through the attached subsystems */
84 struct list_head subsys_list;
85
86 /* The root cgroup for this hierarchy */
87 struct cgroup top_cgroup;
88
89 /* Tracks how many cgroups are currently defined in hierarchy.*/
90 int number_of_cgroups;
91
e5f6a860 92 /* A list running through the active hierarchies */
ddbcc7e8
PM
93 struct list_head root_list;
94
95 /* Hierarchy-specific flags */
96 unsigned long flags;
81a6a5cd 97
e788e066 98 /* The path to use for release notifications. */
81a6a5cd 99 char release_agent_path[PATH_MAX];
c6d57f33
PM
100
101 /* The name for this hierarchy - may be empty */
102 char name[MAX_CGROUP_ROOT_NAMELEN];
ddbcc7e8
PM
103};
104
ddbcc7e8
PM
105/*
106 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
107 * subsystems that are otherwise unattached - it never has more than a
108 * single cgroup, and all tasks are part of that cgroup.
109 */
110static struct cgroupfs_root rootnode;
111
38460b48
KH
112/*
113 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
114 * cgroup_subsys->use_id != 0.
115 */
116#define CSS_ID_MAX (65535)
117struct css_id {
118 /*
119 * The css to which this ID points. This pointer is set to valid value
120 * after cgroup is populated. If cgroup is removed, this will be NULL.
121 * This pointer is expected to be RCU-safe because destroy()
122 * is called after synchronize_rcu(). But for safe use, css_is_removed()
123 * css_tryget() should be used for avoiding race.
124 */
125 struct cgroup_subsys_state *css;
126 /*
127 * ID of this css.
128 */
129 unsigned short id;
130 /*
131 * Depth in hierarchy which this ID belongs to.
132 */
133 unsigned short depth;
134 /*
135 * ID is freed by RCU. (and lookup routine is RCU safe.)
136 */
137 struct rcu_head rcu_head;
138 /*
139 * Hierarchy of CSS ID belongs to.
140 */
141 unsigned short stack[0]; /* Array of Length (depth+1) */
142};
143
144
ddbcc7e8
PM
145/* The list of hierarchy roots */
146
147static LIST_HEAD(roots);
817929ec 148static int root_count;
ddbcc7e8
PM
149
150/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
151#define dummytop (&rootnode.top_cgroup)
152
153/* This flag indicates whether tasks in the fork and exit paths should
a043e3b2
LZ
154 * check for fork/exit handlers to call. This avoids us having to do
155 * extra work in the fork/exit path if none of the subsystems need to
156 * be called.
ddbcc7e8 157 */
8947f9d5 158static int need_forkexit_callback __read_mostly;
ddbcc7e8 159
ddbcc7e8 160/* convenient tests for these bits */
bd89aabc 161inline int cgroup_is_removed(const struct cgroup *cgrp)
ddbcc7e8 162{
bd89aabc 163 return test_bit(CGRP_REMOVED, &cgrp->flags);
ddbcc7e8
PM
164}
165
166/* bits in struct cgroupfs_root flags field */
167enum {
168 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
169};
170
e9685a03 171static int cgroup_is_releasable(const struct cgroup *cgrp)
81a6a5cd
PM
172{
173 const int bits =
bd89aabc
PM
174 (1 << CGRP_RELEASABLE) |
175 (1 << CGRP_NOTIFY_ON_RELEASE);
176 return (cgrp->flags & bits) == bits;
81a6a5cd
PM
177}
178
e9685a03 179static int notify_on_release(const struct cgroup *cgrp)
81a6a5cd 180{
bd89aabc 181 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
81a6a5cd
PM
182}
183
ddbcc7e8
PM
184/*
185 * for_each_subsys() allows you to iterate on each subsystem attached to
186 * an active hierarchy
187 */
188#define for_each_subsys(_root, _ss) \
189list_for_each_entry(_ss, &_root->subsys_list, sibling)
190
e5f6a860
LZ
191/* for_each_active_root() allows you to iterate across the active hierarchies */
192#define for_each_active_root(_root) \
ddbcc7e8
PM
193list_for_each_entry(_root, &roots, root_list)
194
81a6a5cd
PM
195/* the list of cgroups eligible for automatic release. Protected by
196 * release_list_lock */
197static LIST_HEAD(release_list);
198static DEFINE_SPINLOCK(release_list_lock);
199static void cgroup_release_agent(struct work_struct *work);
200static DECLARE_WORK(release_agent_work, cgroup_release_agent);
bd89aabc 201static void check_for_release(struct cgroup *cgrp);
81a6a5cd 202
817929ec
PM
203/* Link structure for associating css_set objects with cgroups */
204struct cg_cgroup_link {
205 /*
206 * List running through cg_cgroup_links associated with a
207 * cgroup, anchored on cgroup->css_sets
208 */
bd89aabc 209 struct list_head cgrp_link_list;
7717f7ba 210 struct cgroup *cgrp;
817929ec
PM
211 /*
212 * List running through cg_cgroup_links pointing at a
213 * single css_set object, anchored on css_set->cg_links
214 */
215 struct list_head cg_link_list;
216 struct css_set *cg;
217};
218
219/* The default css_set - used by init and its children prior to any
220 * hierarchies being mounted. It contains a pointer to the root state
221 * for each subsystem. Also used to anchor the list of css_sets. Not
222 * reference-counted, to improve performance when child cgroups
223 * haven't been created.
224 */
225
226static struct css_set init_css_set;
227static struct cg_cgroup_link init_css_set_link;
228
38460b48
KH
229static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
230
817929ec
PM
231/* css_set_lock protects the list of css_set objects, and the
232 * chain of tasks off each css_set. Nests outside task->alloc_lock
233 * due to cgroup_iter_start() */
234static DEFINE_RWLOCK(css_set_lock);
235static int css_set_count;
236
7717f7ba
PM
237/*
238 * hash table for cgroup groups. This improves the performance to find
239 * an existing css_set. This hash doesn't (currently) take into
240 * account cgroups in empty hierarchies.
241 */
472b1053
LZ
242#define CSS_SET_HASH_BITS 7
243#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
244static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
245
246static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
247{
248 int i;
249 int index;
250 unsigned long tmp = 0UL;
251
252 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
253 tmp += (unsigned long)css[i];
254 tmp = (tmp >> 16) ^ tmp;
255
256 index = hash_long(tmp, CSS_SET_HASH_BITS);
257
258 return &css_set_table[index];
259}
260
817929ec
PM
261/* We don't maintain the lists running through each css_set to its
262 * task until after the first call to cgroup_iter_start(). This
263 * reduces the fork()/exit() overhead for people who have cgroups
264 * compiled into their kernel but not actually in use */
8947f9d5 265static int use_task_css_set_links __read_mostly;
817929ec
PM
266
267/* When we create or destroy a css_set, the operation simply
268 * takes/releases a reference count on all the cgroups referenced
269 * by subsystems in this css_set. This can end up multiple-counting
270 * some cgroups, but that's OK - the ref-count is just a
271 * busy/not-busy indicator; ensuring that we only count each cgroup
272 * once would require taking a global lock to ensure that no
b4f48b63
PM
273 * subsystems moved between hierarchies while we were doing so.
274 *
275 * Possible TODO: decide at boot time based on the number of
276 * registered subsystems and the number of CPUs or NUMA nodes whether
277 * it's better for performance to ref-count every subsystem, or to
278 * take a global lock and only add one ref count to each hierarchy.
279 */
817929ec
PM
280
281/*
282 * unlink a css_set from the list and free it
283 */
81a6a5cd 284static void unlink_css_set(struct css_set *cg)
b4f48b63 285{
71cbb949
KM
286 struct cg_cgroup_link *link;
287 struct cg_cgroup_link *saved_link;
288
472b1053 289 hlist_del(&cg->hlist);
817929ec 290 css_set_count--;
71cbb949
KM
291
292 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
293 cg_link_list) {
817929ec 294 list_del(&link->cg_link_list);
bd89aabc 295 list_del(&link->cgrp_link_list);
817929ec
PM
296 kfree(link);
297 }
81a6a5cd
PM
298}
299
146aa1bd 300static void __put_css_set(struct css_set *cg, int taskexit)
81a6a5cd
PM
301{
302 int i;
146aa1bd
LJ
303 /*
304 * Ensure that the refcount doesn't hit zero while any readers
305 * can see it. Similar to atomic_dec_and_lock(), but for an
306 * rwlock
307 */
308 if (atomic_add_unless(&cg->refcount, -1, 1))
309 return;
310 write_lock(&css_set_lock);
311 if (!atomic_dec_and_test(&cg->refcount)) {
312 write_unlock(&css_set_lock);
313 return;
314 }
81a6a5cd 315 unlink_css_set(cg);
146aa1bd 316 write_unlock(&css_set_lock);
81a6a5cd
PM
317
318 rcu_read_lock();
319 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
a47295e6 320 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
bd89aabc
PM
321 if (atomic_dec_and_test(&cgrp->count) &&
322 notify_on_release(cgrp)) {
81a6a5cd 323 if (taskexit)
bd89aabc
PM
324 set_bit(CGRP_RELEASABLE, &cgrp->flags);
325 check_for_release(cgrp);
81a6a5cd
PM
326 }
327 }
328 rcu_read_unlock();
817929ec 329 kfree(cg);
b4f48b63
PM
330}
331
817929ec
PM
332/*
333 * refcounted get/put for css_set objects
334 */
335static inline void get_css_set(struct css_set *cg)
336{
146aa1bd 337 atomic_inc(&cg->refcount);
817929ec
PM
338}
339
340static inline void put_css_set(struct css_set *cg)
341{
146aa1bd 342 __put_css_set(cg, 0);
817929ec
PM
343}
344
81a6a5cd
PM
345static inline void put_css_set_taskexit(struct css_set *cg)
346{
146aa1bd 347 __put_css_set(cg, 1);
81a6a5cd
PM
348}
349
7717f7ba
PM
350/*
351 * compare_css_sets - helper function for find_existing_css_set().
352 * @cg: candidate css_set being tested
353 * @old_cg: existing css_set for a task
354 * @new_cgrp: cgroup that's being entered by the task
355 * @template: desired set of css pointers in css_set (pre-calculated)
356 *
357 * Returns true if "cg" matches "old_cg" except for the hierarchy
358 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
359 */
360static bool compare_css_sets(struct css_set *cg,
361 struct css_set *old_cg,
362 struct cgroup *new_cgrp,
363 struct cgroup_subsys_state *template[])
364{
365 struct list_head *l1, *l2;
366
367 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
368 /* Not all subsystems matched */
369 return false;
370 }
371
372 /*
373 * Compare cgroup pointers in order to distinguish between
374 * different cgroups in heirarchies with no subsystems. We
375 * could get by with just this check alone (and skip the
376 * memcmp above) but on most setups the memcmp check will
377 * avoid the need for this more expensive check on almost all
378 * candidates.
379 */
380
381 l1 = &cg->cg_links;
382 l2 = &old_cg->cg_links;
383 while (1) {
384 struct cg_cgroup_link *cgl1, *cgl2;
385 struct cgroup *cg1, *cg2;
386
387 l1 = l1->next;
388 l2 = l2->next;
389 /* See if we reached the end - both lists are equal length. */
390 if (l1 == &cg->cg_links) {
391 BUG_ON(l2 != &old_cg->cg_links);
392 break;
393 } else {
394 BUG_ON(l2 == &old_cg->cg_links);
395 }
396 /* Locate the cgroups associated with these links. */
397 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
398 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
399 cg1 = cgl1->cgrp;
400 cg2 = cgl2->cgrp;
401 /* Hierarchies should be linked in the same order. */
402 BUG_ON(cg1->root != cg2->root);
403
404 /*
405 * If this hierarchy is the hierarchy of the cgroup
406 * that's changing, then we need to check that this
407 * css_set points to the new cgroup; if it's any other
408 * hierarchy, then this css_set should point to the
409 * same cgroup as the old css_set.
410 */
411 if (cg1->root == new_cgrp->root) {
412 if (cg1 != new_cgrp)
413 return false;
414 } else {
415 if (cg1 != cg2)
416 return false;
417 }
418 }
419 return true;
420}
421
817929ec
PM
422/*
423 * find_existing_css_set() is a helper for
424 * find_css_set(), and checks to see whether an existing
472b1053 425 * css_set is suitable.
817929ec
PM
426 *
427 * oldcg: the cgroup group that we're using before the cgroup
428 * transition
429 *
bd89aabc 430 * cgrp: the cgroup that we're moving into
817929ec
PM
431 *
432 * template: location in which to build the desired set of subsystem
433 * state objects for the new cgroup group
434 */
817929ec
PM
435static struct css_set *find_existing_css_set(
436 struct css_set *oldcg,
bd89aabc 437 struct cgroup *cgrp,
817929ec 438 struct cgroup_subsys_state *template[])
b4f48b63
PM
439{
440 int i;
bd89aabc 441 struct cgroupfs_root *root = cgrp->root;
472b1053
LZ
442 struct hlist_head *hhead;
443 struct hlist_node *node;
444 struct css_set *cg;
817929ec
PM
445
446 /* Built the set of subsystem state objects that we want to
447 * see in the new css_set */
448 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
8d53d55d 449 if (root->subsys_bits & (1UL << i)) {
817929ec
PM
450 /* Subsystem is in this hierarchy. So we want
451 * the subsystem state from the new
452 * cgroup */
bd89aabc 453 template[i] = cgrp->subsys[i];
817929ec
PM
454 } else {
455 /* Subsystem is not in this hierarchy, so we
456 * don't want to change the subsystem state */
457 template[i] = oldcg->subsys[i];
458 }
459 }
460
472b1053
LZ
461 hhead = css_set_hash(template);
462 hlist_for_each_entry(cg, node, hhead, hlist) {
7717f7ba
PM
463 if (!compare_css_sets(cg, oldcg, cgrp, template))
464 continue;
465
466 /* This css_set matches what we need */
467 return cg;
472b1053 468 }
817929ec
PM
469
470 /* No existing cgroup group matched */
471 return NULL;
472}
473
36553434
LZ
474static void free_cg_links(struct list_head *tmp)
475{
476 struct cg_cgroup_link *link;
477 struct cg_cgroup_link *saved_link;
478
479 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
480 list_del(&link->cgrp_link_list);
481 kfree(link);
482 }
483}
484
817929ec
PM
485/*
486 * allocate_cg_links() allocates "count" cg_cgroup_link structures
bd89aabc 487 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
817929ec
PM
488 * success or a negative error
489 */
817929ec
PM
490static int allocate_cg_links(int count, struct list_head *tmp)
491{
492 struct cg_cgroup_link *link;
493 int i;
494 INIT_LIST_HEAD(tmp);
495 for (i = 0; i < count; i++) {
496 link = kmalloc(sizeof(*link), GFP_KERNEL);
497 if (!link) {
36553434 498 free_cg_links(tmp);
817929ec
PM
499 return -ENOMEM;
500 }
bd89aabc 501 list_add(&link->cgrp_link_list, tmp);
817929ec
PM
502 }
503 return 0;
504}
505
c12f65d4
LZ
506/**
507 * link_css_set - a helper function to link a css_set to a cgroup
508 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
509 * @cg: the css_set to be linked
510 * @cgrp: the destination cgroup
511 */
512static void link_css_set(struct list_head *tmp_cg_links,
513 struct css_set *cg, struct cgroup *cgrp)
514{
515 struct cg_cgroup_link *link;
516
517 BUG_ON(list_empty(tmp_cg_links));
518 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
519 cgrp_link_list);
520 link->cg = cg;
7717f7ba 521 link->cgrp = cgrp;
c12f65d4 522 list_move(&link->cgrp_link_list, &cgrp->css_sets);
7717f7ba
PM
523 /*
524 * Always add links to the tail of the list so that the list
525 * is sorted by order of hierarchy creation
526 */
527 list_add_tail(&link->cg_link_list, &cg->cg_links);
c12f65d4
LZ
528}
529
817929ec
PM
530/*
531 * find_css_set() takes an existing cgroup group and a
532 * cgroup object, and returns a css_set object that's
533 * equivalent to the old group, but with the given cgroup
534 * substituted into the appropriate hierarchy. Must be called with
535 * cgroup_mutex held
536 */
817929ec 537static struct css_set *find_css_set(
bd89aabc 538 struct css_set *oldcg, struct cgroup *cgrp)
817929ec
PM
539{
540 struct css_set *res;
541 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
542 int i;
543
544 struct list_head tmp_cg_links;
817929ec 545
472b1053 546 struct hlist_head *hhead;
7717f7ba 547 struct cg_cgroup_link *link;
472b1053 548
817929ec
PM
549 /* First see if we already have a cgroup group that matches
550 * the desired set */
7e9abd89 551 read_lock(&css_set_lock);
bd89aabc 552 res = find_existing_css_set(oldcg, cgrp, template);
817929ec
PM
553 if (res)
554 get_css_set(res);
7e9abd89 555 read_unlock(&css_set_lock);
817929ec
PM
556
557 if (res)
558 return res;
559
560 res = kmalloc(sizeof(*res), GFP_KERNEL);
561 if (!res)
562 return NULL;
563
564 /* Allocate all the cg_cgroup_link objects that we'll need */
565 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
566 kfree(res);
567 return NULL;
568 }
569
146aa1bd 570 atomic_set(&res->refcount, 1);
817929ec
PM
571 INIT_LIST_HEAD(&res->cg_links);
572 INIT_LIST_HEAD(&res->tasks);
472b1053 573 INIT_HLIST_NODE(&res->hlist);
817929ec
PM
574
575 /* Copy the set of subsystem state objects generated in
576 * find_existing_css_set() */
577 memcpy(res->subsys, template, sizeof(res->subsys));
578
579 write_lock(&css_set_lock);
580 /* Add reference counts and links from the new css_set. */
581 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
bd89aabc 582 struct cgroup *cgrp = res->subsys[i]->cgroup;
bd89aabc 583 atomic_inc(&cgrp->count);
817929ec 584 }
7717f7ba
PM
585 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
586 struct cgroup *c = link->cgrp;
587 if (c->root == cgrp->root)
588 c = cgrp;
589 link_css_set(&tmp_cg_links, res, c);
590 }
817929ec
PM
591
592 BUG_ON(!list_empty(&tmp_cg_links));
593
817929ec 594 css_set_count++;
472b1053
LZ
595
596 /* Add this cgroup group to the hash table */
597 hhead = css_set_hash(res->subsys);
598 hlist_add_head(&res->hlist, hhead);
599
817929ec
PM
600 write_unlock(&css_set_lock);
601
602 return res;
b4f48b63
PM
603}
604
7717f7ba
PM
605/*
606 * Return the cgroup for "task" from the given hierarchy. Must be
607 * called with cgroup_mutex held.
608 */
609static struct cgroup *task_cgroup_from_root(struct task_struct *task,
610 struct cgroupfs_root *root)
611{
612 struct css_set *css;
613 struct cgroup *res = NULL;
614
615 BUG_ON(!mutex_is_locked(&cgroup_mutex));
616 read_lock(&css_set_lock);
617 /*
618 * No need to lock the task - since we hold cgroup_mutex the
619 * task can't change groups, so the only thing that can happen
620 * is that it exits and its css is set back to init_css_set.
621 */
622 css = task->cgroups;
623 if (css == &init_css_set) {
624 res = &root->top_cgroup;
625 } else {
626 struct cg_cgroup_link *link;
627 list_for_each_entry(link, &css->cg_links, cg_link_list) {
628 struct cgroup *c = link->cgrp;
629 if (c->root == root) {
630 res = c;
631 break;
632 }
633 }
634 }
635 read_unlock(&css_set_lock);
636 BUG_ON(!res);
637 return res;
638}
639
ddbcc7e8
PM
640/*
641 * There is one global cgroup mutex. We also require taking
642 * task_lock() when dereferencing a task's cgroup subsys pointers.
643 * See "The task_lock() exception", at the end of this comment.
644 *
645 * A task must hold cgroup_mutex to modify cgroups.
646 *
647 * Any task can increment and decrement the count field without lock.
648 * So in general, code holding cgroup_mutex can't rely on the count
649 * field not changing. However, if the count goes to zero, then only
956db3ca 650 * cgroup_attach_task() can increment it again. Because a count of zero
ddbcc7e8
PM
651 * means that no tasks are currently attached, therefore there is no
652 * way a task attached to that cgroup can fork (the other way to
653 * increment the count). So code holding cgroup_mutex can safely
654 * assume that if the count is zero, it will stay zero. Similarly, if
655 * a task holds cgroup_mutex on a cgroup with zero count, it
656 * knows that the cgroup won't be removed, as cgroup_rmdir()
657 * needs that mutex.
658 *
ddbcc7e8
PM
659 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
660 * (usually) take cgroup_mutex. These are the two most performance
661 * critical pieces of code here. The exception occurs on cgroup_exit(),
662 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
663 * is taken, and if the cgroup count is zero, a usermode call made
a043e3b2
LZ
664 * to the release agent with the name of the cgroup (path relative to
665 * the root of cgroup file system) as the argument.
ddbcc7e8
PM
666 *
667 * A cgroup can only be deleted if both its 'count' of using tasks
668 * is zero, and its list of 'children' cgroups is empty. Since all
669 * tasks in the system use _some_ cgroup, and since there is always at
670 * least one task in the system (init, pid == 1), therefore, top_cgroup
671 * always has either children cgroups and/or using tasks. So we don't
672 * need a special hack to ensure that top_cgroup cannot be deleted.
673 *
674 * The task_lock() exception
675 *
676 * The need for this exception arises from the action of
956db3ca 677 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
a043e3b2 678 * another. It does so using cgroup_mutex, however there are
ddbcc7e8
PM
679 * several performance critical places that need to reference
680 * task->cgroup without the expense of grabbing a system global
681 * mutex. Therefore except as noted below, when dereferencing or, as
956db3ca 682 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
ddbcc7e8
PM
683 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
684 * the task_struct routinely used for such matters.
685 *
686 * P.S. One more locking exception. RCU is used to guard the
956db3ca 687 * update of a tasks cgroup pointer by cgroup_attach_task()
ddbcc7e8
PM
688 */
689
ddbcc7e8
PM
690/**
691 * cgroup_lock - lock out any changes to cgroup structures
692 *
693 */
ddbcc7e8
PM
694void cgroup_lock(void)
695{
696 mutex_lock(&cgroup_mutex);
697}
698
699/**
700 * cgroup_unlock - release lock on cgroup changes
701 *
702 * Undo the lock taken in a previous cgroup_lock() call.
703 */
ddbcc7e8
PM
704void cgroup_unlock(void)
705{
706 mutex_unlock(&cgroup_mutex);
707}
708
709/*
710 * A couple of forward declarations required, due to cyclic reference loop:
711 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
712 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
713 * -> cgroup_mkdir.
714 */
715
716static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
717static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
bd89aabc 718static int cgroup_populate_dir(struct cgroup *cgrp);
6e1d5dcc 719static const struct inode_operations cgroup_dir_inode_operations;
a424316c
PM
720static struct file_operations proc_cgroupstats_operations;
721
722static struct backing_dev_info cgroup_backing_dev_info = {
d993831f 723 .name = "cgroup",
e4ad08fe 724 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
a424316c 725};
ddbcc7e8 726
38460b48
KH
727static int alloc_css_id(struct cgroup_subsys *ss,
728 struct cgroup *parent, struct cgroup *child);
729
ddbcc7e8
PM
730static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
731{
732 struct inode *inode = new_inode(sb);
ddbcc7e8
PM
733
734 if (inode) {
735 inode->i_mode = mode;
76aac0e9
DH
736 inode->i_uid = current_fsuid();
737 inode->i_gid = current_fsgid();
ddbcc7e8
PM
738 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
739 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
740 }
741 return inode;
742}
743
4fca88c8
KH
744/*
745 * Call subsys's pre_destroy handler.
746 * This is called before css refcnt check.
747 */
ec64f515 748static int cgroup_call_pre_destroy(struct cgroup *cgrp)
4fca88c8
KH
749{
750 struct cgroup_subsys *ss;
ec64f515
KH
751 int ret = 0;
752
4fca88c8 753 for_each_subsys(cgrp->root, ss)
ec64f515
KH
754 if (ss->pre_destroy) {
755 ret = ss->pre_destroy(ss, cgrp);
756 if (ret)
757 break;
758 }
759 return ret;
4fca88c8
KH
760}
761
a47295e6
PM
762static void free_cgroup_rcu(struct rcu_head *obj)
763{
764 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
765
766 kfree(cgrp);
767}
768
ddbcc7e8
PM
769static void cgroup_diput(struct dentry *dentry, struct inode *inode)
770{
771 /* is dentry a directory ? if so, kfree() associated cgroup */
772 if (S_ISDIR(inode->i_mode)) {
bd89aabc 773 struct cgroup *cgrp = dentry->d_fsdata;
8dc4f3e1 774 struct cgroup_subsys *ss;
bd89aabc 775 BUG_ON(!(cgroup_is_removed(cgrp)));
81a6a5cd
PM
776 /* It's possible for external users to be holding css
777 * reference counts on a cgroup; css_put() needs to
778 * be able to access the cgroup after decrementing
779 * the reference count in order to know if it needs to
780 * queue the cgroup to be handled by the release
781 * agent */
782 synchronize_rcu();
8dc4f3e1
PM
783
784 mutex_lock(&cgroup_mutex);
785 /*
786 * Release the subsystem state objects.
787 */
75139b82
LZ
788 for_each_subsys(cgrp->root, ss)
789 ss->destroy(ss, cgrp);
8dc4f3e1
PM
790
791 cgrp->root->number_of_cgroups--;
792 mutex_unlock(&cgroup_mutex);
793
a47295e6
PM
794 /*
795 * Drop the active superblock reference that we took when we
796 * created the cgroup
797 */
8dc4f3e1
PM
798 deactivate_super(cgrp->root->sb);
799
a47295e6 800 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
ddbcc7e8
PM
801 }
802 iput(inode);
803}
804
805static void remove_dir(struct dentry *d)
806{
807 struct dentry *parent = dget(d->d_parent);
808
809 d_delete(d);
810 simple_rmdir(parent->d_inode, d);
811 dput(parent);
812}
813
814static void cgroup_clear_directory(struct dentry *dentry)
815{
816 struct list_head *node;
817
818 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
819 spin_lock(&dcache_lock);
820 node = dentry->d_subdirs.next;
821 while (node != &dentry->d_subdirs) {
822 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
823 list_del_init(node);
824 if (d->d_inode) {
825 /* This should never be called on a cgroup
826 * directory with child cgroups */
827 BUG_ON(d->d_inode->i_mode & S_IFDIR);
828 d = dget_locked(d);
829 spin_unlock(&dcache_lock);
830 d_delete(d);
831 simple_unlink(dentry->d_inode, d);
832 dput(d);
833 spin_lock(&dcache_lock);
834 }
835 node = dentry->d_subdirs.next;
836 }
837 spin_unlock(&dcache_lock);
838}
839
840/*
841 * NOTE : the dentry must have been dget()'ed
842 */
843static void cgroup_d_remove_dir(struct dentry *dentry)
844{
845 cgroup_clear_directory(dentry);
846
847 spin_lock(&dcache_lock);
848 list_del_init(&dentry->d_u.d_child);
849 spin_unlock(&dcache_lock);
850 remove_dir(dentry);
851}
852
ec64f515
KH
853/*
854 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
855 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
856 * reference to css->refcnt. In general, this refcnt is expected to goes down
857 * to zero, soon.
858 *
88703267 859 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
ec64f515
KH
860 */
861DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
862
88703267 863static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
ec64f515 864{
88703267 865 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
ec64f515
KH
866 wake_up_all(&cgroup_rmdir_waitq);
867}
868
88703267
KH
869void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
870{
871 css_get(css);
872}
873
874void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
875{
876 cgroup_wakeup_rmdir_waiter(css->cgroup);
877 css_put(css);
878}
879
880
ddbcc7e8
PM
881static int rebind_subsystems(struct cgroupfs_root *root,
882 unsigned long final_bits)
883{
884 unsigned long added_bits, removed_bits;
bd89aabc 885 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
886 int i;
887
888 removed_bits = root->actual_subsys_bits & ~final_bits;
889 added_bits = final_bits & ~root->actual_subsys_bits;
890 /* Check that any added subsystems are currently free */
891 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
8d53d55d 892 unsigned long bit = 1UL << i;
ddbcc7e8
PM
893 struct cgroup_subsys *ss = subsys[i];
894 if (!(bit & added_bits))
895 continue;
896 if (ss->root != &rootnode) {
897 /* Subsystem isn't free */
898 return -EBUSY;
899 }
900 }
901
902 /* Currently we don't handle adding/removing subsystems when
903 * any child cgroups exist. This is theoretically supportable
904 * but involves complex error handling, so it's being left until
905 * later */
307257cf 906 if (root->number_of_cgroups > 1)
ddbcc7e8
PM
907 return -EBUSY;
908
909 /* Process each subsystem */
910 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
911 struct cgroup_subsys *ss = subsys[i];
912 unsigned long bit = 1UL << i;
913 if (bit & added_bits) {
914 /* We're binding this subsystem to this hierarchy */
bd89aabc 915 BUG_ON(cgrp->subsys[i]);
ddbcc7e8
PM
916 BUG_ON(!dummytop->subsys[i]);
917 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
999cd8a4 918 mutex_lock(&ss->hierarchy_mutex);
bd89aabc
PM
919 cgrp->subsys[i] = dummytop->subsys[i];
920 cgrp->subsys[i]->cgroup = cgrp;
33a68ac1 921 list_move(&ss->sibling, &root->subsys_list);
b2aa30f7 922 ss->root = root;
ddbcc7e8 923 if (ss->bind)
bd89aabc 924 ss->bind(ss, cgrp);
999cd8a4 925 mutex_unlock(&ss->hierarchy_mutex);
ddbcc7e8
PM
926 } else if (bit & removed_bits) {
927 /* We're removing this subsystem */
bd89aabc
PM
928 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
929 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
999cd8a4 930 mutex_lock(&ss->hierarchy_mutex);
ddbcc7e8
PM
931 if (ss->bind)
932 ss->bind(ss, dummytop);
933 dummytop->subsys[i]->cgroup = dummytop;
bd89aabc 934 cgrp->subsys[i] = NULL;
b2aa30f7 935 subsys[i]->root = &rootnode;
33a68ac1 936 list_move(&ss->sibling, &rootnode.subsys_list);
999cd8a4 937 mutex_unlock(&ss->hierarchy_mutex);
ddbcc7e8
PM
938 } else if (bit & final_bits) {
939 /* Subsystem state should already exist */
bd89aabc 940 BUG_ON(!cgrp->subsys[i]);
ddbcc7e8
PM
941 } else {
942 /* Subsystem state shouldn't exist */
bd89aabc 943 BUG_ON(cgrp->subsys[i]);
ddbcc7e8
PM
944 }
945 }
946 root->subsys_bits = root->actual_subsys_bits = final_bits;
947 synchronize_rcu();
948
949 return 0;
950}
951
952static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
953{
954 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
955 struct cgroup_subsys *ss;
956
957 mutex_lock(&cgroup_mutex);
958 for_each_subsys(root, ss)
959 seq_printf(seq, ",%s", ss->name);
960 if (test_bit(ROOT_NOPREFIX, &root->flags))
961 seq_puts(seq, ",noprefix");
81a6a5cd
PM
962 if (strlen(root->release_agent_path))
963 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
c6d57f33
PM
964 if (strlen(root->name))
965 seq_printf(seq, ",name=%s", root->name);
ddbcc7e8
PM
966 mutex_unlock(&cgroup_mutex);
967 return 0;
968}
969
970struct cgroup_sb_opts {
971 unsigned long subsys_bits;
972 unsigned long flags;
81a6a5cd 973 char *release_agent;
c6d57f33
PM
974 char *name;
975
976 struct cgroupfs_root *new_root;
ddbcc7e8
PM
977};
978
979/* Convert a hierarchy specifier into a bitmask of subsystems and
980 * flags. */
981static int parse_cgroupfs_options(char *data,
982 struct cgroup_sb_opts *opts)
983{
984 char *token, *o = data ?: "all";
f9ab5b5b
LZ
985 unsigned long mask = (unsigned long)-1;
986
987#ifdef CONFIG_CPUSETS
988 mask = ~(1UL << cpuset_subsys_id);
989#endif
ddbcc7e8 990
c6d57f33 991 memset(opts, 0, sizeof(*opts));
ddbcc7e8
PM
992
993 while ((token = strsep(&o, ",")) != NULL) {
994 if (!*token)
995 return -EINVAL;
996 if (!strcmp(token, "all")) {
8bab8dde
PM
997 /* Add all non-disabled subsystems */
998 int i;
999 opts->subsys_bits = 0;
1000 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1001 struct cgroup_subsys *ss = subsys[i];
1002 if (!ss->disabled)
1003 opts->subsys_bits |= 1ul << i;
1004 }
ddbcc7e8
PM
1005 } else if (!strcmp(token, "noprefix")) {
1006 set_bit(ROOT_NOPREFIX, &opts->flags);
81a6a5cd
PM
1007 } else if (!strncmp(token, "release_agent=", 14)) {
1008 /* Specifying two release agents is forbidden */
1009 if (opts->release_agent)
1010 return -EINVAL;
c6d57f33
PM
1011 opts->release_agent =
1012 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
81a6a5cd
PM
1013 if (!opts->release_agent)
1014 return -ENOMEM;
c6d57f33
PM
1015 } else if (!strncmp(token, "name=", 5)) {
1016 int i;
1017 const char *name = token + 5;
1018 /* Can't specify an empty name */
1019 if (!strlen(name))
1020 return -EINVAL;
1021 /* Must match [\w.-]+ */
1022 for (i = 0; i < strlen(name); i++) {
1023 char c = name[i];
1024 if (isalnum(c))
1025 continue;
1026 if ((c == '.') || (c == '-') || (c == '_'))
1027 continue;
1028 return -EINVAL;
1029 }
1030 /* Specifying two names is forbidden */
1031 if (opts->name)
1032 return -EINVAL;
1033 opts->name = kstrndup(name,
1034 MAX_CGROUP_ROOT_NAMELEN,
1035 GFP_KERNEL);
1036 if (!opts->name)
1037 return -ENOMEM;
ddbcc7e8
PM
1038 } else {
1039 struct cgroup_subsys *ss;
1040 int i;
1041 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1042 ss = subsys[i];
1043 if (!strcmp(token, ss->name)) {
8bab8dde
PM
1044 if (!ss->disabled)
1045 set_bit(i, &opts->subsys_bits);
ddbcc7e8
PM
1046 break;
1047 }
1048 }
1049 if (i == CGROUP_SUBSYS_COUNT)
1050 return -ENOENT;
1051 }
1052 }
1053
f9ab5b5b
LZ
1054 /*
1055 * Option noprefix was introduced just for backward compatibility
1056 * with the old cpuset, so we allow noprefix only if mounting just
1057 * the cpuset subsystem.
1058 */
1059 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1060 (opts->subsys_bits & mask))
1061 return -EINVAL;
1062
ddbcc7e8 1063 /* We can't have an empty hierarchy */
c6d57f33 1064 if (!opts->subsys_bits && !opts->name)
ddbcc7e8
PM
1065 return -EINVAL;
1066
1067 return 0;
1068}
1069
1070static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1071{
1072 int ret = 0;
1073 struct cgroupfs_root *root = sb->s_fs_info;
bd89aabc 1074 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
1075 struct cgroup_sb_opts opts;
1076
337eb00a 1077 lock_kernel();
bd89aabc 1078 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
1079 mutex_lock(&cgroup_mutex);
1080
1081 /* See what subsystems are wanted */
1082 ret = parse_cgroupfs_options(data, &opts);
1083 if (ret)
1084 goto out_unlock;
1085
1086 /* Don't allow flags to change at remount */
1087 if (opts.flags != root->flags) {
1088 ret = -EINVAL;
1089 goto out_unlock;
1090 }
1091
c6d57f33
PM
1092 /* Don't allow name to change at remount */
1093 if (opts.name && strcmp(opts.name, root->name)) {
1094 ret = -EINVAL;
1095 goto out_unlock;
1096 }
1097
ddbcc7e8 1098 ret = rebind_subsystems(root, opts.subsys_bits);
0670e08b
LZ
1099 if (ret)
1100 goto out_unlock;
ddbcc7e8
PM
1101
1102 /* (re)populate subsystem files */
0670e08b 1103 cgroup_populate_dir(cgrp);
ddbcc7e8 1104
81a6a5cd
PM
1105 if (opts.release_agent)
1106 strcpy(root->release_agent_path, opts.release_agent);
ddbcc7e8 1107 out_unlock:
66bdc9cf 1108 kfree(opts.release_agent);
c6d57f33 1109 kfree(opts.name);
ddbcc7e8 1110 mutex_unlock(&cgroup_mutex);
bd89aabc 1111 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
337eb00a 1112 unlock_kernel();
ddbcc7e8
PM
1113 return ret;
1114}
1115
b87221de 1116static const struct super_operations cgroup_ops = {
ddbcc7e8
PM
1117 .statfs = simple_statfs,
1118 .drop_inode = generic_delete_inode,
1119 .show_options = cgroup_show_options,
1120 .remount_fs = cgroup_remount,
1121};
1122
cc31edce
PM
1123static void init_cgroup_housekeeping(struct cgroup *cgrp)
1124{
1125 INIT_LIST_HEAD(&cgrp->sibling);
1126 INIT_LIST_HEAD(&cgrp->children);
1127 INIT_LIST_HEAD(&cgrp->css_sets);
1128 INIT_LIST_HEAD(&cgrp->release_list);
096b7fe0 1129 INIT_LIST_HEAD(&cgrp->pids_list);
cc31edce
PM
1130 init_rwsem(&cgrp->pids_mutex);
1131}
c6d57f33 1132
ddbcc7e8
PM
1133static void init_cgroup_root(struct cgroupfs_root *root)
1134{
bd89aabc 1135 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
1136 INIT_LIST_HEAD(&root->subsys_list);
1137 INIT_LIST_HEAD(&root->root_list);
1138 root->number_of_cgroups = 1;
bd89aabc
PM
1139 cgrp->root = root;
1140 cgrp->top_cgroup = cgrp;
cc31edce 1141 init_cgroup_housekeeping(cgrp);
ddbcc7e8
PM
1142}
1143
1144static int cgroup_test_super(struct super_block *sb, void *data)
1145{
c6d57f33 1146 struct cgroup_sb_opts *opts = data;
ddbcc7e8
PM
1147 struct cgroupfs_root *root = sb->s_fs_info;
1148
c6d57f33
PM
1149 /* If we asked for a name then it must match */
1150 if (opts->name && strcmp(opts->name, root->name))
1151 return 0;
ddbcc7e8 1152
c6d57f33
PM
1153 /* If we asked for subsystems then they must match */
1154 if (opts->subsys_bits && (opts->subsys_bits != root->subsys_bits))
ddbcc7e8
PM
1155 return 0;
1156
1157 return 1;
1158}
1159
c6d57f33
PM
1160static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1161{
1162 struct cgroupfs_root *root;
1163
1164 /* Empty hierarchies aren't supported */
1165 if (!opts->subsys_bits)
1166 return NULL;
1167
1168 root = kzalloc(sizeof(*root), GFP_KERNEL);
1169 if (!root)
1170 return ERR_PTR(-ENOMEM);
1171
1172 init_cgroup_root(root);
1173 root->subsys_bits = opts->subsys_bits;
1174 root->flags = opts->flags;
1175 if (opts->release_agent)
1176 strcpy(root->release_agent_path, opts->release_agent);
1177 if (opts->name)
1178 strcpy(root->name, opts->name);
1179 return root;
1180}
1181
ddbcc7e8
PM
1182static int cgroup_set_super(struct super_block *sb, void *data)
1183{
1184 int ret;
c6d57f33
PM
1185 struct cgroup_sb_opts *opts = data;
1186
1187 /* If we don't have a new root, we can't set up a new sb */
1188 if (!opts->new_root)
1189 return -EINVAL;
1190
1191 BUG_ON(!opts->subsys_bits);
ddbcc7e8
PM
1192
1193 ret = set_anon_super(sb, NULL);
1194 if (ret)
1195 return ret;
1196
c6d57f33
PM
1197 sb->s_fs_info = opts->new_root;
1198 opts->new_root->sb = sb;
ddbcc7e8
PM
1199
1200 sb->s_blocksize = PAGE_CACHE_SIZE;
1201 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1202 sb->s_magic = CGROUP_SUPER_MAGIC;
1203 sb->s_op = &cgroup_ops;
1204
1205 return 0;
1206}
1207
1208static int cgroup_get_rootdir(struct super_block *sb)
1209{
1210 struct inode *inode =
1211 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1212 struct dentry *dentry;
1213
1214 if (!inode)
1215 return -ENOMEM;
1216
ddbcc7e8
PM
1217 inode->i_fop = &simple_dir_operations;
1218 inode->i_op = &cgroup_dir_inode_operations;
1219 /* directories start off with i_nlink == 2 (for "." entry) */
1220 inc_nlink(inode);
1221 dentry = d_alloc_root(inode);
1222 if (!dentry) {
1223 iput(inode);
1224 return -ENOMEM;
1225 }
1226 sb->s_root = dentry;
1227 return 0;
1228}
1229
1230static int cgroup_get_sb(struct file_system_type *fs_type,
1231 int flags, const char *unused_dev_name,
1232 void *data, struct vfsmount *mnt)
1233{
1234 struct cgroup_sb_opts opts;
c6d57f33 1235 struct cgroupfs_root *root;
ddbcc7e8
PM
1236 int ret = 0;
1237 struct super_block *sb;
c6d57f33 1238 struct cgroupfs_root *new_root;
ddbcc7e8
PM
1239
1240 /* First find the desired set of subsystems */
1241 ret = parse_cgroupfs_options(data, &opts);
c6d57f33
PM
1242 if (ret)
1243 goto out_err;
ddbcc7e8 1244
c6d57f33
PM
1245 /*
1246 * Allocate a new cgroup root. We may not need it if we're
1247 * reusing an existing hierarchy.
1248 */
1249 new_root = cgroup_root_from_opts(&opts);
1250 if (IS_ERR(new_root)) {
1251 ret = PTR_ERR(new_root);
1252 goto out_err;
81a6a5cd 1253 }
c6d57f33 1254 opts.new_root = new_root;
ddbcc7e8 1255
c6d57f33
PM
1256 /* Locate an existing or new sb for this hierarchy */
1257 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
ddbcc7e8 1258 if (IS_ERR(sb)) {
c6d57f33
PM
1259 ret = PTR_ERR(sb);
1260 kfree(opts.new_root);
1261 goto out_err;
ddbcc7e8
PM
1262 }
1263
c6d57f33
PM
1264 root = sb->s_fs_info;
1265 BUG_ON(!root);
1266 if (root == opts.new_root) {
1267 /* We used the new root structure, so this is a new hierarchy */
1268 struct list_head tmp_cg_links;
c12f65d4 1269 struct cgroup *root_cgrp = &root->top_cgroup;
817929ec 1270 struct inode *inode;
c6d57f33 1271 struct cgroupfs_root *existing_root;
28fd5dfc 1272 int i;
ddbcc7e8
PM
1273
1274 BUG_ON(sb->s_root != NULL);
1275
1276 ret = cgroup_get_rootdir(sb);
1277 if (ret)
1278 goto drop_new_super;
817929ec 1279 inode = sb->s_root->d_inode;
ddbcc7e8 1280
817929ec 1281 mutex_lock(&inode->i_mutex);
ddbcc7e8
PM
1282 mutex_lock(&cgroup_mutex);
1283
c6d57f33
PM
1284 if (strlen(root->name)) {
1285 /* Check for name clashes with existing mounts */
1286 for_each_active_root(existing_root) {
1287 if (!strcmp(existing_root->name, root->name)) {
1288 ret = -EBUSY;
1289 mutex_unlock(&cgroup_mutex);
1290 mutex_unlock(&inode->i_mutex);
1291 goto drop_new_super;
1292 }
1293 }
1294 }
1295
817929ec
PM
1296 /*
1297 * We're accessing css_set_count without locking
1298 * css_set_lock here, but that's OK - it can only be
1299 * increased by someone holding cgroup_lock, and
1300 * that's us. The worst that can happen is that we
1301 * have some link structures left over
1302 */
1303 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1304 if (ret) {
1305 mutex_unlock(&cgroup_mutex);
1306 mutex_unlock(&inode->i_mutex);
1307 goto drop_new_super;
1308 }
1309
ddbcc7e8
PM
1310 ret = rebind_subsystems(root, root->subsys_bits);
1311 if (ret == -EBUSY) {
1312 mutex_unlock(&cgroup_mutex);
817929ec 1313 mutex_unlock(&inode->i_mutex);
c6d57f33
PM
1314 free_cg_links(&tmp_cg_links);
1315 goto drop_new_super;
ddbcc7e8
PM
1316 }
1317
1318 /* EBUSY should be the only error here */
1319 BUG_ON(ret);
1320
1321 list_add(&root->root_list, &roots);
817929ec 1322 root_count++;
ddbcc7e8 1323
c12f65d4 1324 sb->s_root->d_fsdata = root_cgrp;
ddbcc7e8
PM
1325 root->top_cgroup.dentry = sb->s_root;
1326
817929ec
PM
1327 /* Link the top cgroup in this hierarchy into all
1328 * the css_set objects */
1329 write_lock(&css_set_lock);
28fd5dfc
LZ
1330 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1331 struct hlist_head *hhead = &css_set_table[i];
1332 struct hlist_node *node;
817929ec 1333 struct css_set *cg;
28fd5dfc 1334
c12f65d4
LZ
1335 hlist_for_each_entry(cg, node, hhead, hlist)
1336 link_css_set(&tmp_cg_links, cg, root_cgrp);
28fd5dfc 1337 }
817929ec
PM
1338 write_unlock(&css_set_lock);
1339
1340 free_cg_links(&tmp_cg_links);
1341
c12f65d4
LZ
1342 BUG_ON(!list_empty(&root_cgrp->sibling));
1343 BUG_ON(!list_empty(&root_cgrp->children));
ddbcc7e8
PM
1344 BUG_ON(root->number_of_cgroups != 1);
1345
c12f65d4 1346 cgroup_populate_dir(root_cgrp);
ddbcc7e8 1347 mutex_unlock(&cgroup_mutex);
34f77a90 1348 mutex_unlock(&inode->i_mutex);
c6d57f33
PM
1349 } else {
1350 /*
1351 * We re-used an existing hierarchy - the new root (if
1352 * any) is not needed
1353 */
1354 kfree(opts.new_root);
ddbcc7e8
PM
1355 }
1356
a3ec947c 1357 simple_set_mnt(mnt, sb);
c6d57f33
PM
1358 kfree(opts.release_agent);
1359 kfree(opts.name);
a3ec947c 1360 return 0;
ddbcc7e8
PM
1361
1362 drop_new_super:
6f5bbff9 1363 deactivate_locked_super(sb);
c6d57f33
PM
1364 out_err:
1365 kfree(opts.release_agent);
1366 kfree(opts.name);
1367
ddbcc7e8
PM
1368 return ret;
1369}
1370
1371static void cgroup_kill_sb(struct super_block *sb) {
1372 struct cgroupfs_root *root = sb->s_fs_info;
bd89aabc 1373 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8 1374 int ret;
71cbb949
KM
1375 struct cg_cgroup_link *link;
1376 struct cg_cgroup_link *saved_link;
ddbcc7e8
PM
1377
1378 BUG_ON(!root);
1379
1380 BUG_ON(root->number_of_cgroups != 1);
bd89aabc
PM
1381 BUG_ON(!list_empty(&cgrp->children));
1382 BUG_ON(!list_empty(&cgrp->sibling));
ddbcc7e8
PM
1383
1384 mutex_lock(&cgroup_mutex);
1385
1386 /* Rebind all subsystems back to the default hierarchy */
1387 ret = rebind_subsystems(root, 0);
1388 /* Shouldn't be able to fail ... */
1389 BUG_ON(ret);
1390
817929ec
PM
1391 /*
1392 * Release all the links from css_sets to this hierarchy's
1393 * root cgroup
1394 */
1395 write_lock(&css_set_lock);
71cbb949
KM
1396
1397 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1398 cgrp_link_list) {
817929ec 1399 list_del(&link->cg_link_list);
bd89aabc 1400 list_del(&link->cgrp_link_list);
817929ec
PM
1401 kfree(link);
1402 }
1403 write_unlock(&css_set_lock);
1404
839ec545
PM
1405 if (!list_empty(&root->root_list)) {
1406 list_del(&root->root_list);
1407 root_count--;
1408 }
e5f6a860 1409
ddbcc7e8
PM
1410 mutex_unlock(&cgroup_mutex);
1411
ddbcc7e8 1412 kill_litter_super(sb);
67e055d1 1413 kfree(root);
ddbcc7e8
PM
1414}
1415
1416static struct file_system_type cgroup_fs_type = {
1417 .name = "cgroup",
1418 .get_sb = cgroup_get_sb,
1419 .kill_sb = cgroup_kill_sb,
1420};
1421
bd89aabc 1422static inline struct cgroup *__d_cgrp(struct dentry *dentry)
ddbcc7e8
PM
1423{
1424 return dentry->d_fsdata;
1425}
1426
1427static inline struct cftype *__d_cft(struct dentry *dentry)
1428{
1429 return dentry->d_fsdata;
1430}
1431
a043e3b2
LZ
1432/**
1433 * cgroup_path - generate the path of a cgroup
1434 * @cgrp: the cgroup in question
1435 * @buf: the buffer to write the path into
1436 * @buflen: the length of the buffer
1437 *
a47295e6
PM
1438 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1439 * reference. Writes path of cgroup into buf. Returns 0 on success,
1440 * -errno on error.
ddbcc7e8 1441 */
bd89aabc 1442int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
ddbcc7e8
PM
1443{
1444 char *start;
a47295e6 1445 struct dentry *dentry = rcu_dereference(cgrp->dentry);
ddbcc7e8 1446
a47295e6 1447 if (!dentry || cgrp == dummytop) {
ddbcc7e8
PM
1448 /*
1449 * Inactive subsystems have no dentry for their root
1450 * cgroup
1451 */
1452 strcpy(buf, "/");
1453 return 0;
1454 }
1455
1456 start = buf + buflen;
1457
1458 *--start = '\0';
1459 for (;;) {
a47295e6 1460 int len = dentry->d_name.len;
ddbcc7e8
PM
1461 if ((start -= len) < buf)
1462 return -ENAMETOOLONG;
bd89aabc
PM
1463 memcpy(start, cgrp->dentry->d_name.name, len);
1464 cgrp = cgrp->parent;
1465 if (!cgrp)
ddbcc7e8 1466 break;
a47295e6 1467 dentry = rcu_dereference(cgrp->dentry);
bd89aabc 1468 if (!cgrp->parent)
ddbcc7e8
PM
1469 continue;
1470 if (--start < buf)
1471 return -ENAMETOOLONG;
1472 *start = '/';
1473 }
1474 memmove(buf, start, buf + buflen - start);
1475 return 0;
1476}
1477
a043e3b2
LZ
1478/**
1479 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1480 * @cgrp: the cgroup the task is attaching to
1481 * @tsk: the task to be attached
bbcb81d0 1482 *
a043e3b2
LZ
1483 * Call holding cgroup_mutex. May take task_lock of
1484 * the task 'tsk' during call.
bbcb81d0 1485 */
956db3ca 1486int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
bbcb81d0
PM
1487{
1488 int retval = 0;
1489 struct cgroup_subsys *ss;
bd89aabc 1490 struct cgroup *oldcgrp;
77efecd9 1491 struct css_set *cg;
817929ec 1492 struct css_set *newcg;
bd89aabc 1493 struct cgroupfs_root *root = cgrp->root;
bbcb81d0
PM
1494
1495 /* Nothing to do if the task is already in that cgroup */
7717f7ba 1496 oldcgrp = task_cgroup_from_root(tsk, root);
bd89aabc 1497 if (cgrp == oldcgrp)
bbcb81d0
PM
1498 return 0;
1499
1500 for_each_subsys(root, ss) {
1501 if (ss->can_attach) {
bd89aabc 1502 retval = ss->can_attach(ss, cgrp, tsk);
e18f6318 1503 if (retval)
bbcb81d0 1504 return retval;
bbcb81d0
PM
1505 }
1506 }
1507
77efecd9
LJ
1508 task_lock(tsk);
1509 cg = tsk->cgroups;
1510 get_css_set(cg);
1511 task_unlock(tsk);
817929ec
PM
1512 /*
1513 * Locate or allocate a new css_set for this task,
1514 * based on its final set of cgroups
1515 */
bd89aabc 1516 newcg = find_css_set(cg, cgrp);
77efecd9 1517 put_css_set(cg);
e18f6318 1518 if (!newcg)
817929ec 1519 return -ENOMEM;
817929ec 1520
bbcb81d0
PM
1521 task_lock(tsk);
1522 if (tsk->flags & PF_EXITING) {
1523 task_unlock(tsk);
817929ec 1524 put_css_set(newcg);
bbcb81d0
PM
1525 return -ESRCH;
1526 }
817929ec 1527 rcu_assign_pointer(tsk->cgroups, newcg);
bbcb81d0
PM
1528 task_unlock(tsk);
1529
817929ec
PM
1530 /* Update the css_set linked lists if we're using them */
1531 write_lock(&css_set_lock);
1532 if (!list_empty(&tsk->cg_list)) {
1533 list_del(&tsk->cg_list);
1534 list_add(&tsk->cg_list, &newcg->tasks);
1535 }
1536 write_unlock(&css_set_lock);
1537
bbcb81d0 1538 for_each_subsys(root, ss) {
e18f6318 1539 if (ss->attach)
bd89aabc 1540 ss->attach(ss, cgrp, oldcgrp, tsk);
bbcb81d0 1541 }
bd89aabc 1542 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
bbcb81d0 1543 synchronize_rcu();
817929ec 1544 put_css_set(cg);
ec64f515
KH
1545
1546 /*
1547 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1548 * is no longer empty.
1549 */
88703267 1550 cgroup_wakeup_rmdir_waiter(cgrp);
bbcb81d0
PM
1551 return 0;
1552}
1553
1554/*
af351026
PM
1555 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1556 * held. May take task_lock of task
bbcb81d0 1557 */
af351026 1558static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
bbcb81d0 1559{
bbcb81d0 1560 struct task_struct *tsk;
c69e8d9c 1561 const struct cred *cred = current_cred(), *tcred;
bbcb81d0
PM
1562 int ret;
1563
bbcb81d0
PM
1564 if (pid) {
1565 rcu_read_lock();
73507f33 1566 tsk = find_task_by_vpid(pid);
bbcb81d0
PM
1567 if (!tsk || tsk->flags & PF_EXITING) {
1568 rcu_read_unlock();
1569 return -ESRCH;
1570 }
bbcb81d0 1571
c69e8d9c
DH
1572 tcred = __task_cred(tsk);
1573 if (cred->euid &&
1574 cred->euid != tcred->uid &&
1575 cred->euid != tcred->suid) {
1576 rcu_read_unlock();
bbcb81d0
PM
1577 return -EACCES;
1578 }
c69e8d9c
DH
1579 get_task_struct(tsk);
1580 rcu_read_unlock();
bbcb81d0
PM
1581 } else {
1582 tsk = current;
1583 get_task_struct(tsk);
1584 }
1585
956db3ca 1586 ret = cgroup_attach_task(cgrp, tsk);
bbcb81d0
PM
1587 put_task_struct(tsk);
1588 return ret;
1589}
1590
af351026
PM
1591static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1592{
1593 int ret;
1594 if (!cgroup_lock_live_group(cgrp))
1595 return -ENODEV;
1596 ret = attach_task_by_pid(cgrp, pid);
1597 cgroup_unlock();
1598 return ret;
1599}
1600
ddbcc7e8 1601/* The various types of files and directories in a cgroup file system */
ddbcc7e8
PM
1602enum cgroup_filetype {
1603 FILE_ROOT,
1604 FILE_DIR,
1605 FILE_TASKLIST,
81a6a5cd 1606 FILE_NOTIFY_ON_RELEASE,
81a6a5cd 1607 FILE_RELEASE_AGENT,
ddbcc7e8
PM
1608};
1609
e788e066
PM
1610/**
1611 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1612 * @cgrp: the cgroup to be checked for liveness
1613 *
84eea842
PM
1614 * On success, returns true; the lock should be later released with
1615 * cgroup_unlock(). On failure returns false with no lock held.
e788e066 1616 */
84eea842 1617bool cgroup_lock_live_group(struct cgroup *cgrp)
e788e066
PM
1618{
1619 mutex_lock(&cgroup_mutex);
1620 if (cgroup_is_removed(cgrp)) {
1621 mutex_unlock(&cgroup_mutex);
1622 return false;
1623 }
1624 return true;
1625}
1626
1627static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1628 const char *buffer)
1629{
1630 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1631 if (!cgroup_lock_live_group(cgrp))
1632 return -ENODEV;
1633 strcpy(cgrp->root->release_agent_path, buffer);
84eea842 1634 cgroup_unlock();
e788e066
PM
1635 return 0;
1636}
1637
1638static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1639 struct seq_file *seq)
1640{
1641 if (!cgroup_lock_live_group(cgrp))
1642 return -ENODEV;
1643 seq_puts(seq, cgrp->root->release_agent_path);
1644 seq_putc(seq, '\n');
84eea842 1645 cgroup_unlock();
e788e066
PM
1646 return 0;
1647}
1648
84eea842
PM
1649/* A buffer size big enough for numbers or short strings */
1650#define CGROUP_LOCAL_BUFFER_SIZE 64
1651
e73d2c61 1652static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
f4c753b7
PM
1653 struct file *file,
1654 const char __user *userbuf,
1655 size_t nbytes, loff_t *unused_ppos)
355e0c48 1656{
84eea842 1657 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
355e0c48 1658 int retval = 0;
355e0c48
PM
1659 char *end;
1660
1661 if (!nbytes)
1662 return -EINVAL;
1663 if (nbytes >= sizeof(buffer))
1664 return -E2BIG;
1665 if (copy_from_user(buffer, userbuf, nbytes))
1666 return -EFAULT;
1667
1668 buffer[nbytes] = 0; /* nul-terminate */
b7269dfc 1669 strstrip(buffer);
e73d2c61
PM
1670 if (cft->write_u64) {
1671 u64 val = simple_strtoull(buffer, &end, 0);
1672 if (*end)
1673 return -EINVAL;
1674 retval = cft->write_u64(cgrp, cft, val);
1675 } else {
1676 s64 val = simple_strtoll(buffer, &end, 0);
1677 if (*end)
1678 return -EINVAL;
1679 retval = cft->write_s64(cgrp, cft, val);
1680 }
355e0c48
PM
1681 if (!retval)
1682 retval = nbytes;
1683 return retval;
1684}
1685
db3b1497
PM
1686static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1687 struct file *file,
1688 const char __user *userbuf,
1689 size_t nbytes, loff_t *unused_ppos)
1690{
84eea842 1691 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
db3b1497
PM
1692 int retval = 0;
1693 size_t max_bytes = cft->max_write_len;
1694 char *buffer = local_buffer;
1695
1696 if (!max_bytes)
1697 max_bytes = sizeof(local_buffer) - 1;
1698 if (nbytes >= max_bytes)
1699 return -E2BIG;
1700 /* Allocate a dynamic buffer if we need one */
1701 if (nbytes >= sizeof(local_buffer)) {
1702 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1703 if (buffer == NULL)
1704 return -ENOMEM;
1705 }
5a3eb9f6
LZ
1706 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1707 retval = -EFAULT;
1708 goto out;
1709 }
db3b1497
PM
1710
1711 buffer[nbytes] = 0; /* nul-terminate */
1712 strstrip(buffer);
1713 retval = cft->write_string(cgrp, cft, buffer);
1714 if (!retval)
1715 retval = nbytes;
5a3eb9f6 1716out:
db3b1497
PM
1717 if (buffer != local_buffer)
1718 kfree(buffer);
1719 return retval;
1720}
1721
ddbcc7e8
PM
1722static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1723 size_t nbytes, loff_t *ppos)
1724{
1725 struct cftype *cft = __d_cft(file->f_dentry);
bd89aabc 1726 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
ddbcc7e8 1727
75139b82 1728 if (cgroup_is_removed(cgrp))
ddbcc7e8 1729 return -ENODEV;
355e0c48 1730 if (cft->write)
bd89aabc 1731 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
e73d2c61
PM
1732 if (cft->write_u64 || cft->write_s64)
1733 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
db3b1497
PM
1734 if (cft->write_string)
1735 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
d447ea2f
PE
1736 if (cft->trigger) {
1737 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1738 return ret ? ret : nbytes;
1739 }
355e0c48 1740 return -EINVAL;
ddbcc7e8
PM
1741}
1742
f4c753b7
PM
1743static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1744 struct file *file,
1745 char __user *buf, size_t nbytes,
1746 loff_t *ppos)
ddbcc7e8 1747{
84eea842 1748 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
f4c753b7 1749 u64 val = cft->read_u64(cgrp, cft);
ddbcc7e8
PM
1750 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1751
1752 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1753}
1754
e73d2c61
PM
1755static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1756 struct file *file,
1757 char __user *buf, size_t nbytes,
1758 loff_t *ppos)
1759{
84eea842 1760 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
e73d2c61
PM
1761 s64 val = cft->read_s64(cgrp, cft);
1762 int len = sprintf(tmp, "%lld\n", (long long) val);
1763
1764 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1765}
1766
ddbcc7e8
PM
1767static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1768 size_t nbytes, loff_t *ppos)
1769{
1770 struct cftype *cft = __d_cft(file->f_dentry);
bd89aabc 1771 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
ddbcc7e8 1772
75139b82 1773 if (cgroup_is_removed(cgrp))
ddbcc7e8
PM
1774 return -ENODEV;
1775
1776 if (cft->read)
bd89aabc 1777 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
f4c753b7
PM
1778 if (cft->read_u64)
1779 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
e73d2c61
PM
1780 if (cft->read_s64)
1781 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
ddbcc7e8
PM
1782 return -EINVAL;
1783}
1784
91796569
PM
1785/*
1786 * seqfile ops/methods for returning structured data. Currently just
1787 * supports string->u64 maps, but can be extended in future.
1788 */
1789
1790struct cgroup_seqfile_state {
1791 struct cftype *cft;
1792 struct cgroup *cgroup;
1793};
1794
1795static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1796{
1797 struct seq_file *sf = cb->state;
1798 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1799}
1800
1801static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1802{
1803 struct cgroup_seqfile_state *state = m->private;
1804 struct cftype *cft = state->cft;
29486df3
SH
1805 if (cft->read_map) {
1806 struct cgroup_map_cb cb = {
1807 .fill = cgroup_map_add,
1808 .state = m,
1809 };
1810 return cft->read_map(state->cgroup, cft, &cb);
1811 }
1812 return cft->read_seq_string(state->cgroup, cft, m);
91796569
PM
1813}
1814
96930a63 1815static int cgroup_seqfile_release(struct inode *inode, struct file *file)
91796569
PM
1816{
1817 struct seq_file *seq = file->private_data;
1818 kfree(seq->private);
1819 return single_release(inode, file);
1820}
1821
1822static struct file_operations cgroup_seqfile_operations = {
1823 .read = seq_read,
e788e066 1824 .write = cgroup_file_write,
91796569
PM
1825 .llseek = seq_lseek,
1826 .release = cgroup_seqfile_release,
1827};
1828
ddbcc7e8
PM
1829static int cgroup_file_open(struct inode *inode, struct file *file)
1830{
1831 int err;
1832 struct cftype *cft;
1833
1834 err = generic_file_open(inode, file);
1835 if (err)
1836 return err;
ddbcc7e8 1837 cft = __d_cft(file->f_dentry);
75139b82 1838
29486df3 1839 if (cft->read_map || cft->read_seq_string) {
91796569
PM
1840 struct cgroup_seqfile_state *state =
1841 kzalloc(sizeof(*state), GFP_USER);
1842 if (!state)
1843 return -ENOMEM;
1844 state->cft = cft;
1845 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1846 file->f_op = &cgroup_seqfile_operations;
1847 err = single_open(file, cgroup_seqfile_show, state);
1848 if (err < 0)
1849 kfree(state);
1850 } else if (cft->open)
ddbcc7e8
PM
1851 err = cft->open(inode, file);
1852 else
1853 err = 0;
1854
1855 return err;
1856}
1857
1858static int cgroup_file_release(struct inode *inode, struct file *file)
1859{
1860 struct cftype *cft = __d_cft(file->f_dentry);
1861 if (cft->release)
1862 return cft->release(inode, file);
1863 return 0;
1864}
1865
1866/*
1867 * cgroup_rename - Only allow simple rename of directories in place.
1868 */
1869static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1870 struct inode *new_dir, struct dentry *new_dentry)
1871{
1872 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1873 return -ENOTDIR;
1874 if (new_dentry->d_inode)
1875 return -EEXIST;
1876 if (old_dir != new_dir)
1877 return -EIO;
1878 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1879}
1880
1881static struct file_operations cgroup_file_operations = {
1882 .read = cgroup_file_read,
1883 .write = cgroup_file_write,
1884 .llseek = generic_file_llseek,
1885 .open = cgroup_file_open,
1886 .release = cgroup_file_release,
1887};
1888
6e1d5dcc 1889static const struct inode_operations cgroup_dir_inode_operations = {
ddbcc7e8
PM
1890 .lookup = simple_lookup,
1891 .mkdir = cgroup_mkdir,
1892 .rmdir = cgroup_rmdir,
1893 .rename = cgroup_rename,
1894};
1895
099fca32 1896static int cgroup_create_file(struct dentry *dentry, mode_t mode,
ddbcc7e8
PM
1897 struct super_block *sb)
1898{
3ba13d17 1899 static const struct dentry_operations cgroup_dops = {
ddbcc7e8
PM
1900 .d_iput = cgroup_diput,
1901 };
1902
1903 struct inode *inode;
1904
1905 if (!dentry)
1906 return -ENOENT;
1907 if (dentry->d_inode)
1908 return -EEXIST;
1909
1910 inode = cgroup_new_inode(mode, sb);
1911 if (!inode)
1912 return -ENOMEM;
1913
1914 if (S_ISDIR(mode)) {
1915 inode->i_op = &cgroup_dir_inode_operations;
1916 inode->i_fop = &simple_dir_operations;
1917
1918 /* start off with i_nlink == 2 (for "." entry) */
1919 inc_nlink(inode);
1920
1921 /* start with the directory inode held, so that we can
1922 * populate it without racing with another mkdir */
817929ec 1923 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
ddbcc7e8
PM
1924 } else if (S_ISREG(mode)) {
1925 inode->i_size = 0;
1926 inode->i_fop = &cgroup_file_operations;
1927 }
1928 dentry->d_op = &cgroup_dops;
1929 d_instantiate(dentry, inode);
1930 dget(dentry); /* Extra count - pin the dentry in core */
1931 return 0;
1932}
1933
1934/*
a043e3b2
LZ
1935 * cgroup_create_dir - create a directory for an object.
1936 * @cgrp: the cgroup we create the directory for. It must have a valid
1937 * ->parent field. And we are going to fill its ->dentry field.
1938 * @dentry: dentry of the new cgroup
1939 * @mode: mode to set on new directory.
ddbcc7e8 1940 */
bd89aabc 1941static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
099fca32 1942 mode_t mode)
ddbcc7e8
PM
1943{
1944 struct dentry *parent;
1945 int error = 0;
1946
bd89aabc
PM
1947 parent = cgrp->parent->dentry;
1948 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
ddbcc7e8 1949 if (!error) {
bd89aabc 1950 dentry->d_fsdata = cgrp;
ddbcc7e8 1951 inc_nlink(parent->d_inode);
a47295e6 1952 rcu_assign_pointer(cgrp->dentry, dentry);
ddbcc7e8
PM
1953 dget(dentry);
1954 }
1955 dput(dentry);
1956
1957 return error;
1958}
1959
099fca32
LZ
1960/**
1961 * cgroup_file_mode - deduce file mode of a control file
1962 * @cft: the control file in question
1963 *
1964 * returns cft->mode if ->mode is not 0
1965 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1966 * returns S_IRUGO if it has only a read handler
1967 * returns S_IWUSR if it has only a write hander
1968 */
1969static mode_t cgroup_file_mode(const struct cftype *cft)
1970{
1971 mode_t mode = 0;
1972
1973 if (cft->mode)
1974 return cft->mode;
1975
1976 if (cft->read || cft->read_u64 || cft->read_s64 ||
1977 cft->read_map || cft->read_seq_string)
1978 mode |= S_IRUGO;
1979
1980 if (cft->write || cft->write_u64 || cft->write_s64 ||
1981 cft->write_string || cft->trigger)
1982 mode |= S_IWUSR;
1983
1984 return mode;
1985}
1986
bd89aabc 1987int cgroup_add_file(struct cgroup *cgrp,
ddbcc7e8
PM
1988 struct cgroup_subsys *subsys,
1989 const struct cftype *cft)
1990{
bd89aabc 1991 struct dentry *dir = cgrp->dentry;
ddbcc7e8
PM
1992 struct dentry *dentry;
1993 int error;
099fca32 1994 mode_t mode;
ddbcc7e8
PM
1995
1996 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
bd89aabc 1997 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
ddbcc7e8
PM
1998 strcpy(name, subsys->name);
1999 strcat(name, ".");
2000 }
2001 strcat(name, cft->name);
2002 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2003 dentry = lookup_one_len(name, dir, strlen(name));
2004 if (!IS_ERR(dentry)) {
099fca32
LZ
2005 mode = cgroup_file_mode(cft);
2006 error = cgroup_create_file(dentry, mode | S_IFREG,
bd89aabc 2007 cgrp->root->sb);
ddbcc7e8
PM
2008 if (!error)
2009 dentry->d_fsdata = (void *)cft;
2010 dput(dentry);
2011 } else
2012 error = PTR_ERR(dentry);
2013 return error;
2014}
2015
bd89aabc 2016int cgroup_add_files(struct cgroup *cgrp,
ddbcc7e8
PM
2017 struct cgroup_subsys *subsys,
2018 const struct cftype cft[],
2019 int count)
2020{
2021 int i, err;
2022 for (i = 0; i < count; i++) {
bd89aabc 2023 err = cgroup_add_file(cgrp, subsys, &cft[i]);
ddbcc7e8
PM
2024 if (err)
2025 return err;
2026 }
2027 return 0;
2028}
2029
a043e3b2
LZ
2030/**
2031 * cgroup_task_count - count the number of tasks in a cgroup.
2032 * @cgrp: the cgroup in question
2033 *
2034 * Return the number of tasks in the cgroup.
2035 */
bd89aabc 2036int cgroup_task_count(const struct cgroup *cgrp)
bbcb81d0
PM
2037{
2038 int count = 0;
71cbb949 2039 struct cg_cgroup_link *link;
817929ec
PM
2040
2041 read_lock(&css_set_lock);
71cbb949 2042 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
146aa1bd 2043 count += atomic_read(&link->cg->refcount);
817929ec
PM
2044 }
2045 read_unlock(&css_set_lock);
bbcb81d0
PM
2046 return count;
2047}
2048
817929ec
PM
2049/*
2050 * Advance a list_head iterator. The iterator should be positioned at
2051 * the start of a css_set
2052 */
bd89aabc 2053static void cgroup_advance_iter(struct cgroup *cgrp,
7717f7ba 2054 struct cgroup_iter *it)
817929ec
PM
2055{
2056 struct list_head *l = it->cg_link;
2057 struct cg_cgroup_link *link;
2058 struct css_set *cg;
2059
2060 /* Advance to the next non-empty css_set */
2061 do {
2062 l = l->next;
bd89aabc 2063 if (l == &cgrp->css_sets) {
817929ec
PM
2064 it->cg_link = NULL;
2065 return;
2066 }
bd89aabc 2067 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
817929ec
PM
2068 cg = link->cg;
2069 } while (list_empty(&cg->tasks));
2070 it->cg_link = l;
2071 it->task = cg->tasks.next;
2072}
2073
31a7df01
CW
2074/*
2075 * To reduce the fork() overhead for systems that are not actually
2076 * using their cgroups capability, we don't maintain the lists running
2077 * through each css_set to its tasks until we see the list actually
2078 * used - in other words after the first call to cgroup_iter_start().
2079 *
2080 * The tasklist_lock is not held here, as do_each_thread() and
2081 * while_each_thread() are protected by RCU.
2082 */
3df91fe3 2083static void cgroup_enable_task_cg_lists(void)
31a7df01
CW
2084{
2085 struct task_struct *p, *g;
2086 write_lock(&css_set_lock);
2087 use_task_css_set_links = 1;
2088 do_each_thread(g, p) {
2089 task_lock(p);
0e04388f
LZ
2090 /*
2091 * We should check if the process is exiting, otherwise
2092 * it will race with cgroup_exit() in that the list
2093 * entry won't be deleted though the process has exited.
2094 */
2095 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
31a7df01
CW
2096 list_add(&p->cg_list, &p->cgroups->tasks);
2097 task_unlock(p);
2098 } while_each_thread(g, p);
2099 write_unlock(&css_set_lock);
2100}
2101
bd89aabc 2102void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
817929ec
PM
2103{
2104 /*
2105 * The first time anyone tries to iterate across a cgroup,
2106 * we need to enable the list linking each css_set to its
2107 * tasks, and fix up all existing tasks.
2108 */
31a7df01
CW
2109 if (!use_task_css_set_links)
2110 cgroup_enable_task_cg_lists();
2111
817929ec 2112 read_lock(&css_set_lock);
bd89aabc
PM
2113 it->cg_link = &cgrp->css_sets;
2114 cgroup_advance_iter(cgrp, it);
817929ec
PM
2115}
2116
bd89aabc 2117struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
817929ec
PM
2118 struct cgroup_iter *it)
2119{
2120 struct task_struct *res;
2121 struct list_head *l = it->task;
2019f634 2122 struct cg_cgroup_link *link;
817929ec
PM
2123
2124 /* If the iterator cg is NULL, we have no tasks */
2125 if (!it->cg_link)
2126 return NULL;
2127 res = list_entry(l, struct task_struct, cg_list);
2128 /* Advance iterator to find next entry */
2129 l = l->next;
2019f634
LJ
2130 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2131 if (l == &link->cg->tasks) {
817929ec
PM
2132 /* We reached the end of this task list - move on to
2133 * the next cg_cgroup_link */
bd89aabc 2134 cgroup_advance_iter(cgrp, it);
817929ec
PM
2135 } else {
2136 it->task = l;
2137 }
2138 return res;
2139}
2140
bd89aabc 2141void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
817929ec
PM
2142{
2143 read_unlock(&css_set_lock);
2144}
2145
31a7df01
CW
2146static inline int started_after_time(struct task_struct *t1,
2147 struct timespec *time,
2148 struct task_struct *t2)
2149{
2150 int start_diff = timespec_compare(&t1->start_time, time);
2151 if (start_diff > 0) {
2152 return 1;
2153 } else if (start_diff < 0) {
2154 return 0;
2155 } else {
2156 /*
2157 * Arbitrarily, if two processes started at the same
2158 * time, we'll say that the lower pointer value
2159 * started first. Note that t2 may have exited by now
2160 * so this may not be a valid pointer any longer, but
2161 * that's fine - it still serves to distinguish
2162 * between two tasks started (effectively) simultaneously.
2163 */
2164 return t1 > t2;
2165 }
2166}
2167
2168/*
2169 * This function is a callback from heap_insert() and is used to order
2170 * the heap.
2171 * In this case we order the heap in descending task start time.
2172 */
2173static inline int started_after(void *p1, void *p2)
2174{
2175 struct task_struct *t1 = p1;
2176 struct task_struct *t2 = p2;
2177 return started_after_time(t1, &t2->start_time, t2);
2178}
2179
2180/**
2181 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2182 * @scan: struct cgroup_scanner containing arguments for the scan
2183 *
2184 * Arguments include pointers to callback functions test_task() and
2185 * process_task().
2186 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2187 * and if it returns true, call process_task() for it also.
2188 * The test_task pointer may be NULL, meaning always true (select all tasks).
2189 * Effectively duplicates cgroup_iter_{start,next,end}()
2190 * but does not lock css_set_lock for the call to process_task().
2191 * The struct cgroup_scanner may be embedded in any structure of the caller's
2192 * creation.
2193 * It is guaranteed that process_task() will act on every task that
2194 * is a member of the cgroup for the duration of this call. This
2195 * function may or may not call process_task() for tasks that exit
2196 * or move to a different cgroup during the call, or are forked or
2197 * move into the cgroup during the call.
2198 *
2199 * Note that test_task() may be called with locks held, and may in some
2200 * situations be called multiple times for the same task, so it should
2201 * be cheap.
2202 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2203 * pre-allocated and will be used for heap operations (and its "gt" member will
2204 * be overwritten), else a temporary heap will be used (allocation of which
2205 * may cause this function to fail).
2206 */
2207int cgroup_scan_tasks(struct cgroup_scanner *scan)
2208{
2209 int retval, i;
2210 struct cgroup_iter it;
2211 struct task_struct *p, *dropped;
2212 /* Never dereference latest_task, since it's not refcounted */
2213 struct task_struct *latest_task = NULL;
2214 struct ptr_heap tmp_heap;
2215 struct ptr_heap *heap;
2216 struct timespec latest_time = { 0, 0 };
2217
2218 if (scan->heap) {
2219 /* The caller supplied our heap and pre-allocated its memory */
2220 heap = scan->heap;
2221 heap->gt = &started_after;
2222 } else {
2223 /* We need to allocate our own heap memory */
2224 heap = &tmp_heap;
2225 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2226 if (retval)
2227 /* cannot allocate the heap */
2228 return retval;
2229 }
2230
2231 again:
2232 /*
2233 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2234 * to determine which are of interest, and using the scanner's
2235 * "process_task" callback to process any of them that need an update.
2236 * Since we don't want to hold any locks during the task updates,
2237 * gather tasks to be processed in a heap structure.
2238 * The heap is sorted by descending task start time.
2239 * If the statically-sized heap fills up, we overflow tasks that
2240 * started later, and in future iterations only consider tasks that
2241 * started after the latest task in the previous pass. This
2242 * guarantees forward progress and that we don't miss any tasks.
2243 */
2244 heap->size = 0;
2245 cgroup_iter_start(scan->cg, &it);
2246 while ((p = cgroup_iter_next(scan->cg, &it))) {
2247 /*
2248 * Only affect tasks that qualify per the caller's callback,
2249 * if he provided one
2250 */
2251 if (scan->test_task && !scan->test_task(p, scan))
2252 continue;
2253 /*
2254 * Only process tasks that started after the last task
2255 * we processed
2256 */
2257 if (!started_after_time(p, &latest_time, latest_task))
2258 continue;
2259 dropped = heap_insert(heap, p);
2260 if (dropped == NULL) {
2261 /*
2262 * The new task was inserted; the heap wasn't
2263 * previously full
2264 */
2265 get_task_struct(p);
2266 } else if (dropped != p) {
2267 /*
2268 * The new task was inserted, and pushed out a
2269 * different task
2270 */
2271 get_task_struct(p);
2272 put_task_struct(dropped);
2273 }
2274 /*
2275 * Else the new task was newer than anything already in
2276 * the heap and wasn't inserted
2277 */
2278 }
2279 cgroup_iter_end(scan->cg, &it);
2280
2281 if (heap->size) {
2282 for (i = 0; i < heap->size; i++) {
4fe91d51 2283 struct task_struct *q = heap->ptrs[i];
31a7df01 2284 if (i == 0) {
4fe91d51
PJ
2285 latest_time = q->start_time;
2286 latest_task = q;
31a7df01
CW
2287 }
2288 /* Process the task per the caller's callback */
4fe91d51
PJ
2289 scan->process_task(q, scan);
2290 put_task_struct(q);
31a7df01
CW
2291 }
2292 /*
2293 * If we had to process any tasks at all, scan again
2294 * in case some of them were in the middle of forking
2295 * children that didn't get processed.
2296 * Not the most efficient way to do it, but it avoids
2297 * having to take callback_mutex in the fork path
2298 */
2299 goto again;
2300 }
2301 if (heap == &tmp_heap)
2302 heap_free(&tmp_heap);
2303 return 0;
2304}
2305
bbcb81d0
PM
2306/*
2307 * Stuff for reading the 'tasks' file.
2308 *
2309 * Reading this file can return large amounts of data if a cgroup has
2310 * *lots* of attached tasks. So it may need several calls to read(),
2311 * but we cannot guarantee that the information we produce is correct
2312 * unless we produce it entirely atomically.
2313 *
bbcb81d0 2314 */
bbcb81d0
PM
2315
2316/*
2317 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
bd89aabc 2318 * 'cgrp'. Return actual number of pids loaded. No need to
bbcb81d0
PM
2319 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2320 * read section, so the css_set can't go away, and is
2321 * immutable after creation.
2322 */
bd89aabc 2323static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
bbcb81d0 2324{
e7b80bb6 2325 int n = 0, pid;
817929ec
PM
2326 struct cgroup_iter it;
2327 struct task_struct *tsk;
bd89aabc
PM
2328 cgroup_iter_start(cgrp, &it);
2329 while ((tsk = cgroup_iter_next(cgrp, &it))) {
817929ec
PM
2330 if (unlikely(n == npids))
2331 break;
e7b80bb6
G
2332 pid = task_pid_vnr(tsk);
2333 if (pid > 0)
2334 pidarray[n++] = pid;
817929ec 2335 }
bd89aabc 2336 cgroup_iter_end(cgrp, &it);
bbcb81d0
PM
2337 return n;
2338}
2339
846c7bb0 2340/**
a043e3b2 2341 * cgroupstats_build - build and fill cgroupstats
846c7bb0
BS
2342 * @stats: cgroupstats to fill information into
2343 * @dentry: A dentry entry belonging to the cgroup for which stats have
2344 * been requested.
a043e3b2
LZ
2345 *
2346 * Build and fill cgroupstats so that taskstats can export it to user
2347 * space.
846c7bb0
BS
2348 */
2349int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2350{
2351 int ret = -EINVAL;
bd89aabc 2352 struct cgroup *cgrp;
846c7bb0
BS
2353 struct cgroup_iter it;
2354 struct task_struct *tsk;
33d283be 2355
846c7bb0 2356 /*
33d283be
LZ
2357 * Validate dentry by checking the superblock operations,
2358 * and make sure it's a directory.
846c7bb0 2359 */
33d283be
LZ
2360 if (dentry->d_sb->s_op != &cgroup_ops ||
2361 !S_ISDIR(dentry->d_inode->i_mode))
846c7bb0
BS
2362 goto err;
2363
2364 ret = 0;
bd89aabc 2365 cgrp = dentry->d_fsdata;
846c7bb0 2366
bd89aabc
PM
2367 cgroup_iter_start(cgrp, &it);
2368 while ((tsk = cgroup_iter_next(cgrp, &it))) {
846c7bb0
BS
2369 switch (tsk->state) {
2370 case TASK_RUNNING:
2371 stats->nr_running++;
2372 break;
2373 case TASK_INTERRUPTIBLE:
2374 stats->nr_sleeping++;
2375 break;
2376 case TASK_UNINTERRUPTIBLE:
2377 stats->nr_uninterruptible++;
2378 break;
2379 case TASK_STOPPED:
2380 stats->nr_stopped++;
2381 break;
2382 default:
2383 if (delayacct_is_task_waiting_on_io(tsk))
2384 stats->nr_io_wait++;
2385 break;
2386 }
2387 }
bd89aabc 2388 cgroup_iter_end(cgrp, &it);
846c7bb0 2389
846c7bb0
BS
2390err:
2391 return ret;
2392}
2393
096b7fe0
LZ
2394/*
2395 * Cache pids for all threads in the same pid namespace that are
2396 * opening the same "tasks" file.
2397 */
2398struct cgroup_pids {
2399 /* The node in cgrp->pids_list */
2400 struct list_head list;
2401 /* The cgroup those pids belong to */
2402 struct cgroup *cgrp;
2403 /* The namepsace those pids belong to */
2404 struct pid_namespace *ns;
2405 /* Array of process ids in the cgroup */
2406 pid_t *tasks_pids;
2407 /* How many files are using the this tasks_pids array */
2408 int use_count;
2409 /* Length of the current tasks_pids array */
2410 int length;
2411};
2412
bbcb81d0
PM
2413static int cmppid(const void *a, const void *b)
2414{
2415 return *(pid_t *)a - *(pid_t *)b;
2416}
2417
2418/*
cc31edce
PM
2419 * seq_file methods for the "tasks" file. The seq_file position is the
2420 * next pid to display; the seq_file iterator is a pointer to the pid
2421 * in the cgroup->tasks_pids array.
bbcb81d0 2422 */
cc31edce
PM
2423
2424static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
bbcb81d0 2425{
cc31edce
PM
2426 /*
2427 * Initially we receive a position value that corresponds to
2428 * one more than the last pid shown (or 0 on the first call or
2429 * after a seek to the start). Use a binary-search to find the
2430 * next pid to display, if any
2431 */
096b7fe0
LZ
2432 struct cgroup_pids *cp = s->private;
2433 struct cgroup *cgrp = cp->cgrp;
cc31edce
PM
2434 int index = 0, pid = *pos;
2435 int *iter;
2436
2437 down_read(&cgrp->pids_mutex);
2438 if (pid) {
096b7fe0 2439 int end = cp->length;
20777766 2440
cc31edce
PM
2441 while (index < end) {
2442 int mid = (index + end) / 2;
096b7fe0 2443 if (cp->tasks_pids[mid] == pid) {
cc31edce
PM
2444 index = mid;
2445 break;
096b7fe0 2446 } else if (cp->tasks_pids[mid] <= pid)
cc31edce
PM
2447 index = mid + 1;
2448 else
2449 end = mid;
2450 }
2451 }
2452 /* If we're off the end of the array, we're done */
096b7fe0 2453 if (index >= cp->length)
cc31edce
PM
2454 return NULL;
2455 /* Update the abstract position to be the actual pid that we found */
096b7fe0 2456 iter = cp->tasks_pids + index;
cc31edce
PM
2457 *pos = *iter;
2458 return iter;
2459}
2460
2461static void cgroup_tasks_stop(struct seq_file *s, void *v)
2462{
096b7fe0
LZ
2463 struct cgroup_pids *cp = s->private;
2464 struct cgroup *cgrp = cp->cgrp;
cc31edce
PM
2465 up_read(&cgrp->pids_mutex);
2466}
2467
2468static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2469{
096b7fe0 2470 struct cgroup_pids *cp = s->private;
cc31edce 2471 int *p = v;
096b7fe0 2472 int *end = cp->tasks_pids + cp->length;
cc31edce
PM
2473
2474 /*
2475 * Advance to the next pid in the array. If this goes off the
2476 * end, we're done
2477 */
2478 p++;
2479 if (p >= end) {
2480 return NULL;
2481 } else {
2482 *pos = *p;
2483 return p;
2484 }
2485}
2486
2487static int cgroup_tasks_show(struct seq_file *s, void *v)
2488{
2489 return seq_printf(s, "%d\n", *(int *)v);
2490}
bbcb81d0 2491
88e9d34c 2492static const struct seq_operations cgroup_tasks_seq_operations = {
cc31edce
PM
2493 .start = cgroup_tasks_start,
2494 .stop = cgroup_tasks_stop,
2495 .next = cgroup_tasks_next,
2496 .show = cgroup_tasks_show,
2497};
2498
096b7fe0 2499static void release_cgroup_pid_array(struct cgroup_pids *cp)
cc31edce 2500{
096b7fe0
LZ
2501 struct cgroup *cgrp = cp->cgrp;
2502
cc31edce 2503 down_write(&cgrp->pids_mutex);
096b7fe0
LZ
2504 BUG_ON(!cp->use_count);
2505 if (!--cp->use_count) {
2506 list_del(&cp->list);
2507 put_pid_ns(cp->ns);
2508 kfree(cp->tasks_pids);
2509 kfree(cp);
cc31edce
PM
2510 }
2511 up_write(&cgrp->pids_mutex);
bbcb81d0
PM
2512}
2513
cc31edce
PM
2514static int cgroup_tasks_release(struct inode *inode, struct file *file)
2515{
096b7fe0
LZ
2516 struct seq_file *seq;
2517 struct cgroup_pids *cp;
cc31edce
PM
2518
2519 if (!(file->f_mode & FMODE_READ))
2520 return 0;
2521
096b7fe0
LZ
2522 seq = file->private_data;
2523 cp = seq->private;
2524
2525 release_cgroup_pid_array(cp);
cc31edce
PM
2526 return seq_release(inode, file);
2527}
2528
2529static struct file_operations cgroup_tasks_operations = {
2530 .read = seq_read,
2531 .llseek = seq_lseek,
2532 .write = cgroup_file_write,
2533 .release = cgroup_tasks_release,
2534};
2535
bbcb81d0 2536/*
cc31edce 2537 * Handle an open on 'tasks' file. Prepare an array containing the
bbcb81d0 2538 * process id's of tasks currently attached to the cgroup being opened.
bbcb81d0 2539 */
cc31edce 2540
bbcb81d0
PM
2541static int cgroup_tasks_open(struct inode *unused, struct file *file)
2542{
bd89aabc 2543 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
096b7fe0
LZ
2544 struct pid_namespace *ns = current->nsproxy->pid_ns;
2545 struct cgroup_pids *cp;
bbcb81d0
PM
2546 pid_t *pidarray;
2547 int npids;
cc31edce 2548 int retval;
bbcb81d0 2549
cc31edce 2550 /* Nothing to do for write-only files */
bbcb81d0
PM
2551 if (!(file->f_mode & FMODE_READ))
2552 return 0;
2553
bbcb81d0
PM
2554 /*
2555 * If cgroup gets more users after we read count, we won't have
2556 * enough space - tough. This race is indistinguishable to the
2557 * caller from the case that the additional cgroup users didn't
2558 * show up until sometime later on.
2559 */
bd89aabc 2560 npids = cgroup_task_count(cgrp);
cc31edce
PM
2561 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2562 if (!pidarray)
2563 return -ENOMEM;
2564 npids = pid_array_load(pidarray, npids, cgrp);
2565 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
bbcb81d0 2566
cc31edce
PM
2567 /*
2568 * Store the array in the cgroup, freeing the old
2569 * array if necessary
2570 */
2571 down_write(&cgrp->pids_mutex);
096b7fe0
LZ
2572
2573 list_for_each_entry(cp, &cgrp->pids_list, list) {
2574 if (ns == cp->ns)
2575 goto found;
2576 }
2577
2578 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2579 if (!cp) {
2580 up_write(&cgrp->pids_mutex);
2581 kfree(pidarray);
2582 return -ENOMEM;
2583 }
2584 cp->cgrp = cgrp;
2585 cp->ns = ns;
2586 get_pid_ns(ns);
2587 list_add(&cp->list, &cgrp->pids_list);
2588found:
2589 kfree(cp->tasks_pids);
2590 cp->tasks_pids = pidarray;
2591 cp->length = npids;
2592 cp->use_count++;
cc31edce
PM
2593 up_write(&cgrp->pids_mutex);
2594
2595 file->f_op = &cgroup_tasks_operations;
2596
2597 retval = seq_open(file, &cgroup_tasks_seq_operations);
2598 if (retval) {
096b7fe0 2599 release_cgroup_pid_array(cp);
cc31edce 2600 return retval;
bbcb81d0 2601 }
096b7fe0 2602 ((struct seq_file *)file->private_data)->private = cp;
bbcb81d0
PM
2603 return 0;
2604}
2605
bd89aabc 2606static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
81a6a5cd
PM
2607 struct cftype *cft)
2608{
bd89aabc 2609 return notify_on_release(cgrp);
81a6a5cd
PM
2610}
2611
6379c106
PM
2612static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2613 struct cftype *cft,
2614 u64 val)
2615{
2616 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2617 if (val)
2618 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2619 else
2620 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2621 return 0;
2622}
2623
bbcb81d0
PM
2624/*
2625 * for the common functions, 'private' gives the type of file
2626 */
81a6a5cd
PM
2627static struct cftype files[] = {
2628 {
2629 .name = "tasks",
2630 .open = cgroup_tasks_open,
af351026 2631 .write_u64 = cgroup_tasks_write,
81a6a5cd
PM
2632 .release = cgroup_tasks_release,
2633 .private = FILE_TASKLIST,
099fca32 2634 .mode = S_IRUGO | S_IWUSR,
81a6a5cd
PM
2635 },
2636
2637 {
2638 .name = "notify_on_release",
f4c753b7 2639 .read_u64 = cgroup_read_notify_on_release,
6379c106 2640 .write_u64 = cgroup_write_notify_on_release,
81a6a5cd
PM
2641 .private = FILE_NOTIFY_ON_RELEASE,
2642 },
81a6a5cd
PM
2643};
2644
2645static struct cftype cft_release_agent = {
2646 .name = "release_agent",
e788e066
PM
2647 .read_seq_string = cgroup_release_agent_show,
2648 .write_string = cgroup_release_agent_write,
2649 .max_write_len = PATH_MAX,
81a6a5cd 2650 .private = FILE_RELEASE_AGENT,
bbcb81d0
PM
2651};
2652
bd89aabc 2653static int cgroup_populate_dir(struct cgroup *cgrp)
ddbcc7e8
PM
2654{
2655 int err;
2656 struct cgroup_subsys *ss;
2657
2658 /* First clear out any existing files */
bd89aabc 2659 cgroup_clear_directory(cgrp->dentry);
ddbcc7e8 2660
bd89aabc 2661 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
bbcb81d0
PM
2662 if (err < 0)
2663 return err;
2664
bd89aabc
PM
2665 if (cgrp == cgrp->top_cgroup) {
2666 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
81a6a5cd
PM
2667 return err;
2668 }
2669
bd89aabc
PM
2670 for_each_subsys(cgrp->root, ss) {
2671 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
ddbcc7e8
PM
2672 return err;
2673 }
38460b48
KH
2674 /* This cgroup is ready now */
2675 for_each_subsys(cgrp->root, ss) {
2676 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2677 /*
2678 * Update id->css pointer and make this css visible from
2679 * CSS ID functions. This pointer will be dereferened
2680 * from RCU-read-side without locks.
2681 */
2682 if (css->id)
2683 rcu_assign_pointer(css->id->css, css);
2684 }
ddbcc7e8
PM
2685
2686 return 0;
2687}
2688
2689static void init_cgroup_css(struct cgroup_subsys_state *css,
2690 struct cgroup_subsys *ss,
bd89aabc 2691 struct cgroup *cgrp)
ddbcc7e8 2692{
bd89aabc 2693 css->cgroup = cgrp;
e7c5ec91 2694 atomic_set(&css->refcnt, 1);
ddbcc7e8 2695 css->flags = 0;
38460b48 2696 css->id = NULL;
bd89aabc 2697 if (cgrp == dummytop)
ddbcc7e8 2698 set_bit(CSS_ROOT, &css->flags);
bd89aabc
PM
2699 BUG_ON(cgrp->subsys[ss->subsys_id]);
2700 cgrp->subsys[ss->subsys_id] = css;
ddbcc7e8
PM
2701}
2702
999cd8a4
PM
2703static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2704{
2705 /* We need to take each hierarchy_mutex in a consistent order */
2706 int i;
2707
2708 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2709 struct cgroup_subsys *ss = subsys[i];
2710 if (ss->root == root)
cfebe563 2711 mutex_lock(&ss->hierarchy_mutex);
999cd8a4
PM
2712 }
2713}
2714
2715static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2716{
2717 int i;
2718
2719 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2720 struct cgroup_subsys *ss = subsys[i];
2721 if (ss->root == root)
2722 mutex_unlock(&ss->hierarchy_mutex);
2723 }
2724}
2725
ddbcc7e8 2726/*
a043e3b2
LZ
2727 * cgroup_create - create a cgroup
2728 * @parent: cgroup that will be parent of the new cgroup
2729 * @dentry: dentry of the new cgroup
2730 * @mode: mode to set on new inode
ddbcc7e8 2731 *
a043e3b2 2732 * Must be called with the mutex on the parent inode held
ddbcc7e8 2733 */
ddbcc7e8 2734static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
099fca32 2735 mode_t mode)
ddbcc7e8 2736{
bd89aabc 2737 struct cgroup *cgrp;
ddbcc7e8
PM
2738 struct cgroupfs_root *root = parent->root;
2739 int err = 0;
2740 struct cgroup_subsys *ss;
2741 struct super_block *sb = root->sb;
2742
bd89aabc
PM
2743 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2744 if (!cgrp)
ddbcc7e8
PM
2745 return -ENOMEM;
2746
2747 /* Grab a reference on the superblock so the hierarchy doesn't
2748 * get deleted on unmount if there are child cgroups. This
2749 * can be done outside cgroup_mutex, since the sb can't
2750 * disappear while someone has an open control file on the
2751 * fs */
2752 atomic_inc(&sb->s_active);
2753
2754 mutex_lock(&cgroup_mutex);
2755
cc31edce 2756 init_cgroup_housekeeping(cgrp);
ddbcc7e8 2757
bd89aabc
PM
2758 cgrp->parent = parent;
2759 cgrp->root = parent->root;
2760 cgrp->top_cgroup = parent->top_cgroup;
ddbcc7e8 2761
b6abdb0e
LZ
2762 if (notify_on_release(parent))
2763 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2764
ddbcc7e8 2765 for_each_subsys(root, ss) {
bd89aabc 2766 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
ddbcc7e8
PM
2767 if (IS_ERR(css)) {
2768 err = PTR_ERR(css);
2769 goto err_destroy;
2770 }
bd89aabc 2771 init_cgroup_css(css, ss, cgrp);
38460b48
KH
2772 if (ss->use_id)
2773 if (alloc_css_id(ss, parent, cgrp))
2774 goto err_destroy;
2775 /* At error, ->destroy() callback has to free assigned ID. */
ddbcc7e8
PM
2776 }
2777
999cd8a4 2778 cgroup_lock_hierarchy(root);
bd89aabc 2779 list_add(&cgrp->sibling, &cgrp->parent->children);
999cd8a4 2780 cgroup_unlock_hierarchy(root);
ddbcc7e8
PM
2781 root->number_of_cgroups++;
2782
bd89aabc 2783 err = cgroup_create_dir(cgrp, dentry, mode);
ddbcc7e8
PM
2784 if (err < 0)
2785 goto err_remove;
2786
2787 /* The cgroup directory was pre-locked for us */
bd89aabc 2788 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
ddbcc7e8 2789
bd89aabc 2790 err = cgroup_populate_dir(cgrp);
ddbcc7e8
PM
2791 /* If err < 0, we have a half-filled directory - oh well ;) */
2792
2793 mutex_unlock(&cgroup_mutex);
bd89aabc 2794 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
2795
2796 return 0;
2797
2798 err_remove:
2799
baef99a0 2800 cgroup_lock_hierarchy(root);
bd89aabc 2801 list_del(&cgrp->sibling);
baef99a0 2802 cgroup_unlock_hierarchy(root);
ddbcc7e8
PM
2803 root->number_of_cgroups--;
2804
2805 err_destroy:
2806
2807 for_each_subsys(root, ss) {
bd89aabc
PM
2808 if (cgrp->subsys[ss->subsys_id])
2809 ss->destroy(ss, cgrp);
ddbcc7e8
PM
2810 }
2811
2812 mutex_unlock(&cgroup_mutex);
2813
2814 /* Release the reference count that we took on the superblock */
2815 deactivate_super(sb);
2816
bd89aabc 2817 kfree(cgrp);
ddbcc7e8
PM
2818 return err;
2819}
2820
2821static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2822{
2823 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2824
2825 /* the vfs holds inode->i_mutex already */
2826 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2827}
2828
55b6fd01 2829static int cgroup_has_css_refs(struct cgroup *cgrp)
81a6a5cd
PM
2830{
2831 /* Check the reference count on each subsystem. Since we
2832 * already established that there are no tasks in the
e7c5ec91 2833 * cgroup, if the css refcount is also 1, then there should
81a6a5cd
PM
2834 * be no outstanding references, so the subsystem is safe to
2835 * destroy. We scan across all subsystems rather than using
2836 * the per-hierarchy linked list of mounted subsystems since
2837 * we can be called via check_for_release() with no
2838 * synchronization other than RCU, and the subsystem linked
2839 * list isn't RCU-safe */
2840 int i;
2841 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2842 struct cgroup_subsys *ss = subsys[i];
2843 struct cgroup_subsys_state *css;
2844 /* Skip subsystems not in this hierarchy */
bd89aabc 2845 if (ss->root != cgrp->root)
81a6a5cd 2846 continue;
bd89aabc 2847 css = cgrp->subsys[ss->subsys_id];
81a6a5cd
PM
2848 /* When called from check_for_release() it's possible
2849 * that by this point the cgroup has been removed
2850 * and the css deleted. But a false-positive doesn't
2851 * matter, since it can only happen if the cgroup
2852 * has been deleted and hence no longer needs the
2853 * release agent to be called anyway. */
e7c5ec91 2854 if (css && (atomic_read(&css->refcnt) > 1))
81a6a5cd 2855 return 1;
81a6a5cd
PM
2856 }
2857 return 0;
2858}
2859
e7c5ec91
PM
2860/*
2861 * Atomically mark all (or else none) of the cgroup's CSS objects as
2862 * CSS_REMOVED. Return true on success, or false if the cgroup has
2863 * busy subsystems. Call with cgroup_mutex held
2864 */
2865
2866static int cgroup_clear_css_refs(struct cgroup *cgrp)
2867{
2868 struct cgroup_subsys *ss;
2869 unsigned long flags;
2870 bool failed = false;
2871 local_irq_save(flags);
2872 for_each_subsys(cgrp->root, ss) {
2873 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2874 int refcnt;
804b3c28 2875 while (1) {
e7c5ec91
PM
2876 /* We can only remove a CSS with a refcnt==1 */
2877 refcnt = atomic_read(&css->refcnt);
2878 if (refcnt > 1) {
2879 failed = true;
2880 goto done;
2881 }
2882 BUG_ON(!refcnt);
2883 /*
2884 * Drop the refcnt to 0 while we check other
2885 * subsystems. This will cause any racing
2886 * css_tryget() to spin until we set the
2887 * CSS_REMOVED bits or abort
2888 */
804b3c28
PM
2889 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2890 break;
2891 cpu_relax();
2892 }
e7c5ec91
PM
2893 }
2894 done:
2895 for_each_subsys(cgrp->root, ss) {
2896 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2897 if (failed) {
2898 /*
2899 * Restore old refcnt if we previously managed
2900 * to clear it from 1 to 0
2901 */
2902 if (!atomic_read(&css->refcnt))
2903 atomic_set(&css->refcnt, 1);
2904 } else {
2905 /* Commit the fact that the CSS is removed */
2906 set_bit(CSS_REMOVED, &css->flags);
2907 }
2908 }
2909 local_irq_restore(flags);
2910 return !failed;
2911}
2912
ddbcc7e8
PM
2913static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2914{
bd89aabc 2915 struct cgroup *cgrp = dentry->d_fsdata;
ddbcc7e8
PM
2916 struct dentry *d;
2917 struct cgroup *parent;
ec64f515
KH
2918 DEFINE_WAIT(wait);
2919 int ret;
ddbcc7e8
PM
2920
2921 /* the vfs holds both inode->i_mutex already */
ec64f515 2922again:
ddbcc7e8 2923 mutex_lock(&cgroup_mutex);
bd89aabc 2924 if (atomic_read(&cgrp->count) != 0) {
ddbcc7e8
PM
2925 mutex_unlock(&cgroup_mutex);
2926 return -EBUSY;
2927 }
bd89aabc 2928 if (!list_empty(&cgrp->children)) {
ddbcc7e8
PM
2929 mutex_unlock(&cgroup_mutex);
2930 return -EBUSY;
2931 }
3fa59dfb 2932 mutex_unlock(&cgroup_mutex);
a043e3b2 2933
88703267
KH
2934 /*
2935 * In general, subsystem has no css->refcnt after pre_destroy(). But
2936 * in racy cases, subsystem may have to get css->refcnt after
2937 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
2938 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
2939 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
2940 * and subsystem's reference count handling. Please see css_get/put
2941 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
2942 */
2943 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2944
4fca88c8 2945 /*
a043e3b2
LZ
2946 * Call pre_destroy handlers of subsys. Notify subsystems
2947 * that rmdir() request comes.
4fca88c8 2948 */
ec64f515 2949 ret = cgroup_call_pre_destroy(cgrp);
88703267
KH
2950 if (ret) {
2951 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
ec64f515 2952 return ret;
88703267 2953 }
ddbcc7e8 2954
3fa59dfb
KH
2955 mutex_lock(&cgroup_mutex);
2956 parent = cgrp->parent;
ec64f515 2957 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
88703267 2958 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
ddbcc7e8
PM
2959 mutex_unlock(&cgroup_mutex);
2960 return -EBUSY;
2961 }
ec64f515 2962 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
ec64f515
KH
2963 if (!cgroup_clear_css_refs(cgrp)) {
2964 mutex_unlock(&cgroup_mutex);
88703267
KH
2965 /*
2966 * Because someone may call cgroup_wakeup_rmdir_waiter() before
2967 * prepare_to_wait(), we need to check this flag.
2968 */
2969 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
2970 schedule();
ec64f515
KH
2971 finish_wait(&cgroup_rmdir_waitq, &wait);
2972 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2973 if (signal_pending(current))
2974 return -EINTR;
2975 goto again;
2976 }
2977 /* NO css_tryget() can success after here. */
2978 finish_wait(&cgroup_rmdir_waitq, &wait);
2979 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
ddbcc7e8 2980
81a6a5cd 2981 spin_lock(&release_list_lock);
bd89aabc
PM
2982 set_bit(CGRP_REMOVED, &cgrp->flags);
2983 if (!list_empty(&cgrp->release_list))
2984 list_del(&cgrp->release_list);
81a6a5cd 2985 spin_unlock(&release_list_lock);
999cd8a4
PM
2986
2987 cgroup_lock_hierarchy(cgrp->root);
2988 /* delete this cgroup from parent->children */
bd89aabc 2989 list_del(&cgrp->sibling);
999cd8a4
PM
2990 cgroup_unlock_hierarchy(cgrp->root);
2991
bd89aabc
PM
2992 spin_lock(&cgrp->dentry->d_lock);
2993 d = dget(cgrp->dentry);
ddbcc7e8
PM
2994 spin_unlock(&d->d_lock);
2995
2996 cgroup_d_remove_dir(d);
2997 dput(d);
ddbcc7e8 2998
bd89aabc 2999 set_bit(CGRP_RELEASABLE, &parent->flags);
81a6a5cd
PM
3000 check_for_release(parent);
3001
ddbcc7e8 3002 mutex_unlock(&cgroup_mutex);
ddbcc7e8
PM
3003 return 0;
3004}
3005
06a11920 3006static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
ddbcc7e8 3007{
ddbcc7e8 3008 struct cgroup_subsys_state *css;
cfe36bde
DC
3009
3010 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
ddbcc7e8
PM
3011
3012 /* Create the top cgroup state for this subsystem */
33a68ac1 3013 list_add(&ss->sibling, &rootnode.subsys_list);
ddbcc7e8
PM
3014 ss->root = &rootnode;
3015 css = ss->create(ss, dummytop);
3016 /* We don't handle early failures gracefully */
3017 BUG_ON(IS_ERR(css));
3018 init_cgroup_css(css, ss, dummytop);
3019
e8d55fde 3020 /* Update the init_css_set to contain a subsys
817929ec 3021 * pointer to this state - since the subsystem is
e8d55fde
LZ
3022 * newly registered, all tasks and hence the
3023 * init_css_set is in the subsystem's top cgroup. */
3024 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
ddbcc7e8
PM
3025
3026 need_forkexit_callback |= ss->fork || ss->exit;
3027
e8d55fde
LZ
3028 /* At system boot, before all subsystems have been
3029 * registered, no tasks have been forked, so we don't
3030 * need to invoke fork callbacks here. */
3031 BUG_ON(!list_empty(&init_task.tasks));
3032
999cd8a4 3033 mutex_init(&ss->hierarchy_mutex);
cfebe563 3034 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
ddbcc7e8
PM
3035 ss->active = 1;
3036}
3037
3038/**
a043e3b2
LZ
3039 * cgroup_init_early - cgroup initialization at system boot
3040 *
3041 * Initialize cgroups at system boot, and initialize any
3042 * subsystems that request early init.
ddbcc7e8
PM
3043 */
3044int __init cgroup_init_early(void)
3045{
3046 int i;
146aa1bd 3047 atomic_set(&init_css_set.refcount, 1);
817929ec
PM
3048 INIT_LIST_HEAD(&init_css_set.cg_links);
3049 INIT_LIST_HEAD(&init_css_set.tasks);
472b1053 3050 INIT_HLIST_NODE(&init_css_set.hlist);
817929ec 3051 css_set_count = 1;
ddbcc7e8 3052 init_cgroup_root(&rootnode);
817929ec
PM
3053 root_count = 1;
3054 init_task.cgroups = &init_css_set;
3055
3056 init_css_set_link.cg = &init_css_set;
7717f7ba 3057 init_css_set_link.cgrp = dummytop;
bd89aabc 3058 list_add(&init_css_set_link.cgrp_link_list,
817929ec
PM
3059 &rootnode.top_cgroup.css_sets);
3060 list_add(&init_css_set_link.cg_link_list,
3061 &init_css_set.cg_links);
ddbcc7e8 3062
472b1053
LZ
3063 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3064 INIT_HLIST_HEAD(&css_set_table[i]);
3065
ddbcc7e8
PM
3066 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3067 struct cgroup_subsys *ss = subsys[i];
3068
3069 BUG_ON(!ss->name);
3070 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3071 BUG_ON(!ss->create);
3072 BUG_ON(!ss->destroy);
3073 if (ss->subsys_id != i) {
cfe36bde 3074 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
ddbcc7e8
PM
3075 ss->name, ss->subsys_id);
3076 BUG();
3077 }
3078
3079 if (ss->early_init)
3080 cgroup_init_subsys(ss);
3081 }
3082 return 0;
3083}
3084
3085/**
a043e3b2
LZ
3086 * cgroup_init - cgroup initialization
3087 *
3088 * Register cgroup filesystem and /proc file, and initialize
3089 * any subsystems that didn't request early init.
ddbcc7e8
PM
3090 */
3091int __init cgroup_init(void)
3092{
3093 int err;
3094 int i;
472b1053 3095 struct hlist_head *hhead;
a424316c
PM
3096
3097 err = bdi_init(&cgroup_backing_dev_info);
3098 if (err)
3099 return err;
ddbcc7e8
PM
3100
3101 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3102 struct cgroup_subsys *ss = subsys[i];
3103 if (!ss->early_init)
3104 cgroup_init_subsys(ss);
38460b48
KH
3105 if (ss->use_id)
3106 cgroup_subsys_init_idr(ss);
ddbcc7e8
PM
3107 }
3108
472b1053
LZ
3109 /* Add init_css_set to the hash table */
3110 hhead = css_set_hash(init_css_set.subsys);
3111 hlist_add_head(&init_css_set.hlist, hhead);
3112
ddbcc7e8
PM
3113 err = register_filesystem(&cgroup_fs_type);
3114 if (err < 0)
3115 goto out;
3116
46ae220b 3117 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
a424316c 3118
ddbcc7e8 3119out:
a424316c
PM
3120 if (err)
3121 bdi_destroy(&cgroup_backing_dev_info);
3122
ddbcc7e8
PM
3123 return err;
3124}
b4f48b63 3125
a424316c
PM
3126/*
3127 * proc_cgroup_show()
3128 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3129 * - Used for /proc/<pid>/cgroup.
3130 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3131 * doesn't really matter if tsk->cgroup changes after we read it,
956db3ca 3132 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
a424316c
PM
3133 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3134 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3135 * cgroup to top_cgroup.
3136 */
3137
3138/* TODO: Use a proper seq_file iterator */
3139static int proc_cgroup_show(struct seq_file *m, void *v)
3140{
3141 struct pid *pid;
3142 struct task_struct *tsk;
3143 char *buf;
3144 int retval;
3145 struct cgroupfs_root *root;
3146
3147 retval = -ENOMEM;
3148 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3149 if (!buf)
3150 goto out;
3151
3152 retval = -ESRCH;
3153 pid = m->private;
3154 tsk = get_pid_task(pid, PIDTYPE_PID);
3155 if (!tsk)
3156 goto out_free;
3157
3158 retval = 0;
3159
3160 mutex_lock(&cgroup_mutex);
3161
e5f6a860 3162 for_each_active_root(root) {
a424316c 3163 struct cgroup_subsys *ss;
bd89aabc 3164 struct cgroup *cgrp;
a424316c
PM
3165 int count = 0;
3166
b6c3006d 3167 seq_printf(m, "%lu:", root->subsys_bits);
a424316c
PM
3168 for_each_subsys(root, ss)
3169 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
c6d57f33
PM
3170 if (strlen(root->name))
3171 seq_printf(m, "%sname=%s", count ? "," : "",
3172 root->name);
a424316c 3173 seq_putc(m, ':');
7717f7ba 3174 cgrp = task_cgroup_from_root(tsk, root);
bd89aabc 3175 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
a424316c
PM
3176 if (retval < 0)
3177 goto out_unlock;
3178 seq_puts(m, buf);
3179 seq_putc(m, '\n');
3180 }
3181
3182out_unlock:
3183 mutex_unlock(&cgroup_mutex);
3184 put_task_struct(tsk);
3185out_free:
3186 kfree(buf);
3187out:
3188 return retval;
3189}
3190
3191static int cgroup_open(struct inode *inode, struct file *file)
3192{
3193 struct pid *pid = PROC_I(inode)->pid;
3194 return single_open(file, proc_cgroup_show, pid);
3195}
3196
3197struct file_operations proc_cgroup_operations = {
3198 .open = cgroup_open,
3199 .read = seq_read,
3200 .llseek = seq_lseek,
3201 .release = single_release,
3202};
3203
3204/* Display information about each subsystem and each hierarchy */
3205static int proc_cgroupstats_show(struct seq_file *m, void *v)
3206{
3207 int i;
a424316c 3208
8bab8dde 3209 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
a424316c 3210 mutex_lock(&cgroup_mutex);
a424316c
PM
3211 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3212 struct cgroup_subsys *ss = subsys[i];
8bab8dde 3213 seq_printf(m, "%s\t%lu\t%d\t%d\n",
817929ec 3214 ss->name, ss->root->subsys_bits,
8bab8dde 3215 ss->root->number_of_cgroups, !ss->disabled);
a424316c
PM
3216 }
3217 mutex_unlock(&cgroup_mutex);
3218 return 0;
3219}
3220
3221static int cgroupstats_open(struct inode *inode, struct file *file)
3222{
9dce07f1 3223 return single_open(file, proc_cgroupstats_show, NULL);
a424316c
PM
3224}
3225
3226static struct file_operations proc_cgroupstats_operations = {
3227 .open = cgroupstats_open,
3228 .read = seq_read,
3229 .llseek = seq_lseek,
3230 .release = single_release,
3231};
3232
b4f48b63
PM
3233/**
3234 * cgroup_fork - attach newly forked task to its parents cgroup.
a043e3b2 3235 * @child: pointer to task_struct of forking parent process.
b4f48b63
PM
3236 *
3237 * Description: A task inherits its parent's cgroup at fork().
3238 *
3239 * A pointer to the shared css_set was automatically copied in
3240 * fork.c by dup_task_struct(). However, we ignore that copy, since
3241 * it was not made under the protection of RCU or cgroup_mutex, so
956db3ca 3242 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
817929ec
PM
3243 * have already changed current->cgroups, allowing the previously
3244 * referenced cgroup group to be removed and freed.
b4f48b63
PM
3245 *
3246 * At the point that cgroup_fork() is called, 'current' is the parent
3247 * task, and the passed argument 'child' points to the child task.
3248 */
3249void cgroup_fork(struct task_struct *child)
3250{
817929ec
PM
3251 task_lock(current);
3252 child->cgroups = current->cgroups;
3253 get_css_set(child->cgroups);
3254 task_unlock(current);
3255 INIT_LIST_HEAD(&child->cg_list);
b4f48b63
PM
3256}
3257
3258/**
a043e3b2
LZ
3259 * cgroup_fork_callbacks - run fork callbacks
3260 * @child: the new task
3261 *
3262 * Called on a new task very soon before adding it to the
3263 * tasklist. No need to take any locks since no-one can
3264 * be operating on this task.
b4f48b63
PM
3265 */
3266void cgroup_fork_callbacks(struct task_struct *child)
3267{
3268 if (need_forkexit_callback) {
3269 int i;
3270 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3271 struct cgroup_subsys *ss = subsys[i];
3272 if (ss->fork)
3273 ss->fork(ss, child);
3274 }
3275 }
3276}
3277
817929ec 3278/**
a043e3b2
LZ
3279 * cgroup_post_fork - called on a new task after adding it to the task list
3280 * @child: the task in question
3281 *
3282 * Adds the task to the list running through its css_set if necessary.
3283 * Has to be after the task is visible on the task list in case we race
3284 * with the first call to cgroup_iter_start() - to guarantee that the
3285 * new task ends up on its list.
3286 */
817929ec
PM
3287void cgroup_post_fork(struct task_struct *child)
3288{
3289 if (use_task_css_set_links) {
3290 write_lock(&css_set_lock);
b12b533f 3291 task_lock(child);
817929ec
PM
3292 if (list_empty(&child->cg_list))
3293 list_add(&child->cg_list, &child->cgroups->tasks);
b12b533f 3294 task_unlock(child);
817929ec
PM
3295 write_unlock(&css_set_lock);
3296 }
3297}
b4f48b63
PM
3298/**
3299 * cgroup_exit - detach cgroup from exiting task
3300 * @tsk: pointer to task_struct of exiting process
a043e3b2 3301 * @run_callback: run exit callbacks?
b4f48b63
PM
3302 *
3303 * Description: Detach cgroup from @tsk and release it.
3304 *
3305 * Note that cgroups marked notify_on_release force every task in
3306 * them to take the global cgroup_mutex mutex when exiting.
3307 * This could impact scaling on very large systems. Be reluctant to
3308 * use notify_on_release cgroups where very high task exit scaling
3309 * is required on large systems.
3310 *
3311 * the_top_cgroup_hack:
3312 *
3313 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3314 *
3315 * We call cgroup_exit() while the task is still competent to
3316 * handle notify_on_release(), then leave the task attached to the
3317 * root cgroup in each hierarchy for the remainder of its exit.
3318 *
3319 * To do this properly, we would increment the reference count on
3320 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3321 * code we would add a second cgroup function call, to drop that
3322 * reference. This would just create an unnecessary hot spot on
3323 * the top_cgroup reference count, to no avail.
3324 *
3325 * Normally, holding a reference to a cgroup without bumping its
3326 * count is unsafe. The cgroup could go away, or someone could
3327 * attach us to a different cgroup, decrementing the count on
3328 * the first cgroup that we never incremented. But in this case,
3329 * top_cgroup isn't going away, and either task has PF_EXITING set,
956db3ca
CW
3330 * which wards off any cgroup_attach_task() attempts, or task is a failed
3331 * fork, never visible to cgroup_attach_task.
b4f48b63
PM
3332 */
3333void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3334{
3335 int i;
817929ec 3336 struct css_set *cg;
b4f48b63
PM
3337
3338 if (run_callbacks && need_forkexit_callback) {
3339 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3340 struct cgroup_subsys *ss = subsys[i];
3341 if (ss->exit)
3342 ss->exit(ss, tsk);
3343 }
3344 }
817929ec
PM
3345
3346 /*
3347 * Unlink from the css_set task list if necessary.
3348 * Optimistically check cg_list before taking
3349 * css_set_lock
3350 */
3351 if (!list_empty(&tsk->cg_list)) {
3352 write_lock(&css_set_lock);
3353 if (!list_empty(&tsk->cg_list))
3354 list_del(&tsk->cg_list);
3355 write_unlock(&css_set_lock);
3356 }
3357
b4f48b63
PM
3358 /* Reassign the task to the init_css_set. */
3359 task_lock(tsk);
817929ec
PM
3360 cg = tsk->cgroups;
3361 tsk->cgroups = &init_css_set;
b4f48b63 3362 task_unlock(tsk);
817929ec 3363 if (cg)
81a6a5cd 3364 put_css_set_taskexit(cg);
b4f48b63 3365}
697f4161
PM
3366
3367/**
a043e3b2
LZ
3368 * cgroup_clone - clone the cgroup the given subsystem is attached to
3369 * @tsk: the task to be moved
3370 * @subsys: the given subsystem
e885dcde 3371 * @nodename: the name for the new cgroup
a043e3b2
LZ
3372 *
3373 * Duplicate the current cgroup in the hierarchy that the given
3374 * subsystem is attached to, and move this task into the new
3375 * child.
697f4161 3376 */
e885dcde
SH
3377int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3378 char *nodename)
697f4161
PM
3379{
3380 struct dentry *dentry;
3381 int ret = 0;
697f4161
PM
3382 struct cgroup *parent, *child;
3383 struct inode *inode;
3384 struct css_set *cg;
3385 struct cgroupfs_root *root;
3386 struct cgroup_subsys *ss;
3387
3388 /* We shouldn't be called by an unregistered subsystem */
3389 BUG_ON(!subsys->active);
3390
3391 /* First figure out what hierarchy and cgroup we're dealing
3392 * with, and pin them so we can drop cgroup_mutex */
3393 mutex_lock(&cgroup_mutex);
3394 again:
3395 root = subsys->root;
3396 if (root == &rootnode) {
697f4161
PM
3397 mutex_unlock(&cgroup_mutex);
3398 return 0;
3399 }
697f4161 3400
697f4161 3401 /* Pin the hierarchy */
1404f065 3402 if (!atomic_inc_not_zero(&root->sb->s_active)) {
7b574b7b
LZ
3403 /* We race with the final deactivate_super() */
3404 mutex_unlock(&cgroup_mutex);
3405 return 0;
3406 }
697f4161 3407
817929ec 3408 /* Keep the cgroup alive */
1404f065
LZ
3409 task_lock(tsk);
3410 parent = task_cgroup(tsk, subsys->subsys_id);
3411 cg = tsk->cgroups;
817929ec 3412 get_css_set(cg);
104cbd55 3413 task_unlock(tsk);
1404f065 3414
697f4161
PM
3415 mutex_unlock(&cgroup_mutex);
3416
3417 /* Now do the VFS work to create a cgroup */
3418 inode = parent->dentry->d_inode;
3419
3420 /* Hold the parent directory mutex across this operation to
3421 * stop anyone else deleting the new cgroup */
3422 mutex_lock(&inode->i_mutex);
3423 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3424 if (IS_ERR(dentry)) {
3425 printk(KERN_INFO
cfe36bde 3426 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
697f4161
PM
3427 PTR_ERR(dentry));
3428 ret = PTR_ERR(dentry);
3429 goto out_release;
3430 }
3431
3432 /* Create the cgroup directory, which also creates the cgroup */
75139b82 3433 ret = vfs_mkdir(inode, dentry, 0755);
bd89aabc 3434 child = __d_cgrp(dentry);
697f4161
PM
3435 dput(dentry);
3436 if (ret) {
3437 printk(KERN_INFO
3438 "Failed to create cgroup %s: %d\n", nodename,
3439 ret);
3440 goto out_release;
3441 }
3442
697f4161
PM
3443 /* The cgroup now exists. Retake cgroup_mutex and check
3444 * that we're still in the same state that we thought we
3445 * were. */
3446 mutex_lock(&cgroup_mutex);
3447 if ((root != subsys->root) ||
3448 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3449 /* Aargh, we raced ... */
3450 mutex_unlock(&inode->i_mutex);
817929ec 3451 put_css_set(cg);
697f4161 3452
1404f065 3453 deactivate_super(root->sb);
697f4161
PM
3454 /* The cgroup is still accessible in the VFS, but
3455 * we're not going to try to rmdir() it at this
3456 * point. */
3457 printk(KERN_INFO
3458 "Race in cgroup_clone() - leaking cgroup %s\n",
3459 nodename);
3460 goto again;
3461 }
3462
3463 /* do any required auto-setup */
3464 for_each_subsys(root, ss) {
3465 if (ss->post_clone)
3466 ss->post_clone(ss, child);
3467 }
3468
3469 /* All seems fine. Finish by moving the task into the new cgroup */
956db3ca 3470 ret = cgroup_attach_task(child, tsk);
697f4161
PM
3471 mutex_unlock(&cgroup_mutex);
3472
3473 out_release:
3474 mutex_unlock(&inode->i_mutex);
81a6a5cd
PM
3475
3476 mutex_lock(&cgroup_mutex);
817929ec 3477 put_css_set(cg);
81a6a5cd 3478 mutex_unlock(&cgroup_mutex);
1404f065 3479 deactivate_super(root->sb);
697f4161
PM
3480 return ret;
3481}
3482
a043e3b2 3483/**
313e924c 3484 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
a043e3b2 3485 * @cgrp: the cgroup in question
313e924c 3486 * @task: the task in question
a043e3b2 3487 *
313e924c
GN
3488 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3489 * hierarchy.
697f4161
PM
3490 *
3491 * If we are sending in dummytop, then presumably we are creating
3492 * the top cgroup in the subsystem.
3493 *
3494 * Called only by the ns (nsproxy) cgroup.
3495 */
313e924c 3496int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
697f4161
PM
3497{
3498 int ret;
3499 struct cgroup *target;
697f4161 3500
bd89aabc 3501 if (cgrp == dummytop)
697f4161
PM
3502 return 1;
3503
7717f7ba 3504 target = task_cgroup_from_root(task, cgrp->root);
bd89aabc
PM
3505 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3506 cgrp = cgrp->parent;
3507 ret = (cgrp == target);
697f4161
PM
3508 return ret;
3509}
81a6a5cd 3510
bd89aabc 3511static void check_for_release(struct cgroup *cgrp)
81a6a5cd
PM
3512{
3513 /* All of these checks rely on RCU to keep the cgroup
3514 * structure alive */
bd89aabc
PM
3515 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3516 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
81a6a5cd
PM
3517 /* Control Group is currently removeable. If it's not
3518 * already queued for a userspace notification, queue
3519 * it now */
3520 int need_schedule_work = 0;
3521 spin_lock(&release_list_lock);
bd89aabc
PM
3522 if (!cgroup_is_removed(cgrp) &&
3523 list_empty(&cgrp->release_list)) {
3524 list_add(&cgrp->release_list, &release_list);
81a6a5cd
PM
3525 need_schedule_work = 1;
3526 }
3527 spin_unlock(&release_list_lock);
3528 if (need_schedule_work)
3529 schedule_work(&release_agent_work);
3530 }
3531}
3532
3533void __css_put(struct cgroup_subsys_state *css)
3534{
bd89aabc 3535 struct cgroup *cgrp = css->cgroup;
81a6a5cd 3536 rcu_read_lock();
ec64f515
KH
3537 if (atomic_dec_return(&css->refcnt) == 1) {
3538 if (notify_on_release(cgrp)) {
3539 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3540 check_for_release(cgrp);
3541 }
88703267 3542 cgroup_wakeup_rmdir_waiter(cgrp);
81a6a5cd
PM
3543 }
3544 rcu_read_unlock();
3545}
3546
3547/*
3548 * Notify userspace when a cgroup is released, by running the
3549 * configured release agent with the name of the cgroup (path
3550 * relative to the root of cgroup file system) as the argument.
3551 *
3552 * Most likely, this user command will try to rmdir this cgroup.
3553 *
3554 * This races with the possibility that some other task will be
3555 * attached to this cgroup before it is removed, or that some other
3556 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3557 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3558 * unused, and this cgroup will be reprieved from its death sentence,
3559 * to continue to serve a useful existence. Next time it's released,
3560 * we will get notified again, if it still has 'notify_on_release' set.
3561 *
3562 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3563 * means only wait until the task is successfully execve()'d. The
3564 * separate release agent task is forked by call_usermodehelper(),
3565 * then control in this thread returns here, without waiting for the
3566 * release agent task. We don't bother to wait because the caller of
3567 * this routine has no use for the exit status of the release agent
3568 * task, so no sense holding our caller up for that.
81a6a5cd 3569 */
81a6a5cd
PM
3570static void cgroup_release_agent(struct work_struct *work)
3571{
3572 BUG_ON(work != &release_agent_work);
3573 mutex_lock(&cgroup_mutex);
3574 spin_lock(&release_list_lock);
3575 while (!list_empty(&release_list)) {
3576 char *argv[3], *envp[3];
3577 int i;
e788e066 3578 char *pathbuf = NULL, *agentbuf = NULL;
bd89aabc 3579 struct cgroup *cgrp = list_entry(release_list.next,
81a6a5cd
PM
3580 struct cgroup,
3581 release_list);
bd89aabc 3582 list_del_init(&cgrp->release_list);
81a6a5cd
PM
3583 spin_unlock(&release_list_lock);
3584 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
e788e066
PM
3585 if (!pathbuf)
3586 goto continue_free;
3587 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3588 goto continue_free;
3589 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3590 if (!agentbuf)
3591 goto continue_free;
81a6a5cd
PM
3592
3593 i = 0;
e788e066
PM
3594 argv[i++] = agentbuf;
3595 argv[i++] = pathbuf;
81a6a5cd
PM
3596 argv[i] = NULL;
3597
3598 i = 0;
3599 /* minimal command environment */
3600 envp[i++] = "HOME=/";
3601 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3602 envp[i] = NULL;
3603
3604 /* Drop the lock while we invoke the usermode helper,
3605 * since the exec could involve hitting disk and hence
3606 * be a slow process */
3607 mutex_unlock(&cgroup_mutex);
3608 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
81a6a5cd 3609 mutex_lock(&cgroup_mutex);
e788e066
PM
3610 continue_free:
3611 kfree(pathbuf);
3612 kfree(agentbuf);
81a6a5cd
PM
3613 spin_lock(&release_list_lock);
3614 }
3615 spin_unlock(&release_list_lock);
3616 mutex_unlock(&cgroup_mutex);
3617}
8bab8dde
PM
3618
3619static int __init cgroup_disable(char *str)
3620{
3621 int i;
3622 char *token;
3623
3624 while ((token = strsep(&str, ",")) != NULL) {
3625 if (!*token)
3626 continue;
3627
3628 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3629 struct cgroup_subsys *ss = subsys[i];
3630
3631 if (!strcmp(token, ss->name)) {
3632 ss->disabled = 1;
3633 printk(KERN_INFO "Disabling %s control group"
3634 " subsystem\n", ss->name);
3635 break;
3636 }
3637 }
3638 }
3639 return 1;
3640}
3641__setup("cgroup_disable=", cgroup_disable);
38460b48
KH
3642
3643/*
3644 * Functons for CSS ID.
3645 */
3646
3647/*
3648 *To get ID other than 0, this should be called when !cgroup_is_removed().
3649 */
3650unsigned short css_id(struct cgroup_subsys_state *css)
3651{
3652 struct css_id *cssid = rcu_dereference(css->id);
3653
3654 if (cssid)
3655 return cssid->id;
3656 return 0;
3657}
3658
3659unsigned short css_depth(struct cgroup_subsys_state *css)
3660{
3661 struct css_id *cssid = rcu_dereference(css->id);
3662
3663 if (cssid)
3664 return cssid->depth;
3665 return 0;
3666}
3667
3668bool css_is_ancestor(struct cgroup_subsys_state *child,
0b7f569e 3669 const struct cgroup_subsys_state *root)
38460b48
KH
3670{
3671 struct css_id *child_id = rcu_dereference(child->id);
3672 struct css_id *root_id = rcu_dereference(root->id);
3673
3674 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3675 return false;
3676 return child_id->stack[root_id->depth] == root_id->id;
3677}
3678
3679static void __free_css_id_cb(struct rcu_head *head)
3680{
3681 struct css_id *id;
3682
3683 id = container_of(head, struct css_id, rcu_head);
3684 kfree(id);
3685}
3686
3687void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3688{
3689 struct css_id *id = css->id;
3690 /* When this is called before css_id initialization, id can be NULL */
3691 if (!id)
3692 return;
3693
3694 BUG_ON(!ss->use_id);
3695
3696 rcu_assign_pointer(id->css, NULL);
3697 rcu_assign_pointer(css->id, NULL);
3698 spin_lock(&ss->id_lock);
3699 idr_remove(&ss->idr, id->id);
3700 spin_unlock(&ss->id_lock);
3701 call_rcu(&id->rcu_head, __free_css_id_cb);
3702}
3703
3704/*
3705 * This is called by init or create(). Then, calls to this function are
3706 * always serialized (By cgroup_mutex() at create()).
3707 */
3708
3709static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3710{
3711 struct css_id *newid;
3712 int myid, error, size;
3713
3714 BUG_ON(!ss->use_id);
3715
3716 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3717 newid = kzalloc(size, GFP_KERNEL);
3718 if (!newid)
3719 return ERR_PTR(-ENOMEM);
3720 /* get id */
3721 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3722 error = -ENOMEM;
3723 goto err_out;
3724 }
3725 spin_lock(&ss->id_lock);
3726 /* Don't use 0. allocates an ID of 1-65535 */
3727 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3728 spin_unlock(&ss->id_lock);
3729
3730 /* Returns error when there are no free spaces for new ID.*/
3731 if (error) {
3732 error = -ENOSPC;
3733 goto err_out;
3734 }
3735 if (myid > CSS_ID_MAX)
3736 goto remove_idr;
3737
3738 newid->id = myid;
3739 newid->depth = depth;
3740 return newid;
3741remove_idr:
3742 error = -ENOSPC;
3743 spin_lock(&ss->id_lock);
3744 idr_remove(&ss->idr, myid);
3745 spin_unlock(&ss->id_lock);
3746err_out:
3747 kfree(newid);
3748 return ERR_PTR(error);
3749
3750}
3751
3752static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3753{
3754 struct css_id *newid;
3755 struct cgroup_subsys_state *rootcss;
3756
3757 spin_lock_init(&ss->id_lock);
3758 idr_init(&ss->idr);
3759
3760 rootcss = init_css_set.subsys[ss->subsys_id];
3761 newid = get_new_cssid(ss, 0);
3762 if (IS_ERR(newid))
3763 return PTR_ERR(newid);
3764
3765 newid->stack[0] = newid->id;
3766 newid->css = rootcss;
3767 rootcss->id = newid;
3768 return 0;
3769}
3770
3771static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3772 struct cgroup *child)
3773{
3774 int subsys_id, i, depth = 0;
3775 struct cgroup_subsys_state *parent_css, *child_css;
3776 struct css_id *child_id, *parent_id = NULL;
3777
3778 subsys_id = ss->subsys_id;
3779 parent_css = parent->subsys[subsys_id];
3780 child_css = child->subsys[subsys_id];
3781 depth = css_depth(parent_css) + 1;
3782 parent_id = parent_css->id;
3783
3784 child_id = get_new_cssid(ss, depth);
3785 if (IS_ERR(child_id))
3786 return PTR_ERR(child_id);
3787
3788 for (i = 0; i < depth; i++)
3789 child_id->stack[i] = parent_id->stack[i];
3790 child_id->stack[depth] = child_id->id;
3791 /*
3792 * child_id->css pointer will be set after this cgroup is available
3793 * see cgroup_populate_dir()
3794 */
3795 rcu_assign_pointer(child_css->id, child_id);
3796
3797 return 0;
3798}
3799
3800/**
3801 * css_lookup - lookup css by id
3802 * @ss: cgroup subsys to be looked into.
3803 * @id: the id
3804 *
3805 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3806 * NULL if not. Should be called under rcu_read_lock()
3807 */
3808struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3809{
3810 struct css_id *cssid = NULL;
3811
3812 BUG_ON(!ss->use_id);
3813 cssid = idr_find(&ss->idr, id);
3814
3815 if (unlikely(!cssid))
3816 return NULL;
3817
3818 return rcu_dereference(cssid->css);
3819}
3820
3821/**
3822 * css_get_next - lookup next cgroup under specified hierarchy.
3823 * @ss: pointer to subsystem
3824 * @id: current position of iteration.
3825 * @root: pointer to css. search tree under this.
3826 * @foundid: position of found object.
3827 *
3828 * Search next css under the specified hierarchy of rootid. Calling under
3829 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3830 */
3831struct cgroup_subsys_state *
3832css_get_next(struct cgroup_subsys *ss, int id,
3833 struct cgroup_subsys_state *root, int *foundid)
3834{
3835 struct cgroup_subsys_state *ret = NULL;
3836 struct css_id *tmp;
3837 int tmpid;
3838 int rootid = css_id(root);
3839 int depth = css_depth(root);
3840
3841 if (!rootid)
3842 return NULL;
3843
3844 BUG_ON(!ss->use_id);
3845 /* fill start point for scan */
3846 tmpid = id;
3847 while (1) {
3848 /*
3849 * scan next entry from bitmap(tree), tmpid is updated after
3850 * idr_get_next().
3851 */
3852 spin_lock(&ss->id_lock);
3853 tmp = idr_get_next(&ss->idr, &tmpid);
3854 spin_unlock(&ss->id_lock);
3855
3856 if (!tmp)
3857 break;
3858 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3859 ret = rcu_dereference(tmp->css);
3860 if (ret) {
3861 *foundid = tmpid;
3862 break;
3863 }
3864 }
3865 /* continue to scan from next id */
3866 tmpid = tmpid + 1;
3867 }
3868 return ret;
3869}
3870
fe693435
PM
3871#ifdef CONFIG_CGROUP_DEBUG
3872static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
3873 struct cgroup *cont)
3874{
3875 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
3876
3877 if (!css)
3878 return ERR_PTR(-ENOMEM);
3879
3880 return css;
3881}
3882
3883static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
3884{
3885 kfree(cont->subsys[debug_subsys_id]);
3886}
3887
3888static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
3889{
3890 return atomic_read(&cont->count);
3891}
3892
3893static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
3894{
3895 return cgroup_task_count(cont);
3896}
3897
3898static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
3899{
3900 return (u64)(unsigned long)current->cgroups;
3901}
3902
3903static u64 current_css_set_refcount_read(struct cgroup *cont,
3904 struct cftype *cft)
3905{
3906 u64 count;
3907
3908 rcu_read_lock();
3909 count = atomic_read(&current->cgroups->refcount);
3910 rcu_read_unlock();
3911 return count;
3912}
3913
7717f7ba
PM
3914static int current_css_set_cg_links_read(struct cgroup *cont,
3915 struct cftype *cft,
3916 struct seq_file *seq)
3917{
3918 struct cg_cgroup_link *link;
3919 struct css_set *cg;
3920
3921 read_lock(&css_set_lock);
3922 rcu_read_lock();
3923 cg = rcu_dereference(current->cgroups);
3924 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
3925 struct cgroup *c = link->cgrp;
3926 const char *name;
3927
3928 if (c->dentry)
3929 name = c->dentry->d_name.name;
3930 else
3931 name = "?";
3932 seq_printf(seq, "Root %lu group %s\n",
3933 c->root->subsys_bits, name);
3934 }
3935 rcu_read_unlock();
3936 read_unlock(&css_set_lock);
3937 return 0;
3938}
3939
3940#define MAX_TASKS_SHOWN_PER_CSS 25
3941static int cgroup_css_links_read(struct cgroup *cont,
3942 struct cftype *cft,
3943 struct seq_file *seq)
3944{
3945 struct cg_cgroup_link *link;
3946
3947 read_lock(&css_set_lock);
3948 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
3949 struct css_set *cg = link->cg;
3950 struct task_struct *task;
3951 int count = 0;
3952 seq_printf(seq, "css_set %p\n", cg);
3953 list_for_each_entry(task, &cg->tasks, cg_list) {
3954 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
3955 seq_puts(seq, " ...\n");
3956 break;
3957 } else {
3958 seq_printf(seq, " task %d\n",
3959 task_pid_vnr(task));
3960 }
3961 }
3962 }
3963 read_unlock(&css_set_lock);
3964 return 0;
3965}
3966
fe693435
PM
3967static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
3968{
3969 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
3970}
3971
3972static struct cftype debug_files[] = {
3973 {
3974 .name = "cgroup_refcount",
3975 .read_u64 = cgroup_refcount_read,
3976 },
3977 {
3978 .name = "taskcount",
3979 .read_u64 = debug_taskcount_read,
3980 },
3981
3982 {
3983 .name = "current_css_set",
3984 .read_u64 = current_css_set_read,
3985 },
3986
3987 {
3988 .name = "current_css_set_refcount",
3989 .read_u64 = current_css_set_refcount_read,
3990 },
3991
7717f7ba
PM
3992 {
3993 .name = "current_css_set_cg_links",
3994 .read_seq_string = current_css_set_cg_links_read,
3995 },
3996
3997 {
3998 .name = "cgroup_css_links",
3999 .read_seq_string = cgroup_css_links_read,
4000 },
4001
fe693435
PM
4002 {
4003 .name = "releasable",
4004 .read_u64 = releasable_read,
4005 },
4006};
4007
4008static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4009{
4010 return cgroup_add_files(cont, ss, debug_files,
4011 ARRAY_SIZE(debug_files));
4012}
4013
4014struct cgroup_subsys debug_subsys = {
4015 .name = "debug",
4016 .create = debug_create,
4017 .destroy = debug_destroy,
4018 .populate = debug_populate,
4019 .subsys_id = debug_subsys_id,
4020};
4021#endif /* CONFIG_CGROUP_DEBUG */