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