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