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