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