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