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