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