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