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