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