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