cgroup: saner refcounting for cgroup_root
[linux-2.6-block.git] / kernel / cgroup / 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
0a268dbd
TH
31#include "cgroup-internal.h"
32
2ce9738b 33#include <linux/cred.h>
ddbcc7e8 34#include <linux/errno.h>
2ce9738b 35#include <linux/init_task.h>
ddbcc7e8 36#include <linux/kernel.h>
c9482a5b 37#include <linux/magic.h>
ddbcc7e8
PM
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
a424316c 41#include <linux/proc_fs.h>
ddbcc7e8
PM
42#include <linux/rcupdate.h>
43#include <linux/sched.h>
29930025 44#include <linux/sched/task.h>
ddbcc7e8 45#include <linux/slab.h>
ddbcc7e8 46#include <linux/spinlock.h>
1ed13287 47#include <linux/percpu-rwsem.h>
ddbcc7e8 48#include <linux/string.h>
0ac801fe 49#include <linux/hashtable.h>
2c6ab6d2 50#include <linux/idr.h>
c4c27fbd 51#include <linux/kthread.h>
60063497 52#include <linux/atomic.h>
e93ad19d 53#include <linux/cpuset.h>
a79a908f
AK
54#include <linux/proc_ns.h>
55#include <linux/nsproxy.h>
1f3fe7eb 56#include <linux/file.h>
d4ff749b 57#include <linux/sched/cputime.h>
2ce7135a 58#include <linux/psi.h>
bd1060a1 59#include <net/sock.h>
ddbcc7e8 60
ed1777de
TH
61#define CREATE_TRACE_POINTS
62#include <trace/events/cgroup.h>
63
8d7e6fb0
TH
64#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
65 MAX_CFTYPE_NAME + 2)
b12e3583
TH
66/* let's not notify more than 100 times per second */
67#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
8d7e6fb0 68
e25e2cbb
TH
69/*
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
72 *
f0d9a5f1 73 * css_set_lock protects task->cgroups pointer, the list of css_set
0e1d768f 74 * objects, and the chain of tasks off each css_set.
e25e2cbb 75 *
0e1d768f
TH
76 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
77 * cgroup.h can use them for lockdep annotations.
e25e2cbb 78 */
2219449a 79DEFINE_MUTEX(cgroup_mutex);
f0d9a5f1 80DEFINE_SPINLOCK(css_set_lock);
0a268dbd
TH
81
82#ifdef CONFIG_PROVE_RCU
0e1d768f 83EXPORT_SYMBOL_GPL(cgroup_mutex);
f0d9a5f1 84EXPORT_SYMBOL_GPL(css_set_lock);
2219449a
TH
85#endif
86
e4f8d81c
SRV
87DEFINE_SPINLOCK(trace_cgroup_path_lock);
88char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
5cf8114d 89bool cgroup_debug __read_mostly;
e4f8d81c 90
6fa4918d 91/*
15a4c835
TH
92 * Protects cgroup_idr and css_idr so that IDs can be released without
93 * grabbing cgroup_mutex.
6fa4918d
TH
94 */
95static DEFINE_SPINLOCK(cgroup_idr_lock);
96
34c06254
TH
97/*
98 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
99 * against file removal/re-creation across css hiding.
100 */
101static DEFINE_SPINLOCK(cgroup_file_kn_lock);
102
1ed13287
TH
103struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
104
8353da1f 105#define cgroup_assert_mutex_or_rcu_locked() \
f78f5b90
PM
106 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
107 !lockdep_is_held(&cgroup_mutex), \
8353da1f 108 "cgroup_mutex or RCU read lock required");
780cd8b3 109
e5fca243
TH
110/*
111 * cgroup destruction makes heavy use of work items and there can be a lot
112 * of concurrent destructions. Use a separate workqueue so that cgroup
113 * destruction work items don't end up filling up max_active of system_wq
114 * which may lead to deadlock.
115 */
116static struct workqueue_struct *cgroup_destroy_wq;
117
3ed80a62 118/* generate an array of cgroup subsystem pointers */
073219e9 119#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
0a268dbd 120struct cgroup_subsys *cgroup_subsys[] = {
ddbcc7e8
PM
121#include <linux/cgroup_subsys.h>
122};
073219e9
TH
123#undef SUBSYS
124
125/* array of cgroup subsystem names */
126#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
127static const char *cgroup_subsys_name[] = {
ddbcc7e8
PM
128#include <linux/cgroup_subsys.h>
129};
073219e9 130#undef SUBSYS
ddbcc7e8 131
49d1dc4b
TH
132/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
133#define SUBSYS(_x) \
134 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
135 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
136 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
137 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
138#include <linux/cgroup_subsys.h>
139#undef SUBSYS
140
141#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
142static struct static_key_true *cgroup_subsys_enabled_key[] = {
143#include <linux/cgroup_subsys.h>
144};
145#undef SUBSYS
146
147#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
148static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
149#include <linux/cgroup_subsys.h>
150};
151#undef SUBSYS
152
c58632b3 153static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
041cd640 154
ddbcc7e8 155/*
3dd06ffa 156 * The default hierarchy, reserved for the subsystems that are otherwise
9871bf95
TH
157 * unattached - it never has more than a single cgroup, and all tasks are
158 * part of that cgroup.
ddbcc7e8 159 */
c58632b3 160struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
d0ec4230 161EXPORT_SYMBOL_GPL(cgrp_dfl_root);
9871bf95 162
a2dd4247
TH
163/*
164 * The default hierarchy always exists but is hidden until mounted for the
165 * first time. This is for backward compatibility.
166 */
a7165264 167static bool cgrp_dfl_visible;
ddbcc7e8 168
5533e011 169/* some controllers are not supported in the default hierarchy */
a7165264 170static u16 cgrp_dfl_inhibit_ss_mask;
5533e011 171
f6d635ad 172/* some controllers are implicitly enabled on the default hierarchy */
b807421a 173static u16 cgrp_dfl_implicit_ss_mask;
f6d635ad 174
8cfd8147
TH
175/* some controllers can be threaded on the default hierarchy */
176static u16 cgrp_dfl_threaded_ss_mask;
177
ddbcc7e8 178/* The list of hierarchy roots */
0a268dbd 179LIST_HEAD(cgroup_roots);
9871bf95 180static int cgroup_root_count;
ddbcc7e8 181
3417ae1f 182/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
1a574231 183static DEFINE_IDR(cgroup_hierarchy_idr);
2c6ab6d2 184
794611a1 185/*
0cb51d71
TH
186 * Assign a monotonically increasing serial number to csses. It guarantees
187 * cgroups with bigger numbers are newer than those with smaller numbers.
188 * Also, as csses are always appended to the parent's ->children list, it
189 * guarantees that sibling csses are always sorted in the ascending serial
190 * number order on the list. Protected by cgroup_mutex.
794611a1 191 */
0cb51d71 192static u64 css_serial_nr_next = 1;
794611a1 193
cb4a3167 194/*
b807421a
TH
195 * These bitmasks identify subsystems with specific features to avoid
196 * having to do iterative checks repeatedly.
ddbcc7e8 197 */
6e5c8307
TH
198static u16 have_fork_callback __read_mostly;
199static u16 have_exit_callback __read_mostly;
200static u16 have_free_callback __read_mostly;
b807421a 201static u16 have_canfork_callback __read_mostly;
ddbcc7e8 202
a79a908f
AK
203/* cgroup namespace for init task */
204struct cgroup_namespace init_cgroup_ns = {
387ad967 205 .count = REFCOUNT_INIT(2),
a79a908f
AK
206 .user_ns = &init_user_ns,
207 .ns.ops = &cgroupns_operations,
208 .ns.inum = PROC_CGROUP_INIT_INO,
209 .root_cset = &init_css_set,
210};
211
67e9c74b 212static struct file_system_type cgroup2_fs_type;
d62beb7f 213static struct cftype cgroup_base_files[];
628f7cd4 214
334c3679
TH
215static int cgroup_apply_control(struct cgroup *cgrp);
216static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
ed27b9f7 217static void css_task_iter_advance(struct css_task_iter *it);
42809dd4 218static int cgroup_destroy_locked(struct cgroup *cgrp);
6cd0f5bb
TH
219static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
220 struct cgroup_subsys *ss);
9d755d33 221static void css_release(struct percpu_ref *ref);
f8f22e53 222static void kill_css(struct cgroup_subsys_state *css);
4df8dc90
TH
223static int cgroup_addrm_files(struct cgroup_subsys_state *css,
224 struct cgroup *cgrp, struct cftype cfts[],
2bb566cb 225 bool is_add);
42809dd4 226
fc5ed1e9
TH
227/**
228 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
229 * @ssid: subsys ID of interest
230 *
231 * cgroup_subsys_enabled() can only be used with literal subsys names which
232 * is fine for individual subsystems but unsuitable for cgroup core. This
233 * is slower static_key_enabled() based test indexed by @ssid.
234 */
0a268dbd 235bool cgroup_ssid_enabled(int ssid)
fc5ed1e9 236{
cfe02a8a
AB
237 if (CGROUP_SUBSYS_COUNT == 0)
238 return false;
239
fc5ed1e9
TH
240 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
241}
242
9e10a130
TH
243/**
244 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
245 * @cgrp: the cgroup of interest
246 *
247 * The default hierarchy is the v2 interface of cgroup and this function
248 * can be used to test whether a cgroup is on the default hierarchy for
249 * cases where a subsystem should behave differnetly depending on the
250 * interface version.
251 *
252 * The set of behaviors which change on the default hierarchy are still
253 * being determined and the mount option is prefixed with __DEVEL__.
254 *
255 * List of changed behaviors:
256 *
257 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
258 * and "name" are disallowed.
259 *
260 * - When mounting an existing superblock, mount options should match.
261 *
262 * - Remount is disallowed.
263 *
264 * - rename(2) is disallowed.
265 *
266 * - "tasks" is removed. Everything should be at process granularity. Use
267 * "cgroup.procs" instead.
268 *
269 * - "cgroup.procs" is not sorted. pids will be unique unless they got
270 * recycled inbetween reads.
271 *
272 * - "release_agent" and "notify_on_release" are removed. Replacement
273 * notification mechanism will be implemented.
274 *
275 * - "cgroup.clone_children" is removed.
276 *
277 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
278 * and its descendants contain no task; otherwise, 1. The file also
279 * generates kernfs notification which can be monitored through poll and
280 * [di]notify when the value of the file changes.
281 *
282 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
283 * take masks of ancestors with non-empty cpus/mems, instead of being
284 * moved to an ancestor.
285 *
286 * - cpuset: a task can be moved into an empty cpuset, and again it takes
287 * masks of ancestors.
288 *
289 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
290 * is not created.
291 *
292 * - blkcg: blk-throttle becomes properly hierarchical.
293 *
294 * - debug: disallowed on the default hierarchy.
295 */
0a268dbd 296bool cgroup_on_dfl(const struct cgroup *cgrp)
9e10a130
TH
297{
298 return cgrp->root == &cgrp_dfl_root;
299}
300
6fa4918d
TH
301/* IDR wrappers which synchronize using cgroup_idr_lock */
302static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
303 gfp_t gfp_mask)
304{
305 int ret;
306
307 idr_preload(gfp_mask);
54504e97 308 spin_lock_bh(&cgroup_idr_lock);
d0164adc 309 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
54504e97 310 spin_unlock_bh(&cgroup_idr_lock);
6fa4918d
TH
311 idr_preload_end();
312 return ret;
313}
314
315static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
316{
317 void *ret;
318
54504e97 319 spin_lock_bh(&cgroup_idr_lock);
6fa4918d 320 ret = idr_replace(idr, ptr, id);
54504e97 321 spin_unlock_bh(&cgroup_idr_lock);
6fa4918d
TH
322 return ret;
323}
324
325static void cgroup_idr_remove(struct idr *idr, int id)
326{
54504e97 327 spin_lock_bh(&cgroup_idr_lock);
6fa4918d 328 idr_remove(idr, id);
54504e97 329 spin_unlock_bh(&cgroup_idr_lock);
6fa4918d
TH
330}
331
27f26753 332static bool cgroup_has_tasks(struct cgroup *cgrp)
d51f39b0 333{
27f26753
TH
334 return cgrp->nr_populated_csets;
335}
d51f39b0 336
7a0cf0e7 337bool cgroup_is_threaded(struct cgroup *cgrp)
454000ad
TH
338{
339 return cgrp->dom_cgrp != cgrp;
340}
341
8cfd8147
TH
342/* can @cgrp host both domain and threaded children? */
343static bool cgroup_is_mixable(struct cgroup *cgrp)
344{
345 /*
346 * Root isn't under domain level resource control exempting it from
347 * the no-internal-process constraint, so it can serve as a thread
348 * root and a parent of resource domains at the same time.
349 */
350 return !cgroup_parent(cgrp);
351}
352
353/* can @cgrp become a thread root? should always be true for a thread root */
354static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
355{
356 /* mixables don't care */
357 if (cgroup_is_mixable(cgrp))
358 return true;
359
360 /* domain roots can't be nested under threaded */
361 if (cgroup_is_threaded(cgrp))
362 return false;
363
364 /* can only have either domain or threaded children */
365 if (cgrp->nr_populated_domain_children)
366 return false;
367
368 /* and no domain controllers can be enabled */
369 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
370 return false;
371
372 return true;
373}
374
375/* is @cgrp root of a threaded subtree? */
7a0cf0e7 376bool cgroup_is_thread_root(struct cgroup *cgrp)
8cfd8147
TH
377{
378 /* thread root should be a domain */
379 if (cgroup_is_threaded(cgrp))
380 return false;
381
382 /* a domain w/ threaded children is a thread root */
383 if (cgrp->nr_threaded_children)
384 return true;
385
386 /*
387 * A domain which has tasks and explicit threaded controllers
388 * enabled is a thread root.
389 */
390 if (cgroup_has_tasks(cgrp) &&
391 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
392 return true;
393
394 return false;
395}
396
397/* a domain which isn't connected to the root w/o brekage can't be used */
398static bool cgroup_is_valid_domain(struct cgroup *cgrp)
399{
400 /* the cgroup itself can be a thread root */
401 if (cgroup_is_threaded(cgrp))
402 return false;
403
404 /* but the ancestors can't be unless mixable */
405 while ((cgrp = cgroup_parent(cgrp))) {
406 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
407 return false;
408 if (cgroup_is_threaded(cgrp))
409 return false;
410 }
411
412 return true;
d51f39b0
TH
413}
414
5531dc91
TH
415/* subsystems visibly enabled on a cgroup */
416static u16 cgroup_control(struct cgroup *cgrp)
417{
418 struct cgroup *parent = cgroup_parent(cgrp);
419 u16 root_ss_mask = cgrp->root->subsys_mask;
420
8cfd8147
TH
421 if (parent) {
422 u16 ss_mask = parent->subtree_control;
423
424 /* threaded cgroups can only have threaded controllers */
425 if (cgroup_is_threaded(cgrp))
426 ss_mask &= cgrp_dfl_threaded_ss_mask;
427 return ss_mask;
428 }
5531dc91
TH
429
430 if (cgroup_on_dfl(cgrp))
f6d635ad
TH
431 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
432 cgrp_dfl_implicit_ss_mask);
5531dc91
TH
433 return root_ss_mask;
434}
435
436/* subsystems enabled on a cgroup */
437static u16 cgroup_ss_mask(struct cgroup *cgrp)
438{
439 struct cgroup *parent = cgroup_parent(cgrp);
440
8cfd8147
TH
441 if (parent) {
442 u16 ss_mask = parent->subtree_ss_mask;
443
444 /* threaded cgroups can only have threaded controllers */
445 if (cgroup_is_threaded(cgrp))
446 ss_mask &= cgrp_dfl_threaded_ss_mask;
447 return ss_mask;
448 }
5531dc91
TH
449
450 return cgrp->root->subsys_mask;
451}
452
95109b62
TH
453/**
454 * cgroup_css - obtain a cgroup's css for the specified subsystem
455 * @cgrp: the cgroup of interest
9d800df1 456 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
95109b62 457 *
ca8bdcaf
TH
458 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
459 * function must be called either under cgroup_mutex or rcu_read_lock() and
460 * the caller is responsible for pinning the returned css if it wants to
461 * keep accessing it outside the said locks. This function may return
462 * %NULL if @cgrp doesn't have @subsys_id enabled.
95109b62
TH
463 */
464static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
ca8bdcaf 465 struct cgroup_subsys *ss)
95109b62 466{
ca8bdcaf 467 if (ss)
aec25020 468 return rcu_dereference_check(cgrp->subsys[ss->id],
ace2bee8 469 lockdep_is_held(&cgroup_mutex));
ca8bdcaf 470 else
9d800df1 471 return &cgrp->self;
95109b62 472}
42809dd4 473
d41bf8c9
TH
474/**
475 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
476 * @cgrp: the cgroup of interest
477 * @ss: the subsystem of interest
478 *
479 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist
480 * or is offline, %NULL is returned.
481 */
482static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
483 struct cgroup_subsys *ss)
484{
485 struct cgroup_subsys_state *css;
486
487 rcu_read_lock();
488 css = cgroup_css(cgrp, ss);
489 if (!css || !css_tryget_online(css))
490 css = NULL;
491 rcu_read_unlock();
492
493 return css;
494}
495
aec3dfcb 496/**
fc5a828b 497 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
aec3dfcb 498 * @cgrp: the cgroup of interest
9d800df1 499 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
aec3dfcb 500 *
d0f702e6 501 * Similar to cgroup_css() but returns the effective css, which is defined
aec3dfcb
TH
502 * as the matching css of the nearest ancestor including self which has @ss
503 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
504 * function is guaranteed to return non-NULL css.
505 */
fc5a828b
DZ
506static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
507 struct cgroup_subsys *ss)
aec3dfcb
TH
508{
509 lockdep_assert_held(&cgroup_mutex);
510
511 if (!ss)
9d800df1 512 return &cgrp->self;
aec3dfcb 513
eeecbd19
TH
514 /*
515 * This function is used while updating css associations and thus
5531dc91 516 * can't test the csses directly. Test ss_mask.
eeecbd19 517 */
5531dc91 518 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
d51f39b0 519 cgrp = cgroup_parent(cgrp);
5531dc91
TH
520 if (!cgrp)
521 return NULL;
522 }
aec3dfcb
TH
523
524 return cgroup_css(cgrp, ss);
95109b62 525}
42809dd4 526
fc5a828b
DZ
527/**
528 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
529 * @cgrp: the cgroup of interest
530 * @ss: the subsystem of interest
531 *
532 * Find and get the effective css of @cgrp for @ss. The effective css is
533 * defined as the matching css of the nearest ancestor including self which
534 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
535 * the root css is returned, so this function always returns a valid css.
536 *
537 * The returned css is not guaranteed to be online, and therefore it is the
538 * callers responsiblity to tryget a reference for it.
539 */
540struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
541 struct cgroup_subsys *ss)
542{
543 struct cgroup_subsys_state *css;
544
545 do {
546 css = cgroup_css(cgrp, ss);
547
548 if (css)
549 return css;
550 cgrp = cgroup_parent(cgrp);
551 } while (cgrp);
552
553 return init_css_set.subsys[ss->id];
554}
555
eeecbd19
TH
556/**
557 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
558 * @cgrp: the cgroup of interest
559 * @ss: the subsystem of interest
560 *
561 * Find and get the effective css of @cgrp for @ss. The effective css is
562 * defined as the matching css of the nearest ancestor including self which
563 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
564 * the root css is returned, so this function always returns a valid css.
565 * The returned css must be put using css_put().
566 */
567struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
568 struct cgroup_subsys *ss)
569{
570 struct cgroup_subsys_state *css;
571
572 rcu_read_lock();
573
574 do {
575 css = cgroup_css(cgrp, ss);
576
577 if (css && css_tryget_online(css))
578 goto out_unlock;
579 cgrp = cgroup_parent(cgrp);
580 } while (cgrp);
581
582 css = init_css_set.subsys[ss->id];
583 css_get(css);
584out_unlock:
585 rcu_read_unlock();
586 return css;
587}
588
a590b90d 589static void cgroup_get_live(struct cgroup *cgrp)
052c3f3a
TH
590{
591 WARN_ON_ONCE(cgroup_is_dead(cgrp));
592 css_get(&cgrp->self);
593}
594
b4168640 595struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
59f5296b 596{
2bd59d48 597 struct cgroup *cgrp = of->kn->parent->priv;
b4168640 598 struct cftype *cft = of_cft(of);
2bd59d48
TH
599
600 /*
601 * This is open and unprotected implementation of cgroup_css().
602 * seq_css() is only called from a kernfs file operation which has
603 * an active reference on the file. Because all the subsystem
604 * files are drained before a css is disassociated with a cgroup,
605 * the matching css from the cgroup's subsys table is guaranteed to
606 * be and stay valid until the enclosing operation is complete.
607 */
608 if (cft->ss)
609 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
610 else
9d800df1 611 return &cgrp->self;
59f5296b 612}
b4168640 613EXPORT_SYMBOL_GPL(of_css);
59f5296b 614
1c6727af
TH
615/**
616 * for_each_css - iterate all css's of a cgroup
617 * @css: the iteration cursor
618 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
619 * @cgrp: the target cgroup to iterate css's of
620 *
aec3dfcb 621 * Should be called under cgroup_[tree_]mutex.
1c6727af
TH
622 */
623#define for_each_css(css, ssid, cgrp) \
624 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
625 if (!((css) = rcu_dereference_check( \
626 (cgrp)->subsys[(ssid)], \
627 lockdep_is_held(&cgroup_mutex)))) { } \
628 else
629
aec3dfcb
TH
630/**
631 * for_each_e_css - iterate all effective css's of a cgroup
632 * @css: the iteration cursor
633 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
634 * @cgrp: the target cgroup to iterate css's of
635 *
636 * Should be called under cgroup_[tree_]mutex.
637 */
fc5a828b
DZ
638#define for_each_e_css(css, ssid, cgrp) \
639 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
640 if (!((css) = cgroup_e_css_by_mask(cgrp, \
641 cgroup_subsys[(ssid)]))) \
642 ; \
aec3dfcb
TH
643 else
644
cb4a3167 645/**
b4e0eeaf 646 * do_each_subsys_mask - filter for_each_subsys with a bitmask
cb4a3167
AS
647 * @ss: the iteration cursor
648 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
b4e0eeaf 649 * @ss_mask: the bitmask
cb4a3167
AS
650 *
651 * The block will only run for cases where the ssid-th bit (1 << ssid) of
b4e0eeaf 652 * @ss_mask is set.
cb4a3167 653 */
b4e0eeaf
TH
654#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
655 unsigned long __ss_mask = (ss_mask); \
656 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
4a705c5c 657 (ssid) = 0; \
b4e0eeaf
TH
658 break; \
659 } \
660 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
661 (ss) = cgroup_subsys[ssid]; \
662 {
663
664#define while_each_subsys_mask() \
665 } \
666 } \
667} while (false)
cb4a3167 668
f8f22e53
TH
669/* iterate over child cgrps, lock should be held throughout iteration */
670#define cgroup_for_each_live_child(child, cgrp) \
d5c419b6 671 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
8353da1f 672 if (({ lockdep_assert_held(&cgroup_mutex); \
f8f22e53
TH
673 cgroup_is_dead(child); })) \
674 ; \
675 else
7ae1bad9 676
ce3f1d9d
TH
677/* walk live descendants in preorder */
678#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
679 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
680 if (({ lockdep_assert_held(&cgroup_mutex); \
681 (dsct) = (d_css)->cgroup; \
682 cgroup_is_dead(dsct); })) \
683 ; \
684 else
685
686/* walk live descendants in postorder */
687#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
688 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
689 if (({ lockdep_assert_held(&cgroup_mutex); \
690 (dsct) = (d_css)->cgroup; \
691 cgroup_is_dead(dsct); })) \
692 ; \
693 else
694
172a2c06
TH
695/*
696 * The default css_set - used by init and its children prior to any
817929ec
PM
697 * hierarchies being mounted. It contains a pointer to the root state
698 * for each subsystem. Also used to anchor the list of css_sets. Not
699 * reference-counted, to improve performance when child cgroups
700 * haven't been created.
701 */
5024ae29 702struct css_set init_css_set = {
4b9502e6 703 .refcount = REFCOUNT_INIT(1),
454000ad 704 .dom_cset = &init_css_set,
172a2c06
TH
705 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
706 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
5f617ebb 707 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
454000ad 708 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
5f617ebb 709 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
172a2c06
TH
710 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
711 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
38683148
TH
712
713 /*
714 * The following field is re-initialized when this cset gets linked
715 * in cgroup_init(). However, let's initialize the field
716 * statically too so that the default cgroup can be accessed safely
717 * early during boot.
718 */
719 .dfl_cgrp = &cgrp_dfl_root.cgrp,
172a2c06 720};
817929ec 721
172a2c06 722static int css_set_count = 1; /* 1 for init_css_set */
817929ec 723
454000ad
TH
724static bool css_set_threaded(struct css_set *cset)
725{
726 return cset->dom_cset != cset;
727}
728
0de0942d
TH
729/**
730 * css_set_populated - does a css_set contain any tasks?
731 * @cset: target css_set
73a7242a
WL
732 *
733 * css_set_populated() should be the same as !!cset->nr_tasks at steady
734 * state. However, css_set_populated() can be called while a task is being
735 * added to or removed from the linked list before the nr_tasks is
736 * properly updated. Hence, we can't just look at ->nr_tasks here.
0de0942d
TH
737 */
738static bool css_set_populated(struct css_set *cset)
739{
f0d9a5f1 740 lockdep_assert_held(&css_set_lock);
0de0942d
TH
741
742 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
743}
744
842b597e 745/**
788b950c 746 * cgroup_update_populated - update the populated count of a cgroup
842b597e
TH
747 * @cgrp: the target cgroup
748 * @populated: inc or dec populated count
749 *
0de0942d 750 * One of the css_sets associated with @cgrp is either getting its first
788b950c
TH
751 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
752 * count is propagated towards root so that a given cgroup's
753 * nr_populated_children is zero iff none of its descendants contain any
754 * tasks.
842b597e 755 *
788b950c
TH
756 * @cgrp's interface file "cgroup.populated" is zero if both
757 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
758 * 1 otherwise. When the sum changes from or to zero, userland is notified
759 * that the content of the interface file has changed. This can be used to
760 * detect when @cgrp and its descendants become populated or empty.
842b597e
TH
761 */
762static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
763{
788b950c
TH
764 struct cgroup *child = NULL;
765 int adj = populated ? 1 : -1;
766
f0d9a5f1 767 lockdep_assert_held(&css_set_lock);
842b597e
TH
768
769 do {
788b950c 770 bool was_populated = cgroup_is_populated(cgrp);
842b597e 771
454000ad 772 if (!child) {
788b950c 773 cgrp->nr_populated_csets += adj;
454000ad
TH
774 } else {
775 if (cgroup_is_threaded(child))
776 cgrp->nr_populated_threaded_children += adj;
777 else
778 cgrp->nr_populated_domain_children += adj;
779 }
842b597e 780
788b950c 781 if (was_populated == cgroup_is_populated(cgrp))
842b597e
TH
782 break;
783
d62beb7f 784 cgroup1_check_for_release(cgrp);
6f60eade
TH
785 cgroup_file_notify(&cgrp->events_file);
786
788b950c 787 child = cgrp;
d51f39b0 788 cgrp = cgroup_parent(cgrp);
842b597e
TH
789 } while (cgrp);
790}
791
0de0942d
TH
792/**
793 * css_set_update_populated - update populated state of a css_set
794 * @cset: target css_set
795 * @populated: whether @cset is populated or depopulated
796 *
797 * @cset is either getting the first task or losing the last. Update the
788b950c 798 * populated counters of all associated cgroups accordingly.
0de0942d
TH
799 */
800static void css_set_update_populated(struct css_set *cset, bool populated)
801{
802 struct cgrp_cset_link *link;
803
f0d9a5f1 804 lockdep_assert_held(&css_set_lock);
0de0942d
TH
805
806 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
807 cgroup_update_populated(link->cgrp, populated);
808}
809
f6d7d049
TH
810/**
811 * css_set_move_task - move a task from one css_set to another
812 * @task: task being moved
813 * @from_cset: css_set @task currently belongs to (may be NULL)
814 * @to_cset: new css_set @task is being moved to (may be NULL)
815 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
816 *
817 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
818 * css_set, @from_cset can be NULL. If @task is being disassociated
819 * instead of moved, @to_cset can be NULL.
820 *
788b950c 821 * This function automatically handles populated counter updates and
ed27b9f7
TH
822 * css_task_iter adjustments but the caller is responsible for managing
823 * @from_cset and @to_cset's reference counts.
f6d7d049
TH
824 */
825static void css_set_move_task(struct task_struct *task,
826 struct css_set *from_cset, struct css_set *to_cset,
827 bool use_mg_tasks)
828{
f0d9a5f1 829 lockdep_assert_held(&css_set_lock);
f6d7d049 830
20b454a6
TH
831 if (to_cset && !css_set_populated(to_cset))
832 css_set_update_populated(to_cset, true);
833
f6d7d049 834 if (from_cset) {
ed27b9f7
TH
835 struct css_task_iter *it, *pos;
836
f6d7d049 837 WARN_ON_ONCE(list_empty(&task->cg_list));
ed27b9f7
TH
838
839 /*
840 * @task is leaving, advance task iterators which are
841 * pointing to it so that they can resume at the next
842 * position. Advancing an iterator might remove it from
843 * the list, use safe walk. See css_task_iter_advance*()
844 * for details.
845 */
846 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
847 iters_node)
848 if (it->task_pos == &task->cg_list)
849 css_task_iter_advance(it);
850
f6d7d049
TH
851 list_del_init(&task->cg_list);
852 if (!css_set_populated(from_cset))
853 css_set_update_populated(from_cset, false);
854 } else {
855 WARN_ON_ONCE(!list_empty(&task->cg_list));
856 }
857
858 if (to_cset) {
859 /*
860 * We are synchronized through cgroup_threadgroup_rwsem
861 * against PF_EXITING setting such that we can't race
862 * against cgroup_exit() changing the css_set to
863 * init_css_set and dropping the old one.
864 */
865 WARN_ON_ONCE(task->flags & PF_EXITING);
866
2ce7135a 867 cgroup_move_task(task, to_cset);
f6d7d049
TH
868 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
869 &to_cset->tasks);
870 }
871}
872
7717f7ba
PM
873/*
874 * hash table for cgroup groups. This improves the performance to find
875 * an existing css_set. This hash doesn't (currently) take into
876 * account cgroups in empty hierarchies.
877 */
472b1053 878#define CSS_SET_HASH_BITS 7
0ac801fe 879static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
472b1053 880
0ac801fe 881static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
472b1053 882{
0ac801fe 883 unsigned long key = 0UL;
30159ec7
TH
884 struct cgroup_subsys *ss;
885 int i;
472b1053 886
30159ec7 887 for_each_subsys(ss, i)
0ac801fe
LZ
888 key += (unsigned long)css[i];
889 key = (key >> 16) ^ key;
472b1053 890
0ac801fe 891 return key;
472b1053
LZ
892}
893
dcfe149b 894void put_css_set_locked(struct css_set *cset)
b4f48b63 895{
69d0206c 896 struct cgrp_cset_link *link, *tmp_link;
2d8f243a
TH
897 struct cgroup_subsys *ss;
898 int ssid;
5abb8855 899
f0d9a5f1 900 lockdep_assert_held(&css_set_lock);
89c5509b 901
4b9502e6 902 if (!refcount_dec_and_test(&cset->refcount))
146aa1bd 903 return;
81a6a5cd 904
454000ad
TH
905 WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
906
53254f90
TH
907 /* This css_set is dead. unlink it and release cgroup and css refs */
908 for_each_subsys(ss, ssid) {
2d8f243a 909 list_del(&cset->e_cset_node[ssid]);
53254f90
TH
910 css_put(cset->subsys[ssid]);
911 }
5abb8855 912 hash_del(&cset->hlist);
2c6ab6d2
PM
913 css_set_count--;
914
69d0206c 915 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
69d0206c
TH
916 list_del(&link->cset_link);
917 list_del(&link->cgrp_link);
2ceb231b
TH
918 if (cgroup_parent(link->cgrp))
919 cgroup_put(link->cgrp);
2c6ab6d2 920 kfree(link);
81a6a5cd 921 }
2c6ab6d2 922
454000ad
TH
923 if (css_set_threaded(cset)) {
924 list_del(&cset->threaded_csets_node);
925 put_css_set_locked(cset->dom_cset);
926 }
927
5abb8855 928 kfree_rcu(cset, rcu_head);
b4f48b63
PM
929}
930
b326f9d0 931/**
7717f7ba 932 * compare_css_sets - helper function for find_existing_css_set().
5abb8855
TH
933 * @cset: candidate css_set being tested
934 * @old_cset: existing css_set for a task
7717f7ba
PM
935 * @new_cgrp: cgroup that's being entered by the task
936 * @template: desired set of css pointers in css_set (pre-calculated)
937 *
6f4b7e63 938 * Returns true if "cset" matches "old_cset" except for the hierarchy
7717f7ba
PM
939 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
940 */
5abb8855
TH
941static bool compare_css_sets(struct css_set *cset,
942 struct css_set *old_cset,
7717f7ba
PM
943 struct cgroup *new_cgrp,
944 struct cgroup_subsys_state *template[])
945{
454000ad 946 struct cgroup *new_dfl_cgrp;
7717f7ba
PM
947 struct list_head *l1, *l2;
948
aec3dfcb
TH
949 /*
950 * On the default hierarchy, there can be csets which are
951 * associated with the same set of cgroups but different csses.
952 * Let's first ensure that csses match.
953 */
954 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
7717f7ba 955 return false;
7717f7ba 956
454000ad
TH
957
958 /* @cset's domain should match the default cgroup's */
959 if (cgroup_on_dfl(new_cgrp))
960 new_dfl_cgrp = new_cgrp;
961 else
962 new_dfl_cgrp = old_cset->dfl_cgrp;
963
964 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
965 return false;
966
7717f7ba
PM
967 /*
968 * Compare cgroup pointers in order to distinguish between
aec3dfcb
TH
969 * different cgroups in hierarchies. As different cgroups may
970 * share the same effective css, this comparison is always
971 * necessary.
7717f7ba 972 */
69d0206c
TH
973 l1 = &cset->cgrp_links;
974 l2 = &old_cset->cgrp_links;
7717f7ba 975 while (1) {
69d0206c 976 struct cgrp_cset_link *link1, *link2;
5abb8855 977 struct cgroup *cgrp1, *cgrp2;
7717f7ba
PM
978
979 l1 = l1->next;
980 l2 = l2->next;
981 /* See if we reached the end - both lists are equal length. */
69d0206c
TH
982 if (l1 == &cset->cgrp_links) {
983 BUG_ON(l2 != &old_cset->cgrp_links);
7717f7ba
PM
984 break;
985 } else {
69d0206c 986 BUG_ON(l2 == &old_cset->cgrp_links);
7717f7ba
PM
987 }
988 /* Locate the cgroups associated with these links. */
69d0206c
TH
989 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
990 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
991 cgrp1 = link1->cgrp;
992 cgrp2 = link2->cgrp;
7717f7ba 993 /* Hierarchies should be linked in the same order. */
5abb8855 994 BUG_ON(cgrp1->root != cgrp2->root);
7717f7ba
PM
995
996 /*
997 * If this hierarchy is the hierarchy of the cgroup
998 * that's changing, then we need to check that this
999 * css_set points to the new cgroup; if it's any other
1000 * hierarchy, then this css_set should point to the
1001 * same cgroup as the old css_set.
1002 */
5abb8855
TH
1003 if (cgrp1->root == new_cgrp->root) {
1004 if (cgrp1 != new_cgrp)
7717f7ba
PM
1005 return false;
1006 } else {
5abb8855 1007 if (cgrp1 != cgrp2)
7717f7ba
PM
1008 return false;
1009 }
1010 }
1011 return true;
1012}
1013
b326f9d0
TH
1014/**
1015 * find_existing_css_set - init css array and find the matching css_set
1016 * @old_cset: the css_set that we're using before the cgroup transition
1017 * @cgrp: the cgroup that we're moving into
1018 * @template: out param for the new set of csses, should be clear on entry
817929ec 1019 */
5abb8855
TH
1020static struct css_set *find_existing_css_set(struct css_set *old_cset,
1021 struct cgroup *cgrp,
1022 struct cgroup_subsys_state *template[])
b4f48b63 1023{
3dd06ffa 1024 struct cgroup_root *root = cgrp->root;
30159ec7 1025 struct cgroup_subsys *ss;
5abb8855 1026 struct css_set *cset;
0ac801fe 1027 unsigned long key;
b326f9d0 1028 int i;
817929ec 1029
aae8aab4
BB
1030 /*
1031 * Build the set of subsystem state objects that we want to see in the
1032 * new css_set. while subsystems can change globally, the entries here
1033 * won't change, so no need for locking.
1034 */
30159ec7 1035 for_each_subsys(ss, i) {
f392e51c 1036 if (root->subsys_mask & (1UL << i)) {
aec3dfcb
TH
1037 /*
1038 * @ss is in this hierarchy, so we want the
1039 * effective css from @cgrp.
1040 */
fc5a828b 1041 template[i] = cgroup_e_css_by_mask(cgrp, ss);
817929ec 1042 } else {
aec3dfcb
TH
1043 /*
1044 * @ss is not in this hierarchy, so we don't want
1045 * to change the css.
1046 */
5abb8855 1047 template[i] = old_cset->subsys[i];
817929ec
PM
1048 }
1049 }
1050
0ac801fe 1051 key = css_set_hash(template);
5abb8855
TH
1052 hash_for_each_possible(css_set_table, cset, hlist, key) {
1053 if (!compare_css_sets(cset, old_cset, cgrp, template))
7717f7ba
PM
1054 continue;
1055
1056 /* This css_set matches what we need */
5abb8855 1057 return cset;
472b1053 1058 }
817929ec
PM
1059
1060 /* No existing cgroup group matched */
1061 return NULL;
1062}
1063
69d0206c 1064static void free_cgrp_cset_links(struct list_head *links_to_free)
36553434 1065{
69d0206c 1066 struct cgrp_cset_link *link, *tmp_link;
36553434 1067
69d0206c
TH
1068 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1069 list_del(&link->cset_link);
36553434
LZ
1070 kfree(link);
1071 }
1072}
1073
69d0206c
TH
1074/**
1075 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1076 * @count: the number of links to allocate
1077 * @tmp_links: list_head the allocated links are put on
1078 *
1079 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1080 * through ->cset_link. Returns 0 on success or -errno.
817929ec 1081 */
69d0206c 1082static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
817929ec 1083{
69d0206c 1084 struct cgrp_cset_link *link;
817929ec 1085 int i;
69d0206c
TH
1086
1087 INIT_LIST_HEAD(tmp_links);
1088
817929ec 1089 for (i = 0; i < count; i++) {
f4f4be2b 1090 link = kzalloc(sizeof(*link), GFP_KERNEL);
817929ec 1091 if (!link) {
69d0206c 1092 free_cgrp_cset_links(tmp_links);
817929ec
PM
1093 return -ENOMEM;
1094 }
69d0206c 1095 list_add(&link->cset_link, tmp_links);
817929ec
PM
1096 }
1097 return 0;
1098}
1099
c12f65d4
LZ
1100/**
1101 * link_css_set - a helper function to link a css_set to a cgroup
69d0206c 1102 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
5abb8855 1103 * @cset: the css_set to be linked
c12f65d4
LZ
1104 * @cgrp: the destination cgroup
1105 */
69d0206c
TH
1106static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1107 struct cgroup *cgrp)
c12f65d4 1108{
69d0206c 1109 struct cgrp_cset_link *link;
c12f65d4 1110
69d0206c 1111 BUG_ON(list_empty(tmp_links));
6803c006
TH
1112
1113 if (cgroup_on_dfl(cgrp))
1114 cset->dfl_cgrp = cgrp;
1115
69d0206c
TH
1116 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1117 link->cset = cset;
7717f7ba 1118 link->cgrp = cgrp;
842b597e 1119
7717f7ba 1120 /*
389b9c1b
TH
1121 * Always add links to the tail of the lists so that the lists are
1122 * in choronological order.
7717f7ba 1123 */
389b9c1b 1124 list_move_tail(&link->cset_link, &cgrp->cset_links);
69d0206c 1125 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
2ceb231b
TH
1126
1127 if (cgroup_parent(cgrp))
a590b90d 1128 cgroup_get_live(cgrp);
c12f65d4
LZ
1129}
1130
b326f9d0
TH
1131/**
1132 * find_css_set - return a new css_set with one cgroup updated
1133 * @old_cset: the baseline css_set
1134 * @cgrp: the cgroup to be updated
1135 *
1136 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1137 * substituted into the appropriate hierarchy.
817929ec 1138 */
5abb8855
TH
1139static struct css_set *find_css_set(struct css_set *old_cset,
1140 struct cgroup *cgrp)
817929ec 1141{
b326f9d0 1142 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
5abb8855 1143 struct css_set *cset;
69d0206c
TH
1144 struct list_head tmp_links;
1145 struct cgrp_cset_link *link;
2d8f243a 1146 struct cgroup_subsys *ss;
0ac801fe 1147 unsigned long key;
2d8f243a 1148 int ssid;
472b1053 1149
b326f9d0
TH
1150 lockdep_assert_held(&cgroup_mutex);
1151
817929ec
PM
1152 /* First see if we already have a cgroup group that matches
1153 * the desired set */
82d6489d 1154 spin_lock_irq(&css_set_lock);
5abb8855
TH
1155 cset = find_existing_css_set(old_cset, cgrp, template);
1156 if (cset)
1157 get_css_set(cset);
82d6489d 1158 spin_unlock_irq(&css_set_lock);
817929ec 1159
5abb8855
TH
1160 if (cset)
1161 return cset;
817929ec 1162
f4f4be2b 1163 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
5abb8855 1164 if (!cset)
817929ec
PM
1165 return NULL;
1166
69d0206c 1167 /* Allocate all the cgrp_cset_link objects that we'll need */
9871bf95 1168 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
5abb8855 1169 kfree(cset);
817929ec
PM
1170 return NULL;
1171 }
1172
4b9502e6 1173 refcount_set(&cset->refcount, 1);
454000ad 1174 cset->dom_cset = cset;
5abb8855 1175 INIT_LIST_HEAD(&cset->tasks);
c7561128 1176 INIT_LIST_HEAD(&cset->mg_tasks);
ed27b9f7 1177 INIT_LIST_HEAD(&cset->task_iters);
454000ad 1178 INIT_LIST_HEAD(&cset->threaded_csets);
5abb8855 1179 INIT_HLIST_NODE(&cset->hlist);
5f617ebb
TH
1180 INIT_LIST_HEAD(&cset->cgrp_links);
1181 INIT_LIST_HEAD(&cset->mg_preload_node);
1182 INIT_LIST_HEAD(&cset->mg_node);
817929ec
PM
1183
1184 /* Copy the set of subsystem state objects generated in
1185 * find_existing_css_set() */
5abb8855 1186 memcpy(cset->subsys, template, sizeof(cset->subsys));
817929ec 1187
82d6489d 1188 spin_lock_irq(&css_set_lock);
817929ec 1189 /* Add reference counts and links from the new css_set. */
69d0206c 1190 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
7717f7ba 1191 struct cgroup *c = link->cgrp;
69d0206c 1192
7717f7ba
PM
1193 if (c->root == cgrp->root)
1194 c = cgrp;
69d0206c 1195 link_css_set(&tmp_links, cset, c);
7717f7ba 1196 }
817929ec 1197
69d0206c 1198 BUG_ON(!list_empty(&tmp_links));
817929ec 1199
817929ec 1200 css_set_count++;
472b1053 1201
2d8f243a 1202 /* Add @cset to the hash table */
5abb8855
TH
1203 key = css_set_hash(cset->subsys);
1204 hash_add(css_set_table, &cset->hlist, key);
472b1053 1205
53254f90
TH
1206 for_each_subsys(ss, ssid) {
1207 struct cgroup_subsys_state *css = cset->subsys[ssid];
1208
2d8f243a 1209 list_add_tail(&cset->e_cset_node[ssid],
53254f90
TH
1210 &css->cgroup->e_csets[ssid]);
1211 css_get(css);
1212 }
2d8f243a 1213
82d6489d 1214 spin_unlock_irq(&css_set_lock);
817929ec 1215
454000ad
TH
1216 /*
1217 * If @cset should be threaded, look up the matching dom_cset and
1218 * link them up. We first fully initialize @cset then look for the
1219 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1220 * to stay empty until we return.
1221 */
1222 if (cgroup_is_threaded(cset->dfl_cgrp)) {
1223 struct css_set *dcset;
1224
1225 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1226 if (!dcset) {
1227 put_css_set(cset);
1228 return NULL;
1229 }
1230
1231 spin_lock_irq(&css_set_lock);
1232 cset->dom_cset = dcset;
1233 list_add_tail(&cset->threaded_csets_node,
1234 &dcset->threaded_csets);
1235 spin_unlock_irq(&css_set_lock);
1236 }
1237
5abb8855 1238 return cset;
b4f48b63
PM
1239}
1240
0a268dbd 1241struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
7717f7ba 1242{
3dd06ffa 1243 struct cgroup *root_cgrp = kf_root->kn->priv;
2bd59d48 1244
3dd06ffa 1245 return root_cgrp->root;
2bd59d48
TH
1246}
1247
3dd06ffa 1248static int cgroup_init_root_id(struct cgroup_root *root)
f2e85d57
TH
1249{
1250 int id;
1251
1252 lockdep_assert_held(&cgroup_mutex);
1253
985ed670 1254 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
f2e85d57
TH
1255 if (id < 0)
1256 return id;
1257
1258 root->hierarchy_id = id;
1259 return 0;
1260}
1261
3dd06ffa 1262static void cgroup_exit_root_id(struct cgroup_root *root)
f2e85d57
TH
1263{
1264 lockdep_assert_held(&cgroup_mutex);
1265
8c8a5502 1266 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
f2e85d57
TH
1267}
1268
1592c9b2 1269void cgroup_free_root(struct cgroup_root *root)
f2e85d57
TH
1270{
1271 if (root) {
f2e85d57
TH
1272 idr_destroy(&root->cgroup_idr);
1273 kfree(root);
1274 }
1275}
1276
3dd06ffa 1277static void cgroup_destroy_root(struct cgroup_root *root)
59f5296b 1278{
3dd06ffa 1279 struct cgroup *cgrp = &root->cgrp;
f2e85d57 1280 struct cgrp_cset_link *link, *tmp_link;
f2e85d57 1281
ed1777de
TH
1282 trace_cgroup_destroy_root(root);
1283
334c3679 1284 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
f2e85d57 1285
776f02fa 1286 BUG_ON(atomic_read(&root->nr_cgrps));
d5c419b6 1287 BUG_ON(!list_empty(&cgrp->self.children));
f2e85d57 1288
f2e85d57 1289 /* Rebind all subsystems back to the default hierarchy */
334c3679 1290 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
7717f7ba 1291
7717f7ba 1292 /*
f2e85d57
TH
1293 * Release all the links from cset_links to this hierarchy's
1294 * root cgroup
7717f7ba 1295 */
82d6489d 1296 spin_lock_irq(&css_set_lock);
f2e85d57
TH
1297
1298 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1299 list_del(&link->cset_link);
1300 list_del(&link->cgrp_link);
1301 kfree(link);
1302 }
f0d9a5f1 1303
82d6489d 1304 spin_unlock_irq(&css_set_lock);
f2e85d57
TH
1305
1306 if (!list_empty(&root->root_list)) {
1307 list_del(&root->root_list);
1308 cgroup_root_count--;
1309 }
1310
1311 cgroup_exit_root_id(root);
1312
1313 mutex_unlock(&cgroup_mutex);
f2e85d57 1314
2bd59d48 1315 kernfs_destroy_root(root->kf_root);
f2e85d57
TH
1316 cgroup_free_root(root);
1317}
1318
4f41fc59
SH
1319/*
1320 * look up cgroup associated with current task's cgroup namespace on the
1321 * specified hierarchy
1322 */
1323static struct cgroup *
1324current_cgns_cgroup_from_root(struct cgroup_root *root)
1325{
1326 struct cgroup *res = NULL;
1327 struct css_set *cset;
1328
1329 lockdep_assert_held(&css_set_lock);
1330
1331 rcu_read_lock();
1332
1333 cset = current->nsproxy->cgroup_ns->root_cset;
1334 if (cset == &init_css_set) {
1335 res = &root->cgrp;
1336 } else {
1337 struct cgrp_cset_link *link;
1338
1339 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1340 struct cgroup *c = link->cgrp;
1341
1342 if (c->root == root) {
1343 res = c;
1344 break;
1345 }
1346 }
1347 }
1348 rcu_read_unlock();
1349
1350 BUG_ON(!res);
1351 return res;
1352}
1353
ceb6a081
TH
1354/* look up cgroup associated with given css_set on the specified hierarchy */
1355static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
3dd06ffa 1356 struct cgroup_root *root)
7717f7ba 1357{
7717f7ba
PM
1358 struct cgroup *res = NULL;
1359
96d365e0 1360 lockdep_assert_held(&cgroup_mutex);
f0d9a5f1 1361 lockdep_assert_held(&css_set_lock);
96d365e0 1362
5abb8855 1363 if (cset == &init_css_set) {
3dd06ffa 1364 res = &root->cgrp;
13d82fb7
TH
1365 } else if (root == &cgrp_dfl_root) {
1366 res = cset->dfl_cgrp;
7717f7ba 1367 } else {
69d0206c
TH
1368 struct cgrp_cset_link *link;
1369
1370 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
7717f7ba 1371 struct cgroup *c = link->cgrp;
69d0206c 1372
7717f7ba
PM
1373 if (c->root == root) {
1374 res = c;
1375 break;
1376 }
1377 }
1378 }
96d365e0 1379
7717f7ba
PM
1380 BUG_ON(!res);
1381 return res;
1382}
1383
ddbcc7e8 1384/*
ceb6a081 1385 * Return the cgroup for "task" from the given hierarchy. Must be
f0d9a5f1 1386 * called with cgroup_mutex and css_set_lock held.
ceb6a081 1387 */
0a268dbd
TH
1388struct cgroup *task_cgroup_from_root(struct task_struct *task,
1389 struct cgroup_root *root)
ceb6a081
TH
1390{
1391 /*
1392 * No need to lock the task - since we hold cgroup_mutex the
1393 * task can't change groups, so the only thing that can happen
1394 * is that it exits and its css is set back to init_css_set.
1395 */
1396 return cset_cgroup_from_root(task_css_set(task), root);
1397}
1398
ddbcc7e8 1399/*
ddbcc7e8
PM
1400 * A task must hold cgroup_mutex to modify cgroups.
1401 *
1402 * Any task can increment and decrement the count field without lock.
1403 * So in general, code holding cgroup_mutex can't rely on the count
1404 * field not changing. However, if the count goes to zero, then only
956db3ca 1405 * cgroup_attach_task() can increment it again. Because a count of zero
ddbcc7e8
PM
1406 * means that no tasks are currently attached, therefore there is no
1407 * way a task attached to that cgroup can fork (the other way to
1408 * increment the count). So code holding cgroup_mutex can safely
1409 * assume that if the count is zero, it will stay zero. Similarly, if
1410 * a task holds cgroup_mutex on a cgroup with zero count, it
1411 * knows that the cgroup won't be removed, as cgroup_rmdir()
1412 * needs that mutex.
1413 *
ddbcc7e8
PM
1414 * A cgroup can only be deleted if both its 'count' of using tasks
1415 * is zero, and its list of 'children' cgroups is empty. Since all
1416 * tasks in the system use _some_ cgroup, and since there is always at
3dd06ffa 1417 * least one task in the system (init, pid == 1), therefore, root cgroup
ddbcc7e8 1418 * always has either children cgroups and/or using tasks. So we don't
3dd06ffa 1419 * need a special hack to ensure that root cgroup cannot be deleted.
ddbcc7e8
PM
1420 *
1421 * P.S. One more locking exception. RCU is used to guard the
956db3ca 1422 * update of a tasks cgroup pointer by cgroup_attach_task()
ddbcc7e8
PM
1423 */
1424
2bd59d48 1425static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
a424316c 1426
8d7e6fb0
TH
1427static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1428 char *buf)
ddbcc7e8 1429{
3e1d2eed
TH
1430 struct cgroup_subsys *ss = cft->ss;
1431
8d7e6fb0 1432 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
c1bbd933
TH
1433 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
1434 const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
1435
1436 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
1437 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
3e1d2eed 1438 cft->name);
c1bbd933 1439 } else {
08a77676 1440 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
c1bbd933 1441 }
8d7e6fb0 1442 return buf;
ddbcc7e8
PM
1443}
1444
f2e85d57
TH
1445/**
1446 * cgroup_file_mode - deduce file mode of a control file
1447 * @cft: the control file in question
1448 *
7dbdb199 1449 * S_IRUGO for read, S_IWUSR for write.
f2e85d57
TH
1450 */
1451static umode_t cgroup_file_mode(const struct cftype *cft)
65dff759 1452{
f2e85d57 1453 umode_t mode = 0;
65dff759 1454
f2e85d57
TH
1455 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1456 mode |= S_IRUGO;
1457
7dbdb199
TH
1458 if (cft->write_u64 || cft->write_s64 || cft->write) {
1459 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1460 mode |= S_IWUGO;
1461 else
1462 mode |= S_IWUSR;
1463 }
f2e85d57
TH
1464
1465 return mode;
65dff759
LZ
1466}
1467
af0ba678 1468/**
8699b776 1469 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
0f060deb 1470 * @subtree_control: the new subtree_control mask to consider
5ced2518 1471 * @this_ss_mask: available subsystems
af0ba678
TH
1472 *
1473 * On the default hierarchy, a subsystem may request other subsystems to be
1474 * enabled together through its ->depends_on mask. In such cases, more
1475 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1476 *
0f060deb 1477 * This function calculates which subsystems need to be enabled if
5ced2518 1478 * @subtree_control is to be applied while restricted to @this_ss_mask.
af0ba678 1479 */
5ced2518 1480static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
667c2491 1481{
6e5c8307 1482 u16 cur_ss_mask = subtree_control;
af0ba678
TH
1483 struct cgroup_subsys *ss;
1484 int ssid;
1485
1486 lockdep_assert_held(&cgroup_mutex);
1487
f6d635ad
TH
1488 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1489
af0ba678 1490 while (true) {
6e5c8307 1491 u16 new_ss_mask = cur_ss_mask;
af0ba678 1492
b4e0eeaf 1493 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
a966a4ed 1494 new_ss_mask |= ss->depends_on;
b4e0eeaf 1495 } while_each_subsys_mask();
af0ba678
TH
1496
1497 /*
1498 * Mask out subsystems which aren't available. This can
1499 * happen only if some depended-upon subsystems were bound
1500 * to non-default hierarchies.
1501 */
5ced2518 1502 new_ss_mask &= this_ss_mask;
af0ba678
TH
1503
1504 if (new_ss_mask == cur_ss_mask)
1505 break;
1506 cur_ss_mask = new_ss_mask;
1507 }
1508
0f060deb
TH
1509 return cur_ss_mask;
1510}
1511
a9746d8d
TH
1512/**
1513 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1514 * @kn: the kernfs_node being serviced
1515 *
1516 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1517 * the method finishes if locking succeeded. Note that once this function
1518 * returns the cgroup returned by cgroup_kn_lock_live() may become
1519 * inaccessible any time. If the caller intends to continue to access the
1520 * cgroup, it should pin it before invoking this function.
1521 */
0a268dbd 1522void cgroup_kn_unlock(struct kernfs_node *kn)
ddbcc7e8 1523{
a9746d8d
TH
1524 struct cgroup *cgrp;
1525
1526 if (kernfs_type(kn) == KERNFS_DIR)
1527 cgrp = kn->priv;
1528 else
1529 cgrp = kn->parent->priv;
1530
1531 mutex_unlock(&cgroup_mutex);
a9746d8d
TH
1532
1533 kernfs_unbreak_active_protection(kn);
1534 cgroup_put(cgrp);
ddbcc7e8
PM
1535}
1536
a9746d8d
TH
1537/**
1538 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1539 * @kn: the kernfs_node being serviced
945ba199 1540 * @drain_offline: perform offline draining on the cgroup
a9746d8d
TH
1541 *
1542 * This helper is to be used by a cgroup kernfs method currently servicing
1543 * @kn. It breaks the active protection, performs cgroup locking and
1544 * verifies that the associated cgroup is alive. Returns the cgroup if
1545 * alive; otherwise, %NULL. A successful return should be undone by a
945ba199
TH
1546 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1547 * cgroup is drained of offlining csses before return.
a9746d8d
TH
1548 *
1549 * Any cgroup kernfs method implementation which requires locking the
1550 * associated cgroup should use this helper. It avoids nesting cgroup
1551 * locking under kernfs active protection and allows all kernfs operations
1552 * including self-removal.
1553 */
0a268dbd 1554struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
05ef1d7c 1555{
a9746d8d
TH
1556 struct cgroup *cgrp;
1557
1558 if (kernfs_type(kn) == KERNFS_DIR)
1559 cgrp = kn->priv;
1560 else
1561 cgrp = kn->parent->priv;
05ef1d7c 1562
2739d3cc 1563 /*
01f6474c 1564 * We're gonna grab cgroup_mutex which nests outside kernfs
a9746d8d
TH
1565 * active_ref. cgroup liveliness check alone provides enough
1566 * protection against removal. Ensure @cgrp stays accessible and
1567 * break the active_ref protection.
2739d3cc 1568 */
aa32362f
LZ
1569 if (!cgroup_tryget(cgrp))
1570 return NULL;
a9746d8d
TH
1571 kernfs_break_active_protection(kn);
1572
945ba199
TH
1573 if (drain_offline)
1574 cgroup_lock_and_drain_offline(cgrp);
1575 else
1576 mutex_lock(&cgroup_mutex);
05ef1d7c 1577
a9746d8d
TH
1578 if (!cgroup_is_dead(cgrp))
1579 return cgrp;
1580
1581 cgroup_kn_unlock(kn);
1582 return NULL;
ddbcc7e8 1583}
05ef1d7c 1584
2739d3cc 1585static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
05ef1d7c 1586{
2bd59d48 1587 char name[CGROUP_FILE_NAME_MAX];
05ef1d7c 1588
01f6474c 1589 lockdep_assert_held(&cgroup_mutex);
34c06254
TH
1590
1591 if (cft->file_offset) {
1592 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1593 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1594
1595 spin_lock_irq(&cgroup_file_kn_lock);
1596 cfile->kn = NULL;
1597 spin_unlock_irq(&cgroup_file_kn_lock);
b12e3583
TH
1598
1599 del_timer_sync(&cfile->notify_timer);
34c06254
TH
1600 }
1601
2bd59d48 1602 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
05ef1d7c
TH
1603}
1604
13af07df 1605/**
4df8dc90
TH
1606 * css_clear_dir - remove subsys files in a cgroup directory
1607 * @css: taget css
13af07df 1608 */
334c3679 1609static void css_clear_dir(struct cgroup_subsys_state *css)
05ef1d7c 1610{
334c3679 1611 struct cgroup *cgrp = css->cgroup;
4df8dc90 1612 struct cftype *cfts;
05ef1d7c 1613
88cb04b9
TH
1614 if (!(css->flags & CSS_VISIBLE))
1615 return;
1616
1617 css->flags &= ~CSS_VISIBLE;
1618
5faaf05f
TH
1619 if (!css->ss) {
1620 if (cgroup_on_dfl(cgrp))
1621 cfts = cgroup_base_files;
1622 else
1623 cfts = cgroup1_base_files;
1624
4df8dc90 1625 cgroup_addrm_files(css, cgrp, cfts, false);
5faaf05f
TH
1626 } else {
1627 list_for_each_entry(cfts, &css->ss->cfts, node)
1628 cgroup_addrm_files(css, cgrp, cfts, false);
1629 }
ddbcc7e8
PM
1630}
1631
ccdca218 1632/**
4df8dc90
TH
1633 * css_populate_dir - create subsys files in a cgroup directory
1634 * @css: target css
ccdca218
TH
1635 *
1636 * On failure, no file is added.
1637 */
334c3679 1638static int css_populate_dir(struct cgroup_subsys_state *css)
ccdca218 1639{
334c3679 1640 struct cgroup *cgrp = css->cgroup;
4df8dc90
TH
1641 struct cftype *cfts, *failed_cfts;
1642 int ret;
ccdca218 1643
03970d3c 1644 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
88cb04b9
TH
1645 return 0;
1646
4df8dc90
TH
1647 if (!css->ss) {
1648 if (cgroup_on_dfl(cgrp))
d62beb7f 1649 cfts = cgroup_base_files;
4df8dc90 1650 else
d62beb7f 1651 cfts = cgroup1_base_files;
ccdca218 1652
5faaf05f
TH
1653 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1654 if (ret < 0)
1655 return ret;
1656 } else {
1657 list_for_each_entry(cfts, &css->ss->cfts, node) {
1658 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1659 if (ret < 0) {
1660 failed_cfts = cfts;
1661 goto err;
1662 }
ccdca218
TH
1663 }
1664 }
88cb04b9
TH
1665
1666 css->flags |= CSS_VISIBLE;
1667
ccdca218
TH
1668 return 0;
1669err:
4df8dc90
TH
1670 list_for_each_entry(cfts, &css->ss->cfts, node) {
1671 if (cfts == failed_cfts)
1672 break;
1673 cgroup_addrm_files(css, cgrp, cfts, false);
1674 }
ccdca218
TH
1675 return ret;
1676}
1677
0a268dbd 1678int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
ddbcc7e8 1679{
1ada4838 1680 struct cgroup *dcgrp = &dst_root->cgrp;
30159ec7 1681 struct cgroup_subsys *ss;
2d8f243a 1682 int ssid, i, ret;
ddbcc7e8 1683
ace2bee8 1684 lockdep_assert_held(&cgroup_mutex);
ddbcc7e8 1685
b4e0eeaf 1686 do_each_subsys_mask(ss, ssid, ss_mask) {
f6d635ad
TH
1687 /*
1688 * If @ss has non-root csses attached to it, can't move.
1689 * If @ss is an implicit controller, it is exempt from this
1690 * rule and can be stolen.
1691 */
1692 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1693 !ss->implicit_on_dfl)
3ed80a62 1694 return -EBUSY;
1d5be6b2 1695
5df36032 1696 /* can't move between two non-dummy roots either */
7fd8c565 1697 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
5df36032 1698 return -EBUSY;
b4e0eeaf 1699 } while_each_subsys_mask();
ddbcc7e8 1700
b4e0eeaf 1701 do_each_subsys_mask(ss, ssid, ss_mask) {
1ada4838
TH
1702 struct cgroup_root *src_root = ss->root;
1703 struct cgroup *scgrp = &src_root->cgrp;
1704 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
2d8f243a 1705 struct css_set *cset;
a8a648c4 1706
1ada4838 1707 WARN_ON(!css || cgroup_css(dcgrp, ss));
a8a648c4 1708
334c3679
TH
1709 /* disable from the source */
1710 src_root->subsys_mask &= ~(1 << ssid);
1711 WARN_ON(cgroup_apply_control(scgrp));
1712 cgroup_finalize_control(scgrp, 0);
4df8dc90 1713
334c3679 1714 /* rebind */
1ada4838
TH
1715 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1716 rcu_assign_pointer(dcgrp->subsys[ssid], css);
5df36032 1717 ss->root = dst_root;
1ada4838 1718 css->cgroup = dcgrp;
73e80ed8 1719
82d6489d 1720 spin_lock_irq(&css_set_lock);
2d8f243a
TH
1721 hash_for_each(css_set_table, i, cset, hlist)
1722 list_move_tail(&cset->e_cset_node[ss->id],
1ada4838 1723 &dcgrp->e_csets[ss->id]);
82d6489d 1724 spin_unlock_irq(&css_set_lock);
2d8f243a 1725
bd53d617 1726 /* default hierarchy doesn't enable controllers by default */
f392e51c 1727 dst_root->subsys_mask |= 1 << ssid;
49d1dc4b
TH
1728 if (dst_root == &cgrp_dfl_root) {
1729 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1730 } else {
1ada4838 1731 dcgrp->subtree_control |= 1 << ssid;
49d1dc4b 1732 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
667c2491 1733 }
a8a648c4 1734
334c3679
TH
1735 ret = cgroup_apply_control(dcgrp);
1736 if (ret)
1737 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1738 ss->name, ret);
1739
5df36032
TH
1740 if (ss->bind)
1741 ss->bind(css);
b4e0eeaf 1742 } while_each_subsys_mask();
ddbcc7e8 1743
1ada4838 1744 kernfs_activate(dcgrp->kn);
ddbcc7e8
PM
1745 return 0;
1746}
1747
1592c9b2
TH
1748int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1749 struct kernfs_root *kf_root)
4f41fc59 1750{
09be4c82 1751 int len = 0;
4f41fc59
SH
1752 char *buf = NULL;
1753 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1754 struct cgroup *ns_cgroup;
1755
1756 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1757 if (!buf)
1758 return -ENOMEM;
1759
82d6489d 1760 spin_lock_irq(&css_set_lock);
4f41fc59
SH
1761 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1762 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
82d6489d 1763 spin_unlock_irq(&css_set_lock);
4f41fc59
SH
1764
1765 if (len >= PATH_MAX)
1766 len = -ERANGE;
1767 else if (len > 0) {
1768 seq_escape(sf, buf, " \t\n\\");
1769 len = 0;
1770 }
1771 kfree(buf);
1772 return len;
1773}
1774
5136f636
TH
1775static int parse_cgroup_root_flags(char *data, unsigned int *root_flags)
1776{
1777 char *token;
1778
1779 *root_flags = 0;
1780
e250d91d 1781 if (!data || *data == '\0')
5136f636
TH
1782 return 0;
1783
1784 while ((token = strsep(&data, ",")) != NULL) {
1785 if (!strcmp(token, "nsdelegate")) {
1786 *root_flags |= CGRP_ROOT_NS_DELEGATE;
1787 continue;
1788 }
1789
1790 pr_err("cgroup2: unknown option \"%s\"\n", token);
1791 return -EINVAL;
1792 }
1793
1794 return 0;
1795}
1796
1797static void apply_cgroup_root_flags(unsigned int root_flags)
1798{
1799 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
1800 if (root_flags & CGRP_ROOT_NS_DELEGATE)
1801 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
1802 else
1803 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
1804 }
1805}
1806
1807static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
1808{
1809 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
1810 seq_puts(seq, ",nsdelegate");
1811 return 0;
1812}
1813
2bd59d48 1814static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
ddbcc7e8 1815{
5136f636
TH
1816 unsigned int root_flags;
1817 int ret;
1818
1819 ret = parse_cgroup_root_flags(data, &root_flags);
1820 if (ret)
1821 return ret;
1822
1823 apply_cgroup_root_flags(root_flags);
1824 return 0;
ddbcc7e8
PM
1825}
1826
afeb0f9f
TH
1827/*
1828 * To reduce the fork() overhead for systems that are not actually using
1829 * their cgroups capability, we don't maintain the lists running through
1830 * each css_set to its tasks until we see the list actually used - in other
1831 * words after the first mount.
1832 */
1833static bool use_task_css_set_links __read_mostly;
1834
1835static void cgroup_enable_task_cg_lists(void)
1836{
1837 struct task_struct *p, *g;
1838
afeb0f9f
TH
1839 /*
1840 * We need tasklist_lock because RCU is not safe against
1841 * while_each_thread(). Besides, a forking task that has passed
1842 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1843 * is not guaranteed to have its child immediately visible in the
1844 * tasklist if we walk through it with RCU.
1845 */
1846 read_lock(&tasklist_lock);
d8742e22
TH
1847 spin_lock_irq(&css_set_lock);
1848
1849 if (use_task_css_set_links)
1850 goto out_unlock;
1851
1852 use_task_css_set_links = true;
1853
afeb0f9f 1854 do_each_thread(g, p) {
afeb0f9f
TH
1855 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1856 task_css_set(p) != &init_css_set);
1857
1858 /*
1859 * We should check if the process is exiting, otherwise
1860 * it will race with cgroup_exit() in that the list
1861 * entry won't be deleted though the process has exited.
f153ad11
TH
1862 * Do it while holding siglock so that we don't end up
1863 * racing against cgroup_exit().
82d6489d
DBO
1864 *
1865 * Interrupts were already disabled while acquiring
1866 * the css_set_lock, so we do not need to disable it
1867 * again when acquiring the sighand->siglock here.
afeb0f9f 1868 */
82d6489d 1869 spin_lock(&p->sighand->siglock);
eaf797ab
TH
1870 if (!(p->flags & PF_EXITING)) {
1871 struct css_set *cset = task_css_set(p);
1872
0de0942d
TH
1873 if (!css_set_populated(cset))
1874 css_set_update_populated(cset, true);
389b9c1b 1875 list_add_tail(&p->cg_list, &cset->tasks);
eaf797ab 1876 get_css_set(cset);
73a7242a 1877 cset->nr_tasks++;
eaf797ab 1878 }
82d6489d 1879 spin_unlock(&p->sighand->siglock);
afeb0f9f 1880 } while_each_thread(g, p);
afeb0f9f 1881out_unlock:
82d6489d 1882 spin_unlock_irq(&css_set_lock);
d8742e22 1883 read_unlock(&tasklist_lock);
afeb0f9f 1884}
ddbcc7e8 1885
cc31edce
PM
1886static void init_cgroup_housekeeping(struct cgroup *cgrp)
1887{
2d8f243a
TH
1888 struct cgroup_subsys *ss;
1889 int ssid;
1890
d5c419b6
TH
1891 INIT_LIST_HEAD(&cgrp->self.sibling);
1892 INIT_LIST_HEAD(&cgrp->self.children);
69d0206c 1893 INIT_LIST_HEAD(&cgrp->cset_links);
72a8cb30
BB
1894 INIT_LIST_HEAD(&cgrp->pidlists);
1895 mutex_init(&cgrp->pidlist_mutex);
9d800df1 1896 cgrp->self.cgroup = cgrp;
184faf32 1897 cgrp->self.flags |= CSS_ONLINE;
454000ad 1898 cgrp->dom_cgrp = cgrp;
1a926e0b
RG
1899 cgrp->max_descendants = INT_MAX;
1900 cgrp->max_depth = INT_MAX;
8f53470b 1901 INIT_LIST_HEAD(&cgrp->rstat_css_list);
d4ff749b 1902 prev_cputime_init(&cgrp->prev_cputime);
2d8f243a
TH
1903
1904 for_each_subsys(ss, ssid)
1905 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
f8f22e53
TH
1906
1907 init_waitqueue_head(&cgrp->offline_waitq);
d62beb7f 1908 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
cc31edce 1909}
c6d57f33 1910
1592c9b2 1911void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts)
ddbcc7e8 1912{
3dd06ffa 1913 struct cgroup *cgrp = &root->cgrp;
b0ca5a84 1914
ddbcc7e8 1915 INIT_LIST_HEAD(&root->root_list);
3c9c825b 1916 atomic_set(&root->nr_cgrps, 1);
bd89aabc 1917 cgrp->root = root;
cc31edce 1918 init_cgroup_housekeeping(cgrp);
4e96ee8e 1919 idr_init(&root->cgroup_idr);
c6d57f33 1920
c6d57f33
PM
1921 root->flags = opts->flags;
1922 if (opts->release_agent)
08a77676 1923 strscpy(root->release_agent_path, opts->release_agent, PATH_MAX);
c6d57f33 1924 if (opts->name)
08a77676 1925 strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN);
2260e7fc 1926 if (opts->cpuset_clone_children)
3dd06ffa 1927 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
c6d57f33
PM
1928}
1929
35ac1184 1930int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
2c6ab6d2 1931{
d427dfeb 1932 LIST_HEAD(tmp_links);
3dd06ffa 1933 struct cgroup *root_cgrp = &root->cgrp;
fa069904 1934 struct kernfs_syscall_ops *kf_sops;
d427dfeb 1935 struct css_set *cset;
d427dfeb 1936 int i, ret;
2c6ab6d2 1937
d427dfeb 1938 lockdep_assert_held(&cgroup_mutex);
c6d57f33 1939
cf780b7d 1940 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
d427dfeb 1941 if (ret < 0)
2bd59d48 1942 goto out;
d427dfeb 1943 root_cgrp->id = ret;
b11cfb58 1944 root_cgrp->ancestor_ids[0] = ret;
c6d57f33 1945
9732adc5 1946 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
35ac1184 1947 0, GFP_KERNEL);
9d755d33
TH
1948 if (ret)
1949 goto out;
1950
d427dfeb 1951 /*
f0d9a5f1 1952 * We're accessing css_set_count without locking css_set_lock here,
d427dfeb 1953 * but that's OK - it can only be increased by someone holding
04313591
TH
1954 * cgroup_lock, and that's us. Later rebinding may disable
1955 * controllers on the default hierarchy and thus create new csets,
1956 * which can't be more than the existing ones. Allocate 2x.
d427dfeb 1957 */
04313591 1958 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
d427dfeb 1959 if (ret)
9d755d33 1960 goto cancel_ref;
ddbcc7e8 1961
985ed670 1962 ret = cgroup_init_root_id(root);
ddbcc7e8 1963 if (ret)
9d755d33 1964 goto cancel_ref;
ddbcc7e8 1965
fa069904
TH
1966 kf_sops = root == &cgrp_dfl_root ?
1967 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1968
1969 root->kf_root = kernfs_create_root(kf_sops,
aa818825
SL
1970 KERNFS_ROOT_CREATE_DEACTIVATED |
1971 KERNFS_ROOT_SUPPORT_EXPORTOP,
2bd59d48
TH
1972 root_cgrp);
1973 if (IS_ERR(root->kf_root)) {
1974 ret = PTR_ERR(root->kf_root);
1975 goto exit_root_id;
1976 }
1977 root_cgrp->kn = root->kf_root->kn;
ddbcc7e8 1978
334c3679 1979 ret = css_populate_dir(&root_cgrp->self);
d427dfeb 1980 if (ret)
2bd59d48 1981 goto destroy_root;
ddbcc7e8 1982
5df36032 1983 ret = rebind_subsystems(root, ss_mask);
d427dfeb 1984 if (ret)
2bd59d48 1985 goto destroy_root;
ddbcc7e8 1986
324bda9e
AS
1987 ret = cgroup_bpf_inherit(root_cgrp);
1988 WARN_ON_ONCE(ret);
1989
ed1777de
TH
1990 trace_cgroup_setup_root(root);
1991
d427dfeb
TH
1992 /*
1993 * There must be no failure case after here, since rebinding takes
1994 * care of subsystems' refcounts, which are explicitly dropped in
1995 * the failure exit path.
1996 */
1997 list_add(&root->root_list, &cgroup_roots);
1998 cgroup_root_count++;
0df6a63f 1999
d427dfeb 2000 /*
3dd06ffa 2001 * Link the root cgroup in this hierarchy into all the css_set
d427dfeb
TH
2002 * objects.
2003 */
82d6489d 2004 spin_lock_irq(&css_set_lock);
0de0942d 2005 hash_for_each(css_set_table, i, cset, hlist) {
d427dfeb 2006 link_css_set(&tmp_links, cset, root_cgrp);
0de0942d
TH
2007 if (css_set_populated(cset))
2008 cgroup_update_populated(root_cgrp, true);
2009 }
82d6489d 2010 spin_unlock_irq(&css_set_lock);
ddbcc7e8 2011
d5c419b6 2012 BUG_ON(!list_empty(&root_cgrp->self.children));
3c9c825b 2013 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
ddbcc7e8 2014
2bd59d48 2015 kernfs_activate(root_cgrp->kn);
d427dfeb 2016 ret = 0;
2bd59d48 2017 goto out;
d427dfeb 2018
2bd59d48
TH
2019destroy_root:
2020 kernfs_destroy_root(root->kf_root);
2021 root->kf_root = NULL;
2022exit_root_id:
d427dfeb 2023 cgroup_exit_root_id(root);
9d755d33 2024cancel_ref:
9a1049da 2025 percpu_ref_exit(&root_cgrp->self.refcnt);
2bd59d48 2026out:
d427dfeb
TH
2027 free_cgrp_cset_links(&tmp_links);
2028 return ret;
ddbcc7e8
PM
2029}
2030
1592c9b2
TH
2031struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
2032 struct cgroup_root *root, unsigned long magic,
2033 struct cgroup_namespace *ns)
ddbcc7e8 2034{
2bd59d48 2035 struct dentry *dentry;
399504e2 2036 bool new_sb = false;
ddbcc7e8 2037
633feee3 2038 dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
ed82571b 2039
56fde9e0 2040 /*
633feee3
TH
2041 * In non-init cgroup namespace, instead of root cgroup's dentry,
2042 * we return the dentry corresponding to the cgroupns->root_cgrp.
56fde9e0 2043 */
633feee3
TH
2044 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
2045 struct dentry *nsdentry;
399504e2 2046 struct super_block *sb = dentry->d_sb;
633feee3 2047 struct cgroup *cgrp;
e37a06f1 2048
633feee3
TH
2049 mutex_lock(&cgroup_mutex);
2050 spin_lock_irq(&css_set_lock);
2051
2052 cgrp = cset_cgroup_from_root(ns->root_cset, root);
2053
2054 spin_unlock_irq(&css_set_lock);
2055 mutex_unlock(&cgroup_mutex);
2056
399504e2 2057 nsdentry = kernfs_node_dentry(cgrp->kn, sb);
633feee3 2058 dput(dentry);
399504e2
AV
2059 if (IS_ERR(nsdentry))
2060 deactivate_locked_super(sb);
633feee3 2061 dentry = nsdentry;
67e9c74b
TH
2062 }
2063
399504e2 2064 if (!new_sb)
633feee3
TH
2065 cgroup_put(&root->cgrp);
2066
2067 return dentry;
2068}
2069
f7e83571 2070static struct dentry *cgroup_mount(struct file_system_type *fs_type,
ddbcc7e8 2071 int flags, const char *unused_dev_name,
f7e83571 2072 void *data)
ddbcc7e8 2073{
ed82571b 2074 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
2bd59d48 2075 struct dentry *dentry;
5136f636 2076 int ret;
ddbcc7e8 2077
ed82571b
SH
2078 get_cgroup_ns(ns);
2079
2080 /* Check if the caller has permission to mount. */
2081 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
2082 put_cgroup_ns(ns);
2083 return ERR_PTR(-EPERM);
2084 }
2085
56fde9e0
TH
2086 /*
2087 * The first time anyone tries to mount a cgroup, enable the list
2088 * linking each css_set to its tasks and fix up all existing tasks.
2089 */
2090 if (!use_task_css_set_links)
2091 cgroup_enable_task_cg_lists();
e37a06f1 2092
633feee3 2093 if (fs_type == &cgroup2_fs_type) {
5136f636
TH
2094 unsigned int root_flags;
2095
2096 ret = parse_cgroup_root_flags(data, &root_flags);
2097 if (ret) {
ed82571b 2098 put_cgroup_ns(ns);
5136f636 2099 return ERR_PTR(ret);
67e9c74b 2100 }
5136f636 2101
a7165264 2102 cgrp_dfl_visible = true;
a590b90d 2103 cgroup_get_live(&cgrp_dfl_root.cgrp);
633feee3
TH
2104
2105 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
2106 CGROUP2_SUPER_MAGIC, ns);
5136f636
TH
2107 if (!IS_ERR(dentry))
2108 apply_cgroup_root_flags(root_flags);
633feee3
TH
2109 } else {
2110 dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
2111 CGROUP_SUPER_MAGIC, ns);
67e9c74b
TH
2112 }
2113
ed82571b 2114 put_cgroup_ns(ns);
2bd59d48
TH
2115 return dentry;
2116}
8e30e2b8 2117
2bd59d48
TH
2118static void cgroup_kill_sb(struct super_block *sb)
2119{
2120 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
3dd06ffa 2121 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
a015edd2 2122
970317aa 2123 /*
35ac1184 2124 * If @root doesn't have any children, start killing it.
9d755d33
TH
2125 * This prevents new mounts by disabling percpu_ref_tryget_live().
2126 * cgroup_mount() may wait for @root's release.
1f779fb2
LZ
2127 *
2128 * And don't kill the default root.
970317aa 2129 */
35ac1184
AV
2130 if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
2131 !percpu_ref_is_dying(&root->cgrp.self.refcnt))
9d755d33 2132 percpu_ref_kill(&root->cgrp.self.refcnt);
35ac1184 2133 cgroup_put(&root->cgrp);
2bd59d48 2134 kernfs_kill_sb(sb);
ddbcc7e8 2135}
970317aa 2136
0a268dbd 2137struct file_system_type cgroup_fs_type = {
ddbcc7e8 2138 .name = "cgroup",
f7e83571 2139 .mount = cgroup_mount,
ddbcc7e8 2140 .kill_sb = cgroup_kill_sb,
1c53753e 2141 .fs_flags = FS_USERNS_MOUNT,
ddbcc7e8 2142};
3126121f 2143
67e9c74b
TH
2144static struct file_system_type cgroup2_fs_type = {
2145 .name = "cgroup2",
2146 .mount = cgroup_mount,
2147 .kill_sb = cgroup_kill_sb,
1c53753e 2148 .fs_flags = FS_USERNS_MOUNT,
67e9c74b 2149};
3126121f 2150
0a268dbd
TH
2151int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2152 struct cgroup_namespace *ns)
a79a908f
AK
2153{
2154 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
a79a908f 2155
4c737b41 2156 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
a79a908f
AK
2157}
2158
4c737b41
TH
2159int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2160 struct cgroup_namespace *ns)
a79a908f 2161{
4c737b41 2162 int ret;
a79a908f
AK
2163
2164 mutex_lock(&cgroup_mutex);
82d6489d 2165 spin_lock_irq(&css_set_lock);
a79a908f
AK
2166
2167 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2168
82d6489d 2169 spin_unlock_irq(&css_set_lock);
a79a908f
AK
2170 mutex_unlock(&cgroup_mutex);
2171
2172 return ret;
2173}
2174EXPORT_SYMBOL_GPL(cgroup_path_ns);
2175
857a2beb 2176/**
913ffdb5 2177 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
857a2beb 2178 * @task: target task
857a2beb
TH
2179 * @buf: the buffer to write the path into
2180 * @buflen: the length of the buffer
2181 *
913ffdb5
TH
2182 * Determine @task's cgroup on the first (the one with the lowest non-zero
2183 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2184 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2185 * cgroup controller callbacks.
2186 *
e61734c5 2187 * Return value is the same as kernfs_path().
857a2beb 2188 */
4c737b41 2189int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
857a2beb 2190{
3dd06ffa 2191 struct cgroup_root *root;
913ffdb5 2192 struct cgroup *cgrp;
e61734c5 2193 int hierarchy_id = 1;
4c737b41 2194 int ret;
857a2beb
TH
2195
2196 mutex_lock(&cgroup_mutex);
82d6489d 2197 spin_lock_irq(&css_set_lock);
857a2beb 2198
913ffdb5
TH
2199 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2200
857a2beb
TH
2201 if (root) {
2202 cgrp = task_cgroup_from_root(task, root);
4c737b41 2203 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
913ffdb5
TH
2204 } else {
2205 /* if no hierarchy exists, everyone is in "/" */
4c737b41 2206 ret = strlcpy(buf, "/", buflen);
857a2beb
TH
2207 }
2208
82d6489d 2209 spin_unlock_irq(&css_set_lock);
857a2beb 2210 mutex_unlock(&cgroup_mutex);
4c737b41 2211 return ret;
857a2beb 2212}
913ffdb5 2213EXPORT_SYMBOL_GPL(task_cgroup_path);
857a2beb 2214
adaae5dc 2215/**
e595cd70 2216 * cgroup_migrate_add_task - add a migration target task to a migration context
adaae5dc 2217 * @task: target task
e595cd70 2218 * @mgctx: target migration context
adaae5dc 2219 *
e595cd70
TH
2220 * Add @task, which is a migration target, to @mgctx->tset. This function
2221 * becomes noop if @task doesn't need to be migrated. @task's css_set
2222 * should have been added as a migration source and @task->cg_list will be
2223 * moved from the css_set's tasks list to mg_tasks one.
adaae5dc 2224 */
e595cd70
TH
2225static void cgroup_migrate_add_task(struct task_struct *task,
2226 struct cgroup_mgctx *mgctx)
adaae5dc
TH
2227{
2228 struct css_set *cset;
2229
f0d9a5f1 2230 lockdep_assert_held(&css_set_lock);
adaae5dc
TH
2231
2232 /* @task either already exited or can't exit until the end */
2233 if (task->flags & PF_EXITING)
2234 return;
2235
2236 /* leave @task alone if post_fork() hasn't linked it yet */
2237 if (list_empty(&task->cg_list))
2238 return;
2239
2240 cset = task_css_set(task);
2241 if (!cset->mg_src_cgrp)
2242 return;
2243
61046727
TH
2244 mgctx->tset.nr_tasks++;
2245
adaae5dc
TH
2246 list_move_tail(&task->cg_list, &cset->mg_tasks);
2247 if (list_empty(&cset->mg_node))
e595cd70
TH
2248 list_add_tail(&cset->mg_node,
2249 &mgctx->tset.src_csets);
adaae5dc 2250 if (list_empty(&cset->mg_dst_cset->mg_node))
d8ebf519 2251 list_add_tail(&cset->mg_dst_cset->mg_node,
e595cd70 2252 &mgctx->tset.dst_csets);
adaae5dc
TH
2253}
2254
2f7ee569
TH
2255/**
2256 * cgroup_taskset_first - reset taskset and return the first task
2257 * @tset: taskset of interest
1f7dd3e5 2258 * @dst_cssp: output variable for the destination css
2f7ee569
TH
2259 *
2260 * @tset iteration is initialized and the first task is returned.
2261 */
1f7dd3e5
TH
2262struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2263 struct cgroup_subsys_state **dst_cssp)
2f7ee569 2264{
b3dc094e
TH
2265 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2266 tset->cur_task = NULL;
2267
1f7dd3e5 2268 return cgroup_taskset_next(tset, dst_cssp);
2f7ee569 2269}
2f7ee569
TH
2270
2271/**
2272 * cgroup_taskset_next - iterate to the next task in taskset
2273 * @tset: taskset of interest
1f7dd3e5 2274 * @dst_cssp: output variable for the destination css
2f7ee569
TH
2275 *
2276 * Return the next task in @tset. Iteration must have been initialized
2277 * with cgroup_taskset_first().
2278 */
1f7dd3e5
TH
2279struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2280 struct cgroup_subsys_state **dst_cssp)
2f7ee569 2281{
b3dc094e
TH
2282 struct css_set *cset = tset->cur_cset;
2283 struct task_struct *task = tset->cur_task;
2f7ee569 2284
b3dc094e
TH
2285 while (&cset->mg_node != tset->csets) {
2286 if (!task)
2287 task = list_first_entry(&cset->mg_tasks,
2288 struct task_struct, cg_list);
2289 else
2290 task = list_next_entry(task, cg_list);
2f7ee569 2291
b3dc094e
TH
2292 if (&task->cg_list != &cset->mg_tasks) {
2293 tset->cur_cset = cset;
2294 tset->cur_task = task;
1f7dd3e5
TH
2295
2296 /*
2297 * This function may be called both before and
2298 * after cgroup_taskset_migrate(). The two cases
2299 * can be distinguished by looking at whether @cset
2300 * has its ->mg_dst_cset set.
2301 */
2302 if (cset->mg_dst_cset)
2303 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2304 else
2305 *dst_cssp = cset->subsys[tset->ssid];
2306
b3dc094e
TH
2307 return task;
2308 }
2f7ee569 2309
b3dc094e
TH
2310 cset = list_next_entry(cset, mg_node);
2311 task = NULL;
2312 }
2f7ee569 2313
b3dc094e 2314 return NULL;
2f7ee569 2315}
2f7ee569 2316
adaae5dc 2317/**
37ff9f8f 2318 * cgroup_taskset_migrate - migrate a taskset
e595cd70 2319 * @mgctx: migration context
adaae5dc 2320 *
e595cd70 2321 * Migrate tasks in @mgctx as setup by migration preparation functions.
37ff9f8f 2322 * This function fails iff one of the ->can_attach callbacks fails and
e595cd70
TH
2323 * guarantees that either all or none of the tasks in @mgctx are migrated.
2324 * @mgctx is consumed regardless of success.
adaae5dc 2325 */
bfc2cf6f 2326static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
adaae5dc 2327{
e595cd70 2328 struct cgroup_taskset *tset = &mgctx->tset;
37ff9f8f 2329 struct cgroup_subsys *ss;
adaae5dc
TH
2330 struct task_struct *task, *tmp_task;
2331 struct css_set *cset, *tmp_cset;
37ff9f8f 2332 int ssid, failed_ssid, ret;
adaae5dc 2333
adaae5dc 2334 /* check that we can legitimately attach to the cgroup */
61046727
TH
2335 if (tset->nr_tasks) {
2336 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2337 if (ss->can_attach) {
2338 tset->ssid = ssid;
2339 ret = ss->can_attach(tset);
2340 if (ret) {
2341 failed_ssid = ssid;
2342 goto out_cancel_attach;
2343 }
adaae5dc 2344 }
61046727
TH
2345 } while_each_subsys_mask();
2346 }
adaae5dc
TH
2347
2348 /*
2349 * Now that we're guaranteed success, proceed to move all tasks to
2350 * the new cgroup. There are no failure cases after here, so this
2351 * is the commit point.
2352 */
82d6489d 2353 spin_lock_irq(&css_set_lock);
adaae5dc 2354 list_for_each_entry(cset, &tset->src_csets, mg_node) {
f6d7d049
TH
2355 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2356 struct css_set *from_cset = task_css_set(task);
2357 struct css_set *to_cset = cset->mg_dst_cset;
2358
2359 get_css_set(to_cset);
73a7242a 2360 to_cset->nr_tasks++;
f6d7d049
TH
2361 css_set_move_task(task, from_cset, to_cset, true);
2362 put_css_set_locked(from_cset);
73a7242a 2363 from_cset->nr_tasks--;
f6d7d049 2364 }
adaae5dc 2365 }
82d6489d 2366 spin_unlock_irq(&css_set_lock);
adaae5dc
TH
2367
2368 /*
2369 * Migration is committed, all target tasks are now on dst_csets.
2370 * Nothing is sensitive to fork() after this point. Notify
2371 * controllers that migration is complete.
2372 */
2373 tset->csets = &tset->dst_csets;
2374
61046727
TH
2375 if (tset->nr_tasks) {
2376 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2377 if (ss->attach) {
2378 tset->ssid = ssid;
2379 ss->attach(tset);
2380 }
2381 } while_each_subsys_mask();
2382 }
adaae5dc
TH
2383
2384 ret = 0;
2385 goto out_release_tset;
2386
2387out_cancel_attach:
61046727
TH
2388 if (tset->nr_tasks) {
2389 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2390 if (ssid == failed_ssid)
2391 break;
2392 if (ss->cancel_attach) {
2393 tset->ssid = ssid;
2394 ss->cancel_attach(tset);
2395 }
2396 } while_each_subsys_mask();
2397 }
adaae5dc 2398out_release_tset:
82d6489d 2399 spin_lock_irq(&css_set_lock);
adaae5dc
TH
2400 list_splice_init(&tset->dst_csets, &tset->src_csets);
2401 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2402 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2403 list_del_init(&cset->mg_node);
2404 }
82d6489d 2405 spin_unlock_irq(&css_set_lock);
c4fa6c43
WL
2406
2407 /*
2408 * Re-initialize the cgroup_taskset structure in case it is reused
2409 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2410 * iteration.
2411 */
2412 tset->nr_tasks = 0;
2413 tset->csets = &tset->src_csets;
adaae5dc
TH
2414 return ret;
2415}
2416
6c694c88 2417/**
8cfd8147 2418 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
6c694c88
TH
2419 * @dst_cgrp: destination cgroup to test
2420 *
8cfd8147
TH
2421 * On the default hierarchy, except for the mixable, (possible) thread root
2422 * and threaded cgroups, subtree_control must be zero for migration
2423 * destination cgroups with tasks so that child cgroups don't compete
2424 * against tasks.
6c694c88 2425 */
8cfd8147 2426int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
6c694c88 2427{
8cfd8147
TH
2428 /* v1 doesn't have any restriction */
2429 if (!cgroup_on_dfl(dst_cgrp))
2430 return 0;
2431
2432 /* verify @dst_cgrp can host resources */
2433 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
2434 return -EOPNOTSUPP;
2435
2436 /* mixables don't care */
2437 if (cgroup_is_mixable(dst_cgrp))
2438 return 0;
2439
2440 /*
2441 * If @dst_cgrp is already or can become a thread root or is
2442 * threaded, it doesn't matter.
2443 */
2444 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
2445 return 0;
2446
2447 /* apply no-internal-process constraint */
2448 if (dst_cgrp->subtree_control)
2449 return -EBUSY;
2450
2451 return 0;
6c694c88
TH
2452}
2453
a043e3b2 2454/**
1958d2d5 2455 * cgroup_migrate_finish - cleanup after attach
e595cd70 2456 * @mgctx: migration context
74a1166d 2457 *
1958d2d5
TH
2458 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2459 * those functions for details.
74a1166d 2460 */
e595cd70 2461void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
74a1166d 2462{
e595cd70 2463 LIST_HEAD(preloaded);
1958d2d5 2464 struct css_set *cset, *tmp_cset;
74a1166d 2465
1958d2d5
TH
2466 lockdep_assert_held(&cgroup_mutex);
2467
82d6489d 2468 spin_lock_irq(&css_set_lock);
e595cd70
TH
2469
2470 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2471 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2472
2473 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
1958d2d5 2474 cset->mg_src_cgrp = NULL;
e4857982 2475 cset->mg_dst_cgrp = NULL;
1958d2d5
TH
2476 cset->mg_dst_cset = NULL;
2477 list_del_init(&cset->mg_preload_node);
a25eb52e 2478 put_css_set_locked(cset);
1958d2d5 2479 }
e595cd70 2480
82d6489d 2481 spin_unlock_irq(&css_set_lock);
1958d2d5
TH
2482}
2483
2484/**
2485 * cgroup_migrate_add_src - add a migration source css_set
2486 * @src_cset: the source css_set to add
2487 * @dst_cgrp: the destination cgroup
e595cd70 2488 * @mgctx: migration context
1958d2d5
TH
2489 *
2490 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
e595cd70 2491 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
1958d2d5
TH
2492 * up by cgroup_migrate_finish().
2493 *
1ed13287
TH
2494 * This function may be called without holding cgroup_threadgroup_rwsem
2495 * even if the target is a process. Threads may be created and destroyed
2496 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2497 * into play and the preloaded css_sets are guaranteed to cover all
2498 * migrations.
1958d2d5 2499 */
0a268dbd
TH
2500void cgroup_migrate_add_src(struct css_set *src_cset,
2501 struct cgroup *dst_cgrp,
e595cd70 2502 struct cgroup_mgctx *mgctx)
1958d2d5
TH
2503{
2504 struct cgroup *src_cgrp;
2505
2506 lockdep_assert_held(&cgroup_mutex);
f0d9a5f1 2507 lockdep_assert_held(&css_set_lock);
1958d2d5 2508
2b021cbf
TH
2509 /*
2510 * If ->dead, @src_set is associated with one or more dead cgroups
2511 * and doesn't contain any migratable tasks. Ignore it early so
2512 * that the rest of migration path doesn't get confused by it.
2513 */
2514 if (src_cset->dead)
2515 return;
2516
1958d2d5
TH
2517 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2518
1958d2d5
TH
2519 if (!list_empty(&src_cset->mg_preload_node))
2520 return;
2521
2522 WARN_ON(src_cset->mg_src_cgrp);
e4857982 2523 WARN_ON(src_cset->mg_dst_cgrp);
1958d2d5
TH
2524 WARN_ON(!list_empty(&src_cset->mg_tasks));
2525 WARN_ON(!list_empty(&src_cset->mg_node));
2526
2527 src_cset->mg_src_cgrp = src_cgrp;
e4857982 2528 src_cset->mg_dst_cgrp = dst_cgrp;
1958d2d5 2529 get_css_set(src_cset);
e595cd70 2530 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
1958d2d5
TH
2531}
2532
2533/**
2534 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
e595cd70 2535 * @mgctx: migration context
1958d2d5 2536 *
e4857982 2537 * Tasks are about to be moved and all the source css_sets have been
e595cd70
TH
2538 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2539 * pins all destination css_sets, links each to its source, and append them
2540 * to @mgctx->preloaded_dst_csets.
1958d2d5
TH
2541 *
2542 * This function must be called after cgroup_migrate_add_src() has been
2543 * called on each migration source css_set. After migration is performed
2544 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
e595cd70 2545 * @mgctx.
1958d2d5 2546 */
e595cd70 2547int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
1958d2d5 2548{
f817de98 2549 struct css_set *src_cset, *tmp_cset;
1958d2d5
TH
2550
2551 lockdep_assert_held(&cgroup_mutex);
2552
2553 /* look up the dst cset for each src cset and link it to src */
e595cd70
TH
2554 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2555 mg_preload_node) {
1958d2d5 2556 struct css_set *dst_cset;
bfc2cf6f
TH
2557 struct cgroup_subsys *ss;
2558 int ssid;
1958d2d5 2559
e4857982 2560 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
1958d2d5
TH
2561 if (!dst_cset)
2562 goto err;
2563
2564 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
f817de98
TH
2565
2566 /*
2567 * If src cset equals dst, it's noop. Drop the src.
2568 * cgroup_migrate() will skip the cset too. Note that we
2569 * can't handle src == dst as some nodes are used by both.
2570 */
2571 if (src_cset == dst_cset) {
2572 src_cset->mg_src_cgrp = NULL;
e4857982 2573 src_cset->mg_dst_cgrp = NULL;
f817de98 2574 list_del_init(&src_cset->mg_preload_node);
a25eb52e
ZL
2575 put_css_set(src_cset);
2576 put_css_set(dst_cset);
f817de98
TH
2577 continue;
2578 }
2579
1958d2d5
TH
2580 src_cset->mg_dst_cset = dst_cset;
2581
2582 if (list_empty(&dst_cset->mg_preload_node))
e595cd70
TH
2583 list_add_tail(&dst_cset->mg_preload_node,
2584 &mgctx->preloaded_dst_csets);
1958d2d5 2585 else
a25eb52e 2586 put_css_set(dst_cset);
bfc2cf6f
TH
2587
2588 for_each_subsys(ss, ssid)
2589 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2590 mgctx->ss_mask |= 1 << ssid;
1958d2d5
TH
2591 }
2592
1958d2d5
TH
2593 return 0;
2594err:
e595cd70 2595 cgroup_migrate_finish(mgctx);
1958d2d5
TH
2596 return -ENOMEM;
2597}
2598
2599/**
2600 * cgroup_migrate - migrate a process or task to a cgroup
1958d2d5
TH
2601 * @leader: the leader of the process or the task to migrate
2602 * @threadgroup: whether @leader points to the whole process or a single task
e595cd70 2603 * @mgctx: migration context
1958d2d5 2604 *
37ff9f8f
TH
2605 * Migrate a process or task denoted by @leader. If migrating a process,
2606 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2607 * responsible for invoking cgroup_migrate_add_src() and
1958d2d5
TH
2608 * cgroup_migrate_prepare_dst() on the targets before invoking this
2609 * function and following up with cgroup_migrate_finish().
2610 *
2611 * As long as a controller's ->can_attach() doesn't fail, this function is
2612 * guaranteed to succeed. This means that, excluding ->can_attach()
2613 * failure, when migrating multiple targets, the success or failure can be
2614 * decided for all targets by invoking group_migrate_prepare_dst() before
2615 * actually starting migrating.
2616 */
0a268dbd 2617int cgroup_migrate(struct task_struct *leader, bool threadgroup,
bfc2cf6f 2618 struct cgroup_mgctx *mgctx)
74a1166d 2619{
adaae5dc 2620 struct task_struct *task;
74a1166d 2621
fb5d2b4c
MSB
2622 /*
2623 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2624 * already PF_EXITING could be freed from underneath us unless we
2625 * take an rcu_read_lock.
2626 */
82d6489d 2627 spin_lock_irq(&css_set_lock);
fb5d2b4c 2628 rcu_read_lock();
9db8de37 2629 task = leader;
74a1166d 2630 do {
e595cd70 2631 cgroup_migrate_add_task(task, mgctx);
081aa458
LZ
2632 if (!threadgroup)
2633 break;
9db8de37 2634 } while_each_thread(leader, task);
fb5d2b4c 2635 rcu_read_unlock();
82d6489d 2636 spin_unlock_irq(&css_set_lock);
74a1166d 2637
bfc2cf6f 2638 return cgroup_migrate_execute(mgctx);
74a1166d
BB
2639}
2640
1958d2d5
TH
2641/**
2642 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2643 * @dst_cgrp: the cgroup to attach to
2644 * @leader: the task or the leader of the threadgroup to be attached
2645 * @threadgroup: attach the whole threadgroup?
2646 *
1ed13287 2647 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
1958d2d5 2648 */
0a268dbd
TH
2649int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2650 bool threadgroup)
1958d2d5 2651{
e595cd70 2652 DEFINE_CGROUP_MGCTX(mgctx);
1958d2d5
TH
2653 struct task_struct *task;
2654 int ret;
2655
8cfd8147
TH
2656 ret = cgroup_migrate_vet_dst(dst_cgrp);
2657 if (ret)
2658 return ret;
6c694c88 2659
1958d2d5 2660 /* look up all src csets */
82d6489d 2661 spin_lock_irq(&css_set_lock);
1958d2d5
TH
2662 rcu_read_lock();
2663 task = leader;
2664 do {
e595cd70 2665 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
1958d2d5
TH
2666 if (!threadgroup)
2667 break;
2668 } while_each_thread(leader, task);
2669 rcu_read_unlock();
82d6489d 2670 spin_unlock_irq(&css_set_lock);
1958d2d5
TH
2671
2672 /* prepare dst csets and commit */
e595cd70 2673 ret = cgroup_migrate_prepare_dst(&mgctx);
1958d2d5 2674 if (!ret)
bfc2cf6f 2675 ret = cgroup_migrate(leader, threadgroup, &mgctx);
1958d2d5 2676
e595cd70 2677 cgroup_migrate_finish(&mgctx);
ed1777de
TH
2678
2679 if (!ret)
e4f8d81c 2680 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
ed1777de 2681
1958d2d5 2682 return ret;
74a1166d
BB
2683}
2684
715c809d
TH
2685struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
2686 __acquires(&cgroup_threadgroup_rwsem)
bbcb81d0 2687{
bbcb81d0 2688 struct task_struct *tsk;
acbef755 2689 pid_t pid;
bbcb81d0 2690
acbef755 2691 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
715c809d 2692 return ERR_PTR(-EINVAL);
74a1166d 2693
3014dde7 2694 percpu_down_write(&cgroup_threadgroup_rwsem);
715c809d 2695
b78949eb 2696 rcu_read_lock();
bbcb81d0 2697 if (pid) {
73507f33 2698 tsk = find_task_by_vpid(pid);
74a1166d 2699 if (!tsk) {
715c809d
TH
2700 tsk = ERR_PTR(-ESRCH);
2701 goto out_unlock_threadgroup;
bbcb81d0 2702 }
dedf22e9 2703 } else {
b78949eb 2704 tsk = current;
dedf22e9 2705 }
cd3d0952
TH
2706
2707 if (threadgroup)
b78949eb 2708 tsk = tsk->group_leader;
c4c27fbd
MG
2709
2710 /*
77f88796
TH
2711 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2712 * If userland migrates such a kthread to a non-root cgroup, it can
2713 * become trapped in a cpuset, or RT kthread may be born in a
2714 * cgroup with no rt_runtime allocated. Just say no.
c4c27fbd 2715 */
77f88796 2716 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
715c809d
TH
2717 tsk = ERR_PTR(-EINVAL);
2718 goto out_unlock_threadgroup;
c4c27fbd
MG
2719 }
2720
b78949eb 2721 get_task_struct(tsk);
715c809d
TH
2722 goto out_unlock_rcu;
2723
2724out_unlock_threadgroup:
2725 percpu_up_write(&cgroup_threadgroup_rwsem);
2726out_unlock_rcu:
b78949eb 2727 rcu_read_unlock();
715c809d
TH
2728 return tsk;
2729}
b78949eb 2730
715c809d
TH
2731void cgroup_procs_write_finish(struct task_struct *task)
2732 __releases(&cgroup_threadgroup_rwsem)
2733{
2734 struct cgroup_subsys *ss;
2735 int ssid;
081aa458 2736
715c809d
TH
2737 /* release reference from cgroup_procs_write_start() */
2738 put_task_struct(task);
3014dde7 2739
3014dde7 2740 percpu_up_write(&cgroup_threadgroup_rwsem);
5cf1cacb
TH
2741 for_each_subsys(ss, ssid)
2742 if (ss->post_attach)
2743 ss->post_attach();
af351026
PM
2744}
2745
6e5c8307 2746static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
355e0c48 2747{
f8f22e53
TH
2748 struct cgroup_subsys *ss;
2749 bool printed = false;
2750 int ssid;
a742c59d 2751
b4e0eeaf 2752 do_each_subsys_mask(ss, ssid, ss_mask) {
a966a4ed
AS
2753 if (printed)
2754 seq_putc(seq, ' ');
2755 seq_printf(seq, "%s", ss->name);
2756 printed = true;
b4e0eeaf 2757 } while_each_subsys_mask();
f8f22e53
TH
2758 if (printed)
2759 seq_putc(seq, '\n');
355e0c48
PM
2760}
2761
f8f22e53
TH
2762/* show controllers which are enabled from the parent */
2763static int cgroup_controllers_show(struct seq_file *seq, void *v)
ddbcc7e8 2764{
f8f22e53
TH
2765 struct cgroup *cgrp = seq_css(seq)->cgroup;
2766
5531dc91 2767 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
f8f22e53 2768 return 0;
ddbcc7e8
PM
2769}
2770
f8f22e53
TH
2771/* show controllers which are enabled for a given cgroup's children */
2772static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
ddbcc7e8 2773{
f8f22e53
TH
2774 struct cgroup *cgrp = seq_css(seq)->cgroup;
2775
667c2491 2776 cgroup_print_ss_mask(seq, cgrp->subtree_control);
f8f22e53
TH
2777 return 0;
2778}
2779
2780/**
2781 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2782 * @cgrp: root of the subtree to update csses for
2783 *
54962604
TH
2784 * @cgrp's control masks have changed and its subtree's css associations
2785 * need to be updated accordingly. This function looks up all css_sets
2786 * which are attached to the subtree, creates the matching updated css_sets
2787 * and migrates the tasks to the new ones.
f8f22e53
TH
2788 */
2789static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2790{
e595cd70 2791 DEFINE_CGROUP_MGCTX(mgctx);
54962604
TH
2792 struct cgroup_subsys_state *d_css;
2793 struct cgroup *dsct;
f8f22e53
TH
2794 struct css_set *src_cset;
2795 int ret;
2796
f8f22e53
TH
2797 lockdep_assert_held(&cgroup_mutex);
2798
3014dde7
TH
2799 percpu_down_write(&cgroup_threadgroup_rwsem);
2800
f8f22e53 2801 /* look up all csses currently attached to @cgrp's subtree */
82d6489d 2802 spin_lock_irq(&css_set_lock);
54962604 2803 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
f8f22e53
TH
2804 struct cgrp_cset_link *link;
2805
54962604 2806 list_for_each_entry(link, &dsct->cset_links, cset_link)
e595cd70 2807 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
f8f22e53 2808 }
82d6489d 2809 spin_unlock_irq(&css_set_lock);
f8f22e53
TH
2810
2811 /* NULL dst indicates self on default hierarchy */
e595cd70 2812 ret = cgroup_migrate_prepare_dst(&mgctx);
f8f22e53
TH
2813 if (ret)
2814 goto out_finish;
2815
82d6489d 2816 spin_lock_irq(&css_set_lock);
e595cd70 2817 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
10265075 2818 struct task_struct *task, *ntask;
f8f22e53 2819
10265075
TH
2820 /* all tasks in src_csets need to be migrated */
2821 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
e595cd70 2822 cgroup_migrate_add_task(task, &mgctx);
f8f22e53 2823 }
82d6489d 2824 spin_unlock_irq(&css_set_lock);
f8f22e53 2825
bfc2cf6f 2826 ret = cgroup_migrate_execute(&mgctx);
f8f22e53 2827out_finish:
e595cd70 2828 cgroup_migrate_finish(&mgctx);
3014dde7 2829 percpu_up_write(&cgroup_threadgroup_rwsem);
f8f22e53
TH
2830 return ret;
2831}
2832
1b9b96a1 2833/**
945ba199 2834 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
ce3f1d9d 2835 * @cgrp: root of the target subtree
1b9b96a1
TH
2836 *
2837 * Because css offlining is asynchronous, userland may try to re-enable a
945ba199
TH
2838 * controller while the previous css is still around. This function grabs
2839 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
1b9b96a1 2840 */
0a268dbd 2841void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
945ba199 2842 __acquires(&cgroup_mutex)
1b9b96a1
TH
2843{
2844 struct cgroup *dsct;
ce3f1d9d 2845 struct cgroup_subsys_state *d_css;
1b9b96a1
TH
2846 struct cgroup_subsys *ss;
2847 int ssid;
2848
945ba199
TH
2849restart:
2850 mutex_lock(&cgroup_mutex);
1b9b96a1 2851
ce3f1d9d 2852 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
1b9b96a1
TH
2853 for_each_subsys(ss, ssid) {
2854 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2855 DEFINE_WAIT(wait);
2856
ce3f1d9d 2857 if (!css || !percpu_ref_is_dying(&css->refcnt))
1b9b96a1
TH
2858 continue;
2859
a590b90d 2860 cgroup_get_live(dsct);
1b9b96a1
TH
2861 prepare_to_wait(&dsct->offline_waitq, &wait,
2862 TASK_UNINTERRUPTIBLE);
2863
2864 mutex_unlock(&cgroup_mutex);
2865 schedule();
2866 finish_wait(&dsct->offline_waitq, &wait);
1b9b96a1
TH
2867
2868 cgroup_put(dsct);
945ba199 2869 goto restart;
1b9b96a1
TH
2870 }
2871 }
1b9b96a1
TH
2872}
2873
15a27c36 2874/**
479adb89 2875 * cgroup_save_control - save control masks and dom_cgrp of a subtree
15a27c36
TH
2876 * @cgrp: root of the target subtree
2877 *
479adb89
TH
2878 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
2879 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2880 * itself.
15a27c36
TH
2881 */
2882static void cgroup_save_control(struct cgroup *cgrp)
2883{
2884 struct cgroup *dsct;
2885 struct cgroup_subsys_state *d_css;
2886
2887 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2888 dsct->old_subtree_control = dsct->subtree_control;
2889 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
479adb89 2890 dsct->old_dom_cgrp = dsct->dom_cgrp;
15a27c36
TH
2891 }
2892}
2893
2894/**
2895 * cgroup_propagate_control - refresh control masks of a subtree
2896 * @cgrp: root of the target subtree
2897 *
2898 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2899 * ->subtree_control and propagate controller availability through the
2900 * subtree so that descendants don't have unavailable controllers enabled.
2901 */
2902static void cgroup_propagate_control(struct cgroup *cgrp)
2903{
2904 struct cgroup *dsct;
2905 struct cgroup_subsys_state *d_css;
2906
2907 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2908 dsct->subtree_control &= cgroup_control(dsct);
5ced2518
TH
2909 dsct->subtree_ss_mask =
2910 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2911 cgroup_ss_mask(dsct));
15a27c36
TH
2912 }
2913}
2914
2915/**
479adb89 2916 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
15a27c36
TH
2917 * @cgrp: root of the target subtree
2918 *
479adb89
TH
2919 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
2920 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2921 * itself.
15a27c36
TH
2922 */
2923static void cgroup_restore_control(struct cgroup *cgrp)
2924{
2925 struct cgroup *dsct;
2926 struct cgroup_subsys_state *d_css;
2927
2928 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2929 dsct->subtree_control = dsct->old_subtree_control;
2930 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
479adb89 2931 dsct->dom_cgrp = dsct->old_dom_cgrp;
15a27c36
TH
2932 }
2933}
2934
f6d635ad
TH
2935static bool css_visible(struct cgroup_subsys_state *css)
2936{
2937 struct cgroup_subsys *ss = css->ss;
2938 struct cgroup *cgrp = css->cgroup;
2939
2940 if (cgroup_control(cgrp) & (1 << ss->id))
2941 return true;
2942 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2943 return false;
2944 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2945}
2946
bdb53bd7
TH
2947/**
2948 * cgroup_apply_control_enable - enable or show csses according to control
ce3f1d9d 2949 * @cgrp: root of the target subtree
bdb53bd7 2950 *
ce3f1d9d 2951 * Walk @cgrp's subtree and create new csses or make the existing ones
bdb53bd7
TH
2952 * visible. A css is created invisible if it's being implicitly enabled
2953 * through dependency. An invisible css is made visible when the userland
2954 * explicitly enables it.
2955 *
2956 * Returns 0 on success, -errno on failure. On failure, csses which have
2957 * been processed already aren't cleaned up. The caller is responsible for
8a1115ff 2958 * cleaning up with cgroup_apply_control_disable().
bdb53bd7
TH
2959 */
2960static int cgroup_apply_control_enable(struct cgroup *cgrp)
2961{
2962 struct cgroup *dsct;
ce3f1d9d 2963 struct cgroup_subsys_state *d_css;
bdb53bd7
TH
2964 struct cgroup_subsys *ss;
2965 int ssid, ret;
2966
ce3f1d9d 2967 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
bdb53bd7
TH
2968 for_each_subsys(ss, ssid) {
2969 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2970
945ba199
TH
2971 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2972
bdb53bd7
TH
2973 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
2974 continue;
2975
2976 if (!css) {
2977 css = css_create(dsct, ss);
2978 if (IS_ERR(css))
2979 return PTR_ERR(css);
2980 }
2981
f6d635ad 2982 if (css_visible(css)) {
334c3679 2983 ret = css_populate_dir(css);
bdb53bd7
TH
2984 if (ret)
2985 return ret;
2986 }
2987 }
2988 }
2989
2990 return 0;
2991}
2992
12b3bb6a
TH
2993/**
2994 * cgroup_apply_control_disable - kill or hide csses according to control
ce3f1d9d 2995 * @cgrp: root of the target subtree
12b3bb6a 2996 *
ce3f1d9d 2997 * Walk @cgrp's subtree and kill and hide csses so that they match
12b3bb6a
TH
2998 * cgroup_ss_mask() and cgroup_visible_mask().
2999 *
3000 * A css is hidden when the userland requests it to be disabled while other
3001 * subsystems are still depending on it. The css must not actively control
3002 * resources and be in the vanilla state if it's made visible again later.
3003 * Controllers which may be depended upon should provide ->css_reset() for
3004 * this purpose.
3005 */
3006static void cgroup_apply_control_disable(struct cgroup *cgrp)
3007{
3008 struct cgroup *dsct;
ce3f1d9d 3009 struct cgroup_subsys_state *d_css;
12b3bb6a
TH
3010 struct cgroup_subsys *ss;
3011 int ssid;
3012
ce3f1d9d 3013 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
12b3bb6a
TH
3014 for_each_subsys(ss, ssid) {
3015 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3016
945ba199
TH
3017 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
3018
12b3bb6a
TH
3019 if (!css)
3020 continue;
3021
334c3679
TH
3022 if (css->parent &&
3023 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
12b3bb6a 3024 kill_css(css);
f6d635ad 3025 } else if (!css_visible(css)) {
334c3679 3026 css_clear_dir(css);
12b3bb6a
TH
3027 if (ss->css_reset)
3028 ss->css_reset(css);
3029 }
3030 }
3031 }
3032}
3033
f7b2814b
TH
3034/**
3035 * cgroup_apply_control - apply control mask updates to the subtree
3036 * @cgrp: root of the target subtree
3037 *
3038 * subsystems can be enabled and disabled in a subtree using the following
3039 * steps.
3040 *
3041 * 1. Call cgroup_save_control() to stash the current state.
3042 * 2. Update ->subtree_control masks in the subtree as desired.
3043 * 3. Call cgroup_apply_control() to apply the changes.
3044 * 4. Optionally perform other related operations.
3045 * 5. Call cgroup_finalize_control() to finish up.
3046 *
3047 * This function implements step 3 and propagates the mask changes
3048 * throughout @cgrp's subtree, updates csses accordingly and perform
3049 * process migrations.
3050 */
3051static int cgroup_apply_control(struct cgroup *cgrp)
3052{
3053 int ret;
3054
3055 cgroup_propagate_control(cgrp);
3056
3057 ret = cgroup_apply_control_enable(cgrp);
3058 if (ret)
3059 return ret;
3060
3061 /*
fc5a828b 3062 * At this point, cgroup_e_css_by_mask() results reflect the new csses
f7b2814b
TH
3063 * making the following cgroup_update_dfl_csses() properly update
3064 * css associations of all tasks in the subtree.
3065 */
3066 ret = cgroup_update_dfl_csses(cgrp);
3067 if (ret)
3068 return ret;
3069
3070 return 0;
3071}
3072
3073/**
3074 * cgroup_finalize_control - finalize control mask update
3075 * @cgrp: root of the target subtree
3076 * @ret: the result of the update
3077 *
3078 * Finalize control mask update. See cgroup_apply_control() for more info.
3079 */
3080static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3081{
3082 if (ret) {
3083 cgroup_restore_control(cgrp);
3084 cgroup_propagate_control(cgrp);
3085 }
3086
3087 cgroup_apply_control_disable(cgrp);
3088}
3089
8cfd8147
TH
3090static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
3091{
3092 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
3093
3094 /* if nothing is getting enabled, nothing to worry about */
3095 if (!enable)
3096 return 0;
3097
3098 /* can @cgrp host any resources? */
3099 if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
3100 return -EOPNOTSUPP;
3101
3102 /* mixables don't care */
3103 if (cgroup_is_mixable(cgrp))
3104 return 0;
3105
3106 if (domain_enable) {
3107 /* can't enable domain controllers inside a thread subtree */
3108 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3109 return -EOPNOTSUPP;
3110 } else {
3111 /*
3112 * Threaded controllers can handle internal competitions
3113 * and are always allowed inside a (prospective) thread
3114 * subtree.
3115 */
3116 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3117 return 0;
3118 }
3119
3120 /*
3121 * Controllers can't be enabled for a cgroup with tasks to avoid
3122 * child cgroups competing against tasks.
3123 */
3124 if (cgroup_has_tasks(cgrp))
3125 return -EBUSY;
3126
3127 return 0;
3128}
3129
f8f22e53 3130/* change the enabled child controllers for a cgroup in the default hierarchy */
451af504
TH
3131static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3132 char *buf, size_t nbytes,
3133 loff_t off)
f8f22e53 3134{
6e5c8307 3135 u16 enable = 0, disable = 0;
a9746d8d 3136 struct cgroup *cgrp, *child;
f8f22e53 3137 struct cgroup_subsys *ss;
451af504 3138 char *tok;
f8f22e53
TH
3139 int ssid, ret;
3140
3141 /*
d37167ab
TH
3142 * Parse input - space separated list of subsystem names prefixed
3143 * with either + or -.
f8f22e53 3144 */
451af504
TH
3145 buf = strstrip(buf);
3146 while ((tok = strsep(&buf, " "))) {
d37167ab
TH
3147 if (tok[0] == '\0')
3148 continue;
a7165264 3149 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
fc5ed1e9
TH
3150 if (!cgroup_ssid_enabled(ssid) ||
3151 strcmp(tok + 1, ss->name))
f8f22e53
TH
3152 continue;
3153
3154 if (*tok == '+') {
7d331fa9
TH
3155 enable |= 1 << ssid;
3156 disable &= ~(1 << ssid);
f8f22e53 3157 } else if (*tok == '-') {
7d331fa9
TH
3158 disable |= 1 << ssid;
3159 enable &= ~(1 << ssid);
f8f22e53
TH
3160 } else {
3161 return -EINVAL;
3162 }
3163 break;
b4e0eeaf 3164 } while_each_subsys_mask();
f8f22e53
TH
3165 if (ssid == CGROUP_SUBSYS_COUNT)
3166 return -EINVAL;
3167 }
3168
945ba199 3169 cgrp = cgroup_kn_lock_live(of->kn, true);
a9746d8d
TH
3170 if (!cgrp)
3171 return -ENODEV;
f8f22e53
TH
3172
3173 for_each_subsys(ss, ssid) {
3174 if (enable & (1 << ssid)) {
667c2491 3175 if (cgrp->subtree_control & (1 << ssid)) {
f8f22e53
TH
3176 enable &= ~(1 << ssid);
3177 continue;
3178 }
3179
5531dc91 3180 if (!(cgroup_control(cgrp) & (1 << ssid))) {
c29adf24
TH
3181 ret = -ENOENT;
3182 goto out_unlock;
3183 }
f8f22e53 3184 } else if (disable & (1 << ssid)) {
667c2491 3185 if (!(cgrp->subtree_control & (1 << ssid))) {
f8f22e53
TH
3186 disable &= ~(1 << ssid);
3187 continue;
3188 }
3189
3190 /* a child has it enabled? */
3191 cgroup_for_each_live_child(child, cgrp) {
667c2491 3192 if (child->subtree_control & (1 << ssid)) {
f8f22e53 3193 ret = -EBUSY;
ddab2b6e 3194 goto out_unlock;
f8f22e53
TH
3195 }
3196 }
3197 }
3198 }
3199
3200 if (!enable && !disable) {
3201 ret = 0;
ddab2b6e 3202 goto out_unlock;
f8f22e53
TH
3203 }
3204
8cfd8147
TH
3205 ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3206 if (ret)
27f26753 3207 goto out_unlock;
f8f22e53 3208
15a27c36
TH
3209 /* save and update control masks and prepare csses */
3210 cgroup_save_control(cgrp);
f63070d3 3211
15a27c36
TH
3212 cgrp->subtree_control |= enable;
3213 cgrp->subtree_control &= ~disable;
c29adf24 3214
f7b2814b 3215 ret = cgroup_apply_control(cgrp);
f7b2814b 3216 cgroup_finalize_control(cgrp, ret);
3c745417
TH
3217 if (ret)
3218 goto out_unlock;
f8f22e53
TH
3219
3220 kernfs_activate(cgrp->kn);
f8f22e53 3221out_unlock:
a9746d8d 3222 cgroup_kn_unlock(of->kn);
451af504 3223 return ret ?: nbytes;
f8f22e53
TH
3224}
3225
c705a00d
TH
3226/**
3227 * cgroup_enable_threaded - make @cgrp threaded
3228 * @cgrp: the target cgroup
3229 *
3230 * Called when "threaded" is written to the cgroup.type interface file and
3231 * tries to make @cgrp threaded and join the parent's resource domain.
3232 * This function is never called on the root cgroup as cgroup.type doesn't
3233 * exist on it.
3234 */
8cfd8147
TH
3235static int cgroup_enable_threaded(struct cgroup *cgrp)
3236{
3237 struct cgroup *parent = cgroup_parent(cgrp);
3238 struct cgroup *dom_cgrp = parent->dom_cgrp;
479adb89
TH
3239 struct cgroup *dsct;
3240 struct cgroup_subsys_state *d_css;
8cfd8147
TH
3241 int ret;
3242
3243 lockdep_assert_held(&cgroup_mutex);
3244
3245 /* noop if already threaded */
3246 if (cgroup_is_threaded(cgrp))
3247 return 0;
3248
d1897c95
TH
3249 /*
3250 * If @cgroup is populated or has domain controllers enabled, it
3251 * can't be switched. While the below cgroup_can_be_thread_root()
3252 * test can catch the same conditions, that's only when @parent is
3253 * not mixable, so let's check it explicitly.
3254 */
3255 if (cgroup_is_populated(cgrp) ||
3256 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3257 return -EOPNOTSUPP;
3258
8cfd8147
TH
3259 /* we're joining the parent's domain, ensure its validity */
3260 if (!cgroup_is_valid_domain(dom_cgrp) ||
3261 !cgroup_can_be_thread_root(dom_cgrp))
3262 return -EOPNOTSUPP;
3263
8cfd8147
TH
3264 /*
3265 * The following shouldn't cause actual migrations and should
3266 * always succeed.
3267 */
3268 cgroup_save_control(cgrp);
3269
479adb89
TH
3270 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
3271 if (dsct == cgrp || cgroup_is_threaded(dsct))
3272 dsct->dom_cgrp = dom_cgrp;
3273
8cfd8147
TH
3274 ret = cgroup_apply_control(cgrp);
3275 if (!ret)
3276 parent->nr_threaded_children++;
8cfd8147
TH
3277
3278 cgroup_finalize_control(cgrp, ret);
3279 return ret;
3280}
3281
3282static int cgroup_type_show(struct seq_file *seq, void *v)
3283{
3284 struct cgroup *cgrp = seq_css(seq)->cgroup;
3285
3286 if (cgroup_is_threaded(cgrp))
3287 seq_puts(seq, "threaded\n");
3288 else if (!cgroup_is_valid_domain(cgrp))
3289 seq_puts(seq, "domain invalid\n");
3290 else if (cgroup_is_thread_root(cgrp))
3291 seq_puts(seq, "domain threaded\n");
3292 else
3293 seq_puts(seq, "domain\n");
3294
3295 return 0;
3296}
3297
3298static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
3299 size_t nbytes, loff_t off)
3300{
3301 struct cgroup *cgrp;
3302 int ret;
3303
3304 /* only switching to threaded mode is supported */
3305 if (strcmp(strstrip(buf), "threaded"))
3306 return -EINVAL;
3307
3308 cgrp = cgroup_kn_lock_live(of->kn, false);
3309 if (!cgrp)
3310 return -ENOENT;
3311
3312 /* threaded can only be enabled */
3313 ret = cgroup_enable_threaded(cgrp);
3314
3315 cgroup_kn_unlock(of->kn);
3316 return ret ?: nbytes;
3317}
3318
1a926e0b
RG
3319static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
3320{
3321 struct cgroup *cgrp = seq_css(seq)->cgroup;
3322 int descendants = READ_ONCE(cgrp->max_descendants);
3323
3324 if (descendants == INT_MAX)
3325 seq_puts(seq, "max\n");
3326 else
3327 seq_printf(seq, "%d\n", descendants);
3328
3329 return 0;
3330}
3331
3332static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
3333 char *buf, size_t nbytes, loff_t off)
3334{
3335 struct cgroup *cgrp;
3336 int descendants;
3337 ssize_t ret;
3338
3339 buf = strstrip(buf);
3340 if (!strcmp(buf, "max")) {
3341 descendants = INT_MAX;
3342 } else {
3343 ret = kstrtoint(buf, 0, &descendants);
3344 if (ret)
3345 return ret;
3346 }
3347
696b98f2 3348 if (descendants < 0)
1a926e0b
RG
3349 return -ERANGE;
3350
3351 cgrp = cgroup_kn_lock_live(of->kn, false);
3352 if (!cgrp)
3353 return -ENOENT;
3354
3355 cgrp->max_descendants = descendants;
3356
3357 cgroup_kn_unlock(of->kn);
3358
3359 return nbytes;
3360}
3361
3362static int cgroup_max_depth_show(struct seq_file *seq, void *v)
3363{
3364 struct cgroup *cgrp = seq_css(seq)->cgroup;
3365 int depth = READ_ONCE(cgrp->max_depth);
3366
3367 if (depth == INT_MAX)
3368 seq_puts(seq, "max\n");
3369 else
3370 seq_printf(seq, "%d\n", depth);
3371
3372 return 0;
3373}
3374
3375static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
3376 char *buf, size_t nbytes, loff_t off)
3377{
3378 struct cgroup *cgrp;
3379 ssize_t ret;
3380 int depth;
3381
3382 buf = strstrip(buf);
3383 if (!strcmp(buf, "max")) {
3384 depth = INT_MAX;
3385 } else {
3386 ret = kstrtoint(buf, 0, &depth);
3387 if (ret)
3388 return ret;
3389 }
3390
696b98f2 3391 if (depth < 0)
1a926e0b
RG
3392 return -ERANGE;
3393
3394 cgrp = cgroup_kn_lock_live(of->kn, false);
3395 if (!cgrp)
3396 return -ENOENT;
3397
3398 cgrp->max_depth = depth;
3399
3400 cgroup_kn_unlock(of->kn);
3401
3402 return nbytes;
3403}
3404
4a07c222 3405static int cgroup_events_show(struct seq_file *seq, void *v)
842b597e 3406{
4a07c222 3407 seq_printf(seq, "populated %d\n",
27bd4dbb 3408 cgroup_is_populated(seq_css(seq)->cgroup));
842b597e
TH
3409 return 0;
3410}
3411
3e48930c 3412static int cgroup_stat_show(struct seq_file *seq, void *v)
ec39225c
RG
3413{
3414 struct cgroup *cgroup = seq_css(seq)->cgroup;
3415
3416 seq_printf(seq, "nr_descendants %d\n",
3417 cgroup->nr_descendants);
3418 seq_printf(seq, "nr_dying_descendants %d\n",
3419 cgroup->nr_dying_descendants);
3420
3421 return 0;
3422}
3423
d41bf8c9
TH
3424static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
3425 struct cgroup *cgrp, int ssid)
3426{
3427 struct cgroup_subsys *ss = cgroup_subsys[ssid];
3428 struct cgroup_subsys_state *css;
3429 int ret;
3430
3431 if (!ss->css_extra_stat_show)
3432 return 0;
3433
3434 css = cgroup_tryget_css(cgrp, ss);
3435 if (!css)
3436 return 0;
3437
3438 ret = ss->css_extra_stat_show(seq, css);
3439 css_put(css);
3440 return ret;
3441}
3442
3443static int cpu_stat_show(struct seq_file *seq, void *v)
3444{
c3ba1329 3445 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
d41bf8c9
TH
3446 int ret = 0;
3447
d4ff749b 3448 cgroup_base_stat_cputime_show(seq);
d41bf8c9
TH
3449#ifdef CONFIG_CGROUP_SCHED
3450 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
3451#endif
3452 return ret;
3453}
3454
2ce7135a
JW
3455#ifdef CONFIG_PSI
3456static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
3457{
3458 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_IO);
3459}
3460static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
3461{
3462 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_MEM);
3463}
3464static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
3465{
3466 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_CPU);
3467}
3468#endif
3469
e90cbebc
TH
3470static int cgroup_file_open(struct kernfs_open_file *of)
3471{
3472 struct cftype *cft = of->kn->priv;
3473
3474 if (cft->open)
3475 return cft->open(of);
3476 return 0;
3477}
3478
3479static void cgroup_file_release(struct kernfs_open_file *of)
3480{
3481 struct cftype *cft = of->kn->priv;
3482
3483 if (cft->release)
3484 cft->release(of);
3485}
3486
2bd59d48
TH
3487static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3488 size_t nbytes, loff_t off)
355e0c48 3489{
5136f636 3490 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
2bd59d48
TH
3491 struct cgroup *cgrp = of->kn->parent->priv;
3492 struct cftype *cft = of->kn->priv;
3493 struct cgroup_subsys_state *css;
a742c59d 3494 int ret;
355e0c48 3495
5136f636
TH
3496 /*
3497 * If namespaces are delegation boundaries, disallow writes to
3498 * files in an non-init namespace root from inside the namespace
3499 * except for the files explicitly marked delegatable -
3500 * cgroup.procs and cgroup.subtree_control.
3501 */
3502 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
3503 !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
3504 ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp)
3505 return -EPERM;
3506
b4168640
TH
3507 if (cft->write)
3508 return cft->write(of, buf, nbytes, off);
3509
2bd59d48
TH
3510 /*
3511 * kernfs guarantees that a file isn't deleted with operations in
3512 * flight, which means that the matching css is and stays alive and
3513 * doesn't need to be pinned. The RCU locking is not necessary
3514 * either. It's just for the convenience of using cgroup_css().
3515 */
3516 rcu_read_lock();
3517 css = cgroup_css(cgrp, cft->ss);
3518 rcu_read_unlock();
a742c59d 3519
451af504 3520 if (cft->write_u64) {
a742c59d
TH
3521 unsigned long long v;
3522 ret = kstrtoull(buf, 0, &v);
3523 if (!ret)
3524 ret = cft->write_u64(css, cft, v);
3525 } else if (cft->write_s64) {
3526 long long v;
3527 ret = kstrtoll(buf, 0, &v);
3528 if (!ret)
3529 ret = cft->write_s64(css, cft, v);
e73d2c61 3530 } else {
a742c59d 3531 ret = -EINVAL;
e73d2c61 3532 }
2bd59d48 3533
a742c59d 3534 return ret ?: nbytes;
355e0c48
PM
3535}
3536
6612f05b 3537static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
db3b1497 3538{
2bd59d48 3539 return seq_cft(seq)->seq_start(seq, ppos);
db3b1497
PM
3540}
3541
6612f05b 3542static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
ddbcc7e8 3543{
2bd59d48 3544 return seq_cft(seq)->seq_next(seq, v, ppos);
ddbcc7e8
PM
3545}
3546
6612f05b 3547static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
ddbcc7e8 3548{
e90cbebc
TH
3549 if (seq_cft(seq)->seq_stop)
3550 seq_cft(seq)->seq_stop(seq, v);
ddbcc7e8
PM
3551}
3552
91796569 3553static int cgroup_seqfile_show(struct seq_file *m, void *arg)
e73d2c61 3554{
7da11279
TH
3555 struct cftype *cft = seq_cft(m);
3556 struct cgroup_subsys_state *css = seq_css(m);
e73d2c61 3557
2da8ca82
TH
3558 if (cft->seq_show)
3559 return cft->seq_show(m, arg);
e73d2c61 3560
f4c753b7 3561 if (cft->read_u64)
896f5199
TH
3562 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3563 else if (cft->read_s64)
3564 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3565 else
3566 return -EINVAL;
3567 return 0;
91796569
PM
3568}
3569
2bd59d48
TH
3570static struct kernfs_ops cgroup_kf_single_ops = {
3571 .atomic_write_len = PAGE_SIZE,
e90cbebc
TH
3572 .open = cgroup_file_open,
3573 .release = cgroup_file_release,
2bd59d48
TH
3574 .write = cgroup_file_write,
3575 .seq_show = cgroup_seqfile_show,
91796569
PM
3576};
3577
2bd59d48
TH
3578static struct kernfs_ops cgroup_kf_ops = {
3579 .atomic_write_len = PAGE_SIZE,
e90cbebc
TH
3580 .open = cgroup_file_open,
3581 .release = cgroup_file_release,
2bd59d48
TH
3582 .write = cgroup_file_write,
3583 .seq_start = cgroup_seqfile_start,
3584 .seq_next = cgroup_seqfile_next,
3585 .seq_stop = cgroup_seqfile_stop,
3586 .seq_show = cgroup_seqfile_show,
3587};
ddbcc7e8 3588
49957f8e
TH
3589/* set uid and gid of cgroup dirs and files to that of the creator */
3590static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3591{
3592 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3593 .ia_uid = current_fsuid(),
3594 .ia_gid = current_fsgid(), };
3595
3596 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3597 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3598 return 0;
3599
3600 return kernfs_setattr(kn, &iattr);
3601}
3602
b12e3583
TH
3603static void cgroup_file_notify_timer(struct timer_list *timer)
3604{
3605 cgroup_file_notify(container_of(timer, struct cgroup_file,
3606 notify_timer));
3607}
3608
4df8dc90
TH
3609static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3610 struct cftype *cft)
ddbcc7e8 3611{
8d7e6fb0 3612 char name[CGROUP_FILE_NAME_MAX];
2bd59d48
TH
3613 struct kernfs_node *kn;
3614 struct lock_class_key *key = NULL;
49957f8e 3615 int ret;
05ef1d7c 3616
2bd59d48
TH
3617#ifdef CONFIG_DEBUG_LOCK_ALLOC
3618 key = &cft->lockdep_key;
3619#endif
3620 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
488dee96
DT
3621 cgroup_file_mode(cft),
3622 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
3623 0, cft->kf_ops, cft,
dfeb0750 3624 NULL, key);
49957f8e
TH
3625 if (IS_ERR(kn))
3626 return PTR_ERR(kn);
3627
3628 ret = cgroup_kn_set_ugid(kn);
f8f22e53 3629 if (ret) {
49957f8e 3630 kernfs_remove(kn);
f8f22e53
TH
3631 return ret;
3632 }
3633
6f60eade
TH
3634 if (cft->file_offset) {
3635 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3636
b12e3583
TH
3637 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
3638
34c06254 3639 spin_lock_irq(&cgroup_file_kn_lock);
6f60eade 3640 cfile->kn = kn;
34c06254 3641 spin_unlock_irq(&cgroup_file_kn_lock);
6f60eade
TH
3642 }
3643
f8f22e53 3644 return 0;
ddbcc7e8
PM
3645}
3646
b1f28d31
TH
3647/**
3648 * cgroup_addrm_files - add or remove files to a cgroup directory
4df8dc90
TH
3649 * @css: the target css
3650 * @cgrp: the target cgroup (usually css->cgroup)
b1f28d31
TH
3651 * @cfts: array of cftypes to be added
3652 * @is_add: whether to add or remove
3653 *
3654 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
6732ed85 3655 * For removals, this function never fails.
b1f28d31 3656 */
4df8dc90
TH
3657static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3658 struct cgroup *cgrp, struct cftype cfts[],
2bb566cb 3659 bool is_add)
ddbcc7e8 3660{
6732ed85 3661 struct cftype *cft, *cft_end = NULL;
b598dde3 3662 int ret = 0;
b1f28d31 3663
01f6474c 3664 lockdep_assert_held(&cgroup_mutex);
db0416b6 3665
6732ed85
TH
3666restart:
3667 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
f33fddc2 3668 /* does cft->flags tell us to skip this file on @cgrp? */
05ebb6e6 3669 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
8cbbf2c9 3670 continue;
05ebb6e6 3671 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
873fe09e 3672 continue;
d51f39b0 3673 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
f33fddc2 3674 continue;
d51f39b0 3675 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
f33fddc2 3676 continue;
5cf8114d
WL
3677 if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
3678 continue;
2739d3cc 3679 if (is_add) {
4df8dc90 3680 ret = cgroup_add_file(css, cgrp, cft);
b1f28d31 3681 if (ret) {
ed3d261b
JP
3682 pr_warn("%s: failed to add %s, err=%d\n",
3683 __func__, cft->name, ret);
6732ed85
TH
3684 cft_end = cft;
3685 is_add = false;
3686 goto restart;
b1f28d31 3687 }
2739d3cc
LZ
3688 } else {
3689 cgroup_rm_file(cgrp, cft);
db0416b6 3690 }
ddbcc7e8 3691 }
b598dde3 3692 return ret;
ddbcc7e8
PM
3693}
3694
21a2d343 3695static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
8e3f6541 3696{
2bb566cb 3697 struct cgroup_subsys *ss = cfts[0].ss;
3dd06ffa 3698 struct cgroup *root = &ss->root->cgrp;
492eb21b 3699 struct cgroup_subsys_state *css;
9ccece80 3700 int ret = 0;
8e3f6541 3701
01f6474c 3702 lockdep_assert_held(&cgroup_mutex);
e8c82d20 3703
e8c82d20 3704 /* add/rm files for all cgroups created before */
ca8bdcaf 3705 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
492eb21b
TH
3706 struct cgroup *cgrp = css->cgroup;
3707
88cb04b9 3708 if (!(css->flags & CSS_VISIBLE))
e8c82d20
LZ
3709 continue;
3710
4df8dc90 3711 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
9ccece80
TH
3712 if (ret)
3713 break;
8e3f6541 3714 }
21a2d343
TH
3715
3716 if (is_add && !ret)
3717 kernfs_activate(root->kn);
9ccece80 3718 return ret;
8e3f6541
TH
3719}
3720
2da440a2 3721static void cgroup_exit_cftypes(struct cftype *cfts)
8e3f6541 3722{
2bb566cb 3723 struct cftype *cft;
8e3f6541 3724
2bd59d48
TH
3725 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3726 /* free copy for custom atomic_write_len, see init_cftypes() */
3727 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3728 kfree(cft->kf_ops);
3729 cft->kf_ops = NULL;
2da440a2 3730 cft->ss = NULL;
a8ddc821
TH
3731
3732 /* revert flags set by cgroup core while adding @cfts */
05ebb6e6 3733 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
2bd59d48 3734 }
2da440a2
TH
3735}
3736
2bd59d48 3737static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2da440a2
TH
3738{
3739 struct cftype *cft;
3740
2bd59d48
TH
3741 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3742 struct kernfs_ops *kf_ops;
3743
0adb0704
TH
3744 WARN_ON(cft->ss || cft->kf_ops);
3745
2bd59d48
TH
3746 if (cft->seq_start)
3747 kf_ops = &cgroup_kf_ops;
3748 else
3749 kf_ops = &cgroup_kf_single_ops;
3750
3751 /*
3752 * Ugh... if @cft wants a custom max_write_len, we need to
3753 * make a copy of kf_ops to set its atomic_write_len.
3754 */
3755 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3756 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3757 if (!kf_ops) {
3758 cgroup_exit_cftypes(cfts);
3759 return -ENOMEM;
3760 }
3761 kf_ops->atomic_write_len = cft->max_write_len;
3762 }
8e3f6541 3763
2bd59d48 3764 cft->kf_ops = kf_ops;
2bb566cb 3765 cft->ss = ss;
2bd59d48 3766 }
2bb566cb 3767
2bd59d48 3768 return 0;
2da440a2
TH
3769}
3770
21a2d343
TH
3771static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3772{
01f6474c 3773 lockdep_assert_held(&cgroup_mutex);
21a2d343
TH
3774
3775 if (!cfts || !cfts[0].ss)
3776 return -ENOENT;
3777
3778 list_del(&cfts->node);
3779 cgroup_apply_cftypes(cfts, false);
3780 cgroup_exit_cftypes(cfts);
3781 return 0;
8e3f6541 3782}
8e3f6541 3783
79578621
TH
3784/**
3785 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
79578621
TH
3786 * @cfts: zero-length name terminated array of cftypes
3787 *
2bb566cb
TH
3788 * Unregister @cfts. Files described by @cfts are removed from all
3789 * existing cgroups and all future cgroups won't have them either. This
3790 * function can be called anytime whether @cfts' subsys is attached or not.
79578621
TH
3791 *
3792 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2bb566cb 3793 * registered.
79578621 3794 */
2bb566cb 3795int cgroup_rm_cftypes(struct cftype *cfts)
79578621 3796{
21a2d343 3797 int ret;
79578621 3798
01f6474c 3799 mutex_lock(&cgroup_mutex);
21a2d343 3800 ret = cgroup_rm_cftypes_locked(cfts);
01f6474c 3801 mutex_unlock(&cgroup_mutex);
21a2d343 3802 return ret;
80b13586
TH
3803}
3804
8e3f6541
TH
3805/**
3806 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3807 * @ss: target cgroup subsystem
3808 * @cfts: zero-length name terminated array of cftypes
3809 *
3810 * Register @cfts to @ss. Files described by @cfts are created for all
3811 * existing cgroups to which @ss is attached and all future cgroups will
3812 * have them too. This function can be called anytime whether @ss is
3813 * attached or not.
3814 *
3815 * Returns 0 on successful registration, -errno on failure. Note that this
3816 * function currently returns 0 as long as @cfts registration is successful
3817 * even if some file creation attempts on existing cgroups fail.
3818 */
2cf669a5 3819static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
8e3f6541 3820{
9ccece80 3821 int ret;
8e3f6541 3822
fc5ed1e9 3823 if (!cgroup_ssid_enabled(ss->id))
c731ae1d
LZ
3824 return 0;
3825
dc5736ed
LZ
3826 if (!cfts || cfts[0].name[0] == '\0')
3827 return 0;
2bb566cb 3828
2bd59d48
TH
3829 ret = cgroup_init_cftypes(ss, cfts);
3830 if (ret)
3831 return ret;
79578621 3832
01f6474c 3833 mutex_lock(&cgroup_mutex);
21a2d343 3834
0adb0704 3835 list_add_tail(&cfts->node, &ss->cfts);
21a2d343 3836 ret = cgroup_apply_cftypes(cfts, true);
9ccece80 3837 if (ret)
21a2d343 3838 cgroup_rm_cftypes_locked(cfts);
79578621 3839
01f6474c 3840 mutex_unlock(&cgroup_mutex);
9ccece80 3841 return ret;
79578621
TH
3842}
3843
a8ddc821
TH
3844/**
3845 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3846 * @ss: target cgroup subsystem
3847 * @cfts: zero-length name terminated array of cftypes
3848 *
3849 * Similar to cgroup_add_cftypes() but the added files are only used for
3850 * the default hierarchy.
3851 */
3852int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3853{
3854 struct cftype *cft;
3855
3856 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
05ebb6e6 3857 cft->flags |= __CFTYPE_ONLY_ON_DFL;
a8ddc821
TH
3858 return cgroup_add_cftypes(ss, cfts);
3859}
3860
3861/**
3862 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3863 * @ss: target cgroup subsystem
3864 * @cfts: zero-length name terminated array of cftypes
3865 *
3866 * Similar to cgroup_add_cftypes() but the added files are only used for
3867 * the legacy hierarchies.
3868 */
2cf669a5
TH
3869int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3870{
a8ddc821
TH
3871 struct cftype *cft;
3872
e4b7037c
TH
3873 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3874 cft->flags |= __CFTYPE_NOT_ON_DFL;
2cf669a5
TH
3875 return cgroup_add_cftypes(ss, cfts);
3876}
3877
34c06254
TH
3878/**
3879 * cgroup_file_notify - generate a file modified event for a cgroup_file
3880 * @cfile: target cgroup_file
3881 *
3882 * @cfile must have been obtained by setting cftype->file_offset.
3883 */
3884void cgroup_file_notify(struct cgroup_file *cfile)
3885{
3886 unsigned long flags;
3887
3888 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
b12e3583
TH
3889 if (cfile->kn) {
3890 unsigned long last = cfile->notified_at;
3891 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
3892
3893 if (time_in_range(jiffies, last, next)) {
3894 timer_reduce(&cfile->notify_timer, next);
3895 } else {
3896 kernfs_notify(cfile->kn);
3897 cfile->notified_at = jiffies;
3898 }
3899 }
34c06254
TH
3900 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3901}
3902
53fa5261 3903/**
492eb21b 3904 * css_next_child - find the next child of a given css
c2931b70
TH
3905 * @pos: the current position (%NULL to initiate traversal)
3906 * @parent: css whose children to walk
53fa5261 3907 *
c2931b70 3908 * This function returns the next child of @parent and should be called
87fb54f1 3909 * under either cgroup_mutex or RCU read lock. The only requirement is
c2931b70
TH
3910 * that @parent and @pos are accessible. The next sibling is guaranteed to
3911 * be returned regardless of their states.
3912 *
3913 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3914 * css which finished ->css_online() is guaranteed to be visible in the
3915 * future iterations and will stay visible until the last reference is put.
3916 * A css which hasn't finished ->css_online() or already finished
3917 * ->css_offline() may show up during traversal. It's each subsystem's
3918 * responsibility to synchronize against on/offlining.
53fa5261 3919 */
c2931b70
TH
3920struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3921 struct cgroup_subsys_state *parent)
53fa5261 3922{
c2931b70 3923 struct cgroup_subsys_state *next;
53fa5261 3924
8353da1f 3925 cgroup_assert_mutex_or_rcu_locked();
53fa5261
TH
3926
3927 /*
de3f0341
TH
3928 * @pos could already have been unlinked from the sibling list.
3929 * Once a cgroup is removed, its ->sibling.next is no longer
3930 * updated when its next sibling changes. CSS_RELEASED is set when
3931 * @pos is taken off list, at which time its next pointer is valid,
3932 * and, as releases are serialized, the one pointed to by the next
3933 * pointer is guaranteed to not have started release yet. This
3934 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3935 * critical section, the one pointed to by its next pointer is
3936 * guaranteed to not have finished its RCU grace period even if we
3937 * have dropped rcu_read_lock() inbetween iterations.
3b287a50 3938 *
de3f0341
TH
3939 * If @pos has CSS_RELEASED set, its next pointer can't be
3940 * dereferenced; however, as each css is given a monotonically
3941 * increasing unique serial number and always appended to the
3942 * sibling list, the next one can be found by walking the parent's
3943 * children until the first css with higher serial number than
3944 * @pos's. While this path can be slower, it happens iff iteration
3945 * races against release and the race window is very small.
53fa5261 3946 */
3b287a50 3947 if (!pos) {
c2931b70
TH
3948 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3949 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3950 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
3b287a50 3951 } else {
c2931b70 3952 list_for_each_entry_rcu(next, &parent->children, sibling)
3b287a50
TH
3953 if (next->serial_nr > pos->serial_nr)
3954 break;
53fa5261
TH
3955 }
3956
3b281afb
TH
3957 /*
3958 * @next, if not pointing to the head, can be dereferenced and is
c2931b70 3959 * the next sibling.
3b281afb 3960 */
c2931b70
TH
3961 if (&next->sibling != &parent->children)
3962 return next;
3b281afb 3963 return NULL;
53fa5261 3964}
53fa5261 3965
574bd9f7 3966/**
492eb21b 3967 * css_next_descendant_pre - find the next descendant for pre-order walk
574bd9f7 3968 * @pos: the current position (%NULL to initiate traversal)
492eb21b 3969 * @root: css whose descendants to walk
574bd9f7 3970 *
492eb21b 3971 * To be used by css_for_each_descendant_pre(). Find the next descendant
bd8815a6
TH
3972 * to visit for pre-order traversal of @root's descendants. @root is
3973 * included in the iteration and the first node to be visited.
75501a6d 3974 *
87fb54f1
TH
3975 * While this function requires cgroup_mutex or RCU read locking, it
3976 * doesn't require the whole traversal to be contained in a single critical
3977 * section. This function will return the correct next descendant as long
3978 * as both @pos and @root are accessible and @pos is a descendant of @root.
c2931b70
TH
3979 *
3980 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3981 * css which finished ->css_online() is guaranteed to be visible in the
3982 * future iterations and will stay visible until the last reference is put.
3983 * A css which hasn't finished ->css_online() or already finished
3984 * ->css_offline() may show up during traversal. It's each subsystem's
3985 * responsibility to synchronize against on/offlining.
574bd9f7 3986 */
492eb21b
TH
3987struct cgroup_subsys_state *
3988css_next_descendant_pre(struct cgroup_subsys_state *pos,
3989 struct cgroup_subsys_state *root)
574bd9f7 3990{
492eb21b 3991 struct cgroup_subsys_state *next;
574bd9f7 3992
8353da1f 3993 cgroup_assert_mutex_or_rcu_locked();
574bd9f7 3994
bd8815a6 3995 /* if first iteration, visit @root */
7805d000 3996 if (!pos)
bd8815a6 3997 return root;
574bd9f7
TH
3998
3999 /* visit the first child if exists */
492eb21b 4000 next = css_next_child(NULL, pos);
574bd9f7
TH
4001 if (next)
4002 return next;
4003
4004 /* no child, visit my or the closest ancestor's next sibling */
492eb21b 4005 while (pos != root) {
5c9d535b 4006 next = css_next_child(pos, pos->parent);
75501a6d 4007 if (next)
574bd9f7 4008 return next;
5c9d535b 4009 pos = pos->parent;
7805d000 4010 }
574bd9f7
TH
4011
4012 return NULL;
4013}
574bd9f7 4014
12a9d2fe 4015/**
492eb21b
TH
4016 * css_rightmost_descendant - return the rightmost descendant of a css
4017 * @pos: css of interest
12a9d2fe 4018 *
492eb21b
TH
4019 * Return the rightmost descendant of @pos. If there's no descendant, @pos
4020 * is returned. This can be used during pre-order traversal to skip
12a9d2fe 4021 * subtree of @pos.
75501a6d 4022 *
87fb54f1
TH
4023 * While this function requires cgroup_mutex or RCU read locking, it
4024 * doesn't require the whole traversal to be contained in a single critical
4025 * section. This function will return the correct rightmost descendant as
4026 * long as @pos is accessible.
12a9d2fe 4027 */
492eb21b
TH
4028struct cgroup_subsys_state *
4029css_rightmost_descendant(struct cgroup_subsys_state *pos)
12a9d2fe 4030{
492eb21b 4031 struct cgroup_subsys_state *last, *tmp;
12a9d2fe 4032
8353da1f 4033 cgroup_assert_mutex_or_rcu_locked();
12a9d2fe
TH
4034
4035 do {
4036 last = pos;
4037 /* ->prev isn't RCU safe, walk ->next till the end */
4038 pos = NULL;
492eb21b 4039 css_for_each_child(tmp, last)
12a9d2fe
TH
4040 pos = tmp;
4041 } while (pos);
4042
4043 return last;
4044}
12a9d2fe 4045
492eb21b
TH
4046static struct cgroup_subsys_state *
4047css_leftmost_descendant(struct cgroup_subsys_state *pos)
574bd9f7 4048{
492eb21b 4049 struct cgroup_subsys_state *last;
574bd9f7
TH
4050
4051 do {
4052 last = pos;
492eb21b 4053 pos = css_next_child(NULL, pos);
574bd9f7
TH
4054 } while (pos);
4055
4056 return last;
4057}
4058
4059/**
492eb21b 4060 * css_next_descendant_post - find the next descendant for post-order walk
574bd9f7 4061 * @pos: the current position (%NULL to initiate traversal)
492eb21b 4062 * @root: css whose descendants to walk
574bd9f7 4063 *
492eb21b 4064 * To be used by css_for_each_descendant_post(). Find the next descendant
bd8815a6
TH
4065 * to visit for post-order traversal of @root's descendants. @root is
4066 * included in the iteration and the last node to be visited.
75501a6d 4067 *
87fb54f1
TH
4068 * While this function requires cgroup_mutex or RCU read locking, it
4069 * doesn't require the whole traversal to be contained in a single critical
4070 * section. This function will return the correct next descendant as long
4071 * as both @pos and @cgroup are accessible and @pos is a descendant of
4072 * @cgroup.
c2931b70
TH
4073 *
4074 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4075 * css which finished ->css_online() is guaranteed to be visible in the
4076 * future iterations and will stay visible until the last reference is put.
4077 * A css which hasn't finished ->css_online() or already finished
4078 * ->css_offline() may show up during traversal. It's each subsystem's
4079 * responsibility to synchronize against on/offlining.
574bd9f7 4080 */
492eb21b
TH
4081struct cgroup_subsys_state *
4082css_next_descendant_post(struct cgroup_subsys_state *pos,
4083 struct cgroup_subsys_state *root)
574bd9f7 4084{
492eb21b 4085 struct cgroup_subsys_state *next;
574bd9f7 4086
8353da1f 4087 cgroup_assert_mutex_or_rcu_locked();
574bd9f7 4088
58b79a91
TH
4089 /* if first iteration, visit leftmost descendant which may be @root */
4090 if (!pos)
4091 return css_leftmost_descendant(root);
574bd9f7 4092
bd8815a6
TH
4093 /* if we visited @root, we're done */
4094 if (pos == root)
4095 return NULL;
4096
574bd9f7 4097 /* if there's an unvisited sibling, visit its leftmost descendant */
5c9d535b 4098 next = css_next_child(pos, pos->parent);
75501a6d 4099 if (next)
492eb21b 4100 return css_leftmost_descendant(next);
574bd9f7
TH
4101
4102 /* no sibling left, visit parent */
5c9d535b 4103 return pos->parent;
574bd9f7 4104}
574bd9f7 4105
f3d46500
TH
4106/**
4107 * css_has_online_children - does a css have online children
4108 * @css: the target css
4109 *
4110 * Returns %true if @css has any online children; otherwise, %false. This
4111 * function can be called from any context but the caller is responsible
4112 * for synchronizing against on/offlining as necessary.
4113 */
4114bool css_has_online_children(struct cgroup_subsys_state *css)
cbc125ef 4115{
f3d46500
TH
4116 struct cgroup_subsys_state *child;
4117 bool ret = false;
cbc125ef
TH
4118
4119 rcu_read_lock();
f3d46500 4120 css_for_each_child(child, css) {
99bae5f9 4121 if (child->flags & CSS_ONLINE) {
f3d46500
TH
4122 ret = true;
4123 break;
cbc125ef
TH
4124 }
4125 }
4126 rcu_read_unlock();
f3d46500 4127 return ret;
574bd9f7 4128}
574bd9f7 4129
450ee0c1
TH
4130static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4131{
4132 struct list_head *l;
4133 struct cgrp_cset_link *link;
4134 struct css_set *cset;
4135
4136 lockdep_assert_held(&css_set_lock);
4137
4138 /* find the next threaded cset */
4139 if (it->tcset_pos) {
4140 l = it->tcset_pos->next;
4141
4142 if (l != it->tcset_head) {
4143 it->tcset_pos = l;
4144 return container_of(l, struct css_set,
4145 threaded_csets_node);
4146 }
4147
4148 it->tcset_pos = NULL;
4149 }
4150
4151 /* find the next cset */
4152 l = it->cset_pos;
4153 l = l->next;
4154 if (l == it->cset_head) {
4155 it->cset_pos = NULL;
4156 return NULL;
4157 }
4158
4159 if (it->ss) {
4160 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4161 } else {
4162 link = list_entry(l, struct cgrp_cset_link, cset_link);
4163 cset = link->cset;
4164 }
4165
4166 it->cset_pos = l;
4167
4168 /* initialize threaded css_set walking */
4169 if (it->flags & CSS_TASK_ITER_THREADED) {
4170 if (it->cur_dcset)
4171 put_css_set_locked(it->cur_dcset);
4172 it->cur_dcset = cset;
4173 get_css_set(cset);
4174
4175 it->tcset_head = &cset->threaded_csets;
4176 it->tcset_pos = &cset->threaded_csets;
4177 }
4178
4179 return cset;
4180}
4181
0942eeee 4182/**
ecb9d535 4183 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
0942eeee
TH
4184 * @it: the iterator to advance
4185 *
4186 * Advance @it to the next css_set to walk.
d515876e 4187 */
ecb9d535 4188static void css_task_iter_advance_css_set(struct css_task_iter *it)
d515876e 4189{
d515876e
TH
4190 struct css_set *cset;
4191
f0d9a5f1 4192 lockdep_assert_held(&css_set_lock);
ed27b9f7 4193
d515876e
TH
4194 /* Advance to the next non-empty css_set */
4195 do {
450ee0c1
TH
4196 cset = css_task_iter_next_css_set(it);
4197 if (!cset) {
ecb9d535 4198 it->task_pos = NULL;
d515876e
TH
4199 return;
4200 }
0de0942d 4201 } while (!css_set_populated(cset));
c7561128 4202
c7561128 4203 if (!list_empty(&cset->tasks))
0f0a2b4f 4204 it->task_pos = cset->tasks.next;
c7561128 4205 else
0f0a2b4f
TH
4206 it->task_pos = cset->mg_tasks.next;
4207
4208 it->tasks_head = &cset->tasks;
4209 it->mg_tasks_head = &cset->mg_tasks;
ed27b9f7
TH
4210
4211 /*
4212 * We don't keep css_sets locked across iteration steps and thus
4213 * need to take steps to ensure that iteration can be resumed after
4214 * the lock is re-acquired. Iteration is performed at two levels -
4215 * css_sets and tasks in them.
4216 *
4217 * Once created, a css_set never leaves its cgroup lists, so a
4218 * pinned css_set is guaranteed to stay put and we can resume
4219 * iteration afterwards.
4220 *
4221 * Tasks may leave @cset across iteration steps. This is resolved
4222 * by registering each iterator with the css_set currently being
4223 * walked and making css_set_move_task() advance iterators whose
4224 * next task is leaving.
4225 */
4226 if (it->cur_cset) {
4227 list_del(&it->iters_node);
4228 put_css_set_locked(it->cur_cset);
4229 }
4230 get_css_set(cset);
4231 it->cur_cset = cset;
4232 list_add(&it->iters_node, &cset->task_iters);
d515876e
TH
4233}
4234
ecb9d535
TH
4235static void css_task_iter_advance(struct css_task_iter *it)
4236{
74d0833c 4237 struct list_head *next;
ecb9d535 4238
f0d9a5f1 4239 lockdep_assert_held(&css_set_lock);
bc2fb7ed 4240repeat:
e9d81a1b
TH
4241 if (it->task_pos) {
4242 /*
4243 * Advance iterator to find next entry. cset->tasks is
4244 * consumed first and then ->mg_tasks. After ->mg_tasks,
4245 * we move onto the next cset.
4246 */
4247 next = it->task_pos->next;
ecb9d535 4248
e9d81a1b
TH
4249 if (next == it->tasks_head)
4250 next = it->mg_tasks_head->next;
ecb9d535 4251
e9d81a1b
TH
4252 if (next == it->mg_tasks_head)
4253 css_task_iter_advance_css_set(it);
4254 else
4255 it->task_pos = next;
4256 } else {
4257 /* called from start, proceed to the first cset */
ecb9d535 4258 css_task_iter_advance_css_set(it);
e9d81a1b 4259 }
bc2fb7ed
TH
4260
4261 /* if PROCS, skip over tasks which aren't group leaders */
4262 if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos &&
4263 !thread_group_leader(list_entry(it->task_pos, struct task_struct,
4264 cg_list)))
4265 goto repeat;
ecb9d535
TH
4266}
4267
0942eeee 4268/**
72ec7029
TH
4269 * css_task_iter_start - initiate task iteration
4270 * @css: the css to walk tasks of
bc2fb7ed 4271 * @flags: CSS_TASK_ITER_* flags
0942eeee
TH
4272 * @it: the task iterator to use
4273 *
72ec7029
TH
4274 * Initiate iteration through the tasks of @css. The caller can call
4275 * css_task_iter_next() to walk through the tasks until the function
4276 * returns NULL. On completion of iteration, css_task_iter_end() must be
4277 * called.
0942eeee 4278 */
bc2fb7ed 4279void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
72ec7029 4280 struct css_task_iter *it)
817929ec 4281{
56fde9e0
TH
4282 /* no one should try to iterate before mounting cgroups */
4283 WARN_ON_ONCE(!use_task_css_set_links);
31a7df01 4284
ed27b9f7
TH
4285 memset(it, 0, sizeof(*it));
4286
82d6489d 4287 spin_lock_irq(&css_set_lock);
c59cd3d8 4288
3ebb2b6e 4289 it->ss = css->ss;
bc2fb7ed 4290 it->flags = flags;
3ebb2b6e
TH
4291
4292 if (it->ss)
4293 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4294 else
4295 it->cset_pos = &css->cgroup->cset_links;
4296
0f0a2b4f 4297 it->cset_head = it->cset_pos;
c59cd3d8 4298
e9d81a1b 4299 css_task_iter_advance(it);
ed27b9f7 4300
82d6489d 4301 spin_unlock_irq(&css_set_lock);
817929ec
PM
4302}
4303
0942eeee 4304/**
72ec7029 4305 * css_task_iter_next - return the next task for the iterator
0942eeee
TH
4306 * @it: the task iterator being iterated
4307 *
4308 * The "next" function for task iteration. @it should have been
72ec7029
TH
4309 * initialized via css_task_iter_start(). Returns NULL when the iteration
4310 * reaches the end.
0942eeee 4311 */
72ec7029 4312struct task_struct *css_task_iter_next(struct css_task_iter *it)
817929ec 4313{
d5745675 4314 if (it->cur_task) {
ed27b9f7 4315 put_task_struct(it->cur_task);
d5745675
TH
4316 it->cur_task = NULL;
4317 }
ed27b9f7 4318
82d6489d 4319 spin_lock_irq(&css_set_lock);
ed27b9f7 4320
d5745675
TH
4321 if (it->task_pos) {
4322 it->cur_task = list_entry(it->task_pos, struct task_struct,
4323 cg_list);
4324 get_task_struct(it->cur_task);
4325 css_task_iter_advance(it);
4326 }
ed27b9f7 4327
82d6489d 4328 spin_unlock_irq(&css_set_lock);
ed27b9f7
TH
4329
4330 return it->cur_task;
817929ec
PM
4331}
4332
0942eeee 4333/**
72ec7029 4334 * css_task_iter_end - finish task iteration
0942eeee
TH
4335 * @it: the task iterator to finish
4336 *
72ec7029 4337 * Finish task iteration started by css_task_iter_start().
0942eeee 4338 */
72ec7029 4339void css_task_iter_end(struct css_task_iter *it)
31a7df01 4340{
ed27b9f7 4341 if (it->cur_cset) {
82d6489d 4342 spin_lock_irq(&css_set_lock);
ed27b9f7
TH
4343 list_del(&it->iters_node);
4344 put_css_set_locked(it->cur_cset);
82d6489d 4345 spin_unlock_irq(&css_set_lock);
ed27b9f7
TH
4346 }
4347
450ee0c1
TH
4348 if (it->cur_dcset)
4349 put_css_set(it->cur_dcset);
4350
ed27b9f7
TH
4351 if (it->cur_task)
4352 put_task_struct(it->cur_task);
31a7df01
CW
4353}
4354
b4b90a8e 4355static void cgroup_procs_release(struct kernfs_open_file *of)
31a7df01 4356{
b4b90a8e
TH
4357 if (of->priv) {
4358 css_task_iter_end(of->priv);
4359 kfree(of->priv);
4360 }
4361}
6c694c88 4362
b4b90a8e
TH
4363static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4364{
4365 struct kernfs_open_file *of = s->private;
4366 struct css_task_iter *it = of->priv;
31a7df01 4367
bc2fb7ed 4368 return css_task_iter_next(it);
b4b90a8e 4369}
31a7df01 4370
8cfd8147
TH
4371static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
4372 unsigned int iter_flags)
b4b90a8e
TH
4373{
4374 struct kernfs_open_file *of = s->private;
4375 struct cgroup *cgrp = seq_css(s)->cgroup;
4376 struct css_task_iter *it = of->priv;
8cc99345 4377
952aaa12 4378 /*
b4b90a8e
TH
4379 * When a seq_file is seeked, it's always traversed sequentially
4380 * from position 0, so we can simply keep iterating on !0 *pos.
952aaa12 4381 */
b4b90a8e
TH
4382 if (!it) {
4383 if (WARN_ON_ONCE((*pos)++))
4384 return ERR_PTR(-EINVAL);
8cc99345 4385
b4b90a8e
TH
4386 it = kzalloc(sizeof(*it), GFP_KERNEL);
4387 if (!it)
4388 return ERR_PTR(-ENOMEM);
4389 of->priv = it;
450ee0c1 4390 css_task_iter_start(&cgrp->self, iter_flags, it);
b4b90a8e
TH
4391 } else if (!(*pos)++) {
4392 css_task_iter_end(it);
450ee0c1 4393 css_task_iter_start(&cgrp->self, iter_flags, it);
b4b90a8e 4394 }
bbcb81d0 4395
b4b90a8e
TH
4396 return cgroup_procs_next(s, NULL, NULL);
4397}
24528255 4398
8cfd8147
TH
4399static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
4400{
4401 struct cgroup *cgrp = seq_css(s)->cgroup;
4402
4403 /*
4404 * All processes of a threaded subtree belong to the domain cgroup
4405 * of the subtree. Only threads can be distributed across the
4406 * subtree. Reject reads on cgroup.procs in the subtree proper.
4407 * They're always empty anyway.
4408 */
4409 if (cgroup_is_threaded(cgrp))
4410 return ERR_PTR(-EOPNOTSUPP);
4411
4412 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
4413 CSS_TASK_ITER_THREADED);
4414}
4415
b4b90a8e 4416static int cgroup_procs_show(struct seq_file *s, void *v)
bbcb81d0 4417{
bc2fb7ed 4418 seq_printf(s, "%d\n", task_pid_vnr(v));
97978e6d
DL
4419 return 0;
4420}
4421
715c809d
TH
4422static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
4423 struct cgroup *dst_cgrp,
4424 struct super_block *sb)
4425{
4426 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
4427 struct cgroup *com_cgrp = src_cgrp;
4428 struct inode *inode;
4429 int ret;
4430
4431 lockdep_assert_held(&cgroup_mutex);
4432
4433 /* find the common ancestor */
4434 while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
4435 com_cgrp = cgroup_parent(com_cgrp);
4436
4437 /* %current should be authorized to migrate to the common ancestor */
4438 inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn);
4439 if (!inode)
4440 return -ENOMEM;
4441
4442 ret = inode_permission(inode, MAY_WRITE);
4443 iput(inode);
4444 if (ret)
4445 return ret;
4446
4447 /*
4448 * If namespaces are delegation boundaries, %current must be able
4449 * to see both source and destination cgroups from its namespace.
4450 */
4451 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
4452 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
4453 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
4454 return -ENOENT;
4455
4456 return 0;
4457}
4458
4459static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
4460 char *buf, size_t nbytes, loff_t off)
4461{
4462 struct cgroup *src_cgrp, *dst_cgrp;
4463 struct task_struct *task;
4464 ssize_t ret;
4465
4466 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4467 if (!dst_cgrp)
4468 return -ENODEV;
4469
4470 task = cgroup_procs_write_start(buf, true);
4471 ret = PTR_ERR_OR_ZERO(task);
4472 if (ret)
4473 goto out_unlock;
4474
4475 /* find the source cgroup */
4476 spin_lock_irq(&css_set_lock);
4477 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4478 spin_unlock_irq(&css_set_lock);
4479
4480 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4481 of->file->f_path.dentry->d_sb);
4482 if (ret)
4483 goto out_finish;
4484
4485 ret = cgroup_attach_task(dst_cgrp, task, true);
4486
4487out_finish:
4488 cgroup_procs_write_finish(task);
4489out_unlock:
4490 cgroup_kn_unlock(of->kn);
4491
4492 return ret ?: nbytes;
4493}
4494
8cfd8147
TH
4495static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
4496{
4497 return __cgroup_procs_start(s, pos, 0);
4498}
4499
4500static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
4501 char *buf, size_t nbytes, loff_t off)
4502{
4503 struct cgroup *src_cgrp, *dst_cgrp;
4504 struct task_struct *task;
4505 ssize_t ret;
4506
4507 buf = strstrip(buf);
4508
4509 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4510 if (!dst_cgrp)
4511 return -ENODEV;
4512
4513 task = cgroup_procs_write_start(buf, false);
4514 ret = PTR_ERR_OR_ZERO(task);
4515 if (ret)
4516 goto out_unlock;
4517
4518 /* find the source cgroup */
4519 spin_lock_irq(&css_set_lock);
4520 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4521 spin_unlock_irq(&css_set_lock);
4522
4523 /* thread migrations follow the cgroup.procs delegation rule */
4524 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4525 of->file->f_path.dentry->d_sb);
4526 if (ret)
4527 goto out_finish;
4528
4529 /* and must be contained in the same domain */
4530 ret = -EOPNOTSUPP;
4531 if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)
4532 goto out_finish;
4533
4534 ret = cgroup_attach_task(dst_cgrp, task, false);
4535
4536out_finish:
4537 cgroup_procs_write_finish(task);
4538out_unlock:
4539 cgroup_kn_unlock(of->kn);
4540
4541 return ret ?: nbytes;
4542}
4543
a14c6874 4544/* cgroup core interface files for the default hierarchy */
d62beb7f 4545static struct cftype cgroup_base_files[] = {
8cfd8147
TH
4546 {
4547 .name = "cgroup.type",
4548 .flags = CFTYPE_NOT_ON_ROOT,
4549 .seq_show = cgroup_type_show,
4550 .write = cgroup_type_write,
4551 },
81a6a5cd 4552 {
d5c56ced 4553 .name = "cgroup.procs",
5136f636 4554 .flags = CFTYPE_NS_DELEGATABLE,
6f60eade 4555 .file_offset = offsetof(struct cgroup, procs_file),
b4b90a8e
TH
4556 .release = cgroup_procs_release,
4557 .seq_start = cgroup_procs_start,
4558 .seq_next = cgroup_procs_next,
4559 .seq_show = cgroup_procs_show,
acbef755 4560 .write = cgroup_procs_write,
102a775e 4561 },
8cfd8147
TH
4562 {
4563 .name = "cgroup.threads",
4f58424d 4564 .flags = CFTYPE_NS_DELEGATABLE,
8cfd8147
TH
4565 .release = cgroup_procs_release,
4566 .seq_start = cgroup_threads_start,
4567 .seq_next = cgroup_procs_next,
4568 .seq_show = cgroup_procs_show,
4569 .write = cgroup_threads_write,
4570 },
f8f22e53
TH
4571 {
4572 .name = "cgroup.controllers",
f8f22e53
TH
4573 .seq_show = cgroup_controllers_show,
4574 },
4575 {
4576 .name = "cgroup.subtree_control",
5136f636 4577 .flags = CFTYPE_NS_DELEGATABLE,
f8f22e53 4578 .seq_show = cgroup_subtree_control_show,
451af504 4579 .write = cgroup_subtree_control_write,
f8f22e53 4580 },
842b597e 4581 {
4a07c222 4582 .name = "cgroup.events",
a14c6874 4583 .flags = CFTYPE_NOT_ON_ROOT,
6f60eade 4584 .file_offset = offsetof(struct cgroup, events_file),
4a07c222 4585 .seq_show = cgroup_events_show,
842b597e 4586 },
1a926e0b
RG
4587 {
4588 .name = "cgroup.max.descendants",
4589 .seq_show = cgroup_max_descendants_show,
4590 .write = cgroup_max_descendants_write,
4591 },
4592 {
4593 .name = "cgroup.max.depth",
4594 .seq_show = cgroup_max_depth_show,
4595 .write = cgroup_max_depth_write,
4596 },
ec39225c
RG
4597 {
4598 .name = "cgroup.stat",
3e48930c 4599 .seq_show = cgroup_stat_show,
ec39225c 4600 },
d41bf8c9
TH
4601 {
4602 .name = "cpu.stat",
4603 .flags = CFTYPE_NOT_ON_ROOT,
4604 .seq_show = cpu_stat_show,
4605 },
2ce7135a
JW
4606#ifdef CONFIG_PSI
4607 {
4608 .name = "io.pressure",
4609 .flags = CFTYPE_NOT_ON_ROOT,
4610 .seq_show = cgroup_io_pressure_show,
4611 },
4612 {
4613 .name = "memory.pressure",
4614 .flags = CFTYPE_NOT_ON_ROOT,
4615 .seq_show = cgroup_memory_pressure_show,
4616 },
4617 {
4618 .name = "cpu.pressure",
4619 .flags = CFTYPE_NOT_ON_ROOT,
4620 .seq_show = cgroup_cpu_pressure_show,
4621 },
4622#endif
a14c6874
TH
4623 { } /* terminate */
4624};
d5c56ced 4625
0c21ead1
TH
4626/*
4627 * css destruction is four-stage process.
4628 *
4629 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4630 * Implemented in kill_css().
4631 *
4632 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
ec903c0c
TH
4633 * and thus css_tryget_online() is guaranteed to fail, the css can be
4634 * offlined by invoking offline_css(). After offlining, the base ref is
4635 * put. Implemented in css_killed_work_fn().
0c21ead1
TH
4636 *
4637 * 3. When the percpu_ref reaches zero, the only possible remaining
4638 * accessors are inside RCU read sections. css_release() schedules the
4639 * RCU callback.
4640 *
4641 * 4. After the grace period, the css can be freed. Implemented in
4642 * css_free_work_fn().
4643 *
4644 * It is actually hairier because both step 2 and 4 require process context
4645 * and thus involve punting to css->destroy_work adding two additional
4646 * steps to the already complex sequence.
4647 */
8f36aaec 4648static void css_free_rwork_fn(struct work_struct *work)
48ddbe19 4649{
8f36aaec
TH
4650 struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
4651 struct cgroup_subsys_state, destroy_rwork);
01e58659 4652 struct cgroup_subsys *ss = css->ss;
0c21ead1 4653 struct cgroup *cgrp = css->cgroup;
48ddbe19 4654
9a1049da
TH
4655 percpu_ref_exit(&css->refcnt);
4656
01e58659 4657 if (ss) {
9d755d33 4658 /* css free path */
8bb5ef79 4659 struct cgroup_subsys_state *parent = css->parent;
01e58659
VD
4660 int id = css->id;
4661
01e58659
VD
4662 ss->css_free(css);
4663 cgroup_idr_remove(&ss->css_idr, id);
9d755d33 4664 cgroup_put(cgrp);
8bb5ef79
TH
4665
4666 if (parent)
4667 css_put(parent);
9d755d33
TH
4668 } else {
4669 /* cgroup free path */
4670 atomic_dec(&cgrp->root->nr_cgrps);
d62beb7f 4671 cgroup1_pidlist_destroy_all(cgrp);
971ff493 4672 cancel_work_sync(&cgrp->release_agent_work);
9d755d33 4673
d51f39b0 4674 if (cgroup_parent(cgrp)) {
9d755d33
TH
4675 /*
4676 * We get a ref to the parent, and put the ref when
4677 * this cgroup is being freed, so it's guaranteed
4678 * that the parent won't be destroyed before its
4679 * children.
4680 */
d51f39b0 4681 cgroup_put(cgroup_parent(cgrp));
9d755d33 4682 kernfs_put(cgrp->kn);
2ce7135a 4683 psi_cgroup_free(cgrp);
041cd640 4684 if (cgroup_on_dfl(cgrp))
c58632b3 4685 cgroup_rstat_exit(cgrp);
9d755d33
TH
4686 kfree(cgrp);
4687 } else {
4688 /*
4689 * This is root cgroup's refcnt reaching zero,
4690 * which indicates that the root should be
4691 * released.
4692 */
4693 cgroup_destroy_root(cgrp->root);
4694 }
4695 }
48ddbe19
TH
4696}
4697
25e15d83 4698static void css_release_work_fn(struct work_struct *work)
d3daf28d
TH
4699{
4700 struct cgroup_subsys_state *css =
25e15d83 4701 container_of(work, struct cgroup_subsys_state, destroy_work);
15a4c835 4702 struct cgroup_subsys *ss = css->ss;
9d755d33 4703 struct cgroup *cgrp = css->cgroup;
15a4c835 4704
1fed1b2e
TH
4705 mutex_lock(&cgroup_mutex);
4706
de3f0341 4707 css->flags |= CSS_RELEASED;
1fed1b2e
TH
4708 list_del_rcu(&css->sibling);
4709
9d755d33
TH
4710 if (ss) {
4711 /* css release path */
8f53470b
TH
4712 if (!list_empty(&css->rstat_css_node)) {
4713 cgroup_rstat_flush(cgrp);
4714 list_del_rcu(&css->rstat_css_node);
4715 }
4716
01e58659 4717 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
7d172cc8
TH
4718 if (ss->css_released)
4719 ss->css_released(css);
9d755d33 4720 } else {
0679dee0
RG
4721 struct cgroup *tcgrp;
4722
9d755d33 4723 /* cgroup release path */
e4f8d81c 4724 TRACE_CGROUP_PATH(release, cgrp);
ed1777de 4725
041cd640 4726 if (cgroup_on_dfl(cgrp))
c58632b3 4727 cgroup_rstat_flush(cgrp);
041cd640 4728
0679dee0
RG
4729 for (tcgrp = cgroup_parent(cgrp); tcgrp;
4730 tcgrp = cgroup_parent(tcgrp))
4731 tcgrp->nr_dying_descendants--;
4732
9d755d33
TH
4733 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4734 cgrp->id = -1;
a4189487
LZ
4735
4736 /*
4737 * There are two control paths which try to determine
4738 * cgroup from dentry without going through kernfs -
4739 * cgroupstats_build() and css_tryget_online_from_dir().
4740 * Those are supported by RCU protecting clearing of
4741 * cgrp->kn->priv backpointer.
4742 */
6cd0f5bb
TH
4743 if (cgrp->kn)
4744 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4745 NULL);
30070984
DM
4746
4747 cgroup_bpf_put(cgrp);
9d755d33 4748 }
d3daf28d 4749
1fed1b2e
TH
4750 mutex_unlock(&cgroup_mutex);
4751
8f36aaec
TH
4752 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4753 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
d3daf28d
TH
4754}
4755
d3daf28d
TH
4756static void css_release(struct percpu_ref *ref)
4757{
4758 struct cgroup_subsys_state *css =
4759 container_of(ref, struct cgroup_subsys_state, refcnt);
4760
25e15d83
TH
4761 INIT_WORK(&css->destroy_work, css_release_work_fn);
4762 queue_work(cgroup_destroy_wq, &css->destroy_work);
d3daf28d
TH
4763}
4764
ddfcadab
TH
4765static void init_and_link_css(struct cgroup_subsys_state *css,
4766 struct cgroup_subsys *ss, struct cgroup *cgrp)
ddbcc7e8 4767{
0cb51d71
TH
4768 lockdep_assert_held(&cgroup_mutex);
4769
a590b90d 4770 cgroup_get_live(cgrp);
ddfcadab 4771
d5c419b6 4772 memset(css, 0, sizeof(*css));
bd89aabc 4773 css->cgroup = cgrp;
72c97e54 4774 css->ss = ss;
8fa3b8d6 4775 css->id = -1;
d5c419b6
TH
4776 INIT_LIST_HEAD(&css->sibling);
4777 INIT_LIST_HEAD(&css->children);
8f53470b 4778 INIT_LIST_HEAD(&css->rstat_css_node);
0cb51d71 4779 css->serial_nr = css_serial_nr_next++;
aa226ff4 4780 atomic_set(&css->online_cnt, 0);
0ae78e0b 4781
d51f39b0
TH
4782 if (cgroup_parent(cgrp)) {
4783 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
ddfcadab 4784 css_get(css->parent);
ddfcadab 4785 }
48ddbe19 4786
8f53470b
TH
4787 if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush)
4788 list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
4789
ca8bdcaf 4790 BUG_ON(cgroup_css(cgrp, ss));
ddbcc7e8
PM
4791}
4792
2a4ac633 4793/* invoke ->css_online() on a new CSS and mark it online if successful */
623f926b 4794static int online_css(struct cgroup_subsys_state *css)
a31f2d3f 4795{
623f926b 4796 struct cgroup_subsys *ss = css->ss;
b1929db4
TH
4797 int ret = 0;
4798
a31f2d3f
TH
4799 lockdep_assert_held(&cgroup_mutex);
4800
92fb9748 4801 if (ss->css_online)
eb95419b 4802 ret = ss->css_online(css);
ae7f164a 4803 if (!ret) {
eb95419b 4804 css->flags |= CSS_ONLINE;
aec25020 4805 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
aa226ff4
TH
4806
4807 atomic_inc(&css->online_cnt);
4808 if (css->parent)
4809 atomic_inc(&css->parent->online_cnt);
ae7f164a 4810 }
b1929db4 4811 return ret;
a31f2d3f
TH
4812}
4813
2a4ac633 4814/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
623f926b 4815static void offline_css(struct cgroup_subsys_state *css)
a31f2d3f 4816{
623f926b 4817 struct cgroup_subsys *ss = css->ss;
a31f2d3f
TH
4818
4819 lockdep_assert_held(&cgroup_mutex);
4820
4821 if (!(css->flags & CSS_ONLINE))
4822 return;
4823
d7eeac19 4824 if (ss->css_offline)
eb95419b 4825 ss->css_offline(css);
a31f2d3f 4826
eb95419b 4827 css->flags &= ~CSS_ONLINE;
e3297803 4828 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
f8f22e53
TH
4829
4830 wake_up_all(&css->cgroup->offline_waitq);
a31f2d3f
TH
4831}
4832
c81c925a 4833/**
6cd0f5bb 4834 * css_create - create a cgroup_subsys_state
c81c925a
TH
4835 * @cgrp: the cgroup new css will be associated with
4836 * @ss: the subsys of new css
4837 *
4838 * Create a new css associated with @cgrp - @ss pair. On success, the new
6cd0f5bb
TH
4839 * css is online and installed in @cgrp. This function doesn't create the
4840 * interface files. Returns 0 on success, -errno on failure.
c81c925a 4841 */
6cd0f5bb
TH
4842static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4843 struct cgroup_subsys *ss)
c81c925a 4844{
d51f39b0 4845 struct cgroup *parent = cgroup_parent(cgrp);
1fed1b2e 4846 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
c81c925a
TH
4847 struct cgroup_subsys_state *css;
4848 int err;
4849
c81c925a
TH
4850 lockdep_assert_held(&cgroup_mutex);
4851
1fed1b2e 4852 css = ss->css_alloc(parent_css);
e7e15b87
TH
4853 if (!css)
4854 css = ERR_PTR(-ENOMEM);
c81c925a 4855 if (IS_ERR(css))
6cd0f5bb 4856 return css;
c81c925a 4857
ddfcadab 4858 init_and_link_css(css, ss, cgrp);
a2bed820 4859
2aad2a86 4860 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
c81c925a 4861 if (err)
3eb59ec6 4862 goto err_free_css;
c81c925a 4863
cf780b7d 4864 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
15a4c835 4865 if (err < 0)
b00c52da 4866 goto err_free_css;
15a4c835 4867 css->id = err;
c81c925a 4868
15a4c835 4869 /* @css is ready to be brought online now, make it visible */
1fed1b2e 4870 list_add_tail_rcu(&css->sibling, &parent_css->children);
15a4c835 4871 cgroup_idr_replace(&ss->css_idr, css, css->id);
c81c925a
TH
4872
4873 err = online_css(css);
4874 if (err)
1fed1b2e 4875 goto err_list_del;
94419627 4876
c81c925a 4877 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
d51f39b0 4878 cgroup_parent(parent)) {
ed3d261b 4879 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 4880 current->comm, current->pid, ss->name);
c81c925a 4881 if (!strcmp(ss->name, "memory"))
ed3d261b 4882 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
c81c925a
TH
4883 ss->warned_broken_hierarchy = true;
4884 }
4885
6cd0f5bb 4886 return css;
c81c925a 4887
1fed1b2e
TH
4888err_list_del:
4889 list_del_rcu(&css->sibling);
3eb59ec6 4890err_free_css:
8f53470b 4891 list_del_rcu(&css->rstat_css_node);
8f36aaec
TH
4892 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4893 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
6cd0f5bb 4894 return ERR_PTR(err);
c81c925a
TH
4895}
4896
07cd1294
TH
4897/*
4898 * The returned cgroup is fully initialized including its control mask, but
4899 * it isn't associated with its kernfs_node and doesn't have the control
4900 * mask applied.
4901 */
a5bca215 4902static struct cgroup *cgroup_create(struct cgroup *parent)
ddbcc7e8 4903{
a5bca215 4904 struct cgroup_root *root = parent->root;
a5bca215
TH
4905 struct cgroup *cgrp, *tcgrp;
4906 int level = parent->level + 1;
03970d3c 4907 int ret;
ddbcc7e8 4908
0a950f65 4909 /* allocate the cgroup and its ID, 0 is reserved for the root */
acafe7e3
KC
4910 cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)),
4911 GFP_KERNEL);
a5bca215
TH
4912 if (!cgrp)
4913 return ERR_PTR(-ENOMEM);
0ab02ca8 4914
2aad2a86 4915 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
9d755d33
TH
4916 if (ret)
4917 goto out_free_cgrp;
4918
041cd640 4919 if (cgroup_on_dfl(parent)) {
c58632b3 4920 ret = cgroup_rstat_init(cgrp);
041cd640
TH
4921 if (ret)
4922 goto out_cancel_ref;
4923 }
4924
0ab02ca8
LZ
4925 /*
4926 * Temporarily set the pointer to NULL, so idr_find() won't return
4927 * a half-baked cgroup.
4928 */
cf780b7d 4929 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
0ab02ca8 4930 if (cgrp->id < 0) {
ba0f4d76 4931 ret = -ENOMEM;
041cd640 4932 goto out_stat_exit;
976c06bc
TH
4933 }
4934
cc31edce 4935 init_cgroup_housekeeping(cgrp);
ddbcc7e8 4936
9d800df1 4937 cgrp->self.parent = &parent->self;
ba0f4d76 4938 cgrp->root = root;
b11cfb58 4939 cgrp->level = level;
2ce7135a
JW
4940
4941 ret = psi_cgroup_alloc(cgrp);
324bda9e
AS
4942 if (ret)
4943 goto out_idr_free;
b11cfb58 4944
2ce7135a
JW
4945 ret = cgroup_bpf_inherit(cgrp);
4946 if (ret)
4947 goto out_psi_free;
4948
0679dee0 4949 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
b11cfb58 4950 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
ddbcc7e8 4951
0679dee0
RG
4952 if (tcgrp != cgrp)
4953 tcgrp->nr_descendants++;
4954 }
4955
b6abdb0e
LZ
4956 if (notify_on_release(parent))
4957 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4958
2260e7fc
TH
4959 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4960 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
97978e6d 4961
0cb51d71 4962 cgrp->self.serial_nr = css_serial_nr_next++;
53fa5261 4963
4e139afc 4964 /* allocation complete, commit to creation */
d5c419b6 4965 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
3c9c825b 4966 atomic_inc(&root->nr_cgrps);
a590b90d 4967 cgroup_get_live(parent);
415cf07a 4968
0d80255e
TH
4969 /*
4970 * @cgrp is now fully operational. If something fails after this
4971 * point, it'll be released via the normal destruction path.
4972 */
6fa4918d 4973 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4e96ee8e 4974
bd53d617
TH
4975 /*
4976 * On the default hierarchy, a child doesn't automatically inherit
667c2491 4977 * subtree_control from the parent. Each is configured manually.
bd53d617 4978 */
03970d3c 4979 if (!cgroup_on_dfl(cgrp))
5531dc91 4980 cgrp->subtree_control = cgroup_control(cgrp);
03970d3c
TH
4981
4982 cgroup_propagate_control(cgrp);
4983
a5bca215
TH
4984 return cgrp;
4985
2ce7135a
JW
4986out_psi_free:
4987 psi_cgroup_free(cgrp);
324bda9e
AS
4988out_idr_free:
4989 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
041cd640
TH
4990out_stat_exit:
4991 if (cgroup_on_dfl(parent))
c58632b3 4992 cgroup_rstat_exit(cgrp);
a5bca215
TH
4993out_cancel_ref:
4994 percpu_ref_exit(&cgrp->self.refcnt);
4995out_free_cgrp:
4996 kfree(cgrp);
4997 return ERR_PTR(ret);
a5bca215
TH
4998}
4999
1a926e0b
RG
5000static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
5001{
5002 struct cgroup *cgroup;
5003 int ret = false;
5004 int level = 1;
5005
5006 lockdep_assert_held(&cgroup_mutex);
5007
5008 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
5009 if (cgroup->nr_descendants >= cgroup->max_descendants)
5010 goto fail;
5011
5012 if (level > cgroup->max_depth)
5013 goto fail;
5014
5015 level++;
5016 }
5017
5018 ret = true;
5019fail:
5020 return ret;
5021}
5022
1592c9b2 5023int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
a5bca215
TH
5024{
5025 struct cgroup *parent, *cgrp;
a5bca215 5026 struct kernfs_node *kn;
03970d3c 5027 int ret;
a5bca215
TH
5028
5029 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5030 if (strchr(name, '\n'))
5031 return -EINVAL;
5032
945ba199 5033 parent = cgroup_kn_lock_live(parent_kn, false);
a5bca215
TH
5034 if (!parent)
5035 return -ENODEV;
5036
1a926e0b
RG
5037 if (!cgroup_check_hierarchy_limits(parent)) {
5038 ret = -EAGAIN;
5039 goto out_unlock;
5040 }
5041
a5bca215
TH
5042 cgrp = cgroup_create(parent);
5043 if (IS_ERR(cgrp)) {
5044 ret = PTR_ERR(cgrp);
5045 goto out_unlock;
5046 }
5047
195e9b6c
TH
5048 /* create the directory */
5049 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5050 if (IS_ERR(kn)) {
5051 ret = PTR_ERR(kn);
5052 goto out_destroy;
5053 }
5054 cgrp->kn = kn;
5055
5056 /*
5057 * This extra ref will be put in cgroup_free_fn() and guarantees
5058 * that @cgrp->kn is always accessible.
5059 */
5060 kernfs_get(kn);
5061
5062 ret = cgroup_kn_set_ugid(kn);
5063 if (ret)
5064 goto out_destroy;
5065
334c3679 5066 ret = css_populate_dir(&cgrp->self);
195e9b6c
TH
5067 if (ret)
5068 goto out_destroy;
5069
03970d3c
TH
5070 ret = cgroup_apply_control_enable(cgrp);
5071 if (ret)
5072 goto out_destroy;
195e9b6c 5073
e4f8d81c 5074 TRACE_CGROUP_PATH(mkdir, cgrp);
ed1777de 5075
195e9b6c 5076 /* let's create and online css's */
2bd59d48 5077 kernfs_activate(kn);
ddbcc7e8 5078
ba0f4d76
TH
5079 ret = 0;
5080 goto out_unlock;
ddbcc7e8 5081
a5bca215
TH
5082out_destroy:
5083 cgroup_destroy_locked(cgrp);
ba0f4d76 5084out_unlock:
a9746d8d 5085 cgroup_kn_unlock(parent_kn);
ba0f4d76 5086 return ret;
ddbcc7e8
PM
5087}
5088
223dbc38
TH
5089/*
5090 * This is called when the refcnt of a css is confirmed to be killed.
249f3468
TH
5091 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5092 * initate destruction and put the css ref from kill_css().
223dbc38
TH
5093 */
5094static void css_killed_work_fn(struct work_struct *work)
d3daf28d 5095{
223dbc38
TH
5096 struct cgroup_subsys_state *css =
5097 container_of(work, struct cgroup_subsys_state, destroy_work);
d3daf28d 5098
f20104de 5099 mutex_lock(&cgroup_mutex);
09a503ea 5100
aa226ff4
TH
5101 do {
5102 offline_css(css);
5103 css_put(css);
5104 /* @css can't go away while we're holding cgroup_mutex */
5105 css = css->parent;
5106 } while (css && atomic_dec_and_test(&css->online_cnt));
5107
5108 mutex_unlock(&cgroup_mutex);
d3daf28d
TH
5109}
5110
223dbc38
TH
5111/* css kill confirmation processing requires process context, bounce */
5112static void css_killed_ref_fn(struct percpu_ref *ref)
d3daf28d
TH
5113{
5114 struct cgroup_subsys_state *css =
5115 container_of(ref, struct cgroup_subsys_state, refcnt);
5116
aa226ff4
TH
5117 if (atomic_dec_and_test(&css->online_cnt)) {
5118 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5119 queue_work(cgroup_destroy_wq, &css->destroy_work);
5120 }
d3daf28d
TH
5121}
5122
f392e51c
TH
5123/**
5124 * kill_css - destroy a css
5125 * @css: css to destroy
5126 *
5127 * This function initiates destruction of @css by removing cgroup interface
5128 * files and putting its base reference. ->css_offline() will be invoked
ec903c0c
TH
5129 * asynchronously once css_tryget_online() is guaranteed to fail and when
5130 * the reference count reaches zero, @css will be released.
f392e51c
TH
5131 */
5132static void kill_css(struct cgroup_subsys_state *css)
edae0c33 5133{
01f6474c 5134 lockdep_assert_held(&cgroup_mutex);
94419627 5135
33c35aa4
WL
5136 if (css->flags & CSS_DYING)
5137 return;
5138
5139 css->flags |= CSS_DYING;
5140
2bd59d48
TH
5141 /*
5142 * This must happen before css is disassociated with its cgroup.
5143 * See seq_css() for details.
5144 */
334c3679 5145 css_clear_dir(css);
3c14f8b4 5146
edae0c33
TH
5147 /*
5148 * Killing would put the base ref, but we need to keep it alive
5149 * until after ->css_offline().
5150 */
5151 css_get(css);
5152
5153 /*
5154 * cgroup core guarantees that, by the time ->css_offline() is
5155 * invoked, no new css reference will be given out via
ec903c0c 5156 * css_tryget_online(). We can't simply call percpu_ref_kill() and
edae0c33
TH
5157 * proceed to offlining css's because percpu_ref_kill() doesn't
5158 * guarantee that the ref is seen as killed on all CPUs on return.
5159 *
5160 * Use percpu_ref_kill_and_confirm() to get notifications as each
5161 * css is confirmed to be seen as killed on all CPUs.
5162 */
5163 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
d3daf28d
TH
5164}
5165
5166/**
5167 * cgroup_destroy_locked - the first stage of cgroup destruction
5168 * @cgrp: cgroup to be destroyed
5169 *
5170 * css's make use of percpu refcnts whose killing latency shouldn't be
5171 * exposed to userland and are RCU protected. Also, cgroup core needs to
ec903c0c
TH
5172 * guarantee that css_tryget_online() won't succeed by the time
5173 * ->css_offline() is invoked. To satisfy all the requirements,
5174 * destruction is implemented in the following two steps.
d3daf28d
TH
5175 *
5176 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5177 * userland visible parts and start killing the percpu refcnts of
5178 * css's. Set up so that the next stage will be kicked off once all
5179 * the percpu refcnts are confirmed to be killed.
5180 *
5181 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5182 * rest of destruction. Once all cgroup references are gone, the
5183 * cgroup is RCU-freed.
5184 *
5185 * This function implements s1. After this step, @cgrp is gone as far as
5186 * the userland is concerned and a new cgroup with the same name may be
5187 * created. As cgroup doesn't care about the names internally, this
5188 * doesn't cause any problem.
5189 */
42809dd4
TH
5190static int cgroup_destroy_locked(struct cgroup *cgrp)
5191 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
ddbcc7e8 5192{
0679dee0 5193 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
2bd59d48 5194 struct cgroup_subsys_state *css;
2b021cbf 5195 struct cgrp_cset_link *link;
1c6727af 5196 int ssid;
ddbcc7e8 5197
42809dd4
TH
5198 lockdep_assert_held(&cgroup_mutex);
5199
91486f61
TH
5200 /*
5201 * Only migration can raise populated from zero and we're already
5202 * holding cgroup_mutex.
5203 */
5204 if (cgroup_is_populated(cgrp))
ddbcc7e8 5205 return -EBUSY;
a043e3b2 5206
bb78a92f 5207 /*
d5c419b6
TH
5208 * Make sure there's no live children. We can't test emptiness of
5209 * ->self.children as dead children linger on it while being
5210 * drained; otherwise, "rmdir parent/child parent" may fail.
bb78a92f 5211 */
f3d46500 5212 if (css_has_online_children(&cgrp->self))
bb78a92f
HD
5213 return -EBUSY;
5214
455050d2 5215 /*
2b021cbf
TH
5216 * Mark @cgrp and the associated csets dead. The former prevents
5217 * further task migration and child creation by disabling
5218 * cgroup_lock_live_group(). The latter makes the csets ignored by
5219 * the migration path.
455050d2 5220 */
184faf32 5221 cgrp->self.flags &= ~CSS_ONLINE;
ddbcc7e8 5222
82d6489d 5223 spin_lock_irq(&css_set_lock);
2b021cbf
TH
5224 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5225 link->cset->dead = true;
82d6489d 5226 spin_unlock_irq(&css_set_lock);
2b021cbf 5227
249f3468 5228 /* initiate massacre of all css's */
1c6727af
TH
5229 for_each_css(css, ssid, cgrp)
5230 kill_css(css);
455050d2 5231
5faaf05f
TH
5232 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5233 css_clear_dir(&cgrp->self);
01f6474c 5234 kernfs_remove(cgrp->kn);
f20104de 5235
454000ad
TH
5236 if (parent && cgroup_is_threaded(cgrp))
5237 parent->nr_threaded_children--;
5238
0679dee0
RG
5239 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
5240 tcgrp->nr_descendants--;
5241 tcgrp->nr_dying_descendants++;
5242 }
5243
5a621e6c 5244 cgroup1_check_for_release(parent);
2bd59d48 5245
249f3468 5246 /* put the base reference */
9d755d33 5247 percpu_ref_kill(&cgrp->self.refcnt);
455050d2 5248
ea15f8cc
TH
5249 return 0;
5250};
5251
1592c9b2 5252int cgroup_rmdir(struct kernfs_node *kn)
42809dd4 5253{
a9746d8d 5254 struct cgroup *cgrp;
2bd59d48 5255 int ret = 0;
42809dd4 5256
945ba199 5257 cgrp = cgroup_kn_lock_live(kn, false);
a9746d8d
TH
5258 if (!cgrp)
5259 return 0;
42809dd4 5260
a9746d8d 5261 ret = cgroup_destroy_locked(cgrp);
ed1777de 5262 if (!ret)
e4f8d81c 5263 TRACE_CGROUP_PATH(rmdir, cgrp);
ed1777de 5264
a9746d8d 5265 cgroup_kn_unlock(kn);
42809dd4 5266 return ret;
8e3f6541
TH
5267}
5268
2bd59d48 5269static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5136f636 5270 .show_options = cgroup_show_options,
2bd59d48 5271 .remount_fs = cgroup_remount,
2bd59d48
TH
5272 .mkdir = cgroup_mkdir,
5273 .rmdir = cgroup_rmdir,
4f41fc59 5274 .show_path = cgroup_show_path,
2bd59d48
TH
5275};
5276
15a4c835 5277static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
ddbcc7e8 5278{
ddbcc7e8 5279 struct cgroup_subsys_state *css;
cfe36bde 5280
a5ae9899 5281 pr_debug("Initializing cgroup subsys %s\n", ss->name);
ddbcc7e8 5282
648bb56d
TH
5283 mutex_lock(&cgroup_mutex);
5284
15a4c835 5285 idr_init(&ss->css_idr);
0adb0704 5286 INIT_LIST_HEAD(&ss->cfts);
8e3f6541 5287
3dd06ffa
TH
5288 /* Create the root cgroup state for this subsystem */
5289 ss->root = &cgrp_dfl_root;
5290 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
ddbcc7e8
PM
5291 /* We don't handle early failures gracefully */
5292 BUG_ON(IS_ERR(css));
ddfcadab 5293 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
3b514d24
TH
5294
5295 /*
5296 * Root csses are never destroyed and we can't initialize
5297 * percpu_ref during early init. Disable refcnting.
5298 */
5299 css->flags |= CSS_NO_REF;
5300
15a4c835 5301 if (early) {
9395a450 5302 /* allocation can't be done safely during early init */
15a4c835
TH
5303 css->id = 1;
5304 } else {
5305 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5306 BUG_ON(css->id < 0);
5307 }
ddbcc7e8 5308
e8d55fde 5309 /* Update the init_css_set to contain a subsys
817929ec 5310 * pointer to this state - since the subsystem is
e8d55fde 5311 * newly registered, all tasks and hence the
3dd06ffa 5312 * init_css_set is in the subsystem's root cgroup. */
aec25020 5313 init_css_set.subsys[ss->id] = css;
ddbcc7e8 5314
cb4a3167
AS
5315 have_fork_callback |= (bool)ss->fork << ss->id;
5316 have_exit_callback |= (bool)ss->exit << ss->id;
afcf6c8b 5317 have_free_callback |= (bool)ss->free << ss->id;
7e47682e 5318 have_canfork_callback |= (bool)ss->can_fork << ss->id;
ddbcc7e8 5319
e8d55fde
LZ
5320 /* At system boot, before all subsystems have been
5321 * registered, no tasks have been forked, so we don't
5322 * need to invoke fork callbacks here. */
5323 BUG_ON(!list_empty(&init_task.tasks));
5324
ae7f164a 5325 BUG_ON(online_css(css));
a8638030 5326
cf5d5941
BB
5327 mutex_unlock(&cgroup_mutex);
5328}
cf5d5941 5329
ddbcc7e8 5330/**
a043e3b2
LZ
5331 * cgroup_init_early - cgroup initialization at system boot
5332 *
5333 * Initialize cgroups at system boot, and initialize any
5334 * subsystems that request early init.
ddbcc7e8
PM
5335 */
5336int __init cgroup_init_early(void)
5337{
7b9a6ba5 5338 static struct cgroup_sb_opts __initdata opts;
30159ec7 5339 struct cgroup_subsys *ss;
ddbcc7e8 5340 int i;
30159ec7 5341
3dd06ffa 5342 init_cgroup_root(&cgrp_dfl_root, &opts);
3b514d24
TH
5343 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5344
a4ea1cc9 5345 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
817929ec 5346
3ed80a62 5347 for_each_subsys(ss, i) {
aec25020 5348 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
63253ad8 5349 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
073219e9 5350 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
aec25020 5351 ss->id, ss->name);
073219e9
TH
5352 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5353 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
5354
aec25020 5355 ss->id = i;
073219e9 5356 ss->name = cgroup_subsys_name[i];
3e1d2eed
TH
5357 if (!ss->legacy_name)
5358 ss->legacy_name = cgroup_subsys_name[i];
ddbcc7e8
PM
5359
5360 if (ss->early_init)
15a4c835 5361 cgroup_init_subsys(ss, true);
ddbcc7e8
PM
5362 }
5363 return 0;
5364}
5365
6e5c8307 5366static u16 cgroup_disable_mask __initdata;
a3e72739 5367
ddbcc7e8 5368/**
a043e3b2
LZ
5369 * cgroup_init - cgroup initialization
5370 *
5371 * Register cgroup filesystem and /proc file, and initialize
5372 * any subsystems that didn't request early init.
ddbcc7e8
PM
5373 */
5374int __init cgroup_init(void)
5375{
30159ec7 5376 struct cgroup_subsys *ss;
035f4f51 5377 int ssid;
ddbcc7e8 5378
6e5c8307 5379 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
1ed13287 5380 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
d62beb7f
TH
5381 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
5382 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
ddbcc7e8 5383
c58632b3 5384 cgroup_rstat_boot();
041cd640 5385
3942a9bd 5386 /*
2af3024c 5387 * The latency of the synchronize_rcu() is too high for cgroups,
3942a9bd
PZ
5388 * avoid it at the cost of forcing all readers into the slow path.
5389 */
5390 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5391
a79a908f
AK
5392 get_user_ns(init_cgroup_ns.user_ns);
5393
54e7b4eb 5394 mutex_lock(&cgroup_mutex);
54e7b4eb 5395
2378d8b8
TH
5396 /*
5397 * Add init_css_set to the hash table so that dfl_root can link to
5398 * it during init.
5399 */
5400 hash_add(css_set_table, &init_css_set.hlist,
5401 css_set_hash(init_css_set.subsys));
82fe9b0d 5402
35ac1184 5403 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
4e96ee8e 5404
54e7b4eb
TH
5405 mutex_unlock(&cgroup_mutex);
5406
172a2c06 5407 for_each_subsys(ss, ssid) {
15a4c835
TH
5408 if (ss->early_init) {
5409 struct cgroup_subsys_state *css =
5410 init_css_set.subsys[ss->id];
5411
5412 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5413 GFP_KERNEL);
5414 BUG_ON(css->id < 0);
5415 } else {
5416 cgroup_init_subsys(ss, false);
5417 }
172a2c06 5418
2d8f243a
TH
5419 list_add_tail(&init_css_set.e_cset_node[ssid],
5420 &cgrp_dfl_root.cgrp.e_csets[ssid]);
172a2c06
TH
5421
5422 /*
c731ae1d
LZ
5423 * Setting dfl_root subsys_mask needs to consider the
5424 * disabled flag and cftype registration needs kmalloc,
5425 * both of which aren't available during early_init.
172a2c06 5426 */
a3e72739
TH
5427 if (cgroup_disable_mask & (1 << ssid)) {
5428 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5429 printk(KERN_INFO "Disabling %s control group subsystem\n",
5430 ss->name);
a8ddc821 5431 continue;
a3e72739 5432 }
a8ddc821 5433
d62beb7f 5434 if (cgroup1_ssid_disabled(ssid))
223ffb29
JW
5435 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5436 ss->name);
5437
a8ddc821
TH
5438 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5439
8cfd8147
TH
5440 /* implicit controllers must be threaded too */
5441 WARN_ON(ss->implicit_on_dfl && !ss->threaded);
5442
f6d635ad
TH
5443 if (ss->implicit_on_dfl)
5444 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5445 else if (!ss->dfl_cftypes)
a7165264 5446 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
5de4fa13 5447
8cfd8147
TH
5448 if (ss->threaded)
5449 cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
5450
a8ddc821
TH
5451 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5452 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5453 } else {
5454 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5455 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
c731ae1d 5456 }
295458e6
VD
5457
5458 if (ss->bind)
5459 ss->bind(init_css_set.subsys[ssid]);
7af608e4
TH
5460
5461 mutex_lock(&cgroup_mutex);
5462 css_populate_dir(init_css_set.subsys[ssid]);
5463 mutex_unlock(&cgroup_mutex);
676db4af
GK
5464 }
5465
2378d8b8
TH
5466 /* init_css_set.subsys[] has been updated, re-hash */
5467 hash_del(&init_css_set.hlist);
5468 hash_add(css_set_table, &init_css_set.hlist,
5469 css_set_hash(init_css_set.subsys));
5470
035f4f51
TH
5471 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5472 WARN_ON(register_filesystem(&cgroup_fs_type));
67e9c74b 5473 WARN_ON(register_filesystem(&cgroup2_fs_type));
3f3942ac 5474 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
ddbcc7e8 5475
2bd59d48 5476 return 0;
ddbcc7e8 5477}
b4f48b63 5478
e5fca243
TH
5479static int __init cgroup_wq_init(void)
5480{
5481 /*
5482 * There isn't much point in executing destruction path in
5483 * parallel. Good chunk is serialized with cgroup_mutex anyway.
1a11533f 5484 * Use 1 for @max_active.
e5fca243
TH
5485 *
5486 * We would prefer to do this in cgroup_init() above, but that
5487 * is called before init_workqueues(): so leave this until after.
5488 */
1a11533f 5489 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
e5fca243
TH
5490 BUG_ON(!cgroup_destroy_wq);
5491 return 0;
5492}
5493core_initcall(cgroup_wq_init);
5494
69fd5c39
SL
5495void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
5496 char *buf, size_t buflen)
5497{
5498 struct kernfs_node *kn;
5499
5500 kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id);
5501 if (!kn)
5502 return;
5503 kernfs_path(kn, buf, buflen);
5504 kernfs_put(kn);
5505}
5506
a424316c
PM
5507/*
5508 * proc_cgroup_show()
5509 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5510 * - Used for /proc/<pid>/cgroup.
a424316c 5511 */
006f4ac4
ZL
5512int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5513 struct pid *pid, struct task_struct *tsk)
a424316c 5514{
4c737b41 5515 char *buf;
a424316c 5516 int retval;
3dd06ffa 5517 struct cgroup_root *root;
a424316c
PM
5518
5519 retval = -ENOMEM;
e61734c5 5520 buf = kmalloc(PATH_MAX, GFP_KERNEL);
a424316c
PM
5521 if (!buf)
5522 goto out;
5523
a424316c 5524 mutex_lock(&cgroup_mutex);
82d6489d 5525 spin_lock_irq(&css_set_lock);
a424316c 5526
985ed670 5527 for_each_root(root) {
a424316c 5528 struct cgroup_subsys *ss;
bd89aabc 5529 struct cgroup *cgrp;
b85d2040 5530 int ssid, count = 0;
a424316c 5531
a7165264 5532 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
985ed670
TH
5533 continue;
5534
2c6ab6d2 5535 seq_printf(m, "%d:", root->hierarchy_id);
d98817d4
TH
5536 if (root != &cgrp_dfl_root)
5537 for_each_subsys(ss, ssid)
5538 if (root->subsys_mask & (1 << ssid))
5539 seq_printf(m, "%s%s", count++ ? "," : "",
3e1d2eed 5540 ss->legacy_name);
c6d57f33
PM
5541 if (strlen(root->name))
5542 seq_printf(m, "%sname=%s", count ? "," : "",
5543 root->name);
a424316c 5544 seq_putc(m, ':');
2e91fa7f 5545
7717f7ba 5546 cgrp = task_cgroup_from_root(tsk, root);
2e91fa7f
TH
5547
5548 /*
5549 * On traditional hierarchies, all zombie tasks show up as
5550 * belonging to the root cgroup. On the default hierarchy,
5551 * while a zombie doesn't show up in "cgroup.procs" and
5552 * thus can't be migrated, its /proc/PID/cgroup keeps
5553 * reporting the cgroup it belonged to before exiting. If
5554 * the cgroup is removed before the zombie is reaped,
5555 * " (deleted)" is appended to the cgroup path.
5556 */
5557 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
4c737b41 5558 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
a79a908f 5559 current->nsproxy->cgroup_ns);
e0223003 5560 if (retval >= PATH_MAX)
2e91fa7f 5561 retval = -ENAMETOOLONG;
e0223003 5562 if (retval < 0)
2e91fa7f 5563 goto out_unlock;
4c737b41
TH
5564
5565 seq_puts(m, buf);
2e91fa7f 5566 } else {
4c737b41 5567 seq_puts(m, "/");
e61734c5 5568 }
2e91fa7f 5569
2e91fa7f
TH
5570 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5571 seq_puts(m, " (deleted)\n");
5572 else
5573 seq_putc(m, '\n');
a424316c
PM
5574 }
5575
006f4ac4 5576 retval = 0;
a424316c 5577out_unlock:
82d6489d 5578 spin_unlock_irq(&css_set_lock);
a424316c 5579 mutex_unlock(&cgroup_mutex);
a424316c
PM
5580 kfree(buf);
5581out:
5582 return retval;
5583}
5584
b4f48b63 5585/**
eaf797ab 5586 * cgroup_fork - initialize cgroup related fields during copy_process()
a043e3b2 5587 * @child: pointer to task_struct of forking parent process.
b4f48b63 5588 *
eaf797ab
TH
5589 * A task is associated with the init_css_set until cgroup_post_fork()
5590 * attaches it to the parent's css_set. Empty cg_list indicates that
5591 * @child isn't holding reference to its css_set.
b4f48b63
PM
5592 */
5593void cgroup_fork(struct task_struct *child)
5594{
eaf797ab 5595 RCU_INIT_POINTER(child->cgroups, &init_css_set);
817929ec 5596 INIT_LIST_HEAD(&child->cg_list);
b4f48b63
PM
5597}
5598
7e47682e
AS
5599/**
5600 * cgroup_can_fork - called on a new task before the process is exposed
5601 * @child: the task in question.
5602 *
5603 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5604 * returns an error, the fork aborts with that error code. This allows for
5605 * a cgroup subsystem to conditionally allow or deny new forks.
5606 */
b53202e6 5607int cgroup_can_fork(struct task_struct *child)
7e47682e
AS
5608{
5609 struct cgroup_subsys *ss;
5610 int i, j, ret;
5611
b4e0eeaf 5612 do_each_subsys_mask(ss, i, have_canfork_callback) {
b53202e6 5613 ret = ss->can_fork(child);
7e47682e
AS
5614 if (ret)
5615 goto out_revert;
b4e0eeaf 5616 } while_each_subsys_mask();
7e47682e
AS
5617
5618 return 0;
5619
5620out_revert:
5621 for_each_subsys(ss, j) {
5622 if (j >= i)
5623 break;
5624 if (ss->cancel_fork)
b53202e6 5625 ss->cancel_fork(child);
7e47682e
AS
5626 }
5627
5628 return ret;
5629}
5630
5631/**
5632 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5633 * @child: the task in question
5634 *
5635 * This calls the cancel_fork() callbacks if a fork failed *after*
5636 * cgroup_can_fork() succeded.
5637 */
b53202e6 5638void cgroup_cancel_fork(struct task_struct *child)
7e47682e
AS
5639{
5640 struct cgroup_subsys *ss;
5641 int i;
5642
5643 for_each_subsys(ss, i)
5644 if (ss->cancel_fork)
b53202e6 5645 ss->cancel_fork(child);
7e47682e
AS
5646}
5647
817929ec 5648/**
a043e3b2
LZ
5649 * cgroup_post_fork - called on a new task after adding it to the task list
5650 * @child: the task in question
5651 *
5edee61e
TH
5652 * Adds the task to the list running through its css_set if necessary and
5653 * call the subsystem fork() callbacks. Has to be after the task is
5654 * visible on the task list in case we race with the first call to
0942eeee 5655 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5edee61e 5656 * list.
a043e3b2 5657 */
b53202e6 5658void cgroup_post_fork(struct task_struct *child)
817929ec 5659{
30159ec7 5660 struct cgroup_subsys *ss;
5edee61e
TH
5661 int i;
5662
3ce3230a 5663 /*
251f8c03 5664 * This may race against cgroup_enable_task_cg_lists(). As that
eaf797ab
TH
5665 * function sets use_task_css_set_links before grabbing
5666 * tasklist_lock and we just went through tasklist_lock to add
5667 * @child, it's guaranteed that either we see the set
5668 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5669 * @child during its iteration.
5670 *
5671 * If we won the race, @child is associated with %current's
f0d9a5f1 5672 * css_set. Grabbing css_set_lock guarantees both that the
eaf797ab
TH
5673 * association is stable, and, on completion of the parent's
5674 * migration, @child is visible in the source of migration or
5675 * already in the destination cgroup. This guarantee is necessary
5676 * when implementing operations which need to migrate all tasks of
5677 * a cgroup to another.
5678 *
251f8c03 5679 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
eaf797ab
TH
5680 * will remain in init_css_set. This is safe because all tasks are
5681 * in the init_css_set before cg_links is enabled and there's no
5682 * operation which transfers all tasks out of init_css_set.
3ce3230a 5683 */
817929ec 5684 if (use_task_css_set_links) {
eaf797ab
TH
5685 struct css_set *cset;
5686
82d6489d 5687 spin_lock_irq(&css_set_lock);
0e1d768f 5688 cset = task_css_set(current);
eaf797ab 5689 if (list_empty(&child->cg_list)) {
eaf797ab 5690 get_css_set(cset);
73a7242a 5691 cset->nr_tasks++;
f6d7d049 5692 css_set_move_task(child, NULL, cset, false);
eaf797ab 5693 }
82d6489d 5694 spin_unlock_irq(&css_set_lock);
817929ec 5695 }
5edee61e
TH
5696
5697 /*
5698 * Call ss->fork(). This must happen after @child is linked on
5699 * css_set; otherwise, @child might change state between ->fork()
5700 * and addition to css_set.
5701 */
b4e0eeaf 5702 do_each_subsys_mask(ss, i, have_fork_callback) {
b53202e6 5703 ss->fork(child);
b4e0eeaf 5704 } while_each_subsys_mask();
817929ec 5705}
5edee61e 5706
b4f48b63
PM
5707/**
5708 * cgroup_exit - detach cgroup from exiting task
5709 * @tsk: pointer to task_struct of exiting process
5710 *
5711 * Description: Detach cgroup from @tsk and release it.
5712 *
5713 * Note that cgroups marked notify_on_release force every task in
5714 * them to take the global cgroup_mutex mutex when exiting.
5715 * This could impact scaling on very large systems. Be reluctant to
5716 * use notify_on_release cgroups where very high task exit scaling
5717 * is required on large systems.
5718 *
0e1d768f
TH
5719 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5720 * call cgroup_exit() while the task is still competent to handle
5721 * notify_on_release(), then leave the task attached to the root cgroup in
5722 * each hierarchy for the remainder of its exit. No need to bother with
5723 * init_css_set refcnting. init_css_set never goes away and we can't race
e8604cb4 5724 * with migration path - PF_EXITING is visible to migration path.
b4f48b63 5725 */
1ec41830 5726void cgroup_exit(struct task_struct *tsk)
b4f48b63 5727{
30159ec7 5728 struct cgroup_subsys *ss;
5abb8855 5729 struct css_set *cset;
d41d5a01 5730 int i;
817929ec
PM
5731
5732 /*
0e1d768f 5733 * Unlink from @tsk from its css_set. As migration path can't race
0de0942d 5734 * with us, we can check css_set and cg_list without synchronization.
817929ec 5735 */
0de0942d
TH
5736 cset = task_css_set(tsk);
5737
817929ec 5738 if (!list_empty(&tsk->cg_list)) {
82d6489d 5739 spin_lock_irq(&css_set_lock);
f6d7d049 5740 css_set_move_task(tsk, cset, NULL, false);
73a7242a 5741 cset->nr_tasks--;
82d6489d 5742 spin_unlock_irq(&css_set_lock);
2e91fa7f
TH
5743 } else {
5744 get_css_set(cset);
817929ec
PM
5745 }
5746
cb4a3167 5747 /* see cgroup_post_fork() for details */
b4e0eeaf 5748 do_each_subsys_mask(ss, i, have_exit_callback) {
2e91fa7f 5749 ss->exit(tsk);
b4e0eeaf 5750 } while_each_subsys_mask();
2e91fa7f 5751}
30159ec7 5752
2e91fa7f
TH
5753void cgroup_free(struct task_struct *task)
5754{
5755 struct css_set *cset = task_css_set(task);
afcf6c8b
TH
5756 struct cgroup_subsys *ss;
5757 int ssid;
5758
b4e0eeaf 5759 do_each_subsys_mask(ss, ssid, have_free_callback) {
afcf6c8b 5760 ss->free(task);
b4e0eeaf 5761 } while_each_subsys_mask();
d41d5a01 5762
2e91fa7f 5763 put_css_set(cset);
b4f48b63 5764}
697f4161 5765
8bab8dde
PM
5766static int __init cgroup_disable(char *str)
5767{
30159ec7 5768 struct cgroup_subsys *ss;
8bab8dde 5769 char *token;
30159ec7 5770 int i;
8bab8dde
PM
5771
5772 while ((token = strsep(&str, ",")) != NULL) {
5773 if (!*token)
5774 continue;
be45c900 5775
3ed80a62 5776 for_each_subsys(ss, i) {
3e1d2eed
TH
5777 if (strcmp(token, ss->name) &&
5778 strcmp(token, ss->legacy_name))
5779 continue;
a3e72739 5780 cgroup_disable_mask |= 1 << i;
8bab8dde
PM
5781 }
5782 }
5783 return 1;
5784}
5785__setup("cgroup_disable=", cgroup_disable);
38460b48 5786
5cf8114d
WL
5787void __init __weak enable_debug_cgroup(void) { }
5788
5789static int __init enable_cgroup_debug(char *str)
5790{
5791 cgroup_debug = true;
5792 enable_debug_cgroup();
5793 return 1;
5794}
5795__setup("cgroup_debug", enable_cgroup_debug);
5796
b77d7b60 5797/**
ec903c0c 5798 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
35cf0836
TH
5799 * @dentry: directory dentry of interest
5800 * @ss: subsystem of interest
b77d7b60 5801 *
5a17f543
TH
5802 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5803 * to get the corresponding css and return it. If such css doesn't exist
5804 * or can't be pinned, an ERR_PTR value is returned.
e5d1367f 5805 */
ec903c0c
TH
5806struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5807 struct cgroup_subsys *ss)
e5d1367f 5808{
2bd59d48 5809 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
f17fc25f 5810 struct file_system_type *s_type = dentry->d_sb->s_type;
2bd59d48 5811 struct cgroup_subsys_state *css = NULL;
e5d1367f 5812 struct cgroup *cgrp;
e5d1367f 5813
35cf0836 5814 /* is @dentry a cgroup dir? */
f17fc25f
TH
5815 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5816 !kn || kernfs_type(kn) != KERNFS_DIR)
e5d1367f
SE
5817 return ERR_PTR(-EBADF);
5818
5a17f543
TH
5819 rcu_read_lock();
5820
2bd59d48
TH
5821 /*
5822 * This path doesn't originate from kernfs and @kn could already
5823 * have been or be removed at any point. @kn->priv is RCU
a4189487 5824 * protected for this access. See css_release_work_fn() for details.
2bd59d48 5825 */
e0aed7c7 5826 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
2bd59d48
TH
5827 if (cgrp)
5828 css = cgroup_css(cgrp, ss);
5a17f543 5829
ec903c0c 5830 if (!css || !css_tryget_online(css))
5a17f543
TH
5831 css = ERR_PTR(-ENOENT);
5832
5833 rcu_read_unlock();
5834 return css;
e5d1367f 5835}
e5d1367f 5836
1cb650b9
LZ
5837/**
5838 * css_from_id - lookup css by id
5839 * @id: the cgroup id
5840 * @ss: cgroup subsys to be looked into
5841 *
5842 * Returns the css if there's valid one with @id, otherwise returns NULL.
5843 * Should be called under rcu_read_lock().
5844 */
5845struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5846{
6fa4918d 5847 WARN_ON_ONCE(!rcu_read_lock_held());
d6ccc55e 5848 return idr_find(&ss->css_idr, id);
e5d1367f
SE
5849}
5850
16af4396
TH
5851/**
5852 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5853 * @path: path on the default hierarchy
5854 *
5855 * Find the cgroup at @path on the default hierarchy, increment its
5856 * reference count and return it. Returns pointer to the found cgroup on
5857 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5858 * if @path points to a non-directory.
5859 */
5860struct cgroup *cgroup_get_from_path(const char *path)
5861{
5862 struct kernfs_node *kn;
5863 struct cgroup *cgrp;
5864
5865 mutex_lock(&cgroup_mutex);
5866
5867 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5868 if (kn) {
5869 if (kernfs_type(kn) == KERNFS_DIR) {
5870 cgrp = kn->priv;
a590b90d 5871 cgroup_get_live(cgrp);
16af4396
TH
5872 } else {
5873 cgrp = ERR_PTR(-ENOTDIR);
5874 }
5875 kernfs_put(kn);
5876 } else {
5877 cgrp = ERR_PTR(-ENOENT);
5878 }
5879
5880 mutex_unlock(&cgroup_mutex);
5881 return cgrp;
5882}
5883EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5884
1f3fe7eb
MKL
5885/**
5886 * cgroup_get_from_fd - get a cgroup pointer from a fd
5887 * @fd: fd obtained by open(cgroup2_dir)
5888 *
5889 * Find the cgroup from a fd which should be obtained
5890 * by opening a cgroup directory. Returns a pointer to the
5891 * cgroup on success. ERR_PTR is returned if the cgroup
5892 * cannot be found.
5893 */
5894struct cgroup *cgroup_get_from_fd(int fd)
5895{
5896 struct cgroup_subsys_state *css;
5897 struct cgroup *cgrp;
5898 struct file *f;
5899
5900 f = fget_raw(fd);
5901 if (!f)
5902 return ERR_PTR(-EBADF);
5903
5904 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
5905 fput(f);
5906 if (IS_ERR(css))
5907 return ERR_CAST(css);
5908
5909 cgrp = css->cgroup;
5910 if (!cgroup_on_dfl(cgrp)) {
5911 cgroup_put(cgrp);
5912 return ERR_PTR(-EBADF);
5913 }
5914
5915 return cgrp;
5916}
5917EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
5918
bd1060a1
TH
5919/*
5920 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5921 * definition in cgroup-defs.h.
5922 */
5923#ifdef CONFIG_SOCK_CGROUP_DATA
5924
5925#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5926
3fa4cc9c 5927DEFINE_SPINLOCK(cgroup_sk_update_lock);
bd1060a1
TH
5928static bool cgroup_sk_alloc_disabled __read_mostly;
5929
5930void cgroup_sk_alloc_disable(void)
5931{
5932 if (cgroup_sk_alloc_disabled)
5933 return;
5934 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5935 cgroup_sk_alloc_disabled = true;
5936}
5937
5938#else
5939
5940#define cgroup_sk_alloc_disabled false
5941
5942#endif
5943
5944void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5945{
5946 if (cgroup_sk_alloc_disabled)
5947 return;
5948
d979a39d
JW
5949 /* Socket clone path */
5950 if (skcd->val) {
a590b90d
TH
5951 /*
5952 * We might be cloning a socket which is left in an empty
5953 * cgroup and the cgroup might have already been rmdir'd.
5954 * Don't use cgroup_get_live().
5955 */
d979a39d
JW
5956 cgroup_get(sock_cgroup_ptr(skcd));
5957 return;
5958 }
5959
bd1060a1
TH
5960 rcu_read_lock();
5961
5962 while (true) {
5963 struct css_set *cset;
5964
5965 cset = task_css_set(current);
5966 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5967 skcd->val = (unsigned long)cset->dfl_cgrp;
5968 break;
5969 }
5970 cpu_relax();
5971 }
5972
5973 rcu_read_unlock();
5974}
5975
5976void cgroup_sk_free(struct sock_cgroup_data *skcd)
5977{
5978 cgroup_put(sock_cgroup_ptr(skcd));
5979}
5980
5981#endif /* CONFIG_SOCK_CGROUP_DATA */
5982
30070984 5983#ifdef CONFIG_CGROUP_BPF
324bda9e
AS
5984int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
5985 enum bpf_attach_type type, u32 flags)
5986{
5987 int ret;
5988
5989 mutex_lock(&cgroup_mutex);
5990 ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
5991 mutex_unlock(&cgroup_mutex);
5992 return ret;
5993}
5994int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
5995 enum bpf_attach_type type, u32 flags)
30070984 5996{
7f677633 5997 int ret;
30070984
DM
5998
5999 mutex_lock(&cgroup_mutex);
324bda9e 6000 ret = __cgroup_bpf_detach(cgrp, prog, type, flags);
30070984 6001 mutex_unlock(&cgroup_mutex);
7f677633 6002 return ret;
30070984 6003}
468e2f64
AS
6004int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
6005 union bpf_attr __user *uattr)
6006{
6007 int ret;
6008
6009 mutex_lock(&cgroup_mutex);
6010 ret = __cgroup_bpf_query(cgrp, attr, uattr);
6011 mutex_unlock(&cgroup_mutex);
6012 return ret;
6013}
30070984 6014#endif /* CONFIG_CGROUP_BPF */
01ee6cfb
RG
6015
6016#ifdef CONFIG_SYSFS
6017static ssize_t show_delegatable_files(struct cftype *files, char *buf,
6018 ssize_t size, const char *prefix)
6019{
6020 struct cftype *cft;
6021 ssize_t ret = 0;
6022
6023 for (cft = files; cft && cft->name[0] != '\0'; cft++) {
6024 if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
6025 continue;
6026
6027 if (prefix)
6028 ret += snprintf(buf + ret, size - ret, "%s.", prefix);
6029
6030 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
6031
4d9ebbe2 6032 if (WARN_ON(ret >= size))
01ee6cfb 6033 break;
01ee6cfb
RG
6034 }
6035
6036 return ret;
6037}
6038
6039static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
6040 char *buf)
6041{
6042 struct cgroup_subsys *ss;
6043 int ssid;
6044 ssize_t ret = 0;
6045
6046 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret,
6047 NULL);
6048
6049 for_each_subsys(ss, ssid)
6050 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
6051 PAGE_SIZE - ret,
6052 cgroup_subsys_name[ssid]);
6053
6054 return ret;
6055}
6056static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
6057
5f2e6734
RG
6058static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
6059 char *buf)
6060{
6061 return snprintf(buf, PAGE_SIZE, "nsdelegate\n");
6062}
6063static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
6064
01ee6cfb
RG
6065static struct attribute *cgroup_sysfs_attrs[] = {
6066 &cgroup_delegate_attr.attr,
5f2e6734 6067 &cgroup_features_attr.attr,
01ee6cfb
RG
6068 NULL,
6069};
6070
6071static const struct attribute_group cgroup_sysfs_attr_group = {
6072 .attrs = cgroup_sysfs_attrs,
6073 .name = "cgroup",
6074};
6075
6076static int __init cgroup_sysfs_init(void)
6077{
6078 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
6079}
6080subsys_initcall(cgroup_sysfs_init);
6081#endif /* CONFIG_SYSFS */