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