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