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