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