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