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