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