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