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