aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/cgroup.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/cgroup.c')
-rw-r--r--kernel/cgroup.c3734
1 files changed, 1562 insertions, 2172 deletions
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 105f273b6f86..9fcdaa705b6c 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -40,23 +40,20 @@
40#include <linux/proc_fs.h> 40#include <linux/proc_fs.h>
41#include <linux/rcupdate.h> 41#include <linux/rcupdate.h>
42#include <linux/sched.h> 42#include <linux/sched.h>
43#include <linux/backing-dev.h>
44#include <linux/slab.h> 43#include <linux/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h> 44#include <linux/spinlock.h>
45#include <linux/rwsem.h>
47#include <linux/string.h> 46#include <linux/string.h>
48#include <linux/sort.h> 47#include <linux/sort.h>
49#include <linux/kmod.h> 48#include <linux/kmod.h>
50#include <linux/module.h>
51#include <linux/delayacct.h> 49#include <linux/delayacct.h>
52#include <linux/cgroupstats.h> 50#include <linux/cgroupstats.h>
53#include <linux/hashtable.h> 51#include <linux/hashtable.h>
54#include <linux/namei.h>
55#include <linux/pid_namespace.h> 52#include <linux/pid_namespace.h>
56#include <linux/idr.h> 53#include <linux/idr.h>
57#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ 54#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
58#include <linux/flex_array.h> /* used in cgroup_attach_task */
59#include <linux/kthread.h> 55#include <linux/kthread.h>
56#include <linux/delay.h>
60 57
61#include <linux/atomic.h> 58#include <linux/atomic.h>
62 59
@@ -68,43 +65,49 @@
68 */ 65 */
69#define CGROUP_PIDLIST_DESTROY_DELAY HZ 66#define CGROUP_PIDLIST_DESTROY_DELAY HZ
70 67
68#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
69 MAX_CFTYPE_NAME + 2)
70
71/*
72 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
73 * creation/removal and hierarchy changing operations including cgroup
74 * creation, removal, css association and controller rebinding. This outer
75 * lock is needed mainly to resolve the circular dependency between kernfs
76 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
77 */
78static DEFINE_MUTEX(cgroup_tree_mutex);
79
71/* 80/*
72 * cgroup_mutex is the master lock. Any modification to cgroup or its 81 * cgroup_mutex is the master lock. Any modification to cgroup or its
73 * hierarchy must be performed while holding it. 82 * hierarchy must be performed while holding it.
74 * 83 *
75 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify 84 * css_set_rwsem protects task->cgroups pointer, the list of css_set
76 * cgroupfs_root of any cgroup hierarchy - subsys list, flags, 85 * objects, and the chain of tasks off each css_set.
77 * release_agent_path and so on. Modifying requires both cgroup_mutex and
78 * cgroup_root_mutex. Readers can acquire either of the two. This is to
79 * break the following locking order cycle.
80 *
81 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
82 * B. namespace_sem -> cgroup_mutex
83 * 86 *
84 * B happens only through cgroup_show_options() and using cgroup_root_mutex 87 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
85 * breaks it. 88 * cgroup.h can use them for lockdep annotations.
86 */ 89 */
87#ifdef CONFIG_PROVE_RCU 90#ifdef CONFIG_PROVE_RCU
88DEFINE_MUTEX(cgroup_mutex); 91DEFINE_MUTEX(cgroup_mutex);
89EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ 92DECLARE_RWSEM(css_set_rwsem);
93EXPORT_SYMBOL_GPL(cgroup_mutex);
94EXPORT_SYMBOL_GPL(css_set_rwsem);
90#else 95#else
91static DEFINE_MUTEX(cgroup_mutex); 96static DEFINE_MUTEX(cgroup_mutex);
97static DECLARE_RWSEM(css_set_rwsem);
92#endif 98#endif
93 99
94static DEFINE_MUTEX(cgroup_root_mutex); 100/*
101 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
102 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
103 */
104static DEFINE_SPINLOCK(release_agent_path_lock);
95 105
96#define cgroup_assert_mutex_or_rcu_locked() \ 106#define cgroup_assert_mutexes_or_rcu_locked() \
97 rcu_lockdep_assert(rcu_read_lock_held() || \ 107 rcu_lockdep_assert(rcu_read_lock_held() || \
108 lockdep_is_held(&cgroup_tree_mutex) || \
98 lockdep_is_held(&cgroup_mutex), \ 109 lockdep_is_held(&cgroup_mutex), \
99 "cgroup_mutex or RCU read lock required"); 110 "cgroup_[tree_]mutex or RCU read lock required");
100
101#ifdef CONFIG_LOCKDEP
102#define cgroup_assert_mutex_or_root_locked() \
103 WARN_ON_ONCE(debug_locks && (!lockdep_is_held(&cgroup_mutex) && \
104 !lockdep_is_held(&cgroup_root_mutex)))
105#else
106#define cgroup_assert_mutex_or_root_locked() do { } while (0)
107#endif
108 111
109/* 112/*
110 * cgroup destruction makes heavy use of work items and there can be a lot 113 * cgroup destruction makes heavy use of work items and there can be a lot
@@ -120,42 +123,41 @@ static struct workqueue_struct *cgroup_destroy_wq;
120 */ 123 */
121static struct workqueue_struct *cgroup_pidlist_destroy_wq; 124static struct workqueue_struct *cgroup_pidlist_destroy_wq;
122 125
123/* 126/* generate an array of cgroup subsystem pointers */
124 * Generate an array of cgroup subsystem pointers. At boot time, this is 127#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
125 * populated with the built in subsystems, and modular subsystems are 128static struct cgroup_subsys *cgroup_subsys[] = {
126 * registered after that. The mutable section of this array is protected by 129#include <linux/cgroup_subsys.h>
127 * cgroup_mutex. 130};
128 */ 131#undef SUBSYS
129#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, 132
130#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) 133/* array of cgroup subsystem names */
131static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = { 134#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
135static const char *cgroup_subsys_name[] = {
132#include <linux/cgroup_subsys.h> 136#include <linux/cgroup_subsys.h>
133}; 137};
138#undef SUBSYS
134 139
135/* 140/*
136 * The dummy hierarchy, reserved for the subsystems that are otherwise 141 * The default hierarchy, reserved for the subsystems that are otherwise
137 * unattached - it never has more than a single cgroup, and all tasks are 142 * unattached - it never has more than a single cgroup, and all tasks are
138 * part of that cgroup. 143 * part of that cgroup.
139 */ 144 */
140static struct cgroupfs_root cgroup_dummy_root; 145struct cgroup_root cgrp_dfl_root;
141 146
142/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ 147/*
143static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; 148 * The default hierarchy always exists but is hidden until mounted for the
149 * first time. This is for backward compatibility.
150 */
151static bool cgrp_dfl_root_visible;
144 152
145/* The list of hierarchy roots */ 153/* The list of hierarchy roots */
146 154
147static LIST_HEAD(cgroup_roots); 155static LIST_HEAD(cgroup_roots);
148static int cgroup_root_count; 156static int cgroup_root_count;
149 157
150/* 158/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
151 * Hierarchy ID allocation and mapping. It follows the same exclusion
152 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
153 * writes, either for reads.
154 */
155static DEFINE_IDR(cgroup_hierarchy_idr); 159static DEFINE_IDR(cgroup_hierarchy_idr);
156 160
157static struct cgroup_name root_cgroup_name = { .name = "/" };
158
159/* 161/*
160 * Assign a monotonically increasing serial number to cgroups. It 162 * Assign a monotonically increasing serial number to cgroups. It
161 * guarantees cgroups with bigger numbers are newer than those with smaller 163 * guarantees cgroups with bigger numbers are newer than those with smaller
@@ -175,11 +177,13 @@ static int need_forkexit_callback __read_mostly;
175 177
176static struct cftype cgroup_base_files[]; 178static struct cftype cgroup_base_files[];
177 179
180static void cgroup_put(struct cgroup *cgrp);
181static int rebind_subsystems(struct cgroup_root *dst_root,
182 unsigned long ss_mask);
178static void cgroup_destroy_css_killed(struct cgroup *cgrp); 183static void cgroup_destroy_css_killed(struct cgroup *cgrp);
179static int cgroup_destroy_locked(struct cgroup *cgrp); 184static int cgroup_destroy_locked(struct cgroup *cgrp);
180static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], 185static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
181 bool is_add); 186 bool is_add);
182static int cgroup_file_release(struct inode *inode, struct file *file);
183static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); 187static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
184 188
185/** 189/**
@@ -197,8 +201,9 @@ static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
197 struct cgroup_subsys *ss) 201 struct cgroup_subsys *ss)
198{ 202{
199 if (ss) 203 if (ss)
200 return rcu_dereference_check(cgrp->subsys[ss->subsys_id], 204 return rcu_dereference_check(cgrp->subsys[ss->id],
201 lockdep_is_held(&cgroup_mutex)); 205 lockdep_is_held(&cgroup_tree_mutex) ||
206 lockdep_is_held(&cgroup_mutex));
202 else 207 else
203 return &cgrp->dummy_css; 208 return &cgrp->dummy_css;
204} 209}
@@ -209,6 +214,27 @@ static inline bool cgroup_is_dead(const struct cgroup *cgrp)
209 return test_bit(CGRP_DEAD, &cgrp->flags); 214 return test_bit(CGRP_DEAD, &cgrp->flags);
210} 215}
211 216
217struct cgroup_subsys_state *seq_css(struct seq_file *seq)
218{
219 struct kernfs_open_file *of = seq->private;
220 struct cgroup *cgrp = of->kn->parent->priv;
221 struct cftype *cft = seq_cft(seq);
222
223 /*
224 * This is open and unprotected implementation of cgroup_css().
225 * seq_css() is only called from a kernfs file operation which has
226 * an active reference on the file. Because all the subsystem
227 * files are drained before a css is disassociated with a cgroup,
228 * the matching css from the cgroup's subsys table is guaranteed to
229 * be and stay valid until the enclosing operation is complete.
230 */
231 if (cft->ss)
232 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
233 else
234 return &cgrp->dummy_css;
235}
236EXPORT_SYMBOL_GPL(seq_css);
237
212/** 238/**
213 * cgroup_is_descendant - test ancestry 239 * cgroup_is_descendant - test ancestry
214 * @cgrp: the cgroup to be tested 240 * @cgrp: the cgroup to be tested
@@ -227,7 +253,6 @@ bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
227 } 253 }
228 return false; 254 return false;
229} 255}
230EXPORT_SYMBOL_GPL(cgroup_is_descendant);
231 256
232static int cgroup_is_releasable(const struct cgroup *cgrp) 257static int cgroup_is_releasable(const struct cgroup *cgrp)
233{ 258{
@@ -254,54 +279,23 @@ static int notify_on_release(const struct cgroup *cgrp)
254 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ 279 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
255 if (!((css) = rcu_dereference_check( \ 280 if (!((css) = rcu_dereference_check( \
256 (cgrp)->subsys[(ssid)], \ 281 (cgrp)->subsys[(ssid)], \
282 lockdep_is_held(&cgroup_tree_mutex) || \
257 lockdep_is_held(&cgroup_mutex)))) { } \ 283 lockdep_is_held(&cgroup_mutex)))) { } \
258 else 284 else
259 285
260/** 286/**
261 * for_each_subsys - iterate all loaded cgroup subsystems 287 * for_each_subsys - iterate all enabled cgroup subsystems
262 * @ss: the iteration cursor 288 * @ss: the iteration cursor
263 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end 289 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
264 *
265 * Iterates through all loaded subsystems. Should be called under
266 * cgroup_mutex or cgroup_root_mutex.
267 */ 290 */
268#define for_each_subsys(ss, ssid) \ 291#define for_each_subsys(ss, ssid) \
269 for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; }); \ 292 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
270 (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ 293 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
271 if (!((ss) = cgroup_subsys[(ssid)])) { } \
272 else
273 294
274/** 295/* iterate across the hierarchies */
275 * for_each_builtin_subsys - iterate all built-in cgroup subsystems 296#define for_each_root(root) \
276 * @ss: the iteration cursor
277 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
278 *
279 * Bulit-in subsystems are always present and iteration itself doesn't
280 * require any synchronization.
281 */
282#define for_each_builtin_subsys(ss, i) \
283 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
284 (((ss) = cgroup_subsys[i]) || true); (i)++)
285
286/* iterate across the active hierarchies */
287#define for_each_active_root(root) \
288 list_for_each_entry((root), &cgroup_roots, root_list) 297 list_for_each_entry((root), &cgroup_roots, root_list)
289 298
290static inline struct cgroup *__d_cgrp(struct dentry *dentry)
291{
292 return dentry->d_fsdata;
293}
294
295static inline struct cfent *__d_cfe(struct dentry *dentry)
296{
297 return dentry->d_fsdata;
298}
299
300static inline struct cftype *__d_cft(struct dentry *dentry)
301{
302 return __d_cfe(dentry)->type;
303}
304
305/** 299/**
306 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. 300 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
307 * @cgrp: the cgroup to be checked for liveness 301 * @cgrp: the cgroup to be checked for liveness
@@ -347,23 +341,23 @@ struct cgrp_cset_link {
347 struct list_head cgrp_link; 341 struct list_head cgrp_link;
348}; 342};
349 343
350/* The default css_set - used by init and its children prior to any 344/*
345 * The default css_set - used by init and its children prior to any
351 * hierarchies being mounted. It contains a pointer to the root state 346 * hierarchies being mounted. It contains a pointer to the root state
352 * for each subsystem. Also used to anchor the list of css_sets. Not 347 * for each subsystem. Also used to anchor the list of css_sets. Not
353 * reference-counted, to improve performance when child cgroups 348 * reference-counted, to improve performance when child cgroups
354 * haven't been created. 349 * haven't been created.
355 */ 350 */
351static struct css_set init_css_set = {
352 .refcount = ATOMIC_INIT(1),
353 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
354 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
355 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
356 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
357 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
358};
356 359
357static struct css_set init_css_set; 360static int css_set_count = 1; /* 1 for init_css_set */
358static struct cgrp_cset_link init_cgrp_cset_link;
359
360/*
361 * css_set_lock protects the list of css_set objects, and the chain of
362 * tasks off each css_set. Nests outside task->alloc_lock due to
363 * css_task_iter_start().
364 */
365static DEFINE_RWLOCK(css_set_lock);
366static int css_set_count;
367 361
368/* 362/*
369 * hash table for cgroup groups. This improves the performance to find 363 * hash table for cgroup groups. This improves the performance to find
@@ -386,30 +380,14 @@ static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
386 return key; 380 return key;
387} 381}
388 382
389/* 383static void put_css_set_locked(struct css_set *cset, bool taskexit)
390 * We don't maintain the lists running through each css_set to its task
391 * until after the first call to css_task_iter_start(). This reduces the
392 * fork()/exit() overhead for people who have cgroups compiled into their
393 * kernel but not actually in use.
394 */
395static int use_task_css_set_links __read_mostly;
396
397static void __put_css_set(struct css_set *cset, int taskexit)
398{ 384{
399 struct cgrp_cset_link *link, *tmp_link; 385 struct cgrp_cset_link *link, *tmp_link;
400 386
401 /* 387 lockdep_assert_held(&css_set_rwsem);
402 * Ensure that the refcount doesn't hit zero while any readers 388
403 * can see it. Similar to atomic_dec_and_lock(), but for an 389 if (!atomic_dec_and_test(&cset->refcount))
404 * rwlock
405 */
406 if (atomic_add_unless(&cset->refcount, -1, 1))
407 return;
408 write_lock(&css_set_lock);
409 if (!atomic_dec_and_test(&cset->refcount)) {
410 write_unlock(&css_set_lock);
411 return; 390 return;
412 }
413 391
414 /* This css_set is dead. unlink it and release cgroup refcounts */ 392 /* This css_set is dead. unlink it and release cgroup refcounts */
415 hash_del(&cset->hlist); 393 hash_del(&cset->hlist);
@@ -421,7 +399,7 @@ static void __put_css_set(struct css_set *cset, int taskexit)
421 list_del(&link->cset_link); 399 list_del(&link->cset_link);
422 list_del(&link->cgrp_link); 400 list_del(&link->cgrp_link);
423 401
424 /* @cgrp can't go away while we're holding css_set_lock */ 402 /* @cgrp can't go away while we're holding css_set_rwsem */
425 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { 403 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
426 if (taskexit) 404 if (taskexit)
427 set_bit(CGRP_RELEASABLE, &cgrp->flags); 405 set_bit(CGRP_RELEASABLE, &cgrp->flags);
@@ -431,10 +409,24 @@ static void __put_css_set(struct css_set *cset, int taskexit)
431 kfree(link); 409 kfree(link);
432 } 410 }
433 411
434 write_unlock(&css_set_lock);
435 kfree_rcu(cset, rcu_head); 412 kfree_rcu(cset, rcu_head);
436} 413}
437 414
415static void put_css_set(struct css_set *cset, bool taskexit)
416{
417 /*
418 * Ensure that the refcount doesn't hit zero while any readers
419 * can see it. Similar to atomic_dec_and_lock(), but for an
420 * rwlock
421 */
422 if (atomic_add_unless(&cset->refcount, -1, 1))
423 return;
424
425 down_write(&css_set_rwsem);
426 put_css_set_locked(cset, taskexit);
427 up_write(&css_set_rwsem);
428}
429
438/* 430/*
439 * refcounted get/put for css_set objects 431 * refcounted get/put for css_set objects
440 */ 432 */
@@ -443,16 +435,6 @@ static inline void get_css_set(struct css_set *cset)
443 atomic_inc(&cset->refcount); 435 atomic_inc(&cset->refcount);
444} 436}
445 437
446static inline void put_css_set(struct css_set *cset)
447{
448 __put_css_set(cset, 0);
449}
450
451static inline void put_css_set_taskexit(struct css_set *cset)
452{
453 __put_css_set(cset, 1);
454}
455
456/** 438/**
457 * compare_css_sets - helper function for find_existing_css_set(). 439 * compare_css_sets - helper function for find_existing_css_set().
458 * @cset: candidate css_set being tested 440 * @cset: candidate css_set being tested
@@ -535,7 +517,7 @@ static struct css_set *find_existing_css_set(struct css_set *old_cset,
535 struct cgroup *cgrp, 517 struct cgroup *cgrp,
536 struct cgroup_subsys_state *template[]) 518 struct cgroup_subsys_state *template[])
537{ 519{
538 struct cgroupfs_root *root = cgrp->root; 520 struct cgroup_root *root = cgrp->root;
539 struct cgroup_subsys *ss; 521 struct cgroup_subsys *ss;
540 struct css_set *cset; 522 struct css_set *cset;
541 unsigned long key; 523 unsigned long key;
@@ -547,7 +529,7 @@ static struct css_set *find_existing_css_set(struct css_set *old_cset,
547 * won't change, so no need for locking. 529 * won't change, so no need for locking.
548 */ 530 */
549 for_each_subsys(ss, i) { 531 for_each_subsys(ss, i) {
550 if (root->subsys_mask & (1UL << i)) { 532 if (root->cgrp.subsys_mask & (1UL << i)) {
551 /* Subsystem is in this hierarchy. So we want 533 /* Subsystem is in this hierarchy. So we want
552 * the subsystem state from the new 534 * the subsystem state from the new
553 * cgroup */ 535 * cgroup */
@@ -652,11 +634,11 @@ static struct css_set *find_css_set(struct css_set *old_cset,
652 634
653 /* First see if we already have a cgroup group that matches 635 /* First see if we already have a cgroup group that matches
654 * the desired set */ 636 * the desired set */
655 read_lock(&css_set_lock); 637 down_read(&css_set_rwsem);
656 cset = find_existing_css_set(old_cset, cgrp, template); 638 cset = find_existing_css_set(old_cset, cgrp, template);
657 if (cset) 639 if (cset)
658 get_css_set(cset); 640 get_css_set(cset);
659 read_unlock(&css_set_lock); 641 up_read(&css_set_rwsem);
660 642
661 if (cset) 643 if (cset)
662 return cset; 644 return cset;
@@ -674,13 +656,16 @@ static struct css_set *find_css_set(struct css_set *old_cset,
674 atomic_set(&cset->refcount, 1); 656 atomic_set(&cset->refcount, 1);
675 INIT_LIST_HEAD(&cset->cgrp_links); 657 INIT_LIST_HEAD(&cset->cgrp_links);
676 INIT_LIST_HEAD(&cset->tasks); 658 INIT_LIST_HEAD(&cset->tasks);
659 INIT_LIST_HEAD(&cset->mg_tasks);
660 INIT_LIST_HEAD(&cset->mg_preload_node);
661 INIT_LIST_HEAD(&cset->mg_node);
677 INIT_HLIST_NODE(&cset->hlist); 662 INIT_HLIST_NODE(&cset->hlist);
678 663
679 /* Copy the set of subsystem state objects generated in 664 /* Copy the set of subsystem state objects generated in
680 * find_existing_css_set() */ 665 * find_existing_css_set() */
681 memcpy(cset->subsys, template, sizeof(cset->subsys)); 666 memcpy(cset->subsys, template, sizeof(cset->subsys));
682 667
683 write_lock(&css_set_lock); 668 down_write(&css_set_rwsem);
684 /* Add reference counts and links from the new css_set. */ 669 /* Add reference counts and links from the new css_set. */
685 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { 670 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
686 struct cgroup *c = link->cgrp; 671 struct cgroup *c = link->cgrp;
@@ -698,31 +683,105 @@ static struct css_set *find_css_set(struct css_set *old_cset,
698 key = css_set_hash(cset->subsys); 683 key = css_set_hash(cset->subsys);
699 hash_add(css_set_table, &cset->hlist, key); 684 hash_add(css_set_table, &cset->hlist, key);
700 685
701 write_unlock(&css_set_lock); 686 up_write(&css_set_rwsem);
702 687
703 return cset; 688 return cset;
704} 689}
705 690
706/* 691static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
707 * Return the cgroup for "task" from the given hierarchy. Must be
708 * called with cgroup_mutex held.
709 */
710static struct cgroup *task_cgroup_from_root(struct task_struct *task,
711 struct cgroupfs_root *root)
712{ 692{
713 struct css_set *cset; 693 struct cgroup *root_cgrp = kf_root->kn->priv;
714 struct cgroup *res = NULL; 694
695 return root_cgrp->root;
696}
697
698static int cgroup_init_root_id(struct cgroup_root *root)
699{
700 int id;
701
702 lockdep_assert_held(&cgroup_mutex);
703
704 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
705 if (id < 0)
706 return id;
707
708 root->hierarchy_id = id;
709 return 0;
710}
711
712static void cgroup_exit_root_id(struct cgroup_root *root)
713{
714 lockdep_assert_held(&cgroup_mutex);
715
716 if (root->hierarchy_id) {
717 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
718 root->hierarchy_id = 0;
719 }
720}
721
722static void cgroup_free_root(struct cgroup_root *root)
723{
724 if (root) {
725 /* hierarhcy ID shoulid already have been released */
726 WARN_ON_ONCE(root->hierarchy_id);
727
728 idr_destroy(&root->cgroup_idr);
729 kfree(root);
730 }
731}
732
733static void cgroup_destroy_root(struct cgroup_root *root)
734{
735 struct cgroup *cgrp = &root->cgrp;
736 struct cgrp_cset_link *link, *tmp_link;
737
738 mutex_lock(&cgroup_tree_mutex);
739 mutex_lock(&cgroup_mutex);
740
741 BUG_ON(atomic_read(&root->nr_cgrps));
742 BUG_ON(!list_empty(&cgrp->children));
743
744 /* Rebind all subsystems back to the default hierarchy */
745 rebind_subsystems(&cgrp_dfl_root, cgrp->subsys_mask);
715 746
716 BUG_ON(!mutex_is_locked(&cgroup_mutex));
717 read_lock(&css_set_lock);
718 /* 747 /*
719 * No need to lock the task - since we hold cgroup_mutex the 748 * Release all the links from cset_links to this hierarchy's
720 * task can't change groups, so the only thing that can happen 749 * root cgroup
721 * is that it exits and its css is set back to init_css_set.
722 */ 750 */
723 cset = task_css_set(task); 751 down_write(&css_set_rwsem);
752
753 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
754 list_del(&link->cset_link);
755 list_del(&link->cgrp_link);
756 kfree(link);
757 }
758 up_write(&css_set_rwsem);
759
760 if (!list_empty(&root->root_list)) {
761 list_del(&root->root_list);
762 cgroup_root_count--;
763 }
764
765 cgroup_exit_root_id(root);
766
767 mutex_unlock(&cgroup_mutex);
768 mutex_unlock(&cgroup_tree_mutex);
769
770 kernfs_destroy_root(root->kf_root);
771 cgroup_free_root(root);
772}
773
774/* look up cgroup associated with given css_set on the specified hierarchy */
775static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
776 struct cgroup_root *root)
777{
778 struct cgroup *res = NULL;
779
780 lockdep_assert_held(&cgroup_mutex);
781 lockdep_assert_held(&css_set_rwsem);
782
724 if (cset == &init_css_set) { 783 if (cset == &init_css_set) {
725 res = &root->top_cgroup; 784 res = &root->cgrp;
726 } else { 785 } else {
727 struct cgrp_cset_link *link; 786 struct cgrp_cset_link *link;
728 787
@@ -735,16 +794,27 @@ static struct cgroup *task_cgroup_from_root(struct task_struct *task,
735 } 794 }
736 } 795 }
737 } 796 }
738 read_unlock(&css_set_lock); 797
739 BUG_ON(!res); 798 BUG_ON(!res);
740 return res; 799 return res;
741} 800}
742 801
743/* 802/*
744 * There is one global cgroup mutex. We also require taking 803 * Return the cgroup for "task" from the given hierarchy. Must be
745 * task_lock() when dereferencing a task's cgroup subsys pointers. 804 * called with cgroup_mutex and css_set_rwsem held.
746 * See "The task_lock() exception", at the end of this comment. 805 */
747 * 806static struct cgroup *task_cgroup_from_root(struct task_struct *task,
807 struct cgroup_root *root)
808{
809 /*
810 * No need to lock the task - since we hold cgroup_mutex the
811 * task can't change groups, so the only thing that can happen
812 * is that it exits and its css is set back to init_css_set.
813 */
814 return cset_cgroup_from_root(task_css_set(task), root);
815}
816
817/*
748 * A task must hold cgroup_mutex to modify cgroups. 818 * A task must hold cgroup_mutex to modify cgroups.
749 * 819 *
750 * Any task can increment and decrement the count field without lock. 820 * Any task can increment and decrement the count field without lock.
@@ -770,98 +840,79 @@ static struct cgroup *task_cgroup_from_root(struct task_struct *task,
770 * A cgroup can only be deleted if both its 'count' of using tasks 840 * A cgroup can only be deleted if both its 'count' of using tasks
771 * is zero, and its list of 'children' cgroups is empty. Since all 841 * is zero, and its list of 'children' cgroups is empty. Since all
772 * tasks in the system use _some_ cgroup, and since there is always at 842 * tasks in the system use _some_ cgroup, and since there is always at
773 * least one task in the system (init, pid == 1), therefore, top_cgroup 843 * least one task in the system (init, pid == 1), therefore, root cgroup
774 * always has either children cgroups and/or using tasks. So we don't 844 * always has either children cgroups and/or using tasks. So we don't
775 * need a special hack to ensure that top_cgroup cannot be deleted. 845 * need a special hack to ensure that root cgroup cannot be deleted.
776 *
777 * The task_lock() exception
778 *
779 * The need for this exception arises from the action of
780 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
781 * another. It does so using cgroup_mutex, however there are
782 * several performance critical places that need to reference
783 * task->cgroup without the expense of grabbing a system global
784 * mutex. Therefore except as noted below, when dereferencing or, as
785 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
786 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
787 * the task_struct routinely used for such matters.
788 * 846 *
789 * P.S. One more locking exception. RCU is used to guard the 847 * P.S. One more locking exception. RCU is used to guard the
790 * update of a tasks cgroup pointer by cgroup_attach_task() 848 * update of a tasks cgroup pointer by cgroup_attach_task()
791 */ 849 */
792 850
793/*
794 * A couple of forward declarations required, due to cyclic reference loop:
795 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
796 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
797 * -> cgroup_mkdir.
798 */
799
800static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
801static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
802static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); 851static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
803static const struct inode_operations cgroup_dir_inode_operations; 852static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
804static const struct file_operations proc_cgroupstats_operations; 853static const struct file_operations proc_cgroupstats_operations;
805 854
806static struct backing_dev_info cgroup_backing_dev_info = { 855static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
807 .name = "cgroup", 856 char *buf)
808 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
809};
810
811static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
812{ 857{
813 struct inode *inode = new_inode(sb); 858 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
814 859 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
815 if (inode) { 860 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
816 inode->i_ino = get_next_ino(); 861 cft->ss->name, cft->name);
817 inode->i_mode = mode; 862 else
818 inode->i_uid = current_fsuid(); 863 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
819 inode->i_gid = current_fsgid(); 864 return buf;
820 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
821 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
822 }
823 return inode;
824} 865}
825 866
826static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) 867/**
868 * cgroup_file_mode - deduce file mode of a control file
869 * @cft: the control file in question
870 *
871 * returns cft->mode if ->mode is not 0
872 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
873 * returns S_IRUGO if it has only a read handler
874 * returns S_IWUSR if it has only a write hander
875 */
876static umode_t cgroup_file_mode(const struct cftype *cft)
827{ 877{
828 struct cgroup_name *name; 878 umode_t mode = 0;
829 879
830 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); 880 if (cft->mode)
831 if (!name) 881 return cft->mode;
832 return NULL; 882
833 strcpy(name->name, dentry->d_name.name); 883 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
834 return name; 884 mode |= S_IRUGO;
885
886 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
887 cft->trigger)
888 mode |= S_IWUSR;
889
890 return mode;
835} 891}
836 892
837static void cgroup_free_fn(struct work_struct *work) 893static void cgroup_free_fn(struct work_struct *work)
838{ 894{
839 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); 895 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
840 896
841 mutex_lock(&cgroup_mutex); 897 atomic_dec(&cgrp->root->nr_cgrps);
842 cgrp->root->number_of_cgroups--;
843 mutex_unlock(&cgroup_mutex);
844
845 /*
846 * We get a ref to the parent's dentry, and put the ref when
847 * this cgroup is being freed, so it's guaranteed that the
848 * parent won't be destroyed before its children.
849 */
850 dput(cgrp->parent->dentry);
851
852 /*
853 * Drop the active superblock reference that we took when we
854 * created the cgroup. This will free cgrp->root, if we are
855 * holding the last reference to @sb.
856 */
857 deactivate_super(cgrp->root->sb);
858
859 cgroup_pidlist_destroy_all(cgrp); 898 cgroup_pidlist_destroy_all(cgrp);
860 899
861 simple_xattrs_free(&cgrp->xattrs); 900 if (cgrp->parent) {
862 901 /*
863 kfree(rcu_dereference_raw(cgrp->name)); 902 * We get a ref to the parent, and put the ref when this
864 kfree(cgrp); 903 * cgroup is being freed, so it's guaranteed that the
904 * parent won't be destroyed before its children.
905 */
906 cgroup_put(cgrp->parent);
907 kernfs_put(cgrp->kn);
908 kfree(cgrp);
909 } else {
910 /*
911 * This is root cgroup's refcnt reaching zero, which
912 * indicates that the root should be released.
913 */
914 cgroup_destroy_root(cgrp->root);
915 }
865} 916}
866 917
867static void cgroup_free_rcu(struct rcu_head *head) 918static void cgroup_free_rcu(struct rcu_head *head)
@@ -872,73 +923,40 @@ static void cgroup_free_rcu(struct rcu_head *head)
872 queue_work(cgroup_destroy_wq, &cgrp->destroy_work); 923 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
873} 924}
874 925
875static void cgroup_diput(struct dentry *dentry, struct inode *inode) 926static void cgroup_get(struct cgroup *cgrp)
876{
877 /* is dentry a directory ? if so, kfree() associated cgroup */
878 if (S_ISDIR(inode->i_mode)) {
879 struct cgroup *cgrp = dentry->d_fsdata;
880
881 BUG_ON(!(cgroup_is_dead(cgrp)));
882
883 /*
884 * XXX: cgrp->id is only used to look up css's. As cgroup
885 * and css's lifetimes will be decoupled, it should be made
886 * per-subsystem and moved to css->id so that lookups are
887 * successful until the target css is released.
888 */
889 mutex_lock(&cgroup_mutex);
890 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
891 mutex_unlock(&cgroup_mutex);
892 cgrp->id = -1;
893
894 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
895 } else {
896 struct cfent *cfe = __d_cfe(dentry);
897 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
898
899 WARN_ONCE(!list_empty(&cfe->node) &&
900 cgrp != &cgrp->root->top_cgroup,
901 "cfe still linked for %s\n", cfe->type->name);
902 simple_xattrs_free(&cfe->xattrs);
903 kfree(cfe);
904 }
905 iput(inode);
906}
907
908static void remove_dir(struct dentry *d)
909{ 927{
910 struct dentry *parent = dget(d->d_parent); 928 WARN_ON_ONCE(cgroup_is_dead(cgrp));
911 929 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
912 d_delete(d); 930 atomic_inc(&cgrp->refcnt);
913 simple_rmdir(parent->d_inode, d);
914 dput(parent);
915} 931}
916 932
917static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) 933static void cgroup_put(struct cgroup *cgrp)
918{ 934{
919 struct cfent *cfe; 935 if (!atomic_dec_and_test(&cgrp->refcnt))
920 936 return;
921 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); 937 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
922 lockdep_assert_held(&cgroup_mutex); 938 return;
923 939
924 /* 940 /*
925 * If we're doing cleanup due to failure of cgroup_create(), 941 * XXX: cgrp->id is only used to look up css's. As cgroup and
926 * the corresponding @cfe may not exist. 942 * css's lifetimes will be decoupled, it should be made
943 * per-subsystem and moved to css->id so that lookups are
944 * successful until the target css is released.
927 */ 945 */
928 list_for_each_entry(cfe, &cgrp->files, node) { 946 mutex_lock(&cgroup_mutex);
929 struct dentry *d = cfe->dentry; 947 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
948 mutex_unlock(&cgroup_mutex);
949 cgrp->id = -1;
930 950
931 if (cft && cfe->type != cft) 951 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
932 continue; 952}
933 953
934 dget(d); 954static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
935 d_delete(d); 955{
936 simple_unlink(cgrp->dentry->d_inode, d); 956 char name[CGROUP_FILE_NAME_MAX];
937 list_del_init(&cfe->node);
938 dput(d);
939 957
940 break; 958 lockdep_assert_held(&cgroup_tree_mutex);
941 } 959 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
942} 960}
943 961
944/** 962/**
@@ -952,144 +970,106 @@ static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
952 int i; 970 int i;
953 971
954 for_each_subsys(ss, i) { 972 for_each_subsys(ss, i) {
955 struct cftype_set *set; 973 struct cftype *cfts;
956 974
957 if (!test_bit(i, &subsys_mask)) 975 if (!test_bit(i, &subsys_mask))
958 continue; 976 continue;
959 list_for_each_entry(set, &ss->cftsets, node) 977 list_for_each_entry(cfts, &ss->cfts, node)
960 cgroup_addrm_files(cgrp, set->cfts, false); 978 cgroup_addrm_files(cgrp, cfts, false);
961 } 979 }
962} 980}
963 981
964/* 982static int rebind_subsystems(struct cgroup_root *dst_root,
965 * NOTE : the dentry must have been dget()'ed 983 unsigned long ss_mask)
966 */
967static void cgroup_d_remove_dir(struct dentry *dentry)
968{
969 struct dentry *parent;
970
971 parent = dentry->d_parent;
972 spin_lock(&parent->d_lock);
973 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
974 list_del_init(&dentry->d_u.d_child);
975 spin_unlock(&dentry->d_lock);
976 spin_unlock(&parent->d_lock);
977 remove_dir(dentry);
978}
979
980/*
981 * Call with cgroup_mutex held. Drops reference counts on modules, including
982 * any duplicate ones that parse_cgroupfs_options took. If this function
983 * returns an error, no reference counts are touched.
984 */
985static int rebind_subsystems(struct cgroupfs_root *root,
986 unsigned long added_mask, unsigned removed_mask)
987{ 984{
988 struct cgroup *cgrp = &root->top_cgroup;
989 struct cgroup_subsys *ss; 985 struct cgroup_subsys *ss;
990 unsigned long pinned = 0; 986 int ssid, ret;
991 int i, ret;
992 987
993 BUG_ON(!mutex_is_locked(&cgroup_mutex)); 988 lockdep_assert_held(&cgroup_tree_mutex);
994 BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); 989 lockdep_assert_held(&cgroup_mutex);
995 990
996 /* Check that any added subsystems are currently free */ 991 for_each_subsys(ss, ssid) {
997 for_each_subsys(ss, i) { 992 if (!(ss_mask & (1 << ssid)))
998 if (!(added_mask & (1 << i)))
999 continue; 993 continue;
1000 994
1001 /* is the subsystem mounted elsewhere? */ 995 /* if @ss is on the dummy_root, we can always move it */
1002 if (ss->root != &cgroup_dummy_root) { 996 if (ss->root == &cgrp_dfl_root)
1003 ret = -EBUSY; 997 continue;
1004 goto out_put;
1005 }
1006 998
1007 /* pin the module */ 999 /* if @ss has non-root cgroups attached to it, can't move */
1008 if (!try_module_get(ss->module)) { 1000 if (!list_empty(&ss->root->cgrp.children))
1009 ret = -ENOENT; 1001 return -EBUSY;
1010 goto out_put;
1011 }
1012 pinned |= 1 << i;
1013 }
1014 1002
1015 /* subsys could be missing if unloaded between parsing and here */ 1003 /* can't move between two non-dummy roots either */
1016 if (added_mask != pinned) { 1004 if (dst_root != &cgrp_dfl_root)
1017 ret = -ENOENT; 1005 return -EBUSY;
1018 goto out_put;
1019 } 1006 }
1020 1007
1021 ret = cgroup_populate_dir(cgrp, added_mask); 1008 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1022 if (ret) 1009 if (ret) {
1023 goto out_put; 1010 if (dst_root != &cgrp_dfl_root)
1011 return ret;
1012
1013 /*
1014 * Rebinding back to the default root is not allowed to
1015 * fail. Using both default and non-default roots should
1016 * be rare. Moving subsystems back and forth even more so.
1017 * Just warn about it and continue.
1018 */
1019 if (cgrp_dfl_root_visible) {
1020 pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
1021 ret, ss_mask);
1022 pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
1023 }
1024 }
1024 1025
1025 /* 1026 /*
1026 * Nothing can fail from this point on. Remove files for the 1027 * Nothing can fail from this point on. Remove files for the
1027 * removed subsystems and rebind each subsystem. 1028 * removed subsystems and rebind each subsystem.
1028 */ 1029 */
1029 cgroup_clear_dir(cgrp, removed_mask); 1030 mutex_unlock(&cgroup_mutex);
1030 1031 for_each_subsys(ss, ssid)
1031 for_each_subsys(ss, i) { 1032 if (ss_mask & (1 << ssid))
1032 unsigned long bit = 1UL << i; 1033 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
1033 1034 mutex_lock(&cgroup_mutex);
1034 if (bit & added_mask) {
1035 /* We're binding this subsystem to this hierarchy */
1036 BUG_ON(cgroup_css(cgrp, ss));
1037 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1038 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
1039 1035
1040 rcu_assign_pointer(cgrp->subsys[i], 1036 for_each_subsys(ss, ssid) {
1041 cgroup_css(cgroup_dummy_top, ss)); 1037 struct cgroup_root *src_root;
1042 cgroup_css(cgrp, ss)->cgroup = cgrp; 1038 struct cgroup_subsys_state *css;
1043 1039
1044 ss->root = root; 1040 if (!(ss_mask & (1 << ssid)))
1045 if (ss->bind) 1041 continue;
1046 ss->bind(cgroup_css(cgrp, ss));
1047 1042
1048 /* refcount was already taken, and we're keeping it */ 1043 src_root = ss->root;
1049 root->subsys_mask |= bit; 1044 css = cgroup_css(&src_root->cgrp, ss);
1050 } else if (bit & removed_mask) {
1051 /* We're removing this subsystem */
1052 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1053 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
1054 1045
1055 if (ss->bind) 1046 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
1056 ss->bind(cgroup_css(cgroup_dummy_top, ss));
1057 1047
1058 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; 1048 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1059 RCU_INIT_POINTER(cgrp->subsys[i], NULL); 1049 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
1050 ss->root = dst_root;
1051 css->cgroup = &dst_root->cgrp;
1060 1052
1061 cgroup_subsys[i]->root = &cgroup_dummy_root; 1053 src_root->cgrp.subsys_mask &= ~(1 << ssid);
1054 dst_root->cgrp.subsys_mask |= 1 << ssid;
1062 1055
1063 /* subsystem is now free - drop reference on module */ 1056 if (ss->bind)
1064 module_put(ss->module); 1057 ss->bind(css);
1065 root->subsys_mask &= ~bit;
1066 }
1067 } 1058 }
1068 1059
1069 /* 1060 kernfs_activate(dst_root->cgrp.kn);
1070 * Mark @root has finished binding subsystems. @root->subsys_mask
1071 * now matches the bound subsystems.
1072 */
1073 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1074
1075 return 0; 1061 return 0;
1076
1077out_put:
1078 for_each_subsys(ss, i)
1079 if (pinned & (1 << i))
1080 module_put(ss->module);
1081 return ret;
1082} 1062}
1083 1063
1084static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) 1064static int cgroup_show_options(struct seq_file *seq,
1065 struct kernfs_root *kf_root)
1085{ 1066{
1086 struct cgroupfs_root *root = dentry->d_sb->s_fs_info; 1067 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1087 struct cgroup_subsys *ss; 1068 struct cgroup_subsys *ss;
1088 int ssid; 1069 int ssid;
1089 1070
1090 mutex_lock(&cgroup_root_mutex);
1091 for_each_subsys(ss, ssid) 1071 for_each_subsys(ss, ssid)
1092 if (root->subsys_mask & (1 << ssid)) 1072 if (root->cgrp.subsys_mask & (1 << ssid))
1093 seq_printf(seq, ",%s", ss->name); 1073 seq_printf(seq, ",%s", ss->name);
1094 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) 1074 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1095 seq_puts(seq, ",sane_behavior"); 1075 seq_puts(seq, ",sane_behavior");
@@ -1097,13 +1077,16 @@ static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
1097 seq_puts(seq, ",noprefix"); 1077 seq_puts(seq, ",noprefix");
1098 if (root->flags & CGRP_ROOT_XATTR) 1078 if (root->flags & CGRP_ROOT_XATTR)
1099 seq_puts(seq, ",xattr"); 1079 seq_puts(seq, ",xattr");
1080
1081 spin_lock(&release_agent_path_lock);
1100 if (strlen(root->release_agent_path)) 1082 if (strlen(root->release_agent_path))
1101 seq_printf(seq, ",release_agent=%s", root->release_agent_path); 1083 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
1102 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) 1084 spin_unlock(&release_agent_path_lock);
1085
1086 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
1103 seq_puts(seq, ",clone_children"); 1087 seq_puts(seq, ",clone_children");
1104 if (strlen(root->name)) 1088 if (strlen(root->name))
1105 seq_printf(seq, ",name=%s", root->name); 1089 seq_printf(seq, ",name=%s", root->name);
1106 mutex_unlock(&cgroup_root_mutex);
1107 return 0; 1090 return 0;
1108} 1091}
1109 1092
@@ -1115,9 +1098,6 @@ struct cgroup_sb_opts {
1115 char *name; 1098 char *name;
1116 /* User explicitly requested empty subsystem */ 1099 /* User explicitly requested empty subsystem */
1117 bool none; 1100 bool none;
1118
1119 struct cgroupfs_root *new_root;
1120
1121}; 1101};
1122 1102
1123/* 1103/*
@@ -1137,7 +1117,7 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1137 BUG_ON(!mutex_is_locked(&cgroup_mutex)); 1117 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1138 1118
1139#ifdef CONFIG_CPUSETS 1119#ifdef CONFIG_CPUSETS
1140 mask = ~(1UL << cpuset_subsys_id); 1120 mask = ~(1UL << cpuset_cgrp_id);
1141#endif 1121#endif
1142 1122
1143 memset(opts, 0, sizeof(*opts)); 1123 memset(opts, 0, sizeof(*opts));
@@ -1227,30 +1207,34 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1227 return -ENOENT; 1207 return -ENOENT;
1228 } 1208 }
1229 1209
1230 /*
1231 * If the 'all' option was specified select all the subsystems,
1232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
1234 */
1235 if (all_ss || (!one_ss && !opts->none && !opts->name))
1236 for_each_subsys(ss, i)
1237 if (!ss->disabled)
1238 set_bit(i, &opts->subsys_mask);
1239
1240 /* Consistency checks */ 1210 /* Consistency checks */
1241 1211
1242 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { 1212 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1243 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); 1213 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1244 1214
1245 if (opts->flags & CGRP_ROOT_NOPREFIX) { 1215 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1246 pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); 1216 opts->cpuset_clone_children || opts->release_agent ||
1217 opts->name) {
1218 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
1247 return -EINVAL; 1219 return -EINVAL;
1248 } 1220 }
1221 } else {
1222 /*
1223 * If the 'all' option was specified select all the
1224 * subsystems, otherwise if 'none', 'name=' and a subsystem
1225 * name options were not specified, let's default to 'all'
1226 */
1227 if (all_ss || (!one_ss && !opts->none && !opts->name))
1228 for_each_subsys(ss, i)
1229 if (!ss->disabled)
1230 set_bit(i, &opts->subsys_mask);
1249 1231
1250 if (opts->cpuset_clone_children) { 1232 /*
1251 pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); 1233 * We either have to specify by name or by subsystems. (So
1234 * all empty hierarchies must have a name).
1235 */
1236 if (!opts->subsys_mask && !opts->name)
1252 return -EINVAL; 1237 return -EINVAL;
1253 }
1254 } 1238 }
1255 1239
1256 /* 1240 /*
@@ -1266,21 +1250,13 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1266 if (opts->subsys_mask && opts->none) 1250 if (opts->subsys_mask && opts->none)
1267 return -EINVAL; 1251 return -EINVAL;
1268 1252
1269 /*
1270 * We either have to specify by name or by subsystems. (So all
1271 * empty hierarchies must have a name).
1272 */
1273 if (!opts->subsys_mask && !opts->name)
1274 return -EINVAL;
1275
1276 return 0; 1253 return 0;
1277} 1254}
1278 1255
1279static int cgroup_remount(struct super_block *sb, int *flags, char *data) 1256static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
1280{ 1257{
1281 int ret = 0; 1258 int ret = 0;
1282 struct cgroupfs_root *root = sb->s_fs_info; 1259 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1283 struct cgroup *cgrp = &root->top_cgroup;
1284 struct cgroup_sb_opts opts; 1260 struct cgroup_sb_opts opts;
1285 unsigned long added_mask, removed_mask; 1261 unsigned long added_mask, removed_mask;
1286 1262
@@ -1289,21 +1265,20 @@ static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1289 return -EINVAL; 1265 return -EINVAL;
1290 } 1266 }
1291 1267
1292 mutex_lock(&cgrp->dentry->d_inode->i_mutex); 1268 mutex_lock(&cgroup_tree_mutex);
1293 mutex_lock(&cgroup_mutex); 1269 mutex_lock(&cgroup_mutex);
1294 mutex_lock(&cgroup_root_mutex);
1295 1270
1296 /* See what subsystems are wanted */ 1271 /* See what subsystems are wanted */
1297 ret = parse_cgroupfs_options(data, &opts); 1272 ret = parse_cgroupfs_options(data, &opts);
1298 if (ret) 1273 if (ret)
1299 goto out_unlock; 1274 goto out_unlock;
1300 1275
1301 if (opts.subsys_mask != root->subsys_mask || opts.release_agent) 1276 if (opts.subsys_mask != root->cgrp.subsys_mask || opts.release_agent)
1302 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", 1277 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1303 task_tgid_nr(current), current->comm); 1278 task_tgid_nr(current), current->comm);
1304 1279
1305 added_mask = opts.subsys_mask & ~root->subsys_mask; 1280 added_mask = opts.subsys_mask & ~root->cgrp.subsys_mask;
1306 removed_mask = root->subsys_mask & ~opts.subsys_mask; 1281 removed_mask = root->cgrp.subsys_mask & ~opts.subsys_mask;
1307 1282
1308 /* Don't allow flags or name to change at remount */ 1283 /* Don't allow flags or name to change at remount */
1309 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || 1284 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
@@ -1316,422 +1291,332 @@ static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1316 } 1291 }
1317 1292
1318 /* remounting is not allowed for populated hierarchies */ 1293 /* remounting is not allowed for populated hierarchies */
1319 if (root->number_of_cgroups > 1) { 1294 if (!list_empty(&root->cgrp.children)) {
1320 ret = -EBUSY; 1295 ret = -EBUSY;
1321 goto out_unlock; 1296 goto out_unlock;
1322 } 1297 }
1323 1298
1324 ret = rebind_subsystems(root, added_mask, removed_mask); 1299 ret = rebind_subsystems(root, added_mask);
1325 if (ret) 1300 if (ret)
1326 goto out_unlock; 1301 goto out_unlock;
1327 1302
1328 if (opts.release_agent) 1303 rebind_subsystems(&cgrp_dfl_root, removed_mask);
1304
1305 if (opts.release_agent) {
1306 spin_lock(&release_agent_path_lock);
1329 strcpy(root->release_agent_path, opts.release_agent); 1307 strcpy(root->release_agent_path, opts.release_agent);
1308 spin_unlock(&release_agent_path_lock);
1309 }
1330 out_unlock: 1310 out_unlock:
1331 kfree(opts.release_agent); 1311 kfree(opts.release_agent);
1332 kfree(opts.name); 1312 kfree(opts.name);
1333 mutex_unlock(&cgroup_root_mutex);
1334 mutex_unlock(&cgroup_mutex); 1313 mutex_unlock(&cgroup_mutex);
1335 mutex_unlock(&cgrp->dentry->d_inode->i_mutex); 1314 mutex_unlock(&cgroup_tree_mutex);
1336 return ret; 1315 return ret;
1337} 1316}
1338 1317
1339static const struct super_operations cgroup_ops = { 1318/*
1340 .statfs = simple_statfs, 1319 * To reduce the fork() overhead for systems that are not actually using
1341 .drop_inode = generic_delete_inode, 1320 * their cgroups capability, we don't maintain the lists running through
1342 .show_options = cgroup_show_options, 1321 * each css_set to its tasks until we see the list actually used - in other
1343 .remount_fs = cgroup_remount, 1322 * words after the first mount.
1344}; 1323 */
1324static bool use_task_css_set_links __read_mostly;
1325
1326static void cgroup_enable_task_cg_lists(void)
1327{
1328 struct task_struct *p, *g;
1329
1330 down_write(&css_set_rwsem);
1331
1332 if (use_task_css_set_links)
1333 goto out_unlock;
1334
1335 use_task_css_set_links = true;
1336
1337 /*
1338 * We need tasklist_lock because RCU is not safe against
1339 * while_each_thread(). Besides, a forking task that has passed
1340 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1341 * is not guaranteed to have its child immediately visible in the
1342 * tasklist if we walk through it with RCU.
1343 */
1344 read_lock(&tasklist_lock);
1345 do_each_thread(g, p) {
1346 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1347 task_css_set(p) != &init_css_set);
1348
1349 /*
1350 * We should check if the process is exiting, otherwise
1351 * it will race with cgroup_exit() in that the list
1352 * entry won't be deleted though the process has exited.
1353 * Do it while holding siglock so that we don't end up
1354 * racing against cgroup_exit().
1355 */
1356 spin_lock_irq(&p->sighand->siglock);
1357 if (!(p->flags & PF_EXITING)) {
1358 struct css_set *cset = task_css_set(p);
1359
1360 list_add(&p->cg_list, &cset->tasks);
1361 get_css_set(cset);
1362 }
1363 spin_unlock_irq(&p->sighand->siglock);
1364 } while_each_thread(g, p);
1365 read_unlock(&tasklist_lock);
1366out_unlock:
1367 up_write(&css_set_rwsem);
1368}
1345 1369
1346static void init_cgroup_housekeeping(struct cgroup *cgrp) 1370static void init_cgroup_housekeeping(struct cgroup *cgrp)
1347{ 1371{
1372 atomic_set(&cgrp->refcnt, 1);
1348 INIT_LIST_HEAD(&cgrp->sibling); 1373 INIT_LIST_HEAD(&cgrp->sibling);
1349 INIT_LIST_HEAD(&cgrp->children); 1374 INIT_LIST_HEAD(&cgrp->children);
1350 INIT_LIST_HEAD(&cgrp->files);
1351 INIT_LIST_HEAD(&cgrp->cset_links); 1375 INIT_LIST_HEAD(&cgrp->cset_links);
1352 INIT_LIST_HEAD(&cgrp->release_list); 1376 INIT_LIST_HEAD(&cgrp->release_list);
1353 INIT_LIST_HEAD(&cgrp->pidlists); 1377 INIT_LIST_HEAD(&cgrp->pidlists);
1354 mutex_init(&cgrp->pidlist_mutex); 1378 mutex_init(&cgrp->pidlist_mutex);
1355 cgrp->dummy_css.cgroup = cgrp; 1379 cgrp->dummy_css.cgroup = cgrp;
1356 simple_xattrs_init(&cgrp->xattrs);
1357} 1380}
1358 1381
1359static void init_cgroup_root(struct cgroupfs_root *root) 1382static void init_cgroup_root(struct cgroup_root *root,
1383 struct cgroup_sb_opts *opts)
1360{ 1384{
1361 struct cgroup *cgrp = &root->top_cgroup; 1385 struct cgroup *cgrp = &root->cgrp;
1362 1386
1363 INIT_LIST_HEAD(&root->root_list); 1387 INIT_LIST_HEAD(&root->root_list);
1364 root->number_of_cgroups = 1; 1388 atomic_set(&root->nr_cgrps, 1);
1365 cgrp->root = root; 1389 cgrp->root = root;
1366 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
1367 init_cgroup_housekeeping(cgrp); 1390 init_cgroup_housekeeping(cgrp);
1368 idr_init(&root->cgroup_idr); 1391 idr_init(&root->cgroup_idr);
1369}
1370
1371static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
1372{
1373 int id;
1374 1392
1375 lockdep_assert_held(&cgroup_mutex);
1376 lockdep_assert_held(&cgroup_root_mutex);
1377
1378 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1379 GFP_KERNEL);
1380 if (id < 0)
1381 return id;
1382
1383 root->hierarchy_id = id;
1384 return 0;
1385}
1386
1387static void cgroup_exit_root_id(struct cgroupfs_root *root)
1388{
1389 lockdep_assert_held(&cgroup_mutex);
1390 lockdep_assert_held(&cgroup_root_mutex);
1391
1392 if (root->hierarchy_id) {
1393 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1394 root->hierarchy_id = 0;
1395 }
1396}
1397
1398static int cgroup_test_super(struct super_block *sb, void *data)
1399{
1400 struct cgroup_sb_opts *opts = data;
1401 struct cgroupfs_root *root = sb->s_fs_info;
1402
1403 /* If we asked for a name then it must match */
1404 if (opts->name && strcmp(opts->name, root->name))
1405 return 0;
1406
1407 /*
1408 * If we asked for subsystems (or explicitly for no
1409 * subsystems) then they must match
1410 */
1411 if ((opts->subsys_mask || opts->none)
1412 && (opts->subsys_mask != root->subsys_mask))
1413 return 0;
1414
1415 return 1;
1416}
1417
1418static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1419{
1420 struct cgroupfs_root *root;
1421
1422 if (!opts->subsys_mask && !opts->none)
1423 return NULL;
1424
1425 root = kzalloc(sizeof(*root), GFP_KERNEL);
1426 if (!root)
1427 return ERR_PTR(-ENOMEM);
1428
1429 init_cgroup_root(root);
1430
1431 /*
1432 * We need to set @root->subsys_mask now so that @root can be
1433 * matched by cgroup_test_super() before it finishes
1434 * initialization; otherwise, competing mounts with the same
1435 * options may try to bind the same subsystems instead of waiting
1436 * for the first one leading to unexpected mount errors.
1437 * SUBSYS_BOUND will be set once actual binding is complete.
1438 */
1439 root->subsys_mask = opts->subsys_mask;
1440 root->flags = opts->flags; 1393 root->flags = opts->flags;
1441 if (opts->release_agent) 1394 if (opts->release_agent)
1442 strcpy(root->release_agent_path, opts->release_agent); 1395 strcpy(root->release_agent_path, opts->release_agent);
1443 if (opts->name) 1396 if (opts->name)
1444 strcpy(root->name, opts->name); 1397 strcpy(root->name, opts->name);
1445 if (opts->cpuset_clone_children) 1398 if (opts->cpuset_clone_children)
1446 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); 1399 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
1447 return root;
1448} 1400}
1449 1401
1450static void cgroup_free_root(struct cgroupfs_root *root) 1402static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
1451{ 1403{
1452 if (root) { 1404 LIST_HEAD(tmp_links);
1453 /* hierarhcy ID shoulid already have been released */ 1405 struct cgroup *root_cgrp = &root->cgrp;
1454 WARN_ON_ONCE(root->hierarchy_id); 1406 struct css_set *cset;
1455 1407 int i, ret;
1456 idr_destroy(&root->cgroup_idr);
1457 kfree(root);
1458 }
1459}
1460 1408
1461static int cgroup_set_super(struct super_block *sb, void *data) 1409 lockdep_assert_held(&cgroup_tree_mutex);
1462{ 1410 lockdep_assert_held(&cgroup_mutex);
1463 int ret;
1464 struct cgroup_sb_opts *opts = data;
1465 1411
1466 /* If we don't have a new root, we can't set up a new sb */ 1412 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1467 if (!opts->new_root) 1413 if (ret < 0)
1468 return -EINVAL; 1414 goto out;
1415 root_cgrp->id = ret;
1469 1416
1470 BUG_ON(!opts->subsys_mask && !opts->none); 1417 /*
1418 * We're accessing css_set_count without locking css_set_rwsem here,
1419 * but that's OK - it can only be increased by someone holding
1420 * cgroup_lock, and that's us. The worst that can happen is that we
1421 * have some link structures left over
1422 */
1423 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1424 if (ret)
1425 goto out;
1471 1426
1472 ret = set_anon_super(sb, NULL); 1427 ret = cgroup_init_root_id(root);
1473 if (ret) 1428 if (ret)
1474 return ret; 1429 goto out;
1475 1430
1476 sb->s_fs_info = opts->new_root; 1431 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1477 opts->new_root->sb = sb; 1432 KERNFS_ROOT_CREATE_DEACTIVATED,
1433 root_cgrp);
1434 if (IS_ERR(root->kf_root)) {
1435 ret = PTR_ERR(root->kf_root);
1436 goto exit_root_id;
1437 }
1438 root_cgrp->kn = root->kf_root->kn;
1478 1439
1479 sb->s_blocksize = PAGE_CACHE_SIZE; 1440 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1480 sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 1441 if (ret)
1481 sb->s_magic = CGROUP_SUPER_MAGIC; 1442 goto destroy_root;
1482 sb->s_op = &cgroup_ops;
1483 1443
1484 return 0; 1444 ret = rebind_subsystems(root, ss_mask);
1485} 1445 if (ret)
1446 goto destroy_root;
1486 1447
1487static int cgroup_get_rootdir(struct super_block *sb) 1448 /*
1488{ 1449 * There must be no failure case after here, since rebinding takes
1489 static const struct dentry_operations cgroup_dops = { 1450 * care of subsystems' refcounts, which are explicitly dropped in
1490 .d_iput = cgroup_diput, 1451 * the failure exit path.
1491 .d_delete = always_delete_dentry, 1452 */
1492 }; 1453 list_add(&root->root_list, &cgroup_roots);
1454 cgroup_root_count++;
1493 1455
1494 struct inode *inode = 1456 /*
1495 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); 1457 * Link the root cgroup in this hierarchy into all the css_set
1458 * objects.
1459 */
1460 down_write(&css_set_rwsem);
1461 hash_for_each(css_set_table, i, cset, hlist)
1462 link_css_set(&tmp_links, cset, root_cgrp);
1463 up_write(&css_set_rwsem);
1496 1464
1497 if (!inode) 1465 BUG_ON(!list_empty(&root_cgrp->children));
1498 return -ENOMEM; 1466 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
1499 1467
1500 inode->i_fop = &simple_dir_operations; 1468 kernfs_activate(root_cgrp->kn);
1501 inode->i_op = &cgroup_dir_inode_operations; 1469 ret = 0;
1502 /* directories start off with i_nlink == 2 (for "." entry) */ 1470 goto out;
1503 inc_nlink(inode); 1471
1504 sb->s_root = d_make_root(inode); 1472destroy_root:
1505 if (!sb->s_root) 1473 kernfs_destroy_root(root->kf_root);
1506 return -ENOMEM; 1474 root->kf_root = NULL;
1507 /* for everything else we want ->d_op set */ 1475exit_root_id:
1508 sb->s_d_op = &cgroup_dops; 1476 cgroup_exit_root_id(root);
1509 return 0; 1477out:
1478 free_cgrp_cset_links(&tmp_links);
1479 return ret;
1510} 1480}
1511 1481
1512static struct dentry *cgroup_mount(struct file_system_type *fs_type, 1482static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1513 int flags, const char *unused_dev_name, 1483 int flags, const char *unused_dev_name,
1514 void *data) 1484 void *data)
1515{ 1485{
1486 struct cgroup_root *root;
1516 struct cgroup_sb_opts opts; 1487 struct cgroup_sb_opts opts;
1517 struct cgroupfs_root *root; 1488 struct dentry *dentry;
1518 int ret = 0; 1489 int ret;
1519 struct super_block *sb; 1490 bool new_sb;
1520 struct cgroupfs_root *new_root;
1521 struct list_head tmp_links;
1522 struct inode *inode;
1523 const struct cred *cred;
1524 1491
1525 /* First find the desired set of subsystems */ 1492 /*
1493 * The first time anyone tries to mount a cgroup, enable the list
1494 * linking each css_set to its tasks and fix up all existing tasks.
1495 */
1496 if (!use_task_css_set_links)
1497 cgroup_enable_task_cg_lists();
1498retry:
1499 mutex_lock(&cgroup_tree_mutex);
1526 mutex_lock(&cgroup_mutex); 1500 mutex_lock(&cgroup_mutex);
1501
1502 /* First find the desired set of subsystems */
1527 ret = parse_cgroupfs_options(data, &opts); 1503 ret = parse_cgroupfs_options(data, &opts);
1528 mutex_unlock(&cgroup_mutex);
1529 if (ret) 1504 if (ret)
1530 goto out_err; 1505 goto out_unlock;
1531
1532 /*
1533 * Allocate a new cgroup root. We may not need it if we're
1534 * reusing an existing hierarchy.
1535 */
1536 new_root = cgroup_root_from_opts(&opts);
1537 if (IS_ERR(new_root)) {
1538 ret = PTR_ERR(new_root);
1539 goto out_err;
1540 }
1541 opts.new_root = new_root;
1542 1506
1543 /* Locate an existing or new sb for this hierarchy */ 1507 /* look for a matching existing root */
1544 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); 1508 if (!opts.subsys_mask && !opts.none && !opts.name) {
1545 if (IS_ERR(sb)) { 1509 cgrp_dfl_root_visible = true;
1546 ret = PTR_ERR(sb); 1510 root = &cgrp_dfl_root;
1547 cgroup_free_root(opts.new_root); 1511 cgroup_get(&root->cgrp);
1548 goto out_err; 1512 ret = 0;
1513 goto out_unlock;
1549 } 1514 }
1550 1515
1551 root = sb->s_fs_info; 1516 for_each_root(root) {
1552 BUG_ON(!root); 1517 bool name_match = false;
1553 if (root == opts.new_root) {
1554 /* We used the new root structure, so this is a new hierarchy */
1555 struct cgroup *root_cgrp = &root->top_cgroup;
1556 struct cgroupfs_root *existing_root;
1557 int i;
1558 struct css_set *cset;
1559
1560 BUG_ON(sb->s_root != NULL);
1561 1518
1562 ret = cgroup_get_rootdir(sb); 1519 if (root == &cgrp_dfl_root)
1563 if (ret) 1520 continue;
1564 goto drop_new_super;
1565 inode = sb->s_root->d_inode;
1566
1567 mutex_lock(&inode->i_mutex);
1568 mutex_lock(&cgroup_mutex);
1569 mutex_lock(&cgroup_root_mutex);
1570
1571 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1572 if (ret < 0)
1573 goto unlock_drop;
1574 root_cgrp->id = ret;
1575
1576 /* Check for name clashes with existing mounts */
1577 ret = -EBUSY;
1578 if (strlen(root->name))
1579 for_each_active_root(existing_root)
1580 if (!strcmp(existing_root->name, root->name))
1581 goto unlock_drop;
1582
1583 /*
1584 * We're accessing css_set_count without locking
1585 * css_set_lock here, but that's OK - it can only be
1586 * increased by someone holding cgroup_lock, and
1587 * that's us. The worst that can happen is that we
1588 * have some link structures left over
1589 */
1590 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1591 if (ret)
1592 goto unlock_drop;
1593
1594 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1595 ret = cgroup_init_root_id(root, 2, 0);
1596 if (ret)
1597 goto unlock_drop;
1598
1599 sb->s_root->d_fsdata = root_cgrp;
1600 root_cgrp->dentry = sb->s_root;
1601
1602 /*
1603 * We're inside get_sb() and will call lookup_one_len() to
1604 * create the root files, which doesn't work if SELinux is
1605 * in use. The following cred dancing somehow works around
1606 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1607 * populating new cgroupfs mount") for more details.
1608 */
1609 cred = override_creds(&init_cred);
1610
1611 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1612 if (ret)
1613 goto rm_base_files;
1614
1615 ret = rebind_subsystems(root, root->subsys_mask, 0);
1616 if (ret)
1617 goto rm_base_files;
1618
1619 revert_creds(cred);
1620 1521
1621 /* 1522 /*
1622 * There must be no failure case after here, since rebinding 1523 * If we asked for a name then it must match. Also, if
1623 * takes care of subsystems' refcounts, which are explicitly 1524 * name matches but sybsys_mask doesn't, we should fail.
1624 * dropped in the failure exit path. 1525 * Remember whether name matched.
1625 */ 1526 */
1527 if (opts.name) {
1528 if (strcmp(opts.name, root->name))
1529 continue;
1530 name_match = true;
1531 }
1626 1532
1627 list_add(&root->root_list, &cgroup_roots);
1628 cgroup_root_count++;
1629
1630 /* Link the top cgroup in this hierarchy into all
1631 * the css_set objects */
1632 write_lock(&css_set_lock);
1633 hash_for_each(css_set_table, i, cset, hlist)
1634 link_css_set(&tmp_links, cset, root_cgrp);
1635 write_unlock(&css_set_lock);
1636
1637 free_cgrp_cset_links(&tmp_links);
1638
1639 BUG_ON(!list_empty(&root_cgrp->children));
1640 BUG_ON(root->number_of_cgroups != 1);
1641
1642 mutex_unlock(&cgroup_root_mutex);
1643 mutex_unlock(&cgroup_mutex);
1644 mutex_unlock(&inode->i_mutex);
1645 } else {
1646 /* 1533 /*
1647 * We re-used an existing hierarchy - the new root (if 1534 * If we asked for subsystems (or explicitly for no
1648 * any) is not needed 1535 * subsystems) then they must match.
1649 */ 1536 */
1650 cgroup_free_root(opts.new_root); 1537 if ((opts.subsys_mask || opts.none) &&
1538 (opts.subsys_mask != root->cgrp.subsys_mask)) {
1539 if (!name_match)
1540 continue;
1541 ret = -EBUSY;
1542 goto out_unlock;
1543 }
1651 1544
1652 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { 1545 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
1653 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { 1546 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1654 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); 1547 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1655 ret = -EINVAL; 1548 ret = -EINVAL;
1656 goto drop_new_super; 1549 goto out_unlock;
1657 } else { 1550 } else {
1658 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); 1551 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1659 } 1552 }
1660 } 1553 }
1661 }
1662
1663 kfree(opts.release_agent);
1664 kfree(opts.name);
1665 return dget(sb->s_root);
1666 1554
1667 rm_base_files: 1555 /*
1668 free_cgrp_cset_links(&tmp_links); 1556 * A root's lifetime is governed by its root cgroup. Zero
1669 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); 1557 * ref indicate that the root is being destroyed. Wait for
1670 revert_creds(cred); 1558 * destruction to complete so that the subsystems are free.
1671 unlock_drop: 1559 * We can use wait_queue for the wait but this path is
1672 cgroup_exit_root_id(root); 1560 * super cold. Let's just sleep for a bit and retry.
1673 mutex_unlock(&cgroup_root_mutex); 1561 */
1674 mutex_unlock(&cgroup_mutex); 1562 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
1675 mutex_unlock(&inode->i_mutex); 1563 mutex_unlock(&cgroup_mutex);
1676 drop_new_super: 1564 mutex_unlock(&cgroup_tree_mutex);
1677 deactivate_locked_super(sb); 1565 kfree(opts.release_agent);
1678 out_err: 1566 kfree(opts.name);
1679 kfree(opts.release_agent); 1567 msleep(10);
1680 kfree(opts.name); 1568 goto retry;
1681 return ERR_PTR(ret); 1569 }
1682}
1683
1684static void cgroup_kill_sb(struct super_block *sb)
1685{
1686 struct cgroupfs_root *root = sb->s_fs_info;
1687 struct cgroup *cgrp = &root->top_cgroup;
1688 struct cgrp_cset_link *link, *tmp_link;
1689 int ret;
1690
1691 BUG_ON(!root);
1692
1693 BUG_ON(root->number_of_cgroups != 1);
1694 BUG_ON(!list_empty(&cgrp->children));
1695
1696 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1697 mutex_lock(&cgroup_mutex);
1698 mutex_lock(&cgroup_root_mutex);
1699 1570
1700 /* Rebind all subsystems back to the default hierarchy */ 1571 ret = 0;
1701 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { 1572 goto out_unlock;
1702 ret = rebind_subsystems(root, 0, root->subsys_mask);
1703 /* Shouldn't be able to fail ... */
1704 BUG_ON(ret);
1705 } 1573 }
1706 1574
1707 /* 1575 /*
1708 * Release all the links from cset_links to this hierarchy's 1576 * No such thing, create a new one. name= matching without subsys
1709 * root cgroup 1577 * specification is allowed for already existing hierarchies but we
1578 * can't create new one without subsys specification.
1710 */ 1579 */
1711 write_lock(&css_set_lock); 1580 if (!opts.subsys_mask && !opts.none) {
1712 1581 ret = -EINVAL;
1713 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { 1582 goto out_unlock;
1714 list_del(&link->cset_link);
1715 list_del(&link->cgrp_link);
1716 kfree(link);
1717 } 1583 }
1718 write_unlock(&css_set_lock);
1719 1584
1720 if (!list_empty(&root->root_list)) { 1585 root = kzalloc(sizeof(*root), GFP_KERNEL);
1721 list_del(&root->root_list); 1586 if (!root) {
1722 cgroup_root_count--; 1587 ret = -ENOMEM;
1588 goto out_unlock;
1723 } 1589 }
1724 1590
1725 cgroup_exit_root_id(root); 1591 init_cgroup_root(root, &opts);
1592
1593 ret = cgroup_setup_root(root, opts.subsys_mask);
1594 if (ret)
1595 cgroup_free_root(root);
1726 1596
1727 mutex_unlock(&cgroup_root_mutex); 1597out_unlock:
1728 mutex_unlock(&cgroup_mutex); 1598 mutex_unlock(&cgroup_mutex);
1729 mutex_unlock(&cgrp->dentry->d_inode->i_mutex); 1599 mutex_unlock(&cgroup_tree_mutex);
1600
1601 kfree(opts.release_agent);
1602 kfree(opts.name);
1730 1603
1731 simple_xattrs_free(&cgrp->xattrs); 1604 if (ret)
1605 return ERR_PTR(ret);
1732 1606
1733 kill_litter_super(sb); 1607 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1734 cgroup_free_root(root); 1608 if (IS_ERR(dentry) || !new_sb)
1609 cgroup_put(&root->cgrp);
1610 return dentry;
1611}
1612
1613static void cgroup_kill_sb(struct super_block *sb)
1614{
1615 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1616 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1617
1618 cgroup_put(&root->cgrp);
1619 kernfs_kill_sb(sb);
1735} 1620}
1736 1621
1737static struct file_system_type cgroup_fs_type = { 1622static struct file_system_type cgroup_fs_type = {
@@ -1743,57 +1628,6 @@ static struct file_system_type cgroup_fs_type = {
1743static struct kobject *cgroup_kobj; 1628static struct kobject *cgroup_kobj;
1744 1629
1745/** 1630/**
1746 * cgroup_path - generate the path of a cgroup
1747 * @cgrp: the cgroup in question
1748 * @buf: the buffer to write the path into
1749 * @buflen: the length of the buffer
1750 *
1751 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1752 *
1753 * We can't generate cgroup path using dentry->d_name, as accessing
1754 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1755 * inode's i_mutex, while on the other hand cgroup_path() can be called
1756 * with some irq-safe spinlocks held.
1757 */
1758int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1759{
1760 int ret = -ENAMETOOLONG;
1761 char *start;
1762
1763 if (!cgrp->parent) {
1764 if (strlcpy(buf, "/", buflen) >= buflen)
1765 return -ENAMETOOLONG;
1766 return 0;
1767 }
1768
1769 start = buf + buflen - 1;
1770 *start = '\0';
1771
1772 rcu_read_lock();
1773 do {
1774 const char *name = cgroup_name(cgrp);
1775 int len;
1776
1777 len = strlen(name);
1778 if ((start -= len) < buf)
1779 goto out;
1780 memcpy(start, name, len);
1781
1782 if (--start < buf)
1783 goto out;
1784 *start = '/';
1785
1786 cgrp = cgrp->parent;
1787 } while (cgrp->parent);
1788 ret = 0;
1789 memmove(buf, start, buf + buflen - start);
1790out:
1791 rcu_read_unlock();
1792 return ret;
1793}
1794EXPORT_SYMBOL_GPL(cgroup_path);
1795
1796/**
1797 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy 1631 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1798 * @task: target task 1632 * @task: target task
1799 * @buf: the buffer to write the path into 1633 * @buf: the buffer to write the path into
@@ -1804,49 +1638,55 @@ EXPORT_SYMBOL_GPL(cgroup_path);
1804 * function grabs cgroup_mutex and shouldn't be used inside locks used by 1638 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1805 * cgroup controller callbacks. 1639 * cgroup controller callbacks.
1806 * 1640 *
1807 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. 1641 * Return value is the same as kernfs_path().
1808 */ 1642 */
1809int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) 1643char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
1810{ 1644{
1811 struct cgroupfs_root *root; 1645 struct cgroup_root *root;
1812 struct cgroup *cgrp; 1646 struct cgroup *cgrp;
1813 int hierarchy_id = 1, ret = 0; 1647 int hierarchy_id = 1;
1814 1648 char *path = NULL;
1815 if (buflen < 2)
1816 return -ENAMETOOLONG;
1817 1649
1818 mutex_lock(&cgroup_mutex); 1650 mutex_lock(&cgroup_mutex);
1651 down_read(&css_set_rwsem);
1819 1652
1820 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); 1653 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1821 1654
1822 if (root) { 1655 if (root) {
1823 cgrp = task_cgroup_from_root(task, root); 1656 cgrp = task_cgroup_from_root(task, root);
1824 ret = cgroup_path(cgrp, buf, buflen); 1657 path = cgroup_path(cgrp, buf, buflen);
1825 } else { 1658 } else {
1826 /* if no hierarchy exists, everyone is in "/" */ 1659 /* if no hierarchy exists, everyone is in "/" */
1827 memcpy(buf, "/", 2); 1660 if (strlcpy(buf, "/", buflen) < buflen)
1661 path = buf;
1828 } 1662 }
1829 1663
1664 up_read(&css_set_rwsem);
1830 mutex_unlock(&cgroup_mutex); 1665 mutex_unlock(&cgroup_mutex);
1831 return ret; 1666 return path;
1832} 1667}
1833EXPORT_SYMBOL_GPL(task_cgroup_path); 1668EXPORT_SYMBOL_GPL(task_cgroup_path);
1834 1669
1835/* 1670/* used to track tasks and other necessary states during migration */
1836 * Control Group taskset
1837 */
1838struct task_and_cgroup {
1839 struct task_struct *task;
1840 struct cgroup *cgrp;
1841 struct css_set *cset;
1842};
1843
1844struct cgroup_taskset { 1671struct cgroup_taskset {
1845 struct task_and_cgroup single; 1672 /* the src and dst cset list running through cset->mg_node */
1846 struct flex_array *tc_array; 1673 struct list_head src_csets;
1847 int tc_array_len; 1674 struct list_head dst_csets;
1848 int idx; 1675
1849 struct cgroup *cur_cgrp; 1676 /*
1677 * Fields for cgroup_taskset_*() iteration.
1678 *
1679 * Before migration is committed, the target migration tasks are on
1680 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1681 * the csets on ->dst_csets. ->csets point to either ->src_csets
1682 * or ->dst_csets depending on whether migration is committed.
1683 *
1684 * ->cur_csets and ->cur_task point to the current task position
1685 * during iteration.
1686 */
1687 struct list_head *csets;
1688 struct css_set *cur_cset;
1689 struct task_struct *cur_task;
1850}; 1690};
1851 1691
1852/** 1692/**
@@ -1857,15 +1697,11 @@ struct cgroup_taskset {
1857 */ 1697 */
1858struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) 1698struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1859{ 1699{
1860 if (tset->tc_array) { 1700 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1861 tset->idx = 0; 1701 tset->cur_task = NULL;
1862 return cgroup_taskset_next(tset); 1702
1863 } else { 1703 return cgroup_taskset_next(tset);
1864 tset->cur_cgrp = tset->single.cgrp;
1865 return tset->single.task;
1866 }
1867} 1704}
1868EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1869 1705
1870/** 1706/**
1871 * cgroup_taskset_next - iterate to the next task in taskset 1707 * cgroup_taskset_next - iterate to the next task in taskset
@@ -1876,48 +1712,36 @@ EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1876 */ 1712 */
1877struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) 1713struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1878{ 1714{
1879 struct task_and_cgroup *tc; 1715 struct css_set *cset = tset->cur_cset;
1716 struct task_struct *task = tset->cur_task;
1880 1717
1881 if (!tset->tc_array || tset->idx >= tset->tc_array_len) 1718 while (&cset->mg_node != tset->csets) {
1882 return NULL; 1719 if (!task)
1720 task = list_first_entry(&cset->mg_tasks,
1721 struct task_struct, cg_list);
1722 else
1723 task = list_next_entry(task, cg_list);
1883 1724
1884 tc = flex_array_get(tset->tc_array, tset->idx++); 1725 if (&task->cg_list != &cset->mg_tasks) {
1885 tset->cur_cgrp = tc->cgrp; 1726 tset->cur_cset = cset;
1886 return tc->task; 1727 tset->cur_task = task;
1887} 1728 return task;
1888EXPORT_SYMBOL_GPL(cgroup_taskset_next); 1729 }
1889 1730
1890/** 1731 cset = list_next_entry(cset, mg_node);
1891 * cgroup_taskset_cur_css - return the matching css for the current task 1732 task = NULL;
1892 * @tset: taskset of interest 1733 }
1893 * @subsys_id: the ID of the target subsystem
1894 *
1895 * Return the css for the current (last returned) task of @tset for
1896 * subsystem specified by @subsys_id. This function must be preceded by
1897 * either cgroup_taskset_first() or cgroup_taskset_next().
1898 */
1899struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1900 int subsys_id)
1901{
1902 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
1903}
1904EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
1905 1734
1906/** 1735 return NULL;
1907 * cgroup_taskset_size - return the number of tasks in taskset
1908 * @tset: taskset of interest
1909 */
1910int cgroup_taskset_size(struct cgroup_taskset *tset)
1911{
1912 return tset->tc_array ? tset->tc_array_len : 1;
1913} 1736}
1914EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1915
1916 1737
1917/* 1738/**
1918 * cgroup_task_migrate - move a task from one cgroup to another. 1739 * cgroup_task_migrate - move a task from one cgroup to another.
1740 * @old_cgrp; the cgroup @tsk is being migrated from
1741 * @tsk: the task being migrated
1742 * @new_cset: the new css_set @tsk is being attached to
1919 * 1743 *
1920 * Must be called with cgroup_mutex and threadgroup locked. 1744 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
1921 */ 1745 */
1922static void cgroup_task_migrate(struct cgroup *old_cgrp, 1746static void cgroup_task_migrate(struct cgroup *old_cgrp,
1923 struct task_struct *tsk, 1747 struct task_struct *tsk,
@@ -1925,6 +1749,9 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
1925{ 1749{
1926 struct css_set *old_cset; 1750 struct css_set *old_cset;
1927 1751
1752 lockdep_assert_held(&cgroup_mutex);
1753 lockdep_assert_held(&css_set_rwsem);
1754
1928 /* 1755 /*
1929 * We are synchronized through threadgroup_lock() against PF_EXITING 1756 * We are synchronized through threadgroup_lock() against PF_EXITING
1930 * setting such that we can't race against cgroup_exit() changing the 1757 * setting such that we can't race against cgroup_exit() changing the
@@ -1933,15 +1760,16 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
1933 WARN_ON_ONCE(tsk->flags & PF_EXITING); 1760 WARN_ON_ONCE(tsk->flags & PF_EXITING);
1934 old_cset = task_css_set(tsk); 1761 old_cset = task_css_set(tsk);
1935 1762
1936 task_lock(tsk); 1763 get_css_set(new_cset);
1937 rcu_assign_pointer(tsk->cgroups, new_cset); 1764 rcu_assign_pointer(tsk->cgroups, new_cset);
1938 task_unlock(tsk);
1939 1765
1940 /* Update the css_set linked lists if we're using them */ 1766 /*
1941 write_lock(&css_set_lock); 1767 * Use move_tail so that cgroup_taskset_first() still returns the
1942 if (!list_empty(&tsk->cg_list)) 1768 * leader after migration. This works because cgroup_migrate()
1943 list_move(&tsk->cg_list, &new_cset->tasks); 1769 * ensures that the dst_cset of the leader is the first on the
1944 write_unlock(&css_set_lock); 1770 * tset's dst_csets list.
1771 */
1772 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
1945 1773
1946 /* 1774 /*
1947 * We just gained a reference on old_cset by taking it from the 1775 * We just gained a reference on old_cset by taking it from the
@@ -1949,100 +1777,199 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
1949 * we're safe to drop it here; it will be freed under RCU. 1777 * we're safe to drop it here; it will be freed under RCU.
1950 */ 1778 */
1951 set_bit(CGRP_RELEASABLE, &old_cgrp->flags); 1779 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1952 put_css_set(old_cset); 1780 put_css_set_locked(old_cset, false);
1953} 1781}
1954 1782
1955/** 1783/**
1956 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup 1784 * cgroup_migrate_finish - cleanup after attach
1957 * @cgrp: the cgroup to attach to 1785 * @preloaded_csets: list of preloaded css_sets
1958 * @tsk: the task or the leader of the threadgroup to be attached
1959 * @threadgroup: attach the whole threadgroup?
1960 * 1786 *
1961 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take 1787 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1962 * task_lock of @tsk or each thread in the threadgroup individually in turn. 1788 * those functions for details.
1963 */ 1789 */
1964static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, 1790static void cgroup_migrate_finish(struct list_head *preloaded_csets)
1965 bool threadgroup)
1966{ 1791{
1967 int retval, i, group_size; 1792 struct css_set *cset, *tmp_cset;
1968 struct cgroupfs_root *root = cgrp->root;
1969 struct cgroup_subsys_state *css, *failed_css = NULL;
1970 /* threadgroup list cursor and array */
1971 struct task_struct *leader = tsk;
1972 struct task_and_cgroup *tc;
1973 struct flex_array *group;
1974 struct cgroup_taskset tset = { };
1975 1793
1976 /* 1794 lockdep_assert_held(&cgroup_mutex);
1977 * step 0: in order to do expensive, possibly blocking operations for 1795
1978 * every thread, we cannot iterate the thread group list, since it needs 1796 down_write(&css_set_rwsem);
1979 * rcu or tasklist locked. instead, build an array of all threads in the 1797 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1980 * group - group_rwsem prevents new threads from appearing, and if 1798 cset->mg_src_cgrp = NULL;
1981 * threads exit, this will just be an over-estimate. 1799 cset->mg_dst_cset = NULL;
1982 */ 1800 list_del_init(&cset->mg_preload_node);
1983 if (threadgroup) 1801 put_css_set_locked(cset, false);
1984 group_size = get_nr_threads(tsk); 1802 }
1985 else 1803 up_write(&css_set_rwsem);
1986 group_size = 1; 1804}
1987 /* flex_array supports very large thread-groups better than kmalloc. */ 1805
1988 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); 1806/**
1989 if (!group) 1807 * cgroup_migrate_add_src - add a migration source css_set
1990 return -ENOMEM; 1808 * @src_cset: the source css_set to add
1991 /* pre-allocate to guarantee space while iterating in rcu read-side. */ 1809 * @dst_cgrp: the destination cgroup
1992 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); 1810 * @preloaded_csets: list of preloaded css_sets
1993 if (retval) 1811 *
1994 goto out_free_group_list; 1812 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1813 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1814 * up by cgroup_migrate_finish().
1815 *
1816 * This function may be called without holding threadgroup_lock even if the
1817 * target is a process. Threads may be created and destroyed but as long
1818 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1819 * the preloaded css_sets are guaranteed to cover all migrations.
1820 */
1821static void cgroup_migrate_add_src(struct css_set *src_cset,
1822 struct cgroup *dst_cgrp,
1823 struct list_head *preloaded_csets)
1824{
1825 struct cgroup *src_cgrp;
1826
1827 lockdep_assert_held(&cgroup_mutex);
1828 lockdep_assert_held(&css_set_rwsem);
1829
1830 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1831
1832 /* nothing to do if this cset already belongs to the cgroup */
1833 if (src_cgrp == dst_cgrp)
1834 return;
1835
1836 if (!list_empty(&src_cset->mg_preload_node))
1837 return;
1838
1839 WARN_ON(src_cset->mg_src_cgrp);
1840 WARN_ON(!list_empty(&src_cset->mg_tasks));
1841 WARN_ON(!list_empty(&src_cset->mg_node));
1842
1843 src_cset->mg_src_cgrp = src_cgrp;
1844 get_css_set(src_cset);
1845 list_add(&src_cset->mg_preload_node, preloaded_csets);
1846}
1847
1848/**
1849 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
1850 * @dst_cgrp: the destination cgroup
1851 * @preloaded_csets: list of preloaded source css_sets
1852 *
1853 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1854 * have been preloaded to @preloaded_csets. This function looks up and
1855 * pins all destination css_sets, links each to its source, and put them on
1856 * @preloaded_csets.
1857 *
1858 * This function must be called after cgroup_migrate_add_src() has been
1859 * called on each migration source css_set. After migration is performed
1860 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1861 * @preloaded_csets.
1862 */
1863static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1864 struct list_head *preloaded_csets)
1865{
1866 LIST_HEAD(csets);
1867 struct css_set *src_cset;
1868
1869 lockdep_assert_held(&cgroup_mutex);
1870
1871 /* look up the dst cset for each src cset and link it to src */
1872 list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
1873 struct css_set *dst_cset;
1874
1875 dst_cset = find_css_set(src_cset, dst_cgrp);
1876 if (!dst_cset)
1877 goto err;
1878
1879 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
1880 src_cset->mg_dst_cset = dst_cset;
1881
1882 if (list_empty(&dst_cset->mg_preload_node))
1883 list_add(&dst_cset->mg_preload_node, &csets);
1884 else
1885 put_css_set(dst_cset, false);
1886 }
1887
1888 list_splice(&csets, preloaded_csets);
1889 return 0;
1890err:
1891 cgroup_migrate_finish(&csets);
1892 return -ENOMEM;
1893}
1894
1895/**
1896 * cgroup_migrate - migrate a process or task to a cgroup
1897 * @cgrp: the destination cgroup
1898 * @leader: the leader of the process or the task to migrate
1899 * @threadgroup: whether @leader points to the whole process or a single task
1900 *
1901 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
1902 * process, the caller must be holding threadgroup_lock of @leader. The
1903 * caller is also responsible for invoking cgroup_migrate_add_src() and
1904 * cgroup_migrate_prepare_dst() on the targets before invoking this
1905 * function and following up with cgroup_migrate_finish().
1906 *
1907 * As long as a controller's ->can_attach() doesn't fail, this function is
1908 * guaranteed to succeed. This means that, excluding ->can_attach()
1909 * failure, when migrating multiple targets, the success or failure can be
1910 * decided for all targets by invoking group_migrate_prepare_dst() before
1911 * actually starting migrating.
1912 */
1913static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1914 bool threadgroup)
1915{
1916 struct cgroup_taskset tset = {
1917 .src_csets = LIST_HEAD_INIT(tset.src_csets),
1918 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
1919 .csets = &tset.src_csets,
1920 };
1921 struct cgroup_subsys_state *css, *failed_css = NULL;
1922 struct css_set *cset, *tmp_cset;
1923 struct task_struct *task, *tmp_task;
1924 int i, ret;
1995 1925
1996 i = 0;
1997 /* 1926 /*
1998 * Prevent freeing of tasks while we take a snapshot. Tasks that are 1927 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1999 * already PF_EXITING could be freed from underneath us unless we 1928 * already PF_EXITING could be freed from underneath us unless we
2000 * take an rcu_read_lock. 1929 * take an rcu_read_lock.
2001 */ 1930 */
1931 down_write(&css_set_rwsem);
2002 rcu_read_lock(); 1932 rcu_read_lock();
1933 task = leader;
2003 do { 1934 do {
2004 struct task_and_cgroup ent; 1935 /* @task either already exited or can't exit until the end */
1936 if (task->flags & PF_EXITING)
1937 goto next;
2005 1938
2006 /* @tsk either already exited or can't exit until the end */ 1939 /* leave @task alone if post_fork() hasn't linked it yet */
2007 if (tsk->flags & PF_EXITING) 1940 if (list_empty(&task->cg_list))
2008 goto next; 1941 goto next;
2009 1942
2010 /* as per above, nr_threads may decrease, but not increase. */ 1943 cset = task_css_set(task);
2011 BUG_ON(i >= group_size); 1944 if (!cset->mg_src_cgrp)
2012 ent.task = tsk;
2013 ent.cgrp = task_cgroup_from_root(tsk, root);
2014 /* nothing to do if this task is already in the cgroup */
2015 if (ent.cgrp == cgrp)
2016 goto next; 1945 goto next;
1946
2017 /* 1947 /*
2018 * saying GFP_ATOMIC has no effect here because we did prealloc 1948 * cgroup_taskset_first() must always return the leader.
2019 * earlier, but it's good form to communicate our expectations. 1949 * Take care to avoid disturbing the ordering.
2020 */ 1950 */
2021 retval = flex_array_put(group, i, &ent, GFP_ATOMIC); 1951 list_move_tail(&task->cg_list, &cset->mg_tasks);
2022 BUG_ON(retval != 0); 1952 if (list_empty(&cset->mg_node))
2023 i++; 1953 list_add_tail(&cset->mg_node, &tset.src_csets);
1954 if (list_empty(&cset->mg_dst_cset->mg_node))
1955 list_move_tail(&cset->mg_dst_cset->mg_node,
1956 &tset.dst_csets);
2024 next: 1957 next:
2025 if (!threadgroup) 1958 if (!threadgroup)
2026 break; 1959 break;
2027 } while_each_thread(leader, tsk); 1960 } while_each_thread(leader, task);
2028 rcu_read_unlock(); 1961 rcu_read_unlock();
2029 /* remember the number of threads in the array for later. */ 1962 up_write(&css_set_rwsem);
2030 group_size = i;
2031 tset.tc_array = group;
2032 tset.tc_array_len = group_size;
2033 1963
2034 /* methods shouldn't be called if no task is actually migrating */ 1964 /* methods shouldn't be called if no task is actually migrating */
2035 retval = 0; 1965 if (list_empty(&tset.src_csets))
2036 if (!group_size) 1966 return 0;
2037 goto out_free_group_list;
2038 1967
2039 /* 1968 /* check that we can legitimately attach to the cgroup */
2040 * step 1: check that we can legitimately attach to the cgroup.
2041 */
2042 for_each_css(css, i, cgrp) { 1969 for_each_css(css, i, cgrp) {
2043 if (css->ss->can_attach) { 1970 if (css->ss->can_attach) {
2044 retval = css->ss->can_attach(css, &tset); 1971 ret = css->ss->can_attach(css, &tset);
2045 if (retval) { 1972 if (ret) {
2046 failed_css = css; 1973 failed_css = css;
2047 goto out_cancel_attach; 1974 goto out_cancel_attach;
2048 } 1975 }
@@ -2050,70 +1977,91 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2050 } 1977 }
2051 1978
2052 /* 1979 /*
2053 * step 2: make sure css_sets exist for all threads to be migrated. 1980 * Now that we're guaranteed success, proceed to move all tasks to
2054 * we use find_css_set, which allocates a new one if necessary. 1981 * the new cgroup. There are no failure cases after here, so this
1982 * is the commit point.
2055 */ 1983 */
2056 for (i = 0; i < group_size; i++) { 1984 down_write(&css_set_rwsem);
2057 struct css_set *old_cset; 1985 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2058 1986 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2059 tc = flex_array_get(group, i); 1987 cgroup_task_migrate(cset->mg_src_cgrp, task,
2060 old_cset = task_css_set(tc->task); 1988 cset->mg_dst_cset);
2061 tc->cset = find_css_set(old_cset, cgrp);
2062 if (!tc->cset) {
2063 retval = -ENOMEM;
2064 goto out_put_css_set_refs;
2065 }
2066 } 1989 }
1990 up_write(&css_set_rwsem);
2067 1991
2068 /* 1992 /*
2069 * step 3: now that we're guaranteed success wrt the css_sets, 1993 * Migration is committed, all target tasks are now on dst_csets.
2070 * proceed to move all tasks to the new cgroup. There are no 1994 * Nothing is sensitive to fork() after this point. Notify
2071 * failure cases after here, so this is the commit point. 1995 * controllers that migration is complete.
2072 */ 1996 */
2073 for (i = 0; i < group_size; i++) { 1997 tset.csets = &tset.dst_csets;
2074 tc = flex_array_get(group, i);
2075 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
2076 }
2077 /* nothing is sensitive to fork() after this point. */
2078 1998
2079 /*
2080 * step 4: do subsystem attach callbacks.
2081 */
2082 for_each_css(css, i, cgrp) 1999 for_each_css(css, i, cgrp)
2083 if (css->ss->attach) 2000 if (css->ss->attach)
2084 css->ss->attach(css, &tset); 2001 css->ss->attach(css, &tset);
2085 2002
2086 /* 2003 ret = 0;
2087 * step 5: success! and cleanup 2004 goto out_release_tset;
2088 */ 2005
2089 retval = 0;
2090out_put_css_set_refs:
2091 if (retval) {
2092 for (i = 0; i < group_size; i++) {
2093 tc = flex_array_get(group, i);
2094 if (!tc->cset)
2095 break;
2096 put_css_set(tc->cset);
2097 }
2098 }
2099out_cancel_attach: 2006out_cancel_attach:
2100 if (retval) { 2007 for_each_css(css, i, cgrp) {
2101 for_each_css(css, i, cgrp) { 2008 if (css == failed_css)
2102 if (css == failed_css) 2009 break;
2103 break; 2010 if (css->ss->cancel_attach)
2104 if (css->ss->cancel_attach) 2011 css->ss->cancel_attach(css, &tset);
2105 css->ss->cancel_attach(css, &tset);
2106 }
2107 } 2012 }
2108out_free_group_list: 2013out_release_tset:
2109 flex_array_free(group); 2014 down_write(&css_set_rwsem);
2110 return retval; 2015 list_splice_init(&tset.dst_csets, &tset.src_csets);
2016 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
2017 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2018 list_del_init(&cset->mg_node);
2019 }
2020 up_write(&css_set_rwsem);
2021 return ret;
2022}
2023
2024/**
2025 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2026 * @dst_cgrp: the cgroup to attach to
2027 * @leader: the task or the leader of the threadgroup to be attached
2028 * @threadgroup: attach the whole threadgroup?
2029 *
2030 * Call holding cgroup_mutex and threadgroup_lock of @leader.
2031 */
2032static int cgroup_attach_task(struct cgroup *dst_cgrp,
2033 struct task_struct *leader, bool threadgroup)
2034{
2035 LIST_HEAD(preloaded_csets);
2036 struct task_struct *task;
2037 int ret;
2038
2039 /* look up all src csets */
2040 down_read(&css_set_rwsem);
2041 rcu_read_lock();
2042 task = leader;
2043 do {
2044 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2045 &preloaded_csets);
2046 if (!threadgroup)
2047 break;
2048 } while_each_thread(leader, task);
2049 rcu_read_unlock();
2050 up_read(&css_set_rwsem);
2051
2052 /* prepare dst csets and commit */
2053 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2054 if (!ret)
2055 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2056
2057 cgroup_migrate_finish(&preloaded_csets);
2058 return ret;
2111} 2059}
2112 2060
2113/* 2061/*
2114 * Find the task_struct of the task to attach by vpid and pass it along to the 2062 * Find the task_struct of the task to attach by vpid and pass it along to the
2115 * function to attach either it or all tasks in its threadgroup. Will lock 2063 * function to attach either it or all tasks in its threadgroup. Will lock
2116 * cgroup_mutex and threadgroup; may take task_lock of task. 2064 * cgroup_mutex and threadgroup.
2117 */ 2065 */
2118static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup) 2066static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
2119{ 2067{
@@ -2198,12 +2146,19 @@ out_unlock_cgroup:
2198 */ 2146 */
2199int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) 2147int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2200{ 2148{
2201 struct cgroupfs_root *root; 2149 struct cgroup_root *root;
2202 int retval = 0; 2150 int retval = 0;
2203 2151
2204 mutex_lock(&cgroup_mutex); 2152 mutex_lock(&cgroup_mutex);
2205 for_each_active_root(root) { 2153 for_each_root(root) {
2206 struct cgroup *from_cgrp = task_cgroup_from_root(from, root); 2154 struct cgroup *from_cgrp;
2155
2156 if (root == &cgrp_dfl_root)
2157 continue;
2158
2159 down_read(&css_set_rwsem);
2160 from_cgrp = task_cgroup_from_root(from, root);
2161 up_read(&css_set_rwsem);
2207 2162
2208 retval = cgroup_attach_task(from_cgrp, tsk, false); 2163 retval = cgroup_attach_task(from_cgrp, tsk, false);
2209 if (retval) 2164 if (retval)
@@ -2228,16 +2183,17 @@ static int cgroup_procs_write(struct cgroup_subsys_state *css,
2228} 2183}
2229 2184
2230static int cgroup_release_agent_write(struct cgroup_subsys_state *css, 2185static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2231 struct cftype *cft, const char *buffer) 2186 struct cftype *cft, char *buffer)
2232{ 2187{
2233 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); 2188 struct cgroup_root *root = css->cgroup->root;
2234 if (strlen(buffer) >= PATH_MAX) 2189
2235 return -EINVAL; 2190 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
2236 if (!cgroup_lock_live_group(css->cgroup)) 2191 if (!cgroup_lock_live_group(css->cgroup))
2237 return -ENODEV; 2192 return -ENODEV;
2238 mutex_lock(&cgroup_root_mutex); 2193 spin_lock(&release_agent_path_lock);
2239 strcpy(css->cgroup->root->release_agent_path, buffer); 2194 strlcpy(root->release_agent_path, buffer,
2240 mutex_unlock(&cgroup_root_mutex); 2195 sizeof(root->release_agent_path));
2196 spin_unlock(&release_agent_path_lock);
2241 mutex_unlock(&cgroup_mutex); 2197 mutex_unlock(&cgroup_mutex);
2242 return 0; 2198 return 0;
2243} 2199}
@@ -2262,32 +2218,23 @@ static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
2262 return 0; 2218 return 0;
2263} 2219}
2264 2220
2265/* A buffer size big enough for numbers or short strings */ 2221static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2266#define CGROUP_LOCAL_BUFFER_SIZE 64 2222 size_t nbytes, loff_t off)
2267
2268static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
2269 size_t nbytes, loff_t *ppos)
2270{ 2223{
2271 struct cfent *cfe = __d_cfe(file->f_dentry); 2224 struct cgroup *cgrp = of->kn->parent->priv;
2272 struct cftype *cft = __d_cft(file->f_dentry); 2225 struct cftype *cft = of->kn->priv;
2273 struct cgroup_subsys_state *css = cfe->css; 2226 struct cgroup_subsys_state *css;
2274 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2275 char *buf;
2276 int ret; 2227 int ret;
2277 2228
2278 if (nbytes >= max_bytes) 2229 /*
2279 return -E2BIG; 2230 * kernfs guarantees that a file isn't deleted with operations in
2280 2231 * flight, which means that the matching css is and stays alive and
2281 buf = kmalloc(nbytes + 1, GFP_KERNEL); 2232 * doesn't need to be pinned. The RCU locking is not necessary
2282 if (!buf) 2233 * either. It's just for the convenience of using cgroup_css().
2283 return -ENOMEM; 2234 */
2284 2235 rcu_read_lock();
2285 if (copy_from_user(buf, userbuf, nbytes)) { 2236 css = cgroup_css(cgrp, cft->ss);
2286 ret = -EFAULT; 2237 rcu_read_unlock();
2287 goto out_free;
2288 }
2289
2290 buf[nbytes] = '\0';
2291 2238
2292 if (cft->write_string) { 2239 if (cft->write_string) {
2293 ret = cft->write_string(css, cft, strstrip(buf)); 2240 ret = cft->write_string(css, cft, strstrip(buf));
@@ -2306,53 +2253,23 @@ static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
2306 } else { 2253 } else {
2307 ret = -EINVAL; 2254 ret = -EINVAL;
2308 } 2255 }
2309out_free: 2256
2310 kfree(buf);
2311 return ret ?: nbytes; 2257 return ret ?: nbytes;
2312} 2258}
2313 2259
2314/*
2315 * seqfile ops/methods for returning structured data. Currently just
2316 * supports string->u64 maps, but can be extended in future.
2317 */
2318
2319static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) 2260static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
2320{ 2261{
2321 struct cftype *cft = seq_cft(seq); 2262 return seq_cft(seq)->seq_start(seq, ppos);
2322
2323 if (cft->seq_start) {
2324 return cft->seq_start(seq, ppos);
2325 } else {
2326 /*
2327 * The same behavior and code as single_open(). Returns
2328 * !NULL if pos is at the beginning; otherwise, NULL.
2329 */
2330 return NULL + !*ppos;
2331 }
2332} 2263}
2333 2264
2334static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) 2265static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2335{ 2266{
2336 struct cftype *cft = seq_cft(seq); 2267 return seq_cft(seq)->seq_next(seq, v, ppos);
2337
2338 if (cft->seq_next) {
2339 return cft->seq_next(seq, v, ppos);
2340 } else {
2341 /*
2342 * The same behavior and code as single_open(), always
2343 * terminate after the initial read.
2344 */
2345 ++*ppos;
2346 return NULL;
2347 }
2348} 2268}
2349 2269
2350static void cgroup_seqfile_stop(struct seq_file *seq, void *v) 2270static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2351{ 2271{
2352 struct cftype *cft = seq_cft(seq); 2272 seq_cft(seq)->seq_stop(seq, v);
2353
2354 if (cft->seq_stop)
2355 cft->seq_stop(seq, v);
2356} 2273}
2357 2274
2358static int cgroup_seqfile_show(struct seq_file *m, void *arg) 2275static int cgroup_seqfile_show(struct seq_file *m, void *arg)
@@ -2372,96 +2289,35 @@ static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2372 return 0; 2289 return 0;
2373} 2290}
2374 2291
2375static struct seq_operations cgroup_seq_operations = { 2292static struct kernfs_ops cgroup_kf_single_ops = {
2376 .start = cgroup_seqfile_start, 2293 .atomic_write_len = PAGE_SIZE,
2377 .next = cgroup_seqfile_next, 2294 .write = cgroup_file_write,
2378 .stop = cgroup_seqfile_stop, 2295 .seq_show = cgroup_seqfile_show,
2379 .show = cgroup_seqfile_show,
2380}; 2296};
2381 2297
2382static int cgroup_file_open(struct inode *inode, struct file *file) 2298static struct kernfs_ops cgroup_kf_ops = {
2383{ 2299 .atomic_write_len = PAGE_SIZE,
2384 struct cfent *cfe = __d_cfe(file->f_dentry); 2300 .write = cgroup_file_write,
2385 struct cftype *cft = __d_cft(file->f_dentry); 2301 .seq_start = cgroup_seqfile_start,
2386 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); 2302 .seq_next = cgroup_seqfile_next,
2387 struct cgroup_subsys_state *css; 2303 .seq_stop = cgroup_seqfile_stop,
2388 struct cgroup_open_file *of; 2304 .seq_show = cgroup_seqfile_show,
2389 int err; 2305};
2390
2391 err = generic_file_open(inode, file);
2392 if (err)
2393 return err;
2394
2395 /*
2396 * If the file belongs to a subsystem, pin the css. Will be
2397 * unpinned either on open failure or release. This ensures that
2398 * @css stays alive for all file operations.
2399 */
2400 rcu_read_lock();
2401 css = cgroup_css(cgrp, cft->ss);
2402 if (cft->ss && !css_tryget(css))
2403 css = NULL;
2404 rcu_read_unlock();
2405
2406 if (!css)
2407 return -ENODEV;
2408
2409 /*
2410 * @cfe->css is used by read/write/close to determine the
2411 * associated css. @file->private_data would be a better place but
2412 * that's already used by seqfile. Multiple accessors may use it
2413 * simultaneously which is okay as the association never changes.
2414 */
2415 WARN_ON_ONCE(cfe->css && cfe->css != css);
2416 cfe->css = css;
2417
2418 of = __seq_open_private(file, &cgroup_seq_operations,
2419 sizeof(struct cgroup_open_file));
2420 if (of) {
2421 of->cfe = cfe;
2422 return 0;
2423 }
2424
2425 if (css->ss)
2426 css_put(css);
2427 return -ENOMEM;
2428}
2429
2430static int cgroup_file_release(struct inode *inode, struct file *file)
2431{
2432 struct cfent *cfe = __d_cfe(file->f_dentry);
2433 struct cgroup_subsys_state *css = cfe->css;
2434
2435 if (css->ss)
2436 css_put(css);
2437 return seq_release_private(inode, file);
2438}
2439 2306
2440/* 2307/*
2441 * cgroup_rename - Only allow simple rename of directories in place. 2308 * cgroup_rename - Only allow simple rename of directories in place.
2442 */ 2309 */
2443static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, 2310static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2444 struct inode *new_dir, struct dentry *new_dentry) 2311 const char *new_name_str)
2445{ 2312{
2313 struct cgroup *cgrp = kn->priv;
2446 int ret; 2314 int ret;
2447 struct cgroup_name *name, *old_name;
2448 struct cgroup *cgrp;
2449
2450 /*
2451 * It's convinient to use parent dir's i_mutex to protected
2452 * cgrp->name.
2453 */
2454 lockdep_assert_held(&old_dir->i_mutex);
2455 2315
2456 if (!S_ISDIR(old_dentry->d_inode->i_mode)) 2316 if (kernfs_type(kn) != KERNFS_DIR)
2457 return -ENOTDIR; 2317 return -ENOTDIR;
2458 if (new_dentry->d_inode) 2318 if (kn->parent != new_parent)
2459 return -EEXIST;
2460 if (old_dir != new_dir)
2461 return -EIO; 2319 return -EIO;
2462 2320
2463 cgrp = __d_cgrp(old_dentry);
2464
2465 /* 2321 /*
2466 * This isn't a proper migration and its usefulness is very 2322 * This isn't a proper migration and its usefulness is very
2467 * limited. Disallow if sane_behavior. 2323 * limited. Disallow if sane_behavior.
@@ -2469,218 +2325,61 @@ static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2469 if (cgroup_sane_behavior(cgrp)) 2325 if (cgroup_sane_behavior(cgrp))
2470 return -EPERM; 2326 return -EPERM;
2471 2327
2472 name = cgroup_alloc_name(new_dentry); 2328 /*
2473 if (!name) 2329 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2474 return -ENOMEM; 2330 * active_ref. kernfs_rename() doesn't require active_ref
2475 2331 * protection. Break them before grabbing cgroup_tree_mutex.
2476 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); 2332 */
2477 if (ret) { 2333 kernfs_break_active_protection(new_parent);
2478 kfree(name); 2334 kernfs_break_active_protection(kn);
2479 return ret;
2480 }
2481
2482 old_name = rcu_dereference_protected(cgrp->name, true);
2483 rcu_assign_pointer(cgrp->name, name);
2484
2485 kfree_rcu(old_name, rcu_head);
2486 return 0;
2487}
2488
2489static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2490{
2491 if (S_ISDIR(dentry->d_inode->i_mode))
2492 return &__d_cgrp(dentry)->xattrs;
2493 else
2494 return &__d_cfe(dentry)->xattrs;
2495}
2496
2497static inline int xattr_enabled(struct dentry *dentry)
2498{
2499 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2500 return root->flags & CGRP_ROOT_XATTR;
2501}
2502
2503static bool is_valid_xattr(const char *name)
2504{
2505 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2506 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2507 return true;
2508 return false;
2509}
2510
2511static int cgroup_setxattr(struct dentry *dentry, const char *name,
2512 const void *val, size_t size, int flags)
2513{
2514 if (!xattr_enabled(dentry))
2515 return -EOPNOTSUPP;
2516 if (!is_valid_xattr(name))
2517 return -EINVAL;
2518 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2519}
2520
2521static int cgroup_removexattr(struct dentry *dentry, const char *name)
2522{
2523 if (!xattr_enabled(dentry))
2524 return -EOPNOTSUPP;
2525 if (!is_valid_xattr(name))
2526 return -EINVAL;
2527 return simple_xattr_remove(__d_xattrs(dentry), name);
2528}
2529
2530static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2531 void *buf, size_t size)
2532{
2533 if (!xattr_enabled(dentry))
2534 return -EOPNOTSUPP;
2535 if (!is_valid_xattr(name))
2536 return -EINVAL;
2537 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2538}
2539
2540static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2541{
2542 if (!xattr_enabled(dentry))
2543 return -EOPNOTSUPP;
2544 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2545}
2546
2547static const struct file_operations cgroup_file_operations = {
2548 .read = seq_read,
2549 .write = cgroup_file_write,
2550 .llseek = generic_file_llseek,
2551 .open = cgroup_file_open,
2552 .release = cgroup_file_release,
2553};
2554
2555static const struct inode_operations cgroup_file_inode_operations = {
2556 .setxattr = cgroup_setxattr,
2557 .getxattr = cgroup_getxattr,
2558 .listxattr = cgroup_listxattr,
2559 .removexattr = cgroup_removexattr,
2560};
2561
2562static const struct inode_operations cgroup_dir_inode_operations = {
2563 .lookup = simple_lookup,
2564 .mkdir = cgroup_mkdir,
2565 .rmdir = cgroup_rmdir,
2566 .rename = cgroup_rename,
2567 .setxattr = cgroup_setxattr,
2568 .getxattr = cgroup_getxattr,
2569 .listxattr = cgroup_listxattr,
2570 .removexattr = cgroup_removexattr,
2571};
2572
2573static int cgroup_create_file(struct dentry *dentry, umode_t mode,
2574 struct super_block *sb)
2575{
2576 struct inode *inode;
2577
2578 if (!dentry)
2579 return -ENOENT;
2580 if (dentry->d_inode)
2581 return -EEXIST;
2582 2335
2583 inode = cgroup_new_inode(mode, sb); 2336 mutex_lock(&cgroup_tree_mutex);
2584 if (!inode) 2337 mutex_lock(&cgroup_mutex);
2585 return -ENOMEM;
2586 2338
2587 if (S_ISDIR(mode)) { 2339 ret = kernfs_rename(kn, new_parent, new_name_str);
2588 inode->i_op = &cgroup_dir_inode_operations;
2589 inode->i_fop = &simple_dir_operations;
2590 2340
2591 /* start off with i_nlink == 2 (for "." entry) */ 2341 mutex_unlock(&cgroup_mutex);
2592 inc_nlink(inode); 2342 mutex_unlock(&cgroup_tree_mutex);
2593 inc_nlink(dentry->d_parent->d_inode);
2594 2343
2595 /* 2344 kernfs_unbreak_active_protection(kn);
2596 * Control reaches here with cgroup_mutex held. 2345 kernfs_unbreak_active_protection(new_parent);
2597 * @inode->i_mutex should nest outside cgroup_mutex but we 2346 return ret;
2598 * want to populate it immediately without releasing
2599 * cgroup_mutex. As @inode isn't visible to anyone else
2600 * yet, trylock will always succeed without affecting
2601 * lockdep checks.
2602 */
2603 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
2604 } else if (S_ISREG(mode)) {
2605 inode->i_size = 0;
2606 inode->i_fop = &cgroup_file_operations;
2607 inode->i_op = &cgroup_file_inode_operations;
2608 }
2609 d_instantiate(dentry, inode);
2610 dget(dentry); /* Extra count - pin the dentry in core */
2611 return 0;
2612} 2347}
2613 2348
2614/** 2349/* set uid and gid of cgroup dirs and files to that of the creator */
2615 * cgroup_file_mode - deduce file mode of a control file 2350static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2616 * @cft: the control file in question
2617 *
2618 * returns cft->mode if ->mode is not 0
2619 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2620 * returns S_IRUGO if it has only a read handler
2621 * returns S_IWUSR if it has only a write hander
2622 */
2623static umode_t cgroup_file_mode(const struct cftype *cft)
2624{ 2351{
2625 umode_t mode = 0; 2352 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2626 2353 .ia_uid = current_fsuid(),
2627 if (cft->mode) 2354 .ia_gid = current_fsgid(), };
2628 return cft->mode;
2629
2630 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
2631 mode |= S_IRUGO;
2632 2355
2633 if (cft->write_u64 || cft->write_s64 || cft->write_string || 2356 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2634 cft->trigger) 2357 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2635 mode |= S_IWUSR; 2358 return 0;
2636 2359
2637 return mode; 2360 return kernfs_setattr(kn, &iattr);
2638} 2361}
2639 2362
2640static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) 2363static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
2641{ 2364{
2642 struct dentry *dir = cgrp->dentry; 2365 char name[CGROUP_FILE_NAME_MAX];
2643 struct cgroup *parent = __d_cgrp(dir); 2366 struct kernfs_node *kn;
2644 struct dentry *dentry; 2367 struct lock_class_key *key = NULL;
2645 struct cfent *cfe; 2368 int ret;
2646 int error;
2647 umode_t mode;
2648 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2649
2650 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2651 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2652 strcpy(name, cft->ss->name);
2653 strcat(name, ".");
2654 }
2655 strcat(name, cft->name);
2656
2657 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2658
2659 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2660 if (!cfe)
2661 return -ENOMEM;
2662
2663 dentry = lookup_one_len(name, dir, strlen(name));
2664 if (IS_ERR(dentry)) {
2665 error = PTR_ERR(dentry);
2666 goto out;
2667 }
2668 2369
2669 cfe->type = (void *)cft; 2370#ifdef CONFIG_DEBUG_LOCK_ALLOC
2670 cfe->dentry = dentry; 2371 key = &cft->lockdep_key;
2671 dentry->d_fsdata = cfe; 2372#endif
2672 simple_xattrs_init(&cfe->xattrs); 2373 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2374 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2375 NULL, false, key);
2376 if (IS_ERR(kn))
2377 return PTR_ERR(kn);
2673 2378
2674 mode = cgroup_file_mode(cft); 2379 ret = cgroup_kn_set_ugid(kn);
2675 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); 2380 if (ret)
2676 if (!error) { 2381 kernfs_remove(kn);
2677 list_add_tail(&cfe->node, &parent->files); 2382 return ret;
2678 cfe = NULL;
2679 }
2680 dput(dentry);
2681out:
2682 kfree(cfe);
2683 return error;
2684} 2383}
2685 2384
2686/** 2385/**
@@ -2700,11 +2399,12 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2700 struct cftype *cft; 2399 struct cftype *cft;
2701 int ret; 2400 int ret;
2702 2401
2703 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); 2402 lockdep_assert_held(&cgroup_tree_mutex);
2704 lockdep_assert_held(&cgroup_mutex);
2705 2403
2706 for (cft = cfts; cft->name[0] != '\0'; cft++) { 2404 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2707 /* does cft->flags tell us to skip this file on @cgrp? */ 2405 /* does cft->flags tell us to skip this file on @cgrp? */
2406 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2407 continue;
2708 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) 2408 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2709 continue; 2409 continue;
2710 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) 2410 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
@@ -2726,44 +2426,19 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2726 return 0; 2426 return 0;
2727} 2427}
2728 2428
2729static void cgroup_cfts_prepare(void) 2429static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
2730 __acquires(&cgroup_mutex)
2731{
2732 /*
2733 * Thanks to the entanglement with vfs inode locking, we can't walk
2734 * the existing cgroups under cgroup_mutex and create files.
2735 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2736 * lock before calling cgroup_addrm_files().
2737 */
2738 mutex_lock(&cgroup_mutex);
2739}
2740
2741static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
2742 __releases(&cgroup_mutex)
2743{ 2430{
2744 LIST_HEAD(pending); 2431 LIST_HEAD(pending);
2745 struct cgroup_subsys *ss = cfts[0].ss; 2432 struct cgroup_subsys *ss = cfts[0].ss;
2746 struct cgroup *root = &ss->root->top_cgroup; 2433 struct cgroup *root = &ss->root->cgrp;
2747 struct super_block *sb = ss->root->sb;
2748 struct dentry *prev = NULL;
2749 struct inode *inode;
2750 struct cgroup_subsys_state *css; 2434 struct cgroup_subsys_state *css;
2751 u64 update_before;
2752 int ret = 0; 2435 int ret = 0;
2753 2436
2754 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ 2437 lockdep_assert_held(&cgroup_tree_mutex);
2755 if (!cfts || ss->root == &cgroup_dummy_root ||
2756 !atomic_inc_not_zero(&sb->s_active)) {
2757 mutex_unlock(&cgroup_mutex);
2758 return 0;
2759 }
2760 2438
2761 /* 2439 /* don't bother if @ss isn't attached */
2762 * All cgroups which are created after we drop cgroup_mutex will 2440 if (ss->root == &cgrp_dfl_root)
2763 * have the updated set of files, so we only need to update the 2441 return 0;
2764 * cgroups created before the current @cgroup_serial_nr_next.
2765 */
2766 update_before = cgroup_serial_nr_next;
2767 2442
2768 /* add/rm files for all cgroups created before */ 2443 /* add/rm files for all cgroups created before */
2769 css_for_each_descendant_pre(css, cgroup_css(root, ss)) { 2444 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
@@ -2772,62 +2447,75 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
2772 if (cgroup_is_dead(cgrp)) 2447 if (cgroup_is_dead(cgrp))
2773 continue; 2448 continue;
2774 2449
2775 inode = cgrp->dentry->d_inode; 2450 ret = cgroup_addrm_files(cgrp, cfts, is_add);
2776 dget(cgrp->dentry);
2777 dput(prev);
2778 prev = cgrp->dentry;
2779
2780 mutex_unlock(&cgroup_mutex);
2781 mutex_lock(&inode->i_mutex);
2782 mutex_lock(&cgroup_mutex);
2783 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
2784 ret = cgroup_addrm_files(cgrp, cfts, is_add);
2785 mutex_unlock(&inode->i_mutex);
2786 if (ret) 2451 if (ret)
2787 break; 2452 break;
2788 } 2453 }
2789 mutex_unlock(&cgroup_mutex); 2454
2790 dput(prev); 2455 if (is_add && !ret)
2791 deactivate_super(sb); 2456 kernfs_activate(root->kn);
2792 return ret; 2457 return ret;
2793} 2458}
2794 2459
2795/** 2460static void cgroup_exit_cftypes(struct cftype *cfts)
2796 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2797 * @ss: target cgroup subsystem
2798 * @cfts: zero-length name terminated array of cftypes
2799 *
2800 * Register @cfts to @ss. Files described by @cfts are created for all
2801 * existing cgroups to which @ss is attached and all future cgroups will
2802 * have them too. This function can be called anytime whether @ss is
2803 * attached or not.
2804 *
2805 * Returns 0 on successful registration, -errno on failure. Note that this
2806 * function currently returns 0 as long as @cfts registration is successful
2807 * even if some file creation attempts on existing cgroups fail.
2808 */
2809int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2810{ 2461{
2811 struct cftype_set *set;
2812 struct cftype *cft; 2462 struct cftype *cft;
2813 int ret;
2814 2463
2815 set = kzalloc(sizeof(*set), GFP_KERNEL); 2464 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2816 if (!set) 2465 /* free copy for custom atomic_write_len, see init_cftypes() */
2817 return -ENOMEM; 2466 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2467 kfree(cft->kf_ops);
2468 cft->kf_ops = NULL;
2469 cft->ss = NULL;
2470 }
2471}
2472
2473static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2474{
2475 struct cftype *cft;
2476
2477 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2478 struct kernfs_ops *kf_ops;
2818 2479
2819 for (cft = cfts; cft->name[0] != '\0'; cft++) 2480 WARN_ON(cft->ss || cft->kf_ops);
2481
2482 if (cft->seq_start)
2483 kf_ops = &cgroup_kf_ops;
2484 else
2485 kf_ops = &cgroup_kf_single_ops;
2486
2487 /*
2488 * Ugh... if @cft wants a custom max_write_len, we need to
2489 * make a copy of kf_ops to set its atomic_write_len.
2490 */
2491 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2492 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2493 if (!kf_ops) {
2494 cgroup_exit_cftypes(cfts);
2495 return -ENOMEM;
2496 }
2497 kf_ops->atomic_write_len = cft->max_write_len;
2498 }
2499
2500 cft->kf_ops = kf_ops;
2820 cft->ss = ss; 2501 cft->ss = ss;
2502 }
2821 2503
2822 cgroup_cfts_prepare(); 2504 return 0;
2823 set->cfts = cfts; 2505}
2824 list_add_tail(&set->node, &ss->cftsets); 2506
2825 ret = cgroup_cfts_commit(cfts, true); 2507static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2826 if (ret) 2508{
2827 cgroup_rm_cftypes(cfts); 2509 lockdep_assert_held(&cgroup_tree_mutex);
2828 return ret; 2510
2511 if (!cfts || !cfts[0].ss)
2512 return -ENOENT;
2513
2514 list_del(&cfts->node);
2515 cgroup_apply_cftypes(cfts, false);
2516 cgroup_exit_cftypes(cfts);
2517 return 0;
2829} 2518}
2830EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2831 2519
2832/** 2520/**
2833 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem 2521 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
@@ -2842,24 +2530,48 @@ EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2842 */ 2530 */
2843int cgroup_rm_cftypes(struct cftype *cfts) 2531int cgroup_rm_cftypes(struct cftype *cfts)
2844{ 2532{
2845 struct cftype_set *set; 2533 int ret;
2846 2534
2847 if (!cfts || !cfts[0].ss) 2535 mutex_lock(&cgroup_tree_mutex);
2848 return -ENOENT; 2536 ret = cgroup_rm_cftypes_locked(cfts);
2537 mutex_unlock(&cgroup_tree_mutex);
2538 return ret;
2539}
2849 2540
2850 cgroup_cfts_prepare(); 2541/**
2542 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2543 * @ss: target cgroup subsystem
2544 * @cfts: zero-length name terminated array of cftypes
2545 *
2546 * Register @cfts to @ss. Files described by @cfts are created for all
2547 * existing cgroups to which @ss is attached and all future cgroups will
2548 * have them too. This function can be called anytime whether @ss is
2549 * attached or not.
2550 *
2551 * Returns 0 on successful registration, -errno on failure. Note that this
2552 * function currently returns 0 as long as @cfts registration is successful
2553 * even if some file creation attempts on existing cgroups fail.
2554 */
2555int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2556{
2557 int ret;
2851 2558
2852 list_for_each_entry(set, &cfts[0].ss->cftsets, node) { 2559 if (!cfts || cfts[0].name[0] == '\0')
2853 if (set->cfts == cfts) { 2560 return 0;
2854 list_del(&set->node);
2855 kfree(set);
2856 cgroup_cfts_commit(cfts, false);
2857 return 0;
2858 }
2859 }
2860 2561
2861 cgroup_cfts_commit(NULL, false); 2562 ret = cgroup_init_cftypes(ss, cfts);
2862 return -ENOENT; 2563 if (ret)
2564 return ret;
2565
2566 mutex_lock(&cgroup_tree_mutex);
2567
2568 list_add_tail(&cfts->node, &ss->cfts);
2569 ret = cgroup_apply_cftypes(cfts, true);
2570 if (ret)
2571 cgroup_rm_cftypes_locked(cfts);
2572
2573 mutex_unlock(&cgroup_tree_mutex);
2574 return ret;
2863} 2575}
2864 2576
2865/** 2577/**
@@ -2868,57 +2580,18 @@ int cgroup_rm_cftypes(struct cftype *cfts)
2868 * 2580 *
2869 * Return the number of tasks in the cgroup. 2581 * Return the number of tasks in the cgroup.
2870 */ 2582 */
2871int cgroup_task_count(const struct cgroup *cgrp) 2583static int cgroup_task_count(const struct cgroup *cgrp)
2872{ 2584{
2873 int count = 0; 2585 int count = 0;
2874 struct cgrp_cset_link *link; 2586 struct cgrp_cset_link *link;
2875 2587
2876 read_lock(&css_set_lock); 2588 down_read(&css_set_rwsem);
2877 list_for_each_entry(link, &cgrp->cset_links, cset_link) 2589 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2878 count += atomic_read(&link->cset->refcount); 2590 count += atomic_read(&link->cset->refcount);
2879 read_unlock(&css_set_lock); 2591 up_read(&css_set_rwsem);
2880 return count; 2592 return count;
2881} 2593}
2882 2594
2883/*
2884 * To reduce the fork() overhead for systems that are not actually using
2885 * their cgroups capability, we don't maintain the lists running through
2886 * each css_set to its tasks until we see the list actually used - in other
2887 * words after the first call to css_task_iter_start().
2888 */
2889static void cgroup_enable_task_cg_lists(void)
2890{
2891 struct task_struct *p, *g;
2892 write_lock(&css_set_lock);
2893 use_task_css_set_links = 1;
2894 /*
2895 * We need tasklist_lock because RCU is not safe against
2896 * while_each_thread(). Besides, a forking task that has passed
2897 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2898 * is not guaranteed to have its child immediately visible in the
2899 * tasklist if we walk through it with RCU.
2900 */
2901 read_lock(&tasklist_lock);
2902 do_each_thread(g, p) {
2903 task_lock(p);
2904 /*
2905 * We should check if the process is exiting, otherwise
2906 * it will race with cgroup_exit() in that the list
2907 * entry won't be deleted though the process has exited.
2908 * Do it while holding siglock so that we don't end up
2909 * racing against cgroup_exit().
2910 */
2911 spin_lock_irq(&p->sighand->siglock);
2912 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
2913 list_add(&p->cg_list, &task_css_set(p)->tasks);
2914 spin_unlock_irq(&p->sighand->siglock);
2915
2916 task_unlock(p);
2917 } while_each_thread(g, p);
2918 read_unlock(&tasklist_lock);
2919 write_unlock(&css_set_lock);
2920}
2921
2922/** 2595/**
2923 * css_next_child - find the next child of a given css 2596 * css_next_child - find the next child of a given css
2924 * @pos_css: the current position (%NULL to initiate traversal) 2597 * @pos_css: the current position (%NULL to initiate traversal)
@@ -2937,7 +2610,7 @@ css_next_child(struct cgroup_subsys_state *pos_css,
2937 struct cgroup *cgrp = parent_css->cgroup; 2610 struct cgroup *cgrp = parent_css->cgroup;
2938 struct cgroup *next; 2611 struct cgroup *next;
2939 2612
2940 cgroup_assert_mutex_or_rcu_locked(); 2613 cgroup_assert_mutexes_or_rcu_locked();
2941 2614
2942 /* 2615 /*
2943 * @pos could already have been removed. Once a cgroup is removed, 2616 * @pos could already have been removed. Once a cgroup is removed,
@@ -2973,7 +2646,6 @@ css_next_child(struct cgroup_subsys_state *pos_css,
2973 2646
2974 return cgroup_css(next, parent_css->ss); 2647 return cgroup_css(next, parent_css->ss);
2975} 2648}
2976EXPORT_SYMBOL_GPL(css_next_child);
2977 2649
2978/** 2650/**
2979 * css_next_descendant_pre - find the next descendant for pre-order walk 2651 * css_next_descendant_pre - find the next descendant for pre-order walk
@@ -2995,7 +2667,7 @@ css_next_descendant_pre(struct cgroup_subsys_state *pos,
2995{ 2667{
2996 struct cgroup_subsys_state *next; 2668 struct cgroup_subsys_state *next;
2997 2669
2998 cgroup_assert_mutex_or_rcu_locked(); 2670 cgroup_assert_mutexes_or_rcu_locked();
2999 2671
3000 /* if first iteration, visit @root */ 2672 /* if first iteration, visit @root */
3001 if (!pos) 2673 if (!pos)
@@ -3016,7 +2688,6 @@ css_next_descendant_pre(struct cgroup_subsys_state *pos,
3016 2688
3017 return NULL; 2689 return NULL;
3018} 2690}
3019EXPORT_SYMBOL_GPL(css_next_descendant_pre);
3020 2691
3021/** 2692/**
3022 * css_rightmost_descendant - return the rightmost descendant of a css 2693 * css_rightmost_descendant - return the rightmost descendant of a css
@@ -3036,7 +2707,7 @@ css_rightmost_descendant(struct cgroup_subsys_state *pos)
3036{ 2707{
3037 struct cgroup_subsys_state *last, *tmp; 2708 struct cgroup_subsys_state *last, *tmp;
3038 2709
3039 cgroup_assert_mutex_or_rcu_locked(); 2710 cgroup_assert_mutexes_or_rcu_locked();
3040 2711
3041 do { 2712 do {
3042 last = pos; 2713 last = pos;
@@ -3048,7 +2719,6 @@ css_rightmost_descendant(struct cgroup_subsys_state *pos)
3048 2719
3049 return last; 2720 return last;
3050} 2721}
3051EXPORT_SYMBOL_GPL(css_rightmost_descendant);
3052 2722
3053static struct cgroup_subsys_state * 2723static struct cgroup_subsys_state *
3054css_leftmost_descendant(struct cgroup_subsys_state *pos) 2724css_leftmost_descendant(struct cgroup_subsys_state *pos)
@@ -3084,7 +2754,7 @@ css_next_descendant_post(struct cgroup_subsys_state *pos,
3084{ 2754{
3085 struct cgroup_subsys_state *next; 2755 struct cgroup_subsys_state *next;
3086 2756
3087 cgroup_assert_mutex_or_rcu_locked(); 2757 cgroup_assert_mutexes_or_rcu_locked();
3088 2758
3089 /* if first iteration, visit leftmost descendant which may be @root */ 2759 /* if first iteration, visit leftmost descendant which may be @root */
3090 if (!pos) 2760 if (!pos)
@@ -3102,7 +2772,6 @@ css_next_descendant_post(struct cgroup_subsys_state *pos,
3102 /* no sibling left, visit parent */ 2772 /* no sibling left, visit parent */
3103 return css_parent(pos); 2773 return css_parent(pos);
3104} 2774}
3105EXPORT_SYMBOL_GPL(css_next_descendant_post);
3106 2775
3107/** 2776/**
3108 * css_advance_task_iter - advance a task itererator to the next css_set 2777 * css_advance_task_iter - advance a task itererator to the next css_set
@@ -3125,9 +2794,14 @@ static void css_advance_task_iter(struct css_task_iter *it)
3125 } 2794 }
3126 link = list_entry(l, struct cgrp_cset_link, cset_link); 2795 link = list_entry(l, struct cgrp_cset_link, cset_link);
3127 cset = link->cset; 2796 cset = link->cset;
3128 } while (list_empty(&cset->tasks)); 2797 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
2798
3129 it->cset_link = l; 2799 it->cset_link = l;
3130 it->task = cset->tasks.next; 2800
2801 if (!list_empty(&cset->tasks))
2802 it->task = cset->tasks.next;
2803 else
2804 it->task = cset->mg_tasks.next;
3131} 2805}
3132 2806
3133/** 2807/**
@@ -3146,17 +2820,12 @@ static void css_advance_task_iter(struct css_task_iter *it)
3146 */ 2820 */
3147void css_task_iter_start(struct cgroup_subsys_state *css, 2821void css_task_iter_start(struct cgroup_subsys_state *css,
3148 struct css_task_iter *it) 2822 struct css_task_iter *it)
3149 __acquires(css_set_lock) 2823 __acquires(css_set_rwsem)
3150{ 2824{
3151 /* 2825 /* no one should try to iterate before mounting cgroups */
3152 * The first time anyone tries to iterate across a css, we need to 2826 WARN_ON_ONCE(!use_task_css_set_links);
3153 * enable the list linking each css_set to its tasks, and fix up
3154 * all existing tasks.
3155 */
3156 if (!use_task_css_set_links)
3157 cgroup_enable_task_cg_lists();
3158 2827
3159 read_lock(&css_set_lock); 2828 down_read(&css_set_rwsem);
3160 2829
3161 it->origin_css = css; 2830 it->origin_css = css;
3162 it->cset_link = &css->cgroup->cset_links; 2831 it->cset_link = &css->cgroup->cset_links;
@@ -3176,24 +2845,29 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
3176{ 2845{
3177 struct task_struct *res; 2846 struct task_struct *res;
3178 struct list_head *l = it->task; 2847 struct list_head *l = it->task;
3179 struct cgrp_cset_link *link; 2848 struct cgrp_cset_link *link = list_entry(it->cset_link,
2849 struct cgrp_cset_link, cset_link);
3180 2850
3181 /* If the iterator cg is NULL, we have no tasks */ 2851 /* If the iterator cg is NULL, we have no tasks */
3182 if (!it->cset_link) 2852 if (!it->cset_link)
3183 return NULL; 2853 return NULL;
3184 res = list_entry(l, struct task_struct, cg_list); 2854 res = list_entry(l, struct task_struct, cg_list);
3185 /* Advance iterator to find next entry */ 2855
2856 /*
2857 * Advance iterator to find next entry. cset->tasks is consumed
2858 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
2859 * next cset.
2860 */
3186 l = l->next; 2861 l = l->next;
3187 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); 2862
3188 if (l == &link->cset->tasks) { 2863 if (l == &link->cset->tasks)
3189 /* 2864 l = link->cset->mg_tasks.next;
3190 * We reached the end of this task list - move on to the 2865
3191 * next cgrp_cset_link. 2866 if (l == &link->cset->mg_tasks)
3192 */
3193 css_advance_task_iter(it); 2867 css_advance_task_iter(it);
3194 } else { 2868 else
3195 it->task = l; 2869 it->task = l;
3196 } 2870
3197 return res; 2871 return res;
3198} 2872}
3199 2873
@@ -3204,191 +2878,62 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
3204 * Finish task iteration started by css_task_iter_start(). 2878 * Finish task iteration started by css_task_iter_start().
3205 */ 2879 */
3206void css_task_iter_end(struct css_task_iter *it) 2880void css_task_iter_end(struct css_task_iter *it)
3207 __releases(css_set_lock) 2881 __releases(css_set_rwsem)
3208{
3209 read_unlock(&css_set_lock);
3210}
3211
3212static inline int started_after_time(struct task_struct *t1,
3213 struct timespec *time,
3214 struct task_struct *t2)
3215{
3216 int start_diff = timespec_compare(&t1->start_time, time);
3217 if (start_diff > 0) {
3218 return 1;
3219 } else if (start_diff < 0) {
3220 return 0;
3221 } else {
3222 /*
3223 * Arbitrarily, if two processes started at the same
3224 * time, we'll say that the lower pointer value
3225 * started first. Note that t2 may have exited by now
3226 * so this may not be a valid pointer any longer, but
3227 * that's fine - it still serves to distinguish
3228 * between two tasks started (effectively) simultaneously.
3229 */
3230 return t1 > t2;
3231 }
3232}
3233
3234/*
3235 * This function is a callback from heap_insert() and is used to order
3236 * the heap.
3237 * In this case we order the heap in descending task start time.
3238 */
3239static inline int started_after(void *p1, void *p2)
3240{ 2882{
3241 struct task_struct *t1 = p1; 2883 up_read(&css_set_rwsem);
3242 struct task_struct *t2 = p2;
3243 return started_after_time(t1, &t2->start_time, t2);
3244} 2884}
3245 2885
3246/** 2886/**
3247 * css_scan_tasks - iterate though all the tasks in a css 2887 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3248 * @css: the css to iterate tasks of 2888 * @to: cgroup to which the tasks will be moved
3249 * @test: optional test callback 2889 * @from: cgroup in which the tasks currently reside
3250 * @process: process callback
3251 * @data: data passed to @test and @process
3252 * @heap: optional pre-allocated heap used for task iteration
3253 *
3254 * Iterate through all the tasks in @css, calling @test for each, and if it
3255 * returns %true, call @process for it also.
3256 *
3257 * @test may be NULL, meaning always true (select all tasks), which
3258 * effectively duplicates css_task_iter_{start,next,end}() but does not
3259 * lock css_set_lock for the call to @process.
3260 *
3261 * It is guaranteed that @process will act on every task that is a member
3262 * of @css for the duration of this call. This function may or may not
3263 * call @process for tasks that exit or move to a different css during the
3264 * call, or are forked or move into the css during the call.
3265 *
3266 * Note that @test may be called with locks held, and may in some
3267 * situations be called multiple times for the same task, so it should be
3268 * cheap.
3269 * 2890 *
3270 * If @heap is non-NULL, a heap has been pre-allocated and will be used for 2891 * Locking rules between cgroup_post_fork() and the migration path
3271 * heap operations (and its "gt" member will be overwritten), else a 2892 * guarantee that, if a task is forking while being migrated, the new child
3272 * temporary heap will be used (allocation of which may cause this function 2893 * is guaranteed to be either visible in the source cgroup after the
3273 * to fail). 2894 * parent's migration is complete or put into the target cgroup. No task
2895 * can slip out of migration through forking.
3274 */ 2896 */
3275int css_scan_tasks(struct cgroup_subsys_state *css, 2897int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3276 bool (*test)(struct task_struct *, void *),
3277 void (*process)(struct task_struct *, void *),
3278 void *data, struct ptr_heap *heap)
3279{ 2898{
3280 int retval, i; 2899 LIST_HEAD(preloaded_csets);
2900 struct cgrp_cset_link *link;
3281 struct css_task_iter it; 2901 struct css_task_iter it;
3282 struct task_struct *p, *dropped; 2902 struct task_struct *task;
3283 /* Never dereference latest_task, since it's not refcounted */ 2903 int ret;
3284 struct task_struct *latest_task = NULL;
3285 struct ptr_heap tmp_heap;
3286 struct timespec latest_time = { 0, 0 };
3287
3288 if (heap) {
3289 /* The caller supplied our heap and pre-allocated its memory */
3290 heap->gt = &started_after;
3291 } else {
3292 /* We need to allocate our own heap memory */
3293 heap = &tmp_heap;
3294 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3295 if (retval)
3296 /* cannot allocate the heap */
3297 return retval;
3298 }
3299 2904
3300 again: 2905 mutex_lock(&cgroup_mutex);
3301 /*
3302 * Scan tasks in the css, using the @test callback to determine
3303 * which are of interest, and invoking @process callback on the
3304 * ones which need an update. Since we don't want to hold any
3305 * locks during the task updates, gather tasks to be processed in a
3306 * heap structure. The heap is sorted by descending task start
3307 * time. If the statically-sized heap fills up, we overflow tasks
3308 * that started later, and in future iterations only consider tasks
3309 * that started after the latest task in the previous pass. This
3310 * guarantees forward progress and that we don't miss any tasks.
3311 */
3312 heap->size = 0;
3313 css_task_iter_start(css, &it);
3314 while ((p = css_task_iter_next(&it))) {
3315 /*
3316 * Only affect tasks that qualify per the caller's callback,
3317 * if he provided one
3318 */
3319 if (test && !test(p, data))
3320 continue;
3321 /*
3322 * Only process tasks that started after the last task
3323 * we processed
3324 */
3325 if (!started_after_time(p, &latest_time, latest_task))
3326 continue;
3327 dropped = heap_insert(heap, p);
3328 if (dropped == NULL) {
3329 /*
3330 * The new task was inserted; the heap wasn't
3331 * previously full
3332 */
3333 get_task_struct(p);
3334 } else if (dropped != p) {
3335 /*
3336 * The new task was inserted, and pushed out a
3337 * different task
3338 */
3339 get_task_struct(p);
3340 put_task_struct(dropped);
3341 }
3342 /*
3343 * Else the new task was newer than anything already in
3344 * the heap and wasn't inserted
3345 */
3346 }
3347 css_task_iter_end(&it);
3348 2906
3349 if (heap->size) { 2907 /* all tasks in @from are being moved, all csets are source */
3350 for (i = 0; i < heap->size; i++) { 2908 down_read(&css_set_rwsem);
3351 struct task_struct *q = heap->ptrs[i]; 2909 list_for_each_entry(link, &from->cset_links, cset_link)
3352 if (i == 0) { 2910 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3353 latest_time = q->start_time; 2911 up_read(&css_set_rwsem);
3354 latest_task = q;
3355 }
3356 /* Process the task per the caller's callback */
3357 process(q, data);
3358 put_task_struct(q);
3359 }
3360 /*
3361 * If we had to process any tasks at all, scan again
3362 * in case some of them were in the middle of forking
3363 * children that didn't get processed.
3364 * Not the most efficient way to do it, but it avoids
3365 * having to take callback_mutex in the fork path
3366 */
3367 goto again;
3368 }
3369 if (heap == &tmp_heap)
3370 heap_free(&tmp_heap);
3371 return 0;
3372}
3373 2912
3374static void cgroup_transfer_one_task(struct task_struct *task, void *data) 2913 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3375{ 2914 if (ret)
3376 struct cgroup *new_cgroup = data; 2915 goto out_err;
3377 2916
3378 mutex_lock(&cgroup_mutex); 2917 /*
3379 cgroup_attach_task(new_cgroup, task, false); 2918 * Migrate tasks one-by-one until @form is empty. This fails iff
2919 * ->can_attach() fails.
2920 */
2921 do {
2922 css_task_iter_start(&from->dummy_css, &it);
2923 task = css_task_iter_next(&it);
2924 if (task)
2925 get_task_struct(task);
2926 css_task_iter_end(&it);
2927
2928 if (task) {
2929 ret = cgroup_migrate(to, task, false);
2930 put_task_struct(task);
2931 }
2932 } while (task && !ret);
2933out_err:
2934 cgroup_migrate_finish(&preloaded_csets);
3380 mutex_unlock(&cgroup_mutex); 2935 mutex_unlock(&cgroup_mutex);
3381} 2936 return ret;
3382
3383/**
3384 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3385 * @to: cgroup to which the tasks will be moved
3386 * @from: cgroup in which the tasks currently reside
3387 */
3388int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3389{
3390 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3391 to, NULL);
3392} 2937}
3393 2938
3394/* 2939/*
@@ -3687,21 +3232,31 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3687 */ 3232 */
3688int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) 3233int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3689{ 3234{
3690 int ret = -EINVAL; 3235 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
3691 struct cgroup *cgrp; 3236 struct cgroup *cgrp;
3692 struct css_task_iter it; 3237 struct css_task_iter it;
3693 struct task_struct *tsk; 3238 struct task_struct *tsk;
3694 3239
3240 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3241 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3242 kernfs_type(kn) != KERNFS_DIR)
3243 return -EINVAL;
3244
3245 mutex_lock(&cgroup_mutex);
3246
3695 /* 3247 /*
3696 * Validate dentry by checking the superblock operations, 3248 * We aren't being called from kernfs and there's no guarantee on
3697 * and make sure it's a directory. 3249 * @kn->priv's validity. For this and css_tryget_from_dir(),
3250 * @kn->priv is RCU safe. Let's do the RCU dancing.
3698 */ 3251 */
3699 if (dentry->d_sb->s_op != &cgroup_ops || 3252 rcu_read_lock();
3700 !S_ISDIR(dentry->d_inode->i_mode)) 3253 cgrp = rcu_dereference(kn->priv);
3701 goto err; 3254 if (!cgrp || cgroup_is_dead(cgrp)) {
3702 3255 rcu_read_unlock();
3703 ret = 0; 3256 mutex_unlock(&cgroup_mutex);
3704 cgrp = dentry->d_fsdata; 3257 return -ENOENT;
3258 }
3259 rcu_read_unlock();
3705 3260
3706 css_task_iter_start(&cgrp->dummy_css, &it); 3261 css_task_iter_start(&cgrp->dummy_css, &it);
3707 while ((tsk = css_task_iter_next(&it))) { 3262 while ((tsk = css_task_iter_next(&it))) {
@@ -3726,8 +3281,8 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3726 } 3281 }
3727 css_task_iter_end(&it); 3282 css_task_iter_end(&it);
3728 3283
3729err: 3284 mutex_unlock(&cgroup_mutex);
3730 return ret; 3285 return 0;
3731} 3286}
3732 3287
3733 3288
@@ -3745,7 +3300,7 @@ static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3745 * after a seek to the start). Use a binary-search to find the 3300 * after a seek to the start). Use a binary-search to find the
3746 * next pid to display, if any 3301 * next pid to display, if any
3747 */ 3302 */
3748 struct cgroup_open_file *of = s->private; 3303 struct kernfs_open_file *of = s->private;
3749 struct cgroup *cgrp = seq_css(s)->cgroup; 3304 struct cgroup *cgrp = seq_css(s)->cgroup;
3750 struct cgroup_pidlist *l; 3305 struct cgroup_pidlist *l;
3751 enum cgroup_filetype type = seq_cft(s)->private; 3306 enum cgroup_filetype type = seq_cft(s)->private;
@@ -3800,7 +3355,7 @@ static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3800 3355
3801static void cgroup_pidlist_stop(struct seq_file *s, void *v) 3356static void cgroup_pidlist_stop(struct seq_file *s, void *v)
3802{ 3357{
3803 struct cgroup_open_file *of = s->private; 3358 struct kernfs_open_file *of = s->private;
3804 struct cgroup_pidlist *l = of->priv; 3359 struct cgroup_pidlist *l = of->priv;
3805 3360
3806 if (l) 3361 if (l)
@@ -3811,7 +3366,7 @@ static void cgroup_pidlist_stop(struct seq_file *s, void *v)
3811 3366
3812static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos) 3367static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
3813{ 3368{
3814 struct cgroup_open_file *of = s->private; 3369 struct kernfs_open_file *of = s->private;
3815 struct cgroup_pidlist *l = of->priv; 3370 struct cgroup_pidlist *l = of->priv;
3816 pid_t *p = v; 3371 pid_t *p = v;
3817 pid_t *end = l->list + l->length; 3372 pid_t *end = l->list + l->length;
@@ -3861,23 +3416,6 @@ static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3861 return 0; 3416 return 0;
3862} 3417}
3863 3418
3864/*
3865 * When dput() is called asynchronously, if umount has been done and
3866 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3867 * there's a small window that vfs will see the root dentry with non-zero
3868 * refcnt and trigger BUG().
3869 *
3870 * That's why we hold a reference before dput() and drop it right after.
3871 */
3872static void cgroup_dput(struct cgroup *cgrp)
3873{
3874 struct super_block *sb = cgrp->root->sb;
3875
3876 atomic_inc(&sb->s_active);
3877 dput(cgrp->dentry);
3878 deactivate_super(sb);
3879}
3880
3881static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, 3419static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3882 struct cftype *cft) 3420 struct cftype *cft)
3883{ 3421{
@@ -3944,7 +3482,7 @@ static struct cftype cgroup_base_files[] = {
3944 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, 3482 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
3945 .seq_show = cgroup_release_agent_show, 3483 .seq_show = cgroup_release_agent_show,
3946 .write_string = cgroup_release_agent_write, 3484 .write_string = cgroup_release_agent_write,
3947 .max_write_len = PATH_MAX, 3485 .max_write_len = PATH_MAX - 1,
3948 }, 3486 },
3949 { } /* terminate */ 3487 { } /* terminate */
3950}; 3488};
@@ -3963,13 +3501,13 @@ static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
3963 3501
3964 /* process cftsets of each subsystem */ 3502 /* process cftsets of each subsystem */
3965 for_each_subsys(ss, i) { 3503 for_each_subsys(ss, i) {
3966 struct cftype_set *set; 3504 struct cftype *cfts;
3967 3505
3968 if (!test_bit(i, &subsys_mask)) 3506 if (!test_bit(i, &subsys_mask))
3969 continue; 3507 continue;
3970 3508
3971 list_for_each_entry(set, &ss->cftsets, node) { 3509 list_for_each_entry(cfts, &ss->cfts, node) {
3972 ret = cgroup_addrm_files(cgrp, set->cfts, true); 3510 ret = cgroup_addrm_files(cgrp, cfts, true);
3973 if (ret < 0) 3511 if (ret < 0)
3974 goto err; 3512 goto err;
3975 } 3513 }
@@ -4012,7 +3550,7 @@ static void css_free_work_fn(struct work_struct *work)
4012 css_put(css->parent); 3550 css_put(css->parent);
4013 3551
4014 css->ss->css_free(css); 3552 css->ss->css_free(css);
4015 cgroup_dput(cgrp); 3553 cgroup_put(cgrp);
4016} 3554}
4017 3555
4018static void css_free_rcu_fn(struct rcu_head *rcu_head) 3556static void css_free_rcu_fn(struct rcu_head *rcu_head)
@@ -4020,10 +3558,6 @@ static void css_free_rcu_fn(struct rcu_head *rcu_head)
4020 struct cgroup_subsys_state *css = 3558 struct cgroup_subsys_state *css =
4021 container_of(rcu_head, struct cgroup_subsys_state, rcu_head); 3559 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4022 3560
4023 /*
4024 * css holds an extra ref to @cgrp->dentry which is put on the last
4025 * css_put(). dput() requires process context which we don't have.
4026 */
4027 INIT_WORK(&css->destroy_work, css_free_work_fn); 3561 INIT_WORK(&css->destroy_work, css_free_work_fn);
4028 queue_work(cgroup_destroy_wq, &css->destroy_work); 3562 queue_work(cgroup_destroy_wq, &css->destroy_work);
4029} 3563}
@@ -4033,7 +3567,7 @@ static void css_release(struct percpu_ref *ref)
4033 struct cgroup_subsys_state *css = 3567 struct cgroup_subsys_state *css =
4034 container_of(ref, struct cgroup_subsys_state, refcnt); 3568 container_of(ref, struct cgroup_subsys_state, refcnt);
4035 3569
4036 rcu_assign_pointer(css->cgroup->subsys[css->ss->subsys_id], NULL); 3570 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
4037 call_rcu(&css->rcu_head, css_free_rcu_fn); 3571 call_rcu(&css->rcu_head, css_free_rcu_fn);
4038} 3572}
4039 3573
@@ -4058,6 +3592,7 @@ static int online_css(struct cgroup_subsys_state *css)
4058 struct cgroup_subsys *ss = css->ss; 3592 struct cgroup_subsys *ss = css->ss;
4059 int ret = 0; 3593 int ret = 0;
4060 3594
3595 lockdep_assert_held(&cgroup_tree_mutex);
4061 lockdep_assert_held(&cgroup_mutex); 3596 lockdep_assert_held(&cgroup_mutex);
4062 3597
4063 if (ss->css_online) 3598 if (ss->css_online)
@@ -4065,7 +3600,7 @@ static int online_css(struct cgroup_subsys_state *css)
4065 if (!ret) { 3600 if (!ret) {
4066 css->flags |= CSS_ONLINE; 3601 css->flags |= CSS_ONLINE;
4067 css->cgroup->nr_css++; 3602 css->cgroup->nr_css++;
4068 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css); 3603 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
4069 } 3604 }
4070 return ret; 3605 return ret;
4071} 3606}
@@ -4075,6 +3610,7 @@ static void offline_css(struct cgroup_subsys_state *css)
4075{ 3610{
4076 struct cgroup_subsys *ss = css->ss; 3611 struct cgroup_subsys *ss = css->ss;
4077 3612
3613 lockdep_assert_held(&cgroup_tree_mutex);
4078 lockdep_assert_held(&cgroup_mutex); 3614 lockdep_assert_held(&cgroup_mutex);
4079 3615
4080 if (!(css->flags & CSS_ONLINE)) 3616 if (!(css->flags & CSS_ONLINE))
@@ -4085,7 +3621,7 @@ static void offline_css(struct cgroup_subsys_state *css)
4085 3621
4086 css->flags &= ~CSS_ONLINE; 3622 css->flags &= ~CSS_ONLINE;
4087 css->cgroup->nr_css--; 3623 css->cgroup->nr_css--;
4088 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css); 3624 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
4089} 3625}
4090 3626
4091/** 3627/**
@@ -4103,7 +3639,6 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4103 struct cgroup_subsys_state *css; 3639 struct cgroup_subsys_state *css;
4104 int err; 3640 int err;
4105 3641
4106 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4107 lockdep_assert_held(&cgroup_mutex); 3642 lockdep_assert_held(&cgroup_mutex);
4108 3643
4109 css = ss->css_alloc(cgroup_css(parent, ss)); 3644 css = ss->css_alloc(cgroup_css(parent, ss));
@@ -4112,21 +3647,23 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4112 3647
4113 err = percpu_ref_init(&css->refcnt, css_release); 3648 err = percpu_ref_init(&css->refcnt, css_release);
4114 if (err) 3649 if (err)
4115 goto err_free; 3650 goto err_free_css;
4116 3651
4117 init_css(css, ss, cgrp); 3652 init_css(css, ss, cgrp);
4118 3653
4119 err = cgroup_populate_dir(cgrp, 1 << ss->subsys_id); 3654 err = cgroup_populate_dir(cgrp, 1 << ss->id);
4120 if (err) 3655 if (err)
4121 goto err_free; 3656 goto err_free_percpu_ref;
4122 3657
4123 err = online_css(css); 3658 err = online_css(css);
4124 if (err) 3659 if (err)
4125 goto err_free; 3660 goto err_clear_dir;
4126 3661
4127 dget(cgrp->dentry); 3662 cgroup_get(cgrp);
4128 css_get(css->parent); 3663 css_get(css->parent);
4129 3664
3665 cgrp->subsys_mask |= 1 << ss->id;
3666
4130 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && 3667 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4131 parent->parent) { 3668 parent->parent) {
4132 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", 3669 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",
@@ -4138,41 +3675,43 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4138 3675
4139 return 0; 3676 return 0;
4140 3677
4141err_free: 3678err_clear_dir:
3679 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
3680err_free_percpu_ref:
4142 percpu_ref_cancel_init(&css->refcnt); 3681 percpu_ref_cancel_init(&css->refcnt);
3682err_free_css:
4143 ss->css_free(css); 3683 ss->css_free(css);
4144 return err; 3684 return err;
4145} 3685}
4146 3686
4147/* 3687/**
4148 * cgroup_create - create a cgroup 3688 * cgroup_create - create a cgroup
4149 * @parent: cgroup that will be parent of the new cgroup 3689 * @parent: cgroup that will be parent of the new cgroup
4150 * @dentry: dentry of the new cgroup 3690 * @name: name of the new cgroup
4151 * @mode: mode to set on new inode 3691 * @mode: mode to set on new cgroup
4152 *
4153 * Must be called with the mutex on the parent inode held
4154 */ 3692 */
4155static long cgroup_create(struct cgroup *parent, struct dentry *dentry, 3693static long cgroup_create(struct cgroup *parent, const char *name,
4156 umode_t mode) 3694 umode_t mode)
4157{ 3695{
4158 struct cgroup *cgrp; 3696 struct cgroup *cgrp;
4159 struct cgroup_name *name; 3697 struct cgroup_root *root = parent->root;
4160 struct cgroupfs_root *root = parent->root;
4161 int ssid, err; 3698 int ssid, err;
4162 struct cgroup_subsys *ss; 3699 struct cgroup_subsys *ss;
4163 struct super_block *sb = root->sb; 3700 struct kernfs_node *kn;
3701
3702 /*
3703 * XXX: The default hierarchy isn't fully implemented yet. Block
3704 * !root cgroup creation on it for now.
3705 */
3706 if (root == &cgrp_dfl_root)
3707 return -EINVAL;
4164 3708
4165 /* allocate the cgroup and its ID, 0 is reserved for the root */ 3709 /* allocate the cgroup and its ID, 0 is reserved for the root */
4166 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); 3710 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4167 if (!cgrp) 3711 if (!cgrp)
4168 return -ENOMEM; 3712 return -ENOMEM;
4169 3713
4170 name = cgroup_alloc_name(dentry); 3714 mutex_lock(&cgroup_tree_mutex);
4171 if (!name) {
4172 err = -ENOMEM;
4173 goto err_free_cgrp;
4174 }
4175 rcu_assign_pointer(cgrp->name, name);
4176 3715
4177 /* 3716 /*
4178 * Only live parents can have children. Note that the liveliness 3717 * Only live parents can have children. Note that the liveliness
@@ -4183,7 +3722,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4183 */ 3722 */
4184 if (!cgroup_lock_live_group(parent)) { 3723 if (!cgroup_lock_live_group(parent)) {
4185 err = -ENODEV; 3724 err = -ENODEV;
4186 goto err_free_name; 3725 goto err_unlock_tree;
4187 } 3726 }
4188 3727
4189 /* 3728 /*
@@ -4196,18 +3735,8 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4196 goto err_unlock; 3735 goto err_unlock;
4197 } 3736 }
4198 3737
4199 /* Grab a reference on the superblock so the hierarchy doesn't
4200 * get deleted on unmount if there are child cgroups. This
4201 * can be done outside cgroup_mutex, since the sb can't
4202 * disappear while someone has an open control file on the
4203 * fs */
4204 atomic_inc(&sb->s_active);
4205
4206 init_cgroup_housekeeping(cgrp); 3738 init_cgroup_housekeeping(cgrp);
4207 3739
4208 dentry->d_fsdata = cgrp;
4209 cgrp->dentry = dentry;
4210
4211 cgrp->parent = parent; 3740 cgrp->parent = parent;
4212 cgrp->dummy_css.parent = &parent->dummy_css; 3741 cgrp->dummy_css.parent = &parent->dummy_css;
4213 cgrp->root = parent->root; 3742 cgrp->root = parent->root;
@@ -4218,24 +3747,26 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4218 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) 3747 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4219 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); 3748 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4220 3749
3750 /* create the directory */
3751 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
3752 if (IS_ERR(kn)) {
3753 err = PTR_ERR(kn);
3754 goto err_free_id;
3755 }
3756 cgrp->kn = kn;
3757
4221 /* 3758 /*
4222 * Create directory. cgroup_create_file() returns with the new 3759 * This extra ref will be put in cgroup_free_fn() and guarantees
4223 * directory locked on success so that it can be populated without 3760 * that @cgrp->kn is always accessible.
4224 * dropping cgroup_mutex.
4225 */ 3761 */
4226 err = cgroup_create_file(dentry, S_IFDIR | mode, sb); 3762 kernfs_get(kn);
4227 if (err < 0)
4228 goto err_free_id;
4229 lockdep_assert_held(&dentry->d_inode->i_mutex);
4230 3763
4231 cgrp->serial_nr = cgroup_serial_nr_next++; 3764 cgrp->serial_nr = cgroup_serial_nr_next++;
4232 3765
4233 /* allocation complete, commit to creation */ 3766 /* allocation complete, commit to creation */
4234 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); 3767 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4235 root->number_of_cgroups++; 3768 atomic_inc(&root->nr_cgrps);
4236 3769 cgroup_get(parent);
4237 /* hold a ref to the parent's dentry */
4238 dget(parent->dentry);
4239 3770
4240 /* 3771 /*
4241 * @cgrp is now fully operational. If something fails after this 3772 * @cgrp is now fully operational. If something fails after this
@@ -4243,49 +3774,66 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4243 */ 3774 */
4244 idr_replace(&root->cgroup_idr, cgrp, cgrp->id); 3775 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4245 3776
3777 err = cgroup_kn_set_ugid(kn);
3778 if (err)
3779 goto err_destroy;
3780
4246 err = cgroup_addrm_files(cgrp, cgroup_base_files, true); 3781 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4247 if (err) 3782 if (err)
4248 goto err_destroy; 3783 goto err_destroy;
4249 3784
4250 /* let's create and online css's */ 3785 /* let's create and online css's */
4251 for_each_subsys(ss, ssid) { 3786 for_each_subsys(ss, ssid) {
4252 if (root->subsys_mask & (1 << ssid)) { 3787 if (root->cgrp.subsys_mask & (1 << ssid)) {
4253 err = create_css(cgrp, ss); 3788 err = create_css(cgrp, ss);
4254 if (err) 3789 if (err)
4255 goto err_destroy; 3790 goto err_destroy;
4256 } 3791 }
4257 } 3792 }
4258 3793
3794 kernfs_activate(kn);
3795
4259 mutex_unlock(&cgroup_mutex); 3796 mutex_unlock(&cgroup_mutex);
4260 mutex_unlock(&cgrp->dentry->d_inode->i_mutex); 3797 mutex_unlock(&cgroup_tree_mutex);
4261 3798
4262 return 0; 3799 return 0;
4263 3800
4264err_free_id: 3801err_free_id:
4265 idr_remove(&root->cgroup_idr, cgrp->id); 3802 idr_remove(&root->cgroup_idr, cgrp->id);
4266 /* Release the reference count that we took on the superblock */
4267 deactivate_super(sb);
4268err_unlock: 3803err_unlock:
4269 mutex_unlock(&cgroup_mutex); 3804 mutex_unlock(&cgroup_mutex);
4270err_free_name: 3805err_unlock_tree:
4271 kfree(rcu_dereference_raw(cgrp->name)); 3806 mutex_unlock(&cgroup_tree_mutex);
4272err_free_cgrp:
4273 kfree(cgrp); 3807 kfree(cgrp);
4274 return err; 3808 return err;
4275 3809
4276err_destroy: 3810err_destroy:
4277 cgroup_destroy_locked(cgrp); 3811 cgroup_destroy_locked(cgrp);
4278 mutex_unlock(&cgroup_mutex); 3812 mutex_unlock(&cgroup_mutex);
4279 mutex_unlock(&dentry->d_inode->i_mutex); 3813 mutex_unlock(&cgroup_tree_mutex);
4280 return err; 3814 return err;
4281} 3815}
4282 3816
4283static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 3817static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3818 umode_t mode)
4284{ 3819{
4285 struct cgroup *c_parent = dentry->d_parent->d_fsdata; 3820 struct cgroup *parent = parent_kn->priv;
3821 int ret;
3822
3823 /*
3824 * cgroup_create() grabs cgroup_tree_mutex which nests outside
3825 * kernfs active_ref and cgroup_create() already synchronizes
3826 * properly against removal through cgroup_lock_live_group().
3827 * Break it before calling cgroup_create().
3828 */
3829 cgroup_get(parent);
3830 kernfs_break_active_protection(parent_kn);
3831
3832 ret = cgroup_create(parent, name, mode);
4286 3833
4287 /* the vfs holds inode->i_mutex already */ 3834 kernfs_unbreak_active_protection(parent_kn);
4288 return cgroup_create(c_parent, dentry, mode | S_IFDIR); 3835 cgroup_put(parent);
3836 return ret;
4289} 3837}
4290 3838
4291/* 3839/*
@@ -4298,6 +3846,7 @@ static void css_killed_work_fn(struct work_struct *work)
4298 container_of(work, struct cgroup_subsys_state, destroy_work); 3846 container_of(work, struct cgroup_subsys_state, destroy_work);
4299 struct cgroup *cgrp = css->cgroup; 3847 struct cgroup *cgrp = css->cgroup;
4300 3848
3849 mutex_lock(&cgroup_tree_mutex);
4301 mutex_lock(&cgroup_mutex); 3850 mutex_lock(&cgroup_mutex);
4302 3851
4303 /* 3852 /*
@@ -4315,6 +3864,7 @@ static void css_killed_work_fn(struct work_struct *work)
4315 cgroup_destroy_css_killed(cgrp); 3864 cgroup_destroy_css_killed(cgrp);
4316 3865
4317 mutex_unlock(&cgroup_mutex); 3866 mutex_unlock(&cgroup_mutex);
3867 mutex_unlock(&cgroup_tree_mutex);
4318 3868
4319 /* 3869 /*
4320 * Put the css refs from kill_css(). Each css holds an extra 3870 * Put the css refs from kill_css(). Each css holds an extra
@@ -4336,18 +3886,15 @@ static void css_killed_ref_fn(struct percpu_ref *ref)
4336 queue_work(cgroup_destroy_wq, &css->destroy_work); 3886 queue_work(cgroup_destroy_wq, &css->destroy_work);
4337} 3887}
4338 3888
4339/** 3889static void __kill_css(struct cgroup_subsys_state *css)
4340 * kill_css - destroy a css
4341 * @css: css to destroy
4342 *
4343 * This function initiates destruction of @css by removing cgroup interface
4344 * files and putting its base reference. ->css_offline() will be invoked
4345 * asynchronously once css_tryget() is guaranteed to fail and when the
4346 * reference count reaches zero, @css will be released.
4347 */
4348static void kill_css(struct cgroup_subsys_state *css)
4349{ 3890{
4350 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id); 3891 lockdep_assert_held(&cgroup_tree_mutex);
3892
3893 /*
3894 * This must happen before css is disassociated with its cgroup.
3895 * See seq_css() for details.
3896 */
3897 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
4351 3898
4352 /* 3899 /*
4353 * Killing would put the base ref, but we need to keep it alive 3900 * Killing would put the base ref, but we need to keep it alive
@@ -4369,6 +3916,28 @@ static void kill_css(struct cgroup_subsys_state *css)
4369} 3916}
4370 3917
4371/** 3918/**
3919 * kill_css - destroy a css
3920 * @css: css to destroy
3921 *
3922 * This function initiates destruction of @css by removing cgroup interface
3923 * files and putting its base reference. ->css_offline() will be invoked
3924 * asynchronously once css_tryget() is guaranteed to fail and when the
3925 * reference count reaches zero, @css will be released.
3926 */
3927static void kill_css(struct cgroup_subsys_state *css)
3928{
3929 struct cgroup *cgrp = css->cgroup;
3930
3931 lockdep_assert_held(&cgroup_tree_mutex);
3932
3933 /* if already killed, noop */
3934 if (cgrp->subsys_mask & (1 << css->ss->id)) {
3935 cgrp->subsys_mask &= ~(1 << css->ss->id);
3936 __kill_css(css);
3937 }
3938}
3939
3940/**
4372 * cgroup_destroy_locked - the first stage of cgroup destruction 3941 * cgroup_destroy_locked - the first stage of cgroup destruction
4373 * @cgrp: cgroup to be destroyed 3942 * @cgrp: cgroup to be destroyed
4374 * 3943 *
@@ -4395,22 +3964,21 @@ static void kill_css(struct cgroup_subsys_state *css)
4395static int cgroup_destroy_locked(struct cgroup *cgrp) 3964static int cgroup_destroy_locked(struct cgroup *cgrp)
4396 __releases(&cgroup_mutex) __acquires(&cgroup_mutex) 3965 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4397{ 3966{
4398 struct dentry *d = cgrp->dentry;
4399 struct cgroup_subsys_state *css;
4400 struct cgroup *child; 3967 struct cgroup *child;
3968 struct cgroup_subsys_state *css;
4401 bool empty; 3969 bool empty;
4402 int ssid; 3970 int ssid;
4403 3971
4404 lockdep_assert_held(&d->d_inode->i_mutex); 3972 lockdep_assert_held(&cgroup_tree_mutex);
4405 lockdep_assert_held(&cgroup_mutex); 3973 lockdep_assert_held(&cgroup_mutex);
4406 3974
4407 /* 3975 /*
4408 * css_set_lock synchronizes access to ->cset_links and prevents 3976 * css_set_rwsem synchronizes access to ->cset_links and prevents
4409 * @cgrp from being removed while __put_css_set() is in progress. 3977 * @cgrp from being removed while put_css_set() is in progress.
4410 */ 3978 */
4411 read_lock(&css_set_lock); 3979 down_read(&css_set_rwsem);
4412 empty = list_empty(&cgrp->cset_links); 3980 empty = list_empty(&cgrp->cset_links);
4413 read_unlock(&css_set_lock); 3981 up_read(&css_set_rwsem);
4414 if (!empty) 3982 if (!empty)
4415 return -EBUSY; 3983 return -EBUSY;
4416 3984
@@ -4431,14 +3999,6 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
4431 return -EBUSY; 3999 return -EBUSY;
4432 4000
4433 /* 4001 /*
4434 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4435 * will be invoked to perform the rest of destruction once the
4436 * percpu refs of all css's are confirmed to be killed.
4437 */
4438 for_each_css(css, ssid, cgrp)
4439 kill_css(css);
4440
4441 /*
4442 * Mark @cgrp dead. This prevents further task migration and child 4002 * Mark @cgrp dead. This prevents further task migration and child
4443 * creation by disabling cgroup_lock_live_group(). Note that 4003 * creation by disabling cgroup_lock_live_group(). Note that
4444 * CGRP_DEAD assertion is depended upon by css_next_child() to 4004 * CGRP_DEAD assertion is depended upon by css_next_child() to
@@ -4447,6 +4007,17 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
4447 */ 4007 */
4448 set_bit(CGRP_DEAD, &cgrp->flags); 4008 set_bit(CGRP_DEAD, &cgrp->flags);
4449 4009
4010 /*
4011 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4012 * will be invoked to perform the rest of destruction once the
4013 * percpu refs of all css's are confirmed to be killed. This
4014 * involves removing the subsystem's files, drop cgroup_mutex.
4015 */
4016 mutex_unlock(&cgroup_mutex);
4017 for_each_css(css, ssid, cgrp)
4018 kill_css(css);
4019 mutex_lock(&cgroup_mutex);
4020
4450 /* CGRP_DEAD is set, remove from ->release_list for the last time */ 4021 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4451 raw_spin_lock(&release_list_lock); 4022 raw_spin_lock(&release_list_lock);
4452 if (!list_empty(&cgrp->release_list)) 4023 if (!list_empty(&cgrp->release_list))
@@ -4462,14 +4033,20 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
4462 if (!cgrp->nr_css) 4033 if (!cgrp->nr_css)
4463 cgroup_destroy_css_killed(cgrp); 4034 cgroup_destroy_css_killed(cgrp);
4464 4035
4036 /* remove @cgrp directory along with the base files */
4037 mutex_unlock(&cgroup_mutex);
4038
4465 /* 4039 /*
4466 * Clear the base files and remove @cgrp directory. The removal 4040 * There are two control paths which try to determine cgroup from
4467 * puts the base ref but we aren't quite done with @cgrp yet, so 4041 * dentry without going through kernfs - cgroupstats_build() and
4468 * hold onto it. 4042 * css_tryget_from_dir(). Those are supported by RCU protecting
4043 * clearing of cgrp->kn->priv backpointer, which should happen
4044 * after all files under it have been removed.
4469 */ 4045 */
4470 cgroup_addrm_files(cgrp, cgroup_base_files, false); 4046 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
4471 dget(d); 4047 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
4472 cgroup_d_remove_dir(d); 4048
4049 mutex_lock(&cgroup_mutex);
4473 4050
4474 return 0; 4051 return 0;
4475}; 4052};
@@ -4486,72 +4063,82 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
4486static void cgroup_destroy_css_killed(struct cgroup *cgrp) 4063static void cgroup_destroy_css_killed(struct cgroup *cgrp)
4487{ 4064{
4488 struct cgroup *parent = cgrp->parent; 4065 struct cgroup *parent = cgrp->parent;
4489 struct dentry *d = cgrp->dentry;
4490 4066
4067 lockdep_assert_held(&cgroup_tree_mutex);
4491 lockdep_assert_held(&cgroup_mutex); 4068 lockdep_assert_held(&cgroup_mutex);
4492 4069
4493 /* delete this cgroup from parent->children */ 4070 /* delete this cgroup from parent->children */
4494 list_del_rcu(&cgrp->sibling); 4071 list_del_rcu(&cgrp->sibling);
4495 4072
4496 dput(d); 4073 cgroup_put(cgrp);
4497 4074
4498 set_bit(CGRP_RELEASABLE, &parent->flags); 4075 set_bit(CGRP_RELEASABLE, &parent->flags);
4499 check_for_release(parent); 4076 check_for_release(parent);
4500} 4077}
4501 4078
4502static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) 4079static int cgroup_rmdir(struct kernfs_node *kn)
4503{ 4080{
4504 int ret; 4081 struct cgroup *cgrp = kn->priv;
4505 4082 int ret = 0;
4506 mutex_lock(&cgroup_mutex);
4507 ret = cgroup_destroy_locked(dentry->d_fsdata);
4508 mutex_unlock(&cgroup_mutex);
4509 4083
4510 return ret; 4084 /*
4511} 4085 * This is self-destruction but @kn can't be removed while this
4086 * callback is in progress. Let's break active protection. Once
4087 * the protection is broken, @cgrp can be destroyed at any point.
4088 * Pin it so that it stays accessible.
4089 */
4090 cgroup_get(cgrp);
4091 kernfs_break_active_protection(kn);
4512 4092
4513static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) 4093 mutex_lock(&cgroup_tree_mutex);
4514{ 4094 mutex_lock(&cgroup_mutex);
4515 INIT_LIST_HEAD(&ss->cftsets);
4516 4095
4517 /* 4096 /*
4518 * base_cftset is embedded in subsys itself, no need to worry about 4097 * @cgrp might already have been destroyed while we're trying to
4519 * deregistration. 4098 * grab the mutexes.
4520 */ 4099 */
4521 if (ss->base_cftypes) { 4100 if (!cgroup_is_dead(cgrp))
4522 struct cftype *cft; 4101 ret = cgroup_destroy_locked(cgrp);
4523 4102
4524 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) 4103 mutex_unlock(&cgroup_mutex);
4525 cft->ss = ss; 4104 mutex_unlock(&cgroup_tree_mutex);
4526 4105
4527 ss->base_cftset.cfts = ss->base_cftypes; 4106 kernfs_unbreak_active_protection(kn);
4528 list_add_tail(&ss->base_cftset.node, &ss->cftsets); 4107 cgroup_put(cgrp);
4529 } 4108 return ret;
4530} 4109}
4531 4110
4111static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4112 .remount_fs = cgroup_remount,
4113 .show_options = cgroup_show_options,
4114 .mkdir = cgroup_mkdir,
4115 .rmdir = cgroup_rmdir,
4116 .rename = cgroup_rename,
4117};
4118
4532static void __init cgroup_init_subsys(struct cgroup_subsys *ss) 4119static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
4533{ 4120{
4534 struct cgroup_subsys_state *css; 4121 struct cgroup_subsys_state *css;
4535 4122
4536 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); 4123 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
4537 4124
4125 mutex_lock(&cgroup_tree_mutex);
4538 mutex_lock(&cgroup_mutex); 4126 mutex_lock(&cgroup_mutex);
4539 4127
4540 /* init base cftset */ 4128 INIT_LIST_HEAD(&ss->cfts);
4541 cgroup_init_cftsets(ss);
4542 4129
4543 /* Create the top cgroup state for this subsystem */ 4130 /* Create the root cgroup state for this subsystem */
4544 ss->root = &cgroup_dummy_root; 4131 ss->root = &cgrp_dfl_root;
4545 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); 4132 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
4546 /* We don't handle early failures gracefully */ 4133 /* We don't handle early failures gracefully */
4547 BUG_ON(IS_ERR(css)); 4134 BUG_ON(IS_ERR(css));
4548 init_css(css, ss, cgroup_dummy_top); 4135 init_css(css, ss, &cgrp_dfl_root.cgrp);
4549 4136
4550 /* Update the init_css_set to contain a subsys 4137 /* Update the init_css_set to contain a subsys
4551 * pointer to this state - since the subsystem is 4138 * pointer to this state - since the subsystem is
4552 * newly registered, all tasks and hence the 4139 * newly registered, all tasks and hence the
4553 * init_css_set is in the subsystem's top cgroup. */ 4140 * init_css_set is in the subsystem's root cgroup. */
4554 init_css_set.subsys[ss->subsys_id] = css; 4141 init_css_set.subsys[ss->id] = css;
4555 4142
4556 need_forkexit_callback |= ss->fork || ss->exit; 4143 need_forkexit_callback |= ss->fork || ss->exit;
4557 4144
@@ -4562,185 +4149,11 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
4562 4149
4563 BUG_ON(online_css(css)); 4150 BUG_ON(online_css(css));
4564 4151
4565 mutex_unlock(&cgroup_mutex); 4152 cgrp_dfl_root.cgrp.subsys_mask |= 1 << ss->id;
4566
4567 /* this function shouldn't be used with modular subsystems, since they
4568 * need to register a subsys_id, among other things */
4569 BUG_ON(ss->module);
4570}
4571
4572/**
4573 * cgroup_load_subsys: load and register a modular subsystem at runtime
4574 * @ss: the subsystem to load
4575 *
4576 * This function should be called in a modular subsystem's initcall. If the
4577 * subsystem is built as a module, it will be assigned a new subsys_id and set
4578 * up for use. If the subsystem is built-in anyway, work is delegated to the
4579 * simpler cgroup_init_subsys.
4580 */
4581int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4582{
4583 struct cgroup_subsys_state *css;
4584 int i, ret;
4585 struct hlist_node *tmp;
4586 struct css_set *cset;
4587 unsigned long key;
4588
4589 /* check name and function validity */
4590 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4591 ss->css_alloc == NULL || ss->css_free == NULL)
4592 return -EINVAL;
4593
4594 /*
4595 * we don't support callbacks in modular subsystems. this check is
4596 * before the ss->module check for consistency; a subsystem that could
4597 * be a module should still have no callbacks even if the user isn't
4598 * compiling it as one.
4599 */
4600 if (ss->fork || ss->exit)
4601 return -EINVAL;
4602
4603 /*
4604 * an optionally modular subsystem is built-in: we want to do nothing,
4605 * since cgroup_init_subsys will have already taken care of it.
4606 */
4607 if (ss->module == NULL) {
4608 /* a sanity check */
4609 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
4610 return 0;
4611 }
4612
4613 /* init base cftset */
4614 cgroup_init_cftsets(ss);
4615
4616 mutex_lock(&cgroup_mutex);
4617 mutex_lock(&cgroup_root_mutex);
4618 cgroup_subsys[ss->subsys_id] = ss;
4619
4620 /*
4621 * no ss->css_alloc seems to need anything important in the ss
4622 * struct, so this can happen first (i.e. before the dummy root
4623 * attachment).
4624 */
4625 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
4626 if (IS_ERR(css)) {
4627 /* failure case - need to deassign the cgroup_subsys[] slot. */
4628 cgroup_subsys[ss->subsys_id] = NULL;
4629 mutex_unlock(&cgroup_root_mutex);
4630 mutex_unlock(&cgroup_mutex);
4631 return PTR_ERR(css);
4632 }
4633
4634 ss->root = &cgroup_dummy_root;
4635
4636 /* our new subsystem will be attached to the dummy hierarchy. */
4637 init_css(css, ss, cgroup_dummy_top);
4638
4639 /*
4640 * Now we need to entangle the css into the existing css_sets. unlike
4641 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4642 * will need a new pointer to it; done by iterating the css_set_table.
4643 * furthermore, modifying the existing css_sets will corrupt the hash
4644 * table state, so each changed css_set will need its hash recomputed.
4645 * this is all done under the css_set_lock.
4646 */
4647 write_lock(&css_set_lock);
4648 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
4649 /* skip entries that we already rehashed */
4650 if (cset->subsys[ss->subsys_id])
4651 continue;
4652 /* remove existing entry */
4653 hash_del(&cset->hlist);
4654 /* set new value */
4655 cset->subsys[ss->subsys_id] = css;
4656 /* recompute hash and restore entry */
4657 key = css_set_hash(cset->subsys);
4658 hash_add(css_set_table, &cset->hlist, key);
4659 }
4660 write_unlock(&css_set_lock);
4661
4662 ret = online_css(css);
4663 if (ret) {
4664 ss->css_free(css);
4665 goto err_unload;
4666 }
4667
4668 /* success! */
4669 mutex_unlock(&cgroup_root_mutex);
4670 mutex_unlock(&cgroup_mutex);
4671 return 0;
4672
4673err_unload:
4674 mutex_unlock(&cgroup_root_mutex);
4675 mutex_unlock(&cgroup_mutex);
4676 /* @ss can't be mounted here as try_module_get() would fail */
4677 cgroup_unload_subsys(ss);
4678 return ret;
4679}
4680EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4681
4682/**
4683 * cgroup_unload_subsys: unload a modular subsystem
4684 * @ss: the subsystem to unload
4685 *
4686 * This function should be called in a modular subsystem's exitcall. When this
4687 * function is invoked, the refcount on the subsystem's module will be 0, so
4688 * the subsystem will not be attached to any hierarchy.
4689 */
4690void cgroup_unload_subsys(struct cgroup_subsys *ss)
4691{
4692 struct cgrp_cset_link *link;
4693 struct cgroup_subsys_state *css;
4694
4695 BUG_ON(ss->module == NULL);
4696
4697 /*
4698 * we shouldn't be called if the subsystem is in use, and the use of
4699 * try_module_get() in rebind_subsystems() should ensure that it
4700 * doesn't start being used while we're killing it off.
4701 */
4702 BUG_ON(ss->root != &cgroup_dummy_root);
4703
4704 mutex_lock(&cgroup_mutex);
4705 mutex_lock(&cgroup_root_mutex);
4706
4707 css = cgroup_css(cgroup_dummy_top, ss);
4708 if (css)
4709 offline_css(css);
4710 4153
4711 /* deassign the subsys_id */
4712 cgroup_subsys[ss->subsys_id] = NULL;
4713
4714 /*
4715 * disentangle the css from all css_sets attached to the dummy
4716 * top. as in loading, we need to pay our respects to the hashtable
4717 * gods.
4718 */
4719 write_lock(&css_set_lock);
4720 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
4721 struct css_set *cset = link->cset;
4722 unsigned long key;
4723
4724 hash_del(&cset->hlist);
4725 cset->subsys[ss->subsys_id] = NULL;
4726 key = css_set_hash(cset->subsys);
4727 hash_add(css_set_table, &cset->hlist, key);
4728 }
4729 write_unlock(&css_set_lock);
4730
4731 /*
4732 * remove subsystem's css from the cgroup_dummy_top and free it -
4733 * need to free before marking as null because ss->css_free needs
4734 * the cgrp->subsys pointer to find their state.
4735 */
4736 if (css)
4737 ss->css_free(css);
4738 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
4739
4740 mutex_unlock(&cgroup_root_mutex);
4741 mutex_unlock(&cgroup_mutex); 4154 mutex_unlock(&cgroup_mutex);
4155 mutex_unlock(&cgroup_tree_mutex);
4742} 4156}
4743EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4744 4157
4745/** 4158/**
4746 * cgroup_init_early - cgroup initialization at system boot 4159 * cgroup_init_early - cgroup initialization at system boot
@@ -4750,34 +4163,24 @@ EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4750 */ 4163 */
4751int __init cgroup_init_early(void) 4164int __init cgroup_init_early(void)
4752{ 4165{
4166 static struct cgroup_sb_opts __initdata opts =
4167 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
4753 struct cgroup_subsys *ss; 4168 struct cgroup_subsys *ss;
4754 int i; 4169 int i;
4755 4170
4756 atomic_set(&init_css_set.refcount, 1); 4171 init_cgroup_root(&cgrp_dfl_root, &opts);
4757 INIT_LIST_HEAD(&init_css_set.cgrp_links);
4758 INIT_LIST_HEAD(&init_css_set.tasks);
4759 INIT_HLIST_NODE(&init_css_set.hlist);
4760 css_set_count = 1;
4761 init_cgroup_root(&cgroup_dummy_root);
4762 cgroup_root_count = 1;
4763 RCU_INIT_POINTER(init_task.cgroups, &init_css_set); 4172 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
4764 4173
4765 init_cgrp_cset_link.cset = &init_css_set; 4174 for_each_subsys(ss, i) {
4766 init_cgrp_cset_link.cgrp = cgroup_dummy_top; 4175 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
4767 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links); 4176 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4768 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links); 4177 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
4769 4178 ss->id, ss->name);
4770 /* at bootup time, we don't worry about modular subsystems */ 4179 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4771 for_each_builtin_subsys(ss, i) { 4180 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4772 BUG_ON(!ss->name); 4181
4773 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); 4182 ss->id = i;
4774 BUG_ON(!ss->css_alloc); 4183 ss->name = cgroup_subsys_name[i];
4775 BUG_ON(!ss->css_free);
4776 if (ss->subsys_id != i) {
4777 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
4778 ss->name, ss->subsys_id);
4779 BUG();
4780 }
4781 4184
4782 if (ss->early_init) 4185 if (ss->early_init)
4783 cgroup_init_subsys(ss); 4186 cgroup_init_subsys(ss);
@@ -4795,53 +4198,46 @@ int __init cgroup_init(void)
4795{ 4198{
4796 struct cgroup_subsys *ss; 4199 struct cgroup_subsys *ss;
4797 unsigned long key; 4200 unsigned long key;
4798 int i, err; 4201 int ssid, err;
4799 4202
4800 err = bdi_init(&cgroup_backing_dev_info); 4203 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
4801 if (err)
4802 return err;
4803 4204
4804 for_each_builtin_subsys(ss, i) { 4205 mutex_lock(&cgroup_tree_mutex);
4805 if (!ss->early_init)
4806 cgroup_init_subsys(ss);
4807 }
4808
4809 /* allocate id for the dummy hierarchy */
4810 mutex_lock(&cgroup_mutex); 4206 mutex_lock(&cgroup_mutex);
4811 mutex_lock(&cgroup_root_mutex);
4812 4207
4813 /* Add init_css_set to the hash table */ 4208 /* Add init_css_set to the hash table */
4814 key = css_set_hash(init_css_set.subsys); 4209 key = css_set_hash(init_css_set.subsys);
4815 hash_add(css_set_table, &init_css_set.hlist, key); 4210 hash_add(css_set_table, &init_css_set.hlist, key);
4816 4211
4817 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); 4212 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
4818 4213
4819 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4820 0, 1, GFP_KERNEL);
4821 BUG_ON(err < 0);
4822
4823 mutex_unlock(&cgroup_root_mutex);
4824 mutex_unlock(&cgroup_mutex); 4214 mutex_unlock(&cgroup_mutex);
4215 mutex_unlock(&cgroup_tree_mutex);
4825 4216
4826 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); 4217 for_each_subsys(ss, ssid) {
4827 if (!cgroup_kobj) { 4218 if (!ss->early_init)
4828 err = -ENOMEM; 4219 cgroup_init_subsys(ss);
4829 goto out; 4220
4221 /*
4222 * cftype registration needs kmalloc and can't be done
4223 * during early_init. Register base cftypes separately.
4224 */
4225 if (ss->base_cftypes)
4226 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4830 } 4227 }
4831 4228
4229 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4230 if (!cgroup_kobj)
4231 return -ENOMEM;
4232
4832 err = register_filesystem(&cgroup_fs_type); 4233 err = register_filesystem(&cgroup_fs_type);
4833 if (err < 0) { 4234 if (err < 0) {
4834 kobject_put(cgroup_kobj); 4235 kobject_put(cgroup_kobj);
4835 goto out; 4236 return err;
4836 } 4237 }
4837 4238
4838 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); 4239 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
4839 4240 return 0;
4840out:
4841 if (err)
4842 bdi_destroy(&cgroup_backing_dev_info);
4843
4844 return err;
4845} 4241}
4846 4242
4847static int __init cgroup_wq_init(void) 4243static int __init cgroup_wq_init(void)
@@ -4873,12 +4269,6 @@ core_initcall(cgroup_wq_init);
4873 * proc_cgroup_show() 4269 * proc_cgroup_show()
4874 * - Print task's cgroup paths into seq_file, one line for each hierarchy 4270 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4875 * - Used for /proc/<pid>/cgroup. 4271 * - Used for /proc/<pid>/cgroup.
4876 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4877 * doesn't really matter if tsk->cgroup changes after we read it,
4878 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4879 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4880 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4881 * cgroup to top_cgroup.
4882 */ 4272 */
4883 4273
4884/* TODO: Use a proper seq_file iterator */ 4274/* TODO: Use a proper seq_file iterator */
@@ -4886,12 +4276,12 @@ int proc_cgroup_show(struct seq_file *m, void *v)
4886{ 4276{
4887 struct pid *pid; 4277 struct pid *pid;
4888 struct task_struct *tsk; 4278 struct task_struct *tsk;
4889 char *buf; 4279 char *buf, *path;
4890 int retval; 4280 int retval;
4891 struct cgroupfs_root *root; 4281 struct cgroup_root *root;
4892 4282
4893 retval = -ENOMEM; 4283 retval = -ENOMEM;
4894 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4284 buf = kmalloc(PATH_MAX, GFP_KERNEL);
4895 if (!buf) 4285 if (!buf)
4896 goto out; 4286 goto out;
4897 4287
@@ -4904,29 +4294,36 @@ int proc_cgroup_show(struct seq_file *m, void *v)
4904 retval = 0; 4294 retval = 0;
4905 4295
4906 mutex_lock(&cgroup_mutex); 4296 mutex_lock(&cgroup_mutex);
4297 down_read(&css_set_rwsem);
4907 4298
4908 for_each_active_root(root) { 4299 for_each_root(root) {
4909 struct cgroup_subsys *ss; 4300 struct cgroup_subsys *ss;
4910 struct cgroup *cgrp; 4301 struct cgroup *cgrp;
4911 int ssid, count = 0; 4302 int ssid, count = 0;
4912 4303
4304 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
4305 continue;
4306
4913 seq_printf(m, "%d:", root->hierarchy_id); 4307 seq_printf(m, "%d:", root->hierarchy_id);
4914 for_each_subsys(ss, ssid) 4308 for_each_subsys(ss, ssid)
4915 if (root->subsys_mask & (1 << ssid)) 4309 if (root->cgrp.subsys_mask & (1 << ssid))
4916 seq_printf(m, "%s%s", count++ ? "," : "", ss->name); 4310 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
4917 if (strlen(root->name)) 4311 if (strlen(root->name))
4918 seq_printf(m, "%sname=%s", count ? "," : "", 4312 seq_printf(m, "%sname=%s", count ? "," : "",
4919 root->name); 4313 root->name);
4920 seq_putc(m, ':'); 4314 seq_putc(m, ':');
4921 cgrp = task_cgroup_from_root(tsk, root); 4315 cgrp = task_cgroup_from_root(tsk, root);
4922 retval = cgroup_path(cgrp, buf, PAGE_SIZE); 4316 path = cgroup_path(cgrp, buf, PATH_MAX);
4923 if (retval < 0) 4317 if (!path) {
4318 retval = -ENAMETOOLONG;
4924 goto out_unlock; 4319 goto out_unlock;
4925 seq_puts(m, buf); 4320 }
4321 seq_puts(m, path);
4926 seq_putc(m, '\n'); 4322 seq_putc(m, '\n');
4927 } 4323 }
4928 4324
4929out_unlock: 4325out_unlock:
4326 up_read(&css_set_rwsem);
4930 mutex_unlock(&cgroup_mutex); 4327 mutex_unlock(&cgroup_mutex);
4931 put_task_struct(tsk); 4328 put_task_struct(tsk);
4932out_free: 4329out_free:
@@ -4952,7 +4349,7 @@ static int proc_cgroupstats_show(struct seq_file *m, void *v)
4952 for_each_subsys(ss, i) 4349 for_each_subsys(ss, i)
4953 seq_printf(m, "%s\t%d\t%d\t%d\n", 4350 seq_printf(m, "%s\t%d\t%d\t%d\n",
4954 ss->name, ss->root->hierarchy_id, 4351 ss->name, ss->root->hierarchy_id,
4955 ss->root->number_of_cgroups, !ss->disabled); 4352 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
4956 4353
4957 mutex_unlock(&cgroup_mutex); 4354 mutex_unlock(&cgroup_mutex);
4958 return 0; 4355 return 0;
@@ -4971,27 +4368,16 @@ static const struct file_operations proc_cgroupstats_operations = {
4971}; 4368};
4972 4369
4973/** 4370/**
4974 * cgroup_fork - attach newly forked task to its parents cgroup. 4371 * cgroup_fork - initialize cgroup related fields during copy_process()
4975 * @child: pointer to task_struct of forking parent process. 4372 * @child: pointer to task_struct of forking parent process.
4976 * 4373 *
4977 * Description: A task inherits its parent's cgroup at fork(). 4374 * A task is associated with the init_css_set until cgroup_post_fork()
4978 * 4375 * attaches it to the parent's css_set. Empty cg_list indicates that
4979 * A pointer to the shared css_set was automatically copied in 4376 * @child isn't holding reference to its css_set.
4980 * fork.c by dup_task_struct(). However, we ignore that copy, since
4981 * it was not made under the protection of RCU or cgroup_mutex, so
4982 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4983 * have already changed current->cgroups, allowing the previously
4984 * referenced cgroup group to be removed and freed.
4985 *
4986 * At the point that cgroup_fork() is called, 'current' is the parent
4987 * task, and the passed argument 'child' points to the child task.
4988 */ 4377 */
4989void cgroup_fork(struct task_struct *child) 4378void cgroup_fork(struct task_struct *child)
4990{ 4379{
4991 task_lock(current); 4380 RCU_INIT_POINTER(child->cgroups, &init_css_set);
4992 get_css_set(task_css_set(current));
4993 child->cgroups = current->cgroups;
4994 task_unlock(current);
4995 INIT_LIST_HEAD(&child->cg_list); 4381 INIT_LIST_HEAD(&child->cg_list);
4996} 4382}
4997 4383
@@ -5011,23 +4397,37 @@ void cgroup_post_fork(struct task_struct *child)
5011 int i; 4397 int i;
5012 4398
5013 /* 4399 /*
5014 * use_task_css_set_links is set to 1 before we walk the tasklist 4400 * This may race against cgroup_enable_task_cg_links(). As that
5015 * under the tasklist_lock and we read it here after we added the child 4401 * function sets use_task_css_set_links before grabbing
5016 * to the tasklist under the tasklist_lock as well. If the child wasn't 4402 * tasklist_lock and we just went through tasklist_lock to add
5017 * yet in the tasklist when we walked through it from 4403 * @child, it's guaranteed that either we see the set
5018 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value 4404 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5019 * should be visible now due to the paired locking and barriers implied 4405 * @child during its iteration.
5020 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock 4406 *
5021 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock 4407 * If we won the race, @child is associated with %current's
5022 * lock on fork. 4408 * css_set. Grabbing css_set_rwsem guarantees both that the
4409 * association is stable, and, on completion of the parent's
4410 * migration, @child is visible in the source of migration or
4411 * already in the destination cgroup. This guarantee is necessary
4412 * when implementing operations which need to migrate all tasks of
4413 * a cgroup to another.
4414 *
4415 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4416 * will remain in init_css_set. This is safe because all tasks are
4417 * in the init_css_set before cg_links is enabled and there's no
4418 * operation which transfers all tasks out of init_css_set.
5023 */ 4419 */
5024 if (use_task_css_set_links) { 4420 if (use_task_css_set_links) {
5025 write_lock(&css_set_lock); 4421 struct css_set *cset;
5026 task_lock(child); 4422
5027 if (list_empty(&child->cg_list)) 4423 down_write(&css_set_rwsem);
5028 list_add(&child->cg_list, &task_css_set(child)->tasks); 4424 cset = task_css_set(current);
5029 task_unlock(child); 4425 if (list_empty(&child->cg_list)) {
5030 write_unlock(&css_set_lock); 4426 rcu_assign_pointer(child->cgroups, cset);
4427 list_add(&child->cg_list, &cset->tasks);
4428 get_css_set(cset);
4429 }
4430 up_write(&css_set_rwsem);
5031 } 4431 }
5032 4432
5033 /* 4433 /*
@@ -5036,15 +4436,7 @@ void cgroup_post_fork(struct task_struct *child)
5036 * and addition to css_set. 4436 * and addition to css_set.
5037 */ 4437 */
5038 if (need_forkexit_callback) { 4438 if (need_forkexit_callback) {
5039 /* 4439 for_each_subsys(ss, i)
5040 * fork/exit callbacks are supported only for builtin
5041 * subsystems, and the builtin section of the subsys
5042 * array is immutable, so we don't need to lock the
5043 * subsys array here. On the other hand, modular section
5044 * of the array can be freed at module unload, so we
5045 * can't touch that.
5046 */
5047 for_each_builtin_subsys(ss, i)
5048 if (ss->fork) 4440 if (ss->fork)
5049 ss->fork(child); 4441 ss->fork(child);
5050 } 4442 }
@@ -5053,7 +4445,6 @@ void cgroup_post_fork(struct task_struct *child)
5053/** 4445/**
5054 * cgroup_exit - detach cgroup from exiting task 4446 * cgroup_exit - detach cgroup from exiting task
5055 * @tsk: pointer to task_struct of exiting process 4447 * @tsk: pointer to task_struct of exiting process
5056 * @run_callback: run exit callbacks?
5057 * 4448 *
5058 * Description: Detach cgroup from @tsk and release it. 4449 * Description: Detach cgroup from @tsk and release it.
5059 * 4450 *
@@ -5063,57 +4454,38 @@ void cgroup_post_fork(struct task_struct *child)
5063 * use notify_on_release cgroups where very high task exit scaling 4454 * use notify_on_release cgroups where very high task exit scaling
5064 * is required on large systems. 4455 * is required on large systems.
5065 * 4456 *
5066 * the_top_cgroup_hack: 4457 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5067 * 4458 * call cgroup_exit() while the task is still competent to handle
5068 * Set the exiting tasks cgroup to the root cgroup (top_cgroup). 4459 * notify_on_release(), then leave the task attached to the root cgroup in
5069 * 4460 * each hierarchy for the remainder of its exit. No need to bother with
5070 * We call cgroup_exit() while the task is still competent to 4461 * init_css_set refcnting. init_css_set never goes away and we can't race
5071 * handle notify_on_release(), then leave the task attached to the 4462 * with migration path - PF_EXITING is visible to migration path.
5072 * root cgroup in each hierarchy for the remainder of its exit.
5073 *
5074 * To do this properly, we would increment the reference count on
5075 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5076 * code we would add a second cgroup function call, to drop that
5077 * reference. This would just create an unnecessary hot spot on
5078 * the top_cgroup reference count, to no avail.
5079 *
5080 * Normally, holding a reference to a cgroup without bumping its
5081 * count is unsafe. The cgroup could go away, or someone could
5082 * attach us to a different cgroup, decrementing the count on
5083 * the first cgroup that we never incremented. But in this case,
5084 * top_cgroup isn't going away, and either task has PF_EXITING set,
5085 * which wards off any cgroup_attach_task() attempts, or task is a failed
5086 * fork, never visible to cgroup_attach_task.
5087 */ 4463 */
5088void cgroup_exit(struct task_struct *tsk, int run_callbacks) 4464void cgroup_exit(struct task_struct *tsk)
5089{ 4465{
5090 struct cgroup_subsys *ss; 4466 struct cgroup_subsys *ss;
5091 struct css_set *cset; 4467 struct css_set *cset;
4468 bool put_cset = false;
5092 int i; 4469 int i;
5093 4470
5094 /* 4471 /*
5095 * Unlink from the css_set task list if necessary. 4472 * Unlink from @tsk from its css_set. As migration path can't race
5096 * Optimistically check cg_list before taking 4473 * with us, we can check cg_list without grabbing css_set_rwsem.
5097 * css_set_lock
5098 */ 4474 */
5099 if (!list_empty(&tsk->cg_list)) { 4475 if (!list_empty(&tsk->cg_list)) {
5100 write_lock(&css_set_lock); 4476 down_write(&css_set_rwsem);
5101 if (!list_empty(&tsk->cg_list)) 4477 list_del_init(&tsk->cg_list);
5102 list_del_init(&tsk->cg_list); 4478 up_write(&css_set_rwsem);
5103 write_unlock(&css_set_lock); 4479 put_cset = true;
5104 } 4480 }
5105 4481
5106 /* Reassign the task to the init_css_set. */ 4482 /* Reassign the task to the init_css_set. */
5107 task_lock(tsk);
5108 cset = task_css_set(tsk); 4483 cset = task_css_set(tsk);
5109 RCU_INIT_POINTER(tsk->cgroups, &init_css_set); 4484 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
5110 4485
5111 if (run_callbacks && need_forkexit_callback) { 4486 if (need_forkexit_callback) {
5112 /* 4487 /* see cgroup_post_fork() for details */
5113 * fork/exit callbacks are supported only for builtin 4488 for_each_subsys(ss, i) {
5114 * subsystems, see cgroup_post_fork() for details.
5115 */
5116 for_each_builtin_subsys(ss, i) {
5117 if (ss->exit) { 4489 if (ss->exit) {
5118 struct cgroup_subsys_state *old_css = cset->subsys[i]; 4490 struct cgroup_subsys_state *old_css = cset->subsys[i];
5119 struct cgroup_subsys_state *css = task_css(tsk, i); 4491 struct cgroup_subsys_state *css = task_css(tsk, i);
@@ -5122,9 +4494,9 @@ void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5122 } 4494 }
5123 } 4495 }
5124 } 4496 }
5125 task_unlock(tsk);
5126 4497
5127 put_css_set_taskexit(cset); 4498 if (put_cset)
4499 put_css_set(cset, true);
5128} 4500}
5129 4501
5130static void check_for_release(struct cgroup *cgrp) 4502static void check_for_release(struct cgroup *cgrp)
@@ -5181,16 +4553,17 @@ static void cgroup_release_agent(struct work_struct *work)
5181 while (!list_empty(&release_list)) { 4553 while (!list_empty(&release_list)) {
5182 char *argv[3], *envp[3]; 4554 char *argv[3], *envp[3];
5183 int i; 4555 int i;
5184 char *pathbuf = NULL, *agentbuf = NULL; 4556 char *pathbuf = NULL, *agentbuf = NULL, *path;
5185 struct cgroup *cgrp = list_entry(release_list.next, 4557 struct cgroup *cgrp = list_entry(release_list.next,
5186 struct cgroup, 4558 struct cgroup,
5187 release_list); 4559 release_list);
5188 list_del_init(&cgrp->release_list); 4560 list_del_init(&cgrp->release_list);
5189 raw_spin_unlock(&release_list_lock); 4561 raw_spin_unlock(&release_list_lock);
5190 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4562 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5191 if (!pathbuf) 4563 if (!pathbuf)
5192 goto continue_free; 4564 goto continue_free;
5193 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) 4565 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4566 if (!path)
5194 goto continue_free; 4567 goto continue_free;
5195 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); 4568 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5196 if (!agentbuf) 4569 if (!agentbuf)
@@ -5198,7 +4571,7 @@ static void cgroup_release_agent(struct work_struct *work)
5198 4571
5199 i = 0; 4572 i = 0;
5200 argv[i++] = agentbuf; 4573 argv[i++] = agentbuf;
5201 argv[i++] = pathbuf; 4574 argv[i++] = path;
5202 argv[i] = NULL; 4575 argv[i] = NULL;
5203 4576
5204 i = 0; 4577 i = 0;
@@ -5232,11 +4605,7 @@ static int __init cgroup_disable(char *str)
5232 if (!*token) 4605 if (!*token)
5233 continue; 4606 continue;
5234 4607
5235 /* 4608 for_each_subsys(ss, i) {
5236 * cgroup_disable, being at boot time, can't know about
5237 * module subsystems, so we don't worry about them.
5238 */
5239 for_each_builtin_subsys(ss, i) {
5240 if (!strcmp(token, ss->name)) { 4609 if (!strcmp(token, ss->name)) {
5241 ss->disabled = 1; 4610 ss->disabled = 1;
5242 printk(KERN_INFO "Disabling %s control group" 4611 printk(KERN_INFO "Disabling %s control group"
@@ -5250,28 +4619,42 @@ static int __init cgroup_disable(char *str)
5250__setup("cgroup_disable=", cgroup_disable); 4619__setup("cgroup_disable=", cgroup_disable);
5251 4620
5252/** 4621/**
5253 * css_from_dir - get corresponding css from the dentry of a cgroup dir 4622 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
5254 * @dentry: directory dentry of interest 4623 * @dentry: directory dentry of interest
5255 * @ss: subsystem of interest 4624 * @ss: subsystem of interest
5256 * 4625 *
5257 * Must be called under cgroup_mutex or RCU read lock. The caller is 4626 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5258 * responsible for pinning the returned css if it needs to be accessed 4627 * to get the corresponding css and return it. If such css doesn't exist
5259 * outside the critical section. 4628 * or can't be pinned, an ERR_PTR value is returned.
5260 */ 4629 */
5261struct cgroup_subsys_state *css_from_dir(struct dentry *dentry, 4630struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5262 struct cgroup_subsys *ss) 4631 struct cgroup_subsys *ss)
5263{ 4632{
4633 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4634 struct cgroup_subsys_state *css = NULL;
5264 struct cgroup *cgrp; 4635 struct cgroup *cgrp;
5265 4636
5266 cgroup_assert_mutex_or_rcu_locked();
5267
5268 /* is @dentry a cgroup dir? */ 4637 /* is @dentry a cgroup dir? */
5269 if (!dentry->d_inode || 4638 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5270 dentry->d_inode->i_op != &cgroup_dir_inode_operations) 4639 kernfs_type(kn) != KERNFS_DIR)
5271 return ERR_PTR(-EBADF); 4640 return ERR_PTR(-EBADF);
5272 4641
5273 cgrp = __d_cgrp(dentry); 4642 rcu_read_lock();
5274 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT); 4643
4644 /*
4645 * This path doesn't originate from kernfs and @kn could already
4646 * have been or be removed at any point. @kn->priv is RCU
4647 * protected for this access. See destroy_locked() for details.
4648 */
4649 cgrp = rcu_dereference(kn->priv);
4650 if (cgrp)
4651 css = cgroup_css(cgrp, ss);
4652
4653 if (!css || !css_tryget(css))
4654 css = ERR_PTR(-ENOENT);
4655
4656 rcu_read_unlock();
4657 return css;
5275} 4658}
5276 4659
5277/** 4660/**
@@ -5286,7 +4669,7 @@ struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5286{ 4669{
5287 struct cgroup *cgrp; 4670 struct cgroup *cgrp;
5288 4671
5289 cgroup_assert_mutex_or_rcu_locked(); 4672 cgroup_assert_mutexes_or_rcu_locked();
5290 4673
5291 cgrp = idr_find(&ss->root->cgroup_idr, id); 4674 cgrp = idr_find(&ss->root->cgroup_idr, id);
5292 if (cgrp) 4675 if (cgrp)
@@ -5338,23 +4721,25 @@ static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
5338{ 4721{
5339 struct cgrp_cset_link *link; 4722 struct cgrp_cset_link *link;
5340 struct css_set *cset; 4723 struct css_set *cset;
4724 char *name_buf;
5341 4725
5342 read_lock(&css_set_lock); 4726 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4727 if (!name_buf)
4728 return -ENOMEM;
4729
4730 down_read(&css_set_rwsem);
5343 rcu_read_lock(); 4731 rcu_read_lock();
5344 cset = rcu_dereference(current->cgroups); 4732 cset = rcu_dereference(current->cgroups);
5345 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { 4733 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
5346 struct cgroup *c = link->cgrp; 4734 struct cgroup *c = link->cgrp;
5347 const char *name;
5348 4735
5349 if (c->dentry) 4736 cgroup_name(c, name_buf, NAME_MAX + 1);
5350 name = c->dentry->d_name.name;
5351 else
5352 name = "?";
5353 seq_printf(seq, "Root %d group %s\n", 4737 seq_printf(seq, "Root %d group %s\n",
5354 c->root->hierarchy_id, name); 4738 c->root->hierarchy_id, name_buf);
5355 } 4739 }
5356 rcu_read_unlock(); 4740 rcu_read_unlock();
5357 read_unlock(&css_set_lock); 4741 up_read(&css_set_rwsem);
4742 kfree(name_buf);
5358 return 0; 4743 return 0;
5359} 4744}
5360 4745
@@ -5364,23 +4749,30 @@ static int cgroup_css_links_read(struct seq_file *seq, void *v)
5364 struct cgroup_subsys_state *css = seq_css(seq); 4749 struct cgroup_subsys_state *css = seq_css(seq);
5365 struct cgrp_cset_link *link; 4750 struct cgrp_cset_link *link;
5366 4751
5367 read_lock(&css_set_lock); 4752 down_read(&css_set_rwsem);
5368 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { 4753 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
5369 struct css_set *cset = link->cset; 4754 struct css_set *cset = link->cset;
5370 struct task_struct *task; 4755 struct task_struct *task;
5371 int count = 0; 4756 int count = 0;
4757
5372 seq_printf(seq, "css_set %p\n", cset); 4758 seq_printf(seq, "css_set %p\n", cset);
4759
5373 list_for_each_entry(task, &cset->tasks, cg_list) { 4760 list_for_each_entry(task, &cset->tasks, cg_list) {
5374 if (count++ > MAX_TASKS_SHOWN_PER_CSS) { 4761 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5375 seq_puts(seq, " ...\n"); 4762 goto overflow;
5376 break; 4763 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5377 } else { 4764 }
5378 seq_printf(seq, " task %d\n", 4765
5379 task_pid_vnr(task)); 4766 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5380 } 4767 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4768 goto overflow;
4769 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5381 } 4770 }
4771 continue;
4772 overflow:
4773 seq_puts(seq, " ...\n");
5382 } 4774 }
5383 read_unlock(&css_set_lock); 4775 up_read(&css_set_rwsem);
5384 return 0; 4776 return 0;
5385} 4777}
5386 4778
@@ -5423,11 +4815,9 @@ static struct cftype debug_files[] = {
5423 { } /* terminate */ 4815 { } /* terminate */
5424}; 4816};
5425 4817
5426struct cgroup_subsys debug_subsys = { 4818struct cgroup_subsys debug_cgrp_subsys = {
5427 .name = "debug",
5428 .css_alloc = debug_css_alloc, 4819 .css_alloc = debug_css_alloc,
5429 .css_free = debug_css_free, 4820 .css_free = debug_css_free,
5430 .subsys_id = debug_subsys_id,
5431 .base_cftypes = debug_files, 4821 .base_cftypes = debug_files,
5432}; 4822};
5433#endif /* CONFIG_CGROUP_DEBUG */ 4823#endif /* CONFIG_CGROUP_DEBUG */