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