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