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