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