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