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