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