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