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