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