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