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