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