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