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