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