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