blob: 4a439de621bd336bf501e44d5e080cb5f7c6f8a0 [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
Tejun Heo0a268db2016-12-27 14:49:06 -050031#include "cgroup-internal.h"
32
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/cred.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>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080037#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010044#include <linux/sched/task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040047#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070048#include <linux/string.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080049#include <linux/hashtable.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070050#include <linux/idr.h>
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020051#include <linux/kthread.h>
Arun Sharma600634972011-07-26 16:09:06 -070052#include <linux/atomic.h>
Tejun Heoe93ad192016-01-19 12:18:41 -050053#include <linux/cpuset.h>
Aditya Kalia79a9082016-01-29 02:54:06 -060054#include <linux/proc_ns.h>
55#include <linux/nsproxy.h>
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -070056#include <linux/file.h>
Tejun Heod4ff7492018-04-26 14:29:04 -070057#include <linux/sched/cputime.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050058#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070059
Tejun Heoed1777d2016-08-10 11:23:44 -040060#define CREATE_TRACE_POINTS
61#include <trace/events/cgroup.h>
62
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050063#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
64 MAX_CFTYPE_NAME + 2)
Tejun Heob12e3582018-04-26 14:29:04 -070065/* let's not notify more than 100 times per second */
66#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050067
Tejun Heob1a21362013-11-29 10:42:58 -050068/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069 * cgroup_mutex is the master lock. Any modification to cgroup or its
70 * hierarchy must be performed while holding it.
71 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040072 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050073 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080074 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050075 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
76 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080077 */
Tejun Heo22194492013-04-07 09:29:51 -070078DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040079DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0a268db2016-12-27 14:49:06 -050080
81#ifdef CONFIG_PROVE_RCU
Tejun Heo0e1d7682014-02-25 10:04:03 -050082EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040083EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070084#endif
85
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -040086DEFINE_SPINLOCK(trace_cgroup_path_lock);
87char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
88
Tejun Heo69e943b2014-02-08 10:36:58 -050089/*
Tejun Heo15a4c832014-05-04 15:09:14 -040090 * Protects cgroup_idr and css_idr so that IDs can be released without
91 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040092 */
93static DEFINE_SPINLOCK(cgroup_idr_lock);
94
95/*
Tejun Heo34c06252015-11-05 00:12:24 -050096 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
97 * against file removal/re-creation across css hiding.
98 */
99static DEFINE_SPINLOCK(cgroup_file_kn_lock);
100
Tejun Heo1ed13282015-09-16 12:53:17 -0400101struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
102
Tejun Heo8353da12014-05-13 12:19:23 -0400103#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700104 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
105 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400106 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500107
Ben Blumaae8aab2010-03-10 15:22:07 -0800108/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500109 * cgroup destruction makes heavy use of work items and there can be a lot
110 * of concurrent destructions. Use a separate workqueue so that cgroup
111 * destruction work items don't end up filling up max_active of system_wq
112 * which may lead to deadlock.
113 */
114static struct workqueue_struct *cgroup_destroy_wq;
115
Tejun Heo3ed80a62014-02-08 10:36:58 -0500116/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500117#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo0a268db2016-12-27 14:49:06 -0500118struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119#include <linux/cgroup_subsys.h>
120};
Tejun Heo073219e2014-02-08 10:36:58 -0500121#undef SUBSYS
122
123/* array of cgroup subsystem names */
124#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
125static const char *cgroup_subsys_name[] = {
126#include <linux/cgroup_subsys.h>
127};
128#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129
Tejun Heo49d1dc42015-09-18 11:56:28 -0400130/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
131#define SUBSYS(_x) \
132 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
133 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
134 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
135 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
136#include <linux/cgroup_subsys.h>
137#undef SUBSYS
138
139#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
140static struct static_key_true *cgroup_subsys_enabled_key[] = {
141#include <linux/cgroup_subsys.h>
142};
143#undef SUBSYS
144
145#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
146static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
147#include <linux/cgroup_subsys.h>
148};
149#undef SUBSYS
150
Tejun Heoc58632b2018-04-26 14:29:04 -0700151static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
Tejun Heo041cd642017-09-25 08:12:05 -0700152
Paul Menageddbcc7e2007-10-18 23:39:30 -0700153/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400154 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700155 * unattached - it never has more than a single cgroup, and all tasks are
156 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157 */
Tejun Heoc58632b2018-04-26 14:29:04 -0700158struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
Tejun Heod0ec4232015-08-05 16:03:19 -0400159EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700160
Tejun Heoa2dd4242014-03-19 10:23:55 -0400161/*
162 * The default hierarchy always exists but is hidden until mounted for the
163 * first time. This is for backward compatibility.
164 */
Tejun Heoa7165262016-02-23 10:00:50 -0500165static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166
Tejun Heo5533e012014-05-14 19:33:07 -0400167/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500168static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400169
Tejun Heof6d635ad2016-03-08 11:51:26 -0500170/* some controllers are implicitly enabled on the default hierarchy */
Tejun Heob8074212017-01-20 12:06:08 -0500171static u16 cgrp_dfl_implicit_ss_mask;
Tejun Heof6d635ad2016-03-08 11:51:26 -0500172
Tejun Heo8cfd8142017-07-21 11:14:51 -0400173/* some controllers can be threaded on the default hierarchy */
174static u16 cgrp_dfl_threaded_ss_mask;
175
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176/* The list of hierarchy roots */
Tejun Heo0a268db2016-12-27 14:49:06 -0500177LIST_HEAD(cgroup_roots);
Tejun Heo9871bf92013-06-24 15:21:47 -0700178static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179
Tejun Heo3417ae12014-02-08 10:37:01 -0500180/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700181static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700182
Li Zefan794611a2013-06-18 18:53:53 +0800183/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400184 * Assign a monotonically increasing serial number to csses. It guarantees
185 * cgroups with bigger numbers are newer than those with smaller numbers.
186 * Also, as csses are always appended to the parent's ->children list, it
187 * guarantees that sibling csses are always sorted in the ascending serial
188 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800189 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400190static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800191
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000192/*
Tejun Heob8074212017-01-20 12:06:08 -0500193 * These bitmasks identify subsystems with specific features to avoid
194 * having to do iterative checks repeatedly.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700195 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500196static u16 have_fork_callback __read_mostly;
197static u16 have_exit_callback __read_mostly;
198static u16 have_free_callback __read_mostly;
Tejun Heob8074212017-01-20 12:06:08 -0500199static u16 have_canfork_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200
Aditya Kalia79a9082016-01-29 02:54:06 -0600201/* cgroup namespace for init task */
202struct cgroup_namespace init_cgroup_ns = {
Elena Reshetova387ad962017-02-20 12:19:00 +0200203 .count = REFCOUNT_INIT(2),
Aditya Kalia79a9082016-01-29 02:54:06 -0600204 .user_ns = &init_user_ns,
205 .ns.ops = &cgroupns_operations,
206 .ns.inum = PROC_CGROUP_INIT_INO,
207 .root_cset = &init_css_set,
208};
209
Tejun Heo67e9c742015-11-16 11:13:34 -0500210static struct file_system_type cgroup2_fs_type;
Tejun Heod62beb72016-12-27 14:49:08 -0500211static struct cftype cgroup_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700212
Tejun Heo334c3672016-03-03 09:58:01 -0500213static int cgroup_apply_control(struct cgroup *cgrp);
214static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400215static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800216static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500217static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
218 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400219static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400220static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400221static int cgroup_addrm_files(struct cgroup_subsys_state *css,
222 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400223 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400225/**
226 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
227 * @ssid: subsys ID of interest
228 *
229 * cgroup_subsys_enabled() can only be used with literal subsys names which
230 * is fine for individual subsystems but unsuitable for cgroup core. This
231 * is slower static_key_enabled() based test indexed by @ssid.
232 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500233bool cgroup_ssid_enabled(int ssid)
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400234{
Arnd Bergmanncfe02a82016-03-15 00:21:06 +0100235 if (CGROUP_SUBSYS_COUNT == 0)
236 return false;
237
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400238 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
239}
240
Tejun Heo9e10a132015-09-18 11:56:28 -0400241/**
242 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
243 * @cgrp: the cgroup of interest
244 *
245 * The default hierarchy is the v2 interface of cgroup and this function
246 * can be used to test whether a cgroup is on the default hierarchy for
247 * cases where a subsystem should behave differnetly depending on the
248 * interface version.
249 *
250 * The set of behaviors which change on the default hierarchy are still
251 * being determined and the mount option is prefixed with __DEVEL__.
252 *
253 * List of changed behaviors:
254 *
255 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
256 * and "name" are disallowed.
257 *
258 * - When mounting an existing superblock, mount options should match.
259 *
260 * - Remount is disallowed.
261 *
262 * - rename(2) is disallowed.
263 *
264 * - "tasks" is removed. Everything should be at process granularity. Use
265 * "cgroup.procs" instead.
266 *
267 * - "cgroup.procs" is not sorted. pids will be unique unless they got
268 * recycled inbetween reads.
269 *
270 * - "release_agent" and "notify_on_release" are removed. Replacement
271 * notification mechanism will be implemented.
272 *
273 * - "cgroup.clone_children" is removed.
274 *
275 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
276 * and its descendants contain no task; otherwise, 1. The file also
277 * generates kernfs notification which can be monitored through poll and
278 * [di]notify when the value of the file changes.
279 *
280 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
281 * take masks of ancestors with non-empty cpus/mems, instead of being
282 * moved to an ancestor.
283 *
284 * - cpuset: a task can be moved into an empty cpuset, and again it takes
285 * masks of ancestors.
286 *
287 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
288 * is not created.
289 *
290 * - blkcg: blk-throttle becomes properly hierarchical.
291 *
292 * - debug: disallowed on the default hierarchy.
293 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500294bool cgroup_on_dfl(const struct cgroup *cgrp)
Tejun Heo9e10a132015-09-18 11:56:28 -0400295{
296 return cgrp->root == &cgrp_dfl_root;
297}
298
Tejun Heo6fa49182014-05-04 15:09:13 -0400299/* IDR wrappers which synchronize using cgroup_idr_lock */
300static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
301 gfp_t gfp_mask)
302{
303 int ret;
304
305 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400306 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800307 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400308 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400309 idr_preload_end();
310 return ret;
311}
312
313static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
314{
315 void *ret;
316
Tejun Heo54504e92014-05-13 12:10:59 -0400317 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400318 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400319 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400320 return ret;
321}
322
323static void cgroup_idr_remove(struct idr *idr, int id)
324{
Tejun Heo54504e92014-05-13 12:10:59 -0400325 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400326 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400327 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400328}
329
Tejun Heo27f26752017-07-16 21:44:18 -0400330static bool cgroup_has_tasks(struct cgroup *cgrp)
Tejun Heod51f39b2014-05-16 13:22:48 -0400331{
Tejun Heo27f26752017-07-16 21:44:18 -0400332 return cgrp->nr_populated_csets;
333}
Tejun Heod51f39b2014-05-16 13:22:48 -0400334
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400335bool cgroup_is_threaded(struct cgroup *cgrp)
Tejun Heo454000a2017-05-15 09:34:02 -0400336{
337 return cgrp->dom_cgrp != cgrp;
338}
339
Tejun Heo8cfd8142017-07-21 11:14:51 -0400340/* can @cgrp host both domain and threaded children? */
341static bool cgroup_is_mixable(struct cgroup *cgrp)
342{
343 /*
344 * Root isn't under domain level resource control exempting it from
345 * the no-internal-process constraint, so it can serve as a thread
346 * root and a parent of resource domains at the same time.
347 */
348 return !cgroup_parent(cgrp);
349}
350
351/* can @cgrp become a thread root? should always be true for a thread root */
352static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
353{
354 /* mixables don't care */
355 if (cgroup_is_mixable(cgrp))
356 return true;
357
358 /* domain roots can't be nested under threaded */
359 if (cgroup_is_threaded(cgrp))
360 return false;
361
362 /* can only have either domain or threaded children */
363 if (cgrp->nr_populated_domain_children)
364 return false;
365
366 /* and no domain controllers can be enabled */
367 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
368 return false;
369
370 return true;
371}
372
373/* is @cgrp root of a threaded subtree? */
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400374bool cgroup_is_thread_root(struct cgroup *cgrp)
Tejun Heo8cfd8142017-07-21 11:14:51 -0400375{
376 /* thread root should be a domain */
377 if (cgroup_is_threaded(cgrp))
378 return false;
379
380 /* a domain w/ threaded children is a thread root */
381 if (cgrp->nr_threaded_children)
382 return true;
383
384 /*
385 * A domain which has tasks and explicit threaded controllers
386 * enabled is a thread root.
387 */
388 if (cgroup_has_tasks(cgrp) &&
389 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
390 return true;
391
392 return false;
393}
394
395/* a domain which isn't connected to the root w/o brekage can't be used */
396static bool cgroup_is_valid_domain(struct cgroup *cgrp)
397{
398 /* the cgroup itself can be a thread root */
399 if (cgroup_is_threaded(cgrp))
400 return false;
401
402 /* but the ancestors can't be unless mixable */
403 while ((cgrp = cgroup_parent(cgrp))) {
404 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
405 return false;
406 if (cgroup_is_threaded(cgrp))
407 return false;
408 }
409
410 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400411}
412
Tejun Heo5531dc92016-03-03 09:57:58 -0500413/* subsystems visibly enabled on a cgroup */
414static u16 cgroup_control(struct cgroup *cgrp)
415{
416 struct cgroup *parent = cgroup_parent(cgrp);
417 u16 root_ss_mask = cgrp->root->subsys_mask;
418
Tejun Heo8cfd8142017-07-21 11:14:51 -0400419 if (parent) {
420 u16 ss_mask = parent->subtree_control;
421
422 /* threaded cgroups can only have threaded controllers */
423 if (cgroup_is_threaded(cgrp))
424 ss_mask &= cgrp_dfl_threaded_ss_mask;
425 return ss_mask;
426 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500427
428 if (cgroup_on_dfl(cgrp))
Tejun Heof6d635ad2016-03-08 11:51:26 -0500429 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
430 cgrp_dfl_implicit_ss_mask);
Tejun Heo5531dc92016-03-03 09:57:58 -0500431 return root_ss_mask;
432}
433
434/* subsystems enabled on a cgroup */
435static u16 cgroup_ss_mask(struct cgroup *cgrp)
436{
437 struct cgroup *parent = cgroup_parent(cgrp);
438
Tejun Heo8cfd8142017-07-21 11:14:51 -0400439 if (parent) {
440 u16 ss_mask = parent->subtree_ss_mask;
441
442 /* threaded cgroups can only have threaded controllers */
443 if (cgroup_is_threaded(cgrp))
444 ss_mask &= cgrp_dfl_threaded_ss_mask;
445 return ss_mask;
446 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500447
448 return cgrp->root->subsys_mask;
449}
450
Tejun Heo95109b62013-08-08 20:11:27 -0400451/**
452 * cgroup_css - obtain a cgroup's css for the specified subsystem
453 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400454 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400455 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400456 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
457 * function must be called either under cgroup_mutex or rcu_read_lock() and
458 * the caller is responsible for pinning the returned css if it wants to
459 * keep accessing it outside the said locks. This function may return
460 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400461 */
462static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400463 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400464{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400465 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500466 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500467 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400468 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400469 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400470}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700471
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400472/**
Tejun Heod41bf8c2017-10-23 16:18:27 -0700473 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
474 * @cgrp: the cgroup of interest
475 * @ss: the subsystem of interest
476 *
477 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist
478 * or is offline, %NULL is returned.
479 */
480static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
481 struct cgroup_subsys *ss)
482{
483 struct cgroup_subsys_state *css;
484
485 rcu_read_lock();
486 css = cgroup_css(cgrp, ss);
487 if (!css || !css_tryget_online(css))
488 css = NULL;
489 rcu_read_unlock();
490
491 return css;
492}
493
494/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400495 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
496 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400497 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400498 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400499 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400500 * as the matching css of the nearest ancestor including self which has @ss
501 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
502 * function is guaranteed to return non-NULL css.
503 */
504static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
505 struct cgroup_subsys *ss)
506{
507 lockdep_assert_held(&cgroup_mutex);
508
509 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400510 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400511
Tejun Heoeeecbd12014-11-18 02:49:52 -0500512 /*
513 * This function is used while updating css associations and thus
Tejun Heo5531dc92016-03-03 09:57:58 -0500514 * can't test the csses directly. Test ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500515 */
Tejun Heo5531dc92016-03-03 09:57:58 -0500516 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
Tejun Heod51f39b2014-05-16 13:22:48 -0400517 cgrp = cgroup_parent(cgrp);
Tejun Heo5531dc92016-03-03 09:57:58 -0500518 if (!cgrp)
519 return NULL;
520 }
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400521
522 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700523}
524
Tejun Heoeeecbd12014-11-18 02:49:52 -0500525/**
526 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
527 * @cgrp: the cgroup of interest
528 * @ss: the subsystem of interest
529 *
530 * Find and get the effective css of @cgrp for @ss. The effective css is
531 * defined as the matching css of the nearest ancestor including self which
532 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
533 * the root css is returned, so this function always returns a valid css.
534 * The returned css must be put using css_put().
535 */
536struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
537 struct cgroup_subsys *ss)
538{
539 struct cgroup_subsys_state *css;
540
541 rcu_read_lock();
542
543 do {
544 css = cgroup_css(cgrp, ss);
545
546 if (css && css_tryget_online(css))
547 goto out_unlock;
548 cgrp = cgroup_parent(cgrp);
549 } while (cgrp);
550
551 css = init_css_set.subsys[ss->id];
552 css_get(css);
553out_unlock:
554 rcu_read_unlock();
555 return css;
556}
557
Tejun Heoa590b902017-04-28 15:14:55 -0400558static void cgroup_get_live(struct cgroup *cgrp)
Tejun Heo052c3f32015-10-15 16:41:50 -0400559{
560 WARN_ON_ONCE(cgroup_is_dead(cgrp));
561 css_get(&cgrp->self);
562}
563
Tejun Heob4168642014-05-13 12:16:21 -0400564struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500565{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500566 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400567 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500568
569 /*
570 * This is open and unprotected implementation of cgroup_css().
571 * seq_css() is only called from a kernfs file operation which has
572 * an active reference on the file. Because all the subsystem
573 * files are drained before a css is disassociated with a cgroup,
574 * the matching css from the cgroup's subsys table is guaranteed to
575 * be and stay valid until the enclosing operation is complete.
576 */
577 if (cft->ss)
578 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
579 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400580 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500581}
Tejun Heob4168642014-05-13 12:16:21 -0400582EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500583
Tejun Heo30159ec2013-06-25 11:53:37 -0700584/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500585 * for_each_css - iterate all css's of a cgroup
586 * @css: the iteration cursor
587 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
588 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700589 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400590 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700591 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500592#define for_each_css(css, ssid, cgrp) \
593 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
594 if (!((css) = rcu_dereference_check( \
595 (cgrp)->subsys[(ssid)], \
596 lockdep_is_held(&cgroup_mutex)))) { } \
597 else
598
599/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400600 * for_each_e_css - iterate all effective css's of a cgroup
601 * @css: the iteration cursor
602 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
603 * @cgrp: the target cgroup to iterate css's of
604 *
605 * Should be called under cgroup_[tree_]mutex.
606 */
607#define for_each_e_css(css, ssid, cgrp) \
608 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
609 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
610 ; \
611 else
612
613/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500614 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000615 * @ss: the iteration cursor
616 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500617 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000618 *
619 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500620 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000621 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500622#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
623 unsigned long __ss_mask = (ss_mask); \
624 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000625 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500626 break; \
627 } \
628 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
629 (ss) = cgroup_subsys[ssid]; \
630 {
631
632#define while_each_subsys_mask() \
633 } \
634 } \
635} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000636
Tejun Heof8f22e52014-04-23 11:13:16 -0400637/* iterate over child cgrps, lock should be held throughout iteration */
638#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400639 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400640 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400641 cgroup_is_dead(child); })) \
642 ; \
643 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700644
Tejun Heoce3f1d92016-03-03 09:57:59 -0500645/* walk live descendants in preorder */
646#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
647 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
648 if (({ lockdep_assert_held(&cgroup_mutex); \
649 (dsct) = (d_css)->cgroup; \
650 cgroup_is_dead(dsct); })) \
651 ; \
652 else
653
654/* walk live descendants in postorder */
655#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
656 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
657 if (({ lockdep_assert_held(&cgroup_mutex); \
658 (dsct) = (d_css)->cgroup; \
659 cgroup_is_dead(dsct); })) \
660 ; \
661 else
662
Tejun Heo172a2c062014-03-19 10:23:53 -0400663/*
664 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700665 * hierarchies being mounted. It contains a pointer to the root state
666 * for each subsystem. Also used to anchor the list of css_sets. Not
667 * reference-counted, to improve performance when child cgroups
668 * haven't been created.
669 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400670struct css_set init_css_set = {
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200671 .refcount = REFCOUNT_INIT(1),
Tejun Heo454000a2017-05-15 09:34:02 -0400672 .dom_cset = &init_css_set,
Tejun Heo172a2c062014-03-19 10:23:53 -0400673 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
674 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500675 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo454000a2017-05-15 09:34:02 -0400676 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500677 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
Tejun Heo172a2c062014-03-19 10:23:53 -0400678 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
679 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heo38683142017-09-25 13:50:20 -0700680
681 /*
682 * The following field is re-initialized when this cset gets linked
683 * in cgroup_init(). However, let's initialize the field
684 * statically too so that the default cgroup can be accessed safely
685 * early during boot.
686 */
687 .dfl_cgrp = &cgrp_dfl_root.cgrp,
Tejun Heo172a2c062014-03-19 10:23:53 -0400688};
Paul Menage817929e2007-10-18 23:39:36 -0700689
Tejun Heo172a2c062014-03-19 10:23:53 -0400690static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700691
Tejun Heo454000a2017-05-15 09:34:02 -0400692static bool css_set_threaded(struct css_set *cset)
693{
694 return cset->dom_cset != cset;
695}
696
Tejun Heo842b5972014-04-25 18:28:02 -0400697/**
Tejun Heo0de09422015-10-15 16:41:49 -0400698 * css_set_populated - does a css_set contain any tasks?
699 * @cset: target css_set
Waiman Long73a72422017-06-13 17:18:01 -0400700 *
701 * css_set_populated() should be the same as !!cset->nr_tasks at steady
702 * state. However, css_set_populated() can be called while a task is being
703 * added to or removed from the linked list before the nr_tasks is
704 * properly updated. Hence, we can't just look at ->nr_tasks here.
Tejun Heo0de09422015-10-15 16:41:49 -0400705 */
706static bool css_set_populated(struct css_set *cset)
707{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400708 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400709
710 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
711}
712
713/**
Tejun Heo788b9502017-07-16 21:43:33 -0400714 * cgroup_update_populated - update the populated count of a cgroup
Tejun Heo842b5972014-04-25 18:28:02 -0400715 * @cgrp: the target cgroup
716 * @populated: inc or dec populated count
717 *
Tejun Heo0de09422015-10-15 16:41:49 -0400718 * One of the css_sets associated with @cgrp is either getting its first
Tejun Heo788b9502017-07-16 21:43:33 -0400719 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
720 * count is propagated towards root so that a given cgroup's
721 * nr_populated_children is zero iff none of its descendants contain any
722 * tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400723 *
Tejun Heo788b9502017-07-16 21:43:33 -0400724 * @cgrp's interface file "cgroup.populated" is zero if both
725 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
726 * 1 otherwise. When the sum changes from or to zero, userland is notified
727 * that the content of the interface file has changed. This can be used to
728 * detect when @cgrp and its descendants become populated or empty.
Tejun Heo842b5972014-04-25 18:28:02 -0400729 */
730static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
731{
Tejun Heo788b9502017-07-16 21:43:33 -0400732 struct cgroup *child = NULL;
733 int adj = populated ? 1 : -1;
734
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400735 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400736
737 do {
Tejun Heo788b9502017-07-16 21:43:33 -0400738 bool was_populated = cgroup_is_populated(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400739
Tejun Heo454000a2017-05-15 09:34:02 -0400740 if (!child) {
Tejun Heo788b9502017-07-16 21:43:33 -0400741 cgrp->nr_populated_csets += adj;
Tejun Heo454000a2017-05-15 09:34:02 -0400742 } else {
743 if (cgroup_is_threaded(child))
744 cgrp->nr_populated_threaded_children += adj;
745 else
746 cgrp->nr_populated_domain_children += adj;
747 }
Tejun Heo842b5972014-04-25 18:28:02 -0400748
Tejun Heo788b9502017-07-16 21:43:33 -0400749 if (was_populated == cgroup_is_populated(cgrp))
Tejun Heo842b5972014-04-25 18:28:02 -0400750 break;
751
Tejun Heod62beb72016-12-27 14:49:08 -0500752 cgroup1_check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400753 cgroup_file_notify(&cgrp->events_file);
754
Tejun Heo788b9502017-07-16 21:43:33 -0400755 child = cgrp;
Tejun Heod51f39b2014-05-16 13:22:48 -0400756 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400757 } while (cgrp);
758}
759
Tejun Heo0de09422015-10-15 16:41:49 -0400760/**
761 * css_set_update_populated - update populated state of a css_set
762 * @cset: target css_set
763 * @populated: whether @cset is populated or depopulated
764 *
765 * @cset is either getting the first task or losing the last. Update the
Tejun Heo788b9502017-07-16 21:43:33 -0400766 * populated counters of all associated cgroups accordingly.
Tejun Heo0de09422015-10-15 16:41:49 -0400767 */
768static void css_set_update_populated(struct css_set *cset, bool populated)
769{
770 struct cgrp_cset_link *link;
771
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400772 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400773
774 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
775 cgroup_update_populated(link->cgrp, populated);
776}
777
Tejun Heof6d7d042015-10-15 16:41:52 -0400778/**
779 * css_set_move_task - move a task from one css_set to another
780 * @task: task being moved
781 * @from_cset: css_set @task currently belongs to (may be NULL)
782 * @to_cset: new css_set @task is being moved to (may be NULL)
783 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
784 *
785 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
786 * css_set, @from_cset can be NULL. If @task is being disassociated
787 * instead of moved, @to_cset can be NULL.
788 *
Tejun Heo788b9502017-07-16 21:43:33 -0400789 * This function automatically handles populated counter updates and
Tejun Heoed27b9f2015-10-15 16:41:52 -0400790 * css_task_iter adjustments but the caller is responsible for managing
791 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400792 */
793static void css_set_move_task(struct task_struct *task,
794 struct css_set *from_cset, struct css_set *to_cset,
795 bool use_mg_tasks)
796{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400797 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400798
Tejun Heo20b454a2016-03-03 09:57:57 -0500799 if (to_cset && !css_set_populated(to_cset))
800 css_set_update_populated(to_cset, true);
801
Tejun Heof6d7d042015-10-15 16:41:52 -0400802 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400803 struct css_task_iter *it, *pos;
804
Tejun Heof6d7d042015-10-15 16:41:52 -0400805 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400806
807 /*
808 * @task is leaving, advance task iterators which are
809 * pointing to it so that they can resume at the next
810 * position. Advancing an iterator might remove it from
811 * the list, use safe walk. See css_task_iter_advance*()
812 * for details.
813 */
814 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
815 iters_node)
816 if (it->task_pos == &task->cg_list)
817 css_task_iter_advance(it);
818
Tejun Heof6d7d042015-10-15 16:41:52 -0400819 list_del_init(&task->cg_list);
820 if (!css_set_populated(from_cset))
821 css_set_update_populated(from_cset, false);
822 } else {
823 WARN_ON_ONCE(!list_empty(&task->cg_list));
824 }
825
826 if (to_cset) {
827 /*
828 * We are synchronized through cgroup_threadgroup_rwsem
829 * against PF_EXITING setting such that we can't race
830 * against cgroup_exit() changing the css_set to
831 * init_css_set and dropping the old one.
832 */
833 WARN_ON_ONCE(task->flags & PF_EXITING);
834
Tejun Heof6d7d042015-10-15 16:41:52 -0400835 rcu_assign_pointer(task->cgroups, to_cset);
836 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
837 &to_cset->tasks);
838 }
839}
840
Paul Menage7717f7b2009-09-23 15:56:22 -0700841/*
842 * hash table for cgroup groups. This improves the performance to find
843 * an existing css_set. This hash doesn't (currently) take into
844 * account cgroups in empty hierarchies.
845 */
Li Zefan472b1052008-04-29 01:00:11 -0700846#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800847static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700848
Li Zefan0ac801f2013-01-10 11:49:27 +0800849static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700850{
Li Zefan0ac801f2013-01-10 11:49:27 +0800851 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700852 struct cgroup_subsys *ss;
853 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700854
Tejun Heo30159ec2013-06-25 11:53:37 -0700855 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800856 key += (unsigned long)css[i];
857 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700858
Li Zefan0ac801f2013-01-10 11:49:27 +0800859 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700860}
861
Tejun Heodcfe1492016-12-27 14:49:09 -0500862void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700863{
Tejun Heo69d02062013-06-12 21:04:50 -0700864 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400865 struct cgroup_subsys *ss;
866 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700867
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400868 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500869
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200870 if (!refcount_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700871 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700872
Tejun Heo454000a2017-05-15 09:34:02 -0400873 WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
874
Tejun Heo53254f92015-11-23 14:55:41 -0500875 /* This css_set is dead. unlink it and release cgroup and css refs */
876 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400877 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500878 css_put(cset->subsys[ssid]);
879 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700880 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700881 css_set_count--;
882
Tejun Heo69d02062013-06-12 21:04:50 -0700883 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700884 list_del(&link->cset_link);
885 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400886 if (cgroup_parent(link->cgrp))
887 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700888 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700889 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700890
Tejun Heo454000a2017-05-15 09:34:02 -0400891 if (css_set_threaded(cset)) {
892 list_del(&cset->threaded_csets_node);
893 put_css_set_locked(cset->dom_cset);
894 }
895
Tejun Heo5abb8852013-06-12 21:04:49 -0700896 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700897}
898
Tejun Heob326f9d2013-06-24 15:21:48 -0700899/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700900 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700901 * @cset: candidate css_set being tested
902 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700903 * @new_cgrp: cgroup that's being entered by the task
904 * @template: desired set of css pointers in css_set (pre-calculated)
905 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800906 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700907 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
908 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700909static bool compare_css_sets(struct css_set *cset,
910 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700911 struct cgroup *new_cgrp,
912 struct cgroup_subsys_state *template[])
913{
Tejun Heo454000a2017-05-15 09:34:02 -0400914 struct cgroup *new_dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700915 struct list_head *l1, *l2;
916
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400917 /*
918 * On the default hierarchy, there can be csets which are
919 * associated with the same set of cgroups but different csses.
920 * Let's first ensure that csses match.
921 */
922 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700923 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700924
Tejun Heo454000a2017-05-15 09:34:02 -0400925
926 /* @cset's domain should match the default cgroup's */
927 if (cgroup_on_dfl(new_cgrp))
928 new_dfl_cgrp = new_cgrp;
929 else
930 new_dfl_cgrp = old_cset->dfl_cgrp;
931
932 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
933 return false;
934
Paul Menage7717f7b2009-09-23 15:56:22 -0700935 /*
936 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400937 * different cgroups in hierarchies. As different cgroups may
938 * share the same effective css, this comparison is always
939 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700940 */
Tejun Heo69d02062013-06-12 21:04:50 -0700941 l1 = &cset->cgrp_links;
942 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700943 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700944 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700945 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700946
947 l1 = l1->next;
948 l2 = l2->next;
949 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700950 if (l1 == &cset->cgrp_links) {
951 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700952 break;
953 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700954 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700955 }
956 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700957 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
958 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
959 cgrp1 = link1->cgrp;
960 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700961 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700962 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700963
964 /*
965 * If this hierarchy is the hierarchy of the cgroup
966 * that's changing, then we need to check that this
967 * css_set points to the new cgroup; if it's any other
968 * hierarchy, then this css_set should point to the
969 * same cgroup as the old css_set.
970 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700971 if (cgrp1->root == new_cgrp->root) {
972 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700973 return false;
974 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700975 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700976 return false;
977 }
978 }
979 return true;
980}
981
Tejun Heob326f9d2013-06-24 15:21:48 -0700982/**
983 * find_existing_css_set - init css array and find the matching css_set
984 * @old_cset: the css_set that we're using before the cgroup transition
985 * @cgrp: the cgroup that we're moving into
986 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700987 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700988static struct css_set *find_existing_css_set(struct css_set *old_cset,
989 struct cgroup *cgrp,
990 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700991{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400992 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700993 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700994 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800995 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700996 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700997
Ben Blumaae8aab2010-03-10 15:22:07 -0800998 /*
999 * Build the set of subsystem state objects that we want to see in the
1000 * new css_set. while subsystems can change globally, the entries here
1001 * won't change, so no need for locking.
1002 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001003 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -04001004 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001005 /*
1006 * @ss is in this hierarchy, so we want the
1007 * effective css from @cgrp.
1008 */
1009 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -07001010 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001011 /*
1012 * @ss is not in this hierarchy, so we don't want
1013 * to change the css.
1014 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001015 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -07001016 }
1017 }
1018
Li Zefan0ac801f2013-01-10 11:49:27 +08001019 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -07001020 hash_for_each_possible(css_set_table, cset, hlist, key) {
1021 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -07001022 continue;
1023
1024 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -07001025 return cset;
Li Zefan472b1052008-04-29 01:00:11 -07001026 }
Paul Menage817929e2007-10-18 23:39:36 -07001027
1028 /* No existing cgroup group matched */
1029 return NULL;
1030}
1031
Tejun Heo69d02062013-06-12 21:04:50 -07001032static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -07001033{
Tejun Heo69d02062013-06-12 21:04:50 -07001034 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001035
Tejun Heo69d02062013-06-12 21:04:50 -07001036 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1037 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07001038 kfree(link);
1039 }
1040}
1041
Tejun Heo69d02062013-06-12 21:04:50 -07001042/**
1043 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1044 * @count: the number of links to allocate
1045 * @tmp_links: list_head the allocated links are put on
1046 *
1047 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1048 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -07001049 */
Tejun Heo69d02062013-06-12 21:04:50 -07001050static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -07001051{
Tejun Heo69d02062013-06-12 21:04:50 -07001052 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -07001053 int i;
Tejun Heo69d02062013-06-12 21:04:50 -07001054
1055 INIT_LIST_HEAD(tmp_links);
1056
Li Zefan36553432008-07-29 22:33:19 -07001057 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -07001058 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -07001059 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -07001060 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001061 return -ENOMEM;
1062 }
Tejun Heo69d02062013-06-12 21:04:50 -07001063 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001064 }
1065 return 0;
1066}
1067
Li Zefanc12f65d2009-01-07 18:07:42 -08001068/**
1069 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -07001070 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -07001071 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -08001072 * @cgrp: the destination cgroup
1073 */
Tejun Heo69d02062013-06-12 21:04:50 -07001074static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1075 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -08001076{
Tejun Heo69d02062013-06-12 21:04:50 -07001077 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -08001078
Tejun Heo69d02062013-06-12 21:04:50 -07001079 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -04001080
1081 if (cgroup_on_dfl(cgrp))
1082 cset->dfl_cgrp = cgrp;
1083
Tejun Heo69d02062013-06-12 21:04:50 -07001084 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1085 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07001086 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -04001087
Paul Menage7717f7b2009-09-23 15:56:22 -07001088 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001089 * Always add links to the tail of the lists so that the lists are
1090 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001091 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001092 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001093 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001094
1095 if (cgroup_parent(cgrp))
Tejun Heoa590b902017-04-28 15:14:55 -04001096 cgroup_get_live(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001097}
1098
Tejun Heob326f9d2013-06-24 15:21:48 -07001099/**
1100 * find_css_set - return a new css_set with one cgroup updated
1101 * @old_cset: the baseline css_set
1102 * @cgrp: the cgroup to be updated
1103 *
1104 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1105 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001106 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001107static struct css_set *find_css_set(struct css_set *old_cset,
1108 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001109{
Tejun Heob326f9d2013-06-24 15:21:48 -07001110 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001111 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001112 struct list_head tmp_links;
1113 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001114 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001115 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001116 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001117
Tejun Heob326f9d2013-06-24 15:21:48 -07001118 lockdep_assert_held(&cgroup_mutex);
1119
Paul Menage817929e2007-10-18 23:39:36 -07001120 /* First see if we already have a cgroup group that matches
1121 * the desired set */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001122 spin_lock_irq(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001123 cset = find_existing_css_set(old_cset, cgrp, template);
1124 if (cset)
1125 get_css_set(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001126 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001127
Tejun Heo5abb8852013-06-12 21:04:49 -07001128 if (cset)
1129 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001130
Tejun Heof4f4be22013-06-12 21:04:51 -07001131 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001132 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001133 return NULL;
1134
Tejun Heo69d02062013-06-12 21:04:50 -07001135 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001136 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001137 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001138 return NULL;
1139 }
1140
Elena Reshetova4b9502e62017-03-08 10:00:40 +02001141 refcount_set(&cset->refcount, 1);
Tejun Heo454000a2017-05-15 09:34:02 -04001142 cset->dom_cset = cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07001143 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001144 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001145 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo454000a2017-05-15 09:34:02 -04001146 INIT_LIST_HEAD(&cset->threaded_csets);
Tejun Heo5abb8852013-06-12 21:04:49 -07001147 INIT_HLIST_NODE(&cset->hlist);
Tejun Heo5f617ebb2016-12-27 14:49:05 -05001148 INIT_LIST_HEAD(&cset->cgrp_links);
1149 INIT_LIST_HEAD(&cset->mg_preload_node);
1150 INIT_LIST_HEAD(&cset->mg_node);
Paul Menage817929e2007-10-18 23:39:36 -07001151
1152 /* Copy the set of subsystem state objects generated in
1153 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001154 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001155
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001156 spin_lock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001157 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001158 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001159 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001160
Paul Menage7717f7b2009-09-23 15:56:22 -07001161 if (c->root == cgrp->root)
1162 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001163 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001164 }
Paul Menage817929e2007-10-18 23:39:36 -07001165
Tejun Heo69d02062013-06-12 21:04:50 -07001166 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001167
Paul Menage817929e2007-10-18 23:39:36 -07001168 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001169
Tejun Heo2d8f2432014-04-23 11:13:15 -04001170 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001171 key = css_set_hash(cset->subsys);
1172 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001173
Tejun Heo53254f92015-11-23 14:55:41 -05001174 for_each_subsys(ss, ssid) {
1175 struct cgroup_subsys_state *css = cset->subsys[ssid];
1176
Tejun Heo2d8f2432014-04-23 11:13:15 -04001177 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001178 &css->cgroup->e_csets[ssid]);
1179 css_get(css);
1180 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001181
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001182 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001183
Tejun Heo454000a2017-05-15 09:34:02 -04001184 /*
1185 * If @cset should be threaded, look up the matching dom_cset and
1186 * link them up. We first fully initialize @cset then look for the
1187 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1188 * to stay empty until we return.
1189 */
1190 if (cgroup_is_threaded(cset->dfl_cgrp)) {
1191 struct css_set *dcset;
1192
1193 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1194 if (!dcset) {
1195 put_css_set(cset);
1196 return NULL;
1197 }
1198
1199 spin_lock_irq(&css_set_lock);
1200 cset->dom_cset = dcset;
1201 list_add_tail(&cset->threaded_csets_node,
1202 &dcset->threaded_csets);
1203 spin_unlock_irq(&css_set_lock);
1204 }
1205
Tejun Heo5abb8852013-06-12 21:04:49 -07001206 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001207}
1208
Tejun Heo0a268db2016-12-27 14:49:06 -05001209struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001210{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001211 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001212
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001213 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001214}
1215
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001216static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001217{
1218 int id;
1219
1220 lockdep_assert_held(&cgroup_mutex);
1221
Tejun Heo985ed672014-03-19 10:23:53 -04001222 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001223 if (id < 0)
1224 return id;
1225
1226 root->hierarchy_id = id;
1227 return 0;
1228}
1229
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001230static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001231{
1232 lockdep_assert_held(&cgroup_mutex);
1233
Johannes Weiner8c8a5502016-06-17 12:23:59 -04001234 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heof2e85d52014-02-11 11:52:49 -05001235}
1236
Tejun Heo1592c9b2016-12-27 14:49:08 -05001237void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001238{
1239 if (root) {
Tejun Heof2e85d52014-02-11 11:52:49 -05001240 idr_destroy(&root->cgroup_idr);
1241 kfree(root);
1242 }
1243}
1244
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001245static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001246{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001247 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001248 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001249
Tejun Heoed1777d2016-08-10 11:23:44 -04001250 trace_cgroup_destroy_root(root);
1251
Tejun Heo334c3672016-03-03 09:58:01 -05001252 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -05001253
Tejun Heo776f02f2014-02-12 09:29:50 -05001254 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001255 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001256
Tejun Heof2e85d52014-02-11 11:52:49 -05001257 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo334c3672016-03-03 09:58:01 -05001258 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -05001259
1260 /*
1261 * Release all the links from cset_links to this hierarchy's
1262 * root cgroup
1263 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001264 spin_lock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001265
1266 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1267 list_del(&link->cset_link);
1268 list_del(&link->cgrp_link);
1269 kfree(link);
1270 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001271
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001272 spin_unlock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001273
1274 if (!list_empty(&root->root_list)) {
1275 list_del(&root->root_list);
1276 cgroup_root_count--;
1277 }
1278
1279 cgroup_exit_root_id(root);
1280
1281 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001282
Tejun Heo2bd59d42014-02-11 11:52:49 -05001283 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001284 cgroup_free_root(root);
1285}
1286
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001287/*
1288 * look up cgroup associated with current task's cgroup namespace on the
1289 * specified hierarchy
1290 */
1291static struct cgroup *
1292current_cgns_cgroup_from_root(struct cgroup_root *root)
1293{
1294 struct cgroup *res = NULL;
1295 struct css_set *cset;
1296
1297 lockdep_assert_held(&css_set_lock);
1298
1299 rcu_read_lock();
1300
1301 cset = current->nsproxy->cgroup_ns->root_cset;
1302 if (cset == &init_css_set) {
1303 res = &root->cgrp;
1304 } else {
1305 struct cgrp_cset_link *link;
1306
1307 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1308 struct cgroup *c = link->cgrp;
1309
1310 if (c->root == root) {
1311 res = c;
1312 break;
1313 }
1314 }
1315 }
1316 rcu_read_unlock();
1317
1318 BUG_ON(!res);
1319 return res;
1320}
1321
Tejun Heoceb6a082014-02-25 10:04:02 -05001322/* look up cgroup associated with given css_set on the specified hierarchy */
1323static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001324 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001325{
Paul Menage7717f7b2009-09-23 15:56:22 -07001326 struct cgroup *res = NULL;
1327
Tejun Heo96d365e2014-02-13 06:58:40 -05001328 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001329 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001330
Tejun Heo5abb8852013-06-12 21:04:49 -07001331 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001332 res = &root->cgrp;
Tejun Heo13d82fb2017-08-02 15:39:38 -07001333 } else if (root == &cgrp_dfl_root) {
1334 res = cset->dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001335 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001336 struct cgrp_cset_link *link;
1337
1338 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001339 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001340
Paul Menage7717f7b2009-09-23 15:56:22 -07001341 if (c->root == root) {
1342 res = c;
1343 break;
1344 }
1345 }
1346 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001347
Paul Menage7717f7b2009-09-23 15:56:22 -07001348 BUG_ON(!res);
1349 return res;
1350}
1351
1352/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001353 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001354 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001355 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001356struct cgroup *task_cgroup_from_root(struct task_struct *task,
1357 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001358{
1359 /*
1360 * No need to lock the task - since we hold cgroup_mutex the
1361 * task can't change groups, so the only thing that can happen
1362 * is that it exits and its css is set back to init_css_set.
1363 */
1364 return cset_cgroup_from_root(task_css_set(task), root);
1365}
1366
1367/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 * A task must hold cgroup_mutex to modify cgroups.
1369 *
1370 * Any task can increment and decrement the count field without lock.
1371 * So in general, code holding cgroup_mutex can't rely on the count
1372 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001373 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 * means that no tasks are currently attached, therefore there is no
1375 * way a task attached to that cgroup can fork (the other way to
1376 * increment the count). So code holding cgroup_mutex can safely
1377 * assume that if the count is zero, it will stay zero. Similarly, if
1378 * a task holds cgroup_mutex on a cgroup with zero count, it
1379 * knows that the cgroup won't be removed, as cgroup_rmdir()
1380 * needs that mutex.
1381 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382 * A cgroup can only be deleted if both its 'count' of using tasks
1383 * is zero, and its list of 'children' cgroups is empty. Since all
1384 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001385 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001387 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388 *
1389 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001390 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 */
1392
Tejun Heo2bd59d42014-02-11 11:52:49 -05001393static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Paul Menagea4243162007-10-18 23:39:35 -07001394
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001395static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1396 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001398 struct cgroup_subsys *ss = cft->ss;
1399
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001400 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1401 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1402 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001403 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1404 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001405 else
Tejun Heo08a77672018-01-09 07:21:15 -08001406 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001407 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001408}
1409
Tejun Heof2e85d52014-02-11 11:52:49 -05001410/**
1411 * cgroup_file_mode - deduce file mode of a control file
1412 * @cft: the control file in question
1413 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001414 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001415 */
1416static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001417{
Tejun Heof2e85d52014-02-11 11:52:49 -05001418 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001419
Tejun Heof2e85d52014-02-11 11:52:49 -05001420 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1421 mode |= S_IRUGO;
1422
Tejun Heo7dbdb192015-09-18 17:54:23 -04001423 if (cft->write_u64 || cft->write_s64 || cft->write) {
1424 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1425 mode |= S_IWUGO;
1426 else
1427 mode |= S_IWUSR;
1428 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001429
1430 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001431}
1432
Tejun Heoa9746d82014-05-13 12:19:22 -04001433/**
Tejun Heo8699b772016-02-22 22:25:46 -05001434 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001435 * @subtree_control: the new subtree_control mask to consider
Tejun Heo5ced2512016-03-03 09:58:01 -05001436 * @this_ss_mask: available subsystems
Tejun Heoaf0ba672014-07-08 18:02:57 -04001437 *
1438 * On the default hierarchy, a subsystem may request other subsystems to be
1439 * enabled together through its ->depends_on mask. In such cases, more
1440 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1441 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001442 * This function calculates which subsystems need to be enabled if
Tejun Heo5ced2512016-03-03 09:58:01 -05001443 * @subtree_control is to be applied while restricted to @this_ss_mask.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001444 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001445static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
Tejun Heo667c2492014-07-08 18:02:56 -04001446{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001447 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001448 struct cgroup_subsys *ss;
1449 int ssid;
1450
1451 lockdep_assert_held(&cgroup_mutex);
1452
Tejun Heof6d635ad2016-03-08 11:51:26 -05001453 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1454
Tejun Heoaf0ba672014-07-08 18:02:57 -04001455 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001456 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001457
Tejun Heob4e0eea2016-02-22 22:25:46 -05001458 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001459 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001460 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001461
1462 /*
1463 * Mask out subsystems which aren't available. This can
1464 * happen only if some depended-upon subsystems were bound
1465 * to non-default hierarchies.
1466 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001467 new_ss_mask &= this_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001468
1469 if (new_ss_mask == cur_ss_mask)
1470 break;
1471 cur_ss_mask = new_ss_mask;
1472 }
1473
Tejun Heo0f060de2014-11-18 02:49:50 -05001474 return cur_ss_mask;
1475}
1476
1477/**
Tejun Heoa9746d82014-05-13 12:19:22 -04001478 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1479 * @kn: the kernfs_node being serviced
1480 *
1481 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1482 * the method finishes if locking succeeded. Note that once this function
1483 * returns the cgroup returned by cgroup_kn_lock_live() may become
1484 * inaccessible any time. If the caller intends to continue to access the
1485 * cgroup, it should pin it before invoking this function.
1486 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001487void cgroup_kn_unlock(struct kernfs_node *kn)
Tejun Heoa9746d82014-05-13 12:19:22 -04001488{
1489 struct cgroup *cgrp;
1490
1491 if (kernfs_type(kn) == KERNFS_DIR)
1492 cgrp = kn->priv;
1493 else
1494 cgrp = kn->parent->priv;
1495
1496 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001497
1498 kernfs_unbreak_active_protection(kn);
1499 cgroup_put(cgrp);
1500}
1501
1502/**
1503 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1504 * @kn: the kernfs_node being serviced
Tejun Heo945ba192016-03-03 09:58:00 -05001505 * @drain_offline: perform offline draining on the cgroup
Tejun Heoa9746d82014-05-13 12:19:22 -04001506 *
1507 * This helper is to be used by a cgroup kernfs method currently servicing
1508 * @kn. It breaks the active protection, performs cgroup locking and
1509 * verifies that the associated cgroup is alive. Returns the cgroup if
1510 * alive; otherwise, %NULL. A successful return should be undone by a
Tejun Heo945ba192016-03-03 09:58:00 -05001511 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1512 * cgroup is drained of offlining csses before return.
Tejun Heoa9746d82014-05-13 12:19:22 -04001513 *
1514 * Any cgroup kernfs method implementation which requires locking the
1515 * associated cgroup should use this helper. It avoids nesting cgroup
1516 * locking under kernfs active protection and allows all kernfs operations
1517 * including self-removal.
1518 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001519struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
Tejun Heoa9746d82014-05-13 12:19:22 -04001520{
1521 struct cgroup *cgrp;
1522
1523 if (kernfs_type(kn) == KERNFS_DIR)
1524 cgrp = kn->priv;
1525 else
1526 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527
Tejun Heo2bd59d42014-02-11 11:52:49 -05001528 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001529 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001530 * active_ref. cgroup liveliness check alone provides enough
1531 * protection against removal. Ensure @cgrp stays accessible and
1532 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001533 */
Li Zefanaa323622014-09-04 14:43:38 +08001534 if (!cgroup_tryget(cgrp))
1535 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001536 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
Tejun Heo945ba192016-03-03 09:58:00 -05001538 if (drain_offline)
1539 cgroup_lock_and_drain_offline(cgrp);
1540 else
1541 mutex_lock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001542
1543 if (!cgroup_is_dead(cgrp))
1544 return cgrp;
1545
1546 cgroup_kn_unlock(kn);
1547 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548}
1549
Li Zefan2739d3c2013-01-21 18:18:33 +08001550static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001552 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553
Tejun Heo01f64742014-05-13 12:19:23 -04001554 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001555
1556 if (cft->file_offset) {
1557 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1558 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1559
1560 spin_lock_irq(&cgroup_file_kn_lock);
1561 cfile->kn = NULL;
1562 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heob12e3582018-04-26 14:29:04 -07001563
1564 del_timer_sync(&cfile->notify_timer);
Tejun Heo34c06252015-11-05 00:12:24 -05001565 }
1566
Tejun Heo2bd59d42014-02-11 11:52:49 -05001567 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001568}
1569
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001570/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001571 * css_clear_dir - remove subsys files in a cgroup directory
1572 * @css: taget css
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001573 */
Tejun Heo334c3672016-03-03 09:58:01 -05001574static void css_clear_dir(struct cgroup_subsys_state *css)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001575{
Tejun Heo334c3672016-03-03 09:58:01 -05001576 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001577 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001578
Tejun Heo88cb04b2016-03-03 09:57:58 -05001579 if (!(css->flags & CSS_VISIBLE))
1580 return;
1581
1582 css->flags &= ~CSS_VISIBLE;
1583
Tejun Heo5faaf052018-04-26 14:29:04 -07001584 if (!css->ss) {
1585 if (cgroup_on_dfl(cgrp))
1586 cfts = cgroup_base_files;
1587 else
1588 cfts = cgroup1_base_files;
1589
Tejun Heo4df8dc92015-09-18 17:54:23 -04001590 cgroup_addrm_files(css, cgrp, cfts, false);
Tejun Heo5faaf052018-04-26 14:29:04 -07001591 } else {
1592 list_for_each_entry(cfts, &css->ss->cfts, node)
1593 cgroup_addrm_files(css, cgrp, cfts, false);
1594 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595}
1596
Tejun Heoccdca212015-09-18 17:54:23 -04001597/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001598 * css_populate_dir - create subsys files in a cgroup directory
1599 * @css: target css
Tejun Heoccdca212015-09-18 17:54:23 -04001600 *
1601 * On failure, no file is added.
1602 */
Tejun Heo334c3672016-03-03 09:58:01 -05001603static int css_populate_dir(struct cgroup_subsys_state *css)
Tejun Heoccdca212015-09-18 17:54:23 -04001604{
Tejun Heo334c3672016-03-03 09:58:01 -05001605 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001606 struct cftype *cfts, *failed_cfts;
1607 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001608
Tejun Heo03970d32016-03-03 09:58:00 -05001609 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
Tejun Heo88cb04b2016-03-03 09:57:58 -05001610 return 0;
1611
Tejun Heo4df8dc92015-09-18 17:54:23 -04001612 if (!css->ss) {
1613 if (cgroup_on_dfl(cgrp))
Tejun Heod62beb72016-12-27 14:49:08 -05001614 cfts = cgroup_base_files;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001615 else
Tejun Heod62beb72016-12-27 14:49:08 -05001616 cfts = cgroup1_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001617
Tejun Heo5faaf052018-04-26 14:29:04 -07001618 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1619 if (ret < 0)
1620 return ret;
1621 } else {
1622 list_for_each_entry(cfts, &css->ss->cfts, node) {
1623 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1624 if (ret < 0) {
1625 failed_cfts = cfts;
1626 goto err;
1627 }
Tejun Heoccdca212015-09-18 17:54:23 -04001628 }
1629 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001630
1631 css->flags |= CSS_VISIBLE;
1632
Tejun Heoccdca212015-09-18 17:54:23 -04001633 return 0;
1634err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001635 list_for_each_entry(cfts, &css->ss->cfts, node) {
1636 if (cfts == failed_cfts)
1637 break;
1638 cgroup_addrm_files(css, cgrp, cfts, false);
1639 }
Tejun Heoccdca212015-09-18 17:54:23 -04001640 return ret;
1641}
1642
Tejun Heo0a268db2016-12-27 14:49:06 -05001643int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644{
Tejun Heo1ada4832015-09-18 17:54:23 -04001645 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001646 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001647 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648
Tejun Heoace2bee2014-02-11 11:52:47 -05001649 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001650
Tejun Heob4e0eea2016-02-22 22:25:46 -05001651 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heof6d635ad2016-03-08 11:51:26 -05001652 /*
1653 * If @ss has non-root csses attached to it, can't move.
1654 * If @ss is an implicit controller, it is exempt from this
1655 * rule and can be stolen.
1656 */
1657 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1658 !ss->implicit_on_dfl)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001659 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660
Tejun Heo5df36032014-03-19 10:23:54 -04001661 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001662 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001663 return -EBUSY;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001664 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665
Tejun Heob4e0eea2016-02-22 22:25:46 -05001666 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001667 struct cgroup_root *src_root = ss->root;
1668 struct cgroup *scgrp = &src_root->cgrp;
1669 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001670 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001671
Tejun Heo1ada4832015-09-18 17:54:23 -04001672 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001673
Tejun Heo334c3672016-03-03 09:58:01 -05001674 /* disable from the source */
1675 src_root->subsys_mask &= ~(1 << ssid);
1676 WARN_ON(cgroup_apply_control(scgrp));
1677 cgroup_finalize_control(scgrp, 0);
Tejun Heo4df8dc92015-09-18 17:54:23 -04001678
Tejun Heo334c3672016-03-03 09:58:01 -05001679 /* rebind */
Tejun Heo1ada4832015-09-18 17:54:23 -04001680 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1681 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001682 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001683 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001684
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001685 spin_lock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001686 hash_for_each(css_set_table, i, cset, hlist)
1687 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001688 &dcgrp->e_csets[ss->id]);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001689 spin_unlock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001690
Tejun Heobd53d612014-04-23 11:13:16 -04001691 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001692 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001693 if (dst_root == &cgrp_dfl_root) {
1694 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1695 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001696 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001697 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001698 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001699
Tejun Heo334c3672016-03-03 09:58:01 -05001700 ret = cgroup_apply_control(dcgrp);
1701 if (ret)
1702 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1703 ss->name, ret);
1704
Tejun Heo5df36032014-03-19 10:23:54 -04001705 if (ss->bind)
1706 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001707 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708
Tejun Heo1ada4832015-09-18 17:54:23 -04001709 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 return 0;
1711}
1712
Tejun Heo1592c9b2016-12-27 14:49:08 -05001713int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1714 struct kernfs_root *kf_root)
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001715{
Felipe Balbi09be4c82016-05-12 12:34:38 +03001716 int len = 0;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001717 char *buf = NULL;
1718 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1719 struct cgroup *ns_cgroup;
1720
1721 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1722 if (!buf)
1723 return -ENOMEM;
1724
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001725 spin_lock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001726 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1727 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001728 spin_unlock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001729
1730 if (len >= PATH_MAX)
1731 len = -ERANGE;
1732 else if (len > 0) {
1733 seq_escape(sf, buf, " \t\n\\");
1734 len = 0;
1735 }
1736 kfree(buf);
1737 return len;
1738}
1739
Tejun Heo5136f632017-06-27 14:30:28 -04001740static int parse_cgroup_root_flags(char *data, unsigned int *root_flags)
1741{
1742 char *token;
1743
1744 *root_flags = 0;
1745
1746 if (!data)
1747 return 0;
1748
1749 while ((token = strsep(&data, ",")) != NULL) {
1750 if (!strcmp(token, "nsdelegate")) {
1751 *root_flags |= CGRP_ROOT_NS_DELEGATE;
1752 continue;
1753 }
1754
1755 pr_err("cgroup2: unknown option \"%s\"\n", token);
1756 return -EINVAL;
1757 }
1758
1759 return 0;
1760}
1761
1762static void apply_cgroup_root_flags(unsigned int root_flags)
1763{
1764 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
1765 if (root_flags & CGRP_ROOT_NS_DELEGATE)
1766 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
1767 else
1768 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
1769 }
1770}
1771
1772static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
1773{
1774 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
1775 seq_puts(seq, ",nsdelegate");
1776 return 0;
1777}
1778
Tejun Heo2bd59d42014-02-11 11:52:49 -05001779static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780{
Tejun Heo5136f632017-06-27 14:30:28 -04001781 unsigned int root_flags;
1782 int ret;
1783
1784 ret = parse_cgroup_root_flags(data, &root_flags);
1785 if (ret)
1786 return ret;
1787
1788 apply_cgroup_root_flags(root_flags);
1789 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790}
1791
Tejun Heoafeb0f92014-02-13 06:58:39 -05001792/*
1793 * To reduce the fork() overhead for systems that are not actually using
1794 * their cgroups capability, we don't maintain the lists running through
1795 * each css_set to its tasks until we see the list actually used - in other
1796 * words after the first mount.
1797 */
1798static bool use_task_css_set_links __read_mostly;
1799
1800static void cgroup_enable_task_cg_lists(void)
1801{
1802 struct task_struct *p, *g;
1803
Tejun Heoafeb0f92014-02-13 06:58:39 -05001804 /*
1805 * We need tasklist_lock because RCU is not safe against
1806 * while_each_thread(). Besides, a forking task that has passed
1807 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1808 * is not guaranteed to have its child immediately visible in the
1809 * tasklist if we walk through it with RCU.
1810 */
1811 read_lock(&tasklist_lock);
Tejun Heod8742e22018-05-23 11:04:54 -07001812 spin_lock_irq(&css_set_lock);
1813
1814 if (use_task_css_set_links)
1815 goto out_unlock;
1816
1817 use_task_css_set_links = true;
1818
Tejun Heoafeb0f92014-02-13 06:58:39 -05001819 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001820 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1821 task_css_set(p) != &init_css_set);
1822
1823 /*
1824 * We should check if the process is exiting, otherwise
1825 * it will race with cgroup_exit() in that the list
1826 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001827 * Do it while holding siglock so that we don't end up
1828 * racing against cgroup_exit().
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001829 *
1830 * Interrupts were already disabled while acquiring
1831 * the css_set_lock, so we do not need to disable it
1832 * again when acquiring the sighand->siglock here.
Tejun Heoafeb0f92014-02-13 06:58:39 -05001833 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001834 spin_lock(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001835 if (!(p->flags & PF_EXITING)) {
1836 struct css_set *cset = task_css_set(p);
1837
Tejun Heo0de09422015-10-15 16:41:49 -04001838 if (!css_set_populated(cset))
1839 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001840 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001841 get_css_set(cset);
Waiman Long73a72422017-06-13 17:18:01 -04001842 cset->nr_tasks++;
Tejun Heoeaf797a2014-02-25 10:04:03 -05001843 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001844 spin_unlock(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001845 } while_each_thread(g, p);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001846out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001847 spin_unlock_irq(&css_set_lock);
Tejun Heod8742e22018-05-23 11:04:54 -07001848 read_unlock(&tasklist_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001849}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001850
Paul Menagecc31edc2008-10-18 20:28:04 -07001851static void init_cgroup_housekeeping(struct cgroup *cgrp)
1852{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001853 struct cgroup_subsys *ss;
1854 int ssid;
1855
Tejun Heod5c419b2014-05-16 13:22:48 -04001856 INIT_LIST_HEAD(&cgrp->self.sibling);
1857 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001858 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001859 INIT_LIST_HEAD(&cgrp->pidlists);
1860 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001861 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001862 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo454000a2017-05-15 09:34:02 -04001863 cgrp->dom_cgrp = cgrp;
Roman Gushchin1a926e02017-07-28 18:28:44 +01001864 cgrp->max_descendants = INT_MAX;
1865 cgrp->max_depth = INT_MAX;
Tejun Heo8f534702018-04-26 14:29:05 -07001866 INIT_LIST_HEAD(&cgrp->rstat_css_list);
Tejun Heod4ff7492018-04-26 14:29:04 -07001867 prev_cputime_init(&cgrp->prev_cputime);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001868
1869 for_each_subsys(ss, ssid)
1870 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001871
1872 init_waitqueue_head(&cgrp->offline_waitq);
Tejun Heod62beb72016-12-27 14:49:08 -05001873 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001874}
Paul Menagec6d57f32009-09-23 15:56:19 -07001875
Tejun Heo1592c9b2016-12-27 14:49:08 -05001876void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001877{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001878 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001879
Paul Menageddbcc7e2007-10-18 23:39:30 -07001880 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001881 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001882 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001883 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001884 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001885
Paul Menagec6d57f32009-09-23 15:56:19 -07001886 root->flags = opts->flags;
1887 if (opts->release_agent)
Tejun Heo08a77672018-01-09 07:21:15 -08001888 strscpy(root->release_agent_path, opts->release_agent, PATH_MAX);
Paul Menagec6d57f32009-09-23 15:56:19 -07001889 if (opts->name)
Tejun Heo08a77672018-01-09 07:21:15 -08001890 strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001891 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001892 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001893}
1894
Zefan Li9732adc2017-04-19 10:15:59 +08001895int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001896{
Tejun Heod427dfe2014-02-11 11:52:48 -05001897 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001898 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heofa069902016-12-27 14:49:07 -05001899 struct kernfs_syscall_ops *kf_sops;
Tejun Heod427dfe2014-02-11 11:52:48 -05001900 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001901 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001902
Tejun Heod427dfe2014-02-11 11:52:48 -05001903 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001904
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001905 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001906 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001907 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001908 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001909 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001910
Zefan Li9732adc2017-04-19 10:15:59 +08001911 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
1912 ref_flags, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001913 if (ret)
1914 goto out;
1915
Tejun Heod427dfe2014-02-11 11:52:48 -05001916 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001917 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001918 * but that's OK - it can only be increased by someone holding
Tejun Heo04313592016-03-03 09:58:01 -05001919 * cgroup_lock, and that's us. Later rebinding may disable
1920 * controllers on the default hierarchy and thus create new csets,
1921 * which can't be more than the existing ones. Allocate 2x.
Tejun Heod427dfe2014-02-11 11:52:48 -05001922 */
Tejun Heo04313592016-03-03 09:58:01 -05001923 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001924 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001925 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001926
Tejun Heo985ed672014-03-19 10:23:53 -04001927 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001928 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001929 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001930
Tejun Heofa069902016-12-27 14:49:07 -05001931 kf_sops = root == &cgrp_dfl_root ?
1932 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1933
1934 root->kf_root = kernfs_create_root(kf_sops,
Shaohua Liaa818822017-07-12 11:49:51 -07001935 KERNFS_ROOT_CREATE_DEACTIVATED |
1936 KERNFS_ROOT_SUPPORT_EXPORTOP,
Tejun Heo2bd59d42014-02-11 11:52:49 -05001937 root_cgrp);
1938 if (IS_ERR(root->kf_root)) {
1939 ret = PTR_ERR(root->kf_root);
1940 goto exit_root_id;
1941 }
1942 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001943
Tejun Heo334c3672016-03-03 09:58:01 -05001944 ret = css_populate_dir(&root_cgrp->self);
Tejun Heod427dfe2014-02-11 11:52:48 -05001945 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001946 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001947
Tejun Heo5df36032014-03-19 10:23:54 -04001948 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001949 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001950 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001951
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001952 ret = cgroup_bpf_inherit(root_cgrp);
1953 WARN_ON_ONCE(ret);
1954
Tejun Heoed1777d2016-08-10 11:23:44 -04001955 trace_cgroup_setup_root(root);
1956
Tejun Heod427dfe2014-02-11 11:52:48 -05001957 /*
1958 * There must be no failure case after here, since rebinding takes
1959 * care of subsystems' refcounts, which are explicitly dropped in
1960 * the failure exit path.
1961 */
1962 list_add(&root->root_list, &cgroup_roots);
1963 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001964
Tejun Heod427dfe2014-02-11 11:52:48 -05001965 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001966 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001967 * objects.
1968 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001969 spin_lock_irq(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001970 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001971 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001972 if (css_set_populated(cset))
1973 cgroup_update_populated(root_cgrp, true);
1974 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001975 spin_unlock_irq(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001976
Tejun Heod5c419b2014-05-16 13:22:48 -04001977 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001978 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001979
Tejun Heo2bd59d42014-02-11 11:52:49 -05001980 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001981 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001982 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001983
Tejun Heo2bd59d42014-02-11 11:52:49 -05001984destroy_root:
1985 kernfs_destroy_root(root->kf_root);
1986 root->kf_root = NULL;
1987exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001988 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001989cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001990 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001991out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001992 free_cgrp_cset_links(&tmp_links);
1993 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001994}
1995
Tejun Heo1592c9b2016-12-27 14:49:08 -05001996struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
1997 struct cgroup_root *root, unsigned long magic,
1998 struct cgroup_namespace *ns)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001999{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002000 struct dentry *dentry;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002001 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07002002
Tejun Heo633feee32016-12-27 14:49:07 -05002003 dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
Serge Hallyned825712016-01-29 02:54:09 -06002004
Paul Menagec6d57f32009-09-23 15:56:19 -07002005 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002006 * In non-init cgroup namespace, instead of root cgroup's dentry,
2007 * we return the dentry corresponding to the cgroupns->root_cgrp.
Serge Hallyned825712016-01-29 02:54:09 -06002008 */
2009 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
2010 struct dentry *nsdentry;
2011 struct cgroup *cgrp;
2012
2013 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002014 spin_lock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002015
2016 cgrp = cset_cgroup_from_root(ns->root_cset, root);
2017
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002018 spin_unlock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002019 mutex_unlock(&cgroup_mutex);
2020
2021 nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
2022 dput(dentry);
2023 dentry = nsdentry;
2024 }
2025
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002026 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002027 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002028
Tejun Heo633feee32016-12-27 14:49:07 -05002029 return dentry;
2030}
2031
Tejun Heo633feee32016-12-27 14:49:07 -05002032static struct dentry *cgroup_mount(struct file_system_type *fs_type,
2033 int flags, const char *unused_dev_name,
2034 void *data)
2035{
2036 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
2037 struct dentry *dentry;
Tejun Heo5136f632017-06-27 14:30:28 -04002038 int ret;
Tejun Heo633feee32016-12-27 14:49:07 -05002039
2040 get_cgroup_ns(ns);
2041
2042 /* Check if the caller has permission to mount. */
2043 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
2044 put_cgroup_ns(ns);
2045 return ERR_PTR(-EPERM);
2046 }
2047
Li Zefan3a32bd72014-06-30 11:50:59 +08002048 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002049 * The first time anyone tries to mount a cgroup, enable the list
2050 * linking each css_set to its tasks and fix up all existing tasks.
Li Zefan3a32bd72014-06-30 11:50:59 +08002051 */
Tejun Heo633feee32016-12-27 14:49:07 -05002052 if (!use_task_css_set_links)
2053 cgroup_enable_task_cg_lists();
2054
2055 if (fs_type == &cgroup2_fs_type) {
Tejun Heo5136f632017-06-27 14:30:28 -04002056 unsigned int root_flags;
2057
2058 ret = parse_cgroup_root_flags(data, &root_flags);
2059 if (ret) {
Tejun Heo633feee32016-12-27 14:49:07 -05002060 put_cgroup_ns(ns);
Tejun Heo5136f632017-06-27 14:30:28 -04002061 return ERR_PTR(ret);
Tejun Heo633feee32016-12-27 14:49:07 -05002062 }
Tejun Heo5136f632017-06-27 14:30:28 -04002063
Tejun Heo633feee32016-12-27 14:49:07 -05002064 cgrp_dfl_visible = true;
Tejun Heoa590b902017-04-28 15:14:55 -04002065 cgroup_get_live(&cgrp_dfl_root.cgrp);
Tejun Heo633feee32016-12-27 14:49:07 -05002066
2067 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
2068 CGROUP2_SUPER_MAGIC, ns);
Tejun Heo5136f632017-06-27 14:30:28 -04002069 if (!IS_ERR(dentry))
2070 apply_cgroup_root_flags(root_flags);
Tejun Heo633feee32016-12-27 14:49:07 -05002071 } else {
2072 dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
2073 CGROUP_SUPER_MAGIC, ns);
Li Zefan3a32bd72014-06-30 11:50:59 +08002074 }
2075
Serge Hallyned825712016-01-29 02:54:09 -06002076 put_cgroup_ns(ns);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002077 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002078}
2079
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002080static void cgroup_kill_sb(struct super_block *sb)
2081{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002082 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002083 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002084
Tejun Heo9d755d32014-05-14 09:15:02 -04002085 /*
2086 * If @root doesn't have any mounts or children, start killing it.
2087 * This prevents new mounts by disabling percpu_ref_tryget_live().
2088 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002089 *
2090 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002091 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002092 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002093 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002094 cgroup_put(&root->cgrp);
2095 else
2096 percpu_ref_kill(&root->cgrp.self.refcnt);
2097
Tejun Heo2bd59d42014-02-11 11:52:49 -05002098 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002099}
2100
Tejun Heo0a268db2016-12-27 14:49:06 -05002101struct file_system_type cgroup_fs_type = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002102 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002103 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002104 .kill_sb = cgroup_kill_sb,
Serge Hallyn1c537532016-01-29 02:54:11 -06002105 .fs_flags = FS_USERNS_MOUNT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002106};
2107
Tejun Heo67e9c742015-11-16 11:13:34 -05002108static struct file_system_type cgroup2_fs_type = {
2109 .name = "cgroup2",
2110 .mount = cgroup_mount,
2111 .kill_sb = cgroup_kill_sb,
Serge Hallyn1c537532016-01-29 02:54:11 -06002112 .fs_flags = FS_USERNS_MOUNT,
Tejun Heo67e9c742015-11-16 11:13:34 -05002113};
2114
Tejun Heo0a268db2016-12-27 14:49:06 -05002115int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2116 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002117{
2118 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
Aditya Kalia79a9082016-01-29 02:54:06 -06002119
Tejun Heo4c737b42016-08-10 11:23:44 -04002120 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
Aditya Kalia79a9082016-01-29 02:54:06 -06002121}
2122
Tejun Heo4c737b42016-08-10 11:23:44 -04002123int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2124 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002125{
Tejun Heo4c737b42016-08-10 11:23:44 -04002126 int ret;
Aditya Kalia79a9082016-01-29 02:54:06 -06002127
2128 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002129 spin_lock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002130
2131 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2132
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002133 spin_unlock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002134 mutex_unlock(&cgroup_mutex);
2135
2136 return ret;
2137}
2138EXPORT_SYMBOL_GPL(cgroup_path_ns);
2139
Li Zefana043e3b2008-02-23 15:24:09 -08002140/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002141 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002142 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002143 * @buf: the buffer to write the path into
2144 * @buflen: the length of the buffer
2145 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002146 * Determine @task's cgroup on the first (the one with the lowest non-zero
2147 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2148 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2149 * cgroup controller callbacks.
2150 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002151 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002152 */
Tejun Heo4c737b42016-08-10 11:23:44 -04002153int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002154{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002155 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002156 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002157 int hierarchy_id = 1;
Tejun Heo4c737b42016-08-10 11:23:44 -04002158 int ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002159
2160 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002161 spin_lock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002162
Tejun Heo913ffdb2013-07-11 16:34:48 -07002163 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2164
Tejun Heo857a2be2013-04-14 20:50:08 -07002165 if (root) {
2166 cgrp = task_cgroup_from_root(task, root);
Tejun Heo4c737b42016-08-10 11:23:44 -04002167 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002168 } else {
2169 /* if no hierarchy exists, everyone is in "/" */
Tejun Heo4c737b42016-08-10 11:23:44 -04002170 ret = strlcpy(buf, "/", buflen);
Tejun Heo857a2be2013-04-14 20:50:08 -07002171 }
2172
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002173 spin_unlock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002174 mutex_unlock(&cgroup_mutex);
Tejun Heo4c737b42016-08-10 11:23:44 -04002175 return ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002176}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002177EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002178
Tejun Heoadaae5d2015-09-11 15:00:21 -04002179/**
Tejun Heoe595cd72017-01-15 19:03:41 -05002180 * cgroup_migrate_add_task - add a migration target task to a migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002181 * @task: target task
Tejun Heoe595cd72017-01-15 19:03:41 -05002182 * @mgctx: target migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002183 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002184 * Add @task, which is a migration target, to @mgctx->tset. This function
2185 * becomes noop if @task doesn't need to be migrated. @task's css_set
2186 * should have been added as a migration source and @task->cg_list will be
2187 * moved from the css_set's tasks list to mg_tasks one.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002188 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002189static void cgroup_migrate_add_task(struct task_struct *task,
2190 struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002191{
2192 struct css_set *cset;
2193
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002194 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002195
2196 /* @task either already exited or can't exit until the end */
2197 if (task->flags & PF_EXITING)
2198 return;
2199
2200 /* leave @task alone if post_fork() hasn't linked it yet */
2201 if (list_empty(&task->cg_list))
2202 return;
2203
2204 cset = task_css_set(task);
2205 if (!cset->mg_src_cgrp)
2206 return;
2207
Tejun Heo61046722017-07-08 07:17:02 -04002208 mgctx->tset.nr_tasks++;
2209
Tejun Heoadaae5d2015-09-11 15:00:21 -04002210 list_move_tail(&task->cg_list, &cset->mg_tasks);
2211 if (list_empty(&cset->mg_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002212 list_add_tail(&cset->mg_node,
2213 &mgctx->tset.src_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002214 if (list_empty(&cset->mg_dst_cset->mg_node))
Tejun Heod8ebf512017-01-15 19:03:40 -05002215 list_add_tail(&cset->mg_dst_cset->mg_node,
Tejun Heoe595cd72017-01-15 19:03:41 -05002216 &mgctx->tset.dst_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002217}
2218
Tejun Heo2f7ee562011-12-12 18:12:21 -08002219/**
2220 * cgroup_taskset_first - reset taskset and return the first task
2221 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002222 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002223 *
2224 * @tset iteration is initialized and the first task is returned.
2225 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002226struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2227 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002228{
Tejun Heob3dc0942014-02-25 10:04:01 -05002229 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2230 tset->cur_task = NULL;
2231
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002232 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002233}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002234
2235/**
2236 * cgroup_taskset_next - iterate to the next task in taskset
2237 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002238 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002239 *
2240 * Return the next task in @tset. Iteration must have been initialized
2241 * with cgroup_taskset_first().
2242 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002243struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2244 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002245{
Tejun Heob3dc0942014-02-25 10:04:01 -05002246 struct css_set *cset = tset->cur_cset;
2247 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002248
Tejun Heob3dc0942014-02-25 10:04:01 -05002249 while (&cset->mg_node != tset->csets) {
2250 if (!task)
2251 task = list_first_entry(&cset->mg_tasks,
2252 struct task_struct, cg_list);
2253 else
2254 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002255
Tejun Heob3dc0942014-02-25 10:04:01 -05002256 if (&task->cg_list != &cset->mg_tasks) {
2257 tset->cur_cset = cset;
2258 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002259
2260 /*
2261 * This function may be called both before and
2262 * after cgroup_taskset_migrate(). The two cases
2263 * can be distinguished by looking at whether @cset
2264 * has its ->mg_dst_cset set.
2265 */
2266 if (cset->mg_dst_cset)
2267 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2268 else
2269 *dst_cssp = cset->subsys[tset->ssid];
2270
Tejun Heob3dc0942014-02-25 10:04:01 -05002271 return task;
2272 }
2273
2274 cset = list_next_entry(cset, mg_node);
2275 task = NULL;
2276 }
2277
2278 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002279}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002280
2281/**
Tejun Heo37ff9f82016-03-08 11:51:26 -05002282 * cgroup_taskset_migrate - migrate a taskset
Tejun Heoe595cd72017-01-15 19:03:41 -05002283 * @mgctx: migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002284 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002285 * Migrate tasks in @mgctx as setup by migration preparation functions.
Tejun Heo37ff9f82016-03-08 11:51:26 -05002286 * This function fails iff one of the ->can_attach callbacks fails and
Tejun Heoe595cd72017-01-15 19:03:41 -05002287 * guarantees that either all or none of the tasks in @mgctx are migrated.
2288 * @mgctx is consumed regardless of success.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002289 */
Tejun Heobfc2cf62017-01-15 19:03:41 -05002290static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002291{
Tejun Heoe595cd72017-01-15 19:03:41 -05002292 struct cgroup_taskset *tset = &mgctx->tset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002293 struct cgroup_subsys *ss;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002294 struct task_struct *task, *tmp_task;
2295 struct css_set *cset, *tmp_cset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002296 int ssid, failed_ssid, ret;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002297
Tejun Heoadaae5d2015-09-11 15:00:21 -04002298 /* check that we can legitimately attach to the cgroup */
Tejun Heo61046722017-07-08 07:17:02 -04002299 if (tset->nr_tasks) {
2300 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2301 if (ss->can_attach) {
2302 tset->ssid = ssid;
2303 ret = ss->can_attach(tset);
2304 if (ret) {
2305 failed_ssid = ssid;
2306 goto out_cancel_attach;
2307 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002308 }
Tejun Heo61046722017-07-08 07:17:02 -04002309 } while_each_subsys_mask();
2310 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002311
2312 /*
2313 * Now that we're guaranteed success, proceed to move all tasks to
2314 * the new cgroup. There are no failure cases after here, so this
2315 * is the commit point.
2316 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002317 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002318 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002319 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2320 struct css_set *from_cset = task_css_set(task);
2321 struct css_set *to_cset = cset->mg_dst_cset;
2322
2323 get_css_set(to_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002324 to_cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04002325 css_set_move_task(task, from_cset, to_cset, true);
2326 put_css_set_locked(from_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002327 from_cset->nr_tasks--;
Tejun Heof6d7d042015-10-15 16:41:52 -04002328 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002329 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002330 spin_unlock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002331
2332 /*
2333 * Migration is committed, all target tasks are now on dst_csets.
2334 * Nothing is sensitive to fork() after this point. Notify
2335 * controllers that migration is complete.
2336 */
2337 tset->csets = &tset->dst_csets;
2338
Tejun Heo61046722017-07-08 07:17:02 -04002339 if (tset->nr_tasks) {
2340 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2341 if (ss->attach) {
2342 tset->ssid = ssid;
2343 ss->attach(tset);
2344 }
2345 } while_each_subsys_mask();
2346 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002347
2348 ret = 0;
2349 goto out_release_tset;
2350
2351out_cancel_attach:
Tejun Heo61046722017-07-08 07:17:02 -04002352 if (tset->nr_tasks) {
2353 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2354 if (ssid == failed_ssid)
2355 break;
2356 if (ss->cancel_attach) {
2357 tset->ssid = ssid;
2358 ss->cancel_attach(tset);
2359 }
2360 } while_each_subsys_mask();
2361 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002362out_release_tset:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002363 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002364 list_splice_init(&tset->dst_csets, &tset->src_csets);
2365 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2366 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2367 list_del_init(&cset->mg_node);
2368 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002369 spin_unlock_irq(&css_set_lock);
Waiman Longc4fa6c42017-09-21 09:54:13 -04002370
2371 /*
2372 * Re-initialize the cgroup_taskset structure in case it is reused
2373 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2374 * iteration.
2375 */
2376 tset->nr_tasks = 0;
2377 tset->csets = &tset->src_csets;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002378 return ret;
2379}
2380
2381/**
Tejun Heo8cfd8142017-07-21 11:14:51 -04002382 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
Tejun Heo6c694c82016-03-08 11:51:25 -05002383 * @dst_cgrp: destination cgroup to test
2384 *
Tejun Heo8cfd8142017-07-21 11:14:51 -04002385 * On the default hierarchy, except for the mixable, (possible) thread root
2386 * and threaded cgroups, subtree_control must be zero for migration
2387 * destination cgroups with tasks so that child cgroups don't compete
2388 * against tasks.
Tejun Heo6c694c82016-03-08 11:51:25 -05002389 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04002390int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
Tejun Heo6c694c82016-03-08 11:51:25 -05002391{
Tejun Heo8cfd8142017-07-21 11:14:51 -04002392 /* v1 doesn't have any restriction */
2393 if (!cgroup_on_dfl(dst_cgrp))
2394 return 0;
2395
2396 /* verify @dst_cgrp can host resources */
2397 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
2398 return -EOPNOTSUPP;
2399
2400 /* mixables don't care */
2401 if (cgroup_is_mixable(dst_cgrp))
2402 return 0;
2403
2404 /*
2405 * If @dst_cgrp is already or can become a thread root or is
2406 * threaded, it doesn't matter.
2407 */
2408 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
2409 return 0;
2410
2411 /* apply no-internal-process constraint */
2412 if (dst_cgrp->subtree_control)
2413 return -EBUSY;
2414
2415 return 0;
Tejun Heo6c694c82016-03-08 11:51:25 -05002416}
2417
2418/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002419 * cgroup_migrate_finish - cleanup after attach
Tejun Heoe595cd72017-01-15 19:03:41 -05002420 * @mgctx: migration context
Ben Blum74a11662011-05-26 16:25:20 -07002421 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002422 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2423 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002424 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002425void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002426{
Tejun Heoe595cd72017-01-15 19:03:41 -05002427 LIST_HEAD(preloaded);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002428 struct css_set *cset, *tmp_cset;
2429
2430 lockdep_assert_held(&cgroup_mutex);
2431
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002432 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002433
2434 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2435 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2436
2437 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002438 cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002439 cset->mg_dst_cgrp = NULL;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002440 cset->mg_dst_cset = NULL;
2441 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002442 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002443 }
Tejun Heoe595cd72017-01-15 19:03:41 -05002444
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002445 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002446}
2447
2448/**
2449 * cgroup_migrate_add_src - add a migration source css_set
2450 * @src_cset: the source css_set to add
2451 * @dst_cgrp: the destination cgroup
Tejun Heoe595cd72017-01-15 19:03:41 -05002452 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002453 *
2454 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
Tejun Heoe595cd72017-01-15 19:03:41 -05002455 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
Tejun Heo1958d2d2014-02-25 10:04:03 -05002456 * up by cgroup_migrate_finish().
2457 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002458 * This function may be called without holding cgroup_threadgroup_rwsem
2459 * even if the target is a process. Threads may be created and destroyed
2460 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2461 * into play and the preloaded css_sets are guaranteed to cover all
2462 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002463 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002464void cgroup_migrate_add_src(struct css_set *src_cset,
2465 struct cgroup *dst_cgrp,
Tejun Heoe595cd72017-01-15 19:03:41 -05002466 struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002467{
2468 struct cgroup *src_cgrp;
2469
2470 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002471 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002472
Tejun Heo2b021cb2016-03-15 20:43:04 -04002473 /*
2474 * If ->dead, @src_set is associated with one or more dead cgroups
2475 * and doesn't contain any migratable tasks. Ignore it early so
2476 * that the rest of migration path doesn't get confused by it.
2477 */
2478 if (src_cset->dead)
2479 return;
2480
Tejun Heo1958d2d2014-02-25 10:04:03 -05002481 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2482
Tejun Heo1958d2d2014-02-25 10:04:03 -05002483 if (!list_empty(&src_cset->mg_preload_node))
2484 return;
2485
2486 WARN_ON(src_cset->mg_src_cgrp);
Tejun Heoe4857982016-03-08 11:51:26 -05002487 WARN_ON(src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002488 WARN_ON(!list_empty(&src_cset->mg_tasks));
2489 WARN_ON(!list_empty(&src_cset->mg_node));
2490
2491 src_cset->mg_src_cgrp = src_cgrp;
Tejun Heoe4857982016-03-08 11:51:26 -05002492 src_cset->mg_dst_cgrp = dst_cgrp;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002493 get_css_set(src_cset);
Tejun Heoe595cd72017-01-15 19:03:41 -05002494 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002495}
2496
2497/**
2498 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heoe595cd72017-01-15 19:03:41 -05002499 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002500 *
Tejun Heoe4857982016-03-08 11:51:26 -05002501 * Tasks are about to be moved and all the source css_sets have been
Tejun Heoe595cd72017-01-15 19:03:41 -05002502 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2503 * pins all destination css_sets, links each to its source, and append them
2504 * to @mgctx->preloaded_dst_csets.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002505 *
2506 * This function must be called after cgroup_migrate_add_src() has been
2507 * called on each migration source css_set. After migration is performed
2508 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
Tejun Heoe595cd72017-01-15 19:03:41 -05002509 * @mgctx.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002510 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002511int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002512{
Tejun Heof817de92014-04-23 11:13:16 -04002513 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002514
2515 lockdep_assert_held(&cgroup_mutex);
2516
2517 /* look up the dst cset for each src cset and link it to src */
Tejun Heoe595cd72017-01-15 19:03:41 -05002518 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2519 mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002520 struct css_set *dst_cset;
Tejun Heobfc2cf62017-01-15 19:03:41 -05002521 struct cgroup_subsys *ss;
2522 int ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002523
Tejun Heoe4857982016-03-08 11:51:26 -05002524 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002525 if (!dst_cset)
2526 goto err;
2527
2528 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002529
2530 /*
2531 * If src cset equals dst, it's noop. Drop the src.
2532 * cgroup_migrate() will skip the cset too. Note that we
2533 * can't handle src == dst as some nodes are used by both.
2534 */
2535 if (src_cset == dst_cset) {
2536 src_cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002537 src_cset->mg_dst_cgrp = NULL;
Tejun Heof817de92014-04-23 11:13:16 -04002538 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002539 put_css_set(src_cset);
2540 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002541 continue;
2542 }
2543
Tejun Heo1958d2d2014-02-25 10:04:03 -05002544 src_cset->mg_dst_cset = dst_cset;
2545
2546 if (list_empty(&dst_cset->mg_preload_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002547 list_add_tail(&dst_cset->mg_preload_node,
2548 &mgctx->preloaded_dst_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002549 else
Zefan Lia25eb522014-09-19 16:51:00 +08002550 put_css_set(dst_cset);
Tejun Heobfc2cf62017-01-15 19:03:41 -05002551
2552 for_each_subsys(ss, ssid)
2553 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2554 mgctx->ss_mask |= 1 << ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002555 }
2556
Tejun Heo1958d2d2014-02-25 10:04:03 -05002557 return 0;
2558err:
Tejun Heoe595cd72017-01-15 19:03:41 -05002559 cgroup_migrate_finish(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002560 return -ENOMEM;
2561}
2562
2563/**
2564 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002565 * @leader: the leader of the process or the task to migrate
2566 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heoe595cd72017-01-15 19:03:41 -05002567 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002568 *
Tejun Heo37ff9f82016-03-08 11:51:26 -05002569 * Migrate a process or task denoted by @leader. If migrating a process,
2570 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2571 * responsible for invoking cgroup_migrate_add_src() and
Tejun Heo1958d2d2014-02-25 10:04:03 -05002572 * cgroup_migrate_prepare_dst() on the targets before invoking this
2573 * function and following up with cgroup_migrate_finish().
2574 *
2575 * As long as a controller's ->can_attach() doesn't fail, this function is
2576 * guaranteed to succeed. This means that, excluding ->can_attach()
2577 * failure, when migrating multiple targets, the success or failure can be
2578 * decided for all targets by invoking group_migrate_prepare_dst() before
2579 * actually starting migrating.
2580 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002581int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heobfc2cf62017-01-15 19:03:41 -05002582 struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002583{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002584 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002585
2586 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002587 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2588 * already PF_EXITING could be freed from underneath us unless we
2589 * take an rcu_read_lock.
2590 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002591 spin_lock_irq(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002592 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002593 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002594 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002595 cgroup_migrate_add_task(task, mgctx);
Li Zefan081aa452013-03-13 09:17:09 +08002596 if (!threadgroup)
2597 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002598 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002599 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002600 spin_unlock_irq(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002601
Tejun Heobfc2cf62017-01-15 19:03:41 -05002602 return cgroup_migrate_execute(mgctx);
Ben Blum74a11662011-05-26 16:25:20 -07002603}
2604
Tejun Heo1958d2d2014-02-25 10:04:03 -05002605/**
2606 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2607 * @dst_cgrp: the cgroup to attach to
2608 * @leader: the task or the leader of the threadgroup to be attached
2609 * @threadgroup: attach the whole threadgroup?
2610 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002611 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002612 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002613int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2614 bool threadgroup)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002615{
Tejun Heoe595cd72017-01-15 19:03:41 -05002616 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002617 struct task_struct *task;
2618 int ret;
2619
Tejun Heo8cfd8142017-07-21 11:14:51 -04002620 ret = cgroup_migrate_vet_dst(dst_cgrp);
2621 if (ret)
2622 return ret;
Tejun Heo6c694c82016-03-08 11:51:25 -05002623
Tejun Heo1958d2d2014-02-25 10:04:03 -05002624 /* look up all src csets */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002625 spin_lock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002626 rcu_read_lock();
2627 task = leader;
2628 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002629 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002630 if (!threadgroup)
2631 break;
2632 } while_each_thread(leader, task);
2633 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002634 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002635
2636 /* prepare dst csets and commit */
Tejun Heoe595cd72017-01-15 19:03:41 -05002637 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002638 if (!ret)
Tejun Heobfc2cf62017-01-15 19:03:41 -05002639 ret = cgroup_migrate(leader, threadgroup, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002640
Tejun Heoe595cd72017-01-15 19:03:41 -05002641 cgroup_migrate_finish(&mgctx);
Tejun Heoed1777d2016-08-10 11:23:44 -04002642
2643 if (!ret)
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04002644 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
Tejun Heoed1777d2016-08-10 11:23:44 -04002645
Tejun Heo1958d2d2014-02-25 10:04:03 -05002646 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002647}
2648
Tejun Heo715c8092017-05-15 09:34:00 -04002649struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
2650 __acquires(&cgroup_threadgroup_rwsem)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002651{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002652 struct task_struct *tsk;
Tejun Heoacbef752014-05-13 12:16:22 -04002653 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002654
Tejun Heoacbef752014-05-13 12:16:22 -04002655 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
Tejun Heo715c8092017-05-15 09:34:00 -04002656 return ERR_PTR(-EINVAL);
Ben Blum74a11662011-05-26 16:25:20 -07002657
Tejun Heo3014dde2015-09-16 13:03:02 -04002658 percpu_down_write(&cgroup_threadgroup_rwsem);
Tejun Heo715c8092017-05-15 09:34:00 -04002659
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002660 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002661 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002662 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002663 if (!tsk) {
Tejun Heo715c8092017-05-15 09:34:00 -04002664 tsk = ERR_PTR(-ESRCH);
2665 goto out_unlock_threadgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002666 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002667 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002668 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002669 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002670
2671 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002672 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002673
2674 /*
Tejun Heo77f88792017-03-16 16:54:24 -04002675 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2676 * If userland migrates such a kthread to a non-root cgroup, it can
2677 * become trapped in a cpuset, or RT kthread may be born in a
2678 * cgroup with no rt_runtime allocated. Just say no.
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002679 */
Tejun Heo77f88792017-03-16 16:54:24 -04002680 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
Tejun Heo715c8092017-05-15 09:34:00 -04002681 tsk = ERR_PTR(-EINVAL);
2682 goto out_unlock_threadgroup;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002683 }
2684
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002685 get_task_struct(tsk);
Tejun Heo715c8092017-05-15 09:34:00 -04002686 goto out_unlock_rcu;
Tejun Heocd3d0952011-12-12 18:12:21 -08002687
Tejun Heo715c8092017-05-15 09:34:00 -04002688out_unlock_threadgroup:
2689 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo3014dde2015-09-16 13:03:02 -04002690out_unlock_rcu:
2691 rcu_read_unlock();
Tejun Heo715c8092017-05-15 09:34:00 -04002692 return tsk;
2693}
2694
2695void cgroup_procs_write_finish(struct task_struct *task)
2696 __releases(&cgroup_threadgroup_rwsem)
2697{
2698 struct cgroup_subsys *ss;
2699 int ssid;
2700
2701 /* release reference from cgroup_procs_write_start() */
2702 put_task_struct(task);
2703
Tejun Heo3014dde2015-09-16 13:03:02 -04002704 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002705 for_each_subsys(ss, ssid)
2706 if (ss->post_attach)
2707 ss->post_attach();
Paul Menageaf351022008-07-25 01:47:01 -07002708}
2709
Tejun Heo6e5c8302016-02-22 22:25:47 -05002710static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002711{
2712 struct cgroup_subsys *ss;
2713 bool printed = false;
2714 int ssid;
2715
Tejun Heob4e0eea2016-02-22 22:25:46 -05002716 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002717 if (printed)
2718 seq_putc(seq, ' ');
2719 seq_printf(seq, "%s", ss->name);
2720 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002721 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002722 if (printed)
2723 seq_putc(seq, '\n');
2724}
2725
Tejun Heof8f22e52014-04-23 11:13:16 -04002726/* show controllers which are enabled from the parent */
2727static int cgroup_controllers_show(struct seq_file *seq, void *v)
2728{
2729 struct cgroup *cgrp = seq_css(seq)->cgroup;
2730
Tejun Heo5531dc92016-03-03 09:57:58 -05002731 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04002732 return 0;
2733}
2734
2735/* show controllers which are enabled for a given cgroup's children */
2736static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2737{
2738 struct cgroup *cgrp = seq_css(seq)->cgroup;
2739
Tejun Heo667c2492014-07-08 18:02:56 -04002740 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002741 return 0;
2742}
2743
2744/**
2745 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2746 * @cgrp: root of the subtree to update csses for
2747 *
Tejun Heo54962602016-03-03 09:58:01 -05002748 * @cgrp's control masks have changed and its subtree's css associations
2749 * need to be updated accordingly. This function looks up all css_sets
2750 * which are attached to the subtree, creates the matching updated css_sets
2751 * and migrates the tasks to the new ones.
Tejun Heof8f22e52014-04-23 11:13:16 -04002752 */
2753static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2754{
Tejun Heoe595cd72017-01-15 19:03:41 -05002755 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo54962602016-03-03 09:58:01 -05002756 struct cgroup_subsys_state *d_css;
2757 struct cgroup *dsct;
Tejun Heof8f22e52014-04-23 11:13:16 -04002758 struct css_set *src_cset;
2759 int ret;
2760
Tejun Heof8f22e52014-04-23 11:13:16 -04002761 lockdep_assert_held(&cgroup_mutex);
2762
Tejun Heo3014dde2015-09-16 13:03:02 -04002763 percpu_down_write(&cgroup_threadgroup_rwsem);
2764
Tejun Heof8f22e52014-04-23 11:13:16 -04002765 /* look up all csses currently attached to @cgrp's subtree */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002766 spin_lock_irq(&css_set_lock);
Tejun Heo54962602016-03-03 09:58:01 -05002767 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002768 struct cgrp_cset_link *link;
2769
Tejun Heo54962602016-03-03 09:58:01 -05002770 list_for_each_entry(link, &dsct->cset_links, cset_link)
Tejun Heoe595cd72017-01-15 19:03:41 -05002771 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002772 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002773 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002774
2775 /* NULL dst indicates self on default hierarchy */
Tejun Heoe595cd72017-01-15 19:03:41 -05002776 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002777 if (ret)
2778 goto out_finish;
2779
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002780 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002781 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002782 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002783
Tejun Heo10265072015-09-11 15:00:22 -04002784 /* all tasks in src_csets need to be migrated */
2785 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
Tejun Heoe595cd72017-01-15 19:03:41 -05002786 cgroup_migrate_add_task(task, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002787 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002788 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002789
Tejun Heobfc2cf62017-01-15 19:03:41 -05002790 ret = cgroup_migrate_execute(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002791out_finish:
Tejun Heoe595cd72017-01-15 19:03:41 -05002792 cgroup_migrate_finish(&mgctx);
Tejun Heo3014dde2015-09-16 13:03:02 -04002793 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002794 return ret;
2795}
2796
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002797/**
Tejun Heo945ba192016-03-03 09:58:00 -05002798 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
Tejun Heoce3f1d92016-03-03 09:57:59 -05002799 * @cgrp: root of the target subtree
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002800 *
2801 * Because css offlining is asynchronous, userland may try to re-enable a
Tejun Heo945ba192016-03-03 09:58:00 -05002802 * controller while the previous css is still around. This function grabs
2803 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002804 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002805void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
Tejun Heo945ba192016-03-03 09:58:00 -05002806 __acquires(&cgroup_mutex)
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002807{
2808 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002809 struct cgroup_subsys_state *d_css;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002810 struct cgroup_subsys *ss;
2811 int ssid;
2812
Tejun Heo945ba192016-03-03 09:58:00 -05002813restart:
2814 mutex_lock(&cgroup_mutex);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002815
Tejun Heoce3f1d92016-03-03 09:57:59 -05002816 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002817 for_each_subsys(ss, ssid) {
2818 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2819 DEFINE_WAIT(wait);
2820
Tejun Heoce3f1d92016-03-03 09:57:59 -05002821 if (!css || !percpu_ref_is_dying(&css->refcnt))
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002822 continue;
2823
Tejun Heoa590b902017-04-28 15:14:55 -04002824 cgroup_get_live(dsct);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002825 prepare_to_wait(&dsct->offline_waitq, &wait,
2826 TASK_UNINTERRUPTIBLE);
2827
2828 mutex_unlock(&cgroup_mutex);
2829 schedule();
2830 finish_wait(&dsct->offline_waitq, &wait);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002831
2832 cgroup_put(dsct);
Tejun Heo945ba192016-03-03 09:58:00 -05002833 goto restart;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002834 }
2835 }
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002836}
2837
Tejun Heo12b3bb62016-03-03 09:57:59 -05002838/**
Tejun Heo15a27c32016-03-03 09:57:59 -05002839 * cgroup_save_control - save control masks of a subtree
2840 * @cgrp: root of the target subtree
2841 *
2842 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2843 * prefixed fields for @cgrp's subtree including @cgrp itself.
2844 */
2845static void cgroup_save_control(struct cgroup *cgrp)
2846{
2847 struct cgroup *dsct;
2848 struct cgroup_subsys_state *d_css;
2849
2850 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2851 dsct->old_subtree_control = dsct->subtree_control;
2852 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
2853 }
2854}
2855
2856/**
2857 * cgroup_propagate_control - refresh control masks of a subtree
2858 * @cgrp: root of the target subtree
2859 *
2860 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2861 * ->subtree_control and propagate controller availability through the
2862 * subtree so that descendants don't have unavailable controllers enabled.
2863 */
2864static void cgroup_propagate_control(struct cgroup *cgrp)
2865{
2866 struct cgroup *dsct;
2867 struct cgroup_subsys_state *d_css;
2868
2869 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2870 dsct->subtree_control &= cgroup_control(dsct);
Tejun Heo5ced2512016-03-03 09:58:01 -05002871 dsct->subtree_ss_mask =
2872 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2873 cgroup_ss_mask(dsct));
Tejun Heo15a27c32016-03-03 09:57:59 -05002874 }
2875}
2876
2877/**
2878 * cgroup_restore_control - restore control masks of a subtree
2879 * @cgrp: root of the target subtree
2880 *
2881 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2882 * prefixed fields for @cgrp's subtree including @cgrp itself.
2883 */
2884static void cgroup_restore_control(struct cgroup *cgrp)
2885{
2886 struct cgroup *dsct;
2887 struct cgroup_subsys_state *d_css;
2888
2889 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2890 dsct->subtree_control = dsct->old_subtree_control;
2891 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
2892 }
2893}
2894
Tejun Heof6d635ad2016-03-08 11:51:26 -05002895static bool css_visible(struct cgroup_subsys_state *css)
2896{
2897 struct cgroup_subsys *ss = css->ss;
2898 struct cgroup *cgrp = css->cgroup;
2899
2900 if (cgroup_control(cgrp) & (1 << ss->id))
2901 return true;
2902 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2903 return false;
2904 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2905}
2906
Tejun Heo15a27c32016-03-03 09:57:59 -05002907/**
Tejun Heobdb53bd2016-03-03 09:57:59 -05002908 * cgroup_apply_control_enable - enable or show csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05002909 * @cgrp: root of the target subtree
Tejun Heobdb53bd2016-03-03 09:57:59 -05002910 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05002911 * Walk @cgrp's subtree and create new csses or make the existing ones
Tejun Heobdb53bd2016-03-03 09:57:59 -05002912 * visible. A css is created invisible if it's being implicitly enabled
2913 * through dependency. An invisible css is made visible when the userland
2914 * explicitly enables it.
2915 *
2916 * Returns 0 on success, -errno on failure. On failure, csses which have
2917 * been processed already aren't cleaned up. The caller is responsible for
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08002918 * cleaning up with cgroup_apply_control_disable().
Tejun Heobdb53bd2016-03-03 09:57:59 -05002919 */
2920static int cgroup_apply_control_enable(struct cgroup *cgrp)
2921{
2922 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002923 struct cgroup_subsys_state *d_css;
Tejun Heobdb53bd2016-03-03 09:57:59 -05002924 struct cgroup_subsys *ss;
2925 int ssid, ret;
2926
Tejun Heoce3f1d92016-03-03 09:57:59 -05002927 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heobdb53bd2016-03-03 09:57:59 -05002928 for_each_subsys(ss, ssid) {
2929 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2930
Tejun Heo945ba192016-03-03 09:58:00 -05002931 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2932
Tejun Heobdb53bd2016-03-03 09:57:59 -05002933 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
2934 continue;
2935
2936 if (!css) {
2937 css = css_create(dsct, ss);
2938 if (IS_ERR(css))
2939 return PTR_ERR(css);
2940 }
2941
Tejun Heof6d635ad2016-03-08 11:51:26 -05002942 if (css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05002943 ret = css_populate_dir(css);
Tejun Heobdb53bd2016-03-03 09:57:59 -05002944 if (ret)
2945 return ret;
2946 }
2947 }
2948 }
2949
2950 return 0;
2951}
2952
2953/**
Tejun Heo12b3bb62016-03-03 09:57:59 -05002954 * cgroup_apply_control_disable - kill or hide csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05002955 * @cgrp: root of the target subtree
Tejun Heo12b3bb62016-03-03 09:57:59 -05002956 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05002957 * Walk @cgrp's subtree and kill and hide csses so that they match
Tejun Heo12b3bb62016-03-03 09:57:59 -05002958 * cgroup_ss_mask() and cgroup_visible_mask().
2959 *
2960 * A css is hidden when the userland requests it to be disabled while other
2961 * subsystems are still depending on it. The css must not actively control
2962 * resources and be in the vanilla state if it's made visible again later.
2963 * Controllers which may be depended upon should provide ->css_reset() for
2964 * this purpose.
2965 */
2966static void cgroup_apply_control_disable(struct cgroup *cgrp)
2967{
2968 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002969 struct cgroup_subsys_state *d_css;
Tejun Heo12b3bb62016-03-03 09:57:59 -05002970 struct cgroup_subsys *ss;
2971 int ssid;
2972
Tejun Heoce3f1d92016-03-03 09:57:59 -05002973 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05002974 for_each_subsys(ss, ssid) {
2975 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2976
Tejun Heo945ba192016-03-03 09:58:00 -05002977 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2978
Tejun Heo12b3bb62016-03-03 09:57:59 -05002979 if (!css)
2980 continue;
2981
Tejun Heo334c3672016-03-03 09:58:01 -05002982 if (css->parent &&
2983 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05002984 kill_css(css);
Tejun Heof6d635ad2016-03-08 11:51:26 -05002985 } else if (!css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05002986 css_clear_dir(css);
Tejun Heo12b3bb62016-03-03 09:57:59 -05002987 if (ss->css_reset)
2988 ss->css_reset(css);
2989 }
2990 }
2991 }
2992}
2993
Tejun Heof7b28142016-03-03 09:58:00 -05002994/**
2995 * cgroup_apply_control - apply control mask updates to the subtree
2996 * @cgrp: root of the target subtree
2997 *
2998 * subsystems can be enabled and disabled in a subtree using the following
2999 * steps.
3000 *
3001 * 1. Call cgroup_save_control() to stash the current state.
3002 * 2. Update ->subtree_control masks in the subtree as desired.
3003 * 3. Call cgroup_apply_control() to apply the changes.
3004 * 4. Optionally perform other related operations.
3005 * 5. Call cgroup_finalize_control() to finish up.
3006 *
3007 * This function implements step 3 and propagates the mask changes
3008 * throughout @cgrp's subtree, updates csses accordingly and perform
3009 * process migrations.
3010 */
3011static int cgroup_apply_control(struct cgroup *cgrp)
3012{
3013 int ret;
3014
3015 cgroup_propagate_control(cgrp);
3016
3017 ret = cgroup_apply_control_enable(cgrp);
3018 if (ret)
3019 return ret;
3020
3021 /*
3022 * At this point, cgroup_e_css() results reflect the new csses
3023 * making the following cgroup_update_dfl_csses() properly update
3024 * css associations of all tasks in the subtree.
3025 */
3026 ret = cgroup_update_dfl_csses(cgrp);
3027 if (ret)
3028 return ret;
3029
3030 return 0;
3031}
3032
3033/**
3034 * cgroup_finalize_control - finalize control mask update
3035 * @cgrp: root of the target subtree
3036 * @ret: the result of the update
3037 *
3038 * Finalize control mask update. See cgroup_apply_control() for more info.
3039 */
3040static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3041{
3042 if (ret) {
3043 cgroup_restore_control(cgrp);
3044 cgroup_propagate_control(cgrp);
3045 }
3046
3047 cgroup_apply_control_disable(cgrp);
3048}
3049
Tejun Heo8cfd8142017-07-21 11:14:51 -04003050static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
3051{
3052 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
3053
3054 /* if nothing is getting enabled, nothing to worry about */
3055 if (!enable)
3056 return 0;
3057
3058 /* can @cgrp host any resources? */
3059 if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
3060 return -EOPNOTSUPP;
3061
3062 /* mixables don't care */
3063 if (cgroup_is_mixable(cgrp))
3064 return 0;
3065
3066 if (domain_enable) {
3067 /* can't enable domain controllers inside a thread subtree */
3068 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3069 return -EOPNOTSUPP;
3070 } else {
3071 /*
3072 * Threaded controllers can handle internal competitions
3073 * and are always allowed inside a (prospective) thread
3074 * subtree.
3075 */
3076 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3077 return 0;
3078 }
3079
3080 /*
3081 * Controllers can't be enabled for a cgroup with tasks to avoid
3082 * child cgroups competing against tasks.
3083 */
3084 if (cgroup_has_tasks(cgrp))
3085 return -EBUSY;
3086
3087 return 0;
3088}
3089
Tejun Heof8f22e52014-04-23 11:13:16 -04003090/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003091static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3092 char *buf, size_t nbytes,
3093 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003094{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003095 u16 enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04003096 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003097 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003098 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003099 int ssid, ret;
3100
3101 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003102 * Parse input - space separated list of subsystem names prefixed
3103 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003104 */
Tejun Heo451af502014-05-13 12:16:21 -04003105 buf = strstrip(buf);
3106 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003107 if (tok[0] == '\0')
3108 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003109 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003110 if (!cgroup_ssid_enabled(ssid) ||
3111 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003112 continue;
3113
3114 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003115 enable |= 1 << ssid;
3116 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003117 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003118 disable |= 1 << ssid;
3119 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003120 } else {
3121 return -EINVAL;
3122 }
3123 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003124 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003125 if (ssid == CGROUP_SUBSYS_COUNT)
3126 return -EINVAL;
3127 }
3128
Tejun Heo945ba192016-03-03 09:58:00 -05003129 cgrp = cgroup_kn_lock_live(of->kn, true);
Tejun Heoa9746d82014-05-13 12:19:22 -04003130 if (!cgrp)
3131 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003132
3133 for_each_subsys(ss, ssid) {
3134 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003135 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003136 enable &= ~(1 << ssid);
3137 continue;
3138 }
3139
Tejun Heo5531dc92016-03-03 09:57:58 -05003140 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003141 ret = -ENOENT;
3142 goto out_unlock;
3143 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003144 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003145 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003146 disable &= ~(1 << ssid);
3147 continue;
3148 }
3149
3150 /* a child has it enabled? */
3151 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003152 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003153 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003154 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003155 }
3156 }
3157 }
3158 }
3159
3160 if (!enable && !disable) {
3161 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003162 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003163 }
3164
Tejun Heo8cfd8142017-07-21 11:14:51 -04003165 ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3166 if (ret)
Tejun Heo27f26752017-07-16 21:44:18 -04003167 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003168
Tejun Heo15a27c32016-03-03 09:57:59 -05003169 /* save and update control masks and prepare csses */
3170 cgroup_save_control(cgrp);
Tejun Heoc29adf22014-07-08 18:02:56 -04003171
Tejun Heo15a27c32016-03-03 09:57:59 -05003172 cgrp->subtree_control |= enable;
3173 cgrp->subtree_control &= ~disable;
Tejun Heof63070d2014-07-08 18:02:57 -04003174
Tejun Heof7b28142016-03-03 09:58:00 -05003175 ret = cgroup_apply_control(cgrp);
Tejun Heof7b28142016-03-03 09:58:00 -05003176 cgroup_finalize_control(cgrp, ret);
Tejun Heo3c745412017-07-23 08:14:15 -04003177 if (ret)
3178 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003179
3180 kernfs_activate(cgrp->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003181out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003182 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003183 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003184}
3185
Tejun Heoc705a002017-07-25 13:20:18 -04003186/**
3187 * cgroup_enable_threaded - make @cgrp threaded
3188 * @cgrp: the target cgroup
3189 *
3190 * Called when "threaded" is written to the cgroup.type interface file and
3191 * tries to make @cgrp threaded and join the parent's resource domain.
3192 * This function is never called on the root cgroup as cgroup.type doesn't
3193 * exist on it.
3194 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04003195static int cgroup_enable_threaded(struct cgroup *cgrp)
3196{
3197 struct cgroup *parent = cgroup_parent(cgrp);
3198 struct cgroup *dom_cgrp = parent->dom_cgrp;
3199 int ret;
3200
3201 lockdep_assert_held(&cgroup_mutex);
3202
3203 /* noop if already threaded */
3204 if (cgroup_is_threaded(cgrp))
3205 return 0;
3206
Tejun Heod1897c92018-02-21 11:39:22 -08003207 /*
3208 * If @cgroup is populated or has domain controllers enabled, it
3209 * can't be switched. While the below cgroup_can_be_thread_root()
3210 * test can catch the same conditions, that's only when @parent is
3211 * not mixable, so let's check it explicitly.
3212 */
3213 if (cgroup_is_populated(cgrp) ||
3214 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3215 return -EOPNOTSUPP;
3216
Tejun Heo8cfd8142017-07-21 11:14:51 -04003217 /* we're joining the parent's domain, ensure its validity */
3218 if (!cgroup_is_valid_domain(dom_cgrp) ||
3219 !cgroup_can_be_thread_root(dom_cgrp))
3220 return -EOPNOTSUPP;
3221
3222 /*
Tejun Heo8cfd8142017-07-21 11:14:51 -04003223 * The following shouldn't cause actual migrations and should
3224 * always succeed.
3225 */
3226 cgroup_save_control(cgrp);
3227
3228 cgrp->dom_cgrp = dom_cgrp;
3229 ret = cgroup_apply_control(cgrp);
3230 if (!ret)
3231 parent->nr_threaded_children++;
3232 else
3233 cgrp->dom_cgrp = cgrp;
3234
3235 cgroup_finalize_control(cgrp, ret);
3236 return ret;
3237}
3238
3239static int cgroup_type_show(struct seq_file *seq, void *v)
3240{
3241 struct cgroup *cgrp = seq_css(seq)->cgroup;
3242
3243 if (cgroup_is_threaded(cgrp))
3244 seq_puts(seq, "threaded\n");
3245 else if (!cgroup_is_valid_domain(cgrp))
3246 seq_puts(seq, "domain invalid\n");
3247 else if (cgroup_is_thread_root(cgrp))
3248 seq_puts(seq, "domain threaded\n");
3249 else
3250 seq_puts(seq, "domain\n");
3251
3252 return 0;
3253}
3254
3255static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
3256 size_t nbytes, loff_t off)
3257{
3258 struct cgroup *cgrp;
3259 int ret;
3260
3261 /* only switching to threaded mode is supported */
3262 if (strcmp(strstrip(buf), "threaded"))
3263 return -EINVAL;
3264
3265 cgrp = cgroup_kn_lock_live(of->kn, false);
3266 if (!cgrp)
3267 return -ENOENT;
3268
3269 /* threaded can only be enabled */
3270 ret = cgroup_enable_threaded(cgrp);
3271
3272 cgroup_kn_unlock(of->kn);
3273 return ret ?: nbytes;
3274}
3275
Roman Gushchin1a926e02017-07-28 18:28:44 +01003276static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
3277{
3278 struct cgroup *cgrp = seq_css(seq)->cgroup;
3279 int descendants = READ_ONCE(cgrp->max_descendants);
3280
3281 if (descendants == INT_MAX)
3282 seq_puts(seq, "max\n");
3283 else
3284 seq_printf(seq, "%d\n", descendants);
3285
3286 return 0;
3287}
3288
3289static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
3290 char *buf, size_t nbytes, loff_t off)
3291{
3292 struct cgroup *cgrp;
3293 int descendants;
3294 ssize_t ret;
3295
3296 buf = strstrip(buf);
3297 if (!strcmp(buf, "max")) {
3298 descendants = INT_MAX;
3299 } else {
3300 ret = kstrtoint(buf, 0, &descendants);
3301 if (ret)
3302 return ret;
3303 }
3304
Dan Carpenter696b98f2017-08-09 13:25:21 +03003305 if (descendants < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003306 return -ERANGE;
3307
3308 cgrp = cgroup_kn_lock_live(of->kn, false);
3309 if (!cgrp)
3310 return -ENOENT;
3311
3312 cgrp->max_descendants = descendants;
3313
3314 cgroup_kn_unlock(of->kn);
3315
3316 return nbytes;
3317}
3318
3319static int cgroup_max_depth_show(struct seq_file *seq, void *v)
3320{
3321 struct cgroup *cgrp = seq_css(seq)->cgroup;
3322 int depth = READ_ONCE(cgrp->max_depth);
3323
3324 if (depth == INT_MAX)
3325 seq_puts(seq, "max\n");
3326 else
3327 seq_printf(seq, "%d\n", depth);
3328
3329 return 0;
3330}
3331
3332static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
3333 char *buf, size_t nbytes, loff_t off)
3334{
3335 struct cgroup *cgrp;
3336 ssize_t ret;
3337 int depth;
3338
3339 buf = strstrip(buf);
3340 if (!strcmp(buf, "max")) {
3341 depth = INT_MAX;
3342 } else {
3343 ret = kstrtoint(buf, 0, &depth);
3344 if (ret)
3345 return ret;
3346 }
3347
Dan Carpenter696b98f2017-08-09 13:25:21 +03003348 if (depth < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003349 return -ERANGE;
3350
3351 cgrp = cgroup_kn_lock_live(of->kn, false);
3352 if (!cgrp)
3353 return -ENOENT;
3354
3355 cgrp->max_depth = depth;
3356
3357 cgroup_kn_unlock(of->kn);
3358
3359 return nbytes;
3360}
3361
Tejun Heo4a07c222015-09-18 17:54:22 -04003362static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003363{
Tejun Heo4a07c222015-09-18 17:54:22 -04003364 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003365 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003366 return 0;
3367}
3368
Tejun Heo3e489302017-08-11 05:49:01 -07003369static int cgroup_stat_show(struct seq_file *seq, void *v)
Roman Gushchinec392252017-08-02 17:55:31 +01003370{
3371 struct cgroup *cgroup = seq_css(seq)->cgroup;
3372
3373 seq_printf(seq, "nr_descendants %d\n",
3374 cgroup->nr_descendants);
3375 seq_printf(seq, "nr_dying_descendants %d\n",
3376 cgroup->nr_dying_descendants);
3377
3378 return 0;
3379}
3380
Tejun Heod41bf8c2017-10-23 16:18:27 -07003381static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
3382 struct cgroup *cgrp, int ssid)
3383{
3384 struct cgroup_subsys *ss = cgroup_subsys[ssid];
3385 struct cgroup_subsys_state *css;
3386 int ret;
3387
3388 if (!ss->css_extra_stat_show)
3389 return 0;
3390
3391 css = cgroup_tryget_css(cgrp, ss);
3392 if (!css)
3393 return 0;
3394
3395 ret = ss->css_extra_stat_show(seq, css);
3396 css_put(css);
3397 return ret;
3398}
3399
3400static int cpu_stat_show(struct seq_file *seq, void *v)
3401{
Tejun Heoc3ba1322017-10-30 08:13:14 -07003402 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
Tejun Heod41bf8c2017-10-23 16:18:27 -07003403 int ret = 0;
3404
Tejun Heod4ff7492018-04-26 14:29:04 -07003405 cgroup_base_stat_cputime_show(seq);
Tejun Heod41bf8c2017-10-23 16:18:27 -07003406#ifdef CONFIG_CGROUP_SCHED
3407 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
3408#endif
3409 return ret;
3410}
3411
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003412static int cgroup_file_open(struct kernfs_open_file *of)
3413{
3414 struct cftype *cft = of->kn->priv;
3415
3416 if (cft->open)
3417 return cft->open(of);
3418 return 0;
3419}
3420
3421static void cgroup_file_release(struct kernfs_open_file *of)
3422{
3423 struct cftype *cft = of->kn->priv;
3424
3425 if (cft->release)
3426 cft->release(of);
3427}
3428
Tejun Heo2bd59d42014-02-11 11:52:49 -05003429static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3430 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003431{
Tejun Heo5136f632017-06-27 14:30:28 -04003432 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003433 struct cgroup *cgrp = of->kn->parent->priv;
3434 struct cftype *cft = of->kn->priv;
3435 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003436 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003437
Tejun Heo5136f632017-06-27 14:30:28 -04003438 /*
3439 * If namespaces are delegation boundaries, disallow writes to
3440 * files in an non-init namespace root from inside the namespace
3441 * except for the files explicitly marked delegatable -
3442 * cgroup.procs and cgroup.subtree_control.
3443 */
3444 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
3445 !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
3446 ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp)
3447 return -EPERM;
3448
Tejun Heob4168642014-05-13 12:16:21 -04003449 if (cft->write)
3450 return cft->write(of, buf, nbytes, off);
3451
Tejun Heo2bd59d42014-02-11 11:52:49 -05003452 /*
3453 * kernfs guarantees that a file isn't deleted with operations in
3454 * flight, which means that the matching css is and stays alive and
3455 * doesn't need to be pinned. The RCU locking is not necessary
3456 * either. It's just for the convenience of using cgroup_css().
3457 */
3458 rcu_read_lock();
3459 css = cgroup_css(cgrp, cft->ss);
3460 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003461
Tejun Heo451af502014-05-13 12:16:21 -04003462 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003463 unsigned long long v;
3464 ret = kstrtoull(buf, 0, &v);
3465 if (!ret)
3466 ret = cft->write_u64(css, cft, v);
3467 } else if (cft->write_s64) {
3468 long long v;
3469 ret = kstrtoll(buf, 0, &v);
3470 if (!ret)
3471 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003472 } else {
3473 ret = -EINVAL;
3474 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003475
Tejun Heoa742c592013-12-05 12:28:03 -05003476 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003477}
3478
Tejun Heo6612f052013-12-05 12:28:04 -05003479static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003480{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003481 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003482}
3483
3484static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3485{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003486 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003487}
3488
3489static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3490{
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003491 if (seq_cft(seq)->seq_stop)
3492 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003493}
3494
3495static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3496{
Tejun Heo7da11272013-12-05 12:28:04 -05003497 struct cftype *cft = seq_cft(m);
3498 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003499
Tejun Heo2da8ca82013-12-05 12:28:04 -05003500 if (cft->seq_show)
3501 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003502
Tejun Heo896f5192013-12-05 12:28:04 -05003503 if (cft->read_u64)
3504 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3505 else if (cft->read_s64)
3506 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3507 else
3508 return -EINVAL;
3509 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003510}
3511
Tejun Heo2bd59d42014-02-11 11:52:49 -05003512static struct kernfs_ops cgroup_kf_single_ops = {
3513 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003514 .open = cgroup_file_open,
3515 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003516 .write = cgroup_file_write,
3517 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003518};
3519
Tejun Heo2bd59d42014-02-11 11:52:49 -05003520static struct kernfs_ops cgroup_kf_ops = {
3521 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003522 .open = cgroup_file_open,
3523 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003524 .write = cgroup_file_write,
3525 .seq_start = cgroup_seqfile_start,
3526 .seq_next = cgroup_seqfile_next,
3527 .seq_stop = cgroup_seqfile_stop,
3528 .seq_show = cgroup_seqfile_show,
3529};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003530
Tejun Heo49957f82014-04-07 16:44:47 -04003531/* set uid and gid of cgroup dirs and files to that of the creator */
3532static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3533{
3534 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3535 .ia_uid = current_fsuid(),
3536 .ia_gid = current_fsgid(), };
3537
3538 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3539 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3540 return 0;
3541
3542 return kernfs_setattr(kn, &iattr);
3543}
3544
Tejun Heob12e3582018-04-26 14:29:04 -07003545static void cgroup_file_notify_timer(struct timer_list *timer)
3546{
3547 cgroup_file_notify(container_of(timer, struct cgroup_file,
3548 notify_timer));
3549}
3550
Tejun Heo4df8dc92015-09-18 17:54:23 -04003551static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3552 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003553{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003554 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003555 struct kernfs_node *kn;
3556 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003557 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003558
Tejun Heo2bd59d42014-02-11 11:52:49 -05003559#ifdef CONFIG_DEBUG_LOCK_ALLOC
3560 key = &cft->lockdep_key;
3561#endif
3562 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3563 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003564 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003565 if (IS_ERR(kn))
3566 return PTR_ERR(kn);
3567
3568 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003569 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003570 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003571 return ret;
3572 }
3573
Tejun Heo6f60ead2015-09-18 17:54:23 -04003574 if (cft->file_offset) {
3575 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3576
Tejun Heob12e3582018-04-26 14:29:04 -07003577 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
3578
Tejun Heo34c06252015-11-05 00:12:24 -05003579 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003580 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003581 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003582 }
3583
Tejun Heof8f22e52014-04-23 11:13:16 -04003584 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003585}
3586
Tejun Heob1f28d32013-06-28 16:24:10 -07003587/**
3588 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003589 * @css: the target css
3590 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003591 * @cfts: array of cftypes to be added
3592 * @is_add: whether to add or remove
3593 *
3594 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003595 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003596 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003597static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3598 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003599 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003600{
Tejun Heo6732ed82015-09-18 17:54:23 -04003601 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003602 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003603
Tejun Heo01f64742014-05-13 12:19:23 -04003604 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003605
Tejun Heo6732ed82015-09-18 17:54:23 -04003606restart:
3607 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003608 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003609 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003610 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003611 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003612 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003613 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003614 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003615 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003616 continue;
3617
Li Zefan2739d3c2013-01-21 18:18:33 +08003618 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003619 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003620 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003621 pr_warn("%s: failed to add %s, err=%d\n",
3622 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003623 cft_end = cft;
3624 is_add = false;
3625 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003626 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003627 } else {
3628 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003629 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003630 }
Tejun Heob598dde2016-02-22 22:25:45 -05003631 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003632}
3633
Tejun Heo21a2d342014-02-12 09:29:49 -05003634static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003635{
Tejun Heo2bb566c2013-08-08 20:11:23 -04003636 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003637 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003638 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003639 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003640
Tejun Heo01f64742014-05-13 12:19:23 -04003641 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003642
Li Zefane8c82d22013-06-18 18:48:37 +08003643 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003644 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003645 struct cgroup *cgrp = css->cgroup;
3646
Tejun Heo88cb04b2016-03-03 09:57:58 -05003647 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08003648 continue;
3649
Tejun Heo4df8dc92015-09-18 17:54:23 -04003650 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003651 if (ret)
3652 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003653 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003654
3655 if (is_add && !ret)
3656 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003657 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003658}
3659
Tejun Heo2da440a2014-02-11 11:52:48 -05003660static void cgroup_exit_cftypes(struct cftype *cfts)
3661{
3662 struct cftype *cft;
3663
Tejun Heo2bd59d42014-02-11 11:52:49 -05003664 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3665 /* free copy for custom atomic_write_len, see init_cftypes() */
3666 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3667 kfree(cft->kf_ops);
3668 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003669 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003670
3671 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003672 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003673 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003674}
3675
Tejun Heo2bd59d42014-02-11 11:52:49 -05003676static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003677{
3678 struct cftype *cft;
3679
Tejun Heo2bd59d42014-02-11 11:52:49 -05003680 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3681 struct kernfs_ops *kf_ops;
3682
Tejun Heo0adb0702014-02-12 09:29:48 -05003683 WARN_ON(cft->ss || cft->kf_ops);
3684
Tejun Heo2bd59d42014-02-11 11:52:49 -05003685 if (cft->seq_start)
3686 kf_ops = &cgroup_kf_ops;
3687 else
3688 kf_ops = &cgroup_kf_single_ops;
3689
3690 /*
3691 * Ugh... if @cft wants a custom max_write_len, we need to
3692 * make a copy of kf_ops to set its atomic_write_len.
3693 */
3694 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3695 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3696 if (!kf_ops) {
3697 cgroup_exit_cftypes(cfts);
3698 return -ENOMEM;
3699 }
3700 kf_ops->atomic_write_len = cft->max_write_len;
3701 }
3702
3703 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003704 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003705 }
3706
3707 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003708}
3709
Tejun Heo21a2d342014-02-12 09:29:49 -05003710static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3711{
Tejun Heo01f64742014-05-13 12:19:23 -04003712 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003713
3714 if (!cfts || !cfts[0].ss)
3715 return -ENOENT;
3716
3717 list_del(&cfts->node);
3718 cgroup_apply_cftypes(cfts, false);
3719 cgroup_exit_cftypes(cfts);
3720 return 0;
3721}
3722
Tejun Heo8e3f6542012-04-01 12:09:55 -07003723/**
Tejun Heo80b13582014-02-12 09:29:48 -05003724 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3725 * @cfts: zero-length name terminated array of cftypes
3726 *
3727 * Unregister @cfts. Files described by @cfts are removed from all
3728 * existing cgroups and all future cgroups won't have them either. This
3729 * function can be called anytime whether @cfts' subsys is attached or not.
3730 *
3731 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3732 * registered.
3733 */
3734int cgroup_rm_cftypes(struct cftype *cfts)
3735{
Tejun Heo21a2d342014-02-12 09:29:49 -05003736 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003737
Tejun Heo01f64742014-05-13 12:19:23 -04003738 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003739 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003740 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003741 return ret;
3742}
3743
3744/**
3745 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3746 * @ss: target cgroup subsystem
3747 * @cfts: zero-length name terminated array of cftypes
3748 *
3749 * Register @cfts to @ss. Files described by @cfts are created for all
3750 * existing cgroups to which @ss is attached and all future cgroups will
3751 * have them too. This function can be called anytime whether @ss is
3752 * attached or not.
3753 *
3754 * Returns 0 on successful registration, -errno on failure. Note that this
3755 * function currently returns 0 as long as @cfts registration is successful
3756 * even if some file creation attempts on existing cgroups fail.
3757 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003758static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003759{
Tejun Heo9ccece82013-06-28 16:24:11 -07003760 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003761
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003762 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003763 return 0;
3764
Li Zefandc5736e2014-02-17 10:41:50 +08003765 if (!cfts || cfts[0].name[0] == '\0')
3766 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003767
Tejun Heo2bd59d42014-02-11 11:52:49 -05003768 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003769 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003770 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003771
Tejun Heo01f64742014-05-13 12:19:23 -04003772 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003773
Tejun Heo0adb0702014-02-12 09:29:48 -05003774 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003775 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003776 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003777 cgroup_rm_cftypes_locked(cfts);
3778
Tejun Heo01f64742014-05-13 12:19:23 -04003779 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003780 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003781}
Tejun Heo79578622012-04-01 12:09:56 -07003782
3783/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003784 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3785 * @ss: target cgroup subsystem
3786 * @cfts: zero-length name terminated array of cftypes
3787 *
3788 * Similar to cgroup_add_cftypes() but the added files are only used for
3789 * the default hierarchy.
3790 */
3791int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3792{
3793 struct cftype *cft;
3794
3795 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003796 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003797 return cgroup_add_cftypes(ss, cfts);
3798}
3799
3800/**
3801 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3802 * @ss: target cgroup subsystem
3803 * @cfts: zero-length name terminated array of cftypes
3804 *
3805 * Similar to cgroup_add_cftypes() but the added files are only used for
3806 * the legacy hierarchies.
3807 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003808int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3809{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003810 struct cftype *cft;
3811
Tejun Heoe4b70372015-10-15 17:00:43 -04003812 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3813 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003814 return cgroup_add_cftypes(ss, cfts);
3815}
3816
Li Zefana043e3b2008-02-23 15:24:09 -08003817/**
Tejun Heo34c06252015-11-05 00:12:24 -05003818 * cgroup_file_notify - generate a file modified event for a cgroup_file
3819 * @cfile: target cgroup_file
3820 *
3821 * @cfile must have been obtained by setting cftype->file_offset.
3822 */
3823void cgroup_file_notify(struct cgroup_file *cfile)
3824{
3825 unsigned long flags;
3826
3827 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
Tejun Heob12e3582018-04-26 14:29:04 -07003828 if (cfile->kn) {
3829 unsigned long last = cfile->notified_at;
3830 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
3831
3832 if (time_in_range(jiffies, last, next)) {
3833 timer_reduce(&cfile->notify_timer, next);
3834 } else {
3835 kernfs_notify(cfile->kn);
3836 cfile->notified_at = jiffies;
3837 }
3838 }
Tejun Heo34c06252015-11-05 00:12:24 -05003839 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3840}
3841
3842/**
Tejun Heo492eb212013-08-08 20:11:25 -04003843 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003844 * @pos: the current position (%NULL to initiate traversal)
3845 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003846 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003847 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003848 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003849 * that @parent and @pos are accessible. The next sibling is guaranteed to
3850 * be returned regardless of their states.
3851 *
3852 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3853 * css which finished ->css_online() is guaranteed to be visible in the
3854 * future iterations and will stay visible until the last reference is put.
3855 * A css which hasn't finished ->css_online() or already finished
3856 * ->css_offline() may show up during traversal. It's each subsystem's
3857 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003858 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003859struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3860 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003861{
Tejun Heoc2931b72014-05-16 13:22:51 -04003862 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003863
Tejun Heo8353da12014-05-13 12:19:23 -04003864 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003865
3866 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003867 * @pos could already have been unlinked from the sibling list.
3868 * Once a cgroup is removed, its ->sibling.next is no longer
3869 * updated when its next sibling changes. CSS_RELEASED is set when
3870 * @pos is taken off list, at which time its next pointer is valid,
3871 * and, as releases are serialized, the one pointed to by the next
3872 * pointer is guaranteed to not have started release yet. This
3873 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3874 * critical section, the one pointed to by its next pointer is
3875 * guaranteed to not have finished its RCU grace period even if we
3876 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003877 *
Tejun Heode3f0342014-05-16 13:22:49 -04003878 * If @pos has CSS_RELEASED set, its next pointer can't be
3879 * dereferenced; however, as each css is given a monotonically
3880 * increasing unique serial number and always appended to the
3881 * sibling list, the next one can be found by walking the parent's
3882 * children until the first css with higher serial number than
3883 * @pos's. While this path can be slower, it happens iff iteration
3884 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003885 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003886 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003887 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3888 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3889 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003890 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003891 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003892 if (next->serial_nr > pos->serial_nr)
3893 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003894 }
3895
Tejun Heo3b281af2014-04-23 11:13:15 -04003896 /*
3897 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003898 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003899 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003900 if (&next->sibling != &parent->children)
3901 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003902 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003903}
Tejun Heo53fa5262013-05-24 10:55:38 +09003904
3905/**
Tejun Heo492eb212013-08-08 20:11:25 -04003906 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003907 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003908 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003909 *
Tejun Heo492eb212013-08-08 20:11:25 -04003910 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003911 * to visit for pre-order traversal of @root's descendants. @root is
3912 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003913 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003914 * While this function requires cgroup_mutex or RCU read locking, it
3915 * doesn't require the whole traversal to be contained in a single critical
3916 * section. This function will return the correct next descendant as long
3917 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003918 *
3919 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3920 * css which finished ->css_online() is guaranteed to be visible in the
3921 * future iterations and will stay visible until the last reference is put.
3922 * A css which hasn't finished ->css_online() or already finished
3923 * ->css_offline() may show up during traversal. It's each subsystem's
3924 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003925 */
Tejun Heo492eb212013-08-08 20:11:25 -04003926struct cgroup_subsys_state *
3927css_next_descendant_pre(struct cgroup_subsys_state *pos,
3928 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003929{
Tejun Heo492eb212013-08-08 20:11:25 -04003930 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003931
Tejun Heo8353da12014-05-13 12:19:23 -04003932 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003933
Tejun Heobd8815a2013-08-08 20:11:27 -04003934 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003935 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003936 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003937
3938 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003939 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003940 if (next)
3941 return next;
3942
3943 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003944 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003945 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003946 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003947 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003948 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003949 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003950
3951 return NULL;
3952}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003953
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003954/**
Tejun Heo492eb212013-08-08 20:11:25 -04003955 * css_rightmost_descendant - return the rightmost descendant of a css
3956 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003957 *
Tejun Heo492eb212013-08-08 20:11:25 -04003958 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3959 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003960 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003961 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003962 * While this function requires cgroup_mutex or RCU read locking, it
3963 * doesn't require the whole traversal to be contained in a single critical
3964 * section. This function will return the correct rightmost descendant as
3965 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003966 */
Tejun Heo492eb212013-08-08 20:11:25 -04003967struct cgroup_subsys_state *
3968css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003969{
Tejun Heo492eb212013-08-08 20:11:25 -04003970 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003971
Tejun Heo8353da12014-05-13 12:19:23 -04003972 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003973
3974 do {
3975 last = pos;
3976 /* ->prev isn't RCU safe, walk ->next till the end */
3977 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003978 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003979 pos = tmp;
3980 } while (pos);
3981
3982 return last;
3983}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003984
Tejun Heo492eb212013-08-08 20:11:25 -04003985static struct cgroup_subsys_state *
3986css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003987{
Tejun Heo492eb212013-08-08 20:11:25 -04003988 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003989
3990 do {
3991 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003992 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003993 } while (pos);
3994
3995 return last;
3996}
3997
3998/**
Tejun Heo492eb212013-08-08 20:11:25 -04003999 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004000 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004001 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004002 *
Tejun Heo492eb212013-08-08 20:11:25 -04004003 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004004 * to visit for post-order traversal of @root's descendants. @root is
4005 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004006 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004007 * While this function requires cgroup_mutex or RCU read locking, it
4008 * doesn't require the whole traversal to be contained in a single critical
4009 * section. This function will return the correct next descendant as long
4010 * as both @pos and @cgroup are accessible and @pos is a descendant of
4011 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04004012 *
4013 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4014 * css which finished ->css_online() is guaranteed to be visible in the
4015 * future iterations and will stay visible until the last reference is put.
4016 * A css which hasn't finished ->css_online() or already finished
4017 * ->css_offline() may show up during traversal. It's each subsystem's
4018 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004019 */
Tejun Heo492eb212013-08-08 20:11:25 -04004020struct cgroup_subsys_state *
4021css_next_descendant_post(struct cgroup_subsys_state *pos,
4022 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004023{
Tejun Heo492eb212013-08-08 20:11:25 -04004024 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004025
Tejun Heo8353da12014-05-13 12:19:23 -04004026 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004027
Tejun Heo58b79a92013-09-06 15:31:08 -04004028 /* if first iteration, visit leftmost descendant which may be @root */
4029 if (!pos)
4030 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004031
Tejun Heobd8815a2013-08-08 20:11:27 -04004032 /* if we visited @root, we're done */
4033 if (pos == root)
4034 return NULL;
4035
Tejun Heo574bd9f2012-11-09 09:12:29 -08004036 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004037 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004038 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04004039 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004040
4041 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004042 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004043}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004044
Tejun Heof3d46502014-05-16 13:22:52 -04004045/**
4046 * css_has_online_children - does a css have online children
4047 * @css: the target css
4048 *
4049 * Returns %true if @css has any online children; otherwise, %false. This
4050 * function can be called from any context but the caller is responsible
4051 * for synchronizing against on/offlining as necessary.
4052 */
4053bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04004054{
Tejun Heof3d46502014-05-16 13:22:52 -04004055 struct cgroup_subsys_state *child;
4056 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04004057
4058 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04004059 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08004060 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04004061 ret = true;
4062 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04004063 }
4064 }
4065 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04004066 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08004067}
4068
Tejun Heo450ee0c2017-05-15 09:34:03 -04004069static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4070{
4071 struct list_head *l;
4072 struct cgrp_cset_link *link;
4073 struct css_set *cset;
4074
4075 lockdep_assert_held(&css_set_lock);
4076
4077 /* find the next threaded cset */
4078 if (it->tcset_pos) {
4079 l = it->tcset_pos->next;
4080
4081 if (l != it->tcset_head) {
4082 it->tcset_pos = l;
4083 return container_of(l, struct css_set,
4084 threaded_csets_node);
4085 }
4086
4087 it->tcset_pos = NULL;
4088 }
4089
4090 /* find the next cset */
4091 l = it->cset_pos;
4092 l = l->next;
4093 if (l == it->cset_head) {
4094 it->cset_pos = NULL;
4095 return NULL;
4096 }
4097
4098 if (it->ss) {
4099 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4100 } else {
4101 link = list_entry(l, struct cgrp_cset_link, cset_link);
4102 cset = link->cset;
4103 }
4104
4105 it->cset_pos = l;
4106
4107 /* initialize threaded css_set walking */
4108 if (it->flags & CSS_TASK_ITER_THREADED) {
4109 if (it->cur_dcset)
4110 put_css_set_locked(it->cur_dcset);
4111 it->cur_dcset = cset;
4112 get_css_set(cset);
4113
4114 it->tcset_head = &cset->threaded_csets;
4115 it->tcset_pos = &cset->threaded_csets;
4116 }
4117
4118 return cset;
4119}
4120
Tejun Heo0942eee2013-08-08 20:11:26 -04004121/**
Tejun Heoecb9d532015-10-15 16:41:52 -04004122 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04004123 * @it: the iterator to advance
4124 *
4125 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04004126 */
Tejun Heoecb9d532015-10-15 16:41:52 -04004127static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04004128{
Tejun Heod5158762013-08-08 20:11:26 -04004129 struct css_set *cset;
4130
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004131 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004132
Tejun Heod5158762013-08-08 20:11:26 -04004133 /* Advance to the next non-empty css_set */
4134 do {
Tejun Heo450ee0c2017-05-15 09:34:03 -04004135 cset = css_task_iter_next_css_set(it);
4136 if (!cset) {
Tejun Heoecb9d532015-10-15 16:41:52 -04004137 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04004138 return;
4139 }
Tejun Heo0de09422015-10-15 16:41:49 -04004140 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05004141
Tejun Heoc7561122014-02-25 10:04:01 -05004142 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004143 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05004144 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004145 it->task_pos = cset->mg_tasks.next;
4146
4147 it->tasks_head = &cset->tasks;
4148 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04004149
4150 /*
4151 * We don't keep css_sets locked across iteration steps and thus
4152 * need to take steps to ensure that iteration can be resumed after
4153 * the lock is re-acquired. Iteration is performed at two levels -
4154 * css_sets and tasks in them.
4155 *
4156 * Once created, a css_set never leaves its cgroup lists, so a
4157 * pinned css_set is guaranteed to stay put and we can resume
4158 * iteration afterwards.
4159 *
4160 * Tasks may leave @cset across iteration steps. This is resolved
4161 * by registering each iterator with the css_set currently being
4162 * walked and making css_set_move_task() advance iterators whose
4163 * next task is leaving.
4164 */
4165 if (it->cur_cset) {
4166 list_del(&it->iters_node);
4167 put_css_set_locked(it->cur_cset);
4168 }
4169 get_css_set(cset);
4170 it->cur_cset = cset;
4171 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04004172}
4173
Tejun Heoecb9d532015-10-15 16:41:52 -04004174static void css_task_iter_advance(struct css_task_iter *it)
4175{
Tejun Heo74d08332017-12-20 07:09:19 -08004176 struct list_head *next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004177
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004178 lockdep_assert_held(&css_set_lock);
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004179repeat:
Tejun Heoecb9d532015-10-15 16:41:52 -04004180 /*
4181 * Advance iterator to find next entry. cset->tasks is consumed
4182 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
4183 * next cset.
4184 */
Tejun Heo74d08332017-12-20 07:09:19 -08004185 next = it->task_pos->next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004186
Tejun Heo74d08332017-12-20 07:09:19 -08004187 if (next == it->tasks_head)
4188 next = it->mg_tasks_head->next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004189
Tejun Heo74d08332017-12-20 07:09:19 -08004190 if (next == it->mg_tasks_head)
Tejun Heoecb9d532015-10-15 16:41:52 -04004191 css_task_iter_advance_css_set(it);
4192 else
Tejun Heo74d08332017-12-20 07:09:19 -08004193 it->task_pos = next;
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004194
4195 /* if PROCS, skip over tasks which aren't group leaders */
4196 if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos &&
4197 !thread_group_leader(list_entry(it->task_pos, struct task_struct,
4198 cg_list)))
4199 goto repeat;
Tejun Heoecb9d532015-10-15 16:41:52 -04004200}
4201
Tejun Heo0942eee2013-08-08 20:11:26 -04004202/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004203 * css_task_iter_start - initiate task iteration
4204 * @css: the css to walk tasks of
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004205 * @flags: CSS_TASK_ITER_* flags
Tejun Heo0942eee2013-08-08 20:11:26 -04004206 * @it: the task iterator to use
4207 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004208 * Initiate iteration through the tasks of @css. The caller can call
4209 * css_task_iter_next() to walk through the tasks until the function
4210 * returns NULL. On completion of iteration, css_task_iter_end() must be
4211 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04004212 */
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004213void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
Tejun Heo72ec7022013-08-08 20:11:26 -04004214 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004215{
Tejun Heo56fde9e2014-02-13 06:58:38 -05004216 /* no one should try to iterate before mounting cgroups */
4217 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07004218
Tejun Heoed27b9f2015-10-15 16:41:52 -04004219 memset(it, 0, sizeof(*it));
4220
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004221 spin_lock_irq(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004222
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004223 it->ss = css->ss;
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004224 it->flags = flags;
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004225
4226 if (it->ss)
4227 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4228 else
4229 it->cset_pos = &css->cgroup->cset_links;
4230
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004231 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004232
Tejun Heoecb9d532015-10-15 16:41:52 -04004233 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004234
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004235 spin_unlock_irq(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004236}
Paul Menage817929e2007-10-18 23:39:36 -07004237
Tejun Heo0942eee2013-08-08 20:11:26 -04004238/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004239 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004240 * @it: the task iterator being iterated
4241 *
4242 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004243 * initialized via css_task_iter_start(). Returns NULL when the iteration
4244 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004245 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004246struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004247{
Tejun Heod5745672015-10-29 11:43:05 +09004248 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004249 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004250 it->cur_task = NULL;
4251 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004252
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004253 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004254
Tejun Heod5745672015-10-29 11:43:05 +09004255 if (it->task_pos) {
4256 it->cur_task = list_entry(it->task_pos, struct task_struct,
4257 cg_list);
4258 get_task_struct(it->cur_task);
4259 css_task_iter_advance(it);
4260 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004261
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004262 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004263
4264 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004265}
4266
Tejun Heo0942eee2013-08-08 20:11:26 -04004267/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004268 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004269 * @it: the task iterator to finish
4270 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004271 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004272 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004273void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004274{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004275 if (it->cur_cset) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004276 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004277 list_del(&it->iters_node);
4278 put_css_set_locked(it->cur_cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004279 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004280 }
4281
Tejun Heo450ee0c2017-05-15 09:34:03 -04004282 if (it->cur_dcset)
4283 put_css_set(it->cur_dcset);
4284
Tejun Heoed27b9f2015-10-15 16:41:52 -04004285 if (it->cur_task)
4286 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004287}
4288
Tejun Heob4b90a82016-12-27 14:49:04 -05004289static void cgroup_procs_release(struct kernfs_open_file *of)
Tejun Heo8cc99342013-04-07 09:29:50 -07004290{
Tejun Heob4b90a82016-12-27 14:49:04 -05004291 if (of->priv) {
4292 css_task_iter_end(of->priv);
4293 kfree(of->priv);
4294 }
4295}
4296
4297static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4298{
4299 struct kernfs_open_file *of = s->private;
4300 struct css_task_iter *it = of->priv;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004301
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004302 return css_task_iter_next(it);
Tejun Heo8cc99342013-04-07 09:29:50 -07004303}
4304
Tejun Heo8cfd8142017-07-21 11:14:51 -04004305static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
4306 unsigned int iter_flags)
Ben Blumd1d9fd32009-09-23 15:56:28 -07004307{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004308 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004309 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heob4b90a82016-12-27 14:49:04 -05004310 struct css_task_iter *it = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004311
4312 /*
Tejun Heob4b90a82016-12-27 14:49:04 -05004313 * When a seq_file is seeked, it's always traversed sequentially
4314 * from position 0, so we can simply keep iterating on !0 *pos.
Tejun Heo4bac00d2013-11-29 10:42:59 -05004315 */
Tejun Heob4b90a82016-12-27 14:49:04 -05004316 if (!it) {
4317 if (WARN_ON_ONCE((*pos)++))
4318 return ERR_PTR(-EINVAL);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004319
Tejun Heob4b90a82016-12-27 14:49:04 -05004320 it = kzalloc(sizeof(*it), GFP_KERNEL);
4321 if (!it)
4322 return ERR_PTR(-ENOMEM);
4323 of->priv = it;
Tejun Heo450ee0c2017-05-15 09:34:03 -04004324 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heob4b90a82016-12-27 14:49:04 -05004325 } else if (!(*pos)++) {
4326 css_task_iter_end(it);
Tejun Heo450ee0c2017-05-15 09:34:03 -04004327 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004328 }
Tejun Heo4bac00d2013-11-29 10:42:59 -05004329
Tejun Heob4b90a82016-12-27 14:49:04 -05004330 return cgroup_procs_next(s, NULL, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07004331}
4332
Tejun Heo8cfd8142017-07-21 11:14:51 -04004333static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
4334{
4335 struct cgroup *cgrp = seq_css(s)->cgroup;
4336
4337 /*
4338 * All processes of a threaded subtree belong to the domain cgroup
4339 * of the subtree. Only threads can be distributed across the
4340 * subtree. Reject reads on cgroup.procs in the subtree proper.
4341 * They're always empty anyway.
4342 */
4343 if (cgroup_is_threaded(cgrp))
4344 return ERR_PTR(-EOPNOTSUPP);
4345
4346 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
4347 CSS_TASK_ITER_THREADED);
4348}
4349
Tejun Heob4b90a82016-12-27 14:49:04 -05004350static int cgroup_procs_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004351{
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004352 seq_printf(s, "%d\n", task_pid_vnr(v));
Daniel Lezcano97978e62010-10-27 15:33:35 -07004353 return 0;
4354}
4355
Tejun Heo715c8092017-05-15 09:34:00 -04004356static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
4357 struct cgroup *dst_cgrp,
4358 struct super_block *sb)
4359{
4360 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
4361 struct cgroup *com_cgrp = src_cgrp;
4362 struct inode *inode;
4363 int ret;
4364
4365 lockdep_assert_held(&cgroup_mutex);
4366
4367 /* find the common ancestor */
4368 while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
4369 com_cgrp = cgroup_parent(com_cgrp);
4370
4371 /* %current should be authorized to migrate to the common ancestor */
4372 inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn);
4373 if (!inode)
4374 return -ENOMEM;
4375
4376 ret = inode_permission(inode, MAY_WRITE);
4377 iput(inode);
4378 if (ret)
4379 return ret;
4380
4381 /*
4382 * If namespaces are delegation boundaries, %current must be able
4383 * to see both source and destination cgroups from its namespace.
4384 */
4385 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
4386 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
4387 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
4388 return -ENOENT;
4389
4390 return 0;
4391}
4392
4393static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
4394 char *buf, size_t nbytes, loff_t off)
4395{
4396 struct cgroup *src_cgrp, *dst_cgrp;
4397 struct task_struct *task;
4398 ssize_t ret;
4399
4400 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4401 if (!dst_cgrp)
4402 return -ENODEV;
4403
4404 task = cgroup_procs_write_start(buf, true);
4405 ret = PTR_ERR_OR_ZERO(task);
4406 if (ret)
4407 goto out_unlock;
4408
4409 /* find the source cgroup */
4410 spin_lock_irq(&css_set_lock);
4411 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4412 spin_unlock_irq(&css_set_lock);
4413
4414 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4415 of->file->f_path.dentry->d_sb);
4416 if (ret)
4417 goto out_finish;
4418
4419 ret = cgroup_attach_task(dst_cgrp, task, true);
4420
4421out_finish:
4422 cgroup_procs_write_finish(task);
4423out_unlock:
4424 cgroup_kn_unlock(of->kn);
4425
4426 return ret ?: nbytes;
4427}
4428
Tejun Heo8cfd8142017-07-21 11:14:51 -04004429static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
4430{
4431 return __cgroup_procs_start(s, pos, 0);
4432}
4433
4434static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
4435 char *buf, size_t nbytes, loff_t off)
4436{
4437 struct cgroup *src_cgrp, *dst_cgrp;
4438 struct task_struct *task;
4439 ssize_t ret;
4440
4441 buf = strstrip(buf);
4442
4443 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4444 if (!dst_cgrp)
4445 return -ENODEV;
4446
4447 task = cgroup_procs_write_start(buf, false);
4448 ret = PTR_ERR_OR_ZERO(task);
4449 if (ret)
4450 goto out_unlock;
4451
4452 /* find the source cgroup */
4453 spin_lock_irq(&css_set_lock);
4454 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4455 spin_unlock_irq(&css_set_lock);
4456
4457 /* thread migrations follow the cgroup.procs delegation rule */
4458 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4459 of->file->f_path.dentry->d_sb);
4460 if (ret)
4461 goto out_finish;
4462
4463 /* and must be contained in the same domain */
4464 ret = -EOPNOTSUPP;
4465 if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)
4466 goto out_finish;
4467
4468 ret = cgroup_attach_task(dst_cgrp, task, false);
4469
4470out_finish:
4471 cgroup_procs_write_finish(task);
4472out_unlock:
4473 cgroup_kn_unlock(of->kn);
4474
4475 return ret ?: nbytes;
4476}
4477
Tejun Heoa14c6872014-07-15 11:05:09 -04004478/* cgroup core interface files for the default hierarchy */
Tejun Heod62beb72016-12-27 14:49:08 -05004479static struct cftype cgroup_base_files[] = {
Tejun Heoa14c6872014-07-15 11:05:09 -04004480 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004481 .name = "cgroup.type",
4482 .flags = CFTYPE_NOT_ON_ROOT,
4483 .seq_show = cgroup_type_show,
4484 .write = cgroup_type_write,
4485 },
4486 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004487 .name = "cgroup.procs",
Tejun Heo5136f632017-06-27 14:30:28 -04004488 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004489 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heob4b90a82016-12-27 14:49:04 -05004490 .release = cgroup_procs_release,
4491 .seq_start = cgroup_procs_start,
4492 .seq_next = cgroup_procs_next,
4493 .seq_show = cgroup_procs_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004494 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004495 },
4496 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004497 .name = "cgroup.threads",
Roman Gushchin4f584242018-01-10 04:35:12 -08004498 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo8cfd8142017-07-21 11:14:51 -04004499 .release = cgroup_procs_release,
4500 .seq_start = cgroup_threads_start,
4501 .seq_next = cgroup_procs_next,
4502 .seq_show = cgroup_procs_show,
4503 .write = cgroup_threads_write,
4504 },
4505 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004506 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04004507 .seq_show = cgroup_controllers_show,
4508 },
4509 {
4510 .name = "cgroup.subtree_control",
Tejun Heo5136f632017-06-27 14:30:28 -04004511 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heoa14c6872014-07-15 11:05:09 -04004512 .seq_show = cgroup_subtree_control_show,
4513 .write = cgroup_subtree_control_write,
4514 },
4515 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004516 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004517 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004518 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004519 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004520 },
Roman Gushchin1a926e02017-07-28 18:28:44 +01004521 {
4522 .name = "cgroup.max.descendants",
4523 .seq_show = cgroup_max_descendants_show,
4524 .write = cgroup_max_descendants_write,
4525 },
4526 {
4527 .name = "cgroup.max.depth",
4528 .seq_show = cgroup_max_depth_show,
4529 .write = cgroup_max_depth_write,
4530 },
Roman Gushchinec392252017-08-02 17:55:31 +01004531 {
4532 .name = "cgroup.stat",
Tejun Heo3e489302017-08-11 05:49:01 -07004533 .seq_show = cgroup_stat_show,
Roman Gushchinec392252017-08-02 17:55:31 +01004534 },
Tejun Heod41bf8c2017-10-23 16:18:27 -07004535 {
4536 .name = "cpu.stat",
4537 .flags = CFTYPE_NOT_ON_ROOT,
4538 .seq_show = cpu_stat_show,
4539 },
Tejun Heoa14c6872014-07-15 11:05:09 -04004540 { } /* terminate */
4541};
4542
Tejun Heo0c21ead2013-08-13 20:22:51 -04004543/*
4544 * css destruction is four-stage process.
4545 *
4546 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4547 * Implemented in kill_css().
4548 *
4549 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004550 * and thus css_tryget_online() is guaranteed to fail, the css can be
4551 * offlined by invoking offline_css(). After offlining, the base ref is
4552 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004553 *
4554 * 3. When the percpu_ref reaches zero, the only possible remaining
4555 * accessors are inside RCU read sections. css_release() schedules the
4556 * RCU callback.
4557 *
4558 * 4. After the grace period, the css can be freed. Implemented in
4559 * css_free_work_fn().
4560 *
4561 * It is actually hairier because both step 2 and 4 require process context
4562 * and thus involve punting to css->destroy_work adding two additional
4563 * steps to the already complex sequence.
4564 */
Tejun Heo8f36aae2018-03-14 12:45:14 -07004565static void css_free_rwork_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004566{
Tejun Heo8f36aae2018-03-14 12:45:14 -07004567 struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
4568 struct cgroup_subsys_state, destroy_rwork);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004569 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004570 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004571
Tejun Heo9a1049d2014-06-28 08:10:14 -04004572 percpu_ref_exit(&css->refcnt);
4573
Vladimir Davydov01e58652015-02-12 14:59:26 -08004574 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004575 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004576 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004577 int id = css->id;
4578
Vladimir Davydov01e58652015-02-12 14:59:26 -08004579 ss->css_free(css);
4580 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004581 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004582
4583 if (parent)
4584 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004585 } else {
4586 /* cgroup free path */
4587 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heod62beb72016-12-27 14:49:08 -05004588 cgroup1_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004589 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004590
Tejun Heod51f39b2014-05-16 13:22:48 -04004591 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004592 /*
4593 * We get a ref to the parent, and put the ref when
4594 * this cgroup is being freed, so it's guaranteed
4595 * that the parent won't be destroyed before its
4596 * children.
4597 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004598 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004599 kernfs_put(cgrp->kn);
Tejun Heo041cd642017-09-25 08:12:05 -07004600 if (cgroup_on_dfl(cgrp))
Tejun Heoc58632b2018-04-26 14:29:04 -07004601 cgroup_rstat_exit(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04004602 kfree(cgrp);
4603 } else {
4604 /*
4605 * This is root cgroup's refcnt reaching zero,
4606 * which indicates that the root should be
4607 * released.
4608 */
4609 cgroup_destroy_root(cgrp->root);
4610 }
4611 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004612}
4613
Tejun Heo25e15d82014-05-14 09:15:02 -04004614static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004615{
4616 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004617 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004618 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004619 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004620
Tejun Heo1fed1b22014-05-16 13:22:49 -04004621 mutex_lock(&cgroup_mutex);
4622
Tejun Heode3f0342014-05-16 13:22:49 -04004623 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004624 list_del_rcu(&css->sibling);
4625
Tejun Heo9d755d32014-05-14 09:15:02 -04004626 if (ss) {
4627 /* css release path */
Tejun Heo8f534702018-04-26 14:29:05 -07004628 if (!list_empty(&css->rstat_css_node)) {
4629 cgroup_rstat_flush(cgrp);
4630 list_del_rcu(&css->rstat_css_node);
4631 }
4632
Vladimir Davydov01e58652015-02-12 14:59:26 -08004633 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004634 if (ss->css_released)
4635 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004636 } else {
Roman Gushchin0679dee2017-08-02 17:55:29 +01004637 struct cgroup *tcgrp;
4638
Tejun Heo9d755d32014-05-14 09:15:02 -04004639 /* cgroup release path */
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04004640 TRACE_CGROUP_PATH(release, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04004641
Tejun Heo041cd642017-09-25 08:12:05 -07004642 if (cgroup_on_dfl(cgrp))
Tejun Heoc58632b2018-04-26 14:29:04 -07004643 cgroup_rstat_flush(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004644
Roman Gushchin0679dee2017-08-02 17:55:29 +01004645 for (tcgrp = cgroup_parent(cgrp); tcgrp;
4646 tcgrp = cgroup_parent(tcgrp))
4647 tcgrp->nr_dying_descendants--;
4648
Tejun Heo9d755d32014-05-14 09:15:02 -04004649 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4650 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004651
4652 /*
4653 * There are two control paths which try to determine
4654 * cgroup from dentry without going through kernfs -
4655 * cgroupstats_build() and css_tryget_online_from_dir().
4656 * Those are supported by RCU protecting clearing of
4657 * cgrp->kn->priv backpointer.
4658 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004659 if (cgrp->kn)
4660 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4661 NULL);
Daniel Mack30070982016-11-23 16:52:26 +01004662
4663 cgroup_bpf_put(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04004664 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004665
Tejun Heo1fed1b22014-05-16 13:22:49 -04004666 mutex_unlock(&cgroup_mutex);
4667
Tejun Heo8f36aae2018-03-14 12:45:14 -07004668 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4669 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heod3daf282013-06-13 19:39:16 -07004670}
4671
Tejun Heo48ddbe12012-04-01 12:09:56 -07004672static void css_release(struct percpu_ref *ref)
4673{
4674 struct cgroup_subsys_state *css =
4675 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004676
Tejun Heo25e15d82014-05-14 09:15:02 -04004677 INIT_WORK(&css->destroy_work, css_release_work_fn);
4678 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004679}
4680
Tejun Heoddfcada2014-05-04 15:09:14 -04004681static void init_and_link_css(struct cgroup_subsys_state *css,
4682 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004684 lockdep_assert_held(&cgroup_mutex);
4685
Tejun Heoa590b902017-04-28 15:14:55 -04004686 cgroup_get_live(cgrp);
Tejun Heoddfcada2014-05-04 15:09:14 -04004687
Tejun Heod5c419b2014-05-16 13:22:48 -04004688 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004689 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004690 css->ss = ss;
Tejun Heo8fa3b8d2016-05-26 15:42:13 -04004691 css->id = -1;
Tejun Heod5c419b2014-05-16 13:22:48 -04004692 INIT_LIST_HEAD(&css->sibling);
4693 INIT_LIST_HEAD(&css->children);
Tejun Heo8f534702018-04-26 14:29:05 -07004694 INIT_LIST_HEAD(&css->rstat_css_node);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004695 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05004696 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004697
Tejun Heod51f39b2014-05-16 13:22:48 -04004698 if (cgroup_parent(cgrp)) {
4699 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004700 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004701 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004702
Tejun Heo8f534702018-04-26 14:29:05 -07004703 if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush)
4704 list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
4705
Tejun Heoca8bdca2013-08-26 18:40:56 -04004706 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004707}
4708
Li Zefan2a4ac632013-07-31 16:16:40 +08004709/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004710static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004711{
Tejun Heo623f9262013-08-13 11:01:55 -04004712 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004713 int ret = 0;
4714
Tejun Heoa31f2d32012-11-19 08:13:37 -08004715 lockdep_assert_held(&cgroup_mutex);
4716
Tejun Heo92fb9742012-11-19 08:13:38 -08004717 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004718 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004719 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004720 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004721 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05004722
4723 atomic_inc(&css->online_cnt);
4724 if (css->parent)
4725 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04004726 }
Tejun Heob1929db2012-11-19 08:13:38 -08004727 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004728}
4729
Li Zefan2a4ac632013-07-31 16:16:40 +08004730/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004731static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004732{
Tejun Heo623f9262013-08-13 11:01:55 -04004733 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004734
4735 lockdep_assert_held(&cgroup_mutex);
4736
4737 if (!(css->flags & CSS_ONLINE))
4738 return;
4739
Li Zefand7eeac12013-03-12 15:35:59 -07004740 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004741 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004742
Tejun Heoeb954192013-08-08 20:11:23 -04004743 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004744 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004745
4746 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004747}
4748
Tejun Heoc81c925a2013-12-06 15:11:56 -05004749/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004750 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05004751 * @cgrp: the cgroup new css will be associated with
4752 * @ss: the subsys of new css
4753 *
4754 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004755 * css is online and installed in @cgrp. This function doesn't create the
4756 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004757 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004758static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4759 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004760{
Tejun Heod51f39b2014-05-16 13:22:48 -04004761 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004762 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004763 struct cgroup_subsys_state *css;
4764 int err;
4765
Tejun Heoc81c925a2013-12-06 15:11:56 -05004766 lockdep_assert_held(&cgroup_mutex);
4767
Tejun Heo1fed1b22014-05-16 13:22:49 -04004768 css = ss->css_alloc(parent_css);
Tejun Heoe7e15b82016-06-21 13:06:24 -04004769 if (!css)
4770 css = ERR_PTR(-ENOMEM);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004771 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004772 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004773
Tejun Heoddfcada2014-05-04 15:09:14 -04004774 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004775
Tejun Heo2aad2a82014-09-24 13:31:50 -04004776 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004777 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004778 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004779
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004780 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004781 if (err < 0)
Wenwei Taob00c52d2016-05-13 22:59:20 +08004782 goto err_free_css;
Tejun Heo15a4c832014-05-04 15:09:14 -04004783 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004784
Tejun Heo15a4c832014-05-04 15:09:14 -04004785 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004786 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004787 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004788
4789 err = online_css(css);
4790 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004791 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004792
Tejun Heoc81c925a2013-12-06 15:11:56 -05004793 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004794 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004795 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 -04004796 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004797 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004798 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004799 ss->warned_broken_hierarchy = true;
4800 }
4801
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004802 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004803
Tejun Heo1fed1b22014-05-16 13:22:49 -04004804err_list_del:
4805 list_del_rcu(&css->sibling);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004806err_free_css:
Tejun Heo8f534702018-04-26 14:29:05 -07004807 list_del_rcu(&css->rstat_css_node);
Tejun Heo8f36aae2018-03-14 12:45:14 -07004808 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4809 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004810 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004811}
4812
Tejun Heo07cd1292017-01-26 16:47:28 -05004813/*
4814 * The returned cgroup is fully initialized including its control mask, but
4815 * it isn't associated with its kernfs_node and doesn't have the control
4816 * mask applied.
4817 */
Tejun Heoa5bca212016-03-03 09:57:58 -05004818static struct cgroup *cgroup_create(struct cgroup *parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004819{
Tejun Heoa5bca212016-03-03 09:57:58 -05004820 struct cgroup_root *root = parent->root;
Tejun Heoa5bca212016-03-03 09:57:58 -05004821 struct cgroup *cgrp, *tcgrp;
4822 int level = parent->level + 1;
Tejun Heo03970d32016-03-03 09:58:00 -05004823 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004824
Tejun Heo0a950f62012-11-19 09:02:12 -08004825 /* allocate the cgroup and its ID, 0 is reserved for the root */
Kees Cookacafe7e2018-05-08 13:45:50 -07004826 cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)),
4827 GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05004828 if (!cgrp)
4829 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004830
Tejun Heo2aad2a82014-09-24 13:31:50 -04004831 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004832 if (ret)
4833 goto out_free_cgrp;
4834
Tejun Heo041cd642017-09-25 08:12:05 -07004835 if (cgroup_on_dfl(parent)) {
Tejun Heoc58632b2018-04-26 14:29:04 -07004836 ret = cgroup_rstat_init(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004837 if (ret)
4838 goto out_cancel_ref;
4839 }
4840
Li Zefan0ab02ca2014-02-11 16:05:46 +08004841 /*
4842 * Temporarily set the pointer to NULL, so idr_find() won't return
4843 * a half-baked cgroup.
4844 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004845 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004846 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004847 ret = -ENOMEM;
Tejun Heo041cd642017-09-25 08:12:05 -07004848 goto out_stat_exit;
Tejun Heo976c06b2012-11-05 09:16:59 -08004849 }
4850
Paul Menagecc31edc2008-10-18 20:28:04 -07004851 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004852
Tejun Heo9d800df2014-05-14 09:15:00 -04004853 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004854 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004855 cgrp->level = level;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07004856 ret = cgroup_bpf_inherit(cgrp);
4857 if (ret)
4858 goto out_idr_free;
Tejun Heob11cfb52015-11-20 15:55:52 -05004859
Roman Gushchin0679dee2017-08-02 17:55:29 +01004860 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
Tejun Heob11cfb52015-11-20 15:55:52 -05004861 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004862
Roman Gushchin0679dee2017-08-02 17:55:29 +01004863 if (tcgrp != cgrp)
4864 tcgrp->nr_descendants++;
4865 }
4866
Li Zefanb6abdb02008-03-04 14:28:19 -08004867 if (notify_on_release(parent))
4868 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4869
Tejun Heo2260e7f2012-11-19 08:13:38 -08004870 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4871 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004872
Tejun Heo0cb51d72014-05-16 13:22:49 -04004873 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004874
Tejun Heo4e139af2012-11-19 08:13:36 -08004875 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004876 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004877 atomic_inc(&root->nr_cgrps);
Tejun Heoa590b902017-04-28 15:14:55 -04004878 cgroup_get_live(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004879
Tejun Heo0d802552013-12-06 15:11:56 -05004880 /*
4881 * @cgrp is now fully operational. If something fails after this
4882 * point, it'll be released via the normal destruction path.
4883 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004884 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004885
Tejun Heobd53d612014-04-23 11:13:16 -04004886 /*
4887 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004888 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004889 */
Tejun Heo03970d32016-03-03 09:58:00 -05004890 if (!cgroup_on_dfl(cgrp))
Tejun Heo5531dc92016-03-03 09:57:58 -05004891 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo03970d32016-03-03 09:58:00 -05004892
4893 cgroup_propagate_control(cgrp);
4894
Tejun Heoa5bca212016-03-03 09:57:58 -05004895 return cgrp;
4896
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07004897out_idr_free:
4898 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo041cd642017-09-25 08:12:05 -07004899out_stat_exit:
4900 if (cgroup_on_dfl(parent))
Tejun Heoc58632b2018-04-26 14:29:04 -07004901 cgroup_rstat_exit(cgrp);
Tejun Heoa5bca212016-03-03 09:57:58 -05004902out_cancel_ref:
4903 percpu_ref_exit(&cgrp->self.refcnt);
4904out_free_cgrp:
4905 kfree(cgrp);
4906 return ERR_PTR(ret);
Tejun Heoa5bca212016-03-03 09:57:58 -05004907}
4908
Roman Gushchin1a926e02017-07-28 18:28:44 +01004909static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
4910{
4911 struct cgroup *cgroup;
4912 int ret = false;
4913 int level = 1;
4914
4915 lockdep_assert_held(&cgroup_mutex);
4916
4917 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
4918 if (cgroup->nr_descendants >= cgroup->max_descendants)
4919 goto fail;
4920
4921 if (level > cgroup->max_depth)
4922 goto fail;
4923
4924 level++;
4925 }
4926
4927 ret = true;
4928fail:
4929 return ret;
4930}
4931
Tejun Heo1592c9b2016-12-27 14:49:08 -05004932int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
Tejun Heoa5bca212016-03-03 09:57:58 -05004933{
4934 struct cgroup *parent, *cgrp;
Tejun Heoa5bca212016-03-03 09:57:58 -05004935 struct kernfs_node *kn;
Tejun Heo03970d32016-03-03 09:58:00 -05004936 int ret;
Tejun Heoa5bca212016-03-03 09:57:58 -05004937
4938 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4939 if (strchr(name, '\n'))
4940 return -EINVAL;
4941
Tejun Heo945ba192016-03-03 09:58:00 -05004942 parent = cgroup_kn_lock_live(parent_kn, false);
Tejun Heoa5bca212016-03-03 09:57:58 -05004943 if (!parent)
4944 return -ENODEV;
4945
Roman Gushchin1a926e02017-07-28 18:28:44 +01004946 if (!cgroup_check_hierarchy_limits(parent)) {
4947 ret = -EAGAIN;
4948 goto out_unlock;
4949 }
4950
Tejun Heoa5bca212016-03-03 09:57:58 -05004951 cgrp = cgroup_create(parent);
4952 if (IS_ERR(cgrp)) {
4953 ret = PTR_ERR(cgrp);
4954 goto out_unlock;
4955 }
4956
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004957 /* create the directory */
4958 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
4959 if (IS_ERR(kn)) {
4960 ret = PTR_ERR(kn);
4961 goto out_destroy;
4962 }
4963 cgrp->kn = kn;
4964
4965 /*
4966 * This extra ref will be put in cgroup_free_fn() and guarantees
4967 * that @cgrp->kn is always accessible.
4968 */
4969 kernfs_get(kn);
4970
4971 ret = cgroup_kn_set_ugid(kn);
4972 if (ret)
4973 goto out_destroy;
4974
Tejun Heo334c3672016-03-03 09:58:01 -05004975 ret = css_populate_dir(&cgrp->self);
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004976 if (ret)
4977 goto out_destroy;
4978
Tejun Heo03970d32016-03-03 09:58:00 -05004979 ret = cgroup_apply_control_enable(cgrp);
4980 if (ret)
4981 goto out_destroy;
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004982
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04004983 TRACE_CGROUP_PATH(mkdir, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04004984
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004985 /* let's create and online css's */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004986 kernfs_activate(kn);
4987
Tejun Heoba0f4d72014-05-13 12:19:22 -04004988 ret = 0;
4989 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004990
Tejun Heoa5bca212016-03-03 09:57:58 -05004991out_destroy:
4992 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004993out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004994 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004995 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004996}
4997
Tejun Heo223dbc32013-08-13 20:22:50 -04004998/*
4999 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005000 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5001 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005002 */
5003static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005004{
Tejun Heo223dbc32013-08-13 20:22:50 -04005005 struct cgroup_subsys_state *css =
5006 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005007
Tejun Heof20104d2013-08-13 20:22:50 -04005008 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005009
Tejun Heoaa226ff2016-01-21 15:31:11 -05005010 do {
5011 offline_css(css);
5012 css_put(css);
5013 /* @css can't go away while we're holding cgroup_mutex */
5014 css = css->parent;
5015 } while (css && atomic_dec_and_test(&css->online_cnt));
5016
5017 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005018}
5019
Tejun Heo223dbc32013-08-13 20:22:50 -04005020/* css kill confirmation processing requires process context, bounce */
5021static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005022{
5023 struct cgroup_subsys_state *css =
5024 container_of(ref, struct cgroup_subsys_state, refcnt);
5025
Tejun Heoaa226ff2016-01-21 15:31:11 -05005026 if (atomic_dec_and_test(&css->online_cnt)) {
5027 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5028 queue_work(cgroup_destroy_wq, &css->destroy_work);
5029 }
Tejun Heod3daf282013-06-13 19:39:16 -07005030}
5031
Tejun Heof392e512014-04-23 11:13:14 -04005032/**
5033 * kill_css - destroy a css
5034 * @css: css to destroy
5035 *
5036 * This function initiates destruction of @css by removing cgroup interface
5037 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005038 * asynchronously once css_tryget_online() is guaranteed to fail and when
5039 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005040 */
5041static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005042{
Tejun Heo01f64742014-05-13 12:19:23 -04005043 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005044
Waiman Long33c35aa2017-05-15 09:34:06 -04005045 if (css->flags & CSS_DYING)
5046 return;
5047
5048 css->flags |= CSS_DYING;
5049
Tejun Heo2bd59d42014-02-11 11:52:49 -05005050 /*
5051 * This must happen before css is disassociated with its cgroup.
5052 * See seq_css() for details.
5053 */
Tejun Heo334c3672016-03-03 09:58:01 -05005054 css_clear_dir(css);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005055
Tejun Heoedae0c32013-08-13 20:22:51 -04005056 /*
5057 * Killing would put the base ref, but we need to keep it alive
5058 * until after ->css_offline().
5059 */
5060 css_get(css);
5061
5062 /*
5063 * cgroup core guarantees that, by the time ->css_offline() is
5064 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005065 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005066 * proceed to offlining css's because percpu_ref_kill() doesn't
5067 * guarantee that the ref is seen as killed on all CPUs on return.
5068 *
5069 * Use percpu_ref_kill_and_confirm() to get notifications as each
5070 * css is confirmed to be seen as killed on all CPUs.
5071 */
5072 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005073}
5074
5075/**
5076 * cgroup_destroy_locked - the first stage of cgroup destruction
5077 * @cgrp: cgroup to be destroyed
5078 *
5079 * css's make use of percpu refcnts whose killing latency shouldn't be
5080 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005081 * guarantee that css_tryget_online() won't succeed by the time
5082 * ->css_offline() is invoked. To satisfy all the requirements,
5083 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005084 *
5085 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5086 * userland visible parts and start killing the percpu refcnts of
5087 * css's. Set up so that the next stage will be kicked off once all
5088 * the percpu refcnts are confirmed to be killed.
5089 *
5090 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5091 * rest of destruction. Once all cgroup references are gone, the
5092 * cgroup is RCU-freed.
5093 *
5094 * This function implements s1. After this step, @cgrp is gone as far as
5095 * the userland is concerned and a new cgroup with the same name may be
5096 * created. As cgroup doesn't care about the names internally, this
5097 * doesn't cause any problem.
5098 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005099static int cgroup_destroy_locked(struct cgroup *cgrp)
5100 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005101{
Roman Gushchin0679dee2017-08-02 17:55:29 +01005102 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005103 struct cgroup_subsys_state *css;
Tejun Heo2b021cb2016-03-15 20:43:04 -04005104 struct cgrp_cset_link *link;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005105 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005106
Tejun Heo42809dd2012-11-19 08:13:37 -08005107 lockdep_assert_held(&cgroup_mutex);
5108
Tejun Heo91486f62015-10-15 16:41:51 -04005109 /*
5110 * Only migration can raise populated from zero and we're already
5111 * holding cgroup_mutex.
5112 */
5113 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005114 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005115
Tejun Heo1a90dd52012-11-05 09:16:59 -08005116 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005117 * Make sure there's no live children. We can't test emptiness of
5118 * ->self.children as dead children linger on it while being
5119 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005120 */
Tejun Heof3d46502014-05-16 13:22:52 -04005121 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005122 return -EBUSY;
5123
5124 /*
Tejun Heo2b021cb2016-03-15 20:43:04 -04005125 * Mark @cgrp and the associated csets dead. The former prevents
5126 * further task migration and child creation by disabling
5127 * cgroup_lock_live_group(). The latter makes the csets ignored by
5128 * the migration path.
Tejun Heo455050d2013-06-13 19:27:41 -07005129 */
Tejun Heo184faf32014-05-16 13:22:51 -04005130 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005131
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005132 spin_lock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005133 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5134 link->cset->dead = true;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005135 spin_unlock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005136
Tejun Heo249f3462014-05-14 09:15:01 -04005137 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005138 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005139 kill_css(css);
5140
Tejun Heo5faaf052018-04-26 14:29:04 -07005141 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5142 css_clear_dir(&cgrp->self);
Tejun Heo01f64742014-05-13 12:19:23 -04005143 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005144
Tejun Heo454000a2017-05-15 09:34:02 -04005145 if (parent && cgroup_is_threaded(cgrp))
5146 parent->nr_threaded_children--;
5147
Roman Gushchin0679dee2017-08-02 17:55:29 +01005148 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
5149 tcgrp->nr_descendants--;
5150 tcgrp->nr_dying_descendants++;
5151 }
5152
Roman Gushchin5a621e62017-08-02 17:55:32 +01005153 cgroup1_check_for_release(parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005154
Tejun Heo249f3462014-05-14 09:15:01 -04005155 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005156 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005157
Tejun Heoea15f8c2013-06-13 19:27:42 -07005158 return 0;
5159};
5160
Tejun Heo1592c9b2016-12-27 14:49:08 -05005161int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005162{
Tejun Heoa9746d82014-05-13 12:19:22 -04005163 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005164 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005165
Tejun Heo945ba192016-03-03 09:58:00 -05005166 cgrp = cgroup_kn_lock_live(kn, false);
Tejun Heoa9746d82014-05-13 12:19:22 -04005167 if (!cgrp)
5168 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005169
Tejun Heoa9746d82014-05-13 12:19:22 -04005170 ret = cgroup_destroy_locked(cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005171 if (!ret)
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04005172 TRACE_CGROUP_PATH(rmdir, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005173
Tejun Heoa9746d82014-05-13 12:19:22 -04005174 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005175 return ret;
5176}
5177
Tejun Heo2bd59d42014-02-11 11:52:49 -05005178static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
Tejun Heo5136f632017-06-27 14:30:28 -04005179 .show_options = cgroup_show_options,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005180 .remount_fs = cgroup_remount,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005181 .mkdir = cgroup_mkdir,
5182 .rmdir = cgroup_rmdir,
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05005183 .show_path = cgroup_show_path,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005184};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005185
Tejun Heo15a4c832014-05-04 15:09:14 -04005186static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005187{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005188 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005189
Tejun Heoa5ae9892015-12-29 14:53:56 -05005190 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005191
Tejun Heo648bb562012-11-19 08:13:36 -08005192 mutex_lock(&cgroup_mutex);
5193
Tejun Heo15a4c832014-05-04 15:09:14 -04005194 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005195 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005196
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005197 /* Create the root cgroup state for this subsystem */
5198 ss->root = &cgrp_dfl_root;
5199 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005200 /* We don't handle early failures gracefully */
5201 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005202 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005203
5204 /*
5205 * Root csses are never destroyed and we can't initialize
5206 * percpu_ref during early init. Disable refcnting.
5207 */
5208 css->flags |= CSS_NO_REF;
5209
Tejun Heo15a4c832014-05-04 15:09:14 -04005210 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005211 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005212 css->id = 1;
5213 } else {
5214 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5215 BUG_ON(css->id < 0);
5216 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005217
Li Zefane8d55fd2008-04-29 01:00:13 -07005218 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005219 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005220 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005221 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005222 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005223
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005224 have_fork_callback |= (bool)ss->fork << ss->id;
5225 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005226 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005227 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005228
Li Zefane8d55fd2008-04-29 01:00:13 -07005229 /* At system boot, before all subsystems have been
5230 * registered, no tasks have been forked, so we don't
5231 * need to invoke fork callbacks here. */
5232 BUG_ON(!list_empty(&init_task.tasks));
5233
Tejun Heoae7f1642013-08-13 20:22:50 -04005234 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005235
Tejun Heo648bb562012-11-19 08:13:36 -08005236 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005237}
5238
5239/**
Li Zefana043e3b2008-02-23 15:24:09 -08005240 * cgroup_init_early - cgroup initialization at system boot
5241 *
5242 * Initialize cgroups at system boot, and initialize any
5243 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005244 */
5245int __init cgroup_init_early(void)
5246{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005247 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005248 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005249 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005250
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005251 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005252 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5253
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005254 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005255
Tejun Heo3ed80a62014-02-08 10:36:58 -05005256 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005257 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005258 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005259 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005260 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005261 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5262 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005263
Tejun Heoaec25022014-02-08 10:36:58 -05005264 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005265 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005266 if (!ss->legacy_name)
5267 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005268
5269 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005270 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005271 }
5272 return 0;
5273}
5274
Tejun Heo6e5c8302016-02-22 22:25:47 -05005275static u16 cgroup_disable_mask __initdata;
Tejun Heoa3e72732015-09-25 16:24:27 -04005276
Paul Menageddbcc7e2007-10-18 23:39:30 -07005277/**
Li Zefana043e3b2008-02-23 15:24:09 -08005278 * cgroup_init - cgroup initialization
5279 *
5280 * Register cgroup filesystem and /proc file, and initialize
5281 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005282 */
5283int __init cgroup_init(void)
5284{
Tejun Heo30159ec2013-06-25 11:53:37 -07005285 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005286 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005287
Tejun Heo6e5c8302016-02-22 22:25:47 -05005288 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005289 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heod62beb72016-12-27 14:49:08 -05005290 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
5291 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005292
Tejun Heoc58632b2018-04-26 14:29:04 -07005293 cgroup_rstat_boot();
Tejun Heo041cd642017-09-25 08:12:05 -07005294
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005295 /*
5296 * The latency of the synchronize_sched() is too high for cgroups,
5297 * avoid it at the cost of forcing all readers into the slow path.
5298 */
5299 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5300
Aditya Kalia79a9082016-01-29 02:54:06 -06005301 get_user_ns(init_cgroup_ns.user_ns);
5302
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005303 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005304
Tejun Heo2378d8b2016-03-03 09:57:57 -05005305 /*
5306 * Add init_css_set to the hash table so that dfl_root can link to
5307 * it during init.
5308 */
5309 hash_add(css_set_table, &init_css_set.hlist,
5310 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005311
Zefan Li9732adc2017-04-19 10:15:59 +08005312 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005313
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005314 mutex_unlock(&cgroup_mutex);
5315
Tejun Heo172a2c062014-03-19 10:23:53 -04005316 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005317 if (ss->early_init) {
5318 struct cgroup_subsys_state *css =
5319 init_css_set.subsys[ss->id];
5320
5321 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5322 GFP_KERNEL);
5323 BUG_ON(css->id < 0);
5324 } else {
5325 cgroup_init_subsys(ss, false);
5326 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005327
Tejun Heo2d8f2432014-04-23 11:13:15 -04005328 list_add_tail(&init_css_set.e_cset_node[ssid],
5329 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005330
5331 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005332 * Setting dfl_root subsys_mask needs to consider the
5333 * disabled flag and cftype registration needs kmalloc,
5334 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005335 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005336 if (cgroup_disable_mask & (1 << ssid)) {
5337 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5338 printk(KERN_INFO "Disabling %s control group subsystem\n",
5339 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005340 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005341 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005342
Tejun Heod62beb72016-12-27 14:49:08 -05005343 if (cgroup1_ssid_disabled(ssid))
Johannes Weiner223ffb22016-02-11 13:34:49 -05005344 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5345 ss->name);
5346
Tejun Heoa8ddc822014-07-15 11:05:10 -04005347 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5348
Tejun Heo8cfd8142017-07-21 11:14:51 -04005349 /* implicit controllers must be threaded too */
5350 WARN_ON(ss->implicit_on_dfl && !ss->threaded);
5351
Tejun Heof6d635ad2016-03-08 11:51:26 -05005352 if (ss->implicit_on_dfl)
5353 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5354 else if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005355 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005356
Tejun Heo8cfd8142017-07-21 11:14:51 -04005357 if (ss->threaded)
5358 cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
5359
Tejun Heoa8ddc822014-07-15 11:05:10 -04005360 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5361 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5362 } else {
5363 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5364 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005365 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005366
5367 if (ss->bind)
5368 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo7af608e2017-07-18 17:57:46 -04005369
5370 mutex_lock(&cgroup_mutex);
5371 css_populate_dir(init_css_set.subsys[ssid]);
5372 mutex_unlock(&cgroup_mutex);
Tejun Heo172a2c062014-03-19 10:23:53 -04005373 }
Greg KH676db4a2010-08-05 13:53:35 -07005374
Tejun Heo2378d8b2016-03-03 09:57:57 -05005375 /* init_css_set.subsys[] has been updated, re-hash */
5376 hash_del(&init_css_set.hlist);
5377 hash_add(css_set_table, &init_css_set.hlist,
5378 css_set_hash(init_css_set.subsys));
5379
Tejun Heo035f4f52015-10-15 17:00:43 -04005380 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5381 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005382 WARN_ON(register_filesystem(&cgroup2_fs_type));
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02005383 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
Paul Menagea4243162007-10-18 23:39:35 -07005384
Tejun Heo2bd59d42014-02-11 11:52:49 -05005385 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005386}
Paul Menageb4f48b62007-10-18 23:39:33 -07005387
Tejun Heoe5fca242013-11-22 17:14:39 -05005388static int __init cgroup_wq_init(void)
5389{
5390 /*
5391 * There isn't much point in executing destruction path in
5392 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005393 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005394 *
5395 * We would prefer to do this in cgroup_init() above, but that
5396 * is called before init_workqueues(): so leave this until after.
5397 */
Tejun Heo1a115332014-02-12 19:06:19 -05005398 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005399 BUG_ON(!cgroup_destroy_wq);
5400 return 0;
5401}
5402core_initcall(cgroup_wq_init);
5403
Shaohua Li69fd5c32017-07-12 11:49:55 -07005404void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
5405 char *buf, size_t buflen)
5406{
5407 struct kernfs_node *kn;
5408
5409 kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id);
5410 if (!kn)
5411 return;
5412 kernfs_path(kn, buf, buflen);
5413 kernfs_put(kn);
5414}
5415
Paul Menagea4243162007-10-18 23:39:35 -07005416/*
5417 * proc_cgroup_show()
5418 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5419 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005420 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005421int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5422 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005423{
Tejun Heo4c737b42016-08-10 11:23:44 -04005424 char *buf;
Paul Menagea4243162007-10-18 23:39:35 -07005425 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005426 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005427
5428 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005429 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005430 if (!buf)
5431 goto out;
5432
Paul Menagea4243162007-10-18 23:39:35 -07005433 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005434 spin_lock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005435
Tejun Heo985ed672014-03-19 10:23:53 -04005436 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005437 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005438 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005439 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005440
Tejun Heoa7165262016-02-23 10:00:50 -05005441 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005442 continue;
5443
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005444 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005445 if (root != &cgrp_dfl_root)
5446 for_each_subsys(ss, ssid)
5447 if (root->subsys_mask & (1 << ssid))
5448 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005449 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005450 if (strlen(root->name))
5451 seq_printf(m, "%sname=%s", count ? "," : "",
5452 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005453 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005454
Paul Menage7717f7b2009-09-23 15:56:22 -07005455 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005456
5457 /*
5458 * On traditional hierarchies, all zombie tasks show up as
5459 * belonging to the root cgroup. On the default hierarchy,
5460 * while a zombie doesn't show up in "cgroup.procs" and
5461 * thus can't be migrated, its /proc/PID/cgroup keeps
5462 * reporting the cgroup it belonged to before exiting. If
5463 * the cgroup is removed before the zombie is reaped,
5464 * " (deleted)" is appended to the cgroup path.
5465 */
5466 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
Tejun Heo4c737b42016-08-10 11:23:44 -04005467 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
Aditya Kalia79a9082016-01-29 02:54:06 -06005468 current->nsproxy->cgroup_ns);
Tejun Heoe0223002016-09-29 15:49:40 +02005469 if (retval >= PATH_MAX)
Tejun Heo2e91fa72015-10-15 16:41:53 -04005470 retval = -ENAMETOOLONG;
Tejun Heoe0223002016-09-29 15:49:40 +02005471 if (retval < 0)
Tejun Heo2e91fa72015-10-15 16:41:53 -04005472 goto out_unlock;
Tejun Heo2e91fa72015-10-15 16:41:53 -04005473
Tejun Heo4c737b42016-08-10 11:23:44 -04005474 seq_puts(m, buf);
5475 } else {
5476 seq_puts(m, "/");
5477 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005478
5479 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5480 seq_puts(m, " (deleted)\n");
5481 else
5482 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005483 }
5484
Zefan Li006f4ac2014-09-18 16:03:15 +08005485 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005486out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005487 spin_unlock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005488 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005489 kfree(buf);
5490out:
5491 return retval;
5492}
5493
Paul Menageb4f48b62007-10-18 23:39:33 -07005494/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005495 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005496 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005497 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005498 * A task is associated with the init_css_set until cgroup_post_fork()
5499 * attaches it to the parent's css_set. Empty cg_list indicates that
5500 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005501 */
5502void cgroup_fork(struct task_struct *child)
5503{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005504 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005505 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005506}
5507
5508/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005509 * cgroup_can_fork - called on a new task before the process is exposed
5510 * @child: the task in question.
5511 *
5512 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5513 * returns an error, the fork aborts with that error code. This allows for
5514 * a cgroup subsystem to conditionally allow or deny new forks.
5515 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005516int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005517{
5518 struct cgroup_subsys *ss;
5519 int i, j, ret;
5520
Tejun Heob4e0eea2016-02-22 22:25:46 -05005521 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005522 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005523 if (ret)
5524 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05005525 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10005526
5527 return 0;
5528
5529out_revert:
5530 for_each_subsys(ss, j) {
5531 if (j >= i)
5532 break;
5533 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005534 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005535 }
5536
5537 return ret;
5538}
5539
5540/**
5541 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5542 * @child: the task in question
5543 *
5544 * This calls the cancel_fork() callbacks if a fork failed *after*
5545 * cgroup_can_fork() succeded.
5546 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005547void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005548{
5549 struct cgroup_subsys *ss;
5550 int i;
5551
5552 for_each_subsys(ss, i)
5553 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005554 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005555}
5556
5557/**
Li Zefana043e3b2008-02-23 15:24:09 -08005558 * cgroup_post_fork - called on a new task after adding it to the task list
5559 * @child: the task in question
5560 *
Tejun Heo5edee612012-10-16 15:03:14 -07005561 * Adds the task to the list running through its css_set if necessary and
5562 * call the subsystem fork() callbacks. Has to be after the task is
5563 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005564 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005565 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005566 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005567void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005568{
Tejun Heo30159ec2013-06-25 11:53:37 -07005569 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005570 int i;
5571
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005572 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005573 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005574 * function sets use_task_css_set_links before grabbing
5575 * tasklist_lock and we just went through tasklist_lock to add
5576 * @child, it's guaranteed that either we see the set
5577 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5578 * @child during its iteration.
5579 *
5580 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005581 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005582 * association is stable, and, on completion of the parent's
5583 * migration, @child is visible in the source of migration or
5584 * already in the destination cgroup. This guarantee is necessary
5585 * when implementing operations which need to migrate all tasks of
5586 * a cgroup to another.
5587 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005588 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005589 * will remain in init_css_set. This is safe because all tasks are
5590 * in the init_css_set before cg_links is enabled and there's no
5591 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005592 */
Paul Menage817929e2007-10-18 23:39:36 -07005593 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005594 struct css_set *cset;
5595
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005596 spin_lock_irq(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005597 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005598 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005599 get_css_set(cset);
Waiman Long73a72422017-06-13 17:18:01 -04005600 cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04005601 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005602 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005603 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005604 }
Tejun Heo5edee612012-10-16 15:03:14 -07005605
5606 /*
5607 * Call ss->fork(). This must happen after @child is linked on
5608 * css_set; otherwise, @child might change state between ->fork()
5609 * and addition to css_set.
5610 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005611 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005612 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005613 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07005614}
Tejun Heo5edee612012-10-16 15:03:14 -07005615
Paul Menage817929e2007-10-18 23:39:36 -07005616/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005617 * cgroup_exit - detach cgroup from exiting task
5618 * @tsk: pointer to task_struct of exiting process
5619 *
5620 * Description: Detach cgroup from @tsk and release it.
5621 *
5622 * Note that cgroups marked notify_on_release force every task in
5623 * them to take the global cgroup_mutex mutex when exiting.
5624 * This could impact scaling on very large systems. Be reluctant to
5625 * use notify_on_release cgroups where very high task exit scaling
5626 * is required on large systems.
5627 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005628 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5629 * call cgroup_exit() while the task is still competent to handle
5630 * notify_on_release(), then leave the task attached to the root cgroup in
5631 * each hierarchy for the remainder of its exit. No need to bother with
5632 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005633 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005634 */
Li Zefan1ec41832014-03-28 15:22:19 +08005635void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005636{
Tejun Heo30159ec2013-06-25 11:53:37 -07005637 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005638 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005639 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005640
5641 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005642 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005643 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005644 */
Tejun Heo0de09422015-10-15 16:41:49 -04005645 cset = task_css_set(tsk);
5646
Paul Menage817929e2007-10-18 23:39:36 -07005647 if (!list_empty(&tsk->cg_list)) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005648 spin_lock_irq(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005649 css_set_move_task(tsk, cset, NULL, false);
Waiman Long73a72422017-06-13 17:18:01 -04005650 cset->nr_tasks--;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005651 spin_unlock_irq(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005652 } else {
5653 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005654 }
5655
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005656 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005657 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04005658 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005659 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04005660}
Tejun Heo30159ec2013-06-25 11:53:37 -07005661
Tejun Heo2e91fa72015-10-15 16:41:53 -04005662void cgroup_free(struct task_struct *task)
5663{
5664 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005665 struct cgroup_subsys *ss;
5666 int ssid;
5667
Tejun Heob4e0eea2016-02-22 22:25:46 -05005668 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04005669 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005670 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005671
Tejun Heo2e91fa72015-10-15 16:41:53 -04005672 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005673}
Paul Menage697f4162007-10-18 23:39:34 -07005674
Paul Menage8bab8dd2008-04-04 14:29:57 -07005675static int __init cgroup_disable(char *str)
5676{
Tejun Heo30159ec2013-06-25 11:53:37 -07005677 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005678 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005679 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005680
5681 while ((token = strsep(&str, ",")) != NULL) {
5682 if (!*token)
5683 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005684
Tejun Heo3ed80a62014-02-08 10:36:58 -05005685 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005686 if (strcmp(token, ss->name) &&
5687 strcmp(token, ss->legacy_name))
5688 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005689 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005690 }
5691 }
5692 return 1;
5693}
5694__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005695
Tejun Heob77d7b62013-08-13 11:01:54 -04005696/**
Tejun Heoec903c02014-05-13 12:11:01 -04005697 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005698 * @dentry: directory dentry of interest
5699 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005700 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005701 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5702 * to get the corresponding css and return it. If such css doesn't exist
5703 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005704 */
Tejun Heoec903c02014-05-13 12:11:01 -04005705struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5706 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005707{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005708 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05005709 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005710 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005711 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005712
Tejun Heo35cf0832013-08-26 18:40:56 -04005713 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05005714 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5715 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005716 return ERR_PTR(-EBADF);
5717
Tejun Heo5a17f542014-02-11 11:52:47 -05005718 rcu_read_lock();
5719
Tejun Heo2bd59d42014-02-11 11:52:49 -05005720 /*
5721 * This path doesn't originate from kernfs and @kn could already
5722 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005723 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005724 */
Tejun Heoe0aed7c2016-12-27 14:49:09 -05005725 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005726 if (cgrp)
5727 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005728
Tejun Heoec903c02014-05-13 12:11:01 -04005729 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005730 css = ERR_PTR(-ENOENT);
5731
5732 rcu_read_unlock();
5733 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005734}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005735
Li Zefan1cb650b2013-08-19 10:05:24 +08005736/**
5737 * css_from_id - lookup css by id
5738 * @id: the cgroup id
5739 * @ss: cgroup subsys to be looked into
5740 *
5741 * Returns the css if there's valid one with @id, otherwise returns NULL.
5742 * Should be called under rcu_read_lock().
5743 */
5744struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5745{
Tejun Heo6fa49182014-05-04 15:09:13 -04005746 WARN_ON_ONCE(!rcu_read_lock_held());
Johannes Weinerd6ccc552016-06-17 12:24:27 -04005747 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005748}
5749
Tejun Heo16af4392015-11-20 15:55:52 -05005750/**
5751 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5752 * @path: path on the default hierarchy
5753 *
5754 * Find the cgroup at @path on the default hierarchy, increment its
5755 * reference count and return it. Returns pointer to the found cgroup on
5756 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5757 * if @path points to a non-directory.
5758 */
5759struct cgroup *cgroup_get_from_path(const char *path)
5760{
5761 struct kernfs_node *kn;
5762 struct cgroup *cgrp;
5763
5764 mutex_lock(&cgroup_mutex);
5765
5766 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5767 if (kn) {
5768 if (kernfs_type(kn) == KERNFS_DIR) {
5769 cgrp = kn->priv;
Tejun Heoa590b902017-04-28 15:14:55 -04005770 cgroup_get_live(cgrp);
Tejun Heo16af4392015-11-20 15:55:52 -05005771 } else {
5772 cgrp = ERR_PTR(-ENOTDIR);
5773 }
5774 kernfs_put(kn);
5775 } else {
5776 cgrp = ERR_PTR(-ENOENT);
5777 }
5778
5779 mutex_unlock(&cgroup_mutex);
5780 return cgrp;
5781}
5782EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5783
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07005784/**
5785 * cgroup_get_from_fd - get a cgroup pointer from a fd
5786 * @fd: fd obtained by open(cgroup2_dir)
5787 *
5788 * Find the cgroup from a fd which should be obtained
5789 * by opening a cgroup directory. Returns a pointer to the
5790 * cgroup on success. ERR_PTR is returned if the cgroup
5791 * cannot be found.
5792 */
5793struct cgroup *cgroup_get_from_fd(int fd)
5794{
5795 struct cgroup_subsys_state *css;
5796 struct cgroup *cgrp;
5797 struct file *f;
5798
5799 f = fget_raw(fd);
5800 if (!f)
5801 return ERR_PTR(-EBADF);
5802
5803 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
5804 fput(f);
5805 if (IS_ERR(css))
5806 return ERR_CAST(css);
5807
5808 cgrp = css->cgroup;
5809 if (!cgroup_on_dfl(cgrp)) {
5810 cgroup_put(cgrp);
5811 return ERR_PTR(-EBADF);
5812 }
5813
5814 return cgrp;
5815}
5816EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
5817
Tejun Heobd1060a2015-12-07 17:38:53 -05005818/*
5819 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5820 * definition in cgroup-defs.h.
5821 */
5822#ifdef CONFIG_SOCK_CGROUP_DATA
5823
5824#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5825
Tejun Heo3fa4cc92015-12-14 11:24:06 -05005826DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05005827static bool cgroup_sk_alloc_disabled __read_mostly;
5828
5829void cgroup_sk_alloc_disable(void)
5830{
5831 if (cgroup_sk_alloc_disabled)
5832 return;
5833 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5834 cgroup_sk_alloc_disabled = true;
5835}
5836
5837#else
5838
5839#define cgroup_sk_alloc_disabled false
5840
5841#endif
5842
5843void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5844{
5845 if (cgroup_sk_alloc_disabled)
5846 return;
5847
Johannes Weinerd979a392016-09-19 14:44:38 -07005848 /* Socket clone path */
5849 if (skcd->val) {
Tejun Heoa590b902017-04-28 15:14:55 -04005850 /*
5851 * We might be cloning a socket which is left in an empty
5852 * cgroup and the cgroup might have already been rmdir'd.
5853 * Don't use cgroup_get_live().
5854 */
Johannes Weinerd979a392016-09-19 14:44:38 -07005855 cgroup_get(sock_cgroup_ptr(skcd));
5856 return;
5857 }
5858
Tejun Heobd1060a2015-12-07 17:38:53 -05005859 rcu_read_lock();
5860
5861 while (true) {
5862 struct css_set *cset;
5863
5864 cset = task_css_set(current);
5865 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5866 skcd->val = (unsigned long)cset->dfl_cgrp;
5867 break;
5868 }
5869 cpu_relax();
5870 }
5871
5872 rcu_read_unlock();
5873}
5874
5875void cgroup_sk_free(struct sock_cgroup_data *skcd)
5876{
5877 cgroup_put(sock_cgroup_ptr(skcd));
5878}
5879
5880#endif /* CONFIG_SOCK_CGROUP_DATA */
5881
Daniel Mack30070982016-11-23 16:52:26 +01005882#ifdef CONFIG_CGROUP_BPF
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07005883int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
5884 enum bpf_attach_type type, u32 flags)
Daniel Mack30070982016-11-23 16:52:26 +01005885{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08005886 int ret;
Daniel Mack30070982016-11-23 16:52:26 +01005887
5888 mutex_lock(&cgroup_mutex);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07005889 ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
5890 mutex_unlock(&cgroup_mutex);
5891 return ret;
5892}
5893int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
5894 enum bpf_attach_type type, u32 flags)
5895{
5896 int ret;
5897
5898 mutex_lock(&cgroup_mutex);
5899 ret = __cgroup_bpf_detach(cgrp, prog, type, flags);
Daniel Mack30070982016-11-23 16:52:26 +01005900 mutex_unlock(&cgroup_mutex);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08005901 return ret;
Daniel Mack30070982016-11-23 16:52:26 +01005902}
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07005903int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
5904 union bpf_attr __user *uattr)
5905{
5906 int ret;
5907
5908 mutex_lock(&cgroup_mutex);
5909 ret = __cgroup_bpf_query(cgrp, attr, uattr);
5910 mutex_unlock(&cgroup_mutex);
5911 return ret;
5912}
Daniel Mack30070982016-11-23 16:52:26 +01005913#endif /* CONFIG_CGROUP_BPF */
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05005914
5915#ifdef CONFIG_SYSFS
5916static ssize_t show_delegatable_files(struct cftype *files, char *buf,
5917 ssize_t size, const char *prefix)
5918{
5919 struct cftype *cft;
5920 ssize_t ret = 0;
5921
5922 for (cft = files; cft && cft->name[0] != '\0'; cft++) {
5923 if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
5924 continue;
5925
5926 if (prefix)
5927 ret += snprintf(buf + ret, size - ret, "%s.", prefix);
5928
5929 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
5930
5931 if (unlikely(ret >= size)) {
5932 WARN_ON(1);
5933 break;
5934 }
5935 }
5936
5937 return ret;
5938}
5939
5940static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
5941 char *buf)
5942{
5943 struct cgroup_subsys *ss;
5944 int ssid;
5945 ssize_t ret = 0;
5946
5947 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret,
5948 NULL);
5949
5950 for_each_subsys(ss, ssid)
5951 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
5952 PAGE_SIZE - ret,
5953 cgroup_subsys_name[ssid]);
5954
5955 return ret;
5956}
5957static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
5958
Roman Gushchin5f2e6732017-11-06 13:30:29 -05005959static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
5960 char *buf)
5961{
5962 return snprintf(buf, PAGE_SIZE, "nsdelegate\n");
5963}
5964static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
5965
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05005966static struct attribute *cgroup_sysfs_attrs[] = {
5967 &cgroup_delegate_attr.attr,
Roman Gushchin5f2e6732017-11-06 13:30:29 -05005968 &cgroup_features_attr.attr,
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05005969 NULL,
5970};
5971
5972static const struct attribute_group cgroup_sysfs_attr_group = {
5973 .attrs = cgroup_sysfs_attrs,
5974 .name = "cgroup",
5975};
5976
5977static int __init cgroup_sysfs_init(void)
5978{
5979 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
5980}
5981subsys_initcall(cgroup_sysfs_init);
5982#endif /* CONFIG_SYSFS */