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