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