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