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