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