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