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