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