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