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