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