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