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