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