blob: 930e5aec1bcdb4656cbccedbeafc8ea9822a98c2 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * 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
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mm.h>
40#include <linux/mutex.h>
41#include <linux/mount.h>
42#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070043#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/rcupdate.h>
45#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040048#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050059#include <linux/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Tejun Heoe93ad192016-01-19 12:18:41 -050061#include <linux/cpuset.h>
Aditya Kalia79a9082016-01-29 02:54:06 -060062#include <linux/proc_ns.h>
63#include <linux/nsproxy.h>
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -070064#include <linux/file.h>
Johannes Weinere868a992018-10-26 15:06:31 -070065#include <linux/psi.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050066#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070067
Tejun Heoed1777d2016-08-10 11:23:44 -040068#define CREATE_TRACE_POINTS
69#include <trace/events/cgroup.h>
70
Tejun Heoe25e2cb2011-12-12 18:12:21 -080071/*
Tejun Heob1a21362013-11-29 10:42:58 -050072 * pidlists linger the following amount before being destroyed. The goal
73 * is avoiding frequent destruction in the middle of consecutive read calls
74 * Expiring in the middle is a performance problem not a correctness one.
75 * 1 sec should be enough.
76 */
77#define CGROUP_PIDLIST_DESTROY_DELAY HZ
78
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050079#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
80 MAX_CFTYPE_NAME + 2)
81
Tejun Heob1a21362013-11-29 10:42:58 -050082/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
85 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040086 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050087 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080088 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050089 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
90 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080091 */
Tejun Heo22194492013-04-07 09:29:51 -070092#ifdef CONFIG_PROVE_RCU
93DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040094DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -050095EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040096EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070097#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070098static DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040099static DEFINE_SPINLOCK(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -0700100#endif
101
Tejun Heo69e943b2014-02-08 10:36:58 -0500102/*
Tejun Heo15a4c832014-05-04 15:09:14 -0400103 * Protects cgroup_idr and css_idr so that IDs can be released without
104 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -0400105 */
106static DEFINE_SPINLOCK(cgroup_idr_lock);
107
108/*
Tejun Heo34c06252015-11-05 00:12:24 -0500109 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
110 * against file removal/re-creation across css hiding.
111 */
112static DEFINE_SPINLOCK(cgroup_file_kn_lock);
113
114/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500115 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
116 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
117 */
118static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700119
Tejun Heo1ed13282015-09-16 12:53:17 -0400120struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
121
Tejun Heo8353da12014-05-13 12:19:23 -0400122#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700123 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
124 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400125 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500126
Ben Blumaae8aab2010-03-10 15:22:07 -0800127/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500128 * cgroup destruction makes heavy use of work items and there can be a lot
129 * of concurrent destructions. Use a separate workqueue so that cgroup
130 * destruction work items don't end up filling up max_active of system_wq
131 * which may lead to deadlock.
132 */
133static struct workqueue_struct *cgroup_destroy_wq;
134
135/*
Tejun Heob1a21362013-11-29 10:42:58 -0500136 * pidlist destructions need to be flushed on cgroup destruction. Use a
137 * separate workqueue as flush domain.
138 */
139static struct workqueue_struct *cgroup_pidlist_destroy_wq;
140
Tejun Heo3ed80a62014-02-08 10:36:58 -0500141/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500142#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500143static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144#include <linux/cgroup_subsys.h>
145};
Tejun Heo073219e2014-02-08 10:36:58 -0500146#undef SUBSYS
147
148/* array of cgroup subsystem names */
149#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
150static const char *cgroup_subsys_name[] = {
151#include <linux/cgroup_subsys.h>
152};
153#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154
Tejun Heo49d1dc42015-09-18 11:56:28 -0400155/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
156#define SUBSYS(_x) \
157 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
158 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
159 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
160 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
161#include <linux/cgroup_subsys.h>
162#undef SUBSYS
163
164#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
165static struct static_key_true *cgroup_subsys_enabled_key[] = {
166#include <linux/cgroup_subsys.h>
167};
168#undef SUBSYS
169
170#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
171static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
172#include <linux/cgroup_subsys.h>
173};
174#undef SUBSYS
175
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400177 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700178 * unattached - it never has more than a single cgroup, and all tasks are
179 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700180 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400181struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400182EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700183
Tejun Heoa2dd4242014-03-19 10:23:55 -0400184/*
185 * The default hierarchy always exists but is hidden until mounted for the
186 * first time. This is for backward compatibility.
187 */
Tejun Heoa7165262016-02-23 10:00:50 -0500188static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189
Johannes Weiner223ffb22016-02-11 13:34:49 -0500190/* Controllers blocked by the commandline in v1 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500191static u16 cgroup_no_v1_mask;
Johannes Weiner223ffb22016-02-11 13:34:49 -0500192
Tejun Heo5533e012014-05-14 19:33:07 -0400193/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500194static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400195
Tejun Heof6d635ad2016-03-08 11:51:26 -0500196/* some controllers are implicitly enabled on the default hierarchy */
197static unsigned long cgrp_dfl_implicit_ss_mask;
198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* The list of hierarchy roots */
200
Tejun Heo9871bf92013-06-24 15:21:47 -0700201static LIST_HEAD(cgroup_roots);
202static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700203
Tejun Heo3417ae12014-02-08 10:37:01 -0500204/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700205static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700206
Li Zefan794611a2013-06-18 18:53:53 +0800207/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400208 * Assign a monotonically increasing serial number to csses. It guarantees
209 * cgroups with bigger numbers are newer than those with smaller numbers.
210 * Also, as csses are always appended to the parent's ->children list, it
211 * guarantees that sibling csses are always sorted in the ascending serial
212 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800213 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400214static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800215
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000216/*
217 * These bitmask flags indicate whether tasks in the fork and exit paths have
218 * fork/exit handlers to call. This avoids us having to do extra work in the
219 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700220 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500221static u16 have_fork_callback __read_mostly;
222static u16 have_exit_callback __read_mostly;
223static u16 have_free_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224
Aditya Kalia79a9082016-01-29 02:54:06 -0600225/* cgroup namespace for init task */
226struct cgroup_namespace init_cgroup_ns = {
227 .count = { .counter = 2, },
228 .user_ns = &init_user_ns,
229 .ns.ops = &cgroupns_operations,
230 .ns.inum = PROC_CGROUP_INIT_INO,
231 .root_cset = &init_css_set,
232};
233
Aleksa Sarai7e476822015-06-09 21:32:09 +1000234/* Ditto for the can_fork callback. */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500235static u16 have_canfork_callback __read_mostly;
Aleksa Sarai7e476822015-06-09 21:32:09 +1000236
Tejun Heo67e9c742015-11-16 11:13:34 -0500237static struct file_system_type cgroup2_fs_type;
Tejun Heoa14c6872014-07-15 11:05:09 -0400238static struct cftype cgroup_dfl_base_files[];
239static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700240
Tejun Heo6e5c8302016-02-22 22:25:47 -0500241static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
Tejun Heo945ba192016-03-03 09:58:00 -0500242static void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
Tejun Heo334c3672016-03-03 09:58:01 -0500243static int cgroup_apply_control(struct cgroup *cgrp);
244static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400245static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800246static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500247static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
248 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400249static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400250static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400251static int cgroup_addrm_files(struct cgroup_subsys_state *css,
252 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400253 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800254
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400255/**
256 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
257 * @ssid: subsys ID of interest
258 *
259 * cgroup_subsys_enabled() can only be used with literal subsys names which
260 * is fine for individual subsystems but unsuitable for cgroup core. This
261 * is slower static_key_enabled() based test indexed by @ssid.
262 */
263static bool cgroup_ssid_enabled(int ssid)
264{
Arnd Bergmanncfe02a82016-03-15 00:21:06 +0100265 if (CGROUP_SUBSYS_COUNT == 0)
266 return false;
267
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400268 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
269}
270
Johannes Weiner223ffb22016-02-11 13:34:49 -0500271static bool cgroup_ssid_no_v1(int ssid)
272{
273 return cgroup_no_v1_mask & (1 << ssid);
274}
275
Tejun Heo9e10a132015-09-18 11:56:28 -0400276/**
277 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
278 * @cgrp: the cgroup of interest
279 *
280 * The default hierarchy is the v2 interface of cgroup and this function
281 * can be used to test whether a cgroup is on the default hierarchy for
282 * cases where a subsystem should behave differnetly depending on the
283 * interface version.
284 *
285 * The set of behaviors which change on the default hierarchy are still
286 * being determined and the mount option is prefixed with __DEVEL__.
287 *
288 * List of changed behaviors:
289 *
290 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
291 * and "name" are disallowed.
292 *
293 * - When mounting an existing superblock, mount options should match.
294 *
295 * - Remount is disallowed.
296 *
297 * - rename(2) is disallowed.
298 *
299 * - "tasks" is removed. Everything should be at process granularity. Use
300 * "cgroup.procs" instead.
301 *
302 * - "cgroup.procs" is not sorted. pids will be unique unless they got
303 * recycled inbetween reads.
304 *
305 * - "release_agent" and "notify_on_release" are removed. Replacement
306 * notification mechanism will be implemented.
307 *
308 * - "cgroup.clone_children" is removed.
309 *
310 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
311 * and its descendants contain no task; otherwise, 1. The file also
312 * generates kernfs notification which can be monitored through poll and
313 * [di]notify when the value of the file changes.
314 *
315 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
316 * take masks of ancestors with non-empty cpus/mems, instead of being
317 * moved to an ancestor.
318 *
319 * - cpuset: a task can be moved into an empty cpuset, and again it takes
320 * masks of ancestors.
321 *
322 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
323 * is not created.
324 *
325 * - blkcg: blk-throttle becomes properly hierarchical.
326 *
327 * - debug: disallowed on the default hierarchy.
328 */
329static bool cgroup_on_dfl(const struct cgroup *cgrp)
330{
331 return cgrp->root == &cgrp_dfl_root;
332}
333
Tejun Heo6fa49182014-05-04 15:09:13 -0400334/* IDR wrappers which synchronize using cgroup_idr_lock */
335static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
336 gfp_t gfp_mask)
337{
338 int ret;
339
340 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400341 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800342 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400343 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400344 idr_preload_end();
345 return ret;
346}
347
348static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
349{
350 void *ret;
351
Tejun Heo54504e92014-05-13 12:10:59 -0400352 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400353 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400354 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400355 return ret;
356}
357
358static void cgroup_idr_remove(struct idr *idr, int id)
359{
Tejun Heo54504e92014-05-13 12:10:59 -0400360 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400361 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400362 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400363}
364
Tejun Heo5531dc92016-03-03 09:57:58 -0500365/* subsystems visibly enabled on a cgroup */
366static u16 cgroup_control(struct cgroup *cgrp)
367{
368 struct cgroup *parent = cgroup_parent(cgrp);
369 u16 root_ss_mask = cgrp->root->subsys_mask;
370
371 if (parent)
372 return parent->subtree_control;
373
374 if (cgroup_on_dfl(cgrp))
Tejun Heof6d635ad2016-03-08 11:51:26 -0500375 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
376 cgrp_dfl_implicit_ss_mask);
Tejun Heo5531dc92016-03-03 09:57:58 -0500377 return root_ss_mask;
378}
379
380/* subsystems enabled on a cgroup */
381static u16 cgroup_ss_mask(struct cgroup *cgrp)
382{
383 struct cgroup *parent = cgroup_parent(cgrp);
384
385 if (parent)
386 return parent->subtree_ss_mask;
387
388 return cgrp->root->subsys_mask;
389}
390
Tejun Heo95109b62013-08-08 20:11:27 -0400391/**
392 * cgroup_css - obtain a cgroup's css for the specified subsystem
393 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400394 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400395 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400396 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
397 * function must be called either under cgroup_mutex or rcu_read_lock() and
398 * the caller is responsible for pinning the returned css if it wants to
399 * keep accessing it outside the said locks. This function may return
400 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400401 */
402static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400403 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400404{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400405 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500406 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500407 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400408 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400409 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400410}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700411
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400412/**
413 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
414 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400415 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400416 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400417 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400418 * as the matching css of the nearest ancestor including self which has @ss
419 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
420 * function is guaranteed to return non-NULL css.
421 */
422static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
423 struct cgroup_subsys *ss)
424{
425 lockdep_assert_held(&cgroup_mutex);
426
427 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400428 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400429
Tejun Heoeeecbd12014-11-18 02:49:52 -0500430 /*
431 * This function is used while updating css associations and thus
Tejun Heo5531dc92016-03-03 09:57:58 -0500432 * can't test the csses directly. Test ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500433 */
Tejun Heo5531dc92016-03-03 09:57:58 -0500434 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
Tejun Heod51f39b2014-05-16 13:22:48 -0400435 cgrp = cgroup_parent(cgrp);
Tejun Heo5531dc92016-03-03 09:57:58 -0500436 if (!cgrp)
437 return NULL;
438 }
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400439
440 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700441}
442
Tejun Heoeeecbd12014-11-18 02:49:52 -0500443/**
444 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
445 * @cgrp: the cgroup of interest
446 * @ss: the subsystem of interest
447 *
448 * Find and get the effective css of @cgrp for @ss. The effective css is
449 * defined as the matching css of the nearest ancestor including self which
450 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
451 * the root css is returned, so this function always returns a valid css.
452 * The returned css must be put using css_put().
453 */
454struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
455 struct cgroup_subsys *ss)
456{
457 struct cgroup_subsys_state *css;
458
459 rcu_read_lock();
460
461 do {
462 css = cgroup_css(cgrp, ss);
463
464 if (css && css_tryget_online(css))
465 goto out_unlock;
466 cgrp = cgroup_parent(cgrp);
467 } while (cgrp);
468
469 css = init_css_set.subsys[ss->id];
470 css_get(css);
471out_unlock:
472 rcu_read_unlock();
473 return css;
474}
475
Paul Menageddbcc7e2007-10-18 23:39:30 -0700476/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700477static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700478{
Tejun Heo184faf32014-05-16 13:22:51 -0400479 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700480}
481
Tejun Heob4168642014-05-13 12:16:21 -0400482struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500483{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500484 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400485 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500486
487 /*
488 * This is open and unprotected implementation of cgroup_css().
489 * seq_css() is only called from a kernfs file operation which has
490 * an active reference on the file. Because all the subsystem
491 * files are drained before a css is disassociated with a cgroup,
492 * the matching css from the cgroup's subsys table is guaranteed to
493 * be and stay valid until the enclosing operation is complete.
494 */
495 if (cft->ss)
496 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
497 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400498 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500499}
Tejun Heob4168642014-05-13 12:16:21 -0400500EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500501
Adrian Bunke9685a02008-02-07 00:13:46 -0800502static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700503{
Paul Menagebd89aab2007-10-18 23:40:44 -0700504 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700505}
506
Tejun Heo30159ec2013-06-25 11:53:37 -0700507/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500508 * for_each_css - iterate all css's of a cgroup
509 * @css: the iteration cursor
510 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
511 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700512 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400513 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700514 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500515#define for_each_css(css, ssid, cgrp) \
516 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
517 if (!((css) = rcu_dereference_check( \
518 (cgrp)->subsys[(ssid)], \
519 lockdep_is_held(&cgroup_mutex)))) { } \
520 else
521
522/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400523 * for_each_e_css - iterate all effective css's of a cgroup
524 * @css: the iteration cursor
525 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
526 * @cgrp: the target cgroup to iterate css's of
527 *
528 * Should be called under cgroup_[tree_]mutex.
529 */
530#define for_each_e_css(css, ssid, cgrp) \
531 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
532 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
533 ; \
534 else
535
536/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500537 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700538 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500539 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700540 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500541#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500542 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
543 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700544
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000545/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500546 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000547 * @ss: the iteration cursor
548 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500549 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000550 *
551 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500552 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000553 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500554#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
555 unsigned long __ss_mask = (ss_mask); \
556 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000557 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500558 break; \
559 } \
560 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
561 (ss) = cgroup_subsys[ssid]; \
562 {
563
564#define while_each_subsys_mask() \
565 } \
566 } \
567} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000568
Tejun Heo985ed672014-03-19 10:23:53 -0400569/* iterate across the hierarchies */
570#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700571 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700572
Tejun Heof8f22e52014-04-23 11:13:16 -0400573/* iterate over child cgrps, lock should be held throughout iteration */
574#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400575 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400576 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400577 cgroup_is_dead(child); })) \
578 ; \
579 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700580
Tejun Heoce3f1d92016-03-03 09:57:59 -0500581/* walk live descendants in preorder */
582#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
583 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
584 if (({ lockdep_assert_held(&cgroup_mutex); \
585 (dsct) = (d_css)->cgroup; \
586 cgroup_is_dead(dsct); })) \
587 ; \
588 else
589
590/* walk live descendants in postorder */
591#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
592 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
593 if (({ lockdep_assert_held(&cgroup_mutex); \
594 (dsct) = (d_css)->cgroup; \
595 cgroup_is_dead(dsct); })) \
596 ; \
597 else
598
Paul Menage81a6a5c2007-10-18 23:39:38 -0700599static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700600static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700601
Tejun Heo69d02062013-06-12 21:04:50 -0700602/*
603 * A cgroup can be associated with multiple css_sets as different tasks may
604 * belong to different cgroups on different hierarchies. In the other
605 * direction, a css_set is naturally associated with multiple cgroups.
606 * This M:N relationship is represented by the following link structure
607 * which exists for each association and allows traversing the associations
608 * from both sides.
609 */
610struct cgrp_cset_link {
611 /* the cgroup and css_set this link associates */
612 struct cgroup *cgrp;
613 struct css_set *cset;
614
615 /* list of cgrp_cset_links anchored at cgrp->cset_links */
616 struct list_head cset_link;
617
618 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
619 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700620};
621
Tejun Heo172a2c062014-03-19 10:23:53 -0400622/*
623 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700624 * hierarchies being mounted. It contains a pointer to the root state
625 * for each subsystem. Also used to anchor the list of css_sets. Not
626 * reference-counted, to improve performance when child cgroups
627 * haven't been created.
628 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400629struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400630 .refcount = ATOMIC_INIT(1),
631 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
632 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
633 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
634 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
635 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400636 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400637};
Paul Menage817929e2007-10-18 23:39:36 -0700638
Tejun Heo172a2c062014-03-19 10:23:53 -0400639static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700640
Tejun Heo842b5972014-04-25 18:28:02 -0400641/**
Tejun Heo0de09422015-10-15 16:41:49 -0400642 * css_set_populated - does a css_set contain any tasks?
643 * @cset: target css_set
644 */
645static bool css_set_populated(struct css_set *cset)
646{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400647 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400648
649 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
650}
651
652/**
Tejun Heo842b5972014-04-25 18:28:02 -0400653 * cgroup_update_populated - updated populated count of a cgroup
654 * @cgrp: the target cgroup
655 * @populated: inc or dec populated count
656 *
Tejun Heo0de09422015-10-15 16:41:49 -0400657 * One of the css_sets associated with @cgrp is either getting its first
658 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
659 * count is propagated towards root so that a given cgroup's populated_cnt
660 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400661 *
662 * @cgrp's interface file "cgroup.populated" is zero if
663 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
664 * changes from or to zero, userland is notified that the content of the
665 * interface file has changed. This can be used to detect when @cgrp and
666 * its descendants become populated or empty.
667 */
668static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
669{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400670 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400671
672 do {
673 bool trigger;
674
675 if (populated)
676 trigger = !cgrp->populated_cnt++;
677 else
678 trigger = !--cgrp->populated_cnt;
679
680 if (!trigger)
681 break;
682
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400683 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400684 cgroup_file_notify(&cgrp->events_file);
685
Tejun Heod51f39b2014-05-16 13:22:48 -0400686 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400687 } while (cgrp);
688}
689
Tejun Heo0de09422015-10-15 16:41:49 -0400690/**
691 * css_set_update_populated - update populated state of a css_set
692 * @cset: target css_set
693 * @populated: whether @cset is populated or depopulated
694 *
695 * @cset is either getting the first task or losing the last. Update the
696 * ->populated_cnt of all associated cgroups accordingly.
697 */
698static void css_set_update_populated(struct css_set *cset, bool populated)
699{
700 struct cgrp_cset_link *link;
701
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400702 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400703
704 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
705 cgroup_update_populated(link->cgrp, populated);
706}
707
Tejun Heof6d7d042015-10-15 16:41:52 -0400708/**
709 * css_set_move_task - move a task from one css_set to another
710 * @task: task being moved
711 * @from_cset: css_set @task currently belongs to (may be NULL)
712 * @to_cset: new css_set @task is being moved to (may be NULL)
713 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
714 *
715 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
716 * css_set, @from_cset can be NULL. If @task is being disassociated
717 * instead of moved, @to_cset can be NULL.
718 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400719 * This function automatically handles populated_cnt updates and
720 * css_task_iter adjustments but the caller is responsible for managing
721 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400722 */
723static void css_set_move_task(struct task_struct *task,
724 struct css_set *from_cset, struct css_set *to_cset,
725 bool use_mg_tasks)
726{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400727 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400728
Tejun Heo20b454a2016-03-03 09:57:57 -0500729 if (to_cset && !css_set_populated(to_cset))
730 css_set_update_populated(to_cset, true);
731
Tejun Heof6d7d042015-10-15 16:41:52 -0400732 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400733 struct css_task_iter *it, *pos;
734
Tejun Heof6d7d042015-10-15 16:41:52 -0400735 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400736
737 /*
738 * @task is leaving, advance task iterators which are
739 * pointing to it so that they can resume at the next
740 * position. Advancing an iterator might remove it from
741 * the list, use safe walk. See css_task_iter_advance*()
742 * for details.
743 */
744 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
745 iters_node)
746 if (it->task_pos == &task->cg_list)
747 css_task_iter_advance(it);
748
Tejun Heof6d7d042015-10-15 16:41:52 -0400749 list_del_init(&task->cg_list);
750 if (!css_set_populated(from_cset))
751 css_set_update_populated(from_cset, false);
752 } else {
753 WARN_ON_ONCE(!list_empty(&task->cg_list));
754 }
755
756 if (to_cset) {
757 /*
758 * We are synchronized through cgroup_threadgroup_rwsem
759 * against PF_EXITING setting such that we can't race
760 * against cgroup_exit() changing the css_set to
761 * init_css_set and dropping the old one.
762 */
763 WARN_ON_ONCE(task->flags & PF_EXITING);
764
Johannes Weinere868a992018-10-26 15:06:31 -0700765 cgroup_move_task(task, to_cset);
Tejun Heof6d7d042015-10-15 16:41:52 -0400766 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
767 &to_cset->tasks);
768 }
769}
770
Paul Menage7717f7b2009-09-23 15:56:22 -0700771/*
772 * hash table for cgroup groups. This improves the performance to find
773 * an existing css_set. This hash doesn't (currently) take into
774 * account cgroups in empty hierarchies.
775 */
Li Zefan472b1052008-04-29 01:00:11 -0700776#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800777static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700778
Li Zefan0ac801f2013-01-10 11:49:27 +0800779static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700780{
Li Zefan0ac801f2013-01-10 11:49:27 +0800781 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700782 struct cgroup_subsys *ss;
783 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700784
Tejun Heo30159ec2013-06-25 11:53:37 -0700785 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800786 key += (unsigned long)css[i];
787 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700788
Li Zefan0ac801f2013-01-10 11:49:27 +0800789 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700790}
791
Zefan Lia25eb522014-09-19 16:51:00 +0800792static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700793{
Tejun Heo69d02062013-06-12 21:04:50 -0700794 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400795 struct cgroup_subsys *ss;
796 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700797
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400798 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500799
800 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700801 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700802
Tejun Heo53254f92015-11-23 14:55:41 -0500803 /* This css_set is dead. unlink it and release cgroup and css refs */
804 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400805 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500806 css_put(cset->subsys[ssid]);
807 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700808 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700809 css_set_count--;
810
Tejun Heo69d02062013-06-12 21:04:50 -0700811 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700812 list_del(&link->cset_link);
813 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400814 if (cgroup_parent(link->cgrp))
815 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700816 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700817 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700818
Tejun Heo5abb8852013-06-12 21:04:49 -0700819 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700820}
821
Zefan Lia25eb522014-09-19 16:51:00 +0800822static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500823{
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -0300824 unsigned long flags;
825
Tejun Heo89c55092014-02-13 06:58:40 -0500826 /*
827 * Ensure that the refcount doesn't hit zero while any readers
828 * can see it. Similar to atomic_dec_and_lock(), but for an
829 * rwlock
830 */
831 if (atomic_add_unless(&cset->refcount, -1, 1))
832 return;
833
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -0300834 spin_lock_irqsave(&css_set_lock, flags);
Zefan Lia25eb522014-09-19 16:51:00 +0800835 put_css_set_locked(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -0300836 spin_unlock_irqrestore(&css_set_lock, flags);
Tejun Heo89c55092014-02-13 06:58:40 -0500837}
838
Paul Menage817929e2007-10-18 23:39:36 -0700839/*
840 * refcounted get/put for css_set objects
841 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700842static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700843{
Tejun Heo5abb8852013-06-12 21:04:49 -0700844 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700845}
846
Tejun Heob326f9d2013-06-24 15:21:48 -0700847/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700848 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700849 * @cset: candidate css_set being tested
850 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700851 * @new_cgrp: cgroup that's being entered by the task
852 * @template: desired set of css pointers in css_set (pre-calculated)
853 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800854 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700855 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
856 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700857static bool compare_css_sets(struct css_set *cset,
858 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700859 struct cgroup *new_cgrp,
860 struct cgroup_subsys_state *template[])
861{
862 struct list_head *l1, *l2;
863
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400864 /*
865 * On the default hierarchy, there can be csets which are
866 * associated with the same set of cgroups but different csses.
867 * Let's first ensure that csses match.
868 */
869 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700870 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700871
872 /*
873 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400874 * different cgroups in hierarchies. As different cgroups may
875 * share the same effective css, this comparison is always
876 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700877 */
Tejun Heo69d02062013-06-12 21:04:50 -0700878 l1 = &cset->cgrp_links;
879 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700880 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700881 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700882 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700883
884 l1 = l1->next;
885 l2 = l2->next;
886 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700887 if (l1 == &cset->cgrp_links) {
888 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700889 break;
890 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700891 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700892 }
893 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700894 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
895 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
896 cgrp1 = link1->cgrp;
897 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700898 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700899 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700900
901 /*
902 * If this hierarchy is the hierarchy of the cgroup
903 * that's changing, then we need to check that this
904 * css_set points to the new cgroup; if it's any other
905 * hierarchy, then this css_set should point to the
906 * same cgroup as the old css_set.
907 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700908 if (cgrp1->root == new_cgrp->root) {
909 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700910 return false;
911 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700912 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700913 return false;
914 }
915 }
916 return true;
917}
918
Tejun Heob326f9d2013-06-24 15:21:48 -0700919/**
920 * find_existing_css_set - init css array and find the matching css_set
921 * @old_cset: the css_set that we're using before the cgroup transition
922 * @cgrp: the cgroup that we're moving into
923 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700924 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700925static struct css_set *find_existing_css_set(struct css_set *old_cset,
926 struct cgroup *cgrp,
927 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700928{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400929 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700930 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700931 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800932 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700933 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700934
Ben Blumaae8aab2010-03-10 15:22:07 -0800935 /*
936 * Build the set of subsystem state objects that we want to see in the
937 * new css_set. while subsystems can change globally, the entries here
938 * won't change, so no need for locking.
939 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700940 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400941 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400942 /*
943 * @ss is in this hierarchy, so we want the
944 * effective css from @cgrp.
945 */
946 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700947 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400948 /*
949 * @ss is not in this hierarchy, so we don't want
950 * to change the css.
951 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700952 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700953 }
954 }
955
Li Zefan0ac801f2013-01-10 11:49:27 +0800956 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700957 hash_for_each_possible(css_set_table, cset, hlist, key) {
958 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700959 continue;
960
961 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700962 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700963 }
Paul Menage817929e2007-10-18 23:39:36 -0700964
965 /* No existing cgroup group matched */
966 return NULL;
967}
968
Tejun Heo69d02062013-06-12 21:04:50 -0700969static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700970{
Tejun Heo69d02062013-06-12 21:04:50 -0700971 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700972
Tejun Heo69d02062013-06-12 21:04:50 -0700973 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
974 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700975 kfree(link);
976 }
977}
978
Tejun Heo69d02062013-06-12 21:04:50 -0700979/**
980 * allocate_cgrp_cset_links - allocate cgrp_cset_links
981 * @count: the number of links to allocate
982 * @tmp_links: list_head the allocated links are put on
983 *
984 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
985 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700986 */
Tejun Heo69d02062013-06-12 21:04:50 -0700987static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700988{
Tejun Heo69d02062013-06-12 21:04:50 -0700989 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700990 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700991
992 INIT_LIST_HEAD(tmp_links);
993
Li Zefan36553432008-07-29 22:33:19 -0700994 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700995 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700996 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700997 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700998 return -ENOMEM;
999 }
Tejun Heo69d02062013-06-12 21:04:50 -07001000 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001001 }
1002 return 0;
1003}
1004
Li Zefanc12f65d2009-01-07 18:07:42 -08001005/**
1006 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -07001007 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -07001008 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -08001009 * @cgrp: the destination cgroup
1010 */
Tejun Heo69d02062013-06-12 21:04:50 -07001011static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1012 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -08001013{
Tejun Heo69d02062013-06-12 21:04:50 -07001014 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -08001015
Tejun Heo69d02062013-06-12 21:04:50 -07001016 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -04001017
1018 if (cgroup_on_dfl(cgrp))
1019 cset->dfl_cgrp = cgrp;
1020
Tejun Heo69d02062013-06-12 21:04:50 -07001021 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1022 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07001023 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -04001024
Paul Menage7717f7b2009-09-23 15:56:22 -07001025 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001026 * Always add links to the tail of the lists so that the lists are
1027 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001028 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001029 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001030 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001031
1032 if (cgroup_parent(cgrp))
1033 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001034}
1035
Tejun Heob326f9d2013-06-24 15:21:48 -07001036/**
1037 * find_css_set - return a new css_set with one cgroup updated
1038 * @old_cset: the baseline css_set
1039 * @cgrp: the cgroup to be updated
1040 *
1041 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1042 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001043 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001044static struct css_set *find_css_set(struct css_set *old_cset,
1045 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001046{
Tejun Heob326f9d2013-06-24 15:21:48 -07001047 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001048 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001049 struct list_head tmp_links;
1050 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001051 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001052 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001053 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001054
Tejun Heob326f9d2013-06-24 15:21:48 -07001055 lockdep_assert_held(&cgroup_mutex);
1056
Paul Menage817929e2007-10-18 23:39:36 -07001057 /* First see if we already have a cgroup group that matches
1058 * the desired set */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001059 spin_lock_irq(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001060 cset = find_existing_css_set(old_cset, cgrp, template);
1061 if (cset)
1062 get_css_set(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001063 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001064
Tejun Heo5abb8852013-06-12 21:04:49 -07001065 if (cset)
1066 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001067
Tejun Heof4f4be22013-06-12 21:04:51 -07001068 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001069 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001070 return NULL;
1071
Tejun Heo69d02062013-06-12 21:04:50 -07001072 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001073 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001074 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001075 return NULL;
1076 }
1077
Tejun Heo5abb8852013-06-12 21:04:49 -07001078 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001079 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001080 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001081 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001082 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001083 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001084 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001085 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001086
1087 /* Copy the set of subsystem state objects generated in
1088 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001089 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001090
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001091 spin_lock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001092 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001093 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001094 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001095
Paul Menage7717f7b2009-09-23 15:56:22 -07001096 if (c->root == cgrp->root)
1097 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001098 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001099 }
Paul Menage817929e2007-10-18 23:39:36 -07001100
Tejun Heo69d02062013-06-12 21:04:50 -07001101 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001102
Paul Menage817929e2007-10-18 23:39:36 -07001103 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001104
Tejun Heo2d8f2432014-04-23 11:13:15 -04001105 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001106 key = css_set_hash(cset->subsys);
1107 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001108
Tejun Heo53254f92015-11-23 14:55:41 -05001109 for_each_subsys(ss, ssid) {
1110 struct cgroup_subsys_state *css = cset->subsys[ssid];
1111
Tejun Heo2d8f2432014-04-23 11:13:15 -04001112 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001113 &css->cgroup->e_csets[ssid]);
1114 css_get(css);
1115 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001116
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001117 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001118
Tejun Heo5abb8852013-06-12 21:04:49 -07001119 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001120}
1121
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001122static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001123{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001124 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001125
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001126 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001127}
1128
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001129static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001130{
1131 int id;
1132
1133 lockdep_assert_held(&cgroup_mutex);
1134
Tejun Heo985ed672014-03-19 10:23:53 -04001135 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001136 if (id < 0)
1137 return id;
1138
1139 root->hierarchy_id = id;
1140 return 0;
1141}
1142
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001143static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001144{
1145 lockdep_assert_held(&cgroup_mutex);
1146
Johannes Weiner8c8a5502016-06-17 12:23:59 -04001147 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heof2e85d52014-02-11 11:52:49 -05001148}
1149
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001150static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001151{
1152 if (root) {
Tejun Heof2e85d52014-02-11 11:52:49 -05001153 idr_destroy(&root->cgroup_idr);
1154 kfree(root);
1155 }
1156}
1157
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001158static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001159{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001160 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001161 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001162
Tejun Heoed1777d2016-08-10 11:23:44 -04001163 trace_cgroup_destroy_root(root);
1164
Tejun Heo334c3672016-03-03 09:58:01 -05001165 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -05001166
Tejun Heo776f02f2014-02-12 09:29:50 -05001167 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001168 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001169
Tejun Heof2e85d52014-02-11 11:52:49 -05001170 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo334c3672016-03-03 09:58:01 -05001171 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -05001172
1173 /*
1174 * Release all the links from cset_links to this hierarchy's
1175 * root cgroup
1176 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001177 spin_lock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001178
1179 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1180 list_del(&link->cset_link);
1181 list_del(&link->cgrp_link);
1182 kfree(link);
1183 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001184
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001185 spin_unlock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001186
1187 if (!list_empty(&root->root_list)) {
1188 list_del(&root->root_list);
1189 cgroup_root_count--;
1190 }
1191
1192 cgroup_exit_root_id(root);
1193
1194 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001195
Tejun Heo2bd59d42014-02-11 11:52:49 -05001196 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001197 cgroup_free_root(root);
1198}
1199
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001200/*
1201 * look up cgroup associated with current task's cgroup namespace on the
1202 * specified hierarchy
1203 */
1204static struct cgroup *
1205current_cgns_cgroup_from_root(struct cgroup_root *root)
1206{
1207 struct cgroup *res = NULL;
1208 struct css_set *cset;
1209
1210 lockdep_assert_held(&css_set_lock);
1211
1212 rcu_read_lock();
1213
1214 cset = current->nsproxy->cgroup_ns->root_cset;
1215 if (cset == &init_css_set) {
1216 res = &root->cgrp;
1217 } else {
1218 struct cgrp_cset_link *link;
1219
1220 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1221 struct cgroup *c = link->cgrp;
1222
1223 if (c->root == root) {
1224 res = c;
1225 break;
1226 }
1227 }
1228 }
1229 rcu_read_unlock();
1230
1231 BUG_ON(!res);
1232 return res;
1233}
1234
Tejun Heoceb6a082014-02-25 10:04:02 -05001235/* look up cgroup associated with given css_set on the specified hierarchy */
1236static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001237 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001238{
Paul Menage7717f7b2009-09-23 15:56:22 -07001239 struct cgroup *res = NULL;
1240
Tejun Heo96d365e2014-02-13 06:58:40 -05001241 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001242 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001243
Tejun Heo5abb8852013-06-12 21:04:49 -07001244 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001245 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001246 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001247 struct cgrp_cset_link *link;
1248
1249 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001250 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001251
Paul Menage7717f7b2009-09-23 15:56:22 -07001252 if (c->root == root) {
1253 res = c;
1254 break;
1255 }
1256 }
1257 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001258
Paul Menage7717f7b2009-09-23 15:56:22 -07001259 BUG_ON(!res);
1260 return res;
1261}
1262
1263/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001264 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001265 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001266 */
1267static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001268 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001269{
1270 /*
1271 * No need to lock the task - since we hold cgroup_mutex the
1272 * task can't change groups, so the only thing that can happen
1273 * is that it exits and its css is set back to init_css_set.
1274 */
1275 return cset_cgroup_from_root(task_css_set(task), root);
1276}
1277
1278/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279 * A task must hold cgroup_mutex to modify cgroups.
1280 *
1281 * Any task can increment and decrement the count field without lock.
1282 * So in general, code holding cgroup_mutex can't rely on the count
1283 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001284 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001285 * means that no tasks are currently attached, therefore there is no
1286 * way a task attached to that cgroup can fork (the other way to
1287 * increment the count). So code holding cgroup_mutex can safely
1288 * assume that if the count is zero, it will stay zero. Similarly, if
1289 * a task holds cgroup_mutex on a cgroup with zero count, it
1290 * knows that the cgroup won't be removed, as cgroup_rmdir()
1291 * needs that mutex.
1292 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293 * A cgroup can only be deleted if both its 'count' of using tasks
1294 * is zero, and its list of 'children' cgroups is empty. Since all
1295 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001296 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001297 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001298 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 *
1300 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001301 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 */
1303
Tejun Heo2bd59d42014-02-11 11:52:49 -05001304static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001305static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001306
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001307static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1308 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001309{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001310 struct cgroup_subsys *ss = cft->ss;
1311
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001312 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1313 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1314 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001315 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1316 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001317 else
1318 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1319 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320}
1321
Tejun Heof2e85d52014-02-11 11:52:49 -05001322/**
1323 * cgroup_file_mode - deduce file mode of a control file
1324 * @cft: the control file in question
1325 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001326 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001327 */
1328static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001329{
Tejun Heof2e85d52014-02-11 11:52:49 -05001330 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001331
Tejun Heof2e85d52014-02-11 11:52:49 -05001332 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1333 mode |= S_IRUGO;
1334
Tejun Heo7dbdb192015-09-18 17:54:23 -04001335 if (cft->write_u64 || cft->write_s64 || cft->write) {
1336 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1337 mode |= S_IWUGO;
1338 else
1339 mode |= S_IWUSR;
1340 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001341
1342 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001343}
1344
Tejun Heoa9746d82014-05-13 12:19:22 -04001345/**
Tejun Heo8699b772016-02-22 22:25:46 -05001346 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001347 * @subtree_control: the new subtree_control mask to consider
Tejun Heo5ced2512016-03-03 09:58:01 -05001348 * @this_ss_mask: available subsystems
Tejun Heoaf0ba672014-07-08 18:02:57 -04001349 *
1350 * On the default hierarchy, a subsystem may request other subsystems to be
1351 * enabled together through its ->depends_on mask. In such cases, more
1352 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1353 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001354 * This function calculates which subsystems need to be enabled if
Tejun Heo5ced2512016-03-03 09:58:01 -05001355 * @subtree_control is to be applied while restricted to @this_ss_mask.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001356 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001357static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
Tejun Heo667c2492014-07-08 18:02:56 -04001358{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001359 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001360 struct cgroup_subsys *ss;
1361 int ssid;
1362
1363 lockdep_assert_held(&cgroup_mutex);
1364
Tejun Heof6d635ad2016-03-08 11:51:26 -05001365 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1366
Tejun Heoaf0ba672014-07-08 18:02:57 -04001367 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001368 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001369
Tejun Heob4e0eea2016-02-22 22:25:46 -05001370 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001371 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001372 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001373
1374 /*
1375 * Mask out subsystems which aren't available. This can
1376 * happen only if some depended-upon subsystems were bound
1377 * to non-default hierarchies.
1378 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001379 new_ss_mask &= this_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001380
1381 if (new_ss_mask == cur_ss_mask)
1382 break;
1383 cur_ss_mask = new_ss_mask;
1384 }
1385
Tejun Heo0f060de2014-11-18 02:49:50 -05001386 return cur_ss_mask;
1387}
1388
1389/**
Tejun Heoa9746d82014-05-13 12:19:22 -04001390 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1391 * @kn: the kernfs_node being serviced
1392 *
1393 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1394 * the method finishes if locking succeeded. Note that once this function
1395 * returns the cgroup returned by cgroup_kn_lock_live() may become
1396 * inaccessible any time. If the caller intends to continue to access the
1397 * cgroup, it should pin it before invoking this function.
1398 */
1399static void cgroup_kn_unlock(struct kernfs_node *kn)
1400{
1401 struct cgroup *cgrp;
1402
1403 if (kernfs_type(kn) == KERNFS_DIR)
1404 cgrp = kn->priv;
1405 else
1406 cgrp = kn->parent->priv;
1407
1408 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001409
1410 kernfs_unbreak_active_protection(kn);
1411 cgroup_put(cgrp);
1412}
1413
1414/**
1415 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1416 * @kn: the kernfs_node being serviced
Tejun Heo945ba192016-03-03 09:58:00 -05001417 * @drain_offline: perform offline draining on the cgroup
Tejun Heoa9746d82014-05-13 12:19:22 -04001418 *
1419 * This helper is to be used by a cgroup kernfs method currently servicing
1420 * @kn. It breaks the active protection, performs cgroup locking and
1421 * verifies that the associated cgroup is alive. Returns the cgroup if
1422 * alive; otherwise, %NULL. A successful return should be undone by a
Tejun Heo945ba192016-03-03 09:58:00 -05001423 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1424 * cgroup is drained of offlining csses before return.
Tejun Heoa9746d82014-05-13 12:19:22 -04001425 *
1426 * Any cgroup kernfs method implementation which requires locking the
1427 * associated cgroup should use this helper. It avoids nesting cgroup
1428 * locking under kernfs active protection and allows all kernfs operations
1429 * including self-removal.
1430 */
Tejun Heo945ba192016-03-03 09:58:00 -05001431static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn,
1432 bool drain_offline)
Tejun Heoa9746d82014-05-13 12:19:22 -04001433{
1434 struct cgroup *cgrp;
1435
1436 if (kernfs_type(kn) == KERNFS_DIR)
1437 cgrp = kn->priv;
1438 else
1439 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440
Tejun Heo2bd59d42014-02-11 11:52:49 -05001441 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001442 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001443 * active_ref. cgroup liveliness check alone provides enough
1444 * protection against removal. Ensure @cgrp stays accessible and
1445 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001446 */
Li Zefanaa323622014-09-04 14:43:38 +08001447 if (!cgroup_tryget(cgrp))
1448 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001449 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450
Tejun Heo945ba192016-03-03 09:58:00 -05001451 if (drain_offline)
1452 cgroup_lock_and_drain_offline(cgrp);
1453 else
1454 mutex_lock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001455
1456 if (!cgroup_is_dead(cgrp))
1457 return cgrp;
1458
1459 cgroup_kn_unlock(kn);
1460 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461}
1462
Li Zefan2739d3c2013-01-21 18:18:33 +08001463static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001465 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466
Tejun Heo01f64742014-05-13 12:19:23 -04001467 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001468
1469 if (cft->file_offset) {
1470 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1471 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1472
1473 spin_lock_irq(&cgroup_file_kn_lock);
1474 cfile->kn = NULL;
1475 spin_unlock_irq(&cgroup_file_kn_lock);
1476 }
1477
Tejun Heo2bd59d42014-02-11 11:52:49 -05001478 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001479}
1480
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001481/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001482 * css_clear_dir - remove subsys files in a cgroup directory
1483 * @css: taget css
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001484 */
Tejun Heo334c3672016-03-03 09:58:01 -05001485static void css_clear_dir(struct cgroup_subsys_state *css)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001486{
Tejun Heo334c3672016-03-03 09:58:01 -05001487 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001488 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001489
Tejun Heo88cb04b2016-03-03 09:57:58 -05001490 if (!(css->flags & CSS_VISIBLE))
1491 return;
1492
1493 css->flags &= ~CSS_VISIBLE;
1494
Tejun Heo4df8dc92015-09-18 17:54:23 -04001495 list_for_each_entry(cfts, &css->ss->cfts, node)
1496 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497}
1498
Tejun Heoccdca212015-09-18 17:54:23 -04001499/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001500 * css_populate_dir - create subsys files in a cgroup directory
1501 * @css: target css
Tejun Heoccdca212015-09-18 17:54:23 -04001502 *
1503 * On failure, no file is added.
1504 */
Tejun Heo334c3672016-03-03 09:58:01 -05001505static int css_populate_dir(struct cgroup_subsys_state *css)
Tejun Heoccdca212015-09-18 17:54:23 -04001506{
Tejun Heo334c3672016-03-03 09:58:01 -05001507 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001508 struct cftype *cfts, *failed_cfts;
1509 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001510
Tejun Heo03970d32016-03-03 09:58:00 -05001511 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
Tejun Heo88cb04b2016-03-03 09:57:58 -05001512 return 0;
1513
Tejun Heo4df8dc92015-09-18 17:54:23 -04001514 if (!css->ss) {
1515 if (cgroup_on_dfl(cgrp))
1516 cfts = cgroup_dfl_base_files;
1517 else
1518 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001519
Tejun Heo4df8dc92015-09-18 17:54:23 -04001520 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1521 }
Tejun Heoccdca212015-09-18 17:54:23 -04001522
Tejun Heo4df8dc92015-09-18 17:54:23 -04001523 list_for_each_entry(cfts, &css->ss->cfts, node) {
1524 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1525 if (ret < 0) {
1526 failed_cfts = cfts;
1527 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001528 }
1529 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001530
1531 css->flags |= CSS_VISIBLE;
1532
Tejun Heoccdca212015-09-18 17:54:23 -04001533 return 0;
1534err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001535 list_for_each_entry(cfts, &css->ss->cfts, node) {
1536 if (cfts == failed_cfts)
1537 break;
1538 cgroup_addrm_files(css, cgrp, cfts, false);
1539 }
Tejun Heoccdca212015-09-18 17:54:23 -04001540 return ret;
1541}
1542
Tejun Heo6e5c8302016-02-22 22:25:47 -05001543static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544{
Tejun Heo1ada4832015-09-18 17:54:23 -04001545 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001546 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001547 int ssid, i, ret;
Waiman Longabd8bb72b2021-09-18 18:53:08 -04001548 u16 dfl_disable_ss_mask = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
Tejun Heoace2bee2014-02-11 11:52:47 -05001550 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001551
Tejun Heob4e0eea2016-02-22 22:25:46 -05001552 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heof6d635ad2016-03-08 11:51:26 -05001553 /*
1554 * If @ss has non-root csses attached to it, can't move.
1555 * If @ss is an implicit controller, it is exempt from this
1556 * rule and can be stolen.
1557 */
1558 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1559 !ss->implicit_on_dfl)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001560 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561
Tejun Heo5df36032014-03-19 10:23:54 -04001562 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001563 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001564 return -EBUSY;
Waiman Longabd8bb72b2021-09-18 18:53:08 -04001565
1566 /*
1567 * Collect ssid's that need to be disabled from default
1568 * hierarchy.
1569 */
1570 if (ss->root == &cgrp_dfl_root)
1571 dfl_disable_ss_mask |= 1 << ssid;
1572
Tejun Heob4e0eea2016-02-22 22:25:46 -05001573 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
Waiman Longabd8bb72b2021-09-18 18:53:08 -04001575 if (dfl_disable_ss_mask) {
1576 struct cgroup *scgrp = &cgrp_dfl_root.cgrp;
1577
1578 /*
1579 * Controllers from default hierarchy that need to be rebound
1580 * are all disabled together in one go.
1581 */
1582 cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask;
1583 WARN_ON(cgroup_apply_control(scgrp));
1584 cgroup_finalize_control(scgrp, 0);
1585 }
1586
Tejun Heob4e0eea2016-02-22 22:25:46 -05001587 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001588 struct cgroup_root *src_root = ss->root;
1589 struct cgroup *scgrp = &src_root->cgrp;
1590 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001591 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001592
Tejun Heo1ada4832015-09-18 17:54:23 -04001593 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001594
Waiman Longabd8bb72b2021-09-18 18:53:08 -04001595 if (src_root != &cgrp_dfl_root) {
1596 /* disable from the source */
1597 src_root->subsys_mask &= ~(1 << ssid);
1598 WARN_ON(cgroup_apply_control(scgrp));
1599 cgroup_finalize_control(scgrp, 0);
1600 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001601
Tejun Heo334c3672016-03-03 09:58:01 -05001602 /* rebind */
Tejun Heo1ada4832015-09-18 17:54:23 -04001603 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1604 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001605 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001606 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001607
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001608 spin_lock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001609 hash_for_each(css_set_table, i, cset, hlist)
1610 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001611 &dcgrp->e_csets[ss->id]);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001612 spin_unlock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001613
Tejun Heobd53d612014-04-23 11:13:16 -04001614 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001615 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001616 if (dst_root == &cgrp_dfl_root) {
1617 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1618 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001619 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001620 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001621 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001622
Tejun Heo334c3672016-03-03 09:58:01 -05001623 ret = cgroup_apply_control(dcgrp);
1624 if (ret)
1625 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1626 ss->name, ret);
1627
Tejun Heo5df36032014-03-19 10:23:54 -04001628 if (ss->bind)
1629 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001630 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631
Tejun Heo1ada4832015-09-18 17:54:23 -04001632 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 return 0;
1634}
1635
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001636static int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1637 struct kernfs_root *kf_root)
1638{
Felipe Balbi09be4c82016-05-12 12:34:38 +03001639 int len = 0;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001640 char *buf = NULL;
1641 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1642 struct cgroup *ns_cgroup;
1643
1644 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1645 if (!buf)
1646 return -ENOMEM;
1647
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001648 spin_lock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001649 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1650 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001651 spin_unlock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001652
1653 if (len >= PATH_MAX)
1654 len = -ERANGE;
1655 else if (len > 0) {
1656 seq_escape(sf, buf, " \t\n\\");
1657 len = 0;
1658 }
1659 kfree(buf);
1660 return len;
1661}
1662
Tejun Heo2bd59d42014-02-11 11:52:49 -05001663static int cgroup_show_options(struct seq_file *seq,
1664 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001666 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001668 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669
Tejun Heod98817d2015-08-18 13:58:16 -07001670 if (root != &cgrp_dfl_root)
1671 for_each_subsys(ss, ssid)
1672 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001673 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001674 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001676 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001677 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001678
1679 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001680 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001681 seq_show_option(seq, "release_agent",
1682 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001683 spin_unlock(&release_agent_path_lock);
1684
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001685 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001686 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001687 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001688 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 return 0;
1690}
1691
1692struct cgroup_sb_opts {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001693 u16 subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001694 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001695 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001696 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001697 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001698 /* User explicitly requested empty subsystem */
1699 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700};
1701
Ben Blumcf5d5942010-03-10 15:22:09 -08001702static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001704 char *token, *o = data;
1705 bool all_ss = false, one_ss = false;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001706 u16 mask = U16_MAX;
Tejun Heo30159ec2013-06-25 11:53:37 -07001707 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001708 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001709 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001710
1711#ifdef CONFIG_CPUSETS
Tejun Heo6e5c8302016-02-22 22:25:47 -05001712 mask = ~((u16)1 << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001713#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714
Paul Menagec6d57f32009-09-23 15:56:19 -07001715 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716
1717 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001718 nr_opts++;
1719
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720 if (!*token)
1721 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001722 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001723 /* Explicitly have no subsystems */
1724 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001725 continue;
1726 }
1727 if (!strcmp(token, "all")) {
1728 /* Mutually exclusive option 'all' + subsystem name */
1729 if (one_ss)
1730 return -EINVAL;
1731 all_ss = true;
1732 continue;
1733 }
1734 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001735 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001736 continue;
1737 }
1738 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001739 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001740 continue;
1741 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001742 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001743 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001744 continue;
1745 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001746 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001747 /* Specifying two release agents is forbidden */
1748 if (opts->release_agent)
1749 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001750 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001751 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001752 if (!opts->release_agent)
1753 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001754 continue;
1755 }
1756 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001757 const char *name = token + 5;
1758 /* Can't specify an empty name */
1759 if (!strlen(name))
1760 return -EINVAL;
1761 /* Must match [\w.-]+ */
1762 for (i = 0; i < strlen(name); i++) {
1763 char c = name[i];
1764 if (isalnum(c))
1765 continue;
1766 if ((c == '.') || (c == '-') || (c == '_'))
1767 continue;
1768 return -EINVAL;
1769 }
1770 /* Specifying two names is forbidden */
1771 if (opts->name)
1772 return -EINVAL;
1773 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001774 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001775 GFP_KERNEL);
1776 if (!opts->name)
1777 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001778
1779 continue;
1780 }
1781
Tejun Heo30159ec2013-06-25 11:53:37 -07001782 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001783 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001784 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001785 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001786 continue;
Johannes Weiner223ffb22016-02-11 13:34:49 -05001787 if (cgroup_ssid_no_v1(i))
1788 continue;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001789
1790 /* Mutually exclusive option 'all' + subsystem name */
1791 if (all_ss)
1792 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001793 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001794 one_ss = true;
1795
1796 break;
1797 }
1798 if (i == CGROUP_SUBSYS_COUNT)
1799 return -ENOENT;
1800 }
1801
Li Zefanf9ab5b52009-06-17 16:26:33 -07001802 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001803 * If the 'all' option was specified select all the subsystems,
1804 * otherwise if 'none', 'name=' and a subsystem name options were
1805 * not specified, let's default to 'all'
1806 */
1807 if (all_ss || (!one_ss && !opts->none && !opts->name))
1808 for_each_subsys(ss, i)
Johannes Weiner223ffb22016-02-11 13:34:49 -05001809 if (cgroup_ssid_enabled(i) && !cgroup_ssid_no_v1(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001810 opts->subsys_mask |= (1 << i);
1811
1812 /*
1813 * We either have to specify by name or by subsystems. (So all
1814 * empty hierarchies must have a name).
1815 */
1816 if (!opts->subsys_mask && !opts->name)
1817 return -EINVAL;
1818
1819 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001820 * Option noprefix was introduced just for backward compatibility
1821 * with the old cpuset, so we allow noprefix only if mounting just
1822 * the cpuset subsystem.
1823 */
Tejun Heo93438622013-04-14 20:15:25 -07001824 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001825 return -EINVAL;
1826
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001827 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001828 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001829 return -EINVAL;
1830
Paul Menageddbcc7e2007-10-18 23:39:30 -07001831 return 0;
1832}
1833
Tejun Heo2bd59d42014-02-11 11:52:49 -05001834static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835{
1836 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001837 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Eric W. Biederman7e33a0a2022-01-20 11:04:01 -06001838 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001839 struct cgroup_sb_opts opts;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001840 u16 added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001841
Tejun Heoaa6ec292014-07-09 10:08:08 -04001842 if (root == &cgrp_dfl_root) {
1843 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001844 return -EINVAL;
1845 }
1846
Tejun Heo334c3672016-03-03 09:58:01 -05001847 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001848
1849 /* See what subsystems are wanted */
1850 ret = parse_cgroupfs_options(data, &opts);
1851 if (ret)
1852 goto out_unlock;
1853
Tejun Heof392e512014-04-23 11:13:14 -04001854 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001855 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001856 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001857
Eric W. Biederman7e33a0a2022-01-20 11:04:01 -06001858 /* See cgroup_mount release_agent handling */
1859 if (opts.release_agent &&
1860 ((ns->user_ns != &init_user_ns) || !capable(CAP_SYS_ADMIN))) {
1861 ret = -EINVAL;
1862 goto out_unlock;
1863 }
1864
Tejun Heof392e512014-04-23 11:13:14 -04001865 added_mask = opts.subsys_mask & ~root->subsys_mask;
1866 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001867
Ben Blumcf5d5942010-03-10 15:22:09 -08001868 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001869 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001870 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001871 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001872 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001873 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001874 goto out_unlock;
1875 }
1876
Tejun Heof172e672013-06-28 17:07:30 -07001877 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001878 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001879 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001880 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001881 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001882
Tejun Heo5df36032014-03-19 10:23:54 -04001883 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001884 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001885 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001886
Tejun Heo334c3672016-03-03 09:58:01 -05001887 WARN_ON(rebind_subsystems(&cgrp_dfl_root, removed_mask));
Tejun Heo5df36032014-03-19 10:23:54 -04001888
Tejun Heo69e943b2014-02-08 10:36:58 -05001889 if (opts.release_agent) {
1890 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001891 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001892 spin_unlock(&release_agent_path_lock);
1893 }
Tejun Heoed1777d2016-08-10 11:23:44 -04001894
1895 trace_cgroup_remount(root);
1896
Paul Menageddbcc7e2007-10-18 23:39:30 -07001897 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001898 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001899 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001900 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001901 return ret;
1902}
1903
Tejun Heoafeb0f92014-02-13 06:58:39 -05001904/*
1905 * To reduce the fork() overhead for systems that are not actually using
1906 * their cgroups capability, we don't maintain the lists running through
1907 * each css_set to its tasks until we see the list actually used - in other
1908 * words after the first mount.
1909 */
1910static bool use_task_css_set_links __read_mostly;
1911
1912static void cgroup_enable_task_cg_lists(void)
1913{
1914 struct task_struct *p, *g;
1915
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001916 spin_lock_irq(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001917
1918 if (use_task_css_set_links)
1919 goto out_unlock;
1920
1921 use_task_css_set_links = true;
1922
1923 /*
1924 * We need tasklist_lock because RCU is not safe against
1925 * while_each_thread(). Besides, a forking task that has passed
1926 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1927 * is not guaranteed to have its child immediately visible in the
1928 * tasklist if we walk through it with RCU.
1929 */
1930 read_lock(&tasklist_lock);
1931 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001932 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1933 task_css_set(p) != &init_css_set);
1934
1935 /*
1936 * We should check if the process is exiting, otherwise
1937 * it will race with cgroup_exit() in that the list
1938 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001939 * Do it while holding siglock so that we don't end up
1940 * racing against cgroup_exit().
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001941 *
1942 * Interrupts were already disabled while acquiring
1943 * the css_set_lock, so we do not need to disable it
1944 * again when acquiring the sighand->siglock here.
Tejun Heoafeb0f92014-02-13 06:58:39 -05001945 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001946 spin_lock(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001947 if (!(p->flags & PF_EXITING)) {
1948 struct css_set *cset = task_css_set(p);
1949
Tejun Heo0de09422015-10-15 16:41:49 -04001950 if (!css_set_populated(cset))
1951 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001952 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001953 get_css_set(cset);
1954 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001955 spin_unlock(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001956 } while_each_thread(g, p);
1957 read_unlock(&tasklist_lock);
1958out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001959 spin_unlock_irq(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001960}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001961
Paul Menagecc31edc2008-10-18 20:28:04 -07001962static void init_cgroup_housekeeping(struct cgroup *cgrp)
1963{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001964 struct cgroup_subsys *ss;
1965 int ssid;
1966
Tejun Heod5c419b2014-05-16 13:22:48 -04001967 INIT_LIST_HEAD(&cgrp->self.sibling);
1968 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001969 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001970 INIT_LIST_HEAD(&cgrp->pidlists);
1971 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001972 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001973 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001974
1975 for_each_subsys(ss, ssid)
1976 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001977
1978 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001979 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001980}
Paul Menagec6d57f32009-09-23 15:56:19 -07001981
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001982static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001983 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001984{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001985 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001986
Paul Menageddbcc7e2007-10-18 23:39:30 -07001987 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001988 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001989 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001990 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001991 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001992
Paul Menagec6d57f32009-09-23 15:56:19 -07001993 root->flags = opts->flags;
1994 if (opts->release_agent)
1995 strcpy(root->release_agent_path, opts->release_agent);
1996 if (opts->name)
1997 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001998 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001999 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07002000}
2001
Tejun Heo6e5c8302016-02-22 22:25:47 -05002002static int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07002003{
Tejun Heod427dfe2014-02-11 11:52:48 -05002004 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002005 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05002006 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05002007 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07002008
Tejun Heod427dfe2014-02-11 11:52:48 -05002009 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07002010
Vladimir Davydovcf780b72015-08-03 15:32:26 +03002011 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05002012 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002013 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05002014 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05002015 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07002016
Tejun Heo2aad2a82014-09-24 13:31:50 -04002017 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
2018 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04002019 if (ret)
2020 goto out;
2021
Tejun Heod427dfe2014-02-11 11:52:48 -05002022 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002023 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05002024 * but that's OK - it can only be increased by someone holding
Tejun Heo04313592016-03-03 09:58:01 -05002025 * cgroup_lock, and that's us. Later rebinding may disable
2026 * controllers on the default hierarchy and thus create new csets,
2027 * which can't be more than the existing ones. Allocate 2x.
Tejun Heod427dfe2014-02-11 11:52:48 -05002028 */
Tejun Heo04313592016-03-03 09:58:01 -05002029 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002030 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04002031 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002032
Tejun Heo985ed672014-03-19 10:23:53 -04002033 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05002034 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04002035 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002036
Tejun Heo2bd59d42014-02-11 11:52:49 -05002037 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
2038 KERNFS_ROOT_CREATE_DEACTIVATED,
2039 root_cgrp);
2040 if (IS_ERR(root->kf_root)) {
2041 ret = PTR_ERR(root->kf_root);
2042 goto exit_root_id;
2043 }
2044 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002045
Tejun Heo334c3672016-03-03 09:58:01 -05002046 ret = css_populate_dir(&root_cgrp->self);
Tejun Heod427dfe2014-02-11 11:52:48 -05002047 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002048 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002049
Tejun Heo5df36032014-03-19 10:23:54 -04002050 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05002051 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002052 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05002053
Alexei Starovoitov148f1112017-10-02 22:50:21 -07002054 ret = cgroup_bpf_inherit(root_cgrp);
2055 WARN_ON_ONCE(ret);
2056
Tejun Heoed1777d2016-08-10 11:23:44 -04002057 trace_cgroup_setup_root(root);
2058
Tejun Heod427dfe2014-02-11 11:52:48 -05002059 /*
2060 * There must be no failure case after here, since rebinding takes
2061 * care of subsystems' refcounts, which are explicitly dropped in
2062 * the failure exit path.
2063 */
2064 list_add(&root->root_list, &cgroup_roots);
2065 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002066
Tejun Heod427dfe2014-02-11 11:52:48 -05002067 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002068 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05002069 * objects.
2070 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002071 spin_lock_irq(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04002072 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05002073 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04002074 if (css_set_populated(cset))
2075 cgroup_update_populated(root_cgrp, true);
2076 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002077 spin_unlock_irq(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002078
Tejun Heod5c419b2014-05-16 13:22:48 -04002079 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05002080 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05002081
Tejun Heo2bd59d42014-02-11 11:52:49 -05002082 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05002083 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002084 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05002085
Tejun Heo2bd59d42014-02-11 11:52:49 -05002086destroy_root:
2087 kernfs_destroy_root(root->kf_root);
2088 root->kf_root = NULL;
2089exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05002090 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04002091cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04002092 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002093out:
Tejun Heod427dfe2014-02-11 11:52:48 -05002094 free_cgrp_cset_links(&tmp_links);
2095 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002096}
2097
Al Virof7e83572010-07-26 13:23:11 +04002098static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002099 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04002100 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002101{
Tejun Heo67e9c742015-11-16 11:13:34 -05002102 bool is_v2 = fs_type == &cgroup2_fs_type;
Li Zefan3a32bd72014-06-30 11:50:59 +08002103 struct super_block *pinned_sb = NULL;
Serge Hallyned825712016-01-29 02:54:09 -06002104 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
Li Zefan970317a2014-06-30 11:49:58 +08002105 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002106 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002107 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002108 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002109 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08002110 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002111 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07002112
Serge Hallyned825712016-01-29 02:54:09 -06002113 get_cgroup_ns(ns);
2114
2115 /* Check if the caller has permission to mount. */
2116 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
2117 put_cgroup_ns(ns);
2118 return ERR_PTR(-EPERM);
2119 }
2120
Paul Menagec6d57f32009-09-23 15:56:19 -07002121 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05002122 * The first time anyone tries to mount a cgroup, enable the list
2123 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07002124 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05002125 if (!use_task_css_set_links)
2126 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002127
Tejun Heo67e9c742015-11-16 11:13:34 -05002128 if (is_v2) {
2129 if (data) {
2130 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
Serge Hallyned825712016-01-29 02:54:09 -06002131 put_cgroup_ns(ns);
Tejun Heo67e9c742015-11-16 11:13:34 -05002132 return ERR_PTR(-EINVAL);
2133 }
Tejun Heoa7165262016-02-23 10:00:50 -05002134 cgrp_dfl_visible = true;
Tejun Heo67e9c742015-11-16 11:13:34 -05002135 root = &cgrp_dfl_root;
2136 cgroup_get(&root->cgrp);
2137 goto out_mount;
2138 }
2139
Tejun Heo334c3672016-03-03 09:58:01 -05002140 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002141
Paul Menageddbcc7e2007-10-18 23:39:30 -07002142 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002143 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002144 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002145 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002146
Li Zefan970317a2014-06-30 11:49:58 +08002147 /*
2148 * Destruction of cgroup root is asynchronous, so subsystems may
2149 * still be dying after the previous unmount. Let's drain the
2150 * dying subsystems. We just need to ensure that the ones
2151 * unmounted previously finish dying and don't care about new ones
2152 * starting. Testing ref liveliness is good enough.
2153 */
2154 for_each_subsys(ss, i) {
2155 if (!(opts.subsys_mask & (1 << i)) ||
2156 ss->root == &cgrp_dfl_root)
2157 continue;
2158
2159 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2160 mutex_unlock(&cgroup_mutex);
2161 msleep(10);
2162 ret = restart_syscall();
2163 goto out_free;
2164 }
2165 cgroup_put(&ss->root->cgrp);
2166 }
2167
Tejun Heo985ed672014-03-19 10:23:53 -04002168 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002169 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002170
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002171 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002172 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002173
Paul Menage817929e2007-10-18 23:39:36 -07002174 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002175 * If we asked for a name then it must match. Also, if
2176 * name matches but sybsys_mask doesn't, we should fail.
2177 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002178 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002179 if (opts.name) {
2180 if (strcmp(opts.name, root->name))
2181 continue;
2182 name_match = true;
2183 }
Tejun Heo31261212013-06-28 17:07:30 -07002184
2185 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002186 * If we asked for subsystems (or explicitly for no
2187 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002188 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002189 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002190 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002191 if (!name_match)
2192 continue;
2193 ret = -EBUSY;
2194 goto out_unlock;
2195 }
Tejun Heo873fe092013-04-14 20:15:26 -07002196
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002197 if (root->flags ^ opts.flags)
2198 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002199
Tejun Heo776f02f2014-02-12 09:29:50 -05002200 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002201 * We want to reuse @root whose lifetime is governed by its
2202 * ->cgrp. Let's check whether @root is alive and keep it
2203 * that way. As cgroup_kill_sb() can happen anytime, we
2204 * want to block it by pinning the sb so that @root doesn't
2205 * get killed before mount is complete.
2206 *
2207 * With the sb pinned, tryget_live can reliably indicate
2208 * whether @root can be reused. If it's being killed,
2209 * drain it. We can use wait_queue for the wait but this
2210 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002211 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002212 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2213 if (IS_ERR(pinned_sb) ||
2214 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002215 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002216 if (!IS_ERR_OR_NULL(pinned_sb))
2217 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002218 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002219 ret = restart_syscall();
2220 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002221 }
2222
2223 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002224 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002225 }
2226
Tejun Heo172a2c062014-03-19 10:23:53 -04002227 /*
2228 * No such thing, create a new one. name= matching without subsys
2229 * specification is allowed for already existing hierarchies but we
2230 * can't create new one without subsys specification.
2231 */
2232 if (!opts.subsys_mask && !opts.none) {
2233 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002234 goto out_unlock;
2235 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002236
Eric W. Biederman726a4992016-07-15 06:36:44 -05002237 /* Hierarchies may only be created in the initial cgroup namespace. */
2238 if (ns != &init_cgroup_ns) {
Serge Hallyned825712016-01-29 02:54:09 -06002239 ret = -EPERM;
2240 goto out_unlock;
2241 }
2242
Eric W. Biederman7e33a0a2022-01-20 11:04:01 -06002243 /*
2244 * Release agent gets called with all capabilities,
2245 * require capabilities to set release agent.
2246 */
2247 if (opts.release_agent &&
2248 ((ns->user_ns != &init_user_ns) || !capable(CAP_SYS_ADMIN))) {
2249 ret = -EINVAL;
2250 goto out_unlock;
2251 }
2252
Tejun Heo172a2c062014-03-19 10:23:53 -04002253 root = kzalloc(sizeof(*root), GFP_KERNEL);
2254 if (!root) {
2255 ret = -ENOMEM;
2256 goto out_unlock;
2257 }
2258
2259 init_cgroup_root(root, &opts);
2260
Tejun Heo35585572014-02-13 06:58:38 -05002261 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002262 if (ret)
2263 cgroup_free_root(root);
2264
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002265out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002266 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002267out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002268 kfree(opts.release_agent);
2269 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002270
Serge Hallyned825712016-01-29 02:54:09 -06002271 if (ret) {
2272 put_cgroup_ns(ns);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002273 return ERR_PTR(ret);
Serge Hallyned825712016-01-29 02:54:09 -06002274 }
Tejun Heo67e9c742015-11-16 11:13:34 -05002275out_mount:
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002276 dentry = kernfs_mount(fs_type, flags, root->kf_root,
Tejun Heo67e9c742015-11-16 11:13:34 -05002277 is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
2278 &new_sb);
Serge Hallyned825712016-01-29 02:54:09 -06002279
2280 /*
2281 * In non-init cgroup namespace, instead of root cgroup's
2282 * dentry, we return the dentry corresponding to the
2283 * cgroupns->root_cgrp.
2284 */
2285 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
2286 struct dentry *nsdentry;
2287 struct cgroup *cgrp;
2288
2289 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002290 spin_lock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002291
2292 cgrp = cset_cgroup_from_root(ns->root_cset, root);
2293
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002294 spin_unlock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002295 mutex_unlock(&cgroup_mutex);
2296
2297 nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
2298 dput(dentry);
2299 dentry = nsdentry;
2300 }
2301
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002302 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002303 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002304
2305 /*
2306 * If @pinned_sb, we're reusing an existing root and holding an
2307 * extra ref on its sb. Mount is complete. Put the extra ref.
2308 */
2309 if (pinned_sb) {
2310 WARN_ON(new_sb);
2311 deactivate_super(pinned_sb);
2312 }
2313
Serge Hallyned825712016-01-29 02:54:09 -06002314 put_cgroup_ns(ns);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002315 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002316}
2317
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002318static void cgroup_kill_sb(struct super_block *sb)
2319{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002320 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002321 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002322
Tejun Heo9d755d32014-05-14 09:15:02 -04002323 /*
2324 * If @root doesn't have any mounts or children, start killing it.
2325 * This prevents new mounts by disabling percpu_ref_tryget_live().
2326 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002327 *
2328 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002329 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002330 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002331 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002332 cgroup_put(&root->cgrp);
2333 else
2334 percpu_ref_kill(&root->cgrp.self.refcnt);
2335
Tejun Heo2bd59d42014-02-11 11:52:49 -05002336 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002337}
2338
2339static struct file_system_type cgroup_fs_type = {
2340 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002341 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002342 .kill_sb = cgroup_kill_sb,
Serge Hallyn1c537532016-01-29 02:54:11 -06002343 .fs_flags = FS_USERNS_MOUNT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002344};
2345
Tejun Heo67e9c742015-11-16 11:13:34 -05002346static struct file_system_type cgroup2_fs_type = {
2347 .name = "cgroup2",
2348 .mount = cgroup_mount,
2349 .kill_sb = cgroup_kill_sb,
Serge Hallyn1c537532016-01-29 02:54:11 -06002350 .fs_flags = FS_USERNS_MOUNT,
Tejun Heo67e9c742015-11-16 11:13:34 -05002351};
2352
Tejun Heo4c737b42016-08-10 11:23:44 -04002353static int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2354 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002355{
2356 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
Aditya Kalia79a9082016-01-29 02:54:06 -06002357
Tejun Heo4c737b42016-08-10 11:23:44 -04002358 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
Aditya Kalia79a9082016-01-29 02:54:06 -06002359}
2360
Tejun Heo4c737b42016-08-10 11:23:44 -04002361int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2362 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002363{
Tejun Heo4c737b42016-08-10 11:23:44 -04002364 int ret;
Aditya Kalia79a9082016-01-29 02:54:06 -06002365
2366 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002367 spin_lock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002368
2369 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2370
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002371 spin_unlock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002372 mutex_unlock(&cgroup_mutex);
2373
2374 return ret;
2375}
2376EXPORT_SYMBOL_GPL(cgroup_path_ns);
2377
Li Zefana043e3b2008-02-23 15:24:09 -08002378/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002379 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002380 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002381 * @buf: the buffer to write the path into
2382 * @buflen: the length of the buffer
2383 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002384 * Determine @task's cgroup on the first (the one with the lowest non-zero
2385 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2386 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2387 * cgroup controller callbacks.
2388 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002389 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002390 */
Tejun Heo4c737b42016-08-10 11:23:44 -04002391int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002392{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002393 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002394 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002395 int hierarchy_id = 1;
Tejun Heo4c737b42016-08-10 11:23:44 -04002396 int ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002397
2398 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002399 spin_lock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002400
Tejun Heo913ffdb2013-07-11 16:34:48 -07002401 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2402
Tejun Heo857a2be2013-04-14 20:50:08 -07002403 if (root) {
2404 cgrp = task_cgroup_from_root(task, root);
Tejun Heo4c737b42016-08-10 11:23:44 -04002405 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002406 } else {
2407 /* if no hierarchy exists, everyone is in "/" */
Tejun Heo4c737b42016-08-10 11:23:44 -04002408 ret = strlcpy(buf, "/", buflen);
Tejun Heo857a2be2013-04-14 20:50:08 -07002409 }
2410
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002411 spin_unlock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002412 mutex_unlock(&cgroup_mutex);
Tejun Heo4c737b42016-08-10 11:23:44 -04002413 return ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002414}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002415EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002416
Tejun Heob3dc0942014-02-25 10:04:01 -05002417/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002418struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002419 /* the src and dst cset list running through cset->mg_node */
2420 struct list_head src_csets;
2421 struct list_head dst_csets;
2422
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002423 /* the subsys currently being processed */
2424 int ssid;
2425
Tejun Heob3dc0942014-02-25 10:04:01 -05002426 /*
2427 * Fields for cgroup_taskset_*() iteration.
2428 *
2429 * Before migration is committed, the target migration tasks are on
2430 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2431 * the csets on ->dst_csets. ->csets point to either ->src_csets
2432 * or ->dst_csets depending on whether migration is committed.
2433 *
2434 * ->cur_csets and ->cur_task point to the current task position
2435 * during iteration.
2436 */
2437 struct list_head *csets;
2438 struct css_set *cur_cset;
2439 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002440};
2441
Tejun Heoadaae5d2015-09-11 15:00:21 -04002442#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2443 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2444 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2445 .csets = &tset.src_csets, \
2446}
2447
2448/**
2449 * cgroup_taskset_add - try to add a migration target task to a taskset
2450 * @task: target task
2451 * @tset: target taskset
2452 *
2453 * Add @task, which is a migration target, to @tset. This function becomes
2454 * noop if @task doesn't need to be migrated. @task's css_set should have
2455 * been added as a migration source and @task->cg_list will be moved from
2456 * the css_set's tasks list to mg_tasks one.
2457 */
2458static void cgroup_taskset_add(struct task_struct *task,
2459 struct cgroup_taskset *tset)
2460{
2461 struct css_set *cset;
2462
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002463 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002464
2465 /* @task either already exited or can't exit until the end */
2466 if (task->flags & PF_EXITING)
2467 return;
2468
2469 /* leave @task alone if post_fork() hasn't linked it yet */
2470 if (list_empty(&task->cg_list))
2471 return;
2472
2473 cset = task_css_set(task);
2474 if (!cset->mg_src_cgrp)
2475 return;
2476
2477 list_move_tail(&task->cg_list, &cset->mg_tasks);
2478 if (list_empty(&cset->mg_node))
2479 list_add_tail(&cset->mg_node, &tset->src_csets);
2480 if (list_empty(&cset->mg_dst_cset->mg_node))
2481 list_move_tail(&cset->mg_dst_cset->mg_node,
2482 &tset->dst_csets);
2483}
2484
Tejun Heo2f7ee562011-12-12 18:12:21 -08002485/**
2486 * cgroup_taskset_first - reset taskset and return the first task
2487 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002488 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002489 *
2490 * @tset iteration is initialized and the first task is returned.
2491 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002492struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2493 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002494{
Tejun Heob3dc0942014-02-25 10:04:01 -05002495 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2496 tset->cur_task = NULL;
2497
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002498 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002499}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002500
2501/**
2502 * cgroup_taskset_next - iterate to the next task in taskset
2503 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002504 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002505 *
2506 * Return the next task in @tset. Iteration must have been initialized
2507 * with cgroup_taskset_first().
2508 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002509struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2510 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002511{
Tejun Heob3dc0942014-02-25 10:04:01 -05002512 struct css_set *cset = tset->cur_cset;
2513 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002514
Tejun Heob3dc0942014-02-25 10:04:01 -05002515 while (&cset->mg_node != tset->csets) {
2516 if (!task)
2517 task = list_first_entry(&cset->mg_tasks,
2518 struct task_struct, cg_list);
2519 else
2520 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002521
Tejun Heob3dc0942014-02-25 10:04:01 -05002522 if (&task->cg_list != &cset->mg_tasks) {
2523 tset->cur_cset = cset;
2524 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002525
2526 /*
2527 * This function may be called both before and
2528 * after cgroup_taskset_migrate(). The two cases
2529 * can be distinguished by looking at whether @cset
2530 * has its ->mg_dst_cset set.
2531 */
2532 if (cset->mg_dst_cset)
2533 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2534 else
2535 *dst_cssp = cset->subsys[tset->ssid];
2536
Tejun Heob3dc0942014-02-25 10:04:01 -05002537 return task;
2538 }
2539
2540 cset = list_next_entry(cset, mg_node);
2541 task = NULL;
2542 }
2543
2544 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002545}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002546
2547/**
Tejun Heo37ff9f82016-03-08 11:51:26 -05002548 * cgroup_taskset_migrate - migrate a taskset
Tejun Heoadaae5d2015-09-11 15:00:21 -04002549 * @tset: taget taskset
Tejun Heo37ff9f82016-03-08 11:51:26 -05002550 * @root: cgroup root the migration is taking place on
Tejun Heoadaae5d2015-09-11 15:00:21 -04002551 *
Tejun Heo37ff9f82016-03-08 11:51:26 -05002552 * Migrate tasks in @tset as setup by migration preparation functions.
2553 * This function fails iff one of the ->can_attach callbacks fails and
2554 * guarantees that either all or none of the tasks in @tset are migrated.
2555 * @tset is consumed regardless of success.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002556 */
2557static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
Tejun Heo37ff9f82016-03-08 11:51:26 -05002558 struct cgroup_root *root)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002559{
Tejun Heo37ff9f82016-03-08 11:51:26 -05002560 struct cgroup_subsys *ss;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002561 struct task_struct *task, *tmp_task;
2562 struct css_set *cset, *tmp_cset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002563 int ssid, failed_ssid, ret;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002564
2565 /* methods shouldn't be called if no task is actually migrating */
2566 if (list_empty(&tset->src_csets))
2567 return 0;
2568
2569 /* check that we can legitimately attach to the cgroup */
Tejun Heo37ff9f82016-03-08 11:51:26 -05002570 do_each_subsys_mask(ss, ssid, root->subsys_mask) {
2571 if (ss->can_attach) {
2572 tset->ssid = ssid;
2573 ret = ss->can_attach(tset);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002574 if (ret) {
Tejun Heo37ff9f82016-03-08 11:51:26 -05002575 failed_ssid = ssid;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002576 goto out_cancel_attach;
2577 }
2578 }
Tejun Heo37ff9f82016-03-08 11:51:26 -05002579 } while_each_subsys_mask();
Tejun Heoadaae5d2015-09-11 15:00:21 -04002580
2581 /*
2582 * Now that we're guaranteed success, proceed to move all tasks to
2583 * the new cgroup. There are no failure cases after here, so this
2584 * is the commit point.
2585 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002586 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002587 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002588 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2589 struct css_set *from_cset = task_css_set(task);
2590 struct css_set *to_cset = cset->mg_dst_cset;
2591
2592 get_css_set(to_cset);
2593 css_set_move_task(task, from_cset, to_cset, true);
2594 put_css_set_locked(from_cset);
2595 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002596 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002597 spin_unlock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002598
2599 /*
2600 * Migration is committed, all target tasks are now on dst_csets.
2601 * Nothing is sensitive to fork() after this point. Notify
2602 * controllers that migration is complete.
2603 */
2604 tset->csets = &tset->dst_csets;
2605
Tejun Heo37ff9f82016-03-08 11:51:26 -05002606 do_each_subsys_mask(ss, ssid, root->subsys_mask) {
2607 if (ss->attach) {
2608 tset->ssid = ssid;
2609 ss->attach(tset);
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002610 }
Tejun Heo37ff9f82016-03-08 11:51:26 -05002611 } while_each_subsys_mask();
Tejun Heoadaae5d2015-09-11 15:00:21 -04002612
2613 ret = 0;
2614 goto out_release_tset;
2615
2616out_cancel_attach:
Tejun Heo37ff9f82016-03-08 11:51:26 -05002617 do_each_subsys_mask(ss, ssid, root->subsys_mask) {
2618 if (ssid == failed_ssid)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002619 break;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002620 if (ss->cancel_attach) {
2621 tset->ssid = ssid;
2622 ss->cancel_attach(tset);
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002623 }
Tejun Heo37ff9f82016-03-08 11:51:26 -05002624 } while_each_subsys_mask();
Tejun Heoadaae5d2015-09-11 15:00:21 -04002625out_release_tset:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002626 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002627 list_splice_init(&tset->dst_csets, &tset->src_csets);
2628 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2629 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2630 list_del_init(&cset->mg_node);
2631 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002632 spin_unlock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002633 return ret;
2634}
2635
2636/**
Tejun Heo6c694c82016-03-08 11:51:25 -05002637 * cgroup_may_migrate_to - verify whether a cgroup can be migration destination
2638 * @dst_cgrp: destination cgroup to test
2639 *
2640 * On the default hierarchy, except for the root, subtree_control must be
2641 * zero for migration destination cgroups with tasks so that child cgroups
2642 * don't compete against tasks.
2643 */
2644static bool cgroup_may_migrate_to(struct cgroup *dst_cgrp)
2645{
2646 return !cgroup_on_dfl(dst_cgrp) || !cgroup_parent(dst_cgrp) ||
2647 !dst_cgrp->subtree_control;
2648}
2649
2650/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002651 * cgroup_migrate_finish - cleanup after attach
2652 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002653 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002654 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2655 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002656 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002657static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002658{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002659 struct css_set *cset, *tmp_cset;
2660
2661 lockdep_assert_held(&cgroup_mutex);
2662
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002663 spin_lock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002664 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2665 cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002666 cset->mg_dst_cgrp = NULL;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002667 cset->mg_dst_cset = NULL;
2668 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002669 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002670 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002671 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002672}
2673
2674/**
2675 * cgroup_migrate_add_src - add a migration source css_set
2676 * @src_cset: the source css_set to add
2677 * @dst_cgrp: the destination cgroup
2678 * @preloaded_csets: list of preloaded css_sets
2679 *
2680 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2681 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2682 * up by cgroup_migrate_finish().
2683 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002684 * This function may be called without holding cgroup_threadgroup_rwsem
2685 * even if the target is a process. Threads may be created and destroyed
2686 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2687 * into play and the preloaded css_sets are guaranteed to cover all
2688 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002689 */
2690static void cgroup_migrate_add_src(struct css_set *src_cset,
2691 struct cgroup *dst_cgrp,
2692 struct list_head *preloaded_csets)
2693{
2694 struct cgroup *src_cgrp;
2695
2696 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002697 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002698
Tejun Heo2b021cb2016-03-15 20:43:04 -04002699 /*
2700 * If ->dead, @src_set is associated with one or more dead cgroups
2701 * and doesn't contain any migratable tasks. Ignore it early so
2702 * that the rest of migration path doesn't get confused by it.
2703 */
2704 if (src_cset->dead)
2705 return;
2706
Tejun Heo1958d2d2014-02-25 10:04:03 -05002707 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2708
Tejun Heo1958d2d2014-02-25 10:04:03 -05002709 if (!list_empty(&src_cset->mg_preload_node))
2710 return;
2711
2712 WARN_ON(src_cset->mg_src_cgrp);
Tejun Heoe4857982016-03-08 11:51:26 -05002713 WARN_ON(src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002714 WARN_ON(!list_empty(&src_cset->mg_tasks));
2715 WARN_ON(!list_empty(&src_cset->mg_node));
2716
2717 src_cset->mg_src_cgrp = src_cgrp;
Tejun Heoe4857982016-03-08 11:51:26 -05002718 src_cset->mg_dst_cgrp = dst_cgrp;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002719 get_css_set(src_cset);
2720 list_add(&src_cset->mg_preload_node, preloaded_csets);
2721}
2722
2723/**
2724 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heo1958d2d2014-02-25 10:04:03 -05002725 * @preloaded_csets: list of preloaded source css_sets
2726 *
Tejun Heoe4857982016-03-08 11:51:26 -05002727 * Tasks are about to be moved and all the source css_sets have been
2728 * preloaded to @preloaded_csets. This function looks up and pins all
2729 * destination css_sets, links each to its source, and append them to
2730 * @preloaded_csets.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002731 *
2732 * This function must be called after cgroup_migrate_add_src() has been
2733 * called on each migration source css_set. After migration is performed
2734 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2735 * @preloaded_csets.
2736 */
Tejun Heoe4857982016-03-08 11:51:26 -05002737static int cgroup_migrate_prepare_dst(struct list_head *preloaded_csets)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002738{
2739 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002740 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002741
2742 lockdep_assert_held(&cgroup_mutex);
2743
2744 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002745 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002746 struct css_set *dst_cset;
2747
Tejun Heoe4857982016-03-08 11:51:26 -05002748 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002749 if (!dst_cset)
2750 goto err;
2751
2752 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002753
2754 /*
2755 * If src cset equals dst, it's noop. Drop the src.
2756 * cgroup_migrate() will skip the cset too. Note that we
2757 * can't handle src == dst as some nodes are used by both.
2758 */
2759 if (src_cset == dst_cset) {
2760 src_cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002761 src_cset->mg_dst_cgrp = NULL;
Tejun Heof817de92014-04-23 11:13:16 -04002762 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002763 put_css_set(src_cset);
2764 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002765 continue;
2766 }
2767
Tejun Heo1958d2d2014-02-25 10:04:03 -05002768 src_cset->mg_dst_cset = dst_cset;
2769
2770 if (list_empty(&dst_cset->mg_preload_node))
2771 list_add(&dst_cset->mg_preload_node, &csets);
2772 else
Zefan Lia25eb522014-09-19 16:51:00 +08002773 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002774 }
2775
Tejun Heof817de92014-04-23 11:13:16 -04002776 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002777 return 0;
2778err:
2779 cgroup_migrate_finish(&csets);
2780 return -ENOMEM;
2781}
2782
2783/**
2784 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002785 * @leader: the leader of the process or the task to migrate
2786 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo37ff9f82016-03-08 11:51:26 -05002787 * @root: cgroup root migration is taking place on
Tejun Heo1958d2d2014-02-25 10:04:03 -05002788 *
Tejun Heo37ff9f82016-03-08 11:51:26 -05002789 * Migrate a process or task denoted by @leader. If migrating a process,
2790 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2791 * responsible for invoking cgroup_migrate_add_src() and
Tejun Heo1958d2d2014-02-25 10:04:03 -05002792 * cgroup_migrate_prepare_dst() on the targets before invoking this
2793 * function and following up with cgroup_migrate_finish().
2794 *
2795 * As long as a controller's ->can_attach() doesn't fail, this function is
2796 * guaranteed to succeed. This means that, excluding ->can_attach()
2797 * failure, when migrating multiple targets, the success or failure can be
2798 * decided for all targets by invoking group_migrate_prepare_dst() before
2799 * actually starting migrating.
2800 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002801static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heo37ff9f82016-03-08 11:51:26 -05002802 struct cgroup_root *root)
Ben Blum74a11662011-05-26 16:25:20 -07002803{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002804 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2805 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002806
2807 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002808 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2809 * already PF_EXITING could be freed from underneath us unless we
2810 * take an rcu_read_lock.
2811 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002812 spin_lock_irq(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002813 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002814 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002815 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002816 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002817 if (!threadgroup)
2818 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002819 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002820 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002821 spin_unlock_irq(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002822
Tejun Heo37ff9f82016-03-08 11:51:26 -05002823 return cgroup_taskset_migrate(&tset, root);
Ben Blum74a11662011-05-26 16:25:20 -07002824}
2825
Tejun Heo1958d2d2014-02-25 10:04:03 -05002826/**
2827 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2828 * @dst_cgrp: the cgroup to attach to
2829 * @leader: the task or the leader of the threadgroup to be attached
2830 * @threadgroup: attach the whole threadgroup?
2831 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002832 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002833 */
2834static int cgroup_attach_task(struct cgroup *dst_cgrp,
2835 struct task_struct *leader, bool threadgroup)
2836{
2837 LIST_HEAD(preloaded_csets);
2838 struct task_struct *task;
2839 int ret;
2840
Tejun Heo6c694c82016-03-08 11:51:25 -05002841 if (!cgroup_may_migrate_to(dst_cgrp))
2842 return -EBUSY;
2843
Tejun Heo1958d2d2014-02-25 10:04:03 -05002844 /* look up all src csets */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002845 spin_lock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002846 rcu_read_lock();
2847 task = leader;
2848 do {
2849 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2850 &preloaded_csets);
2851 if (!threadgroup)
2852 break;
2853 } while_each_thread(leader, task);
2854 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002855 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002856
2857 /* prepare dst csets and commit */
Tejun Heoe4857982016-03-08 11:51:26 -05002858 ret = cgroup_migrate_prepare_dst(&preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002859 if (!ret)
Tejun Heo37ff9f82016-03-08 11:51:26 -05002860 ret = cgroup_migrate(leader, threadgroup, dst_cgrp->root);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002861
2862 cgroup_migrate_finish(&preloaded_csets);
Tejun Heoed1777d2016-08-10 11:23:44 -04002863
2864 if (!ret)
2865 trace_cgroup_attach_task(dst_cgrp, leader, threadgroup);
2866
Tejun Heo1958d2d2014-02-25 10:04:03 -05002867 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002868}
2869
Amit Pundir3ba5a3b2015-12-21 16:01:10 +05302870int subsys_cgroup_allow_attach(struct cgroup_taskset *tset)
Rom Lemarchande3a09412014-11-07 12:48:17 -08002871{
2872 const struct cred *cred = current_cred(), *tcred;
2873 struct task_struct *task;
Amit Pundir3ba5a3b2015-12-21 16:01:10 +05302874 struct cgroup_subsys_state *css;
Rom Lemarchande3a09412014-11-07 12:48:17 -08002875
2876 if (capable(CAP_SYS_NICE))
2877 return 0;
2878
Amit Pundir3ba5a3b2015-12-21 16:01:10 +05302879 cgroup_taskset_for_each(task, css, tset) {
Rom Lemarchande3a09412014-11-07 12:48:17 -08002880 tcred = __task_cred(task);
2881
2882 if (current != task && !uid_eq(cred->euid, tcred->uid) &&
2883 !uid_eq(cred->euid, tcred->suid))
2884 return -EACCES;
2885 }
2886
2887 return 0;
2888}
2889
Tejun Heo187fe842015-06-18 16:54:28 -04002890static int cgroup_procs_write_permission(struct task_struct *task,
2891 struct cgroup *dst_cgrp,
2892 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002893{
2894 const struct cred *cred = current_cred();
2895 const struct cred *tcred = get_task_cred(task);
2896 int ret = 0;
2897
2898 /*
2899 * even if we're attaching all tasks in the thread group, we only
2900 * need to check permissions on one of them.
2901 */
2902 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2903 !uid_eq(cred->euid, tcred->uid) &&
Dmitry Torokhov579a63b2016-10-06 16:14:16 -07002904 !uid_eq(cred->euid, tcred->suid) &&
Dmitry Shmidt5b9202d2017-04-25 12:41:58 -07002905 !ns_capable(tcred->user_ns, CAP_SYS_NICE))
Tejun Heodedf22e2015-06-18 16:54:28 -04002906 ret = -EACCES;
2907
Tejun Heo187fe842015-06-18 16:54:28 -04002908 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2909 struct super_block *sb = of->file->f_path.dentry->d_sb;
2910 struct cgroup *cgrp;
2911 struct inode *inode;
2912
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002913 spin_lock_irq(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002914 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002915 spin_unlock_irq(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002916
2917 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2918 cgrp = cgroup_parent(cgrp);
2919
2920 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002921 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002922 if (inode) {
2923 ret = inode_permission(inode, MAY_WRITE);
2924 iput(inode);
2925 }
2926 }
2927
Tejun Heodedf22e2015-06-18 16:54:28 -04002928 put_cred(tcred);
2929 return ret;
2930}
2931
Ben Blum74a11662011-05-26 16:25:20 -07002932/*
2933 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002934 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002935 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002936 */
Tejun Heoacbef752014-05-13 12:16:22 -04002937static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2938 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002939{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002940 struct task_struct *tsk;
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002941 struct cgroup_subsys *ss;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002942 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002943 pid_t pid;
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002944 int ssid, ret;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002945
Tejun Heoacbef752014-05-13 12:16:22 -04002946 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2947 return -EINVAL;
2948
Tejun Heo945ba192016-03-03 09:58:00 -05002949 cgrp = cgroup_kn_lock_live(of->kn, false);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002950 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002951 return -ENODEV;
2952
Tejun Heo3014dde2015-09-16 13:03:02 -04002953 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002954 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002955 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002956 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002957 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002958 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002959 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002960 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002961 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002962 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002963 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002964
2965 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002966 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002967
2968 /*
Tejun Heof44236a2017-03-16 16:54:24 -04002969 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2970 * If userland migrates such a kthread to a non-root cgroup, it can
2971 * become trapped in a cpuset, or RT kthread may be born in a
2972 * cgroup with no rt_runtime allocated. Just say no.
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002973 */
Tejun Heof44236a2017-03-16 16:54:24 -04002974 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002975 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002976 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002977 }
2978
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002979 get_task_struct(tsk);
2980 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002981
Tejun Heo187fe842015-06-18 16:54:28 -04002982 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002983 if (!ret)
2984 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002985
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002986 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002987 goto out_unlock_threadgroup;
2988
2989out_unlock_rcu:
2990 rcu_read_unlock();
2991out_unlock_threadgroup:
2992 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002993 for_each_subsys(ss, ssid)
2994 if (ss->post_attach)
2995 ss->post_attach();
Tejun Heoe76ecae2014-05-13 12:19:23 -04002996 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002997 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002998}
2999
Tejun Heo7ae1bad2013-04-07 09:29:51 -07003000/**
3001 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
3002 * @from: attach to all cgroups of a given task
3003 * @tsk: the task to be attached
3004 */
3005int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
3006{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003007 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07003008 int retval = 0;
3009
Tejun Heo47cfcd02013-04-07 09:29:51 -07003010 mutex_lock(&cgroup_mutex);
Eric W. Biedermaneedd0f42016-07-15 06:35:51 -05003011 percpu_down_write(&cgroup_threadgroup_rwsem);
Tejun Heo985ed672014-03-19 10:23:53 -04003012 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05003013 struct cgroup *from_cgrp;
3014
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003015 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04003016 continue;
3017
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03003018 spin_lock_irq(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05003019 from_cgrp = task_cgroup_from_root(from, root);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03003020 spin_unlock_irq(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07003021
Li Zefan6f4b7e62013-07-31 16:18:36 +08003022 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07003023 if (retval)
3024 break;
3025 }
Eric W. Biedermaneedd0f42016-07-15 06:35:51 -05003026 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003027 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07003028
3029 return retval;
3030}
3031EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
3032
Tejun Heoacbef752014-05-13 12:16:22 -04003033static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
3034 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07003035{
Tejun Heoacbef752014-05-13 12:16:22 -04003036 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07003037}
3038
Tejun Heoacbef752014-05-13 12:16:22 -04003039static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
3040 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07003041{
Tejun Heoacbef752014-05-13 12:16:22 -04003042 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07003043}
3044
Tejun Heo451af502014-05-13 12:16:21 -04003045static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
3046 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07003047{
Tejun Heoe76ecae2014-05-13 12:19:23 -04003048 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05003049
Tejun Heoe76ecae2014-05-13 12:19:23 -04003050 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
3051
Eric W. Biederman7e33a0a2022-01-20 11:04:01 -06003052 /*
3053 * Release agent gets called with all capabilities,
3054 * require capabilities to set release agent.
3055 */
3056 if ((of->file->f_cred->user_ns != &init_user_ns) ||
3057 !capable(CAP_SYS_ADMIN))
3058 return -EPERM;
3059
Tejun Heo945ba192016-03-03 09:58:00 -05003060 cgrp = cgroup_kn_lock_live(of->kn, false);
Tejun Heoe76ecae2014-05-13 12:19:23 -04003061 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07003062 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05003063 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04003064 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
3065 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05003066 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04003067 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003068 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07003069}
3070
Tejun Heo2da8ca82013-12-05 12:28:04 -05003071static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07003072{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003073 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04003074
Tejun Heo46cfeb02014-05-13 12:11:00 -04003075 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07003076 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04003077 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07003078 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07003079 return 0;
3080}
3081
Tejun Heo2da8ca82013-12-05 12:28:04 -05003082static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07003083{
Tejun Heoc1d5d422014-07-09 10:08:08 -04003084 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07003085 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003086}
3087
Tejun Heo6e5c8302016-02-22 22:25:47 -05003088static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04003089{
3090 struct cgroup_subsys *ss;
3091 bool printed = false;
3092 int ssid;
3093
Tejun Heob4e0eea2016-02-22 22:25:46 -05003094 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10003095 if (printed)
3096 seq_putc(seq, ' ');
3097 seq_printf(seq, "%s", ss->name);
3098 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003099 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003100 if (printed)
3101 seq_putc(seq, '\n');
3102}
3103
Tejun Heof8f22e52014-04-23 11:13:16 -04003104/* show controllers which are enabled from the parent */
3105static int cgroup_controllers_show(struct seq_file *seq, void *v)
3106{
3107 struct cgroup *cgrp = seq_css(seq)->cgroup;
3108
Tejun Heo5531dc92016-03-03 09:57:58 -05003109 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04003110 return 0;
3111}
3112
3113/* show controllers which are enabled for a given cgroup's children */
3114static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
3115{
3116 struct cgroup *cgrp = seq_css(seq)->cgroup;
3117
Tejun Heo667c2492014-07-08 18:02:56 -04003118 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04003119 return 0;
3120}
3121
3122/**
3123 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
3124 * @cgrp: root of the subtree to update csses for
3125 *
Tejun Heo54962602016-03-03 09:58:01 -05003126 * @cgrp's control masks have changed and its subtree's css associations
3127 * need to be updated accordingly. This function looks up all css_sets
3128 * which are attached to the subtree, creates the matching updated css_sets
3129 * and migrates the tasks to the new ones.
Tejun Heof8f22e52014-04-23 11:13:16 -04003130 */
3131static int cgroup_update_dfl_csses(struct cgroup *cgrp)
3132{
3133 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04003134 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heo54962602016-03-03 09:58:01 -05003135 struct cgroup_subsys_state *d_css;
3136 struct cgroup *dsct;
Tejun Heof8f22e52014-04-23 11:13:16 -04003137 struct css_set *src_cset;
3138 int ret;
3139
Tejun Heof8f22e52014-04-23 11:13:16 -04003140 lockdep_assert_held(&cgroup_mutex);
3141
Tejun Heo3014dde2015-09-16 13:03:02 -04003142 percpu_down_write(&cgroup_threadgroup_rwsem);
3143
Tejun Heof8f22e52014-04-23 11:13:16 -04003144 /* look up all csses currently attached to @cgrp's subtree */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03003145 spin_lock_irq(&css_set_lock);
Tejun Heo54962602016-03-03 09:58:01 -05003146 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003147 struct cgrp_cset_link *link;
3148
Tejun Heo54962602016-03-03 09:58:01 -05003149 list_for_each_entry(link, &dsct->cset_links, cset_link)
Tejun Heo58cdb1c2016-03-08 11:51:25 -05003150 cgroup_migrate_add_src(link->cset, dsct,
Tejun Heof8f22e52014-04-23 11:13:16 -04003151 &preloaded_csets);
3152 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03003153 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04003154
3155 /* NULL dst indicates self on default hierarchy */
Tejun Heoe4857982016-03-08 11:51:26 -05003156 ret = cgroup_migrate_prepare_dst(&preloaded_csets);
Tejun Heof8f22e52014-04-23 11:13:16 -04003157 if (ret)
3158 goto out_finish;
3159
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03003160 spin_lock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04003161 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04003162 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04003163
3164 /* src_csets precede dst_csets, break on the first dst_cset */
3165 if (!src_cset->mg_src_cgrp)
3166 break;
3167
Tejun Heo10265072015-09-11 15:00:22 -04003168 /* all tasks in src_csets need to be migrated */
3169 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
3170 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04003171 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03003172 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04003173
Tejun Heo37ff9f82016-03-08 11:51:26 -05003174 ret = cgroup_taskset_migrate(&tset, cgrp->root);
Tejun Heof8f22e52014-04-23 11:13:16 -04003175out_finish:
3176 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04003177 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04003178 return ret;
3179}
3180
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003181/**
Tejun Heo945ba192016-03-03 09:58:00 -05003182 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
Tejun Heoce3f1d92016-03-03 09:57:59 -05003183 * @cgrp: root of the target subtree
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003184 *
3185 * Because css offlining is asynchronous, userland may try to re-enable a
Tejun Heo945ba192016-03-03 09:58:00 -05003186 * controller while the previous css is still around. This function grabs
3187 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003188 */
Tejun Heo945ba192016-03-03 09:58:00 -05003189static void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
3190 __acquires(&cgroup_mutex)
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003191{
3192 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003193 struct cgroup_subsys_state *d_css;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003194 struct cgroup_subsys *ss;
3195 int ssid;
3196
Tejun Heo945ba192016-03-03 09:58:00 -05003197restart:
3198 mutex_lock(&cgroup_mutex);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003199
Tejun Heoce3f1d92016-03-03 09:57:59 -05003200 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003201 for_each_subsys(ss, ssid) {
3202 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3203 DEFINE_WAIT(wait);
3204
Tejun Heoce3f1d92016-03-03 09:57:59 -05003205 if (!css || !percpu_ref_is_dying(&css->refcnt))
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003206 continue;
3207
3208 cgroup_get(dsct);
3209 prepare_to_wait(&dsct->offline_waitq, &wait,
3210 TASK_UNINTERRUPTIBLE);
3211
3212 mutex_unlock(&cgroup_mutex);
3213 schedule();
3214 finish_wait(&dsct->offline_waitq, &wait);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003215
3216 cgroup_put(dsct);
Tejun Heo945ba192016-03-03 09:58:00 -05003217 goto restart;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003218 }
3219 }
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003220}
3221
Tejun Heo12b3bb62016-03-03 09:57:59 -05003222/**
Tejun Heo15a27c32016-03-03 09:57:59 -05003223 * cgroup_save_control - save control masks of a subtree
3224 * @cgrp: root of the target subtree
3225 *
3226 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
3227 * prefixed fields for @cgrp's subtree including @cgrp itself.
3228 */
3229static void cgroup_save_control(struct cgroup *cgrp)
3230{
3231 struct cgroup *dsct;
3232 struct cgroup_subsys_state *d_css;
3233
3234 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3235 dsct->old_subtree_control = dsct->subtree_control;
3236 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
3237 }
3238}
3239
3240/**
3241 * cgroup_propagate_control - refresh control masks of a subtree
3242 * @cgrp: root of the target subtree
3243 *
3244 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
3245 * ->subtree_control and propagate controller availability through the
3246 * subtree so that descendants don't have unavailable controllers enabled.
3247 */
3248static void cgroup_propagate_control(struct cgroup *cgrp)
3249{
3250 struct cgroup *dsct;
3251 struct cgroup_subsys_state *d_css;
3252
3253 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3254 dsct->subtree_control &= cgroup_control(dsct);
Tejun Heo5ced2512016-03-03 09:58:01 -05003255 dsct->subtree_ss_mask =
3256 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
3257 cgroup_ss_mask(dsct));
Tejun Heo15a27c32016-03-03 09:57:59 -05003258 }
3259}
3260
3261/**
3262 * cgroup_restore_control - restore control masks of a subtree
3263 * @cgrp: root of the target subtree
3264 *
3265 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
3266 * prefixed fields for @cgrp's subtree including @cgrp itself.
3267 */
3268static void cgroup_restore_control(struct cgroup *cgrp)
3269{
3270 struct cgroup *dsct;
3271 struct cgroup_subsys_state *d_css;
3272
3273 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
3274 dsct->subtree_control = dsct->old_subtree_control;
3275 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
3276 }
3277}
3278
Tejun Heof6d635ad2016-03-08 11:51:26 -05003279static bool css_visible(struct cgroup_subsys_state *css)
3280{
3281 struct cgroup_subsys *ss = css->ss;
3282 struct cgroup *cgrp = css->cgroup;
3283
3284 if (cgroup_control(cgrp) & (1 << ss->id))
3285 return true;
3286 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
3287 return false;
3288 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
3289}
3290
Tejun Heo15a27c32016-03-03 09:57:59 -05003291/**
Tejun Heobdb53bd2016-03-03 09:57:59 -05003292 * cgroup_apply_control_enable - enable or show csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003293 * @cgrp: root of the target subtree
Tejun Heobdb53bd2016-03-03 09:57:59 -05003294 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003295 * Walk @cgrp's subtree and create new csses or make the existing ones
Tejun Heobdb53bd2016-03-03 09:57:59 -05003296 * visible. A css is created invisible if it's being implicitly enabled
3297 * through dependency. An invisible css is made visible when the userland
3298 * explicitly enables it.
3299 *
3300 * Returns 0 on success, -errno on failure. On failure, csses which have
3301 * been processed already aren't cleaned up. The caller is responsible for
3302 * cleaning up with cgroup_apply_control_disble().
3303 */
3304static int cgroup_apply_control_enable(struct cgroup *cgrp)
3305{
3306 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003307 struct cgroup_subsys_state *d_css;
Tejun Heobdb53bd2016-03-03 09:57:59 -05003308 struct cgroup_subsys *ss;
3309 int ssid, ret;
3310
Tejun Heoce3f1d92016-03-03 09:57:59 -05003311 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heobdb53bd2016-03-03 09:57:59 -05003312 for_each_subsys(ss, ssid) {
3313 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3314
Tejun Heo945ba192016-03-03 09:58:00 -05003315 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
3316
Tejun Heobdb53bd2016-03-03 09:57:59 -05003317 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
3318 continue;
3319
3320 if (!css) {
3321 css = css_create(dsct, ss);
3322 if (IS_ERR(css))
3323 return PTR_ERR(css);
3324 }
3325
Tejun Heof6d635ad2016-03-08 11:51:26 -05003326 if (css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05003327 ret = css_populate_dir(css);
Tejun Heobdb53bd2016-03-03 09:57:59 -05003328 if (ret)
3329 return ret;
3330 }
3331 }
3332 }
3333
3334 return 0;
3335}
3336
3337/**
Tejun Heo12b3bb62016-03-03 09:57:59 -05003338 * cgroup_apply_control_disable - kill or hide csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003339 * @cgrp: root of the target subtree
Tejun Heo12b3bb62016-03-03 09:57:59 -05003340 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003341 * Walk @cgrp's subtree and kill and hide csses so that they match
Tejun Heo12b3bb62016-03-03 09:57:59 -05003342 * cgroup_ss_mask() and cgroup_visible_mask().
3343 *
3344 * A css is hidden when the userland requests it to be disabled while other
3345 * subsystems are still depending on it. The css must not actively control
3346 * resources and be in the vanilla state if it's made visible again later.
3347 * Controllers which may be depended upon should provide ->css_reset() for
3348 * this purpose.
3349 */
3350static void cgroup_apply_control_disable(struct cgroup *cgrp)
3351{
3352 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003353 struct cgroup_subsys_state *d_css;
Tejun Heo12b3bb62016-03-03 09:57:59 -05003354 struct cgroup_subsys *ss;
3355 int ssid;
3356
Tejun Heoce3f1d92016-03-03 09:57:59 -05003357 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003358 for_each_subsys(ss, ssid) {
3359 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3360
Tejun Heo945ba192016-03-03 09:58:00 -05003361 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
3362
Tejun Heo12b3bb62016-03-03 09:57:59 -05003363 if (!css)
3364 continue;
3365
Tejun Heo334c3672016-03-03 09:58:01 -05003366 if (css->parent &&
3367 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003368 kill_css(css);
Tejun Heof6d635ad2016-03-08 11:51:26 -05003369 } else if (!css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05003370 css_clear_dir(css);
Tejun Heo12b3bb62016-03-03 09:57:59 -05003371 if (ss->css_reset)
3372 ss->css_reset(css);
3373 }
3374 }
3375 }
3376}
3377
Tejun Heof7b28142016-03-03 09:58:00 -05003378/**
3379 * cgroup_apply_control - apply control mask updates to the subtree
3380 * @cgrp: root of the target subtree
3381 *
3382 * subsystems can be enabled and disabled in a subtree using the following
3383 * steps.
3384 *
3385 * 1. Call cgroup_save_control() to stash the current state.
3386 * 2. Update ->subtree_control masks in the subtree as desired.
3387 * 3. Call cgroup_apply_control() to apply the changes.
3388 * 4. Optionally perform other related operations.
3389 * 5. Call cgroup_finalize_control() to finish up.
3390 *
3391 * This function implements step 3 and propagates the mask changes
3392 * throughout @cgrp's subtree, updates csses accordingly and perform
3393 * process migrations.
3394 */
3395static int cgroup_apply_control(struct cgroup *cgrp)
3396{
3397 int ret;
3398
3399 cgroup_propagate_control(cgrp);
3400
3401 ret = cgroup_apply_control_enable(cgrp);
3402 if (ret)
3403 return ret;
3404
3405 /*
3406 * At this point, cgroup_e_css() results reflect the new csses
3407 * making the following cgroup_update_dfl_csses() properly update
3408 * css associations of all tasks in the subtree.
3409 */
3410 ret = cgroup_update_dfl_csses(cgrp);
3411 if (ret)
3412 return ret;
3413
3414 return 0;
3415}
3416
3417/**
3418 * cgroup_finalize_control - finalize control mask update
3419 * @cgrp: root of the target subtree
3420 * @ret: the result of the update
3421 *
3422 * Finalize control mask update. See cgroup_apply_control() for more info.
3423 */
3424static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3425{
3426 if (ret) {
3427 cgroup_restore_control(cgrp);
3428 cgroup_propagate_control(cgrp);
3429 }
3430
3431 cgroup_apply_control_disable(cgrp);
3432}
3433
Tejun Heof8f22e52014-04-23 11:13:16 -04003434/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003435static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3436 char *buf, size_t nbytes,
3437 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003438{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003439 u16 enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04003440 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003441 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003442 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003443 int ssid, ret;
3444
3445 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003446 * Parse input - space separated list of subsystem names prefixed
3447 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003448 */
Tejun Heo451af502014-05-13 12:16:21 -04003449 buf = strstrip(buf);
3450 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003451 if (tok[0] == '\0')
3452 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003453 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003454 if (!cgroup_ssid_enabled(ssid) ||
3455 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003456 continue;
3457
3458 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003459 enable |= 1 << ssid;
3460 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003461 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003462 disable |= 1 << ssid;
3463 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003464 } else {
3465 return -EINVAL;
3466 }
3467 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003468 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003469 if (ssid == CGROUP_SUBSYS_COUNT)
3470 return -EINVAL;
3471 }
3472
Tejun Heo945ba192016-03-03 09:58:00 -05003473 cgrp = cgroup_kn_lock_live(of->kn, true);
Tejun Heoa9746d82014-05-13 12:19:22 -04003474 if (!cgrp)
3475 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003476
3477 for_each_subsys(ss, ssid) {
3478 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003479 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003480 enable &= ~(1 << ssid);
3481 continue;
3482 }
3483
Tejun Heo5531dc92016-03-03 09:57:58 -05003484 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003485 ret = -ENOENT;
3486 goto out_unlock;
3487 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003488 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003489 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003490 disable &= ~(1 << ssid);
3491 continue;
3492 }
3493
3494 /* a child has it enabled? */
3495 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003496 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003497 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003498 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003499 }
3500 }
3501 }
3502 }
3503
3504 if (!enable && !disable) {
3505 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003506 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003507 }
3508
3509 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003510 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003511 * with tasks so that child cgroups don't compete against tasks.
3512 */
Tejun Heo91570562016-09-23 16:55:49 -04003513 if (enable && cgroup_parent(cgrp)) {
3514 struct cgrp_cset_link *link;
3515
3516 /*
3517 * Because namespaces pin csets too, @cgrp->cset_links
3518 * might not be empty even when @cgrp is empty. Walk and
3519 * verify each cset.
3520 */
3521 spin_lock_irq(&css_set_lock);
3522
3523 ret = 0;
3524 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
3525 if (css_set_populated(link->cset)) {
3526 ret = -EBUSY;
3527 break;
3528 }
3529 }
3530
3531 spin_unlock_irq(&css_set_lock);
3532
3533 if (ret)
3534 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003535 }
3536
Tejun Heo15a27c32016-03-03 09:57:59 -05003537 /* save and update control masks and prepare csses */
3538 cgroup_save_control(cgrp);
Tejun Heoc29adf22014-07-08 18:02:56 -04003539
Tejun Heo15a27c32016-03-03 09:57:59 -05003540 cgrp->subtree_control |= enable;
3541 cgrp->subtree_control &= ~disable;
Tejun Heof63070d2014-07-08 18:02:57 -04003542
Tejun Heof7b28142016-03-03 09:58:00 -05003543 ret = cgroup_apply_control(cgrp);
Tejun Heof7b28142016-03-03 09:58:00 -05003544 cgroup_finalize_control(cgrp, ret);
Tejun Heo445ee6c2017-07-23 08:14:15 -04003545 if (ret)
3546 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003547
3548 kernfs_activate(cgrp->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003549out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003550 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003551 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003552}
3553
Tejun Heo4a07c222015-09-18 17:54:22 -04003554static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003555{
Tejun Heo4a07c222015-09-18 17:54:22 -04003556 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003557 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003558 return 0;
3559}
3560
Johannes Weinere868a992018-10-26 15:06:31 -07003561#ifdef CONFIG_PSI
3562static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
3563{
3564 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_IO);
3565}
3566static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
3567{
3568 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_MEM);
3569}
3570static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
3571{
3572 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_CPU);
3573}
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08003574
3575static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
3576 size_t nbytes, enum psi_res res)
3577{
3578 struct psi_trigger *new;
3579 struct cgroup *cgrp;
3580
3581 cgrp = cgroup_kn_lock_live(of->kn, false);
3582 if (!cgrp)
3583 return -ENODEV;
3584
3585 cgroup_get(cgrp);
3586 cgroup_kn_unlock(of->kn);
3587
Suren Baghdasaryan91179d02022-01-11 15:23:09 -08003588 /* Allow only one trigger per file descriptor */
3589 if (of->priv) {
3590 cgroup_put(cgrp);
3591 return -EBUSY;
3592 }
3593
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08003594 new = psi_trigger_create(&cgrp->psi, buf, nbytes, res);
3595 if (IS_ERR(new)) {
3596 cgroup_put(cgrp);
3597 return PTR_ERR(new);
3598 }
3599
Suren Baghdasaryan91179d02022-01-11 15:23:09 -08003600 smp_store_release(&of->priv, new);
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08003601 cgroup_put(cgrp);
3602
3603 return nbytes;
3604}
3605
3606static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of,
3607 char *buf, size_t nbytes,
3608 loff_t off)
3609{
3610 return cgroup_pressure_write(of, buf, nbytes, PSI_IO);
3611}
3612
3613static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of,
3614 char *buf, size_t nbytes,
3615 loff_t off)
3616{
3617 return cgroup_pressure_write(of, buf, nbytes, PSI_MEM);
3618}
3619
3620static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of,
3621 char *buf, size_t nbytes,
3622 loff_t off)
3623{
3624 return cgroup_pressure_write(of, buf, nbytes, PSI_CPU);
3625}
3626
3627static unsigned int cgroup_pressure_poll(struct kernfs_open_file *of,
3628 poll_table *pt)
3629{
3630 return psi_trigger_poll(&of->priv, of->file, pt);
3631}
3632
3633static void cgroup_pressure_release(struct kernfs_open_file *of)
3634{
Suren Baghdasaryan91179d02022-01-11 15:23:09 -08003635 psi_trigger_destroy(of->priv);
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08003636}
3637#endif /* CONFIG_PSI */
Johannes Weinere868a992018-10-26 15:06:31 -07003638
Tejun Heo59735bf2016-12-27 14:49:03 -05003639static int cgroup_file_open(struct kernfs_open_file *of)
3640{
3641 struct cftype *cft = of->kn->priv;
3642
3643 if (cft->open)
3644 return cft->open(of);
3645 return 0;
3646}
3647
3648static void cgroup_file_release(struct kernfs_open_file *of)
3649{
3650 struct cftype *cft = of->kn->priv;
3651
3652 if (cft->release)
3653 cft->release(of);
3654}
3655
Tejun Heo2bd59d42014-02-11 11:52:49 -05003656static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3657 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003658{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003659 struct cgroup *cgrp = of->kn->parent->priv;
3660 struct cftype *cft = of->kn->priv;
3661 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003662 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003663
Tejun Heob4168642014-05-13 12:16:21 -04003664 if (cft->write)
3665 return cft->write(of, buf, nbytes, off);
3666
Tejun Heo2bd59d42014-02-11 11:52:49 -05003667 /*
3668 * kernfs guarantees that a file isn't deleted with operations in
3669 * flight, which means that the matching css is and stays alive and
3670 * doesn't need to be pinned. The RCU locking is not necessary
3671 * either. It's just for the convenience of using cgroup_css().
3672 */
3673 rcu_read_lock();
3674 css = cgroup_css(cgrp, cft->ss);
3675 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003676
Tejun Heo451af502014-05-13 12:16:21 -04003677 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003678 unsigned long long v;
3679 ret = kstrtoull(buf, 0, &v);
3680 if (!ret)
3681 ret = cft->write_u64(css, cft, v);
3682 } else if (cft->write_s64) {
3683 long long v;
3684 ret = kstrtoll(buf, 0, &v);
3685 if (!ret)
3686 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003687 } else {
3688 ret = -EINVAL;
3689 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003690
Tejun Heoa742c592013-12-05 12:28:03 -05003691 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003692}
3693
Johannes Weiner0ee3cb32018-12-06 09:06:47 -05003694static unsigned int cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt)
3695{
3696 struct cftype *cft = of->kn->priv;
3697
3698 if (cft->poll)
3699 return cft->poll(of, pt);
3700
3701 return kernfs_generic_poll(of, pt);
3702}
3703
Tejun Heo6612f052013-12-05 12:28:04 -05003704static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003705{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003706 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003707}
3708
3709static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3710{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003711 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003712}
3713
3714static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3715{
Tejun Heo59735bf2016-12-27 14:49:03 -05003716 if (seq_cft(seq)->seq_stop)
3717 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003718}
3719
3720static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3721{
Tejun Heo7da11272013-12-05 12:28:04 -05003722 struct cftype *cft = seq_cft(m);
3723 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003724
Tejun Heo2da8ca82013-12-05 12:28:04 -05003725 if (cft->seq_show)
3726 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003727
Tejun Heo896f5192013-12-05 12:28:04 -05003728 if (cft->read_u64)
3729 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3730 else if (cft->read_s64)
3731 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3732 else
3733 return -EINVAL;
3734 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003735}
3736
Tejun Heo2bd59d42014-02-11 11:52:49 -05003737static struct kernfs_ops cgroup_kf_single_ops = {
3738 .atomic_write_len = PAGE_SIZE,
Tejun Heo59735bf2016-12-27 14:49:03 -05003739 .open = cgroup_file_open,
3740 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003741 .write = cgroup_file_write,
Johannes Weiner0ee3cb32018-12-06 09:06:47 -05003742 .poll = cgroup_file_poll,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003743 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003744};
3745
Tejun Heo2bd59d42014-02-11 11:52:49 -05003746static struct kernfs_ops cgroup_kf_ops = {
3747 .atomic_write_len = PAGE_SIZE,
Tejun Heo59735bf2016-12-27 14:49:03 -05003748 .open = cgroup_file_open,
3749 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003750 .write = cgroup_file_write,
Johannes Weiner0ee3cb32018-12-06 09:06:47 -05003751 .poll = cgroup_file_poll,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003752 .seq_start = cgroup_seqfile_start,
3753 .seq_next = cgroup_seqfile_next,
3754 .seq_stop = cgroup_seqfile_stop,
3755 .seq_show = cgroup_seqfile_show,
3756};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003757
3758/*
3759 * cgroup_rename - Only allow simple rename of directories in place.
3760 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003761static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3762 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003763{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003764 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003765 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003766
Alexander Kuznetsov0e3481c2021-06-09 10:17:19 +03003767 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
3768 if (strchr(new_name_str, '\n'))
3769 return -EINVAL;
3770
Tejun Heo2bd59d42014-02-11 11:52:49 -05003771 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003772 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003773 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003774 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003775
Tejun Heo6db8e852013-06-14 11:18:22 -07003776 /*
3777 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003778 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003779 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003780 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003781 return -EPERM;
3782
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003783 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003784 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003785 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003786 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003787 */
3788 kernfs_break_active_protection(new_parent);
3789 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003790
Tejun Heo2bd59d42014-02-11 11:52:49 -05003791 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003792
Tejun Heo2bd59d42014-02-11 11:52:49 -05003793 ret = kernfs_rename(kn, new_parent, new_name_str);
Tejun Heoed1777d2016-08-10 11:23:44 -04003794 if (!ret)
3795 trace_cgroup_rename(cgrp);
Li Zefan65dff752013-03-01 15:01:56 +08003796
Tejun Heo2bd59d42014-02-11 11:52:49 -05003797 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003798
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003799 kernfs_unbreak_active_protection(kn);
3800 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003801 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003802}
3803
Tejun Heo49957f82014-04-07 16:44:47 -04003804/* set uid and gid of cgroup dirs and files to that of the creator */
3805static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3806{
3807 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3808 .ia_uid = current_fsuid(),
3809 .ia_gid = current_fsgid(), };
3810
3811 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3812 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3813 return 0;
3814
3815 return kernfs_setattr(kn, &iattr);
3816}
3817
Tejun Heo4df8dc92015-09-18 17:54:23 -04003818static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3819 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003820{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003821 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003822 struct kernfs_node *kn;
3823 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003824 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003825
Tejun Heo2bd59d42014-02-11 11:52:49 -05003826#ifdef CONFIG_DEBUG_LOCK_ALLOC
3827 key = &cft->lockdep_key;
3828#endif
3829 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3830 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003831 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003832 if (IS_ERR(kn))
3833 return PTR_ERR(kn);
3834
3835 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003836 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003837 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003838 return ret;
3839 }
3840
Tejun Heo6f60ead2015-09-18 17:54:23 -04003841 if (cft->file_offset) {
3842 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3843
Tejun Heo34c06252015-11-05 00:12:24 -05003844 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003845 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003846 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003847 }
3848
Tejun Heof8f22e52014-04-23 11:13:16 -04003849 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003850}
3851
Tejun Heob1f28d32013-06-28 16:24:10 -07003852/**
3853 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003854 * @css: the target css
3855 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003856 * @cfts: array of cftypes to be added
3857 * @is_add: whether to add or remove
3858 *
3859 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003860 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003861 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003862static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3863 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003864 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003865{
Tejun Heo6732ed82015-09-18 17:54:23 -04003866 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003867 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003868
Tejun Heo01f64742014-05-13 12:19:23 -04003869 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003870
Tejun Heo6732ed82015-09-18 17:54:23 -04003871restart:
3872 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003873 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003874 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003875 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003876 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003877 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003878 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003879 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003880 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003881 continue;
3882
Li Zefan2739d3c2013-01-21 18:18:33 +08003883 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003884 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003885 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003886 pr_warn("%s: failed to add %s, err=%d\n",
3887 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003888 cft_end = cft;
3889 is_add = false;
3890 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003891 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003892 } else {
3893 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003894 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003895 }
Tejun Heob598dde2016-02-22 22:25:45 -05003896 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003897}
3898
Tejun Heo21a2d3432014-02-12 09:29:49 -05003899static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003900{
3901 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003902 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003903 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003904 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003905 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003906
Tejun Heo01f64742014-05-13 12:19:23 -04003907 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003908
Li Zefane8c82d22013-06-18 18:48:37 +08003909 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003910 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003911 struct cgroup *cgrp = css->cgroup;
3912
Tejun Heo88cb04b2016-03-03 09:57:58 -05003913 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08003914 continue;
3915
Tejun Heo4df8dc92015-09-18 17:54:23 -04003916 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003917 if (ret)
3918 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003919 }
Tejun Heo21a2d3432014-02-12 09:29:49 -05003920
3921 if (is_add && !ret)
3922 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003923 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003924}
3925
Tejun Heo2da440a2014-02-11 11:52:48 -05003926static void cgroup_exit_cftypes(struct cftype *cfts)
3927{
3928 struct cftype *cft;
3929
Tejun Heo2bd59d42014-02-11 11:52:49 -05003930 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3931 /* free copy for custom atomic_write_len, see init_cftypes() */
3932 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3933 kfree(cft->kf_ops);
3934 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003935 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003936
3937 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003938 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003939 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003940}
3941
Tejun Heo2bd59d42014-02-11 11:52:49 -05003942static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003943{
3944 struct cftype *cft;
3945
Tejun Heo2bd59d42014-02-11 11:52:49 -05003946 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3947 struct kernfs_ops *kf_ops;
3948
Tejun Heo0adb0702014-02-12 09:29:48 -05003949 WARN_ON(cft->ss || cft->kf_ops);
3950
Tejun Heo2bd59d42014-02-11 11:52:49 -05003951 if (cft->seq_start)
3952 kf_ops = &cgroup_kf_ops;
3953 else
3954 kf_ops = &cgroup_kf_single_ops;
3955
3956 /*
3957 * Ugh... if @cft wants a custom max_write_len, we need to
3958 * make a copy of kf_ops to set its atomic_write_len.
3959 */
3960 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3961 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3962 if (!kf_ops) {
3963 cgroup_exit_cftypes(cfts);
3964 return -ENOMEM;
3965 }
3966 kf_ops->atomic_write_len = cft->max_write_len;
3967 }
3968
3969 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003970 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003971 }
3972
3973 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003974}
3975
Tejun Heo21a2d3432014-02-12 09:29:49 -05003976static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3977{
Tejun Heo01f64742014-05-13 12:19:23 -04003978 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d3432014-02-12 09:29:49 -05003979
3980 if (!cfts || !cfts[0].ss)
3981 return -ENOENT;
3982
3983 list_del(&cfts->node);
3984 cgroup_apply_cftypes(cfts, false);
3985 cgroup_exit_cftypes(cfts);
3986 return 0;
3987}
3988
Tejun Heo8e3f6542012-04-01 12:09:55 -07003989/**
Tejun Heo80b13582014-02-12 09:29:48 -05003990 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3991 * @cfts: zero-length name terminated array of cftypes
3992 *
3993 * Unregister @cfts. Files described by @cfts are removed from all
3994 * existing cgroups and all future cgroups won't have them either. This
3995 * function can be called anytime whether @cfts' subsys is attached or not.
3996 *
3997 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3998 * registered.
3999 */
4000int cgroup_rm_cftypes(struct cftype *cfts)
4001{
Tejun Heo21a2d3432014-02-12 09:29:49 -05004002 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05004003
Tejun Heo01f64742014-05-13 12:19:23 -04004004 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d3432014-02-12 09:29:49 -05004005 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04004006 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004007 return ret;
4008}
4009
4010/**
4011 * cgroup_add_cftypes - add an array of cftypes to a subsystem
4012 * @ss: target cgroup subsystem
4013 * @cfts: zero-length name terminated array of cftypes
4014 *
4015 * Register @cfts to @ss. Files described by @cfts are created for all
4016 * existing cgroups to which @ss is attached and all future cgroups will
4017 * have them too. This function can be called anytime whether @ss is
4018 * attached or not.
4019 *
4020 * Returns 0 on successful registration, -errno on failure. Note that this
4021 * function currently returns 0 as long as @cfts registration is successful
4022 * even if some file creation attempts on existing cgroups fail.
4023 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04004024static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07004025{
Tejun Heo9ccece82013-06-28 16:24:11 -07004026 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004027
Tejun Heofc5ed1e2015-09-18 11:56:28 -04004028 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08004029 return 0;
4030
Li Zefandc5736e2014-02-17 10:41:50 +08004031 if (!cfts || cfts[0].name[0] == '\0')
4032 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004033
Tejun Heo2bd59d42014-02-11 11:52:49 -05004034 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07004035 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05004036 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004037
Tejun Heo01f64742014-05-13 12:19:23 -04004038 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d3432014-02-12 09:29:49 -05004039
Tejun Heo0adb0702014-02-12 09:29:48 -05004040 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d3432014-02-12 09:29:49 -05004041 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07004042 if (ret)
Tejun Heo21a2d3432014-02-12 09:29:49 -05004043 cgroup_rm_cftypes_locked(cfts);
4044
Tejun Heo01f64742014-05-13 12:19:23 -04004045 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07004046 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004047}
Tejun Heo79578622012-04-01 12:09:56 -07004048
4049/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04004050 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
4051 * @ss: target cgroup subsystem
4052 * @cfts: zero-length name terminated array of cftypes
4053 *
4054 * Similar to cgroup_add_cftypes() but the added files are only used for
4055 * the default hierarchy.
4056 */
4057int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4058{
4059 struct cftype *cft;
4060
4061 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04004062 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04004063 return cgroup_add_cftypes(ss, cfts);
4064}
4065
4066/**
4067 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
4068 * @ss: target cgroup subsystem
4069 * @cfts: zero-length name terminated array of cftypes
4070 *
4071 * Similar to cgroup_add_cftypes() but the added files are only used for
4072 * the legacy hierarchies.
4073 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04004074int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4075{
Tejun Heoa8ddc822014-07-15 11:05:10 -04004076 struct cftype *cft;
4077
Tejun Heoe4b70372015-10-15 17:00:43 -04004078 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
4079 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04004080 return cgroup_add_cftypes(ss, cfts);
4081}
4082
Li Zefana043e3b2008-02-23 15:24:09 -08004083/**
Tejun Heo34c06252015-11-05 00:12:24 -05004084 * cgroup_file_notify - generate a file modified event for a cgroup_file
4085 * @cfile: target cgroup_file
4086 *
4087 * @cfile must have been obtained by setting cftype->file_offset.
4088 */
4089void cgroup_file_notify(struct cgroup_file *cfile)
4090{
4091 unsigned long flags;
4092
4093 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
4094 if (cfile->kn)
4095 kernfs_notify(cfile->kn);
4096 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
4097}
4098
4099/**
Li Zefana043e3b2008-02-23 15:24:09 -08004100 * cgroup_task_count - count the number of tasks in a cgroup.
4101 * @cgrp: the cgroup in question
4102 *
Tejun Heo91570562016-09-23 16:55:49 -04004103 * Return the number of tasks in the cgroup. The returned number can be
4104 * higher than the actual number of tasks due to css_set references from
4105 * namespace roots and temporary usages.
Li Zefana043e3b2008-02-23 15:24:09 -08004106 */
Tejun Heo07bc3562014-02-13 06:58:39 -05004107static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004108{
4109 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07004110 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004111
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004112 spin_lock_irq(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07004113 list_for_each_entry(link, &cgrp->cset_links, cset_link)
4114 count += atomic_read(&link->cset->refcount);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004115 spin_unlock_irq(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07004116 return count;
4117}
4118
Tejun Heo574bd9f2012-11-09 09:12:29 -08004119/**
Tejun Heo492eb212013-08-08 20:11:25 -04004120 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04004121 * @pos: the current position (%NULL to initiate traversal)
4122 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09004123 *
Tejun Heoc2931b72014-05-16 13:22:51 -04004124 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05004125 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04004126 * that @parent and @pos are accessible. The next sibling is guaranteed to
4127 * be returned regardless of their states.
4128 *
4129 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4130 * css which finished ->css_online() is guaranteed to be visible in the
4131 * future iterations and will stay visible until the last reference is put.
4132 * A css which hasn't finished ->css_online() or already finished
4133 * ->css_offline() may show up during traversal. It's each subsystem's
4134 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09004135 */
Tejun Heoc2931b72014-05-16 13:22:51 -04004136struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
4137 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09004138{
Tejun Heoc2931b72014-05-16 13:22:51 -04004139 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09004140
Tejun Heo8353da12014-05-13 12:19:23 -04004141 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09004142
4143 /*
Tejun Heode3f0342014-05-16 13:22:49 -04004144 * @pos could already have been unlinked from the sibling list.
4145 * Once a cgroup is removed, its ->sibling.next is no longer
4146 * updated when its next sibling changes. CSS_RELEASED is set when
4147 * @pos is taken off list, at which time its next pointer is valid,
4148 * and, as releases are serialized, the one pointed to by the next
4149 * pointer is guaranteed to not have started release yet. This
4150 * implies that if we observe !CSS_RELEASED on @pos in this RCU
4151 * critical section, the one pointed to by its next pointer is
4152 * guaranteed to not have finished its RCU grace period even if we
4153 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04004154 *
Tejun Heode3f0342014-05-16 13:22:49 -04004155 * If @pos has CSS_RELEASED set, its next pointer can't be
4156 * dereferenced; however, as each css is given a monotonically
4157 * increasing unique serial number and always appended to the
4158 * sibling list, the next one can be found by walking the parent's
4159 * children until the first css with higher serial number than
4160 * @pos's. While this path can be slower, it happens iff iteration
4161 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09004162 */
Tejun Heo3b287a52013-08-08 20:11:24 -04004163 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04004164 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
4165 } else if (likely(!(pos->flags & CSS_RELEASED))) {
4166 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04004167 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04004168 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04004169 if (next->serial_nr > pos->serial_nr)
4170 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09004171 }
4172
Tejun Heo3b281af2014-04-23 11:13:15 -04004173 /*
4174 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04004175 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04004176 */
Tejun Heoc2931b72014-05-16 13:22:51 -04004177 if (&next->sibling != &parent->children)
4178 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04004179 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09004180}
Tejun Heo53fa5262013-05-24 10:55:38 +09004181
4182/**
Tejun Heo492eb212013-08-08 20:11:25 -04004183 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004184 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004185 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004186 *
Tejun Heo492eb212013-08-08 20:11:25 -04004187 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004188 * to visit for pre-order traversal of @root's descendants. @root is
4189 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004190 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004191 * While this function requires cgroup_mutex or RCU read locking, it
4192 * doesn't require the whole traversal to be contained in a single critical
4193 * section. This function will return the correct next descendant as long
4194 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04004195 *
4196 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4197 * css which finished ->css_online() is guaranteed to be visible in the
4198 * future iterations and will stay visible until the last reference is put.
4199 * A css which hasn't finished ->css_online() or already finished
4200 * ->css_offline() may show up during traversal. It's each subsystem's
4201 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004202 */
Tejun Heo492eb212013-08-08 20:11:25 -04004203struct cgroup_subsys_state *
4204css_next_descendant_pre(struct cgroup_subsys_state *pos,
4205 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004206{
Tejun Heo492eb212013-08-08 20:11:25 -04004207 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004208
Tejun Heo8353da12014-05-13 12:19:23 -04004209 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004210
Tejun Heobd8815a2013-08-08 20:11:27 -04004211 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09004212 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04004213 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004214
4215 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04004216 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004217 if (next)
4218 return next;
4219
4220 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04004221 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04004222 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004223 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004224 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04004225 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09004226 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08004227
4228 return NULL;
4229}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004230
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004231/**
Tejun Heo492eb212013-08-08 20:11:25 -04004232 * css_rightmost_descendant - return the rightmost descendant of a css
4233 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004234 *
Tejun Heo492eb212013-08-08 20:11:25 -04004235 * Return the rightmost descendant of @pos. If there's no descendant, @pos
4236 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004237 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09004238 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004239 * While this function requires cgroup_mutex or RCU read locking, it
4240 * doesn't require the whole traversal to be contained in a single critical
4241 * section. This function will return the correct rightmost descendant as
4242 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004243 */
Tejun Heo492eb212013-08-08 20:11:25 -04004244struct cgroup_subsys_state *
4245css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004246{
Tejun Heo492eb212013-08-08 20:11:25 -04004247 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004248
Tejun Heo8353da12014-05-13 12:19:23 -04004249 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004250
4251 do {
4252 last = pos;
4253 /* ->prev isn't RCU safe, walk ->next till the end */
4254 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04004255 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004256 pos = tmp;
4257 } while (pos);
4258
4259 return last;
4260}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004261
Tejun Heo492eb212013-08-08 20:11:25 -04004262static struct cgroup_subsys_state *
4263css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004264{
Tejun Heo492eb212013-08-08 20:11:25 -04004265 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004266
4267 do {
4268 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04004269 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004270 } while (pos);
4271
4272 return last;
4273}
4274
4275/**
Tejun Heo492eb212013-08-08 20:11:25 -04004276 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004277 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004278 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004279 *
Tejun Heo492eb212013-08-08 20:11:25 -04004280 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004281 * to visit for post-order traversal of @root's descendants. @root is
4282 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004283 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004284 * While this function requires cgroup_mutex or RCU read locking, it
4285 * doesn't require the whole traversal to be contained in a single critical
4286 * section. This function will return the correct next descendant as long
4287 * as both @pos and @cgroup are accessible and @pos is a descendant of
4288 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04004289 *
4290 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4291 * css which finished ->css_online() is guaranteed to be visible in the
4292 * future iterations and will stay visible until the last reference is put.
4293 * A css which hasn't finished ->css_online() or already finished
4294 * ->css_offline() may show up during traversal. It's each subsystem's
4295 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004296 */
Tejun Heo492eb212013-08-08 20:11:25 -04004297struct cgroup_subsys_state *
4298css_next_descendant_post(struct cgroup_subsys_state *pos,
4299 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004300{
Tejun Heo492eb212013-08-08 20:11:25 -04004301 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004302
Tejun Heo8353da12014-05-13 12:19:23 -04004303 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004304
Tejun Heo58b79a92013-09-06 15:31:08 -04004305 /* if first iteration, visit leftmost descendant which may be @root */
4306 if (!pos)
4307 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004308
Tejun Heobd8815a2013-08-08 20:11:27 -04004309 /* if we visited @root, we're done */
4310 if (pos == root)
4311 return NULL;
4312
Tejun Heo574bd9f2012-11-09 09:12:29 -08004313 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004314 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004315 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04004316 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004317
4318 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004319 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004320}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004321
Tejun Heof3d46502014-05-16 13:22:52 -04004322/**
4323 * css_has_online_children - does a css have online children
4324 * @css: the target css
4325 *
4326 * Returns %true if @css has any online children; otherwise, %false. This
4327 * function can be called from any context but the caller is responsible
4328 * for synchronizing against on/offlining as necessary.
4329 */
4330bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04004331{
Tejun Heof3d46502014-05-16 13:22:52 -04004332 struct cgroup_subsys_state *child;
4333 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04004334
4335 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04004336 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08004337 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04004338 ret = true;
4339 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04004340 }
4341 }
4342 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04004343 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08004344}
4345
Tejun Heo0942eee2013-08-08 20:11:26 -04004346/**
Tejun Heoecb9d532015-10-15 16:41:52 -04004347 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04004348 * @it: the iterator to advance
4349 *
4350 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04004351 */
Tejun Heoecb9d532015-10-15 16:41:52 -04004352static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04004353{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004354 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04004355 struct cgrp_cset_link *link;
4356 struct css_set *cset;
4357
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004358 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004359
Tejun Heod5158762013-08-08 20:11:26 -04004360 /* Advance to the next non-empty css_set */
4361 do {
4362 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004363 if (l == it->cset_head) {
4364 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04004365 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04004366 return;
4367 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004368
4369 if (it->ss) {
4370 cset = container_of(l, struct css_set,
4371 e_cset_node[it->ss->id]);
4372 } else {
4373 link = list_entry(l, struct cgrp_cset_link, cset_link);
4374 cset = link->cset;
4375 }
Tejun Heo0de09422015-10-15 16:41:49 -04004376 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05004377
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004378 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05004379
4380 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004381 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05004382 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004383 it->task_pos = cset->mg_tasks.next;
4384
4385 it->tasks_head = &cset->tasks;
4386 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04004387
4388 /*
4389 * We don't keep css_sets locked across iteration steps and thus
4390 * need to take steps to ensure that iteration can be resumed after
4391 * the lock is re-acquired. Iteration is performed at two levels -
4392 * css_sets and tasks in them.
4393 *
4394 * Once created, a css_set never leaves its cgroup lists, so a
4395 * pinned css_set is guaranteed to stay put and we can resume
4396 * iteration afterwards.
4397 *
4398 * Tasks may leave @cset across iteration steps. This is resolved
4399 * by registering each iterator with the css_set currently being
4400 * walked and making css_set_move_task() advance iterators whose
4401 * next task is leaving.
4402 */
4403 if (it->cur_cset) {
4404 list_del(&it->iters_node);
4405 put_css_set_locked(it->cur_cset);
4406 }
4407 get_css_set(cset);
4408 it->cur_cset = cset;
4409 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04004410}
4411
Tejun Heoecb9d532015-10-15 16:41:52 -04004412static void css_task_iter_advance(struct css_task_iter *it)
4413{
4414 struct list_head *l = it->task_pos;
4415
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004416 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04004417 WARN_ON_ONCE(!l);
4418
4419 /*
4420 * Advance iterator to find next entry. cset->tasks is consumed
4421 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
4422 * next cset.
4423 */
4424 l = l->next;
4425
4426 if (l == it->tasks_head)
4427 l = it->mg_tasks_head->next;
4428
4429 if (l == it->mg_tasks_head)
4430 css_task_iter_advance_css_set(it);
4431 else
4432 it->task_pos = l;
4433}
4434
Tejun Heo0942eee2013-08-08 20:11:26 -04004435/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004436 * css_task_iter_start - initiate task iteration
4437 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04004438 * @it: the task iterator to use
4439 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004440 * Initiate iteration through the tasks of @css. The caller can call
4441 * css_task_iter_next() to walk through the tasks until the function
4442 * returns NULL. On completion of iteration, css_task_iter_end() must be
4443 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04004444 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004445void css_task_iter_start(struct cgroup_subsys_state *css,
4446 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004447{
Tejun Heo56fde9e2014-02-13 06:58:38 -05004448 /* no one should try to iterate before mounting cgroups */
4449 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07004450
Tejun Heoed27b9f2015-10-15 16:41:52 -04004451 memset(it, 0, sizeof(*it));
4452
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004453 spin_lock_irq(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004454
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004455 it->ss = css->ss;
4456
4457 if (it->ss)
4458 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4459 else
4460 it->cset_pos = &css->cgroup->cset_links;
4461
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004462 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004463
Tejun Heoecb9d532015-10-15 16:41:52 -04004464 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004465
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004466 spin_unlock_irq(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004467}
Paul Menage817929e2007-10-18 23:39:36 -07004468
Tejun Heo0942eee2013-08-08 20:11:26 -04004469/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004470 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004471 * @it: the task iterator being iterated
4472 *
4473 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004474 * initialized via css_task_iter_start(). Returns NULL when the iteration
4475 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004476 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004477struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004478{
Tejun Heod5745672015-10-29 11:43:05 +09004479 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004480 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004481 it->cur_task = NULL;
4482 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004483
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004484 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004485
Tejun Heod5745672015-10-29 11:43:05 +09004486 if (it->task_pos) {
4487 it->cur_task = list_entry(it->task_pos, struct task_struct,
4488 cg_list);
4489 get_task_struct(it->cur_task);
4490 css_task_iter_advance(it);
4491 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004492
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004493 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004494
4495 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004496}
4497
Tejun Heo0942eee2013-08-08 20:11:26 -04004498/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004499 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004500 * @it: the task iterator to finish
4501 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004502 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004503 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004504void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004505{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004506 if (it->cur_cset) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004507 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004508 list_del(&it->iters_node);
4509 put_css_set_locked(it->cur_cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004510 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004511 }
4512
4513 if (it->cur_task)
4514 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004515}
4516
4517/**
4518 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4519 * @to: cgroup to which the tasks will be moved
4520 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004521 *
4522 * Locking rules between cgroup_post_fork() and the migration path
4523 * guarantee that, if a task is forking while being migrated, the new child
4524 * is guaranteed to be either visible in the source cgroup after the
4525 * parent's migration is complete or put into the target cgroup. No task
4526 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004527 */
4528int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4529{
Tejun Heo952aaa12014-02-25 10:04:03 -05004530 LIST_HEAD(preloaded_csets);
4531 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004532 struct css_task_iter it;
4533 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004534 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004535
Tejun Heo6c694c82016-03-08 11:51:25 -05004536 if (!cgroup_may_migrate_to(to))
4537 return -EBUSY;
4538
Tejun Heo952aaa12014-02-25 10:04:03 -05004539 mutex_lock(&cgroup_mutex);
4540
Eric W. Biedermaneedd0f42016-07-15 06:35:51 -05004541 percpu_down_write(&cgroup_threadgroup_rwsem);
4542
Tejun Heo952aaa12014-02-25 10:04:03 -05004543 /* all tasks in @from are being moved, all csets are source */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004544 spin_lock_irq(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004545 list_for_each_entry(link, &from->cset_links, cset_link)
4546 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004547 spin_unlock_irq(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004548
Tejun Heoe4857982016-03-08 11:51:26 -05004549 ret = cgroup_migrate_prepare_dst(&preloaded_csets);
Tejun Heo952aaa12014-02-25 10:04:03 -05004550 if (ret)
4551 goto out_err;
4552
4553 /*
Rami Rosen2cfa2b12016-01-09 23:33:06 +02004554 * Migrate tasks one-by-one until @from is empty. This fails iff
Tejun Heo952aaa12014-02-25 10:04:03 -05004555 * ->can_attach() fails.
4556 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004557 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004558 css_task_iter_start(&from->self, &it);
Prateek Sood49b74f12017-12-19 16:11:54 +05304559
4560 do {
4561 task = css_task_iter_next(&it);
4562 } while (task && (task->flags & PF_EXITING));
4563
Tejun Heoe406d1c2014-02-13 06:58:39 -05004564 if (task)
4565 get_task_struct(task);
4566 css_task_iter_end(&it);
4567
4568 if (task) {
Tejun Heo37ff9f82016-03-08 11:51:26 -05004569 ret = cgroup_migrate(task, false, to->root);
Tejun Heoed1777d2016-08-10 11:23:44 -04004570 if (!ret)
4571 trace_cgroup_transfer_tasks(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004572 put_task_struct(task);
4573 }
4574 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004575out_err:
4576 cgroup_migrate_finish(&preloaded_csets);
Eric W. Biedermaneedd0f42016-07-15 06:35:51 -05004577 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo952aaa12014-02-25 10:04:03 -05004578 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004579 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004580}
4581
Paul Menage817929e2007-10-18 23:39:36 -07004582/*
Ben Blum102a7752009-09-23 15:56:26 -07004583 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004584 *
4585 * Reading this file can return large amounts of data if a cgroup has
4586 * *lots* of attached tasks. So it may need several calls to read(),
4587 * but we cannot guarantee that the information we produce is correct
4588 * unless we produce it entirely atomically.
4589 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004590 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004591
Li Zefan24528252012-01-20 11:58:43 +08004592/* which pidlist file are we talking about? */
4593enum cgroup_filetype {
4594 CGROUP_FILE_PROCS,
4595 CGROUP_FILE_TASKS,
4596};
4597
4598/*
4599 * A pidlist is a list of pids that virtually represents the contents of one
4600 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4601 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4602 * to the cgroup.
4603 */
4604struct cgroup_pidlist {
4605 /*
4606 * used to find which pidlist is wanted. doesn't change as long as
4607 * this particular list stays in the list.
4608 */
4609 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4610 /* array of xids */
4611 pid_t *list;
4612 /* how many elements the above list has */
4613 int length;
Li Zefan24528252012-01-20 11:58:43 +08004614 /* each of these stored in a list by its cgroup */
4615 struct list_head links;
4616 /* pointer to the cgroup we belong to, for list removal purposes */
4617 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004618 /* for delayed destruction */
4619 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004620};
4621
Paul Menagebbcb81d2007-10-18 23:39:32 -07004622/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004623 * The following two functions "fix" the issue where there are more pids
4624 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4625 * TODO: replace with a kernel-wide solution to this problem
4626 */
4627#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4628static void *pidlist_allocate(int count)
4629{
4630 if (PIDLIST_TOO_LARGE(count))
4631 return vmalloc(count * sizeof(pid_t));
4632 else
4633 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4634}
Tejun Heob1a21362013-11-29 10:42:58 -05004635
Ben Blumd1d9fd32009-09-23 15:56:28 -07004636static void pidlist_free(void *p)
4637{
Bandan Das58794512015-03-02 17:51:10 -05004638 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004639}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004640
4641/*
Tejun Heob1a21362013-11-29 10:42:58 -05004642 * Used to destroy all pidlists lingering waiting for destroy timer. None
4643 * should be left afterwards.
4644 */
4645static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4646{
4647 struct cgroup_pidlist *l, *tmp_l;
4648
4649 mutex_lock(&cgrp->pidlist_mutex);
4650 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4651 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4652 mutex_unlock(&cgrp->pidlist_mutex);
4653
4654 flush_workqueue(cgroup_pidlist_destroy_wq);
4655 BUG_ON(!list_empty(&cgrp->pidlists));
4656}
4657
4658static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4659{
4660 struct delayed_work *dwork = to_delayed_work(work);
4661 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4662 destroy_dwork);
4663 struct cgroup_pidlist *tofree = NULL;
4664
4665 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004666
4667 /*
Tejun Heo04502362013-11-29 10:42:59 -05004668 * Destroy iff we didn't get queued again. The state won't change
4669 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004670 */
Tejun Heo04502362013-11-29 10:42:59 -05004671 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004672 list_del(&l->links);
4673 pidlist_free(l->list);
4674 put_pid_ns(l->key.ns);
4675 tofree = l;
4676 }
4677
Tejun Heob1a21362013-11-29 10:42:58 -05004678 mutex_unlock(&l->owner->pidlist_mutex);
4679 kfree(tofree);
4680}
4681
4682/*
Ben Blum102a7752009-09-23 15:56:26 -07004683 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004684 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004685 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004686static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004687{
Ben Blum102a7752009-09-23 15:56:26 -07004688 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004689
4690 /*
4691 * we presume the 0th element is unique, so i starts at 1. trivial
4692 * edge cases first; no work needs to be done for either
4693 */
4694 if (length == 0 || length == 1)
4695 return length;
4696 /* src and dest walk down the list; dest counts unique elements */
4697 for (src = 1; src < length; src++) {
4698 /* find next unique element */
4699 while (list[src] == list[src-1]) {
4700 src++;
4701 if (src == length)
4702 goto after;
4703 }
4704 /* dest always points to where the next unique element goes */
4705 list[dest] = list[src];
4706 dest++;
4707 }
4708after:
Ben Blum102a7752009-09-23 15:56:26 -07004709 return dest;
4710}
4711
Tejun Heoafb2bc12013-11-29 10:42:59 -05004712/*
4713 * The two pid files - task and cgroup.procs - guaranteed that the result
4714 * is sorted, which forced this whole pidlist fiasco. As pid order is
4715 * different per namespace, each namespace needs differently sorted list,
4716 * making it impossible to use, for example, single rbtree of member tasks
4717 * sorted by task pointer. As pidlists can be fairly large, allocating one
4718 * per open file is dangerous, so cgroup had to implement shared pool of
4719 * pidlists keyed by cgroup and namespace.
4720 *
4721 * All this extra complexity was caused by the original implementation
4722 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004723 * want to do away with it. Explicitly scramble sort order if on the
4724 * default hierarchy so that no such expectation exists in the new
4725 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004726 *
4727 * Scrambling is done by swapping every two consecutive bits, which is
4728 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4729 */
4730static pid_t pid_fry(pid_t pid)
4731{
4732 unsigned a = pid & 0x55555555;
4733 unsigned b = pid & 0xAAAAAAAA;
4734
4735 return (a << 1) | (b >> 1);
4736}
4737
4738static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4739{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004740 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004741 return pid_fry(pid);
4742 else
4743 return pid;
4744}
4745
Ben Blum102a7752009-09-23 15:56:26 -07004746static int cmppid(const void *a, const void *b)
4747{
4748 return *(pid_t *)a - *(pid_t *)b;
4749}
4750
Tejun Heoafb2bc12013-11-29 10:42:59 -05004751static int fried_cmppid(const void *a, const void *b)
4752{
4753 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4754}
4755
Ben Blum72a8cb32009-09-23 15:56:27 -07004756static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4757 enum cgroup_filetype type)
4758{
4759 struct cgroup_pidlist *l;
4760 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004761 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004762
Tejun Heoe6b81712013-11-29 10:42:59 -05004763 lockdep_assert_held(&cgrp->pidlist_mutex);
4764
4765 list_for_each_entry(l, &cgrp->pidlists, links)
4766 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004767 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004768 return NULL;
4769}
4770
Ben Blum72a8cb32009-09-23 15:56:27 -07004771/*
4772 * find the appropriate pidlist for our purpose (given procs vs tasks)
4773 * returns with the lock on that pidlist already held, and takes care
4774 * of the use count, or returns NULL with no locks held if we're out of
4775 * memory.
4776 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004777static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4778 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004779{
4780 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004781
Tejun Heoe6b81712013-11-29 10:42:59 -05004782 lockdep_assert_held(&cgrp->pidlist_mutex);
4783
4784 l = cgroup_pidlist_find(cgrp, type);
4785 if (l)
4786 return l;
4787
Ben Blum72a8cb32009-09-23 15:56:27 -07004788 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004789 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004790 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004791 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004792
Tejun Heob1a21362013-11-29 10:42:58 -05004793 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004794 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004795 /* don't need task_nsproxy() if we're looking at ourself */
4796 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004797 l->owner = cgrp;
4798 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004799 return l;
4800}
4801
4802/*
Ben Blum102a7752009-09-23 15:56:26 -07004803 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4804 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004805static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4806 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004807{
4808 pid_t *array;
4809 int length;
4810 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004811 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004812 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004813 struct cgroup_pidlist *l;
4814
Tejun Heo4bac00d2013-11-29 10:42:59 -05004815 lockdep_assert_held(&cgrp->pidlist_mutex);
4816
Ben Blum102a7752009-09-23 15:56:26 -07004817 /*
4818 * If cgroup gets more users after we read count, we won't have
4819 * enough space - tough. This race is indistinguishable to the
4820 * caller from the case that the additional cgroup users didn't
4821 * show up until sometime later on.
4822 */
4823 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004824 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004825 if (!array)
4826 return -ENOMEM;
4827 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004828 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004829 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004830 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004831 break;
Ben Blum102a7752009-09-23 15:56:26 -07004832 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004833 if (type == CGROUP_FILE_PROCS)
4834 pid = task_tgid_vnr(tsk);
4835 else
4836 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004837 if (pid > 0) /* make sure to only use valid results */
4838 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004839 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004840 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004841 length = n;
4842 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004843 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004844 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4845 else
4846 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004847 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004848 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004849
Tejun Heoe6b81712013-11-29 10:42:59 -05004850 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004851 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004852 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004853 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004854 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004855
4856 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004857 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004858 l->list = array;
4859 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004860 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004861 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004862}
4863
Balbir Singh846c7bb2007-10-18 23:39:44 -07004864/**
Li Zefana043e3b2008-02-23 15:24:09 -08004865 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004866 * @stats: cgroupstats to fill information into
4867 * @dentry: A dentry entry belonging to the cgroup for which stats have
4868 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004869 *
4870 * Build and fill cgroupstats so that taskstats can export it to user
4871 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004872 */
4873int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4874{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004875 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004876 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004877 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004878 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004879
Tejun Heo2bd59d42014-02-11 11:52:49 -05004880 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4881 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4882 kernfs_type(kn) != KERNFS_DIR)
4883 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004884
Li Zefanbad34662014-02-14 16:54:28 +08004885 mutex_lock(&cgroup_mutex);
4886
Tejun Heo2bd59d42014-02-11 11:52:49 -05004887 /*
4888 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004889 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004890 * @kn->priv is RCU safe. Let's do the RCU dancing.
4891 */
4892 rcu_read_lock();
4893 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004894 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004895 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004896 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004897 return -ENOENT;
4898 }
Li Zefanbad34662014-02-14 16:54:28 +08004899 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004900
Tejun Heo9d800df2014-05-14 09:15:00 -04004901 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004902 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004903 switch (tsk->state) {
4904 case TASK_RUNNING:
4905 stats->nr_running++;
4906 break;
4907 case TASK_INTERRUPTIBLE:
4908 stats->nr_sleeping++;
4909 break;
4910 case TASK_UNINTERRUPTIBLE:
4911 stats->nr_uninterruptible++;
4912 break;
4913 case TASK_STOPPED:
4914 stats->nr_stopped++;
4915 break;
4916 default:
4917 if (delayacct_is_task_waiting_on_io(tsk))
4918 stats->nr_io_wait++;
4919 break;
4920 }
4921 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004922 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004923
Li Zefanbad34662014-02-14 16:54:28 +08004924 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004925 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004926}
4927
Paul Menage8f3ff202009-09-23 15:56:25 -07004928
Paul Menagecc31edc2008-10-18 20:28:04 -07004929/*
Ben Blum102a7752009-09-23 15:56:26 -07004930 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004931 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004932 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004933 */
4934
Ben Blum102a7752009-09-23 15:56:26 -07004935static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004936{
4937 /*
4938 * Initially we receive a position value that corresponds to
4939 * one more than the last pid shown (or 0 on the first call or
4940 * after a seek to the start). Use a binary-search to find the
4941 * next pid to display, if any
4942 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004943 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004944 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004945 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004946 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004947 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004948 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004949
Tejun Heo4bac00d2013-11-29 10:42:59 -05004950 mutex_lock(&cgrp->pidlist_mutex);
4951
4952 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004953 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004954 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004955 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004956 * could already have been destroyed.
4957 */
Tejun Heo5d224442013-12-05 12:28:04 -05004958 if (of->priv)
4959 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004960
4961 /*
4962 * Either this is the first start() after open or the matching
4963 * pidlist has been destroyed inbetween. Create a new one.
4964 */
Tejun Heo5d224442013-12-05 12:28:04 -05004965 if (!of->priv) {
4966 ret = pidlist_array_load(cgrp, type,
4967 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004968 if (ret)
4969 return ERR_PTR(ret);
4970 }
Tejun Heo5d224442013-12-05 12:28:04 -05004971 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004972
Paul Menagecc31edc2008-10-18 20:28:04 -07004973 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004974 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004975
Paul Menagecc31edc2008-10-18 20:28:04 -07004976 while (index < end) {
4977 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004978 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004979 index = mid;
4980 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004981 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004982 index = mid + 1;
4983 else
4984 end = mid;
4985 }
4986 }
4987 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004988 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004989 return NULL;
4990 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004991 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004992 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004993 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004994}
4995
Ben Blum102a7752009-09-23 15:56:26 -07004996static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004997{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004998 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004999 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05005000
Tejun Heo5d224442013-12-05 12:28:04 -05005001 if (l)
5002 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05005003 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05005004 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07005005}
5006
Ben Blum102a7752009-09-23 15:56:26 -07005007static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07005008{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005009 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05005010 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07005011 pid_t *p = v;
5012 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07005013 /*
5014 * Advance to the next pid in the array. If this goes off the
5015 * end, we're done
5016 */
5017 p++;
5018 if (p >= end) {
5019 return NULL;
5020 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05005021 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07005022 return p;
5023 }
5024}
5025
Ben Blum102a7752009-09-23 15:56:26 -07005026static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07005027{
Joe Perches94ff2122015-04-15 16:18:20 -07005028 seq_printf(s, "%d\n", *(int *)v);
5029
5030 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07005031}
5032
Tejun Heo182446d2013-08-08 20:11:24 -04005033static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
5034 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005035{
Tejun Heo182446d2013-08-08 20:11:24 -04005036 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005037}
5038
Tejun Heo182446d2013-08-08 20:11:24 -04005039static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
5040 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07005041{
Paul Menage6379c102008-07-25 01:47:01 -07005042 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04005043 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07005044 else
Tejun Heo182446d2013-08-08 20:11:24 -04005045 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07005046 return 0;
5047}
5048
Tejun Heo182446d2013-08-08 20:11:24 -04005049static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
5050 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07005051{
Tejun Heo182446d2013-08-08 20:11:24 -04005052 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005053}
5054
Tejun Heo182446d2013-08-08 20:11:24 -04005055static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
5056 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07005057{
5058 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04005059 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005060 else
Tejun Heo182446d2013-08-08 20:11:24 -04005061 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005062 return 0;
5063}
5064
Tejun Heoa14c6872014-07-15 11:05:09 -04005065/* cgroup core interface files for the default hierarchy */
5066static struct cftype cgroup_dfl_base_files[] = {
5067 {
5068 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04005069 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04005070 .seq_start = cgroup_pidlist_start,
5071 .seq_next = cgroup_pidlist_next,
5072 .seq_stop = cgroup_pidlist_stop,
5073 .seq_show = cgroup_pidlist_show,
5074 .private = CGROUP_FILE_PROCS,
5075 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04005076 },
5077 {
5078 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04005079 .seq_show = cgroup_controllers_show,
5080 },
5081 {
5082 .name = "cgroup.subtree_control",
5083 .seq_show = cgroup_subtree_control_show,
5084 .write = cgroup_subtree_control_write,
5085 },
5086 {
Tejun Heo4a07c222015-09-18 17:54:22 -04005087 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04005088 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04005089 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04005090 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04005091 },
Johannes Weinere868a992018-10-26 15:06:31 -07005092#ifdef CONFIG_PSI
5093 {
5094 .name = "io.pressure",
5095 .flags = CFTYPE_NOT_ON_ROOT,
5096 .seq_show = cgroup_io_pressure_show,
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08005097 .write = cgroup_io_pressure_write,
5098 .poll = cgroup_pressure_poll,
5099 .release = cgroup_pressure_release,
Johannes Weinere868a992018-10-26 15:06:31 -07005100 },
5101 {
5102 .name = "memory.pressure",
5103 .flags = CFTYPE_NOT_ON_ROOT,
5104 .seq_show = cgroup_memory_pressure_show,
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08005105 .write = cgroup_memory_pressure_write,
5106 .poll = cgroup_pressure_poll,
5107 .release = cgroup_pressure_release,
Johannes Weinere868a992018-10-26 15:06:31 -07005108 },
5109 {
5110 .name = "cpu.pressure",
5111 .flags = CFTYPE_NOT_ON_ROOT,
5112 .seq_show = cgroup_cpu_pressure_show,
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08005113 .write = cgroup_cpu_pressure_write,
5114 .poll = cgroup_pressure_poll,
5115 .release = cgroup_pressure_release,
Johannes Weinere868a992018-10-26 15:06:31 -07005116 },
Suren Baghdasaryana163d3f2018-12-03 17:36:42 -08005117#endif /* CONFIG_PSI */
Tejun Heoa14c6872014-07-15 11:05:09 -04005118 { } /* terminate */
5119};
5120
5121/* cgroup core interface files for the legacy hierarchies */
5122static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005123 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07005124 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05005125 .seq_start = cgroup_pidlist_start,
5126 .seq_next = cgroup_pidlist_next,
5127 .seq_stop = cgroup_pidlist_stop,
5128 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05005129 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04005130 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07005131 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07005132 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07005133 .name = "cgroup.clone_children",
5134 .read_u64 = cgroup_clone_children_read,
5135 .write_u64 = cgroup_clone_children_write,
5136 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07005137 {
Tejun Heo873fe092013-04-14 20:15:26 -07005138 .name = "cgroup.sane_behavior",
5139 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05005140 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07005141 },
Tejun Heof8f22e52014-04-23 11:13:16 -04005142 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07005143 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05005144 .seq_start = cgroup_pidlist_start,
5145 .seq_next = cgroup_pidlist_next,
5146 .seq_stop = cgroup_pidlist_stop,
5147 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05005148 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04005149 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07005150 },
5151 {
5152 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07005153 .read_u64 = cgroup_read_notify_on_release,
5154 .write_u64 = cgroup_write_notify_on_release,
5155 },
Tejun Heo873fe092013-04-14 20:15:26 -07005156 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07005157 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04005158 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05005159 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04005160 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05005161 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07005162 },
Tejun Heodb0416b2012-04-01 12:09:55 -07005163 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07005164};
5165
Tejun Heo0c21ead2013-08-13 20:22:51 -04005166/*
5167 * css destruction is four-stage process.
5168 *
5169 * 1. Destruction starts. Killing of the percpu_ref is initiated.
5170 * Implemented in kill_css().
5171 *
5172 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04005173 * and thus css_tryget_online() is guaranteed to fail, the css can be
5174 * offlined by invoking offline_css(). After offlining, the base ref is
5175 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04005176 *
5177 * 3. When the percpu_ref reaches zero, the only possible remaining
5178 * accessors are inside RCU read sections. css_release() schedules the
5179 * RCU callback.
5180 *
5181 * 4. After the grace period, the css can be freed. Implemented in
5182 * css_free_work_fn().
5183 *
5184 * It is actually hairier because both step 2 and 4 require process context
5185 * and thus involve punting to css->destroy_work adding two additional
5186 * steps to the already complex sequence.
5187 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04005188static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07005189{
5190 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04005191 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08005192 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04005193 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07005194
Tejun Heo9a1049d2014-06-28 08:10:14 -04005195 percpu_ref_exit(&css->refcnt);
5196
Vladimir Davydov01e58652015-02-12 14:59:26 -08005197 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04005198 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05005199 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08005200 int id = css->id;
5201
Vladimir Davydov01e58652015-02-12 14:59:26 -08005202 ss->css_free(css);
5203 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04005204 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05005205
5206 if (parent)
5207 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04005208 } else {
5209 /* cgroup free path */
5210 atomic_dec(&cgrp->root->nr_cgrps);
5211 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08005212 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04005213
Tejun Heod51f39b2014-05-16 13:22:48 -04005214 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04005215 /*
5216 * We get a ref to the parent, and put the ref when
5217 * this cgroup is being freed, so it's guaranteed
5218 * that the parent won't be destroyed before its
5219 * children.
5220 */
Tejun Heod51f39b2014-05-16 13:22:48 -04005221 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04005222 kernfs_put(cgrp->kn);
Johannes Weinere868a992018-10-26 15:06:31 -07005223 if (cgroup_on_dfl(cgrp))
5224 psi_cgroup_free(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04005225 kfree(cgrp);
5226 } else {
5227 /*
5228 * This is root cgroup's refcnt reaching zero,
5229 * which indicates that the root should be
5230 * released.
5231 */
5232 cgroup_destroy_root(cgrp->root);
5233 }
5234 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04005235}
5236
5237static void css_free_rcu_fn(struct rcu_head *rcu_head)
5238{
5239 struct cgroup_subsys_state *css =
5240 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
5241
Tejun Heo0c21ead2013-08-13 20:22:51 -04005242 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05005243 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005244}
5245
Tejun Heo25e15d82014-05-14 09:15:02 -04005246static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005247{
5248 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04005249 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04005250 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04005251 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07005252
Tejun Heo1fed1b22014-05-16 13:22:49 -04005253 mutex_lock(&cgroup_mutex);
5254
Tejun Heode3f0342014-05-16 13:22:49 -04005255 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04005256 list_del_rcu(&css->sibling);
5257
Tejun Heo9d755d32014-05-14 09:15:02 -04005258 if (ss) {
5259 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08005260 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05005261 if (ss->css_released)
5262 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04005263 } else {
5264 /* cgroup release path */
Tejun Heoed1777d2016-08-10 11:23:44 -04005265 trace_cgroup_release(cgrp);
5266
Tejun Heo9d755d32014-05-14 09:15:02 -04005267 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
5268 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08005269
5270 /*
5271 * There are two control paths which try to determine
5272 * cgroup from dentry without going through kernfs -
5273 * cgroupstats_build() and css_tryget_online_from_dir().
5274 * Those are supported by RCU protecting clearing of
5275 * cgrp->kn->priv backpointer.
5276 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005277 if (cgrp->kn)
5278 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
5279 NULL);
Daniel Mackf791c422016-11-23 16:52:26 +01005280
5281 cgroup_bpf_put(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04005282 }
Tejun Heo15a4c832014-05-04 15:09:14 -04005283
Tejun Heo1fed1b22014-05-16 13:22:49 -04005284 mutex_unlock(&cgroup_mutex);
5285
Tejun Heo0c21ead2013-08-13 20:22:51 -04005286 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005287}
5288
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005289static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005290{
5291 struct cgroup_subsys_state *css =
5292 container_of(ref, struct cgroup_subsys_state, refcnt);
5293
Tejun Heo25e15d82014-05-14 09:15:02 -04005294 INIT_WORK(&css->destroy_work, css_release_work_fn);
5295 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005296}
5297
Tejun Heoddfcada2014-05-04 15:09:14 -04005298static void init_and_link_css(struct cgroup_subsys_state *css,
5299 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005300{
Tejun Heo0cb51d72014-05-16 13:22:49 -04005301 lockdep_assert_held(&cgroup_mutex);
5302
Tejun Heoddfcada2014-05-04 15:09:14 -04005303 cgroup_get(cgrp);
5304
Tejun Heod5c419b2014-05-16 13:22:48 -04005305 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07005306 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04005307 css->ss = ss;
Tejun Heo8fa3b8d2016-05-26 15:42:13 -04005308 css->id = -1;
Tejun Heod5c419b2014-05-16 13:22:48 -04005309 INIT_LIST_HEAD(&css->sibling);
5310 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04005311 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05005312 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005313
Tejun Heod51f39b2014-05-16 13:22:48 -04005314 if (cgroup_parent(cgrp)) {
5315 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04005316 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04005317 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04005318
Tejun Heoca8bdca2013-08-26 18:40:56 -04005319 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005320}
5321
Li Zefan2a4ac632013-07-31 16:16:40 +08005322/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04005323static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08005324{
Tejun Heo623f9262013-08-13 11:01:55 -04005325 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08005326 int ret = 0;
5327
Tejun Heoa31f2d32012-11-19 08:13:37 -08005328 lockdep_assert_held(&cgroup_mutex);
5329
Tejun Heo92fb9742012-11-19 08:13:38 -08005330 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04005331 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04005332 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04005333 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05005334 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05005335
5336 atomic_inc(&css->online_cnt);
5337 if (css->parent)
5338 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04005339 }
Tejun Heob1929db2012-11-19 08:13:38 -08005340 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08005341}
5342
Li Zefan2a4ac632013-07-31 16:16:40 +08005343/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04005344static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08005345{
Tejun Heo623f9262013-08-13 11:01:55 -04005346 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08005347
5348 lockdep_assert_held(&cgroup_mutex);
5349
5350 if (!(css->flags & CSS_ONLINE))
5351 return;
5352
Vladimir Davydovfa062352016-03-01 19:56:30 +03005353 if (ss->css_reset)
5354 ss->css_reset(css);
5355
Li Zefand7eeac12013-03-12 15:35:59 -07005356 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04005357 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08005358
Tejun Heoeb954192013-08-08 20:11:23 -04005359 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04005360 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04005361
5362 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08005363}
5364
Tejun Heoc81c925a2013-12-06 15:11:56 -05005365/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005366 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05005367 * @cgrp: the cgroup new css will be associated with
5368 * @ss: the subsys of new css
5369 *
5370 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005371 * css is online and installed in @cgrp. This function doesn't create the
5372 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05005373 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005374static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
5375 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05005376{
Tejun Heod51f39b2014-05-16 13:22:48 -04005377 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04005378 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005379 struct cgroup_subsys_state *css;
5380 int err;
5381
Tejun Heoc81c925a2013-12-06 15:11:56 -05005382 lockdep_assert_held(&cgroup_mutex);
5383
Tejun Heo1fed1b22014-05-16 13:22:49 -04005384 css = ss->css_alloc(parent_css);
Tejun Heoe7e15b82016-06-21 13:06:24 -04005385 if (!css)
5386 css = ERR_PTR(-ENOMEM);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005387 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005388 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005389
Tejun Heoddfcada2014-05-04 15:09:14 -04005390 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04005391
Tejun Heo2aad2a82014-09-24 13:31:50 -04005392 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005393 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08005394 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005395
Vladimir Davydovcf780b72015-08-03 15:32:26 +03005396 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04005397 if (err < 0)
Wenwei Taob00c52d2016-05-13 22:59:20 +08005398 goto err_free_css;
Tejun Heo15a4c832014-05-04 15:09:14 -04005399 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005400
Tejun Heo15a4c832014-05-04 15:09:14 -04005401 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04005402 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04005403 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005404
5405 err = online_css(css);
5406 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04005407 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04005408
Tejun Heoc81c925a2013-12-06 15:11:56 -05005409 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04005410 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04005411 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04005412 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005413 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04005414 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05005415 ss->warned_broken_hierarchy = true;
5416 }
5417
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005418 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005419
Tejun Heo1fed1b22014-05-16 13:22:49 -04005420err_list_del:
5421 list_del_rcu(&css->sibling);
Li Zefan3eb59ec2014-03-18 17:02:36 +08005422err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04005423 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005424 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005425}
5426
Tejun Heo1d887912017-01-26 16:47:28 -05005427/*
5428 * The returned cgroup is fully initialized including its control mask, but
5429 * it isn't associated with its kernfs_node and doesn't have the control
5430 * mask applied.
5431 */
Tejun Heoa5bca212016-03-03 09:57:58 -05005432static struct cgroup *cgroup_create(struct cgroup *parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005433{
Tejun Heoa5bca212016-03-03 09:57:58 -05005434 struct cgroup_root *root = parent->root;
Tejun Heoa5bca212016-03-03 09:57:58 -05005435 struct cgroup *cgrp, *tcgrp;
5436 int level = parent->level + 1;
Tejun Heo03970d32016-03-03 09:58:00 -05005437 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005438
Tejun Heo0a950f62012-11-19 09:02:12 -08005439 /* allocate the cgroup and its ID, 0 is reserved for the root */
Tejun Heob11cfb52015-11-20 15:55:52 -05005440 cgrp = kzalloc(sizeof(*cgrp) +
5441 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05005442 if (!cgrp)
5443 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08005444
Tejun Heo2aad2a82014-09-24 13:31:50 -04005445 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04005446 if (ret)
5447 goto out_free_cgrp;
5448
Li Zefan0ab02ca2014-02-11 16:05:46 +08005449 /*
5450 * Temporarily set the pointer to NULL, so idr_find() won't return
5451 * a half-baked cgroup.
5452 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03005453 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08005454 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04005455 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04005456 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08005457 }
5458
Paul Menagecc31edc2008-10-18 20:28:04 -07005459 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005460
Tejun Heo9d800df2014-05-14 09:15:00 -04005461 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04005462 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05005463 cgrp->level = level;
Alexei Starovoitov148f1112017-10-02 22:50:21 -07005464 ret = cgroup_bpf_inherit(cgrp);
5465 if (ret)
5466 goto out_idr_free;
Tejun Heob11cfb52015-11-20 15:55:52 -05005467
5468 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
5469 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005470
Li Zefanb6abdb02008-03-04 14:28:19 -08005471 if (notify_on_release(parent))
5472 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
5473
Tejun Heo2260e7f2012-11-19 08:13:38 -08005474 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
5475 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005476
Tejun Heo0cb51d72014-05-16 13:22:49 -04005477 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09005478
Tejun Heo4e139af2012-11-19 08:13:36 -08005479 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04005480 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05005481 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05005482 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08005483
Tejun Heo0d802552013-12-06 15:11:56 -05005484 /*
5485 * @cgrp is now fully operational. If something fails after this
5486 * point, it'll be released via the normal destruction path.
5487 */
Tejun Heo6fa49182014-05-04 15:09:13 -04005488 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee82013-07-31 09:50:50 +08005489
Tejun Heobd53d612014-04-23 11:13:16 -04005490 /*
5491 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04005492 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04005493 */
Tejun Heo03970d32016-03-03 09:58:00 -05005494 if (!cgroup_on_dfl(cgrp))
Tejun Heo5531dc92016-03-03 09:57:58 -05005495 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo03970d32016-03-03 09:58:00 -05005496
Johannes Weinere868a992018-10-26 15:06:31 -07005497 if (cgroup_on_dfl(cgrp)) {
5498 ret = psi_cgroup_alloc(cgrp);
5499 if (ret)
5500 goto out_idr_free;
5501 }
5502
Tejun Heo03970d32016-03-03 09:58:00 -05005503 cgroup_propagate_control(cgrp);
5504
Tejun Heoa5bca212016-03-03 09:57:58 -05005505 return cgrp;
5506
Johannes Weinere868a992018-10-26 15:06:31 -07005507out_idr_free:
5508 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoa5bca212016-03-03 09:57:58 -05005509out_cancel_ref:
5510 percpu_ref_exit(&cgrp->self.refcnt);
5511out_free_cgrp:
5512 kfree(cgrp);
5513 return ERR_PTR(ret);
Tejun Heoa5bca212016-03-03 09:57:58 -05005514}
5515
5516static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
5517 umode_t mode)
5518{
5519 struct cgroup *parent, *cgrp;
Tejun Heoa5bca212016-03-03 09:57:58 -05005520 struct kernfs_node *kn;
Tejun Heo03970d32016-03-03 09:58:00 -05005521 int ret;
Tejun Heoa5bca212016-03-03 09:57:58 -05005522
5523 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5524 if (strchr(name, '\n'))
5525 return -EINVAL;
5526
Tejun Heo945ba192016-03-03 09:58:00 -05005527 parent = cgroup_kn_lock_live(parent_kn, false);
Tejun Heoa5bca212016-03-03 09:57:58 -05005528 if (!parent)
5529 return -ENODEV;
5530
5531 cgrp = cgroup_create(parent);
5532 if (IS_ERR(cgrp)) {
5533 ret = PTR_ERR(cgrp);
5534 goto out_unlock;
5535 }
5536
Tejun Heo195e9b62016-03-03 09:57:58 -05005537 /* create the directory */
5538 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5539 if (IS_ERR(kn)) {
5540 ret = PTR_ERR(kn);
5541 goto out_destroy;
5542 }
5543 cgrp->kn = kn;
5544
5545 /*
5546 * This extra ref will be put in cgroup_free_fn() and guarantees
5547 * that @cgrp->kn is always accessible.
5548 */
5549 kernfs_get(kn);
5550
5551 ret = cgroup_kn_set_ugid(kn);
5552 if (ret)
5553 goto out_destroy;
5554
Tejun Heo334c3672016-03-03 09:58:01 -05005555 ret = css_populate_dir(&cgrp->self);
Tejun Heo195e9b62016-03-03 09:57:58 -05005556 if (ret)
5557 goto out_destroy;
5558
Tejun Heo03970d32016-03-03 09:58:00 -05005559 ret = cgroup_apply_control_enable(cgrp);
5560 if (ret)
5561 goto out_destroy;
Tejun Heo195e9b62016-03-03 09:57:58 -05005562
Tejun Heoed1777d2016-08-10 11:23:44 -04005563 trace_cgroup_mkdir(cgrp);
5564
Tejun Heo195e9b62016-03-03 09:57:58 -05005565 /* let's create and online css's */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005566 kernfs_activate(kn);
5567
Tejun Heoba0f4d72014-05-13 12:19:22 -04005568 ret = 0;
5569 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005570
Tejun Heoa5bca212016-03-03 09:57:58 -05005571out_destroy:
5572 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005573out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005574 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005575 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005576}
5577
Tejun Heo223dbc32013-08-13 20:22:50 -04005578/*
5579 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005580 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5581 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005582 */
5583static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005584{
Tejun Heo223dbc32013-08-13 20:22:50 -04005585 struct cgroup_subsys_state *css =
5586 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005587
Tejun Heof20104d2013-08-13 20:22:50 -04005588 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005589
Tejun Heoaa226ff2016-01-21 15:31:11 -05005590 do {
5591 offline_css(css);
5592 css_put(css);
5593 /* @css can't go away while we're holding cgroup_mutex */
5594 css = css->parent;
5595 } while (css && atomic_dec_and_test(&css->online_cnt));
5596
5597 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005598}
5599
Tejun Heo223dbc32013-08-13 20:22:50 -04005600/* css kill confirmation processing requires process context, bounce */
5601static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005602{
5603 struct cgroup_subsys_state *css =
5604 container_of(ref, struct cgroup_subsys_state, refcnt);
5605
Tejun Heoaa226ff2016-01-21 15:31:11 -05005606 if (atomic_dec_and_test(&css->online_cnt)) {
5607 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5608 queue_work(cgroup_destroy_wq, &css->destroy_work);
5609 }
Tejun Heod3daf282013-06-13 19:39:16 -07005610}
5611
Tejun Heof392e512014-04-23 11:13:14 -04005612/**
5613 * kill_css - destroy a css
5614 * @css: css to destroy
5615 *
5616 * This function initiates destruction of @css by removing cgroup interface
5617 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005618 * asynchronously once css_tryget_online() is guaranteed to fail and when
5619 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005620 */
5621static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005622{
Tejun Heo01f64742014-05-13 12:19:23 -04005623 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005624
Waiman Longdff4c8b2017-05-15 09:34:06 -04005625 if (css->flags & CSS_DYING)
5626 return;
5627
5628 css->flags |= CSS_DYING;
5629
Tejun Heo2bd59d42014-02-11 11:52:49 -05005630 /*
5631 * This must happen before css is disassociated with its cgroup.
5632 * See seq_css() for details.
5633 */
Tejun Heo334c3672016-03-03 09:58:01 -05005634 css_clear_dir(css);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005635
Tejun Heoedae0c32013-08-13 20:22:51 -04005636 /*
5637 * Killing would put the base ref, but we need to keep it alive
5638 * until after ->css_offline().
5639 */
5640 css_get(css);
5641
5642 /*
5643 * cgroup core guarantees that, by the time ->css_offline() is
5644 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005645 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005646 * proceed to offlining css's because percpu_ref_kill() doesn't
5647 * guarantee that the ref is seen as killed on all CPUs on return.
5648 *
5649 * Use percpu_ref_kill_and_confirm() to get notifications as each
5650 * css is confirmed to be seen as killed on all CPUs.
5651 */
5652 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005653}
5654
5655/**
5656 * cgroup_destroy_locked - the first stage of cgroup destruction
5657 * @cgrp: cgroup to be destroyed
5658 *
5659 * css's make use of percpu refcnts whose killing latency shouldn't be
5660 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005661 * guarantee that css_tryget_online() won't succeed by the time
5662 * ->css_offline() is invoked. To satisfy all the requirements,
5663 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005664 *
5665 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5666 * userland visible parts and start killing the percpu refcnts of
5667 * css's. Set up so that the next stage will be kicked off once all
5668 * the percpu refcnts are confirmed to be killed.
5669 *
5670 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5671 * rest of destruction. Once all cgroup references are gone, the
5672 * cgroup is RCU-freed.
5673 *
5674 * This function implements s1. After this step, @cgrp is gone as far as
5675 * the userland is concerned and a new cgroup with the same name may be
5676 * created. As cgroup doesn't care about the names internally, this
5677 * doesn't cause any problem.
5678 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005679static int cgroup_destroy_locked(struct cgroup *cgrp)
5680 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005681{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005682 struct cgroup_subsys_state *css;
Tejun Heo2b021cb2016-03-15 20:43:04 -04005683 struct cgrp_cset_link *link;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005684 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005685
Tejun Heo42809dd2012-11-19 08:13:37 -08005686 lockdep_assert_held(&cgroup_mutex);
5687
Tejun Heo91486f62015-10-15 16:41:51 -04005688 /*
5689 * Only migration can raise populated from zero and we're already
5690 * holding cgroup_mutex.
5691 */
5692 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005693 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005694
Tejun Heo1a90dd52012-11-05 09:16:59 -08005695 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005696 * Make sure there's no live children. We can't test emptiness of
5697 * ->self.children as dead children linger on it while being
5698 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005699 */
Tejun Heof3d46502014-05-16 13:22:52 -04005700 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005701 return -EBUSY;
5702
5703 /*
Tejun Heo2b021cb2016-03-15 20:43:04 -04005704 * Mark @cgrp and the associated csets dead. The former prevents
5705 * further task migration and child creation by disabling
5706 * cgroup_lock_live_group(). The latter makes the csets ignored by
5707 * the migration path.
Tejun Heo455050d2013-06-13 19:27:41 -07005708 */
Tejun Heo184faf32014-05-16 13:22:51 -04005709 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005710
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005711 spin_lock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005712 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5713 link->cset->dead = true;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005714 spin_unlock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005715
Tejun Heo249f3462014-05-14 09:15:01 -04005716 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005717 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005718 kill_css(css);
5719
Tejun Heo455050d2013-06-13 19:27:41 -07005720 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005721 * Remove @cgrp directory along with the base files. @cgrp has an
5722 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005723 */
Tejun Heo01f64742014-05-13 12:19:23 -04005724 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005725
Tejun Heod51f39b2014-05-16 13:22:48 -04005726 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005727
Tejun Heo249f3462014-05-14 09:15:01 -04005728 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005729 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005730
Tejun Heoea15f8c2013-06-13 19:27:42 -07005731 return 0;
5732};
5733
Tejun Heo2bd59d42014-02-11 11:52:49 -05005734static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005735{
Tejun Heoa9746d82014-05-13 12:19:22 -04005736 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005737 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005738
Tejun Heo945ba192016-03-03 09:58:00 -05005739 cgrp = cgroup_kn_lock_live(kn, false);
Tejun Heoa9746d82014-05-13 12:19:22 -04005740 if (!cgrp)
5741 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005742
Tejun Heoa9746d82014-05-13 12:19:22 -04005743 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005744
Tejun Heoed1777d2016-08-10 11:23:44 -04005745 if (!ret)
5746 trace_cgroup_rmdir(cgrp);
5747
Tejun Heoa9746d82014-05-13 12:19:22 -04005748 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005749 return ret;
5750}
5751
Tejun Heo2bd59d42014-02-11 11:52:49 -05005752static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5753 .remount_fs = cgroup_remount,
5754 .show_options = cgroup_show_options,
5755 .mkdir = cgroup_mkdir,
5756 .rmdir = cgroup_rmdir,
5757 .rename = cgroup_rename,
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05005758 .show_path = cgroup_show_path,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005759};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005760
Tejun Heo15a4c832014-05-04 15:09:14 -04005761static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005762{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005763 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005764
Tejun Heoa5ae9892015-12-29 14:53:56 -05005765 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005766
Tejun Heo648bb562012-11-19 08:13:36 -08005767 mutex_lock(&cgroup_mutex);
5768
Tejun Heo15a4c832014-05-04 15:09:14 -04005769 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005770 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005771
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005772 /* Create the root cgroup state for this subsystem */
5773 ss->root = &cgrp_dfl_root;
5774 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005775 /* We don't handle early failures gracefully */
5776 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005777 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005778
5779 /*
5780 * Root csses are never destroyed and we can't initialize
5781 * percpu_ref during early init. Disable refcnting.
5782 */
5783 css->flags |= CSS_NO_REF;
5784
Tejun Heo15a4c832014-05-04 15:09:14 -04005785 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005786 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005787 css->id = 1;
5788 } else {
5789 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5790 BUG_ON(css->id < 0);
5791 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005792
Li Zefane8d55fd2008-04-29 01:00:13 -07005793 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005794 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005795 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005796 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005797 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005798
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005799 have_fork_callback |= (bool)ss->fork << ss->id;
5800 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005801 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005802 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005803
Li Zefane8d55fd2008-04-29 01:00:13 -07005804 /* At system boot, before all subsystems have been
5805 * registered, no tasks have been forked, so we don't
5806 * need to invoke fork callbacks here. */
5807 BUG_ON(!list_empty(&init_task.tasks));
5808
Tejun Heoae7f1642013-08-13 20:22:50 -04005809 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005810
Tejun Heo648bb562012-11-19 08:13:36 -08005811 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005812}
5813
5814/**
Li Zefana043e3b2008-02-23 15:24:09 -08005815 * cgroup_init_early - cgroup initialization at system boot
5816 *
5817 * Initialize cgroups at system boot, and initialize any
5818 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005819 */
5820int __init cgroup_init_early(void)
5821{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005822 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005823 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005824 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005825
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005826 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005827 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5828
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005829 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005830
Tejun Heo3ed80a62014-02-08 10:36:58 -05005831 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005832 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005833 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005834 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005835 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005836 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5837 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005838
Tejun Heoaec25022014-02-08 10:36:58 -05005839 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005840 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005841 if (!ss->legacy_name)
5842 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005843
5844 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005845 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005846 }
5847 return 0;
5848}
5849
5850/**
Li Zefana043e3b2008-02-23 15:24:09 -08005851 * cgroup_init - cgroup initialization
5852 *
5853 * Register cgroup filesystem and /proc file, and initialize
5854 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005855 */
5856int __init cgroup_init(void)
5857{
Tejun Heo30159ec2013-06-25 11:53:37 -07005858 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005859 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005860
Tejun Heo6e5c8302016-02-22 22:25:47 -05005861 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005862 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005863 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5864 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005865
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005866 /*
5867 * The latency of the synchronize_sched() is too high for cgroups,
5868 * avoid it at the cost of forcing all readers into the slow path.
5869 */
5870 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5871
Aditya Kalia79a9082016-01-29 02:54:06 -06005872 get_user_ns(init_cgroup_ns.user_ns);
5873
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005874 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005875
Tejun Heo2378d8b2016-03-03 09:57:57 -05005876 /*
5877 * Add init_css_set to the hash table so that dfl_root can link to
5878 * it during init.
5879 */
5880 hash_add(css_set_table, &init_css_set.hlist,
5881 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005882
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005883 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005884
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005885 mutex_unlock(&cgroup_mutex);
5886
Tejun Heo172a2c062014-03-19 10:23:53 -04005887 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005888 if (ss->early_init) {
5889 struct cgroup_subsys_state *css =
5890 init_css_set.subsys[ss->id];
5891
5892 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5893 GFP_KERNEL);
5894 BUG_ON(css->id < 0);
5895 } else {
5896 cgroup_init_subsys(ss, false);
5897 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005898
Tejun Heo2d8f2432014-04-23 11:13:15 -04005899 list_add_tail(&init_css_set.e_cset_node[ssid],
5900 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005901
5902 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005903 * Setting dfl_root subsys_mask needs to consider the
5904 * disabled flag and cftype registration needs kmalloc,
5905 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005906 */
Shakeel Butt3fd1a1a2021-05-12 13:19:46 -07005907 if (!cgroup_ssid_enabled(ssid))
Tejun Heoa8ddc822014-07-15 11:05:10 -04005908 continue;
5909
Johannes Weiner223ffb22016-02-11 13:34:49 -05005910 if (cgroup_ssid_no_v1(ssid))
5911 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5912 ss->name);
5913
Tejun Heoa8ddc822014-07-15 11:05:10 -04005914 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5915
Tejun Heof6d635ad2016-03-08 11:51:26 -05005916 if (ss->implicit_on_dfl)
5917 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5918 else if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005919 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005920
Tejun Heoa8ddc822014-07-15 11:05:10 -04005921 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5922 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5923 } else {
5924 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5925 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005926 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005927
5928 if (ss->bind)
5929 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo4a99eac2017-07-18 17:57:46 -04005930
5931 mutex_lock(&cgroup_mutex);
5932 css_populate_dir(init_css_set.subsys[ssid]);
5933 mutex_unlock(&cgroup_mutex);
Tejun Heo172a2c062014-03-19 10:23:53 -04005934 }
Greg KH676db4a2010-08-05 13:53:35 -07005935
Tejun Heo2378d8b2016-03-03 09:57:57 -05005936 /* init_css_set.subsys[] has been updated, re-hash */
5937 hash_del(&init_css_set.hlist);
5938 hash_add(css_set_table, &init_css_set.hlist,
5939 css_set_hash(init_css_set.subsys));
5940
Tejun Heo035f4f52015-10-15 17:00:43 -04005941 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5942 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005943 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005944 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005945
Tejun Heo2bd59d42014-02-11 11:52:49 -05005946 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005947}
Paul Menageb4f48b62007-10-18 23:39:33 -07005948
Tejun Heoe5fca242013-11-22 17:14:39 -05005949static int __init cgroup_wq_init(void)
5950{
5951 /*
5952 * There isn't much point in executing destruction path in
5953 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005954 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005955 *
5956 * We would prefer to do this in cgroup_init() above, but that
5957 * is called before init_workqueues(): so leave this until after.
5958 */
Tejun Heo1a115332014-02-12 19:06:19 -05005959 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005960 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005961
5962 /*
5963 * Used to destroy pidlists and separate to serve as flush domain.
5964 * Cap @max_active to 1 too.
5965 */
5966 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5967 0, 1);
5968 BUG_ON(!cgroup_pidlist_destroy_wq);
5969
Tejun Heoe5fca242013-11-22 17:14:39 -05005970 return 0;
5971}
5972core_initcall(cgroup_wq_init);
5973
Paul Menagea4243162007-10-18 23:39:35 -07005974/*
5975 * proc_cgroup_show()
5976 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5977 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005978 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005979int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5980 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005981{
Tejun Heo4c737b42016-08-10 11:23:44 -04005982 char *buf;
Paul Menagea4243162007-10-18 23:39:35 -07005983 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005984 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005985
5986 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005987 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005988 if (!buf)
5989 goto out;
5990
Paul Menagea4243162007-10-18 23:39:35 -07005991 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005992 spin_lock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005993
Tejun Heo985ed672014-03-19 10:23:53 -04005994 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005995 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005996 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005997 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005998
Tejun Heoa7165262016-02-23 10:00:50 -05005999 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04006000 continue;
6001
Paul Menage2c6ab6d2009-09-23 15:56:23 -07006002 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07006003 if (root != &cgrp_dfl_root)
6004 for_each_subsys(ss, ssid)
6005 if (root->subsys_mask & (1 << ssid))
6006 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07006007 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07006008 if (strlen(root->name))
6009 seq_printf(m, "%sname=%s", count ? "," : "",
6010 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07006011 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04006012
Paul Menage7717f7b2009-09-23 15:56:22 -07006013 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04006014
6015 /*
6016 * On traditional hierarchies, all zombie tasks show up as
6017 * belonging to the root cgroup. On the default hierarchy,
6018 * while a zombie doesn't show up in "cgroup.procs" and
6019 * thus can't be migrated, its /proc/PID/cgroup keeps
6020 * reporting the cgroup it belonged to before exiting. If
6021 * the cgroup is removed before the zombie is reaped,
6022 * " (deleted)" is appended to the cgroup path.
6023 */
6024 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
Tejun Heo4c737b42016-08-10 11:23:44 -04006025 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
Aditya Kalia79a9082016-01-29 02:54:06 -06006026 current->nsproxy->cgroup_ns);
Tejun Heoe0223002016-09-29 15:49:40 +02006027 if (retval >= PATH_MAX)
Tejun Heo2e91fa72015-10-15 16:41:53 -04006028 retval = -ENAMETOOLONG;
Tejun Heoe0223002016-09-29 15:49:40 +02006029 if (retval < 0)
Tejun Heo2e91fa72015-10-15 16:41:53 -04006030 goto out_unlock;
Tejun Heo2e91fa72015-10-15 16:41:53 -04006031
Tejun Heo4c737b42016-08-10 11:23:44 -04006032 seq_puts(m, buf);
6033 } else {
6034 seq_puts(m, "/");
6035 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04006036
6037 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
6038 seq_puts(m, " (deleted)\n");
6039 else
6040 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07006041 }
6042
Zefan Li006f4ac2014-09-18 16:03:15 +08006043 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07006044out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006045 spin_unlock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07006046 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07006047 kfree(buf);
6048out:
6049 return retval;
6050}
6051
Paul Menagea4243162007-10-18 23:39:35 -07006052/* Display information about each subsystem and each hierarchy */
6053static int proc_cgroupstats_show(struct seq_file *m, void *v)
6054{
Tejun Heo30159ec2013-06-25 11:53:37 -07006055 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07006056 int i;
Paul Menagea4243162007-10-18 23:39:35 -07006057
Paul Menage8bab8dd2008-04-04 14:29:57 -07006058 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08006059 /*
6060 * ideally we don't want subsystems moving around while we do this.
6061 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
6062 * subsys/hierarchy state.
6063 */
Paul Menagea4243162007-10-18 23:39:35 -07006064 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07006065
6066 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07006067 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07006068 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04006069 atomic_read(&ss->root->nr_cgrps),
6070 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07006071
Paul Menagea4243162007-10-18 23:39:35 -07006072 mutex_unlock(&cgroup_mutex);
6073 return 0;
6074}
6075
6076static int cgroupstats_open(struct inode *inode, struct file *file)
6077{
Al Viro9dce07f2008-03-29 03:07:28 +00006078 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07006079}
6080
Alexey Dobriyan828c0952009-10-01 15:43:56 -07006081static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07006082 .open = cgroupstats_open,
6083 .read = seq_read,
6084 .llseek = seq_lseek,
6085 .release = single_release,
6086};
6087
Paul Menageb4f48b62007-10-18 23:39:33 -07006088/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05006089 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08006090 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07006091 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05006092 * A task is associated with the init_css_set until cgroup_post_fork()
6093 * attaches it to the parent's css_set. Empty cg_list indicates that
6094 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07006095 */
6096void cgroup_fork(struct task_struct *child)
6097{
Tejun Heoeaf797a2014-02-25 10:04:03 -05006098 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07006099 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07006100}
6101
6102/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10006103 * cgroup_can_fork - called on a new task before the process is exposed
6104 * @child: the task in question.
6105 *
6106 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
6107 * returns an error, the fork aborts with that error code. This allows for
6108 * a cgroup subsystem to conditionally allow or deny new forks.
6109 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006110int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10006111{
6112 struct cgroup_subsys *ss;
6113 int i, j, ret;
6114
Tejun Heob4e0eea2016-02-22 22:25:46 -05006115 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006116 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10006117 if (ret)
6118 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05006119 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10006120
6121 return 0;
6122
6123out_revert:
6124 for_each_subsys(ss, j) {
6125 if (j >= i)
6126 break;
6127 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006128 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10006129 }
6130
6131 return ret;
6132}
6133
6134/**
6135 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
6136 * @child: the task in question
6137 *
6138 * This calls the cancel_fork() callbacks if a fork failed *after*
6139 * cgroup_can_fork() succeded.
6140 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006141void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10006142{
6143 struct cgroup_subsys *ss;
6144 int i;
6145
6146 for_each_subsys(ss, i)
6147 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006148 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10006149}
6150
6151/**
Li Zefana043e3b2008-02-23 15:24:09 -08006152 * cgroup_post_fork - called on a new task after adding it to the task list
6153 * @child: the task in question
6154 *
Tejun Heo5edee612012-10-16 15:03:14 -07006155 * Adds the task to the list running through its css_set if necessary and
6156 * call the subsystem fork() callbacks. Has to be after the task is
6157 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04006158 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07006159 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08006160 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006161void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07006162{
Tejun Heo30159ec2013-06-25 11:53:37 -07006163 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07006164 int i;
6165
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01006166 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08006167 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05006168 * function sets use_task_css_set_links before grabbing
6169 * tasklist_lock and we just went through tasklist_lock to add
6170 * @child, it's guaranteed that either we see the set
6171 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
6172 * @child during its iteration.
6173 *
6174 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006175 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05006176 * association is stable, and, on completion of the parent's
6177 * migration, @child is visible in the source of migration or
6178 * already in the destination cgroup. This guarantee is necessary
6179 * when implementing operations which need to migrate all tasks of
6180 * a cgroup to another.
6181 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08006182 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05006183 * will remain in init_css_set. This is safe because all tasks are
6184 * in the init_css_set before cg_links is enabled and there's no
6185 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01006186 */
Paul Menage817929e2007-10-18 23:39:36 -07006187 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05006188 struct css_set *cset;
6189
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006190 spin_lock_irq(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05006191 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05006192 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05006193 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04006194 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05006195 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006196 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07006197 }
Tejun Heo5edee612012-10-16 15:03:14 -07006198
6199 /*
6200 * Call ss->fork(). This must happen after @child is linked on
6201 * css_set; otherwise, @child might change state between ->fork()
6202 * and addition to css_set.
6203 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05006204 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006205 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05006206 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07006207}
Tejun Heo5edee612012-10-16 15:03:14 -07006208
Paul Menage817929e2007-10-18 23:39:36 -07006209/**
Paul Menageb4f48b62007-10-18 23:39:33 -07006210 * cgroup_exit - detach cgroup from exiting task
6211 * @tsk: pointer to task_struct of exiting process
6212 *
6213 * Description: Detach cgroup from @tsk and release it.
6214 *
6215 * Note that cgroups marked notify_on_release force every task in
6216 * them to take the global cgroup_mutex mutex when exiting.
6217 * This could impact scaling on very large systems. Be reluctant to
6218 * use notify_on_release cgroups where very high task exit scaling
6219 * is required on large systems.
6220 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05006221 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
6222 * call cgroup_exit() while the task is still competent to handle
6223 * notify_on_release(), then leave the task attached to the root cgroup in
6224 * each hierarchy for the remainder of its exit. No need to bother with
6225 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08006226 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07006227 */
Li Zefan1ec41832014-03-28 15:22:19 +08006228void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07006229{
Tejun Heo30159ec2013-06-25 11:53:37 -07006230 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07006231 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01006232 int i;
Paul Menage817929e2007-10-18 23:39:36 -07006233
6234 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05006235 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04006236 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07006237 */
Tejun Heo0de09422015-10-15 16:41:49 -04006238 cset = task_css_set(tsk);
6239
Paul Menage817929e2007-10-18 23:39:36 -07006240 if (!list_empty(&tsk->cg_list)) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006241 spin_lock_irq(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04006242 css_set_move_task(tsk, cset, NULL, false);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006243 spin_unlock_irq(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04006244 } else {
6245 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07006246 }
6247
Aleksa Saraicb4a3162015-06-06 10:02:14 +10006248 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05006249 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04006250 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05006251 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04006252}
Tejun Heo30159ec2013-06-25 11:53:37 -07006253
Tejun Heo2e91fa72015-10-15 16:41:53 -04006254void cgroup_free(struct task_struct *task)
6255{
6256 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04006257 struct cgroup_subsys *ss;
6258 int ssid;
6259
Tejun Heob4e0eea2016-02-22 22:25:46 -05006260 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04006261 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05006262 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01006263
Tejun Heo2e91fa72015-10-15 16:41:53 -04006264 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07006265}
Paul Menage697f4162007-10-18 23:39:34 -07006266
Paul Menagebd89aab2007-10-18 23:40:44 -07006267static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07006268{
Tejun Heo27bd4db2015-10-15 16:41:50 -04006269 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08006270 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
6271 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07006272}
6273
Paul Menage81a6a5c2007-10-18 23:39:38 -07006274/*
6275 * Notify userspace when a cgroup is released, by running the
6276 * configured release agent with the name of the cgroup (path
6277 * relative to the root of cgroup file system) as the argument.
6278 *
6279 * Most likely, this user command will try to rmdir this cgroup.
6280 *
6281 * This races with the possibility that some other task will be
6282 * attached to this cgroup before it is removed, or that some other
6283 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
6284 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
6285 * unused, and this cgroup will be reprieved from its death sentence,
6286 * to continue to serve a useful existence. Next time it's released,
6287 * we will get notified again, if it still has 'notify_on_release' set.
6288 *
6289 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
6290 * means only wait until the task is successfully execve()'d. The
6291 * separate release agent task is forked by call_usermodehelper(),
6292 * then control in this thread returns here, without waiting for the
6293 * release agent task. We don't bother to wait because the caller of
6294 * this routine has no use for the exit status of the release agent
6295 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07006296 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07006297static void cgroup_release_agent(struct work_struct *work)
6298{
Zefan Li971ff492014-09-18 16:06:19 +08006299 struct cgroup *cgrp =
6300 container_of(work, struct cgroup, release_agent_work);
Tejun Heo4c737b42016-08-10 11:23:44 -04006301 char *pathbuf = NULL, *agentbuf = NULL;
Zefan Li971ff492014-09-18 16:06:19 +08006302 char *argv[3], *envp[3];
Tejun Heo4c737b42016-08-10 11:23:44 -04006303 int ret;
Zefan Li971ff492014-09-18 16:06:19 +08006304
Paul Menage81a6a5c2007-10-18 23:39:38 -07006305 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07006306
Zefan Li971ff492014-09-18 16:06:19 +08006307 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
6308 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
6309 if (!pathbuf || !agentbuf)
6310 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07006311
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006312 spin_lock_irq(&css_set_lock);
Tejun Heo4c737b42016-08-10 11:23:44 -04006313 ret = cgroup_path_ns_locked(cgrp, pathbuf, PATH_MAX, &init_cgroup_ns);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006314 spin_unlock_irq(&css_set_lock);
Tejun Heoe0223002016-09-29 15:49:40 +02006315 if (ret < 0 || ret >= PATH_MAX)
Zefan Li971ff492014-09-18 16:06:19 +08006316 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07006317
Zefan Li971ff492014-09-18 16:06:19 +08006318 argv[0] = agentbuf;
Tejun Heo4c737b42016-08-10 11:23:44 -04006319 argv[1] = pathbuf;
Zefan Li971ff492014-09-18 16:06:19 +08006320 argv[2] = NULL;
6321
6322 /* minimal command environment */
6323 envp[0] = "HOME=/";
6324 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
6325 envp[2] = NULL;
6326
Paul Menage81a6a5c2007-10-18 23:39:38 -07006327 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08006328 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08006329 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08006330out:
Zefan Li3e2cd912014-09-20 14:35:43 +08006331 mutex_unlock(&cgroup_mutex);
6332out_free:
Zefan Li971ff492014-09-18 16:06:19 +08006333 kfree(agentbuf);
6334 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07006335}
Paul Menage8bab8dd2008-04-04 14:29:57 -07006336
6337static int __init cgroup_disable(char *str)
6338{
Tejun Heo30159ec2013-06-25 11:53:37 -07006339 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07006340 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07006341 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07006342
6343 while ((token = strsep(&str, ",")) != NULL) {
6344 if (!*token)
6345 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07006346
Tejun Heo3ed80a62014-02-08 10:36:58 -05006347 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07006348 if (strcmp(token, ss->name) &&
6349 strcmp(token, ss->legacy_name))
6350 continue;
Shakeel Butt3fd1a1a2021-05-12 13:19:46 -07006351
6352 static_branch_disable(cgroup_subsys_enabled_key[i]);
6353 pr_info("Disabling %s control group subsystem\n",
6354 ss->name);
Paul Menage8bab8dd2008-04-04 14:29:57 -07006355 }
6356 }
6357 return 1;
6358}
6359__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07006360
Johannes Weiner223ffb22016-02-11 13:34:49 -05006361static int __init cgroup_no_v1(char *str)
6362{
6363 struct cgroup_subsys *ss;
6364 char *token;
6365 int i;
6366
6367 while ((token = strsep(&str, ",")) != NULL) {
6368 if (!*token)
6369 continue;
6370
6371 if (!strcmp(token, "all")) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05006372 cgroup_no_v1_mask = U16_MAX;
Johannes Weiner223ffb22016-02-11 13:34:49 -05006373 break;
6374 }
6375
6376 for_each_subsys(ss, i) {
6377 if (strcmp(token, ss->name) &&
6378 strcmp(token, ss->legacy_name))
6379 continue;
6380
6381 cgroup_no_v1_mask |= 1 << i;
6382 }
6383 }
6384 return 1;
6385}
6386__setup("cgroup_no_v1=", cgroup_no_v1);
6387
Tejun Heob77d7b62013-08-13 11:01:54 -04006388/**
Tejun Heoec903c02014-05-13 12:11:01 -04006389 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04006390 * @dentry: directory dentry of interest
6391 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04006392 *
Tejun Heo5a17f542014-02-11 11:52:47 -05006393 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
6394 * to get the corresponding css and return it. If such css doesn't exist
6395 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02006396 */
Tejun Heoec903c02014-05-13 12:11:01 -04006397struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
6398 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006399{
Tejun Heo2bd59d42014-02-11 11:52:49 -05006400 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05006401 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05006402 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006403 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006404
Tejun Heo35cf0832013-08-26 18:40:56 -04006405 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05006406 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
6407 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006408 return ERR_PTR(-EBADF);
6409
Tejun Heo5a17f542014-02-11 11:52:47 -05006410 rcu_read_lock();
6411
Tejun Heo2bd59d42014-02-11 11:52:49 -05006412 /*
6413 * This path doesn't originate from kernfs and @kn could already
6414 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08006415 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05006416 */
6417 cgrp = rcu_dereference(kn->priv);
6418 if (cgrp)
6419 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05006420
Tejun Heoec903c02014-05-13 12:11:01 -04006421 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05006422 css = ERR_PTR(-ENOENT);
6423
6424 rcu_read_unlock();
6425 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006426}
Stephane Eraniane5d13672011-02-14 11:20:01 +02006427
Li Zefan1cb650b2013-08-19 10:05:24 +08006428/**
6429 * css_from_id - lookup css by id
6430 * @id: the cgroup id
6431 * @ss: cgroup subsys to be looked into
6432 *
6433 * Returns the css if there's valid one with @id, otherwise returns NULL.
6434 * Should be called under rcu_read_lock().
6435 */
6436struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
6437{
Tejun Heo6fa49182014-05-04 15:09:13 -04006438 WARN_ON_ONCE(!rcu_read_lock_held());
Johannes Weinerd6ccc552016-06-17 12:24:27 -04006439 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006440}
6441
Tejun Heo16af4392015-11-20 15:55:52 -05006442/**
6443 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
6444 * @path: path on the default hierarchy
6445 *
6446 * Find the cgroup at @path on the default hierarchy, increment its
6447 * reference count and return it. Returns pointer to the found cgroup on
6448 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
6449 * if @path points to a non-directory.
6450 */
6451struct cgroup *cgroup_get_from_path(const char *path)
6452{
6453 struct kernfs_node *kn;
6454 struct cgroup *cgrp;
6455
6456 mutex_lock(&cgroup_mutex);
6457
6458 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
6459 if (kn) {
6460 if (kernfs_type(kn) == KERNFS_DIR) {
6461 cgrp = kn->priv;
6462 cgroup_get(cgrp);
6463 } else {
6464 cgrp = ERR_PTR(-ENOTDIR);
6465 }
6466 kernfs_put(kn);
6467 } else {
6468 cgrp = ERR_PTR(-ENOENT);
6469 }
6470
6471 mutex_unlock(&cgroup_mutex);
6472 return cgrp;
6473}
6474EXPORT_SYMBOL_GPL(cgroup_get_from_path);
6475
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07006476/**
6477 * cgroup_get_from_fd - get a cgroup pointer from a fd
6478 * @fd: fd obtained by open(cgroup2_dir)
6479 *
6480 * Find the cgroup from a fd which should be obtained
6481 * by opening a cgroup directory. Returns a pointer to the
6482 * cgroup on success. ERR_PTR is returned if the cgroup
6483 * cannot be found.
6484 */
6485struct cgroup *cgroup_get_from_fd(int fd)
6486{
6487 struct cgroup_subsys_state *css;
6488 struct cgroup *cgrp;
6489 struct file *f;
6490
6491 f = fget_raw(fd);
6492 if (!f)
6493 return ERR_PTR(-EBADF);
6494
6495 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
6496 fput(f);
6497 if (IS_ERR(css))
6498 return ERR_CAST(css);
6499
6500 cgrp = css->cgroup;
6501 if (!cgroup_on_dfl(cgrp)) {
6502 cgroup_put(cgrp);
6503 return ERR_PTR(-EBADF);
6504 }
6505
6506 return cgrp;
6507}
6508EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
6509
Tejun Heobd1060a2015-12-07 17:38:53 -05006510/*
6511 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
6512 * definition in cgroup-defs.h.
6513 */
6514#ifdef CONFIG_SOCK_CGROUP_DATA
6515
6516#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
6517
Tejun Heo3fa4cc92015-12-14 11:24:06 -05006518DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05006519static bool cgroup_sk_alloc_disabled __read_mostly;
6520
6521void cgroup_sk_alloc_disable(void)
6522{
6523 if (cgroup_sk_alloc_disabled)
6524 return;
6525 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
6526 cgroup_sk_alloc_disabled = true;
6527}
6528
6529#else
6530
6531#define cgroup_sk_alloc_disabled false
6532
6533#endif
6534
6535void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
6536{
Cong Wang51fbad62020-07-02 11:52:56 -07006537 if (cgroup_sk_alloc_disabled) {
6538 skcd->no_refcnt = 1;
Johannes Weinerd979a392016-09-19 14:44:38 -07006539 return;
6540 }
6541
Shakeel Butt529f4b72020-03-09 22:16:05 -07006542 /* Don't associate the sock with unrelated interrupted task's cgroup. */
6543 if (in_interrupt())
6544 return;
6545
Tejun Heobd1060a2015-12-07 17:38:53 -05006546 rcu_read_lock();
6547
6548 while (true) {
6549 struct css_set *cset;
6550
6551 cset = task_css_set(current);
6552 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
6553 skcd->val = (unsigned long)cset->dfl_cgrp;
6554 break;
6555 }
6556 cpu_relax();
6557 }
6558
6559 rcu_read_unlock();
6560}
6561
Cong Wang51fbad62020-07-02 11:52:56 -07006562void cgroup_sk_clone(struct sock_cgroup_data *skcd)
6563{
6564 /* Socket clone path */
6565 if (skcd->val) {
Yang Yingliangf3b1d642020-08-13 20:27:57 +00006566 if (skcd->no_refcnt)
6567 return;
Cong Wang51fbad62020-07-02 11:52:56 -07006568 /*
6569 * We might be cloning a socket which is left in an empty
6570 * cgroup and the cgroup might have already been rmdir'd.
6571 * Don't use cgroup_get_live().
6572 */
6573 cgroup_get(sock_cgroup_ptr(skcd));
6574 }
6575}
6576
Tejun Heobd1060a2015-12-07 17:38:53 -05006577void cgroup_sk_free(struct sock_cgroup_data *skcd)
6578{
Cong Wang51fbad62020-07-02 11:52:56 -07006579 if (skcd->no_refcnt)
6580 return;
6581
Tejun Heobd1060a2015-12-07 17:38:53 -05006582 cgroup_put(sock_cgroup_ptr(skcd));
6583}
6584
6585#endif /* CONFIG_SOCK_CGROUP_DATA */
6586
Aditya Kalia79a9082016-01-29 02:54:06 -06006587/* cgroup namespaces */
6588
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006589static struct ucounts *inc_cgroup_namespaces(struct user_namespace *ns)
6590{
6591 return inc_ucount(ns, current_euid(), UCOUNT_CGROUP_NAMESPACES);
6592}
6593
6594static void dec_cgroup_namespaces(struct ucounts *ucounts)
6595{
6596 dec_ucount(ucounts, UCOUNT_CGROUP_NAMESPACES);
6597}
6598
Aditya Kalia79a9082016-01-29 02:54:06 -06006599static struct cgroup_namespace *alloc_cgroup_ns(void)
6600{
6601 struct cgroup_namespace *new_ns;
6602 int ret;
6603
6604 new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
6605 if (!new_ns)
6606 return ERR_PTR(-ENOMEM);
6607 ret = ns_alloc_inum(&new_ns->ns);
6608 if (ret) {
6609 kfree(new_ns);
6610 return ERR_PTR(ret);
6611 }
6612 atomic_set(&new_ns->count, 1);
6613 new_ns->ns.ops = &cgroupns_operations;
6614 return new_ns;
6615}
6616
6617void free_cgroup_ns(struct cgroup_namespace *ns)
6618{
6619 put_css_set(ns->root_cset);
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006620 dec_cgroup_namespaces(ns->ucounts);
Aditya Kalia79a9082016-01-29 02:54:06 -06006621 put_user_ns(ns->user_ns);
6622 ns_free_inum(&ns->ns);
6623 kfree(ns);
6624}
6625EXPORT_SYMBOL(free_cgroup_ns);
6626
6627struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
6628 struct user_namespace *user_ns,
6629 struct cgroup_namespace *old_ns)
6630{
Tejun Heofa5ff8a2016-02-28 08:59:33 -05006631 struct cgroup_namespace *new_ns;
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006632 struct ucounts *ucounts;
Tejun Heofa5ff8a2016-02-28 08:59:33 -05006633 struct css_set *cset;
Aditya Kalia79a9082016-01-29 02:54:06 -06006634
6635 BUG_ON(!old_ns);
6636
6637 if (!(flags & CLONE_NEWCGROUP)) {
6638 get_cgroup_ns(old_ns);
6639 return old_ns;
6640 }
6641
6642 /* Allow only sysadmin to create cgroup namespace. */
Aditya Kalia79a9082016-01-29 02:54:06 -06006643 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
Tejun Heofa5ff8a2016-02-28 08:59:33 -05006644 return ERR_PTR(-EPERM);
Aditya Kalia79a9082016-01-29 02:54:06 -06006645
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006646 ucounts = inc_cgroup_namespaces(user_ns);
6647 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -05006648 return ERR_PTR(-ENOSPC);
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006649
Eric W. Biederman7bd88302016-07-15 06:35:24 -05006650 /* It is not safe to take cgroup_mutex here */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006651 spin_lock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06006652 cset = task_css_set(current);
6653 get_css_set(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006654 spin_unlock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06006655
Aditya Kalia79a9082016-01-29 02:54:06 -06006656 new_ns = alloc_cgroup_ns();
Tejun Heod2202552016-02-18 11:44:24 -05006657 if (IS_ERR(new_ns)) {
Tejun Heofa5ff8a2016-02-28 08:59:33 -05006658 put_css_set(cset);
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006659 dec_cgroup_namespaces(ucounts);
Tejun Heofa5ff8a2016-02-28 08:59:33 -05006660 return new_ns;
Tejun Heod2202552016-02-18 11:44:24 -05006661 }
Aditya Kalia79a9082016-01-29 02:54:06 -06006662
6663 new_ns->user_ns = get_user_ns(user_ns);
Eric W. Biedermand08311d2016-08-08 14:25:30 -05006664 new_ns->ucounts = ucounts;
Aditya Kalia79a9082016-01-29 02:54:06 -06006665 new_ns->root_cset = cset;
6666
6667 return new_ns;
Aditya Kalia79a9082016-01-29 02:54:06 -06006668}
6669
6670static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
6671{
6672 return container_of(ns, struct cgroup_namespace, ns);
6673}
6674
Aditya Kalia0530e02016-01-29 02:54:07 -06006675static int cgroupns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06006676{
Aditya Kalia0530e02016-01-29 02:54:07 -06006677 struct cgroup_namespace *cgroup_ns = to_cg_ns(ns);
6678
6679 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN) ||
6680 !ns_capable(cgroup_ns->user_ns, CAP_SYS_ADMIN))
6681 return -EPERM;
6682
6683 /* Don't need to do anything if we are attaching to our own cgroupns. */
6684 if (cgroup_ns == nsproxy->cgroup_ns)
6685 return 0;
6686
6687 get_cgroup_ns(cgroup_ns);
6688 put_cgroup_ns(nsproxy->cgroup_ns);
6689 nsproxy->cgroup_ns = cgroup_ns;
6690
6691 return 0;
Aditya Kalia79a9082016-01-29 02:54:06 -06006692}
6693
6694static struct ns_common *cgroupns_get(struct task_struct *task)
6695{
6696 struct cgroup_namespace *ns = NULL;
6697 struct nsproxy *nsproxy;
6698
6699 task_lock(task);
6700 nsproxy = task->nsproxy;
6701 if (nsproxy) {
6702 ns = nsproxy->cgroup_ns;
6703 get_cgroup_ns(ns);
6704 }
6705 task_unlock(task);
6706
6707 return ns ? &ns->ns : NULL;
6708}
6709
6710static void cgroupns_put(struct ns_common *ns)
6711{
6712 put_cgroup_ns(to_cg_ns(ns));
6713}
6714
Andrey Vaginbcac25a2016-09-06 00:47:13 -07006715static struct user_namespace *cgroupns_owner(struct ns_common *ns)
6716{
6717 return to_cg_ns(ns)->user_ns;
6718}
6719
Aditya Kalia79a9082016-01-29 02:54:06 -06006720const struct proc_ns_operations cgroupns_operations = {
6721 .name = "cgroup",
6722 .type = CLONE_NEWCGROUP,
6723 .get = cgroupns_get,
6724 .put = cgroupns_put,
6725 .install = cgroupns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07006726 .owner = cgroupns_owner,
Aditya Kalia79a9082016-01-29 02:54:06 -06006727};
6728
6729static __init int cgroup_namespaces_init(void)
6730{
6731 return 0;
6732}
6733subsys_initcall(cgroup_namespaces_init);
6734
Daniel Mackf791c422016-11-23 16:52:26 +01006735#ifdef CONFIG_CGROUP_BPF
Alexei Starovoitov148f1112017-10-02 22:50:21 -07006736int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
6737 enum bpf_attach_type type, u32 flags)
Daniel Mackf791c422016-11-23 16:52:26 +01006738{
Alexei Starovoitov1ee2b4b2017-02-10 20:28:24 -08006739 int ret;
Daniel Mackf791c422016-11-23 16:52:26 +01006740
6741 mutex_lock(&cgroup_mutex);
Alexei Starovoitov148f1112017-10-02 22:50:21 -07006742 ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
6743 mutex_unlock(&cgroup_mutex);
6744 return ret;
6745}
6746int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
6747 enum bpf_attach_type type, u32 flags)
6748{
6749 int ret;
6750
6751 mutex_lock(&cgroup_mutex);
6752 ret = __cgroup_bpf_detach(cgrp, prog, type, flags);
Daniel Mackf791c422016-11-23 16:52:26 +01006753 mutex_unlock(&cgroup_mutex);
Alexei Starovoitov1ee2b4b2017-02-10 20:28:24 -08006754 return ret;
Daniel Mackf791c422016-11-23 16:52:26 +01006755}
6756#endif /* CONFIG_CGROUP_BPF */
6757
Paul Menagefe693432009-09-23 15:56:20 -07006758#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04006759static struct cgroup_subsys_state *
6760debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07006761{
6762 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
6763
6764 if (!css)
6765 return ERR_PTR(-ENOMEM);
6766
6767 return css;
6768}
6769
Tejun Heoeb954192013-08-08 20:11:23 -04006770static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07006771{
Tejun Heoeb954192013-08-08 20:11:23 -04006772 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07006773}
6774
Tejun Heo182446d2013-08-08 20:11:24 -04006775static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
6776 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006777{
Tejun Heo182446d2013-08-08 20:11:24 -04006778 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07006779}
6780
Tejun Heo182446d2013-08-08 20:11:24 -04006781static u64 current_css_set_read(struct cgroup_subsys_state *css,
6782 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006783{
6784 return (u64)(unsigned long)current->cgroups;
6785}
6786
Tejun Heo182446d2013-08-08 20:11:24 -04006787static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08006788 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006789{
6790 u64 count;
6791
6792 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07006793 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07006794 rcu_read_unlock();
6795 return count;
6796}
6797
Tejun Heo2da8ca82013-12-05 12:28:04 -05006798static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006799{
Tejun Heo69d02062013-06-12 21:04:50 -07006800 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07006801 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05006802 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07006803
Tejun Heoe61734c2014-02-12 09:29:50 -05006804 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
6805 if (!name_buf)
6806 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07006807
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006808 spin_lock_irq(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006809 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07006810 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07006811 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07006812 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07006813
Tejun Heoa2dd4242014-03-19 10:23:55 -04006814 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07006815 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04006816 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006817 }
6818 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006819 spin_unlock_irq(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05006820 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006821 return 0;
6822}
6823
6824#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05006825static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006826{
Tejun Heo2da8ca82013-12-05 12:28:04 -05006827 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07006828 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07006829
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006830 spin_lock_irq(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04006831 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07006832 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07006833 struct task_struct *task;
6834 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05006835
Nick Desaulniersc6bb32b2016-09-12 15:47:42 -07006836 seq_printf(seq, "css_set %pK\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05006837
Tejun Heo5abb8852013-06-12 21:04:49 -07006838 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05006839 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6840 goto overflow;
6841 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07006842 }
Tejun Heoc7561122014-02-25 10:04:01 -05006843
6844 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
6845 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6846 goto overflow;
6847 seq_printf(seq, " task %d\n", task_pid_vnr(task));
6848 }
6849 continue;
6850 overflow:
6851 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07006852 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006853 spin_unlock_irq(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006854 return 0;
6855}
6856
Tejun Heo182446d2013-08-08 20:11:24 -04006857static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006858{
Tejun Heo27bd4db2015-10-15 16:41:50 -04006859 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08006860 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07006861}
6862
6863static struct cftype debug_files[] = {
6864 {
Paul Menagefe693432009-09-23 15:56:20 -07006865 .name = "taskcount",
6866 .read_u64 = debug_taskcount_read,
6867 },
6868
6869 {
6870 .name = "current_css_set",
6871 .read_u64 = current_css_set_read,
6872 },
6873
6874 {
6875 .name = "current_css_set_refcount",
6876 .read_u64 = current_css_set_refcount_read,
6877 },
6878
6879 {
Paul Menage7717f7b2009-09-23 15:56:22 -07006880 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006881 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006882 },
6883
6884 {
6885 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006886 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006887 },
6888
6889 {
Paul Menagefe693432009-09-23 15:56:20 -07006890 .name = "releasable",
6891 .read_u64 = releasable_read,
6892 },
Paul Menagefe693432009-09-23 15:56:20 -07006893
Tejun Heo4baf6e32012-04-01 12:09:55 -07006894 { } /* terminate */
6895};
Paul Menagefe693432009-09-23 15:56:20 -07006896
Tejun Heo073219e2014-02-08 10:36:58 -05006897struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006898 .css_alloc = debug_css_alloc,
6899 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04006900 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07006901};
6902#endif /* CONFIG_CGROUP_DEBUG */