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