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