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