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