xref: /openbmc/linux/kernel/cgroup/cgroup.c (revision 04d2fea2)
1201af4c0STejun Heo /*
2201af4c0STejun Heo  *  Generic process-grouping system.
3201af4c0STejun Heo  *
4201af4c0STejun Heo  *  Based originally on the cpuset system, extracted by Paul Menage
5201af4c0STejun Heo  *  Copyright (C) 2006 Google, Inc
6201af4c0STejun Heo  *
7201af4c0STejun Heo  *  Notifications support
8201af4c0STejun Heo  *  Copyright (C) 2009 Nokia Corporation
9201af4c0STejun Heo  *  Author: Kirill A. Shutemov
10201af4c0STejun Heo  *
11201af4c0STejun Heo  *  Copyright notices from the original cpuset code:
12201af4c0STejun Heo  *  --------------------------------------------------
13201af4c0STejun Heo  *  Copyright (C) 2003 BULL SA.
14201af4c0STejun Heo  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
15201af4c0STejun Heo  *
16201af4c0STejun Heo  *  Portions derived from Patrick Mochel's sysfs code.
17201af4c0STejun Heo  *  sysfs is Copyright (c) 2001-3 Patrick Mochel
18201af4c0STejun Heo  *
19201af4c0STejun Heo  *  2003-10-10 Written by Simon Derr.
20201af4c0STejun Heo  *  2003-10-22 Updates by Stephen Hemminger.
21201af4c0STejun Heo  *  2004 May-July Rework by Paul Jackson.
22201af4c0STejun Heo  *  ---------------------------------------------------
23201af4c0STejun Heo  *
24201af4c0STejun Heo  *  This file is subject to the terms and conditions of the GNU General Public
25201af4c0STejun Heo  *  License.  See the file COPYING in the main directory of the Linux
26201af4c0STejun Heo  *  distribution for more details.
27201af4c0STejun Heo  */
28201af4c0STejun Heo 
29201af4c0STejun Heo #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30201af4c0STejun Heo 
310a268dbdSTejun Heo #include "cgroup-internal.h"
320a268dbdSTejun Heo 
33aef2fedaSJakub Kicinski #include <linux/bpf-cgroup.h>
34201af4c0STejun Heo #include <linux/cred.h>
35201af4c0STejun Heo #include <linux/errno.h>
36201af4c0STejun Heo #include <linux/init_task.h>
37201af4c0STejun Heo #include <linux/kernel.h>
38201af4c0STejun Heo #include <linux/magic.h>
39201af4c0STejun Heo #include <linux/mutex.h>
40201af4c0STejun Heo #include <linux/mount.h>
41201af4c0STejun Heo #include <linux/pagemap.h>
42201af4c0STejun Heo #include <linux/proc_fs.h>
43201af4c0STejun Heo #include <linux/rcupdate.h>
44201af4c0STejun Heo #include <linux/sched.h>
4529930025SIngo Molnar #include <linux/sched/task.h>
46201af4c0STejun Heo #include <linux/slab.h>
47201af4c0STejun Heo #include <linux/spinlock.h>
48201af4c0STejun Heo #include <linux/percpu-rwsem.h>
49201af4c0STejun Heo #include <linux/string.h>
50201af4c0STejun Heo #include <linux/hashtable.h>
51201af4c0STejun Heo #include <linux/idr.h>
52201af4c0STejun Heo #include <linux/kthread.h>
53201af4c0STejun Heo #include <linux/atomic.h>
54201af4c0STejun Heo #include <linux/cpuset.h>
55201af4c0STejun Heo #include <linux/proc_ns.h>
56201af4c0STejun Heo #include <linux/nsproxy.h>
57201af4c0STejun Heo #include <linux/file.h>
58e34a98d5SAl Viro #include <linux/fs_parser.h>
59d4ff749bSTejun Heo #include <linux/sched/cputime.h>
606c24849fSJuri Lelli #include <linux/sched/deadline.h>
612ce7135aSJohannes Weiner #include <linux/psi.h>
62201af4c0STejun Heo #include <net/sock.h>
63201af4c0STejun Heo 
64201af4c0STejun Heo #define CREATE_TRACE_POINTS
65201af4c0STejun Heo #include <trace/events/cgroup.h>
66201af4c0STejun Heo 
67201af4c0STejun Heo #define CGROUP_FILE_NAME_MAX		(MAX_CGROUP_TYPE_NAMELEN +	\
68201af4c0STejun Heo 					 MAX_CFTYPE_NAME + 2)
69b12e3583STejun Heo /* let's not notify more than 100 times per second */
70b12e3583STejun Heo #define CGROUP_FILE_NOTIFY_MIN_INTV	DIV_ROUND_UP(HZ, 100)
71201af4c0STejun Heo 
72201af4c0STejun Heo /*
73d20d30ebSKees Cook  * To avoid confusing the compiler (and generating warnings) with code
74d20d30ebSKees Cook  * that attempts to access what would be a 0-element array (i.e. sized
75d20d30ebSKees Cook  * to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
76d20d30ebSKees Cook  * constant expression can be added.
77d20d30ebSKees Cook  */
78d20d30ebSKees Cook #define CGROUP_HAS_SUBSYS_CONFIG	(CGROUP_SUBSYS_COUNT > 0)
79d20d30ebSKees Cook 
80d20d30ebSKees Cook /*
81201af4c0STejun Heo  * cgroup_mutex is the master lock.  Any modification to cgroup or its
82201af4c0STejun Heo  * hierarchy must be performed while holding it.
83201af4c0STejun Heo  *
84201af4c0STejun Heo  * css_set_lock protects task->cgroups pointer, the list of css_set
85201af4c0STejun Heo  * objects, and the chain of tasks off each css_set.
86201af4c0STejun Heo  *
87201af4c0STejun Heo  * These locks are exported if CONFIG_PROVE_RCU so that accessors in
88201af4c0STejun Heo  * cgroup.h can use them for lockdep annotations.
89201af4c0STejun Heo  */
90201af4c0STejun Heo DEFINE_MUTEX(cgroup_mutex);
91201af4c0STejun Heo DEFINE_SPINLOCK(css_set_lock);
920a268dbdSTejun Heo 
930a268dbdSTejun Heo #ifdef CONFIG_PROVE_RCU
94201af4c0STejun Heo EXPORT_SYMBOL_GPL(cgroup_mutex);
95201af4c0STejun Heo EXPORT_SYMBOL_GPL(css_set_lock);
96201af4c0STejun Heo #endif
97201af4c0STejun Heo 
98e4f8d81cSSteven Rostedt (VMware) DEFINE_SPINLOCK(trace_cgroup_path_lock);
99e4f8d81cSSteven Rostedt (VMware) char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
10029ed1738SXiu Jianfeng static bool cgroup_debug __read_mostly;
101e4f8d81cSSteven Rostedt (VMware) 
102201af4c0STejun Heo /*
103201af4c0STejun Heo  * Protects cgroup_idr and css_idr so that IDs can be released without
104201af4c0STejun Heo  * grabbing cgroup_mutex.
105201af4c0STejun Heo  */
106201af4c0STejun Heo static DEFINE_SPINLOCK(cgroup_idr_lock);
107201af4c0STejun Heo 
108201af4c0STejun Heo /*
109201af4c0STejun Heo  * Protects cgroup_file->kn for !self csses.  It synchronizes notifications
110201af4c0STejun Heo  * against file removal/re-creation across css hiding.
111201af4c0STejun Heo  */
112201af4c0STejun Heo static DEFINE_SPINLOCK(cgroup_file_kn_lock);
113201af4c0STejun Heo 
1143f2947b7SOleg Nesterov DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem);
115201af4c0STejun Heo 
116201af4c0STejun Heo #define cgroup_assert_mutex_or_rcu_locked()				\
117201af4c0STejun Heo 	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&			\
118201af4c0STejun Heo 			   !lockdep_is_held(&cgroup_mutex),		\
119201af4c0STejun Heo 			   "cgroup_mutex or RCU read lock required");
120201af4c0STejun Heo 
121201af4c0STejun Heo /*
122201af4c0STejun Heo  * cgroup destruction makes heavy use of work items and there can be a lot
123201af4c0STejun Heo  * of concurrent destructions.  Use a separate workqueue so that cgroup
124201af4c0STejun Heo  * destruction work items don't end up filling up max_active of system_wq
125201af4c0STejun Heo  * which may lead to deadlock.
126201af4c0STejun Heo  */
127201af4c0STejun Heo static struct workqueue_struct *cgroup_destroy_wq;
128201af4c0STejun Heo 
129201af4c0STejun Heo /* generate an array of cgroup subsystem pointers */
130201af4c0STejun Heo #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
1310a268dbdSTejun Heo struct cgroup_subsys *cgroup_subsys[] = {
132201af4c0STejun Heo #include <linux/cgroup_subsys.h>
133201af4c0STejun Heo };
134201af4c0STejun Heo #undef SUBSYS
135201af4c0STejun Heo 
136201af4c0STejun Heo /* array of cgroup subsystem names */
137201af4c0STejun Heo #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
138201af4c0STejun Heo static const char *cgroup_subsys_name[] = {
139201af4c0STejun Heo #include <linux/cgroup_subsys.h>
140201af4c0STejun Heo };
141201af4c0STejun Heo #undef SUBSYS
142201af4c0STejun Heo 
143201af4c0STejun Heo /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
144201af4c0STejun Heo #define SUBSYS(_x)								\
145201af4c0STejun Heo 	DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key);			\
146201af4c0STejun Heo 	DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key);			\
147201af4c0STejun Heo 	EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key);			\
148201af4c0STejun Heo 	EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
149201af4c0STejun Heo #include <linux/cgroup_subsys.h>
150201af4c0STejun Heo #undef SUBSYS
151201af4c0STejun Heo 
152201af4c0STejun Heo #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
153201af4c0STejun Heo static struct static_key_true *cgroup_subsys_enabled_key[] = {
154201af4c0STejun Heo #include <linux/cgroup_subsys.h>
155201af4c0STejun Heo };
156201af4c0STejun Heo #undef SUBSYS
157201af4c0STejun Heo 
158201af4c0STejun Heo #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
159201af4c0STejun Heo static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
160201af4c0STejun Heo #include <linux/cgroup_subsys.h>
161201af4c0STejun Heo };
162201af4c0STejun Heo #undef SUBSYS
163201af4c0STejun Heo 
164c58632b3STejun Heo static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
165041cd640STejun Heo 
1666b6ebb34SZefan Li /* the default hierarchy */
167c58632b3STejun Heo struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
168201af4c0STejun Heo EXPORT_SYMBOL_GPL(cgrp_dfl_root);
169201af4c0STejun Heo 
170201af4c0STejun Heo /*
171201af4c0STejun Heo  * The default hierarchy always exists but is hidden until mounted for the
172201af4c0STejun Heo  * first time.  This is for backward compatibility.
173201af4c0STejun Heo  */
174201af4c0STejun Heo static bool cgrp_dfl_visible;
175201af4c0STejun Heo 
176201af4c0STejun Heo /* some controllers are not supported in the default hierarchy */
177201af4c0STejun Heo static u16 cgrp_dfl_inhibit_ss_mask;
178201af4c0STejun Heo 
179201af4c0STejun Heo /* some controllers are implicitly enabled on the default hierarchy */
180b807421aSTejun Heo static u16 cgrp_dfl_implicit_ss_mask;
181201af4c0STejun Heo 
1828cfd8147STejun Heo /* some controllers can be threaded on the default hierarchy */
1838cfd8147STejun Heo static u16 cgrp_dfl_threaded_ss_mask;
1848cfd8147STejun Heo 
185201af4c0STejun Heo /* The list of hierarchy roots */
1860a268dbdSTejun Heo LIST_HEAD(cgroup_roots);
187201af4c0STejun Heo static int cgroup_root_count;
188201af4c0STejun Heo 
189201af4c0STejun Heo /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
190201af4c0STejun Heo static DEFINE_IDR(cgroup_hierarchy_idr);
191201af4c0STejun Heo 
192201af4c0STejun Heo /*
193201af4c0STejun Heo  * Assign a monotonically increasing serial number to csses.  It guarantees
194201af4c0STejun Heo  * cgroups with bigger numbers are newer than those with smaller numbers.
195201af4c0STejun Heo  * Also, as csses are always appended to the parent's ->children list, it
196201af4c0STejun Heo  * guarantees that sibling csses are always sorted in the ascending serial
197201af4c0STejun Heo  * number order on the list.  Protected by cgroup_mutex.
198201af4c0STejun Heo  */
199201af4c0STejun Heo static u64 css_serial_nr_next = 1;
200201af4c0STejun Heo 
201201af4c0STejun Heo /*
202b807421aSTejun Heo  * These bitmasks identify subsystems with specific features to avoid
203b807421aSTejun Heo  * having to do iterative checks repeatedly.
204201af4c0STejun Heo  */
205201af4c0STejun Heo static u16 have_fork_callback __read_mostly;
206201af4c0STejun Heo static u16 have_exit_callback __read_mostly;
20751bee5abSOleg Nesterov static u16 have_release_callback __read_mostly;
208b807421aSTejun Heo static u16 have_canfork_callback __read_mostly;
209201af4c0STejun Heo 
210201af4c0STejun Heo /* cgroup namespace for init task */
211201af4c0STejun Heo struct cgroup_namespace init_cgroup_ns = {
212f387882dSKirill Tkhai 	.ns.count	= REFCOUNT_INIT(2),
213201af4c0STejun Heo 	.user_ns	= &init_user_ns,
214201af4c0STejun Heo 	.ns.ops		= &cgroupns_operations,
215201af4c0STejun Heo 	.ns.inum	= PROC_CGROUP_INIT_INO,
216201af4c0STejun Heo 	.root_cset	= &init_css_set,
217201af4c0STejun Heo };
218201af4c0STejun Heo 
219201af4c0STejun Heo static struct file_system_type cgroup2_fs_type;
220d62beb7fSTejun Heo static struct cftype cgroup_base_files[];
2218a693f77STejun Heo static struct cftype cgroup_psi_files[];
222201af4c0STejun Heo 
2233958e2d0SSuren Baghdasaryan /* cgroup optional features */
2243958e2d0SSuren Baghdasaryan enum cgroup_opt_features {
2253958e2d0SSuren Baghdasaryan #ifdef CONFIG_PSI
2263958e2d0SSuren Baghdasaryan 	OPT_FEATURE_PRESSURE,
2273958e2d0SSuren Baghdasaryan #endif
2283958e2d0SSuren Baghdasaryan 	OPT_FEATURE_COUNT
2293958e2d0SSuren Baghdasaryan };
2303958e2d0SSuren Baghdasaryan 
2313958e2d0SSuren Baghdasaryan static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = {
2323958e2d0SSuren Baghdasaryan #ifdef CONFIG_PSI
2333958e2d0SSuren Baghdasaryan 	"pressure",
2343958e2d0SSuren Baghdasaryan #endif
2353958e2d0SSuren Baghdasaryan };
2363958e2d0SSuren Baghdasaryan 
2373958e2d0SSuren Baghdasaryan static u16 cgroup_feature_disable_mask __read_mostly;
2383958e2d0SSuren Baghdasaryan 
239201af4c0STejun Heo static int cgroup_apply_control(struct cgroup *cgrp);
240201af4c0STejun Heo static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
241b636fd38STejun Heo static void css_task_iter_skip(struct css_task_iter *it,
242b636fd38STejun Heo 			       struct task_struct *task);
243201af4c0STejun Heo static int cgroup_destroy_locked(struct cgroup *cgrp);
244201af4c0STejun Heo static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
245201af4c0STejun Heo 					      struct cgroup_subsys *ss);
246201af4c0STejun Heo static void css_release(struct percpu_ref *ref);
247201af4c0STejun Heo static void kill_css(struct cgroup_subsys_state *css);
248201af4c0STejun Heo static int cgroup_addrm_files(struct cgroup_subsys_state *css,
249201af4c0STejun Heo 			      struct cgroup *cgrp, struct cftype cfts[],
250201af4c0STejun Heo 			      bool is_add);
251201af4c0STejun Heo 
2526ab42860STejun Heo #ifdef CONFIG_DEBUG_CGROUP_REF
2536ab42860STejun Heo #define CGROUP_REF_FN_ATTRS	noinline
25479a7f41fSTejun Heo #define CGROUP_REF_EXPORT(fn)	EXPORT_SYMBOL_GPL(fn);
2556ab42860STejun Heo #include <linux/cgroup_refcnt.h>
2566ab42860STejun Heo #endif
2576ab42860STejun Heo 
258201af4c0STejun Heo /**
259201af4c0STejun Heo  * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
260201af4c0STejun Heo  * @ssid: subsys ID of interest
261201af4c0STejun Heo  *
262201af4c0STejun Heo  * cgroup_subsys_enabled() can only be used with literal subsys names which
263201af4c0STejun Heo  * is fine for individual subsystems but unsuitable for cgroup core.  This
264201af4c0STejun Heo  * is slower static_key_enabled() based test indexed by @ssid.
265201af4c0STejun Heo  */
cgroup_ssid_enabled(int ssid)2660a268dbdSTejun Heo bool cgroup_ssid_enabled(int ssid)
267201af4c0STejun Heo {
268d20d30ebSKees Cook 	if (!CGROUP_HAS_SUBSYS_CONFIG)
269201af4c0STejun Heo 		return false;
270201af4c0STejun Heo 
271201af4c0STejun Heo 	return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
272201af4c0STejun Heo }
273201af4c0STejun Heo 
274201af4c0STejun Heo /**
275201af4c0STejun Heo  * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
276201af4c0STejun Heo  * @cgrp: the cgroup of interest
277201af4c0STejun Heo  *
278201af4c0STejun Heo  * The default hierarchy is the v2 interface of cgroup and this function
279201af4c0STejun Heo  * can be used to test whether a cgroup is on the default hierarchy for
28058315c96SBhaskar Chowdhury  * cases where a subsystem should behave differently depending on the
281201af4c0STejun Heo  * interface version.
282201af4c0STejun Heo  *
283201af4c0STejun Heo  * List of changed behaviors:
284201af4c0STejun Heo  *
285201af4c0STejun Heo  * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
286201af4c0STejun Heo  *   and "name" are disallowed.
287201af4c0STejun Heo  *
288201af4c0STejun Heo  * - When mounting an existing superblock, mount options should match.
289201af4c0STejun Heo  *
290201af4c0STejun Heo  * - rename(2) is disallowed.
291201af4c0STejun Heo  *
292201af4c0STejun Heo  * - "tasks" is removed.  Everything should be at process granularity.  Use
293201af4c0STejun Heo  *   "cgroup.procs" instead.
294201af4c0STejun Heo  *
295201af4c0STejun Heo  * - "cgroup.procs" is not sorted.  pids will be unique unless they got
29658315c96SBhaskar Chowdhury  *   recycled in-between reads.
297201af4c0STejun Heo  *
298201af4c0STejun Heo  * - "release_agent" and "notify_on_release" are removed.  Replacement
299201af4c0STejun Heo  *   notification mechanism will be implemented.
300201af4c0STejun Heo  *
301201af4c0STejun Heo  * - "cgroup.clone_children" is removed.
302201af4c0STejun Heo  *
303201af4c0STejun Heo  * - "cgroup.subtree_populated" is available.  Its value is 0 if the cgroup
304201af4c0STejun Heo  *   and its descendants contain no task; otherwise, 1.  The file also
305201af4c0STejun Heo  *   generates kernfs notification which can be monitored through poll and
306201af4c0STejun Heo  *   [di]notify when the value of the file changes.
307201af4c0STejun Heo  *
308201af4c0STejun Heo  * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
309201af4c0STejun Heo  *   take masks of ancestors with non-empty cpus/mems, instead of being
310201af4c0STejun Heo  *   moved to an ancestor.
311201af4c0STejun Heo  *
312201af4c0STejun Heo  * - cpuset: a task can be moved into an empty cpuset, and again it takes
313201af4c0STejun Heo  *   masks of ancestors.
314201af4c0STejun Heo  *
315201af4c0STejun Heo  * - blkcg: blk-throttle becomes properly hierarchical.
316201af4c0STejun Heo  */
cgroup_on_dfl(const struct cgroup * cgrp)3170a268dbdSTejun Heo bool cgroup_on_dfl(const struct cgroup *cgrp)
318201af4c0STejun Heo {
319201af4c0STejun Heo 	return cgrp->root == &cgrp_dfl_root;
320201af4c0STejun Heo }
321201af4c0STejun Heo 
322201af4c0STejun Heo /* IDR wrappers which synchronize using cgroup_idr_lock */
cgroup_idr_alloc(struct idr * idr,void * ptr,int start,int end,gfp_t gfp_mask)323201af4c0STejun Heo static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
324201af4c0STejun Heo 			    gfp_t gfp_mask)
325201af4c0STejun Heo {
326201af4c0STejun Heo 	int ret;
327201af4c0STejun Heo 
328201af4c0STejun Heo 	idr_preload(gfp_mask);
329201af4c0STejun Heo 	spin_lock_bh(&cgroup_idr_lock);
330201af4c0STejun Heo 	ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
331201af4c0STejun Heo 	spin_unlock_bh(&cgroup_idr_lock);
332201af4c0STejun Heo 	idr_preload_end();
333201af4c0STejun Heo 	return ret;
334201af4c0STejun Heo }
335201af4c0STejun Heo 
cgroup_idr_replace(struct idr * idr,void * ptr,int id)336201af4c0STejun Heo static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
337201af4c0STejun Heo {
338201af4c0STejun Heo 	void *ret;
339201af4c0STejun Heo 
340201af4c0STejun Heo 	spin_lock_bh(&cgroup_idr_lock);
341201af4c0STejun Heo 	ret = idr_replace(idr, ptr, id);
342201af4c0STejun Heo 	spin_unlock_bh(&cgroup_idr_lock);
343201af4c0STejun Heo 	return ret;
344201af4c0STejun Heo }
345201af4c0STejun Heo 
cgroup_idr_remove(struct idr * idr,int id)346201af4c0STejun Heo static void cgroup_idr_remove(struct idr *idr, int id)
347201af4c0STejun Heo {
348201af4c0STejun Heo 	spin_lock_bh(&cgroup_idr_lock);
349201af4c0STejun Heo 	idr_remove(idr, id);
350201af4c0STejun Heo 	spin_unlock_bh(&cgroup_idr_lock);
351201af4c0STejun Heo }
352201af4c0STejun Heo 
cgroup_has_tasks(struct cgroup * cgrp)35327f26753STejun Heo static bool cgroup_has_tasks(struct cgroup *cgrp)
354201af4c0STejun Heo {
35527f26753STejun Heo 	return cgrp->nr_populated_csets;
35627f26753STejun Heo }
357201af4c0STejun Heo 
cgroup_is_threaded(struct cgroup * cgrp)3580dad9b07SMiaohe Lin static bool cgroup_is_threaded(struct cgroup *cgrp)
359454000adSTejun Heo {
360454000adSTejun Heo 	return cgrp->dom_cgrp != cgrp;
361454000adSTejun Heo }
362454000adSTejun Heo 
3638cfd8147STejun Heo /* can @cgrp host both domain and threaded children? */
cgroup_is_mixable(struct cgroup * cgrp)3648cfd8147STejun Heo static bool cgroup_is_mixable(struct cgroup *cgrp)
3658cfd8147STejun Heo {
3668cfd8147STejun Heo 	/*
3678cfd8147STejun Heo 	 * Root isn't under domain level resource control exempting it from
3688cfd8147STejun Heo 	 * the no-internal-process constraint, so it can serve as a thread
3698cfd8147STejun Heo 	 * root and a parent of resource domains at the same time.
3708cfd8147STejun Heo 	 */
3718cfd8147STejun Heo 	return !cgroup_parent(cgrp);
3728cfd8147STejun Heo }
3738cfd8147STejun Heo 
37458315c96SBhaskar Chowdhury /* can @cgrp become a thread root? Should always be true for a thread root */
cgroup_can_be_thread_root(struct cgroup * cgrp)3758cfd8147STejun Heo static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
3768cfd8147STejun Heo {
3778cfd8147STejun Heo 	/* mixables don't care */
3788cfd8147STejun Heo 	if (cgroup_is_mixable(cgrp))
3798cfd8147STejun Heo 		return true;
3808cfd8147STejun Heo 
3818cfd8147STejun Heo 	/* domain roots can't be nested under threaded */
3828cfd8147STejun Heo 	if (cgroup_is_threaded(cgrp))
3838cfd8147STejun Heo 		return false;
3848cfd8147STejun Heo 
3858cfd8147STejun Heo 	/* can only have either domain or threaded children */
3868cfd8147STejun Heo 	if (cgrp->nr_populated_domain_children)
3878cfd8147STejun Heo 		return false;
3888cfd8147STejun Heo 
3898cfd8147STejun Heo 	/* and no domain controllers can be enabled */
3908cfd8147STejun Heo 	if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3918cfd8147STejun Heo 		return false;
3928cfd8147STejun Heo 
3938cfd8147STejun Heo 	return true;
3948cfd8147STejun Heo }
3958cfd8147STejun Heo 
3968cfd8147STejun Heo /* is @cgrp root of a threaded subtree? */
cgroup_is_thread_root(struct cgroup * cgrp)3970dad9b07SMiaohe Lin static bool cgroup_is_thread_root(struct cgroup *cgrp)
3988cfd8147STejun Heo {
3998cfd8147STejun Heo 	/* thread root should be a domain */
4008cfd8147STejun Heo 	if (cgroup_is_threaded(cgrp))
4018cfd8147STejun Heo 		return false;
4028cfd8147STejun Heo 
4038cfd8147STejun Heo 	/* a domain w/ threaded children is a thread root */
4048cfd8147STejun Heo 	if (cgrp->nr_threaded_children)
4058cfd8147STejun Heo 		return true;
4068cfd8147STejun Heo 
4078cfd8147STejun Heo 	/*
4088cfd8147STejun Heo 	 * A domain which has tasks and explicit threaded controllers
4098cfd8147STejun Heo 	 * enabled is a thread root.
4108cfd8147STejun Heo 	 */
4118cfd8147STejun Heo 	if (cgroup_has_tasks(cgrp) &&
4128cfd8147STejun Heo 	    (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
4138cfd8147STejun Heo 		return true;
4148cfd8147STejun Heo 
4158cfd8147STejun Heo 	return false;
4168cfd8147STejun Heo }
4178cfd8147STejun Heo 
4188cfd8147STejun Heo /* a domain which isn't connected to the root w/o brekage can't be used */
cgroup_is_valid_domain(struct cgroup * cgrp)4198cfd8147STejun Heo static bool cgroup_is_valid_domain(struct cgroup *cgrp)
4208cfd8147STejun Heo {
4218cfd8147STejun Heo 	/* the cgroup itself can be a thread root */
4228cfd8147STejun Heo 	if (cgroup_is_threaded(cgrp))
4238cfd8147STejun Heo 		return false;
4248cfd8147STejun Heo 
4258cfd8147STejun Heo 	/* but the ancestors can't be unless mixable */
4268cfd8147STejun Heo 	while ((cgrp = cgroup_parent(cgrp))) {
4278cfd8147STejun Heo 		if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
4288cfd8147STejun Heo 			return false;
4298cfd8147STejun Heo 		if (cgroup_is_threaded(cgrp))
4308cfd8147STejun Heo 			return false;
4318cfd8147STejun Heo 	}
4328cfd8147STejun Heo 
4338cfd8147STejun Heo 	return true;
434201af4c0STejun Heo }
435201af4c0STejun Heo 
436201af4c0STejun Heo /* subsystems visibly enabled on a cgroup */
cgroup_control(struct cgroup * cgrp)437201af4c0STejun Heo static u16 cgroup_control(struct cgroup *cgrp)
438201af4c0STejun Heo {
439201af4c0STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
440201af4c0STejun Heo 	u16 root_ss_mask = cgrp->root->subsys_mask;
441201af4c0STejun Heo 
4428cfd8147STejun Heo 	if (parent) {
4438cfd8147STejun Heo 		u16 ss_mask = parent->subtree_control;
4448cfd8147STejun Heo 
4458cfd8147STejun Heo 		/* threaded cgroups can only have threaded controllers */
4468cfd8147STejun Heo 		if (cgroup_is_threaded(cgrp))
4478cfd8147STejun Heo 			ss_mask &= cgrp_dfl_threaded_ss_mask;
4488cfd8147STejun Heo 		return ss_mask;
4498cfd8147STejun Heo 	}
450201af4c0STejun Heo 
451201af4c0STejun Heo 	if (cgroup_on_dfl(cgrp))
452201af4c0STejun Heo 		root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
453201af4c0STejun Heo 				  cgrp_dfl_implicit_ss_mask);
454201af4c0STejun Heo 	return root_ss_mask;
455201af4c0STejun Heo }
456201af4c0STejun Heo 
457201af4c0STejun Heo /* subsystems enabled on a cgroup */
cgroup_ss_mask(struct cgroup * cgrp)458201af4c0STejun Heo static u16 cgroup_ss_mask(struct cgroup *cgrp)
459201af4c0STejun Heo {
460201af4c0STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
461201af4c0STejun Heo 
4628cfd8147STejun Heo 	if (parent) {
4638cfd8147STejun Heo 		u16 ss_mask = parent->subtree_ss_mask;
4648cfd8147STejun Heo 
4658cfd8147STejun Heo 		/* threaded cgroups can only have threaded controllers */
4668cfd8147STejun Heo 		if (cgroup_is_threaded(cgrp))
4678cfd8147STejun Heo 			ss_mask &= cgrp_dfl_threaded_ss_mask;
4688cfd8147STejun Heo 		return ss_mask;
4698cfd8147STejun Heo 	}
470201af4c0STejun Heo 
471201af4c0STejun Heo 	return cgrp->root->subsys_mask;
472201af4c0STejun Heo }
473201af4c0STejun Heo 
474201af4c0STejun Heo /**
475201af4c0STejun Heo  * cgroup_css - obtain a cgroup's css for the specified subsystem
476201af4c0STejun Heo  * @cgrp: the cgroup of interest
477201af4c0STejun Heo  * @ss: the subsystem of interest (%NULL returns @cgrp->self)
478201af4c0STejun Heo  *
479201af4c0STejun Heo  * Return @cgrp's css (cgroup_subsys_state) associated with @ss.  This
480201af4c0STejun Heo  * function must be called either under cgroup_mutex or rcu_read_lock() and
481201af4c0STejun Heo  * the caller is responsible for pinning the returned css if it wants to
482201af4c0STejun Heo  * keep accessing it outside the said locks.  This function may return
483201af4c0STejun Heo  * %NULL if @cgrp doesn't have @subsys_id enabled.
484201af4c0STejun Heo  */
cgroup_css(struct cgroup * cgrp,struct cgroup_subsys * ss)485201af4c0STejun Heo static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
486201af4c0STejun Heo 					      struct cgroup_subsys *ss)
487201af4c0STejun Heo {
488d20d30ebSKees Cook 	if (CGROUP_HAS_SUBSYS_CONFIG && ss)
489201af4c0STejun Heo 		return rcu_dereference_check(cgrp->subsys[ss->id],
490201af4c0STejun Heo 					lockdep_is_held(&cgroup_mutex));
491201af4c0STejun Heo 	else
492201af4c0STejun Heo 		return &cgrp->self;
493201af4c0STejun Heo }
494201af4c0STejun Heo 
495201af4c0STejun Heo /**
496fc5a828bSDennis Zhou  * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
497201af4c0STejun Heo  * @cgrp: the cgroup of interest
498201af4c0STejun Heo  * @ss: the subsystem of interest (%NULL returns @cgrp->self)
499201af4c0STejun Heo  *
500201af4c0STejun Heo  * Similar to cgroup_css() but returns the effective css, which is defined
501201af4c0STejun Heo  * as the matching css of the nearest ancestor including self which has @ss
502201af4c0STejun Heo  * enabled.  If @ss is associated with the hierarchy @cgrp is on, this
503201af4c0STejun Heo  * function is guaranteed to return non-NULL css.
504201af4c0STejun Heo  */
cgroup_e_css_by_mask(struct cgroup * cgrp,struct cgroup_subsys * ss)505fc5a828bSDennis Zhou static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
506201af4c0STejun Heo 							struct cgroup_subsys *ss)
507201af4c0STejun Heo {
508201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
509201af4c0STejun Heo 
510201af4c0STejun Heo 	if (!ss)
511201af4c0STejun Heo 		return &cgrp->self;
512201af4c0STejun Heo 
513201af4c0STejun Heo 	/*
514201af4c0STejun Heo 	 * This function is used while updating css associations and thus
515201af4c0STejun Heo 	 * can't test the csses directly.  Test ss_mask.
516201af4c0STejun Heo 	 */
517201af4c0STejun Heo 	while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
518201af4c0STejun Heo 		cgrp = cgroup_parent(cgrp);
519201af4c0STejun Heo 		if (!cgrp)
520201af4c0STejun Heo 			return NULL;
521201af4c0STejun Heo 	}
522201af4c0STejun Heo 
523201af4c0STejun Heo 	return cgroup_css(cgrp, ss);
524201af4c0STejun Heo }
525201af4c0STejun Heo 
526201af4c0STejun Heo /**
527fc5a828bSDennis Zhou  * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
528fc5a828bSDennis Zhou  * @cgrp: the cgroup of interest
529fc5a828bSDennis Zhou  * @ss: the subsystem of interest
530fc5a828bSDennis Zhou  *
531fc5a828bSDennis Zhou  * Find and get the effective css of @cgrp for @ss.  The effective css is
532fc5a828bSDennis Zhou  * defined as the matching css of the nearest ancestor including self which
533fc5a828bSDennis Zhou  * has @ss enabled.  If @ss is not mounted on the hierarchy @cgrp is on,
534fc5a828bSDennis Zhou  * the root css is returned, so this function always returns a valid css.
535fc5a828bSDennis Zhou  *
536fc5a828bSDennis Zhou  * The returned css is not guaranteed to be online, and therefore it is the
53758315c96SBhaskar Chowdhury  * callers responsibility to try get a reference for it.
538fc5a828bSDennis Zhou  */
cgroup_e_css(struct cgroup * cgrp,struct cgroup_subsys * ss)539fc5a828bSDennis Zhou struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
540fc5a828bSDennis Zhou 					 struct cgroup_subsys *ss)
541fc5a828bSDennis Zhou {
542fc5a828bSDennis Zhou 	struct cgroup_subsys_state *css;
543fc5a828bSDennis Zhou 
544d20d30ebSKees Cook 	if (!CGROUP_HAS_SUBSYS_CONFIG)
545d20d30ebSKees Cook 		return NULL;
546d20d30ebSKees Cook 
547fc5a828bSDennis Zhou 	do {
548fc5a828bSDennis Zhou 		css = cgroup_css(cgrp, ss);
549fc5a828bSDennis Zhou 
550fc5a828bSDennis Zhou 		if (css)
551fc5a828bSDennis Zhou 			return css;
552fc5a828bSDennis Zhou 		cgrp = cgroup_parent(cgrp);
553fc5a828bSDennis Zhou 	} while (cgrp);
554fc5a828bSDennis Zhou 
555fc5a828bSDennis Zhou 	return init_css_set.subsys[ss->id];
556fc5a828bSDennis Zhou }
557fc5a828bSDennis Zhou 
558fc5a828bSDennis Zhou /**
559201af4c0STejun Heo  * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
560201af4c0STejun Heo  * @cgrp: the cgroup of interest
561201af4c0STejun Heo  * @ss: the subsystem of interest
562201af4c0STejun Heo  *
563201af4c0STejun Heo  * Find and get the effective css of @cgrp for @ss.  The effective css is
564201af4c0STejun Heo  * defined as the matching css of the nearest ancestor including self which
565201af4c0STejun Heo  * has @ss enabled.  If @ss is not mounted on the hierarchy @cgrp is on,
566201af4c0STejun Heo  * the root css is returned, so this function always returns a valid css.
567201af4c0STejun Heo  * The returned css must be put using css_put().
568201af4c0STejun Heo  */
cgroup_get_e_css(struct cgroup * cgrp,struct cgroup_subsys * ss)569201af4c0STejun Heo struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
570201af4c0STejun Heo 					     struct cgroup_subsys *ss)
571201af4c0STejun Heo {
572201af4c0STejun Heo 	struct cgroup_subsys_state *css;
573201af4c0STejun Heo 
574d20d30ebSKees Cook 	if (!CGROUP_HAS_SUBSYS_CONFIG)
575d20d30ebSKees Cook 		return NULL;
576d20d30ebSKees Cook 
577201af4c0STejun Heo 	rcu_read_lock();
578201af4c0STejun Heo 
579201af4c0STejun Heo 	do {
580201af4c0STejun Heo 		css = cgroup_css(cgrp, ss);
581201af4c0STejun Heo 
582201af4c0STejun Heo 		if (css && css_tryget_online(css))
583201af4c0STejun Heo 			goto out_unlock;
584201af4c0STejun Heo 		cgrp = cgroup_parent(cgrp);
585201af4c0STejun Heo 	} while (cgrp);
586201af4c0STejun Heo 
587201af4c0STejun Heo 	css = init_css_set.subsys[ss->id];
588201af4c0STejun Heo 	css_get(css);
589201af4c0STejun Heo out_unlock:
590201af4c0STejun Heo 	rcu_read_unlock();
591201af4c0STejun Heo 	return css;
592201af4c0STejun Heo }
593c74d40e8SDan Schatzberg EXPORT_SYMBOL_GPL(cgroup_get_e_css);
594201af4c0STejun Heo 
cgroup_get_live(struct cgroup * cgrp)595a590b90dSTejun Heo static void cgroup_get_live(struct cgroup *cgrp)
596201af4c0STejun Heo {
597201af4c0STejun Heo 	WARN_ON_ONCE(cgroup_is_dead(cgrp));
5987bf11e90SGaosheng Cui 	cgroup_get(cgrp);
599201af4c0STejun Heo }
600201af4c0STejun Heo 
601aade7f9eSRoman Gushchin /**
602aade7f9eSRoman Gushchin  * __cgroup_task_count - count the number of tasks in a cgroup. The caller
603aade7f9eSRoman Gushchin  * is responsible for taking the css_set_lock.
604aade7f9eSRoman Gushchin  * @cgrp: the cgroup in question
605aade7f9eSRoman Gushchin  */
__cgroup_task_count(const struct cgroup * cgrp)606aade7f9eSRoman Gushchin int __cgroup_task_count(const struct cgroup *cgrp)
607aade7f9eSRoman Gushchin {
608aade7f9eSRoman Gushchin 	int count = 0;
609aade7f9eSRoman Gushchin 	struct cgrp_cset_link *link;
610aade7f9eSRoman Gushchin 
611aade7f9eSRoman Gushchin 	lockdep_assert_held(&css_set_lock);
612aade7f9eSRoman Gushchin 
613aade7f9eSRoman Gushchin 	list_for_each_entry(link, &cgrp->cset_links, cset_link)
614aade7f9eSRoman Gushchin 		count += link->cset->nr_tasks;
615aade7f9eSRoman Gushchin 
616aade7f9eSRoman Gushchin 	return count;
617aade7f9eSRoman Gushchin }
618aade7f9eSRoman Gushchin 
619aade7f9eSRoman Gushchin /**
620aade7f9eSRoman Gushchin  * cgroup_task_count - count the number of tasks in a cgroup.
621aade7f9eSRoman Gushchin  * @cgrp: the cgroup in question
622aade7f9eSRoman Gushchin  */
cgroup_task_count(const struct cgroup * cgrp)623aade7f9eSRoman Gushchin int cgroup_task_count(const struct cgroup *cgrp)
624aade7f9eSRoman Gushchin {
625aade7f9eSRoman Gushchin 	int count;
626aade7f9eSRoman Gushchin 
627aade7f9eSRoman Gushchin 	spin_lock_irq(&css_set_lock);
628aade7f9eSRoman Gushchin 	count = __cgroup_task_count(cgrp);
629aade7f9eSRoman Gushchin 	spin_unlock_irq(&css_set_lock);
630aade7f9eSRoman Gushchin 
631aade7f9eSRoman Gushchin 	return count;
632aade7f9eSRoman Gushchin }
633aade7f9eSRoman Gushchin 
of_css(struct kernfs_open_file * of)634201af4c0STejun Heo struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
635201af4c0STejun Heo {
636201af4c0STejun Heo 	struct cgroup *cgrp = of->kn->parent->priv;
637201af4c0STejun Heo 	struct cftype *cft = of_cft(of);
638201af4c0STejun Heo 
639201af4c0STejun Heo 	/*
640201af4c0STejun Heo 	 * This is open and unprotected implementation of cgroup_css().
641201af4c0STejun Heo 	 * seq_css() is only called from a kernfs file operation which has
642201af4c0STejun Heo 	 * an active reference on the file.  Because all the subsystem
643201af4c0STejun Heo 	 * files are drained before a css is disassociated with a cgroup,
644201af4c0STejun Heo 	 * the matching css from the cgroup's subsys table is guaranteed to
645201af4c0STejun Heo 	 * be and stay valid until the enclosing operation is complete.
646201af4c0STejun Heo 	 */
647d20d30ebSKees Cook 	if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss)
648201af4c0STejun Heo 		return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
649201af4c0STejun Heo 	else
650201af4c0STejun Heo 		return &cgrp->self;
651201af4c0STejun Heo }
652201af4c0STejun Heo EXPORT_SYMBOL_GPL(of_css);
653201af4c0STejun Heo 
654201af4c0STejun Heo /**
655201af4c0STejun Heo  * for_each_css - iterate all css's of a cgroup
656201af4c0STejun Heo  * @css: the iteration cursor
657201af4c0STejun Heo  * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
658201af4c0STejun Heo  * @cgrp: the target cgroup to iterate css's of
659201af4c0STejun Heo  *
660868f87b3SMiaohe Lin  * Should be called under cgroup_mutex.
661201af4c0STejun Heo  */
662201af4c0STejun Heo #define for_each_css(css, ssid, cgrp)					\
663201af4c0STejun Heo 	for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++)	\
664201af4c0STejun Heo 		if (!((css) = rcu_dereference_check(			\
665201af4c0STejun Heo 				(cgrp)->subsys[(ssid)],			\
666201af4c0STejun Heo 				lockdep_is_held(&cgroup_mutex)))) { }	\
667201af4c0STejun Heo 		else
668201af4c0STejun Heo 
669201af4c0STejun Heo /**
670201af4c0STejun Heo  * do_each_subsys_mask - filter for_each_subsys with a bitmask
671201af4c0STejun Heo  * @ss: the iteration cursor
672201af4c0STejun Heo  * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
673201af4c0STejun Heo  * @ss_mask: the bitmask
674201af4c0STejun Heo  *
675201af4c0STejun Heo  * The block will only run for cases where the ssid-th bit (1 << ssid) of
676201af4c0STejun Heo  * @ss_mask is set.
677201af4c0STejun Heo  */
678201af4c0STejun Heo #define do_each_subsys_mask(ss, ssid, ss_mask) do {			\
679201af4c0STejun Heo 	unsigned long __ss_mask = (ss_mask);				\
680d20d30ebSKees Cook 	if (!CGROUP_HAS_SUBSYS_CONFIG) {				\
681201af4c0STejun Heo 		(ssid) = 0;						\
682201af4c0STejun Heo 		break;							\
683201af4c0STejun Heo 	}								\
684201af4c0STejun Heo 	for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) {	\
685201af4c0STejun Heo 		(ss) = cgroup_subsys[ssid];				\
686201af4c0STejun Heo 		{
687201af4c0STejun Heo 
688201af4c0STejun Heo #define while_each_subsys_mask()					\
689201af4c0STejun Heo 		}							\
690201af4c0STejun Heo 	}								\
691201af4c0STejun Heo } while (false)
692201af4c0STejun Heo 
693201af4c0STejun Heo /* iterate over child cgrps, lock should be held throughout iteration */
694201af4c0STejun Heo #define cgroup_for_each_live_child(child, cgrp)				\
695201af4c0STejun Heo 	list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
696201af4c0STejun Heo 		if (({ lockdep_assert_held(&cgroup_mutex);		\
697201af4c0STejun Heo 		       cgroup_is_dead(child); }))			\
698201af4c0STejun Heo 			;						\
699201af4c0STejun Heo 		else
700201af4c0STejun Heo 
701201af4c0STejun Heo /* walk live descendants in pre order */
702201af4c0STejun Heo #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)		\
703201af4c0STejun Heo 	css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL))	\
704201af4c0STejun Heo 		if (({ lockdep_assert_held(&cgroup_mutex);		\
705201af4c0STejun Heo 		       (dsct) = (d_css)->cgroup;			\
706201af4c0STejun Heo 		       cgroup_is_dead(dsct); }))			\
707201af4c0STejun Heo 			;						\
708201af4c0STejun Heo 		else
709201af4c0STejun Heo 
710201af4c0STejun Heo /* walk live descendants in postorder */
711201af4c0STejun Heo #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp)		\
712201af4c0STejun Heo 	css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL))	\
713201af4c0STejun Heo 		if (({ lockdep_assert_held(&cgroup_mutex);		\
714201af4c0STejun Heo 		       (dsct) = (d_css)->cgroup;			\
715201af4c0STejun Heo 		       cgroup_is_dead(dsct); }))			\
716201af4c0STejun Heo 			;						\
717201af4c0STejun Heo 		else
718201af4c0STejun Heo 
719201af4c0STejun Heo /*
720201af4c0STejun Heo  * The default css_set - used by init and its children prior to any
721201af4c0STejun Heo  * hierarchies being mounted. It contains a pointer to the root state
722201af4c0STejun Heo  * for each subsystem. Also used to anchor the list of css_sets. Not
723201af4c0STejun Heo  * reference-counted, to improve performance when child cgroups
724201af4c0STejun Heo  * haven't been created.
725201af4c0STejun Heo  */
726201af4c0STejun Heo struct css_set init_css_set = {
7274b9502e6SElena Reshetova 	.refcount		= REFCOUNT_INIT(1),
728454000adSTejun Heo 	.dom_cset		= &init_css_set,
729201af4c0STejun Heo 	.tasks			= LIST_HEAD_INIT(init_css_set.tasks),
730201af4c0STejun Heo 	.mg_tasks		= LIST_HEAD_INIT(init_css_set.mg_tasks),
731c03cd773STejun Heo 	.dying_tasks		= LIST_HEAD_INIT(init_css_set.dying_tasks),
732201af4c0STejun Heo 	.task_iters		= LIST_HEAD_INIT(init_css_set.task_iters),
733454000adSTejun Heo 	.threaded_csets		= LIST_HEAD_INIT(init_css_set.threaded_csets),
734201af4c0STejun Heo 	.cgrp_links		= LIST_HEAD_INIT(init_css_set.cgrp_links),
73507fd5b6cSTejun Heo 	.mg_src_preload_node	= LIST_HEAD_INIT(init_css_set.mg_src_preload_node),
73607fd5b6cSTejun Heo 	.mg_dst_preload_node	= LIST_HEAD_INIT(init_css_set.mg_dst_preload_node),
737201af4c0STejun Heo 	.mg_node		= LIST_HEAD_INIT(init_css_set.mg_node),
73838683148STejun Heo 
73938683148STejun Heo 	/*
74038683148STejun Heo 	 * The following field is re-initialized when this cset gets linked
74138683148STejun Heo 	 * in cgroup_init().  However, let's initialize the field
74238683148STejun Heo 	 * statically too so that the default cgroup can be accessed safely
74338683148STejun Heo 	 * early during boot.
74438683148STejun Heo 	 */
74538683148STejun Heo 	.dfl_cgrp		= &cgrp_dfl_root.cgrp,
746201af4c0STejun Heo };
747201af4c0STejun Heo 
748201af4c0STejun Heo static int css_set_count	= 1;	/* 1 for init_css_set */
749201af4c0STejun Heo 
css_set_threaded(struct css_set * cset)750454000adSTejun Heo static bool css_set_threaded(struct css_set *cset)
751454000adSTejun Heo {
752454000adSTejun Heo 	return cset->dom_cset != cset;
753454000adSTejun Heo }
754454000adSTejun Heo 
755201af4c0STejun Heo /**
756201af4c0STejun Heo  * css_set_populated - does a css_set contain any tasks?
757201af4c0STejun Heo  * @cset: target css_set
75873a7242aSWaiman Long  *
75973a7242aSWaiman Long  * css_set_populated() should be the same as !!cset->nr_tasks at steady
76073a7242aSWaiman Long  * state. However, css_set_populated() can be called while a task is being
76173a7242aSWaiman Long  * added to or removed from the linked list before the nr_tasks is
76273a7242aSWaiman Long  * properly updated. Hence, we can't just look at ->nr_tasks here.
763201af4c0STejun Heo  */
css_set_populated(struct css_set * cset)764201af4c0STejun Heo static bool css_set_populated(struct css_set *cset)
765201af4c0STejun Heo {
766201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
767201af4c0STejun Heo 
768201af4c0STejun Heo 	return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
769201af4c0STejun Heo }
770201af4c0STejun Heo 
771201af4c0STejun Heo /**
772788b950cSTejun Heo  * cgroup_update_populated - update the populated count of a cgroup
773201af4c0STejun Heo  * @cgrp: the target cgroup
774201af4c0STejun Heo  * @populated: inc or dec populated count
775201af4c0STejun Heo  *
776201af4c0STejun Heo  * One of the css_sets associated with @cgrp is either getting its first
777788b950cSTejun Heo  * task or losing the last.  Update @cgrp->nr_populated_* accordingly.  The
778788b950cSTejun Heo  * count is propagated towards root so that a given cgroup's
779788b950cSTejun Heo  * nr_populated_children is zero iff none of its descendants contain any
780788b950cSTejun Heo  * tasks.
781201af4c0STejun Heo  *
782788b950cSTejun Heo  * @cgrp's interface file "cgroup.populated" is zero if both
783788b950cSTejun Heo  * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
784788b950cSTejun Heo  * 1 otherwise.  When the sum changes from or to zero, userland is notified
785788b950cSTejun Heo  * that the content of the interface file has changed.  This can be used to
786788b950cSTejun Heo  * detect when @cgrp and its descendants become populated or empty.
787201af4c0STejun Heo  */
cgroup_update_populated(struct cgroup * cgrp,bool populated)788201af4c0STejun Heo static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
789201af4c0STejun Heo {
790788b950cSTejun Heo 	struct cgroup *child = NULL;
791788b950cSTejun Heo 	int adj = populated ? 1 : -1;
792788b950cSTejun Heo 
793201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
794201af4c0STejun Heo 
795201af4c0STejun Heo 	do {
796788b950cSTejun Heo 		bool was_populated = cgroup_is_populated(cgrp);
797201af4c0STejun Heo 
798454000adSTejun Heo 		if (!child) {
799788b950cSTejun Heo 			cgrp->nr_populated_csets += adj;
800454000adSTejun Heo 		} else {
801454000adSTejun Heo 			if (cgroup_is_threaded(child))
802454000adSTejun Heo 				cgrp->nr_populated_threaded_children += adj;
803201af4c0STejun Heo 			else
804454000adSTejun Heo 				cgrp->nr_populated_domain_children += adj;
805454000adSTejun Heo 		}
806201af4c0STejun Heo 
807788b950cSTejun Heo 		if (was_populated == cgroup_is_populated(cgrp))
808201af4c0STejun Heo 			break;
809201af4c0STejun Heo 
810d62beb7fSTejun Heo 		cgroup1_check_for_release(cgrp);
8114c476d8cSRoman Gushchin 		TRACE_CGROUP_PATH(notify_populated, cgrp,
8124c476d8cSRoman Gushchin 				  cgroup_is_populated(cgrp));
813201af4c0STejun Heo 		cgroup_file_notify(&cgrp->events_file);
814201af4c0STejun Heo 
815788b950cSTejun Heo 		child = cgrp;
816201af4c0STejun Heo 		cgrp = cgroup_parent(cgrp);
817201af4c0STejun Heo 	} while (cgrp);
818201af4c0STejun Heo }
819201af4c0STejun Heo 
820201af4c0STejun Heo /**
821201af4c0STejun Heo  * css_set_update_populated - update populated state of a css_set
822201af4c0STejun Heo  * @cset: target css_set
823201af4c0STejun Heo  * @populated: whether @cset is populated or depopulated
824201af4c0STejun Heo  *
825201af4c0STejun Heo  * @cset is either getting the first task or losing the last.  Update the
826788b950cSTejun Heo  * populated counters of all associated cgroups accordingly.
827201af4c0STejun Heo  */
css_set_update_populated(struct css_set * cset,bool populated)828201af4c0STejun Heo static void css_set_update_populated(struct css_set *cset, bool populated)
829201af4c0STejun Heo {
830201af4c0STejun Heo 	struct cgrp_cset_link *link;
831201af4c0STejun Heo 
832201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
833201af4c0STejun Heo 
834201af4c0STejun Heo 	list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
835201af4c0STejun Heo 		cgroup_update_populated(link->cgrp, populated);
836201af4c0STejun Heo }
837201af4c0STejun Heo 
838b636fd38STejun Heo /*
839b636fd38STejun Heo  * @task is leaving, advance task iterators which are pointing to it so
840b636fd38STejun Heo  * that they can resume at the next position.  Advancing an iterator might
841b636fd38STejun Heo  * remove it from the list, use safe walk.  See css_task_iter_skip() for
842b636fd38STejun Heo  * details.
843b636fd38STejun Heo  */
css_set_skip_task_iters(struct css_set * cset,struct task_struct * task)844b636fd38STejun Heo static void css_set_skip_task_iters(struct css_set *cset,
845b636fd38STejun Heo 				    struct task_struct *task)
846b636fd38STejun Heo {
847b636fd38STejun Heo 	struct css_task_iter *it, *pos;
848b636fd38STejun Heo 
849b636fd38STejun Heo 	list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node)
850b636fd38STejun Heo 		css_task_iter_skip(it, task);
851b636fd38STejun Heo }
852b636fd38STejun Heo 
853201af4c0STejun Heo /**
854201af4c0STejun Heo  * css_set_move_task - move a task from one css_set to another
855201af4c0STejun Heo  * @task: task being moved
856201af4c0STejun Heo  * @from_cset: css_set @task currently belongs to (may be NULL)
857201af4c0STejun Heo  * @to_cset: new css_set @task is being moved to (may be NULL)
858201af4c0STejun Heo  * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
859201af4c0STejun Heo  *
860201af4c0STejun Heo  * Move @task from @from_cset to @to_cset.  If @task didn't belong to any
861201af4c0STejun Heo  * css_set, @from_cset can be NULL.  If @task is being disassociated
862201af4c0STejun Heo  * instead of moved, @to_cset can be NULL.
863201af4c0STejun Heo  *
864788b950cSTejun Heo  * This function automatically handles populated counter updates and
865201af4c0STejun Heo  * css_task_iter adjustments but the caller is responsible for managing
866201af4c0STejun Heo  * @from_cset and @to_cset's reference counts.
867201af4c0STejun Heo  */
css_set_move_task(struct task_struct * task,struct css_set * from_cset,struct css_set * to_cset,bool use_mg_tasks)868201af4c0STejun Heo static void css_set_move_task(struct task_struct *task,
869201af4c0STejun Heo 			      struct css_set *from_cset, struct css_set *to_cset,
870201af4c0STejun Heo 			      bool use_mg_tasks)
871201af4c0STejun Heo {
872201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
873201af4c0STejun Heo 
874201af4c0STejun Heo 	if (to_cset && !css_set_populated(to_cset))
875201af4c0STejun Heo 		css_set_update_populated(to_cset, true);
876201af4c0STejun Heo 
877201af4c0STejun Heo 	if (from_cset) {
878201af4c0STejun Heo 		WARN_ON_ONCE(list_empty(&task->cg_list));
879201af4c0STejun Heo 
880b636fd38STejun Heo 		css_set_skip_task_iters(from_cset, task);
881201af4c0STejun Heo 		list_del_init(&task->cg_list);
882201af4c0STejun Heo 		if (!css_set_populated(from_cset))
883201af4c0STejun Heo 			css_set_update_populated(from_cset, false);
884201af4c0STejun Heo 	} else {
885201af4c0STejun Heo 		WARN_ON_ONCE(!list_empty(&task->cg_list));
886201af4c0STejun Heo 	}
887201af4c0STejun Heo 
888201af4c0STejun Heo 	if (to_cset) {
889201af4c0STejun Heo 		/*
890201af4c0STejun Heo 		 * We are synchronized through cgroup_threadgroup_rwsem
891201af4c0STejun Heo 		 * against PF_EXITING setting such that we can't race
892e7c7b1d8SMichal Koutný 		 * against cgroup_exit()/cgroup_free() dropping the css_set.
893201af4c0STejun Heo 		 */
894201af4c0STejun Heo 		WARN_ON_ONCE(task->flags & PF_EXITING);
895201af4c0STejun Heo 
8962ce7135aSJohannes Weiner 		cgroup_move_task(task, to_cset);
897201af4c0STejun Heo 		list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
898201af4c0STejun Heo 							     &to_cset->tasks);
899201af4c0STejun Heo 	}
900201af4c0STejun Heo }
901201af4c0STejun Heo 
902201af4c0STejun Heo /*
903201af4c0STejun Heo  * hash table for cgroup groups. This improves the performance to find
904201af4c0STejun Heo  * an existing css_set. This hash doesn't (currently) take into
905201af4c0STejun Heo  * account cgroups in empty hierarchies.
906201af4c0STejun Heo  */
907201af4c0STejun Heo #define CSS_SET_HASH_BITS	7
908201af4c0STejun Heo static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
909201af4c0STejun Heo 
css_set_hash(struct cgroup_subsys_state ** css)91078d44b82SGustavo A. R. Silva static unsigned long css_set_hash(struct cgroup_subsys_state **css)
911201af4c0STejun Heo {
912201af4c0STejun Heo 	unsigned long key = 0UL;
913201af4c0STejun Heo 	struct cgroup_subsys *ss;
914201af4c0STejun Heo 	int i;
915201af4c0STejun Heo 
916201af4c0STejun Heo 	for_each_subsys(ss, i)
917201af4c0STejun Heo 		key += (unsigned long)css[i];
918201af4c0STejun Heo 	key = (key >> 16) ^ key;
919201af4c0STejun Heo 
920201af4c0STejun Heo 	return key;
921201af4c0STejun Heo }
922201af4c0STejun Heo 
put_css_set_locked(struct css_set * cset)923dcfe149bSTejun Heo void put_css_set_locked(struct css_set *cset)
924201af4c0STejun Heo {
925201af4c0STejun Heo 	struct cgrp_cset_link *link, *tmp_link;
926201af4c0STejun Heo 	struct cgroup_subsys *ss;
927201af4c0STejun Heo 	int ssid;
928201af4c0STejun Heo 
929201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
930201af4c0STejun Heo 
9314b9502e6SElena Reshetova 	if (!refcount_dec_and_test(&cset->refcount))
932201af4c0STejun Heo 		return;
933201af4c0STejun Heo 
934454000adSTejun Heo 	WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
935454000adSTejun Heo 
93658315c96SBhaskar Chowdhury 	/* This css_set is dead. Unlink it and release cgroup and css refs */
937201af4c0STejun Heo 	for_each_subsys(ss, ssid) {
938201af4c0STejun Heo 		list_del(&cset->e_cset_node[ssid]);
939201af4c0STejun Heo 		css_put(cset->subsys[ssid]);
940201af4c0STejun Heo 	}
941201af4c0STejun Heo 	hash_del(&cset->hlist);
942201af4c0STejun Heo 	css_set_count--;
943201af4c0STejun Heo 
944201af4c0STejun Heo 	list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
945201af4c0STejun Heo 		list_del(&link->cset_link);
946201af4c0STejun Heo 		list_del(&link->cgrp_link);
947201af4c0STejun Heo 		if (cgroup_parent(link->cgrp))
948201af4c0STejun Heo 			cgroup_put(link->cgrp);
949201af4c0STejun Heo 		kfree(link);
950201af4c0STejun Heo 	}
951201af4c0STejun Heo 
952454000adSTejun Heo 	if (css_set_threaded(cset)) {
953454000adSTejun Heo 		list_del(&cset->threaded_csets_node);
954454000adSTejun Heo 		put_css_set_locked(cset->dom_cset);
955454000adSTejun Heo 	}
956454000adSTejun Heo 
957201af4c0STejun Heo 	kfree_rcu(cset, rcu_head);
958201af4c0STejun Heo }
959201af4c0STejun Heo 
960201af4c0STejun Heo /**
961201af4c0STejun Heo  * compare_css_sets - helper function for find_existing_css_set().
962201af4c0STejun Heo  * @cset: candidate css_set being tested
963201af4c0STejun Heo  * @old_cset: existing css_set for a task
964201af4c0STejun Heo  * @new_cgrp: cgroup that's being entered by the task
965201af4c0STejun Heo  * @template: desired set of css pointers in css_set (pre-calculated)
966201af4c0STejun Heo  *
967201af4c0STejun Heo  * Returns true if "cset" matches "old_cset" except for the hierarchy
968201af4c0STejun Heo  * which "new_cgrp" belongs to, for which it should match "new_cgrp".
969201af4c0STejun Heo  */
compare_css_sets(struct css_set * cset,struct css_set * old_cset,struct cgroup * new_cgrp,struct cgroup_subsys_state * template[])970201af4c0STejun Heo static bool compare_css_sets(struct css_set *cset,
971201af4c0STejun Heo 			     struct css_set *old_cset,
972201af4c0STejun Heo 			     struct cgroup *new_cgrp,
973201af4c0STejun Heo 			     struct cgroup_subsys_state *template[])
974201af4c0STejun Heo {
975454000adSTejun Heo 	struct cgroup *new_dfl_cgrp;
976201af4c0STejun Heo 	struct list_head *l1, *l2;
977201af4c0STejun Heo 
978201af4c0STejun Heo 	/*
979201af4c0STejun Heo 	 * On the default hierarchy, there can be csets which are
980201af4c0STejun Heo 	 * associated with the same set of cgroups but different csses.
981201af4c0STejun Heo 	 * Let's first ensure that csses match.
982201af4c0STejun Heo 	 */
983201af4c0STejun Heo 	if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
984201af4c0STejun Heo 		return false;
985201af4c0STejun Heo 
986454000adSTejun Heo 
987454000adSTejun Heo 	/* @cset's domain should match the default cgroup's */
988454000adSTejun Heo 	if (cgroup_on_dfl(new_cgrp))
989454000adSTejun Heo 		new_dfl_cgrp = new_cgrp;
990454000adSTejun Heo 	else
991454000adSTejun Heo 		new_dfl_cgrp = old_cset->dfl_cgrp;
992454000adSTejun Heo 
993454000adSTejun Heo 	if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
994454000adSTejun Heo 		return false;
995454000adSTejun Heo 
996201af4c0STejun Heo 	/*
997201af4c0STejun Heo 	 * Compare cgroup pointers in order to distinguish between
998201af4c0STejun Heo 	 * different cgroups in hierarchies.  As different cgroups may
999201af4c0STejun Heo 	 * share the same effective css, this comparison is always
1000201af4c0STejun Heo 	 * necessary.
1001201af4c0STejun Heo 	 */
1002201af4c0STejun Heo 	l1 = &cset->cgrp_links;
1003201af4c0STejun Heo 	l2 = &old_cset->cgrp_links;
1004201af4c0STejun Heo 	while (1) {
1005201af4c0STejun Heo 		struct cgrp_cset_link *link1, *link2;
1006201af4c0STejun Heo 		struct cgroup *cgrp1, *cgrp2;
1007201af4c0STejun Heo 
1008201af4c0STejun Heo 		l1 = l1->next;
1009201af4c0STejun Heo 		l2 = l2->next;
1010201af4c0STejun Heo 		/* See if we reached the end - both lists are equal length. */
1011201af4c0STejun Heo 		if (l1 == &cset->cgrp_links) {
1012201af4c0STejun Heo 			BUG_ON(l2 != &old_cset->cgrp_links);
1013201af4c0STejun Heo 			break;
1014201af4c0STejun Heo 		} else {
1015201af4c0STejun Heo 			BUG_ON(l2 == &old_cset->cgrp_links);
1016201af4c0STejun Heo 		}
1017201af4c0STejun Heo 		/* Locate the cgroups associated with these links. */
1018201af4c0STejun Heo 		link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
1019201af4c0STejun Heo 		link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
1020201af4c0STejun Heo 		cgrp1 = link1->cgrp;
1021201af4c0STejun Heo 		cgrp2 = link2->cgrp;
1022201af4c0STejun Heo 		/* Hierarchies should be linked in the same order. */
1023201af4c0STejun Heo 		BUG_ON(cgrp1->root != cgrp2->root);
1024201af4c0STejun Heo 
1025201af4c0STejun Heo 		/*
1026201af4c0STejun Heo 		 * If this hierarchy is the hierarchy of the cgroup
1027201af4c0STejun Heo 		 * that's changing, then we need to check that this
1028201af4c0STejun Heo 		 * css_set points to the new cgroup; if it's any other
1029201af4c0STejun Heo 		 * hierarchy, then this css_set should point to the
1030201af4c0STejun Heo 		 * same cgroup as the old css_set.
1031201af4c0STejun Heo 		 */
1032201af4c0STejun Heo 		if (cgrp1->root == new_cgrp->root) {
1033201af4c0STejun Heo 			if (cgrp1 != new_cgrp)
1034201af4c0STejun Heo 				return false;
1035201af4c0STejun Heo 		} else {
1036201af4c0STejun Heo 			if (cgrp1 != cgrp2)
1037201af4c0STejun Heo 				return false;
1038201af4c0STejun Heo 		}
1039201af4c0STejun Heo 	}
1040201af4c0STejun Heo 	return true;
1041201af4c0STejun Heo }
1042201af4c0STejun Heo 
1043201af4c0STejun Heo /**
1044201af4c0STejun Heo  * find_existing_css_set - init css array and find the matching css_set
1045201af4c0STejun Heo  * @old_cset: the css_set that we're using before the cgroup transition
1046201af4c0STejun Heo  * @cgrp: the cgroup that we're moving into
1047201af4c0STejun Heo  * @template: out param for the new set of csses, should be clear on entry
1048201af4c0STejun Heo  */
find_existing_css_set(struct css_set * old_cset,struct cgroup * cgrp,struct cgroup_subsys_state ** template)1049201af4c0STejun Heo static struct css_set *find_existing_css_set(struct css_set *old_cset,
1050201af4c0STejun Heo 					struct cgroup *cgrp,
105178d44b82SGustavo A. R. Silva 					struct cgroup_subsys_state **template)
1052201af4c0STejun Heo {
1053201af4c0STejun Heo 	struct cgroup_root *root = cgrp->root;
1054201af4c0STejun Heo 	struct cgroup_subsys *ss;
1055201af4c0STejun Heo 	struct css_set *cset;
1056201af4c0STejun Heo 	unsigned long key;
1057201af4c0STejun Heo 	int i;
1058201af4c0STejun Heo 
1059201af4c0STejun Heo 	/*
1060201af4c0STejun Heo 	 * Build the set of subsystem state objects that we want to see in the
106158315c96SBhaskar Chowdhury 	 * new css_set. While subsystems can change globally, the entries here
1062201af4c0STejun Heo 	 * won't change, so no need for locking.
1063201af4c0STejun Heo 	 */
1064201af4c0STejun Heo 	for_each_subsys(ss, i) {
1065201af4c0STejun Heo 		if (root->subsys_mask & (1UL << i)) {
1066201af4c0STejun Heo 			/*
1067201af4c0STejun Heo 			 * @ss is in this hierarchy, so we want the
1068201af4c0STejun Heo 			 * effective css from @cgrp.
1069201af4c0STejun Heo 			 */
1070fc5a828bSDennis Zhou 			template[i] = cgroup_e_css_by_mask(cgrp, ss);
1071201af4c0STejun Heo 		} else {
1072201af4c0STejun Heo 			/*
1073201af4c0STejun Heo 			 * @ss is not in this hierarchy, so we don't want
1074201af4c0STejun Heo 			 * to change the css.
1075201af4c0STejun Heo 			 */
1076201af4c0STejun Heo 			template[i] = old_cset->subsys[i];
1077201af4c0STejun Heo 		}
1078201af4c0STejun Heo 	}
1079201af4c0STejun Heo 
1080201af4c0STejun Heo 	key = css_set_hash(template);
1081201af4c0STejun Heo 	hash_for_each_possible(css_set_table, cset, hlist, key) {
1082201af4c0STejun Heo 		if (!compare_css_sets(cset, old_cset, cgrp, template))
1083201af4c0STejun Heo 			continue;
1084201af4c0STejun Heo 
1085201af4c0STejun Heo 		/* This css_set matches what we need */
1086201af4c0STejun Heo 		return cset;
1087201af4c0STejun Heo 	}
1088201af4c0STejun Heo 
1089201af4c0STejun Heo 	/* No existing cgroup group matched */
1090201af4c0STejun Heo 	return NULL;
1091201af4c0STejun Heo }
1092201af4c0STejun Heo 
free_cgrp_cset_links(struct list_head * links_to_free)1093201af4c0STejun Heo static void free_cgrp_cset_links(struct list_head *links_to_free)
1094201af4c0STejun Heo {
1095201af4c0STejun Heo 	struct cgrp_cset_link *link, *tmp_link;
1096201af4c0STejun Heo 
1097201af4c0STejun Heo 	list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1098201af4c0STejun Heo 		list_del(&link->cset_link);
1099201af4c0STejun Heo 		kfree(link);
1100201af4c0STejun Heo 	}
1101201af4c0STejun Heo }
1102201af4c0STejun Heo 
1103201af4c0STejun Heo /**
1104201af4c0STejun Heo  * allocate_cgrp_cset_links - allocate cgrp_cset_links
1105201af4c0STejun Heo  * @count: the number of links to allocate
1106201af4c0STejun Heo  * @tmp_links: list_head the allocated links are put on
1107201af4c0STejun Heo  *
1108201af4c0STejun Heo  * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1109201af4c0STejun Heo  * through ->cset_link.  Returns 0 on success or -errno.
1110201af4c0STejun Heo  */
allocate_cgrp_cset_links(int count,struct list_head * tmp_links)1111201af4c0STejun Heo static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
1112201af4c0STejun Heo {
1113201af4c0STejun Heo 	struct cgrp_cset_link *link;
1114201af4c0STejun Heo 	int i;
1115201af4c0STejun Heo 
1116201af4c0STejun Heo 	INIT_LIST_HEAD(tmp_links);
1117201af4c0STejun Heo 
1118201af4c0STejun Heo 	for (i = 0; i < count; i++) {
1119201af4c0STejun Heo 		link = kzalloc(sizeof(*link), GFP_KERNEL);
1120201af4c0STejun Heo 		if (!link) {
1121201af4c0STejun Heo 			free_cgrp_cset_links(tmp_links);
1122201af4c0STejun Heo 			return -ENOMEM;
1123201af4c0STejun Heo 		}
1124201af4c0STejun Heo 		list_add(&link->cset_link, tmp_links);
1125201af4c0STejun Heo 	}
1126201af4c0STejun Heo 	return 0;
1127201af4c0STejun Heo }
1128201af4c0STejun Heo 
1129201af4c0STejun Heo /**
1130201af4c0STejun Heo  * link_css_set - a helper function to link a css_set to a cgroup
1131201af4c0STejun Heo  * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
1132201af4c0STejun Heo  * @cset: the css_set to be linked
1133201af4c0STejun Heo  * @cgrp: the destination cgroup
1134201af4c0STejun Heo  */
link_css_set(struct list_head * tmp_links,struct css_set * cset,struct cgroup * cgrp)1135201af4c0STejun Heo static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1136201af4c0STejun Heo 			 struct cgroup *cgrp)
1137201af4c0STejun Heo {
1138201af4c0STejun Heo 	struct cgrp_cset_link *link;
1139201af4c0STejun Heo 
1140201af4c0STejun Heo 	BUG_ON(list_empty(tmp_links));
1141201af4c0STejun Heo 
1142201af4c0STejun Heo 	if (cgroup_on_dfl(cgrp))
1143201af4c0STejun Heo 		cset->dfl_cgrp = cgrp;
1144201af4c0STejun Heo 
1145201af4c0STejun Heo 	link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1146201af4c0STejun Heo 	link->cset = cset;
1147201af4c0STejun Heo 	link->cgrp = cgrp;
1148201af4c0STejun Heo 
1149201af4c0STejun Heo 	/*
1150201af4c0STejun Heo 	 * Always add links to the tail of the lists so that the lists are
115158315c96SBhaskar Chowdhury 	 * in chronological order.
1152201af4c0STejun Heo 	 */
1153201af4c0STejun Heo 	list_move_tail(&link->cset_link, &cgrp->cset_links);
1154201af4c0STejun Heo 	list_add_tail(&link->cgrp_link, &cset->cgrp_links);
1155201af4c0STejun Heo 
1156201af4c0STejun Heo 	if (cgroup_parent(cgrp))
1157a590b90dSTejun Heo 		cgroup_get_live(cgrp);
1158201af4c0STejun Heo }
1159201af4c0STejun Heo 
1160201af4c0STejun Heo /**
1161201af4c0STejun Heo  * find_css_set - return a new css_set with one cgroup updated
1162201af4c0STejun Heo  * @old_cset: the baseline css_set
1163201af4c0STejun Heo  * @cgrp: the cgroup to be updated
1164201af4c0STejun Heo  *
1165201af4c0STejun Heo  * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1166201af4c0STejun Heo  * substituted into the appropriate hierarchy.
1167201af4c0STejun Heo  */
find_css_set(struct css_set * old_cset,struct cgroup * cgrp)1168201af4c0STejun Heo static struct css_set *find_css_set(struct css_set *old_cset,
1169201af4c0STejun Heo 				    struct cgroup *cgrp)
1170201af4c0STejun Heo {
1171201af4c0STejun Heo 	struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
1172201af4c0STejun Heo 	struct css_set *cset;
1173201af4c0STejun Heo 	struct list_head tmp_links;
1174201af4c0STejun Heo 	struct cgrp_cset_link *link;
1175201af4c0STejun Heo 	struct cgroup_subsys *ss;
1176201af4c0STejun Heo 	unsigned long key;
1177201af4c0STejun Heo 	int ssid;
1178201af4c0STejun Heo 
1179201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1180201af4c0STejun Heo 
1181201af4c0STejun Heo 	/* First see if we already have a cgroup group that matches
1182201af4c0STejun Heo 	 * the desired set */
1183201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
1184201af4c0STejun Heo 	cset = find_existing_css_set(old_cset, cgrp, template);
1185201af4c0STejun Heo 	if (cset)
1186201af4c0STejun Heo 		get_css_set(cset);
1187201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
1188201af4c0STejun Heo 
1189201af4c0STejun Heo 	if (cset)
1190201af4c0STejun Heo 		return cset;
1191201af4c0STejun Heo 
1192201af4c0STejun Heo 	cset = kzalloc(sizeof(*cset), GFP_KERNEL);
1193201af4c0STejun Heo 	if (!cset)
1194201af4c0STejun Heo 		return NULL;
1195201af4c0STejun Heo 
1196201af4c0STejun Heo 	/* Allocate all the cgrp_cset_link objects that we'll need */
1197201af4c0STejun Heo 	if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
1198201af4c0STejun Heo 		kfree(cset);
1199201af4c0STejun Heo 		return NULL;
1200201af4c0STejun Heo 	}
1201201af4c0STejun Heo 
12024b9502e6SElena Reshetova 	refcount_set(&cset->refcount, 1);
1203454000adSTejun Heo 	cset->dom_cset = cset;
1204201af4c0STejun Heo 	INIT_LIST_HEAD(&cset->tasks);
1205201af4c0STejun Heo 	INIT_LIST_HEAD(&cset->mg_tasks);
1206c03cd773STejun Heo 	INIT_LIST_HEAD(&cset->dying_tasks);
1207201af4c0STejun Heo 	INIT_LIST_HEAD(&cset->task_iters);
1208454000adSTejun Heo 	INIT_LIST_HEAD(&cset->threaded_csets);
1209201af4c0STejun Heo 	INIT_HLIST_NODE(&cset->hlist);
1210201af4c0STejun Heo 	INIT_LIST_HEAD(&cset->cgrp_links);
121107fd5b6cSTejun Heo 	INIT_LIST_HEAD(&cset->mg_src_preload_node);
121207fd5b6cSTejun Heo 	INIT_LIST_HEAD(&cset->mg_dst_preload_node);
1213201af4c0STejun Heo 	INIT_LIST_HEAD(&cset->mg_node);
1214201af4c0STejun Heo 
1215201af4c0STejun Heo 	/* Copy the set of subsystem state objects generated in
1216201af4c0STejun Heo 	 * find_existing_css_set() */
1217201af4c0STejun Heo 	memcpy(cset->subsys, template, sizeof(cset->subsys));
1218201af4c0STejun Heo 
1219201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
1220201af4c0STejun Heo 	/* Add reference counts and links from the new css_set. */
1221201af4c0STejun Heo 	list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
1222201af4c0STejun Heo 		struct cgroup *c = link->cgrp;
1223201af4c0STejun Heo 
1224201af4c0STejun Heo 		if (c->root == cgrp->root)
1225201af4c0STejun Heo 			c = cgrp;
1226201af4c0STejun Heo 		link_css_set(&tmp_links, cset, c);
1227201af4c0STejun Heo 	}
1228201af4c0STejun Heo 
1229201af4c0STejun Heo 	BUG_ON(!list_empty(&tmp_links));
1230201af4c0STejun Heo 
1231201af4c0STejun Heo 	css_set_count++;
1232201af4c0STejun Heo 
1233201af4c0STejun Heo 	/* Add @cset to the hash table */
1234201af4c0STejun Heo 	key = css_set_hash(cset->subsys);
1235201af4c0STejun Heo 	hash_add(css_set_table, &cset->hlist, key);
1236201af4c0STejun Heo 
1237201af4c0STejun Heo 	for_each_subsys(ss, ssid) {
1238201af4c0STejun Heo 		struct cgroup_subsys_state *css = cset->subsys[ssid];
1239201af4c0STejun Heo 
1240201af4c0STejun Heo 		list_add_tail(&cset->e_cset_node[ssid],
1241201af4c0STejun Heo 			      &css->cgroup->e_csets[ssid]);
1242201af4c0STejun Heo 		css_get(css);
1243201af4c0STejun Heo 	}
1244201af4c0STejun Heo 
1245201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
1246201af4c0STejun Heo 
1247454000adSTejun Heo 	/*
1248454000adSTejun Heo 	 * If @cset should be threaded, look up the matching dom_cset and
1249454000adSTejun Heo 	 * link them up.  We first fully initialize @cset then look for the
1250454000adSTejun Heo 	 * dom_cset.  It's simpler this way and safe as @cset is guaranteed
1251454000adSTejun Heo 	 * to stay empty until we return.
1252454000adSTejun Heo 	 */
1253454000adSTejun Heo 	if (cgroup_is_threaded(cset->dfl_cgrp)) {
1254454000adSTejun Heo 		struct css_set *dcset;
1255454000adSTejun Heo 
1256454000adSTejun Heo 		dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1257454000adSTejun Heo 		if (!dcset) {
1258454000adSTejun Heo 			put_css_set(cset);
1259454000adSTejun Heo 			return NULL;
1260454000adSTejun Heo 		}
1261454000adSTejun Heo 
1262454000adSTejun Heo 		spin_lock_irq(&css_set_lock);
1263454000adSTejun Heo 		cset->dom_cset = dcset;
1264454000adSTejun Heo 		list_add_tail(&cset->threaded_csets_node,
1265454000adSTejun Heo 			      &dcset->threaded_csets);
1266454000adSTejun Heo 		spin_unlock_irq(&css_set_lock);
1267454000adSTejun Heo 	}
1268454000adSTejun Heo 
1269201af4c0STejun Heo 	return cset;
1270201af4c0STejun Heo }
1271201af4c0STejun Heo 
cgroup_root_from_kf(struct kernfs_root * kf_root)12720a268dbdSTejun Heo struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
1273201af4c0STejun Heo {
1274f2eb478fSGreg Kroah-Hartman 	struct cgroup *root_cgrp = kernfs_root_to_node(kf_root)->priv;
1275201af4c0STejun Heo 
1276201af4c0STejun Heo 	return root_cgrp->root;
1277201af4c0STejun Heo }
1278201af4c0STejun Heo 
cgroup_favor_dynmods(struct cgroup_root * root,bool favor)12796a010a49STejun Heo void cgroup_favor_dynmods(struct cgroup_root *root, bool favor)
12806a010a49STejun Heo {
12816a010a49STejun Heo 	bool favoring = root->flags & CGRP_ROOT_FAVOR_DYNMODS;
12826a010a49STejun Heo 
12836a010a49STejun Heo 	/* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition */
12846a010a49STejun Heo 	if (favor && !favoring) {
12856a010a49STejun Heo 		rcu_sync_enter(&cgroup_threadgroup_rwsem.rss);
12866a010a49STejun Heo 		root->flags |= CGRP_ROOT_FAVOR_DYNMODS;
12876a010a49STejun Heo 	} else if (!favor && favoring) {
12886a010a49STejun Heo 		rcu_sync_exit(&cgroup_threadgroup_rwsem.rss);
12896a010a49STejun Heo 		root->flags &= ~CGRP_ROOT_FAVOR_DYNMODS;
12906a010a49STejun Heo 	}
12916a010a49STejun Heo }
12926a010a49STejun Heo 
cgroup_init_root_id(struct cgroup_root * root)1293201af4c0STejun Heo static int cgroup_init_root_id(struct cgroup_root *root)
1294201af4c0STejun Heo {
1295201af4c0STejun Heo 	int id;
1296201af4c0STejun Heo 
1297201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1298201af4c0STejun Heo 
1299201af4c0STejun Heo 	id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
1300201af4c0STejun Heo 	if (id < 0)
1301201af4c0STejun Heo 		return id;
1302201af4c0STejun Heo 
1303201af4c0STejun Heo 	root->hierarchy_id = id;
1304201af4c0STejun Heo 	return 0;
1305201af4c0STejun Heo }
1306201af4c0STejun Heo 
cgroup_exit_root_id(struct cgroup_root * root)1307201af4c0STejun Heo static void cgroup_exit_root_id(struct cgroup_root *root)
1308201af4c0STejun Heo {
1309201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1310201af4c0STejun Heo 
1311201af4c0STejun Heo 	idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1312201af4c0STejun Heo }
1313201af4c0STejun Heo 
cgroup_free_root(struct cgroup_root * root)13141592c9b2STejun Heo void cgroup_free_root(struct cgroup_root *root)
1315201af4c0STejun Heo {
1316201af4c0STejun Heo 	kfree(root);
1317201af4c0STejun Heo }
1318201af4c0STejun Heo 
cgroup_destroy_root(struct cgroup_root * root)1319201af4c0STejun Heo static void cgroup_destroy_root(struct cgroup_root *root)
1320201af4c0STejun Heo {
1321201af4c0STejun Heo 	struct cgroup *cgrp = &root->cgrp;
1322201af4c0STejun Heo 	struct cgrp_cset_link *link, *tmp_link;
1323201af4c0STejun Heo 
1324201af4c0STejun Heo 	trace_cgroup_destroy_root(root);
1325201af4c0STejun Heo 
1326201af4c0STejun Heo 	cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1327201af4c0STejun Heo 
1328201af4c0STejun Heo 	BUG_ON(atomic_read(&root->nr_cgrps));
1329201af4c0STejun Heo 	BUG_ON(!list_empty(&cgrp->self.children));
1330201af4c0STejun Heo 
1331201af4c0STejun Heo 	/* Rebind all subsystems back to the default hierarchy */
1332201af4c0STejun Heo 	WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
1333201af4c0STejun Heo 
1334201af4c0STejun Heo 	/*
1335201af4c0STejun Heo 	 * Release all the links from cset_links to this hierarchy's
1336201af4c0STejun Heo 	 * root cgroup
1337201af4c0STejun Heo 	 */
1338201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
1339201af4c0STejun Heo 
1340201af4c0STejun Heo 	list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1341201af4c0STejun Heo 		list_del(&link->cset_link);
1342201af4c0STejun Heo 		list_del(&link->cgrp_link);
1343201af4c0STejun Heo 		kfree(link);
1344201af4c0STejun Heo 	}
1345201af4c0STejun Heo 
1346201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
1347201af4c0STejun Heo 
1348201af4c0STejun Heo 	if (!list_empty(&root->root_list)) {
1349201af4c0STejun Heo 		list_del(&root->root_list);
1350201af4c0STejun Heo 		cgroup_root_count--;
1351201af4c0STejun Heo 	}
1352201af4c0STejun Heo 
13536a010a49STejun Heo 	cgroup_favor_dynmods(root, false);
1354201af4c0STejun Heo 	cgroup_exit_root_id(root);
1355201af4c0STejun Heo 
13564cdb91b0SKamalesh Babulal 	cgroup_unlock();
1357201af4c0STejun Heo 
1358a7df69b8SJohannes Weiner 	cgroup_rstat_exit(cgrp);
1359201af4c0STejun Heo 	kernfs_destroy_root(root->kf_root);
1360201af4c0STejun Heo 	cgroup_free_root(root);
1361201af4c0STejun Heo }
1362201af4c0STejun Heo 
136346307fd6SMichal Koutný /*
136446307fd6SMichal Koutný  * Returned cgroup is without refcount but it's valid as long as cset pins it.
136546307fd6SMichal Koutný  */
__cset_cgroup_from_root(struct css_set * cset,struct cgroup_root * root)1366e210a89fSLin Feng static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
1367e210a89fSLin Feng 					    struct cgroup_root *root)
1368e210a89fSLin Feng {
1369e210a89fSLin Feng 	struct cgroup *res_cgroup = NULL;
1370e210a89fSLin Feng 
1371e210a89fSLin Feng 	if (cset == &init_css_set) {
1372e210a89fSLin Feng 		res_cgroup = &root->cgrp;
1373e210a89fSLin Feng 	} else if (root == &cgrp_dfl_root) {
1374e210a89fSLin Feng 		res_cgroup = cset->dfl_cgrp;
1375e210a89fSLin Feng 	} else {
1376e210a89fSLin Feng 		struct cgrp_cset_link *link;
137746307fd6SMichal Koutný 		lockdep_assert_held(&css_set_lock);
1378e210a89fSLin Feng 
1379e210a89fSLin Feng 		list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1380e210a89fSLin Feng 			struct cgroup *c = link->cgrp;
1381e210a89fSLin Feng 
1382e210a89fSLin Feng 			if (c->root == root) {
1383e210a89fSLin Feng 				res_cgroup = c;
1384e210a89fSLin Feng 				break;
1385e210a89fSLin Feng 			}
1386e210a89fSLin Feng 		}
1387e210a89fSLin Feng 	}
1388e210a89fSLin Feng 
138946307fd6SMichal Koutný 	BUG_ON(!res_cgroup);
1390e210a89fSLin Feng 	return res_cgroup;
1391e210a89fSLin Feng }
1392e210a89fSLin Feng 
1393201af4c0STejun Heo /*
1394201af4c0STejun Heo  * look up cgroup associated with current task's cgroup namespace on the
1395201af4c0STejun Heo  * specified hierarchy
1396201af4c0STejun Heo  */
1397201af4c0STejun Heo static struct cgroup *
current_cgns_cgroup_from_root(struct cgroup_root * root)1398201af4c0STejun Heo current_cgns_cgroup_from_root(struct cgroup_root *root)
1399201af4c0STejun Heo {
1400201af4c0STejun Heo 	struct cgroup *res = NULL;
1401201af4c0STejun Heo 	struct css_set *cset;
1402201af4c0STejun Heo 
1403201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
1404201af4c0STejun Heo 
1405201af4c0STejun Heo 	rcu_read_lock();
1406201af4c0STejun Heo 
1407201af4c0STejun Heo 	cset = current->nsproxy->cgroup_ns->root_cset;
1408e210a89fSLin Feng 	res = __cset_cgroup_from_root(cset, root);
1409201af4c0STejun Heo 
1410201af4c0STejun Heo 	rcu_read_unlock();
1411201af4c0STejun Heo 
1412201af4c0STejun Heo 	return res;
1413201af4c0STejun Heo }
1414201af4c0STejun Heo 
141546307fd6SMichal Koutný /*
141646307fd6SMichal Koutný  * Look up cgroup associated with current task's cgroup namespace on the default
141746307fd6SMichal Koutný  * hierarchy.
141846307fd6SMichal Koutný  *
141946307fd6SMichal Koutný  * Unlike current_cgns_cgroup_from_root(), this doesn't need locks:
142046307fd6SMichal Koutný  * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu
142146307fd6SMichal Koutný  *   pointers.
142246307fd6SMichal Koutný  * - css_set_lock is not needed because we just read cset->dfl_cgrp.
142346307fd6SMichal Koutný  * - As a bonus returned cgrp is pinned with the current because it cannot
142446307fd6SMichal Koutný  *   switch cgroup_ns asynchronously.
142546307fd6SMichal Koutný  */
current_cgns_cgroup_dfl(void)142646307fd6SMichal Koutný static struct cgroup *current_cgns_cgroup_dfl(void)
142746307fd6SMichal Koutný {
142846307fd6SMichal Koutný 	struct css_set *cset;
142946307fd6SMichal Koutný 
1430b8a2e3f9STejun Heo 	if (current->nsproxy) {
143146307fd6SMichal Koutný 		cset = current->nsproxy->cgroup_ns->root_cset;
143246307fd6SMichal Koutný 		return __cset_cgroup_from_root(cset, &cgrp_dfl_root);
1433b8a2e3f9STejun Heo 	} else {
1434b8a2e3f9STejun Heo 		/*
1435b8a2e3f9STejun Heo 		 * NOTE: This function may be called from bpf_cgroup_from_id()
1436b8a2e3f9STejun Heo 		 * on a task which has already passed exit_task_namespaces() and
1437b8a2e3f9STejun Heo 		 * nsproxy == NULL. Fall back to cgrp_dfl_root which will make all
1438b8a2e3f9STejun Heo 		 * cgroups visible for lookups.
1439b8a2e3f9STejun Heo 		 */
1440b8a2e3f9STejun Heo 		return &cgrp_dfl_root.cgrp;
1441b8a2e3f9STejun Heo 	}
144246307fd6SMichal Koutný }
144346307fd6SMichal Koutný 
1444201af4c0STejun Heo /* look up cgroup associated with given css_set on the specified hierarchy */
cset_cgroup_from_root(struct css_set * cset,struct cgroup_root * root)1445201af4c0STejun Heo static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
1446201af4c0STejun Heo 					    struct cgroup_root *root)
1447201af4c0STejun Heo {
1448201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1449201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
1450201af4c0STejun Heo 
145146307fd6SMichal Koutný 	return __cset_cgroup_from_root(cset, root);
1452201af4c0STejun Heo }
1453201af4c0STejun Heo 
1454201af4c0STejun Heo /*
1455201af4c0STejun Heo  * Return the cgroup for "task" from the given hierarchy. Must be
1456201af4c0STejun Heo  * called with cgroup_mutex and css_set_lock held.
1457201af4c0STejun Heo  */
task_cgroup_from_root(struct task_struct * task,struct cgroup_root * root)14580a268dbdSTejun Heo struct cgroup *task_cgroup_from_root(struct task_struct *task,
1459201af4c0STejun Heo 				     struct cgroup_root *root)
1460201af4c0STejun Heo {
1461201af4c0STejun Heo 	/*
1462e7c7b1d8SMichal Koutný 	 * No need to lock the task - since we hold css_set_lock the
1463e7c7b1d8SMichal Koutný 	 * task can't change groups.
1464201af4c0STejun Heo 	 */
1465201af4c0STejun Heo 	return cset_cgroup_from_root(task_css_set(task), root);
1466201af4c0STejun Heo }
1467201af4c0STejun Heo 
1468201af4c0STejun Heo /*
1469201af4c0STejun Heo  * A task must hold cgroup_mutex to modify cgroups.
1470201af4c0STejun Heo  *
1471201af4c0STejun Heo  * Any task can increment and decrement the count field without lock.
1472201af4c0STejun Heo  * So in general, code holding cgroup_mutex can't rely on the count
1473201af4c0STejun Heo  * field not changing.  However, if the count goes to zero, then only
1474201af4c0STejun Heo  * cgroup_attach_task() can increment it again.  Because a count of zero
1475201af4c0STejun Heo  * means that no tasks are currently attached, therefore there is no
1476201af4c0STejun Heo  * way a task attached to that cgroup can fork (the other way to
1477201af4c0STejun Heo  * increment the count).  So code holding cgroup_mutex can safely
1478201af4c0STejun Heo  * assume that if the count is zero, it will stay zero. Similarly, if
1479201af4c0STejun Heo  * a task holds cgroup_mutex on a cgroup with zero count, it
1480201af4c0STejun Heo  * knows that the cgroup won't be removed, as cgroup_rmdir()
1481201af4c0STejun Heo  * needs that mutex.
1482201af4c0STejun Heo  *
1483201af4c0STejun Heo  * A cgroup can only be deleted if both its 'count' of using tasks
1484201af4c0STejun Heo  * is zero, and its list of 'children' cgroups is empty.  Since all
1485201af4c0STejun Heo  * tasks in the system use _some_ cgroup, and since there is always at
1486201af4c0STejun Heo  * least one task in the system (init, pid == 1), therefore, root cgroup
1487201af4c0STejun Heo  * always has either children cgroups and/or using tasks.  So we don't
1488201af4c0STejun Heo  * need a special hack to ensure that root cgroup cannot be deleted.
1489201af4c0STejun Heo  *
1490201af4c0STejun Heo  * P.S.  One more locking exception.  RCU is used to guard the
1491201af4c0STejun Heo  * update of a tasks cgroup pointer by cgroup_attach_task()
1492201af4c0STejun Heo  */
1493201af4c0STejun Heo 
1494201af4c0STejun Heo static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
1495201af4c0STejun Heo 
cgroup_file_name(struct cgroup * cgrp,const struct cftype * cft,char * buf)1496cf892988SJens Axboe static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1497cf892988SJens Axboe 			      char *buf)
1498201af4c0STejun Heo {
1499201af4c0STejun Heo 	struct cgroup_subsys *ss = cft->ss;
1500201af4c0STejun Heo 
1501201af4c0STejun Heo 	if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1502c1bbd933STejun Heo 	    !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
1503c1bbd933STejun Heo 		const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
1504c1bbd933STejun Heo 
1505c1bbd933STejun Heo 		snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
1506c1bbd933STejun Heo 			 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1507cf892988SJens Axboe 			 cft->name);
1508c1bbd933STejun Heo 	} else {
1509cf892988SJens Axboe 		strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1510c1bbd933STejun Heo 	}
1511201af4c0STejun Heo 	return buf;
1512201af4c0STejun Heo }
1513201af4c0STejun Heo 
1514201af4c0STejun Heo /**
1515201af4c0STejun Heo  * cgroup_file_mode - deduce file mode of a control file
1516201af4c0STejun Heo  * @cft: the control file in question
1517201af4c0STejun Heo  *
1518201af4c0STejun Heo  * S_IRUGO for read, S_IWUSR for write.
1519201af4c0STejun Heo  */
cgroup_file_mode(const struct cftype * cft)1520201af4c0STejun Heo static umode_t cgroup_file_mode(const struct cftype *cft)
1521201af4c0STejun Heo {
1522201af4c0STejun Heo 	umode_t mode = 0;
1523201af4c0STejun Heo 
1524201af4c0STejun Heo 	if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1525201af4c0STejun Heo 		mode |= S_IRUGO;
1526201af4c0STejun Heo 
1527201af4c0STejun Heo 	if (cft->write_u64 || cft->write_s64 || cft->write) {
1528201af4c0STejun Heo 		if (cft->flags & CFTYPE_WORLD_WRITABLE)
1529201af4c0STejun Heo 			mode |= S_IWUGO;
1530201af4c0STejun Heo 		else
1531201af4c0STejun Heo 			mode |= S_IWUSR;
1532201af4c0STejun Heo 	}
1533201af4c0STejun Heo 
1534201af4c0STejun Heo 	return mode;
1535201af4c0STejun Heo }
1536201af4c0STejun Heo 
1537201af4c0STejun Heo /**
1538201af4c0STejun Heo  * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1539201af4c0STejun Heo  * @subtree_control: the new subtree_control mask to consider
1540201af4c0STejun Heo  * @this_ss_mask: available subsystems
1541201af4c0STejun Heo  *
1542201af4c0STejun Heo  * On the default hierarchy, a subsystem may request other subsystems to be
1543201af4c0STejun Heo  * enabled together through its ->depends_on mask.  In such cases, more
1544201af4c0STejun Heo  * subsystems than specified in "cgroup.subtree_control" may be enabled.
1545201af4c0STejun Heo  *
1546201af4c0STejun Heo  * This function calculates which subsystems need to be enabled if
1547201af4c0STejun Heo  * @subtree_control is to be applied while restricted to @this_ss_mask.
1548201af4c0STejun Heo  */
cgroup_calc_subtree_ss_mask(u16 subtree_control,u16 this_ss_mask)1549201af4c0STejun Heo static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
1550201af4c0STejun Heo {
1551201af4c0STejun Heo 	u16 cur_ss_mask = subtree_control;
1552201af4c0STejun Heo 	struct cgroup_subsys *ss;
1553201af4c0STejun Heo 	int ssid;
1554201af4c0STejun Heo 
1555201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1556201af4c0STejun Heo 
1557201af4c0STejun Heo 	cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1558201af4c0STejun Heo 
1559201af4c0STejun Heo 	while (true) {
1560201af4c0STejun Heo 		u16 new_ss_mask = cur_ss_mask;
1561201af4c0STejun Heo 
1562201af4c0STejun Heo 		do_each_subsys_mask(ss, ssid, cur_ss_mask) {
1563201af4c0STejun Heo 			new_ss_mask |= ss->depends_on;
1564201af4c0STejun Heo 		} while_each_subsys_mask();
1565201af4c0STejun Heo 
1566201af4c0STejun Heo 		/*
1567201af4c0STejun Heo 		 * Mask out subsystems which aren't available.  This can
1568201af4c0STejun Heo 		 * happen only if some depended-upon subsystems were bound
1569201af4c0STejun Heo 		 * to non-default hierarchies.
1570201af4c0STejun Heo 		 */
1571201af4c0STejun Heo 		new_ss_mask &= this_ss_mask;
1572201af4c0STejun Heo 
1573201af4c0STejun Heo 		if (new_ss_mask == cur_ss_mask)
1574201af4c0STejun Heo 			break;
1575201af4c0STejun Heo 		cur_ss_mask = new_ss_mask;
1576201af4c0STejun Heo 	}
1577201af4c0STejun Heo 
1578201af4c0STejun Heo 	return cur_ss_mask;
1579201af4c0STejun Heo }
1580201af4c0STejun Heo 
1581201af4c0STejun Heo /**
1582201af4c0STejun Heo  * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1583201af4c0STejun Heo  * @kn: the kernfs_node being serviced
1584201af4c0STejun Heo  *
1585201af4c0STejun Heo  * This helper undoes cgroup_kn_lock_live() and should be invoked before
1586201af4c0STejun Heo  * the method finishes if locking succeeded.  Note that once this function
1587201af4c0STejun Heo  * returns the cgroup returned by cgroup_kn_lock_live() may become
1588201af4c0STejun Heo  * inaccessible any time.  If the caller intends to continue to access the
1589201af4c0STejun Heo  * cgroup, it should pin it before invoking this function.
1590201af4c0STejun Heo  */
cgroup_kn_unlock(struct kernfs_node * kn)15910a268dbdSTejun Heo void cgroup_kn_unlock(struct kernfs_node *kn)
1592201af4c0STejun Heo {
1593201af4c0STejun Heo 	struct cgroup *cgrp;
1594201af4c0STejun Heo 
1595201af4c0STejun Heo 	if (kernfs_type(kn) == KERNFS_DIR)
1596201af4c0STejun Heo 		cgrp = kn->priv;
1597201af4c0STejun Heo 	else
1598201af4c0STejun Heo 		cgrp = kn->parent->priv;
1599201af4c0STejun Heo 
16004cdb91b0SKamalesh Babulal 	cgroup_unlock();
1601201af4c0STejun Heo 
1602201af4c0STejun Heo 	kernfs_unbreak_active_protection(kn);
1603201af4c0STejun Heo 	cgroup_put(cgrp);
1604201af4c0STejun Heo }
1605201af4c0STejun Heo 
1606201af4c0STejun Heo /**
1607201af4c0STejun Heo  * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1608201af4c0STejun Heo  * @kn: the kernfs_node being serviced
1609201af4c0STejun Heo  * @drain_offline: perform offline draining on the cgroup
1610201af4c0STejun Heo  *
1611201af4c0STejun Heo  * This helper is to be used by a cgroup kernfs method currently servicing
1612201af4c0STejun Heo  * @kn.  It breaks the active protection, performs cgroup locking and
1613201af4c0STejun Heo  * verifies that the associated cgroup is alive.  Returns the cgroup if
1614201af4c0STejun Heo  * alive; otherwise, %NULL.  A successful return should be undone by a
1615201af4c0STejun Heo  * matching cgroup_kn_unlock() invocation.  If @drain_offline is %true, the
1616201af4c0STejun Heo  * cgroup is drained of offlining csses before return.
1617201af4c0STejun Heo  *
1618201af4c0STejun Heo  * Any cgroup kernfs method implementation which requires locking the
1619201af4c0STejun Heo  * associated cgroup should use this helper.  It avoids nesting cgroup
1620201af4c0STejun Heo  * locking under kernfs active protection and allows all kernfs operations
1621201af4c0STejun Heo  * including self-removal.
1622201af4c0STejun Heo  */
cgroup_kn_lock_live(struct kernfs_node * kn,bool drain_offline)16230a268dbdSTejun Heo struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
1624201af4c0STejun Heo {
1625201af4c0STejun Heo 	struct cgroup *cgrp;
1626201af4c0STejun Heo 
1627201af4c0STejun Heo 	if (kernfs_type(kn) == KERNFS_DIR)
1628201af4c0STejun Heo 		cgrp = kn->priv;
1629201af4c0STejun Heo 	else
1630201af4c0STejun Heo 		cgrp = kn->parent->priv;
1631201af4c0STejun Heo 
1632201af4c0STejun Heo 	/*
1633201af4c0STejun Heo 	 * We're gonna grab cgroup_mutex which nests outside kernfs
1634201af4c0STejun Heo 	 * active_ref.  cgroup liveliness check alone provides enough
1635201af4c0STejun Heo 	 * protection against removal.  Ensure @cgrp stays accessible and
1636201af4c0STejun Heo 	 * break the active_ref protection.
1637201af4c0STejun Heo 	 */
1638201af4c0STejun Heo 	if (!cgroup_tryget(cgrp))
1639201af4c0STejun Heo 		return NULL;
1640201af4c0STejun Heo 	kernfs_break_active_protection(kn);
1641201af4c0STejun Heo 
1642201af4c0STejun Heo 	if (drain_offline)
1643201af4c0STejun Heo 		cgroup_lock_and_drain_offline(cgrp);
1644201af4c0STejun Heo 	else
16454cdb91b0SKamalesh Babulal 		cgroup_lock();
1646201af4c0STejun Heo 
1647201af4c0STejun Heo 	if (!cgroup_is_dead(cgrp))
1648201af4c0STejun Heo 		return cgrp;
1649201af4c0STejun Heo 
1650201af4c0STejun Heo 	cgroup_kn_unlock(kn);
1651201af4c0STejun Heo 	return NULL;
1652201af4c0STejun Heo }
1653201af4c0STejun Heo 
cgroup_rm_file(struct cgroup * cgrp,const struct cftype * cft)1654201af4c0STejun Heo static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
1655201af4c0STejun Heo {
1656201af4c0STejun Heo 	char name[CGROUP_FILE_NAME_MAX];
1657201af4c0STejun Heo 
1658201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1659201af4c0STejun Heo 
1660201af4c0STejun Heo 	if (cft->file_offset) {
1661201af4c0STejun Heo 		struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1662201af4c0STejun Heo 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
1663201af4c0STejun Heo 
1664201af4c0STejun Heo 		spin_lock_irq(&cgroup_file_kn_lock);
1665201af4c0STejun Heo 		cfile->kn = NULL;
1666201af4c0STejun Heo 		spin_unlock_irq(&cgroup_file_kn_lock);
1667b12e3583STejun Heo 
1668b12e3583STejun Heo 		del_timer_sync(&cfile->notify_timer);
1669201af4c0STejun Heo 	}
1670201af4c0STejun Heo 
1671201af4c0STejun Heo 	kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
1672201af4c0STejun Heo }
1673201af4c0STejun Heo 
1674201af4c0STejun Heo /**
1675201af4c0STejun Heo  * css_clear_dir - remove subsys files in a cgroup directory
167608b2b6fdSZhen Lei  * @css: target css
1677201af4c0STejun Heo  */
css_clear_dir(struct cgroup_subsys_state * css)1678201af4c0STejun Heo static void css_clear_dir(struct cgroup_subsys_state *css)
1679201af4c0STejun Heo {
1680201af4c0STejun Heo 	struct cgroup *cgrp = css->cgroup;
1681201af4c0STejun Heo 	struct cftype *cfts;
1682201af4c0STejun Heo 
1683201af4c0STejun Heo 	if (!(css->flags & CSS_VISIBLE))
1684201af4c0STejun Heo 		return;
1685201af4c0STejun Heo 
1686201af4c0STejun Heo 	css->flags &= ~CSS_VISIBLE;
1687201af4c0STejun Heo 
16885faaf05fSTejun Heo 	if (!css->ss) {
16898a693f77STejun Heo 		if (cgroup_on_dfl(cgrp)) {
16908a693f77STejun Heo 			cgroup_addrm_files(css, cgrp,
16918a693f77STejun Heo 					   cgroup_base_files, false);
16928a693f77STejun Heo 			if (cgroup_psi_enabled())
16938a693f77STejun Heo 				cgroup_addrm_files(css, cgrp,
16948a693f77STejun Heo 						   cgroup_psi_files, false);
16958a693f77STejun Heo 		} else {
16968a693f77STejun Heo 			cgroup_addrm_files(css, cgrp,
16978a693f77STejun Heo 					   cgroup1_base_files, false);
16988a693f77STejun Heo 		}
16995faaf05fSTejun Heo 	} else {
1700201af4c0STejun Heo 		list_for_each_entry(cfts, &css->ss->cfts, node)
1701201af4c0STejun Heo 			cgroup_addrm_files(css, cgrp, cfts, false);
1702201af4c0STejun Heo 	}
17035faaf05fSTejun Heo }
1704201af4c0STejun Heo 
1705201af4c0STejun Heo /**
1706201af4c0STejun Heo  * css_populate_dir - create subsys files in a cgroup directory
1707201af4c0STejun Heo  * @css: target css
1708201af4c0STejun Heo  *
1709201af4c0STejun Heo  * On failure, no file is added.
1710201af4c0STejun Heo  */
css_populate_dir(struct cgroup_subsys_state * css)1711201af4c0STejun Heo static int css_populate_dir(struct cgroup_subsys_state *css)
1712201af4c0STejun Heo {
1713201af4c0STejun Heo 	struct cgroup *cgrp = css->cgroup;
1714201af4c0STejun Heo 	struct cftype *cfts, *failed_cfts;
1715201af4c0STejun Heo 	int ret;
1716201af4c0STejun Heo 
1717c25ff4b9SKamalesh Babulal 	if (css->flags & CSS_VISIBLE)
1718201af4c0STejun Heo 		return 0;
1719201af4c0STejun Heo 
1720201af4c0STejun Heo 	if (!css->ss) {
17218a693f77STejun Heo 		if (cgroup_on_dfl(cgrp)) {
17228a693f77STejun Heo 			ret = cgroup_addrm_files(&cgrp->self, cgrp,
17238a693f77STejun Heo 						 cgroup_base_files, true);
17245faaf05fSTejun Heo 			if (ret < 0)
17255faaf05fSTejun Heo 				return ret;
17268a693f77STejun Heo 
17278a693f77STejun Heo 			if (cgroup_psi_enabled()) {
17288a693f77STejun Heo 				ret = cgroup_addrm_files(&cgrp->self, cgrp,
17298a693f77STejun Heo 							 cgroup_psi_files, true);
17308a693f77STejun Heo 				if (ret < 0)
17318a693f77STejun Heo 					return ret;
17328a693f77STejun Heo 			}
17338a693f77STejun Heo 		} else {
17348a693f77STejun Heo 			cgroup_addrm_files(css, cgrp,
17358a693f77STejun Heo 					   cgroup1_base_files, true);
17368a693f77STejun Heo 		}
17375faaf05fSTejun Heo 	} else {
1738201af4c0STejun Heo 		list_for_each_entry(cfts, &css->ss->cfts, node) {
1739201af4c0STejun Heo 			ret = cgroup_addrm_files(css, cgrp, cfts, true);
1740201af4c0STejun Heo 			if (ret < 0) {
1741201af4c0STejun Heo 				failed_cfts = cfts;
1742201af4c0STejun Heo 				goto err;
1743201af4c0STejun Heo 			}
1744201af4c0STejun Heo 		}
17455faaf05fSTejun Heo 	}
1746201af4c0STejun Heo 
1747201af4c0STejun Heo 	css->flags |= CSS_VISIBLE;
1748201af4c0STejun Heo 
1749201af4c0STejun Heo 	return 0;
1750201af4c0STejun Heo err:
1751201af4c0STejun Heo 	list_for_each_entry(cfts, &css->ss->cfts, node) {
1752201af4c0STejun Heo 		if (cfts == failed_cfts)
1753201af4c0STejun Heo 			break;
1754201af4c0STejun Heo 		cgroup_addrm_files(css, cgrp, cfts, false);
1755201af4c0STejun Heo 	}
1756201af4c0STejun Heo 	return ret;
1757201af4c0STejun Heo }
1758201af4c0STejun Heo 
rebind_subsystems(struct cgroup_root * dst_root,u16 ss_mask)17590a268dbdSTejun Heo int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
1760201af4c0STejun Heo {
1761201af4c0STejun Heo 	struct cgroup *dcgrp = &dst_root->cgrp;
1762201af4c0STejun Heo 	struct cgroup_subsys *ss;
17636f363f5aSXiu Jianfeng 	int ssid, ret;
17647ee28539SWaiman Long 	u16 dfl_disable_ss_mask = 0;
1765201af4c0STejun Heo 
1766201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
1767201af4c0STejun Heo 
1768201af4c0STejun Heo 	do_each_subsys_mask(ss, ssid, ss_mask) {
1769201af4c0STejun Heo 		/*
1770201af4c0STejun Heo 		 * If @ss has non-root csses attached to it, can't move.
1771201af4c0STejun Heo 		 * If @ss is an implicit controller, it is exempt from this
1772201af4c0STejun Heo 		 * rule and can be stolen.
1773201af4c0STejun Heo 		 */
1774201af4c0STejun Heo 		if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1775201af4c0STejun Heo 		    !ss->implicit_on_dfl)
1776201af4c0STejun Heo 			return -EBUSY;
1777201af4c0STejun Heo 
1778201af4c0STejun Heo 		/* can't move between two non-dummy roots either */
1779201af4c0STejun Heo 		if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
1780201af4c0STejun Heo 			return -EBUSY;
17817ee28539SWaiman Long 
17827ee28539SWaiman Long 		/*
17837ee28539SWaiman Long 		 * Collect ssid's that need to be disabled from default
17847ee28539SWaiman Long 		 * hierarchy.
17857ee28539SWaiman Long 		 */
17867ee28539SWaiman Long 		if (ss->root == &cgrp_dfl_root)
17877ee28539SWaiman Long 			dfl_disable_ss_mask |= 1 << ssid;
17887ee28539SWaiman Long 
1789201af4c0STejun Heo 	} while_each_subsys_mask();
1790201af4c0STejun Heo 
17917ee28539SWaiman Long 	if (dfl_disable_ss_mask) {
17927ee28539SWaiman Long 		struct cgroup *scgrp = &cgrp_dfl_root.cgrp;
17937ee28539SWaiman Long 
17947ee28539SWaiman Long 		/*
17957ee28539SWaiman Long 		 * Controllers from default hierarchy that need to be rebound
17967ee28539SWaiman Long 		 * are all disabled together in one go.
17977ee28539SWaiman Long 		 */
17987ee28539SWaiman Long 		cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask;
17997ee28539SWaiman Long 		WARN_ON(cgroup_apply_control(scgrp));
18007ee28539SWaiman Long 		cgroup_finalize_control(scgrp, 0);
18017ee28539SWaiman Long 	}
18027ee28539SWaiman Long 
1803201af4c0STejun Heo 	do_each_subsys_mask(ss, ssid, ss_mask) {
1804201af4c0STejun Heo 		struct cgroup_root *src_root = ss->root;
1805201af4c0STejun Heo 		struct cgroup *scgrp = &src_root->cgrp;
1806201af4c0STejun Heo 		struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
18076f363f5aSXiu Jianfeng 		struct css_set *cset, *cset_pos;
18086f363f5aSXiu Jianfeng 		struct css_task_iter *it;
1809201af4c0STejun Heo 
1810201af4c0STejun Heo 		WARN_ON(!css || cgroup_css(dcgrp, ss));
1811201af4c0STejun Heo 
18127ee28539SWaiman Long 		if (src_root != &cgrp_dfl_root) {
1813201af4c0STejun Heo 			/* disable from the source */
1814201af4c0STejun Heo 			src_root->subsys_mask &= ~(1 << ssid);
1815201af4c0STejun Heo 			WARN_ON(cgroup_apply_control(scgrp));
1816201af4c0STejun Heo 			cgroup_finalize_control(scgrp, 0);
18177ee28539SWaiman Long 		}
1818201af4c0STejun Heo 
1819201af4c0STejun Heo 		/* rebind */
1820201af4c0STejun Heo 		RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1821201af4c0STejun Heo 		rcu_assign_pointer(dcgrp->subsys[ssid], css);
1822201af4c0STejun Heo 		ss->root = dst_root;
1823201af4c0STejun Heo 		css->cgroup = dcgrp;
1824201af4c0STejun Heo 
1825201af4c0STejun Heo 		spin_lock_irq(&css_set_lock);
18266f363f5aSXiu Jianfeng 		WARN_ON(!list_empty(&dcgrp->e_csets[ss->id]));
18276f363f5aSXiu Jianfeng 		list_for_each_entry_safe(cset, cset_pos, &scgrp->e_csets[ss->id],
18286f363f5aSXiu Jianfeng 					 e_cset_node[ss->id]) {
1829201af4c0STejun Heo 			list_move_tail(&cset->e_cset_node[ss->id],
1830201af4c0STejun Heo 				       &dcgrp->e_csets[ss->id]);
18316f363f5aSXiu Jianfeng 			/*
18326f363f5aSXiu Jianfeng 			 * all css_sets of scgrp together in same order to dcgrp,
18336f363f5aSXiu Jianfeng 			 * patch in-flight iterators to preserve correct iteration.
18346f363f5aSXiu Jianfeng 			 * since the iterator is always advanced right away and
18356f363f5aSXiu Jianfeng 			 * finished when it->cset_pos meets it->cset_head, so only
18366f363f5aSXiu Jianfeng 			 * update it->cset_head is enough here.
18376f363f5aSXiu Jianfeng 			 */
18386f363f5aSXiu Jianfeng 			list_for_each_entry(it, &cset->task_iters, iters_node)
18396f363f5aSXiu Jianfeng 				if (it->cset_head == &scgrp->e_csets[ss->id])
18406f363f5aSXiu Jianfeng 					it->cset_head = &dcgrp->e_csets[ss->id];
18416f363f5aSXiu Jianfeng 		}
1842201af4c0STejun Heo 		spin_unlock_irq(&css_set_lock);
1843201af4c0STejun Heo 
1844a7df69b8SJohannes Weiner 		if (ss->css_rstat_flush) {
1845a7df69b8SJohannes Weiner 			list_del_rcu(&css->rstat_css_node);
1846763f4fb7SJing-Ting Wu 			synchronize_rcu();
1847a7df69b8SJohannes Weiner 			list_add_rcu(&css->rstat_css_node,
1848a7df69b8SJohannes Weiner 				     &dcgrp->rstat_css_list);
1849a7df69b8SJohannes Weiner 		}
1850a7df69b8SJohannes Weiner 
1851201af4c0STejun Heo 		/* default hierarchy doesn't enable controllers by default */
1852201af4c0STejun Heo 		dst_root->subsys_mask |= 1 << ssid;
1853201af4c0STejun Heo 		if (dst_root == &cgrp_dfl_root) {
1854201af4c0STejun Heo 			static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1855201af4c0STejun Heo 		} else {
1856201af4c0STejun Heo 			dcgrp->subtree_control |= 1 << ssid;
1857201af4c0STejun Heo 			static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
1858201af4c0STejun Heo 		}
1859201af4c0STejun Heo 
1860201af4c0STejun Heo 		ret = cgroup_apply_control(dcgrp);
1861201af4c0STejun Heo 		if (ret)
1862201af4c0STejun Heo 			pr_warn("partial failure to rebind %s controller (err=%d)\n",
1863201af4c0STejun Heo 				ss->name, ret);
1864201af4c0STejun Heo 
1865201af4c0STejun Heo 		if (ss->bind)
1866201af4c0STejun Heo 			ss->bind(css);
1867201af4c0STejun Heo 	} while_each_subsys_mask();
1868201af4c0STejun Heo 
1869201af4c0STejun Heo 	kernfs_activate(dcgrp->kn);
1870201af4c0STejun Heo 	return 0;
1871201af4c0STejun Heo }
1872201af4c0STejun Heo 
cgroup_show_path(struct seq_file * sf,struct kernfs_node * kf_node,struct kernfs_root * kf_root)18731592c9b2STejun Heo int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1874201af4c0STejun Heo 		     struct kernfs_root *kf_root)
1875201af4c0STejun Heo {
1876201af4c0STejun Heo 	int len = 0;
1877201af4c0STejun Heo 	char *buf = NULL;
1878201af4c0STejun Heo 	struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1879201af4c0STejun Heo 	struct cgroup *ns_cgroup;
1880201af4c0STejun Heo 
1881201af4c0STejun Heo 	buf = kmalloc(PATH_MAX, GFP_KERNEL);
1882201af4c0STejun Heo 	if (!buf)
1883201af4c0STejun Heo 		return -ENOMEM;
1884201af4c0STejun Heo 
1885201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
1886201af4c0STejun Heo 	ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1887201af4c0STejun Heo 	len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
1888201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
1889201af4c0STejun Heo 
1890201af4c0STejun Heo 	if (len >= PATH_MAX)
1891201af4c0STejun Heo 		len = -ERANGE;
1892201af4c0STejun Heo 	else if (len > 0) {
1893201af4c0STejun Heo 		seq_escape(sf, buf, " \t\n\\");
1894201af4c0STejun Heo 		len = 0;
1895201af4c0STejun Heo 	}
1896201af4c0STejun Heo 	kfree(buf);
1897201af4c0STejun Heo 	return len;
1898201af4c0STejun Heo }
1899201af4c0STejun Heo 
1900e34a98d5SAl Viro enum cgroup2_param {
1901e34a98d5SAl Viro 	Opt_nsdelegate,
1902c808f463STejun Heo 	Opt_favordynmods,
19039852ae3fSChris Down 	Opt_memory_localevents,
19048a931f80SJohannes Weiner 	Opt_memory_recursiveprot,
1905e34a98d5SAl Viro 	nr__cgroup2_params
1906e34a98d5SAl Viro };
1907e34a98d5SAl Viro 
1908d7167b14SAl Viro static const struct fs_parameter_spec cgroup2_fs_parameters[] = {
1909e34a98d5SAl Viro 	fsparam_flag("nsdelegate",		Opt_nsdelegate),
19106a010a49STejun Heo 	fsparam_flag("favordynmods",		Opt_favordynmods),
19119852ae3fSChris Down 	fsparam_flag("memory_localevents",	Opt_memory_localevents),
19128a931f80SJohannes Weiner 	fsparam_flag("memory_recursiveprot",	Opt_memory_recursiveprot),
1913e34a98d5SAl Viro 	{}
1914e34a98d5SAl Viro };
1915e34a98d5SAl Viro 
cgroup2_parse_param(struct fs_context * fc,struct fs_parameter * param)1916e34a98d5SAl Viro static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param)
19175136f636STejun Heo {
1918e34a98d5SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1919e34a98d5SAl Viro 	struct fs_parse_result result;
1920e34a98d5SAl Viro 	int opt;
19215136f636STejun Heo 
1922d7167b14SAl Viro 	opt = fs_parse(fc, cgroup2_fs_parameters, param, &result);
1923e34a98d5SAl Viro 	if (opt < 0)
1924e34a98d5SAl Viro 		return opt;
19255136f636STejun Heo 
1926e34a98d5SAl Viro 	switch (opt) {
1927e34a98d5SAl Viro 	case Opt_nsdelegate:
1928e34a98d5SAl Viro 		ctx->flags |= CGRP_ROOT_NS_DELEGATE;
19295136f636STejun Heo 		return 0;
19306a010a49STejun Heo 	case Opt_favordynmods:
19316a010a49STejun Heo 		ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
19326a010a49STejun Heo 		return 0;
19339852ae3fSChris Down 	case Opt_memory_localevents:
19349852ae3fSChris Down 		ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
19359852ae3fSChris Down 		return 0;
19368a931f80SJohannes Weiner 	case Opt_memory_recursiveprot:
19378a931f80SJohannes Weiner 		ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
19388a931f80SJohannes Weiner 		return 0;
19395136f636STejun Heo 	}
19405136f636STejun Heo 	return -EINVAL;
19415136f636STejun Heo }
19425136f636STejun Heo 
apply_cgroup_root_flags(unsigned int root_flags)19435136f636STejun Heo static void apply_cgroup_root_flags(unsigned int root_flags)
19445136f636STejun Heo {
19455136f636STejun Heo 	if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
19465136f636STejun Heo 		if (root_flags & CGRP_ROOT_NS_DELEGATE)
19475136f636STejun Heo 			cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
19485136f636STejun Heo 		else
19495136f636STejun Heo 			cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
19509852ae3fSChris Down 
19516a010a49STejun Heo 		cgroup_favor_dynmods(&cgrp_dfl_root,
19526a010a49STejun Heo 				     root_flags & CGRP_ROOT_FAVOR_DYNMODS);
19536a010a49STejun Heo 
19549852ae3fSChris Down 		if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
19559852ae3fSChris Down 			cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
19569852ae3fSChris Down 		else
19579852ae3fSChris Down 			cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS;
19588a931f80SJohannes Weiner 
19598a931f80SJohannes Weiner 		if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
19608a931f80SJohannes Weiner 			cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
19618a931f80SJohannes Weiner 		else
19628a931f80SJohannes Weiner 			cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT;
19635136f636STejun Heo 	}
19645136f636STejun Heo }
19655136f636STejun Heo 
cgroup_show_options(struct seq_file * seq,struct kernfs_root * kf_root)19665136f636STejun Heo static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
19675136f636STejun Heo {
19685136f636STejun Heo 	if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
19695136f636STejun Heo 		seq_puts(seq, ",nsdelegate");
19706a010a49STejun Heo 	if (cgrp_dfl_root.flags & CGRP_ROOT_FAVOR_DYNMODS)
19716a010a49STejun Heo 		seq_puts(seq, ",favordynmods");
19729852ae3fSChris Down 	if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
19739852ae3fSChris Down 		seq_puts(seq, ",memory_localevents");
19748a931f80SJohannes Weiner 	if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
19758a931f80SJohannes Weiner 		seq_puts(seq, ",memory_recursiveprot");
19765136f636STejun Heo 	return 0;
19775136f636STejun Heo }
19785136f636STejun Heo 
cgroup_reconfigure(struct fs_context * fc)197990129625SAl Viro static int cgroup_reconfigure(struct fs_context *fc)
1980fa069904STejun Heo {
198190129625SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
19825136f636STejun Heo 
1983f5dfb531SAl Viro 	apply_cgroup_root_flags(ctx->flags);
19845136f636STejun Heo 	return 0;
1985fa069904STejun Heo }
1986fa069904STejun Heo 
init_cgroup_housekeeping(struct cgroup * cgrp)1987201af4c0STejun Heo static void init_cgroup_housekeeping(struct cgroup *cgrp)
1988201af4c0STejun Heo {
1989201af4c0STejun Heo 	struct cgroup_subsys *ss;
1990201af4c0STejun Heo 	int ssid;
1991201af4c0STejun Heo 
1992201af4c0STejun Heo 	INIT_LIST_HEAD(&cgrp->self.sibling);
1993201af4c0STejun Heo 	INIT_LIST_HEAD(&cgrp->self.children);
1994201af4c0STejun Heo 	INIT_LIST_HEAD(&cgrp->cset_links);
1995201af4c0STejun Heo 	INIT_LIST_HEAD(&cgrp->pidlists);
1996201af4c0STejun Heo 	mutex_init(&cgrp->pidlist_mutex);
1997201af4c0STejun Heo 	cgrp->self.cgroup = cgrp;
1998201af4c0STejun Heo 	cgrp->self.flags |= CSS_ONLINE;
1999454000adSTejun Heo 	cgrp->dom_cgrp = cgrp;
20001a926e0bSRoman Gushchin 	cgrp->max_descendants = INT_MAX;
20011a926e0bSRoman Gushchin 	cgrp->max_depth = INT_MAX;
20028f53470bSTejun Heo 	INIT_LIST_HEAD(&cgrp->rstat_css_list);
2003d4ff749bSTejun Heo 	prev_cputime_init(&cgrp->prev_cputime);
2004201af4c0STejun Heo 
2005201af4c0STejun Heo 	for_each_subsys(ss, ssid)
2006201af4c0STejun Heo 		INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
2007201af4c0STejun Heo 
2008201af4c0STejun Heo 	init_waitqueue_head(&cgrp->offline_waitq);
2009d62beb7fSTejun Heo 	INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
2010201af4c0STejun Heo }
2011201af4c0STejun Heo 
init_cgroup_root(struct cgroup_fs_context * ctx)2012cf6299b1SAl Viro void init_cgroup_root(struct cgroup_fs_context *ctx)
2013201af4c0STejun Heo {
2014cf6299b1SAl Viro 	struct cgroup_root *root = ctx->root;
2015201af4c0STejun Heo 	struct cgroup *cgrp = &root->cgrp;
2016201af4c0STejun Heo 
2017201af4c0STejun Heo 	INIT_LIST_HEAD(&root->root_list);
2018201af4c0STejun Heo 	atomic_set(&root->nr_cgrps, 1);
2019201af4c0STejun Heo 	cgrp->root = root;
2020201af4c0STejun Heo 	init_cgroup_housekeeping(cgrp);
2021201af4c0STejun Heo 
20226a010a49STejun Heo 	/* DYNMODS must be modified through cgroup_favor_dynmods() */
20236a010a49STejun Heo 	root->flags = ctx->flags & ~CGRP_ROOT_FAVOR_DYNMODS;
2024f5dfb531SAl Viro 	if (ctx->release_agent)
2025f5dfb531SAl Viro 		strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX);
2026f5dfb531SAl Viro 	if (ctx->name)
2027f5dfb531SAl Viro 		strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN);
2028f5dfb531SAl Viro 	if (ctx->cpuset_clone_children)
2029201af4c0STejun Heo 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
2030201af4c0STejun Heo }
2031201af4c0STejun Heo 
cgroup_setup_root(struct cgroup_root * root,u16 ss_mask)203235ac1184SAl Viro int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
2033201af4c0STejun Heo {
2034201af4c0STejun Heo 	LIST_HEAD(tmp_links);
2035201af4c0STejun Heo 	struct cgroup *root_cgrp = &root->cgrp;
2036fa069904STejun Heo 	struct kernfs_syscall_ops *kf_sops;
2037201af4c0STejun Heo 	struct css_set *cset;
2038201af4c0STejun Heo 	int i, ret;
2039201af4c0STejun Heo 
2040201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
2041201af4c0STejun Heo 
20429732adc5SZefan Li 	ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
204335ac1184SAl Viro 			      0, GFP_KERNEL);
2044201af4c0STejun Heo 	if (ret)
2045201af4c0STejun Heo 		goto out;
2046201af4c0STejun Heo 
2047201af4c0STejun Heo 	/*
2048201af4c0STejun Heo 	 * We're accessing css_set_count without locking css_set_lock here,
2049201af4c0STejun Heo 	 * but that's OK - it can only be increased by someone holding
2050201af4c0STejun Heo 	 * cgroup_lock, and that's us.  Later rebinding may disable
2051201af4c0STejun Heo 	 * controllers on the default hierarchy and thus create new csets,
2052201af4c0STejun Heo 	 * which can't be more than the existing ones.  Allocate 2x.
2053201af4c0STejun Heo 	 */
2054201af4c0STejun Heo 	ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
2055201af4c0STejun Heo 	if (ret)
2056201af4c0STejun Heo 		goto cancel_ref;
2057201af4c0STejun Heo 
2058201af4c0STejun Heo 	ret = cgroup_init_root_id(root);
2059201af4c0STejun Heo 	if (ret)
2060201af4c0STejun Heo 		goto cancel_ref;
2061201af4c0STejun Heo 
2062fa069904STejun Heo 	kf_sops = root == &cgrp_dfl_root ?
2063fa069904STejun Heo 		&cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
2064fa069904STejun Heo 
2065fa069904STejun Heo 	root->kf_root = kernfs_create_root(kf_sops,
2066aa818825SShaohua Li 					   KERNFS_ROOT_CREATE_DEACTIVATED |
206738aca307SDaniel Xu 					   KERNFS_ROOT_SUPPORT_EXPORTOP |
206838aca307SDaniel Xu 					   KERNFS_ROOT_SUPPORT_USER_XATTR,
2069201af4c0STejun Heo 					   root_cgrp);
2070201af4c0STejun Heo 	if (IS_ERR(root->kf_root)) {
2071201af4c0STejun Heo 		ret = PTR_ERR(root->kf_root);
2072201af4c0STejun Heo 		goto exit_root_id;
2073201af4c0STejun Heo 	}
2074f2eb478fSGreg Kroah-Hartman 	root_cgrp->kn = kernfs_root_to_node(root->kf_root);
2075d7495343STejun Heo 	WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1);
20767f203bc8STejun Heo 	root_cgrp->ancestors[0] = root_cgrp;
2077201af4c0STejun Heo 
2078201af4c0STejun Heo 	ret = css_populate_dir(&root_cgrp->self);
2079201af4c0STejun Heo 	if (ret)
2080201af4c0STejun Heo 		goto destroy_root;
2081201af4c0STejun Heo 
2082a7df69b8SJohannes Weiner 	ret = cgroup_rstat_init(root_cgrp);
2083201af4c0STejun Heo 	if (ret)
2084201af4c0STejun Heo 		goto destroy_root;
2085201af4c0STejun Heo 
2086a7df69b8SJohannes Weiner 	ret = rebind_subsystems(root, ss_mask);
2087a7df69b8SJohannes Weiner 	if (ret)
2088a7df69b8SJohannes Weiner 		goto exit_stats;
2089a7df69b8SJohannes Weiner 
2090324bda9eSAlexei Starovoitov 	ret = cgroup_bpf_inherit(root_cgrp);
2091324bda9eSAlexei Starovoitov 	WARN_ON_ONCE(ret);
2092324bda9eSAlexei Starovoitov 
2093201af4c0STejun Heo 	trace_cgroup_setup_root(root);
2094201af4c0STejun Heo 
2095201af4c0STejun Heo 	/*
2096201af4c0STejun Heo 	 * There must be no failure case after here, since rebinding takes
2097201af4c0STejun Heo 	 * care of subsystems' refcounts, which are explicitly dropped in
2098201af4c0STejun Heo 	 * the failure exit path.
2099201af4c0STejun Heo 	 */
2100201af4c0STejun Heo 	list_add(&root->root_list, &cgroup_roots);
2101201af4c0STejun Heo 	cgroup_root_count++;
2102201af4c0STejun Heo 
2103201af4c0STejun Heo 	/*
2104201af4c0STejun Heo 	 * Link the root cgroup in this hierarchy into all the css_set
2105201af4c0STejun Heo 	 * objects.
2106201af4c0STejun Heo 	 */
2107201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2108201af4c0STejun Heo 	hash_for_each(css_set_table, i, cset, hlist) {
2109201af4c0STejun Heo 		link_css_set(&tmp_links, cset, root_cgrp);
2110201af4c0STejun Heo 		if (css_set_populated(cset))
2111201af4c0STejun Heo 			cgroup_update_populated(root_cgrp, true);
2112201af4c0STejun Heo 	}
2113201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
2114201af4c0STejun Heo 
2115201af4c0STejun Heo 	BUG_ON(!list_empty(&root_cgrp->self.children));
2116201af4c0STejun Heo 	BUG_ON(atomic_read(&root->nr_cgrps) != 1);
2117201af4c0STejun Heo 
2118201af4c0STejun Heo 	ret = 0;
2119201af4c0STejun Heo 	goto out;
2120201af4c0STejun Heo 
2121a7df69b8SJohannes Weiner exit_stats:
2122a7df69b8SJohannes Weiner 	cgroup_rstat_exit(root_cgrp);
2123201af4c0STejun Heo destroy_root:
2124201af4c0STejun Heo 	kernfs_destroy_root(root->kf_root);
2125201af4c0STejun Heo 	root->kf_root = NULL;
2126201af4c0STejun Heo exit_root_id:
2127201af4c0STejun Heo 	cgroup_exit_root_id(root);
2128201af4c0STejun Heo cancel_ref:
2129201af4c0STejun Heo 	percpu_ref_exit(&root_cgrp->self.refcnt);
2130201af4c0STejun Heo out:
2131201af4c0STejun Heo 	free_cgrp_cset_links(&tmp_links);
2132201af4c0STejun Heo 	return ret;
2133201af4c0STejun Heo }
2134201af4c0STejun Heo 
cgroup_do_get_tree(struct fs_context * fc)2135cca8f327SAl Viro int cgroup_do_get_tree(struct fs_context *fc)
2136201af4c0STejun Heo {
213771d883c3SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
213823bf1b6bSDavid Howells 	int ret;
2139201af4c0STejun Heo 
214023bf1b6bSDavid Howells 	ctx->kfc.root = ctx->root->kf_root;
2141cca8f327SAl Viro 	if (fc->fs_type == &cgroup2_fs_type)
214223bf1b6bSDavid Howells 		ctx->kfc.magic = CGROUP2_SUPER_MAGIC;
2143cca8f327SAl Viro 	else
214423bf1b6bSDavid Howells 		ctx->kfc.magic = CGROUP_SUPER_MAGIC;
214523bf1b6bSDavid Howells 	ret = kernfs_get_tree(fc);
2146201af4c0STejun Heo 
2147201af4c0STejun Heo 	/*
2148633feee3STejun Heo 	 * In non-init cgroup namespace, instead of root cgroup's dentry,
2149633feee3STejun Heo 	 * we return the dentry corresponding to the cgroupns->root_cgrp.
2150201af4c0STejun Heo 	 */
2151cca8f327SAl Viro 	if (!ret && ctx->ns != &init_cgroup_ns) {
2152633feee3STejun Heo 		struct dentry *nsdentry;
215371d883c3SAl Viro 		struct super_block *sb = fc->root->d_sb;
2154633feee3STejun Heo 		struct cgroup *cgrp;
2155201af4c0STejun Heo 
21564cdb91b0SKamalesh Babulal 		cgroup_lock();
2157633feee3STejun Heo 		spin_lock_irq(&css_set_lock);
2158633feee3STejun Heo 
2159cca8f327SAl Viro 		cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root);
2160633feee3STejun Heo 
2161633feee3STejun Heo 		spin_unlock_irq(&css_set_lock);
21624cdb91b0SKamalesh Babulal 		cgroup_unlock();
2163633feee3STejun Heo 
2164399504e2SAl Viro 		nsdentry = kernfs_node_dentry(cgrp->kn, sb);
216571d883c3SAl Viro 		dput(fc->root);
216671d883c3SAl Viro 		if (IS_ERR(nsdentry)) {
2167399504e2SAl Viro 			deactivate_locked_super(sb);
2168630faf81SAl Viro 			ret = PTR_ERR(nsdentry);
2169630faf81SAl Viro 			nsdentry = NULL;
217071d883c3SAl Viro 		}
2171630faf81SAl Viro 		fc->root = nsdentry;
2172201af4c0STejun Heo 	}
2173633feee3STejun Heo 
217423bf1b6bSDavid Howells 	if (!ctx->kfc.new_sb_created)
217571d883c3SAl Viro 		cgroup_put(&ctx->root->cgrp);
2176633feee3STejun Heo 
217771d883c3SAl Viro 	return ret;
2178201af4c0STejun Heo }
2179201af4c0STejun Heo 
218090129625SAl Viro /*
218190129625SAl Viro  * Destroy a cgroup filesystem context.
218290129625SAl Viro  */
cgroup_fs_context_free(struct fs_context * fc)218390129625SAl Viro static void cgroup_fs_context_free(struct fs_context *fc)
2184633feee3STejun Heo {
218590129625SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
218690129625SAl Viro 
2187f5dfb531SAl Viro 	kfree(ctx->name);
2188f5dfb531SAl Viro 	kfree(ctx->release_agent);
2189cca8f327SAl Viro 	put_cgroup_ns(ctx->ns);
219023bf1b6bSDavid Howells 	kernfs_free_fs_context(fc);
219190129625SAl Viro 	kfree(ctx);
219290129625SAl Viro }
219390129625SAl Viro 
cgroup_get_tree(struct fs_context * fc)219490129625SAl Viro static int cgroup_get_tree(struct fs_context *fc)
2195633feee3STejun Heo {
219690129625SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
21975136f636STejun Heo 	int ret;
2198633feee3STejun Heo 
2199dc79ec1bSTejun Heo 	WRITE_ONCE(cgrp_dfl_visible, true);
220090129625SAl Viro 	cgroup_get_live(&cgrp_dfl_root.cgrp);
2201cf6299b1SAl Viro 	ctx->root = &cgrp_dfl_root;
2202633feee3STejun Heo 
2203cca8f327SAl Viro 	ret = cgroup_do_get_tree(fc);
220471d883c3SAl Viro 	if (!ret)
2205f5dfb531SAl Viro 		apply_cgroup_root_flags(ctx->flags);
220671d883c3SAl Viro 	return ret;
2207633feee3STejun Heo }
2208633feee3STejun Heo 
220990129625SAl Viro static const struct fs_context_operations cgroup_fs_context_ops = {
221090129625SAl Viro 	.free		= cgroup_fs_context_free,
2211e34a98d5SAl Viro 	.parse_param	= cgroup2_parse_param,
221290129625SAl Viro 	.get_tree	= cgroup_get_tree,
221390129625SAl Viro 	.reconfigure	= cgroup_reconfigure,
221490129625SAl Viro };
221590129625SAl Viro 
221690129625SAl Viro static const struct fs_context_operations cgroup1_fs_context_ops = {
221790129625SAl Viro 	.free		= cgroup_fs_context_free,
22188d2451f4SAl Viro 	.parse_param	= cgroup1_parse_param,
221990129625SAl Viro 	.get_tree	= cgroup1_get_tree,
222090129625SAl Viro 	.reconfigure	= cgroup1_reconfigure,
222190129625SAl Viro };
222290129625SAl Viro 
222390129625SAl Viro /*
222423bf1b6bSDavid Howells  * Initialise the cgroup filesystem creation/reconfiguration context.  Notably,
222523bf1b6bSDavid Howells  * we select the namespace we're going to use.
222690129625SAl Viro  */
cgroup_init_fs_context(struct fs_context * fc)222790129625SAl Viro static int cgroup_init_fs_context(struct fs_context *fc)
222890129625SAl Viro {
222990129625SAl Viro 	struct cgroup_fs_context *ctx;
223090129625SAl Viro 
223190129625SAl Viro 	ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL);
223290129625SAl Viro 	if (!ctx)
223390129625SAl Viro 		return -ENOMEM;
223490129625SAl Viro 
2235cca8f327SAl Viro 	ctx->ns = current->nsproxy->cgroup_ns;
2236cca8f327SAl Viro 	get_cgroup_ns(ctx->ns);
223723bf1b6bSDavid Howells 	fc->fs_private = &ctx->kfc;
223890129625SAl Viro 	if (fc->fs_type == &cgroup2_fs_type)
223990129625SAl Viro 		fc->ops = &cgroup_fs_context_ops;
224090129625SAl Viro 	else
224190129625SAl Viro 		fc->ops = &cgroup1_fs_context_ops;
224223bf1b6bSDavid Howells 	put_user_ns(fc->user_ns);
224323bf1b6bSDavid Howells 	fc->user_ns = get_user_ns(ctx->ns->user_ns);
224423bf1b6bSDavid Howells 	fc->global = true;
22456a010a49STejun Heo 
22466a010a49STejun Heo #ifdef CONFIG_CGROUP_FAVOR_DYNMODS
22476a010a49STejun Heo 	ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
22486a010a49STejun Heo #endif
224990129625SAl Viro 	return 0;
2250201af4c0STejun Heo }
2251201af4c0STejun Heo 
cgroup_kill_sb(struct super_block * sb)2252201af4c0STejun Heo static void cgroup_kill_sb(struct super_block *sb)
2253201af4c0STejun Heo {
2254201af4c0STejun Heo 	struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
2255201af4c0STejun Heo 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
2256201af4c0STejun Heo 
2257201af4c0STejun Heo 	/*
225835ac1184SAl Viro 	 * If @root doesn't have any children, start killing it.
2259201af4c0STejun Heo 	 * This prevents new mounts by disabling percpu_ref_tryget_live().
2260201af4c0STejun Heo 	 *
2261201af4c0STejun Heo 	 * And don't kill the default root.
2262201af4c0STejun Heo 	 */
226335ac1184SAl Viro 	if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
226404f8ef56SQuanyang Wang 	    !percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
226504f8ef56SQuanyang Wang 		cgroup_bpf_offline(&root->cgrp);
2266201af4c0STejun Heo 		percpu_ref_kill(&root->cgrp.self.refcnt);
226704f8ef56SQuanyang Wang 	}
226835ac1184SAl Viro 	cgroup_put(&root->cgrp);
2269201af4c0STejun Heo 	kernfs_kill_sb(sb);
2270201af4c0STejun Heo }
2271201af4c0STejun Heo 
22720a268dbdSTejun Heo struct file_system_type cgroup_fs_type = {
2273201af4c0STejun Heo 	.name			= "cgroup",
227490129625SAl Viro 	.init_fs_context	= cgroup_init_fs_context,
2275d7167b14SAl Viro 	.parameters		= cgroup1_fs_parameters,
2276201af4c0STejun Heo 	.kill_sb		= cgroup_kill_sb,
2277201af4c0STejun Heo 	.fs_flags		= FS_USERNS_MOUNT,
2278201af4c0STejun Heo };
2279201af4c0STejun Heo 
2280201af4c0STejun Heo static struct file_system_type cgroup2_fs_type = {
2281201af4c0STejun Heo 	.name			= "cgroup2",
228290129625SAl Viro 	.init_fs_context	= cgroup_init_fs_context,
2283d7167b14SAl Viro 	.parameters		= cgroup2_fs_parameters,
2284201af4c0STejun Heo 	.kill_sb		= cgroup_kill_sb,
2285201af4c0STejun Heo 	.fs_flags		= FS_USERNS_MOUNT,
2286201af4c0STejun Heo };
2287201af4c0STejun Heo 
2288d5f68d33SAl Viro #ifdef CONFIG_CPUSETS
2289d5f68d33SAl Viro static const struct fs_context_operations cpuset_fs_context_ops = {
2290d5f68d33SAl Viro 	.get_tree	= cgroup1_get_tree,
2291d5f68d33SAl Viro 	.free		= cgroup_fs_context_free,
2292d5f68d33SAl Viro };
2293d5f68d33SAl Viro 
2294d5f68d33SAl Viro /*
2295d5f68d33SAl Viro  * This is ugly, but preserves the userspace API for existing cpuset
2296d5f68d33SAl Viro  * users. If someone tries to mount the "cpuset" filesystem, we
2297d5f68d33SAl Viro  * silently switch it to mount "cgroup" instead
2298d5f68d33SAl Viro  */
cpuset_init_fs_context(struct fs_context * fc)2299d5f68d33SAl Viro static int cpuset_init_fs_context(struct fs_context *fc)
2300d5f68d33SAl Viro {
2301d5f68d33SAl Viro 	char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER);
2302d5f68d33SAl Viro 	struct cgroup_fs_context *ctx;
2303d5f68d33SAl Viro 	int err;
2304d5f68d33SAl Viro 
2305d5f68d33SAl Viro 	err = cgroup_init_fs_context(fc);
2306d5f68d33SAl Viro 	if (err) {
2307d5f68d33SAl Viro 		kfree(agent);
2308d5f68d33SAl Viro 		return err;
2309d5f68d33SAl Viro 	}
2310d5f68d33SAl Viro 
2311d5f68d33SAl Viro 	fc->ops = &cpuset_fs_context_ops;
2312d5f68d33SAl Viro 
2313d5f68d33SAl Viro 	ctx = cgroup_fc2context(fc);
2314d5f68d33SAl Viro 	ctx->subsys_mask = 1 << cpuset_cgrp_id;
2315d5f68d33SAl Viro 	ctx->flags |= CGRP_ROOT_NOPREFIX;
2316d5f68d33SAl Viro 	ctx->release_agent = agent;
2317d5f68d33SAl Viro 
2318d5f68d33SAl Viro 	get_filesystem(&cgroup_fs_type);
2319d5f68d33SAl Viro 	put_filesystem(fc->fs_type);
2320d5f68d33SAl Viro 	fc->fs_type = &cgroup_fs_type;
2321d5f68d33SAl Viro 
2322d5f68d33SAl Viro 	return 0;
2323d5f68d33SAl Viro }
2324d5f68d33SAl Viro 
2325d5f68d33SAl Viro static struct file_system_type cpuset_fs_type = {
2326d5f68d33SAl Viro 	.name			= "cpuset",
2327d5f68d33SAl Viro 	.init_fs_context	= cpuset_init_fs_context,
2328d5f68d33SAl Viro 	.fs_flags		= FS_USERNS_MOUNT,
2329d5f68d33SAl Viro };
2330d5f68d33SAl Viro #endif
2331d5f68d33SAl Viro 
cgroup_path_ns_locked(struct cgroup * cgrp,char * buf,size_t buflen,struct cgroup_namespace * ns)23320a268dbdSTejun Heo int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2333201af4c0STejun Heo 			  struct cgroup_namespace *ns)
2334201af4c0STejun Heo {
2335201af4c0STejun Heo 	struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
2336201af4c0STejun Heo 
2337201af4c0STejun Heo 	return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
2338201af4c0STejun Heo }
2339201af4c0STejun Heo 
cgroup_path_ns(struct cgroup * cgrp,char * buf,size_t buflen,struct cgroup_namespace * ns)2340201af4c0STejun Heo int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2341201af4c0STejun Heo 		   struct cgroup_namespace *ns)
2342201af4c0STejun Heo {
2343201af4c0STejun Heo 	int ret;
2344201af4c0STejun Heo 
23454cdb91b0SKamalesh Babulal 	cgroup_lock();
2346201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2347201af4c0STejun Heo 
2348201af4c0STejun Heo 	ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2349201af4c0STejun Heo 
2350201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
23514cdb91b0SKamalesh Babulal 	cgroup_unlock();
2352201af4c0STejun Heo 
2353201af4c0STejun Heo 	return ret;
2354201af4c0STejun Heo }
2355201af4c0STejun Heo EXPORT_SYMBOL_GPL(cgroup_path_ns);
2356201af4c0STejun Heo 
2357201af4c0STejun Heo /**
23584f7e7236STejun Heo  * cgroup_attach_lock - Lock for ->attach()
23594f7e7236STejun Heo  * @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem
23604f7e7236STejun Heo  *
23614f7e7236STejun Heo  * cgroup migration sometimes needs to stabilize threadgroups against forks and
23624f7e7236STejun Heo  * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach()
23634f7e7236STejun Heo  * implementations (e.g. cpuset), also need to disable CPU hotplug.
23644f7e7236STejun Heo  * Unfortunately, letting ->attach() operations acquire cpus_read_lock() can
23654f7e7236STejun Heo  * lead to deadlocks.
23664f7e7236STejun Heo  *
23674f7e7236STejun Heo  * Bringing up a CPU may involve creating and destroying tasks which requires
23684f7e7236STejun Heo  * read-locking threadgroup_rwsem, so threadgroup_rwsem nests inside
23694f7e7236STejun Heo  * cpus_read_lock(). If we call an ->attach() which acquires the cpus lock while
23704f7e7236STejun Heo  * write-locking threadgroup_rwsem, the locking order is reversed and we end up
23714f7e7236STejun Heo  * waiting for an on-going CPU hotplug operation which in turn is waiting for
23724f7e7236STejun Heo  * the threadgroup_rwsem to be released to create new tasks. For more details:
23734f7e7236STejun Heo  *
23744f7e7236STejun Heo  *   http://lkml.kernel.org/r/20220711174629.uehfmqegcwn2lqzu@wubuntu
23754f7e7236STejun Heo  *
23764f7e7236STejun Heo  * Resolve the situation by always acquiring cpus_read_lock() before optionally
23774f7e7236STejun Heo  * write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that
23784f7e7236STejun Heo  * CPU hotplug is disabled on entry.
23794f7e7236STejun Heo  */
cgroup_attach_lock(bool lock_threadgroup)2380075b593fSTetsuo Handa void cgroup_attach_lock(bool lock_threadgroup)
23814f7e7236STejun Heo {
23824f7e7236STejun Heo 	cpus_read_lock();
23834f7e7236STejun Heo 	if (lock_threadgroup)
23844f7e7236STejun Heo 		percpu_down_write(&cgroup_threadgroup_rwsem);
23854f7e7236STejun Heo }
23864f7e7236STejun Heo 
23874f7e7236STejun Heo /**
23884f7e7236STejun Heo  * cgroup_attach_unlock - Undo cgroup_attach_lock()
23894f7e7236STejun Heo  * @lock_threadgroup: whether to up_write cgroup_threadgroup_rwsem
23904f7e7236STejun Heo  */
cgroup_attach_unlock(bool lock_threadgroup)2391075b593fSTetsuo Handa void cgroup_attach_unlock(bool lock_threadgroup)
23924f7e7236STejun Heo {
23934f7e7236STejun Heo 	if (lock_threadgroup)
23944f7e7236STejun Heo 		percpu_up_write(&cgroup_threadgroup_rwsem);
23954f7e7236STejun Heo 	cpus_read_unlock();
23964f7e7236STejun Heo }
23974f7e7236STejun Heo 
23984f7e7236STejun Heo /**
2399e595cd70STejun Heo  * cgroup_migrate_add_task - add a migration target task to a migration context
2400201af4c0STejun Heo  * @task: target task
2401e595cd70STejun Heo  * @mgctx: target migration context
2402201af4c0STejun Heo  *
2403e595cd70STejun Heo  * Add @task, which is a migration target, to @mgctx->tset.  This function
2404e595cd70STejun Heo  * becomes noop if @task doesn't need to be migrated.  @task's css_set
2405e595cd70STejun Heo  * should have been added as a migration source and @task->cg_list will be
2406e595cd70STejun Heo  * moved from the css_set's tasks list to mg_tasks one.
2407201af4c0STejun Heo  */
cgroup_migrate_add_task(struct task_struct * task,struct cgroup_mgctx * mgctx)2408e595cd70STejun Heo static void cgroup_migrate_add_task(struct task_struct *task,
2409e595cd70STejun Heo 				    struct cgroup_mgctx *mgctx)
2410201af4c0STejun Heo {
2411201af4c0STejun Heo 	struct css_set *cset;
2412201af4c0STejun Heo 
2413201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
2414201af4c0STejun Heo 
2415201af4c0STejun Heo 	/* @task either already exited or can't exit until the end */
2416201af4c0STejun Heo 	if (task->flags & PF_EXITING)
2417201af4c0STejun Heo 		return;
2418201af4c0STejun Heo 
24195153faacSTejun Heo 	/* cgroup_threadgroup_rwsem protects racing against forks */
24205153faacSTejun Heo 	WARN_ON_ONCE(list_empty(&task->cg_list));
2421201af4c0STejun Heo 
2422201af4c0STejun Heo 	cset = task_css_set(task);
2423201af4c0STejun Heo 	if (!cset->mg_src_cgrp)
2424201af4c0STejun Heo 		return;
2425201af4c0STejun Heo 
242661046727STejun Heo 	mgctx->tset.nr_tasks++;
242761046727STejun Heo 
2428201af4c0STejun Heo 	list_move_tail(&task->cg_list, &cset->mg_tasks);
2429201af4c0STejun Heo 	if (list_empty(&cset->mg_node))
2430e595cd70STejun Heo 		list_add_tail(&cset->mg_node,
2431e595cd70STejun Heo 			      &mgctx->tset.src_csets);
2432201af4c0STejun Heo 	if (list_empty(&cset->mg_dst_cset->mg_node))
2433d8ebf519STejun Heo 		list_add_tail(&cset->mg_dst_cset->mg_node,
2434e595cd70STejun Heo 			      &mgctx->tset.dst_csets);
2435201af4c0STejun Heo }
2436201af4c0STejun Heo 
2437201af4c0STejun Heo /**
2438201af4c0STejun Heo  * cgroup_taskset_first - reset taskset and return the first task
2439201af4c0STejun Heo  * @tset: taskset of interest
2440201af4c0STejun Heo  * @dst_cssp: output variable for the destination css
2441201af4c0STejun Heo  *
2442201af4c0STejun Heo  * @tset iteration is initialized and the first task is returned.
2443201af4c0STejun Heo  */
cgroup_taskset_first(struct cgroup_taskset * tset,struct cgroup_subsys_state ** dst_cssp)2444201af4c0STejun Heo struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2445201af4c0STejun Heo 					 struct cgroup_subsys_state **dst_cssp)
2446201af4c0STejun Heo {
2447201af4c0STejun Heo 	tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2448201af4c0STejun Heo 	tset->cur_task = NULL;
2449201af4c0STejun Heo 
2450201af4c0STejun Heo 	return cgroup_taskset_next(tset, dst_cssp);
2451201af4c0STejun Heo }
2452201af4c0STejun Heo 
2453201af4c0STejun Heo /**
2454201af4c0STejun Heo  * cgroup_taskset_next - iterate to the next task in taskset
2455201af4c0STejun Heo  * @tset: taskset of interest
2456201af4c0STejun Heo  * @dst_cssp: output variable for the destination css
2457201af4c0STejun Heo  *
2458201af4c0STejun Heo  * Return the next task in @tset.  Iteration must have been initialized
2459201af4c0STejun Heo  * with cgroup_taskset_first().
2460201af4c0STejun Heo  */
cgroup_taskset_next(struct cgroup_taskset * tset,struct cgroup_subsys_state ** dst_cssp)2461201af4c0STejun Heo struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2462201af4c0STejun Heo 					struct cgroup_subsys_state **dst_cssp)
2463201af4c0STejun Heo {
2464201af4c0STejun Heo 	struct css_set *cset = tset->cur_cset;
2465201af4c0STejun Heo 	struct task_struct *task = tset->cur_task;
2466201af4c0STejun Heo 
2467d20d30ebSKees Cook 	while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) {
2468201af4c0STejun Heo 		if (!task)
2469201af4c0STejun Heo 			task = list_first_entry(&cset->mg_tasks,
2470201af4c0STejun Heo 						struct task_struct, cg_list);
2471201af4c0STejun Heo 		else
2472201af4c0STejun Heo 			task = list_next_entry(task, cg_list);
2473201af4c0STejun Heo 
2474201af4c0STejun Heo 		if (&task->cg_list != &cset->mg_tasks) {
2475201af4c0STejun Heo 			tset->cur_cset = cset;
2476201af4c0STejun Heo 			tset->cur_task = task;
2477201af4c0STejun Heo 
2478201af4c0STejun Heo 			/*
2479201af4c0STejun Heo 			 * This function may be called both before and
24806f71780eSMiaohe Lin 			 * after cgroup_migrate_execute().  The two cases
2481201af4c0STejun Heo 			 * can be distinguished by looking at whether @cset
2482201af4c0STejun Heo 			 * has its ->mg_dst_cset set.
2483201af4c0STejun Heo 			 */
2484201af4c0STejun Heo 			if (cset->mg_dst_cset)
2485201af4c0STejun Heo 				*dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2486201af4c0STejun Heo 			else
2487201af4c0STejun Heo 				*dst_cssp = cset->subsys[tset->ssid];
2488201af4c0STejun Heo 
2489201af4c0STejun Heo 			return task;
2490201af4c0STejun Heo 		}
2491201af4c0STejun Heo 
2492201af4c0STejun Heo 		cset = list_next_entry(cset, mg_node);
2493201af4c0STejun Heo 		task = NULL;
2494201af4c0STejun Heo 	}
2495201af4c0STejun Heo 
2496201af4c0STejun Heo 	return NULL;
2497201af4c0STejun Heo }
2498201af4c0STejun Heo 
2499201af4c0STejun Heo /**
25002ca11b0eSYang Li  * cgroup_migrate_execute - migrate a taskset
2501e595cd70STejun Heo  * @mgctx: migration context
2502201af4c0STejun Heo  *
2503e595cd70STejun Heo  * Migrate tasks in @mgctx as setup by migration preparation functions.
2504201af4c0STejun Heo  * This function fails iff one of the ->can_attach callbacks fails and
2505e595cd70STejun Heo  * guarantees that either all or none of the tasks in @mgctx are migrated.
2506e595cd70STejun Heo  * @mgctx is consumed regardless of success.
2507201af4c0STejun Heo  */
cgroup_migrate_execute(struct cgroup_mgctx * mgctx)2508bfc2cf6fSTejun Heo static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
2509201af4c0STejun Heo {
2510e595cd70STejun Heo 	struct cgroup_taskset *tset = &mgctx->tset;
2511201af4c0STejun Heo 	struct cgroup_subsys *ss;
2512201af4c0STejun Heo 	struct task_struct *task, *tmp_task;
2513201af4c0STejun Heo 	struct css_set *cset, *tmp_cset;
2514201af4c0STejun Heo 	int ssid, failed_ssid, ret;
2515201af4c0STejun Heo 
2516201af4c0STejun Heo 	/* check that we can legitimately attach to the cgroup */
251761046727STejun Heo 	if (tset->nr_tasks) {
2518bfc2cf6fSTejun Heo 		do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2519201af4c0STejun Heo 			if (ss->can_attach) {
2520201af4c0STejun Heo 				tset->ssid = ssid;
2521201af4c0STejun Heo 				ret = ss->can_attach(tset);
2522201af4c0STejun Heo 				if (ret) {
2523201af4c0STejun Heo 					failed_ssid = ssid;
2524201af4c0STejun Heo 					goto out_cancel_attach;
2525201af4c0STejun Heo 				}
2526201af4c0STejun Heo 			}
2527201af4c0STejun Heo 		} while_each_subsys_mask();
252861046727STejun Heo 	}
2529201af4c0STejun Heo 
2530201af4c0STejun Heo 	/*
2531201af4c0STejun Heo 	 * Now that we're guaranteed success, proceed to move all tasks to
2532201af4c0STejun Heo 	 * the new cgroup.  There are no failure cases after here, so this
2533201af4c0STejun Heo 	 * is the commit point.
2534201af4c0STejun Heo 	 */
2535201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2536201af4c0STejun Heo 	list_for_each_entry(cset, &tset->src_csets, mg_node) {
2537201af4c0STejun Heo 		list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2538201af4c0STejun Heo 			struct css_set *from_cset = task_css_set(task);
2539201af4c0STejun Heo 			struct css_set *to_cset = cset->mg_dst_cset;
2540201af4c0STejun Heo 
2541201af4c0STejun Heo 			get_css_set(to_cset);
254273a7242aSWaiman Long 			to_cset->nr_tasks++;
2543201af4c0STejun Heo 			css_set_move_task(task, from_cset, to_cset, true);
254473a7242aSWaiman Long 			from_cset->nr_tasks--;
254576f969e8SRoman Gushchin 			/*
254676f969e8SRoman Gushchin 			 * If the source or destination cgroup is frozen,
254776f969e8SRoman Gushchin 			 * the task might require to change its state.
254876f969e8SRoman Gushchin 			 */
254976f969e8SRoman Gushchin 			cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp,
255076f969e8SRoman Gushchin 						    to_cset->dfl_cgrp);
255176f969e8SRoman Gushchin 			put_css_set_locked(from_cset);
255276f969e8SRoman Gushchin 
2553201af4c0STejun Heo 		}
2554201af4c0STejun Heo 	}
2555201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
2556201af4c0STejun Heo 
2557201af4c0STejun Heo 	/*
2558201af4c0STejun Heo 	 * Migration is committed, all target tasks are now on dst_csets.
2559201af4c0STejun Heo 	 * Nothing is sensitive to fork() after this point.  Notify
2560201af4c0STejun Heo 	 * controllers that migration is complete.
2561201af4c0STejun Heo 	 */
2562201af4c0STejun Heo 	tset->csets = &tset->dst_csets;
2563201af4c0STejun Heo 
256461046727STejun Heo 	if (tset->nr_tasks) {
2565bfc2cf6fSTejun Heo 		do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2566201af4c0STejun Heo 			if (ss->attach) {
2567201af4c0STejun Heo 				tset->ssid = ssid;
2568201af4c0STejun Heo 				ss->attach(tset);
2569201af4c0STejun Heo 			}
2570201af4c0STejun Heo 		} while_each_subsys_mask();
257161046727STejun Heo 	}
2572201af4c0STejun Heo 
2573201af4c0STejun Heo 	ret = 0;
2574201af4c0STejun Heo 	goto out_release_tset;
2575201af4c0STejun Heo 
2576201af4c0STejun Heo out_cancel_attach:
257761046727STejun Heo 	if (tset->nr_tasks) {
2578bfc2cf6fSTejun Heo 		do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2579201af4c0STejun Heo 			if (ssid == failed_ssid)
2580201af4c0STejun Heo 				break;
2581201af4c0STejun Heo 			if (ss->cancel_attach) {
2582201af4c0STejun Heo 				tset->ssid = ssid;
2583201af4c0STejun Heo 				ss->cancel_attach(tset);
2584201af4c0STejun Heo 			}
2585201af4c0STejun Heo 		} while_each_subsys_mask();
258661046727STejun Heo 	}
2587201af4c0STejun Heo out_release_tset:
2588201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2589201af4c0STejun Heo 	list_splice_init(&tset->dst_csets, &tset->src_csets);
2590201af4c0STejun Heo 	list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2591201af4c0STejun Heo 		list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2592201af4c0STejun Heo 		list_del_init(&cset->mg_node);
2593201af4c0STejun Heo 	}
2594201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
2595c4fa6c43SWaiman Long 
2596c4fa6c43SWaiman Long 	/*
2597c4fa6c43SWaiman Long 	 * Re-initialize the cgroup_taskset structure in case it is reused
2598c4fa6c43SWaiman Long 	 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2599c4fa6c43SWaiman Long 	 * iteration.
2600c4fa6c43SWaiman Long 	 */
2601c4fa6c43SWaiman Long 	tset->nr_tasks = 0;
2602c4fa6c43SWaiman Long 	tset->csets    = &tset->src_csets;
2603201af4c0STejun Heo 	return ret;
2604201af4c0STejun Heo }
2605201af4c0STejun Heo 
2606201af4c0STejun Heo /**
26078cfd8147STejun Heo  * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
2608201af4c0STejun Heo  * @dst_cgrp: destination cgroup to test
2609201af4c0STejun Heo  *
26108cfd8147STejun Heo  * On the default hierarchy, except for the mixable, (possible) thread root
26118cfd8147STejun Heo  * and threaded cgroups, subtree_control must be zero for migration
26128cfd8147STejun Heo  * destination cgroups with tasks so that child cgroups don't compete
26138cfd8147STejun Heo  * against tasks.
2614201af4c0STejun Heo  */
cgroup_migrate_vet_dst(struct cgroup * dst_cgrp)26158cfd8147STejun Heo int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
2616201af4c0STejun Heo {
26178cfd8147STejun Heo 	/* v1 doesn't have any restriction */
26188cfd8147STejun Heo 	if (!cgroup_on_dfl(dst_cgrp))
26198cfd8147STejun Heo 		return 0;
26208cfd8147STejun Heo 
26218cfd8147STejun Heo 	/* verify @dst_cgrp can host resources */
26228cfd8147STejun Heo 	if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
26238cfd8147STejun Heo 		return -EOPNOTSUPP;
26248cfd8147STejun Heo 
26258cfd8147STejun Heo 	/*
26268cfd8147STejun Heo 	 * If @dst_cgrp is already or can become a thread root or is
26278cfd8147STejun Heo 	 * threaded, it doesn't matter.
26288cfd8147STejun Heo 	 */
26298cfd8147STejun Heo 	if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
26308cfd8147STejun Heo 		return 0;
26318cfd8147STejun Heo 
26328cfd8147STejun Heo 	/* apply no-internal-process constraint */
26338cfd8147STejun Heo 	if (dst_cgrp->subtree_control)
26348cfd8147STejun Heo 		return -EBUSY;
26358cfd8147STejun Heo 
26368cfd8147STejun Heo 	return 0;
2637201af4c0STejun Heo }
2638201af4c0STejun Heo 
2639201af4c0STejun Heo /**
2640201af4c0STejun Heo  * cgroup_migrate_finish - cleanup after attach
2641e595cd70STejun Heo  * @mgctx: migration context
2642201af4c0STejun Heo  *
2643201af4c0STejun Heo  * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst().  See
2644201af4c0STejun Heo  * those functions for details.
2645201af4c0STejun Heo  */
cgroup_migrate_finish(struct cgroup_mgctx * mgctx)2646e595cd70STejun Heo void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
2647201af4c0STejun Heo {
2648201af4c0STejun Heo 	struct css_set *cset, *tmp_cset;
2649201af4c0STejun Heo 
2650201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
2651201af4c0STejun Heo 
2652201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2653e595cd70STejun Heo 
265407fd5b6cSTejun Heo 	list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_src_csets,
265507fd5b6cSTejun Heo 				 mg_src_preload_node) {
2656201af4c0STejun Heo 		cset->mg_src_cgrp = NULL;
2657201af4c0STejun Heo 		cset->mg_dst_cgrp = NULL;
2658201af4c0STejun Heo 		cset->mg_dst_cset = NULL;
265907fd5b6cSTejun Heo 		list_del_init(&cset->mg_src_preload_node);
266007fd5b6cSTejun Heo 		put_css_set_locked(cset);
266107fd5b6cSTejun Heo 	}
266207fd5b6cSTejun Heo 
266307fd5b6cSTejun Heo 	list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_dst_csets,
266407fd5b6cSTejun Heo 				 mg_dst_preload_node) {
266507fd5b6cSTejun Heo 		cset->mg_src_cgrp = NULL;
266607fd5b6cSTejun Heo 		cset->mg_dst_cgrp = NULL;
266707fd5b6cSTejun Heo 		cset->mg_dst_cset = NULL;
266807fd5b6cSTejun Heo 		list_del_init(&cset->mg_dst_preload_node);
2669201af4c0STejun Heo 		put_css_set_locked(cset);
2670201af4c0STejun Heo 	}
2671e595cd70STejun Heo 
2672201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
2673201af4c0STejun Heo }
2674201af4c0STejun Heo 
2675201af4c0STejun Heo /**
2676201af4c0STejun Heo  * cgroup_migrate_add_src - add a migration source css_set
2677201af4c0STejun Heo  * @src_cset: the source css_set to add
2678201af4c0STejun Heo  * @dst_cgrp: the destination cgroup
2679e595cd70STejun Heo  * @mgctx: migration context
2680201af4c0STejun Heo  *
2681201af4c0STejun Heo  * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp.  Pin
2682e595cd70STejun Heo  * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2683201af4c0STejun Heo  * up by cgroup_migrate_finish().
2684201af4c0STejun Heo  *
2685201af4c0STejun Heo  * This function may be called without holding cgroup_threadgroup_rwsem
2686201af4c0STejun Heo  * even if the target is a process.  Threads may be created and destroyed
2687201af4c0STejun Heo  * but as long as cgroup_mutex is not dropped, no new css_set can be put
2688201af4c0STejun Heo  * into play and the preloaded css_sets are guaranteed to cover all
2689201af4c0STejun Heo  * migrations.
2690201af4c0STejun Heo  */
cgroup_migrate_add_src(struct css_set * src_cset,struct cgroup * dst_cgrp,struct cgroup_mgctx * mgctx)26910a268dbdSTejun Heo void cgroup_migrate_add_src(struct css_set *src_cset,
2692201af4c0STejun Heo 			    struct cgroup *dst_cgrp,
2693e595cd70STejun Heo 			    struct cgroup_mgctx *mgctx)
2694201af4c0STejun Heo {
2695201af4c0STejun Heo 	struct cgroup *src_cgrp;
2696201af4c0STejun Heo 
2697201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
2698201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
2699201af4c0STejun Heo 
2700201af4c0STejun Heo 	/*
2701201af4c0STejun Heo 	 * If ->dead, @src_set is associated with one or more dead cgroups
2702201af4c0STejun Heo 	 * and doesn't contain any migratable tasks.  Ignore it early so
2703201af4c0STejun Heo 	 * that the rest of migration path doesn't get confused by it.
2704201af4c0STejun Heo 	 */
2705201af4c0STejun Heo 	if (src_cset->dead)
2706201af4c0STejun Heo 		return;
2707201af4c0STejun Heo 
270807fd5b6cSTejun Heo 	if (!list_empty(&src_cset->mg_src_preload_node))
2709201af4c0STejun Heo 		return;
2710201af4c0STejun Heo 
27111815775eSWei Yang 	src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
27121815775eSWei Yang 
2713201af4c0STejun Heo 	WARN_ON(src_cset->mg_src_cgrp);
2714201af4c0STejun Heo 	WARN_ON(src_cset->mg_dst_cgrp);
2715201af4c0STejun Heo 	WARN_ON(!list_empty(&src_cset->mg_tasks));
2716201af4c0STejun Heo 	WARN_ON(!list_empty(&src_cset->mg_node));
2717201af4c0STejun Heo 
2718201af4c0STejun Heo 	src_cset->mg_src_cgrp = src_cgrp;
2719201af4c0STejun Heo 	src_cset->mg_dst_cgrp = dst_cgrp;
2720201af4c0STejun Heo 	get_css_set(src_cset);
272107fd5b6cSTejun Heo 	list_add_tail(&src_cset->mg_src_preload_node, &mgctx->preloaded_src_csets);
2722201af4c0STejun Heo }
2723201af4c0STejun Heo 
2724201af4c0STejun Heo /**
2725201af4c0STejun Heo  * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2726e595cd70STejun Heo  * @mgctx: migration context
2727201af4c0STejun Heo  *
2728201af4c0STejun Heo  * Tasks are about to be moved and all the source css_sets have been
2729e595cd70STejun Heo  * preloaded to @mgctx->preloaded_src_csets.  This function looks up and
2730e595cd70STejun Heo  * pins all destination css_sets, links each to its source, and append them
2731e595cd70STejun Heo  * to @mgctx->preloaded_dst_csets.
2732201af4c0STejun Heo  *
2733201af4c0STejun Heo  * This function must be called after cgroup_migrate_add_src() has been
2734201af4c0STejun Heo  * called on each migration source css_set.  After migration is performed
2735201af4c0STejun Heo  * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2736e595cd70STejun Heo  * @mgctx.
2737201af4c0STejun Heo  */
cgroup_migrate_prepare_dst(struct cgroup_mgctx * mgctx)2738e595cd70STejun Heo int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
2739201af4c0STejun Heo {
2740201af4c0STejun Heo 	struct css_set *src_cset, *tmp_cset;
2741201af4c0STejun Heo 
2742201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
2743201af4c0STejun Heo 
2744201af4c0STejun Heo 	/* look up the dst cset for each src cset and link it to src */
2745e595cd70STejun Heo 	list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
274607fd5b6cSTejun Heo 				 mg_src_preload_node) {
2747201af4c0STejun Heo 		struct css_set *dst_cset;
2748bfc2cf6fSTejun Heo 		struct cgroup_subsys *ss;
2749bfc2cf6fSTejun Heo 		int ssid;
2750201af4c0STejun Heo 
2751201af4c0STejun Heo 		dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
2752201af4c0STejun Heo 		if (!dst_cset)
2753d6e486eeSShakeel Butt 			return -ENOMEM;
2754201af4c0STejun Heo 
2755201af4c0STejun Heo 		WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
2756201af4c0STejun Heo 
2757201af4c0STejun Heo 		/*
2758201af4c0STejun Heo 		 * If src cset equals dst, it's noop.  Drop the src.
2759201af4c0STejun Heo 		 * cgroup_migrate() will skip the cset too.  Note that we
2760201af4c0STejun Heo 		 * can't handle src == dst as some nodes are used by both.
2761201af4c0STejun Heo 		 */
2762201af4c0STejun Heo 		if (src_cset == dst_cset) {
2763201af4c0STejun Heo 			src_cset->mg_src_cgrp = NULL;
2764201af4c0STejun Heo 			src_cset->mg_dst_cgrp = NULL;
276507fd5b6cSTejun Heo 			list_del_init(&src_cset->mg_src_preload_node);
2766201af4c0STejun Heo 			put_css_set(src_cset);
2767201af4c0STejun Heo 			put_css_set(dst_cset);
2768201af4c0STejun Heo 			continue;
2769201af4c0STejun Heo 		}
2770201af4c0STejun Heo 
2771201af4c0STejun Heo 		src_cset->mg_dst_cset = dst_cset;
2772201af4c0STejun Heo 
277307fd5b6cSTejun Heo 		if (list_empty(&dst_cset->mg_dst_preload_node))
277407fd5b6cSTejun Heo 			list_add_tail(&dst_cset->mg_dst_preload_node,
2775e595cd70STejun Heo 				      &mgctx->preloaded_dst_csets);
2776201af4c0STejun Heo 		else
2777201af4c0STejun Heo 			put_css_set(dst_cset);
2778bfc2cf6fSTejun Heo 
2779bfc2cf6fSTejun Heo 		for_each_subsys(ss, ssid)
2780bfc2cf6fSTejun Heo 			if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2781bfc2cf6fSTejun Heo 				mgctx->ss_mask |= 1 << ssid;
2782201af4c0STejun Heo 	}
2783201af4c0STejun Heo 
2784201af4c0STejun Heo 	return 0;
2785201af4c0STejun Heo }
2786201af4c0STejun Heo 
2787201af4c0STejun Heo /**
2788201af4c0STejun Heo  * cgroup_migrate - migrate a process or task to a cgroup
2789201af4c0STejun Heo  * @leader: the leader of the process or the task to migrate
2790201af4c0STejun Heo  * @threadgroup: whether @leader points to the whole process or a single task
2791e595cd70STejun Heo  * @mgctx: migration context
2792201af4c0STejun Heo  *
2793201af4c0STejun Heo  * Migrate a process or task denoted by @leader.  If migrating a process,
2794201af4c0STejun Heo  * the caller must be holding cgroup_threadgroup_rwsem.  The caller is also
2795201af4c0STejun Heo  * responsible for invoking cgroup_migrate_add_src() and
2796201af4c0STejun Heo  * cgroup_migrate_prepare_dst() on the targets before invoking this
2797201af4c0STejun Heo  * function and following up with cgroup_migrate_finish().
2798201af4c0STejun Heo  *
2799201af4c0STejun Heo  * As long as a controller's ->can_attach() doesn't fail, this function is
2800201af4c0STejun Heo  * guaranteed to succeed.  This means that, excluding ->can_attach()
2801201af4c0STejun Heo  * failure, when migrating multiple targets, the success or failure can be
2802201af4c0STejun Heo  * decided for all targets by invoking group_migrate_prepare_dst() before
2803201af4c0STejun Heo  * actually starting migrating.
2804201af4c0STejun Heo  */
cgroup_migrate(struct task_struct * leader,bool threadgroup,struct cgroup_mgctx * mgctx)28050a268dbdSTejun Heo int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2806bfc2cf6fSTejun Heo 		   struct cgroup_mgctx *mgctx)
2807201af4c0STejun Heo {
2808201af4c0STejun Heo 	struct task_struct *task;
2809201af4c0STejun Heo 
2810201af4c0STejun Heo 	/*
2811659db078SXiu Jianfeng 	 * The following thread iteration should be inside an RCU critical
2812659db078SXiu Jianfeng 	 * section to prevent tasks from being freed while taking the snapshot.
2813659db078SXiu Jianfeng 	 * spin_lock_irq() implies RCU critical section here.
2814201af4c0STejun Heo 	 */
2815201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2816201af4c0STejun Heo 	task = leader;
2817201af4c0STejun Heo 	do {
2818e595cd70STejun Heo 		cgroup_migrate_add_task(task, mgctx);
2819201af4c0STejun Heo 		if (!threadgroup)
2820201af4c0STejun Heo 			break;
2821201af4c0STejun Heo 	} while_each_thread(leader, task);
2822201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
2823201af4c0STejun Heo 
2824bfc2cf6fSTejun Heo 	return cgroup_migrate_execute(mgctx);
2825201af4c0STejun Heo }
2826201af4c0STejun Heo 
2827201af4c0STejun Heo /**
2828201af4c0STejun Heo  * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2829201af4c0STejun Heo  * @dst_cgrp: the cgroup to attach to
2830201af4c0STejun Heo  * @leader: the task or the leader of the threadgroup to be attached
2831201af4c0STejun Heo  * @threadgroup: attach the whole threadgroup?
2832201af4c0STejun Heo  *
2833201af4c0STejun Heo  * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2834201af4c0STejun Heo  */
cgroup_attach_task(struct cgroup * dst_cgrp,struct task_struct * leader,bool threadgroup)28350a268dbdSTejun Heo int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
28360a268dbdSTejun Heo 		       bool threadgroup)
2837201af4c0STejun Heo {
2838e595cd70STejun Heo 	DEFINE_CGROUP_MGCTX(mgctx);
2839201af4c0STejun Heo 	struct task_struct *task;
28406df970e4SChristian Brauner 	int ret = 0;
2841201af4c0STejun Heo 
2842201af4c0STejun Heo 	/* look up all src csets */
2843201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2844201af4c0STejun Heo 	rcu_read_lock();
2845201af4c0STejun Heo 	task = leader;
2846201af4c0STejun Heo 	do {
2847e595cd70STejun Heo 		cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
2848201af4c0STejun Heo 		if (!threadgroup)
2849201af4c0STejun Heo 			break;
2850201af4c0STejun Heo 	} while_each_thread(leader, task);
2851201af4c0STejun Heo 	rcu_read_unlock();
2852201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
2853201af4c0STejun Heo 
2854201af4c0STejun Heo 	/* prepare dst csets and commit */
2855e595cd70STejun Heo 	ret = cgroup_migrate_prepare_dst(&mgctx);
2856201af4c0STejun Heo 	if (!ret)
2857bfc2cf6fSTejun Heo 		ret = cgroup_migrate(leader, threadgroup, &mgctx);
2858201af4c0STejun Heo 
2859e595cd70STejun Heo 	cgroup_migrate_finish(&mgctx);
2860201af4c0STejun Heo 
2861201af4c0STejun Heo 	if (!ret)
2862e4f8d81cSSteven Rostedt (VMware) 		TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
2863201af4c0STejun Heo 
2864201af4c0STejun Heo 	return ret;
2865201af4c0STejun Heo }
2866201af4c0STejun Heo 
cgroup_procs_write_start(char * buf,bool threadgroup,bool * threadgroup_locked)28679a3284faSMichal Koutný struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
28684f7e7236STejun Heo 					     bool *threadgroup_locked)
2869201af4c0STejun Heo {
2870201af4c0STejun Heo 	struct task_struct *tsk;
2871201af4c0STejun Heo 	pid_t pid;
2872201af4c0STejun Heo 
2873201af4c0STejun Heo 	if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2874715c809dSTejun Heo 		return ERR_PTR(-EINVAL);
2875201af4c0STejun Heo 
28769a3284faSMichal Koutný 	/*
28779a3284faSMichal Koutný 	 * If we migrate a single thread, we don't care about threadgroup
28789a3284faSMichal Koutný 	 * stability. If the thread is `current`, it won't exit(2) under our
28799a3284faSMichal Koutný 	 * hands or change PID through exec(2). We exclude
28809a3284faSMichal Koutný 	 * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write
28819a3284faSMichal Koutný 	 * callers by cgroup_mutex.
28829a3284faSMichal Koutný 	 * Therefore, we can skip the global lock.
28839a3284faSMichal Koutný 	 */
28849a3284faSMichal Koutný 	lockdep_assert_held(&cgroup_mutex);
28854f7e7236STejun Heo 	*threadgroup_locked = pid || threadgroup;
28864f7e7236STejun Heo 	cgroup_attach_lock(*threadgroup_locked);
2887715c809dSTejun Heo 
2888201af4c0STejun Heo 	rcu_read_lock();
2889201af4c0STejun Heo 	if (pid) {
2890201af4c0STejun Heo 		tsk = find_task_by_vpid(pid);
2891201af4c0STejun Heo 		if (!tsk) {
2892715c809dSTejun Heo 			tsk = ERR_PTR(-ESRCH);
2893715c809dSTejun Heo 			goto out_unlock_threadgroup;
2894201af4c0STejun Heo 		}
2895201af4c0STejun Heo 	} else {
2896201af4c0STejun Heo 		tsk = current;
2897201af4c0STejun Heo 	}
2898201af4c0STejun Heo 
2899201af4c0STejun Heo 	if (threadgroup)
2900201af4c0STejun Heo 		tsk = tsk->group_leader;
2901201af4c0STejun Heo 
2902201af4c0STejun Heo 	/*
290377f88796STejun Heo 	 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
290477f88796STejun Heo 	 * If userland migrates such a kthread to a non-root cgroup, it can
290577f88796STejun Heo 	 * become trapped in a cpuset, or RT kthread may be born in a
290677f88796STejun Heo 	 * cgroup with no rt_runtime allocated.  Just say no.
2907201af4c0STejun Heo 	 */
290877f88796STejun Heo 	if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
2909715c809dSTejun Heo 		tsk = ERR_PTR(-EINVAL);
2910715c809dSTejun Heo 		goto out_unlock_threadgroup;
2911201af4c0STejun Heo 	}
2912201af4c0STejun Heo 
2913201af4c0STejun Heo 	get_task_struct(tsk);
2914715c809dSTejun Heo 	goto out_unlock_rcu;
2915201af4c0STejun Heo 
2916715c809dSTejun Heo out_unlock_threadgroup:
29174f7e7236STejun Heo 	cgroup_attach_unlock(*threadgroup_locked);
29184f7e7236STejun Heo 	*threadgroup_locked = false;
2919201af4c0STejun Heo out_unlock_rcu:
2920201af4c0STejun Heo 	rcu_read_unlock();
2921715c809dSTejun Heo 	return tsk;
2922715c809dSTejun Heo }
2923715c809dSTejun Heo 
cgroup_procs_write_finish(struct task_struct * task,bool threadgroup_locked)29244f7e7236STejun Heo void cgroup_procs_write_finish(struct task_struct *task, bool threadgroup_locked)
2925715c809dSTejun Heo {
2926715c809dSTejun Heo 	struct cgroup_subsys *ss;
2927715c809dSTejun Heo 	int ssid;
2928715c809dSTejun Heo 
2929715c809dSTejun Heo 	/* release reference from cgroup_procs_write_start() */
2930715c809dSTejun Heo 	put_task_struct(task);
2931715c809dSTejun Heo 
29324f7e7236STejun Heo 	cgroup_attach_unlock(threadgroup_locked);
29334f7e7236STejun Heo 
2934201af4c0STejun Heo 	for_each_subsys(ss, ssid)
2935201af4c0STejun Heo 		if (ss->post_attach)
2936201af4c0STejun Heo 			ss->post_attach();
2937201af4c0STejun Heo }
2938201af4c0STejun Heo 
cgroup_print_ss_mask(struct seq_file * seq,u16 ss_mask)2939201af4c0STejun Heo static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
2940201af4c0STejun Heo {
2941201af4c0STejun Heo 	struct cgroup_subsys *ss;
2942201af4c0STejun Heo 	bool printed = false;
2943201af4c0STejun Heo 	int ssid;
2944201af4c0STejun Heo 
2945201af4c0STejun Heo 	do_each_subsys_mask(ss, ssid, ss_mask) {
2946201af4c0STejun Heo 		if (printed)
2947201af4c0STejun Heo 			seq_putc(seq, ' ');
294885db0023SMarkus Elfring 		seq_puts(seq, ss->name);
2949201af4c0STejun Heo 		printed = true;
2950201af4c0STejun Heo 	} while_each_subsys_mask();
2951201af4c0STejun Heo 	if (printed)
2952201af4c0STejun Heo 		seq_putc(seq, '\n');
2953201af4c0STejun Heo }
2954201af4c0STejun Heo 
2955201af4c0STejun Heo /* show controllers which are enabled from the parent */
cgroup_controllers_show(struct seq_file * seq,void * v)2956201af4c0STejun Heo static int cgroup_controllers_show(struct seq_file *seq, void *v)
2957201af4c0STejun Heo {
2958201af4c0STejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
2959201af4c0STejun Heo 
2960201af4c0STejun Heo 	cgroup_print_ss_mask(seq, cgroup_control(cgrp));
2961201af4c0STejun Heo 	return 0;
2962201af4c0STejun Heo }
2963201af4c0STejun Heo 
2964201af4c0STejun Heo /* show controllers which are enabled for a given cgroup's children */
cgroup_subtree_control_show(struct seq_file * seq,void * v)2965201af4c0STejun Heo static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2966201af4c0STejun Heo {
2967201af4c0STejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
2968201af4c0STejun Heo 
2969201af4c0STejun Heo 	cgroup_print_ss_mask(seq, cgrp->subtree_control);
2970201af4c0STejun Heo 	return 0;
2971201af4c0STejun Heo }
2972201af4c0STejun Heo 
2973201af4c0STejun Heo /**
2974201af4c0STejun Heo  * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2975201af4c0STejun Heo  * @cgrp: root of the subtree to update csses for
2976201af4c0STejun Heo  *
2977201af4c0STejun Heo  * @cgrp's control masks have changed and its subtree's css associations
2978201af4c0STejun Heo  * need to be updated accordingly.  This function looks up all css_sets
2979201af4c0STejun Heo  * which are attached to the subtree, creates the matching updated css_sets
2980201af4c0STejun Heo  * and migrates the tasks to the new ones.
2981201af4c0STejun Heo  */
cgroup_update_dfl_csses(struct cgroup * cgrp)2982201af4c0STejun Heo static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2983201af4c0STejun Heo {
2984e595cd70STejun Heo 	DEFINE_CGROUP_MGCTX(mgctx);
2985201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
2986201af4c0STejun Heo 	struct cgroup *dsct;
2987201af4c0STejun Heo 	struct css_set *src_cset;
2988671c11f0STejun Heo 	bool has_tasks;
2989201af4c0STejun Heo 	int ret;
2990201af4c0STejun Heo 
2991201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
2992201af4c0STejun Heo 
2993201af4c0STejun Heo 	/* look up all csses currently attached to @cgrp's subtree */
2994201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
2995201af4c0STejun Heo 	cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2996201af4c0STejun Heo 		struct cgrp_cset_link *link;
2997201af4c0STejun Heo 
2998265792d0SWaiman Long 		/*
2999265792d0SWaiman Long 		 * As cgroup_update_dfl_csses() is only called by
3000265792d0SWaiman Long 		 * cgroup_apply_control(). The csses associated with the
3001265792d0SWaiman Long 		 * given cgrp will not be affected by changes made to
3002265792d0SWaiman Long 		 * its subtree_control file. We can skip them.
3003265792d0SWaiman Long 		 */
3004265792d0SWaiman Long 		if (dsct == cgrp)
3005265792d0SWaiman Long 			continue;
3006265792d0SWaiman Long 
3007201af4c0STejun Heo 		list_for_each_entry(link, &dsct->cset_links, cset_link)
3008e595cd70STejun Heo 			cgroup_migrate_add_src(link->cset, dsct, &mgctx);
3009201af4c0STejun Heo 	}
3010201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
3011201af4c0STejun Heo 
3012671c11f0STejun Heo 	/*
3013671c11f0STejun Heo 	 * We need to write-lock threadgroup_rwsem while migrating tasks.
3014671c11f0STejun Heo 	 * However, if there are no source csets for @cgrp, changing its
3015671c11f0STejun Heo 	 * controllers isn't gonna produce any task migrations and the
3016671c11f0STejun Heo 	 * write-locking can be skipped safely.
3017671c11f0STejun Heo 	 */
3018671c11f0STejun Heo 	has_tasks = !list_empty(&mgctx.preloaded_src_csets);
30194f7e7236STejun Heo 	cgroup_attach_lock(has_tasks);
3020671c11f0STejun Heo 
3021201af4c0STejun Heo 	/* NULL dst indicates self on default hierarchy */
3022e595cd70STejun Heo 	ret = cgroup_migrate_prepare_dst(&mgctx);
3023201af4c0STejun Heo 	if (ret)
3024201af4c0STejun Heo 		goto out_finish;
3025201af4c0STejun Heo 
3026201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
302707fd5b6cSTejun Heo 	list_for_each_entry(src_cset, &mgctx.preloaded_src_csets,
302807fd5b6cSTejun Heo 			    mg_src_preload_node) {
3029201af4c0STejun Heo 		struct task_struct *task, *ntask;
3030201af4c0STejun Heo 
3031201af4c0STejun Heo 		/* all tasks in src_csets need to be migrated */
3032201af4c0STejun Heo 		list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
3033e595cd70STejun Heo 			cgroup_migrate_add_task(task, &mgctx);
3034201af4c0STejun Heo 	}
3035201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
3036201af4c0STejun Heo 
3037bfc2cf6fSTejun Heo 	ret = cgroup_migrate_execute(&mgctx);
3038201af4c0STejun Heo out_finish:
3039e595cd70STejun Heo 	cgroup_migrate_finish(&mgctx);
30404f7e7236STejun Heo 	cgroup_attach_unlock(has_tasks);
3041201af4c0STejun Heo 	return ret;
3042201af4c0STejun Heo }
3043201af4c0STejun Heo 
3044201af4c0STejun Heo /**
3045201af4c0STejun Heo  * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
3046201af4c0STejun Heo  * @cgrp: root of the target subtree
3047201af4c0STejun Heo  *
3048201af4c0STejun Heo  * Because css offlining is asynchronous, userland may try to re-enable a
3049201af4c0STejun Heo  * controller while the previous css is still around.  This function grabs
3050201af4c0STejun Heo  * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
3051201af4c0STejun Heo  */
cgroup_lock_and_drain_offline(struct cgroup * cgrp)30520a268dbdSTejun Heo void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
3053201af4c0STejun Heo 	__acquires(&cgroup_mutex)
3054201af4c0STejun Heo {
3055201af4c0STejun Heo 	struct cgroup *dsct;
3056201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
3057201af4c0STejun Heo 	struct cgroup_subsys *ss;
3058201af4c0STejun Heo 	int ssid;
3059201af4c0STejun Heo 
3060201af4c0STejun Heo restart:
30614cdb91b0SKamalesh Babulal 	cgroup_lock();
3062201af4c0STejun Heo 
3063201af4c0STejun Heo 	cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
3064201af4c0STejun Heo 		for_each_subsys(ss, ssid) {
3065201af4c0STejun Heo 			struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3066201af4c0STejun Heo 			DEFINE_WAIT(wait);
3067201af4c0STejun Heo 
3068201af4c0STejun Heo 			if (!css || !percpu_ref_is_dying(&css->refcnt))
3069201af4c0STejun Heo 				continue;
3070201af4c0STejun Heo 
3071a590b90dSTejun Heo 			cgroup_get_live(dsct);
3072201af4c0STejun Heo 			prepare_to_wait(&dsct->offline_waitq, &wait,
3073201af4c0STejun Heo 					TASK_UNINTERRUPTIBLE);
3074201af4c0STejun Heo 
30754cdb91b0SKamalesh Babulal 			cgroup_unlock();
3076201af4c0STejun Heo 			schedule();
3077201af4c0STejun Heo 			finish_wait(&dsct->offline_waitq, &wait);
3078201af4c0STejun Heo 
3079201af4c0STejun Heo 			cgroup_put(dsct);
3080201af4c0STejun Heo 			goto restart;
3081201af4c0STejun Heo 		}
3082201af4c0STejun Heo 	}
3083201af4c0STejun Heo }
3084201af4c0STejun Heo 
3085201af4c0STejun Heo /**
3086479adb89STejun Heo  * cgroup_save_control - save control masks and dom_cgrp of a subtree
3087201af4c0STejun Heo  * @cgrp: root of the target subtree
3088201af4c0STejun Heo  *
3089479adb89STejun Heo  * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
3090479adb89STejun Heo  * respective old_ prefixed fields for @cgrp's subtree including @cgrp
3091479adb89STejun Heo  * itself.
3092201af4c0STejun Heo  */
cgroup_save_control(struct cgroup * cgrp)3093201af4c0STejun Heo static void cgroup_save_control(struct cgroup *cgrp)
3094201af4c0STejun Heo {
3095201af4c0STejun Heo 	struct cgroup *dsct;
3096201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
3097201af4c0STejun Heo 
3098201af4c0STejun Heo 	cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3099201af4c0STejun Heo 		dsct->old_subtree_control = dsct->subtree_control;
3100201af4c0STejun Heo 		dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
3101479adb89STejun Heo 		dsct->old_dom_cgrp = dsct->dom_cgrp;
3102201af4c0STejun Heo 	}
3103201af4c0STejun Heo }
3104201af4c0STejun Heo 
3105201af4c0STejun Heo /**
3106201af4c0STejun Heo  * cgroup_propagate_control - refresh control masks of a subtree
3107201af4c0STejun Heo  * @cgrp: root of the target subtree
3108201af4c0STejun Heo  *
3109201af4c0STejun Heo  * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
3110201af4c0STejun Heo  * ->subtree_control and propagate controller availability through the
3111201af4c0STejun Heo  * subtree so that descendants don't have unavailable controllers enabled.
3112201af4c0STejun Heo  */
cgroup_propagate_control(struct cgroup * cgrp)3113201af4c0STejun Heo static void cgroup_propagate_control(struct cgroup *cgrp)
3114201af4c0STejun Heo {
3115201af4c0STejun Heo 	struct cgroup *dsct;
3116201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
3117201af4c0STejun Heo 
3118201af4c0STejun Heo 	cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3119201af4c0STejun Heo 		dsct->subtree_control &= cgroup_control(dsct);
3120201af4c0STejun Heo 		dsct->subtree_ss_mask =
3121201af4c0STejun Heo 			cgroup_calc_subtree_ss_mask(dsct->subtree_control,
3122201af4c0STejun Heo 						    cgroup_ss_mask(dsct));
3123201af4c0STejun Heo 	}
3124201af4c0STejun Heo }
3125201af4c0STejun Heo 
3126201af4c0STejun Heo /**
3127479adb89STejun Heo  * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
3128201af4c0STejun Heo  * @cgrp: root of the target subtree
3129201af4c0STejun Heo  *
3130479adb89STejun Heo  * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
3131479adb89STejun Heo  * respective old_ prefixed fields for @cgrp's subtree including @cgrp
3132479adb89STejun Heo  * itself.
3133201af4c0STejun Heo  */
cgroup_restore_control(struct cgroup * cgrp)3134201af4c0STejun Heo static void cgroup_restore_control(struct cgroup *cgrp)
3135201af4c0STejun Heo {
3136201af4c0STejun Heo 	struct cgroup *dsct;
3137201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
3138201af4c0STejun Heo 
3139201af4c0STejun Heo 	cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
3140201af4c0STejun Heo 		dsct->subtree_control = dsct->old_subtree_control;
3141201af4c0STejun Heo 		dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
3142479adb89STejun Heo 		dsct->dom_cgrp = dsct->old_dom_cgrp;
3143201af4c0STejun Heo 	}
3144201af4c0STejun Heo }
3145201af4c0STejun Heo 
css_visible(struct cgroup_subsys_state * css)3146201af4c0STejun Heo static bool css_visible(struct cgroup_subsys_state *css)
3147201af4c0STejun Heo {
3148201af4c0STejun Heo 	struct cgroup_subsys *ss = css->ss;
3149201af4c0STejun Heo 	struct cgroup *cgrp = css->cgroup;
3150201af4c0STejun Heo 
3151201af4c0STejun Heo 	if (cgroup_control(cgrp) & (1 << ss->id))
3152201af4c0STejun Heo 		return true;
3153201af4c0STejun Heo 	if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
3154201af4c0STejun Heo 		return false;
3155201af4c0STejun Heo 	return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
3156201af4c0STejun Heo }
3157201af4c0STejun Heo 
3158201af4c0STejun Heo /**
3159201af4c0STejun Heo  * cgroup_apply_control_enable - enable or show csses according to control
3160201af4c0STejun Heo  * @cgrp: root of the target subtree
3161201af4c0STejun Heo  *
3162201af4c0STejun Heo  * Walk @cgrp's subtree and create new csses or make the existing ones
3163201af4c0STejun Heo  * visible.  A css is created invisible if it's being implicitly enabled
3164201af4c0STejun Heo  * through dependency.  An invisible css is made visible when the userland
3165201af4c0STejun Heo  * explicitly enables it.
3166201af4c0STejun Heo  *
3167201af4c0STejun Heo  * Returns 0 on success, -errno on failure.  On failure, csses which have
3168201af4c0STejun Heo  * been processed already aren't cleaned up.  The caller is responsible for
31698a1115ffSMasahiro Yamada  * cleaning up with cgroup_apply_control_disable().
3170201af4c0STejun Heo  */
cgroup_apply_control_enable(struct cgroup * cgrp)3171201af4c0STejun Heo static int cgroup_apply_control_enable(struct cgroup *cgrp)
3172201af4c0STejun Heo {
3173201af4c0STejun Heo 	struct cgroup *dsct;
3174201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
3175201af4c0STejun Heo 	struct cgroup_subsys *ss;
3176201af4c0STejun Heo 	int ssid, ret;
3177201af4c0STejun Heo 
3178201af4c0STejun Heo 	cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3179201af4c0STejun Heo 		for_each_subsys(ss, ssid) {
3180201af4c0STejun Heo 			struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3181201af4c0STejun Heo 
3182201af4c0STejun Heo 			if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
3183201af4c0STejun Heo 				continue;
3184201af4c0STejun Heo 
3185201af4c0STejun Heo 			if (!css) {
3186201af4c0STejun Heo 				css = css_create(dsct, ss);
3187201af4c0STejun Heo 				if (IS_ERR(css))
3188201af4c0STejun Heo 					return PTR_ERR(css);
3189201af4c0STejun Heo 			}
3190201af4c0STejun Heo 
31913bc0bb36SMichal Koutný 			WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
31923bc0bb36SMichal Koutný 
3193201af4c0STejun Heo 			if (css_visible(css)) {
3194201af4c0STejun Heo 				ret = css_populate_dir(css);
3195201af4c0STejun Heo 				if (ret)
3196201af4c0STejun Heo 					return ret;
3197201af4c0STejun Heo 			}
3198201af4c0STejun Heo 		}
3199201af4c0STejun Heo 	}
3200201af4c0STejun Heo 
3201201af4c0STejun Heo 	return 0;
3202201af4c0STejun Heo }
3203201af4c0STejun Heo 
3204201af4c0STejun Heo /**
3205201af4c0STejun Heo  * cgroup_apply_control_disable - kill or hide csses according to control
3206201af4c0STejun Heo  * @cgrp: root of the target subtree
3207201af4c0STejun Heo  *
3208201af4c0STejun Heo  * Walk @cgrp's subtree and kill and hide csses so that they match
3209201af4c0STejun Heo  * cgroup_ss_mask() and cgroup_visible_mask().
3210201af4c0STejun Heo  *
3211201af4c0STejun Heo  * A css is hidden when the userland requests it to be disabled while other
3212201af4c0STejun Heo  * subsystems are still depending on it.  The css must not actively control
3213201af4c0STejun Heo  * resources and be in the vanilla state if it's made visible again later.
3214201af4c0STejun Heo  * Controllers which may be depended upon should provide ->css_reset() for
3215201af4c0STejun Heo  * this purpose.
3216201af4c0STejun Heo  */
cgroup_apply_control_disable(struct cgroup * cgrp)3217201af4c0STejun Heo static void cgroup_apply_control_disable(struct cgroup *cgrp)
3218201af4c0STejun Heo {
3219201af4c0STejun Heo 	struct cgroup *dsct;
3220201af4c0STejun Heo 	struct cgroup_subsys_state *d_css;
3221201af4c0STejun Heo 	struct cgroup_subsys *ss;
3222201af4c0STejun Heo 	int ssid;
3223201af4c0STejun Heo 
3224201af4c0STejun Heo 	cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
3225201af4c0STejun Heo 		for_each_subsys(ss, ssid) {
3226201af4c0STejun Heo 			struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3227201af4c0STejun Heo 
3228201af4c0STejun Heo 			if (!css)
3229201af4c0STejun Heo 				continue;
3230201af4c0STejun Heo 
32313bc0bb36SMichal Koutný 			WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
32323bc0bb36SMichal Koutný 
3233201af4c0STejun Heo 			if (css->parent &&
3234201af4c0STejun Heo 			    !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
3235201af4c0STejun Heo 				kill_css(css);
3236201af4c0STejun Heo 			} else if (!css_visible(css)) {
3237201af4c0STejun Heo 				css_clear_dir(css);
3238201af4c0STejun Heo 				if (ss->css_reset)
3239201af4c0STejun Heo 					ss->css_reset(css);
3240201af4c0STejun Heo 			}
3241201af4c0STejun Heo 		}
3242201af4c0STejun Heo 	}
3243201af4c0STejun Heo }
3244201af4c0STejun Heo 
3245201af4c0STejun Heo /**
3246201af4c0STejun Heo  * cgroup_apply_control - apply control mask updates to the subtree
3247201af4c0STejun Heo  * @cgrp: root of the target subtree
3248201af4c0STejun Heo  *
3249201af4c0STejun Heo  * subsystems can be enabled and disabled in a subtree using the following
3250201af4c0STejun Heo  * steps.
3251201af4c0STejun Heo  *
3252201af4c0STejun Heo  * 1. Call cgroup_save_control() to stash the current state.
3253201af4c0STejun Heo  * 2. Update ->subtree_control masks in the subtree as desired.
3254201af4c0STejun Heo  * 3. Call cgroup_apply_control() to apply the changes.
3255201af4c0STejun Heo  * 4. Optionally perform other related operations.
3256201af4c0STejun Heo  * 5. Call cgroup_finalize_control() to finish up.
3257201af4c0STejun Heo  *
3258201af4c0STejun Heo  * This function implements step 3 and propagates the mask changes
3259201af4c0STejun Heo  * throughout @cgrp's subtree, updates csses accordingly and perform
3260201af4c0STejun Heo  * process migrations.
3261201af4c0STejun Heo  */
cgroup_apply_control(struct cgroup * cgrp)3262201af4c0STejun Heo static int cgroup_apply_control(struct cgroup *cgrp)
3263201af4c0STejun Heo {
3264201af4c0STejun Heo 	int ret;
3265201af4c0STejun Heo 
3266201af4c0STejun Heo 	cgroup_propagate_control(cgrp);
3267201af4c0STejun Heo 
3268201af4c0STejun Heo 	ret = cgroup_apply_control_enable(cgrp);
3269201af4c0STejun Heo 	if (ret)
3270201af4c0STejun Heo 		return ret;
3271201af4c0STejun Heo 
3272201af4c0STejun Heo 	/*
3273fc5a828bSDennis Zhou 	 * At this point, cgroup_e_css_by_mask() results reflect the new csses
3274201af4c0STejun Heo 	 * making the following cgroup_update_dfl_csses() properly update
3275201af4c0STejun Heo 	 * css associations of all tasks in the subtree.
3276201af4c0STejun Heo 	 */
327761c41711SWilliam Dean 	return cgroup_update_dfl_csses(cgrp);
3278201af4c0STejun Heo }
3279201af4c0STejun Heo 
3280201af4c0STejun Heo /**
3281201af4c0STejun Heo  * cgroup_finalize_control - finalize control mask update
3282201af4c0STejun Heo  * @cgrp: root of the target subtree
3283201af4c0STejun Heo  * @ret: the result of the update
3284201af4c0STejun Heo  *
3285201af4c0STejun Heo  * Finalize control mask update.  See cgroup_apply_control() for more info.
3286201af4c0STejun Heo  */
cgroup_finalize_control(struct cgroup * cgrp,int ret)3287201af4c0STejun Heo static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3288201af4c0STejun Heo {
3289201af4c0STejun Heo 	if (ret) {
3290201af4c0STejun Heo 		cgroup_restore_control(cgrp);
3291201af4c0STejun Heo 		cgroup_propagate_control(cgrp);
3292201af4c0STejun Heo 	}
3293201af4c0STejun Heo 
3294201af4c0STejun Heo 	cgroup_apply_control_disable(cgrp);
3295201af4c0STejun Heo }
3296201af4c0STejun Heo 
cgroup_vet_subtree_control_enable(struct cgroup * cgrp,u16 enable)32978cfd8147STejun Heo static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
32988cfd8147STejun Heo {
32998cfd8147STejun Heo 	u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
33008cfd8147STejun Heo 
33018cfd8147STejun Heo 	/* if nothing is getting enabled, nothing to worry about */
33028cfd8147STejun Heo 	if (!enable)
33038cfd8147STejun Heo 		return 0;
33048cfd8147STejun Heo 
33058cfd8147STejun Heo 	/* can @cgrp host any resources? */
33068cfd8147STejun Heo 	if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
33078cfd8147STejun Heo 		return -EOPNOTSUPP;
33088cfd8147STejun Heo 
33098cfd8147STejun Heo 	/* mixables don't care */
33108cfd8147STejun Heo 	if (cgroup_is_mixable(cgrp))
33118cfd8147STejun Heo 		return 0;
33128cfd8147STejun Heo 
33138cfd8147STejun Heo 	if (domain_enable) {
33148cfd8147STejun Heo 		/* can't enable domain controllers inside a thread subtree */
33158cfd8147STejun Heo 		if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
33168cfd8147STejun Heo 			return -EOPNOTSUPP;
33178cfd8147STejun Heo 	} else {
33188cfd8147STejun Heo 		/*
33198cfd8147STejun Heo 		 * Threaded controllers can handle internal competitions
33208cfd8147STejun Heo 		 * and are always allowed inside a (prospective) thread
33218cfd8147STejun Heo 		 * subtree.
33228cfd8147STejun Heo 		 */
33238cfd8147STejun Heo 		if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
33248cfd8147STejun Heo 			return 0;
33258cfd8147STejun Heo 	}
33268cfd8147STejun Heo 
33278cfd8147STejun Heo 	/*
33288cfd8147STejun Heo 	 * Controllers can't be enabled for a cgroup with tasks to avoid
33298cfd8147STejun Heo 	 * child cgroups competing against tasks.
33308cfd8147STejun Heo 	 */
33318cfd8147STejun Heo 	if (cgroup_has_tasks(cgrp))
33328cfd8147STejun Heo 		return -EBUSY;
33338cfd8147STejun Heo 
33348cfd8147STejun Heo 	return 0;
33358cfd8147STejun Heo }
33368cfd8147STejun Heo 
3337201af4c0STejun Heo /* change the enabled child controllers for a cgroup in the default hierarchy */
cgroup_subtree_control_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)3338201af4c0STejun Heo static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3339201af4c0STejun Heo 					    char *buf, size_t nbytes,
3340201af4c0STejun Heo 					    loff_t off)
3341201af4c0STejun Heo {
3342201af4c0STejun Heo 	u16 enable = 0, disable = 0;
3343201af4c0STejun Heo 	struct cgroup *cgrp, *child;
3344201af4c0STejun Heo 	struct cgroup_subsys *ss;
3345201af4c0STejun Heo 	char *tok;
3346201af4c0STejun Heo 	int ssid, ret;
3347201af4c0STejun Heo 
3348201af4c0STejun Heo 	/*
3349201af4c0STejun Heo 	 * Parse input - space separated list of subsystem names prefixed
3350201af4c0STejun Heo 	 * with either + or -.
3351201af4c0STejun Heo 	 */
3352201af4c0STejun Heo 	buf = strstrip(buf);
3353201af4c0STejun Heo 	while ((tok = strsep(&buf, " "))) {
3354201af4c0STejun Heo 		if (tok[0] == '\0')
3355201af4c0STejun Heo 			continue;
3356201af4c0STejun Heo 		do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
3357201af4c0STejun Heo 			if (!cgroup_ssid_enabled(ssid) ||
3358201af4c0STejun Heo 			    strcmp(tok + 1, ss->name))
3359201af4c0STejun Heo 				continue;
3360201af4c0STejun Heo 
3361201af4c0STejun Heo 			if (*tok == '+') {
3362201af4c0STejun Heo 				enable |= 1 << ssid;
3363201af4c0STejun Heo 				disable &= ~(1 << ssid);
3364201af4c0STejun Heo 			} else if (*tok == '-') {
3365201af4c0STejun Heo 				disable |= 1 << ssid;
3366201af4c0STejun Heo 				enable &= ~(1 << ssid);
3367201af4c0STejun Heo 			} else {
3368201af4c0STejun Heo 				return -EINVAL;
3369201af4c0STejun Heo 			}
3370201af4c0STejun Heo 			break;
3371201af4c0STejun Heo 		} while_each_subsys_mask();
3372201af4c0STejun Heo 		if (ssid == CGROUP_SUBSYS_COUNT)
3373201af4c0STejun Heo 			return -EINVAL;
3374201af4c0STejun Heo 	}
3375201af4c0STejun Heo 
3376201af4c0STejun Heo 	cgrp = cgroup_kn_lock_live(of->kn, true);
3377201af4c0STejun Heo 	if (!cgrp)
3378201af4c0STejun Heo 		return -ENODEV;
3379201af4c0STejun Heo 
3380201af4c0STejun Heo 	for_each_subsys(ss, ssid) {
3381201af4c0STejun Heo 		if (enable & (1 << ssid)) {
3382201af4c0STejun Heo 			if (cgrp->subtree_control & (1 << ssid)) {
3383201af4c0STejun Heo 				enable &= ~(1 << ssid);
3384201af4c0STejun Heo 				continue;
3385201af4c0STejun Heo 			}
3386201af4c0STejun Heo 
3387201af4c0STejun Heo 			if (!(cgroup_control(cgrp) & (1 << ssid))) {
3388201af4c0STejun Heo 				ret = -ENOENT;
3389201af4c0STejun Heo 				goto out_unlock;
3390201af4c0STejun Heo 			}
3391201af4c0STejun Heo 		} else if (disable & (1 << ssid)) {
3392201af4c0STejun Heo 			if (!(cgrp->subtree_control & (1 << ssid))) {
3393201af4c0STejun Heo 				disable &= ~(1 << ssid);
3394201af4c0STejun Heo 				continue;
3395201af4c0STejun Heo 			}
3396201af4c0STejun Heo 
3397201af4c0STejun Heo 			/* a child has it enabled? */
3398201af4c0STejun Heo 			cgroup_for_each_live_child(child, cgrp) {
3399201af4c0STejun Heo 				if (child->subtree_control & (1 << ssid)) {
3400201af4c0STejun Heo 					ret = -EBUSY;
3401201af4c0STejun Heo 					goto out_unlock;
3402201af4c0STejun Heo 				}
3403201af4c0STejun Heo 			}
3404201af4c0STejun Heo 		}
3405201af4c0STejun Heo 	}
3406201af4c0STejun Heo 
3407201af4c0STejun Heo 	if (!enable && !disable) {
3408201af4c0STejun Heo 		ret = 0;
3409201af4c0STejun Heo 		goto out_unlock;
3410201af4c0STejun Heo 	}
3411201af4c0STejun Heo 
34128cfd8147STejun Heo 	ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3413201af4c0STejun Heo 	if (ret)
3414201af4c0STejun Heo 		goto out_unlock;
3415201af4c0STejun Heo 
3416201af4c0STejun Heo 	/* save and update control masks and prepare csses */
3417201af4c0STejun Heo 	cgroup_save_control(cgrp);
3418201af4c0STejun Heo 
3419201af4c0STejun Heo 	cgrp->subtree_control |= enable;
3420201af4c0STejun Heo 	cgrp->subtree_control &= ~disable;
3421201af4c0STejun Heo 
3422201af4c0STejun Heo 	ret = cgroup_apply_control(cgrp);
3423201af4c0STejun Heo 	cgroup_finalize_control(cgrp, ret);
34243c745417STejun Heo 	if (ret)
34253c745417STejun Heo 		goto out_unlock;
3426201af4c0STejun Heo 
3427201af4c0STejun Heo 	kernfs_activate(cgrp->kn);
3428201af4c0STejun Heo out_unlock:
3429201af4c0STejun Heo 	cgroup_kn_unlock(of->kn);
3430201af4c0STejun Heo 	return ret ?: nbytes;
3431201af4c0STejun Heo }
3432201af4c0STejun Heo 
3433c705a00dSTejun Heo /**
3434c705a00dSTejun Heo  * cgroup_enable_threaded - make @cgrp threaded
3435c705a00dSTejun Heo  * @cgrp: the target cgroup
3436c705a00dSTejun Heo  *
3437c705a00dSTejun Heo  * Called when "threaded" is written to the cgroup.type interface file and
3438c705a00dSTejun Heo  * tries to make @cgrp threaded and join the parent's resource domain.
3439c705a00dSTejun Heo  * This function is never called on the root cgroup as cgroup.type doesn't
3440c705a00dSTejun Heo  * exist on it.
3441c705a00dSTejun Heo  */
cgroup_enable_threaded(struct cgroup * cgrp)34428cfd8147STejun Heo static int cgroup_enable_threaded(struct cgroup *cgrp)
34438cfd8147STejun Heo {
34448cfd8147STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
34458cfd8147STejun Heo 	struct cgroup *dom_cgrp = parent->dom_cgrp;
3446479adb89STejun Heo 	struct cgroup *dsct;
3447479adb89STejun Heo 	struct cgroup_subsys_state *d_css;
34488cfd8147STejun Heo 	int ret;
34498cfd8147STejun Heo 
34508cfd8147STejun Heo 	lockdep_assert_held(&cgroup_mutex);
34518cfd8147STejun Heo 
34528cfd8147STejun Heo 	/* noop if already threaded */
34538cfd8147STejun Heo 	if (cgroup_is_threaded(cgrp))
34548cfd8147STejun Heo 		return 0;
34558cfd8147STejun Heo 
3456d1897c95STejun Heo 	/*
3457d1897c95STejun Heo 	 * If @cgroup is populated or has domain controllers enabled, it
3458d1897c95STejun Heo 	 * can't be switched.  While the below cgroup_can_be_thread_root()
3459d1897c95STejun Heo 	 * test can catch the same conditions, that's only when @parent is
3460d1897c95STejun Heo 	 * not mixable, so let's check it explicitly.
3461d1897c95STejun Heo 	 */
3462d1897c95STejun Heo 	if (cgroup_is_populated(cgrp) ||
3463d1897c95STejun Heo 	    cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3464d1897c95STejun Heo 		return -EOPNOTSUPP;
3465d1897c95STejun Heo 
34668cfd8147STejun Heo 	/* we're joining the parent's domain, ensure its validity */
34678cfd8147STejun Heo 	if (!cgroup_is_valid_domain(dom_cgrp) ||
34688cfd8147STejun Heo 	    !cgroup_can_be_thread_root(dom_cgrp))
34698cfd8147STejun Heo 		return -EOPNOTSUPP;
34708cfd8147STejun Heo 
34718cfd8147STejun Heo 	/*
34728cfd8147STejun Heo 	 * The following shouldn't cause actual migrations and should
34738cfd8147STejun Heo 	 * always succeed.
34748cfd8147STejun Heo 	 */
34758cfd8147STejun Heo 	cgroup_save_control(cgrp);
34768cfd8147STejun Heo 
3477479adb89STejun Heo 	cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
3478479adb89STejun Heo 		if (dsct == cgrp || cgroup_is_threaded(dsct))
3479479adb89STejun Heo 			dsct->dom_cgrp = dom_cgrp;
3480479adb89STejun Heo 
34818cfd8147STejun Heo 	ret = cgroup_apply_control(cgrp);
34828cfd8147STejun Heo 	if (!ret)
34838cfd8147STejun Heo 		parent->nr_threaded_children++;
34848cfd8147STejun Heo 
34858cfd8147STejun Heo 	cgroup_finalize_control(cgrp, ret);
34868cfd8147STejun Heo 	return ret;
34878cfd8147STejun Heo }
34888cfd8147STejun Heo 
cgroup_type_show(struct seq_file * seq,void * v)34898cfd8147STejun Heo static int cgroup_type_show(struct seq_file *seq, void *v)
34908cfd8147STejun Heo {
34918cfd8147STejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
34928cfd8147STejun Heo 
34938cfd8147STejun Heo 	if (cgroup_is_threaded(cgrp))
34948cfd8147STejun Heo 		seq_puts(seq, "threaded\n");
34958cfd8147STejun Heo 	else if (!cgroup_is_valid_domain(cgrp))
34968cfd8147STejun Heo 		seq_puts(seq, "domain invalid\n");
34978cfd8147STejun Heo 	else if (cgroup_is_thread_root(cgrp))
34988cfd8147STejun Heo 		seq_puts(seq, "domain threaded\n");
34998cfd8147STejun Heo 	else
35008cfd8147STejun Heo 		seq_puts(seq, "domain\n");
35018cfd8147STejun Heo 
35028cfd8147STejun Heo 	return 0;
35038cfd8147STejun Heo }
35048cfd8147STejun Heo 
cgroup_type_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)35058cfd8147STejun Heo static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
35068cfd8147STejun Heo 				 size_t nbytes, loff_t off)
35078cfd8147STejun Heo {
35088cfd8147STejun Heo 	struct cgroup *cgrp;
35098cfd8147STejun Heo 	int ret;
35108cfd8147STejun Heo 
35118cfd8147STejun Heo 	/* only switching to threaded mode is supported */
35128cfd8147STejun Heo 	if (strcmp(strstrip(buf), "threaded"))
35138cfd8147STejun Heo 		return -EINVAL;
35148cfd8147STejun Heo 
35153bc0bb36SMichal Koutný 	/* drain dying csses before we re-apply (threaded) subtree control */
35163bc0bb36SMichal Koutný 	cgrp = cgroup_kn_lock_live(of->kn, true);
35178cfd8147STejun Heo 	if (!cgrp)
35188cfd8147STejun Heo 		return -ENOENT;
35198cfd8147STejun Heo 
35208cfd8147STejun Heo 	/* threaded can only be enabled */
35218cfd8147STejun Heo 	ret = cgroup_enable_threaded(cgrp);
35228cfd8147STejun Heo 
35238cfd8147STejun Heo 	cgroup_kn_unlock(of->kn);
35248cfd8147STejun Heo 	return ret ?: nbytes;
35258cfd8147STejun Heo }
35268cfd8147STejun Heo 
cgroup_max_descendants_show(struct seq_file * seq,void * v)35271a926e0bSRoman Gushchin static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
35281a926e0bSRoman Gushchin {
35291a926e0bSRoman Gushchin 	struct cgroup *cgrp = seq_css(seq)->cgroup;
35301a926e0bSRoman Gushchin 	int descendants = READ_ONCE(cgrp->max_descendants);
35311a926e0bSRoman Gushchin 
35321a926e0bSRoman Gushchin 	if (descendants == INT_MAX)
35331a926e0bSRoman Gushchin 		seq_puts(seq, "max\n");
35341a926e0bSRoman Gushchin 	else
35351a926e0bSRoman Gushchin 		seq_printf(seq, "%d\n", descendants);
35361a926e0bSRoman Gushchin 
35371a926e0bSRoman Gushchin 	return 0;
35381a926e0bSRoman Gushchin }
35391a926e0bSRoman Gushchin 
cgroup_max_descendants_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)35401a926e0bSRoman Gushchin static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
35411a926e0bSRoman Gushchin 					   char *buf, size_t nbytes, loff_t off)
35421a926e0bSRoman Gushchin {
35431a926e0bSRoman Gushchin 	struct cgroup *cgrp;
35441a926e0bSRoman Gushchin 	int descendants;
35451a926e0bSRoman Gushchin 	ssize_t ret;
35461a926e0bSRoman Gushchin 
35471a926e0bSRoman Gushchin 	buf = strstrip(buf);
35481a926e0bSRoman Gushchin 	if (!strcmp(buf, "max")) {
35491a926e0bSRoman Gushchin 		descendants = INT_MAX;
35501a926e0bSRoman Gushchin 	} else {
35511a926e0bSRoman Gushchin 		ret = kstrtoint(buf, 0, &descendants);
35521a926e0bSRoman Gushchin 		if (ret)
35531a926e0bSRoman Gushchin 			return ret;
35541a926e0bSRoman Gushchin 	}
35551a926e0bSRoman Gushchin 
3556696b98f2SDan Carpenter 	if (descendants < 0)
35571a926e0bSRoman Gushchin 		return -ERANGE;
35581a926e0bSRoman Gushchin 
35591a926e0bSRoman Gushchin 	cgrp = cgroup_kn_lock_live(of->kn, false);
35601a926e0bSRoman Gushchin 	if (!cgrp)
35611a926e0bSRoman Gushchin 		return -ENOENT;
35621a926e0bSRoman Gushchin 
35631a926e0bSRoman Gushchin 	cgrp->max_descendants = descendants;
35641a926e0bSRoman Gushchin 
35651a926e0bSRoman Gushchin 	cgroup_kn_unlock(of->kn);
35661a926e0bSRoman Gushchin 
35671a926e0bSRoman Gushchin 	return nbytes;
35681a926e0bSRoman Gushchin }
35691a926e0bSRoman Gushchin 
cgroup_max_depth_show(struct seq_file * seq,void * v)35701a926e0bSRoman Gushchin static int cgroup_max_depth_show(struct seq_file *seq, void *v)
35711a926e0bSRoman Gushchin {
35721a926e0bSRoman Gushchin 	struct cgroup *cgrp = seq_css(seq)->cgroup;
35731a926e0bSRoman Gushchin 	int depth = READ_ONCE(cgrp->max_depth);
35741a926e0bSRoman Gushchin 
35751a926e0bSRoman Gushchin 	if (depth == INT_MAX)
35761a926e0bSRoman Gushchin 		seq_puts(seq, "max\n");
35771a926e0bSRoman Gushchin 	else
35781a926e0bSRoman Gushchin 		seq_printf(seq, "%d\n", depth);
35791a926e0bSRoman Gushchin 
35801a926e0bSRoman Gushchin 	return 0;
35811a926e0bSRoman Gushchin }
35821a926e0bSRoman Gushchin 
cgroup_max_depth_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)35831a926e0bSRoman Gushchin static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
35841a926e0bSRoman Gushchin 				      char *buf, size_t nbytes, loff_t off)
35851a926e0bSRoman Gushchin {
35861a926e0bSRoman Gushchin 	struct cgroup *cgrp;
35871a926e0bSRoman Gushchin 	ssize_t ret;
35881a926e0bSRoman Gushchin 	int depth;
35891a926e0bSRoman Gushchin 
35901a926e0bSRoman Gushchin 	buf = strstrip(buf);
35911a926e0bSRoman Gushchin 	if (!strcmp(buf, "max")) {
35921a926e0bSRoman Gushchin 		depth = INT_MAX;
35931a926e0bSRoman Gushchin 	} else {
35941a926e0bSRoman Gushchin 		ret = kstrtoint(buf, 0, &depth);
35951a926e0bSRoman Gushchin 		if (ret)
35961a926e0bSRoman Gushchin 			return ret;
35971a926e0bSRoman Gushchin 	}
35981a926e0bSRoman Gushchin 
3599696b98f2SDan Carpenter 	if (depth < 0)
36001a926e0bSRoman Gushchin 		return -ERANGE;
36011a926e0bSRoman Gushchin 
36021a926e0bSRoman Gushchin 	cgrp = cgroup_kn_lock_live(of->kn, false);
36031a926e0bSRoman Gushchin 	if (!cgrp)
36041a926e0bSRoman Gushchin 		return -ENOENT;
36051a926e0bSRoman Gushchin 
36061a926e0bSRoman Gushchin 	cgrp->max_depth = depth;
36071a926e0bSRoman Gushchin 
36081a926e0bSRoman Gushchin 	cgroup_kn_unlock(of->kn);
36091a926e0bSRoman Gushchin 
36101a926e0bSRoman Gushchin 	return nbytes;
36111a926e0bSRoman Gushchin }
36121a926e0bSRoman Gushchin 
cgroup_events_show(struct seq_file * seq,void * v)3613201af4c0STejun Heo static int cgroup_events_show(struct seq_file *seq, void *v)
3614201af4c0STejun Heo {
361576f969e8SRoman Gushchin 	struct cgroup *cgrp = seq_css(seq)->cgroup;
361676f969e8SRoman Gushchin 
361776f969e8SRoman Gushchin 	seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp));
361876f969e8SRoman Gushchin 	seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags));
361976f969e8SRoman Gushchin 
3620201af4c0STejun Heo 	return 0;
3621201af4c0STejun Heo }
3622201af4c0STejun Heo 
cgroup_stat_show(struct seq_file * seq,void * v)36233e48930cSTejun Heo static int cgroup_stat_show(struct seq_file *seq, void *v)
3624ec39225cSRoman Gushchin {
3625ec39225cSRoman Gushchin 	struct cgroup *cgroup = seq_css(seq)->cgroup;
3626ec39225cSRoman Gushchin 
3627ec39225cSRoman Gushchin 	seq_printf(seq, "nr_descendants %d\n",
3628ec39225cSRoman Gushchin 		   cgroup->nr_descendants);
3629ec39225cSRoman Gushchin 	seq_printf(seq, "nr_dying_descendants %d\n",
3630ec39225cSRoman Gushchin 		   cgroup->nr_dying_descendants);
3631ec39225cSRoman Gushchin 
3632ec39225cSRoman Gushchin 	return 0;
3633ec39225cSRoman Gushchin }
3634ec39225cSRoman Gushchin 
36351299eb2bSMiaohe Lin #ifdef CONFIG_CGROUP_SCHED
3636d1d4ff5dSMiaohe Lin /**
3637d1d4ff5dSMiaohe Lin  * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
3638d1d4ff5dSMiaohe Lin  * @cgrp: the cgroup of interest
3639d1d4ff5dSMiaohe Lin  * @ss: the subsystem of interest
3640d1d4ff5dSMiaohe Lin  *
3641d1d4ff5dSMiaohe Lin  * Find and get @cgrp's css associated with @ss.  If the css doesn't exist
3642d1d4ff5dSMiaohe Lin  * or is offline, %NULL is returned.
3643d1d4ff5dSMiaohe Lin  */
cgroup_tryget_css(struct cgroup * cgrp,struct cgroup_subsys * ss)3644d1d4ff5dSMiaohe Lin static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
3645d1d4ff5dSMiaohe Lin 						     struct cgroup_subsys *ss)
3646d41bf8c9STejun Heo {
3647d1d4ff5dSMiaohe Lin 	struct cgroup_subsys_state *css;
3648d1d4ff5dSMiaohe Lin 
3649d1d4ff5dSMiaohe Lin 	rcu_read_lock();
3650d1d4ff5dSMiaohe Lin 	css = cgroup_css(cgrp, ss);
3651d1d4ff5dSMiaohe Lin 	if (css && !css_tryget_online(css))
3652d1d4ff5dSMiaohe Lin 		css = NULL;
3653d1d4ff5dSMiaohe Lin 	rcu_read_unlock();
3654d1d4ff5dSMiaohe Lin 
3655d1d4ff5dSMiaohe Lin 	return css;
3656d1d4ff5dSMiaohe Lin }
3657d1d4ff5dSMiaohe Lin 
cgroup_extra_stat_show(struct seq_file * seq,int ssid)36581299eb2bSMiaohe Lin static int cgroup_extra_stat_show(struct seq_file *seq, int ssid)
3659d41bf8c9STejun Heo {
36601299eb2bSMiaohe Lin 	struct cgroup *cgrp = seq_css(seq)->cgroup;
3661d41bf8c9STejun Heo 	struct cgroup_subsys *ss = cgroup_subsys[ssid];
3662d41bf8c9STejun Heo 	struct cgroup_subsys_state *css;
3663d41bf8c9STejun Heo 	int ret;
3664d41bf8c9STejun Heo 
3665d41bf8c9STejun Heo 	if (!ss->css_extra_stat_show)
3666d41bf8c9STejun Heo 		return 0;
3667d41bf8c9STejun Heo 
3668d41bf8c9STejun Heo 	css = cgroup_tryget_css(cgrp, ss);
3669d41bf8c9STejun Heo 	if (!css)
3670d41bf8c9STejun Heo 		return 0;
3671d41bf8c9STejun Heo 
3672d41bf8c9STejun Heo 	ret = ss->css_extra_stat_show(seq, css);
3673d41bf8c9STejun Heo 	css_put(css);
3674d41bf8c9STejun Heo 	return ret;
3675d41bf8c9STejun Heo }
3676d41bf8c9STejun Heo 
cgroup_local_stat_show(struct seq_file * seq,struct cgroup * cgrp,int ssid)3677*76be05d4SLinus Torvalds static int cgroup_local_stat_show(struct seq_file *seq,
3678677ea015SJosh Don 				  struct cgroup *cgrp, int ssid)
3679677ea015SJosh Don {
3680677ea015SJosh Don 	struct cgroup_subsys *ss = cgroup_subsys[ssid];
3681677ea015SJosh Don 	struct cgroup_subsys_state *css;
3682677ea015SJosh Don 	int ret;
3683677ea015SJosh Don 
3684677ea015SJosh Don 	if (!ss->css_local_stat_show)
3685677ea015SJosh Don 		return 0;
3686677ea015SJosh Don 
3687677ea015SJosh Don 	css = cgroup_tryget_css(cgrp, ss);
3688677ea015SJosh Don 	if (!css)
3689677ea015SJosh Don 		return 0;
3690677ea015SJosh Don 
3691677ea015SJosh Don 	ret = ss->css_local_stat_show(seq, css);
3692677ea015SJosh Don 	css_put(css);
3693677ea015SJosh Don 	return ret;
3694677ea015SJosh Don }
3695*76be05d4SLinus Torvalds #endif
3696*76be05d4SLinus Torvalds 
cpu_stat_show(struct seq_file * seq,void * v)3697*76be05d4SLinus Torvalds static int cpu_stat_show(struct seq_file *seq, void *v)
3698*76be05d4SLinus Torvalds {
3699*76be05d4SLinus Torvalds 	int ret = 0;
3700*76be05d4SLinus Torvalds 
3701*76be05d4SLinus Torvalds 	cgroup_base_stat_cputime_show(seq);
3702*76be05d4SLinus Torvalds #ifdef CONFIG_CGROUP_SCHED
3703*76be05d4SLinus Torvalds 	ret = cgroup_extra_stat_show(seq, cpu_cgrp_id);
3704*76be05d4SLinus Torvalds #endif
3705*76be05d4SLinus Torvalds 	return ret;
3706*76be05d4SLinus Torvalds }
3707677ea015SJosh Don 
cpu_local_stat_show(struct seq_file * seq,void * v)3708677ea015SJosh Don static int cpu_local_stat_show(struct seq_file *seq, void *v)
3709677ea015SJosh Don {
3710677ea015SJosh Don 	struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
3711677ea015SJosh Don 	int ret = 0;
3712677ea015SJosh Don 
3713677ea015SJosh Don #ifdef CONFIG_CGROUP_SCHED
3714677ea015SJosh Don 	ret = cgroup_local_stat_show(seq, cgrp, cpu_cgrp_id);
3715677ea015SJosh Don #endif
3716677ea015SJosh Don 	return ret;
3717677ea015SJosh Don }
3718677ea015SJosh Don 
37192ce7135aSJohannes Weiner #ifdef CONFIG_PSI
cgroup_io_pressure_show(struct seq_file * seq,void * v)37202ce7135aSJohannes Weiner static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
37212ce7135aSJohannes Weiner {
372274321038STejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
372357899a66SChengming Zhou 	struct psi_group *psi = cgroup_psi(cgrp);
3724df5ba5beSDan Schatzberg 
3725df5ba5beSDan Schatzberg 	return psi_show(seq, psi, PSI_IO);
37262ce7135aSJohannes Weiner }
cgroup_memory_pressure_show(struct seq_file * seq,void * v)37272ce7135aSJohannes Weiner static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
37282ce7135aSJohannes Weiner {
372974321038STejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
373057899a66SChengming Zhou 	struct psi_group *psi = cgroup_psi(cgrp);
3731df5ba5beSDan Schatzberg 
3732df5ba5beSDan Schatzberg 	return psi_show(seq, psi, PSI_MEM);
37332ce7135aSJohannes Weiner }
cgroup_cpu_pressure_show(struct seq_file * seq,void * v)37342ce7135aSJohannes Weiner static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
37352ce7135aSJohannes Weiner {
373674321038STejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
373757899a66SChengming Zhou 	struct psi_group *psi = cgroup_psi(cgrp);
3738df5ba5beSDan Schatzberg 
3739df5ba5beSDan Schatzberg 	return psi_show(seq, psi, PSI_CPU);
37402ce7135aSJohannes Weiner }
37410e94682bSSuren Baghdasaryan 
pressure_write(struct kernfs_open_file * of,char * buf,size_t nbytes,enum psi_res res)374234f26a15SChengming Zhou static ssize_t pressure_write(struct kernfs_open_file *of, char *buf,
37430e94682bSSuren Baghdasaryan 			      size_t nbytes, enum psi_res res)
37440e94682bSSuren Baghdasaryan {
37450d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
37460e94682bSSuren Baghdasaryan 	struct psi_trigger *new;
37470e94682bSSuren Baghdasaryan 	struct cgroup *cgrp;
3748385aac15SOdin Ugedal 	struct psi_group *psi;
37490e94682bSSuren Baghdasaryan 
37500e94682bSSuren Baghdasaryan 	cgrp = cgroup_kn_lock_live(of->kn, false);
37510e94682bSSuren Baghdasaryan 	if (!cgrp)
37520e94682bSSuren Baghdasaryan 		return -ENODEV;
37530e94682bSSuren Baghdasaryan 
37540e94682bSSuren Baghdasaryan 	cgroup_get(cgrp);
37550e94682bSSuren Baghdasaryan 	cgroup_kn_unlock(of->kn);
37560e94682bSSuren Baghdasaryan 
3757a06247c6SSuren Baghdasaryan 	/* Allow only one trigger per file descriptor */
3758a06247c6SSuren Baghdasaryan 	if (ctx->psi.trigger) {
3759a06247c6SSuren Baghdasaryan 		cgroup_put(cgrp);
3760a06247c6SSuren Baghdasaryan 		return -EBUSY;
3761a06247c6SSuren Baghdasaryan 	}
3762a06247c6SSuren Baghdasaryan 
376357899a66SChengming Zhou 	psi = cgroup_psi(cgrp);
3764aff03707SSuren Baghdasaryan 	new = psi_trigger_create(psi, buf, res, of->file, of);
37650e94682bSSuren Baghdasaryan 	if (IS_ERR(new)) {
37660e94682bSSuren Baghdasaryan 		cgroup_put(cgrp);
37670e94682bSSuren Baghdasaryan 		return PTR_ERR(new);
37680e94682bSSuren Baghdasaryan 	}
37690e94682bSSuren Baghdasaryan 
3770a06247c6SSuren Baghdasaryan 	smp_store_release(&ctx->psi.trigger, new);
37710e94682bSSuren Baghdasaryan 	cgroup_put(cgrp);
37720e94682bSSuren Baghdasaryan 
37730e94682bSSuren Baghdasaryan 	return nbytes;
37740e94682bSSuren Baghdasaryan }
37750e94682bSSuren Baghdasaryan 
cgroup_io_pressure_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)37760e94682bSSuren Baghdasaryan static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of,
37770e94682bSSuren Baghdasaryan 					  char *buf, size_t nbytes,
37780e94682bSSuren Baghdasaryan 					  loff_t off)
37790e94682bSSuren Baghdasaryan {
378034f26a15SChengming Zhou 	return pressure_write(of, buf, nbytes, PSI_IO);
37810e94682bSSuren Baghdasaryan }
37820e94682bSSuren Baghdasaryan 
cgroup_memory_pressure_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)37830e94682bSSuren Baghdasaryan static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of,
37840e94682bSSuren Baghdasaryan 					  char *buf, size_t nbytes,
37850e94682bSSuren Baghdasaryan 					  loff_t off)
37860e94682bSSuren Baghdasaryan {
378734f26a15SChengming Zhou 	return pressure_write(of, buf, nbytes, PSI_MEM);
37880e94682bSSuren Baghdasaryan }
37890e94682bSSuren Baghdasaryan 
cgroup_cpu_pressure_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)37900e94682bSSuren Baghdasaryan static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of,
37910e94682bSSuren Baghdasaryan 					  char *buf, size_t nbytes,
37920e94682bSSuren Baghdasaryan 					  loff_t off)
37930e94682bSSuren Baghdasaryan {
379434f26a15SChengming Zhou 	return pressure_write(of, buf, nbytes, PSI_CPU);
37950e94682bSSuren Baghdasaryan }
37960e94682bSSuren Baghdasaryan 
379752b1364bSChengming Zhou #ifdef CONFIG_IRQ_TIME_ACCOUNTING
cgroup_irq_pressure_show(struct seq_file * seq,void * v)379852b1364bSChengming Zhou static int cgroup_irq_pressure_show(struct seq_file *seq, void *v)
379952b1364bSChengming Zhou {
380052b1364bSChengming Zhou 	struct cgroup *cgrp = seq_css(seq)->cgroup;
380157899a66SChengming Zhou 	struct psi_group *psi = cgroup_psi(cgrp);
380252b1364bSChengming Zhou 
380352b1364bSChengming Zhou 	return psi_show(seq, psi, PSI_IRQ);
380452b1364bSChengming Zhou }
380552b1364bSChengming Zhou 
cgroup_irq_pressure_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)380652b1364bSChengming Zhou static ssize_t cgroup_irq_pressure_write(struct kernfs_open_file *of,
380752b1364bSChengming Zhou 					 char *buf, size_t nbytes,
380852b1364bSChengming Zhou 					 loff_t off)
380952b1364bSChengming Zhou {
381034f26a15SChengming Zhou 	return pressure_write(of, buf, nbytes, PSI_IRQ);
381152b1364bSChengming Zhou }
381252b1364bSChengming Zhou #endif
381352b1364bSChengming Zhou 
cgroup_pressure_show(struct seq_file * seq,void * v)381434f26a15SChengming Zhou static int cgroup_pressure_show(struct seq_file *seq, void *v)
381534f26a15SChengming Zhou {
381634f26a15SChengming Zhou 	struct cgroup *cgrp = seq_css(seq)->cgroup;
381734f26a15SChengming Zhou 	struct psi_group *psi = cgroup_psi(cgrp);
381834f26a15SChengming Zhou 
381934f26a15SChengming Zhou 	seq_printf(seq, "%d\n", psi->enabled);
382034f26a15SChengming Zhou 
382134f26a15SChengming Zhou 	return 0;
382234f26a15SChengming Zhou }
382334f26a15SChengming Zhou 
cgroup_pressure_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)382434f26a15SChengming Zhou static ssize_t cgroup_pressure_write(struct kernfs_open_file *of,
382534f26a15SChengming Zhou 				     char *buf, size_t nbytes,
382634f26a15SChengming Zhou 				     loff_t off)
382734f26a15SChengming Zhou {
382834f26a15SChengming Zhou 	ssize_t ret;
382934f26a15SChengming Zhou 	int enable;
383034f26a15SChengming Zhou 	struct cgroup *cgrp;
383134f26a15SChengming Zhou 	struct psi_group *psi;
383234f26a15SChengming Zhou 
383334f26a15SChengming Zhou 	ret = kstrtoint(strstrip(buf), 0, &enable);
383434f26a15SChengming Zhou 	if (ret)
383534f26a15SChengming Zhou 		return ret;
383634f26a15SChengming Zhou 
383734f26a15SChengming Zhou 	if (enable < 0 || enable > 1)
383834f26a15SChengming Zhou 		return -ERANGE;
383934f26a15SChengming Zhou 
384034f26a15SChengming Zhou 	cgrp = cgroup_kn_lock_live(of->kn, false);
384134f26a15SChengming Zhou 	if (!cgrp)
384234f26a15SChengming Zhou 		return -ENOENT;
384334f26a15SChengming Zhou 
384434f26a15SChengming Zhou 	psi = cgroup_psi(cgrp);
384534f26a15SChengming Zhou 	if (psi->enabled != enable) {
384634f26a15SChengming Zhou 		int i;
384734f26a15SChengming Zhou 
384834f26a15SChengming Zhou 		/* show or hide {cpu,memory,io,irq}.pressure files */
384934f26a15SChengming Zhou 		for (i = 0; i < NR_PSI_RESOURCES; i++)
385034f26a15SChengming Zhou 			cgroup_file_show(&cgrp->psi_files[i], enable);
385134f26a15SChengming Zhou 
385234f26a15SChengming Zhou 		psi->enabled = enable;
385334f26a15SChengming Zhou 		if (enable)
385434f26a15SChengming Zhou 			psi_cgroup_restart(psi);
385534f26a15SChengming Zhou 	}
385634f26a15SChengming Zhou 
385734f26a15SChengming Zhou 	cgroup_kn_unlock(of->kn);
385834f26a15SChengming Zhou 
385934f26a15SChengming Zhou 	return nbytes;
38600e94682bSSuren Baghdasaryan }
38610e94682bSSuren Baghdasaryan 
cgroup_pressure_poll(struct kernfs_open_file * of,poll_table * pt)38620e94682bSSuren Baghdasaryan static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of,
38630e94682bSSuren Baghdasaryan 					  poll_table *pt)
38640e94682bSSuren Baghdasaryan {
38650d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
38660d2b5955STejun Heo 
38670d2b5955STejun Heo 	return psi_trigger_poll(&ctx->psi.trigger, of->file, pt);
38680e94682bSSuren Baghdasaryan }
38690e94682bSSuren Baghdasaryan 
cgroup_pressure_release(struct kernfs_open_file * of)38700e94682bSSuren Baghdasaryan static void cgroup_pressure_release(struct kernfs_open_file *of)
38710e94682bSSuren Baghdasaryan {
38720d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
38730d2b5955STejun Heo 
3874a06247c6SSuren Baghdasaryan 	psi_trigger_destroy(ctx->psi.trigger);
38750e94682bSSuren Baghdasaryan }
38763958e2d0SSuren Baghdasaryan 
cgroup_psi_enabled(void)38773958e2d0SSuren Baghdasaryan bool cgroup_psi_enabled(void)
38783958e2d0SSuren Baghdasaryan {
387958d8c258SChengming Zhou 	if (static_branch_likely(&psi_disabled))
388058d8c258SChengming Zhou 		return false;
388158d8c258SChengming Zhou 
38823958e2d0SSuren Baghdasaryan 	return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0;
38833958e2d0SSuren Baghdasaryan }
38843958e2d0SSuren Baghdasaryan 
38853958e2d0SSuren Baghdasaryan #else /* CONFIG_PSI */
cgroup_psi_enabled(void)38863958e2d0SSuren Baghdasaryan bool cgroup_psi_enabled(void)
38873958e2d0SSuren Baghdasaryan {
38883958e2d0SSuren Baghdasaryan 	return false;
38893958e2d0SSuren Baghdasaryan }
38903958e2d0SSuren Baghdasaryan 
38910e94682bSSuren Baghdasaryan #endif /* CONFIG_PSI */
38922ce7135aSJohannes Weiner 
cgroup_freeze_show(struct seq_file * seq,void * v)389376f969e8SRoman Gushchin static int cgroup_freeze_show(struct seq_file *seq, void *v)
389476f969e8SRoman Gushchin {
389576f969e8SRoman Gushchin 	struct cgroup *cgrp = seq_css(seq)->cgroup;
389676f969e8SRoman Gushchin 
389776f969e8SRoman Gushchin 	seq_printf(seq, "%d\n", cgrp->freezer.freeze);
389876f969e8SRoman Gushchin 
389976f969e8SRoman Gushchin 	return 0;
390076f969e8SRoman Gushchin }
390176f969e8SRoman Gushchin 
cgroup_freeze_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)390276f969e8SRoman Gushchin static ssize_t cgroup_freeze_write(struct kernfs_open_file *of,
390376f969e8SRoman Gushchin 				   char *buf, size_t nbytes, loff_t off)
390476f969e8SRoman Gushchin {
390576f969e8SRoman Gushchin 	struct cgroup *cgrp;
390676f969e8SRoman Gushchin 	ssize_t ret;
390776f969e8SRoman Gushchin 	int freeze;
390876f969e8SRoman Gushchin 
390976f969e8SRoman Gushchin 	ret = kstrtoint(strstrip(buf), 0, &freeze);
391076f969e8SRoman Gushchin 	if (ret)
391176f969e8SRoman Gushchin 		return ret;
391276f969e8SRoman Gushchin 
391376f969e8SRoman Gushchin 	if (freeze < 0 || freeze > 1)
391476f969e8SRoman Gushchin 		return -ERANGE;
391576f969e8SRoman Gushchin 
391676f969e8SRoman Gushchin 	cgrp = cgroup_kn_lock_live(of->kn, false);
391776f969e8SRoman Gushchin 	if (!cgrp)
391876f969e8SRoman Gushchin 		return -ENOENT;
391976f969e8SRoman Gushchin 
392076f969e8SRoman Gushchin 	cgroup_freeze(cgrp, freeze);
392176f969e8SRoman Gushchin 
392276f969e8SRoman Gushchin 	cgroup_kn_unlock(of->kn);
392376f969e8SRoman Gushchin 
392476f969e8SRoman Gushchin 	return nbytes;
392576f969e8SRoman Gushchin }
392676f969e8SRoman Gushchin 
__cgroup_kill(struct cgroup * cgrp)3927661ee628SChristian Brauner static void __cgroup_kill(struct cgroup *cgrp)
3928661ee628SChristian Brauner {
3929661ee628SChristian Brauner 	struct css_task_iter it;
3930661ee628SChristian Brauner 	struct task_struct *task;
3931661ee628SChristian Brauner 
3932661ee628SChristian Brauner 	lockdep_assert_held(&cgroup_mutex);
3933661ee628SChristian Brauner 
3934661ee628SChristian Brauner 	spin_lock_irq(&css_set_lock);
3935661ee628SChristian Brauner 	set_bit(CGRP_KILL, &cgrp->flags);
3936661ee628SChristian Brauner 	spin_unlock_irq(&css_set_lock);
3937661ee628SChristian Brauner 
3938661ee628SChristian Brauner 	css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it);
3939661ee628SChristian Brauner 	while ((task = css_task_iter_next(&it))) {
3940661ee628SChristian Brauner 		/* Ignore kernel threads here. */
3941661ee628SChristian Brauner 		if (task->flags & PF_KTHREAD)
3942661ee628SChristian Brauner 			continue;
3943661ee628SChristian Brauner 
3944661ee628SChristian Brauner 		/* Skip tasks that are already dying. */
3945661ee628SChristian Brauner 		if (__fatal_signal_pending(task))
3946661ee628SChristian Brauner 			continue;
3947661ee628SChristian Brauner 
3948661ee628SChristian Brauner 		send_sig(SIGKILL, task, 0);
3949661ee628SChristian Brauner 	}
3950661ee628SChristian Brauner 	css_task_iter_end(&it);
3951661ee628SChristian Brauner 
3952661ee628SChristian Brauner 	spin_lock_irq(&css_set_lock);
3953661ee628SChristian Brauner 	clear_bit(CGRP_KILL, &cgrp->flags);
3954661ee628SChristian Brauner 	spin_unlock_irq(&css_set_lock);
3955661ee628SChristian Brauner }
3956661ee628SChristian Brauner 
cgroup_kill(struct cgroup * cgrp)3957661ee628SChristian Brauner static void cgroup_kill(struct cgroup *cgrp)
3958661ee628SChristian Brauner {
3959661ee628SChristian Brauner 	struct cgroup_subsys_state *css;
3960661ee628SChristian Brauner 	struct cgroup *dsct;
3961661ee628SChristian Brauner 
3962661ee628SChristian Brauner 	lockdep_assert_held(&cgroup_mutex);
3963661ee628SChristian Brauner 
3964661ee628SChristian Brauner 	cgroup_for_each_live_descendant_pre(dsct, css, cgrp)
3965661ee628SChristian Brauner 		__cgroup_kill(dsct);
3966661ee628SChristian Brauner }
3967661ee628SChristian Brauner 
cgroup_kill_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)3968661ee628SChristian Brauner static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf,
3969661ee628SChristian Brauner 				 size_t nbytes, loff_t off)
3970661ee628SChristian Brauner {
3971661ee628SChristian Brauner 	ssize_t ret = 0;
3972661ee628SChristian Brauner 	int kill;
3973661ee628SChristian Brauner 	struct cgroup *cgrp;
3974661ee628SChristian Brauner 
3975661ee628SChristian Brauner 	ret = kstrtoint(strstrip(buf), 0, &kill);
3976661ee628SChristian Brauner 	if (ret)
3977661ee628SChristian Brauner 		return ret;
3978661ee628SChristian Brauner 
3979661ee628SChristian Brauner 	if (kill != 1)
3980661ee628SChristian Brauner 		return -ERANGE;
3981661ee628SChristian Brauner 
3982661ee628SChristian Brauner 	cgrp = cgroup_kn_lock_live(of->kn, false);
3983661ee628SChristian Brauner 	if (!cgrp)
3984661ee628SChristian Brauner 		return -ENOENT;
3985661ee628SChristian Brauner 
3986661ee628SChristian Brauner 	/*
3987661ee628SChristian Brauner 	 * Killing is a process directed operation, i.e. the whole thread-group
3988661ee628SChristian Brauner 	 * is taken down so act like we do for cgroup.procs and only make this
3989661ee628SChristian Brauner 	 * writable in non-threaded cgroups.
3990661ee628SChristian Brauner 	 */
3991661ee628SChristian Brauner 	if (cgroup_is_threaded(cgrp))
3992661ee628SChristian Brauner 		ret = -EOPNOTSUPP;
3993661ee628SChristian Brauner 	else
3994661ee628SChristian Brauner 		cgroup_kill(cgrp);
3995661ee628SChristian Brauner 
3996661ee628SChristian Brauner 	cgroup_kn_unlock(of->kn);
3997661ee628SChristian Brauner 
3998661ee628SChristian Brauner 	return ret ?: nbytes;
3999661ee628SChristian Brauner }
4000661ee628SChristian Brauner 
cgroup_file_open(struct kernfs_open_file * of)4001201af4c0STejun Heo static int cgroup_file_open(struct kernfs_open_file *of)
4002201af4c0STejun Heo {
40035a7b5f32SHui Su 	struct cftype *cft = of_cft(of);
40040d2b5955STejun Heo 	struct cgroup_file_ctx *ctx;
40050d2b5955STejun Heo 	int ret;
4006201af4c0STejun Heo 
40070d2b5955STejun Heo 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
40080d2b5955STejun Heo 	if (!ctx)
40090d2b5955STejun Heo 		return -ENOMEM;
4010e5745764STejun Heo 
4011e5745764STejun Heo 	ctx->ns = current->nsproxy->cgroup_ns;
4012e5745764STejun Heo 	get_cgroup_ns(ctx->ns);
40130d2b5955STejun Heo 	of->priv = ctx;
40140d2b5955STejun Heo 
40150d2b5955STejun Heo 	if (!cft->open)
4016201af4c0STejun Heo 		return 0;
40170d2b5955STejun Heo 
40180d2b5955STejun Heo 	ret = cft->open(of);
4019e5745764STejun Heo 	if (ret) {
4020e5745764STejun Heo 		put_cgroup_ns(ctx->ns);
40210d2b5955STejun Heo 		kfree(ctx);
4022e5745764STejun Heo 	}
40230d2b5955STejun Heo 	return ret;
4024201af4c0STejun Heo }
4025201af4c0STejun Heo 
cgroup_file_release(struct kernfs_open_file * of)4026201af4c0STejun Heo static void cgroup_file_release(struct kernfs_open_file *of)
4027201af4c0STejun Heo {
40285a7b5f32SHui Su 	struct cftype *cft = of_cft(of);
40290d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
4030201af4c0STejun Heo 
4031201af4c0STejun Heo 	if (cft->release)
4032201af4c0STejun Heo 		cft->release(of);
4033e5745764STejun Heo 	put_cgroup_ns(ctx->ns);
40340d2b5955STejun Heo 	kfree(ctx);
4035201af4c0STejun Heo }
4036201af4c0STejun Heo 
cgroup_file_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)4037201af4c0STejun Heo static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
4038201af4c0STejun Heo 				 size_t nbytes, loff_t off)
4039201af4c0STejun Heo {
4040e5745764STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
4041201af4c0STejun Heo 	struct cgroup *cgrp = of->kn->parent->priv;
40425a7b5f32SHui Su 	struct cftype *cft = of_cft(of);
4043201af4c0STejun Heo 	struct cgroup_subsys_state *css;
4044201af4c0STejun Heo 	int ret;
4045201af4c0STejun Heo 
404665026da5SJouni Roivas 	if (!nbytes)
404765026da5SJouni Roivas 		return 0;
404865026da5SJouni Roivas 
40495136f636STejun Heo 	/*
40505136f636STejun Heo 	 * If namespaces are delegation boundaries, disallow writes to
40515136f636STejun Heo 	 * files in an non-init namespace root from inside the namespace
40525136f636STejun Heo 	 * except for the files explicitly marked delegatable -
40535136f636STejun Heo 	 * cgroup.procs and cgroup.subtree_control.
40545136f636STejun Heo 	 */
40555136f636STejun Heo 	if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
40565136f636STejun Heo 	    !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
4057e5745764STejun Heo 	    ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp)
40585136f636STejun Heo 		return -EPERM;
40595136f636STejun Heo 
4060201af4c0STejun Heo 	if (cft->write)
4061201af4c0STejun Heo 		return cft->write(of, buf, nbytes, off);
4062201af4c0STejun Heo 
4063201af4c0STejun Heo 	/*
4064201af4c0STejun Heo 	 * kernfs guarantees that a file isn't deleted with operations in
4065201af4c0STejun Heo 	 * flight, which means that the matching css is and stays alive and
4066201af4c0STejun Heo 	 * doesn't need to be pinned.  The RCU locking is not necessary
4067201af4c0STejun Heo 	 * either.  It's just for the convenience of using cgroup_css().
4068201af4c0STejun Heo 	 */
4069201af4c0STejun Heo 	rcu_read_lock();
4070201af4c0STejun Heo 	css = cgroup_css(cgrp, cft->ss);
4071201af4c0STejun Heo 	rcu_read_unlock();
4072201af4c0STejun Heo 
4073201af4c0STejun Heo 	if (cft->write_u64) {
4074201af4c0STejun Heo 		unsigned long long v;
4075201af4c0STejun Heo 		ret = kstrtoull(buf, 0, &v);
4076201af4c0STejun Heo 		if (!ret)
4077201af4c0STejun Heo 			ret = cft->write_u64(css, cft, v);
4078201af4c0STejun Heo 	} else if (cft->write_s64) {
4079201af4c0STejun Heo 		long long v;
4080201af4c0STejun Heo 		ret = kstrtoll(buf, 0, &v);
4081201af4c0STejun Heo 		if (!ret)
4082201af4c0STejun Heo 			ret = cft->write_s64(css, cft, v);
4083201af4c0STejun Heo 	} else {
4084201af4c0STejun Heo 		ret = -EINVAL;
4085201af4c0STejun Heo 	}
4086201af4c0STejun Heo 
4087201af4c0STejun Heo 	return ret ?: nbytes;
4088201af4c0STejun Heo }
4089201af4c0STejun Heo 
cgroup_file_poll(struct kernfs_open_file * of,poll_table * pt)4090dc50537bSJohannes Weiner static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt)
4091dc50537bSJohannes Weiner {
40925a7b5f32SHui Su 	struct cftype *cft = of_cft(of);
4093dc50537bSJohannes Weiner 
4094dc50537bSJohannes Weiner 	if (cft->poll)
4095dc50537bSJohannes Weiner 		return cft->poll(of, pt);
4096dc50537bSJohannes Weiner 
4097dc50537bSJohannes Weiner 	return kernfs_generic_poll(of, pt);
4098dc50537bSJohannes Weiner }
4099dc50537bSJohannes Weiner 
cgroup_seqfile_start(struct seq_file * seq,loff_t * ppos)4100201af4c0STejun Heo static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
4101201af4c0STejun Heo {
4102201af4c0STejun Heo 	return seq_cft(seq)->seq_start(seq, ppos);
4103201af4c0STejun Heo }
4104201af4c0STejun Heo 
cgroup_seqfile_next(struct seq_file * seq,void * v,loff_t * ppos)4105201af4c0STejun Heo static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
4106201af4c0STejun Heo {
4107201af4c0STejun Heo 	return seq_cft(seq)->seq_next(seq, v, ppos);
4108201af4c0STejun Heo }
4109201af4c0STejun Heo 
cgroup_seqfile_stop(struct seq_file * seq,void * v)4110201af4c0STejun Heo static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
4111201af4c0STejun Heo {
4112201af4c0STejun Heo 	if (seq_cft(seq)->seq_stop)
4113201af4c0STejun Heo 		seq_cft(seq)->seq_stop(seq, v);
4114201af4c0STejun Heo }
4115201af4c0STejun Heo 
cgroup_seqfile_show(struct seq_file * m,void * arg)4116201af4c0STejun Heo static int cgroup_seqfile_show(struct seq_file *m, void *arg)
4117201af4c0STejun Heo {
4118201af4c0STejun Heo 	struct cftype *cft = seq_cft(m);
4119201af4c0STejun Heo 	struct cgroup_subsys_state *css = seq_css(m);
4120201af4c0STejun Heo 
4121201af4c0STejun Heo 	if (cft->seq_show)
4122201af4c0STejun Heo 		return cft->seq_show(m, arg);
4123201af4c0STejun Heo 
4124201af4c0STejun Heo 	if (cft->read_u64)
4125201af4c0STejun Heo 		seq_printf(m, "%llu\n", cft->read_u64(css, cft));
4126201af4c0STejun Heo 	else if (cft->read_s64)
4127201af4c0STejun Heo 		seq_printf(m, "%lld\n", cft->read_s64(css, cft));
4128201af4c0STejun Heo 	else
4129201af4c0STejun Heo 		return -EINVAL;
4130201af4c0STejun Heo 	return 0;
4131201af4c0STejun Heo }
4132201af4c0STejun Heo 
4133201af4c0STejun Heo static struct kernfs_ops cgroup_kf_single_ops = {
4134201af4c0STejun Heo 	.atomic_write_len	= PAGE_SIZE,
4135201af4c0STejun Heo 	.open			= cgroup_file_open,
4136201af4c0STejun Heo 	.release		= cgroup_file_release,
4137201af4c0STejun Heo 	.write			= cgroup_file_write,
4138dc50537bSJohannes Weiner 	.poll			= cgroup_file_poll,
4139201af4c0STejun Heo 	.seq_show		= cgroup_seqfile_show,
4140201af4c0STejun Heo };
4141201af4c0STejun Heo 
4142201af4c0STejun Heo static struct kernfs_ops cgroup_kf_ops = {
4143201af4c0STejun Heo 	.atomic_write_len	= PAGE_SIZE,
4144201af4c0STejun Heo 	.open			= cgroup_file_open,
4145201af4c0STejun Heo 	.release		= cgroup_file_release,
4146201af4c0STejun Heo 	.write			= cgroup_file_write,
4147dc50537bSJohannes Weiner 	.poll			= cgroup_file_poll,
4148201af4c0STejun Heo 	.seq_start		= cgroup_seqfile_start,
4149201af4c0STejun Heo 	.seq_next		= cgroup_seqfile_next,
4150201af4c0STejun Heo 	.seq_stop		= cgroup_seqfile_stop,
4151201af4c0STejun Heo 	.seq_show		= cgroup_seqfile_show,
4152201af4c0STejun Heo };
4153201af4c0STejun Heo 
4154201af4c0STejun Heo /* set uid and gid of cgroup dirs and files to that of the creator */
cgroup_kn_set_ugid(struct kernfs_node * kn)4155201af4c0STejun Heo static int cgroup_kn_set_ugid(struct kernfs_node *kn)
4156201af4c0STejun Heo {
4157201af4c0STejun Heo 	struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
4158201af4c0STejun Heo 			       .ia_uid = current_fsuid(),
4159201af4c0STejun Heo 			       .ia_gid = current_fsgid(), };
4160201af4c0STejun Heo 
4161201af4c0STejun Heo 	if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
4162201af4c0STejun Heo 	    gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
4163201af4c0STejun Heo 		return 0;
4164201af4c0STejun Heo 
4165201af4c0STejun Heo 	return kernfs_setattr(kn, &iattr);
4166201af4c0STejun Heo }
4167201af4c0STejun Heo 
cgroup_file_notify_timer(struct timer_list * timer)4168b12e3583STejun Heo static void cgroup_file_notify_timer(struct timer_list *timer)
4169b12e3583STejun Heo {
4170b12e3583STejun Heo 	cgroup_file_notify(container_of(timer, struct cgroup_file,
4171b12e3583STejun Heo 					notify_timer));
4172b12e3583STejun Heo }
4173b12e3583STejun Heo 
cgroup_add_file(struct cgroup_subsys_state * css,struct cgroup * cgrp,struct cftype * cft)4174201af4c0STejun Heo static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
4175201af4c0STejun Heo 			   struct cftype *cft)
4176201af4c0STejun Heo {
4177201af4c0STejun Heo 	char name[CGROUP_FILE_NAME_MAX];
4178201af4c0STejun Heo 	struct kernfs_node *kn;
4179201af4c0STejun Heo 	struct lock_class_key *key = NULL;
4180201af4c0STejun Heo 	int ret;
4181201af4c0STejun Heo 
4182201af4c0STejun Heo #ifdef CONFIG_DEBUG_LOCK_ALLOC
4183201af4c0STejun Heo 	key = &cft->lockdep_key;
4184201af4c0STejun Heo #endif
4185201af4c0STejun Heo 	kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
4186488dee96SDmitry Torokhov 				  cgroup_file_mode(cft),
4187488dee96SDmitry Torokhov 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
4188488dee96SDmitry Torokhov 				  0, cft->kf_ops, cft,
4189201af4c0STejun Heo 				  NULL, key);
4190201af4c0STejun Heo 	if (IS_ERR(kn))
4191201af4c0STejun Heo 		return PTR_ERR(kn);
4192201af4c0STejun Heo 
4193201af4c0STejun Heo 	ret = cgroup_kn_set_ugid(kn);
4194201af4c0STejun Heo 	if (ret) {
4195201af4c0STejun Heo 		kernfs_remove(kn);
4196201af4c0STejun Heo 		return ret;
4197201af4c0STejun Heo 	}
4198201af4c0STejun Heo 
4199201af4c0STejun Heo 	if (cft->file_offset) {
4200201af4c0STejun Heo 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
4201201af4c0STejun Heo 
4202b12e3583STejun Heo 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
4203b12e3583STejun Heo 
4204201af4c0STejun Heo 		spin_lock_irq(&cgroup_file_kn_lock);
4205201af4c0STejun Heo 		cfile->kn = kn;
4206201af4c0STejun Heo 		spin_unlock_irq(&cgroup_file_kn_lock);
4207201af4c0STejun Heo 	}
4208201af4c0STejun Heo 
4209201af4c0STejun Heo 	return 0;
4210201af4c0STejun Heo }
4211201af4c0STejun Heo 
4212201af4c0STejun Heo /**
4213201af4c0STejun Heo  * cgroup_addrm_files - add or remove files to a cgroup directory
4214201af4c0STejun Heo  * @css: the target css
4215201af4c0STejun Heo  * @cgrp: the target cgroup (usually css->cgroup)
4216201af4c0STejun Heo  * @cfts: array of cftypes to be added
4217201af4c0STejun Heo  * @is_add: whether to add or remove
4218201af4c0STejun Heo  *
4219201af4c0STejun Heo  * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
4220201af4c0STejun Heo  * For removals, this function never fails.
4221201af4c0STejun Heo  */
cgroup_addrm_files(struct cgroup_subsys_state * css,struct cgroup * cgrp,struct cftype cfts[],bool is_add)4222201af4c0STejun Heo static int cgroup_addrm_files(struct cgroup_subsys_state *css,
4223201af4c0STejun Heo 			      struct cgroup *cgrp, struct cftype cfts[],
4224201af4c0STejun Heo 			      bool is_add)
4225201af4c0STejun Heo {
4226201af4c0STejun Heo 	struct cftype *cft, *cft_end = NULL;
4227201af4c0STejun Heo 	int ret = 0;
4228201af4c0STejun Heo 
4229201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
4230201af4c0STejun Heo 
4231201af4c0STejun Heo restart:
4232201af4c0STejun Heo 	for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
4233201af4c0STejun Heo 		/* does cft->flags tell us to skip this file on @cgrp? */
4234201af4c0STejun Heo 		if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
4235201af4c0STejun Heo 			continue;
4236201af4c0STejun Heo 		if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
4237201af4c0STejun Heo 			continue;
4238201af4c0STejun Heo 		if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
4239201af4c0STejun Heo 			continue;
4240201af4c0STejun Heo 		if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
4241201af4c0STejun Heo 			continue;
42425cf8114dSWaiman Long 		if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
42435cf8114dSWaiman Long 			continue;
4244201af4c0STejun Heo 		if (is_add) {
4245201af4c0STejun Heo 			ret = cgroup_add_file(css, cgrp, cft);
4246201af4c0STejun Heo 			if (ret) {
4247201af4c0STejun Heo 				pr_warn("%s: failed to add %s, err=%d\n",
4248201af4c0STejun Heo 					__func__, cft->name, ret);
4249201af4c0STejun Heo 				cft_end = cft;
4250201af4c0STejun Heo 				is_add = false;
4251201af4c0STejun Heo 				goto restart;
4252201af4c0STejun Heo 			}
4253201af4c0STejun Heo 		} else {
4254201af4c0STejun Heo 			cgroup_rm_file(cgrp, cft);
4255201af4c0STejun Heo 		}
4256201af4c0STejun Heo 	}
4257201af4c0STejun Heo 	return ret;
4258201af4c0STejun Heo }
4259201af4c0STejun Heo 
cgroup_apply_cftypes(struct cftype * cfts,bool is_add)4260201af4c0STejun Heo static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
4261201af4c0STejun Heo {
4262201af4c0STejun Heo 	struct cgroup_subsys *ss = cfts[0].ss;
4263201af4c0STejun Heo 	struct cgroup *root = &ss->root->cgrp;
4264201af4c0STejun Heo 	struct cgroup_subsys_state *css;
4265201af4c0STejun Heo 	int ret = 0;
4266201af4c0STejun Heo 
4267201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
4268201af4c0STejun Heo 
4269201af4c0STejun Heo 	/* add/rm files for all cgroups created before */
4270201af4c0STejun Heo 	css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
4271201af4c0STejun Heo 		struct cgroup *cgrp = css->cgroup;
4272201af4c0STejun Heo 
4273201af4c0STejun Heo 		if (!(css->flags & CSS_VISIBLE))
4274201af4c0STejun Heo 			continue;
4275201af4c0STejun Heo 
4276201af4c0STejun Heo 		ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
4277201af4c0STejun Heo 		if (ret)
4278201af4c0STejun Heo 			break;
4279201af4c0STejun Heo 	}
4280201af4c0STejun Heo 
4281201af4c0STejun Heo 	if (is_add && !ret)
4282201af4c0STejun Heo 		kernfs_activate(root->kn);
4283201af4c0STejun Heo 	return ret;
4284201af4c0STejun Heo }
4285201af4c0STejun Heo 
cgroup_exit_cftypes(struct cftype * cfts)4286201af4c0STejun Heo static void cgroup_exit_cftypes(struct cftype *cfts)
4287201af4c0STejun Heo {
4288201af4c0STejun Heo 	struct cftype *cft;
4289201af4c0STejun Heo 
4290201af4c0STejun Heo 	for (cft = cfts; cft->name[0] != '\0'; cft++) {
4291201af4c0STejun Heo 		/* free copy for custom atomic_write_len, see init_cftypes() */
4292201af4c0STejun Heo 		if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
4293201af4c0STejun Heo 			kfree(cft->kf_ops);
4294201af4c0STejun Heo 		cft->kf_ops = NULL;
4295201af4c0STejun Heo 		cft->ss = NULL;
4296201af4c0STejun Heo 
4297201af4c0STejun Heo 		/* revert flags set by cgroup core while adding @cfts */
42980083d27bSTejun Heo 		cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL |
42990083d27bSTejun Heo 				__CFTYPE_ADDED);
4300201af4c0STejun Heo 	}
4301201af4c0STejun Heo }
4302201af4c0STejun Heo 
cgroup_init_cftypes(struct cgroup_subsys * ss,struct cftype * cfts)4303201af4c0STejun Heo static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4304201af4c0STejun Heo {
4305201af4c0STejun Heo 	struct cftype *cft;
43060083d27bSTejun Heo 	int ret = 0;
4307201af4c0STejun Heo 
4308201af4c0STejun Heo 	for (cft = cfts; cft->name[0] != '\0'; cft++) {
4309201af4c0STejun Heo 		struct kernfs_ops *kf_ops;
4310201af4c0STejun Heo 
4311201af4c0STejun Heo 		WARN_ON(cft->ss || cft->kf_ops);
4312201af4c0STejun Heo 
43130083d27bSTejun Heo 		if (cft->flags & __CFTYPE_ADDED) {
43140083d27bSTejun Heo 			ret = -EBUSY;
43150083d27bSTejun Heo 			break;
43160083d27bSTejun Heo 		}
43173958e2d0SSuren Baghdasaryan 
4318201af4c0STejun Heo 		if (cft->seq_start)
4319201af4c0STejun Heo 			kf_ops = &cgroup_kf_ops;
4320201af4c0STejun Heo 		else
4321201af4c0STejun Heo 			kf_ops = &cgroup_kf_single_ops;
4322201af4c0STejun Heo 
4323201af4c0STejun Heo 		/*
4324201af4c0STejun Heo 		 * Ugh... if @cft wants a custom max_write_len, we need to
4325201af4c0STejun Heo 		 * make a copy of kf_ops to set its atomic_write_len.
4326201af4c0STejun Heo 		 */
4327201af4c0STejun Heo 		if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
4328201af4c0STejun Heo 			kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
4329201af4c0STejun Heo 			if (!kf_ops) {
43300083d27bSTejun Heo 				ret = -ENOMEM;
43310083d27bSTejun Heo 				break;
4332201af4c0STejun Heo 			}
4333201af4c0STejun Heo 			kf_ops->atomic_write_len = cft->max_write_len;
4334201af4c0STejun Heo 		}
4335201af4c0STejun Heo 
4336201af4c0STejun Heo 		cft->kf_ops = kf_ops;
4337201af4c0STejun Heo 		cft->ss = ss;
43380083d27bSTejun Heo 		cft->flags |= __CFTYPE_ADDED;
4339201af4c0STejun Heo 	}
4340201af4c0STejun Heo 
43410083d27bSTejun Heo 	if (ret)
43420083d27bSTejun Heo 		cgroup_exit_cftypes(cfts);
43430083d27bSTejun Heo 	return ret;
4344201af4c0STejun Heo }
4345201af4c0STejun Heo 
cgroup_rm_cftypes_locked(struct cftype * cfts)43462246ca53SMiaohe Lin static void cgroup_rm_cftypes_locked(struct cftype *cfts)
4347201af4c0STejun Heo {
4348201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
4349201af4c0STejun Heo 
4350201af4c0STejun Heo 	list_del(&cfts->node);
4351201af4c0STejun Heo 	cgroup_apply_cftypes(cfts, false);
4352201af4c0STejun Heo 	cgroup_exit_cftypes(cfts);
4353201af4c0STejun Heo }
4354201af4c0STejun Heo 
4355201af4c0STejun Heo /**
4356201af4c0STejun Heo  * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
4357201af4c0STejun Heo  * @cfts: zero-length name terminated array of cftypes
4358201af4c0STejun Heo  *
4359201af4c0STejun Heo  * Unregister @cfts.  Files described by @cfts are removed from all
4360201af4c0STejun Heo  * existing cgroups and all future cgroups won't have them either.  This
4361201af4c0STejun Heo  * function can be called anytime whether @cfts' subsys is attached or not.
4362201af4c0STejun Heo  *
4363201af4c0STejun Heo  * Returns 0 on successful unregistration, -ENOENT if @cfts is not
4364201af4c0STejun Heo  * registered.
4365201af4c0STejun Heo  */
cgroup_rm_cftypes(struct cftype * cfts)4366201af4c0STejun Heo int cgroup_rm_cftypes(struct cftype *cfts)
4367201af4c0STejun Heo {
43680083d27bSTejun Heo 	if (!cfts || cfts[0].name[0] == '\0')
43690083d27bSTejun Heo 		return 0;
43700083d27bSTejun Heo 
43710083d27bSTejun Heo 	if (!(cfts[0].flags & __CFTYPE_ADDED))
43720083d27bSTejun Heo 		return -ENOENT;
43730083d27bSTejun Heo 
43744cdb91b0SKamalesh Babulal 	cgroup_lock();
43752246ca53SMiaohe Lin 	cgroup_rm_cftypes_locked(cfts);
43764cdb91b0SKamalesh Babulal 	cgroup_unlock();
43772246ca53SMiaohe Lin 	return 0;
4378201af4c0STejun Heo }
4379201af4c0STejun Heo 
4380201af4c0STejun Heo /**
4381201af4c0STejun Heo  * cgroup_add_cftypes - add an array of cftypes to a subsystem
4382201af4c0STejun Heo  * @ss: target cgroup subsystem
4383201af4c0STejun Heo  * @cfts: zero-length name terminated array of cftypes
4384201af4c0STejun Heo  *
4385201af4c0STejun Heo  * Register @cfts to @ss.  Files described by @cfts are created for all
4386201af4c0STejun Heo  * existing cgroups to which @ss is attached and all future cgroups will
4387201af4c0STejun Heo  * have them too.  This function can be called anytime whether @ss is
4388201af4c0STejun Heo  * attached or not.
4389201af4c0STejun Heo  *
4390201af4c0STejun Heo  * Returns 0 on successful registration, -errno on failure.  Note that this
4391201af4c0STejun Heo  * function currently returns 0 as long as @cfts registration is successful
4392201af4c0STejun Heo  * even if some file creation attempts on existing cgroups fail.
4393201af4c0STejun Heo  */
cgroup_add_cftypes(struct cgroup_subsys * ss,struct cftype * cfts)4394201af4c0STejun Heo static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4395201af4c0STejun Heo {
4396201af4c0STejun Heo 	int ret;
4397201af4c0STejun Heo 
4398201af4c0STejun Heo 	if (!cgroup_ssid_enabled(ss->id))
4399201af4c0STejun Heo 		return 0;
4400201af4c0STejun Heo 
4401201af4c0STejun Heo 	if (!cfts || cfts[0].name[0] == '\0')
4402201af4c0STejun Heo 		return 0;
4403201af4c0STejun Heo 
4404201af4c0STejun Heo 	ret = cgroup_init_cftypes(ss, cfts);
4405201af4c0STejun Heo 	if (ret)
4406201af4c0STejun Heo 		return ret;
4407201af4c0STejun Heo 
44084cdb91b0SKamalesh Babulal 	cgroup_lock();
4409201af4c0STejun Heo 
4410201af4c0STejun Heo 	list_add_tail(&cfts->node, &ss->cfts);
4411201af4c0STejun Heo 	ret = cgroup_apply_cftypes(cfts, true);
4412201af4c0STejun Heo 	if (ret)
4413201af4c0STejun Heo 		cgroup_rm_cftypes_locked(cfts);
4414201af4c0STejun Heo 
44154cdb91b0SKamalesh Babulal 	cgroup_unlock();
4416201af4c0STejun Heo 	return ret;
4417201af4c0STejun Heo }
4418201af4c0STejun Heo 
4419201af4c0STejun Heo /**
4420201af4c0STejun Heo  * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
4421201af4c0STejun Heo  * @ss: target cgroup subsystem
4422201af4c0STejun Heo  * @cfts: zero-length name terminated array of cftypes
4423201af4c0STejun Heo  *
4424201af4c0STejun Heo  * Similar to cgroup_add_cftypes() but the added files are only used for
4425201af4c0STejun Heo  * the default hierarchy.
4426201af4c0STejun Heo  */
cgroup_add_dfl_cftypes(struct cgroup_subsys * ss,struct cftype * cfts)4427201af4c0STejun Heo int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4428201af4c0STejun Heo {
4429201af4c0STejun Heo 	struct cftype *cft;
4430201af4c0STejun Heo 
4431201af4c0STejun Heo 	for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
4432201af4c0STejun Heo 		cft->flags |= __CFTYPE_ONLY_ON_DFL;
4433201af4c0STejun Heo 	return cgroup_add_cftypes(ss, cfts);
4434201af4c0STejun Heo }
4435201af4c0STejun Heo 
4436201af4c0STejun Heo /**
4437201af4c0STejun Heo  * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
4438201af4c0STejun Heo  * @ss: target cgroup subsystem
4439201af4c0STejun Heo  * @cfts: zero-length name terminated array of cftypes
4440201af4c0STejun Heo  *
4441201af4c0STejun Heo  * Similar to cgroup_add_cftypes() but the added files are only used for
4442201af4c0STejun Heo  * the legacy hierarchies.
4443201af4c0STejun Heo  */
cgroup_add_legacy_cftypes(struct cgroup_subsys * ss,struct cftype * cfts)4444201af4c0STejun Heo int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4445201af4c0STejun Heo {
4446201af4c0STejun Heo 	struct cftype *cft;
4447201af4c0STejun Heo 
4448201af4c0STejun Heo 	for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
4449201af4c0STejun Heo 		cft->flags |= __CFTYPE_NOT_ON_DFL;
4450201af4c0STejun Heo 	return cgroup_add_cftypes(ss, cfts);
4451201af4c0STejun Heo }
4452201af4c0STejun Heo 
4453201af4c0STejun Heo /**
4454201af4c0STejun Heo  * cgroup_file_notify - generate a file modified event for a cgroup_file
4455201af4c0STejun Heo  * @cfile: target cgroup_file
4456201af4c0STejun Heo  *
4457201af4c0STejun Heo  * @cfile must have been obtained by setting cftype->file_offset.
4458201af4c0STejun Heo  */
cgroup_file_notify(struct cgroup_file * cfile)4459201af4c0STejun Heo void cgroup_file_notify(struct cgroup_file *cfile)
4460201af4c0STejun Heo {
4461201af4c0STejun Heo 	unsigned long flags;
4462201af4c0STejun Heo 
4463201af4c0STejun Heo 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
4464b12e3583STejun Heo 	if (cfile->kn) {
4465b12e3583STejun Heo 		unsigned long last = cfile->notified_at;
4466b12e3583STejun Heo 		unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
4467b12e3583STejun Heo 
4468b12e3583STejun Heo 		if (time_in_range(jiffies, last, next)) {
4469b12e3583STejun Heo 			timer_reduce(&cfile->notify_timer, next);
4470b12e3583STejun Heo 		} else {
4471201af4c0STejun Heo 			kernfs_notify(cfile->kn);
4472b12e3583STejun Heo 			cfile->notified_at = jiffies;
4473b12e3583STejun Heo 		}
4474b12e3583STejun Heo 	}
4475201af4c0STejun Heo 	spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
4476201af4c0STejun Heo }
4477201af4c0STejun Heo 
4478201af4c0STejun Heo /**
4479e2691f6bSTejun Heo  * cgroup_file_show - show or hide a hidden cgroup file
4480e2691f6bSTejun Heo  * @cfile: target cgroup_file obtained by setting cftype->file_offset
4481e2691f6bSTejun Heo  * @show: whether to show or hide
4482e2691f6bSTejun Heo  */
cgroup_file_show(struct cgroup_file * cfile,bool show)4483e2691f6bSTejun Heo void cgroup_file_show(struct cgroup_file *cfile, bool show)
4484e2691f6bSTejun Heo {
4485e2691f6bSTejun Heo 	struct kernfs_node *kn;
4486e2691f6bSTejun Heo 
4487e2691f6bSTejun Heo 	spin_lock_irq(&cgroup_file_kn_lock);
4488e2691f6bSTejun Heo 	kn = cfile->kn;
4489e2691f6bSTejun Heo 	kernfs_get(kn);
4490e2691f6bSTejun Heo 	spin_unlock_irq(&cgroup_file_kn_lock);
4491e2691f6bSTejun Heo 
4492e2691f6bSTejun Heo 	if (kn)
4493e2691f6bSTejun Heo 		kernfs_show(kn, show);
4494e2691f6bSTejun Heo 
4495e2691f6bSTejun Heo 	kernfs_put(kn);
4496e2691f6bSTejun Heo }
4497e2691f6bSTejun Heo 
4498e2691f6bSTejun Heo /**
4499201af4c0STejun Heo  * css_next_child - find the next child of a given css
4500201af4c0STejun Heo  * @pos: the current position (%NULL to initiate traversal)
4501201af4c0STejun Heo  * @parent: css whose children to walk
4502201af4c0STejun Heo  *
4503201af4c0STejun Heo  * This function returns the next child of @parent and should be called
4504201af4c0STejun Heo  * under either cgroup_mutex or RCU read lock.  The only requirement is
4505201af4c0STejun Heo  * that @parent and @pos are accessible.  The next sibling is guaranteed to
4506201af4c0STejun Heo  * be returned regardless of their states.
4507201af4c0STejun Heo  *
4508201af4c0STejun Heo  * If a subsystem synchronizes ->css_online() and the start of iteration, a
4509201af4c0STejun Heo  * css which finished ->css_online() is guaranteed to be visible in the
4510201af4c0STejun Heo  * future iterations and will stay visible until the last reference is put.
4511201af4c0STejun Heo  * A css which hasn't finished ->css_online() or already finished
4512201af4c0STejun Heo  * ->css_offline() may show up during traversal.  It's each subsystem's
4513201af4c0STejun Heo  * responsibility to synchronize against on/offlining.
4514201af4c0STejun Heo  */
css_next_child(struct cgroup_subsys_state * pos,struct cgroup_subsys_state * parent)4515201af4c0STejun Heo struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
4516201af4c0STejun Heo 					   struct cgroup_subsys_state *parent)
4517201af4c0STejun Heo {
4518201af4c0STejun Heo 	struct cgroup_subsys_state *next;
4519201af4c0STejun Heo 
4520201af4c0STejun Heo 	cgroup_assert_mutex_or_rcu_locked();
4521201af4c0STejun Heo 
4522201af4c0STejun Heo 	/*
4523201af4c0STejun Heo 	 * @pos could already have been unlinked from the sibling list.
4524201af4c0STejun Heo 	 * Once a cgroup is removed, its ->sibling.next is no longer
4525201af4c0STejun Heo 	 * updated when its next sibling changes.  CSS_RELEASED is set when
4526201af4c0STejun Heo 	 * @pos is taken off list, at which time its next pointer is valid,
4527201af4c0STejun Heo 	 * and, as releases are serialized, the one pointed to by the next
4528201af4c0STejun Heo 	 * pointer is guaranteed to not have started release yet.  This
4529201af4c0STejun Heo 	 * implies that if we observe !CSS_RELEASED on @pos in this RCU
4530201af4c0STejun Heo 	 * critical section, the one pointed to by its next pointer is
4531201af4c0STejun Heo 	 * guaranteed to not have finished its RCU grace period even if we
453258315c96SBhaskar Chowdhury 	 * have dropped rcu_read_lock() in-between iterations.
4533201af4c0STejun Heo 	 *
4534201af4c0STejun Heo 	 * If @pos has CSS_RELEASED set, its next pointer can't be
4535201af4c0STejun Heo 	 * dereferenced; however, as each css is given a monotonically
4536201af4c0STejun Heo 	 * increasing unique serial number and always appended to the
4537201af4c0STejun Heo 	 * sibling list, the next one can be found by walking the parent's
4538201af4c0STejun Heo 	 * children until the first css with higher serial number than
4539201af4c0STejun Heo 	 * @pos's.  While this path can be slower, it happens iff iteration
4540201af4c0STejun Heo 	 * races against release and the race window is very small.
4541201af4c0STejun Heo 	 */
4542201af4c0STejun Heo 	if (!pos) {
4543201af4c0STejun Heo 		next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
4544201af4c0STejun Heo 	} else if (likely(!(pos->flags & CSS_RELEASED))) {
4545201af4c0STejun Heo 		next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
4546201af4c0STejun Heo 	} else {
45473010c5b9SMadhuparna Bhowmik 		list_for_each_entry_rcu(next, &parent->children, sibling,
45483010c5b9SMadhuparna Bhowmik 					lockdep_is_held(&cgroup_mutex))
4549201af4c0STejun Heo 			if (next->serial_nr > pos->serial_nr)
4550201af4c0STejun Heo 				break;
4551201af4c0STejun Heo 	}
4552201af4c0STejun Heo 
4553201af4c0STejun Heo 	/*
4554201af4c0STejun Heo 	 * @next, if not pointing to the head, can be dereferenced and is
4555201af4c0STejun Heo 	 * the next sibling.
4556201af4c0STejun Heo 	 */
4557201af4c0STejun Heo 	if (&next->sibling != &parent->children)
4558201af4c0STejun Heo 		return next;
4559201af4c0STejun Heo 	return NULL;
4560201af4c0STejun Heo }
4561201af4c0STejun Heo 
4562201af4c0STejun Heo /**
4563201af4c0STejun Heo  * css_next_descendant_pre - find the next descendant for pre-order walk
4564201af4c0STejun Heo  * @pos: the current position (%NULL to initiate traversal)
4565201af4c0STejun Heo  * @root: css whose descendants to walk
4566201af4c0STejun Heo  *
4567201af4c0STejun Heo  * To be used by css_for_each_descendant_pre().  Find the next descendant
4568201af4c0STejun Heo  * to visit for pre-order traversal of @root's descendants.  @root is
4569201af4c0STejun Heo  * included in the iteration and the first node to be visited.
4570201af4c0STejun Heo  *
4571201af4c0STejun Heo  * While this function requires cgroup_mutex or RCU read locking, it
4572201af4c0STejun Heo  * doesn't require the whole traversal to be contained in a single critical
4573201af4c0STejun Heo  * section.  This function will return the correct next descendant as long
4574201af4c0STejun Heo  * as both @pos and @root are accessible and @pos is a descendant of @root.
4575201af4c0STejun Heo  *
4576201af4c0STejun Heo  * If a subsystem synchronizes ->css_online() and the start of iteration, a
4577201af4c0STejun Heo  * css which finished ->css_online() is guaranteed to be visible in the
4578201af4c0STejun Heo  * future iterations and will stay visible until the last reference is put.
4579201af4c0STejun Heo  * A css which hasn't finished ->css_online() or already finished
4580201af4c0STejun Heo  * ->css_offline() may show up during traversal.  It's each subsystem's
4581201af4c0STejun Heo  * responsibility to synchronize against on/offlining.
4582201af4c0STejun Heo  */
4583201af4c0STejun Heo struct cgroup_subsys_state *
css_next_descendant_pre(struct cgroup_subsys_state * pos,struct cgroup_subsys_state * root)4584201af4c0STejun Heo css_next_descendant_pre(struct cgroup_subsys_state *pos,
4585201af4c0STejun Heo 			struct cgroup_subsys_state *root)
4586201af4c0STejun Heo {
4587201af4c0STejun Heo 	struct cgroup_subsys_state *next;
4588201af4c0STejun Heo 
4589201af4c0STejun Heo 	cgroup_assert_mutex_or_rcu_locked();
4590201af4c0STejun Heo 
4591201af4c0STejun Heo 	/* if first iteration, visit @root */
4592201af4c0STejun Heo 	if (!pos)
4593201af4c0STejun Heo 		return root;
4594201af4c0STejun Heo 
4595201af4c0STejun Heo 	/* visit the first child if exists */
4596201af4c0STejun Heo 	next = css_next_child(NULL, pos);
4597201af4c0STejun Heo 	if (next)
4598201af4c0STejun Heo 		return next;
4599201af4c0STejun Heo 
4600201af4c0STejun Heo 	/* no child, visit my or the closest ancestor's next sibling */
4601201af4c0STejun Heo 	while (pos != root) {
4602201af4c0STejun Heo 		next = css_next_child(pos, pos->parent);
4603201af4c0STejun Heo 		if (next)
4604201af4c0STejun Heo 			return next;
4605201af4c0STejun Heo 		pos = pos->parent;
4606201af4c0STejun Heo 	}
4607201af4c0STejun Heo 
4608201af4c0STejun Heo 	return NULL;
4609201af4c0STejun Heo }
4610474a2800SChristoph Hellwig EXPORT_SYMBOL_GPL(css_next_descendant_pre);
4611201af4c0STejun Heo 
4612201af4c0STejun Heo /**
4613201af4c0STejun Heo  * css_rightmost_descendant - return the rightmost descendant of a css
4614201af4c0STejun Heo  * @pos: css of interest
4615201af4c0STejun Heo  *
4616201af4c0STejun Heo  * Return the rightmost descendant of @pos.  If there's no descendant, @pos
4617201af4c0STejun Heo  * is returned.  This can be used during pre-order traversal to skip
4618201af4c0STejun Heo  * subtree of @pos.
4619201af4c0STejun Heo  *
4620201af4c0STejun Heo  * While this function requires cgroup_mutex or RCU read locking, it
4621201af4c0STejun Heo  * doesn't require the whole traversal to be contained in a single critical
4622201af4c0STejun Heo  * section.  This function will return the correct rightmost descendant as
4623201af4c0STejun Heo  * long as @pos is accessible.
4624201af4c0STejun Heo  */
4625201af4c0STejun Heo struct cgroup_subsys_state *
css_rightmost_descendant(struct cgroup_subsys_state * pos)4626201af4c0STejun Heo css_rightmost_descendant(struct cgroup_subsys_state *pos)
4627201af4c0STejun Heo {
4628201af4c0STejun Heo 	struct cgroup_subsys_state *last, *tmp;
4629201af4c0STejun Heo 
4630201af4c0STejun Heo 	cgroup_assert_mutex_or_rcu_locked();
4631201af4c0STejun Heo 
4632201af4c0STejun Heo 	do {
4633201af4c0STejun Heo 		last = pos;
4634201af4c0STejun Heo 		/* ->prev isn't RCU safe, walk ->next till the end */
4635201af4c0STejun Heo 		pos = NULL;
4636201af4c0STejun Heo 		css_for_each_child(tmp, last)
4637201af4c0STejun Heo 			pos = tmp;
4638201af4c0STejun Heo 	} while (pos);
4639201af4c0STejun Heo 
4640201af4c0STejun Heo 	return last;
4641201af4c0STejun Heo }
4642201af4c0STejun Heo 
4643201af4c0STejun Heo static struct cgroup_subsys_state *
css_leftmost_descendant(struct cgroup_subsys_state * pos)4644201af4c0STejun Heo css_leftmost_descendant(struct cgroup_subsys_state *pos)
4645201af4c0STejun Heo {
4646201af4c0STejun Heo 	struct cgroup_subsys_state *last;
4647201af4c0STejun Heo 
4648201af4c0STejun Heo 	do {
4649201af4c0STejun Heo 		last = pos;
4650201af4c0STejun Heo 		pos = css_next_child(NULL, pos);
4651201af4c0STejun Heo 	} while (pos);
4652201af4c0STejun Heo 
4653201af4c0STejun Heo 	return last;
4654201af4c0STejun Heo }
4655201af4c0STejun Heo 
4656201af4c0STejun Heo /**
4657201af4c0STejun Heo  * css_next_descendant_post - find the next descendant for post-order walk
4658201af4c0STejun Heo  * @pos: the current position (%NULL to initiate traversal)
4659201af4c0STejun Heo  * @root: css whose descendants to walk
4660201af4c0STejun Heo  *
4661201af4c0STejun Heo  * To be used by css_for_each_descendant_post().  Find the next descendant
4662201af4c0STejun Heo  * to visit for post-order traversal of @root's descendants.  @root is
4663201af4c0STejun Heo  * included in the iteration and the last node to be visited.
4664201af4c0STejun Heo  *
4665201af4c0STejun Heo  * While this function requires cgroup_mutex or RCU read locking, it
4666201af4c0STejun Heo  * doesn't require the whole traversal to be contained in a single critical
4667201af4c0STejun Heo  * section.  This function will return the correct next descendant as long
4668201af4c0STejun Heo  * as both @pos and @cgroup are accessible and @pos is a descendant of
4669201af4c0STejun Heo  * @cgroup.
4670201af4c0STejun Heo  *
4671201af4c0STejun Heo  * If a subsystem synchronizes ->css_online() and the start of iteration, a
4672201af4c0STejun Heo  * css which finished ->css_online() is guaranteed to be visible in the
4673201af4c0STejun Heo  * future iterations and will stay visible until the last reference is put.
4674201af4c0STejun Heo  * A css which hasn't finished ->css_online() or already finished
4675201af4c0STejun Heo  * ->css_offline() may show up during traversal.  It's each subsystem's
4676201af4c0STejun Heo  * responsibility to synchronize against on/offlining.
4677201af4c0STejun Heo  */
4678201af4c0STejun Heo struct cgroup_subsys_state *
css_next_descendant_post(struct cgroup_subsys_state * pos,struct cgroup_subsys_state * root)4679201af4c0STejun Heo css_next_descendant_post(struct cgroup_subsys_state *pos,
4680201af4c0STejun Heo 			 struct cgroup_subsys_state *root)
4681201af4c0STejun Heo {
4682201af4c0STejun Heo 	struct cgroup_subsys_state *next;
4683201af4c0STejun Heo 
4684201af4c0STejun Heo 	cgroup_assert_mutex_or_rcu_locked();
4685201af4c0STejun Heo 
4686201af4c0STejun Heo 	/* if first iteration, visit leftmost descendant which may be @root */
4687201af4c0STejun Heo 	if (!pos)
4688201af4c0STejun Heo 		return css_leftmost_descendant(root);
4689201af4c0STejun Heo 
4690201af4c0STejun Heo 	/* if we visited @root, we're done */
4691201af4c0STejun Heo 	if (pos == root)
4692201af4c0STejun Heo 		return NULL;
4693201af4c0STejun Heo 
4694201af4c0STejun Heo 	/* if there's an unvisited sibling, visit its leftmost descendant */
4695201af4c0STejun Heo 	next = css_next_child(pos, pos->parent);
4696201af4c0STejun Heo 	if (next)
4697201af4c0STejun Heo 		return css_leftmost_descendant(next);
4698201af4c0STejun Heo 
4699201af4c0STejun Heo 	/* no sibling left, visit parent */
4700201af4c0STejun Heo 	return pos->parent;
4701201af4c0STejun Heo }
4702201af4c0STejun Heo 
4703201af4c0STejun Heo /**
4704201af4c0STejun Heo  * css_has_online_children - does a css have online children
4705201af4c0STejun Heo  * @css: the target css
4706201af4c0STejun Heo  *
4707201af4c0STejun Heo  * Returns %true if @css has any online children; otherwise, %false.  This
4708201af4c0STejun Heo  * function can be called from any context but the caller is responsible
4709201af4c0STejun Heo  * for synchronizing against on/offlining as necessary.
4710201af4c0STejun Heo  */
css_has_online_children(struct cgroup_subsys_state * css)4711201af4c0STejun Heo bool css_has_online_children(struct cgroup_subsys_state *css)
4712201af4c0STejun Heo {
4713201af4c0STejun Heo 	struct cgroup_subsys_state *child;
4714201af4c0STejun Heo 	bool ret = false;
4715201af4c0STejun Heo 
4716201af4c0STejun Heo 	rcu_read_lock();
4717201af4c0STejun Heo 	css_for_each_child(child, css) {
4718201af4c0STejun Heo 		if (child->flags & CSS_ONLINE) {
4719201af4c0STejun Heo 			ret = true;
4720201af4c0STejun Heo 			break;
4721201af4c0STejun Heo 		}
4722201af4c0STejun Heo 	}
4723201af4c0STejun Heo 	rcu_read_unlock();
4724201af4c0STejun Heo 	return ret;
4725201af4c0STejun Heo }
4726201af4c0STejun Heo 
css_task_iter_next_css_set(struct css_task_iter * it)4727450ee0c1STejun Heo static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4728450ee0c1STejun Heo {
4729450ee0c1STejun Heo 	struct list_head *l;
4730450ee0c1STejun Heo 	struct cgrp_cset_link *link;
4731450ee0c1STejun Heo 	struct css_set *cset;
4732450ee0c1STejun Heo 
4733450ee0c1STejun Heo 	lockdep_assert_held(&css_set_lock);
4734450ee0c1STejun Heo 
4735450ee0c1STejun Heo 	/* find the next threaded cset */
4736450ee0c1STejun Heo 	if (it->tcset_pos) {
4737450ee0c1STejun Heo 		l = it->tcset_pos->next;
4738450ee0c1STejun Heo 
4739450ee0c1STejun Heo 		if (l != it->tcset_head) {
4740450ee0c1STejun Heo 			it->tcset_pos = l;
4741450ee0c1STejun Heo 			return container_of(l, struct css_set,
4742450ee0c1STejun Heo 					    threaded_csets_node);
4743450ee0c1STejun Heo 		}
4744450ee0c1STejun Heo 
4745450ee0c1STejun Heo 		it->tcset_pos = NULL;
4746450ee0c1STejun Heo 	}
4747450ee0c1STejun Heo 
4748450ee0c1STejun Heo 	/* find the next cset */
4749450ee0c1STejun Heo 	l = it->cset_pos;
4750450ee0c1STejun Heo 	l = l->next;
4751450ee0c1STejun Heo 	if (l == it->cset_head) {
4752450ee0c1STejun Heo 		it->cset_pos = NULL;
4753450ee0c1STejun Heo 		return NULL;
4754450ee0c1STejun Heo 	}
4755450ee0c1STejun Heo 
4756450ee0c1STejun Heo 	if (it->ss) {
4757450ee0c1STejun Heo 		cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4758450ee0c1STejun Heo 	} else {
4759450ee0c1STejun Heo 		link = list_entry(l, struct cgrp_cset_link, cset_link);
4760450ee0c1STejun Heo 		cset = link->cset;
4761450ee0c1STejun Heo 	}
4762450ee0c1STejun Heo 
4763450ee0c1STejun Heo 	it->cset_pos = l;
4764450ee0c1STejun Heo 
4765450ee0c1STejun Heo 	/* initialize threaded css_set walking */
4766450ee0c1STejun Heo 	if (it->flags & CSS_TASK_ITER_THREADED) {
4767450ee0c1STejun Heo 		if (it->cur_dcset)
4768450ee0c1STejun Heo 			put_css_set_locked(it->cur_dcset);
4769450ee0c1STejun Heo 		it->cur_dcset = cset;
4770450ee0c1STejun Heo 		get_css_set(cset);
4771450ee0c1STejun Heo 
4772450ee0c1STejun Heo 		it->tcset_head = &cset->threaded_csets;
4773450ee0c1STejun Heo 		it->tcset_pos = &cset->threaded_csets;
4774450ee0c1STejun Heo 	}
4775450ee0c1STejun Heo 
4776450ee0c1STejun Heo 	return cset;
4777450ee0c1STejun Heo }
4778450ee0c1STejun Heo 
4779201af4c0STejun Heo /**
478058315c96SBhaskar Chowdhury  * css_task_iter_advance_css_set - advance a task iterator to the next css_set
4781201af4c0STejun Heo  * @it: the iterator to advance
4782201af4c0STejun Heo  *
4783201af4c0STejun Heo  * Advance @it to the next css_set to walk.
4784201af4c0STejun Heo  */
css_task_iter_advance_css_set(struct css_task_iter * it)4785201af4c0STejun Heo static void css_task_iter_advance_css_set(struct css_task_iter *it)
4786201af4c0STejun Heo {
4787201af4c0STejun Heo 	struct css_set *cset;
4788201af4c0STejun Heo 
4789201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
4790201af4c0STejun Heo 
4791f43caa2aSMichal Koutný 	/* Advance to the next non-empty css_set and find first non-empty tasks list*/
4792f43caa2aSMichal Koutný 	while ((cset = css_task_iter_next_css_set(it))) {
4793f43caa2aSMichal Koutný 		if (!list_empty(&cset->tasks)) {
4794f43caa2aSMichal Koutný 			it->cur_tasks_head = &cset->tasks;
4795f43caa2aSMichal Koutný 			break;
4796f43caa2aSMichal Koutný 		} else if (!list_empty(&cset->mg_tasks)) {
4797f43caa2aSMichal Koutný 			it->cur_tasks_head = &cset->mg_tasks;
4798f43caa2aSMichal Koutný 			break;
4799f43caa2aSMichal Koutný 		} else if (!list_empty(&cset->dying_tasks)) {
4800f43caa2aSMichal Koutný 			it->cur_tasks_head = &cset->dying_tasks;
4801f43caa2aSMichal Koutný 			break;
4802f43caa2aSMichal Koutný 		}
4803f43caa2aSMichal Koutný 	}
4804450ee0c1STejun Heo 	if (!cset) {
4805201af4c0STejun Heo 		it->task_pos = NULL;
4806201af4c0STejun Heo 		return;
4807201af4c0STejun Heo 	}
4808f43caa2aSMichal Koutný 	it->task_pos = it->cur_tasks_head->next;
4809201af4c0STejun Heo 
4810201af4c0STejun Heo 	/*
4811201af4c0STejun Heo 	 * We don't keep css_sets locked across iteration steps and thus
4812201af4c0STejun Heo 	 * need to take steps to ensure that iteration can be resumed after
4813201af4c0STejun Heo 	 * the lock is re-acquired.  Iteration is performed at two levels -
4814201af4c0STejun Heo 	 * css_sets and tasks in them.
4815201af4c0STejun Heo 	 *
4816201af4c0STejun Heo 	 * Once created, a css_set never leaves its cgroup lists, so a
4817201af4c0STejun Heo 	 * pinned css_set is guaranteed to stay put and we can resume
4818201af4c0STejun Heo 	 * iteration afterwards.
4819201af4c0STejun Heo 	 *
4820201af4c0STejun Heo 	 * Tasks may leave @cset across iteration steps.  This is resolved
4821201af4c0STejun Heo 	 * by registering each iterator with the css_set currently being
4822201af4c0STejun Heo 	 * walked and making css_set_move_task() advance iterators whose
4823201af4c0STejun Heo 	 * next task is leaving.
4824201af4c0STejun Heo 	 */
4825201af4c0STejun Heo 	if (it->cur_cset) {
4826201af4c0STejun Heo 		list_del(&it->iters_node);
4827201af4c0STejun Heo 		put_css_set_locked(it->cur_cset);
4828201af4c0STejun Heo 	}
4829201af4c0STejun Heo 	get_css_set(cset);
4830201af4c0STejun Heo 	it->cur_cset = cset;
4831201af4c0STejun Heo 	list_add(&it->iters_node, &cset->task_iters);
4832201af4c0STejun Heo }
4833201af4c0STejun Heo 
css_task_iter_skip(struct css_task_iter * it,struct task_struct * task)4834b636fd38STejun Heo static void css_task_iter_skip(struct css_task_iter *it,
4835b636fd38STejun Heo 			       struct task_struct *task)
4836b636fd38STejun Heo {
4837b636fd38STejun Heo 	lockdep_assert_held(&css_set_lock);
4838b636fd38STejun Heo 
4839b636fd38STejun Heo 	if (it->task_pos == &task->cg_list) {
4840b636fd38STejun Heo 		it->task_pos = it->task_pos->next;
4841b636fd38STejun Heo 		it->flags |= CSS_TASK_ITER_SKIPPED;
4842b636fd38STejun Heo 	}
4843b636fd38STejun Heo }
4844b636fd38STejun Heo 
css_task_iter_advance(struct css_task_iter * it)4845201af4c0STejun Heo static void css_task_iter_advance(struct css_task_iter *it)
4846201af4c0STejun Heo {
4847c03cd773STejun Heo 	struct task_struct *task;
4848201af4c0STejun Heo 
4849201af4c0STejun Heo 	lockdep_assert_held(&css_set_lock);
4850bc2fb7edSTejun Heo repeat:
4851e9d81a1bSTejun Heo 	if (it->task_pos) {
4852201af4c0STejun Heo 		/*
4853f43caa2aSMichal Koutný 		 * Advance iterator to find next entry. We go through cset
4854f43caa2aSMichal Koutný 		 * tasks, mg_tasks and dying_tasks, when consumed we move onto
4855f43caa2aSMichal Koutný 		 * the next cset.
4856201af4c0STejun Heo 		 */
4857b636fd38STejun Heo 		if (it->flags & CSS_TASK_ITER_SKIPPED)
4858b636fd38STejun Heo 			it->flags &= ~CSS_TASK_ITER_SKIPPED;
4859201af4c0STejun Heo 		else
4860b636fd38STejun Heo 			it->task_pos = it->task_pos->next;
4861b636fd38STejun Heo 
4862f43caa2aSMichal Koutný 		if (it->task_pos == &it->cur_cset->tasks) {
4863f43caa2aSMichal Koutný 			it->cur_tasks_head = &it->cur_cset->mg_tasks;
4864f43caa2aSMichal Koutný 			it->task_pos = it->cur_tasks_head->next;
48659c974c77SMichal Koutný 		}
4866f43caa2aSMichal Koutný 		if (it->task_pos == &it->cur_cset->mg_tasks) {
4867f43caa2aSMichal Koutný 			it->cur_tasks_head = &it->cur_cset->dying_tasks;
4868f43caa2aSMichal Koutný 			it->task_pos = it->cur_tasks_head->next;
48699c974c77SMichal Koutný 		}
4870f43caa2aSMichal Koutný 		if (it->task_pos == &it->cur_cset->dying_tasks)
4871b636fd38STejun Heo 			css_task_iter_advance_css_set(it);
4872e9d81a1bSTejun Heo 	} else {
4873e9d81a1bSTejun Heo 		/* called from start, proceed to the first cset */
4874e9d81a1bSTejun Heo 		css_task_iter_advance_css_set(it);
4875e9d81a1bSTejun Heo 	}
4876bc2fb7edSTejun Heo 
4877c03cd773STejun Heo 	if (!it->task_pos)
4878c03cd773STejun Heo 		return;
4879c03cd773STejun Heo 
4880c03cd773STejun Heo 	task = list_entry(it->task_pos, struct task_struct, cg_list);
4881c03cd773STejun Heo 
4882c03cd773STejun Heo 	if (it->flags & CSS_TASK_ITER_PROCS) {
4883bc2fb7edSTejun Heo 		/* if PROCS, skip over tasks which aren't group leaders */
4884c03cd773STejun Heo 		if (!thread_group_leader(task))
4885bc2fb7edSTejun Heo 			goto repeat;
4886c03cd773STejun Heo 
4887c03cd773STejun Heo 		/* and dying leaders w/o live member threads */
4888f43caa2aSMichal Koutný 		if (it->cur_tasks_head == &it->cur_cset->dying_tasks &&
48899c974c77SMichal Koutný 		    !atomic_read(&task->signal->live))
4890c03cd773STejun Heo 			goto repeat;
4891c03cd773STejun Heo 	} else {
4892c03cd773STejun Heo 		/* skip all dying ones */
4893f43caa2aSMichal Koutný 		if (it->cur_tasks_head == &it->cur_cset->dying_tasks)
4894c03cd773STejun Heo 			goto repeat;
4895c03cd773STejun Heo 	}
4896201af4c0STejun Heo }
4897201af4c0STejun Heo 
4898201af4c0STejun Heo /**
4899201af4c0STejun Heo  * css_task_iter_start - initiate task iteration
4900201af4c0STejun Heo  * @css: the css to walk tasks of
4901bc2fb7edSTejun Heo  * @flags: CSS_TASK_ITER_* flags
4902201af4c0STejun Heo  * @it: the task iterator to use
4903201af4c0STejun Heo  *
4904201af4c0STejun Heo  * Initiate iteration through the tasks of @css.  The caller can call
4905201af4c0STejun Heo  * css_task_iter_next() to walk through the tasks until the function
4906201af4c0STejun Heo  * returns NULL.  On completion of iteration, css_task_iter_end() must be
4907201af4c0STejun Heo  * called.
4908201af4c0STejun Heo  */
css_task_iter_start(struct cgroup_subsys_state * css,unsigned int flags,struct css_task_iter * it)4909bc2fb7edSTejun Heo void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
4910201af4c0STejun Heo 			 struct css_task_iter *it)
4911201af4c0STejun Heo {
4912201af4c0STejun Heo 	memset(it, 0, sizeof(*it));
4913201af4c0STejun Heo 
4914201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
4915201af4c0STejun Heo 
4916201af4c0STejun Heo 	it->ss = css->ss;
4917bc2fb7edSTejun Heo 	it->flags = flags;
4918201af4c0STejun Heo 
4919d20d30ebSKees Cook 	if (CGROUP_HAS_SUBSYS_CONFIG && it->ss)
4920201af4c0STejun Heo 		it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4921201af4c0STejun Heo 	else
4922201af4c0STejun Heo 		it->cset_pos = &css->cgroup->cset_links;
4923201af4c0STejun Heo 
4924201af4c0STejun Heo 	it->cset_head = it->cset_pos;
4925201af4c0STejun Heo 
4926e9d81a1bSTejun Heo 	css_task_iter_advance(it);
4927201af4c0STejun Heo 
4928201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
4929201af4c0STejun Heo }
4930201af4c0STejun Heo 
4931201af4c0STejun Heo /**
4932201af4c0STejun Heo  * css_task_iter_next - return the next task for the iterator
4933201af4c0STejun Heo  * @it: the task iterator being iterated
4934201af4c0STejun Heo  *
4935201af4c0STejun Heo  * The "next" function for task iteration.  @it should have been
4936201af4c0STejun Heo  * initialized via css_task_iter_start().  Returns NULL when the iteration
4937201af4c0STejun Heo  * reaches the end.
4938201af4c0STejun Heo  */
css_task_iter_next(struct css_task_iter * it)4939201af4c0STejun Heo struct task_struct *css_task_iter_next(struct css_task_iter *it)
4940201af4c0STejun Heo {
4941201af4c0STejun Heo 	if (it->cur_task) {
4942201af4c0STejun Heo 		put_task_struct(it->cur_task);
4943201af4c0STejun Heo 		it->cur_task = NULL;
4944201af4c0STejun Heo 	}
4945201af4c0STejun Heo 
4946201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
4947201af4c0STejun Heo 
4948cee0c33cSTejun Heo 	/* @it may be half-advanced by skips, finish advancing */
4949cee0c33cSTejun Heo 	if (it->flags & CSS_TASK_ITER_SKIPPED)
4950cee0c33cSTejun Heo 		css_task_iter_advance(it);
4951cee0c33cSTejun Heo 
4952201af4c0STejun Heo 	if (it->task_pos) {
4953201af4c0STejun Heo 		it->cur_task = list_entry(it->task_pos, struct task_struct,
4954201af4c0STejun Heo 					  cg_list);
4955201af4c0STejun Heo 		get_task_struct(it->cur_task);
4956201af4c0STejun Heo 		css_task_iter_advance(it);
4957201af4c0STejun Heo 	}
4958201af4c0STejun Heo 
4959201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
4960201af4c0STejun Heo 
4961201af4c0STejun Heo 	return it->cur_task;
4962201af4c0STejun Heo }
4963201af4c0STejun Heo 
4964201af4c0STejun Heo /**
4965201af4c0STejun Heo  * css_task_iter_end - finish task iteration
4966201af4c0STejun Heo  * @it: the task iterator to finish
4967201af4c0STejun Heo  *
4968201af4c0STejun Heo  * Finish task iteration started by css_task_iter_start().
4969201af4c0STejun Heo  */
css_task_iter_end(struct css_task_iter * it)4970201af4c0STejun Heo void css_task_iter_end(struct css_task_iter *it)
4971201af4c0STejun Heo {
4972201af4c0STejun Heo 	if (it->cur_cset) {
4973201af4c0STejun Heo 		spin_lock_irq(&css_set_lock);
4974201af4c0STejun Heo 		list_del(&it->iters_node);
4975201af4c0STejun Heo 		put_css_set_locked(it->cur_cset);
4976201af4c0STejun Heo 		spin_unlock_irq(&css_set_lock);
4977201af4c0STejun Heo 	}
4978201af4c0STejun Heo 
4979450ee0c1STejun Heo 	if (it->cur_dcset)
4980450ee0c1STejun Heo 		put_css_set(it->cur_dcset);
4981450ee0c1STejun Heo 
4982201af4c0STejun Heo 	if (it->cur_task)
4983201af4c0STejun Heo 		put_task_struct(it->cur_task);
4984201af4c0STejun Heo }
4985201af4c0STejun Heo 
cgroup_procs_release(struct kernfs_open_file * of)4986201af4c0STejun Heo static void cgroup_procs_release(struct kernfs_open_file *of)
4987201af4c0STejun Heo {
49880d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
49890d2b5955STejun Heo 
49900d2b5955STejun Heo 	if (ctx->procs.started)
49910d2b5955STejun Heo 		css_task_iter_end(&ctx->procs.iter);
4992201af4c0STejun Heo }
4993201af4c0STejun Heo 
cgroup_procs_next(struct seq_file * s,void * v,loff_t * pos)4994201af4c0STejun Heo static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4995201af4c0STejun Heo {
4996201af4c0STejun Heo 	struct kernfs_open_file *of = s->private;
49970d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
4998201af4c0STejun Heo 
49992d4ecb03SVasily Averin 	if (pos)
50002d4ecb03SVasily Averin 		(*pos)++;
50012d4ecb03SVasily Averin 
50020d2b5955STejun Heo 	return css_task_iter_next(&ctx->procs.iter);
5003201af4c0STejun Heo }
5004201af4c0STejun Heo 
__cgroup_procs_start(struct seq_file * s,loff_t * pos,unsigned int iter_flags)50058cfd8147STejun Heo static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
50068cfd8147STejun Heo 				  unsigned int iter_flags)
5007201af4c0STejun Heo {
5008201af4c0STejun Heo 	struct kernfs_open_file *of = s->private;
5009201af4c0STejun Heo 	struct cgroup *cgrp = seq_css(s)->cgroup;
50100d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
50110d2b5955STejun Heo 	struct css_task_iter *it = &ctx->procs.iter;
5012201af4c0STejun Heo 
5013201af4c0STejun Heo 	/*
5014201af4c0STejun Heo 	 * When a seq_file is seeked, it's always traversed sequentially
5015201af4c0STejun Heo 	 * from position 0, so we can simply keep iterating on !0 *pos.
5016201af4c0STejun Heo 	 */
50170d2b5955STejun Heo 	if (!ctx->procs.started) {
50182d4ecb03SVasily Averin 		if (WARN_ON_ONCE((*pos)))
5019201af4c0STejun Heo 			return ERR_PTR(-EINVAL);
5020450ee0c1STejun Heo 		css_task_iter_start(&cgrp->self, iter_flags, it);
50210d2b5955STejun Heo 		ctx->procs.started = true;
50222d4ecb03SVasily Averin 	} else if (!(*pos)) {
5023201af4c0STejun Heo 		css_task_iter_end(it);
5024450ee0c1STejun Heo 		css_task_iter_start(&cgrp->self, iter_flags, it);
50252d4ecb03SVasily Averin 	} else
50262d4ecb03SVasily Averin 		return it->cur_task;
5027201af4c0STejun Heo 
5028201af4c0STejun Heo 	return cgroup_procs_next(s, NULL, NULL);
5029201af4c0STejun Heo }
5030201af4c0STejun Heo 
cgroup_procs_start(struct seq_file * s,loff_t * pos)50318cfd8147STejun Heo static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
50328cfd8147STejun Heo {
50338cfd8147STejun Heo 	struct cgroup *cgrp = seq_css(s)->cgroup;
50348cfd8147STejun Heo 
50358cfd8147STejun Heo 	/*
50368cfd8147STejun Heo 	 * All processes of a threaded subtree belong to the domain cgroup
50378cfd8147STejun Heo 	 * of the subtree.  Only threads can be distributed across the
50388cfd8147STejun Heo 	 * subtree.  Reject reads on cgroup.procs in the subtree proper.
50398cfd8147STejun Heo 	 * They're always empty anyway.
50408cfd8147STejun Heo 	 */
50418cfd8147STejun Heo 	if (cgroup_is_threaded(cgrp))
50428cfd8147STejun Heo 		return ERR_PTR(-EOPNOTSUPP);
50438cfd8147STejun Heo 
50448cfd8147STejun Heo 	return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
50458cfd8147STejun Heo 					    CSS_TASK_ITER_THREADED);
50468cfd8147STejun Heo }
50478cfd8147STejun Heo 
cgroup_procs_show(struct seq_file * s,void * v)5048201af4c0STejun Heo static int cgroup_procs_show(struct seq_file *s, void *v)
5049201af4c0STejun Heo {
5050bc2fb7edSTejun Heo 	seq_printf(s, "%d\n", task_pid_vnr(v));
5051201af4c0STejun Heo 	return 0;
5052201af4c0STejun Heo }
5053201af4c0STejun Heo 
cgroup_may_write(const struct cgroup * cgrp,struct super_block * sb)5054f3553220SChristian Brauner static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb)
5055f3553220SChristian Brauner {
5056f3553220SChristian Brauner 	int ret;
5057f3553220SChristian Brauner 	struct inode *inode;
5058f3553220SChristian Brauner 
5059f3553220SChristian Brauner 	lockdep_assert_held(&cgroup_mutex);
5060f3553220SChristian Brauner 
5061f3553220SChristian Brauner 	inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
5062f3553220SChristian Brauner 	if (!inode)
5063f3553220SChristian Brauner 		return -ENOMEM;
5064f3553220SChristian Brauner 
50654609e1f1SChristian Brauner 	ret = inode_permission(&nop_mnt_idmap, inode, MAY_WRITE);
5066f3553220SChristian Brauner 	iput(inode);
5067f3553220SChristian Brauner 	return ret;
5068f3553220SChristian Brauner }
5069f3553220SChristian Brauner 
cgroup_procs_write_permission(struct cgroup * src_cgrp,struct cgroup * dst_cgrp,struct super_block * sb,struct cgroup_namespace * ns)5070715c809dSTejun Heo static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
5071715c809dSTejun Heo 					 struct cgroup *dst_cgrp,
5072e5745764STejun Heo 					 struct super_block *sb,
5073e5745764STejun Heo 					 struct cgroup_namespace *ns)
5074715c809dSTejun Heo {
5075715c809dSTejun Heo 	struct cgroup *com_cgrp = src_cgrp;
5076715c809dSTejun Heo 	int ret;
5077715c809dSTejun Heo 
5078715c809dSTejun Heo 	lockdep_assert_held(&cgroup_mutex);
5079715c809dSTejun Heo 
5080715c809dSTejun Heo 	/* find the common ancestor */
5081715c809dSTejun Heo 	while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
5082715c809dSTejun Heo 		com_cgrp = cgroup_parent(com_cgrp);
5083715c809dSTejun Heo 
5084715c809dSTejun Heo 	/* %current should be authorized to migrate to the common ancestor */
5085f3553220SChristian Brauner 	ret = cgroup_may_write(com_cgrp, sb);
5086715c809dSTejun Heo 	if (ret)
5087715c809dSTejun Heo 		return ret;
5088715c809dSTejun Heo 
5089715c809dSTejun Heo 	/*
5090715c809dSTejun Heo 	 * If namespaces are delegation boundaries, %current must be able
5091715c809dSTejun Heo 	 * to see both source and destination cgroups from its namespace.
5092715c809dSTejun Heo 	 */
5093715c809dSTejun Heo 	if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
5094715c809dSTejun Heo 	    (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
5095715c809dSTejun Heo 	     !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
5096715c809dSTejun Heo 		return -ENOENT;
5097715c809dSTejun Heo 
5098715c809dSTejun Heo 	return 0;
5099715c809dSTejun Heo }
5100715c809dSTejun Heo 
cgroup_attach_permissions(struct cgroup * src_cgrp,struct cgroup * dst_cgrp,struct super_block * sb,bool threadgroup,struct cgroup_namespace * ns)51016df970e4SChristian Brauner static int cgroup_attach_permissions(struct cgroup *src_cgrp,
51026df970e4SChristian Brauner 				     struct cgroup *dst_cgrp,
5103e5745764STejun Heo 				     struct super_block *sb, bool threadgroup,
5104e5745764STejun Heo 				     struct cgroup_namespace *ns)
51056df970e4SChristian Brauner {
51066df970e4SChristian Brauner 	int ret = 0;
51076df970e4SChristian Brauner 
5108e5745764STejun Heo 	ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns);
51096df970e4SChristian Brauner 	if (ret)
51106df970e4SChristian Brauner 		return ret;
51116df970e4SChristian Brauner 
51126df970e4SChristian Brauner 	ret = cgroup_migrate_vet_dst(dst_cgrp);
51136df970e4SChristian Brauner 	if (ret)
51146df970e4SChristian Brauner 		return ret;
51156df970e4SChristian Brauner 
51166df970e4SChristian Brauner 	if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp))
51176df970e4SChristian Brauner 		ret = -EOPNOTSUPP;
51186df970e4SChristian Brauner 
51196df970e4SChristian Brauner 	return ret;
51206df970e4SChristian Brauner }
51216df970e4SChristian Brauner 
__cgroup_procs_write(struct kernfs_open_file * of,char * buf,bool threadgroup)5122da70862eSMichal Koutný static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
5123da70862eSMichal Koutný 				    bool threadgroup)
5124715c809dSTejun Heo {
5125e5745764STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
5126715c809dSTejun Heo 	struct cgroup *src_cgrp, *dst_cgrp;
5127715c809dSTejun Heo 	struct task_struct *task;
51281756d799STejun Heo 	const struct cred *saved_cred;
5129715c809dSTejun Heo 	ssize_t ret;
51304f7e7236STejun Heo 	bool threadgroup_locked;
5131715c809dSTejun Heo 
5132715c809dSTejun Heo 	dst_cgrp = cgroup_kn_lock_live(of->kn, false);
5133715c809dSTejun Heo 	if (!dst_cgrp)
5134715c809dSTejun Heo 		return -ENODEV;
5135715c809dSTejun Heo 
51364f7e7236STejun Heo 	task = cgroup_procs_write_start(buf, threadgroup, &threadgroup_locked);
5137715c809dSTejun Heo 	ret = PTR_ERR_OR_ZERO(task);
5138715c809dSTejun Heo 	if (ret)
5139715c809dSTejun Heo 		goto out_unlock;
5140715c809dSTejun Heo 
5141715c809dSTejun Heo 	/* find the source cgroup */
5142715c809dSTejun Heo 	spin_lock_irq(&css_set_lock);
5143715c809dSTejun Heo 	src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
5144715c809dSTejun Heo 	spin_unlock_irq(&css_set_lock);
5145715c809dSTejun Heo 
51461756d799STejun Heo 	/*
51471756d799STejun Heo 	 * Process and thread migrations follow same delegation rule. Check
51481756d799STejun Heo 	 * permissions using the credentials from file open to protect against
51491756d799STejun Heo 	 * inherited fd attacks.
51501756d799STejun Heo 	 */
51511756d799STejun Heo 	saved_cred = override_creds(of->file->f_cred);
51526df970e4SChristian Brauner 	ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
5153e5745764STejun Heo 					of->file->f_path.dentry->d_sb,
5154e5745764STejun Heo 					threadgroup, ctx->ns);
51551756d799STejun Heo 	revert_creds(saved_cred);
5156715c809dSTejun Heo 	if (ret)
5157715c809dSTejun Heo 		goto out_finish;
5158715c809dSTejun Heo 
5159da70862eSMichal Koutný 	ret = cgroup_attach_task(dst_cgrp, task, threadgroup);
5160715c809dSTejun Heo 
5161715c809dSTejun Heo out_finish:
51624f7e7236STejun Heo 	cgroup_procs_write_finish(task, threadgroup_locked);
5163715c809dSTejun Heo out_unlock:
5164715c809dSTejun Heo 	cgroup_kn_unlock(of->kn);
5165715c809dSTejun Heo 
5166da70862eSMichal Koutný 	return ret;
5167da70862eSMichal Koutný }
5168da70862eSMichal Koutný 
cgroup_procs_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)5169da70862eSMichal Koutný static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
5170da70862eSMichal Koutný 				  char *buf, size_t nbytes, loff_t off)
5171da70862eSMichal Koutný {
5172da70862eSMichal Koutný 	return __cgroup_procs_write(of, buf, true) ?: nbytes;
5173715c809dSTejun Heo }
5174715c809dSTejun Heo 
cgroup_threads_start(struct seq_file * s,loff_t * pos)51758cfd8147STejun Heo static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
51768cfd8147STejun Heo {
51778cfd8147STejun Heo 	return __cgroup_procs_start(s, pos, 0);
51788cfd8147STejun Heo }
51798cfd8147STejun Heo 
cgroup_threads_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)51808cfd8147STejun Heo static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
51818cfd8147STejun Heo 				    char *buf, size_t nbytes, loff_t off)
51828cfd8147STejun Heo {
5183da70862eSMichal Koutný 	return __cgroup_procs_write(of, buf, false) ?: nbytes;
51848cfd8147STejun Heo }
51858cfd8147STejun Heo 
5186201af4c0STejun Heo /* cgroup core interface files for the default hierarchy */
5187d62beb7fSTejun Heo static struct cftype cgroup_base_files[] = {
5188201af4c0STejun Heo 	{
51898cfd8147STejun Heo 		.name = "cgroup.type",
51908cfd8147STejun Heo 		.flags = CFTYPE_NOT_ON_ROOT,
51918cfd8147STejun Heo 		.seq_show = cgroup_type_show,
51928cfd8147STejun Heo 		.write = cgroup_type_write,
51938cfd8147STejun Heo 	},
51948cfd8147STejun Heo 	{
5195201af4c0STejun Heo 		.name = "cgroup.procs",
51965136f636STejun Heo 		.flags = CFTYPE_NS_DELEGATABLE,
5197201af4c0STejun Heo 		.file_offset = offsetof(struct cgroup, procs_file),
5198201af4c0STejun Heo 		.release = cgroup_procs_release,
5199201af4c0STejun Heo 		.seq_start = cgroup_procs_start,
5200201af4c0STejun Heo 		.seq_next = cgroup_procs_next,
5201201af4c0STejun Heo 		.seq_show = cgroup_procs_show,
5202201af4c0STejun Heo 		.write = cgroup_procs_write,
5203201af4c0STejun Heo 	},
5204201af4c0STejun Heo 	{
52058cfd8147STejun Heo 		.name = "cgroup.threads",
52064f58424dSRoman Gushchin 		.flags = CFTYPE_NS_DELEGATABLE,
52078cfd8147STejun Heo 		.release = cgroup_procs_release,
52088cfd8147STejun Heo 		.seq_start = cgroup_threads_start,
52098cfd8147STejun Heo 		.seq_next = cgroup_procs_next,
52108cfd8147STejun Heo 		.seq_show = cgroup_procs_show,
52118cfd8147STejun Heo 		.write = cgroup_threads_write,
52128cfd8147STejun Heo 	},
52138cfd8147STejun Heo 	{
5214201af4c0STejun Heo 		.name = "cgroup.controllers",
5215201af4c0STejun Heo 		.seq_show = cgroup_controllers_show,
5216201af4c0STejun Heo 	},
5217201af4c0STejun Heo 	{
5218201af4c0STejun Heo 		.name = "cgroup.subtree_control",
52195136f636STejun Heo 		.flags = CFTYPE_NS_DELEGATABLE,
5220201af4c0STejun Heo 		.seq_show = cgroup_subtree_control_show,
5221201af4c0STejun Heo 		.write = cgroup_subtree_control_write,
5222201af4c0STejun Heo 	},
5223201af4c0STejun Heo 	{
5224201af4c0STejun Heo 		.name = "cgroup.events",
5225201af4c0STejun Heo 		.flags = CFTYPE_NOT_ON_ROOT,
5226201af4c0STejun Heo 		.file_offset = offsetof(struct cgroup, events_file),
5227201af4c0STejun Heo 		.seq_show = cgroup_events_show,
5228201af4c0STejun Heo 	},
52291a926e0bSRoman Gushchin 	{
52301a926e0bSRoman Gushchin 		.name = "cgroup.max.descendants",
52311a926e0bSRoman Gushchin 		.seq_show = cgroup_max_descendants_show,
52321a926e0bSRoman Gushchin 		.write = cgroup_max_descendants_write,
52331a926e0bSRoman Gushchin 	},
52341a926e0bSRoman Gushchin 	{
52351a926e0bSRoman Gushchin 		.name = "cgroup.max.depth",
52361a926e0bSRoman Gushchin 		.seq_show = cgroup_max_depth_show,
52371a926e0bSRoman Gushchin 		.write = cgroup_max_depth_write,
52381a926e0bSRoman Gushchin 	},
5239ec39225cSRoman Gushchin 	{
5240ec39225cSRoman Gushchin 		.name = "cgroup.stat",
52413e48930cSTejun Heo 		.seq_show = cgroup_stat_show,
5242ec39225cSRoman Gushchin 	},
5243d41bf8c9STejun Heo 	{
524476f969e8SRoman Gushchin 		.name = "cgroup.freeze",
524576f969e8SRoman Gushchin 		.flags = CFTYPE_NOT_ON_ROOT,
524676f969e8SRoman Gushchin 		.seq_show = cgroup_freeze_show,
524776f969e8SRoman Gushchin 		.write = cgroup_freeze_write,
524876f969e8SRoman Gushchin 	},
524976f969e8SRoman Gushchin 	{
5250661ee628SChristian Brauner 		.name = "cgroup.kill",
5251661ee628SChristian Brauner 		.flags = CFTYPE_NOT_ON_ROOT,
5252661ee628SChristian Brauner 		.write = cgroup_kill_write,
5253661ee628SChristian Brauner 	},
5254661ee628SChristian Brauner 	{
5255d41bf8c9STejun Heo 		.name = "cpu.stat",
5256d41bf8c9STejun Heo 		.seq_show = cpu_stat_show,
5257d41bf8c9STejun Heo 	},
5258677ea015SJosh Don 	{
5259677ea015SJosh Don 		.name = "cpu.stat.local",
5260677ea015SJosh Don 		.seq_show = cpu_local_stat_show,
5261677ea015SJosh Don 	},
52628a693f77STejun Heo 	{ }	/* terminate */
52638a693f77STejun Heo };
52648a693f77STejun Heo 
52658a693f77STejun Heo static struct cftype cgroup_psi_files[] = {
52662ce7135aSJohannes Weiner #ifdef CONFIG_PSI
52672ce7135aSJohannes Weiner 	{
52682ce7135aSJohannes Weiner 		.name = "io.pressure",
526934f26a15SChengming Zhou 		.file_offset = offsetof(struct cgroup, psi_files[PSI_IO]),
52702ce7135aSJohannes Weiner 		.seq_show = cgroup_io_pressure_show,
52710e94682bSSuren Baghdasaryan 		.write = cgroup_io_pressure_write,
52720e94682bSSuren Baghdasaryan 		.poll = cgroup_pressure_poll,
52730e94682bSSuren Baghdasaryan 		.release = cgroup_pressure_release,
52742ce7135aSJohannes Weiner 	},
52752ce7135aSJohannes Weiner 	{
52762ce7135aSJohannes Weiner 		.name = "memory.pressure",
527734f26a15SChengming Zhou 		.file_offset = offsetof(struct cgroup, psi_files[PSI_MEM]),
52782ce7135aSJohannes Weiner 		.seq_show = cgroup_memory_pressure_show,
52790e94682bSSuren Baghdasaryan 		.write = cgroup_memory_pressure_write,
52800e94682bSSuren Baghdasaryan 		.poll = cgroup_pressure_poll,
52810e94682bSSuren Baghdasaryan 		.release = cgroup_pressure_release,
52822ce7135aSJohannes Weiner 	},
52832ce7135aSJohannes Weiner 	{
52842ce7135aSJohannes Weiner 		.name = "cpu.pressure",
528534f26a15SChengming Zhou 		.file_offset = offsetof(struct cgroup, psi_files[PSI_CPU]),
52862ce7135aSJohannes Weiner 		.seq_show = cgroup_cpu_pressure_show,
52870e94682bSSuren Baghdasaryan 		.write = cgroup_cpu_pressure_write,
52880e94682bSSuren Baghdasaryan 		.poll = cgroup_pressure_poll,
52890e94682bSSuren Baghdasaryan 		.release = cgroup_pressure_release,
52902ce7135aSJohannes Weiner 	},
529152b1364bSChengming Zhou #ifdef CONFIG_IRQ_TIME_ACCOUNTING
529252b1364bSChengming Zhou 	{
529352b1364bSChengming Zhou 		.name = "irq.pressure",
529434f26a15SChengming Zhou 		.file_offset = offsetof(struct cgroup, psi_files[PSI_IRQ]),
529552b1364bSChengming Zhou 		.seq_show = cgroup_irq_pressure_show,
529652b1364bSChengming Zhou 		.write = cgroup_irq_pressure_write,
529752b1364bSChengming Zhou 		.poll = cgroup_pressure_poll,
529852b1364bSChengming Zhou 		.release = cgroup_pressure_release,
529952b1364bSChengming Zhou 	},
530052b1364bSChengming Zhou #endif
530134f26a15SChengming Zhou 	{
530234f26a15SChengming Zhou 		.name = "cgroup.pressure",
530334f26a15SChengming Zhou 		.seq_show = cgroup_pressure_show,
530434f26a15SChengming Zhou 		.write = cgroup_pressure_write,
530534f26a15SChengming Zhou 	},
53060e94682bSSuren Baghdasaryan #endif /* CONFIG_PSI */
5307201af4c0STejun Heo 	{ }	/* terminate */
5308201af4c0STejun Heo };
5309201af4c0STejun Heo 
5310201af4c0STejun Heo /*
5311201af4c0STejun Heo  * css destruction is four-stage process.
5312201af4c0STejun Heo  *
5313201af4c0STejun Heo  * 1. Destruction starts.  Killing of the percpu_ref is initiated.
5314201af4c0STejun Heo  *    Implemented in kill_css().
5315201af4c0STejun Heo  *
5316201af4c0STejun Heo  * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
5317201af4c0STejun Heo  *    and thus css_tryget_online() is guaranteed to fail, the css can be
5318201af4c0STejun Heo  *    offlined by invoking offline_css().  After offlining, the base ref is
5319201af4c0STejun Heo  *    put.  Implemented in css_killed_work_fn().
5320201af4c0STejun Heo  *
5321201af4c0STejun Heo  * 3. When the percpu_ref reaches zero, the only possible remaining
5322201af4c0STejun Heo  *    accessors are inside RCU read sections.  css_release() schedules the
5323201af4c0STejun Heo  *    RCU callback.
5324201af4c0STejun Heo  *
5325201af4c0STejun Heo  * 4. After the grace period, the css can be freed.  Implemented in
5326a2c15fecSMiaohe Lin  *    css_free_rwork_fn().
5327201af4c0STejun Heo  *
5328201af4c0STejun Heo  * It is actually hairier because both step 2 and 4 require process context
5329201af4c0STejun Heo  * and thus involve punting to css->destroy_work adding two additional
5330201af4c0STejun Heo  * steps to the already complex sequence.
5331201af4c0STejun Heo  */
css_free_rwork_fn(struct work_struct * work)53328f36aaecSTejun Heo static void css_free_rwork_fn(struct work_struct *work)
5333201af4c0STejun Heo {
53348f36aaecSTejun Heo 	struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
53358f36aaecSTejun Heo 				struct cgroup_subsys_state, destroy_rwork);
5336201af4c0STejun Heo 	struct cgroup_subsys *ss = css->ss;
5337201af4c0STejun Heo 	struct cgroup *cgrp = css->cgroup;
5338201af4c0STejun Heo 
5339201af4c0STejun Heo 	percpu_ref_exit(&css->refcnt);
5340201af4c0STejun Heo 
5341201af4c0STejun Heo 	if (ss) {
5342201af4c0STejun Heo 		/* css free path */
5343201af4c0STejun Heo 		struct cgroup_subsys_state *parent = css->parent;
5344201af4c0STejun Heo 		int id = css->id;
5345201af4c0STejun Heo 
5346201af4c0STejun Heo 		ss->css_free(css);
5347201af4c0STejun Heo 		cgroup_idr_remove(&ss->css_idr, id);
5348201af4c0STejun Heo 		cgroup_put(cgrp);
5349201af4c0STejun Heo 
5350201af4c0STejun Heo 		if (parent)
5351201af4c0STejun Heo 			css_put(parent);
5352201af4c0STejun Heo 	} else {
5353201af4c0STejun Heo 		/* cgroup free path */
5354201af4c0STejun Heo 		atomic_dec(&cgrp->root->nr_cgrps);
5355d62beb7fSTejun Heo 		cgroup1_pidlist_destroy_all(cgrp);
5356201af4c0STejun Heo 		cancel_work_sync(&cgrp->release_agent_work);
5357c4bcfb38SYonghong Song 		bpf_cgrp_storage_free(cgrp);
5358201af4c0STejun Heo 
5359201af4c0STejun Heo 		if (cgroup_parent(cgrp)) {
5360201af4c0STejun Heo 			/*
5361201af4c0STejun Heo 			 * We get a ref to the parent, and put the ref when
5362201af4c0STejun Heo 			 * this cgroup is being freed, so it's guaranteed
5363201af4c0STejun Heo 			 * that the parent won't be destroyed before its
5364201af4c0STejun Heo 			 * children.
5365201af4c0STejun Heo 			 */
5366201af4c0STejun Heo 			cgroup_put(cgroup_parent(cgrp));
5367201af4c0STejun Heo 			kernfs_put(cgrp->kn);
53682ce7135aSJohannes Weiner 			psi_cgroup_free(cgrp);
5369c58632b3STejun Heo 			cgroup_rstat_exit(cgrp);
5370201af4c0STejun Heo 			kfree(cgrp);
5371201af4c0STejun Heo 		} else {
5372201af4c0STejun Heo 			/*
5373201af4c0STejun Heo 			 * This is root cgroup's refcnt reaching zero,
5374201af4c0STejun Heo 			 * which indicates that the root should be
5375201af4c0STejun Heo 			 * released.
5376201af4c0STejun Heo 			 */
5377201af4c0STejun Heo 			cgroup_destroy_root(cgrp->root);
5378201af4c0STejun Heo 		}
5379201af4c0STejun Heo 	}
5380201af4c0STejun Heo }
5381201af4c0STejun Heo 
css_release_work_fn(struct work_struct * work)5382201af4c0STejun Heo static void css_release_work_fn(struct work_struct *work)
5383201af4c0STejun Heo {
5384201af4c0STejun Heo 	struct cgroup_subsys_state *css =
5385201af4c0STejun Heo 		container_of(work, struct cgroup_subsys_state, destroy_work);
5386201af4c0STejun Heo 	struct cgroup_subsys *ss = css->ss;
5387201af4c0STejun Heo 	struct cgroup *cgrp = css->cgroup;
5388201af4c0STejun Heo 
53894cdb91b0SKamalesh Babulal 	cgroup_lock();
5390201af4c0STejun Heo 
5391201af4c0STejun Heo 	css->flags |= CSS_RELEASED;
5392201af4c0STejun Heo 	list_del_rcu(&css->sibling);
5393201af4c0STejun Heo 
5394201af4c0STejun Heo 	if (ss) {
5395201af4c0STejun Heo 		/* css release path */
53968f53470bSTejun Heo 		if (!list_empty(&css->rstat_css_node)) {
53978f53470bSTejun Heo 			cgroup_rstat_flush(cgrp);
53988f53470bSTejun Heo 			list_del_rcu(&css->rstat_css_node);
53998f53470bSTejun Heo 		}
54008f53470bSTejun Heo 
5401201af4c0STejun Heo 		cgroup_idr_replace(&ss->css_idr, NULL, css->id);
5402201af4c0STejun Heo 		if (ss->css_released)
5403201af4c0STejun Heo 			ss->css_released(css);
5404201af4c0STejun Heo 	} else {
54050679dee0SRoman Gushchin 		struct cgroup *tcgrp;
54060679dee0SRoman Gushchin 
5407201af4c0STejun Heo 		/* cgroup release path */
5408e4f8d81cSSteven Rostedt (VMware) 		TRACE_CGROUP_PATH(release, cgrp);
5409201af4c0STejun Heo 
5410c58632b3STejun Heo 		cgroup_rstat_flush(cgrp);
5411041cd640STejun Heo 
54124dcabeceSRoman Gushchin 		spin_lock_irq(&css_set_lock);
54130679dee0SRoman Gushchin 		for (tcgrp = cgroup_parent(cgrp); tcgrp;
54140679dee0SRoman Gushchin 		     tcgrp = cgroup_parent(tcgrp))
54150679dee0SRoman Gushchin 			tcgrp->nr_dying_descendants--;
54164dcabeceSRoman Gushchin 		spin_unlock_irq(&css_set_lock);
54170679dee0SRoman Gushchin 
5418201af4c0STejun Heo 		/*
5419201af4c0STejun Heo 		 * There are two control paths which try to determine
5420201af4c0STejun Heo 		 * cgroup from dentry without going through kernfs -
5421201af4c0STejun Heo 		 * cgroupstats_build() and css_tryget_online_from_dir().
5422201af4c0STejun Heo 		 * Those are supported by RCU protecting clearing of
5423201af4c0STejun Heo 		 * cgrp->kn->priv backpointer.
5424201af4c0STejun Heo 		 */
5425201af4c0STejun Heo 		if (cgrp->kn)
5426201af4c0STejun Heo 			RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
5427201af4c0STejun Heo 					 NULL);
5428201af4c0STejun Heo 	}
5429201af4c0STejun Heo 
54304cdb91b0SKamalesh Babulal 	cgroup_unlock();
5431201af4c0STejun Heo 
54328f36aaecSTejun Heo 	INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
54338f36aaecSTejun Heo 	queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
5434201af4c0STejun Heo }
5435201af4c0STejun Heo 
css_release(struct percpu_ref * ref)5436201af4c0STejun Heo static void css_release(struct percpu_ref *ref)
5437201af4c0STejun Heo {
5438201af4c0STejun Heo 	struct cgroup_subsys_state *css =
5439201af4c0STejun Heo 		container_of(ref, struct cgroup_subsys_state, refcnt);
5440201af4c0STejun Heo 
5441201af4c0STejun Heo 	INIT_WORK(&css->destroy_work, css_release_work_fn);
5442201af4c0STejun Heo 	queue_work(cgroup_destroy_wq, &css->destroy_work);
5443201af4c0STejun Heo }
5444201af4c0STejun Heo 
init_and_link_css(struct cgroup_subsys_state * css,struct cgroup_subsys * ss,struct cgroup * cgrp)5445201af4c0STejun Heo static void init_and_link_css(struct cgroup_subsys_state *css,
5446201af4c0STejun Heo 			      struct cgroup_subsys *ss, struct cgroup *cgrp)
5447201af4c0STejun Heo {
5448201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
5449201af4c0STejun Heo 
5450a590b90dSTejun Heo 	cgroup_get_live(cgrp);
5451201af4c0STejun Heo 
5452201af4c0STejun Heo 	memset(css, 0, sizeof(*css));
5453201af4c0STejun Heo 	css->cgroup = cgrp;
5454201af4c0STejun Heo 	css->ss = ss;
5455201af4c0STejun Heo 	css->id = -1;
5456201af4c0STejun Heo 	INIT_LIST_HEAD(&css->sibling);
5457201af4c0STejun Heo 	INIT_LIST_HEAD(&css->children);
54588f53470bSTejun Heo 	INIT_LIST_HEAD(&css->rstat_css_node);
5459201af4c0STejun Heo 	css->serial_nr = css_serial_nr_next++;
5460201af4c0STejun Heo 	atomic_set(&css->online_cnt, 0);
5461201af4c0STejun Heo 
5462201af4c0STejun Heo 	if (cgroup_parent(cgrp)) {
5463201af4c0STejun Heo 		css->parent = cgroup_css(cgroup_parent(cgrp), ss);
5464201af4c0STejun Heo 		css_get(css->parent);
5465201af4c0STejun Heo 	}
5466201af4c0STejun Heo 
5467a7df69b8SJohannes Weiner 	if (ss->css_rstat_flush)
54688f53470bSTejun Heo 		list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
54698f53470bSTejun Heo 
5470201af4c0STejun Heo 	BUG_ON(cgroup_css(cgrp, ss));
5471201af4c0STejun Heo }
5472201af4c0STejun Heo 
5473201af4c0STejun Heo /* invoke ->css_online() on a new CSS and mark it online if successful */
online_css(struct cgroup_subsys_state * css)5474201af4c0STejun Heo static int online_css(struct cgroup_subsys_state *css)
5475201af4c0STejun Heo {
5476201af4c0STejun Heo 	struct cgroup_subsys *ss = css->ss;
5477201af4c0STejun Heo 	int ret = 0;
5478201af4c0STejun Heo 
5479201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
5480201af4c0STejun Heo 
5481201af4c0STejun Heo 	if (ss->css_online)
5482201af4c0STejun Heo 		ret = ss->css_online(css);
5483201af4c0STejun Heo 	if (!ret) {
5484201af4c0STejun Heo 		css->flags |= CSS_ONLINE;
5485201af4c0STejun Heo 		rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
5486201af4c0STejun Heo 
5487201af4c0STejun Heo 		atomic_inc(&css->online_cnt);
5488201af4c0STejun Heo 		if (css->parent)
5489201af4c0STejun Heo 			atomic_inc(&css->parent->online_cnt);
5490201af4c0STejun Heo 	}
5491201af4c0STejun Heo 	return ret;
5492201af4c0STejun Heo }
5493201af4c0STejun Heo 
5494201af4c0STejun Heo /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
offline_css(struct cgroup_subsys_state * css)5495201af4c0STejun Heo static void offline_css(struct cgroup_subsys_state *css)
5496201af4c0STejun Heo {
5497201af4c0STejun Heo 	struct cgroup_subsys *ss = css->ss;
5498201af4c0STejun Heo 
5499201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
5500201af4c0STejun Heo 
5501201af4c0STejun Heo 	if (!(css->flags & CSS_ONLINE))
5502201af4c0STejun Heo 		return;
5503201af4c0STejun Heo 
5504201af4c0STejun Heo 	if (ss->css_offline)
5505201af4c0STejun Heo 		ss->css_offline(css);
5506201af4c0STejun Heo 
5507201af4c0STejun Heo 	css->flags &= ~CSS_ONLINE;
5508201af4c0STejun Heo 	RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
5509201af4c0STejun Heo 
5510201af4c0STejun Heo 	wake_up_all(&css->cgroup->offline_waitq);
5511201af4c0STejun Heo }
5512201af4c0STejun Heo 
5513201af4c0STejun Heo /**
5514201af4c0STejun Heo  * css_create - create a cgroup_subsys_state
5515201af4c0STejun Heo  * @cgrp: the cgroup new css will be associated with
5516201af4c0STejun Heo  * @ss: the subsys of new css
5517201af4c0STejun Heo  *
5518201af4c0STejun Heo  * Create a new css associated with @cgrp - @ss pair.  On success, the new
5519201af4c0STejun Heo  * css is online and installed in @cgrp.  This function doesn't create the
5520201af4c0STejun Heo  * interface files.  Returns 0 on success, -errno on failure.
5521201af4c0STejun Heo  */
css_create(struct cgroup * cgrp,struct cgroup_subsys * ss)5522201af4c0STejun Heo static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
5523201af4c0STejun Heo 					      struct cgroup_subsys *ss)
5524201af4c0STejun Heo {
5525201af4c0STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
5526201af4c0STejun Heo 	struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
5527201af4c0STejun Heo 	struct cgroup_subsys_state *css;
5528201af4c0STejun Heo 	int err;
5529201af4c0STejun Heo 
5530201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
5531201af4c0STejun Heo 
5532201af4c0STejun Heo 	css = ss->css_alloc(parent_css);
5533201af4c0STejun Heo 	if (!css)
5534201af4c0STejun Heo 		css = ERR_PTR(-ENOMEM);
5535201af4c0STejun Heo 	if (IS_ERR(css))
5536201af4c0STejun Heo 		return css;
5537201af4c0STejun Heo 
5538201af4c0STejun Heo 	init_and_link_css(css, ss, cgrp);
5539201af4c0STejun Heo 
5540201af4c0STejun Heo 	err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
5541201af4c0STejun Heo 	if (err)
5542201af4c0STejun Heo 		goto err_free_css;
5543201af4c0STejun Heo 
5544201af4c0STejun Heo 	err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
5545201af4c0STejun Heo 	if (err < 0)
5546201af4c0STejun Heo 		goto err_free_css;
5547201af4c0STejun Heo 	css->id = err;
5548201af4c0STejun Heo 
5549201af4c0STejun Heo 	/* @css is ready to be brought online now, make it visible */
5550201af4c0STejun Heo 	list_add_tail_rcu(&css->sibling, &parent_css->children);
5551201af4c0STejun Heo 	cgroup_idr_replace(&ss->css_idr, css, css->id);
5552201af4c0STejun Heo 
5553201af4c0STejun Heo 	err = online_css(css);
5554201af4c0STejun Heo 	if (err)
5555201af4c0STejun Heo 		goto err_list_del;
5556201af4c0STejun Heo 
5557201af4c0STejun Heo 	return css;
5558201af4c0STejun Heo 
5559201af4c0STejun Heo err_list_del:
5560201af4c0STejun Heo 	list_del_rcu(&css->sibling);
5561201af4c0STejun Heo err_free_css:
55628f53470bSTejun Heo 	list_del_rcu(&css->rstat_css_node);
55638f36aaecSTejun Heo 	INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
55648f36aaecSTejun Heo 	queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
5565201af4c0STejun Heo 	return ERR_PTR(err);
5566201af4c0STejun Heo }
5567201af4c0STejun Heo 
5568bdf3d06bSTejun Heo /*
5569bdf3d06bSTejun Heo  * The returned cgroup is fully initialized including its control mask, but
5570a3fdeeb3SMiaohe Lin  * it doesn't have the control mask applied.
5571bdf3d06bSTejun Heo  */
cgroup_create(struct cgroup * parent,const char * name,umode_t mode)557274321038STejun Heo static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
557374321038STejun Heo 				    umode_t mode)
5574201af4c0STejun Heo {
5575201af4c0STejun Heo 	struct cgroup_root *root = parent->root;
5576201af4c0STejun Heo 	struct cgroup *cgrp, *tcgrp;
557774321038STejun Heo 	struct kernfs_node *kn;
5578201af4c0STejun Heo 	int level = parent->level + 1;
5579201af4c0STejun Heo 	int ret;
5580201af4c0STejun Heo 
5581201af4c0STejun Heo 	/* allocate the cgroup and its ID, 0 is reserved for the root */
55827f203bc8STejun Heo 	cgrp = kzalloc(struct_size(cgrp, ancestors, (level + 1)), GFP_KERNEL);
5583201af4c0STejun Heo 	if (!cgrp)
5584201af4c0STejun Heo 		return ERR_PTR(-ENOMEM);
5585201af4c0STejun Heo 
5586201af4c0STejun Heo 	ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
5587201af4c0STejun Heo 	if (ret)
5588201af4c0STejun Heo 		goto out_free_cgrp;
5589201af4c0STejun Heo 
5590c58632b3STejun Heo 	ret = cgroup_rstat_init(cgrp);
5591041cd640STejun Heo 	if (ret)
5592041cd640STejun Heo 		goto out_cancel_ref;
5593041cd640STejun Heo 
559474321038STejun Heo 	/* create the directory */
559574321038STejun Heo 	kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
559674321038STejun Heo 	if (IS_ERR(kn)) {
559774321038STejun Heo 		ret = PTR_ERR(kn);
5598041cd640STejun Heo 		goto out_stat_exit;
5599201af4c0STejun Heo 	}
560074321038STejun Heo 	cgrp->kn = kn;
5601201af4c0STejun Heo 
5602201af4c0STejun Heo 	init_cgroup_housekeeping(cgrp);
5603201af4c0STejun Heo 
5604201af4c0STejun Heo 	cgrp->self.parent = &parent->self;
5605201af4c0STejun Heo 	cgrp->root = root;
5606201af4c0STejun Heo 	cgrp->level = level;
56072ce7135aSJohannes Weiner 
56082ce7135aSJohannes Weiner 	ret = psi_cgroup_alloc(cgrp);
5609324bda9eSAlexei Starovoitov 	if (ret)
561074321038STejun Heo 		goto out_kernfs_remove;
5611201af4c0STejun Heo 
56122ce7135aSJohannes Weiner 	ret = cgroup_bpf_inherit(cgrp);
56132ce7135aSJohannes Weiner 	if (ret)
56142ce7135aSJohannes Weiner 		goto out_psi_free;
56152ce7135aSJohannes Weiner 
561676f969e8SRoman Gushchin 	/*
561776f969e8SRoman Gushchin 	 * New cgroup inherits effective freeze counter, and
561876f969e8SRoman Gushchin 	 * if the parent has to be frozen, the child has too.
561976f969e8SRoman Gushchin 	 */
562076f969e8SRoman Gushchin 	cgrp->freezer.e_freeze = parent->freezer.e_freeze;
562197a61369SRoman Gushchin 	if (cgrp->freezer.e_freeze) {
562297a61369SRoman Gushchin 		/*
562397a61369SRoman Gushchin 		 * Set the CGRP_FREEZE flag, so when a process will be
562497a61369SRoman Gushchin 		 * attached to the child cgroup, it will become frozen.
562597a61369SRoman Gushchin 		 * At this point the new cgroup is unpopulated, so we can
562697a61369SRoman Gushchin 		 * consider it frozen immediately.
562797a61369SRoman Gushchin 		 */
562897a61369SRoman Gushchin 		set_bit(CGRP_FREEZE, &cgrp->flags);
562976f969e8SRoman Gushchin 		set_bit(CGRP_FROZEN, &cgrp->flags);
563097a61369SRoman Gushchin 	}
563176f969e8SRoman Gushchin 
56324dcabeceSRoman Gushchin 	spin_lock_irq(&css_set_lock);
56330679dee0SRoman Gushchin 	for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
56347f203bc8STejun Heo 		cgrp->ancestors[tcgrp->level] = tcgrp;
5635201af4c0STejun Heo 
563676f969e8SRoman Gushchin 		if (tcgrp != cgrp) {
56370679dee0SRoman Gushchin 			tcgrp->nr_descendants++;
563876f969e8SRoman Gushchin 
563976f969e8SRoman Gushchin 			/*
564076f969e8SRoman Gushchin 			 * If the new cgroup is frozen, all ancestor cgroups
564176f969e8SRoman Gushchin 			 * get a new frozen descendant, but their state can't
564276f969e8SRoman Gushchin 			 * change because of this.
564376f969e8SRoman Gushchin 			 */
564476f969e8SRoman Gushchin 			if (cgrp->freezer.e_freeze)
564576f969e8SRoman Gushchin 				tcgrp->freezer.nr_frozen_descendants++;
564676f969e8SRoman Gushchin 		}
56470679dee0SRoman Gushchin 	}
56484dcabeceSRoman Gushchin 	spin_unlock_irq(&css_set_lock);
56490679dee0SRoman Gushchin 
5650201af4c0STejun Heo 	if (notify_on_release(parent))
5651201af4c0STejun Heo 		set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
5652201af4c0STejun Heo 
5653201af4c0STejun Heo 	if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
5654201af4c0STejun Heo 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
5655201af4c0STejun Heo 
5656201af4c0STejun Heo 	cgrp->self.serial_nr = css_serial_nr_next++;
5657201af4c0STejun Heo 
5658201af4c0STejun Heo 	/* allocation complete, commit to creation */
5659201af4c0STejun Heo 	list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
5660201af4c0STejun Heo 	atomic_inc(&root->nr_cgrps);
5661a590b90dSTejun Heo 	cgroup_get_live(parent);
5662201af4c0STejun Heo 
5663201af4c0STejun Heo 	/*
5664201af4c0STejun Heo 	 * On the default hierarchy, a child doesn't automatically inherit
5665201af4c0STejun Heo 	 * subtree_control from the parent.  Each is configured manually.
5666201af4c0STejun Heo 	 */
5667201af4c0STejun Heo 	if (!cgroup_on_dfl(cgrp))
5668201af4c0STejun Heo 		cgrp->subtree_control = cgroup_control(cgrp);
5669201af4c0STejun Heo 
5670201af4c0STejun Heo 	cgroup_propagate_control(cgrp);
5671201af4c0STejun Heo 
5672201af4c0STejun Heo 	return cgrp;
5673201af4c0STejun Heo 
56742ce7135aSJohannes Weiner out_psi_free:
56752ce7135aSJohannes Weiner 	psi_cgroup_free(cgrp);
567674321038STejun Heo out_kernfs_remove:
567774321038STejun Heo 	kernfs_remove(cgrp->kn);
5678041cd640STejun Heo out_stat_exit:
5679c58632b3STejun Heo 	cgroup_rstat_exit(cgrp);
5680201af4c0STejun Heo out_cancel_ref:
5681201af4c0STejun Heo 	percpu_ref_exit(&cgrp->self.refcnt);
5682201af4c0STejun Heo out_free_cgrp:
5683201af4c0STejun Heo 	kfree(cgrp);
5684201af4c0STejun Heo 	return ERR_PTR(ret);
5685201af4c0STejun Heo }
5686201af4c0STejun Heo 
cgroup_check_hierarchy_limits(struct cgroup * parent)56871a926e0bSRoman Gushchin static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
56881a926e0bSRoman Gushchin {
56891a926e0bSRoman Gushchin 	struct cgroup *cgroup;
56901a926e0bSRoman Gushchin 	int ret = false;
56911a926e0bSRoman Gushchin 	int level = 1;
56921a926e0bSRoman Gushchin 
56931a926e0bSRoman Gushchin 	lockdep_assert_held(&cgroup_mutex);
56941a926e0bSRoman Gushchin 
56951a926e0bSRoman Gushchin 	for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
56961a926e0bSRoman Gushchin 		if (cgroup->nr_descendants >= cgroup->max_descendants)
56971a926e0bSRoman Gushchin 			goto fail;
56981a926e0bSRoman Gushchin 
56991a926e0bSRoman Gushchin 		if (level > cgroup->max_depth)
57001a926e0bSRoman Gushchin 			goto fail;
57011a926e0bSRoman Gushchin 
57021a926e0bSRoman Gushchin 		level++;
57031a926e0bSRoman Gushchin 	}
57041a926e0bSRoman Gushchin 
57051a926e0bSRoman Gushchin 	ret = true;
57061a926e0bSRoman Gushchin fail:
57071a926e0bSRoman Gushchin 	return ret;
57081a926e0bSRoman Gushchin }
57091a926e0bSRoman Gushchin 
cgroup_mkdir(struct kernfs_node * parent_kn,const char * name,umode_t mode)57101592c9b2STejun Heo int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
5711201af4c0STejun Heo {
5712201af4c0STejun Heo 	struct cgroup *parent, *cgrp;
5713201af4c0STejun Heo 	int ret;
5714201af4c0STejun Heo 
5715201af4c0STejun Heo 	/* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5716201af4c0STejun Heo 	if (strchr(name, '\n'))
5717201af4c0STejun Heo 		return -EINVAL;
5718201af4c0STejun Heo 
5719201af4c0STejun Heo 	parent = cgroup_kn_lock_live(parent_kn, false);
5720201af4c0STejun Heo 	if (!parent)
5721201af4c0STejun Heo 		return -ENODEV;
5722201af4c0STejun Heo 
57231a926e0bSRoman Gushchin 	if (!cgroup_check_hierarchy_limits(parent)) {
57241a926e0bSRoman Gushchin 		ret = -EAGAIN;
57251a926e0bSRoman Gushchin 		goto out_unlock;
57261a926e0bSRoman Gushchin 	}
57271a926e0bSRoman Gushchin 
572874321038STejun Heo 	cgrp = cgroup_create(parent, name, mode);
5729201af4c0STejun Heo 	if (IS_ERR(cgrp)) {
5730201af4c0STejun Heo 		ret = PTR_ERR(cgrp);
5731201af4c0STejun Heo 		goto out_unlock;
5732201af4c0STejun Heo 	}
5733201af4c0STejun Heo 
5734201af4c0STejun Heo 	/*
5735201af4c0STejun Heo 	 * This extra ref will be put in cgroup_free_fn() and guarantees
5736201af4c0STejun Heo 	 * that @cgrp->kn is always accessible.
5737201af4c0STejun Heo 	 */
573874321038STejun Heo 	kernfs_get(cgrp->kn);
5739201af4c0STejun Heo 
574074321038STejun Heo 	ret = cgroup_kn_set_ugid(cgrp->kn);
5741201af4c0STejun Heo 	if (ret)
5742201af4c0STejun Heo 		goto out_destroy;
5743201af4c0STejun Heo 
5744201af4c0STejun Heo 	ret = css_populate_dir(&cgrp->self);
5745201af4c0STejun Heo 	if (ret)
5746201af4c0STejun Heo 		goto out_destroy;
5747201af4c0STejun Heo 
5748201af4c0STejun Heo 	ret = cgroup_apply_control_enable(cgrp);
5749201af4c0STejun Heo 	if (ret)
5750201af4c0STejun Heo 		goto out_destroy;
5751201af4c0STejun Heo 
5752e4f8d81cSSteven Rostedt (VMware) 	TRACE_CGROUP_PATH(mkdir, cgrp);
5753201af4c0STejun Heo 
5754201af4c0STejun Heo 	/* let's create and online css's */
575574321038STejun Heo 	kernfs_activate(cgrp->kn);
5756201af4c0STejun Heo 
5757201af4c0STejun Heo 	ret = 0;
5758201af4c0STejun Heo 	goto out_unlock;
5759201af4c0STejun Heo 
5760201af4c0STejun Heo out_destroy:
5761201af4c0STejun Heo 	cgroup_destroy_locked(cgrp);
5762201af4c0STejun Heo out_unlock:
5763201af4c0STejun Heo 	cgroup_kn_unlock(parent_kn);
5764201af4c0STejun Heo 	return ret;
5765201af4c0STejun Heo }
5766201af4c0STejun Heo 
5767201af4c0STejun Heo /*
5768201af4c0STejun Heo  * This is called when the refcnt of a css is confirmed to be killed.
5769201af4c0STejun Heo  * css_tryget_online() is now guaranteed to fail.  Tell the subsystem to
577008b2b6fdSZhen Lei  * initiate destruction and put the css ref from kill_css().
5771201af4c0STejun Heo  */
css_killed_work_fn(struct work_struct * work)5772201af4c0STejun Heo static void css_killed_work_fn(struct work_struct *work)
5773201af4c0STejun Heo {
5774201af4c0STejun Heo 	struct cgroup_subsys_state *css =
5775201af4c0STejun Heo 		container_of(work, struct cgroup_subsys_state, destroy_work);
5776201af4c0STejun Heo 
57774cdb91b0SKamalesh Babulal 	cgroup_lock();
5778201af4c0STejun Heo 
5779201af4c0STejun Heo 	do {
5780201af4c0STejun Heo 		offline_css(css);
5781201af4c0STejun Heo 		css_put(css);
5782201af4c0STejun Heo 		/* @css can't go away while we're holding cgroup_mutex */
5783201af4c0STejun Heo 		css = css->parent;
5784201af4c0STejun Heo 	} while (css && atomic_dec_and_test(&css->online_cnt));
5785201af4c0STejun Heo 
57864cdb91b0SKamalesh Babulal 	cgroup_unlock();
5787201af4c0STejun Heo }
5788201af4c0STejun Heo 
5789201af4c0STejun Heo /* css kill confirmation processing requires process context, bounce */
css_killed_ref_fn(struct percpu_ref * ref)5790201af4c0STejun Heo static void css_killed_ref_fn(struct percpu_ref *ref)
5791201af4c0STejun Heo {
5792201af4c0STejun Heo 	struct cgroup_subsys_state *css =
5793201af4c0STejun Heo 		container_of(ref, struct cgroup_subsys_state, refcnt);
5794201af4c0STejun Heo 
5795201af4c0STejun Heo 	if (atomic_dec_and_test(&css->online_cnt)) {
5796201af4c0STejun Heo 		INIT_WORK(&css->destroy_work, css_killed_work_fn);
5797201af4c0STejun Heo 		queue_work(cgroup_destroy_wq, &css->destroy_work);
5798201af4c0STejun Heo 	}
5799201af4c0STejun Heo }
5800201af4c0STejun Heo 
5801201af4c0STejun Heo /**
5802201af4c0STejun Heo  * kill_css - destroy a css
5803201af4c0STejun Heo  * @css: css to destroy
5804201af4c0STejun Heo  *
5805201af4c0STejun Heo  * This function initiates destruction of @css by removing cgroup interface
5806201af4c0STejun Heo  * files and putting its base reference.  ->css_offline() will be invoked
5807201af4c0STejun Heo  * asynchronously once css_tryget_online() is guaranteed to fail and when
5808201af4c0STejun Heo  * the reference count reaches zero, @css will be released.
5809201af4c0STejun Heo  */
kill_css(struct cgroup_subsys_state * css)5810201af4c0STejun Heo static void kill_css(struct cgroup_subsys_state *css)
5811201af4c0STejun Heo {
5812201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
5813201af4c0STejun Heo 
581433c35aa4SWaiman Long 	if (css->flags & CSS_DYING)
581533c35aa4SWaiman Long 		return;
581633c35aa4SWaiman Long 
581733c35aa4SWaiman Long 	css->flags |= CSS_DYING;
581833c35aa4SWaiman Long 
5819201af4c0STejun Heo 	/*
5820201af4c0STejun Heo 	 * This must happen before css is disassociated with its cgroup.
5821201af4c0STejun Heo 	 * See seq_css() for details.
5822201af4c0STejun Heo 	 */
5823201af4c0STejun Heo 	css_clear_dir(css);
5824201af4c0STejun Heo 
5825201af4c0STejun Heo 	/*
5826201af4c0STejun Heo 	 * Killing would put the base ref, but we need to keep it alive
5827201af4c0STejun Heo 	 * until after ->css_offline().
5828201af4c0STejun Heo 	 */
5829201af4c0STejun Heo 	css_get(css);
5830201af4c0STejun Heo 
5831201af4c0STejun Heo 	/*
5832201af4c0STejun Heo 	 * cgroup core guarantees that, by the time ->css_offline() is
5833201af4c0STejun Heo 	 * invoked, no new css reference will be given out via
5834201af4c0STejun Heo 	 * css_tryget_online().  We can't simply call percpu_ref_kill() and
5835201af4c0STejun Heo 	 * proceed to offlining css's because percpu_ref_kill() doesn't
5836201af4c0STejun Heo 	 * guarantee that the ref is seen as killed on all CPUs on return.
5837201af4c0STejun Heo 	 *
5838201af4c0STejun Heo 	 * Use percpu_ref_kill_and_confirm() to get notifications as each
5839201af4c0STejun Heo 	 * css is confirmed to be seen as killed on all CPUs.
5840201af4c0STejun Heo 	 */
5841201af4c0STejun Heo 	percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
5842201af4c0STejun Heo }
5843201af4c0STejun Heo 
5844201af4c0STejun Heo /**
5845201af4c0STejun Heo  * cgroup_destroy_locked - the first stage of cgroup destruction
5846201af4c0STejun Heo  * @cgrp: cgroup to be destroyed
5847201af4c0STejun Heo  *
5848201af4c0STejun Heo  * css's make use of percpu refcnts whose killing latency shouldn't be
5849201af4c0STejun Heo  * exposed to userland and are RCU protected.  Also, cgroup core needs to
5850201af4c0STejun Heo  * guarantee that css_tryget_online() won't succeed by the time
5851201af4c0STejun Heo  * ->css_offline() is invoked.  To satisfy all the requirements,
5852201af4c0STejun Heo  * destruction is implemented in the following two steps.
5853201af4c0STejun Heo  *
5854201af4c0STejun Heo  * s1. Verify @cgrp can be destroyed and mark it dying.  Remove all
5855201af4c0STejun Heo  *     userland visible parts and start killing the percpu refcnts of
5856201af4c0STejun Heo  *     css's.  Set up so that the next stage will be kicked off once all
5857201af4c0STejun Heo  *     the percpu refcnts are confirmed to be killed.
5858201af4c0STejun Heo  *
5859201af4c0STejun Heo  * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5860201af4c0STejun Heo  *     rest of destruction.  Once all cgroup references are gone, the
5861201af4c0STejun Heo  *     cgroup is RCU-freed.
5862201af4c0STejun Heo  *
5863201af4c0STejun Heo  * This function implements s1.  After this step, @cgrp is gone as far as
5864201af4c0STejun Heo  * the userland is concerned and a new cgroup with the same name may be
5865201af4c0STejun Heo  * created.  As cgroup doesn't care about the names internally, this
5866201af4c0STejun Heo  * doesn't cause any problem.
5867201af4c0STejun Heo  */
cgroup_destroy_locked(struct cgroup * cgrp)5868201af4c0STejun Heo static int cgroup_destroy_locked(struct cgroup *cgrp)
5869201af4c0STejun Heo 	__releases(&cgroup_mutex) __acquires(&cgroup_mutex)
5870201af4c0STejun Heo {
58710679dee0SRoman Gushchin 	struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
5872201af4c0STejun Heo 	struct cgroup_subsys_state *css;
5873201af4c0STejun Heo 	struct cgrp_cset_link *link;
5874201af4c0STejun Heo 	int ssid;
5875201af4c0STejun Heo 
5876201af4c0STejun Heo 	lockdep_assert_held(&cgroup_mutex);
5877201af4c0STejun Heo 
5878201af4c0STejun Heo 	/*
5879201af4c0STejun Heo 	 * Only migration can raise populated from zero and we're already
5880201af4c0STejun Heo 	 * holding cgroup_mutex.
5881201af4c0STejun Heo 	 */
5882201af4c0STejun Heo 	if (cgroup_is_populated(cgrp))
5883201af4c0STejun Heo 		return -EBUSY;
5884201af4c0STejun Heo 
5885201af4c0STejun Heo 	/*
5886201af4c0STejun Heo 	 * Make sure there's no live children.  We can't test emptiness of
5887201af4c0STejun Heo 	 * ->self.children as dead children linger on it while being
5888201af4c0STejun Heo 	 * drained; otherwise, "rmdir parent/child parent" may fail.
5889201af4c0STejun Heo 	 */
5890201af4c0STejun Heo 	if (css_has_online_children(&cgrp->self))
5891201af4c0STejun Heo 		return -EBUSY;
5892201af4c0STejun Heo 
5893201af4c0STejun Heo 	/*
5894201af4c0STejun Heo 	 * Mark @cgrp and the associated csets dead.  The former prevents
5895201af4c0STejun Heo 	 * further task migration and child creation by disabling
58967f828eacSMiaohe Lin 	 * cgroup_kn_lock_live().  The latter makes the csets ignored by
5897201af4c0STejun Heo 	 * the migration path.
5898201af4c0STejun Heo 	 */
5899201af4c0STejun Heo 	cgrp->self.flags &= ~CSS_ONLINE;
5900201af4c0STejun Heo 
5901201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
5902201af4c0STejun Heo 	list_for_each_entry(link, &cgrp->cset_links, cset_link)
5903201af4c0STejun Heo 		link->cset->dead = true;
5904201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
5905201af4c0STejun Heo 
5906201af4c0STejun Heo 	/* initiate massacre of all css's */
5907201af4c0STejun Heo 	for_each_css(css, ssid, cgrp)
5908201af4c0STejun Heo 		kill_css(css);
5909201af4c0STejun Heo 
59105faaf05fSTejun Heo 	/* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
59115faaf05fSTejun Heo 	css_clear_dir(&cgrp->self);
5912201af4c0STejun Heo 	kernfs_remove(cgrp->kn);
5913201af4c0STejun Heo 
5914b154a017SShida Zhang 	if (cgroup_is_threaded(cgrp))
5915454000adSTejun Heo 		parent->nr_threaded_children--;
5916454000adSTejun Heo 
59174dcabeceSRoman Gushchin 	spin_lock_irq(&css_set_lock);
5918fcbb485dSMiaohe Lin 	for (tcgrp = parent; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
59190679dee0SRoman Gushchin 		tcgrp->nr_descendants--;
59200679dee0SRoman Gushchin 		tcgrp->nr_dying_descendants++;
592176f969e8SRoman Gushchin 		/*
592276f969e8SRoman Gushchin 		 * If the dying cgroup is frozen, decrease frozen descendants
592376f969e8SRoman Gushchin 		 * counters of ancestor cgroups.
592476f969e8SRoman Gushchin 		 */
592576f969e8SRoman Gushchin 		if (test_bit(CGRP_FROZEN, &cgrp->flags))
592676f969e8SRoman Gushchin 			tcgrp->freezer.nr_frozen_descendants--;
59270679dee0SRoman Gushchin 	}
59284dcabeceSRoman Gushchin 	spin_unlock_irq(&css_set_lock);
59290679dee0SRoman Gushchin 
59305a621e6cSRoman Gushchin 	cgroup1_check_for_release(parent);
5931201af4c0STejun Heo 
59324bfc0bb2SRoman Gushchin 	cgroup_bpf_offline(cgrp);
59334bfc0bb2SRoman Gushchin 
5934201af4c0STejun Heo 	/* put the base reference */
5935201af4c0STejun Heo 	percpu_ref_kill(&cgrp->self.refcnt);
5936201af4c0STejun Heo 
5937201af4c0STejun Heo 	return 0;
5938201af4c0STejun Heo };
5939201af4c0STejun Heo 
cgroup_rmdir(struct kernfs_node * kn)59401592c9b2STejun Heo int cgroup_rmdir(struct kernfs_node *kn)
5941201af4c0STejun Heo {
5942201af4c0STejun Heo 	struct cgroup *cgrp;
5943201af4c0STejun Heo 	int ret = 0;
5944201af4c0STejun Heo 
5945201af4c0STejun Heo 	cgrp = cgroup_kn_lock_live(kn, false);
5946201af4c0STejun Heo 	if (!cgrp)
5947201af4c0STejun Heo 		return 0;
5948201af4c0STejun Heo 
5949201af4c0STejun Heo 	ret = cgroup_destroy_locked(cgrp);
5950201af4c0STejun Heo 	if (!ret)
5951e4f8d81cSSteven Rostedt (VMware) 		TRACE_CGROUP_PATH(rmdir, cgrp);
5952201af4c0STejun Heo 
5953201af4c0STejun Heo 	cgroup_kn_unlock(kn);
5954201af4c0STejun Heo 	return ret;
5955201af4c0STejun Heo }
5956201af4c0STejun Heo 
5957fa069904STejun Heo static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
59585136f636STejun Heo 	.show_options		= cgroup_show_options,
5959fa069904STejun Heo 	.mkdir			= cgroup_mkdir,
5960fa069904STejun Heo 	.rmdir			= cgroup_rmdir,
5961201af4c0STejun Heo 	.show_path		= cgroup_show_path,
5962201af4c0STejun Heo };
5963201af4c0STejun Heo 
cgroup_init_subsys(struct cgroup_subsys * ss,bool early)5964201af4c0STejun Heo static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
5965201af4c0STejun Heo {
5966201af4c0STejun Heo 	struct cgroup_subsys_state *css;
5967201af4c0STejun Heo 
5968201af4c0STejun Heo 	pr_debug("Initializing cgroup subsys %s\n", ss->name);
5969201af4c0STejun Heo 
59704cdb91b0SKamalesh Babulal 	cgroup_lock();
5971201af4c0STejun Heo 
5972201af4c0STejun Heo 	idr_init(&ss->css_idr);
5973201af4c0STejun Heo 	INIT_LIST_HEAD(&ss->cfts);
5974201af4c0STejun Heo 
5975201af4c0STejun Heo 	/* Create the root cgroup state for this subsystem */
5976201af4c0STejun Heo 	ss->root = &cgrp_dfl_root;
59778291471eSWei Yang 	css = ss->css_alloc(NULL);
5978201af4c0STejun Heo 	/* We don't handle early failures gracefully */
5979201af4c0STejun Heo 	BUG_ON(IS_ERR(css));
5980201af4c0STejun Heo 	init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
5981201af4c0STejun Heo 
5982201af4c0STejun Heo 	/*
5983201af4c0STejun Heo 	 * Root csses are never destroyed and we can't initialize
5984201af4c0STejun Heo 	 * percpu_ref during early init.  Disable refcnting.
5985201af4c0STejun Heo 	 */
5986201af4c0STejun Heo 	css->flags |= CSS_NO_REF;
5987201af4c0STejun Heo 
5988201af4c0STejun Heo 	if (early) {
5989201af4c0STejun Heo 		/* allocation can't be done safely during early init */
5990201af4c0STejun Heo 		css->id = 1;
5991201af4c0STejun Heo 	} else {
5992201af4c0STejun Heo 		css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5993201af4c0STejun Heo 		BUG_ON(css->id < 0);
5994201af4c0STejun Heo 	}
5995201af4c0STejun Heo 
5996201af4c0STejun Heo 	/* Update the init_css_set to contain a subsys
5997201af4c0STejun Heo 	 * pointer to this state - since the subsystem is
5998201af4c0STejun Heo 	 * newly registered, all tasks and hence the
5999201af4c0STejun Heo 	 * init_css_set is in the subsystem's root cgroup. */
6000201af4c0STejun Heo 	init_css_set.subsys[ss->id] = css;
6001201af4c0STejun Heo 
6002201af4c0STejun Heo 	have_fork_callback |= (bool)ss->fork << ss->id;
6003201af4c0STejun Heo 	have_exit_callback |= (bool)ss->exit << ss->id;
600451bee5abSOleg Nesterov 	have_release_callback |= (bool)ss->release << ss->id;
6005201af4c0STejun Heo 	have_canfork_callback |= (bool)ss->can_fork << ss->id;
6006201af4c0STejun Heo 
6007201af4c0STejun Heo 	/* At system boot, before all subsystems have been
6008201af4c0STejun Heo 	 * registered, no tasks have been forked, so we don't
6009201af4c0STejun Heo 	 * need to invoke fork callbacks here. */
6010201af4c0STejun Heo 	BUG_ON(!list_empty(&init_task.tasks));
6011201af4c0STejun Heo 
6012201af4c0STejun Heo 	BUG_ON(online_css(css));
6013201af4c0STejun Heo 
60144cdb91b0SKamalesh Babulal 	cgroup_unlock();
6015201af4c0STejun Heo }
6016201af4c0STejun Heo 
6017201af4c0STejun Heo /**
6018201af4c0STejun Heo  * cgroup_init_early - cgroup initialization at system boot
6019201af4c0STejun Heo  *
6020201af4c0STejun Heo  * Initialize cgroups at system boot, and initialize any
6021201af4c0STejun Heo  * subsystems that request early init.
6022201af4c0STejun Heo  */
cgroup_init_early(void)6023201af4c0STejun Heo int __init cgroup_init_early(void)
6024201af4c0STejun Heo {
6025f5dfb531SAl Viro 	static struct cgroup_fs_context __initdata ctx;
6026201af4c0STejun Heo 	struct cgroup_subsys *ss;
6027201af4c0STejun Heo 	int i;
6028201af4c0STejun Heo 
6029cf6299b1SAl Viro 	ctx.root = &cgrp_dfl_root;
6030cf6299b1SAl Viro 	init_cgroup_root(&ctx);
6031201af4c0STejun Heo 	cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
6032201af4c0STejun Heo 
6033201af4c0STejun Heo 	RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
6034201af4c0STejun Heo 
6035201af4c0STejun Heo 	for_each_subsys(ss, i) {
6036201af4c0STejun Heo 		WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
6037201af4c0STejun Heo 		     "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
6038201af4c0STejun Heo 		     i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
6039201af4c0STejun Heo 		     ss->id, ss->name);
6040201af4c0STejun Heo 		WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
6041201af4c0STejun Heo 		     "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
6042201af4c0STejun Heo 
6043201af4c0STejun Heo 		ss->id = i;
6044201af4c0STejun Heo 		ss->name = cgroup_subsys_name[i];
6045201af4c0STejun Heo 		if (!ss->legacy_name)
6046201af4c0STejun Heo 			ss->legacy_name = cgroup_subsys_name[i];
6047201af4c0STejun Heo 
6048201af4c0STejun Heo 		if (ss->early_init)
6049201af4c0STejun Heo 			cgroup_init_subsys(ss, true);
6050201af4c0STejun Heo 	}
6051201af4c0STejun Heo 	return 0;
6052201af4c0STejun Heo }
6053201af4c0STejun Heo 
6054201af4c0STejun Heo /**
6055201af4c0STejun Heo  * cgroup_init - cgroup initialization
6056201af4c0STejun Heo  *
6057201af4c0STejun Heo  * Register cgroup filesystem and /proc file, and initialize
6058201af4c0STejun Heo  * any subsystems that didn't request early init.
6059201af4c0STejun Heo  */
cgroup_init(void)6060201af4c0STejun Heo int __init cgroup_init(void)
6061201af4c0STejun Heo {
6062201af4c0STejun Heo 	struct cgroup_subsys *ss;
6063201af4c0STejun Heo 	int ssid;
6064201af4c0STejun Heo 
6065201af4c0STejun Heo 	BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
6066d62beb7fSTejun Heo 	BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
60678a693f77STejun Heo 	BUG_ON(cgroup_init_cftypes(NULL, cgroup_psi_files));
6068d62beb7fSTejun Heo 	BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
6069201af4c0STejun Heo 
6070c58632b3STejun Heo 	cgroup_rstat_boot();
6071041cd640STejun Heo 
6072201af4c0STejun Heo 	get_user_ns(init_cgroup_ns.user_ns);
6073201af4c0STejun Heo 
60744cdb91b0SKamalesh Babulal 	cgroup_lock();
6075201af4c0STejun Heo 
6076201af4c0STejun Heo 	/*
6077201af4c0STejun Heo 	 * Add init_css_set to the hash table so that dfl_root can link to
6078201af4c0STejun Heo 	 * it during init.
6079201af4c0STejun Heo 	 */
6080201af4c0STejun Heo 	hash_add(css_set_table, &init_css_set.hlist,
6081201af4c0STejun Heo 		 css_set_hash(init_css_set.subsys));
6082201af4c0STejun Heo 
608335ac1184SAl Viro 	BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
6084201af4c0STejun Heo 
60854cdb91b0SKamalesh Babulal 	cgroup_unlock();
6086201af4c0STejun Heo 
6087201af4c0STejun Heo 	for_each_subsys(ss, ssid) {
6088201af4c0STejun Heo 		if (ss->early_init) {
6089201af4c0STejun Heo 			struct cgroup_subsys_state *css =
6090201af4c0STejun Heo 				init_css_set.subsys[ss->id];
6091201af4c0STejun Heo 
6092201af4c0STejun Heo 			css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
6093201af4c0STejun Heo 						   GFP_KERNEL);
6094201af4c0STejun Heo 			BUG_ON(css->id < 0);
6095201af4c0STejun Heo 		} else {
6096201af4c0STejun Heo 			cgroup_init_subsys(ss, false);
6097201af4c0STejun Heo 		}
6098201af4c0STejun Heo 
6099201af4c0STejun Heo 		list_add_tail(&init_css_set.e_cset_node[ssid],
6100201af4c0STejun Heo 			      &cgrp_dfl_root.cgrp.e_csets[ssid]);
6101201af4c0STejun Heo 
6102201af4c0STejun Heo 		/*
6103201af4c0STejun Heo 		 * Setting dfl_root subsys_mask needs to consider the
6104201af4c0STejun Heo 		 * disabled flag and cftype registration needs kmalloc,
6105201af4c0STejun Heo 		 * both of which aren't available during early_init.
6106201af4c0STejun Heo 		 */
610745e1ba40SShakeel Butt 		if (!cgroup_ssid_enabled(ssid))
6108201af4c0STejun Heo 			continue;
6109201af4c0STejun Heo 
6110d62beb7fSTejun Heo 		if (cgroup1_ssid_disabled(ssid))
611155a5956aSKamalesh Babulal 			pr_info("Disabling %s control group subsystem in v1 mounts\n",
6112201af4c0STejun Heo 				ss->name);
6113201af4c0STejun Heo 
6114201af4c0STejun Heo 		cgrp_dfl_root.subsys_mask |= 1 << ss->id;
6115201af4c0STejun Heo 
61168cfd8147STejun Heo 		/* implicit controllers must be threaded too */
61178cfd8147STejun Heo 		WARN_ON(ss->implicit_on_dfl && !ss->threaded);
61188cfd8147STejun Heo 
6119201af4c0STejun Heo 		if (ss->implicit_on_dfl)
6120201af4c0STejun Heo 			cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
6121201af4c0STejun Heo 		else if (!ss->dfl_cftypes)
6122201af4c0STejun Heo 			cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
6123201af4c0STejun Heo 
61248cfd8147STejun Heo 		if (ss->threaded)
61258cfd8147STejun Heo 			cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
61268cfd8147STejun Heo 
6127201af4c0STejun Heo 		if (ss->dfl_cftypes == ss->legacy_cftypes) {
6128201af4c0STejun Heo 			WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
6129201af4c0STejun Heo 		} else {
6130201af4c0STejun Heo 			WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
6131201af4c0STejun Heo 			WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
6132201af4c0STejun Heo 		}
6133201af4c0STejun Heo 
6134201af4c0STejun Heo 		if (ss->bind)
6135201af4c0STejun Heo 			ss->bind(init_css_set.subsys[ssid]);
61367af608e4STejun Heo 
61374cdb91b0SKamalesh Babulal 		cgroup_lock();
61387af608e4STejun Heo 		css_populate_dir(init_css_set.subsys[ssid]);
61394cdb91b0SKamalesh Babulal 		cgroup_unlock();
6140201af4c0STejun Heo 	}
6141201af4c0STejun Heo 
6142201af4c0STejun Heo 	/* init_css_set.subsys[] has been updated, re-hash */
6143201af4c0STejun Heo 	hash_del(&init_css_set.hlist);
6144201af4c0STejun Heo 	hash_add(css_set_table, &init_css_set.hlist,
6145201af4c0STejun Heo 		 css_set_hash(init_css_set.subsys));
6146201af4c0STejun Heo 
6147201af4c0STejun Heo 	WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
6148201af4c0STejun Heo 	WARN_ON(register_filesystem(&cgroup_fs_type));
6149201af4c0STejun Heo 	WARN_ON(register_filesystem(&cgroup2_fs_type));
61503f3942acSChristoph Hellwig 	WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
6151d5f68d33SAl Viro #ifdef CONFIG_CPUSETS
6152d5f68d33SAl Viro 	WARN_ON(register_filesystem(&cpuset_fs_type));
6153d5f68d33SAl Viro #endif
6154201af4c0STejun Heo 
6155201af4c0STejun Heo 	return 0;
6156201af4c0STejun Heo }
6157201af4c0STejun Heo 
cgroup_wq_init(void)6158201af4c0STejun Heo static int __init cgroup_wq_init(void)
6159201af4c0STejun Heo {
6160201af4c0STejun Heo 	/*
6161201af4c0STejun Heo 	 * There isn't much point in executing destruction path in
6162201af4c0STejun Heo 	 * parallel.  Good chunk is serialized with cgroup_mutex anyway.
6163201af4c0STejun Heo 	 * Use 1 for @max_active.
6164201af4c0STejun Heo 	 *
6165201af4c0STejun Heo 	 * We would prefer to do this in cgroup_init() above, but that
6166201af4c0STejun Heo 	 * is called before init_workqueues(): so leave this until after.
6167201af4c0STejun Heo 	 */
6168201af4c0STejun Heo 	cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
6169201af4c0STejun Heo 	BUG_ON(!cgroup_destroy_wq);
6170201af4c0STejun Heo 	return 0;
6171201af4c0STejun Heo }
6172201af4c0STejun Heo core_initcall(cgroup_wq_init);
6173201af4c0STejun Heo 
cgroup_path_from_kernfs_id(u64 id,char * buf,size_t buflen)617467c0496eSTejun Heo void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
617569fd5c39SShaohua Li {
617669fd5c39SShaohua Li 	struct kernfs_node *kn;
617769fd5c39SShaohua Li 
6178fe0f726cSTejun Heo 	kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
617969fd5c39SShaohua Li 	if (!kn)
618069fd5c39SShaohua Li 		return;
618169fd5c39SShaohua Li 	kernfs_path(kn, buf, buflen);
618269fd5c39SShaohua Li 	kernfs_put(kn);
618369fd5c39SShaohua Li }
618469fd5c39SShaohua Li 
6185201af4c0STejun Heo /*
61866b658c48SMuneendra Kumar  * cgroup_get_from_id : get the cgroup associated with cgroup id
61876b658c48SMuneendra Kumar  * @id: cgroup id
6188fa7e439cSMichal Koutný  * On success return the cgrp or ERR_PTR on failure
61894534dee9SMichal Koutný  * Only cgroups within current task's cgroup NS are valid.
61906b658c48SMuneendra Kumar  */
cgroup_get_from_id(u64 id)61916b658c48SMuneendra Kumar struct cgroup *cgroup_get_from_id(u64 id)
61926b658c48SMuneendra Kumar {
61936b658c48SMuneendra Kumar 	struct kernfs_node *kn;
61947e1eb543STejun Heo 	struct cgroup *cgrp, *root_cgrp;
61956b658c48SMuneendra Kumar 
61966b658c48SMuneendra Kumar 	kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
61976b658c48SMuneendra Kumar 	if (!kn)
61987e1eb543STejun Heo 		return ERR_PTR(-ENOENT);
61996b658c48SMuneendra Kumar 
62007e1eb543STejun Heo 	if (kernfs_type(kn) != KERNFS_DIR) {
62017e1eb543STejun Heo 		kernfs_put(kn);
62027e1eb543STejun Heo 		return ERR_PTR(-ENOENT);
62037e1eb543STejun Heo 	}
6204df02452fSMing Lei 
6205be288169SShakeel Butt 	rcu_read_lock();
6206be288169SShakeel Butt 
6207be288169SShakeel Butt 	cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
6208be288169SShakeel Butt 	if (cgrp && !cgroup_tryget(cgrp))
62096b658c48SMuneendra Kumar 		cgrp = NULL;
6210be288169SShakeel Butt 
6211be288169SShakeel Butt 	rcu_read_unlock();
62126b658c48SMuneendra Kumar 	kernfs_put(kn);
62134534dee9SMichal Koutný 
62144534dee9SMichal Koutný 	if (!cgrp)
62157e1eb543STejun Heo 		return ERR_PTR(-ENOENT);
62164534dee9SMichal Koutný 
621746307fd6SMichal Koutný 	root_cgrp = current_cgns_cgroup_dfl();
62184534dee9SMichal Koutný 	if (!cgroup_is_descendant(cgrp, root_cgrp)) {
62194534dee9SMichal Koutný 		cgroup_put(cgrp);
62207e1eb543STejun Heo 		return ERR_PTR(-ENOENT);
62214534dee9SMichal Koutný 	}
62227e1eb543STejun Heo 
62236b658c48SMuneendra Kumar 	return cgrp;
62246b658c48SMuneendra Kumar }
62256b658c48SMuneendra Kumar EXPORT_SYMBOL_GPL(cgroup_get_from_id);
62266b658c48SMuneendra Kumar 
62276b658c48SMuneendra Kumar /*
6228201af4c0STejun Heo  * proc_cgroup_show()
6229201af4c0STejun Heo  *  - Print task's cgroup paths into seq_file, one line for each hierarchy
6230201af4c0STejun Heo  *  - Used for /proc/<pid>/cgroup.
6231201af4c0STejun Heo  */
proc_cgroup_show(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * tsk)6232201af4c0STejun Heo int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
6233201af4c0STejun Heo 		     struct pid *pid, struct task_struct *tsk)
6234201af4c0STejun Heo {
6235201af4c0STejun Heo 	char *buf;
6236201af4c0STejun Heo 	int retval;
6237201af4c0STejun Heo 	struct cgroup_root *root;
6238201af4c0STejun Heo 
6239201af4c0STejun Heo 	retval = -ENOMEM;
6240201af4c0STejun Heo 	buf = kmalloc(PATH_MAX, GFP_KERNEL);
6241201af4c0STejun Heo 	if (!buf)
6242201af4c0STejun Heo 		goto out;
6243201af4c0STejun Heo 
62444cdb91b0SKamalesh Babulal 	cgroup_lock();
6245201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
6246201af4c0STejun Heo 
6247201af4c0STejun Heo 	for_each_root(root) {
6248201af4c0STejun Heo 		struct cgroup_subsys *ss;
6249201af4c0STejun Heo 		struct cgroup *cgrp;
6250201af4c0STejun Heo 		int ssid, count = 0;
6251201af4c0STejun Heo 
6252dc79ec1bSTejun Heo 		if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible))
6253201af4c0STejun Heo 			continue;
6254201af4c0STejun Heo 
6255201af4c0STejun Heo 		seq_printf(m, "%d:", root->hierarchy_id);
6256201af4c0STejun Heo 		if (root != &cgrp_dfl_root)
6257201af4c0STejun Heo 			for_each_subsys(ss, ssid)
6258201af4c0STejun Heo 				if (root->subsys_mask & (1 << ssid))
6259201af4c0STejun Heo 					seq_printf(m, "%s%s", count++ ? "," : "",
6260201af4c0STejun Heo 						   ss->legacy_name);
6261201af4c0STejun Heo 		if (strlen(root->name))
6262201af4c0STejun Heo 			seq_printf(m, "%sname=%s", count ? "," : "",
6263201af4c0STejun Heo 				   root->name);
6264201af4c0STejun Heo 		seq_putc(m, ':');
6265201af4c0STejun Heo 
6266201af4c0STejun Heo 		cgrp = task_cgroup_from_root(tsk, root);
6267201af4c0STejun Heo 
6268201af4c0STejun Heo 		/*
6269201af4c0STejun Heo 		 * On traditional hierarchies, all zombie tasks show up as
6270201af4c0STejun Heo 		 * belonging to the root cgroup.  On the default hierarchy,
6271201af4c0STejun Heo 		 * while a zombie doesn't show up in "cgroup.procs" and
6272201af4c0STejun Heo 		 * thus can't be migrated, its /proc/PID/cgroup keeps
6273201af4c0STejun Heo 		 * reporting the cgroup it belonged to before exiting.  If
6274201af4c0STejun Heo 		 * the cgroup is removed before the zombie is reaped,
6275201af4c0STejun Heo 		 * " (deleted)" is appended to the cgroup path.
6276201af4c0STejun Heo 		 */
6277201af4c0STejun Heo 		if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
6278201af4c0STejun Heo 			retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
6279201af4c0STejun Heo 						current->nsproxy->cgroup_ns);
6280201af4c0STejun Heo 			if (retval >= PATH_MAX)
6281201af4c0STejun Heo 				retval = -ENAMETOOLONG;
6282201af4c0STejun Heo 			if (retval < 0)
6283201af4c0STejun Heo 				goto out_unlock;
6284201af4c0STejun Heo 
6285201af4c0STejun Heo 			seq_puts(m, buf);
6286201af4c0STejun Heo 		} else {
6287201af4c0STejun Heo 			seq_puts(m, "/");
6288201af4c0STejun Heo 		}
6289201af4c0STejun Heo 
6290201af4c0STejun Heo 		if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
6291201af4c0STejun Heo 			seq_puts(m, " (deleted)\n");
6292201af4c0STejun Heo 		else
6293201af4c0STejun Heo 			seq_putc(m, '\n');
6294201af4c0STejun Heo 	}
6295201af4c0STejun Heo 
6296201af4c0STejun Heo 	retval = 0;
6297201af4c0STejun Heo out_unlock:
6298201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
62994cdb91b0SKamalesh Babulal 	cgroup_unlock();
6300201af4c0STejun Heo 	kfree(buf);
6301201af4c0STejun Heo out:
6302201af4c0STejun Heo 	return retval;
6303201af4c0STejun Heo }
6304201af4c0STejun Heo 
6305201af4c0STejun Heo /**
6306201af4c0STejun Heo  * cgroup_fork - initialize cgroup related fields during copy_process()
6307201af4c0STejun Heo  * @child: pointer to task_struct of forking parent process.
6308201af4c0STejun Heo  *
6309201af4c0STejun Heo  * A task is associated with the init_css_set until cgroup_post_fork()
6310ef2c41cfSChristian Brauner  * attaches it to the target css_set.
6311201af4c0STejun Heo  */
cgroup_fork(struct task_struct * child)6312201af4c0STejun Heo void cgroup_fork(struct task_struct *child)
6313201af4c0STejun Heo {
6314201af4c0STejun Heo 	RCU_INIT_POINTER(child->cgroups, &init_css_set);
6315201af4c0STejun Heo 	INIT_LIST_HEAD(&child->cg_list);
6316201af4c0STejun Heo }
6317201af4c0STejun Heo 
6318a6d1ce59SYosry Ahmed /**
6319a6d1ce59SYosry Ahmed  * cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer
6320a6d1ce59SYosry Ahmed  * @f: file corresponding to cgroup_dir
6321a6d1ce59SYosry Ahmed  *
6322a6d1ce59SYosry Ahmed  * Find the cgroup from a file pointer associated with a cgroup directory.
6323a6d1ce59SYosry Ahmed  * Returns a pointer to the cgroup on success. ERR_PTR is returned if the
6324a6d1ce59SYosry Ahmed  * cgroup cannot be found.
6325a6d1ce59SYosry Ahmed  */
cgroup_v1v2_get_from_file(struct file * f)6326a6d1ce59SYosry Ahmed static struct cgroup *cgroup_v1v2_get_from_file(struct file *f)
632717703097SChristian Brauner {
632817703097SChristian Brauner 	struct cgroup_subsys_state *css;
632917703097SChristian Brauner 
633017703097SChristian Brauner 	css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
633117703097SChristian Brauner 	if (IS_ERR(css))
633217703097SChristian Brauner 		return ERR_CAST(css);
633317703097SChristian Brauner 
6334a6d1ce59SYosry Ahmed 	return css->cgroup;
6335a6d1ce59SYosry Ahmed }
6336a6d1ce59SYosry Ahmed 
6337a6d1ce59SYosry Ahmed /**
6338a6d1ce59SYosry Ahmed  * cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports
6339a6d1ce59SYosry Ahmed  * cgroup2.
6340b675d4bdSYosry Ahmed  * @f: file corresponding to cgroup2_dir
6341a6d1ce59SYosry Ahmed  */
cgroup_get_from_file(struct file * f)6342a6d1ce59SYosry Ahmed static struct cgroup *cgroup_get_from_file(struct file *f)
6343a6d1ce59SYosry Ahmed {
6344a6d1ce59SYosry Ahmed 	struct cgroup *cgrp = cgroup_v1v2_get_from_file(f);
6345a6d1ce59SYosry Ahmed 
6346a6d1ce59SYosry Ahmed 	if (IS_ERR(cgrp))
6347a6d1ce59SYosry Ahmed 		return ERR_CAST(cgrp);
6348a6d1ce59SYosry Ahmed 
634903db7716STejun Heo 	if (!cgroup_on_dfl(cgrp)) {
635003db7716STejun Heo 		cgroup_put(cgrp);
635103db7716STejun Heo 		return ERR_PTR(-EBADF);
635203db7716STejun Heo 	}
635303db7716STejun Heo 
635417703097SChristian Brauner 	return cgrp;
635517703097SChristian Brauner }
635617703097SChristian Brauner 
6357201af4c0STejun Heo /**
6358ef2c41cfSChristian Brauner  * cgroup_css_set_fork - find or create a css_set for a child process
6359ef2c41cfSChristian Brauner  * @kargs: the arguments passed to create the child process
6360ef2c41cfSChristian Brauner  *
6361ef2c41cfSChristian Brauner  * This functions finds or creates a new css_set which the child
6362ef2c41cfSChristian Brauner  * process will be attached to in cgroup_post_fork(). By default,
6363ef2c41cfSChristian Brauner  * the child process will be given the same css_set as its parent.
6364ef2c41cfSChristian Brauner  *
6365ef2c41cfSChristian Brauner  * If CLONE_INTO_CGROUP is specified this function will try to find an
6366ef2c41cfSChristian Brauner  * existing css_set which includes the requested cgroup and if not create
6367ef2c41cfSChristian Brauner  * a new css_set that the child will be attached to later. If this function
6368ef2c41cfSChristian Brauner  * succeeds it will hold cgroup_threadgroup_rwsem on return. If
6369ef2c41cfSChristian Brauner  * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex
6370ef2c41cfSChristian Brauner  * before grabbing cgroup_threadgroup_rwsem and will hold a reference
6371ef2c41cfSChristian Brauner  * to the target cgroup.
6372ef2c41cfSChristian Brauner  */
cgroup_css_set_fork(struct kernel_clone_args * kargs)6373ef2c41cfSChristian Brauner static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
6374ef2c41cfSChristian Brauner 	__acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem)
6375ef2c41cfSChristian Brauner {
6376ef2c41cfSChristian Brauner 	int ret;
6377ef2c41cfSChristian Brauner 	struct cgroup *dst_cgrp = NULL;
6378ef2c41cfSChristian Brauner 	struct css_set *cset;
6379ef2c41cfSChristian Brauner 	struct super_block *sb;
6380ef2c41cfSChristian Brauner 	struct file *f;
6381ef2c41cfSChristian Brauner 
6382ef2c41cfSChristian Brauner 	if (kargs->flags & CLONE_INTO_CGROUP)
63834cdb91b0SKamalesh Babulal 		cgroup_lock();
6384ef2c41cfSChristian Brauner 
6385ef2c41cfSChristian Brauner 	cgroup_threadgroup_change_begin(current);
6386ef2c41cfSChristian Brauner 
6387ef2c41cfSChristian Brauner 	spin_lock_irq(&css_set_lock);
6388ef2c41cfSChristian Brauner 	cset = task_css_set(current);
6389ef2c41cfSChristian Brauner 	get_css_set(cset);
6390ef2c41cfSChristian Brauner 	spin_unlock_irq(&css_set_lock);
6391ef2c41cfSChristian Brauner 
6392ef2c41cfSChristian Brauner 	if (!(kargs->flags & CLONE_INTO_CGROUP)) {
6393ef2c41cfSChristian Brauner 		kargs->cset = cset;
6394ef2c41cfSChristian Brauner 		return 0;
6395ef2c41cfSChristian Brauner 	}
6396ef2c41cfSChristian Brauner 
6397ef2c41cfSChristian Brauner 	f = fget_raw(kargs->cgroup);
6398ef2c41cfSChristian Brauner 	if (!f) {
6399ef2c41cfSChristian Brauner 		ret = -EBADF;
6400ef2c41cfSChristian Brauner 		goto err;
6401ef2c41cfSChristian Brauner 	}
6402ef2c41cfSChristian Brauner 	sb = f->f_path.dentry->d_sb;
6403ef2c41cfSChristian Brauner 
6404ef2c41cfSChristian Brauner 	dst_cgrp = cgroup_get_from_file(f);
6405ef2c41cfSChristian Brauner 	if (IS_ERR(dst_cgrp)) {
6406ef2c41cfSChristian Brauner 		ret = PTR_ERR(dst_cgrp);
6407ef2c41cfSChristian Brauner 		dst_cgrp = NULL;
6408ef2c41cfSChristian Brauner 		goto err;
6409ef2c41cfSChristian Brauner 	}
6410ef2c41cfSChristian Brauner 
6411ef2c41cfSChristian Brauner 	if (cgroup_is_dead(dst_cgrp)) {
6412ef2c41cfSChristian Brauner 		ret = -ENODEV;
6413ef2c41cfSChristian Brauner 		goto err;
6414ef2c41cfSChristian Brauner 	}
6415ef2c41cfSChristian Brauner 
6416ef2c41cfSChristian Brauner 	/*
6417ef2c41cfSChristian Brauner 	 * Verify that we the target cgroup is writable for us. This is
6418ef2c41cfSChristian Brauner 	 * usually done by the vfs layer but since we're not going through
6419ef2c41cfSChristian Brauner 	 * the vfs layer here we need to do it "manually".
6420ef2c41cfSChristian Brauner 	 */
6421ef2c41cfSChristian Brauner 	ret = cgroup_may_write(dst_cgrp, sb);
6422ef2c41cfSChristian Brauner 	if (ret)
6423ef2c41cfSChristian Brauner 		goto err;
6424ef2c41cfSChristian Brauner 
64256d3971daSChristian Brauner 	/*
64266d3971daSChristian Brauner 	 * Spawning a task directly into a cgroup works by passing a file
64276d3971daSChristian Brauner 	 * descriptor to the target cgroup directory. This can even be an O_PATH
64286d3971daSChristian Brauner 	 * file descriptor. But it can never be a cgroup.procs file descriptor.
64296d3971daSChristian Brauner 	 * This was done on purpose so spawning into a cgroup could be
64306d3971daSChristian Brauner 	 * conceptualized as an atomic
64316d3971daSChristian Brauner 	 *
64326d3971daSChristian Brauner 	 *   fd = openat(dfd_cgroup, "cgroup.procs", ...);
64336d3971daSChristian Brauner 	 *   write(fd, <child-pid>, ...);
64346d3971daSChristian Brauner 	 *
64356d3971daSChristian Brauner 	 * sequence, i.e. it's a shorthand for the caller opening and writing
64366d3971daSChristian Brauner 	 * cgroup.procs of the cgroup indicated by @dfd_cgroup. This allows us
64376d3971daSChristian Brauner 	 * to always use the caller's credentials.
64386d3971daSChristian Brauner 	 */
6439ef2c41cfSChristian Brauner 	ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb,
6440e5745764STejun Heo 					!(kargs->flags & CLONE_THREAD),
6441e5745764STejun Heo 					current->nsproxy->cgroup_ns);
6442ef2c41cfSChristian Brauner 	if (ret)
6443ef2c41cfSChristian Brauner 		goto err;
6444ef2c41cfSChristian Brauner 
6445ef2c41cfSChristian Brauner 	kargs->cset = find_css_set(cset, dst_cgrp);
6446ef2c41cfSChristian Brauner 	if (!kargs->cset) {
6447ef2c41cfSChristian Brauner 		ret = -ENOMEM;
6448ef2c41cfSChristian Brauner 		goto err;
6449ef2c41cfSChristian Brauner 	}
6450ef2c41cfSChristian Brauner 
6451ef2c41cfSChristian Brauner 	put_css_set(cset);
6452ef2c41cfSChristian Brauner 	fput(f);
6453ef2c41cfSChristian Brauner 	kargs->cgrp = dst_cgrp;
6454ef2c41cfSChristian Brauner 	return ret;
6455ef2c41cfSChristian Brauner 
6456ef2c41cfSChristian Brauner err:
6457ef2c41cfSChristian Brauner 	cgroup_threadgroup_change_end(current);
64584cdb91b0SKamalesh Babulal 	cgroup_unlock();
6459ef2c41cfSChristian Brauner 	if (f)
6460ef2c41cfSChristian Brauner 		fput(f);
6461ef2c41cfSChristian Brauner 	if (dst_cgrp)
6462ef2c41cfSChristian Brauner 		cgroup_put(dst_cgrp);
6463ef2c41cfSChristian Brauner 	put_css_set(cset);
6464ef2c41cfSChristian Brauner 	if (kargs->cset)
6465ef2c41cfSChristian Brauner 		put_css_set(kargs->cset);
6466ef2c41cfSChristian Brauner 	return ret;
6467ef2c41cfSChristian Brauner }
6468ef2c41cfSChristian Brauner 
6469ef2c41cfSChristian Brauner /**
6470ef2c41cfSChristian Brauner  * cgroup_css_set_put_fork - drop references we took during fork
6471ef2c41cfSChristian Brauner  * @kargs: the arguments passed to create the child process
6472ef2c41cfSChristian Brauner  *
6473ef2c41cfSChristian Brauner  * Drop references to the prepared css_set and target cgroup if
6474ef2c41cfSChristian Brauner  * CLONE_INTO_CGROUP was requested.
6475ef2c41cfSChristian Brauner  */
cgroup_css_set_put_fork(struct kernel_clone_args * kargs)6476ef2c41cfSChristian Brauner static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs)
6477ef2c41cfSChristian Brauner 	__releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
6478ef2c41cfSChristian Brauner {
6479ef2c41cfSChristian Brauner 	struct cgroup *cgrp = kargs->cgrp;
6480ef2c41cfSChristian Brauner 	struct css_set *cset = kargs->cset;
6481ef2c41cfSChristian Brauner 
64822bd11033SJohn Sperbeck 	cgroup_threadgroup_change_end(current);
6483ef2c41cfSChristian Brauner 
6484ef2c41cfSChristian Brauner 	if (cset) {
6485ef2c41cfSChristian Brauner 		put_css_set(cset);
6486ef2c41cfSChristian Brauner 		kargs->cset = NULL;
6487ef2c41cfSChristian Brauner 	}
6488ef2c41cfSChristian Brauner 
64892bd11033SJohn Sperbeck 	if (kargs->flags & CLONE_INTO_CGROUP) {
64902bd11033SJohn Sperbeck 		cgroup_unlock();
6491ef2c41cfSChristian Brauner 		if (cgrp) {
6492ef2c41cfSChristian Brauner 			cgroup_put(cgrp);
6493ef2c41cfSChristian Brauner 			kargs->cgrp = NULL;
6494ef2c41cfSChristian Brauner 		}
6495ef2c41cfSChristian Brauner 	}
6496ef2c41cfSChristian Brauner }
6497ef2c41cfSChristian Brauner 
6498201af4c0STejun Heo /**
6499201af4c0STejun Heo  * cgroup_can_fork - called on a new task before the process is exposed
65005a5cf5cbSChristian Brauner  * @child: the child process
6501ffacbd11SYang Li  * @kargs: the arguments passed to create the child process
6502201af4c0STejun Heo  *
6503ef2c41cfSChristian Brauner  * This prepares a new css_set for the child process which the child will
6504ef2c41cfSChristian Brauner  * be attached to in cgroup_post_fork().
65055a5cf5cbSChristian Brauner  * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork()
65065a5cf5cbSChristian Brauner  * callback returns an error, the fork aborts with that error code. This
65075a5cf5cbSChristian Brauner  * allows for a cgroup subsystem to conditionally allow or deny new forks.
6508201af4c0STejun Heo  */
cgroup_can_fork(struct task_struct * child,struct kernel_clone_args * kargs)6509ef2c41cfSChristian Brauner int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs)
6510201af4c0STejun Heo {
6511201af4c0STejun Heo 	struct cgroup_subsys *ss;
6512201af4c0STejun Heo 	int i, j, ret;
6513201af4c0STejun Heo 
6514ef2c41cfSChristian Brauner 	ret = cgroup_css_set_fork(kargs);
6515ef2c41cfSChristian Brauner 	if (ret)
6516ef2c41cfSChristian Brauner 		return ret;
65175a5cf5cbSChristian Brauner 
6518201af4c0STejun Heo 	do_each_subsys_mask(ss, i, have_canfork_callback) {
6519ef2c41cfSChristian Brauner 		ret = ss->can_fork(child, kargs->cset);
6520201af4c0STejun Heo 		if (ret)
6521201af4c0STejun Heo 			goto out_revert;
6522201af4c0STejun Heo 	} while_each_subsys_mask();
6523201af4c0STejun Heo 
6524201af4c0STejun Heo 	return 0;
6525201af4c0STejun Heo 
6526201af4c0STejun Heo out_revert:
6527201af4c0STejun Heo 	for_each_subsys(ss, j) {
6528201af4c0STejun Heo 		if (j >= i)
6529201af4c0STejun Heo 			break;
6530201af4c0STejun Heo 		if (ss->cancel_fork)
6531ef2c41cfSChristian Brauner 			ss->cancel_fork(child, kargs->cset);
6532201af4c0STejun Heo 	}
6533201af4c0STejun Heo 
6534ef2c41cfSChristian Brauner 	cgroup_css_set_put_fork(kargs);
65355a5cf5cbSChristian Brauner 
6536201af4c0STejun Heo 	return ret;
6537201af4c0STejun Heo }
6538201af4c0STejun Heo 
6539201af4c0STejun Heo /**
6540201af4c0STejun Heo  * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
65415a5cf5cbSChristian Brauner  * @child: the child process
6542ef2c41cfSChristian Brauner  * @kargs: the arguments passed to create the child process
6543201af4c0STejun Heo  *
6544201af4c0STejun Heo  * This calls the cancel_fork() callbacks if a fork failed *after*
654508b2b6fdSZhen Lei  * cgroup_can_fork() succeeded and cleans up references we took to
6546ef2c41cfSChristian Brauner  * prepare a new css_set for the child process in cgroup_can_fork().
6547201af4c0STejun Heo  */
cgroup_cancel_fork(struct task_struct * child,struct kernel_clone_args * kargs)6548ef2c41cfSChristian Brauner void cgroup_cancel_fork(struct task_struct *child,
6549ef2c41cfSChristian Brauner 			struct kernel_clone_args *kargs)
6550201af4c0STejun Heo {
6551201af4c0STejun Heo 	struct cgroup_subsys *ss;
6552201af4c0STejun Heo 	int i;
6553201af4c0STejun Heo 
6554201af4c0STejun Heo 	for_each_subsys(ss, i)
6555201af4c0STejun Heo 		if (ss->cancel_fork)
6556ef2c41cfSChristian Brauner 			ss->cancel_fork(child, kargs->cset);
65575a5cf5cbSChristian Brauner 
6558ef2c41cfSChristian Brauner 	cgroup_css_set_put_fork(kargs);
6559201af4c0STejun Heo }
6560201af4c0STejun Heo 
6561201af4c0STejun Heo /**
65625a5cf5cbSChristian Brauner  * cgroup_post_fork - finalize cgroup setup for the child process
65635a5cf5cbSChristian Brauner  * @child: the child process
6564ffacbd11SYang Li  * @kargs: the arguments passed to create the child process
6565201af4c0STejun Heo  *
65665a5cf5cbSChristian Brauner  * Attach the child process to its css_set calling the subsystem fork()
65675a5cf5cbSChristian Brauner  * callbacks.
6568201af4c0STejun Heo  */
cgroup_post_fork(struct task_struct * child,struct kernel_clone_args * kargs)6569ef2c41cfSChristian Brauner void cgroup_post_fork(struct task_struct *child,
6570ef2c41cfSChristian Brauner 		      struct kernel_clone_args *kargs)
6571ef2c41cfSChristian Brauner 	__releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
6572201af4c0STejun Heo {
6573661ee628SChristian Brauner 	unsigned long cgrp_flags = 0;
6574661ee628SChristian Brauner 	bool kill = false;
6575201af4c0STejun Heo 	struct cgroup_subsys *ss;
65765153faacSTejun Heo 	struct css_set *cset;
6577201af4c0STejun Heo 	int i;
6578201af4c0STejun Heo 
6579ef2c41cfSChristian Brauner 	cset = kargs->cset;
6580ef2c41cfSChristian Brauner 	kargs->cset = NULL;
6581ef2c41cfSChristian Brauner 
6582201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
65835153faacSTejun Heo 
65840cd9d33aSTejun Heo 	/* init tasks are special, only link regular threads */
65850cd9d33aSTejun Heo 	if (likely(child->pid)) {
6586661ee628SChristian Brauner 		if (kargs->cgrp)
6587661ee628SChristian Brauner 			cgrp_flags = kargs->cgrp->flags;
6588661ee628SChristian Brauner 		else
6589661ee628SChristian Brauner 			cgrp_flags = cset->dfl_cgrp->flags;
6590661ee628SChristian Brauner 
65915153faacSTejun Heo 		WARN_ON_ONCE(!list_empty(&child->cg_list));
659273a7242aSWaiman Long 		cset->nr_tasks++;
6593201af4c0STejun Heo 		css_set_move_task(child, NULL, cset, false);
6594ef2c41cfSChristian Brauner 	} else {
6595ef2c41cfSChristian Brauner 		put_css_set(cset);
6596ef2c41cfSChristian Brauner 		cset = NULL;
65970cd9d33aSTejun Heo 	}
659876f969e8SRoman Gushchin 
6599661ee628SChristian Brauner 	if (!(child->flags & PF_KTHREAD)) {
6600661ee628SChristian Brauner 		if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) {
660176f969e8SRoman Gushchin 			/*
6602661ee628SChristian Brauner 			 * If the cgroup has to be frozen, the new task has
6603661ee628SChristian Brauner 			 * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to
6604661ee628SChristian Brauner 			 * get the task into the frozen state.
660576f969e8SRoman Gushchin 			 */
660676f969e8SRoman Gushchin 			spin_lock(&child->sighand->siglock);
660776f969e8SRoman Gushchin 			WARN_ON_ONCE(child->frozen);
660876f969e8SRoman Gushchin 			child->jobctl |= JOBCTL_TRAP_FREEZE;
660976f969e8SRoman Gushchin 			spin_unlock(&child->sighand->siglock);
661076f969e8SRoman Gushchin 
661176f969e8SRoman Gushchin 			/*
661276f969e8SRoman Gushchin 			 * Calling cgroup_update_frozen() isn't required here,
66135153faacSTejun Heo 			 * because it will be called anyway a bit later from
6614661ee628SChristian Brauner 			 * do_freezer_trap(). So we avoid cgroup's transient
6615661ee628SChristian Brauner 			 * switch from the frozen state and back.
661676f969e8SRoman Gushchin 			 */
661776f969e8SRoman Gushchin 		}
661876f969e8SRoman Gushchin 
6619661ee628SChristian Brauner 		/*
6620661ee628SChristian Brauner 		 * If the cgroup is to be killed notice it now and take the
6621661ee628SChristian Brauner 		 * child down right after we finished preparing it for
6622661ee628SChristian Brauner 		 * userspace.
6623661ee628SChristian Brauner 		 */
6624661ee628SChristian Brauner 		kill = test_bit(CGRP_KILL, &cgrp_flags);
6625661ee628SChristian Brauner 	}
6626661ee628SChristian Brauner 
6627201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
6628201af4c0STejun Heo 
6629201af4c0STejun Heo 	/*
6630201af4c0STejun Heo 	 * Call ss->fork().  This must happen after @child is linked on
6631201af4c0STejun Heo 	 * css_set; otherwise, @child might change state between ->fork()
6632201af4c0STejun Heo 	 * and addition to css_set.
6633201af4c0STejun Heo 	 */
6634201af4c0STejun Heo 	do_each_subsys_mask(ss, i, have_fork_callback) {
6635201af4c0STejun Heo 		ss->fork(child);
6636201af4c0STejun Heo 	} while_each_subsys_mask();
66375a5cf5cbSChristian Brauner 
6638ef2c41cfSChristian Brauner 	/* Make the new cset the root_cset of the new cgroup namespace. */
6639ef2c41cfSChristian Brauner 	if (kargs->flags & CLONE_NEWCGROUP) {
6640ef2c41cfSChristian Brauner 		struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset;
6641ef2c41cfSChristian Brauner 
6642ef2c41cfSChristian Brauner 		get_css_set(cset);
6643ef2c41cfSChristian Brauner 		child->nsproxy->cgroup_ns->root_cset = cset;
6644ef2c41cfSChristian Brauner 		put_css_set(rcset);
6645ef2c41cfSChristian Brauner 	}
6646ef2c41cfSChristian Brauner 
6647661ee628SChristian Brauner 	/* Cgroup has to be killed so take down child immediately. */
6648661ee628SChristian Brauner 	if (unlikely(kill))
6649661ee628SChristian Brauner 		do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID);
6650661ee628SChristian Brauner 
6651ef2c41cfSChristian Brauner 	cgroup_css_set_put_fork(kargs);
6652201af4c0STejun Heo }
6653201af4c0STejun Heo 
6654201af4c0STejun Heo /**
6655201af4c0STejun Heo  * cgroup_exit - detach cgroup from exiting task
6656201af4c0STejun Heo  * @tsk: pointer to task_struct of exiting process
6657201af4c0STejun Heo  *
6658e7c7b1d8SMichal Koutný  * Description: Detach cgroup from @tsk.
6659201af4c0STejun Heo  *
6660201af4c0STejun Heo  */
cgroup_exit(struct task_struct * tsk)6661201af4c0STejun Heo void cgroup_exit(struct task_struct *tsk)
6662201af4c0STejun Heo {
6663201af4c0STejun Heo 	struct cgroup_subsys *ss;
6664201af4c0STejun Heo 	struct css_set *cset;
6665201af4c0STejun Heo 	int i;
6666201af4c0STejun Heo 
6667201af4c0STejun Heo 	spin_lock_irq(&css_set_lock);
66685153faacSTejun Heo 
66695153faacSTejun Heo 	WARN_ON_ONCE(list_empty(&tsk->cg_list));
66705153faacSTejun Heo 	cset = task_css_set(tsk);
6671201af4c0STejun Heo 	css_set_move_task(tsk, cset, NULL, false);
6672c03cd773STejun Heo 	list_add_tail(&tsk->cg_list, &cset->dying_tasks);
667373a7242aSWaiman Long 	cset->nr_tasks--;
667476f969e8SRoman Gushchin 
66756c24849fSJuri Lelli 	if (dl_task(tsk))
66766c24849fSJuri Lelli 		dec_dl_tasks_cs(tsk);
66776c24849fSJuri Lelli 
667896b9c592SRoman Gushchin 	WARN_ON_ONCE(cgroup_task_frozen(tsk));
6679f4f809f6SRoman Gushchin 	if (unlikely(!(tsk->flags & PF_KTHREAD) &&
6680f4f809f6SRoman Gushchin 		     test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags)))
668176f969e8SRoman Gushchin 		cgroup_update_frozen(task_dfl_cgroup(tsk));
668276f969e8SRoman Gushchin 
6683201af4c0STejun Heo 	spin_unlock_irq(&css_set_lock);
6684201af4c0STejun Heo 
6685201af4c0STejun Heo 	/* see cgroup_post_fork() for details */
6686201af4c0STejun Heo 	do_each_subsys_mask(ss, i, have_exit_callback) {
6687201af4c0STejun Heo 		ss->exit(tsk);
6688201af4c0STejun Heo 	} while_each_subsys_mask();
6689201af4c0STejun Heo }
6690201af4c0STejun Heo 
cgroup_release(struct task_struct * task)669151bee5abSOleg Nesterov void cgroup_release(struct task_struct *task)
6692201af4c0STejun Heo {
6693201af4c0STejun Heo 	struct cgroup_subsys *ss;
6694201af4c0STejun Heo 	int ssid;
6695201af4c0STejun Heo 
669651bee5abSOleg Nesterov 	do_each_subsys_mask(ss, ssid, have_release_callback) {
669751bee5abSOleg Nesterov 		ss->release(task);
6698201af4c0STejun Heo 	} while_each_subsys_mask();
6699c03cd773STejun Heo 
6700c03cd773STejun Heo 	spin_lock_irq(&css_set_lock);
6701c03cd773STejun Heo 	css_set_skip_task_iters(task_css_set(task), task);
6702c03cd773STejun Heo 	list_del_init(&task->cg_list);
6703c03cd773STejun Heo 	spin_unlock_irq(&css_set_lock);
6704c03cd773STejun Heo }
6705201af4c0STejun Heo 
cgroup_free(struct task_struct * task)670651bee5abSOleg Nesterov void cgroup_free(struct task_struct *task)
670751bee5abSOleg Nesterov {
670851bee5abSOleg Nesterov 	struct css_set *cset = task_css_set(task);
6709201af4c0STejun Heo 	put_css_set(cset);
6710201af4c0STejun Heo }
6711201af4c0STejun Heo 
cgroup_disable(char * str)6712201af4c0STejun Heo static int __init cgroup_disable(char *str)
6713201af4c0STejun Heo {
6714201af4c0STejun Heo 	struct cgroup_subsys *ss;
6715201af4c0STejun Heo 	char *token;
6716201af4c0STejun Heo 	int i;
6717201af4c0STejun Heo 
6718201af4c0STejun Heo 	while ((token = strsep(&str, ",")) != NULL) {
6719201af4c0STejun Heo 		if (!*token)
6720201af4c0STejun Heo 			continue;
6721201af4c0STejun Heo 
6722201af4c0STejun Heo 		for_each_subsys(ss, i) {
6723201af4c0STejun Heo 			if (strcmp(token, ss->name) &&
6724201af4c0STejun Heo 			    strcmp(token, ss->legacy_name))
6725201af4c0STejun Heo 				continue;
672645e1ba40SShakeel Butt 
672745e1ba40SShakeel Butt 			static_branch_disable(cgroup_subsys_enabled_key[i]);
672845e1ba40SShakeel Butt 			pr_info("Disabling %s control group subsystem\n",
672945e1ba40SShakeel Butt 				ss->name);
6730201af4c0STejun Heo 		}
67313958e2d0SSuren Baghdasaryan 
67323958e2d0SSuren Baghdasaryan 		for (i = 0; i < OPT_FEATURE_COUNT; i++) {
67333958e2d0SSuren Baghdasaryan 			if (strcmp(token, cgroup_opt_feature_names[i]))
67343958e2d0SSuren Baghdasaryan 				continue;
67353958e2d0SSuren Baghdasaryan 			cgroup_feature_disable_mask |= 1 << i;
67363958e2d0SSuren Baghdasaryan 			pr_info("Disabling %s control group feature\n",
67373958e2d0SSuren Baghdasaryan 				cgroup_opt_feature_names[i]);
67383958e2d0SSuren Baghdasaryan 			break;
67393958e2d0SSuren Baghdasaryan 		}
6740201af4c0STejun Heo 	}
6741201af4c0STejun Heo 	return 1;
6742201af4c0STejun Heo }
6743201af4c0STejun Heo __setup("cgroup_disable=", cgroup_disable);
6744201af4c0STejun Heo 
enable_debug_cgroup(void)67455cf8114dSWaiman Long void __init __weak enable_debug_cgroup(void) { }
67465cf8114dSWaiman Long 
enable_cgroup_debug(char * str)67475cf8114dSWaiman Long static int __init enable_cgroup_debug(char *str)
67485cf8114dSWaiman Long {
67495cf8114dSWaiman Long 	cgroup_debug = true;
67505cf8114dSWaiman Long 	enable_debug_cgroup();
67515cf8114dSWaiman Long 	return 1;
67525cf8114dSWaiman Long }
67535cf8114dSWaiman Long __setup("cgroup_debug", enable_cgroup_debug);
67545cf8114dSWaiman Long 
6755201af4c0STejun Heo /**
6756201af4c0STejun Heo  * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
6757201af4c0STejun Heo  * @dentry: directory dentry of interest
6758201af4c0STejun Heo  * @ss: subsystem of interest
6759201af4c0STejun Heo  *
6760201af4c0STejun Heo  * If @dentry is a directory for a cgroup which has @ss enabled on it, try
6761201af4c0STejun Heo  * to get the corresponding css and return it.  If such css doesn't exist
6762201af4c0STejun Heo  * or can't be pinned, an ERR_PTR value is returned.
6763201af4c0STejun Heo  */
css_tryget_online_from_dir(struct dentry * dentry,struct cgroup_subsys * ss)6764201af4c0STejun Heo struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
6765201af4c0STejun Heo 						       struct cgroup_subsys *ss)
6766201af4c0STejun Heo {
6767201af4c0STejun Heo 	struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
6768201af4c0STejun Heo 	struct file_system_type *s_type = dentry->d_sb->s_type;
6769201af4c0STejun Heo 	struct cgroup_subsys_state *css = NULL;
6770201af4c0STejun Heo 	struct cgroup *cgrp;
6771201af4c0STejun Heo 
6772201af4c0STejun Heo 	/* is @dentry a cgroup dir? */
6773201af4c0STejun Heo 	if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
6774201af4c0STejun Heo 	    !kn || kernfs_type(kn) != KERNFS_DIR)
6775201af4c0STejun Heo 		return ERR_PTR(-EBADF);
6776201af4c0STejun Heo 
6777201af4c0STejun Heo 	rcu_read_lock();
6778201af4c0STejun Heo 
6779201af4c0STejun Heo 	/*
6780201af4c0STejun Heo 	 * This path doesn't originate from kernfs and @kn could already
6781201af4c0STejun Heo 	 * have been or be removed at any point.  @kn->priv is RCU
6782201af4c0STejun Heo 	 * protected for this access.  See css_release_work_fn() for details.
6783201af4c0STejun Heo 	 */
6784e0aed7c7STejun Heo 	cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
6785201af4c0STejun Heo 	if (cgrp)
6786201af4c0STejun Heo 		css = cgroup_css(cgrp, ss);
6787201af4c0STejun Heo 
6788201af4c0STejun Heo 	if (!css || !css_tryget_online(css))
6789201af4c0STejun Heo 		css = ERR_PTR(-ENOENT);
6790201af4c0STejun Heo 
6791201af4c0STejun Heo 	rcu_read_unlock();
6792201af4c0STejun Heo 	return css;
6793201af4c0STejun Heo }
6794201af4c0STejun Heo 
6795201af4c0STejun Heo /**
6796201af4c0STejun Heo  * css_from_id - lookup css by id
6797201af4c0STejun Heo  * @id: the cgroup id
6798201af4c0STejun Heo  * @ss: cgroup subsys to be looked into
6799201af4c0STejun Heo  *
6800201af4c0STejun Heo  * Returns the css if there's valid one with @id, otherwise returns NULL.
6801201af4c0STejun Heo  * Should be called under rcu_read_lock().
6802201af4c0STejun Heo  */
css_from_id(int id,struct cgroup_subsys * ss)6803201af4c0STejun Heo struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
6804201af4c0STejun Heo {
6805201af4c0STejun Heo 	WARN_ON_ONCE(!rcu_read_lock_held());
6806201af4c0STejun Heo 	return idr_find(&ss->css_idr, id);
6807201af4c0STejun Heo }
6808201af4c0STejun Heo 
6809201af4c0STejun Heo /**
6810201af4c0STejun Heo  * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
6811201af4c0STejun Heo  * @path: path on the default hierarchy
6812201af4c0STejun Heo  *
6813201af4c0STejun Heo  * Find the cgroup at @path on the default hierarchy, increment its
6814201af4c0STejun Heo  * reference count and return it.  Returns pointer to the found cgroup on
6815be288169SShakeel Butt  * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already
6816be288169SShakeel Butt  * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory.
6817201af4c0STejun Heo  */
cgroup_get_from_path(const char * path)6818201af4c0STejun Heo struct cgroup *cgroup_get_from_path(const char *path)
6819201af4c0STejun Heo {
6820201af4c0STejun Heo 	struct kernfs_node *kn;
6821be288169SShakeel Butt 	struct cgroup *cgrp = ERR_PTR(-ENOENT);
682274e4b956SMichal Koutný 	struct cgroup *root_cgrp;
6823201af4c0STejun Heo 
682446307fd6SMichal Koutný 	root_cgrp = current_cgns_cgroup_dfl();
682574e4b956SMichal Koutný 	kn = kernfs_walk_and_get(root_cgrp->kn, path);
6826be288169SShakeel Butt 	if (!kn)
6827be288169SShakeel Butt 		goto out;
6828be288169SShakeel Butt 
6829be288169SShakeel Butt 	if (kernfs_type(kn) != KERNFS_DIR) {
6830201af4c0STejun Heo 		cgrp = ERR_PTR(-ENOTDIR);
6831be288169SShakeel Butt 		goto out_kernfs;
6832201af4c0STejun Heo 	}
6833201af4c0STejun Heo 
6834be288169SShakeel Butt 	rcu_read_lock();
6835be288169SShakeel Butt 
6836be288169SShakeel Butt 	cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
6837be288169SShakeel Butt 	if (!cgrp || !cgroup_tryget(cgrp))
6838be288169SShakeel Butt 		cgrp = ERR_PTR(-ENOENT);
6839be288169SShakeel Butt 
6840be288169SShakeel Butt 	rcu_read_unlock();
6841be288169SShakeel Butt 
6842be288169SShakeel Butt out_kernfs:
6843be288169SShakeel Butt 	kernfs_put(kn);
6844be288169SShakeel Butt out:
6845201af4c0STejun Heo 	return cgrp;
6846201af4c0STejun Heo }
6847201af4c0STejun Heo EXPORT_SYMBOL_GPL(cgroup_get_from_path);
6848201af4c0STejun Heo 
6849201af4c0STejun Heo /**
6850b675d4bdSYosry Ahmed  * cgroup_v1v2_get_from_fd - get a cgroup pointer from a fd
6851a6d1ce59SYosry Ahmed  * @fd: fd obtained by open(cgroup_dir)
6852201af4c0STejun Heo  *
6853201af4c0STejun Heo  * Find the cgroup from a fd which should be obtained
6854201af4c0STejun Heo  * by opening a cgroup directory.  Returns a pointer to the
6855201af4c0STejun Heo  * cgroup on success. ERR_PTR is returned if the cgroup
6856201af4c0STejun Heo  * cannot be found.
6857201af4c0STejun Heo  */
cgroup_v1v2_get_from_fd(int fd)6858a6d1ce59SYosry Ahmed struct cgroup *cgroup_v1v2_get_from_fd(int fd)
6859201af4c0STejun Heo {
6860201af4c0STejun Heo 	struct cgroup *cgrp;
68612f31fa02SAl Viro 	struct fd f = fdget_raw(fd);
68622f31fa02SAl Viro 	if (!f.file)
6863201af4c0STejun Heo 		return ERR_PTR(-EBADF);
6864201af4c0STejun Heo 
68652f31fa02SAl Viro 	cgrp = cgroup_v1v2_get_from_file(f.file);
68662f31fa02SAl Viro 	fdput(f);
6867201af4c0STejun Heo 	return cgrp;
6868201af4c0STejun Heo }
6869a6d1ce59SYosry Ahmed 
6870a6d1ce59SYosry Ahmed /**
6871a6d1ce59SYosry Ahmed  * cgroup_get_from_fd - same as cgroup_v1v2_get_from_fd, but only supports
6872a6d1ce59SYosry Ahmed  * cgroup2.
6873b675d4bdSYosry Ahmed  * @fd: fd obtained by open(cgroup2_dir)
6874a6d1ce59SYosry Ahmed  */
cgroup_get_from_fd(int fd)6875a6d1ce59SYosry Ahmed struct cgroup *cgroup_get_from_fd(int fd)
6876a6d1ce59SYosry Ahmed {
6877a6d1ce59SYosry Ahmed 	struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd);
6878a6d1ce59SYosry Ahmed 
6879a6d1ce59SYosry Ahmed 	if (IS_ERR(cgrp))
6880a6d1ce59SYosry Ahmed 		return ERR_CAST(cgrp);
6881a6d1ce59SYosry Ahmed 
6882a6d1ce59SYosry Ahmed 	if (!cgroup_on_dfl(cgrp)) {
6883a6d1ce59SYosry Ahmed 		cgroup_put(cgrp);
6884a6d1ce59SYosry Ahmed 		return ERR_PTR(-EBADF);
6885a6d1ce59SYosry Ahmed 	}
6886a6d1ce59SYosry Ahmed 	return cgrp;
6887a6d1ce59SYosry Ahmed }
6888201af4c0STejun Heo EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
6889201af4c0STejun Heo 
power_of_ten(int power)689038cf3a68STejun Heo static u64 power_of_ten(int power)
689138cf3a68STejun Heo {
689238cf3a68STejun Heo 	u64 v = 1;
689338cf3a68STejun Heo 	while (power--)
689438cf3a68STejun Heo 		v *= 10;
689538cf3a68STejun Heo 	return v;
689638cf3a68STejun Heo }
689738cf3a68STejun Heo 
689838cf3a68STejun Heo /**
689938cf3a68STejun Heo  * cgroup_parse_float - parse a floating number
690038cf3a68STejun Heo  * @input: input string
690138cf3a68STejun Heo  * @dec_shift: number of decimal digits to shift
690238cf3a68STejun Heo  * @v: output
690338cf3a68STejun Heo  *
690438cf3a68STejun Heo  * Parse a decimal floating point number in @input and store the result in
690538cf3a68STejun Heo  * @v with decimal point right shifted @dec_shift times.  For example, if
690638cf3a68STejun Heo  * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345.
690738cf3a68STejun Heo  * Returns 0 on success, -errno otherwise.
690838cf3a68STejun Heo  *
690938cf3a68STejun Heo  * There's nothing cgroup specific about this function except that it's
691038cf3a68STejun Heo  * currently the only user.
691138cf3a68STejun Heo  */
cgroup_parse_float(const char * input,unsigned dec_shift,s64 * v)691238cf3a68STejun Heo int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
691338cf3a68STejun Heo {
691438cf3a68STejun Heo 	s64 whole, frac = 0;
691538cf3a68STejun Heo 	int fstart = 0, fend = 0, flen;
691638cf3a68STejun Heo 
691738cf3a68STejun Heo 	if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend))
691838cf3a68STejun Heo 		return -EINVAL;
691938cf3a68STejun Heo 	if (frac < 0)
692038cf3a68STejun Heo 		return -EINVAL;
692138cf3a68STejun Heo 
692238cf3a68STejun Heo 	flen = fend > fstart ? fend - fstart : 0;
692338cf3a68STejun Heo 	if (flen < dec_shift)
692438cf3a68STejun Heo 		frac *= power_of_ten(dec_shift - flen);
692538cf3a68STejun Heo 	else
692638cf3a68STejun Heo 		frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
692738cf3a68STejun Heo 
692838cf3a68STejun Heo 	*v = whole * power_of_ten(dec_shift) + frac;
692938cf3a68STejun Heo 	return 0;
693038cf3a68STejun Heo }
693138cf3a68STejun Heo 
6932201af4c0STejun Heo /*
6933201af4c0STejun Heo  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
6934201af4c0STejun Heo  * definition in cgroup-defs.h.
6935201af4c0STejun Heo  */
6936201af4c0STejun Heo #ifdef CONFIG_SOCK_CGROUP_DATA
6937201af4c0STejun Heo 
cgroup_sk_alloc(struct sock_cgroup_data * skcd)6938201af4c0STejun Heo void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
6939201af4c0STejun Heo {
694078cc316eSDaniel Borkmann 	struct cgroup *cgroup;
6941e876ecc6SShakeel Butt 
6942201af4c0STejun Heo 	rcu_read_lock();
694378cc316eSDaniel Borkmann 	/* Don't associate the sock with unrelated interrupted task's cgroup. */
694478cc316eSDaniel Borkmann 	if (in_interrupt()) {
694578cc316eSDaniel Borkmann 		cgroup = &cgrp_dfl_root.cgrp;
694678cc316eSDaniel Borkmann 		cgroup_get(cgroup);
694778cc316eSDaniel Borkmann 		goto out;
694878cc316eSDaniel Borkmann 	}
694978cc316eSDaniel Borkmann 
6950201af4c0STejun Heo 	while (true) {
6951201af4c0STejun Heo 		struct css_set *cset;
6952201af4c0STejun Heo 
6953201af4c0STejun Heo 		cset = task_css_set(current);
6954201af4c0STejun Heo 		if (likely(cgroup_tryget(cset->dfl_cgrp))) {
695578cc316eSDaniel Borkmann 			cgroup = cset->dfl_cgrp;
6956201af4c0STejun Heo 			break;
6957201af4c0STejun Heo 		}
6958201af4c0STejun Heo 		cpu_relax();
6959201af4c0STejun Heo 	}
696078cc316eSDaniel Borkmann out:
696178cc316eSDaniel Borkmann 	skcd->cgroup = cgroup;
696278cc316eSDaniel Borkmann 	cgroup_bpf_get(cgroup);
6963201af4c0STejun Heo 	rcu_read_unlock();
6964201af4c0STejun Heo }
6965201af4c0STejun Heo 
cgroup_sk_clone(struct sock_cgroup_data * skcd)6966ad0f75e5SCong Wang void cgroup_sk_clone(struct sock_cgroup_data *skcd)
6967ad0f75e5SCong Wang {
69688520e224SDaniel Borkmann 	struct cgroup *cgrp = sock_cgroup_ptr(skcd);
69698520e224SDaniel Borkmann 
6970ad0f75e5SCong Wang 	/*
6971ad0f75e5SCong Wang 	 * We might be cloning a socket which is left in an empty
6972ad0f75e5SCong Wang 	 * cgroup and the cgroup might have already been rmdir'd.
6973ad0f75e5SCong Wang 	 * Don't use cgroup_get_live().
6974ad0f75e5SCong Wang 	 */
69758520e224SDaniel Borkmann 	cgroup_get(cgrp);
69768520e224SDaniel Borkmann 	cgroup_bpf_get(cgrp);
6977ad0f75e5SCong Wang }
6978ad0f75e5SCong Wang 
cgroup_sk_free(struct sock_cgroup_data * skcd)6979201af4c0STejun Heo void cgroup_sk_free(struct sock_cgroup_data *skcd)
6980201af4c0STejun Heo {
69814bfc0bb2SRoman Gushchin 	struct cgroup *cgrp = sock_cgroup_ptr(skcd);
69824bfc0bb2SRoman Gushchin 
69834bfc0bb2SRoman Gushchin 	cgroup_bpf_put(cgrp);
69844bfc0bb2SRoman Gushchin 	cgroup_put(cgrp);
6985201af4c0STejun Heo }
6986201af4c0STejun Heo 
6987201af4c0STejun Heo #endif	/* CONFIG_SOCK_CGROUP_DATA */
6988201af4c0STejun Heo 
698901ee6cfbSRoman Gushchin #ifdef CONFIG_SYSFS
show_delegatable_files(struct cftype * files,char * buf,ssize_t size,const char * prefix)699001ee6cfbSRoman Gushchin static ssize_t show_delegatable_files(struct cftype *files, char *buf,
699101ee6cfbSRoman Gushchin 				      ssize_t size, const char *prefix)
699201ee6cfbSRoman Gushchin {
699301ee6cfbSRoman Gushchin 	struct cftype *cft;
699401ee6cfbSRoman Gushchin 	ssize_t ret = 0;
699501ee6cfbSRoman Gushchin 
699601ee6cfbSRoman Gushchin 	for (cft = files; cft && cft->name[0] != '\0'; cft++) {
699701ee6cfbSRoman Gushchin 		if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
699801ee6cfbSRoman Gushchin 			continue;
699901ee6cfbSRoman Gushchin 
700001ee6cfbSRoman Gushchin 		if (prefix)
700101ee6cfbSRoman Gushchin 			ret += snprintf(buf + ret, size - ret, "%s.", prefix);
700201ee6cfbSRoman Gushchin 
700301ee6cfbSRoman Gushchin 		ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
700401ee6cfbSRoman Gushchin 
70054d9ebbe2SYangtao Li 		if (WARN_ON(ret >= size))
700601ee6cfbSRoman Gushchin 			break;
700701ee6cfbSRoman Gushchin 	}
700801ee6cfbSRoman Gushchin 
700901ee6cfbSRoman Gushchin 	return ret;
701001ee6cfbSRoman Gushchin }
701101ee6cfbSRoman Gushchin 
delegate_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)701201ee6cfbSRoman Gushchin static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
701301ee6cfbSRoman Gushchin 			      char *buf)
701401ee6cfbSRoman Gushchin {
701501ee6cfbSRoman Gushchin 	struct cgroup_subsys *ss;
701601ee6cfbSRoman Gushchin 	int ssid;
701701ee6cfbSRoman Gushchin 	ssize_t ret = 0;
701801ee6cfbSRoman Gushchin 
70198a693f77STejun Heo 	ret = show_delegatable_files(cgroup_base_files, buf + ret,
70208a693f77STejun Heo 				     PAGE_SIZE - ret, NULL);
70218a693f77STejun Heo 	if (cgroup_psi_enabled())
70228a693f77STejun Heo 		ret += show_delegatable_files(cgroup_psi_files, buf + ret,
70238a693f77STejun Heo 					      PAGE_SIZE - ret, NULL);
702401ee6cfbSRoman Gushchin 
702501ee6cfbSRoman Gushchin 	for_each_subsys(ss, ssid)
702601ee6cfbSRoman Gushchin 		ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
702701ee6cfbSRoman Gushchin 					      PAGE_SIZE - ret,
702801ee6cfbSRoman Gushchin 					      cgroup_subsys_name[ssid]);
702901ee6cfbSRoman Gushchin 
703001ee6cfbSRoman Gushchin 	return ret;
703101ee6cfbSRoman Gushchin }
703201ee6cfbSRoman Gushchin static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
703301ee6cfbSRoman Gushchin 
features_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)70345f2e6734SRoman Gushchin static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
70355f2e6734SRoman Gushchin 			     char *buf)
70365f2e6734SRoman Gushchin {
70378a931f80SJohannes Weiner 	return snprintf(buf, PAGE_SIZE,
70388a931f80SJohannes Weiner 			"nsdelegate\n"
70396a010a49STejun Heo 			"favordynmods\n"
70408a931f80SJohannes Weiner 			"memory_localevents\n"
70418a931f80SJohannes Weiner 			"memory_recursiveprot\n");
70425f2e6734SRoman Gushchin }
70435f2e6734SRoman Gushchin static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
70445f2e6734SRoman Gushchin 
704501ee6cfbSRoman Gushchin static struct attribute *cgroup_sysfs_attrs[] = {
704601ee6cfbSRoman Gushchin 	&cgroup_delegate_attr.attr,
70475f2e6734SRoman Gushchin 	&cgroup_features_attr.attr,
704801ee6cfbSRoman Gushchin 	NULL,
704901ee6cfbSRoman Gushchin };
705001ee6cfbSRoman Gushchin 
705101ee6cfbSRoman Gushchin static const struct attribute_group cgroup_sysfs_attr_group = {
705201ee6cfbSRoman Gushchin 	.attrs = cgroup_sysfs_attrs,
705301ee6cfbSRoman Gushchin 	.name = "cgroup",
705401ee6cfbSRoman Gushchin };
705501ee6cfbSRoman Gushchin 
cgroup_sysfs_init(void)705601ee6cfbSRoman Gushchin static int __init cgroup_sysfs_init(void)
705701ee6cfbSRoman Gushchin {
705801ee6cfbSRoman Gushchin 	return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
705901ee6cfbSRoman Gushchin }
706001ee6cfbSRoman Gushchin subsys_initcall(cgroup_sysfs_init);
7061a5e112e6STejun Heo 
706201ee6cfbSRoman Gushchin #endif /* CONFIG_SYSFS */
7063