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