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