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