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