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