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