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