cgroup: remove css_scan_tasks()
[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>
ddbcc7e8 43#include <linux/slab.h>
ddbcc7e8 44#include <linux/spinlock.h>
96d365e0 45#include <linux/rwsem.h>
ddbcc7e8 46#include <linux/string.h>
bbcb81d0 47#include <linux/sort.h>
81a6a5cd 48#include <linux/kmod.h>
846c7bb0
BS
49#include <linux/delayacct.h>
50#include <linux/cgroupstats.h>
0ac801fe 51#include <linux/hashtable.h>
096b7fe0 52#include <linux/pid_namespace.h>
2c6ab6d2 53#include <linux/idr.h>
d1d9fd33 54#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
081aa458 55#include <linux/flex_array.h> /* used in cgroup_attach_task */
c4c27fbd 56#include <linux/kthread.h>
776f02fa 57#include <linux/delay.h>
846c7bb0 58
60063497 59#include <linux/atomic.h>
ddbcc7e8 60
b1a21367
TH
61/*
62 * pidlists linger the following amount before being destroyed. The goal
63 * is avoiding frequent destruction in the middle of consecutive read calls
64 * Expiring in the middle is a performance problem not a correctness one.
65 * 1 sec should be enough.
66 */
67#define CGROUP_PIDLIST_DESTROY_DELAY HZ
68
8d7e6fb0
TH
69#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
70 MAX_CFTYPE_NAME + 2)
71
ace2bee8
TH
72/*
73 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
74 * creation/removal and hierarchy changing operations including cgroup
75 * creation, removal, css association and controller rebinding. This outer
76 * lock is needed mainly to resolve the circular dependency between kernfs
77 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
78 */
79static DEFINE_MUTEX(cgroup_tree_mutex);
80
e25e2cbb
TH
81/*
82 * cgroup_mutex is the master lock. Any modification to cgroup or its
83 * hierarchy must be performed while holding it.
e25e2cbb 84 */
2219449a
TH
85#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
8af01f56 87EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
2219449a 88#else
81a6a5cd 89static DEFINE_MUTEX(cgroup_mutex);
2219449a
TH
90#endif
91
69e943b7
TH
92/*
93 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
94 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
95 */
96static DEFINE_SPINLOCK(release_agent_path_lock);
97
ace2bee8 98#define cgroup_assert_mutexes_or_rcu_locked() \
87fb54f1 99 rcu_lockdep_assert(rcu_read_lock_held() || \
ace2bee8 100 lockdep_is_held(&cgroup_tree_mutex) || \
87fb54f1 101 lockdep_is_held(&cgroup_mutex), \
ace2bee8 102 "cgroup_[tree_]mutex or RCU read lock required");
87fb54f1 103
e5fca243
TH
104/*
105 * cgroup destruction makes heavy use of work items and there can be a lot
106 * of concurrent destructions. Use a separate workqueue so that cgroup
107 * destruction work items don't end up filling up max_active of system_wq
108 * which may lead to deadlock.
109 */
110static struct workqueue_struct *cgroup_destroy_wq;
111
b1a21367
TH
112/*
113 * pidlist destructions need to be flushed on cgroup destruction. Use a
114 * separate workqueue as flush domain.
115 */
116static struct workqueue_struct *cgroup_pidlist_destroy_wq;
117
3ed80a62 118/* generate an array of cgroup subsystem pointers */
073219e9 119#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
3ed80a62 120static struct cgroup_subsys *cgroup_subsys[] = {
ddbcc7e8
PM
121#include <linux/cgroup_subsys.h>
122};
073219e9
TH
123#undef SUBSYS
124
125/* array of cgroup subsystem names */
126#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
127static const char *cgroup_subsys_name[] = {
128#include <linux/cgroup_subsys.h>
129};
130#undef SUBSYS
ddbcc7e8 131
ddbcc7e8 132/*
9871bf95
TH
133 * The dummy hierarchy, reserved for the subsystems that are otherwise
134 * unattached - it never has more than a single cgroup, and all tasks are
135 * part of that cgroup.
ddbcc7e8 136 */
9871bf95
TH
137static struct cgroupfs_root cgroup_dummy_root;
138
139/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
140static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
ddbcc7e8
PM
141
142/* The list of hierarchy roots */
143
9871bf95
TH
144static LIST_HEAD(cgroup_roots);
145static int cgroup_root_count;
ddbcc7e8 146
3417ae1f 147/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
1a574231 148static DEFINE_IDR(cgroup_hierarchy_idr);
2c6ab6d2 149
794611a1
LZ
150/*
151 * Assign a monotonically increasing serial number to cgroups. It
152 * guarantees cgroups with bigger numbers are newer than those with smaller
153 * numbers. Also, as cgroups are always appended to the parent's
154 * ->children list, it guarantees that sibling cgroups are always sorted in
00356bd5
TH
155 * the ascending serial number order on the list. Protected by
156 * cgroup_mutex.
794611a1 157 */
00356bd5 158static u64 cgroup_serial_nr_next = 1;
794611a1 159
ddbcc7e8 160/* This flag indicates whether tasks in the fork and exit paths should
a043e3b2
LZ
161 * check for fork/exit handlers to call. This avoids us having to do
162 * extra work in the fork/exit path if none of the subsystems need to
163 * be called.
ddbcc7e8 164 */
8947f9d5 165static int need_forkexit_callback __read_mostly;
ddbcc7e8 166
628f7cd4
TH
167static struct cftype cgroup_base_files[];
168
59f5296b 169static void cgroup_put(struct cgroup *cgrp);
f2e85d57
TH
170static int rebind_subsystems(struct cgroupfs_root *root,
171 unsigned long added_mask, unsigned removed_mask);
f20104de 172static void cgroup_destroy_css_killed(struct cgroup *cgrp);
42809dd4 173static int cgroup_destroy_locked(struct cgroup *cgrp);
2bb566cb
TH
174static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
175 bool is_add);
b1a21367 176static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
42809dd4 177
95109b62
TH
178/**
179 * cgroup_css - obtain a cgroup's css for the specified subsystem
180 * @cgrp: the cgroup of interest
ca8bdcaf 181 * @ss: the subsystem of interest (%NULL returns the dummy_css)
95109b62 182 *
ca8bdcaf
TH
183 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
184 * function must be called either under cgroup_mutex or rcu_read_lock() and
185 * the caller is responsible for pinning the returned css if it wants to
186 * keep accessing it outside the said locks. This function may return
187 * %NULL if @cgrp doesn't have @subsys_id enabled.
95109b62
TH
188 */
189static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
ca8bdcaf 190 struct cgroup_subsys *ss)
95109b62 191{
ca8bdcaf 192 if (ss)
aec25020 193 return rcu_dereference_check(cgrp->subsys[ss->id],
ace2bee8
TH
194 lockdep_is_held(&cgroup_tree_mutex) ||
195 lockdep_is_held(&cgroup_mutex));
ca8bdcaf
TH
196 else
197 return &cgrp->dummy_css;
95109b62 198}
42809dd4 199
ddbcc7e8 200/* convenient tests for these bits */
54766d4a 201static inline bool cgroup_is_dead(const struct cgroup *cgrp)
ddbcc7e8 202{
54766d4a 203 return test_bit(CGRP_DEAD, &cgrp->flags);
ddbcc7e8
PM
204}
205
59f5296b
TH
206struct cgroup_subsys_state *seq_css(struct seq_file *seq)
207{
2bd59d48
TH
208 struct kernfs_open_file *of = seq->private;
209 struct cgroup *cgrp = of->kn->parent->priv;
210 struct cftype *cft = seq_cft(seq);
211
212 /*
213 * This is open and unprotected implementation of cgroup_css().
214 * seq_css() is only called from a kernfs file operation which has
215 * an active reference on the file. Because all the subsystem
216 * files are drained before a css is disassociated with a cgroup,
217 * the matching css from the cgroup's subsys table is guaranteed to
218 * be and stay valid until the enclosing operation is complete.
219 */
220 if (cft->ss)
221 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
222 else
223 return &cgrp->dummy_css;
59f5296b
TH
224}
225EXPORT_SYMBOL_GPL(seq_css);
226
78574cf9
LZ
227/**
228 * cgroup_is_descendant - test ancestry
229 * @cgrp: the cgroup to be tested
230 * @ancestor: possible ancestor of @cgrp
231 *
232 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
233 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
234 * and @ancestor are accessible.
235 */
236bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
237{
238 while (cgrp) {
239 if (cgrp == ancestor)
240 return true;
241 cgrp = cgrp->parent;
242 }
243 return false;
244}
245EXPORT_SYMBOL_GPL(cgroup_is_descendant);
ddbcc7e8 246
e9685a03 247static int cgroup_is_releasable(const struct cgroup *cgrp)
81a6a5cd
PM
248{
249 const int bits =
bd89aabc
PM
250 (1 << CGRP_RELEASABLE) |
251 (1 << CGRP_NOTIFY_ON_RELEASE);
252 return (cgrp->flags & bits) == bits;
81a6a5cd
PM
253}
254
e9685a03 255static int notify_on_release(const struct cgroup *cgrp)
81a6a5cd 256{
bd89aabc 257 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
81a6a5cd
PM
258}
259
1c6727af
TH
260/**
261 * for_each_css - iterate all css's of a cgroup
262 * @css: the iteration cursor
263 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
264 * @cgrp: the target cgroup to iterate css's of
265 *
266 * Should be called under cgroup_mutex.
267 */
268#define for_each_css(css, ssid, cgrp) \
269 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
270 if (!((css) = rcu_dereference_check( \
271 (cgrp)->subsys[(ssid)], \
ace2bee8 272 lockdep_is_held(&cgroup_tree_mutex) || \
1c6727af
TH
273 lockdep_is_held(&cgroup_mutex)))) { } \
274 else
275
30159ec7 276/**
3ed80a62 277 * for_each_subsys - iterate all enabled cgroup subsystems
30159ec7 278 * @ss: the iteration cursor
780cd8b3 279 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
30159ec7 280 */
780cd8b3 281#define for_each_subsys(ss, ssid) \
3ed80a62
TH
282 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
283 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
30159ec7 284
5549c497
TH
285/* iterate across the active hierarchies */
286#define for_each_active_root(root) \
287 list_for_each_entry((root), &cgroup_roots, root_list)
ddbcc7e8 288
7ae1bad9
TH
289/**
290 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
291 * @cgrp: the cgroup to be checked for liveness
292 *
47cfcd09
TH
293 * On success, returns true; the mutex should be later unlocked. On
294 * failure returns false with no lock held.
7ae1bad9 295 */
b9777cf8 296static bool cgroup_lock_live_group(struct cgroup *cgrp)
7ae1bad9
TH
297{
298 mutex_lock(&cgroup_mutex);
54766d4a 299 if (cgroup_is_dead(cgrp)) {
7ae1bad9
TH
300 mutex_unlock(&cgroup_mutex);
301 return false;
302 }
303 return true;
304}
7ae1bad9 305
81a6a5cd
PM
306/* the list of cgroups eligible for automatic release. Protected by
307 * release_list_lock */
308static LIST_HEAD(release_list);
cdcc136f 309static DEFINE_RAW_SPINLOCK(release_list_lock);
81a6a5cd
PM
310static void cgroup_release_agent(struct work_struct *work);
311static DECLARE_WORK(release_agent_work, cgroup_release_agent);
bd89aabc 312static void check_for_release(struct cgroup *cgrp);
81a6a5cd 313
69d0206c
TH
314/*
315 * A cgroup can be associated with multiple css_sets as different tasks may
316 * belong to different cgroups on different hierarchies. In the other
317 * direction, a css_set is naturally associated with multiple cgroups.
318 * This M:N relationship is represented by the following link structure
319 * which exists for each association and allows traversing the associations
320 * from both sides.
321 */
322struct cgrp_cset_link {
323 /* the cgroup and css_set this link associates */
324 struct cgroup *cgrp;
325 struct css_set *cset;
326
327 /* list of cgrp_cset_links anchored at cgrp->cset_links */
328 struct list_head cset_link;
329
330 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
331 struct list_head cgrp_link;
817929ec
PM
332};
333
334/* The default css_set - used by init and its children prior to any
335 * hierarchies being mounted. It contains a pointer to the root state
336 * for each subsystem. Also used to anchor the list of css_sets. Not
337 * reference-counted, to improve performance when child cgroups
338 * haven't been created.
339 */
340
341static struct css_set init_css_set;
69d0206c 342static struct cgrp_cset_link init_cgrp_cset_link;
817929ec 343
0942eeee 344/*
96d365e0
TH
345 * css_set_rwsem protects the list of css_set objects, and the chain of
346 * tasks off each css_set.
0942eeee 347 */
96d365e0 348static DECLARE_RWSEM(css_set_rwsem);
817929ec
PM
349static int css_set_count;
350
7717f7ba
PM
351/*
352 * hash table for cgroup groups. This improves the performance to find
353 * an existing css_set. This hash doesn't (currently) take into
354 * account cgroups in empty hierarchies.
355 */
472b1053 356#define CSS_SET_HASH_BITS 7
0ac801fe 357static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
472b1053 358
0ac801fe 359static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
472b1053 360{
0ac801fe 361 unsigned long key = 0UL;
30159ec7
TH
362 struct cgroup_subsys *ss;
363 int i;
472b1053 364
30159ec7 365 for_each_subsys(ss, i)
0ac801fe
LZ
366 key += (unsigned long)css[i];
367 key = (key >> 16) ^ key;
472b1053 368
0ac801fe 369 return key;
472b1053
LZ
370}
371
5abb8855 372static void __put_css_set(struct css_set *cset, int taskexit)
b4f48b63 373{
69d0206c 374 struct cgrp_cset_link *link, *tmp_link;
5abb8855 375
146aa1bd
LJ
376 /*
377 * Ensure that the refcount doesn't hit zero while any readers
378 * can see it. Similar to atomic_dec_and_lock(), but for an
379 * rwlock
380 */
5abb8855 381 if (atomic_add_unless(&cset->refcount, -1, 1))
146aa1bd 382 return;
96d365e0 383 down_write(&css_set_rwsem);
5abb8855 384 if (!atomic_dec_and_test(&cset->refcount)) {
96d365e0 385 up_write(&css_set_rwsem);
146aa1bd
LJ
386 return;
387 }
81a6a5cd 388
2c6ab6d2 389 /* This css_set is dead. unlink it and release cgroup refcounts */
5abb8855 390 hash_del(&cset->hlist);
2c6ab6d2
PM
391 css_set_count--;
392
69d0206c 393 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
2c6ab6d2 394 struct cgroup *cgrp = link->cgrp;
5abb8855 395
69d0206c
TH
396 list_del(&link->cset_link);
397 list_del(&link->cgrp_link);
71b5707e 398
96d365e0 399 /* @cgrp can't go away while we're holding css_set_rwsem */
6f3d828f 400 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
81a6a5cd 401 if (taskexit)
bd89aabc
PM
402 set_bit(CGRP_RELEASABLE, &cgrp->flags);
403 check_for_release(cgrp);
81a6a5cd 404 }
2c6ab6d2
PM
405
406 kfree(link);
81a6a5cd 407 }
2c6ab6d2 408
96d365e0 409 up_write(&css_set_rwsem);
5abb8855 410 kfree_rcu(cset, rcu_head);
b4f48b63
PM
411}
412
817929ec
PM
413/*
414 * refcounted get/put for css_set objects
415 */
5abb8855 416static inline void get_css_set(struct css_set *cset)
817929ec 417{
5abb8855 418 atomic_inc(&cset->refcount);
817929ec
PM
419}
420
5abb8855 421static inline void put_css_set(struct css_set *cset)
817929ec 422{
5abb8855 423 __put_css_set(cset, 0);
817929ec
PM
424}
425
5abb8855 426static inline void put_css_set_taskexit(struct css_set *cset)
81a6a5cd 427{
5abb8855 428 __put_css_set(cset, 1);
81a6a5cd
PM
429}
430
b326f9d0 431/**
7717f7ba 432 * compare_css_sets - helper function for find_existing_css_set().
5abb8855
TH
433 * @cset: candidate css_set being tested
434 * @old_cset: existing css_set for a task
7717f7ba
PM
435 * @new_cgrp: cgroup that's being entered by the task
436 * @template: desired set of css pointers in css_set (pre-calculated)
437 *
6f4b7e63 438 * Returns true if "cset" matches "old_cset" except for the hierarchy
7717f7ba
PM
439 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
440 */
5abb8855
TH
441static bool compare_css_sets(struct css_set *cset,
442 struct css_set *old_cset,
7717f7ba
PM
443 struct cgroup *new_cgrp,
444 struct cgroup_subsys_state *template[])
445{
446 struct list_head *l1, *l2;
447
5abb8855 448 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
7717f7ba
PM
449 /* Not all subsystems matched */
450 return false;
451 }
452
453 /*
454 * Compare cgroup pointers in order to distinguish between
455 * different cgroups in heirarchies with no subsystems. We
456 * could get by with just this check alone (and skip the
457 * memcmp above) but on most setups the memcmp check will
458 * avoid the need for this more expensive check on almost all
459 * candidates.
460 */
461
69d0206c
TH
462 l1 = &cset->cgrp_links;
463 l2 = &old_cset->cgrp_links;
7717f7ba 464 while (1) {
69d0206c 465 struct cgrp_cset_link *link1, *link2;
5abb8855 466 struct cgroup *cgrp1, *cgrp2;
7717f7ba
PM
467
468 l1 = l1->next;
469 l2 = l2->next;
470 /* See if we reached the end - both lists are equal length. */
69d0206c
TH
471 if (l1 == &cset->cgrp_links) {
472 BUG_ON(l2 != &old_cset->cgrp_links);
7717f7ba
PM
473 break;
474 } else {
69d0206c 475 BUG_ON(l2 == &old_cset->cgrp_links);
7717f7ba
PM
476 }
477 /* Locate the cgroups associated with these links. */
69d0206c
TH
478 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
479 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
480 cgrp1 = link1->cgrp;
481 cgrp2 = link2->cgrp;
7717f7ba 482 /* Hierarchies should be linked in the same order. */
5abb8855 483 BUG_ON(cgrp1->root != cgrp2->root);
7717f7ba
PM
484
485 /*
486 * If this hierarchy is the hierarchy of the cgroup
487 * that's changing, then we need to check that this
488 * css_set points to the new cgroup; if it's any other
489 * hierarchy, then this css_set should point to the
490 * same cgroup as the old css_set.
491 */
5abb8855
TH
492 if (cgrp1->root == new_cgrp->root) {
493 if (cgrp1 != new_cgrp)
7717f7ba
PM
494 return false;
495 } else {
5abb8855 496 if (cgrp1 != cgrp2)
7717f7ba
PM
497 return false;
498 }
499 }
500 return true;
501}
502
b326f9d0
TH
503/**
504 * find_existing_css_set - init css array and find the matching css_set
505 * @old_cset: the css_set that we're using before the cgroup transition
506 * @cgrp: the cgroup that we're moving into
507 * @template: out param for the new set of csses, should be clear on entry
817929ec 508 */
5abb8855
TH
509static struct css_set *find_existing_css_set(struct css_set *old_cset,
510 struct cgroup *cgrp,
511 struct cgroup_subsys_state *template[])
b4f48b63 512{
bd89aabc 513 struct cgroupfs_root *root = cgrp->root;
30159ec7 514 struct cgroup_subsys *ss;
5abb8855 515 struct css_set *cset;
0ac801fe 516 unsigned long key;
b326f9d0 517 int i;
817929ec 518
aae8aab4
BB
519 /*
520 * Build the set of subsystem state objects that we want to see in the
521 * new css_set. while subsystems can change globally, the entries here
522 * won't change, so no need for locking.
523 */
30159ec7 524 for_each_subsys(ss, i) {
a1a71b45 525 if (root->subsys_mask & (1UL << i)) {
817929ec
PM
526 /* Subsystem is in this hierarchy. So we want
527 * the subsystem state from the new
528 * cgroup */
ca8bdcaf 529 template[i] = cgroup_css(cgrp, ss);
817929ec
PM
530 } else {
531 /* Subsystem is not in this hierarchy, so we
532 * don't want to change the subsystem state */
5abb8855 533 template[i] = old_cset->subsys[i];
817929ec
PM
534 }
535 }
536
0ac801fe 537 key = css_set_hash(template);
5abb8855
TH
538 hash_for_each_possible(css_set_table, cset, hlist, key) {
539 if (!compare_css_sets(cset, old_cset, cgrp, template))
7717f7ba
PM
540 continue;
541
542 /* This css_set matches what we need */
5abb8855 543 return cset;
472b1053 544 }
817929ec
PM
545
546 /* No existing cgroup group matched */
547 return NULL;
548}
549
69d0206c 550static void free_cgrp_cset_links(struct list_head *links_to_free)
36553434 551{
69d0206c 552 struct cgrp_cset_link *link, *tmp_link;
36553434 553
69d0206c
TH
554 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
555 list_del(&link->cset_link);
36553434
LZ
556 kfree(link);
557 }
558}
559
69d0206c
TH
560/**
561 * allocate_cgrp_cset_links - allocate cgrp_cset_links
562 * @count: the number of links to allocate
563 * @tmp_links: list_head the allocated links are put on
564 *
565 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
566 * through ->cset_link. Returns 0 on success or -errno.
817929ec 567 */
69d0206c 568static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
817929ec 569{
69d0206c 570 struct cgrp_cset_link *link;
817929ec 571 int i;
69d0206c
TH
572
573 INIT_LIST_HEAD(tmp_links);
574
817929ec 575 for (i = 0; i < count; i++) {
f4f4be2b 576 link = kzalloc(sizeof(*link), GFP_KERNEL);
817929ec 577 if (!link) {
69d0206c 578 free_cgrp_cset_links(tmp_links);
817929ec
PM
579 return -ENOMEM;
580 }
69d0206c 581 list_add(&link->cset_link, tmp_links);
817929ec
PM
582 }
583 return 0;
584}
585
c12f65d4
LZ
586/**
587 * link_css_set - a helper function to link a css_set to a cgroup
69d0206c 588 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
5abb8855 589 * @cset: the css_set to be linked
c12f65d4
LZ
590 * @cgrp: the destination cgroup
591 */
69d0206c
TH
592static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
593 struct cgroup *cgrp)
c12f65d4 594{
69d0206c 595 struct cgrp_cset_link *link;
c12f65d4 596
69d0206c
TH
597 BUG_ON(list_empty(tmp_links));
598 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
599 link->cset = cset;
7717f7ba 600 link->cgrp = cgrp;
69d0206c 601 list_move(&link->cset_link, &cgrp->cset_links);
7717f7ba
PM
602 /*
603 * Always add links to the tail of the list so that the list
604 * is sorted by order of hierarchy creation
605 */
69d0206c 606 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
c12f65d4
LZ
607}
608
b326f9d0
TH
609/**
610 * find_css_set - return a new css_set with one cgroup updated
611 * @old_cset: the baseline css_set
612 * @cgrp: the cgroup to be updated
613 *
614 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
615 * substituted into the appropriate hierarchy.
817929ec 616 */
5abb8855
TH
617static struct css_set *find_css_set(struct css_set *old_cset,
618 struct cgroup *cgrp)
817929ec 619{
b326f9d0 620 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
5abb8855 621 struct css_set *cset;
69d0206c
TH
622 struct list_head tmp_links;
623 struct cgrp_cset_link *link;
0ac801fe 624 unsigned long key;
472b1053 625
b326f9d0
TH
626 lockdep_assert_held(&cgroup_mutex);
627
817929ec
PM
628 /* First see if we already have a cgroup group that matches
629 * the desired set */
96d365e0 630 down_read(&css_set_rwsem);
5abb8855
TH
631 cset = find_existing_css_set(old_cset, cgrp, template);
632 if (cset)
633 get_css_set(cset);
96d365e0 634 up_read(&css_set_rwsem);
817929ec 635
5abb8855
TH
636 if (cset)
637 return cset;
817929ec 638
f4f4be2b 639 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
5abb8855 640 if (!cset)
817929ec
PM
641 return NULL;
642
69d0206c 643 /* Allocate all the cgrp_cset_link objects that we'll need */
9871bf95 644 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
5abb8855 645 kfree(cset);
817929ec
PM
646 return NULL;
647 }
648
5abb8855 649 atomic_set(&cset->refcount, 1);
69d0206c 650 INIT_LIST_HEAD(&cset->cgrp_links);
5abb8855
TH
651 INIT_LIST_HEAD(&cset->tasks);
652 INIT_HLIST_NODE(&cset->hlist);
817929ec
PM
653
654 /* Copy the set of subsystem state objects generated in
655 * find_existing_css_set() */
5abb8855 656 memcpy(cset->subsys, template, sizeof(cset->subsys));
817929ec 657
96d365e0 658 down_write(&css_set_rwsem);
817929ec 659 /* Add reference counts and links from the new css_set. */
69d0206c 660 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
7717f7ba 661 struct cgroup *c = link->cgrp;
69d0206c 662
7717f7ba
PM
663 if (c->root == cgrp->root)
664 c = cgrp;
69d0206c 665 link_css_set(&tmp_links, cset, c);
7717f7ba 666 }
817929ec 667
69d0206c 668 BUG_ON(!list_empty(&tmp_links));
817929ec 669
817929ec 670 css_set_count++;
472b1053
LZ
671
672 /* Add this cgroup group to the hash table */
5abb8855
TH
673 key = css_set_hash(cset->subsys);
674 hash_add(css_set_table, &cset->hlist, key);
472b1053 675
96d365e0 676 up_write(&css_set_rwsem);
817929ec 677
5abb8855 678 return cset;
b4f48b63
PM
679}
680
2bd59d48
TH
681static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
682{
683 struct cgroup *top_cgrp = kf_root->kn->priv;
684
685 return top_cgrp->root;
686}
687
f2e85d57
TH
688static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
689{
690 int id;
691
692 lockdep_assert_held(&cgroup_mutex);
693
694 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
695 GFP_KERNEL);
696 if (id < 0)
697 return id;
698
699 root->hierarchy_id = id;
700 return 0;
701}
702
703static void cgroup_exit_root_id(struct cgroupfs_root *root)
704{
705 lockdep_assert_held(&cgroup_mutex);
706
707 if (root->hierarchy_id) {
708 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
709 root->hierarchy_id = 0;
710 }
711}
712
713static void cgroup_free_root(struct cgroupfs_root *root)
714{
715 if (root) {
716 /* hierarhcy ID shoulid already have been released */
717 WARN_ON_ONCE(root->hierarchy_id);
718
719 idr_destroy(&root->cgroup_idr);
720 kfree(root);
721 }
722}
723
776f02fa 724static void cgroup_destroy_root(struct cgroupfs_root *root)
59f5296b 725{
f2e85d57
TH
726 struct cgroup *cgrp = &root->top_cgroup;
727 struct cgrp_cset_link *link, *tmp_link;
f2e85d57 728
2bd59d48 729 mutex_lock(&cgroup_tree_mutex);
2bd59d48 730 mutex_lock(&cgroup_mutex);
f2e85d57 731
776f02fa 732 BUG_ON(atomic_read(&root->nr_cgrps));
f2e85d57
TH
733 BUG_ON(!list_empty(&cgrp->children));
734
f2e85d57 735 /* Rebind all subsystems back to the default hierarchy */
35585573 736 WARN_ON(rebind_subsystems(root, 0, root->subsys_mask));
f2e85d57
TH
737
738 /*
739 * Release all the links from cset_links to this hierarchy's
740 * root cgroup
741 */
96d365e0 742 down_write(&css_set_rwsem);
f2e85d57
TH
743
744 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
745 list_del(&link->cset_link);
746 list_del(&link->cgrp_link);
747 kfree(link);
748 }
96d365e0 749 up_write(&css_set_rwsem);
f2e85d57
TH
750
751 if (!list_empty(&root->root_list)) {
752 list_del(&root->root_list);
753 cgroup_root_count--;
754 }
755
756 cgroup_exit_root_id(root);
757
758 mutex_unlock(&cgroup_mutex);
759 mutex_unlock(&cgroup_tree_mutex);
f2e85d57 760
2bd59d48 761 kernfs_destroy_root(root->kf_root);
f2e85d57
TH
762 cgroup_free_root(root);
763}
764
7717f7ba
PM
765/*
766 * Return the cgroup for "task" from the given hierarchy. Must be
96d365e0 767 * called with cgroup_mutex and css_set_rwsem held.
7717f7ba
PM
768 */
769static struct cgroup *task_cgroup_from_root(struct task_struct *task,
770 struct cgroupfs_root *root)
771{
5abb8855 772 struct css_set *cset;
7717f7ba
PM
773 struct cgroup *res = NULL;
774
96d365e0
TH
775 lockdep_assert_held(&cgroup_mutex);
776 lockdep_assert_held(&css_set_rwsem);
777
7717f7ba
PM
778 /*
779 * No need to lock the task - since we hold cgroup_mutex the
780 * task can't change groups, so the only thing that can happen
781 * is that it exits and its css is set back to init_css_set.
782 */
a8ad805c 783 cset = task_css_set(task);
5abb8855 784 if (cset == &init_css_set) {
7717f7ba
PM
785 res = &root->top_cgroup;
786 } else {
69d0206c
TH
787 struct cgrp_cset_link *link;
788
789 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
7717f7ba 790 struct cgroup *c = link->cgrp;
69d0206c 791
7717f7ba
PM
792 if (c->root == root) {
793 res = c;
794 break;
795 }
796 }
797 }
96d365e0 798
7717f7ba
PM
799 BUG_ON(!res);
800 return res;
801}
802
ddbcc7e8
PM
803/*
804 * There is one global cgroup mutex. We also require taking
805 * task_lock() when dereferencing a task's cgroup subsys pointers.
806 * See "The task_lock() exception", at the end of this comment.
807 *
808 * A task must hold cgroup_mutex to modify cgroups.
809 *
810 * Any task can increment and decrement the count field without lock.
811 * So in general, code holding cgroup_mutex can't rely on the count
812 * field not changing. However, if the count goes to zero, then only
956db3ca 813 * cgroup_attach_task() can increment it again. Because a count of zero
ddbcc7e8
PM
814 * means that no tasks are currently attached, therefore there is no
815 * way a task attached to that cgroup can fork (the other way to
816 * increment the count). So code holding cgroup_mutex can safely
817 * assume that if the count is zero, it will stay zero. Similarly, if
818 * a task holds cgroup_mutex on a cgroup with zero count, it
819 * knows that the cgroup won't be removed, as cgroup_rmdir()
820 * needs that mutex.
821 *
ddbcc7e8
PM
822 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
823 * (usually) take cgroup_mutex. These are the two most performance
824 * critical pieces of code here. The exception occurs on cgroup_exit(),
825 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
826 * is taken, and if the cgroup count is zero, a usermode call made
a043e3b2
LZ
827 * to the release agent with the name of the cgroup (path relative to
828 * the root of cgroup file system) as the argument.
ddbcc7e8
PM
829 *
830 * A cgroup can only be deleted if both its 'count' of using tasks
831 * is zero, and its list of 'children' cgroups is empty. Since all
832 * tasks in the system use _some_ cgroup, and since there is always at
833 * least one task in the system (init, pid == 1), therefore, top_cgroup
834 * always has either children cgroups and/or using tasks. So we don't
835 * need a special hack to ensure that top_cgroup cannot be deleted.
836 *
837 * The task_lock() exception
838 *
839 * The need for this exception arises from the action of
d0b2fdd2 840 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
a043e3b2 841 * another. It does so using cgroup_mutex, however there are
ddbcc7e8
PM
842 * several performance critical places that need to reference
843 * task->cgroup without the expense of grabbing a system global
844 * mutex. Therefore except as noted below, when dereferencing or, as
d0b2fdd2 845 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
ddbcc7e8
PM
846 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
847 * the task_struct routinely used for such matters.
848 *
849 * P.S. One more locking exception. RCU is used to guard the
956db3ca 850 * update of a tasks cgroup pointer by cgroup_attach_task()
ddbcc7e8
PM
851 */
852
628f7cd4 853static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
2bd59d48 854static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
828c0950 855static const struct file_operations proc_cgroupstats_operations;
a424316c 856
8d7e6fb0
TH
857static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
858 char *buf)
859{
860 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
861 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
862 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
863 cft->ss->name, cft->name);
864 else
865 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
866 return buf;
867}
868
f2e85d57
TH
869/**
870 * cgroup_file_mode - deduce file mode of a control file
871 * @cft: the control file in question
872 *
873 * returns cft->mode if ->mode is not 0
874 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
875 * returns S_IRUGO if it has only a read handler
876 * returns S_IWUSR if it has only a write hander
877 */
878static umode_t cgroup_file_mode(const struct cftype *cft)
879{
880 umode_t mode = 0;
881
882 if (cft->mode)
883 return cft->mode;
884
885 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
886 mode |= S_IRUGO;
887
888 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
889 cft->trigger)
890 mode |= S_IWUSR;
891
892 return mode;
893}
894
be445626
LZ
895static void cgroup_free_fn(struct work_struct *work)
896{
ea15f8cc 897 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
be445626 898
3c9c825b 899 atomic_dec(&cgrp->root->nr_cgrps);
b1a21367 900 cgroup_pidlist_destroy_all(cgrp);
be445626 901
776f02fa
TH
902 if (cgrp->parent) {
903 /*
904 * We get a ref to the parent, and put the ref when this
905 * cgroup is being freed, so it's guaranteed that the
906 * parent won't be destroyed before its children.
907 */
908 cgroup_put(cgrp->parent);
909 kernfs_put(cgrp->kn);
910 kfree(cgrp);
911 } else {
912 /*
913 * This is top cgroup's refcnt reaching zero, which
914 * indicates that the root should be released.
915 */
916 cgroup_destroy_root(cgrp->root);
917 }
be445626
LZ
918}
919
920static void cgroup_free_rcu(struct rcu_head *head)
921{
922 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
923
ea15f8cc 924 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
e5fca243 925 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
be445626
LZ
926}
927
59f5296b
TH
928static void cgroup_get(struct cgroup *cgrp)
929{
2bd59d48
TH
930 WARN_ON_ONCE(cgroup_is_dead(cgrp));
931 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
932 atomic_inc(&cgrp->refcnt);
ddbcc7e8
PM
933}
934
59f5296b
TH
935static void cgroup_put(struct cgroup *cgrp)
936{
2bd59d48
TH
937 if (!atomic_dec_and_test(&cgrp->refcnt))
938 return;
776f02fa 939 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
2bd59d48 940 return;
59f5296b 941
2bd59d48
TH
942 /*
943 * XXX: cgrp->id is only used to look up css's. As cgroup and
944 * css's lifetimes will be decoupled, it should be made
945 * per-subsystem and moved to css->id so that lookups are
946 * successful until the target css is released.
947 */
948 mutex_lock(&cgroup_mutex);
949 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
950 mutex_unlock(&cgroup_mutex);
951 cgrp->id = -1;
ddbcc7e8 952
2bd59d48 953 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
ddbcc7e8
PM
954}
955
2739d3cc 956static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
05ef1d7c 957{
2bd59d48 958 char name[CGROUP_FILE_NAME_MAX];
05ef1d7c 959
ace2bee8 960 lockdep_assert_held(&cgroup_tree_mutex);
2bd59d48 961 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
05ef1d7c
TH
962}
963
13af07df 964/**
628f7cd4 965 * cgroup_clear_dir - remove subsys files in a cgroup directory
8f89140a 966 * @cgrp: target cgroup
13af07df
AR
967 * @subsys_mask: mask of the subsystem ids whose files should be removed
968 */
628f7cd4 969static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
05ef1d7c 970{
13af07df 971 struct cgroup_subsys *ss;
b420ba7d 972 int i;
05ef1d7c 973
b420ba7d 974 for_each_subsys(ss, i) {
0adb0704 975 struct cftype *cfts;
b420ba7d
TH
976
977 if (!test_bit(i, &subsys_mask))
13af07df 978 continue;
0adb0704
TH
979 list_for_each_entry(cfts, &ss->cfts, node)
980 cgroup_addrm_files(cgrp, cfts, false);
13af07df 981 }
ddbcc7e8
PM
982}
983
ddbcc7e8 984static int rebind_subsystems(struct cgroupfs_root *root,
a8a648c4 985 unsigned long added_mask, unsigned removed_mask)
ddbcc7e8 986{
bd89aabc 987 struct cgroup *cgrp = &root->top_cgroup;
30159ec7 988 struct cgroup_subsys *ss;
3126121f 989 int i, ret;
ddbcc7e8 990
ace2bee8
TH
991 lockdep_assert_held(&cgroup_tree_mutex);
992 lockdep_assert_held(&cgroup_mutex);
aae8aab4 993
ddbcc7e8 994 /* Check that any added subsystems are currently free */
3ed80a62
TH
995 for_each_subsys(ss, i)
996 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
997 return -EBUSY;
ddbcc7e8 998
3126121f
TH
999 ret = cgroup_populate_dir(cgrp, added_mask);
1000 if (ret)
3ed80a62 1001 return ret;
3126121f
TH
1002
1003 /*
1004 * Nothing can fail from this point on. Remove files for the
1005 * removed subsystems and rebind each subsystem.
1006 */
4ac06017 1007 mutex_unlock(&cgroup_mutex);
3126121f 1008 cgroup_clear_dir(cgrp, removed_mask);
4ac06017 1009 mutex_lock(&cgroup_mutex);
ddbcc7e8 1010
30159ec7 1011 for_each_subsys(ss, i) {
ddbcc7e8 1012 unsigned long bit = 1UL << i;
30159ec7 1013
a1a71b45 1014 if (bit & added_mask) {
ddbcc7e8 1015 /* We're binding this subsystem to this hierarchy */
ca8bdcaf
TH
1016 BUG_ON(cgroup_css(cgrp, ss));
1017 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1018 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
a8a648c4 1019
73e80ed8 1020 rcu_assign_pointer(cgrp->subsys[i],
ca8bdcaf
TH
1021 cgroup_css(cgroup_dummy_top, ss));
1022 cgroup_css(cgrp, ss)->cgroup = cgrp;
a8a648c4 1023
b2aa30f7 1024 ss->root = root;
ddbcc7e8 1025 if (ss->bind)
ca8bdcaf 1026 ss->bind(cgroup_css(cgrp, ss));
a8a648c4 1027
cf5d5941 1028 /* refcount was already taken, and we're keeping it */
a8a648c4 1029 root->subsys_mask |= bit;
a1a71b45 1030 } else if (bit & removed_mask) {
ddbcc7e8 1031 /* We're removing this subsystem */
ca8bdcaf
TH
1032 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1033 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
a8a648c4 1034
ddbcc7e8 1035 if (ss->bind)
ca8bdcaf 1036 ss->bind(cgroup_css(cgroup_dummy_top, ss));
73e80ed8 1037
ca8bdcaf 1038 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
73e80ed8
TH
1039 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1040
9871bf95 1041 cgroup_subsys[i]->root = &cgroup_dummy_root;
a8a648c4 1042 root->subsys_mask &= ~bit;
ddbcc7e8
PM
1043 }
1044 }
ddbcc7e8 1045
2bd59d48 1046 kernfs_activate(cgrp->kn);
ddbcc7e8
PM
1047 return 0;
1048}
1049
2bd59d48
TH
1050static int cgroup_show_options(struct seq_file *seq,
1051 struct kernfs_root *kf_root)
ddbcc7e8 1052{
2bd59d48 1053 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
ddbcc7e8 1054 struct cgroup_subsys *ss;
b85d2040 1055 int ssid;
ddbcc7e8 1056
b85d2040
TH
1057 for_each_subsys(ss, ssid)
1058 if (root->subsys_mask & (1 << ssid))
1059 seq_printf(seq, ",%s", ss->name);
873fe09e
TH
1060 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1061 seq_puts(seq, ",sane_behavior");
93438629 1062 if (root->flags & CGRP_ROOT_NOPREFIX)
ddbcc7e8 1063 seq_puts(seq, ",noprefix");
93438629 1064 if (root->flags & CGRP_ROOT_XATTR)
03b1cde6 1065 seq_puts(seq, ",xattr");
69e943b7
TH
1066
1067 spin_lock(&release_agent_path_lock);
81a6a5cd
PM
1068 if (strlen(root->release_agent_path))
1069 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
69e943b7
TH
1070 spin_unlock(&release_agent_path_lock);
1071
2260e7fc 1072 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
97978e6d 1073 seq_puts(seq, ",clone_children");
c6d57f33
PM
1074 if (strlen(root->name))
1075 seq_printf(seq, ",name=%s", root->name);
ddbcc7e8
PM
1076 return 0;
1077}
1078
1079struct cgroup_sb_opts {
a1a71b45 1080 unsigned long subsys_mask;
ddbcc7e8 1081 unsigned long flags;
81a6a5cd 1082 char *release_agent;
2260e7fc 1083 bool cpuset_clone_children;
c6d57f33 1084 char *name;
2c6ab6d2
PM
1085 /* User explicitly requested empty subsystem */
1086 bool none;
ddbcc7e8
PM
1087};
1088
aae8aab4 1089/*
9871bf95
TH
1090 * Convert a hierarchy specifier into a bitmask of subsystems and
1091 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1092 * array. This function takes refcounts on subsystems to be used, unless it
1093 * returns error, in which case no refcounts are taken.
aae8aab4 1094 */
cf5d5941 1095static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
ddbcc7e8 1096{
32a8cf23
DL
1097 char *token, *o = data;
1098 bool all_ss = false, one_ss = false;
f9ab5b5b 1099 unsigned long mask = (unsigned long)-1;
30159ec7
TH
1100 struct cgroup_subsys *ss;
1101 int i;
f9ab5b5b 1102
aae8aab4
BB
1103 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1104
f9ab5b5b 1105#ifdef CONFIG_CPUSETS
073219e9 1106 mask = ~(1UL << cpuset_cgrp_id);
f9ab5b5b 1107#endif
ddbcc7e8 1108
c6d57f33 1109 memset(opts, 0, sizeof(*opts));
ddbcc7e8
PM
1110
1111 while ((token = strsep(&o, ",")) != NULL) {
1112 if (!*token)
1113 return -EINVAL;
32a8cf23 1114 if (!strcmp(token, "none")) {
2c6ab6d2
PM
1115 /* Explicitly have no subsystems */
1116 opts->none = true;
32a8cf23
DL
1117 continue;
1118 }
1119 if (!strcmp(token, "all")) {
1120 /* Mutually exclusive option 'all' + subsystem name */
1121 if (one_ss)
1122 return -EINVAL;
1123 all_ss = true;
1124 continue;
1125 }
873fe09e
TH
1126 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1127 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1128 continue;
1129 }
32a8cf23 1130 if (!strcmp(token, "noprefix")) {
93438629 1131 opts->flags |= CGRP_ROOT_NOPREFIX;
32a8cf23
DL
1132 continue;
1133 }
1134 if (!strcmp(token, "clone_children")) {
2260e7fc 1135 opts->cpuset_clone_children = true;
32a8cf23
DL
1136 continue;
1137 }
03b1cde6 1138 if (!strcmp(token, "xattr")) {
93438629 1139 opts->flags |= CGRP_ROOT_XATTR;
03b1cde6
AR
1140 continue;
1141 }
32a8cf23 1142 if (!strncmp(token, "release_agent=", 14)) {
81a6a5cd
PM
1143 /* Specifying two release agents is forbidden */
1144 if (opts->release_agent)
1145 return -EINVAL;
c6d57f33 1146 opts->release_agent =
e400c285 1147 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
81a6a5cd
PM
1148 if (!opts->release_agent)
1149 return -ENOMEM;
32a8cf23
DL
1150 continue;
1151 }
1152 if (!strncmp(token, "name=", 5)) {
c6d57f33
PM
1153 const char *name = token + 5;
1154 /* Can't specify an empty name */
1155 if (!strlen(name))
1156 return -EINVAL;
1157 /* Must match [\w.-]+ */
1158 for (i = 0; i < strlen(name); i++) {
1159 char c = name[i];
1160 if (isalnum(c))
1161 continue;
1162 if ((c == '.') || (c == '-') || (c == '_'))
1163 continue;
1164 return -EINVAL;
1165 }
1166 /* Specifying two names is forbidden */
1167 if (opts->name)
1168 return -EINVAL;
1169 opts->name = kstrndup(name,
e400c285 1170 MAX_CGROUP_ROOT_NAMELEN - 1,
c6d57f33
PM
1171 GFP_KERNEL);
1172 if (!opts->name)
1173 return -ENOMEM;
32a8cf23
DL
1174
1175 continue;
1176 }
1177
30159ec7 1178 for_each_subsys(ss, i) {
32a8cf23
DL
1179 if (strcmp(token, ss->name))
1180 continue;
1181 if (ss->disabled)
1182 continue;
1183
1184 /* Mutually exclusive option 'all' + subsystem name */
1185 if (all_ss)
1186 return -EINVAL;
a1a71b45 1187 set_bit(i, &opts->subsys_mask);
32a8cf23
DL
1188 one_ss = true;
1189
1190 break;
1191 }
1192 if (i == CGROUP_SUBSYS_COUNT)
1193 return -ENOENT;
1194 }
1195
1196 /*
1197 * If the 'all' option was specified select all the subsystems,
0d19ea86
LZ
1198 * otherwise if 'none', 'name=' and a subsystem name options
1199 * were not specified, let's default to 'all'
32a8cf23 1200 */
30159ec7
TH
1201 if (all_ss || (!one_ss && !opts->none && !opts->name))
1202 for_each_subsys(ss, i)
1203 if (!ss->disabled)
1204 set_bit(i, &opts->subsys_mask);
ddbcc7e8 1205
2c6ab6d2
PM
1206 /* Consistency checks */
1207
873fe09e
TH
1208 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1209 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1210
d3ba07c3
TH
1211 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1212 opts->cpuset_clone_children || opts->release_agent ||
1213 opts->name) {
1214 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
873fe09e
TH
1215 return -EINVAL;
1216 }
873fe09e
TH
1217 }
1218
f9ab5b5b
LZ
1219 /*
1220 * Option noprefix was introduced just for backward compatibility
1221 * with the old cpuset, so we allow noprefix only if mounting just
1222 * the cpuset subsystem.
1223 */
93438629 1224 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
f9ab5b5b
LZ
1225 return -EINVAL;
1226
2c6ab6d2
PM
1227
1228 /* Can't specify "none" and some subsystems */
a1a71b45 1229 if (opts->subsys_mask && opts->none)
2c6ab6d2
PM
1230 return -EINVAL;
1231
1232 /*
1233 * We either have to specify by name or by subsystems. (So all
1234 * empty hierarchies must have a name).
1235 */
a1a71b45 1236 if (!opts->subsys_mask && !opts->name)
ddbcc7e8
PM
1237 return -EINVAL;
1238
1239 return 0;
1240}
1241
2bd59d48 1242static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
ddbcc7e8
PM
1243{
1244 int ret = 0;
2bd59d48 1245 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
ddbcc7e8 1246 struct cgroup_sb_opts opts;
a1a71b45 1247 unsigned long added_mask, removed_mask;
ddbcc7e8 1248
873fe09e
TH
1249 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1250 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1251 return -EINVAL;
1252 }
1253
ace2bee8 1254 mutex_lock(&cgroup_tree_mutex);
ddbcc7e8
PM
1255 mutex_lock(&cgroup_mutex);
1256
1257 /* See what subsystems are wanted */
1258 ret = parse_cgroupfs_options(data, &opts);
1259 if (ret)
1260 goto out_unlock;
1261
a8a648c4 1262 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
8b5a5a9d
TH
1263 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1264 task_tgid_nr(current), current->comm);
1265
a1a71b45
AR
1266 added_mask = opts.subsys_mask & ~root->subsys_mask;
1267 removed_mask = root->subsys_mask & ~opts.subsys_mask;
13af07df 1268
cf5d5941 1269 /* Don't allow flags or name to change at remount */
0ce6cba3 1270 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
cf5d5941 1271 (opts.name && strcmp(opts.name, root->name))) {
0ce6cba3
TH
1272 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1273 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1274 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
c6d57f33
PM
1275 ret = -EINVAL;
1276 goto out_unlock;
1277 }
1278
f172e67c 1279 /* remounting is not allowed for populated hierarchies */
3c9c825b 1280 if (!list_empty(&root->top_cgroup.children)) {
f172e67c 1281 ret = -EBUSY;
0670e08b 1282 goto out_unlock;
cf5d5941 1283 }
ddbcc7e8 1284
a8a648c4 1285 ret = rebind_subsystems(root, added_mask, removed_mask);
3126121f 1286 if (ret)
0670e08b 1287 goto out_unlock;
ddbcc7e8 1288
69e943b7
TH
1289 if (opts.release_agent) {
1290 spin_lock(&release_agent_path_lock);
81a6a5cd 1291 strcpy(root->release_agent_path, opts.release_agent);
69e943b7
TH
1292 spin_unlock(&release_agent_path_lock);
1293 }
ddbcc7e8 1294 out_unlock:
66bdc9cf 1295 kfree(opts.release_agent);
c6d57f33 1296 kfree(opts.name);
ddbcc7e8 1297 mutex_unlock(&cgroup_mutex);
ace2bee8 1298 mutex_unlock(&cgroup_tree_mutex);
ddbcc7e8
PM
1299 return ret;
1300}
1301
afeb0f9f
TH
1302/*
1303 * To reduce the fork() overhead for systems that are not actually using
1304 * their cgroups capability, we don't maintain the lists running through
1305 * each css_set to its tasks until we see the list actually used - in other
1306 * words after the first mount.
1307 */
1308static bool use_task_css_set_links __read_mostly;
1309
1310static void cgroup_enable_task_cg_lists(void)
1311{
1312 struct task_struct *p, *g;
1313
96d365e0 1314 down_write(&css_set_rwsem);
afeb0f9f
TH
1315
1316 if (use_task_css_set_links)
1317 goto out_unlock;
1318
1319 use_task_css_set_links = true;
1320
1321 /*
1322 * We need tasklist_lock because RCU is not safe against
1323 * while_each_thread(). Besides, a forking task that has passed
1324 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1325 * is not guaranteed to have its child immediately visible in the
1326 * tasklist if we walk through it with RCU.
1327 */
1328 read_lock(&tasklist_lock);
1329 do_each_thread(g, p) {
1330 task_lock(p);
1331
1332 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1333 task_css_set(p) != &init_css_set);
1334
1335 /*
1336 * We should check if the process is exiting, otherwise
1337 * it will race with cgroup_exit() in that the list
1338 * entry won't be deleted though the process has exited.
1339 */
1340 if (!(p->flags & PF_EXITING))
1341 list_add(&p->cg_list, &task_css_set(p)->tasks);
1342
1343 task_unlock(p);
1344 } while_each_thread(g, p);
1345 read_unlock(&tasklist_lock);
1346out_unlock:
96d365e0 1347 up_write(&css_set_rwsem);
afeb0f9f
TH
1348}
1349
cc31edce
PM
1350static void init_cgroup_housekeeping(struct cgroup *cgrp)
1351{
2bd59d48 1352 atomic_set(&cgrp->refcnt, 1);
cc31edce
PM
1353 INIT_LIST_HEAD(&cgrp->sibling);
1354 INIT_LIST_HEAD(&cgrp->children);
69d0206c 1355 INIT_LIST_HEAD(&cgrp->cset_links);
cc31edce 1356 INIT_LIST_HEAD(&cgrp->release_list);
72a8cb30
BB
1357 INIT_LIST_HEAD(&cgrp->pidlists);
1358 mutex_init(&cgrp->pidlist_mutex);
67f4c36f 1359 cgrp->dummy_css.cgroup = cgrp;
cc31edce 1360}
c6d57f33 1361
ddbcc7e8
PM
1362static void init_cgroup_root(struct cgroupfs_root *root)
1363{
bd89aabc 1364 struct cgroup *cgrp = &root->top_cgroup;
b0ca5a84 1365
ddbcc7e8 1366 INIT_LIST_HEAD(&root->root_list);
3c9c825b 1367 atomic_set(&root->nr_cgrps, 1);
bd89aabc 1368 cgrp->root = root;
cc31edce 1369 init_cgroup_housekeeping(cgrp);
4e96ee8e 1370 idr_init(&root->cgroup_idr);
ddbcc7e8
PM
1371}
1372
c6d57f33
PM
1373static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1374{
1375 struct cgroupfs_root *root;
1376
a1a71b45 1377 if (!opts->subsys_mask && !opts->none)
2bd59d48 1378 return ERR_PTR(-EINVAL);
c6d57f33
PM
1379
1380 root = kzalloc(sizeof(*root), GFP_KERNEL);
1381 if (!root)
1382 return ERR_PTR(-ENOMEM);
1383
1384 init_cgroup_root(root);
2c6ab6d2 1385
c6d57f33
PM
1386 root->flags = opts->flags;
1387 if (opts->release_agent)
1388 strcpy(root->release_agent_path, opts->release_agent);
1389 if (opts->name)
1390 strcpy(root->name, opts->name);
2260e7fc
TH
1391 if (opts->cpuset_clone_children)
1392 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
c6d57f33
PM
1393 return root;
1394}
1395
35585573 1396static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
d427dfeb
TH
1397{
1398 LIST_HEAD(tmp_links);
d427dfeb 1399 struct cgroup *root_cgrp = &root->top_cgroup;
d427dfeb 1400 struct css_set *cset;
d427dfeb
TH
1401 int i, ret;
1402
1403 lockdep_assert_held(&cgroup_tree_mutex);
1404 lockdep_assert_held(&cgroup_mutex);
d427dfeb
TH
1405
1406 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1407 if (ret < 0)
2bd59d48 1408 goto out;
d427dfeb
TH
1409 root_cgrp->id = ret;
1410
d427dfeb 1411 /*
96d365e0 1412 * We're accessing css_set_count without locking css_set_rwsem here,
d427dfeb
TH
1413 * but that's OK - it can only be increased by someone holding
1414 * cgroup_lock, and that's us. The worst that can happen is that we
1415 * have some link structures left over
1416 */
1417 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1418 if (ret)
2bd59d48 1419 goto out;
d427dfeb
TH
1420
1421 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1422 ret = cgroup_init_root_id(root, 2, 0);
1423 if (ret)
2bd59d48 1424 goto out;
d427dfeb 1425
2bd59d48
TH
1426 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1427 KERNFS_ROOT_CREATE_DEACTIVATED,
1428 root_cgrp);
1429 if (IS_ERR(root->kf_root)) {
1430 ret = PTR_ERR(root->kf_root);
1431 goto exit_root_id;
1432 }
1433 root_cgrp->kn = root->kf_root->kn;
d427dfeb
TH
1434
1435 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1436 if (ret)
2bd59d48 1437 goto destroy_root;
d427dfeb 1438
35585573 1439 ret = rebind_subsystems(root, ss_mask, 0);
d427dfeb 1440 if (ret)
2bd59d48 1441 goto destroy_root;
d427dfeb
TH
1442
1443 /*
1444 * There must be no failure case after here, since rebinding takes
1445 * care of subsystems' refcounts, which are explicitly dropped in
1446 * the failure exit path.
1447 */
1448 list_add(&root->root_list, &cgroup_roots);
1449 cgroup_root_count++;
1450
1451 /*
1452 * Link the top cgroup in this hierarchy into all the css_set
1453 * objects.
1454 */
96d365e0 1455 down_write(&css_set_rwsem);
d427dfeb
TH
1456 hash_for_each(css_set_table, i, cset, hlist)
1457 link_css_set(&tmp_links, cset, root_cgrp);
96d365e0 1458 up_write(&css_set_rwsem);
d427dfeb
TH
1459
1460 BUG_ON(!list_empty(&root_cgrp->children));
3c9c825b 1461 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
d427dfeb 1462
2bd59d48 1463 kernfs_activate(root_cgrp->kn);
d427dfeb 1464 ret = 0;
2bd59d48 1465 goto out;
d427dfeb 1466
2bd59d48
TH
1467destroy_root:
1468 kernfs_destroy_root(root->kf_root);
1469 root->kf_root = NULL;
1470exit_root_id:
d427dfeb 1471 cgroup_exit_root_id(root);
2bd59d48 1472out:
d427dfeb
TH
1473 free_cgrp_cset_links(&tmp_links);
1474 return ret;
1475}
1476
f7e83571 1477static struct dentry *cgroup_mount(struct file_system_type *fs_type,
ddbcc7e8 1478 int flags, const char *unused_dev_name,
f7e83571 1479 void *data)
ddbcc7e8 1480{
2bd59d48 1481 struct cgroupfs_root *root;
ddbcc7e8 1482 struct cgroup_sb_opts opts;
2bd59d48 1483 struct dentry *dentry;
8e30e2b8 1484 int ret;
56fde9e0
TH
1485
1486 /*
1487 * The first time anyone tries to mount a cgroup, enable the list
1488 * linking each css_set to its tasks and fix up all existing tasks.
1489 */
1490 if (!use_task_css_set_links)
1491 cgroup_enable_task_cg_lists();
776f02fa 1492retry:
8e30e2b8 1493 mutex_lock(&cgroup_tree_mutex);
aae8aab4 1494 mutex_lock(&cgroup_mutex);
8e30e2b8
TH
1495
1496 /* First find the desired set of subsystems */
ddbcc7e8 1497 ret = parse_cgroupfs_options(data, &opts);
c6d57f33 1498 if (ret)
8e30e2b8 1499 goto out_unlock;
ddbcc7e8 1500
2bd59d48
TH
1501 /* look for a matching existing root */
1502 for_each_active_root(root) {
1503 bool name_match = false;
ddbcc7e8 1504
2bd59d48
TH
1505 /*
1506 * If we asked for a name then it must match. Also, if
1507 * name matches but sybsys_mask doesn't, we should fail.
1508 * Remember whether name matched.
1509 */
1510 if (opts.name) {
1511 if (strcmp(opts.name, root->name))
1512 continue;
1513 name_match = true;
1514 }
ddbcc7e8 1515
c6d57f33 1516 /*
2bd59d48
TH
1517 * If we asked for subsystems (or explicitly for no
1518 * subsystems) then they must match.
c6d57f33 1519 */
2bd59d48
TH
1520 if ((opts.subsys_mask || opts.none) &&
1521 (opts.subsys_mask != root->subsys_mask)) {
1522 if (!name_match)
1523 continue;
1524 ret = -EBUSY;
1525 goto out_unlock;
1526 }
873fe09e 1527
c7ba8287 1528 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
2a0ff3fb
JL
1529 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1530 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1531 ret = -EINVAL;
8e30e2b8 1532 goto out_unlock;
2a0ff3fb
JL
1533 } else {
1534 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1535 }
873fe09e 1536 }
2bd59d48 1537
776f02fa
TH
1538 /*
1539 * A root's lifetime is governed by its top cgroup. Zero
1540 * ref indicate that the root is being destroyed. Wait for
1541 * destruction to complete so that the subsystems are free.
1542 * We can use wait_queue for the wait but this path is
1543 * super cold. Let's just sleep for a bit and retry.
1544 */
1545 if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
1546 mutex_unlock(&cgroup_mutex);
1547 mutex_unlock(&cgroup_tree_mutex);
1548 msleep(10);
1549 goto retry;
1550 }
1551
1552 ret = 0;
2bd59d48 1553 goto out_unlock;
ddbcc7e8
PM
1554 }
1555
2bd59d48
TH
1556 /* no such thing, create a new one */
1557 root = cgroup_root_from_opts(&opts);
1558 if (IS_ERR(root)) {
1559 ret = PTR_ERR(root);
1560 goto out_unlock;
1561 }
1562
35585573 1563 ret = cgroup_setup_root(root, opts.subsys_mask);
2bd59d48
TH
1564 if (ret)
1565 cgroup_free_root(root);
1566
8e30e2b8 1567out_unlock:
e25e2cbb 1568 mutex_unlock(&cgroup_mutex);
ace2bee8 1569 mutex_unlock(&cgroup_tree_mutex);
8e30e2b8 1570
c6d57f33
PM
1571 kfree(opts.release_agent);
1572 kfree(opts.name);
8e30e2b8 1573
2bd59d48 1574 if (ret)
8e30e2b8 1575 return ERR_PTR(ret);
2bd59d48
TH
1576
1577 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1578 if (IS_ERR(dentry))
776f02fa 1579 cgroup_put(&root->top_cgroup);
2bd59d48
TH
1580 return dentry;
1581}
1582
1583static void cgroup_kill_sb(struct super_block *sb)
1584{
1585 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1586 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1587
776f02fa 1588 cgroup_put(&root->top_cgroup);
2bd59d48 1589 kernfs_kill_sb(sb);
ddbcc7e8
PM
1590}
1591
ddbcc7e8
PM
1592static struct file_system_type cgroup_fs_type = {
1593 .name = "cgroup",
f7e83571 1594 .mount = cgroup_mount,
ddbcc7e8
PM
1595 .kill_sb = cgroup_kill_sb,
1596};
1597
676db4af
GK
1598static struct kobject *cgroup_kobj;
1599
857a2beb 1600/**
913ffdb5 1601 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
857a2beb 1602 * @task: target task
857a2beb
TH
1603 * @buf: the buffer to write the path into
1604 * @buflen: the length of the buffer
1605 *
913ffdb5
TH
1606 * Determine @task's cgroup on the first (the one with the lowest non-zero
1607 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1608 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1609 * cgroup controller callbacks.
1610 *
e61734c5 1611 * Return value is the same as kernfs_path().
857a2beb 1612 */
e61734c5 1613char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
857a2beb
TH
1614{
1615 struct cgroupfs_root *root;
913ffdb5 1616 struct cgroup *cgrp;
e61734c5
TH
1617 int hierarchy_id = 1;
1618 char *path = NULL;
857a2beb
TH
1619
1620 mutex_lock(&cgroup_mutex);
96d365e0 1621 down_read(&css_set_rwsem);
857a2beb 1622
913ffdb5
TH
1623 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1624
857a2beb
TH
1625 if (root) {
1626 cgrp = task_cgroup_from_root(task, root);
e61734c5 1627 path = cgroup_path(cgrp, buf, buflen);
913ffdb5
TH
1628 } else {
1629 /* if no hierarchy exists, everyone is in "/" */
e61734c5
TH
1630 if (strlcpy(buf, "/", buflen) < buflen)
1631 path = buf;
857a2beb
TH
1632 }
1633
96d365e0 1634 up_read(&css_set_rwsem);
857a2beb 1635 mutex_unlock(&cgroup_mutex);
e61734c5 1636 return path;
857a2beb 1637}
913ffdb5 1638EXPORT_SYMBOL_GPL(task_cgroup_path);
857a2beb 1639
2f7ee569
TH
1640/*
1641 * Control Group taskset
1642 */
134d3373
TH
1643struct task_and_cgroup {
1644 struct task_struct *task;
1645 struct cgroup *cgrp;
6f4b7e63 1646 struct css_set *cset;
134d3373
TH
1647};
1648
2f7ee569
TH
1649struct cgroup_taskset {
1650 struct task_and_cgroup single;
1651 struct flex_array *tc_array;
1652 int tc_array_len;
1653 int idx;
1654 struct cgroup *cur_cgrp;
1655};
1656
1657/**
1658 * cgroup_taskset_first - reset taskset and return the first task
1659 * @tset: taskset of interest
1660 *
1661 * @tset iteration is initialized and the first task is returned.
1662 */
1663struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1664{
1665 if (tset->tc_array) {
1666 tset->idx = 0;
1667 return cgroup_taskset_next(tset);
1668 } else {
1669 tset->cur_cgrp = tset->single.cgrp;
1670 return tset->single.task;
1671 }
1672}
1673EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1674
1675/**
1676 * cgroup_taskset_next - iterate to the next task in taskset
1677 * @tset: taskset of interest
1678 *
1679 * Return the next task in @tset. Iteration must have been initialized
1680 * with cgroup_taskset_first().
1681 */
1682struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1683{
1684 struct task_and_cgroup *tc;
1685
1686 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1687 return NULL;
1688
1689 tc = flex_array_get(tset->tc_array, tset->idx++);
1690 tset->cur_cgrp = tc->cgrp;
1691 return tc->task;
1692}
1693EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1694
1695/**
d99c8727 1696 * cgroup_taskset_cur_css - return the matching css for the current task
2f7ee569 1697 * @tset: taskset of interest
d99c8727 1698 * @subsys_id: the ID of the target subsystem
2f7ee569 1699 *
d99c8727
TH
1700 * Return the css for the current (last returned) task of @tset for
1701 * subsystem specified by @subsys_id. This function must be preceded by
1702 * either cgroup_taskset_first() or cgroup_taskset_next().
2f7ee569 1703 */
d99c8727
TH
1704struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1705 int subsys_id)
2f7ee569 1706{
ca8bdcaf 1707 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
2f7ee569 1708}
d99c8727 1709EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
2f7ee569
TH
1710
1711/**
1712 * cgroup_taskset_size - return the number of tasks in taskset
1713 * @tset: taskset of interest
1714 */
1715int cgroup_taskset_size(struct cgroup_taskset *tset)
1716{
1717 return tset->tc_array ? tset->tc_array_len : 1;
1718}
1719EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1720
1721
74a1166d
BB
1722/*
1723 * cgroup_task_migrate - move a task from one cgroup to another.
1724 *
d0b2fdd2 1725 * Must be called with cgroup_mutex and threadgroup locked.
74a1166d 1726 */
5abb8855
TH
1727static void cgroup_task_migrate(struct cgroup *old_cgrp,
1728 struct task_struct *tsk,
1729 struct css_set *new_cset)
74a1166d 1730{
5abb8855 1731 struct css_set *old_cset;
74a1166d
BB
1732
1733 /*
026085ef
MSB
1734 * We are synchronized through threadgroup_lock() against PF_EXITING
1735 * setting such that we can't race against cgroup_exit() changing the
1736 * css_set to init_css_set and dropping the old one.
74a1166d 1737 */
c84cdf75 1738 WARN_ON_ONCE(tsk->flags & PF_EXITING);
a8ad805c 1739 old_cset = task_css_set(tsk);
74a1166d 1740
74a1166d 1741 task_lock(tsk);
5abb8855 1742 rcu_assign_pointer(tsk->cgroups, new_cset);
74a1166d
BB
1743 task_unlock(tsk);
1744
96d365e0 1745 down_write(&css_set_rwsem);
56fde9e0 1746 list_move(&tsk->cg_list, &new_cset->tasks);
96d365e0 1747 up_write(&css_set_rwsem);
74a1166d
BB
1748
1749 /*
5abb8855
TH
1750 * We just gained a reference on old_cset by taking it from the
1751 * task. As trading it for new_cset is protected by cgroup_mutex,
1752 * we're safe to drop it here; it will be freed under RCU.
74a1166d 1753 */
5abb8855
TH
1754 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1755 put_css_set(old_cset);
74a1166d
BB
1756}
1757
a043e3b2 1758/**
081aa458 1759 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
74a1166d 1760 * @cgrp: the cgroup to attach to
081aa458
LZ
1761 * @tsk: the task or the leader of the threadgroup to be attached
1762 * @threadgroup: attach the whole threadgroup?
74a1166d 1763 *
257058ae 1764 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
081aa458 1765 * task_lock of @tsk or each thread in the threadgroup individually in turn.
74a1166d 1766 */
47cfcd09
TH
1767static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1768 bool threadgroup)
74a1166d
BB
1769{
1770 int retval, i, group_size;
74a1166d 1771 struct cgroupfs_root *root = cgrp->root;
1c6727af 1772 struct cgroup_subsys_state *css, *failed_css = NULL;
74a1166d 1773 /* threadgroup list cursor and array */
081aa458 1774 struct task_struct *leader = tsk;
134d3373 1775 struct task_and_cgroup *tc;
d846687d 1776 struct flex_array *group;
2f7ee569 1777 struct cgroup_taskset tset = { };
74a1166d
BB
1778
1779 /*
1780 * step 0: in order to do expensive, possibly blocking operations for
1781 * every thread, we cannot iterate the thread group list, since it needs
1782 * rcu or tasklist locked. instead, build an array of all threads in the
257058ae
TH
1783 * group - group_rwsem prevents new threads from appearing, and if
1784 * threads exit, this will just be an over-estimate.
74a1166d 1785 */
081aa458
LZ
1786 if (threadgroup)
1787 group_size = get_nr_threads(tsk);
1788 else
1789 group_size = 1;
d846687d 1790 /* flex_array supports very large thread-groups better than kmalloc. */
134d3373 1791 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
74a1166d
BB
1792 if (!group)
1793 return -ENOMEM;
d846687d 1794 /* pre-allocate to guarantee space while iterating in rcu read-side. */
3ac1707a 1795 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
d846687d
BB
1796 if (retval)
1797 goto out_free_group_list;
74a1166d 1798
74a1166d 1799 i = 0;
fb5d2b4c
MSB
1800 /*
1801 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1802 * already PF_EXITING could be freed from underneath us unless we
1803 * take an rcu_read_lock.
1804 */
96d365e0 1805 down_read(&css_set_rwsem);
fb5d2b4c 1806 rcu_read_lock();
74a1166d 1807 do {
134d3373
TH
1808 struct task_and_cgroup ent;
1809
cd3d0952
TH
1810 /* @tsk either already exited or can't exit until the end */
1811 if (tsk->flags & PF_EXITING)
ea84753c 1812 goto next;
cd3d0952 1813
74a1166d
BB
1814 /* as per above, nr_threads may decrease, but not increase. */
1815 BUG_ON(i >= group_size);
134d3373
TH
1816 ent.task = tsk;
1817 ent.cgrp = task_cgroup_from_root(tsk, root);
892a2b90
MSB
1818 /* nothing to do if this task is already in the cgroup */
1819 if (ent.cgrp == cgrp)
ea84753c 1820 goto next;
61d1d219
MSB
1821 /*
1822 * saying GFP_ATOMIC has no effect here because we did prealloc
1823 * earlier, but it's good form to communicate our expectations.
1824 */
134d3373 1825 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
d846687d 1826 BUG_ON(retval != 0);
74a1166d 1827 i++;
ea84753c 1828 next:
081aa458
LZ
1829 if (!threadgroup)
1830 break;
74a1166d 1831 } while_each_thread(leader, tsk);
fb5d2b4c 1832 rcu_read_unlock();
96d365e0 1833 up_read(&css_set_rwsem);
74a1166d
BB
1834 /* remember the number of threads in the array for later. */
1835 group_size = i;
2f7ee569
TH
1836 tset.tc_array = group;
1837 tset.tc_array_len = group_size;
74a1166d 1838
134d3373
TH
1839 /* methods shouldn't be called if no task is actually migrating */
1840 retval = 0;
892a2b90 1841 if (!group_size)
b07ef774 1842 goto out_free_group_list;
134d3373 1843
74a1166d
BB
1844 /*
1845 * step 1: check that we can legitimately attach to the cgroup.
1846 */
1c6727af
TH
1847 for_each_css(css, i, cgrp) {
1848 if (css->ss->can_attach) {
1849 retval = css->ss->can_attach(css, &tset);
74a1166d 1850 if (retval) {
1c6727af 1851 failed_css = css;
74a1166d
BB
1852 goto out_cancel_attach;
1853 }
1854 }
74a1166d
BB
1855 }
1856
1857 /*
1858 * step 2: make sure css_sets exist for all threads to be migrated.
1859 * we use find_css_set, which allocates a new one if necessary.
1860 */
74a1166d 1861 for (i = 0; i < group_size; i++) {
a8ad805c
TH
1862 struct css_set *old_cset;
1863
134d3373 1864 tc = flex_array_get(group, i);
a8ad805c 1865 old_cset = task_css_set(tc->task);
6f4b7e63
LZ
1866 tc->cset = find_css_set(old_cset, cgrp);
1867 if (!tc->cset) {
61d1d219
MSB
1868 retval = -ENOMEM;
1869 goto out_put_css_set_refs;
74a1166d
BB
1870 }
1871 }
1872
1873 /*
494c167c
TH
1874 * step 3: now that we're guaranteed success wrt the css_sets,
1875 * proceed to move all tasks to the new cgroup. There are no
1876 * failure cases after here, so this is the commit point.
74a1166d 1877 */
74a1166d 1878 for (i = 0; i < group_size; i++) {
134d3373 1879 tc = flex_array_get(group, i);
6f4b7e63 1880 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
74a1166d
BB
1881 }
1882 /* nothing is sensitive to fork() after this point. */
1883
1884 /*
494c167c 1885 * step 4: do subsystem attach callbacks.
74a1166d 1886 */
1c6727af
TH
1887 for_each_css(css, i, cgrp)
1888 if (css->ss->attach)
1889 css->ss->attach(css, &tset);
74a1166d
BB
1890
1891 /*
1892 * step 5: success! and cleanup
1893 */
74a1166d 1894 retval = 0;
61d1d219
MSB
1895out_put_css_set_refs:
1896 if (retval) {
1897 for (i = 0; i < group_size; i++) {
1898 tc = flex_array_get(group, i);
6f4b7e63 1899 if (!tc->cset)
61d1d219 1900 break;
6f4b7e63 1901 put_css_set(tc->cset);
61d1d219 1902 }
74a1166d
BB
1903 }
1904out_cancel_attach:
74a1166d 1905 if (retval) {
1c6727af
TH
1906 for_each_css(css, i, cgrp) {
1907 if (css == failed_css)
74a1166d 1908 break;
1c6727af
TH
1909 if (css->ss->cancel_attach)
1910 css->ss->cancel_attach(css, &tset);
74a1166d
BB
1911 }
1912 }
74a1166d 1913out_free_group_list:
d846687d 1914 flex_array_free(group);
74a1166d
BB
1915 return retval;
1916}
1917
1918/*
1919 * Find the task_struct of the task to attach by vpid and pass it along to the
cd3d0952
TH
1920 * function to attach either it or all tasks in its threadgroup. Will lock
1921 * cgroup_mutex and threadgroup; may take task_lock of task.
bbcb81d0 1922 */
74a1166d 1923static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
bbcb81d0 1924{
bbcb81d0 1925 struct task_struct *tsk;
c69e8d9c 1926 const struct cred *cred = current_cred(), *tcred;
bbcb81d0
PM
1927 int ret;
1928
74a1166d
BB
1929 if (!cgroup_lock_live_group(cgrp))
1930 return -ENODEV;
1931
b78949eb
MSB
1932retry_find_task:
1933 rcu_read_lock();
bbcb81d0 1934 if (pid) {
73507f33 1935 tsk = find_task_by_vpid(pid);
74a1166d
BB
1936 if (!tsk) {
1937 rcu_read_unlock();
dd4b0a46 1938 ret = -ESRCH;
b78949eb 1939 goto out_unlock_cgroup;
bbcb81d0 1940 }
74a1166d
BB
1941 /*
1942 * even if we're attaching all tasks in the thread group, we
1943 * only need to check permissions on one of them.
1944 */
c69e8d9c 1945 tcred = __task_cred(tsk);
14a590c3
EB
1946 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1947 !uid_eq(cred->euid, tcred->uid) &&
1948 !uid_eq(cred->euid, tcred->suid)) {
c69e8d9c 1949 rcu_read_unlock();
b78949eb
MSB
1950 ret = -EACCES;
1951 goto out_unlock_cgroup;
bbcb81d0 1952 }
b78949eb
MSB
1953 } else
1954 tsk = current;
cd3d0952
TH
1955
1956 if (threadgroup)
b78949eb 1957 tsk = tsk->group_leader;
c4c27fbd
MG
1958
1959 /*
14a40ffc 1960 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
c4c27fbd
MG
1961 * trapped in a cpuset, or RT worker may be born in a cgroup
1962 * with no rt_runtime allocated. Just say no.
1963 */
14a40ffc 1964 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
c4c27fbd
MG
1965 ret = -EINVAL;
1966 rcu_read_unlock();
1967 goto out_unlock_cgroup;
1968 }
1969
b78949eb
MSB
1970 get_task_struct(tsk);
1971 rcu_read_unlock();
1972
1973 threadgroup_lock(tsk);
1974 if (threadgroup) {
1975 if (!thread_group_leader(tsk)) {
1976 /*
1977 * a race with de_thread from another thread's exec()
1978 * may strip us of our leadership, if this happens,
1979 * there is no choice but to throw this task away and
1980 * try again; this is
1981 * "double-double-toil-and-trouble-check locking".
1982 */
1983 threadgroup_unlock(tsk);
1984 put_task_struct(tsk);
1985 goto retry_find_task;
1986 }
081aa458
LZ
1987 }
1988
1989 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
1990
cd3d0952
TH
1991 threadgroup_unlock(tsk);
1992
bbcb81d0 1993 put_task_struct(tsk);
b78949eb 1994out_unlock_cgroup:
47cfcd09 1995 mutex_unlock(&cgroup_mutex);
bbcb81d0
PM
1996 return ret;
1997}
1998
7ae1bad9
TH
1999/**
2000 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2001 * @from: attach to all cgroups of a given task
2002 * @tsk: the task to be attached
2003 */
2004int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2005{
2006 struct cgroupfs_root *root;
2007 int retval = 0;
2008
47cfcd09 2009 mutex_lock(&cgroup_mutex);
7ae1bad9 2010 for_each_active_root(root) {
96d365e0
TH
2011 struct cgroup *from_cgrp;
2012
2013 down_read(&css_set_rwsem);
2014 from_cgrp = task_cgroup_from_root(from, root);
2015 up_read(&css_set_rwsem);
7ae1bad9 2016
6f4b7e63 2017 retval = cgroup_attach_task(from_cgrp, tsk, false);
7ae1bad9
TH
2018 if (retval)
2019 break;
2020 }
47cfcd09 2021 mutex_unlock(&cgroup_mutex);
7ae1bad9
TH
2022
2023 return retval;
2024}
2025EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2026
182446d0
TH
2027static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2028 struct cftype *cft, u64 pid)
74a1166d 2029{
182446d0 2030 return attach_task_by_pid(css->cgroup, pid, false);
74a1166d
BB
2031}
2032
182446d0
TH
2033static int cgroup_procs_write(struct cgroup_subsys_state *css,
2034 struct cftype *cft, u64 tgid)
af351026 2035{
182446d0 2036 return attach_task_by_pid(css->cgroup, tgid, true);
af351026
PM
2037}
2038
182446d0
TH
2039static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2040 struct cftype *cft, const char *buffer)
e788e066 2041{
5f469907
TH
2042 struct cgroupfs_root *root = css->cgroup->root;
2043
2044 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
182446d0 2045 if (!cgroup_lock_live_group(css->cgroup))
e788e066 2046 return -ENODEV;
69e943b7 2047 spin_lock(&release_agent_path_lock);
5f469907
TH
2048 strlcpy(root->release_agent_path, buffer,
2049 sizeof(root->release_agent_path));
69e943b7 2050 spin_unlock(&release_agent_path_lock);
47cfcd09 2051 mutex_unlock(&cgroup_mutex);
e788e066
PM
2052 return 0;
2053}
2054
2da8ca82 2055static int cgroup_release_agent_show(struct seq_file *seq, void *v)
e788e066 2056{
2da8ca82 2057 struct cgroup *cgrp = seq_css(seq)->cgroup;
182446d0 2058
e788e066
PM
2059 if (!cgroup_lock_live_group(cgrp))
2060 return -ENODEV;
2061 seq_puts(seq, cgrp->root->release_agent_path);
2062 seq_putc(seq, '\n');
47cfcd09 2063 mutex_unlock(&cgroup_mutex);
e788e066
PM
2064 return 0;
2065}
2066
2da8ca82 2067static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
873fe09e 2068{
2da8ca82
TH
2069 struct cgroup *cgrp = seq_css(seq)->cgroup;
2070
2071 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
e788e066
PM
2072 return 0;
2073}
2074
2bd59d48
TH
2075static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2076 size_t nbytes, loff_t off)
355e0c48 2077{
2bd59d48
TH
2078 struct cgroup *cgrp = of->kn->parent->priv;
2079 struct cftype *cft = of->kn->priv;
2080 struct cgroup_subsys_state *css;
a742c59d 2081 int ret;
355e0c48 2082
2bd59d48
TH
2083 /*
2084 * kernfs guarantees that a file isn't deleted with operations in
2085 * flight, which means that the matching css is and stays alive and
2086 * doesn't need to be pinned. The RCU locking is not necessary
2087 * either. It's just for the convenience of using cgroup_css().
2088 */
2089 rcu_read_lock();
2090 css = cgroup_css(cgrp, cft->ss);
2091 rcu_read_unlock();
a742c59d
TH
2092
2093 if (cft->write_string) {
2094 ret = cft->write_string(css, cft, strstrip(buf));
2095 } else if (cft->write_u64) {
2096 unsigned long long v;
2097 ret = kstrtoull(buf, 0, &v);
2098 if (!ret)
2099 ret = cft->write_u64(css, cft, v);
2100 } else if (cft->write_s64) {
2101 long long v;
2102 ret = kstrtoll(buf, 0, &v);
2103 if (!ret)
2104 ret = cft->write_s64(css, cft, v);
2105 } else if (cft->trigger) {
2106 ret = cft->trigger(css, (unsigned int)cft->private);
e73d2c61 2107 } else {
a742c59d 2108 ret = -EINVAL;
e73d2c61 2109 }
2bd59d48 2110
a742c59d 2111 return ret ?: nbytes;
355e0c48
PM
2112}
2113
6612f05b 2114static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
db3b1497 2115{
2bd59d48 2116 return seq_cft(seq)->seq_start(seq, ppos);
db3b1497
PM
2117}
2118
6612f05b 2119static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
ddbcc7e8 2120{
2bd59d48 2121 return seq_cft(seq)->seq_next(seq, v, ppos);
ddbcc7e8
PM
2122}
2123
6612f05b 2124static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
ddbcc7e8 2125{
2bd59d48 2126 seq_cft(seq)->seq_stop(seq, v);
ddbcc7e8
PM
2127}
2128
91796569 2129static int cgroup_seqfile_show(struct seq_file *m, void *arg)
e73d2c61 2130{
7da11279
TH
2131 struct cftype *cft = seq_cft(m);
2132 struct cgroup_subsys_state *css = seq_css(m);
e73d2c61 2133
2da8ca82
TH
2134 if (cft->seq_show)
2135 return cft->seq_show(m, arg);
e73d2c61 2136
f4c753b7 2137 if (cft->read_u64)
896f5199
TH
2138 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2139 else if (cft->read_s64)
2140 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2141 else
2142 return -EINVAL;
2143 return 0;
91796569
PM
2144}
2145
2bd59d48
TH
2146static struct kernfs_ops cgroup_kf_single_ops = {
2147 .atomic_write_len = PAGE_SIZE,
2148 .write = cgroup_file_write,
2149 .seq_show = cgroup_seqfile_show,
91796569
PM
2150};
2151
2bd59d48
TH
2152static struct kernfs_ops cgroup_kf_ops = {
2153 .atomic_write_len = PAGE_SIZE,
2154 .write = cgroup_file_write,
2155 .seq_start = cgroup_seqfile_start,
2156 .seq_next = cgroup_seqfile_next,
2157 .seq_stop = cgroup_seqfile_stop,
2158 .seq_show = cgroup_seqfile_show,
2159};
ddbcc7e8
PM
2160
2161/*
2162 * cgroup_rename - Only allow simple rename of directories in place.
2163 */
2bd59d48
TH
2164static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2165 const char *new_name_str)
ddbcc7e8 2166{
2bd59d48 2167 struct cgroup *cgrp = kn->priv;
2bd59d48 2168 int ret;
65dff759 2169
2bd59d48 2170 if (kernfs_type(kn) != KERNFS_DIR)
ddbcc7e8 2171 return -ENOTDIR;
2bd59d48 2172 if (kn->parent != new_parent)
ddbcc7e8 2173 return -EIO;
65dff759 2174
6db8e85c
TH
2175 /*
2176 * This isn't a proper migration and its usefulness is very
2177 * limited. Disallow if sane_behavior.
2178 */
2179 if (cgroup_sane_behavior(cgrp))
2180 return -EPERM;
2181
2bd59d48
TH
2182 mutex_lock(&cgroup_tree_mutex);
2183 mutex_lock(&cgroup_mutex);
2184
2185 ret = kernfs_rename(kn, new_parent, new_name_str);
65dff759 2186
2bd59d48
TH
2187 mutex_unlock(&cgroup_mutex);
2188 mutex_unlock(&cgroup_tree_mutex);
2bd59d48 2189 return ret;
ddbcc7e8
PM
2190}
2191
2bb566cb 2192static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
ddbcc7e8 2193{
8d7e6fb0 2194 char name[CGROUP_FILE_NAME_MAX];
2bd59d48
TH
2195 struct kernfs_node *kn;
2196 struct lock_class_key *key = NULL;
05ef1d7c 2197
2bd59d48
TH
2198#ifdef CONFIG_DEBUG_LOCK_ALLOC
2199 key = &cft->lockdep_key;
2200#endif
2201 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2202 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2203 NULL, false, key);
2204 if (IS_ERR(kn))
2205 return PTR_ERR(kn);
2206 return 0;
ddbcc7e8
PM
2207}
2208
b1f28d31
TH
2209/**
2210 * cgroup_addrm_files - add or remove files to a cgroup directory
2211 * @cgrp: the target cgroup
b1f28d31
TH
2212 * @cfts: array of cftypes to be added
2213 * @is_add: whether to add or remove
2214 *
2215 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2bb566cb
TH
2216 * For removals, this function never fails. If addition fails, this
2217 * function doesn't remove files already added. The caller is responsible
2218 * for cleaning up.
b1f28d31 2219 */
2bb566cb
TH
2220static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2221 bool is_add)
ddbcc7e8 2222{
03b1cde6 2223 struct cftype *cft;
b1f28d31
TH
2224 int ret;
2225
ace2bee8 2226 lockdep_assert_held(&cgroup_tree_mutex);
db0416b6
TH
2227
2228 for (cft = cfts; cft->name[0] != '\0'; cft++) {
f33fddc2 2229 /* does cft->flags tell us to skip this file on @cgrp? */
873fe09e
TH
2230 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2231 continue;
f33fddc2
G
2232 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2233 continue;
2234 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2235 continue;
2236
2739d3cc 2237 if (is_add) {
2bb566cb 2238 ret = cgroup_add_file(cgrp, cft);
b1f28d31 2239 if (ret) {
2739d3cc 2240 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
b1f28d31
TH
2241 cft->name, ret);
2242 return ret;
2243 }
2739d3cc
LZ
2244 } else {
2245 cgroup_rm_file(cgrp, cft);
db0416b6 2246 }
ddbcc7e8 2247 }
b1f28d31 2248 return 0;
ddbcc7e8
PM
2249}
2250
21a2d343 2251static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
8e3f6541
TH
2252{
2253 LIST_HEAD(pending);
2bb566cb 2254 struct cgroup_subsys *ss = cfts[0].ss;
492eb21b 2255 struct cgroup *root = &ss->root->top_cgroup;
492eb21b 2256 struct cgroup_subsys_state *css;
9ccece80 2257 int ret = 0;
8e3f6541 2258
21a2d343 2259 lockdep_assert_held(&cgroup_tree_mutex);
4ac06017 2260
21a2d343
TH
2261 /* don't bother if @ss isn't attached */
2262 if (ss->root == &cgroup_dummy_root)
9ccece80 2263 return 0;
e8c82d20 2264
e8c82d20 2265 /* add/rm files for all cgroups created before */
ca8bdcaf 2266 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
492eb21b
TH
2267 struct cgroup *cgrp = css->cgroup;
2268
e8c82d20
LZ
2269 if (cgroup_is_dead(cgrp))
2270 continue;
2271
21a2d343 2272 ret = cgroup_addrm_files(cgrp, cfts, is_add);
9ccece80
TH
2273 if (ret)
2274 break;
8e3f6541 2275 }
21a2d343
TH
2276
2277 if (is_add && !ret)
2278 kernfs_activate(root->kn);
9ccece80 2279 return ret;
8e3f6541
TH
2280}
2281
2da440a2
TH
2282static void cgroup_exit_cftypes(struct cftype *cfts)
2283{
2284 struct cftype *cft;
2285
2bd59d48
TH
2286 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2287 /* free copy for custom atomic_write_len, see init_cftypes() */
2288 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2289 kfree(cft->kf_ops);
2290 cft->kf_ops = NULL;
2da440a2 2291 cft->ss = NULL;
2bd59d48 2292 }
2da440a2
TH
2293}
2294
2bd59d48 2295static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2da440a2
TH
2296{
2297 struct cftype *cft;
2298
2bd59d48
TH
2299 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2300 struct kernfs_ops *kf_ops;
2301
0adb0704
TH
2302 WARN_ON(cft->ss || cft->kf_ops);
2303
2bd59d48
TH
2304 if (cft->seq_start)
2305 kf_ops = &cgroup_kf_ops;
2306 else
2307 kf_ops = &cgroup_kf_single_ops;
2308
2309 /*
2310 * Ugh... if @cft wants a custom max_write_len, we need to
2311 * make a copy of kf_ops to set its atomic_write_len.
2312 */
2313 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2314 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2315 if (!kf_ops) {
2316 cgroup_exit_cftypes(cfts);
2317 return -ENOMEM;
2318 }
2319 kf_ops->atomic_write_len = cft->max_write_len;
2320 }
2321
2322 cft->kf_ops = kf_ops;
2da440a2 2323 cft->ss = ss;
2bd59d48
TH
2324 }
2325
2326 return 0;
2da440a2
TH
2327}
2328
21a2d343
TH
2329static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2330{
2331 lockdep_assert_held(&cgroup_tree_mutex);
2332
2333 if (!cfts || !cfts[0].ss)
2334 return -ENOENT;
2335
2336 list_del(&cfts->node);
2337 cgroup_apply_cftypes(cfts, false);
2338 cgroup_exit_cftypes(cfts);
2339 return 0;
2340}
2341
80b13586
TH
2342/**
2343 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2344 * @cfts: zero-length name terminated array of cftypes
2345 *
2346 * Unregister @cfts. Files described by @cfts are removed from all
2347 * existing cgroups and all future cgroups won't have them either. This
2348 * function can be called anytime whether @cfts' subsys is attached or not.
2349 *
2350 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2351 * registered.
2352 */
2353int cgroup_rm_cftypes(struct cftype *cfts)
2354{
21a2d343 2355 int ret;
80b13586 2356
21a2d343
TH
2357 mutex_lock(&cgroup_tree_mutex);
2358 ret = cgroup_rm_cftypes_locked(cfts);
2359 mutex_unlock(&cgroup_tree_mutex);
2360 return ret;
80b13586
TH
2361}
2362
8e3f6541
TH
2363/**
2364 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2365 * @ss: target cgroup subsystem
2366 * @cfts: zero-length name terminated array of cftypes
2367 *
2368 * Register @cfts to @ss. Files described by @cfts are created for all
2369 * existing cgroups to which @ss is attached and all future cgroups will
2370 * have them too. This function can be called anytime whether @ss is
2371 * attached or not.
2372 *
2373 * Returns 0 on successful registration, -errno on failure. Note that this
2374 * function currently returns 0 as long as @cfts registration is successful
2375 * even if some file creation attempts on existing cgroups fail.
2376 */
03b1cde6 2377int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
8e3f6541 2378{
9ccece80 2379 int ret;
8e3f6541 2380
2bd59d48
TH
2381 ret = cgroup_init_cftypes(ss, cfts);
2382 if (ret)
2383 return ret;
2bb566cb 2384
21a2d343
TH
2385 mutex_lock(&cgroup_tree_mutex);
2386
0adb0704 2387 list_add_tail(&cfts->node, &ss->cfts);
21a2d343 2388 ret = cgroup_apply_cftypes(cfts, true);
9ccece80 2389 if (ret)
21a2d343
TH
2390 cgroup_rm_cftypes_locked(cfts);
2391
2392 mutex_unlock(&cgroup_tree_mutex);
9ccece80 2393 return ret;
8e3f6541
TH
2394}
2395EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2396
a043e3b2
LZ
2397/**
2398 * cgroup_task_count - count the number of tasks in a cgroup.
2399 * @cgrp: the cgroup in question
2400 *
2401 * Return the number of tasks in the cgroup.
2402 */
07bc356e 2403static int cgroup_task_count(const struct cgroup *cgrp)
bbcb81d0
PM
2404{
2405 int count = 0;
69d0206c 2406 struct cgrp_cset_link *link;
817929ec 2407
96d365e0 2408 down_read(&css_set_rwsem);
69d0206c
TH
2409 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2410 count += atomic_read(&link->cset->refcount);
96d365e0 2411 up_read(&css_set_rwsem);
bbcb81d0
PM
2412 return count;
2413}
2414
53fa5261 2415/**
492eb21b
TH
2416 * css_next_child - find the next child of a given css
2417 * @pos_css: the current position (%NULL to initiate traversal)
2418 * @parent_css: css whose children to walk
53fa5261 2419 *
492eb21b 2420 * This function returns the next child of @parent_css and should be called
87fb54f1
TH
2421 * under either cgroup_mutex or RCU read lock. The only requirement is
2422 * that @parent_css and @pos_css are accessible. The next sibling is
2423 * guaranteed to be returned regardless of their states.
53fa5261 2424 */
492eb21b
TH
2425struct cgroup_subsys_state *
2426css_next_child(struct cgroup_subsys_state *pos_css,
2427 struct cgroup_subsys_state *parent_css)
53fa5261 2428{
492eb21b
TH
2429 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2430 struct cgroup *cgrp = parent_css->cgroup;
53fa5261
TH
2431 struct cgroup *next;
2432
ace2bee8 2433 cgroup_assert_mutexes_or_rcu_locked();
53fa5261
TH
2434
2435 /*
2436 * @pos could already have been removed. Once a cgroup is removed,
2437 * its ->sibling.next is no longer updated when its next sibling
ea15f8cc
TH
2438 * changes. As CGRP_DEAD assertion is serialized and happens
2439 * before the cgroup is taken off the ->sibling list, if we see it
2440 * unasserted, it's guaranteed that the next sibling hasn't
2441 * finished its grace period even if it's already removed, and thus
2442 * safe to dereference from this RCU critical section. If
2443 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2444 * to be visible as %true here.
3b287a50
TH
2445 *
2446 * If @pos is dead, its next pointer can't be dereferenced;
2447 * however, as each cgroup is given a monotonically increasing
2448 * unique serial number and always appended to the sibling list,
2449 * the next one can be found by walking the parent's children until
2450 * we see a cgroup with higher serial number than @pos's. While
2451 * this path can be slower, it's taken only when either the current
2452 * cgroup is removed or iteration and removal race.
53fa5261 2453 */
3b287a50
TH
2454 if (!pos) {
2455 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2456 } else if (likely(!cgroup_is_dead(pos))) {
53fa5261 2457 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3b287a50
TH
2458 } else {
2459 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2460 if (next->serial_nr > pos->serial_nr)
2461 break;
53fa5261
TH
2462 }
2463
492eb21b
TH
2464 if (&next->sibling == &cgrp->children)
2465 return NULL;
2466
ca8bdcaf 2467 return cgroup_css(next, parent_css->ss);
53fa5261 2468}
492eb21b 2469EXPORT_SYMBOL_GPL(css_next_child);
53fa5261 2470
574bd9f7 2471/**
492eb21b 2472 * css_next_descendant_pre - find the next descendant for pre-order walk
574bd9f7 2473 * @pos: the current position (%NULL to initiate traversal)
492eb21b 2474 * @root: css whose descendants to walk
574bd9f7 2475 *
492eb21b 2476 * To be used by css_for_each_descendant_pre(). Find the next descendant
bd8815a6
TH
2477 * to visit for pre-order traversal of @root's descendants. @root is
2478 * included in the iteration and the first node to be visited.
75501a6d 2479 *
87fb54f1
TH
2480 * While this function requires cgroup_mutex or RCU read locking, it
2481 * doesn't require the whole traversal to be contained in a single critical
2482 * section. This function will return the correct next descendant as long
2483 * as both @pos and @root are accessible and @pos is a descendant of @root.
574bd9f7 2484 */
492eb21b
TH
2485struct cgroup_subsys_state *
2486css_next_descendant_pre(struct cgroup_subsys_state *pos,
2487 struct cgroup_subsys_state *root)
574bd9f7 2488{
492eb21b 2489 struct cgroup_subsys_state *next;
574bd9f7 2490
ace2bee8 2491 cgroup_assert_mutexes_or_rcu_locked();
574bd9f7 2492
bd8815a6 2493 /* if first iteration, visit @root */
7805d000 2494 if (!pos)
bd8815a6 2495 return root;
574bd9f7
TH
2496
2497 /* visit the first child if exists */
492eb21b 2498 next = css_next_child(NULL, pos);
574bd9f7
TH
2499 if (next)
2500 return next;
2501
2502 /* no child, visit my or the closest ancestor's next sibling */
492eb21b
TH
2503 while (pos != root) {
2504 next = css_next_child(pos, css_parent(pos));
75501a6d 2505 if (next)
574bd9f7 2506 return next;
492eb21b 2507 pos = css_parent(pos);
7805d000 2508 }
574bd9f7
TH
2509
2510 return NULL;
2511}
492eb21b 2512EXPORT_SYMBOL_GPL(css_next_descendant_pre);
574bd9f7 2513
12a9d2fe 2514/**
492eb21b
TH
2515 * css_rightmost_descendant - return the rightmost descendant of a css
2516 * @pos: css of interest
12a9d2fe 2517 *
492eb21b
TH
2518 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2519 * is returned. This can be used during pre-order traversal to skip
12a9d2fe 2520 * subtree of @pos.
75501a6d 2521 *
87fb54f1
TH
2522 * While this function requires cgroup_mutex or RCU read locking, it
2523 * doesn't require the whole traversal to be contained in a single critical
2524 * section. This function will return the correct rightmost descendant as
2525 * long as @pos is accessible.
12a9d2fe 2526 */
492eb21b
TH
2527struct cgroup_subsys_state *
2528css_rightmost_descendant(struct cgroup_subsys_state *pos)
12a9d2fe 2529{
492eb21b 2530 struct cgroup_subsys_state *last, *tmp;
12a9d2fe 2531
ace2bee8 2532 cgroup_assert_mutexes_or_rcu_locked();
12a9d2fe
TH
2533
2534 do {
2535 last = pos;
2536 /* ->prev isn't RCU safe, walk ->next till the end */
2537 pos = NULL;
492eb21b 2538 css_for_each_child(tmp, last)
12a9d2fe
TH
2539 pos = tmp;
2540 } while (pos);
2541
2542 return last;
2543}
492eb21b 2544EXPORT_SYMBOL_GPL(css_rightmost_descendant);
12a9d2fe 2545
492eb21b
TH
2546static struct cgroup_subsys_state *
2547css_leftmost_descendant(struct cgroup_subsys_state *pos)
574bd9f7 2548{
492eb21b 2549 struct cgroup_subsys_state *last;
574bd9f7
TH
2550
2551 do {
2552 last = pos;
492eb21b 2553 pos = css_next_child(NULL, pos);
574bd9f7
TH
2554 } while (pos);
2555
2556 return last;
2557}
2558
2559/**
492eb21b 2560 * css_next_descendant_post - find the next descendant for post-order walk
574bd9f7 2561 * @pos: the current position (%NULL to initiate traversal)
492eb21b 2562 * @root: css whose descendants to walk
574bd9f7 2563 *
492eb21b 2564 * To be used by css_for_each_descendant_post(). Find the next descendant
bd8815a6
TH
2565 * to visit for post-order traversal of @root's descendants. @root is
2566 * included in the iteration and the last node to be visited.
75501a6d 2567 *
87fb54f1
TH
2568 * While this function requires cgroup_mutex or RCU read locking, it
2569 * doesn't require the whole traversal to be contained in a single critical
2570 * section. This function will return the correct next descendant as long
2571 * as both @pos and @cgroup are accessible and @pos is a descendant of
2572 * @cgroup.
574bd9f7 2573 */
492eb21b
TH
2574struct cgroup_subsys_state *
2575css_next_descendant_post(struct cgroup_subsys_state *pos,
2576 struct cgroup_subsys_state *root)
574bd9f7 2577{
492eb21b 2578 struct cgroup_subsys_state *next;
574bd9f7 2579
ace2bee8 2580 cgroup_assert_mutexes_or_rcu_locked();
574bd9f7 2581
58b79a91
TH
2582 /* if first iteration, visit leftmost descendant which may be @root */
2583 if (!pos)
2584 return css_leftmost_descendant(root);
574bd9f7 2585
bd8815a6
TH
2586 /* if we visited @root, we're done */
2587 if (pos == root)
2588 return NULL;
2589
574bd9f7 2590 /* if there's an unvisited sibling, visit its leftmost descendant */
492eb21b 2591 next = css_next_child(pos, css_parent(pos));
75501a6d 2592 if (next)
492eb21b 2593 return css_leftmost_descendant(next);
574bd9f7
TH
2594
2595 /* no sibling left, visit parent */
bd8815a6 2596 return css_parent(pos);
574bd9f7 2597}
492eb21b 2598EXPORT_SYMBOL_GPL(css_next_descendant_post);
574bd9f7 2599
0942eeee 2600/**
72ec7029 2601 * css_advance_task_iter - advance a task itererator to the next css_set
0942eeee
TH
2602 * @it: the iterator to advance
2603 *
2604 * Advance @it to the next css_set to walk.
d515876e 2605 */
72ec7029 2606static void css_advance_task_iter(struct css_task_iter *it)
d515876e
TH
2607{
2608 struct list_head *l = it->cset_link;
2609 struct cgrp_cset_link *link;
2610 struct css_set *cset;
2611
2612 /* Advance to the next non-empty css_set */
2613 do {
2614 l = l->next;
72ec7029 2615 if (l == &it->origin_css->cgroup->cset_links) {
d515876e
TH
2616 it->cset_link = NULL;
2617 return;
2618 }
2619 link = list_entry(l, struct cgrp_cset_link, cset_link);
2620 cset = link->cset;
2621 } while (list_empty(&cset->tasks));
2622 it->cset_link = l;
2623 it->task = cset->tasks.next;
2624}
2625
0942eeee 2626/**
72ec7029
TH
2627 * css_task_iter_start - initiate task iteration
2628 * @css: the css to walk tasks of
0942eeee
TH
2629 * @it: the task iterator to use
2630 *
72ec7029
TH
2631 * Initiate iteration through the tasks of @css. The caller can call
2632 * css_task_iter_next() to walk through the tasks until the function
2633 * returns NULL. On completion of iteration, css_task_iter_end() must be
2634 * called.
0942eeee
TH
2635 *
2636 * Note that this function acquires a lock which is released when the
2637 * iteration finishes. The caller can't sleep while iteration is in
2638 * progress.
2639 */
72ec7029
TH
2640void css_task_iter_start(struct cgroup_subsys_state *css,
2641 struct css_task_iter *it)
96d365e0 2642 __acquires(css_set_rwsem)
817929ec 2643{
56fde9e0
TH
2644 /* no one should try to iterate before mounting cgroups */
2645 WARN_ON_ONCE(!use_task_css_set_links);
31a7df01 2646
96d365e0 2647 down_read(&css_set_rwsem);
c59cd3d8 2648
72ec7029
TH
2649 it->origin_css = css;
2650 it->cset_link = &css->cgroup->cset_links;
c59cd3d8 2651
72ec7029 2652 css_advance_task_iter(it);
817929ec
PM
2653}
2654
0942eeee 2655/**
72ec7029 2656 * css_task_iter_next - return the next task for the iterator
0942eeee
TH
2657 * @it: the task iterator being iterated
2658 *
2659 * The "next" function for task iteration. @it should have been
72ec7029
TH
2660 * initialized via css_task_iter_start(). Returns NULL when the iteration
2661 * reaches the end.
0942eeee 2662 */
72ec7029 2663struct task_struct *css_task_iter_next(struct css_task_iter *it)
817929ec
PM
2664{
2665 struct task_struct *res;
2666 struct list_head *l = it->task;
69d0206c 2667 struct cgrp_cset_link *link;
817929ec
PM
2668
2669 /* If the iterator cg is NULL, we have no tasks */
69d0206c 2670 if (!it->cset_link)
817929ec
PM
2671 return NULL;
2672 res = list_entry(l, struct task_struct, cg_list);
2673 /* Advance iterator to find next entry */
2674 l = l->next;
69d0206c
TH
2675 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2676 if (l == &link->cset->tasks) {
0942eeee
TH
2677 /*
2678 * We reached the end of this task list - move on to the
2679 * next cgrp_cset_link.
2680 */
72ec7029 2681 css_advance_task_iter(it);
817929ec
PM
2682 } else {
2683 it->task = l;
2684 }
2685 return res;
2686}
2687
0942eeee 2688/**
72ec7029 2689 * css_task_iter_end - finish task iteration
0942eeee
TH
2690 * @it: the task iterator to finish
2691 *
72ec7029 2692 * Finish task iteration started by css_task_iter_start().
0942eeee 2693 */
72ec7029 2694void css_task_iter_end(struct css_task_iter *it)
96d365e0 2695 __releases(css_set_rwsem)
817929ec 2696{
96d365e0 2697 up_read(&css_set_rwsem);
817929ec
PM
2698}
2699
8cc99345
TH
2700/**
2701 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2702 * @to: cgroup to which the tasks will be moved
2703 * @from: cgroup in which the tasks currently reside
2704 */
2705int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2706{
e406d1cf
TH
2707 struct css_task_iter it;
2708 struct task_struct *task;
2709 int ret = 0;
2710
2711 do {
2712 css_task_iter_start(&from->dummy_css, &it);
2713 task = css_task_iter_next(&it);
2714 if (task)
2715 get_task_struct(task);
2716 css_task_iter_end(&it);
2717
2718 if (task) {
2719 mutex_lock(&cgroup_mutex);
2720 ret = cgroup_attach_task(to, task, false);
2721 mutex_unlock(&cgroup_mutex);
2722 put_task_struct(task);
2723 }
2724 } while (task && !ret);
2725
2726 return ret;
8cc99345
TH
2727}
2728
bbcb81d0 2729/*
102a775e 2730 * Stuff for reading the 'tasks'/'procs' files.
bbcb81d0
PM
2731 *
2732 * Reading this file can return large amounts of data if a cgroup has
2733 * *lots* of attached tasks. So it may need several calls to read(),
2734 * but we cannot guarantee that the information we produce is correct
2735 * unless we produce it entirely atomically.
2736 *
bbcb81d0 2737 */
bbcb81d0 2738
24528255
LZ
2739/* which pidlist file are we talking about? */
2740enum cgroup_filetype {
2741 CGROUP_FILE_PROCS,
2742 CGROUP_FILE_TASKS,
2743};
2744
2745/*
2746 * A pidlist is a list of pids that virtually represents the contents of one
2747 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2748 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2749 * to the cgroup.
2750 */
2751struct cgroup_pidlist {
2752 /*
2753 * used to find which pidlist is wanted. doesn't change as long as
2754 * this particular list stays in the list.
2755 */
2756 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2757 /* array of xids */
2758 pid_t *list;
2759 /* how many elements the above list has */
2760 int length;
24528255
LZ
2761 /* each of these stored in a list by its cgroup */
2762 struct list_head links;
2763 /* pointer to the cgroup we belong to, for list removal purposes */
2764 struct cgroup *owner;
b1a21367
TH
2765 /* for delayed destruction */
2766 struct delayed_work destroy_dwork;
24528255
LZ
2767};
2768
d1d9fd33
BB
2769/*
2770 * The following two functions "fix" the issue where there are more pids
2771 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2772 * TODO: replace with a kernel-wide solution to this problem
2773 */
2774#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2775static void *pidlist_allocate(int count)
2776{
2777 if (PIDLIST_TOO_LARGE(count))
2778 return vmalloc(count * sizeof(pid_t));
2779 else
2780 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2781}
b1a21367 2782
d1d9fd33
BB
2783static void pidlist_free(void *p)
2784{
2785 if (is_vmalloc_addr(p))
2786 vfree(p);
2787 else
2788 kfree(p);
2789}
d1d9fd33 2790
b1a21367
TH
2791/*
2792 * Used to destroy all pidlists lingering waiting for destroy timer. None
2793 * should be left afterwards.
2794 */
2795static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2796{
2797 struct cgroup_pidlist *l, *tmp_l;
2798
2799 mutex_lock(&cgrp->pidlist_mutex);
2800 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2801 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2802 mutex_unlock(&cgrp->pidlist_mutex);
2803
2804 flush_workqueue(cgroup_pidlist_destroy_wq);
2805 BUG_ON(!list_empty(&cgrp->pidlists));
2806}
2807
2808static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2809{
2810 struct delayed_work *dwork = to_delayed_work(work);
2811 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2812 destroy_dwork);
2813 struct cgroup_pidlist *tofree = NULL;
2814
2815 mutex_lock(&l->owner->pidlist_mutex);
b1a21367
TH
2816
2817 /*
04502365
TH
2818 * Destroy iff we didn't get queued again. The state won't change
2819 * as destroy_dwork can only be queued while locked.
b1a21367 2820 */
04502365 2821 if (!delayed_work_pending(dwork)) {
b1a21367
TH
2822 list_del(&l->links);
2823 pidlist_free(l->list);
2824 put_pid_ns(l->key.ns);
2825 tofree = l;
2826 }
2827
b1a21367
TH
2828 mutex_unlock(&l->owner->pidlist_mutex);
2829 kfree(tofree);
2830}
2831
bbcb81d0 2832/*
102a775e 2833 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
6ee211ad 2834 * Returns the number of unique elements.
bbcb81d0 2835 */
6ee211ad 2836static int pidlist_uniq(pid_t *list, int length)
bbcb81d0 2837{
102a775e 2838 int src, dest = 1;
102a775e
BB
2839
2840 /*
2841 * we presume the 0th element is unique, so i starts at 1. trivial
2842 * edge cases first; no work needs to be done for either
2843 */
2844 if (length == 0 || length == 1)
2845 return length;
2846 /* src and dest walk down the list; dest counts unique elements */
2847 for (src = 1; src < length; src++) {
2848 /* find next unique element */
2849 while (list[src] == list[src-1]) {
2850 src++;
2851 if (src == length)
2852 goto after;
2853 }
2854 /* dest always points to where the next unique element goes */
2855 list[dest] = list[src];
2856 dest++;
2857 }
2858after:
102a775e
BB
2859 return dest;
2860}
2861
afb2bc14
TH
2862/*
2863 * The two pid files - task and cgroup.procs - guaranteed that the result
2864 * is sorted, which forced this whole pidlist fiasco. As pid order is
2865 * different per namespace, each namespace needs differently sorted list,
2866 * making it impossible to use, for example, single rbtree of member tasks
2867 * sorted by task pointer. As pidlists can be fairly large, allocating one
2868 * per open file is dangerous, so cgroup had to implement shared pool of
2869 * pidlists keyed by cgroup and namespace.
2870 *
2871 * All this extra complexity was caused by the original implementation
2872 * committing to an entirely unnecessary property. In the long term, we
2873 * want to do away with it. Explicitly scramble sort order if
2874 * sane_behavior so that no such expectation exists in the new interface.
2875 *
2876 * Scrambling is done by swapping every two consecutive bits, which is
2877 * non-identity one-to-one mapping which disturbs sort order sufficiently.
2878 */
2879static pid_t pid_fry(pid_t pid)
2880{
2881 unsigned a = pid & 0x55555555;
2882 unsigned b = pid & 0xAAAAAAAA;
2883
2884 return (a << 1) | (b >> 1);
2885}
2886
2887static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
2888{
2889 if (cgroup_sane_behavior(cgrp))
2890 return pid_fry(pid);
2891 else
2892 return pid;
2893}
2894
102a775e
BB
2895static int cmppid(const void *a, const void *b)
2896{
2897 return *(pid_t *)a - *(pid_t *)b;
2898}
2899
afb2bc14
TH
2900static int fried_cmppid(const void *a, const void *b)
2901{
2902 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
2903}
2904
e6b81710
TH
2905static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2906 enum cgroup_filetype type)
2907{
2908 struct cgroup_pidlist *l;
2909 /* don't need task_nsproxy() if we're looking at ourself */
2910 struct pid_namespace *ns = task_active_pid_ns(current);
2911
2912 lockdep_assert_held(&cgrp->pidlist_mutex);
2913
2914 list_for_each_entry(l, &cgrp->pidlists, links)
2915 if (l->key.type == type && l->key.ns == ns)
2916 return l;
2917 return NULL;
2918}
2919
72a8cb30
BB
2920/*
2921 * find the appropriate pidlist for our purpose (given procs vs tasks)
2922 * returns with the lock on that pidlist already held, and takes care
2923 * of the use count, or returns NULL with no locks held if we're out of
2924 * memory.
2925 */
e6b81710
TH
2926static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
2927 enum cgroup_filetype type)
72a8cb30
BB
2928{
2929 struct cgroup_pidlist *l;
b70cc5fd 2930
e6b81710
TH
2931 lockdep_assert_held(&cgrp->pidlist_mutex);
2932
2933 l = cgroup_pidlist_find(cgrp, type);
2934 if (l)
2935 return l;
2936
72a8cb30 2937 /* entry not found; create a new one */
f4f4be2b 2938 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
e6b81710 2939 if (!l)
72a8cb30 2940 return l;
e6b81710 2941
b1a21367 2942 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
72a8cb30 2943 l->key.type = type;
e6b81710
TH
2944 /* don't need task_nsproxy() if we're looking at ourself */
2945 l->key.ns = get_pid_ns(task_active_pid_ns(current));
72a8cb30
BB
2946 l->owner = cgrp;
2947 list_add(&l->links, &cgrp->pidlists);
72a8cb30
BB
2948 return l;
2949}
2950
102a775e
BB
2951/*
2952 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2953 */
72a8cb30
BB
2954static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2955 struct cgroup_pidlist **lp)
102a775e
BB
2956{
2957 pid_t *array;
2958 int length;
2959 int pid, n = 0; /* used for populating the array */
72ec7029 2960 struct css_task_iter it;
817929ec 2961 struct task_struct *tsk;
102a775e
BB
2962 struct cgroup_pidlist *l;
2963
4bac00d1
TH
2964 lockdep_assert_held(&cgrp->pidlist_mutex);
2965
102a775e
BB
2966 /*
2967 * If cgroup gets more users after we read count, we won't have
2968 * enough space - tough. This race is indistinguishable to the
2969 * caller from the case that the additional cgroup users didn't
2970 * show up until sometime later on.
2971 */
2972 length = cgroup_task_count(cgrp);
d1d9fd33 2973 array = pidlist_allocate(length);
102a775e
BB
2974 if (!array)
2975 return -ENOMEM;
2976 /* now, populate the array */
72ec7029
TH
2977 css_task_iter_start(&cgrp->dummy_css, &it);
2978 while ((tsk = css_task_iter_next(&it))) {
102a775e 2979 if (unlikely(n == length))
817929ec 2980 break;
102a775e 2981 /* get tgid or pid for procs or tasks file respectively */
72a8cb30
BB
2982 if (type == CGROUP_FILE_PROCS)
2983 pid = task_tgid_vnr(tsk);
2984 else
2985 pid = task_pid_vnr(tsk);
102a775e
BB
2986 if (pid > 0) /* make sure to only use valid results */
2987 array[n++] = pid;
817929ec 2988 }
72ec7029 2989 css_task_iter_end(&it);
102a775e
BB
2990 length = n;
2991 /* now sort & (if procs) strip out duplicates */
afb2bc14
TH
2992 if (cgroup_sane_behavior(cgrp))
2993 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
2994 else
2995 sort(array, length, sizeof(pid_t), cmppid, NULL);
72a8cb30 2996 if (type == CGROUP_FILE_PROCS)
6ee211ad 2997 length = pidlist_uniq(array, length);
e6b81710 2998
e6b81710 2999 l = cgroup_pidlist_find_create(cgrp, type);
72a8cb30 3000 if (!l) {
e6b81710 3001 mutex_unlock(&cgrp->pidlist_mutex);
d1d9fd33 3002 pidlist_free(array);
72a8cb30 3003 return -ENOMEM;
102a775e 3004 }
e6b81710
TH
3005
3006 /* store array, freeing old if necessary */
d1d9fd33 3007 pidlist_free(l->list);
102a775e
BB
3008 l->list = array;
3009 l->length = length;
72a8cb30 3010 *lp = l;
102a775e 3011 return 0;
bbcb81d0
PM
3012}
3013
846c7bb0 3014/**
a043e3b2 3015 * cgroupstats_build - build and fill cgroupstats
846c7bb0
BS
3016 * @stats: cgroupstats to fill information into
3017 * @dentry: A dentry entry belonging to the cgroup for which stats have
3018 * been requested.
a043e3b2
LZ
3019 *
3020 * Build and fill cgroupstats so that taskstats can export it to user
3021 * space.
846c7bb0
BS
3022 */
3023int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3024{
2bd59d48 3025 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
bd89aabc 3026 struct cgroup *cgrp;
72ec7029 3027 struct css_task_iter it;
846c7bb0 3028 struct task_struct *tsk;
33d283be 3029
2bd59d48
TH
3030 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3031 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3032 kernfs_type(kn) != KERNFS_DIR)
3033 return -EINVAL;
3034
846c7bb0 3035 /*
2bd59d48
TH
3036 * We aren't being called from kernfs and there's no guarantee on
3037 * @kn->priv's validity. For this and css_tryget_from_dir(),
3038 * @kn->priv is RCU safe. Let's do the RCU dancing.
846c7bb0 3039 */
2bd59d48
TH
3040 rcu_read_lock();
3041 cgrp = rcu_dereference(kn->priv);
3042 if (!cgrp) {
3043 rcu_read_unlock();
3044 return -ENOENT;
3045 }
846c7bb0 3046
72ec7029
TH
3047 css_task_iter_start(&cgrp->dummy_css, &it);
3048 while ((tsk = css_task_iter_next(&it))) {
846c7bb0
BS
3049 switch (tsk->state) {
3050 case TASK_RUNNING:
3051 stats->nr_running++;
3052 break;
3053 case TASK_INTERRUPTIBLE:
3054 stats->nr_sleeping++;
3055 break;
3056 case TASK_UNINTERRUPTIBLE:
3057 stats->nr_uninterruptible++;
3058 break;
3059 case TASK_STOPPED:
3060 stats->nr_stopped++;
3061 break;
3062 default:
3063 if (delayacct_is_task_waiting_on_io(tsk))
3064 stats->nr_io_wait++;
3065 break;
3066 }
3067 }
72ec7029 3068 css_task_iter_end(&it);
846c7bb0 3069
2bd59d48
TH
3070 rcu_read_unlock();
3071 return 0;
846c7bb0
BS
3072}
3073
8f3ff208 3074
bbcb81d0 3075/*
102a775e 3076 * seq_file methods for the tasks/procs files. The seq_file position is the
cc31edce 3077 * next pid to display; the seq_file iterator is a pointer to the pid
102a775e 3078 * in the cgroup->l->list array.
bbcb81d0 3079 */
cc31edce 3080
102a775e 3081static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
bbcb81d0 3082{
cc31edce
PM
3083 /*
3084 * Initially we receive a position value that corresponds to
3085 * one more than the last pid shown (or 0 on the first call or
3086 * after a seek to the start). Use a binary-search to find the
3087 * next pid to display, if any
3088 */
2bd59d48 3089 struct kernfs_open_file *of = s->private;
7da11279 3090 struct cgroup *cgrp = seq_css(s)->cgroup;
4bac00d1 3091 struct cgroup_pidlist *l;
7da11279 3092 enum cgroup_filetype type = seq_cft(s)->private;
cc31edce 3093 int index = 0, pid = *pos;
4bac00d1
TH
3094 int *iter, ret;
3095
3096 mutex_lock(&cgrp->pidlist_mutex);
3097
3098 /*
5d22444f 3099 * !NULL @of->priv indicates that this isn't the first start()
4bac00d1 3100 * after open. If the matching pidlist is around, we can use that.
5d22444f 3101 * Look for it. Note that @of->priv can't be used directly. It
4bac00d1
TH
3102 * could already have been destroyed.
3103 */
5d22444f
TH
3104 if (of->priv)
3105 of->priv = cgroup_pidlist_find(cgrp, type);
4bac00d1
TH
3106
3107 /*
3108 * Either this is the first start() after open or the matching
3109 * pidlist has been destroyed inbetween. Create a new one.
3110 */
5d22444f
TH
3111 if (!of->priv) {
3112 ret = pidlist_array_load(cgrp, type,
3113 (struct cgroup_pidlist **)&of->priv);
4bac00d1
TH
3114 if (ret)
3115 return ERR_PTR(ret);
3116 }
5d22444f 3117 l = of->priv;
cc31edce 3118
cc31edce 3119 if (pid) {
102a775e 3120 int end = l->length;
20777766 3121
cc31edce
PM
3122 while (index < end) {
3123 int mid = (index + end) / 2;
afb2bc14 3124 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
cc31edce
PM
3125 index = mid;
3126 break;
afb2bc14 3127 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
cc31edce
PM
3128 index = mid + 1;
3129 else
3130 end = mid;
3131 }
3132 }
3133 /* If we're off the end of the array, we're done */
102a775e 3134 if (index >= l->length)
cc31edce
PM
3135 return NULL;
3136 /* Update the abstract position to be the actual pid that we found */
102a775e 3137 iter = l->list + index;
afb2bc14 3138 *pos = cgroup_pid_fry(cgrp, *iter);
cc31edce
PM
3139 return iter;
3140}
3141
102a775e 3142static void cgroup_pidlist_stop(struct seq_file *s, void *v)
cc31edce 3143{
2bd59d48 3144 struct kernfs_open_file *of = s->private;
5d22444f 3145 struct cgroup_pidlist *l = of->priv;
62236858 3146
5d22444f
TH
3147 if (l)
3148 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
04502365 3149 CGROUP_PIDLIST_DESTROY_DELAY);
7da11279 3150 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
cc31edce
PM
3151}
3152
102a775e 3153static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
cc31edce 3154{
2bd59d48 3155 struct kernfs_open_file *of = s->private;
5d22444f 3156 struct cgroup_pidlist *l = of->priv;
102a775e
BB
3157 pid_t *p = v;
3158 pid_t *end = l->list + l->length;
cc31edce
PM
3159 /*
3160 * Advance to the next pid in the array. If this goes off the
3161 * end, we're done
3162 */
3163 p++;
3164 if (p >= end) {
3165 return NULL;
3166 } else {
7da11279 3167 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
cc31edce
PM
3168 return p;
3169 }
3170}
3171
102a775e 3172static int cgroup_pidlist_show(struct seq_file *s, void *v)
cc31edce
PM
3173{
3174 return seq_printf(s, "%d\n", *(int *)v);
3175}
bbcb81d0 3176
102a775e
BB
3177/*
3178 * seq_operations functions for iterating on pidlists through seq_file -
3179 * independent of whether it's tasks or procs
3180 */
3181static const struct seq_operations cgroup_pidlist_seq_operations = {
3182 .start = cgroup_pidlist_start,
3183 .stop = cgroup_pidlist_stop,
3184 .next = cgroup_pidlist_next,
3185 .show = cgroup_pidlist_show,
cc31edce
PM
3186};
3187
182446d0
TH
3188static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3189 struct cftype *cft)
81a6a5cd 3190{
182446d0 3191 return notify_on_release(css->cgroup);
81a6a5cd
PM
3192}
3193
182446d0
TH
3194static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3195 struct cftype *cft, u64 val)
6379c106 3196{
182446d0 3197 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
6379c106 3198 if (val)
182446d0 3199 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
6379c106 3200 else
182446d0 3201 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
6379c106
PM
3202 return 0;
3203}
3204
182446d0
TH
3205static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3206 struct cftype *cft)
97978e6d 3207{
182446d0 3208 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
97978e6d
DL
3209}
3210
182446d0
TH
3211static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3212 struct cftype *cft, u64 val)
97978e6d
DL
3213{
3214 if (val)
182446d0 3215 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
97978e6d 3216 else
182446d0 3217 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
97978e6d
DL
3218 return 0;
3219}
3220
d5c56ced 3221static struct cftype cgroup_base_files[] = {
81a6a5cd 3222 {
d5c56ced 3223 .name = "cgroup.procs",
6612f05b
TH
3224 .seq_start = cgroup_pidlist_start,
3225 .seq_next = cgroup_pidlist_next,
3226 .seq_stop = cgroup_pidlist_stop,
3227 .seq_show = cgroup_pidlist_show,
5d22444f 3228 .private = CGROUP_FILE_PROCS,
74a1166d 3229 .write_u64 = cgroup_procs_write,
74a1166d 3230 .mode = S_IRUGO | S_IWUSR,
102a775e 3231 },
97978e6d
DL
3232 {
3233 .name = "cgroup.clone_children",
873fe09e 3234 .flags = CFTYPE_INSANE,
97978e6d
DL
3235 .read_u64 = cgroup_clone_children_read,
3236 .write_u64 = cgroup_clone_children_write,
3237 },
873fe09e
TH
3238 {
3239 .name = "cgroup.sane_behavior",
3240 .flags = CFTYPE_ONLY_ON_ROOT,
2da8ca82 3241 .seq_show = cgroup_sane_behavior_show,
873fe09e 3242 },
d5c56ced
TH
3243
3244 /*
3245 * Historical crazy stuff. These don't have "cgroup." prefix and
3246 * don't exist if sane_behavior. If you're depending on these, be
3247 * prepared to be burned.
3248 */
3249 {
3250 .name = "tasks",
3251 .flags = CFTYPE_INSANE, /* use "procs" instead */
6612f05b
TH
3252 .seq_start = cgroup_pidlist_start,
3253 .seq_next = cgroup_pidlist_next,
3254 .seq_stop = cgroup_pidlist_stop,
3255 .seq_show = cgroup_pidlist_show,
5d22444f 3256 .private = CGROUP_FILE_TASKS,
d5c56ced 3257 .write_u64 = cgroup_tasks_write,
d5c56ced
TH
3258 .mode = S_IRUGO | S_IWUSR,
3259 },
3260 {
3261 .name = "notify_on_release",
3262 .flags = CFTYPE_INSANE,
3263 .read_u64 = cgroup_read_notify_on_release,
3264 .write_u64 = cgroup_write_notify_on_release,
3265 },
6e6ff25b
TH
3266 {
3267 .name = "release_agent",
cc5943a7 3268 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
2da8ca82 3269 .seq_show = cgroup_release_agent_show,
6e6ff25b 3270 .write_string = cgroup_release_agent_write,
5f469907 3271 .max_write_len = PATH_MAX - 1,
6e6ff25b 3272 },
db0416b6 3273 { } /* terminate */
bbcb81d0
PM
3274};
3275
13af07df 3276/**
628f7cd4 3277 * cgroup_populate_dir - create subsys files in a cgroup directory
13af07df 3278 * @cgrp: target cgroup
13af07df 3279 * @subsys_mask: mask of the subsystem ids whose files should be added
bee55099
TH
3280 *
3281 * On failure, no file is added.
13af07df 3282 */
628f7cd4 3283static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
ddbcc7e8 3284{
ddbcc7e8 3285 struct cgroup_subsys *ss;
b420ba7d 3286 int i, ret = 0;
bbcb81d0 3287
8e3f6541 3288 /* process cftsets of each subsystem */
b420ba7d 3289 for_each_subsys(ss, i) {
0adb0704 3290 struct cftype *cfts;
b420ba7d
TH
3291
3292 if (!test_bit(i, &subsys_mask))
13af07df 3293 continue;
8e3f6541 3294
0adb0704
TH
3295 list_for_each_entry(cfts, &ss->cfts, node) {
3296 ret = cgroup_addrm_files(cgrp, cfts, true);
bee55099
TH
3297 if (ret < 0)
3298 goto err;
3299 }
ddbcc7e8 3300 }
ddbcc7e8 3301 return 0;
bee55099
TH
3302err:
3303 cgroup_clear_dir(cgrp, subsys_mask);
3304 return ret;
ddbcc7e8
PM
3305}
3306
0c21ead1
TH
3307/*
3308 * css destruction is four-stage process.
3309 *
3310 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3311 * Implemented in kill_css().
3312 *
3313 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3314 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3315 * by invoking offline_css(). After offlining, the base ref is put.
3316 * Implemented in css_killed_work_fn().
3317 *
3318 * 3. When the percpu_ref reaches zero, the only possible remaining
3319 * accessors are inside RCU read sections. css_release() schedules the
3320 * RCU callback.
3321 *
3322 * 4. After the grace period, the css can be freed. Implemented in
3323 * css_free_work_fn().
3324 *
3325 * It is actually hairier because both step 2 and 4 require process context
3326 * and thus involve punting to css->destroy_work adding two additional
3327 * steps to the already complex sequence.
3328 */
35ef10da 3329static void css_free_work_fn(struct work_struct *work)
48ddbe19
TH
3330{
3331 struct cgroup_subsys_state *css =
35ef10da 3332 container_of(work, struct cgroup_subsys_state, destroy_work);
0c21ead1 3333 struct cgroup *cgrp = css->cgroup;
48ddbe19 3334
0ae78e0b
TH
3335 if (css->parent)
3336 css_put(css->parent);
3337
0c21ead1 3338 css->ss->css_free(css);
2bd59d48 3339 cgroup_put(cgrp);
48ddbe19
TH
3340}
3341
0c21ead1 3342static void css_free_rcu_fn(struct rcu_head *rcu_head)
d3daf28d
TH
3343{
3344 struct cgroup_subsys_state *css =
0c21ead1 3345 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
d3daf28d 3346
35ef10da 3347 INIT_WORK(&css->destroy_work, css_free_work_fn);
e5fca243 3348 queue_work(cgroup_destroy_wq, &css->destroy_work);
48ddbe19
TH
3349}
3350
d3daf28d
TH
3351static void css_release(struct percpu_ref *ref)
3352{
3353 struct cgroup_subsys_state *css =
3354 container_of(ref, struct cgroup_subsys_state, refcnt);
3355
aec25020 3356 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
0c21ead1 3357 call_rcu(&css->rcu_head, css_free_rcu_fn);
d3daf28d
TH
3358}
3359
623f926b
TH
3360static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3361 struct cgroup *cgrp)
ddbcc7e8 3362{
bd89aabc 3363 css->cgroup = cgrp;
72c97e54 3364 css->ss = ss;
ddbcc7e8 3365 css->flags = 0;
0ae78e0b
TH
3366
3367 if (cgrp->parent)
ca8bdcaf 3368 css->parent = cgroup_css(cgrp->parent, ss);
0ae78e0b 3369 else
38b53aba 3370 css->flags |= CSS_ROOT;
48ddbe19 3371
ca8bdcaf 3372 BUG_ON(cgroup_css(cgrp, ss));
ddbcc7e8
PM
3373}
3374
2a4ac633 3375/* invoke ->css_online() on a new CSS and mark it online if successful */
623f926b 3376static int online_css(struct cgroup_subsys_state *css)
a31f2d3f 3377{
623f926b 3378 struct cgroup_subsys *ss = css->ss;
b1929db4
TH
3379 int ret = 0;
3380
ace2bee8 3381 lockdep_assert_held(&cgroup_tree_mutex);
a31f2d3f
TH
3382 lockdep_assert_held(&cgroup_mutex);
3383
92fb9748 3384 if (ss->css_online)
eb95419b 3385 ret = ss->css_online(css);
ae7f164a 3386 if (!ret) {
eb95419b 3387 css->flags |= CSS_ONLINE;
f20104de 3388 css->cgroup->nr_css++;
aec25020 3389 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
ae7f164a 3390 }
b1929db4 3391 return ret;
a31f2d3f
TH
3392}
3393
2a4ac633 3394/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
623f926b 3395static void offline_css(struct cgroup_subsys_state *css)
a31f2d3f 3396{
623f926b 3397 struct cgroup_subsys *ss = css->ss;
a31f2d3f 3398
ace2bee8 3399 lockdep_assert_held(&cgroup_tree_mutex);
a31f2d3f
TH
3400 lockdep_assert_held(&cgroup_mutex);
3401
3402 if (!(css->flags & CSS_ONLINE))
3403 return;
3404
d7eeac19 3405 if (ss->css_offline)
eb95419b 3406 ss->css_offline(css);
a31f2d3f 3407
eb95419b 3408 css->flags &= ~CSS_ONLINE;
09a503ea 3409 css->cgroup->nr_css--;
aec25020 3410 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
a31f2d3f
TH
3411}
3412
c81c925a
TH
3413/**
3414 * create_css - create a cgroup_subsys_state
3415 * @cgrp: the cgroup new css will be associated with
3416 * @ss: the subsys of new css
3417 *
3418 * Create a new css associated with @cgrp - @ss pair. On success, the new
3419 * css is online and installed in @cgrp with all interface files created.
3420 * Returns 0 on success, -errno on failure.
3421 */
3422static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3423{
3424 struct cgroup *parent = cgrp->parent;
3425 struct cgroup_subsys_state *css;
3426 int err;
3427
c81c925a
TH
3428 lockdep_assert_held(&cgroup_mutex);
3429
3430 css = ss->css_alloc(cgroup_css(parent, ss));
3431 if (IS_ERR(css))
3432 return PTR_ERR(css);
3433
3434 err = percpu_ref_init(&css->refcnt, css_release);
3435 if (err)
3436 goto err_free;
3437
3438 init_css(css, ss, cgrp);
3439
aec25020 3440 err = cgroup_populate_dir(cgrp, 1 << ss->id);
c81c925a
TH
3441 if (err)
3442 goto err_free;
3443
3444 err = online_css(css);
3445 if (err)
3446 goto err_free;
3447
59f5296b 3448 cgroup_get(cgrp);
c81c925a
TH
3449 css_get(css->parent);
3450
3451 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3452 parent->parent) {
3453 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",
3454 current->comm, current->pid, ss->name);
3455 if (!strcmp(ss->name, "memory"))
3456 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3457 ss->warned_broken_hierarchy = true;
3458 }
3459
3460 return 0;
3461
3462err_free:
3463 percpu_ref_cancel_init(&css->refcnt);
3464 ss->css_free(css);
3465 return err;
3466}
3467
2bd59d48 3468/**
a043e3b2
LZ
3469 * cgroup_create - create a cgroup
3470 * @parent: cgroup that will be parent of the new cgroup
e61734c5 3471 * @name: name of the new cgroup
2bd59d48 3472 * @mode: mode to set on new cgroup
ddbcc7e8 3473 */
e61734c5 3474static long cgroup_create(struct cgroup *parent, const char *name,
2bd59d48 3475 umode_t mode)
ddbcc7e8 3476{
bd89aabc 3477 struct cgroup *cgrp;
ddbcc7e8 3478 struct cgroupfs_root *root = parent->root;
b58c8998 3479 int ssid, err;
ddbcc7e8 3480 struct cgroup_subsys *ss;
2bd59d48 3481 struct kernfs_node *kn;
ddbcc7e8 3482
0a950f65 3483 /* allocate the cgroup and its ID, 0 is reserved for the root */
bd89aabc
PM
3484 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3485 if (!cgrp)
ddbcc7e8
PM
3486 return -ENOMEM;
3487
ace2bee8
TH
3488 mutex_lock(&cgroup_tree_mutex);
3489
976c06bc
TH
3490 /*
3491 * Only live parents can have children. Note that the liveliness
3492 * check isn't strictly necessary because cgroup_mkdir() and
3493 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3494 * anyway so that locking is contained inside cgroup proper and we
3495 * don't get nasty surprises if we ever grow another caller.
3496 */
3497 if (!cgroup_lock_live_group(parent)) {
3498 err = -ENODEV;
ace2bee8 3499 goto err_unlock_tree;
0ab02ca8
LZ
3500 }
3501
3502 /*
3503 * Temporarily set the pointer to NULL, so idr_find() won't return
3504 * a half-baked cgroup.
3505 */
3506 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3507 if (cgrp->id < 0) {
3508 err = -ENOMEM;
3509 goto err_unlock;
976c06bc
TH
3510 }
3511
cc31edce 3512 init_cgroup_housekeeping(cgrp);
ddbcc7e8 3513
bd89aabc 3514 cgrp->parent = parent;
0ae78e0b 3515 cgrp->dummy_css.parent = &parent->dummy_css;
bd89aabc 3516 cgrp->root = parent->root;
ddbcc7e8 3517
b6abdb0e
LZ
3518 if (notify_on_release(parent))
3519 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3520
2260e7fc
TH
3521 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3522 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
97978e6d 3523
2bd59d48 3524 /* create the directory */
e61734c5 3525 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
2bd59d48
TH
3526 if (IS_ERR(kn)) {
3527 err = PTR_ERR(kn);
0ab02ca8 3528 goto err_free_id;
2bd59d48
TH
3529 }
3530 cgrp->kn = kn;
ddbcc7e8 3531
6f30558f
TH
3532 /*
3533 * This extra ref will be put in cgroup_free_fn() and guarantees
3534 * that @cgrp->kn is always accessible.
3535 */
3536 kernfs_get(kn);
3537
00356bd5 3538 cgrp->serial_nr = cgroup_serial_nr_next++;
53fa5261 3539
4e139afc 3540 /* allocation complete, commit to creation */
4e139afc 3541 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
3c9c825b 3542 atomic_inc(&root->nr_cgrps);
59f5296b 3543 cgroup_get(parent);
415cf07a 3544
0d80255e
TH
3545 /*
3546 * @cgrp is now fully operational. If something fails after this
3547 * point, it'll be released via the normal destruction path.
3548 */
4e96ee8e
LZ
3549 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3550
2bb566cb 3551 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
628f7cd4
TH
3552 if (err)
3553 goto err_destroy;
3554
9d403e99 3555 /* let's create and online css's */
b85d2040
TH
3556 for_each_subsys(ss, ssid) {
3557 if (root->subsys_mask & (1 << ssid)) {
3558 err = create_css(cgrp, ss);
3559 if (err)
3560 goto err_destroy;
3561 }
a8638030 3562 }
ddbcc7e8 3563
2bd59d48
TH
3564 kernfs_activate(kn);
3565
ddbcc7e8 3566 mutex_unlock(&cgroup_mutex);
ace2bee8 3567 mutex_unlock(&cgroup_tree_mutex);
ddbcc7e8
PM
3568
3569 return 0;
3570
0a950f65 3571err_free_id:
4e96ee8e 3572 idr_remove(&root->cgroup_idr, cgrp->id);
0ab02ca8
LZ
3573err_unlock:
3574 mutex_unlock(&cgroup_mutex);
ace2bee8
TH
3575err_unlock_tree:
3576 mutex_unlock(&cgroup_tree_mutex);
bd89aabc 3577 kfree(cgrp);
ddbcc7e8 3578 return err;
4b8b47eb
TH
3579
3580err_destroy:
3581 cgroup_destroy_locked(cgrp);
3582 mutex_unlock(&cgroup_mutex);
ace2bee8 3583 mutex_unlock(&cgroup_tree_mutex);
4b8b47eb 3584 return err;
ddbcc7e8
PM
3585}
3586
2bd59d48
TH
3587static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3588 umode_t mode)
ddbcc7e8 3589{
2bd59d48 3590 struct cgroup *parent = parent_kn->priv;
ddbcc7e8 3591
2bd59d48 3592 return cgroup_create(parent, name, mode);
ddbcc7e8
PM
3593}
3594
223dbc38
TH
3595/*
3596 * This is called when the refcnt of a css is confirmed to be killed.
3597 * css_tryget() is now guaranteed to fail.
3598 */
3599static void css_killed_work_fn(struct work_struct *work)
d3daf28d 3600{
223dbc38
TH
3601 struct cgroup_subsys_state *css =
3602 container_of(work, struct cgroup_subsys_state, destroy_work);
3603 struct cgroup *cgrp = css->cgroup;
d3daf28d 3604
ace2bee8 3605 mutex_lock(&cgroup_tree_mutex);
f20104de
TH
3606 mutex_lock(&cgroup_mutex);
3607
09a503ea
TH
3608 /*
3609 * css_tryget() is guaranteed to fail now. Tell subsystems to
3610 * initate destruction.
3611 */
3612 offline_css(css);
3613
f20104de
TH
3614 /*
3615 * If @cgrp is marked dead, it's waiting for refs of all css's to
3616 * be disabled before proceeding to the second phase of cgroup
3617 * destruction. If we are the last one, kick it off.
3618 */
09a503ea 3619 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
f20104de
TH
3620 cgroup_destroy_css_killed(cgrp);
3621
3622 mutex_unlock(&cgroup_mutex);
ace2bee8 3623 mutex_unlock(&cgroup_tree_mutex);
09a503ea
TH
3624
3625 /*
3626 * Put the css refs from kill_css(). Each css holds an extra
3627 * reference to the cgroup's dentry and cgroup removal proceeds
3628 * regardless of css refs. On the last put of each css, whenever
3629 * that may be, the extra dentry ref is put so that dentry
3630 * destruction happens only after all css's are released.
3631 */
3632 css_put(css);
d3daf28d
TH
3633}
3634
223dbc38
TH
3635/* css kill confirmation processing requires process context, bounce */
3636static void css_killed_ref_fn(struct percpu_ref *ref)
d3daf28d
TH
3637{
3638 struct cgroup_subsys_state *css =
3639 container_of(ref, struct cgroup_subsys_state, refcnt);
3640
223dbc38 3641 INIT_WORK(&css->destroy_work, css_killed_work_fn);
e5fca243 3642 queue_work(cgroup_destroy_wq, &css->destroy_work);
d3daf28d
TH
3643}
3644
edae0c33
TH
3645/**
3646 * kill_css - destroy a css
3647 * @css: css to destroy
3648 *
3c14f8b4
TH
3649 * This function initiates destruction of @css by removing cgroup interface
3650 * files and putting its base reference. ->css_offline() will be invoked
3651 * asynchronously once css_tryget() is guaranteed to fail and when the
3652 * reference count reaches zero, @css will be released.
edae0c33
TH
3653 */
3654static void kill_css(struct cgroup_subsys_state *css)
3655{
2bd59d48
TH
3656 /*
3657 * This must happen before css is disassociated with its cgroup.
3658 * See seq_css() for details.
3659 */
aec25020 3660 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
3c14f8b4 3661
edae0c33
TH
3662 /*
3663 * Killing would put the base ref, but we need to keep it alive
3664 * until after ->css_offline().
3665 */
3666 css_get(css);
3667
3668 /*
3669 * cgroup core guarantees that, by the time ->css_offline() is
3670 * invoked, no new css reference will be given out via
3671 * css_tryget(). We can't simply call percpu_ref_kill() and
3672 * proceed to offlining css's because percpu_ref_kill() doesn't
3673 * guarantee that the ref is seen as killed on all CPUs on return.
3674 *
3675 * Use percpu_ref_kill_and_confirm() to get notifications as each
3676 * css is confirmed to be seen as killed on all CPUs.
3677 */
3678 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
d3daf28d
TH
3679}
3680
3681/**
3682 * cgroup_destroy_locked - the first stage of cgroup destruction
3683 * @cgrp: cgroup to be destroyed
3684 *
3685 * css's make use of percpu refcnts whose killing latency shouldn't be
3686 * exposed to userland and are RCU protected. Also, cgroup core needs to
3687 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3688 * invoked. To satisfy all the requirements, destruction is implemented in
3689 * the following two steps.
3690 *
3691 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3692 * userland visible parts and start killing the percpu refcnts of
3693 * css's. Set up so that the next stage will be kicked off once all
3694 * the percpu refcnts are confirmed to be killed.
3695 *
3696 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3697 * rest of destruction. Once all cgroup references are gone, the
3698 * cgroup is RCU-freed.
3699 *
3700 * This function implements s1. After this step, @cgrp is gone as far as
3701 * the userland is concerned and a new cgroup with the same name may be
3702 * created. As cgroup doesn't care about the names internally, this
3703 * doesn't cause any problem.
3704 */
42809dd4
TH
3705static int cgroup_destroy_locked(struct cgroup *cgrp)
3706 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
ddbcc7e8 3707{
bb78a92f 3708 struct cgroup *child;
2bd59d48 3709 struct cgroup_subsys_state *css;
ddd69148 3710 bool empty;
1c6727af 3711 int ssid;
ddbcc7e8 3712
ace2bee8 3713 lockdep_assert_held(&cgroup_tree_mutex);
42809dd4
TH
3714 lockdep_assert_held(&cgroup_mutex);
3715
ddd69148 3716 /*
96d365e0 3717 * css_set_rwsem synchronizes access to ->cset_links and prevents
6f3d828f 3718 * @cgrp from being removed while __put_css_set() is in progress.
ddd69148 3719 */
96d365e0 3720 down_read(&css_set_rwsem);
bb78a92f 3721 empty = list_empty(&cgrp->cset_links);
96d365e0 3722 up_read(&css_set_rwsem);
ddd69148 3723 if (!empty)
ddbcc7e8 3724 return -EBUSY;
a043e3b2 3725
bb78a92f
HD
3726 /*
3727 * Make sure there's no live children. We can't test ->children
3728 * emptiness as dead children linger on it while being destroyed;
3729 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3730 */
3731 empty = true;
3732 rcu_read_lock();
3733 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3734 empty = cgroup_is_dead(child);
3735 if (!empty)
3736 break;
3737 }
3738 rcu_read_unlock();
3739 if (!empty)
3740 return -EBUSY;
3741
88703267 3742 /*
edae0c33
TH
3743 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3744 * will be invoked to perform the rest of destruction once the
4ac06017
TH
3745 * percpu refs of all css's are confirmed to be killed. This
3746 * involves removing the subsystem's files, drop cgroup_mutex.
88703267 3747 */
4ac06017 3748 mutex_unlock(&cgroup_mutex);
1c6727af
TH
3749 for_each_css(css, ssid, cgrp)
3750 kill_css(css);
4ac06017 3751 mutex_lock(&cgroup_mutex);
455050d2
TH
3752
3753 /*
3754 * Mark @cgrp dead. This prevents further task migration and child
3755 * creation by disabling cgroup_lock_live_group(). Note that
492eb21b 3756 * CGRP_DEAD assertion is depended upon by css_next_child() to
455050d2 3757 * resume iteration after dropping RCU read lock. See
492eb21b 3758 * css_next_child() for details.
455050d2 3759 */
54766d4a 3760 set_bit(CGRP_DEAD, &cgrp->flags);
ddbcc7e8 3761
455050d2
TH
3762 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3763 raw_spin_lock(&release_list_lock);
3764 if (!list_empty(&cgrp->release_list))
3765 list_del_init(&cgrp->release_list);
3766 raw_spin_unlock(&release_list_lock);
3767
3768 /*
f20104de
TH
3769 * If @cgrp has css's attached, the second stage of cgroup
3770 * destruction is kicked off from css_killed_work_fn() after the
3771 * refs of all attached css's are killed. If @cgrp doesn't have
3772 * any css, we kick it off here.
3773 */
3774 if (!cgrp->nr_css)
3775 cgroup_destroy_css_killed(cgrp);
3776
2bd59d48
TH
3777 /* remove @cgrp directory along with the base files */
3778 mutex_unlock(&cgroup_mutex);
3779
455050d2 3780 /*
2bd59d48
TH
3781 * There are two control paths which try to determine cgroup from
3782 * dentry without going through kernfs - cgroupstats_build() and
3783 * css_tryget_from_dir(). Those are supported by RCU protecting
3784 * clearing of cgrp->kn->priv backpointer, which should happen
3785 * after all files under it have been removed.
455050d2 3786 */
6f30558f 3787 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
2bd59d48 3788 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
2bd59d48 3789
4ac06017 3790 mutex_lock(&cgroup_mutex);
455050d2 3791
ea15f8cc
TH
3792 return 0;
3793};
3794
d3daf28d 3795/**
f20104de 3796 * cgroup_destroy_css_killed - the second step of cgroup destruction
d3daf28d
TH
3797 * @work: cgroup->destroy_free_work
3798 *
3799 * This function is invoked from a work item for a cgroup which is being
09a503ea
TH
3800 * destroyed after all css's are offlined and performs the rest of
3801 * destruction. This is the second step of destruction described in the
3802 * comment above cgroup_destroy_locked().
d3daf28d 3803 */
f20104de 3804static void cgroup_destroy_css_killed(struct cgroup *cgrp)
ea15f8cc 3805{
ea15f8cc 3806 struct cgroup *parent = cgrp->parent;
ea15f8cc 3807
ace2bee8 3808 lockdep_assert_held(&cgroup_tree_mutex);
f20104de 3809 lockdep_assert_held(&cgroup_mutex);
ea15f8cc 3810
999cd8a4 3811 /* delete this cgroup from parent->children */
eb6fd504 3812 list_del_rcu(&cgrp->sibling);
ed957793 3813
59f5296b 3814 cgroup_put(cgrp);
ddbcc7e8 3815
bd89aabc 3816 set_bit(CGRP_RELEASABLE, &parent->flags);
81a6a5cd 3817 check_for_release(parent);
ddbcc7e8
PM
3818}
3819
2bd59d48 3820static int cgroup_rmdir(struct kernfs_node *kn)
42809dd4 3821{
2bd59d48
TH
3822 struct cgroup *cgrp = kn->priv;
3823 int ret = 0;
3824
3825 /*
3826 * This is self-destruction but @kn can't be removed while this
3827 * callback is in progress. Let's break active protection. Once
3828 * the protection is broken, @cgrp can be destroyed at any point.
3829 * Pin it so that it stays accessible.
3830 */
3831 cgroup_get(cgrp);
3832 kernfs_break_active_protection(kn);
42809dd4 3833
ace2bee8 3834 mutex_lock(&cgroup_tree_mutex);
42809dd4 3835 mutex_lock(&cgroup_mutex);
2bd59d48
TH
3836
3837 /*
3838 * @cgrp might already have been destroyed while we're trying to
3839 * grab the mutexes.
3840 */
3841 if (!cgroup_is_dead(cgrp))
3842 ret = cgroup_destroy_locked(cgrp);
3843
42809dd4 3844 mutex_unlock(&cgroup_mutex);
ace2bee8 3845 mutex_unlock(&cgroup_tree_mutex);
42809dd4 3846
2bd59d48
TH
3847 kernfs_unbreak_active_protection(kn);
3848 cgroup_put(cgrp);
42809dd4
TH
3849 return ret;
3850}
3851
2bd59d48
TH
3852static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
3853 .remount_fs = cgroup_remount,
3854 .show_options = cgroup_show_options,
3855 .mkdir = cgroup_mkdir,
3856 .rmdir = cgroup_rmdir,
3857 .rename = cgroup_rename,
3858};
3859
06a11920 3860static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
ddbcc7e8 3861{
ddbcc7e8 3862 struct cgroup_subsys_state *css;
cfe36bde
DC
3863
3864 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
ddbcc7e8 3865
ace2bee8 3866 mutex_lock(&cgroup_tree_mutex);
648bb56d
TH
3867 mutex_lock(&cgroup_mutex);
3868
0adb0704 3869 INIT_LIST_HEAD(&ss->cfts);
8e3f6541 3870
ddbcc7e8 3871 /* Create the top cgroup state for this subsystem */
9871bf95 3872 ss->root = &cgroup_dummy_root;
ca8bdcaf 3873 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
ddbcc7e8
PM
3874 /* We don't handle early failures gracefully */
3875 BUG_ON(IS_ERR(css));
623f926b 3876 init_css(css, ss, cgroup_dummy_top);
ddbcc7e8 3877
e8d55fde 3878 /* Update the init_css_set to contain a subsys
817929ec 3879 * pointer to this state - since the subsystem is
e8d55fde
LZ
3880 * newly registered, all tasks and hence the
3881 * init_css_set is in the subsystem's top cgroup. */
aec25020 3882 init_css_set.subsys[ss->id] = css;
ddbcc7e8
PM
3883
3884 need_forkexit_callback |= ss->fork || ss->exit;
3885
e8d55fde
LZ
3886 /* At system boot, before all subsystems have been
3887 * registered, no tasks have been forked, so we don't
3888 * need to invoke fork callbacks here. */
3889 BUG_ON(!list_empty(&init_task.tasks));
3890
ae7f164a 3891 BUG_ON(online_css(css));
a8638030 3892
648bb56d 3893 mutex_unlock(&cgroup_mutex);
ace2bee8 3894 mutex_unlock(&cgroup_tree_mutex);
e6a1105b
BB
3895}
3896
ddbcc7e8 3897/**
a043e3b2
LZ
3898 * cgroup_init_early - cgroup initialization at system boot
3899 *
3900 * Initialize cgroups at system boot, and initialize any
3901 * subsystems that request early init.
ddbcc7e8
PM
3902 */
3903int __init cgroup_init_early(void)
3904{
30159ec7 3905 struct cgroup_subsys *ss;
ddbcc7e8 3906 int i;
30159ec7 3907
146aa1bd 3908 atomic_set(&init_css_set.refcount, 1);
69d0206c 3909 INIT_LIST_HEAD(&init_css_set.cgrp_links);
817929ec 3910 INIT_LIST_HEAD(&init_css_set.tasks);
472b1053 3911 INIT_HLIST_NODE(&init_css_set.hlist);
817929ec 3912 css_set_count = 1;
9871bf95
TH
3913 init_cgroup_root(&cgroup_dummy_root);
3914 cgroup_root_count = 1;
a4ea1cc9 3915 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
817929ec 3916
69d0206c 3917 init_cgrp_cset_link.cset = &init_css_set;
9871bf95
TH
3918 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
3919 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
69d0206c 3920 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
ddbcc7e8 3921
3ed80a62 3922 for_each_subsys(ss, i) {
aec25020 3923 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
073219e9
TH
3924 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
3925 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
aec25020 3926 ss->id, ss->name);
073219e9
TH
3927 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
3928 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
3929
aec25020 3930 ss->id = i;
073219e9 3931 ss->name = cgroup_subsys_name[i];
ddbcc7e8
PM
3932
3933 if (ss->early_init)
3934 cgroup_init_subsys(ss);
3935 }
3936 return 0;
3937}
3938
3939/**
a043e3b2
LZ
3940 * cgroup_init - cgroup initialization
3941 *
3942 * Register cgroup filesystem and /proc file, and initialize
3943 * any subsystems that didn't request early init.
ddbcc7e8
PM
3944 */
3945int __init cgroup_init(void)
3946{
30159ec7 3947 struct cgroup_subsys *ss;
0ac801fe 3948 unsigned long key;
30159ec7 3949 int i, err;
a424316c 3950
2bd59d48 3951 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
2da440a2 3952
3ed80a62 3953 for_each_subsys(ss, i) {
ddbcc7e8
PM
3954 if (!ss->early_init)
3955 cgroup_init_subsys(ss);
de00ffa5
TH
3956
3957 /*
3958 * cftype registration needs kmalloc and can't be done
3959 * during early_init. Register base cftypes separately.
3960 */
3961 if (ss->base_cftypes)
3962 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
ddbcc7e8
PM
3963 }
3964
fa3ca07e 3965 /* allocate id for the dummy hierarchy */
54e7b4eb 3966 mutex_lock(&cgroup_mutex);
54e7b4eb 3967
82fe9b0d
TH
3968 /* Add init_css_set to the hash table */
3969 key = css_set_hash(init_css_set.subsys);
3970 hash_add(css_set_table, &init_css_set.hlist, key);
3971
fc76df70 3972 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
676db4af 3973
4e96ee8e
LZ
3974 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
3975 0, 1, GFP_KERNEL);
3976 BUG_ON(err < 0);
3977
54e7b4eb
TH
3978 mutex_unlock(&cgroup_mutex);
3979
676db4af 3980 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
2bd59d48
TH
3981 if (!cgroup_kobj)
3982 return -ENOMEM;
676db4af 3983
ddbcc7e8 3984 err = register_filesystem(&cgroup_fs_type);
676db4af
GK
3985 if (err < 0) {
3986 kobject_put(cgroup_kobj);
2bd59d48 3987 return err;
676db4af 3988 }
ddbcc7e8 3989
46ae220b 3990 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
2bd59d48 3991 return 0;
ddbcc7e8 3992}
b4f48b63 3993
e5fca243
TH
3994static int __init cgroup_wq_init(void)
3995{
3996 /*
3997 * There isn't much point in executing destruction path in
3998 * parallel. Good chunk is serialized with cgroup_mutex anyway.
ab3f5faa
HD
3999 *
4000 * XXX: Must be ordered to make sure parent is offlined after
4001 * children. The ordering requirement is for memcg where a
4002 * parent's offline may wait for a child's leading to deadlock. In
4003 * the long term, this should be fixed from memcg side.
e5fca243
TH
4004 *
4005 * We would prefer to do this in cgroup_init() above, but that
4006 * is called before init_workqueues(): so leave this until after.
4007 */
ab3f5faa 4008 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
e5fca243 4009 BUG_ON(!cgroup_destroy_wq);
b1a21367
TH
4010
4011 /*
4012 * Used to destroy pidlists and separate to serve as flush domain.
4013 * Cap @max_active to 1 too.
4014 */
4015 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4016 0, 1);
4017 BUG_ON(!cgroup_pidlist_destroy_wq);
4018
e5fca243
TH
4019 return 0;
4020}
4021core_initcall(cgroup_wq_init);
4022
a424316c
PM
4023/*
4024 * proc_cgroup_show()
4025 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4026 * - Used for /proc/<pid>/cgroup.
4027 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4028 * doesn't really matter if tsk->cgroup changes after we read it,
956db3ca 4029 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
a424316c
PM
4030 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4031 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4032 * cgroup to top_cgroup.
4033 */
4034
4035/* TODO: Use a proper seq_file iterator */
8d8b97ba 4036int proc_cgroup_show(struct seq_file *m, void *v)
a424316c
PM
4037{
4038 struct pid *pid;
4039 struct task_struct *tsk;
e61734c5 4040 char *buf, *path;
a424316c
PM
4041 int retval;
4042 struct cgroupfs_root *root;
4043
4044 retval = -ENOMEM;
e61734c5 4045 buf = kmalloc(PATH_MAX, GFP_KERNEL);
a424316c
PM
4046 if (!buf)
4047 goto out;
4048
4049 retval = -ESRCH;
4050 pid = m->private;
4051 tsk = get_pid_task(pid, PIDTYPE_PID);
4052 if (!tsk)
4053 goto out_free;
4054
4055 retval = 0;
4056
4057 mutex_lock(&cgroup_mutex);
96d365e0 4058 down_read(&css_set_rwsem);
a424316c 4059
e5f6a860 4060 for_each_active_root(root) {
a424316c 4061 struct cgroup_subsys *ss;
bd89aabc 4062 struct cgroup *cgrp;
b85d2040 4063 int ssid, count = 0;
a424316c 4064
2c6ab6d2 4065 seq_printf(m, "%d:", root->hierarchy_id);
b85d2040
TH
4066 for_each_subsys(ss, ssid)
4067 if (root->subsys_mask & (1 << ssid))
4068 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
c6d57f33
PM
4069 if (strlen(root->name))
4070 seq_printf(m, "%sname=%s", count ? "," : "",
4071 root->name);
a424316c 4072 seq_putc(m, ':');
7717f7ba 4073 cgrp = task_cgroup_from_root(tsk, root);
e61734c5
TH
4074 path = cgroup_path(cgrp, buf, PATH_MAX);
4075 if (!path) {
4076 retval = -ENAMETOOLONG;
a424316c 4077 goto out_unlock;
e61734c5
TH
4078 }
4079 seq_puts(m, path);
a424316c
PM
4080 seq_putc(m, '\n');
4081 }
4082
4083out_unlock:
96d365e0 4084 up_read(&css_set_rwsem);
a424316c
PM
4085 mutex_unlock(&cgroup_mutex);
4086 put_task_struct(tsk);
4087out_free:
4088 kfree(buf);
4089out:
4090 return retval;
4091}
4092
a424316c
PM
4093/* Display information about each subsystem and each hierarchy */
4094static int proc_cgroupstats_show(struct seq_file *m, void *v)
4095{
30159ec7 4096 struct cgroup_subsys *ss;
a424316c 4097 int i;
a424316c 4098
8bab8dde 4099 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
aae8aab4
BB
4100 /*
4101 * ideally we don't want subsystems moving around while we do this.
4102 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4103 * subsys/hierarchy state.
4104 */
a424316c 4105 mutex_lock(&cgroup_mutex);
30159ec7
TH
4106
4107 for_each_subsys(ss, i)
2c6ab6d2
PM
4108 seq_printf(m, "%s\t%d\t%d\t%d\n",
4109 ss->name, ss->root->hierarchy_id,
3c9c825b 4110 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
30159ec7 4111
a424316c
PM
4112 mutex_unlock(&cgroup_mutex);
4113 return 0;
4114}
4115
4116static int cgroupstats_open(struct inode *inode, struct file *file)
4117{
9dce07f1 4118 return single_open(file, proc_cgroupstats_show, NULL);
a424316c
PM
4119}
4120
828c0950 4121static const struct file_operations proc_cgroupstats_operations = {
a424316c
PM
4122 .open = cgroupstats_open,
4123 .read = seq_read,
4124 .llseek = seq_lseek,
4125 .release = single_release,
4126};
4127
b4f48b63
PM
4128/**
4129 * cgroup_fork - attach newly forked task to its parents cgroup.
a043e3b2 4130 * @child: pointer to task_struct of forking parent process.
b4f48b63
PM
4131 *
4132 * Description: A task inherits its parent's cgroup at fork().
4133 *
4134 * A pointer to the shared css_set was automatically copied in
4135 * fork.c by dup_task_struct(). However, we ignore that copy, since
9bb71308
TH
4136 * it was not made under the protection of RCU or cgroup_mutex, so
4137 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4138 * have already changed current->cgroups, allowing the previously
4139 * referenced cgroup group to be removed and freed.
b4f48b63
PM
4140 *
4141 * At the point that cgroup_fork() is called, 'current' is the parent
4142 * task, and the passed argument 'child' points to the child task.
4143 */
4144void cgroup_fork(struct task_struct *child)
4145{
9bb71308 4146 task_lock(current);
a8ad805c 4147 get_css_set(task_css_set(current));
817929ec 4148 child->cgroups = current->cgroups;
9bb71308 4149 task_unlock(current);
817929ec 4150 INIT_LIST_HEAD(&child->cg_list);
b4f48b63
PM
4151}
4152
817929ec 4153/**
a043e3b2
LZ
4154 * cgroup_post_fork - called on a new task after adding it to the task list
4155 * @child: the task in question
4156 *
5edee61e
TH
4157 * Adds the task to the list running through its css_set if necessary and
4158 * call the subsystem fork() callbacks. Has to be after the task is
4159 * visible on the task list in case we race with the first call to
0942eeee 4160 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5edee61e 4161 * list.
a043e3b2 4162 */
817929ec
PM
4163void cgroup_post_fork(struct task_struct *child)
4164{
30159ec7 4165 struct cgroup_subsys *ss;
5edee61e
TH
4166 int i;
4167
3ce3230a
FW
4168 /*
4169 * use_task_css_set_links is set to 1 before we walk the tasklist
4170 * under the tasklist_lock and we read it here after we added the child
4171 * to the tasklist under the tasklist_lock as well. If the child wasn't
4172 * yet in the tasklist when we walked through it from
4173 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4174 * should be visible now due to the paired locking and barriers implied
4175 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4176 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4177 * lock on fork.
4178 */
817929ec 4179 if (use_task_css_set_links) {
96d365e0 4180 down_write(&css_set_rwsem);
d8783832
TH
4181 task_lock(child);
4182 if (list_empty(&child->cg_list))
a8ad805c 4183 list_add(&child->cg_list, &task_css_set(child)->tasks);
d8783832 4184 task_unlock(child);
96d365e0 4185 up_write(&css_set_rwsem);
817929ec 4186 }
5edee61e
TH
4187
4188 /*
4189 * Call ss->fork(). This must happen after @child is linked on
4190 * css_set; otherwise, @child might change state between ->fork()
4191 * and addition to css_set.
4192 */
4193 if (need_forkexit_callback) {
3ed80a62 4194 for_each_subsys(ss, i)
5edee61e
TH
4195 if (ss->fork)
4196 ss->fork(child);
5edee61e 4197 }
817929ec 4198}
5edee61e 4199
b4f48b63
PM
4200/**
4201 * cgroup_exit - detach cgroup from exiting task
4202 * @tsk: pointer to task_struct of exiting process
a043e3b2 4203 * @run_callback: run exit callbacks?
b4f48b63
PM
4204 *
4205 * Description: Detach cgroup from @tsk and release it.
4206 *
4207 * Note that cgroups marked notify_on_release force every task in
4208 * them to take the global cgroup_mutex mutex when exiting.
4209 * This could impact scaling on very large systems. Be reluctant to
4210 * use notify_on_release cgroups where very high task exit scaling
4211 * is required on large systems.
4212 *
4213 * the_top_cgroup_hack:
4214 *
4215 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4216 *
4217 * We call cgroup_exit() while the task is still competent to
4218 * handle notify_on_release(), then leave the task attached to the
4219 * root cgroup in each hierarchy for the remainder of its exit.
4220 *
4221 * To do this properly, we would increment the reference count on
4222 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4223 * code we would add a second cgroup function call, to drop that
4224 * reference. This would just create an unnecessary hot spot on
4225 * the top_cgroup reference count, to no avail.
4226 *
4227 * Normally, holding a reference to a cgroup without bumping its
4228 * count is unsafe. The cgroup could go away, or someone could
4229 * attach us to a different cgroup, decrementing the count on
4230 * the first cgroup that we never incremented. But in this case,
4231 * top_cgroup isn't going away, and either task has PF_EXITING set,
956db3ca
CW
4232 * which wards off any cgroup_attach_task() attempts, or task is a failed
4233 * fork, never visible to cgroup_attach_task.
b4f48b63
PM
4234 */
4235void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4236{
30159ec7 4237 struct cgroup_subsys *ss;
5abb8855 4238 struct css_set *cset;
d41d5a01 4239 int i;
817929ec
PM
4240
4241 /*
96d365e0
TH
4242 * Unlink from the css_set task list if necessary. Optimistically
4243 * check cg_list before taking css_set_rwsem.
817929ec
PM
4244 */
4245 if (!list_empty(&tsk->cg_list)) {
96d365e0 4246 down_write(&css_set_rwsem);
817929ec 4247 if (!list_empty(&tsk->cg_list))
8d258797 4248 list_del_init(&tsk->cg_list);
96d365e0 4249 up_write(&css_set_rwsem);
817929ec
PM
4250 }
4251
b4f48b63
PM
4252 /* Reassign the task to the init_css_set. */
4253 task_lock(tsk);
a8ad805c
TH
4254 cset = task_css_set(tsk);
4255 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
d41d5a01
PZ
4256
4257 if (run_callbacks && need_forkexit_callback) {
3ed80a62
TH
4258 /* see cgroup_post_fork() for details */
4259 for_each_subsys(ss, i) {
d41d5a01 4260 if (ss->exit) {
eb95419b
TH
4261 struct cgroup_subsys_state *old_css = cset->subsys[i];
4262 struct cgroup_subsys_state *css = task_css(tsk, i);
30159ec7 4263
eb95419b 4264 ss->exit(css, old_css, tsk);
d41d5a01
PZ
4265 }
4266 }
4267 }
b4f48b63 4268 task_unlock(tsk);
d41d5a01 4269
5abb8855 4270 put_css_set_taskexit(cset);
b4f48b63 4271}
697f4161 4272
bd89aabc 4273static void check_for_release(struct cgroup *cgrp)
81a6a5cd 4274{
f50daa70 4275 if (cgroup_is_releasable(cgrp) &&
6f3d828f 4276 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
f50daa70
LZ
4277 /*
4278 * Control Group is currently removeable. If it's not
81a6a5cd 4279 * already queued for a userspace notification, queue
f50daa70
LZ
4280 * it now
4281 */
81a6a5cd 4282 int need_schedule_work = 0;
f50daa70 4283
cdcc136f 4284 raw_spin_lock(&release_list_lock);
54766d4a 4285 if (!cgroup_is_dead(cgrp) &&
bd89aabc
PM
4286 list_empty(&cgrp->release_list)) {
4287 list_add(&cgrp->release_list, &release_list);
81a6a5cd
PM
4288 need_schedule_work = 1;
4289 }
cdcc136f 4290 raw_spin_unlock(&release_list_lock);
81a6a5cd
PM
4291 if (need_schedule_work)
4292 schedule_work(&release_agent_work);
4293 }
4294}
4295
81a6a5cd
PM
4296/*
4297 * Notify userspace when a cgroup is released, by running the
4298 * configured release agent with the name of the cgroup (path
4299 * relative to the root of cgroup file system) as the argument.
4300 *
4301 * Most likely, this user command will try to rmdir this cgroup.
4302 *
4303 * This races with the possibility that some other task will be
4304 * attached to this cgroup before it is removed, or that some other
4305 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4306 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4307 * unused, and this cgroup will be reprieved from its death sentence,
4308 * to continue to serve a useful existence. Next time it's released,
4309 * we will get notified again, if it still has 'notify_on_release' set.
4310 *
4311 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4312 * means only wait until the task is successfully execve()'d. The
4313 * separate release agent task is forked by call_usermodehelper(),
4314 * then control in this thread returns here, without waiting for the
4315 * release agent task. We don't bother to wait because the caller of
4316 * this routine has no use for the exit status of the release agent
4317 * task, so no sense holding our caller up for that.
81a6a5cd 4318 */
81a6a5cd
PM
4319static void cgroup_release_agent(struct work_struct *work)
4320{
4321 BUG_ON(work != &release_agent_work);
4322 mutex_lock(&cgroup_mutex);
cdcc136f 4323 raw_spin_lock(&release_list_lock);
81a6a5cd
PM
4324 while (!list_empty(&release_list)) {
4325 char *argv[3], *envp[3];
4326 int i;
e61734c5 4327 char *pathbuf = NULL, *agentbuf = NULL, *path;
bd89aabc 4328 struct cgroup *cgrp = list_entry(release_list.next,
81a6a5cd
PM
4329 struct cgroup,
4330 release_list);
bd89aabc 4331 list_del_init(&cgrp->release_list);
cdcc136f 4332 raw_spin_unlock(&release_list_lock);
e61734c5 4333 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
e788e066
PM
4334 if (!pathbuf)
4335 goto continue_free;
e61734c5
TH
4336 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4337 if (!path)
e788e066
PM
4338 goto continue_free;
4339 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4340 if (!agentbuf)
4341 goto continue_free;
81a6a5cd
PM
4342
4343 i = 0;
e788e066 4344 argv[i++] = agentbuf;
e61734c5 4345 argv[i++] = path;
81a6a5cd
PM
4346 argv[i] = NULL;
4347
4348 i = 0;
4349 /* minimal command environment */
4350 envp[i++] = "HOME=/";
4351 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4352 envp[i] = NULL;
4353
4354 /* Drop the lock while we invoke the usermode helper,
4355 * since the exec could involve hitting disk and hence
4356 * be a slow process */
4357 mutex_unlock(&cgroup_mutex);
4358 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
81a6a5cd 4359 mutex_lock(&cgroup_mutex);
e788e066
PM
4360 continue_free:
4361 kfree(pathbuf);
4362 kfree(agentbuf);
cdcc136f 4363 raw_spin_lock(&release_list_lock);
81a6a5cd 4364 }
cdcc136f 4365 raw_spin_unlock(&release_list_lock);
81a6a5cd
PM
4366 mutex_unlock(&cgroup_mutex);
4367}
8bab8dde
PM
4368
4369static int __init cgroup_disable(char *str)
4370{
30159ec7 4371 struct cgroup_subsys *ss;
8bab8dde 4372 char *token;
30159ec7 4373 int i;
8bab8dde
PM
4374
4375 while ((token = strsep(&str, ",")) != NULL) {
4376 if (!*token)
4377 continue;
be45c900 4378
3ed80a62 4379 for_each_subsys(ss, i) {
8bab8dde
PM
4380 if (!strcmp(token, ss->name)) {
4381 ss->disabled = 1;
4382 printk(KERN_INFO "Disabling %s control group"
4383 " subsystem\n", ss->name);
4384 break;
4385 }
4386 }
4387 }
4388 return 1;
4389}
4390__setup("cgroup_disable=", cgroup_disable);
38460b48 4391
b77d7b60 4392/**
5a17f543 4393 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
35cf0836
TH
4394 * @dentry: directory dentry of interest
4395 * @ss: subsystem of interest
b77d7b60 4396 *
5a17f543
TH
4397 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4398 * to get the corresponding css and return it. If such css doesn't exist
4399 * or can't be pinned, an ERR_PTR value is returned.
e5d1367f 4400 */
5a17f543
TH
4401struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4402 struct cgroup_subsys *ss)
e5d1367f 4403{
2bd59d48
TH
4404 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4405 struct cgroup_subsys_state *css = NULL;
e5d1367f 4406 struct cgroup *cgrp;
b77d7b60 4407
35cf0836 4408 /* is @dentry a cgroup dir? */
2bd59d48
TH
4409 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4410 kernfs_type(kn) != KERNFS_DIR)
e5d1367f
SE
4411 return ERR_PTR(-EBADF);
4412
5a17f543
TH
4413 rcu_read_lock();
4414
2bd59d48
TH
4415 /*
4416 * This path doesn't originate from kernfs and @kn could already
4417 * have been or be removed at any point. @kn->priv is RCU
4418 * protected for this access. See destroy_locked() for details.
4419 */
4420 cgrp = rcu_dereference(kn->priv);
4421 if (cgrp)
4422 css = cgroup_css(cgrp, ss);
5a17f543
TH
4423
4424 if (!css || !css_tryget(css))
4425 css = ERR_PTR(-ENOENT);
4426
4427 rcu_read_unlock();
4428 return css;
e5d1367f 4429}
e5d1367f 4430
1cb650b9
LZ
4431/**
4432 * css_from_id - lookup css by id
4433 * @id: the cgroup id
4434 * @ss: cgroup subsys to be looked into
4435 *
4436 * Returns the css if there's valid one with @id, otherwise returns NULL.
4437 * Should be called under rcu_read_lock().
4438 */
4439struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4440{
4441 struct cgroup *cgrp;
4442
ace2bee8 4443 cgroup_assert_mutexes_or_rcu_locked();
1cb650b9
LZ
4444
4445 cgrp = idr_find(&ss->root->cgroup_idr, id);
4446 if (cgrp)
d1625964 4447 return cgroup_css(cgrp, ss);
1cb650b9 4448 return NULL;
e5d1367f
SE
4449}
4450
fe693435 4451#ifdef CONFIG_CGROUP_DEBUG
eb95419b
TH
4452static struct cgroup_subsys_state *
4453debug_css_alloc(struct cgroup_subsys_state *parent_css)
fe693435
PM
4454{
4455 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4456
4457 if (!css)
4458 return ERR_PTR(-ENOMEM);
4459
4460 return css;
4461}
4462
eb95419b 4463static void debug_css_free(struct cgroup_subsys_state *css)
fe693435 4464{
eb95419b 4465 kfree(css);
fe693435
PM
4466}
4467
182446d0
TH
4468static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4469 struct cftype *cft)
fe693435 4470{
182446d0 4471 return cgroup_task_count(css->cgroup);
fe693435
PM
4472}
4473
182446d0
TH
4474static u64 current_css_set_read(struct cgroup_subsys_state *css,
4475 struct cftype *cft)
fe693435
PM
4476{
4477 return (u64)(unsigned long)current->cgroups;
4478}
4479
182446d0 4480static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
03c78cbe 4481 struct cftype *cft)
fe693435
PM
4482{
4483 u64 count;
4484
4485 rcu_read_lock();
a8ad805c 4486 count = atomic_read(&task_css_set(current)->refcount);
fe693435
PM
4487 rcu_read_unlock();
4488 return count;
4489}
4490
2da8ca82 4491static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
7717f7ba 4492{
69d0206c 4493 struct cgrp_cset_link *link;
5abb8855 4494 struct css_set *cset;
e61734c5
TH
4495 char *name_buf;
4496
4497 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4498 if (!name_buf)
4499 return -ENOMEM;
7717f7ba 4500
96d365e0 4501 down_read(&css_set_rwsem);
7717f7ba 4502 rcu_read_lock();
5abb8855 4503 cset = rcu_dereference(current->cgroups);
69d0206c 4504 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
7717f7ba 4505 struct cgroup *c = link->cgrp;
59f5296b
TH
4506 const char *name = "?";
4507
e61734c5
TH
4508 if (c != cgroup_dummy_top) {
4509 cgroup_name(c, name_buf, NAME_MAX + 1);
4510 name = name_buf;
4511 }
7717f7ba 4512
2c6ab6d2
PM
4513 seq_printf(seq, "Root %d group %s\n",
4514 c->root->hierarchy_id, name);
7717f7ba
PM
4515 }
4516 rcu_read_unlock();
96d365e0 4517 up_read(&css_set_rwsem);
e61734c5 4518 kfree(name_buf);
7717f7ba
PM
4519 return 0;
4520}
4521
4522#define MAX_TASKS_SHOWN_PER_CSS 25
2da8ca82 4523static int cgroup_css_links_read(struct seq_file *seq, void *v)
7717f7ba 4524{
2da8ca82 4525 struct cgroup_subsys_state *css = seq_css(seq);
69d0206c 4526 struct cgrp_cset_link *link;
7717f7ba 4527
96d365e0 4528 down_read(&css_set_rwsem);
182446d0 4529 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
69d0206c 4530 struct css_set *cset = link->cset;
7717f7ba
PM
4531 struct task_struct *task;
4532 int count = 0;
5abb8855
TH
4533 seq_printf(seq, "css_set %p\n", cset);
4534 list_for_each_entry(task, &cset->tasks, cg_list) {
7717f7ba
PM
4535 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4536 seq_puts(seq, " ...\n");
4537 break;
4538 } else {
4539 seq_printf(seq, " task %d\n",
4540 task_pid_vnr(task));
4541 }
4542 }
4543 }
96d365e0 4544 up_read(&css_set_rwsem);
7717f7ba
PM
4545 return 0;
4546}
4547
182446d0 4548static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
fe693435 4549{
182446d0 4550 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
fe693435
PM
4551}
4552
4553static struct cftype debug_files[] = {
fe693435
PM
4554 {
4555 .name = "taskcount",
4556 .read_u64 = debug_taskcount_read,
4557 },
4558
4559 {
4560 .name = "current_css_set",
4561 .read_u64 = current_css_set_read,
4562 },
4563
4564 {
4565 .name = "current_css_set_refcount",
4566 .read_u64 = current_css_set_refcount_read,
4567 },
4568
7717f7ba
PM
4569 {
4570 .name = "current_css_set_cg_links",
2da8ca82 4571 .seq_show = current_css_set_cg_links_read,
7717f7ba
PM
4572 },
4573
4574 {
4575 .name = "cgroup_css_links",
2da8ca82 4576 .seq_show = cgroup_css_links_read,
7717f7ba
PM
4577 },
4578
fe693435
PM
4579 {
4580 .name = "releasable",
4581 .read_u64 = releasable_read,
4582 },
fe693435 4583
4baf6e33
TH
4584 { } /* terminate */
4585};
fe693435 4586
073219e9 4587struct cgroup_subsys debug_cgrp_subsys = {
92fb9748
TH
4588 .css_alloc = debug_css_alloc,
4589 .css_free = debug_css_free,
4baf6e33 4590 .base_cftypes = debug_files,
fe693435
PM
4591};
4592#endif /* CONFIG_CGROUP_DEBUG */
This page took 1.154643 seconds and 5 git commands to generate.