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