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