blob: 64360a46d4dfab28848493e66b79dc40ab833148 [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>
Al Viroe34a98d2019-01-17 00:22:58 -050057#include <linux/fs_parser.h>
Tejun Heod4ff7492018-04-26 14:29:04 -070058#include <linux/sched/cputime.h>
Johannes Weiner2ce71352018-10-26 15:06:31 -070059#include <linux/psi.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050060#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoed1777d2016-08-10 11:23:44 -040062#define CREATE_TRACE_POINTS
63#include <trace/events/cgroup.h>
64
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050065#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
66 MAX_CFTYPE_NAME + 2)
Tejun Heob12e3582018-04-26 14:29:04 -070067/* let's not notify more than 100 times per second */
68#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050069
Tejun Heob1a21362013-11-29 10:42:58 -050070/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080071 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040074 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050075 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080076 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050077 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
78 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080079 */
Tejun Heo22194492013-04-07 09:29:51 -070080DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040081DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0a268db2016-12-27 14:49:06 -050082
83#ifdef CONFIG_PROVE_RCU
Tejun Heo0e1d7682014-02-25 10:04:03 -050084EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040085EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070086#endif
87
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -040088DEFINE_SPINLOCK(trace_cgroup_path_lock);
89char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
Waiman Long5cf81142018-11-08 10:08:46 -050090bool cgroup_debug __read_mostly;
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -040091
Tejun Heo69e943b2014-02-08 10:36:58 -050092/*
Tejun Heo15a4c832014-05-04 15:09:14 -040093 * Protects cgroup_idr and css_idr so that IDs can be released without
94 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040095 */
96static DEFINE_SPINLOCK(cgroup_idr_lock);
97
98/*
Tejun Heo34c06252015-11-05 00:12:24 -050099 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
100 * against file removal/re-creation across css hiding.
101 */
102static DEFINE_SPINLOCK(cgroup_file_kn_lock);
103
Tejun Heo1ed13282015-09-16 12:53:17 -0400104struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
105
Tejun Heo8353da12014-05-13 12:19:23 -0400106#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700107 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
108 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400109 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500110
Ben Blumaae8aab2010-03-10 15:22:07 -0800111/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500112 * cgroup destruction makes heavy use of work items and there can be a lot
113 * of concurrent destructions. Use a separate workqueue so that cgroup
114 * destruction work items don't end up filling up max_active of system_wq
115 * which may lead to deadlock.
116 */
117static struct workqueue_struct *cgroup_destroy_wq;
118
Tejun Heo3ed80a62014-02-08 10:36:58 -0500119/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500120#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo0a268db2016-12-27 14:49:06 -0500121struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122#include <linux/cgroup_subsys.h>
123};
Tejun Heo073219e2014-02-08 10:36:58 -0500124#undef SUBSYS
125
126/* array of cgroup subsystem names */
127#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
128static const char *cgroup_subsys_name[] = {
129#include <linux/cgroup_subsys.h>
130};
131#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132
Tejun Heo49d1dc42015-09-18 11:56:28 -0400133/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
134#define SUBSYS(_x) \
135 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
136 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
137 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
138 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
139#include <linux/cgroup_subsys.h>
140#undef SUBSYS
141
142#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
143static struct static_key_true *cgroup_subsys_enabled_key[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
147
148#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
149static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
150#include <linux/cgroup_subsys.h>
151};
152#undef SUBSYS
153
Tejun Heoc58632b2018-04-26 14:29:04 -0700154static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
Tejun Heo041cd642017-09-25 08:12:05 -0700155
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400157 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700158 * unattached - it never has more than a single cgroup, and all tasks are
159 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160 */
Tejun Heoc58632b2018-04-26 14:29:04 -0700161struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
Tejun Heod0ec4232015-08-05 16:03:19 -0400162EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700163
Tejun Heoa2dd4242014-03-19 10:23:55 -0400164/*
165 * The default hierarchy always exists but is hidden until mounted for the
166 * first time. This is for backward compatibility.
167 */
Tejun Heoa7165262016-02-23 10:00:50 -0500168static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169
Tejun Heo5533e012014-05-14 19:33:07 -0400170/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500171static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400172
Tejun Heof6d635ad2016-03-08 11:51:26 -0500173/* some controllers are implicitly enabled on the default hierarchy */
Tejun Heob8074212017-01-20 12:06:08 -0500174static u16 cgrp_dfl_implicit_ss_mask;
Tejun Heof6d635ad2016-03-08 11:51:26 -0500175
Tejun Heo8cfd8142017-07-21 11:14:51 -0400176/* some controllers can be threaded on the default hierarchy */
177static u16 cgrp_dfl_threaded_ss_mask;
178
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179/* The list of hierarchy roots */
Tejun Heo0a268db2016-12-27 14:49:06 -0500180LIST_HEAD(cgroup_roots);
Tejun Heo9871bf92013-06-24 15:21:47 -0700181static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700182
Tejun Heo3417ae12014-02-08 10:37:01 -0500183/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700184static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700185
Li Zefan794611a2013-06-18 18:53:53 +0800186/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400187 * Assign a monotonically increasing serial number to csses. It guarantees
188 * cgroups with bigger numbers are newer than those with smaller numbers.
189 * Also, as csses are always appended to the parent's ->children list, it
190 * guarantees that sibling csses are always sorted in the ascending serial
191 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800192 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400193static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800194
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000195/*
Tejun Heob8074212017-01-20 12:06:08 -0500196 * These bitmasks identify subsystems with specific features to avoid
197 * having to do iterative checks repeatedly.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500199static u16 have_fork_callback __read_mostly;
200static u16 have_exit_callback __read_mostly;
201static u16 have_free_callback __read_mostly;
Tejun Heob8074212017-01-20 12:06:08 -0500202static u16 have_canfork_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700203
Aditya Kalia79a9082016-01-29 02:54:06 -0600204/* cgroup namespace for init task */
205struct cgroup_namespace init_cgroup_ns = {
Elena Reshetova387ad962017-02-20 12:19:00 +0200206 .count = REFCOUNT_INIT(2),
Aditya Kalia79a9082016-01-29 02:54:06 -0600207 .user_ns = &init_user_ns,
208 .ns.ops = &cgroupns_operations,
209 .ns.inum = PROC_CGROUP_INIT_INO,
210 .root_cset = &init_css_set,
211};
212
Tejun Heo67e9c742015-11-16 11:13:34 -0500213static struct file_system_type cgroup2_fs_type;
Tejun Heod62beb72016-12-27 14:49:08 -0500214static struct cftype cgroup_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700215
Tejun Heo334c3672016-03-03 09:58:01 -0500216static int cgroup_apply_control(struct cgroup *cgrp);
217static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400218static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800219static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500220static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
221 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400222static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400223static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400224static int cgroup_addrm_files(struct cgroup_subsys_state *css,
225 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400226 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800227
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400228/**
229 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
230 * @ssid: subsys ID of interest
231 *
232 * cgroup_subsys_enabled() can only be used with literal subsys names which
233 * is fine for individual subsystems but unsuitable for cgroup core. This
234 * is slower static_key_enabled() based test indexed by @ssid.
235 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500236bool cgroup_ssid_enabled(int ssid)
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400237{
Arnd Bergmanncfe02a82016-03-15 00:21:06 +0100238 if (CGROUP_SUBSYS_COUNT == 0)
239 return false;
240
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400241 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
242}
243
Tejun Heo9e10a132015-09-18 11:56:28 -0400244/**
245 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
246 * @cgrp: the cgroup of interest
247 *
248 * The default hierarchy is the v2 interface of cgroup and this function
249 * can be used to test whether a cgroup is on the default hierarchy for
250 * cases where a subsystem should behave differnetly depending on the
251 * interface version.
252 *
253 * The set of behaviors which change on the default hierarchy are still
254 * being determined and the mount option is prefixed with __DEVEL__.
255 *
256 * List of changed behaviors:
257 *
258 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
259 * and "name" are disallowed.
260 *
261 * - When mounting an existing superblock, mount options should match.
262 *
263 * - Remount is disallowed.
264 *
265 * - rename(2) is disallowed.
266 *
267 * - "tasks" is removed. Everything should be at process granularity. Use
268 * "cgroup.procs" instead.
269 *
270 * - "cgroup.procs" is not sorted. pids will be unique unless they got
271 * recycled inbetween reads.
272 *
273 * - "release_agent" and "notify_on_release" are removed. Replacement
274 * notification mechanism will be implemented.
275 *
276 * - "cgroup.clone_children" is removed.
277 *
278 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
279 * and its descendants contain no task; otherwise, 1. The file also
280 * generates kernfs notification which can be monitored through poll and
281 * [di]notify when the value of the file changes.
282 *
283 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
284 * take masks of ancestors with non-empty cpus/mems, instead of being
285 * moved to an ancestor.
286 *
287 * - cpuset: a task can be moved into an empty cpuset, and again it takes
288 * masks of ancestors.
289 *
290 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
291 * is not created.
292 *
293 * - blkcg: blk-throttle becomes properly hierarchical.
294 *
295 * - debug: disallowed on the default hierarchy.
296 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500297bool cgroup_on_dfl(const struct cgroup *cgrp)
Tejun Heo9e10a132015-09-18 11:56:28 -0400298{
299 return cgrp->root == &cgrp_dfl_root;
300}
301
Tejun Heo6fa49182014-05-04 15:09:13 -0400302/* IDR wrappers which synchronize using cgroup_idr_lock */
303static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
304 gfp_t gfp_mask)
305{
306 int ret;
307
308 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400309 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800310 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400311 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400312 idr_preload_end();
313 return ret;
314}
315
316static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
317{
318 void *ret;
319
Tejun Heo54504e92014-05-13 12:10:59 -0400320 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400321 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400322 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400323 return ret;
324}
325
326static void cgroup_idr_remove(struct idr *idr, int id)
327{
Tejun Heo54504e92014-05-13 12:10:59 -0400328 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400329 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400330 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400331}
332
Tejun Heo27f26752017-07-16 21:44:18 -0400333static bool cgroup_has_tasks(struct cgroup *cgrp)
Tejun Heod51f39b2014-05-16 13:22:48 -0400334{
Tejun Heo27f26752017-07-16 21:44:18 -0400335 return cgrp->nr_populated_csets;
336}
Tejun Heod51f39b2014-05-16 13:22:48 -0400337
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400338bool cgroup_is_threaded(struct cgroup *cgrp)
Tejun Heo454000a2017-05-15 09:34:02 -0400339{
340 return cgrp->dom_cgrp != cgrp;
341}
342
Tejun Heo8cfd8142017-07-21 11:14:51 -0400343/* can @cgrp host both domain and threaded children? */
344static bool cgroup_is_mixable(struct cgroup *cgrp)
345{
346 /*
347 * Root isn't under domain level resource control exempting it from
348 * the no-internal-process constraint, so it can serve as a thread
349 * root and a parent of resource domains at the same time.
350 */
351 return !cgroup_parent(cgrp);
352}
353
354/* can @cgrp become a thread root? should always be true for a thread root */
355static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
356{
357 /* mixables don't care */
358 if (cgroup_is_mixable(cgrp))
359 return true;
360
361 /* domain roots can't be nested under threaded */
362 if (cgroup_is_threaded(cgrp))
363 return false;
364
365 /* can only have either domain or threaded children */
366 if (cgrp->nr_populated_domain_children)
367 return false;
368
369 /* and no domain controllers can be enabled */
370 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
371 return false;
372
373 return true;
374}
375
376/* is @cgrp root of a threaded subtree? */
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400377bool cgroup_is_thread_root(struct cgroup *cgrp)
Tejun Heo8cfd8142017-07-21 11:14:51 -0400378{
379 /* thread root should be a domain */
380 if (cgroup_is_threaded(cgrp))
381 return false;
382
383 /* a domain w/ threaded children is a thread root */
384 if (cgrp->nr_threaded_children)
385 return true;
386
387 /*
388 * A domain which has tasks and explicit threaded controllers
389 * enabled is a thread root.
390 */
391 if (cgroup_has_tasks(cgrp) &&
392 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
393 return true;
394
395 return false;
396}
397
398/* a domain which isn't connected to the root w/o brekage can't be used */
399static bool cgroup_is_valid_domain(struct cgroup *cgrp)
400{
401 /* the cgroup itself can be a thread root */
402 if (cgroup_is_threaded(cgrp))
403 return false;
404
405 /* but the ancestors can't be unless mixable */
406 while ((cgrp = cgroup_parent(cgrp))) {
407 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
408 return false;
409 if (cgroup_is_threaded(cgrp))
410 return false;
411 }
412
413 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400414}
415
Tejun Heo5531dc92016-03-03 09:57:58 -0500416/* subsystems visibly enabled on a cgroup */
417static u16 cgroup_control(struct cgroup *cgrp)
418{
419 struct cgroup *parent = cgroup_parent(cgrp);
420 u16 root_ss_mask = cgrp->root->subsys_mask;
421
Tejun Heo8cfd8142017-07-21 11:14:51 -0400422 if (parent) {
423 u16 ss_mask = parent->subtree_control;
424
425 /* threaded cgroups can only have threaded controllers */
426 if (cgroup_is_threaded(cgrp))
427 ss_mask &= cgrp_dfl_threaded_ss_mask;
428 return ss_mask;
429 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500430
431 if (cgroup_on_dfl(cgrp))
Tejun Heof6d635ad2016-03-08 11:51:26 -0500432 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
433 cgrp_dfl_implicit_ss_mask);
Tejun Heo5531dc92016-03-03 09:57:58 -0500434 return root_ss_mask;
435}
436
437/* subsystems enabled on a cgroup */
438static u16 cgroup_ss_mask(struct cgroup *cgrp)
439{
440 struct cgroup *parent = cgroup_parent(cgrp);
441
Tejun Heo8cfd8142017-07-21 11:14:51 -0400442 if (parent) {
443 u16 ss_mask = parent->subtree_ss_mask;
444
445 /* threaded cgroups can only have threaded controllers */
446 if (cgroup_is_threaded(cgrp))
447 ss_mask &= cgrp_dfl_threaded_ss_mask;
448 return ss_mask;
449 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500450
451 return cgrp->root->subsys_mask;
452}
453
Tejun Heo95109b62013-08-08 20:11:27 -0400454/**
455 * cgroup_css - obtain a cgroup's css for the specified subsystem
456 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400457 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400458 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400459 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
460 * function must be called either under cgroup_mutex or rcu_read_lock() and
461 * the caller is responsible for pinning the returned css if it wants to
462 * keep accessing it outside the said locks. This function may return
463 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400464 */
465static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400466 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400467{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400468 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500469 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500470 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400471 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400472 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400473}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700474
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400475/**
Tejun Heod41bf8c2017-10-23 16:18:27 -0700476 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
477 * @cgrp: the cgroup of interest
478 * @ss: the subsystem of interest
479 *
480 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist
481 * or is offline, %NULL is returned.
482 */
483static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
484 struct cgroup_subsys *ss)
485{
486 struct cgroup_subsys_state *css;
487
488 rcu_read_lock();
489 css = cgroup_css(cgrp, ss);
490 if (!css || !css_tryget_online(css))
491 css = NULL;
492 rcu_read_unlock();
493
494 return css;
495}
496
497/**
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500498 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400499 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400500 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400501 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400502 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400503 * as the matching css of the nearest ancestor including self which has @ss
504 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
505 * function is guaranteed to return non-NULL css.
506 */
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500507static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
508 struct cgroup_subsys *ss)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400509{
510 lockdep_assert_held(&cgroup_mutex);
511
512 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400513 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400514
Tejun Heoeeecbd12014-11-18 02:49:52 -0500515 /*
516 * This function is used while updating css associations and thus
Tejun Heo5531dc92016-03-03 09:57:58 -0500517 * can't test the csses directly. Test ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500518 */
Tejun Heo5531dc92016-03-03 09:57:58 -0500519 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
Tejun Heod51f39b2014-05-16 13:22:48 -0400520 cgrp = cgroup_parent(cgrp);
Tejun Heo5531dc92016-03-03 09:57:58 -0500521 if (!cgrp)
522 return NULL;
523 }
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400524
525 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700526}
527
Tejun Heoeeecbd12014-11-18 02:49:52 -0500528/**
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500529 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
530 * @cgrp: the cgroup of interest
531 * @ss: the subsystem of interest
532 *
533 * Find and get the effective css of @cgrp for @ss. The effective css is
534 * defined as the matching css of the nearest ancestor including self which
535 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
536 * the root css is returned, so this function always returns a valid css.
537 *
538 * The returned css is not guaranteed to be online, and therefore it is the
539 * callers responsiblity to tryget a reference for it.
540 */
541struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
542 struct cgroup_subsys *ss)
543{
544 struct cgroup_subsys_state *css;
545
546 do {
547 css = cgroup_css(cgrp, ss);
548
549 if (css)
550 return css;
551 cgrp = cgroup_parent(cgrp);
552 } while (cgrp);
553
554 return init_css_set.subsys[ss->id];
555}
556
557/**
Tejun Heoeeecbd12014-11-18 02:49:52 -0500558 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
559 * @cgrp: the cgroup of interest
560 * @ss: the subsystem of interest
561 *
562 * Find and get the effective css of @cgrp for @ss. The effective css is
563 * defined as the matching css of the nearest ancestor including self which
564 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
565 * the root css is returned, so this function always returns a valid css.
566 * The returned css must be put using css_put().
567 */
568struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
569 struct cgroup_subsys *ss)
570{
571 struct cgroup_subsys_state *css;
572
573 rcu_read_lock();
574
575 do {
576 css = cgroup_css(cgrp, ss);
577
578 if (css && css_tryget_online(css))
579 goto out_unlock;
580 cgrp = cgroup_parent(cgrp);
581 } while (cgrp);
582
583 css = init_css_set.subsys[ss->id];
584 css_get(css);
585out_unlock:
586 rcu_read_unlock();
587 return css;
588}
589
Tejun Heoa590b902017-04-28 15:14:55 -0400590static void cgroup_get_live(struct cgroup *cgrp)
Tejun Heo052c3f32015-10-15 16:41:50 -0400591{
592 WARN_ON_ONCE(cgroup_is_dead(cgrp));
593 css_get(&cgrp->self);
594}
595
Tejun Heob4168642014-05-13 12:16:21 -0400596struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500597{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500598 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400599 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500600
601 /*
602 * This is open and unprotected implementation of cgroup_css().
603 * seq_css() is only called from a kernfs file operation which has
604 * an active reference on the file. Because all the subsystem
605 * files are drained before a css is disassociated with a cgroup,
606 * the matching css from the cgroup's subsys table is guaranteed to
607 * be and stay valid until the enclosing operation is complete.
608 */
609 if (cft->ss)
610 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
611 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400612 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500613}
Tejun Heob4168642014-05-13 12:16:21 -0400614EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500615
Tejun Heo30159ec2013-06-25 11:53:37 -0700616/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500617 * for_each_css - iterate all css's of a cgroup
618 * @css: the iteration cursor
619 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
620 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700621 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400622 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700623 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500624#define for_each_css(css, ssid, cgrp) \
625 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
626 if (!((css) = rcu_dereference_check( \
627 (cgrp)->subsys[(ssid)], \
628 lockdep_is_held(&cgroup_mutex)))) { } \
629 else
630
631/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400632 * for_each_e_css - iterate all effective css's of a cgroup
633 * @css: the iteration cursor
634 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
635 * @cgrp: the target cgroup to iterate css's of
636 *
637 * Should be called under cgroup_[tree_]mutex.
638 */
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500639#define for_each_e_css(css, ssid, cgrp) \
640 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
641 if (!((css) = cgroup_e_css_by_mask(cgrp, \
642 cgroup_subsys[(ssid)]))) \
643 ; \
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400644 else
645
646/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500647 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000648 * @ss: the iteration cursor
649 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500650 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000651 *
652 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500653 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000654 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500655#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
656 unsigned long __ss_mask = (ss_mask); \
657 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000658 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500659 break; \
660 } \
661 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
662 (ss) = cgroup_subsys[ssid]; \
663 {
664
665#define while_each_subsys_mask() \
666 } \
667 } \
668} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000669
Tejun Heof8f22e52014-04-23 11:13:16 -0400670/* iterate over child cgrps, lock should be held throughout iteration */
671#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400672 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400673 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400674 cgroup_is_dead(child); })) \
675 ; \
676 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700677
Tejun Heoce3f1d92016-03-03 09:57:59 -0500678/* walk live descendants in preorder */
679#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
680 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
681 if (({ lockdep_assert_held(&cgroup_mutex); \
682 (dsct) = (d_css)->cgroup; \
683 cgroup_is_dead(dsct); })) \
684 ; \
685 else
686
687/* walk live descendants in postorder */
688#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
689 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
690 if (({ lockdep_assert_held(&cgroup_mutex); \
691 (dsct) = (d_css)->cgroup; \
692 cgroup_is_dead(dsct); })) \
693 ; \
694 else
695
Tejun Heo172a2c062014-03-19 10:23:53 -0400696/*
697 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700698 * hierarchies being mounted. It contains a pointer to the root state
699 * for each subsystem. Also used to anchor the list of css_sets. Not
700 * reference-counted, to improve performance when child cgroups
701 * haven't been created.
702 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400703struct css_set init_css_set = {
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200704 .refcount = REFCOUNT_INIT(1),
Tejun Heo454000a2017-05-15 09:34:02 -0400705 .dom_cset = &init_css_set,
Tejun Heo172a2c062014-03-19 10:23:53 -0400706 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
707 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500708 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo454000a2017-05-15 09:34:02 -0400709 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500710 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
Tejun Heo172a2c062014-03-19 10:23:53 -0400711 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
712 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heo38683142017-09-25 13:50:20 -0700713
714 /*
715 * The following field is re-initialized when this cset gets linked
716 * in cgroup_init(). However, let's initialize the field
717 * statically too so that the default cgroup can be accessed safely
718 * early during boot.
719 */
720 .dfl_cgrp = &cgrp_dfl_root.cgrp,
Tejun Heo172a2c062014-03-19 10:23:53 -0400721};
Paul Menage817929e2007-10-18 23:39:36 -0700722
Tejun Heo172a2c062014-03-19 10:23:53 -0400723static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700724
Tejun Heo454000a2017-05-15 09:34:02 -0400725static bool css_set_threaded(struct css_set *cset)
726{
727 return cset->dom_cset != cset;
728}
729
Tejun Heo842b5972014-04-25 18:28:02 -0400730/**
Tejun Heo0de09422015-10-15 16:41:49 -0400731 * css_set_populated - does a css_set contain any tasks?
732 * @cset: target css_set
Waiman Long73a72422017-06-13 17:18:01 -0400733 *
734 * css_set_populated() should be the same as !!cset->nr_tasks at steady
735 * state. However, css_set_populated() can be called while a task is being
736 * added to or removed from the linked list before the nr_tasks is
737 * properly updated. Hence, we can't just look at ->nr_tasks here.
Tejun Heo0de09422015-10-15 16:41:49 -0400738 */
739static bool css_set_populated(struct css_set *cset)
740{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400741 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400742
743 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
744}
745
746/**
Tejun Heo788b9502017-07-16 21:43:33 -0400747 * cgroup_update_populated - update the populated count of a cgroup
Tejun Heo842b5972014-04-25 18:28:02 -0400748 * @cgrp: the target cgroup
749 * @populated: inc or dec populated count
750 *
Tejun Heo0de09422015-10-15 16:41:49 -0400751 * One of the css_sets associated with @cgrp is either getting its first
Tejun Heo788b9502017-07-16 21:43:33 -0400752 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
753 * count is propagated towards root so that a given cgroup's
754 * nr_populated_children is zero iff none of its descendants contain any
755 * tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400756 *
Tejun Heo788b9502017-07-16 21:43:33 -0400757 * @cgrp's interface file "cgroup.populated" is zero if both
758 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
759 * 1 otherwise. When the sum changes from or to zero, userland is notified
760 * that the content of the interface file has changed. This can be used to
761 * detect when @cgrp and its descendants become populated or empty.
Tejun Heo842b5972014-04-25 18:28:02 -0400762 */
763static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
764{
Tejun Heo788b9502017-07-16 21:43:33 -0400765 struct cgroup *child = NULL;
766 int adj = populated ? 1 : -1;
767
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400768 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400769
770 do {
Tejun Heo788b9502017-07-16 21:43:33 -0400771 bool was_populated = cgroup_is_populated(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400772
Tejun Heo454000a2017-05-15 09:34:02 -0400773 if (!child) {
Tejun Heo788b9502017-07-16 21:43:33 -0400774 cgrp->nr_populated_csets += adj;
Tejun Heo454000a2017-05-15 09:34:02 -0400775 } else {
776 if (cgroup_is_threaded(child))
777 cgrp->nr_populated_threaded_children += adj;
778 else
779 cgrp->nr_populated_domain_children += adj;
780 }
Tejun Heo842b5972014-04-25 18:28:02 -0400781
Tejun Heo788b9502017-07-16 21:43:33 -0400782 if (was_populated == cgroup_is_populated(cgrp))
Tejun Heo842b5972014-04-25 18:28:02 -0400783 break;
784
Tejun Heod62beb72016-12-27 14:49:08 -0500785 cgroup1_check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400786 cgroup_file_notify(&cgrp->events_file);
787
Tejun Heo788b9502017-07-16 21:43:33 -0400788 child = cgrp;
Tejun Heod51f39b2014-05-16 13:22:48 -0400789 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400790 } while (cgrp);
791}
792
Tejun Heo0de09422015-10-15 16:41:49 -0400793/**
794 * css_set_update_populated - update populated state of a css_set
795 * @cset: target css_set
796 * @populated: whether @cset is populated or depopulated
797 *
798 * @cset is either getting the first task or losing the last. Update the
Tejun Heo788b9502017-07-16 21:43:33 -0400799 * populated counters of all associated cgroups accordingly.
Tejun Heo0de09422015-10-15 16:41:49 -0400800 */
801static void css_set_update_populated(struct css_set *cset, bool populated)
802{
803 struct cgrp_cset_link *link;
804
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400805 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400806
807 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
808 cgroup_update_populated(link->cgrp, populated);
809}
810
Tejun Heof6d7d042015-10-15 16:41:52 -0400811/**
812 * css_set_move_task - move a task from one css_set to another
813 * @task: task being moved
814 * @from_cset: css_set @task currently belongs to (may be NULL)
815 * @to_cset: new css_set @task is being moved to (may be NULL)
816 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
817 *
818 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
819 * css_set, @from_cset can be NULL. If @task is being disassociated
820 * instead of moved, @to_cset can be NULL.
821 *
Tejun Heo788b9502017-07-16 21:43:33 -0400822 * This function automatically handles populated counter updates and
Tejun Heoed27b9f2015-10-15 16:41:52 -0400823 * css_task_iter adjustments but the caller is responsible for managing
824 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400825 */
826static void css_set_move_task(struct task_struct *task,
827 struct css_set *from_cset, struct css_set *to_cset,
828 bool use_mg_tasks)
829{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400830 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400831
Tejun Heo20b454a2016-03-03 09:57:57 -0500832 if (to_cset && !css_set_populated(to_cset))
833 css_set_update_populated(to_cset, true);
834
Tejun Heof6d7d042015-10-15 16:41:52 -0400835 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400836 struct css_task_iter *it, *pos;
837
Tejun Heof6d7d042015-10-15 16:41:52 -0400838 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400839
840 /*
841 * @task is leaving, advance task iterators which are
842 * pointing to it so that they can resume at the next
843 * position. Advancing an iterator might remove it from
844 * the list, use safe walk. See css_task_iter_advance*()
845 * for details.
846 */
847 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
848 iters_node)
849 if (it->task_pos == &task->cg_list)
850 css_task_iter_advance(it);
851
Tejun Heof6d7d042015-10-15 16:41:52 -0400852 list_del_init(&task->cg_list);
853 if (!css_set_populated(from_cset))
854 css_set_update_populated(from_cset, false);
855 } else {
856 WARN_ON_ONCE(!list_empty(&task->cg_list));
857 }
858
859 if (to_cset) {
860 /*
861 * We are synchronized through cgroup_threadgroup_rwsem
862 * against PF_EXITING setting such that we can't race
863 * against cgroup_exit() changing the css_set to
864 * init_css_set and dropping the old one.
865 */
866 WARN_ON_ONCE(task->flags & PF_EXITING);
867
Johannes Weiner2ce71352018-10-26 15:06:31 -0700868 cgroup_move_task(task, to_cset);
Tejun Heof6d7d042015-10-15 16:41:52 -0400869 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
870 &to_cset->tasks);
871 }
872}
873
Paul Menage7717f7b2009-09-23 15:56:22 -0700874/*
875 * hash table for cgroup groups. This improves the performance to find
876 * an existing css_set. This hash doesn't (currently) take into
877 * account cgroups in empty hierarchies.
878 */
Li Zefan472b1052008-04-29 01:00:11 -0700879#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800880static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700881
Li Zefan0ac801f2013-01-10 11:49:27 +0800882static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700883{
Li Zefan0ac801f2013-01-10 11:49:27 +0800884 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700885 struct cgroup_subsys *ss;
886 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700887
Tejun Heo30159ec2013-06-25 11:53:37 -0700888 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800889 key += (unsigned long)css[i];
890 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700891
Li Zefan0ac801f2013-01-10 11:49:27 +0800892 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700893}
894
Tejun Heodcfe1492016-12-27 14:49:09 -0500895void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700896{
Tejun Heo69d02062013-06-12 21:04:50 -0700897 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400898 struct cgroup_subsys *ss;
899 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700900
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400901 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500902
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200903 if (!refcount_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700904 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700905
Tejun Heo454000a2017-05-15 09:34:02 -0400906 WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
907
Tejun Heo53254f92015-11-23 14:55:41 -0500908 /* This css_set is dead. unlink it and release cgroup and css refs */
909 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400910 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500911 css_put(cset->subsys[ssid]);
912 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700913 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700914 css_set_count--;
915
Tejun Heo69d02062013-06-12 21:04:50 -0700916 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700917 list_del(&link->cset_link);
918 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400919 if (cgroup_parent(link->cgrp))
920 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700921 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700922 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700923
Tejun Heo454000a2017-05-15 09:34:02 -0400924 if (css_set_threaded(cset)) {
925 list_del(&cset->threaded_csets_node);
926 put_css_set_locked(cset->dom_cset);
927 }
928
Tejun Heo5abb8852013-06-12 21:04:49 -0700929 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700930}
931
Tejun Heob326f9d2013-06-24 15:21:48 -0700932/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700933 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700934 * @cset: candidate css_set being tested
935 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700936 * @new_cgrp: cgroup that's being entered by the task
937 * @template: desired set of css pointers in css_set (pre-calculated)
938 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800939 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700940 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
941 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700942static bool compare_css_sets(struct css_set *cset,
943 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700944 struct cgroup *new_cgrp,
945 struct cgroup_subsys_state *template[])
946{
Tejun Heo454000a2017-05-15 09:34:02 -0400947 struct cgroup *new_dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700948 struct list_head *l1, *l2;
949
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400950 /*
951 * On the default hierarchy, there can be csets which are
952 * associated with the same set of cgroups but different csses.
953 * Let's first ensure that csses match.
954 */
955 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700956 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700957
Tejun Heo454000a2017-05-15 09:34:02 -0400958
959 /* @cset's domain should match the default cgroup's */
960 if (cgroup_on_dfl(new_cgrp))
961 new_dfl_cgrp = new_cgrp;
962 else
963 new_dfl_cgrp = old_cset->dfl_cgrp;
964
965 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
966 return false;
967
Paul Menage7717f7b2009-09-23 15:56:22 -0700968 /*
969 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400970 * different cgroups in hierarchies. As different cgroups may
971 * share the same effective css, this comparison is always
972 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700973 */
Tejun Heo69d02062013-06-12 21:04:50 -0700974 l1 = &cset->cgrp_links;
975 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700976 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700977 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700978 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700979
980 l1 = l1->next;
981 l2 = l2->next;
982 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700983 if (l1 == &cset->cgrp_links) {
984 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700985 break;
986 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700987 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700988 }
989 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700990 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
991 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
992 cgrp1 = link1->cgrp;
993 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700994 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700995 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700996
997 /*
998 * If this hierarchy is the hierarchy of the cgroup
999 * that's changing, then we need to check that this
1000 * css_set points to the new cgroup; if it's any other
1001 * hierarchy, then this css_set should point to the
1002 * same cgroup as the old css_set.
1003 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001004 if (cgrp1->root == new_cgrp->root) {
1005 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -07001006 return false;
1007 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -07001008 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -07001009 return false;
1010 }
1011 }
1012 return true;
1013}
1014
Tejun Heob326f9d2013-06-24 15:21:48 -07001015/**
1016 * find_existing_css_set - init css array and find the matching css_set
1017 * @old_cset: the css_set that we're using before the cgroup transition
1018 * @cgrp: the cgroup that we're moving into
1019 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -07001020 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001021static struct css_set *find_existing_css_set(struct css_set *old_cset,
1022 struct cgroup *cgrp,
1023 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -07001024{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001025 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -07001026 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07001027 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08001028 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -07001029 int i;
Paul Menage817929e2007-10-18 23:39:36 -07001030
Ben Blumaae8aab2010-03-10 15:22:07 -08001031 /*
1032 * Build the set of subsystem state objects that we want to see in the
1033 * new css_set. while subsystems can change globally, the entries here
1034 * won't change, so no need for locking.
1035 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001036 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -04001037 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001038 /*
1039 * @ss is in this hierarchy, so we want the
1040 * effective css from @cgrp.
1041 */
Dennis Zhoufc5a8282018-12-05 12:10:36 -05001042 template[i] = cgroup_e_css_by_mask(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -07001043 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001044 /*
1045 * @ss is not in this hierarchy, so we don't want
1046 * to change the css.
1047 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001048 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -07001049 }
1050 }
1051
Li Zefan0ac801f2013-01-10 11:49:27 +08001052 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -07001053 hash_for_each_possible(css_set_table, cset, hlist, key) {
1054 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -07001055 continue;
1056
1057 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -07001058 return cset;
Li Zefan472b1052008-04-29 01:00:11 -07001059 }
Paul Menage817929e2007-10-18 23:39:36 -07001060
1061 /* No existing cgroup group matched */
1062 return NULL;
1063}
1064
Tejun Heo69d02062013-06-12 21:04:50 -07001065static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -07001066{
Tejun Heo69d02062013-06-12 21:04:50 -07001067 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001068
Tejun Heo69d02062013-06-12 21:04:50 -07001069 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1070 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07001071 kfree(link);
1072 }
1073}
1074
Tejun Heo69d02062013-06-12 21:04:50 -07001075/**
1076 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1077 * @count: the number of links to allocate
1078 * @tmp_links: list_head the allocated links are put on
1079 *
1080 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1081 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -07001082 */
Tejun Heo69d02062013-06-12 21:04:50 -07001083static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -07001084{
Tejun Heo69d02062013-06-12 21:04:50 -07001085 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -07001086 int i;
Tejun Heo69d02062013-06-12 21:04:50 -07001087
1088 INIT_LIST_HEAD(tmp_links);
1089
Li Zefan36553432008-07-29 22:33:19 -07001090 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -07001091 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -07001092 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -07001093 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001094 return -ENOMEM;
1095 }
Tejun Heo69d02062013-06-12 21:04:50 -07001096 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001097 }
1098 return 0;
1099}
1100
Li Zefanc12f65d2009-01-07 18:07:42 -08001101/**
1102 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -07001103 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -07001104 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -08001105 * @cgrp: the destination cgroup
1106 */
Tejun Heo69d02062013-06-12 21:04:50 -07001107static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1108 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -08001109{
Tejun Heo69d02062013-06-12 21:04:50 -07001110 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -08001111
Tejun Heo69d02062013-06-12 21:04:50 -07001112 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -04001113
1114 if (cgroup_on_dfl(cgrp))
1115 cset->dfl_cgrp = cgrp;
1116
Tejun Heo69d02062013-06-12 21:04:50 -07001117 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1118 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07001119 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -04001120
Paul Menage7717f7b2009-09-23 15:56:22 -07001121 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001122 * Always add links to the tail of the lists so that the lists are
1123 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001124 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001125 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001126 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001127
1128 if (cgroup_parent(cgrp))
Tejun Heoa590b902017-04-28 15:14:55 -04001129 cgroup_get_live(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001130}
1131
Tejun Heob326f9d2013-06-24 15:21:48 -07001132/**
1133 * find_css_set - return a new css_set with one cgroup updated
1134 * @old_cset: the baseline css_set
1135 * @cgrp: the cgroup to be updated
1136 *
1137 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1138 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001139 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001140static struct css_set *find_css_set(struct css_set *old_cset,
1141 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001142{
Tejun Heob326f9d2013-06-24 15:21:48 -07001143 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001144 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001145 struct list_head tmp_links;
1146 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001147 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001148 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001149 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001150
Tejun Heob326f9d2013-06-24 15:21:48 -07001151 lockdep_assert_held(&cgroup_mutex);
1152
Paul Menage817929e2007-10-18 23:39:36 -07001153 /* First see if we already have a cgroup group that matches
1154 * the desired set */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001155 spin_lock_irq(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001156 cset = find_existing_css_set(old_cset, cgrp, template);
1157 if (cset)
1158 get_css_set(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001159 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001160
Tejun Heo5abb8852013-06-12 21:04:49 -07001161 if (cset)
1162 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001163
Tejun Heof4f4be22013-06-12 21:04:51 -07001164 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001165 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001166 return NULL;
1167
Tejun Heo69d02062013-06-12 21:04:50 -07001168 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001169 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001170 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001171 return NULL;
1172 }
1173
Elena Reshetova4b9502e62017-03-08 10:00:40 +02001174 refcount_set(&cset->refcount, 1);
Tejun Heo454000a2017-05-15 09:34:02 -04001175 cset->dom_cset = cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07001176 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001177 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001178 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo454000a2017-05-15 09:34:02 -04001179 INIT_LIST_HEAD(&cset->threaded_csets);
Tejun Heo5abb8852013-06-12 21:04:49 -07001180 INIT_HLIST_NODE(&cset->hlist);
Tejun Heo5f617ebb2016-12-27 14:49:05 -05001181 INIT_LIST_HEAD(&cset->cgrp_links);
1182 INIT_LIST_HEAD(&cset->mg_preload_node);
1183 INIT_LIST_HEAD(&cset->mg_node);
Paul Menage817929e2007-10-18 23:39:36 -07001184
1185 /* Copy the set of subsystem state objects generated in
1186 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001187 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001188
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001189 spin_lock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001190 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001191 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001192 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001193
Paul Menage7717f7b2009-09-23 15:56:22 -07001194 if (c->root == cgrp->root)
1195 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001196 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001197 }
Paul Menage817929e2007-10-18 23:39:36 -07001198
Tejun Heo69d02062013-06-12 21:04:50 -07001199 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001200
Paul Menage817929e2007-10-18 23:39:36 -07001201 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001202
Tejun Heo2d8f2432014-04-23 11:13:15 -04001203 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001204 key = css_set_hash(cset->subsys);
1205 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001206
Tejun Heo53254f92015-11-23 14:55:41 -05001207 for_each_subsys(ss, ssid) {
1208 struct cgroup_subsys_state *css = cset->subsys[ssid];
1209
Tejun Heo2d8f2432014-04-23 11:13:15 -04001210 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001211 &css->cgroup->e_csets[ssid]);
1212 css_get(css);
1213 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001214
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001215 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001216
Tejun Heo454000a2017-05-15 09:34:02 -04001217 /*
1218 * If @cset should be threaded, look up the matching dom_cset and
1219 * link them up. We first fully initialize @cset then look for the
1220 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1221 * to stay empty until we return.
1222 */
1223 if (cgroup_is_threaded(cset->dfl_cgrp)) {
1224 struct css_set *dcset;
1225
1226 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1227 if (!dcset) {
1228 put_css_set(cset);
1229 return NULL;
1230 }
1231
1232 spin_lock_irq(&css_set_lock);
1233 cset->dom_cset = dcset;
1234 list_add_tail(&cset->threaded_csets_node,
1235 &dcset->threaded_csets);
1236 spin_unlock_irq(&css_set_lock);
1237 }
1238
Tejun Heo5abb8852013-06-12 21:04:49 -07001239 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001240}
1241
Tejun Heo0a268db2016-12-27 14:49:06 -05001242struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001243{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001244 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001245
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001246 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001247}
1248
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001249static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001250{
1251 int id;
1252
1253 lockdep_assert_held(&cgroup_mutex);
1254
Tejun Heo985ed672014-03-19 10:23:53 -04001255 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001256 if (id < 0)
1257 return id;
1258
1259 root->hierarchy_id = id;
1260 return 0;
1261}
1262
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001263static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001264{
1265 lockdep_assert_held(&cgroup_mutex);
1266
Johannes Weiner8c8a5502016-06-17 12:23:59 -04001267 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heof2e85d52014-02-11 11:52:49 -05001268}
1269
Tejun Heo1592c9b2016-12-27 14:49:08 -05001270void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001271{
1272 if (root) {
Tejun Heof2e85d52014-02-11 11:52:49 -05001273 idr_destroy(&root->cgroup_idr);
1274 kfree(root);
1275 }
1276}
1277
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001278static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001279{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001280 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001281 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001282
Tejun Heoed1777d2016-08-10 11:23:44 -04001283 trace_cgroup_destroy_root(root);
1284
Tejun Heo334c3672016-03-03 09:58:01 -05001285 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -05001286
Tejun Heo776f02f2014-02-12 09:29:50 -05001287 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001288 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001289
Tejun Heof2e85d52014-02-11 11:52:49 -05001290 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo334c3672016-03-03 09:58:01 -05001291 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -05001292
1293 /*
1294 * Release all the links from cset_links to this hierarchy's
1295 * root cgroup
1296 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001297 spin_lock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001298
1299 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1300 list_del(&link->cset_link);
1301 list_del(&link->cgrp_link);
1302 kfree(link);
1303 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001304
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001305 spin_unlock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001306
1307 if (!list_empty(&root->root_list)) {
1308 list_del(&root->root_list);
1309 cgroup_root_count--;
1310 }
1311
1312 cgroup_exit_root_id(root);
1313
1314 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001315
Tejun Heo2bd59d42014-02-11 11:52:49 -05001316 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001317 cgroup_free_root(root);
1318}
1319
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001320/*
1321 * look up cgroup associated with current task's cgroup namespace on the
1322 * specified hierarchy
1323 */
1324static struct cgroup *
1325current_cgns_cgroup_from_root(struct cgroup_root *root)
1326{
1327 struct cgroup *res = NULL;
1328 struct css_set *cset;
1329
1330 lockdep_assert_held(&css_set_lock);
1331
1332 rcu_read_lock();
1333
1334 cset = current->nsproxy->cgroup_ns->root_cset;
1335 if (cset == &init_css_set) {
1336 res = &root->cgrp;
1337 } else {
1338 struct cgrp_cset_link *link;
1339
1340 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1341 struct cgroup *c = link->cgrp;
1342
1343 if (c->root == root) {
1344 res = c;
1345 break;
1346 }
1347 }
1348 }
1349 rcu_read_unlock();
1350
1351 BUG_ON(!res);
1352 return res;
1353}
1354
Tejun Heoceb6a082014-02-25 10:04:02 -05001355/* look up cgroup associated with given css_set on the specified hierarchy */
1356static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001357 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001358{
Paul Menage7717f7b2009-09-23 15:56:22 -07001359 struct cgroup *res = NULL;
1360
Tejun Heo96d365e2014-02-13 06:58:40 -05001361 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001362 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001363
Tejun Heo5abb8852013-06-12 21:04:49 -07001364 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001365 res = &root->cgrp;
Tejun Heo13d82fb2017-08-02 15:39:38 -07001366 } else if (root == &cgrp_dfl_root) {
1367 res = cset->dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001368 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001369 struct cgrp_cset_link *link;
1370
1371 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001372 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001373
Paul Menage7717f7b2009-09-23 15:56:22 -07001374 if (c->root == root) {
1375 res = c;
1376 break;
1377 }
1378 }
1379 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001380
Paul Menage7717f7b2009-09-23 15:56:22 -07001381 BUG_ON(!res);
1382 return res;
1383}
1384
1385/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001386 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001387 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001388 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001389struct cgroup *task_cgroup_from_root(struct task_struct *task,
1390 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001391{
1392 /*
1393 * No need to lock the task - since we hold cgroup_mutex the
1394 * task can't change groups, so the only thing that can happen
1395 * is that it exits and its css is set back to init_css_set.
1396 */
1397 return cset_cgroup_from_root(task_css_set(task), root);
1398}
1399
1400/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 * A task must hold cgroup_mutex to modify cgroups.
1402 *
1403 * Any task can increment and decrement the count field without lock.
1404 * So in general, code holding cgroup_mutex can't rely on the count
1405 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001406 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407 * means that no tasks are currently attached, therefore there is no
1408 * way a task attached to that cgroup can fork (the other way to
1409 * increment the count). So code holding cgroup_mutex can safely
1410 * assume that if the count is zero, it will stay zero. Similarly, if
1411 * a task holds cgroup_mutex on a cgroup with zero count, it
1412 * knows that the cgroup won't be removed, as cgroup_rmdir()
1413 * needs that mutex.
1414 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415 * A cgroup can only be deleted if both its 'count' of using tasks
1416 * is zero, and its list of 'children' cgroups is empty. Since all
1417 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001418 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001420 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421 *
1422 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001423 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424 */
1425
Tejun Heo2bd59d42014-02-11 11:52:49 -05001426static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Paul Menagea4243162007-10-18 23:39:35 -07001427
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001428static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1429 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001431 struct cgroup_subsys *ss = cft->ss;
1432
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001433 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
Tejun Heoc1bbd932018-11-13 12:06:41 -08001434 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
1435 const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
1436
1437 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
1438 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001439 cft->name);
Tejun Heoc1bbd932018-11-13 12:06:41 -08001440 } else {
Tejun Heo08a77672018-01-09 07:21:15 -08001441 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
Tejun Heoc1bbd932018-11-13 12:06:41 -08001442 }
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001443 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444}
1445
Tejun Heof2e85d52014-02-11 11:52:49 -05001446/**
1447 * cgroup_file_mode - deduce file mode of a control file
1448 * @cft: the control file in question
1449 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001450 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001451 */
1452static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001453{
Tejun Heof2e85d52014-02-11 11:52:49 -05001454 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001455
Tejun Heof2e85d52014-02-11 11:52:49 -05001456 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1457 mode |= S_IRUGO;
1458
Tejun Heo7dbdb192015-09-18 17:54:23 -04001459 if (cft->write_u64 || cft->write_s64 || cft->write) {
1460 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1461 mode |= S_IWUGO;
1462 else
1463 mode |= S_IWUSR;
1464 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001465
1466 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001467}
1468
Tejun Heoa9746d82014-05-13 12:19:22 -04001469/**
Tejun Heo8699b772016-02-22 22:25:46 -05001470 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001471 * @subtree_control: the new subtree_control mask to consider
Tejun Heo5ced2512016-03-03 09:58:01 -05001472 * @this_ss_mask: available subsystems
Tejun Heoaf0ba672014-07-08 18:02:57 -04001473 *
1474 * On the default hierarchy, a subsystem may request other subsystems to be
1475 * enabled together through its ->depends_on mask. In such cases, more
1476 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1477 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001478 * This function calculates which subsystems need to be enabled if
Tejun Heo5ced2512016-03-03 09:58:01 -05001479 * @subtree_control is to be applied while restricted to @this_ss_mask.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001480 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001481static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
Tejun Heo667c2492014-07-08 18:02:56 -04001482{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001483 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001484 struct cgroup_subsys *ss;
1485 int ssid;
1486
1487 lockdep_assert_held(&cgroup_mutex);
1488
Tejun Heof6d635ad2016-03-08 11:51:26 -05001489 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1490
Tejun Heoaf0ba672014-07-08 18:02:57 -04001491 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001492 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001493
Tejun Heob4e0eea2016-02-22 22:25:46 -05001494 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001495 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001496 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001497
1498 /*
1499 * Mask out subsystems which aren't available. This can
1500 * happen only if some depended-upon subsystems were bound
1501 * to non-default hierarchies.
1502 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001503 new_ss_mask &= this_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001504
1505 if (new_ss_mask == cur_ss_mask)
1506 break;
1507 cur_ss_mask = new_ss_mask;
1508 }
1509
Tejun Heo0f060de2014-11-18 02:49:50 -05001510 return cur_ss_mask;
1511}
1512
1513/**
Tejun Heoa9746d82014-05-13 12:19:22 -04001514 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1515 * @kn: the kernfs_node being serviced
1516 *
1517 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1518 * the method finishes if locking succeeded. Note that once this function
1519 * returns the cgroup returned by cgroup_kn_lock_live() may become
1520 * inaccessible any time. If the caller intends to continue to access the
1521 * cgroup, it should pin it before invoking this function.
1522 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001523void cgroup_kn_unlock(struct kernfs_node *kn)
Tejun Heoa9746d82014-05-13 12:19:22 -04001524{
1525 struct cgroup *cgrp;
1526
1527 if (kernfs_type(kn) == KERNFS_DIR)
1528 cgrp = kn->priv;
1529 else
1530 cgrp = kn->parent->priv;
1531
1532 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001533
1534 kernfs_unbreak_active_protection(kn);
1535 cgroup_put(cgrp);
1536}
1537
1538/**
1539 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1540 * @kn: the kernfs_node being serviced
Tejun Heo945ba192016-03-03 09:58:00 -05001541 * @drain_offline: perform offline draining on the cgroup
Tejun Heoa9746d82014-05-13 12:19:22 -04001542 *
1543 * This helper is to be used by a cgroup kernfs method currently servicing
1544 * @kn. It breaks the active protection, performs cgroup locking and
1545 * verifies that the associated cgroup is alive. Returns the cgroup if
1546 * alive; otherwise, %NULL. A successful return should be undone by a
Tejun Heo945ba192016-03-03 09:58:00 -05001547 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1548 * cgroup is drained of offlining csses before return.
Tejun Heoa9746d82014-05-13 12:19:22 -04001549 *
1550 * Any cgroup kernfs method implementation which requires locking the
1551 * associated cgroup should use this helper. It avoids nesting cgroup
1552 * locking under kernfs active protection and allows all kernfs operations
1553 * including self-removal.
1554 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001555struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
Tejun Heoa9746d82014-05-13 12:19:22 -04001556{
1557 struct cgroup *cgrp;
1558
1559 if (kernfs_type(kn) == KERNFS_DIR)
1560 cgrp = kn->priv;
1561 else
1562 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563
Tejun Heo2bd59d42014-02-11 11:52:49 -05001564 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001565 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001566 * active_ref. cgroup liveliness check alone provides enough
1567 * protection against removal. Ensure @cgrp stays accessible and
1568 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001569 */
Li Zefanaa323622014-09-04 14:43:38 +08001570 if (!cgroup_tryget(cgrp))
1571 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001572 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573
Tejun Heo945ba192016-03-03 09:58:00 -05001574 if (drain_offline)
1575 cgroup_lock_and_drain_offline(cgrp);
1576 else
1577 mutex_lock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001578
1579 if (!cgroup_is_dead(cgrp))
1580 return cgrp;
1581
1582 cgroup_kn_unlock(kn);
1583 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584}
1585
Li Zefan2739d3c2013-01-21 18:18:33 +08001586static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001588 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589
Tejun Heo01f64742014-05-13 12:19:23 -04001590 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001591
1592 if (cft->file_offset) {
1593 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1594 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1595
1596 spin_lock_irq(&cgroup_file_kn_lock);
1597 cfile->kn = NULL;
1598 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heob12e3582018-04-26 14:29:04 -07001599
1600 del_timer_sync(&cfile->notify_timer);
Tejun Heo34c06252015-11-05 00:12:24 -05001601 }
1602
Tejun Heo2bd59d42014-02-11 11:52:49 -05001603 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001604}
1605
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001606/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001607 * css_clear_dir - remove subsys files in a cgroup directory
1608 * @css: taget css
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001609 */
Tejun Heo334c3672016-03-03 09:58:01 -05001610static void css_clear_dir(struct cgroup_subsys_state *css)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001611{
Tejun Heo334c3672016-03-03 09:58:01 -05001612 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001613 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001614
Tejun Heo88cb04b2016-03-03 09:57:58 -05001615 if (!(css->flags & CSS_VISIBLE))
1616 return;
1617
1618 css->flags &= ~CSS_VISIBLE;
1619
Tejun Heo5faaf052018-04-26 14:29:04 -07001620 if (!css->ss) {
1621 if (cgroup_on_dfl(cgrp))
1622 cfts = cgroup_base_files;
1623 else
1624 cfts = cgroup1_base_files;
1625
Tejun Heo4df8dc92015-09-18 17:54:23 -04001626 cgroup_addrm_files(css, cgrp, cfts, false);
Tejun Heo5faaf052018-04-26 14:29:04 -07001627 } else {
1628 list_for_each_entry(cfts, &css->ss->cfts, node)
1629 cgroup_addrm_files(css, cgrp, cfts, false);
1630 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631}
1632
Tejun Heoccdca212015-09-18 17:54:23 -04001633/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001634 * css_populate_dir - create subsys files in a cgroup directory
1635 * @css: target css
Tejun Heoccdca212015-09-18 17:54:23 -04001636 *
1637 * On failure, no file is added.
1638 */
Tejun Heo334c3672016-03-03 09:58:01 -05001639static int css_populate_dir(struct cgroup_subsys_state *css)
Tejun Heoccdca212015-09-18 17:54:23 -04001640{
Tejun Heo334c3672016-03-03 09:58:01 -05001641 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001642 struct cftype *cfts, *failed_cfts;
1643 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001644
Tejun Heo03970d32016-03-03 09:58:00 -05001645 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
Tejun Heo88cb04b2016-03-03 09:57:58 -05001646 return 0;
1647
Tejun Heo4df8dc92015-09-18 17:54:23 -04001648 if (!css->ss) {
1649 if (cgroup_on_dfl(cgrp))
Tejun Heod62beb72016-12-27 14:49:08 -05001650 cfts = cgroup_base_files;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001651 else
Tejun Heod62beb72016-12-27 14:49:08 -05001652 cfts = cgroup1_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001653
Tejun Heo5faaf052018-04-26 14:29:04 -07001654 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1655 if (ret < 0)
1656 return ret;
1657 } else {
1658 list_for_each_entry(cfts, &css->ss->cfts, node) {
1659 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1660 if (ret < 0) {
1661 failed_cfts = cfts;
1662 goto err;
1663 }
Tejun Heoccdca212015-09-18 17:54:23 -04001664 }
1665 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001666
1667 css->flags |= CSS_VISIBLE;
1668
Tejun Heoccdca212015-09-18 17:54:23 -04001669 return 0;
1670err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001671 list_for_each_entry(cfts, &css->ss->cfts, node) {
1672 if (cfts == failed_cfts)
1673 break;
1674 cgroup_addrm_files(css, cgrp, cfts, false);
1675 }
Tejun Heoccdca212015-09-18 17:54:23 -04001676 return ret;
1677}
1678
Tejun Heo0a268db2016-12-27 14:49:06 -05001679int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680{
Tejun Heo1ada4832015-09-18 17:54:23 -04001681 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001682 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001683 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684
Tejun Heoace2bee2014-02-11 11:52:47 -05001685 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001686
Tejun Heob4e0eea2016-02-22 22:25:46 -05001687 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heof6d635ad2016-03-08 11:51:26 -05001688 /*
1689 * If @ss has non-root csses attached to it, can't move.
1690 * If @ss is an implicit controller, it is exempt from this
1691 * rule and can be stolen.
1692 */
1693 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1694 !ss->implicit_on_dfl)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001695 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
Tejun Heo5df36032014-03-19 10:23:54 -04001697 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001698 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001699 return -EBUSY;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001700 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701
Tejun Heob4e0eea2016-02-22 22:25:46 -05001702 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001703 struct cgroup_root *src_root = ss->root;
1704 struct cgroup *scgrp = &src_root->cgrp;
1705 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001706 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001707
Tejun Heo1ada4832015-09-18 17:54:23 -04001708 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001709
Tejun Heo334c3672016-03-03 09:58:01 -05001710 /* disable from the source */
1711 src_root->subsys_mask &= ~(1 << ssid);
1712 WARN_ON(cgroup_apply_control(scgrp));
1713 cgroup_finalize_control(scgrp, 0);
Tejun Heo4df8dc92015-09-18 17:54:23 -04001714
Tejun Heo334c3672016-03-03 09:58:01 -05001715 /* rebind */
Tejun Heo1ada4832015-09-18 17:54:23 -04001716 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1717 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001718 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001719 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001720
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001721 spin_lock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001722 hash_for_each(css_set_table, i, cset, hlist)
1723 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001724 &dcgrp->e_csets[ss->id]);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001725 spin_unlock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001726
Tejun Heobd53d612014-04-23 11:13:16 -04001727 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001728 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001729 if (dst_root == &cgrp_dfl_root) {
1730 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1731 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001732 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001733 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001734 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001735
Tejun Heo334c3672016-03-03 09:58:01 -05001736 ret = cgroup_apply_control(dcgrp);
1737 if (ret)
1738 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1739 ss->name, ret);
1740
Tejun Heo5df36032014-03-19 10:23:54 -04001741 if (ss->bind)
1742 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001743 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744
Tejun Heo1ada4832015-09-18 17:54:23 -04001745 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746 return 0;
1747}
1748
Tejun Heo1592c9b2016-12-27 14:49:08 -05001749int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1750 struct kernfs_root *kf_root)
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001751{
Felipe Balbi09be4c82016-05-12 12:34:38 +03001752 int len = 0;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001753 char *buf = NULL;
1754 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1755 struct cgroup *ns_cgroup;
1756
1757 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1758 if (!buf)
1759 return -ENOMEM;
1760
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001761 spin_lock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001762 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1763 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001764 spin_unlock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001765
1766 if (len >= PATH_MAX)
1767 len = -ERANGE;
1768 else if (len > 0) {
1769 seq_escape(sf, buf, " \t\n\\");
1770 len = 0;
1771 }
1772 kfree(buf);
1773 return len;
1774}
1775
Al Viroe34a98d2019-01-17 00:22:58 -05001776enum cgroup2_param {
1777 Opt_nsdelegate,
1778 nr__cgroup2_params
1779};
1780
1781static const struct fs_parameter_spec cgroup2_param_specs[] = {
1782 fsparam_flag ("nsdelegate", Opt_nsdelegate),
1783 {}
1784};
1785
1786static const struct fs_parameter_description cgroup2_fs_parameters = {
1787 .name = "cgroup2",
1788 .specs = cgroup2_param_specs,
1789};
1790
1791static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param)
Tejun Heo5136f632017-06-27 14:30:28 -04001792{
Al Viroe34a98d2019-01-17 00:22:58 -05001793 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1794 struct fs_parse_result result;
1795 int opt;
Tejun Heo5136f632017-06-27 14:30:28 -04001796
Al Viroe34a98d2019-01-17 00:22:58 -05001797 opt = fs_parse(fc, &cgroup2_fs_parameters, param, &result);
1798 if (opt < 0)
1799 return opt;
Tejun Heo5136f632017-06-27 14:30:28 -04001800
Al Viroe34a98d2019-01-17 00:22:58 -05001801 switch (opt) {
1802 case Opt_nsdelegate:
1803 ctx->flags |= CGRP_ROOT_NS_DELEGATE;
Tejun Heo5136f632017-06-27 14:30:28 -04001804 return 0;
Tejun Heo5136f632017-06-27 14:30:28 -04001805 }
Al Viroe34a98d2019-01-17 00:22:58 -05001806 return -EINVAL;
Tejun Heo5136f632017-06-27 14:30:28 -04001807}
1808
1809static void apply_cgroup_root_flags(unsigned int root_flags)
1810{
1811 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
1812 if (root_flags & CGRP_ROOT_NS_DELEGATE)
1813 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
1814 else
1815 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
1816 }
1817}
1818
1819static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
1820{
1821 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
1822 seq_puts(seq, ",nsdelegate");
1823 return 0;
1824}
1825
Al Viro90129622019-01-05 00:38:03 -05001826static int cgroup_reconfigure(struct fs_context *fc)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827{
Al Viro90129622019-01-05 00:38:03 -05001828 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
Tejun Heo5136f632017-06-27 14:30:28 -04001829
Al Virof5dfb532019-01-16 23:42:38 -05001830 apply_cgroup_root_flags(ctx->flags);
Tejun Heo5136f632017-06-27 14:30:28 -04001831 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001832}
1833
Tejun Heoafeb0f92014-02-13 06:58:39 -05001834/*
1835 * To reduce the fork() overhead for systems that are not actually using
1836 * their cgroups capability, we don't maintain the lists running through
1837 * each css_set to its tasks until we see the list actually used - in other
1838 * words after the first mount.
1839 */
1840static bool use_task_css_set_links __read_mostly;
1841
1842static void cgroup_enable_task_cg_lists(void)
1843{
1844 struct task_struct *p, *g;
1845
Tejun Heoafeb0f92014-02-13 06:58:39 -05001846 /*
1847 * We need tasklist_lock because RCU is not safe against
1848 * while_each_thread(). Besides, a forking task that has passed
1849 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1850 * is not guaranteed to have its child immediately visible in the
1851 * tasklist if we walk through it with RCU.
1852 */
1853 read_lock(&tasklist_lock);
Tejun Heod8742e22018-05-23 11:04:54 -07001854 spin_lock_irq(&css_set_lock);
1855
1856 if (use_task_css_set_links)
1857 goto out_unlock;
1858
1859 use_task_css_set_links = true;
1860
Tejun Heoafeb0f92014-02-13 06:58:39 -05001861 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001862 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1863 task_css_set(p) != &init_css_set);
1864
1865 /*
1866 * We should check if the process is exiting, otherwise
1867 * it will race with cgroup_exit() in that the list
1868 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001869 * Do it while holding siglock so that we don't end up
1870 * racing against cgroup_exit().
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001871 *
1872 * Interrupts were already disabled while acquiring
1873 * the css_set_lock, so we do not need to disable it
1874 * again when acquiring the sighand->siglock here.
Tejun Heoafeb0f92014-02-13 06:58:39 -05001875 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001876 spin_lock(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001877 if (!(p->flags & PF_EXITING)) {
1878 struct css_set *cset = task_css_set(p);
1879
Tejun Heo0de09422015-10-15 16:41:49 -04001880 if (!css_set_populated(cset))
1881 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001882 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001883 get_css_set(cset);
Waiman Long73a72422017-06-13 17:18:01 -04001884 cset->nr_tasks++;
Tejun Heoeaf797a2014-02-25 10:04:03 -05001885 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001886 spin_unlock(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001887 } while_each_thread(g, p);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001888out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001889 spin_unlock_irq(&css_set_lock);
Tejun Heod8742e22018-05-23 11:04:54 -07001890 read_unlock(&tasklist_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001891}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001892
Paul Menagecc31edc2008-10-18 20:28:04 -07001893static void init_cgroup_housekeeping(struct cgroup *cgrp)
1894{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001895 struct cgroup_subsys *ss;
1896 int ssid;
1897
Tejun Heod5c419b2014-05-16 13:22:48 -04001898 INIT_LIST_HEAD(&cgrp->self.sibling);
1899 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001900 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001901 INIT_LIST_HEAD(&cgrp->pidlists);
1902 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001903 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001904 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo454000a2017-05-15 09:34:02 -04001905 cgrp->dom_cgrp = cgrp;
Roman Gushchin1a926e02017-07-28 18:28:44 +01001906 cgrp->max_descendants = INT_MAX;
1907 cgrp->max_depth = INT_MAX;
Tejun Heo8f534702018-04-26 14:29:05 -07001908 INIT_LIST_HEAD(&cgrp->rstat_css_list);
Tejun Heod4ff7492018-04-26 14:29:04 -07001909 prev_cputime_init(&cgrp->prev_cputime);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001910
1911 for_each_subsys(ss, ssid)
1912 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001913
1914 init_waitqueue_head(&cgrp->offline_waitq);
Tejun Heod62beb72016-12-27 14:49:08 -05001915 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001916}
Paul Menagec6d57f32009-09-23 15:56:19 -07001917
Al Virocf6299b12019-01-17 02:25:51 -05001918void init_cgroup_root(struct cgroup_fs_context *ctx)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001919{
Al Virocf6299b12019-01-17 02:25:51 -05001920 struct cgroup_root *root = ctx->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001921 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001922
Paul Menageddbcc7e2007-10-18 23:39:30 -07001923 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001924 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001925 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001926 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001927 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001928
Al Virof5dfb532019-01-16 23:42:38 -05001929 root->flags = ctx->flags;
1930 if (ctx->release_agent)
1931 strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX);
1932 if (ctx->name)
1933 strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN);
1934 if (ctx->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001935 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001936}
1937
Al Viro35ac1182019-01-12 00:20:54 -05001938int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001939{
Tejun Heod427dfe2014-02-11 11:52:48 -05001940 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001941 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heofa069902016-12-27 14:49:07 -05001942 struct kernfs_syscall_ops *kf_sops;
Tejun Heod427dfe2014-02-11 11:52:48 -05001943 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001944 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001945
Tejun Heod427dfe2014-02-11 11:52:48 -05001946 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001947
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001948 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001949 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001950 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001951 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001952 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001953
Zefan Li9732adc2017-04-19 10:15:59 +08001954 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
Al Viro35ac1182019-01-12 00:20:54 -05001955 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001956 if (ret)
1957 goto out;
1958
Tejun Heod427dfe2014-02-11 11:52:48 -05001959 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001960 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001961 * but that's OK - it can only be increased by someone holding
Tejun Heo04313592016-03-03 09:58:01 -05001962 * cgroup_lock, and that's us. Later rebinding may disable
1963 * controllers on the default hierarchy and thus create new csets,
1964 * which can't be more than the existing ones. Allocate 2x.
Tejun Heod427dfe2014-02-11 11:52:48 -05001965 */
Tejun Heo04313592016-03-03 09:58:01 -05001966 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001967 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001968 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001969
Tejun Heo985ed672014-03-19 10:23:53 -04001970 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001971 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001972 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001973
Tejun Heofa069902016-12-27 14:49:07 -05001974 kf_sops = root == &cgrp_dfl_root ?
1975 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1976
1977 root->kf_root = kernfs_create_root(kf_sops,
Shaohua Liaa818822017-07-12 11:49:51 -07001978 KERNFS_ROOT_CREATE_DEACTIVATED |
1979 KERNFS_ROOT_SUPPORT_EXPORTOP,
Tejun Heo2bd59d42014-02-11 11:52:49 -05001980 root_cgrp);
1981 if (IS_ERR(root->kf_root)) {
1982 ret = PTR_ERR(root->kf_root);
1983 goto exit_root_id;
1984 }
1985 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001986
Tejun Heo334c3672016-03-03 09:58:01 -05001987 ret = css_populate_dir(&root_cgrp->self);
Tejun Heod427dfe2014-02-11 11:52:48 -05001988 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001989 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001990
Tejun Heo5df36032014-03-19 10:23:54 -04001991 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001992 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001993 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001994
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001995 ret = cgroup_bpf_inherit(root_cgrp);
1996 WARN_ON_ONCE(ret);
1997
Tejun Heoed1777d2016-08-10 11:23:44 -04001998 trace_cgroup_setup_root(root);
1999
Tejun Heod427dfe2014-02-11 11:52:48 -05002000 /*
2001 * There must be no failure case after here, since rebinding takes
2002 * care of subsystems' refcounts, which are explicitly dropped in
2003 * the failure exit path.
2004 */
2005 list_add(&root->root_list, &cgroup_roots);
2006 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002007
Tejun Heod427dfe2014-02-11 11:52:48 -05002008 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002009 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05002010 * objects.
2011 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002012 spin_lock_irq(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04002013 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05002014 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04002015 if (css_set_populated(cset))
2016 cgroup_update_populated(root_cgrp, true);
2017 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002018 spin_unlock_irq(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002019
Tejun Heod5c419b2014-05-16 13:22:48 -04002020 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05002021 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05002022
Tejun Heo2bd59d42014-02-11 11:52:49 -05002023 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05002024 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002025 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05002026
Tejun Heo2bd59d42014-02-11 11:52:49 -05002027destroy_root:
2028 kernfs_destroy_root(root->kf_root);
2029 root->kf_root = NULL;
2030exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05002031 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04002032cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04002033 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002034out:
Tejun Heod427dfe2014-02-11 11:52:48 -05002035 free_cgrp_cset_links(&tmp_links);
2036 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002037}
2038
Al Viro71d883c2019-01-17 02:44:07 -05002039int cgroup_do_mount(struct fs_context *fc, unsigned long magic,
2040 struct cgroup_namespace *ns)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002041{
Al Viro71d883c2019-01-17 02:44:07 -05002042 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
Al Viro399504e2019-01-06 11:41:29 -05002043 bool new_sb = false;
Al Viro71d883c2019-01-17 02:44:07 -05002044 int ret = 0;
Paul Menagec6d57f32009-09-23 15:56:19 -07002045
Al Viro71d883c2019-01-17 02:44:07 -05002046 fc->root = kernfs_mount(fc->fs_type, fc->sb_flags, ctx->root->kf_root,
2047 magic, &new_sb);
2048 if (IS_ERR(fc->root))
2049 ret = PTR_ERR(fc->root);
Serge Hallyned825712016-01-29 02:54:09 -06002050
Paul Menagec6d57f32009-09-23 15:56:19 -07002051 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002052 * In non-init cgroup namespace, instead of root cgroup's dentry,
2053 * we return the dentry corresponding to the cgroupns->root_cgrp.
Serge Hallyned825712016-01-29 02:54:09 -06002054 */
Al Viro71d883c2019-01-17 02:44:07 -05002055 if (!ret && ns != &init_cgroup_ns) {
Serge Hallyned825712016-01-29 02:54:09 -06002056 struct dentry *nsdentry;
Al Viro71d883c2019-01-17 02:44:07 -05002057 struct super_block *sb = fc->root->d_sb;
Serge Hallyned825712016-01-29 02:54:09 -06002058 struct cgroup *cgrp;
2059
2060 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002061 spin_lock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002062
Al Viro71d883c2019-01-17 02:44:07 -05002063 cgrp = cset_cgroup_from_root(ns->root_cset, ctx->root);
Serge Hallyned825712016-01-29 02:54:09 -06002064
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002065 spin_unlock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002066 mutex_unlock(&cgroup_mutex);
2067
Al Viro399504e2019-01-06 11:41:29 -05002068 nsdentry = kernfs_node_dentry(cgrp->kn, sb);
Al Viro71d883c2019-01-17 02:44:07 -05002069 dput(fc->root);
2070 fc->root = nsdentry;
2071 if (IS_ERR(nsdentry)) {
2072 ret = PTR_ERR(nsdentry);
Al Viro399504e2019-01-06 11:41:29 -05002073 deactivate_locked_super(sb);
Al Viro71d883c2019-01-17 02:44:07 -05002074 }
Serge Hallyned825712016-01-29 02:54:09 -06002075 }
2076
Al Viro399504e2019-01-06 11:41:29 -05002077 if (!new_sb)
Al Viro71d883c2019-01-17 02:44:07 -05002078 cgroup_put(&ctx->root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002079
Al Viro71d883c2019-01-17 02:44:07 -05002080 return ret;
Tejun Heo633feee32016-12-27 14:49:07 -05002081}
2082
Al Viro90129622019-01-05 00:38:03 -05002083/*
2084 * Destroy a cgroup filesystem context.
2085 */
2086static void cgroup_fs_context_free(struct fs_context *fc)
2087{
2088 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
2089
Al Virof5dfb532019-01-16 23:42:38 -05002090 kfree(ctx->name);
2091 kfree(ctx->release_agent);
Al Viro90129622019-01-05 00:38:03 -05002092 kfree(ctx);
2093}
2094
Al Viro90129622019-01-05 00:38:03 -05002095static int cgroup_get_tree(struct fs_context *fc)
Tejun Heo633feee32016-12-27 14:49:07 -05002096{
2097 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
Al Viro90129622019-01-05 00:38:03 -05002098 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
Al Viro71d883c2019-01-17 02:44:07 -05002099 int ret;
Tejun Heo633feee32016-12-27 14:49:07 -05002100
Al Viro90129622019-01-05 00:38:03 -05002101 /* Check if the caller has permission to mount. */
2102 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
2103 return -EPERM;
2104
Al Viro90129622019-01-05 00:38:03 -05002105 cgrp_dfl_visible = true;
2106 cgroup_get_live(&cgrp_dfl_root.cgrp);
Al Virocf6299b12019-01-17 02:25:51 -05002107 ctx->root = &cgrp_dfl_root;
Al Viro90129622019-01-05 00:38:03 -05002108
Al Viro71d883c2019-01-17 02:44:07 -05002109 ret = cgroup_do_mount(fc, CGROUP2_SUPER_MAGIC, ns);
2110 if (!ret)
2111 apply_cgroup_root_flags(ctx->flags);
2112 return ret;
Al Viro90129622019-01-05 00:38:03 -05002113}
2114
Al Viro90129622019-01-05 00:38:03 -05002115static const struct fs_context_operations cgroup_fs_context_ops = {
2116 .free = cgroup_fs_context_free,
Al Viroe34a98d2019-01-17 00:22:58 -05002117 .parse_param = cgroup2_parse_param,
Al Viro90129622019-01-05 00:38:03 -05002118 .get_tree = cgroup_get_tree,
2119 .reconfigure = cgroup_reconfigure,
2120};
2121
2122static const struct fs_context_operations cgroup1_fs_context_ops = {
2123 .free = cgroup_fs_context_free,
Al Viro8d2451f2019-01-17 00:15:11 -05002124 .parse_param = cgroup1_parse_param,
Al Viro90129622019-01-05 00:38:03 -05002125 .get_tree = cgroup1_get_tree,
2126 .reconfigure = cgroup1_reconfigure,
2127};
2128
2129/*
2130 * Initialise the cgroup filesystem creation/reconfiguration context.
2131 */
2132static int cgroup_init_fs_context(struct fs_context *fc)
2133{
2134 struct cgroup_fs_context *ctx;
2135
2136 ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL);
2137 if (!ctx)
2138 return -ENOMEM;
Tejun Heo633feee32016-12-27 14:49:07 -05002139
Li Zefan3a32bd72014-06-30 11:50:59 +08002140 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002141 * The first time anyone tries to mount a cgroup, enable the list
2142 * linking each css_set to its tasks and fix up all existing tasks.
Li Zefan3a32bd72014-06-30 11:50:59 +08002143 */
Tejun Heo633feee32016-12-27 14:49:07 -05002144 if (!use_task_css_set_links)
2145 cgroup_enable_task_cg_lists();
2146
Al Viro90129622019-01-05 00:38:03 -05002147 fc->fs_private = ctx;
2148 if (fc->fs_type == &cgroup2_fs_type)
2149 fc->ops = &cgroup_fs_context_ops;
2150 else
2151 fc->ops = &cgroup1_fs_context_ops;
2152 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002153}
2154
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002155static void cgroup_kill_sb(struct super_block *sb)
2156{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002157 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002158 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002159
Tejun Heo9d755d32014-05-14 09:15:02 -04002160 /*
Al Viro35ac1182019-01-12 00:20:54 -05002161 * If @root doesn't have any children, start killing it.
Tejun Heo9d755d32014-05-14 09:15:02 -04002162 * This prevents new mounts by disabling percpu_ref_tryget_live().
2163 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002164 *
2165 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002166 */
Al Viro35ac1182019-01-12 00:20:54 -05002167 if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
2168 !percpu_ref_is_dying(&root->cgrp.self.refcnt))
Tejun Heo9d755d32014-05-14 09:15:02 -04002169 percpu_ref_kill(&root->cgrp.self.refcnt);
Al Viro35ac1182019-01-12 00:20:54 -05002170 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002171 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002172}
2173
Tejun Heo0a268db2016-12-27 14:49:06 -05002174struct file_system_type cgroup_fs_type = {
Al Viro8d2451f2019-01-17 00:15:11 -05002175 .name = "cgroup",
2176 .init_fs_context = cgroup_init_fs_context,
2177 .parameters = &cgroup1_fs_parameters,
2178 .kill_sb = cgroup_kill_sb,
2179 .fs_flags = FS_USERNS_MOUNT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002180};
2181
Tejun Heo67e9c742015-11-16 11:13:34 -05002182static struct file_system_type cgroup2_fs_type = {
Al Viroe34a98d2019-01-17 00:22:58 -05002183 .name = "cgroup2",
2184 .init_fs_context = cgroup_init_fs_context,
2185 .parameters = &cgroup2_fs_parameters,
2186 .kill_sb = cgroup_kill_sb,
2187 .fs_flags = FS_USERNS_MOUNT,
Tejun Heo67e9c742015-11-16 11:13:34 -05002188};
2189
Tejun Heo0a268db2016-12-27 14:49:06 -05002190int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2191 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002192{
2193 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
Aditya Kalia79a9082016-01-29 02:54:06 -06002194
Tejun Heo4c737b42016-08-10 11:23:44 -04002195 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
Aditya Kalia79a9082016-01-29 02:54:06 -06002196}
2197
Tejun Heo4c737b42016-08-10 11:23:44 -04002198int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2199 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002200{
Tejun Heo4c737b42016-08-10 11:23:44 -04002201 int ret;
Aditya Kalia79a9082016-01-29 02:54:06 -06002202
2203 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002204 spin_lock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002205
2206 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2207
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002208 spin_unlock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002209 mutex_unlock(&cgroup_mutex);
2210
2211 return ret;
2212}
2213EXPORT_SYMBOL_GPL(cgroup_path_ns);
2214
Li Zefana043e3b2008-02-23 15:24:09 -08002215/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002216 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002217 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002218 * @buf: the buffer to write the path into
2219 * @buflen: the length of the buffer
2220 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002221 * Determine @task's cgroup on the first (the one with the lowest non-zero
2222 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2223 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2224 * cgroup controller callbacks.
2225 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002226 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002227 */
Tejun Heo4c737b42016-08-10 11:23:44 -04002228int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002229{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002230 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002231 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002232 int hierarchy_id = 1;
Tejun Heo4c737b42016-08-10 11:23:44 -04002233 int ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002234
2235 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002236 spin_lock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002237
Tejun Heo913ffdb2013-07-11 16:34:48 -07002238 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2239
Tejun Heo857a2be2013-04-14 20:50:08 -07002240 if (root) {
2241 cgrp = task_cgroup_from_root(task, root);
Tejun Heo4c737b42016-08-10 11:23:44 -04002242 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002243 } else {
2244 /* if no hierarchy exists, everyone is in "/" */
Tejun Heo4c737b42016-08-10 11:23:44 -04002245 ret = strlcpy(buf, "/", buflen);
Tejun Heo857a2be2013-04-14 20:50:08 -07002246 }
2247
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002248 spin_unlock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002249 mutex_unlock(&cgroup_mutex);
Tejun Heo4c737b42016-08-10 11:23:44 -04002250 return ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002251}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002252EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002253
Tejun Heoadaae5d2015-09-11 15:00:21 -04002254/**
Tejun Heoe595cd72017-01-15 19:03:41 -05002255 * cgroup_migrate_add_task - add a migration target task to a migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002256 * @task: target task
Tejun Heoe595cd72017-01-15 19:03:41 -05002257 * @mgctx: target migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002258 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002259 * Add @task, which is a migration target, to @mgctx->tset. This function
2260 * becomes noop if @task doesn't need to be migrated. @task's css_set
2261 * should have been added as a migration source and @task->cg_list will be
2262 * moved from the css_set's tasks list to mg_tasks one.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002263 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002264static void cgroup_migrate_add_task(struct task_struct *task,
2265 struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002266{
2267 struct css_set *cset;
2268
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002269 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002270
2271 /* @task either already exited or can't exit until the end */
2272 if (task->flags & PF_EXITING)
2273 return;
2274
2275 /* leave @task alone if post_fork() hasn't linked it yet */
2276 if (list_empty(&task->cg_list))
2277 return;
2278
2279 cset = task_css_set(task);
2280 if (!cset->mg_src_cgrp)
2281 return;
2282
Tejun Heo61046722017-07-08 07:17:02 -04002283 mgctx->tset.nr_tasks++;
2284
Tejun Heoadaae5d2015-09-11 15:00:21 -04002285 list_move_tail(&task->cg_list, &cset->mg_tasks);
2286 if (list_empty(&cset->mg_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002287 list_add_tail(&cset->mg_node,
2288 &mgctx->tset.src_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002289 if (list_empty(&cset->mg_dst_cset->mg_node))
Tejun Heod8ebf512017-01-15 19:03:40 -05002290 list_add_tail(&cset->mg_dst_cset->mg_node,
Tejun Heoe595cd72017-01-15 19:03:41 -05002291 &mgctx->tset.dst_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002292}
2293
Tejun Heo2f7ee562011-12-12 18:12:21 -08002294/**
2295 * cgroup_taskset_first - reset taskset and return the first task
2296 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002297 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002298 *
2299 * @tset iteration is initialized and the first task is returned.
2300 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002301struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2302 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002303{
Tejun Heob3dc0942014-02-25 10:04:01 -05002304 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2305 tset->cur_task = NULL;
2306
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002307 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002308}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002309
2310/**
2311 * cgroup_taskset_next - iterate to the next task in taskset
2312 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002313 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002314 *
2315 * Return the next task in @tset. Iteration must have been initialized
2316 * with cgroup_taskset_first().
2317 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002318struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2319 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002320{
Tejun Heob3dc0942014-02-25 10:04:01 -05002321 struct css_set *cset = tset->cur_cset;
2322 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002323
Tejun Heob3dc0942014-02-25 10:04:01 -05002324 while (&cset->mg_node != tset->csets) {
2325 if (!task)
2326 task = list_first_entry(&cset->mg_tasks,
2327 struct task_struct, cg_list);
2328 else
2329 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002330
Tejun Heob3dc0942014-02-25 10:04:01 -05002331 if (&task->cg_list != &cset->mg_tasks) {
2332 tset->cur_cset = cset;
2333 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002334
2335 /*
2336 * This function may be called both before and
2337 * after cgroup_taskset_migrate(). The two cases
2338 * can be distinguished by looking at whether @cset
2339 * has its ->mg_dst_cset set.
2340 */
2341 if (cset->mg_dst_cset)
2342 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2343 else
2344 *dst_cssp = cset->subsys[tset->ssid];
2345
Tejun Heob3dc0942014-02-25 10:04:01 -05002346 return task;
2347 }
2348
2349 cset = list_next_entry(cset, mg_node);
2350 task = NULL;
2351 }
2352
2353 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002354}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002355
2356/**
Tejun Heo37ff9f82016-03-08 11:51:26 -05002357 * cgroup_taskset_migrate - migrate a taskset
Tejun Heoe595cd72017-01-15 19:03:41 -05002358 * @mgctx: migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002359 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002360 * Migrate tasks in @mgctx as setup by migration preparation functions.
Tejun Heo37ff9f82016-03-08 11:51:26 -05002361 * This function fails iff one of the ->can_attach callbacks fails and
Tejun Heoe595cd72017-01-15 19:03:41 -05002362 * guarantees that either all or none of the tasks in @mgctx are migrated.
2363 * @mgctx is consumed regardless of success.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002364 */
Tejun Heobfc2cf62017-01-15 19:03:41 -05002365static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002366{
Tejun Heoe595cd72017-01-15 19:03:41 -05002367 struct cgroup_taskset *tset = &mgctx->tset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002368 struct cgroup_subsys *ss;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002369 struct task_struct *task, *tmp_task;
2370 struct css_set *cset, *tmp_cset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002371 int ssid, failed_ssid, ret;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002372
Tejun Heoadaae5d2015-09-11 15:00:21 -04002373 /* check that we can legitimately attach to the cgroup */
Tejun Heo61046722017-07-08 07:17:02 -04002374 if (tset->nr_tasks) {
2375 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2376 if (ss->can_attach) {
2377 tset->ssid = ssid;
2378 ret = ss->can_attach(tset);
2379 if (ret) {
2380 failed_ssid = ssid;
2381 goto out_cancel_attach;
2382 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002383 }
Tejun Heo61046722017-07-08 07:17:02 -04002384 } while_each_subsys_mask();
2385 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002386
2387 /*
2388 * Now that we're guaranteed success, proceed to move all tasks to
2389 * the new cgroup. There are no failure cases after here, so this
2390 * is the commit point.
2391 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002392 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002393 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002394 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2395 struct css_set *from_cset = task_css_set(task);
2396 struct css_set *to_cset = cset->mg_dst_cset;
2397
2398 get_css_set(to_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002399 to_cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04002400 css_set_move_task(task, from_cset, to_cset, true);
2401 put_css_set_locked(from_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002402 from_cset->nr_tasks--;
Tejun Heof6d7d042015-10-15 16:41:52 -04002403 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002404 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002405 spin_unlock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002406
2407 /*
2408 * Migration is committed, all target tasks are now on dst_csets.
2409 * Nothing is sensitive to fork() after this point. Notify
2410 * controllers that migration is complete.
2411 */
2412 tset->csets = &tset->dst_csets;
2413
Tejun Heo61046722017-07-08 07:17:02 -04002414 if (tset->nr_tasks) {
2415 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2416 if (ss->attach) {
2417 tset->ssid = ssid;
2418 ss->attach(tset);
2419 }
2420 } while_each_subsys_mask();
2421 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002422
2423 ret = 0;
2424 goto out_release_tset;
2425
2426out_cancel_attach:
Tejun Heo61046722017-07-08 07:17:02 -04002427 if (tset->nr_tasks) {
2428 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2429 if (ssid == failed_ssid)
2430 break;
2431 if (ss->cancel_attach) {
2432 tset->ssid = ssid;
2433 ss->cancel_attach(tset);
2434 }
2435 } while_each_subsys_mask();
2436 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002437out_release_tset:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002438 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002439 list_splice_init(&tset->dst_csets, &tset->src_csets);
2440 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2441 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2442 list_del_init(&cset->mg_node);
2443 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002444 spin_unlock_irq(&css_set_lock);
Waiman Longc4fa6c42017-09-21 09:54:13 -04002445
2446 /*
2447 * Re-initialize the cgroup_taskset structure in case it is reused
2448 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2449 * iteration.
2450 */
2451 tset->nr_tasks = 0;
2452 tset->csets = &tset->src_csets;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002453 return ret;
2454}
2455
2456/**
Tejun Heo8cfd8142017-07-21 11:14:51 -04002457 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
Tejun Heo6c694c82016-03-08 11:51:25 -05002458 * @dst_cgrp: destination cgroup to test
2459 *
Tejun Heo8cfd8142017-07-21 11:14:51 -04002460 * On the default hierarchy, except for the mixable, (possible) thread root
2461 * and threaded cgroups, subtree_control must be zero for migration
2462 * destination cgroups with tasks so that child cgroups don't compete
2463 * against tasks.
Tejun Heo6c694c82016-03-08 11:51:25 -05002464 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04002465int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
Tejun Heo6c694c82016-03-08 11:51:25 -05002466{
Tejun Heo8cfd8142017-07-21 11:14:51 -04002467 /* v1 doesn't have any restriction */
2468 if (!cgroup_on_dfl(dst_cgrp))
2469 return 0;
2470
2471 /* verify @dst_cgrp can host resources */
2472 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
2473 return -EOPNOTSUPP;
2474
2475 /* mixables don't care */
2476 if (cgroup_is_mixable(dst_cgrp))
2477 return 0;
2478
2479 /*
2480 * If @dst_cgrp is already or can become a thread root or is
2481 * threaded, it doesn't matter.
2482 */
2483 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
2484 return 0;
2485
2486 /* apply no-internal-process constraint */
2487 if (dst_cgrp->subtree_control)
2488 return -EBUSY;
2489
2490 return 0;
Tejun Heo6c694c82016-03-08 11:51:25 -05002491}
2492
2493/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002494 * cgroup_migrate_finish - cleanup after attach
Tejun Heoe595cd72017-01-15 19:03:41 -05002495 * @mgctx: migration context
Ben Blum74a11662011-05-26 16:25:20 -07002496 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002497 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2498 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002499 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002500void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002501{
Tejun Heoe595cd72017-01-15 19:03:41 -05002502 LIST_HEAD(preloaded);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002503 struct css_set *cset, *tmp_cset;
2504
2505 lockdep_assert_held(&cgroup_mutex);
2506
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002507 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002508
2509 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2510 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2511
2512 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002513 cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002514 cset->mg_dst_cgrp = NULL;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002515 cset->mg_dst_cset = NULL;
2516 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002517 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002518 }
Tejun Heoe595cd72017-01-15 19:03:41 -05002519
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002520 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002521}
2522
2523/**
2524 * cgroup_migrate_add_src - add a migration source css_set
2525 * @src_cset: the source css_set to add
2526 * @dst_cgrp: the destination cgroup
Tejun Heoe595cd72017-01-15 19:03:41 -05002527 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002528 *
2529 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
Tejun Heoe595cd72017-01-15 19:03:41 -05002530 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
Tejun Heo1958d2d2014-02-25 10:04:03 -05002531 * up by cgroup_migrate_finish().
2532 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002533 * This function may be called without holding cgroup_threadgroup_rwsem
2534 * even if the target is a process. Threads may be created and destroyed
2535 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2536 * into play and the preloaded css_sets are guaranteed to cover all
2537 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002538 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002539void cgroup_migrate_add_src(struct css_set *src_cset,
2540 struct cgroup *dst_cgrp,
Tejun Heoe595cd72017-01-15 19:03:41 -05002541 struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002542{
2543 struct cgroup *src_cgrp;
2544
2545 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002546 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002547
Tejun Heo2b021cb2016-03-15 20:43:04 -04002548 /*
2549 * If ->dead, @src_set is associated with one or more dead cgroups
2550 * and doesn't contain any migratable tasks. Ignore it early so
2551 * that the rest of migration path doesn't get confused by it.
2552 */
2553 if (src_cset->dead)
2554 return;
2555
Tejun Heo1958d2d2014-02-25 10:04:03 -05002556 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2557
Tejun Heo1958d2d2014-02-25 10:04:03 -05002558 if (!list_empty(&src_cset->mg_preload_node))
2559 return;
2560
2561 WARN_ON(src_cset->mg_src_cgrp);
Tejun Heoe4857982016-03-08 11:51:26 -05002562 WARN_ON(src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002563 WARN_ON(!list_empty(&src_cset->mg_tasks));
2564 WARN_ON(!list_empty(&src_cset->mg_node));
2565
2566 src_cset->mg_src_cgrp = src_cgrp;
Tejun Heoe4857982016-03-08 11:51:26 -05002567 src_cset->mg_dst_cgrp = dst_cgrp;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002568 get_css_set(src_cset);
Tejun Heoe595cd72017-01-15 19:03:41 -05002569 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002570}
2571
2572/**
2573 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heoe595cd72017-01-15 19:03:41 -05002574 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002575 *
Tejun Heoe4857982016-03-08 11:51:26 -05002576 * Tasks are about to be moved and all the source css_sets have been
Tejun Heoe595cd72017-01-15 19:03:41 -05002577 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2578 * pins all destination css_sets, links each to its source, and append them
2579 * to @mgctx->preloaded_dst_csets.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002580 *
2581 * This function must be called after cgroup_migrate_add_src() has been
2582 * called on each migration source css_set. After migration is performed
2583 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
Tejun Heoe595cd72017-01-15 19:03:41 -05002584 * @mgctx.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002585 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002586int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002587{
Tejun Heof817de92014-04-23 11:13:16 -04002588 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002589
2590 lockdep_assert_held(&cgroup_mutex);
2591
2592 /* look up the dst cset for each src cset and link it to src */
Tejun Heoe595cd72017-01-15 19:03:41 -05002593 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2594 mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002595 struct css_set *dst_cset;
Tejun Heobfc2cf62017-01-15 19:03:41 -05002596 struct cgroup_subsys *ss;
2597 int ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002598
Tejun Heoe4857982016-03-08 11:51:26 -05002599 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002600 if (!dst_cset)
2601 goto err;
2602
2603 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002604
2605 /*
2606 * If src cset equals dst, it's noop. Drop the src.
2607 * cgroup_migrate() will skip the cset too. Note that we
2608 * can't handle src == dst as some nodes are used by both.
2609 */
2610 if (src_cset == dst_cset) {
2611 src_cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002612 src_cset->mg_dst_cgrp = NULL;
Tejun Heof817de92014-04-23 11:13:16 -04002613 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002614 put_css_set(src_cset);
2615 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002616 continue;
2617 }
2618
Tejun Heo1958d2d2014-02-25 10:04:03 -05002619 src_cset->mg_dst_cset = dst_cset;
2620
2621 if (list_empty(&dst_cset->mg_preload_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002622 list_add_tail(&dst_cset->mg_preload_node,
2623 &mgctx->preloaded_dst_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002624 else
Zefan Lia25eb522014-09-19 16:51:00 +08002625 put_css_set(dst_cset);
Tejun Heobfc2cf62017-01-15 19:03:41 -05002626
2627 for_each_subsys(ss, ssid)
2628 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2629 mgctx->ss_mask |= 1 << ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002630 }
2631
Tejun Heo1958d2d2014-02-25 10:04:03 -05002632 return 0;
2633err:
Tejun Heoe595cd72017-01-15 19:03:41 -05002634 cgroup_migrate_finish(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002635 return -ENOMEM;
2636}
2637
2638/**
2639 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002640 * @leader: the leader of the process or the task to migrate
2641 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heoe595cd72017-01-15 19:03:41 -05002642 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002643 *
Tejun Heo37ff9f82016-03-08 11:51:26 -05002644 * Migrate a process or task denoted by @leader. If migrating a process,
2645 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2646 * responsible for invoking cgroup_migrate_add_src() and
Tejun Heo1958d2d2014-02-25 10:04:03 -05002647 * cgroup_migrate_prepare_dst() on the targets before invoking this
2648 * function and following up with cgroup_migrate_finish().
2649 *
2650 * As long as a controller's ->can_attach() doesn't fail, this function is
2651 * guaranteed to succeed. This means that, excluding ->can_attach()
2652 * failure, when migrating multiple targets, the success or failure can be
2653 * decided for all targets by invoking group_migrate_prepare_dst() before
2654 * actually starting migrating.
2655 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002656int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heobfc2cf62017-01-15 19:03:41 -05002657 struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002658{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002659 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002660
2661 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002662 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2663 * already PF_EXITING could be freed from underneath us unless we
2664 * take an rcu_read_lock.
2665 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002666 spin_lock_irq(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002667 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002668 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002669 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002670 cgroup_migrate_add_task(task, mgctx);
Li Zefan081aa452013-03-13 09:17:09 +08002671 if (!threadgroup)
2672 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002673 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002674 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002675 spin_unlock_irq(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002676
Tejun Heobfc2cf62017-01-15 19:03:41 -05002677 return cgroup_migrate_execute(mgctx);
Ben Blum74a11662011-05-26 16:25:20 -07002678}
2679
Tejun Heo1958d2d2014-02-25 10:04:03 -05002680/**
2681 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2682 * @dst_cgrp: the cgroup to attach to
2683 * @leader: the task or the leader of the threadgroup to be attached
2684 * @threadgroup: attach the whole threadgroup?
2685 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002686 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002687 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002688int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2689 bool threadgroup)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002690{
Tejun Heoe595cd72017-01-15 19:03:41 -05002691 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002692 struct task_struct *task;
2693 int ret;
2694
Tejun Heo8cfd8142017-07-21 11:14:51 -04002695 ret = cgroup_migrate_vet_dst(dst_cgrp);
2696 if (ret)
2697 return ret;
Tejun Heo6c694c82016-03-08 11:51:25 -05002698
Tejun Heo1958d2d2014-02-25 10:04:03 -05002699 /* look up all src csets */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002700 spin_lock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002701 rcu_read_lock();
2702 task = leader;
2703 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002704 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002705 if (!threadgroup)
2706 break;
2707 } while_each_thread(leader, task);
2708 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002709 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002710
2711 /* prepare dst csets and commit */
Tejun Heoe595cd72017-01-15 19:03:41 -05002712 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002713 if (!ret)
Tejun Heobfc2cf62017-01-15 19:03:41 -05002714 ret = cgroup_migrate(leader, threadgroup, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002715
Tejun Heoe595cd72017-01-15 19:03:41 -05002716 cgroup_migrate_finish(&mgctx);
Tejun Heoed1777d2016-08-10 11:23:44 -04002717
2718 if (!ret)
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04002719 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
Tejun Heoed1777d2016-08-10 11:23:44 -04002720
Tejun Heo1958d2d2014-02-25 10:04:03 -05002721 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002722}
2723
Tejun Heo715c8092017-05-15 09:34:00 -04002724struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
2725 __acquires(&cgroup_threadgroup_rwsem)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002726{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002727 struct task_struct *tsk;
Tejun Heoacbef752014-05-13 12:16:22 -04002728 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002729
Tejun Heoacbef752014-05-13 12:16:22 -04002730 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
Tejun Heo715c8092017-05-15 09:34:00 -04002731 return ERR_PTR(-EINVAL);
Ben Blum74a11662011-05-26 16:25:20 -07002732
Tejun Heo3014dde2015-09-16 13:03:02 -04002733 percpu_down_write(&cgroup_threadgroup_rwsem);
Tejun Heo715c8092017-05-15 09:34:00 -04002734
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002735 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002736 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002737 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002738 if (!tsk) {
Tejun Heo715c8092017-05-15 09:34:00 -04002739 tsk = ERR_PTR(-ESRCH);
2740 goto out_unlock_threadgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002741 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002742 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002743 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002744 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002745
2746 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002747 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002748
2749 /*
Tejun Heo77f88792017-03-16 16:54:24 -04002750 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2751 * If userland migrates such a kthread to a non-root cgroup, it can
2752 * become trapped in a cpuset, or RT kthread may be born in a
2753 * cgroup with no rt_runtime allocated. Just say no.
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002754 */
Tejun Heo77f88792017-03-16 16:54:24 -04002755 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
Tejun Heo715c8092017-05-15 09:34:00 -04002756 tsk = ERR_PTR(-EINVAL);
2757 goto out_unlock_threadgroup;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002758 }
2759
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002760 get_task_struct(tsk);
Tejun Heo715c8092017-05-15 09:34:00 -04002761 goto out_unlock_rcu;
Tejun Heocd3d0952011-12-12 18:12:21 -08002762
Tejun Heo715c8092017-05-15 09:34:00 -04002763out_unlock_threadgroup:
2764 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo3014dde2015-09-16 13:03:02 -04002765out_unlock_rcu:
2766 rcu_read_unlock();
Tejun Heo715c8092017-05-15 09:34:00 -04002767 return tsk;
2768}
2769
2770void cgroup_procs_write_finish(struct task_struct *task)
2771 __releases(&cgroup_threadgroup_rwsem)
2772{
2773 struct cgroup_subsys *ss;
2774 int ssid;
2775
2776 /* release reference from cgroup_procs_write_start() */
2777 put_task_struct(task);
2778
Tejun Heo3014dde2015-09-16 13:03:02 -04002779 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002780 for_each_subsys(ss, ssid)
2781 if (ss->post_attach)
2782 ss->post_attach();
Paul Menageaf351022008-07-25 01:47:01 -07002783}
2784
Tejun Heo6e5c8302016-02-22 22:25:47 -05002785static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002786{
2787 struct cgroup_subsys *ss;
2788 bool printed = false;
2789 int ssid;
2790
Tejun Heob4e0eea2016-02-22 22:25:46 -05002791 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002792 if (printed)
2793 seq_putc(seq, ' ');
2794 seq_printf(seq, "%s", ss->name);
2795 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002796 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002797 if (printed)
2798 seq_putc(seq, '\n');
2799}
2800
Tejun Heof8f22e52014-04-23 11:13:16 -04002801/* show controllers which are enabled from the parent */
2802static int cgroup_controllers_show(struct seq_file *seq, void *v)
2803{
2804 struct cgroup *cgrp = seq_css(seq)->cgroup;
2805
Tejun Heo5531dc92016-03-03 09:57:58 -05002806 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04002807 return 0;
2808}
2809
2810/* show controllers which are enabled for a given cgroup's children */
2811static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2812{
2813 struct cgroup *cgrp = seq_css(seq)->cgroup;
2814
Tejun Heo667c2492014-07-08 18:02:56 -04002815 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002816 return 0;
2817}
2818
2819/**
2820 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2821 * @cgrp: root of the subtree to update csses for
2822 *
Tejun Heo54962602016-03-03 09:58:01 -05002823 * @cgrp's control masks have changed and its subtree's css associations
2824 * need to be updated accordingly. This function looks up all css_sets
2825 * which are attached to the subtree, creates the matching updated css_sets
2826 * and migrates the tasks to the new ones.
Tejun Heof8f22e52014-04-23 11:13:16 -04002827 */
2828static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2829{
Tejun Heoe595cd72017-01-15 19:03:41 -05002830 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo54962602016-03-03 09:58:01 -05002831 struct cgroup_subsys_state *d_css;
2832 struct cgroup *dsct;
Tejun Heof8f22e52014-04-23 11:13:16 -04002833 struct css_set *src_cset;
2834 int ret;
2835
Tejun Heof8f22e52014-04-23 11:13:16 -04002836 lockdep_assert_held(&cgroup_mutex);
2837
Tejun Heo3014dde2015-09-16 13:03:02 -04002838 percpu_down_write(&cgroup_threadgroup_rwsem);
2839
Tejun Heof8f22e52014-04-23 11:13:16 -04002840 /* look up all csses currently attached to @cgrp's subtree */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002841 spin_lock_irq(&css_set_lock);
Tejun Heo54962602016-03-03 09:58:01 -05002842 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002843 struct cgrp_cset_link *link;
2844
Tejun Heo54962602016-03-03 09:58:01 -05002845 list_for_each_entry(link, &dsct->cset_links, cset_link)
Tejun Heoe595cd72017-01-15 19:03:41 -05002846 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002847 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002848 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002849
2850 /* NULL dst indicates self on default hierarchy */
Tejun Heoe595cd72017-01-15 19:03:41 -05002851 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002852 if (ret)
2853 goto out_finish;
2854
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002855 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002856 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002857 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002858
Tejun Heo10265072015-09-11 15:00:22 -04002859 /* all tasks in src_csets need to be migrated */
2860 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
Tejun Heoe595cd72017-01-15 19:03:41 -05002861 cgroup_migrate_add_task(task, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002862 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002863 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002864
Tejun Heobfc2cf62017-01-15 19:03:41 -05002865 ret = cgroup_migrate_execute(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002866out_finish:
Tejun Heoe595cd72017-01-15 19:03:41 -05002867 cgroup_migrate_finish(&mgctx);
Tejun Heo3014dde2015-09-16 13:03:02 -04002868 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002869 return ret;
2870}
2871
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002872/**
Tejun Heo945ba192016-03-03 09:58:00 -05002873 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
Tejun Heoce3f1d92016-03-03 09:57:59 -05002874 * @cgrp: root of the target subtree
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002875 *
2876 * Because css offlining is asynchronous, userland may try to re-enable a
Tejun Heo945ba192016-03-03 09:58:00 -05002877 * controller while the previous css is still around. This function grabs
2878 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002879 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002880void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
Tejun Heo945ba192016-03-03 09:58:00 -05002881 __acquires(&cgroup_mutex)
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002882{
2883 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002884 struct cgroup_subsys_state *d_css;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002885 struct cgroup_subsys *ss;
2886 int ssid;
2887
Tejun Heo945ba192016-03-03 09:58:00 -05002888restart:
2889 mutex_lock(&cgroup_mutex);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002890
Tejun Heoce3f1d92016-03-03 09:57:59 -05002891 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002892 for_each_subsys(ss, ssid) {
2893 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2894 DEFINE_WAIT(wait);
2895
Tejun Heoce3f1d92016-03-03 09:57:59 -05002896 if (!css || !percpu_ref_is_dying(&css->refcnt))
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002897 continue;
2898
Tejun Heoa590b902017-04-28 15:14:55 -04002899 cgroup_get_live(dsct);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002900 prepare_to_wait(&dsct->offline_waitq, &wait,
2901 TASK_UNINTERRUPTIBLE);
2902
2903 mutex_unlock(&cgroup_mutex);
2904 schedule();
2905 finish_wait(&dsct->offline_waitq, &wait);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002906
2907 cgroup_put(dsct);
Tejun Heo945ba192016-03-03 09:58:00 -05002908 goto restart;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002909 }
2910 }
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002911}
2912
Tejun Heo12b3bb62016-03-03 09:57:59 -05002913/**
Tejun Heo479adb82018-10-04 13:28:08 -07002914 * cgroup_save_control - save control masks and dom_cgrp of a subtree
Tejun Heo15a27c32016-03-03 09:57:59 -05002915 * @cgrp: root of the target subtree
2916 *
Tejun Heo479adb82018-10-04 13:28:08 -07002917 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
2918 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2919 * itself.
Tejun Heo15a27c32016-03-03 09:57:59 -05002920 */
2921static void cgroup_save_control(struct cgroup *cgrp)
2922{
2923 struct cgroup *dsct;
2924 struct cgroup_subsys_state *d_css;
2925
2926 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2927 dsct->old_subtree_control = dsct->subtree_control;
2928 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
Tejun Heo479adb82018-10-04 13:28:08 -07002929 dsct->old_dom_cgrp = dsct->dom_cgrp;
Tejun Heo15a27c32016-03-03 09:57:59 -05002930 }
2931}
2932
2933/**
2934 * cgroup_propagate_control - refresh control masks of a subtree
2935 * @cgrp: root of the target subtree
2936 *
2937 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2938 * ->subtree_control and propagate controller availability through the
2939 * subtree so that descendants don't have unavailable controllers enabled.
2940 */
2941static void cgroup_propagate_control(struct cgroup *cgrp)
2942{
2943 struct cgroup *dsct;
2944 struct cgroup_subsys_state *d_css;
2945
2946 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2947 dsct->subtree_control &= cgroup_control(dsct);
Tejun Heo5ced2512016-03-03 09:58:01 -05002948 dsct->subtree_ss_mask =
2949 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2950 cgroup_ss_mask(dsct));
Tejun Heo15a27c32016-03-03 09:57:59 -05002951 }
2952}
2953
2954/**
Tejun Heo479adb82018-10-04 13:28:08 -07002955 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
Tejun Heo15a27c32016-03-03 09:57:59 -05002956 * @cgrp: root of the target subtree
2957 *
Tejun Heo479adb82018-10-04 13:28:08 -07002958 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
2959 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2960 * itself.
Tejun Heo15a27c32016-03-03 09:57:59 -05002961 */
2962static void cgroup_restore_control(struct cgroup *cgrp)
2963{
2964 struct cgroup *dsct;
2965 struct cgroup_subsys_state *d_css;
2966
2967 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2968 dsct->subtree_control = dsct->old_subtree_control;
2969 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
Tejun Heo479adb82018-10-04 13:28:08 -07002970 dsct->dom_cgrp = dsct->old_dom_cgrp;
Tejun Heo15a27c32016-03-03 09:57:59 -05002971 }
2972}
2973
Tejun Heof6d635ad2016-03-08 11:51:26 -05002974static bool css_visible(struct cgroup_subsys_state *css)
2975{
2976 struct cgroup_subsys *ss = css->ss;
2977 struct cgroup *cgrp = css->cgroup;
2978
2979 if (cgroup_control(cgrp) & (1 << ss->id))
2980 return true;
2981 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2982 return false;
2983 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2984}
2985
Tejun Heo15a27c32016-03-03 09:57:59 -05002986/**
Tejun Heobdb53bd2016-03-03 09:57:59 -05002987 * cgroup_apply_control_enable - enable or show csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05002988 * @cgrp: root of the target subtree
Tejun Heobdb53bd2016-03-03 09:57:59 -05002989 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05002990 * Walk @cgrp's subtree and create new csses or make the existing ones
Tejun Heobdb53bd2016-03-03 09:57:59 -05002991 * visible. A css is created invisible if it's being implicitly enabled
2992 * through dependency. An invisible css is made visible when the userland
2993 * explicitly enables it.
2994 *
2995 * Returns 0 on success, -errno on failure. On failure, csses which have
2996 * been processed already aren't cleaned up. The caller is responsible for
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08002997 * cleaning up with cgroup_apply_control_disable().
Tejun Heobdb53bd2016-03-03 09:57:59 -05002998 */
2999static int cgroup_apply_control_enable(struct cgroup *cgrp)
3000{
3001 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003002 struct cgroup_subsys_state *d_css;
Tejun Heobdb53bd2016-03-03 09:57:59 -05003003 struct cgroup_subsys *ss;
3004 int ssid, ret;
3005
Tejun Heoce3f1d92016-03-03 09:57:59 -05003006 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heobdb53bd2016-03-03 09:57:59 -05003007 for_each_subsys(ss, ssid) {
3008 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3009
Tejun Heo945ba192016-03-03 09:58:00 -05003010 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
3011
Tejun Heobdb53bd2016-03-03 09:57:59 -05003012 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
3013 continue;
3014
3015 if (!css) {
3016 css = css_create(dsct, ss);
3017 if (IS_ERR(css))
3018 return PTR_ERR(css);
3019 }
3020
Tejun Heof6d635ad2016-03-08 11:51:26 -05003021 if (css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05003022 ret = css_populate_dir(css);
Tejun Heobdb53bd2016-03-03 09:57:59 -05003023 if (ret)
3024 return ret;
3025 }
3026 }
3027 }
3028
3029 return 0;
3030}
3031
3032/**
Tejun Heo12b3bb62016-03-03 09:57:59 -05003033 * cgroup_apply_control_disable - kill or hide csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003034 * @cgrp: root of the target subtree
Tejun Heo12b3bb62016-03-03 09:57:59 -05003035 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003036 * Walk @cgrp's subtree and kill and hide csses so that they match
Tejun Heo12b3bb62016-03-03 09:57:59 -05003037 * cgroup_ss_mask() and cgroup_visible_mask().
3038 *
3039 * A css is hidden when the userland requests it to be disabled while other
3040 * subsystems are still depending on it. The css must not actively control
3041 * resources and be in the vanilla state if it's made visible again later.
3042 * Controllers which may be depended upon should provide ->css_reset() for
3043 * this purpose.
3044 */
3045static void cgroup_apply_control_disable(struct cgroup *cgrp)
3046{
3047 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003048 struct cgroup_subsys_state *d_css;
Tejun Heo12b3bb62016-03-03 09:57:59 -05003049 struct cgroup_subsys *ss;
3050 int ssid;
3051
Tejun Heoce3f1d92016-03-03 09:57:59 -05003052 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003053 for_each_subsys(ss, ssid) {
3054 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3055
Tejun Heo945ba192016-03-03 09:58:00 -05003056 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
3057
Tejun Heo12b3bb62016-03-03 09:57:59 -05003058 if (!css)
3059 continue;
3060
Tejun Heo334c3672016-03-03 09:58:01 -05003061 if (css->parent &&
3062 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003063 kill_css(css);
Tejun Heof6d635ad2016-03-08 11:51:26 -05003064 } else if (!css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05003065 css_clear_dir(css);
Tejun Heo12b3bb62016-03-03 09:57:59 -05003066 if (ss->css_reset)
3067 ss->css_reset(css);
3068 }
3069 }
3070 }
3071}
3072
Tejun Heof7b28142016-03-03 09:58:00 -05003073/**
3074 * cgroup_apply_control - apply control mask updates to the subtree
3075 * @cgrp: root of the target subtree
3076 *
3077 * subsystems can be enabled and disabled in a subtree using the following
3078 * steps.
3079 *
3080 * 1. Call cgroup_save_control() to stash the current state.
3081 * 2. Update ->subtree_control masks in the subtree as desired.
3082 * 3. Call cgroup_apply_control() to apply the changes.
3083 * 4. Optionally perform other related operations.
3084 * 5. Call cgroup_finalize_control() to finish up.
3085 *
3086 * This function implements step 3 and propagates the mask changes
3087 * throughout @cgrp's subtree, updates csses accordingly and perform
3088 * process migrations.
3089 */
3090static int cgroup_apply_control(struct cgroup *cgrp)
3091{
3092 int ret;
3093
3094 cgroup_propagate_control(cgrp);
3095
3096 ret = cgroup_apply_control_enable(cgrp);
3097 if (ret)
3098 return ret;
3099
3100 /*
Dennis Zhoufc5a8282018-12-05 12:10:36 -05003101 * At this point, cgroup_e_css_by_mask() results reflect the new csses
Tejun Heof7b28142016-03-03 09:58:00 -05003102 * making the following cgroup_update_dfl_csses() properly update
3103 * css associations of all tasks in the subtree.
3104 */
3105 ret = cgroup_update_dfl_csses(cgrp);
3106 if (ret)
3107 return ret;
3108
3109 return 0;
3110}
3111
3112/**
3113 * cgroup_finalize_control - finalize control mask update
3114 * @cgrp: root of the target subtree
3115 * @ret: the result of the update
3116 *
3117 * Finalize control mask update. See cgroup_apply_control() for more info.
3118 */
3119static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3120{
3121 if (ret) {
3122 cgroup_restore_control(cgrp);
3123 cgroup_propagate_control(cgrp);
3124 }
3125
3126 cgroup_apply_control_disable(cgrp);
3127}
3128
Tejun Heo8cfd8142017-07-21 11:14:51 -04003129static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
3130{
3131 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
3132
3133 /* if nothing is getting enabled, nothing to worry about */
3134 if (!enable)
3135 return 0;
3136
3137 /* can @cgrp host any resources? */
3138 if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
3139 return -EOPNOTSUPP;
3140
3141 /* mixables don't care */
3142 if (cgroup_is_mixable(cgrp))
3143 return 0;
3144
3145 if (domain_enable) {
3146 /* can't enable domain controllers inside a thread subtree */
3147 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3148 return -EOPNOTSUPP;
3149 } else {
3150 /*
3151 * Threaded controllers can handle internal competitions
3152 * and are always allowed inside a (prospective) thread
3153 * subtree.
3154 */
3155 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3156 return 0;
3157 }
3158
3159 /*
3160 * Controllers can't be enabled for a cgroup with tasks to avoid
3161 * child cgroups competing against tasks.
3162 */
3163 if (cgroup_has_tasks(cgrp))
3164 return -EBUSY;
3165
3166 return 0;
3167}
3168
Tejun Heof8f22e52014-04-23 11:13:16 -04003169/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003170static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3171 char *buf, size_t nbytes,
3172 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003173{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003174 u16 enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04003175 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003176 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003177 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003178 int ssid, ret;
3179
3180 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003181 * Parse input - space separated list of subsystem names prefixed
3182 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003183 */
Tejun Heo451af502014-05-13 12:16:21 -04003184 buf = strstrip(buf);
3185 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003186 if (tok[0] == '\0')
3187 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003188 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003189 if (!cgroup_ssid_enabled(ssid) ||
3190 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003191 continue;
3192
3193 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003194 enable |= 1 << ssid;
3195 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003196 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003197 disable |= 1 << ssid;
3198 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003199 } else {
3200 return -EINVAL;
3201 }
3202 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003203 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003204 if (ssid == CGROUP_SUBSYS_COUNT)
3205 return -EINVAL;
3206 }
3207
Tejun Heo945ba192016-03-03 09:58:00 -05003208 cgrp = cgroup_kn_lock_live(of->kn, true);
Tejun Heoa9746d82014-05-13 12:19:22 -04003209 if (!cgrp)
3210 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003211
3212 for_each_subsys(ss, ssid) {
3213 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003214 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003215 enable &= ~(1 << ssid);
3216 continue;
3217 }
3218
Tejun Heo5531dc92016-03-03 09:57:58 -05003219 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003220 ret = -ENOENT;
3221 goto out_unlock;
3222 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003223 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003224 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003225 disable &= ~(1 << ssid);
3226 continue;
3227 }
3228
3229 /* a child has it enabled? */
3230 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003231 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003232 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003233 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003234 }
3235 }
3236 }
3237 }
3238
3239 if (!enable && !disable) {
3240 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003241 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003242 }
3243
Tejun Heo8cfd8142017-07-21 11:14:51 -04003244 ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3245 if (ret)
Tejun Heo27f26752017-07-16 21:44:18 -04003246 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003247
Tejun Heo15a27c32016-03-03 09:57:59 -05003248 /* save and update control masks and prepare csses */
3249 cgroup_save_control(cgrp);
Tejun Heoc29adf22014-07-08 18:02:56 -04003250
Tejun Heo15a27c32016-03-03 09:57:59 -05003251 cgrp->subtree_control |= enable;
3252 cgrp->subtree_control &= ~disable;
Tejun Heof63070d2014-07-08 18:02:57 -04003253
Tejun Heof7b28142016-03-03 09:58:00 -05003254 ret = cgroup_apply_control(cgrp);
Tejun Heof7b28142016-03-03 09:58:00 -05003255 cgroup_finalize_control(cgrp, ret);
Tejun Heo3c745412017-07-23 08:14:15 -04003256 if (ret)
3257 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003258
3259 kernfs_activate(cgrp->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003260out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003261 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003262 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003263}
3264
Tejun Heoc705a002017-07-25 13:20:18 -04003265/**
3266 * cgroup_enable_threaded - make @cgrp threaded
3267 * @cgrp: the target cgroup
3268 *
3269 * Called when "threaded" is written to the cgroup.type interface file and
3270 * tries to make @cgrp threaded and join the parent's resource domain.
3271 * This function is never called on the root cgroup as cgroup.type doesn't
3272 * exist on it.
3273 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04003274static int cgroup_enable_threaded(struct cgroup *cgrp)
3275{
3276 struct cgroup *parent = cgroup_parent(cgrp);
3277 struct cgroup *dom_cgrp = parent->dom_cgrp;
Tejun Heo479adb82018-10-04 13:28:08 -07003278 struct cgroup *dsct;
3279 struct cgroup_subsys_state *d_css;
Tejun Heo8cfd8142017-07-21 11:14:51 -04003280 int ret;
3281
3282 lockdep_assert_held(&cgroup_mutex);
3283
3284 /* noop if already threaded */
3285 if (cgroup_is_threaded(cgrp))
3286 return 0;
3287
Tejun Heod1897c92018-02-21 11:39:22 -08003288 /*
3289 * If @cgroup is populated or has domain controllers enabled, it
3290 * can't be switched. While the below cgroup_can_be_thread_root()
3291 * test can catch the same conditions, that's only when @parent is
3292 * not mixable, so let's check it explicitly.
3293 */
3294 if (cgroup_is_populated(cgrp) ||
3295 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3296 return -EOPNOTSUPP;
3297
Tejun Heo8cfd8142017-07-21 11:14:51 -04003298 /* we're joining the parent's domain, ensure its validity */
3299 if (!cgroup_is_valid_domain(dom_cgrp) ||
3300 !cgroup_can_be_thread_root(dom_cgrp))
3301 return -EOPNOTSUPP;
3302
3303 /*
Tejun Heo8cfd8142017-07-21 11:14:51 -04003304 * The following shouldn't cause actual migrations and should
3305 * always succeed.
3306 */
3307 cgroup_save_control(cgrp);
3308
Tejun Heo479adb82018-10-04 13:28:08 -07003309 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
3310 if (dsct == cgrp || cgroup_is_threaded(dsct))
3311 dsct->dom_cgrp = dom_cgrp;
3312
Tejun Heo8cfd8142017-07-21 11:14:51 -04003313 ret = cgroup_apply_control(cgrp);
3314 if (!ret)
3315 parent->nr_threaded_children++;
Tejun Heo8cfd8142017-07-21 11:14:51 -04003316
3317 cgroup_finalize_control(cgrp, ret);
3318 return ret;
3319}
3320
3321static int cgroup_type_show(struct seq_file *seq, void *v)
3322{
3323 struct cgroup *cgrp = seq_css(seq)->cgroup;
3324
3325 if (cgroup_is_threaded(cgrp))
3326 seq_puts(seq, "threaded\n");
3327 else if (!cgroup_is_valid_domain(cgrp))
3328 seq_puts(seq, "domain invalid\n");
3329 else if (cgroup_is_thread_root(cgrp))
3330 seq_puts(seq, "domain threaded\n");
3331 else
3332 seq_puts(seq, "domain\n");
3333
3334 return 0;
3335}
3336
3337static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
3338 size_t nbytes, loff_t off)
3339{
3340 struct cgroup *cgrp;
3341 int ret;
3342
3343 /* only switching to threaded mode is supported */
3344 if (strcmp(strstrip(buf), "threaded"))
3345 return -EINVAL;
3346
3347 cgrp = cgroup_kn_lock_live(of->kn, false);
3348 if (!cgrp)
3349 return -ENOENT;
3350
3351 /* threaded can only be enabled */
3352 ret = cgroup_enable_threaded(cgrp);
3353
3354 cgroup_kn_unlock(of->kn);
3355 return ret ?: nbytes;
3356}
3357
Roman Gushchin1a926e02017-07-28 18:28:44 +01003358static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
3359{
3360 struct cgroup *cgrp = seq_css(seq)->cgroup;
3361 int descendants = READ_ONCE(cgrp->max_descendants);
3362
3363 if (descendants == INT_MAX)
3364 seq_puts(seq, "max\n");
3365 else
3366 seq_printf(seq, "%d\n", descendants);
3367
3368 return 0;
3369}
3370
3371static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
3372 char *buf, size_t nbytes, loff_t off)
3373{
3374 struct cgroup *cgrp;
3375 int descendants;
3376 ssize_t ret;
3377
3378 buf = strstrip(buf);
3379 if (!strcmp(buf, "max")) {
3380 descendants = INT_MAX;
3381 } else {
3382 ret = kstrtoint(buf, 0, &descendants);
3383 if (ret)
3384 return ret;
3385 }
3386
Dan Carpenter696b98f2017-08-09 13:25:21 +03003387 if (descendants < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003388 return -ERANGE;
3389
3390 cgrp = cgroup_kn_lock_live(of->kn, false);
3391 if (!cgrp)
3392 return -ENOENT;
3393
3394 cgrp->max_descendants = descendants;
3395
3396 cgroup_kn_unlock(of->kn);
3397
3398 return nbytes;
3399}
3400
3401static int cgroup_max_depth_show(struct seq_file *seq, void *v)
3402{
3403 struct cgroup *cgrp = seq_css(seq)->cgroup;
3404 int depth = READ_ONCE(cgrp->max_depth);
3405
3406 if (depth == INT_MAX)
3407 seq_puts(seq, "max\n");
3408 else
3409 seq_printf(seq, "%d\n", depth);
3410
3411 return 0;
3412}
3413
3414static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
3415 char *buf, size_t nbytes, loff_t off)
3416{
3417 struct cgroup *cgrp;
3418 ssize_t ret;
3419 int depth;
3420
3421 buf = strstrip(buf);
3422 if (!strcmp(buf, "max")) {
3423 depth = INT_MAX;
3424 } else {
3425 ret = kstrtoint(buf, 0, &depth);
3426 if (ret)
3427 return ret;
3428 }
3429
Dan Carpenter696b98f2017-08-09 13:25:21 +03003430 if (depth < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003431 return -ERANGE;
3432
3433 cgrp = cgroup_kn_lock_live(of->kn, false);
3434 if (!cgrp)
3435 return -ENOENT;
3436
3437 cgrp->max_depth = depth;
3438
3439 cgroup_kn_unlock(of->kn);
3440
3441 return nbytes;
3442}
3443
Tejun Heo4a07c222015-09-18 17:54:22 -04003444static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003445{
Tejun Heo4a07c222015-09-18 17:54:22 -04003446 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003447 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003448 return 0;
3449}
3450
Tejun Heo3e489302017-08-11 05:49:01 -07003451static int cgroup_stat_show(struct seq_file *seq, void *v)
Roman Gushchinec392252017-08-02 17:55:31 +01003452{
3453 struct cgroup *cgroup = seq_css(seq)->cgroup;
3454
3455 seq_printf(seq, "nr_descendants %d\n",
3456 cgroup->nr_descendants);
3457 seq_printf(seq, "nr_dying_descendants %d\n",
3458 cgroup->nr_dying_descendants);
3459
3460 return 0;
3461}
3462
Tejun Heod41bf8c2017-10-23 16:18:27 -07003463static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
3464 struct cgroup *cgrp, int ssid)
3465{
3466 struct cgroup_subsys *ss = cgroup_subsys[ssid];
3467 struct cgroup_subsys_state *css;
3468 int ret;
3469
3470 if (!ss->css_extra_stat_show)
3471 return 0;
3472
3473 css = cgroup_tryget_css(cgrp, ss);
3474 if (!css)
3475 return 0;
3476
3477 ret = ss->css_extra_stat_show(seq, css);
3478 css_put(css);
3479 return ret;
3480}
3481
3482static int cpu_stat_show(struct seq_file *seq, void *v)
3483{
Tejun Heoc3ba1322017-10-30 08:13:14 -07003484 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
Tejun Heod41bf8c2017-10-23 16:18:27 -07003485 int ret = 0;
3486
Tejun Heod4ff7492018-04-26 14:29:04 -07003487 cgroup_base_stat_cputime_show(seq);
Tejun Heod41bf8c2017-10-23 16:18:27 -07003488#ifdef CONFIG_CGROUP_SCHED
3489 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
3490#endif
3491 return ret;
3492}
3493
Johannes Weiner2ce71352018-10-26 15:06:31 -07003494#ifdef CONFIG_PSI
3495static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
3496{
3497 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_IO);
3498}
3499static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
3500{
3501 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_MEM);
3502}
3503static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
3504{
3505 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_CPU);
3506}
3507#endif
3508
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003509static int cgroup_file_open(struct kernfs_open_file *of)
3510{
3511 struct cftype *cft = of->kn->priv;
3512
3513 if (cft->open)
3514 return cft->open(of);
3515 return 0;
3516}
3517
3518static void cgroup_file_release(struct kernfs_open_file *of)
3519{
3520 struct cftype *cft = of->kn->priv;
3521
3522 if (cft->release)
3523 cft->release(of);
3524}
3525
Tejun Heo2bd59d42014-02-11 11:52:49 -05003526static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3527 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003528{
Tejun Heo5136f632017-06-27 14:30:28 -04003529 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003530 struct cgroup *cgrp = of->kn->parent->priv;
3531 struct cftype *cft = of->kn->priv;
3532 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003533 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003534
Tejun Heo5136f632017-06-27 14:30:28 -04003535 /*
3536 * If namespaces are delegation boundaries, disallow writes to
3537 * files in an non-init namespace root from inside the namespace
3538 * except for the files explicitly marked delegatable -
3539 * cgroup.procs and cgroup.subtree_control.
3540 */
3541 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
3542 !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
3543 ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp)
3544 return -EPERM;
3545
Tejun Heob4168642014-05-13 12:16:21 -04003546 if (cft->write)
3547 return cft->write(of, buf, nbytes, off);
3548
Tejun Heo2bd59d42014-02-11 11:52:49 -05003549 /*
3550 * kernfs guarantees that a file isn't deleted with operations in
3551 * flight, which means that the matching css is and stays alive and
3552 * doesn't need to be pinned. The RCU locking is not necessary
3553 * either. It's just for the convenience of using cgroup_css().
3554 */
3555 rcu_read_lock();
3556 css = cgroup_css(cgrp, cft->ss);
3557 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003558
Tejun Heo451af502014-05-13 12:16:21 -04003559 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003560 unsigned long long v;
3561 ret = kstrtoull(buf, 0, &v);
3562 if (!ret)
3563 ret = cft->write_u64(css, cft, v);
3564 } else if (cft->write_s64) {
3565 long long v;
3566 ret = kstrtoll(buf, 0, &v);
3567 if (!ret)
3568 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003569 } else {
3570 ret = -EINVAL;
3571 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003572
Tejun Heoa742c592013-12-05 12:28:03 -05003573 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003574}
3575
Tejun Heo6612f052013-12-05 12:28:04 -05003576static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003577{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003578 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003579}
3580
3581static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3582{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003583 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003584}
3585
3586static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3587{
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003588 if (seq_cft(seq)->seq_stop)
3589 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003590}
3591
3592static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3593{
Tejun Heo7da11272013-12-05 12:28:04 -05003594 struct cftype *cft = seq_cft(m);
3595 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003596
Tejun Heo2da8ca82013-12-05 12:28:04 -05003597 if (cft->seq_show)
3598 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003599
Tejun Heo896f5192013-12-05 12:28:04 -05003600 if (cft->read_u64)
3601 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3602 else if (cft->read_s64)
3603 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3604 else
3605 return -EINVAL;
3606 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003607}
3608
Tejun Heo2bd59d42014-02-11 11:52:49 -05003609static struct kernfs_ops cgroup_kf_single_ops = {
3610 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003611 .open = cgroup_file_open,
3612 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003613 .write = cgroup_file_write,
3614 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003615};
3616
Tejun Heo2bd59d42014-02-11 11:52:49 -05003617static struct kernfs_ops cgroup_kf_ops = {
3618 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003619 .open = cgroup_file_open,
3620 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003621 .write = cgroup_file_write,
3622 .seq_start = cgroup_seqfile_start,
3623 .seq_next = cgroup_seqfile_next,
3624 .seq_stop = cgroup_seqfile_stop,
3625 .seq_show = cgroup_seqfile_show,
3626};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003627
Tejun Heo49957f82014-04-07 16:44:47 -04003628/* set uid and gid of cgroup dirs and files to that of the creator */
3629static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3630{
3631 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3632 .ia_uid = current_fsuid(),
3633 .ia_gid = current_fsgid(), };
3634
3635 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3636 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3637 return 0;
3638
3639 return kernfs_setattr(kn, &iattr);
3640}
3641
Tejun Heob12e3582018-04-26 14:29:04 -07003642static void cgroup_file_notify_timer(struct timer_list *timer)
3643{
3644 cgroup_file_notify(container_of(timer, struct cgroup_file,
3645 notify_timer));
3646}
3647
Tejun Heo4df8dc92015-09-18 17:54:23 -04003648static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3649 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003650{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003651 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003652 struct kernfs_node *kn;
3653 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003654 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003655
Tejun Heo2bd59d42014-02-11 11:52:49 -05003656#ifdef CONFIG_DEBUG_LOCK_ALLOC
3657 key = &cft->lockdep_key;
3658#endif
3659 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
Dmitry Torokhov488dee92018-07-20 21:56:47 +00003660 cgroup_file_mode(cft),
3661 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
3662 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003663 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003664 if (IS_ERR(kn))
3665 return PTR_ERR(kn);
3666
3667 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003668 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003669 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003670 return ret;
3671 }
3672
Tejun Heo6f60ead2015-09-18 17:54:23 -04003673 if (cft->file_offset) {
3674 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3675
Tejun Heob12e3582018-04-26 14:29:04 -07003676 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
3677
Tejun Heo34c06252015-11-05 00:12:24 -05003678 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003679 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003680 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003681 }
3682
Tejun Heof8f22e52014-04-23 11:13:16 -04003683 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003684}
3685
Tejun Heob1f28d32013-06-28 16:24:10 -07003686/**
3687 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003688 * @css: the target css
3689 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003690 * @cfts: array of cftypes to be added
3691 * @is_add: whether to add or remove
3692 *
3693 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003694 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003695 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003696static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3697 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003698 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003699{
Tejun Heo6732ed82015-09-18 17:54:23 -04003700 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003701 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003702
Tejun Heo01f64742014-05-13 12:19:23 -04003703 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003704
Tejun Heo6732ed82015-09-18 17:54:23 -04003705restart:
3706 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003707 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003708 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003709 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003710 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003711 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003712 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003713 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003714 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003715 continue;
Waiman Long5cf81142018-11-08 10:08:46 -05003716 if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
3717 continue;
Li Zefan2739d3c2013-01-21 18:18:33 +08003718 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003719 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003720 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003721 pr_warn("%s: failed to add %s, err=%d\n",
3722 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003723 cft_end = cft;
3724 is_add = false;
3725 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003726 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003727 } else {
3728 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003729 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003730 }
Tejun Heob598dde2016-02-22 22:25:45 -05003731 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003732}
3733
Tejun Heo21a2d342014-02-12 09:29:49 -05003734static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003735{
Tejun Heo2bb566c2013-08-08 20:11:23 -04003736 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003737 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003738 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003739 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003740
Tejun Heo01f64742014-05-13 12:19:23 -04003741 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003742
Li Zefane8c82d22013-06-18 18:48:37 +08003743 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003744 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003745 struct cgroup *cgrp = css->cgroup;
3746
Tejun Heo88cb04b2016-03-03 09:57:58 -05003747 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08003748 continue;
3749
Tejun Heo4df8dc92015-09-18 17:54:23 -04003750 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003751 if (ret)
3752 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003753 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003754
3755 if (is_add && !ret)
3756 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003757 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003758}
3759
Tejun Heo2da440a2014-02-11 11:52:48 -05003760static void cgroup_exit_cftypes(struct cftype *cfts)
3761{
3762 struct cftype *cft;
3763
Tejun Heo2bd59d42014-02-11 11:52:49 -05003764 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3765 /* free copy for custom atomic_write_len, see init_cftypes() */
3766 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3767 kfree(cft->kf_ops);
3768 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003769 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003770
3771 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003772 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003773 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003774}
3775
Tejun Heo2bd59d42014-02-11 11:52:49 -05003776static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003777{
3778 struct cftype *cft;
3779
Tejun Heo2bd59d42014-02-11 11:52:49 -05003780 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3781 struct kernfs_ops *kf_ops;
3782
Tejun Heo0adb0702014-02-12 09:29:48 -05003783 WARN_ON(cft->ss || cft->kf_ops);
3784
Tejun Heo2bd59d42014-02-11 11:52:49 -05003785 if (cft->seq_start)
3786 kf_ops = &cgroup_kf_ops;
3787 else
3788 kf_ops = &cgroup_kf_single_ops;
3789
3790 /*
3791 * Ugh... if @cft wants a custom max_write_len, we need to
3792 * make a copy of kf_ops to set its atomic_write_len.
3793 */
3794 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3795 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3796 if (!kf_ops) {
3797 cgroup_exit_cftypes(cfts);
3798 return -ENOMEM;
3799 }
3800 kf_ops->atomic_write_len = cft->max_write_len;
3801 }
3802
3803 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003804 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003805 }
3806
3807 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003808}
3809
Tejun Heo21a2d342014-02-12 09:29:49 -05003810static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3811{
Tejun Heo01f64742014-05-13 12:19:23 -04003812 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003813
3814 if (!cfts || !cfts[0].ss)
3815 return -ENOENT;
3816
3817 list_del(&cfts->node);
3818 cgroup_apply_cftypes(cfts, false);
3819 cgroup_exit_cftypes(cfts);
3820 return 0;
3821}
3822
Tejun Heo8e3f6542012-04-01 12:09:55 -07003823/**
Tejun Heo80b13582014-02-12 09:29:48 -05003824 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3825 * @cfts: zero-length name terminated array of cftypes
3826 *
3827 * Unregister @cfts. Files described by @cfts are removed from all
3828 * existing cgroups and all future cgroups won't have them either. This
3829 * function can be called anytime whether @cfts' subsys is attached or not.
3830 *
3831 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3832 * registered.
3833 */
3834int cgroup_rm_cftypes(struct cftype *cfts)
3835{
Tejun Heo21a2d342014-02-12 09:29:49 -05003836 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003837
Tejun Heo01f64742014-05-13 12:19:23 -04003838 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003839 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003840 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003841 return ret;
3842}
3843
3844/**
3845 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3846 * @ss: target cgroup subsystem
3847 * @cfts: zero-length name terminated array of cftypes
3848 *
3849 * Register @cfts to @ss. Files described by @cfts are created for all
3850 * existing cgroups to which @ss is attached and all future cgroups will
3851 * have them too. This function can be called anytime whether @ss is
3852 * attached or not.
3853 *
3854 * Returns 0 on successful registration, -errno on failure. Note that this
3855 * function currently returns 0 as long as @cfts registration is successful
3856 * even if some file creation attempts on existing cgroups fail.
3857 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003858static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003859{
Tejun Heo9ccece82013-06-28 16:24:11 -07003860 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003861
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003862 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003863 return 0;
3864
Li Zefandc5736e2014-02-17 10:41:50 +08003865 if (!cfts || cfts[0].name[0] == '\0')
3866 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003867
Tejun Heo2bd59d42014-02-11 11:52:49 -05003868 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003869 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003870 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003871
Tejun Heo01f64742014-05-13 12:19:23 -04003872 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003873
Tejun Heo0adb0702014-02-12 09:29:48 -05003874 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003875 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003876 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003877 cgroup_rm_cftypes_locked(cfts);
3878
Tejun Heo01f64742014-05-13 12:19:23 -04003879 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003880 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003881}
Tejun Heo79578622012-04-01 12:09:56 -07003882
3883/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003884 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3885 * @ss: target cgroup subsystem
3886 * @cfts: zero-length name terminated array of cftypes
3887 *
3888 * Similar to cgroup_add_cftypes() but the added files are only used for
3889 * the default hierarchy.
3890 */
3891int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3892{
3893 struct cftype *cft;
3894
3895 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003896 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003897 return cgroup_add_cftypes(ss, cfts);
3898}
3899
3900/**
3901 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3902 * @ss: target cgroup subsystem
3903 * @cfts: zero-length name terminated array of cftypes
3904 *
3905 * Similar to cgroup_add_cftypes() but the added files are only used for
3906 * the legacy hierarchies.
3907 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003908int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3909{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003910 struct cftype *cft;
3911
Tejun Heoe4b70372015-10-15 17:00:43 -04003912 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3913 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003914 return cgroup_add_cftypes(ss, cfts);
3915}
3916
Li Zefana043e3b2008-02-23 15:24:09 -08003917/**
Tejun Heo34c06252015-11-05 00:12:24 -05003918 * cgroup_file_notify - generate a file modified event for a cgroup_file
3919 * @cfile: target cgroup_file
3920 *
3921 * @cfile must have been obtained by setting cftype->file_offset.
3922 */
3923void cgroup_file_notify(struct cgroup_file *cfile)
3924{
3925 unsigned long flags;
3926
3927 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
Tejun Heob12e3582018-04-26 14:29:04 -07003928 if (cfile->kn) {
3929 unsigned long last = cfile->notified_at;
3930 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
3931
3932 if (time_in_range(jiffies, last, next)) {
3933 timer_reduce(&cfile->notify_timer, next);
3934 } else {
3935 kernfs_notify(cfile->kn);
3936 cfile->notified_at = jiffies;
3937 }
3938 }
Tejun Heo34c06252015-11-05 00:12:24 -05003939 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3940}
3941
3942/**
Tejun Heo492eb212013-08-08 20:11:25 -04003943 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003944 * @pos: the current position (%NULL to initiate traversal)
3945 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003946 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003947 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003948 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003949 * that @parent and @pos are accessible. The next sibling is guaranteed to
3950 * be returned regardless of their states.
3951 *
3952 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3953 * css which finished ->css_online() is guaranteed to be visible in the
3954 * future iterations and will stay visible until the last reference is put.
3955 * A css which hasn't finished ->css_online() or already finished
3956 * ->css_offline() may show up during traversal. It's each subsystem's
3957 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003958 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003959struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3960 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003961{
Tejun Heoc2931b72014-05-16 13:22:51 -04003962 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003963
Tejun Heo8353da12014-05-13 12:19:23 -04003964 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003965
3966 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003967 * @pos could already have been unlinked from the sibling list.
3968 * Once a cgroup is removed, its ->sibling.next is no longer
3969 * updated when its next sibling changes. CSS_RELEASED is set when
3970 * @pos is taken off list, at which time its next pointer is valid,
3971 * and, as releases are serialized, the one pointed to by the next
3972 * pointer is guaranteed to not have started release yet. This
3973 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3974 * critical section, the one pointed to by its next pointer is
3975 * guaranteed to not have finished its RCU grace period even if we
3976 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003977 *
Tejun Heode3f0342014-05-16 13:22:49 -04003978 * If @pos has CSS_RELEASED set, its next pointer can't be
3979 * dereferenced; however, as each css is given a monotonically
3980 * increasing unique serial number and always appended to the
3981 * sibling list, the next one can be found by walking the parent's
3982 * children until the first css with higher serial number than
3983 * @pos's. While this path can be slower, it happens iff iteration
3984 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003985 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003986 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003987 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3988 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3989 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003990 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003991 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003992 if (next->serial_nr > pos->serial_nr)
3993 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003994 }
3995
Tejun Heo3b281af2014-04-23 11:13:15 -04003996 /*
3997 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003998 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003999 */
Tejun Heoc2931b72014-05-16 13:22:51 -04004000 if (&next->sibling != &parent->children)
4001 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04004002 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09004003}
Tejun Heo53fa5262013-05-24 10:55:38 +09004004
4005/**
Tejun Heo492eb212013-08-08 20:11:25 -04004006 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004007 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004008 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004009 *
Tejun Heo492eb212013-08-08 20:11:25 -04004010 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004011 * to visit for pre-order traversal of @root's descendants. @root is
4012 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004013 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004014 * While this function requires cgroup_mutex or RCU read locking, it
4015 * doesn't require the whole traversal to be contained in a single critical
4016 * section. This function will return the correct next descendant as long
4017 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04004018 *
4019 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4020 * css which finished ->css_online() is guaranteed to be visible in the
4021 * future iterations and will stay visible until the last reference is put.
4022 * A css which hasn't finished ->css_online() or already finished
4023 * ->css_offline() may show up during traversal. It's each subsystem's
4024 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004025 */
Tejun Heo492eb212013-08-08 20:11:25 -04004026struct cgroup_subsys_state *
4027css_next_descendant_pre(struct cgroup_subsys_state *pos,
4028 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004029{
Tejun Heo492eb212013-08-08 20:11:25 -04004030 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004031
Tejun Heo8353da12014-05-13 12:19:23 -04004032 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004033
Tejun Heobd8815a2013-08-08 20:11:27 -04004034 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09004035 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04004036 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004037
4038 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04004039 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004040 if (next)
4041 return next;
4042
4043 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04004044 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04004045 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004046 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004047 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04004048 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09004049 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08004050
4051 return NULL;
4052}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004053
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004054/**
Tejun Heo492eb212013-08-08 20:11:25 -04004055 * css_rightmost_descendant - return the rightmost descendant of a css
4056 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004057 *
Tejun Heo492eb212013-08-08 20:11:25 -04004058 * Return the rightmost descendant of @pos. If there's no descendant, @pos
4059 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004060 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09004061 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004062 * While this function requires cgroup_mutex or RCU read locking, it
4063 * doesn't require the whole traversal to be contained in a single critical
4064 * section. This function will return the correct rightmost descendant as
4065 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004066 */
Tejun Heo492eb212013-08-08 20:11:25 -04004067struct cgroup_subsys_state *
4068css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004069{
Tejun Heo492eb212013-08-08 20:11:25 -04004070 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004071
Tejun Heo8353da12014-05-13 12:19:23 -04004072 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004073
4074 do {
4075 last = pos;
4076 /* ->prev isn't RCU safe, walk ->next till the end */
4077 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04004078 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004079 pos = tmp;
4080 } while (pos);
4081
4082 return last;
4083}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004084
Tejun Heo492eb212013-08-08 20:11:25 -04004085static struct cgroup_subsys_state *
4086css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004087{
Tejun Heo492eb212013-08-08 20:11:25 -04004088 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004089
4090 do {
4091 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04004092 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004093 } while (pos);
4094
4095 return last;
4096}
4097
4098/**
Tejun Heo492eb212013-08-08 20:11:25 -04004099 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004100 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004101 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004102 *
Tejun Heo492eb212013-08-08 20:11:25 -04004103 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004104 * to visit for post-order traversal of @root's descendants. @root is
4105 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004106 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004107 * While this function requires cgroup_mutex or RCU read locking, it
4108 * doesn't require the whole traversal to be contained in a single critical
4109 * section. This function will return the correct next descendant as long
4110 * as both @pos and @cgroup are accessible and @pos is a descendant of
4111 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04004112 *
4113 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4114 * css which finished ->css_online() is guaranteed to be visible in the
4115 * future iterations and will stay visible until the last reference is put.
4116 * A css which hasn't finished ->css_online() or already finished
4117 * ->css_offline() may show up during traversal. It's each subsystem's
4118 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004119 */
Tejun Heo492eb212013-08-08 20:11:25 -04004120struct cgroup_subsys_state *
4121css_next_descendant_post(struct cgroup_subsys_state *pos,
4122 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004123{
Tejun Heo492eb212013-08-08 20:11:25 -04004124 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004125
Tejun Heo8353da12014-05-13 12:19:23 -04004126 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004127
Tejun Heo58b79a92013-09-06 15:31:08 -04004128 /* if first iteration, visit leftmost descendant which may be @root */
4129 if (!pos)
4130 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004131
Tejun Heobd8815a2013-08-08 20:11:27 -04004132 /* if we visited @root, we're done */
4133 if (pos == root)
4134 return NULL;
4135
Tejun Heo574bd9f2012-11-09 09:12:29 -08004136 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004137 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004138 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04004139 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004140
4141 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004142 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004143}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004144
Tejun Heof3d46502014-05-16 13:22:52 -04004145/**
4146 * css_has_online_children - does a css have online children
4147 * @css: the target css
4148 *
4149 * Returns %true if @css has any online children; otherwise, %false. This
4150 * function can be called from any context but the caller is responsible
4151 * for synchronizing against on/offlining as necessary.
4152 */
4153bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04004154{
Tejun Heof3d46502014-05-16 13:22:52 -04004155 struct cgroup_subsys_state *child;
4156 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04004157
4158 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04004159 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08004160 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04004161 ret = true;
4162 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04004163 }
4164 }
4165 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04004166 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08004167}
4168
Tejun Heo450ee0c2017-05-15 09:34:03 -04004169static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4170{
4171 struct list_head *l;
4172 struct cgrp_cset_link *link;
4173 struct css_set *cset;
4174
4175 lockdep_assert_held(&css_set_lock);
4176
4177 /* find the next threaded cset */
4178 if (it->tcset_pos) {
4179 l = it->tcset_pos->next;
4180
4181 if (l != it->tcset_head) {
4182 it->tcset_pos = l;
4183 return container_of(l, struct css_set,
4184 threaded_csets_node);
4185 }
4186
4187 it->tcset_pos = NULL;
4188 }
4189
4190 /* find the next cset */
4191 l = it->cset_pos;
4192 l = l->next;
4193 if (l == it->cset_head) {
4194 it->cset_pos = NULL;
4195 return NULL;
4196 }
4197
4198 if (it->ss) {
4199 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4200 } else {
4201 link = list_entry(l, struct cgrp_cset_link, cset_link);
4202 cset = link->cset;
4203 }
4204
4205 it->cset_pos = l;
4206
4207 /* initialize threaded css_set walking */
4208 if (it->flags & CSS_TASK_ITER_THREADED) {
4209 if (it->cur_dcset)
4210 put_css_set_locked(it->cur_dcset);
4211 it->cur_dcset = cset;
4212 get_css_set(cset);
4213
4214 it->tcset_head = &cset->threaded_csets;
4215 it->tcset_pos = &cset->threaded_csets;
4216 }
4217
4218 return cset;
4219}
4220
Tejun Heo0942eee2013-08-08 20:11:26 -04004221/**
Tejun Heoecb9d532015-10-15 16:41:52 -04004222 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04004223 * @it: the iterator to advance
4224 *
4225 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04004226 */
Tejun Heoecb9d532015-10-15 16:41:52 -04004227static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04004228{
Tejun Heod5158762013-08-08 20:11:26 -04004229 struct css_set *cset;
4230
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004231 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004232
Tejun Heod5158762013-08-08 20:11:26 -04004233 /* Advance to the next non-empty css_set */
4234 do {
Tejun Heo450ee0c2017-05-15 09:34:03 -04004235 cset = css_task_iter_next_css_set(it);
4236 if (!cset) {
Tejun Heoecb9d532015-10-15 16:41:52 -04004237 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04004238 return;
4239 }
Tejun Heo0de09422015-10-15 16:41:49 -04004240 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05004241
Tejun Heoc7561122014-02-25 10:04:01 -05004242 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004243 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05004244 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004245 it->task_pos = cset->mg_tasks.next;
4246
4247 it->tasks_head = &cset->tasks;
4248 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04004249
4250 /*
4251 * We don't keep css_sets locked across iteration steps and thus
4252 * need to take steps to ensure that iteration can be resumed after
4253 * the lock is re-acquired. Iteration is performed at two levels -
4254 * css_sets and tasks in them.
4255 *
4256 * Once created, a css_set never leaves its cgroup lists, so a
4257 * pinned css_set is guaranteed to stay put and we can resume
4258 * iteration afterwards.
4259 *
4260 * Tasks may leave @cset across iteration steps. This is resolved
4261 * by registering each iterator with the css_set currently being
4262 * walked and making css_set_move_task() advance iterators whose
4263 * next task is leaving.
4264 */
4265 if (it->cur_cset) {
4266 list_del(&it->iters_node);
4267 put_css_set_locked(it->cur_cset);
4268 }
4269 get_css_set(cset);
4270 it->cur_cset = cset;
4271 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04004272}
4273
Tejun Heoecb9d532015-10-15 16:41:52 -04004274static void css_task_iter_advance(struct css_task_iter *it)
4275{
Tejun Heo74d08332017-12-20 07:09:19 -08004276 struct list_head *next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004277
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004278 lockdep_assert_held(&css_set_lock);
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004279repeat:
Tejun Heoe9d81a12018-11-08 12:15:15 -08004280 if (it->task_pos) {
4281 /*
4282 * Advance iterator to find next entry. cset->tasks is
4283 * consumed first and then ->mg_tasks. After ->mg_tasks,
4284 * we move onto the next cset.
4285 */
4286 next = it->task_pos->next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004287
Tejun Heoe9d81a12018-11-08 12:15:15 -08004288 if (next == it->tasks_head)
4289 next = it->mg_tasks_head->next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004290
Tejun Heoe9d81a12018-11-08 12:15:15 -08004291 if (next == it->mg_tasks_head)
4292 css_task_iter_advance_css_set(it);
4293 else
4294 it->task_pos = next;
4295 } else {
4296 /* called from start, proceed to the first cset */
Tejun Heoecb9d532015-10-15 16:41:52 -04004297 css_task_iter_advance_css_set(it);
Tejun Heoe9d81a12018-11-08 12:15:15 -08004298 }
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004299
4300 /* if PROCS, skip over tasks which aren't group leaders */
4301 if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos &&
4302 !thread_group_leader(list_entry(it->task_pos, struct task_struct,
4303 cg_list)))
4304 goto repeat;
Tejun Heoecb9d532015-10-15 16:41:52 -04004305}
4306
Tejun Heo0942eee2013-08-08 20:11:26 -04004307/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004308 * css_task_iter_start - initiate task iteration
4309 * @css: the css to walk tasks of
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004310 * @flags: CSS_TASK_ITER_* flags
Tejun Heo0942eee2013-08-08 20:11:26 -04004311 * @it: the task iterator to use
4312 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004313 * Initiate iteration through the tasks of @css. The caller can call
4314 * css_task_iter_next() to walk through the tasks until the function
4315 * returns NULL. On completion of iteration, css_task_iter_end() must be
4316 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04004317 */
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004318void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
Tejun Heo72ec7022013-08-08 20:11:26 -04004319 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004320{
Tejun Heo56fde9e2014-02-13 06:58:38 -05004321 /* no one should try to iterate before mounting cgroups */
4322 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07004323
Tejun Heoed27b9f2015-10-15 16:41:52 -04004324 memset(it, 0, sizeof(*it));
4325
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004326 spin_lock_irq(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004327
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004328 it->ss = css->ss;
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004329 it->flags = flags;
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004330
4331 if (it->ss)
4332 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4333 else
4334 it->cset_pos = &css->cgroup->cset_links;
4335
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004336 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004337
Tejun Heoe9d81a12018-11-08 12:15:15 -08004338 css_task_iter_advance(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004339
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004340 spin_unlock_irq(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004341}
Paul Menage817929e2007-10-18 23:39:36 -07004342
Tejun Heo0942eee2013-08-08 20:11:26 -04004343/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004344 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004345 * @it: the task iterator being iterated
4346 *
4347 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004348 * initialized via css_task_iter_start(). Returns NULL when the iteration
4349 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004350 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004351struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004352{
Tejun Heod5745672015-10-29 11:43:05 +09004353 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004354 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004355 it->cur_task = NULL;
4356 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004357
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004358 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004359
Tejun Heod5745672015-10-29 11:43:05 +09004360 if (it->task_pos) {
4361 it->cur_task = list_entry(it->task_pos, struct task_struct,
4362 cg_list);
4363 get_task_struct(it->cur_task);
4364 css_task_iter_advance(it);
4365 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004366
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004367 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004368
4369 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004370}
4371
Tejun Heo0942eee2013-08-08 20:11:26 -04004372/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004373 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004374 * @it: the task iterator to finish
4375 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004376 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004377 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004378void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004379{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004380 if (it->cur_cset) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004381 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004382 list_del(&it->iters_node);
4383 put_css_set_locked(it->cur_cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004384 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004385 }
4386
Tejun Heo450ee0c2017-05-15 09:34:03 -04004387 if (it->cur_dcset)
4388 put_css_set(it->cur_dcset);
4389
Tejun Heoed27b9f2015-10-15 16:41:52 -04004390 if (it->cur_task)
4391 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004392}
4393
Tejun Heob4b90a82016-12-27 14:49:04 -05004394static void cgroup_procs_release(struct kernfs_open_file *of)
Tejun Heo8cc99342013-04-07 09:29:50 -07004395{
Tejun Heob4b90a82016-12-27 14:49:04 -05004396 if (of->priv) {
4397 css_task_iter_end(of->priv);
4398 kfree(of->priv);
4399 }
4400}
4401
4402static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4403{
4404 struct kernfs_open_file *of = s->private;
4405 struct css_task_iter *it = of->priv;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004406
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004407 return css_task_iter_next(it);
Tejun Heo8cc99342013-04-07 09:29:50 -07004408}
4409
Tejun Heo8cfd8142017-07-21 11:14:51 -04004410static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
4411 unsigned int iter_flags)
Ben Blumd1d9fd32009-09-23 15:56:28 -07004412{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004413 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004414 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heob4b90a82016-12-27 14:49:04 -05004415 struct css_task_iter *it = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004416
4417 /*
Tejun Heob4b90a82016-12-27 14:49:04 -05004418 * When a seq_file is seeked, it's always traversed sequentially
4419 * from position 0, so we can simply keep iterating on !0 *pos.
Tejun Heo4bac00d2013-11-29 10:42:59 -05004420 */
Tejun Heob4b90a82016-12-27 14:49:04 -05004421 if (!it) {
4422 if (WARN_ON_ONCE((*pos)++))
4423 return ERR_PTR(-EINVAL);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004424
Tejun Heob4b90a82016-12-27 14:49:04 -05004425 it = kzalloc(sizeof(*it), GFP_KERNEL);
4426 if (!it)
4427 return ERR_PTR(-ENOMEM);
4428 of->priv = it;
Tejun Heo450ee0c2017-05-15 09:34:03 -04004429 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heob4b90a82016-12-27 14:49:04 -05004430 } else if (!(*pos)++) {
4431 css_task_iter_end(it);
Tejun Heo450ee0c2017-05-15 09:34:03 -04004432 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004433 }
Tejun Heo4bac00d2013-11-29 10:42:59 -05004434
Tejun Heob4b90a82016-12-27 14:49:04 -05004435 return cgroup_procs_next(s, NULL, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07004436}
4437
Tejun Heo8cfd8142017-07-21 11:14:51 -04004438static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
4439{
4440 struct cgroup *cgrp = seq_css(s)->cgroup;
4441
4442 /*
4443 * All processes of a threaded subtree belong to the domain cgroup
4444 * of the subtree. Only threads can be distributed across the
4445 * subtree. Reject reads on cgroup.procs in the subtree proper.
4446 * They're always empty anyway.
4447 */
4448 if (cgroup_is_threaded(cgrp))
4449 return ERR_PTR(-EOPNOTSUPP);
4450
4451 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
4452 CSS_TASK_ITER_THREADED);
4453}
4454
Tejun Heob4b90a82016-12-27 14:49:04 -05004455static int cgroup_procs_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004456{
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004457 seq_printf(s, "%d\n", task_pid_vnr(v));
Daniel Lezcano97978e62010-10-27 15:33:35 -07004458 return 0;
4459}
4460
Tejun Heo715c8092017-05-15 09:34:00 -04004461static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
4462 struct cgroup *dst_cgrp,
4463 struct super_block *sb)
4464{
4465 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
4466 struct cgroup *com_cgrp = src_cgrp;
4467 struct inode *inode;
4468 int ret;
4469
4470 lockdep_assert_held(&cgroup_mutex);
4471
4472 /* find the common ancestor */
4473 while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
4474 com_cgrp = cgroup_parent(com_cgrp);
4475
4476 /* %current should be authorized to migrate to the common ancestor */
4477 inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn);
4478 if (!inode)
4479 return -ENOMEM;
4480
4481 ret = inode_permission(inode, MAY_WRITE);
4482 iput(inode);
4483 if (ret)
4484 return ret;
4485
4486 /*
4487 * If namespaces are delegation boundaries, %current must be able
4488 * to see both source and destination cgroups from its namespace.
4489 */
4490 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
4491 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
4492 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
4493 return -ENOENT;
4494
4495 return 0;
4496}
4497
4498static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
4499 char *buf, size_t nbytes, loff_t off)
4500{
4501 struct cgroup *src_cgrp, *dst_cgrp;
4502 struct task_struct *task;
4503 ssize_t ret;
4504
4505 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4506 if (!dst_cgrp)
4507 return -ENODEV;
4508
4509 task = cgroup_procs_write_start(buf, true);
4510 ret = PTR_ERR_OR_ZERO(task);
4511 if (ret)
4512 goto out_unlock;
4513
4514 /* find the source cgroup */
4515 spin_lock_irq(&css_set_lock);
4516 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4517 spin_unlock_irq(&css_set_lock);
4518
4519 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4520 of->file->f_path.dentry->d_sb);
4521 if (ret)
4522 goto out_finish;
4523
4524 ret = cgroup_attach_task(dst_cgrp, task, true);
4525
4526out_finish:
4527 cgroup_procs_write_finish(task);
4528out_unlock:
4529 cgroup_kn_unlock(of->kn);
4530
4531 return ret ?: nbytes;
4532}
4533
Tejun Heo8cfd8142017-07-21 11:14:51 -04004534static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
4535{
4536 return __cgroup_procs_start(s, pos, 0);
4537}
4538
4539static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
4540 char *buf, size_t nbytes, loff_t off)
4541{
4542 struct cgroup *src_cgrp, *dst_cgrp;
4543 struct task_struct *task;
4544 ssize_t ret;
4545
4546 buf = strstrip(buf);
4547
4548 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4549 if (!dst_cgrp)
4550 return -ENODEV;
4551
4552 task = cgroup_procs_write_start(buf, false);
4553 ret = PTR_ERR_OR_ZERO(task);
4554 if (ret)
4555 goto out_unlock;
4556
4557 /* find the source cgroup */
4558 spin_lock_irq(&css_set_lock);
4559 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4560 spin_unlock_irq(&css_set_lock);
4561
4562 /* thread migrations follow the cgroup.procs delegation rule */
4563 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4564 of->file->f_path.dentry->d_sb);
4565 if (ret)
4566 goto out_finish;
4567
4568 /* and must be contained in the same domain */
4569 ret = -EOPNOTSUPP;
4570 if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)
4571 goto out_finish;
4572
4573 ret = cgroup_attach_task(dst_cgrp, task, false);
4574
4575out_finish:
4576 cgroup_procs_write_finish(task);
4577out_unlock:
4578 cgroup_kn_unlock(of->kn);
4579
4580 return ret ?: nbytes;
4581}
4582
Tejun Heoa14c6872014-07-15 11:05:09 -04004583/* cgroup core interface files for the default hierarchy */
Tejun Heod62beb72016-12-27 14:49:08 -05004584static struct cftype cgroup_base_files[] = {
Tejun Heoa14c6872014-07-15 11:05:09 -04004585 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004586 .name = "cgroup.type",
4587 .flags = CFTYPE_NOT_ON_ROOT,
4588 .seq_show = cgroup_type_show,
4589 .write = cgroup_type_write,
4590 },
4591 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004592 .name = "cgroup.procs",
Tejun Heo5136f632017-06-27 14:30:28 -04004593 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004594 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heob4b90a82016-12-27 14:49:04 -05004595 .release = cgroup_procs_release,
4596 .seq_start = cgroup_procs_start,
4597 .seq_next = cgroup_procs_next,
4598 .seq_show = cgroup_procs_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004599 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004600 },
4601 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004602 .name = "cgroup.threads",
Roman Gushchin4f584242018-01-10 04:35:12 -08004603 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo8cfd8142017-07-21 11:14:51 -04004604 .release = cgroup_procs_release,
4605 .seq_start = cgroup_threads_start,
4606 .seq_next = cgroup_procs_next,
4607 .seq_show = cgroup_procs_show,
4608 .write = cgroup_threads_write,
4609 },
4610 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004611 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04004612 .seq_show = cgroup_controllers_show,
4613 },
4614 {
4615 .name = "cgroup.subtree_control",
Tejun Heo5136f632017-06-27 14:30:28 -04004616 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heoa14c6872014-07-15 11:05:09 -04004617 .seq_show = cgroup_subtree_control_show,
4618 .write = cgroup_subtree_control_write,
4619 },
4620 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004621 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004622 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004623 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004624 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004625 },
Roman Gushchin1a926e02017-07-28 18:28:44 +01004626 {
4627 .name = "cgroup.max.descendants",
4628 .seq_show = cgroup_max_descendants_show,
4629 .write = cgroup_max_descendants_write,
4630 },
4631 {
4632 .name = "cgroup.max.depth",
4633 .seq_show = cgroup_max_depth_show,
4634 .write = cgroup_max_depth_write,
4635 },
Roman Gushchinec392252017-08-02 17:55:31 +01004636 {
4637 .name = "cgroup.stat",
Tejun Heo3e489302017-08-11 05:49:01 -07004638 .seq_show = cgroup_stat_show,
Roman Gushchinec392252017-08-02 17:55:31 +01004639 },
Tejun Heod41bf8c2017-10-23 16:18:27 -07004640 {
4641 .name = "cpu.stat",
4642 .flags = CFTYPE_NOT_ON_ROOT,
4643 .seq_show = cpu_stat_show,
4644 },
Johannes Weiner2ce71352018-10-26 15:06:31 -07004645#ifdef CONFIG_PSI
4646 {
4647 .name = "io.pressure",
4648 .flags = CFTYPE_NOT_ON_ROOT,
4649 .seq_show = cgroup_io_pressure_show,
4650 },
4651 {
4652 .name = "memory.pressure",
4653 .flags = CFTYPE_NOT_ON_ROOT,
4654 .seq_show = cgroup_memory_pressure_show,
4655 },
4656 {
4657 .name = "cpu.pressure",
4658 .flags = CFTYPE_NOT_ON_ROOT,
4659 .seq_show = cgroup_cpu_pressure_show,
4660 },
4661#endif
Tejun Heoa14c6872014-07-15 11:05:09 -04004662 { } /* terminate */
4663};
4664
Tejun Heo0c21ead2013-08-13 20:22:51 -04004665/*
4666 * css destruction is four-stage process.
4667 *
4668 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4669 * Implemented in kill_css().
4670 *
4671 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004672 * and thus css_tryget_online() is guaranteed to fail, the css can be
4673 * offlined by invoking offline_css(). After offlining, the base ref is
4674 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004675 *
4676 * 3. When the percpu_ref reaches zero, the only possible remaining
4677 * accessors are inside RCU read sections. css_release() schedules the
4678 * RCU callback.
4679 *
4680 * 4. After the grace period, the css can be freed. Implemented in
4681 * css_free_work_fn().
4682 *
4683 * It is actually hairier because both step 2 and 4 require process context
4684 * and thus involve punting to css->destroy_work adding two additional
4685 * steps to the already complex sequence.
4686 */
Tejun Heo8f36aae2018-03-14 12:45:14 -07004687static void css_free_rwork_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004688{
Tejun Heo8f36aae2018-03-14 12:45:14 -07004689 struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
4690 struct cgroup_subsys_state, destroy_rwork);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004691 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004692 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004693
Tejun Heo9a1049d2014-06-28 08:10:14 -04004694 percpu_ref_exit(&css->refcnt);
4695
Vladimir Davydov01e58652015-02-12 14:59:26 -08004696 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004697 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004698 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004699 int id = css->id;
4700
Vladimir Davydov01e58652015-02-12 14:59:26 -08004701 ss->css_free(css);
4702 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004703 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004704
4705 if (parent)
4706 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004707 } else {
4708 /* cgroup free path */
4709 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heod62beb72016-12-27 14:49:08 -05004710 cgroup1_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004711 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004712
Tejun Heod51f39b2014-05-16 13:22:48 -04004713 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004714 /*
4715 * We get a ref to the parent, and put the ref when
4716 * this cgroup is being freed, so it's guaranteed
4717 * that the parent won't be destroyed before its
4718 * children.
4719 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004720 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004721 kernfs_put(cgrp->kn);
Johannes Weiner2ce71352018-10-26 15:06:31 -07004722 psi_cgroup_free(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004723 if (cgroup_on_dfl(cgrp))
Tejun Heoc58632b2018-04-26 14:29:04 -07004724 cgroup_rstat_exit(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04004725 kfree(cgrp);
4726 } else {
4727 /*
4728 * This is root cgroup's refcnt reaching zero,
4729 * which indicates that the root should be
4730 * released.
4731 */
4732 cgroup_destroy_root(cgrp->root);
4733 }
4734 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004735}
4736
Tejun Heo25e15d82014-05-14 09:15:02 -04004737static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004738{
4739 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004740 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004741 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004742 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004743
Tejun Heo1fed1b22014-05-16 13:22:49 -04004744 mutex_lock(&cgroup_mutex);
4745
Tejun Heode3f0342014-05-16 13:22:49 -04004746 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004747 list_del_rcu(&css->sibling);
4748
Tejun Heo9d755d32014-05-14 09:15:02 -04004749 if (ss) {
4750 /* css release path */
Tejun Heo8f534702018-04-26 14:29:05 -07004751 if (!list_empty(&css->rstat_css_node)) {
4752 cgroup_rstat_flush(cgrp);
4753 list_del_rcu(&css->rstat_css_node);
4754 }
4755
Vladimir Davydov01e58652015-02-12 14:59:26 -08004756 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004757 if (ss->css_released)
4758 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004759 } else {
Roman Gushchin0679dee2017-08-02 17:55:29 +01004760 struct cgroup *tcgrp;
4761
Tejun Heo9d755d32014-05-14 09:15:02 -04004762 /* cgroup release path */
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04004763 TRACE_CGROUP_PATH(release, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04004764
Tejun Heo041cd642017-09-25 08:12:05 -07004765 if (cgroup_on_dfl(cgrp))
Tejun Heoc58632b2018-04-26 14:29:04 -07004766 cgroup_rstat_flush(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004767
Roman Gushchin0679dee2017-08-02 17:55:29 +01004768 for (tcgrp = cgroup_parent(cgrp); tcgrp;
4769 tcgrp = cgroup_parent(tcgrp))
4770 tcgrp->nr_dying_descendants--;
4771
Tejun Heo9d755d32014-05-14 09:15:02 -04004772 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4773 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004774
4775 /*
4776 * There are two control paths which try to determine
4777 * cgroup from dentry without going through kernfs -
4778 * cgroupstats_build() and css_tryget_online_from_dir().
4779 * Those are supported by RCU protecting clearing of
4780 * cgrp->kn->priv backpointer.
4781 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004782 if (cgrp->kn)
4783 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4784 NULL);
Daniel Mack30070982016-11-23 16:52:26 +01004785
4786 cgroup_bpf_put(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04004787 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004788
Tejun Heo1fed1b22014-05-16 13:22:49 -04004789 mutex_unlock(&cgroup_mutex);
4790
Tejun Heo8f36aae2018-03-14 12:45:14 -07004791 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4792 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heod3daf282013-06-13 19:39:16 -07004793}
4794
Tejun Heo48ddbe12012-04-01 12:09:56 -07004795static void css_release(struct percpu_ref *ref)
4796{
4797 struct cgroup_subsys_state *css =
4798 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004799
Tejun Heo25e15d82014-05-14 09:15:02 -04004800 INIT_WORK(&css->destroy_work, css_release_work_fn);
4801 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004802}
4803
Tejun Heoddfcada2014-05-04 15:09:14 -04004804static void init_and_link_css(struct cgroup_subsys_state *css,
4805 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004806{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004807 lockdep_assert_held(&cgroup_mutex);
4808
Tejun Heoa590b902017-04-28 15:14:55 -04004809 cgroup_get_live(cgrp);
Tejun Heoddfcada2014-05-04 15:09:14 -04004810
Tejun Heod5c419b2014-05-16 13:22:48 -04004811 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004812 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004813 css->ss = ss;
Tejun Heo8fa3b8d2016-05-26 15:42:13 -04004814 css->id = -1;
Tejun Heod5c419b2014-05-16 13:22:48 -04004815 INIT_LIST_HEAD(&css->sibling);
4816 INIT_LIST_HEAD(&css->children);
Tejun Heo8f534702018-04-26 14:29:05 -07004817 INIT_LIST_HEAD(&css->rstat_css_node);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004818 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05004819 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004820
Tejun Heod51f39b2014-05-16 13:22:48 -04004821 if (cgroup_parent(cgrp)) {
4822 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004823 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004824 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004825
Tejun Heo8f534702018-04-26 14:29:05 -07004826 if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush)
4827 list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
4828
Tejun Heoca8bdca2013-08-26 18:40:56 -04004829 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004830}
4831
Li Zefan2a4ac632013-07-31 16:16:40 +08004832/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004833static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004834{
Tejun Heo623f9262013-08-13 11:01:55 -04004835 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004836 int ret = 0;
4837
Tejun Heoa31f2d32012-11-19 08:13:37 -08004838 lockdep_assert_held(&cgroup_mutex);
4839
Tejun Heo92fb9742012-11-19 08:13:38 -08004840 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004841 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004842 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004843 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004844 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05004845
4846 atomic_inc(&css->online_cnt);
4847 if (css->parent)
4848 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04004849 }
Tejun Heob1929db2012-11-19 08:13:38 -08004850 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004851}
4852
Li Zefan2a4ac632013-07-31 16:16:40 +08004853/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004854static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004855{
Tejun Heo623f9262013-08-13 11:01:55 -04004856 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004857
4858 lockdep_assert_held(&cgroup_mutex);
4859
4860 if (!(css->flags & CSS_ONLINE))
4861 return;
4862
Li Zefand7eeac12013-03-12 15:35:59 -07004863 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004864 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004865
Tejun Heoeb954192013-08-08 20:11:23 -04004866 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004867 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004868
4869 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004870}
4871
Tejun Heoc81c925a2013-12-06 15:11:56 -05004872/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004873 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05004874 * @cgrp: the cgroup new css will be associated with
4875 * @ss: the subsys of new css
4876 *
4877 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004878 * css is online and installed in @cgrp. This function doesn't create the
4879 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004880 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004881static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4882 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004883{
Tejun Heod51f39b2014-05-16 13:22:48 -04004884 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004885 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004886 struct cgroup_subsys_state *css;
4887 int err;
4888
Tejun Heoc81c925a2013-12-06 15:11:56 -05004889 lockdep_assert_held(&cgroup_mutex);
4890
Tejun Heo1fed1b22014-05-16 13:22:49 -04004891 css = ss->css_alloc(parent_css);
Tejun Heoe7e15b82016-06-21 13:06:24 -04004892 if (!css)
4893 css = ERR_PTR(-ENOMEM);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004894 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004895 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004896
Tejun Heoddfcada2014-05-04 15:09:14 -04004897 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004898
Tejun Heo2aad2a82014-09-24 13:31:50 -04004899 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004900 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004901 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004902
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004903 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004904 if (err < 0)
Wenwei Taob00c52d2016-05-13 22:59:20 +08004905 goto err_free_css;
Tejun Heo15a4c832014-05-04 15:09:14 -04004906 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004907
Tejun Heo15a4c832014-05-04 15:09:14 -04004908 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004909 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004910 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004911
4912 err = online_css(css);
4913 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004914 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004915
Tejun Heoc81c925a2013-12-06 15:11:56 -05004916 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004917 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004918 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 -04004919 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004920 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004921 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004922 ss->warned_broken_hierarchy = true;
4923 }
4924
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004925 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004926
Tejun Heo1fed1b22014-05-16 13:22:49 -04004927err_list_del:
4928 list_del_rcu(&css->sibling);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004929err_free_css:
Tejun Heo8f534702018-04-26 14:29:05 -07004930 list_del_rcu(&css->rstat_css_node);
Tejun Heo8f36aae2018-03-14 12:45:14 -07004931 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4932 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004933 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004934}
4935
Tejun Heo07cd1292017-01-26 16:47:28 -05004936/*
4937 * The returned cgroup is fully initialized including its control mask, but
4938 * it isn't associated with its kernfs_node and doesn't have the control
4939 * mask applied.
4940 */
Tejun Heoa5bca212016-03-03 09:57:58 -05004941static struct cgroup *cgroup_create(struct cgroup *parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004942{
Tejun Heoa5bca212016-03-03 09:57:58 -05004943 struct cgroup_root *root = parent->root;
Tejun Heoa5bca212016-03-03 09:57:58 -05004944 struct cgroup *cgrp, *tcgrp;
4945 int level = parent->level + 1;
Tejun Heo03970d32016-03-03 09:58:00 -05004946 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004947
Tejun Heo0a950f62012-11-19 09:02:12 -08004948 /* allocate the cgroup and its ID, 0 is reserved for the root */
Kees Cookacafe7e2018-05-08 13:45:50 -07004949 cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)),
4950 GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05004951 if (!cgrp)
4952 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004953
Tejun Heo2aad2a82014-09-24 13:31:50 -04004954 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004955 if (ret)
4956 goto out_free_cgrp;
4957
Tejun Heo041cd642017-09-25 08:12:05 -07004958 if (cgroup_on_dfl(parent)) {
Tejun Heoc58632b2018-04-26 14:29:04 -07004959 ret = cgroup_rstat_init(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004960 if (ret)
4961 goto out_cancel_ref;
4962 }
4963
Li Zefan0ab02ca2014-02-11 16:05:46 +08004964 /*
4965 * Temporarily set the pointer to NULL, so idr_find() won't return
4966 * a half-baked cgroup.
4967 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004968 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004969 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004970 ret = -ENOMEM;
Tejun Heo041cd642017-09-25 08:12:05 -07004971 goto out_stat_exit;
Tejun Heo976c06b2012-11-05 09:16:59 -08004972 }
4973
Paul Menagecc31edc2008-10-18 20:28:04 -07004974 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004975
Tejun Heo9d800df2014-05-14 09:15:00 -04004976 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004977 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004978 cgrp->level = level;
Johannes Weiner2ce71352018-10-26 15:06:31 -07004979
4980 ret = psi_cgroup_alloc(cgrp);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07004981 if (ret)
4982 goto out_idr_free;
Tejun Heob11cfb52015-11-20 15:55:52 -05004983
Johannes Weiner2ce71352018-10-26 15:06:31 -07004984 ret = cgroup_bpf_inherit(cgrp);
4985 if (ret)
4986 goto out_psi_free;
4987
Roman Gushchin0679dee2017-08-02 17:55:29 +01004988 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
Tejun Heob11cfb52015-11-20 15:55:52 -05004989 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004990
Roman Gushchin0679dee2017-08-02 17:55:29 +01004991 if (tcgrp != cgrp)
4992 tcgrp->nr_descendants++;
4993 }
4994
Li Zefanb6abdb02008-03-04 14:28:19 -08004995 if (notify_on_release(parent))
4996 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4997
Tejun Heo2260e7f2012-11-19 08:13:38 -08004998 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4999 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005000
Tejun Heo0cb51d72014-05-16 13:22:49 -04005001 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09005002
Tejun Heo4e139af2012-11-19 08:13:36 -08005003 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04005004 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05005005 atomic_inc(&root->nr_cgrps);
Tejun Heoa590b902017-04-28 15:14:55 -04005006 cgroup_get_live(parent);
Li Zefan415cf072013-04-08 14:35:02 +08005007
Tejun Heo0d802552013-12-06 15:11:56 -05005008 /*
5009 * @cgrp is now fully operational. If something fails after this
5010 * point, it'll be released via the normal destruction path.
5011 */
Tejun Heo6fa49182014-05-04 15:09:13 -04005012 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005013
Tejun Heobd53d612014-04-23 11:13:16 -04005014 /*
5015 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04005016 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04005017 */
Tejun Heo03970d32016-03-03 09:58:00 -05005018 if (!cgroup_on_dfl(cgrp))
Tejun Heo5531dc92016-03-03 09:57:58 -05005019 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo03970d32016-03-03 09:58:00 -05005020
5021 cgroup_propagate_control(cgrp);
5022
Tejun Heoa5bca212016-03-03 09:57:58 -05005023 return cgrp;
5024
Johannes Weiner2ce71352018-10-26 15:06:31 -07005025out_psi_free:
5026 psi_cgroup_free(cgrp);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07005027out_idr_free:
5028 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo041cd642017-09-25 08:12:05 -07005029out_stat_exit:
5030 if (cgroup_on_dfl(parent))
Tejun Heoc58632b2018-04-26 14:29:04 -07005031 cgroup_rstat_exit(cgrp);
Tejun Heoa5bca212016-03-03 09:57:58 -05005032out_cancel_ref:
5033 percpu_ref_exit(&cgrp->self.refcnt);
5034out_free_cgrp:
5035 kfree(cgrp);
5036 return ERR_PTR(ret);
Tejun Heoa5bca212016-03-03 09:57:58 -05005037}
5038
Roman Gushchin1a926e02017-07-28 18:28:44 +01005039static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
5040{
5041 struct cgroup *cgroup;
5042 int ret = false;
5043 int level = 1;
5044
5045 lockdep_assert_held(&cgroup_mutex);
5046
5047 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
5048 if (cgroup->nr_descendants >= cgroup->max_descendants)
5049 goto fail;
5050
5051 if (level > cgroup->max_depth)
5052 goto fail;
5053
5054 level++;
5055 }
5056
5057 ret = true;
5058fail:
5059 return ret;
5060}
5061
Tejun Heo1592c9b2016-12-27 14:49:08 -05005062int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
Tejun Heoa5bca212016-03-03 09:57:58 -05005063{
5064 struct cgroup *parent, *cgrp;
Tejun Heoa5bca212016-03-03 09:57:58 -05005065 struct kernfs_node *kn;
Tejun Heo03970d32016-03-03 09:58:00 -05005066 int ret;
Tejun Heoa5bca212016-03-03 09:57:58 -05005067
5068 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5069 if (strchr(name, '\n'))
5070 return -EINVAL;
5071
Tejun Heo945ba192016-03-03 09:58:00 -05005072 parent = cgroup_kn_lock_live(parent_kn, false);
Tejun Heoa5bca212016-03-03 09:57:58 -05005073 if (!parent)
5074 return -ENODEV;
5075
Roman Gushchin1a926e02017-07-28 18:28:44 +01005076 if (!cgroup_check_hierarchy_limits(parent)) {
5077 ret = -EAGAIN;
5078 goto out_unlock;
5079 }
5080
Tejun Heoa5bca212016-03-03 09:57:58 -05005081 cgrp = cgroup_create(parent);
5082 if (IS_ERR(cgrp)) {
5083 ret = PTR_ERR(cgrp);
5084 goto out_unlock;
5085 }
5086
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005087 /* create the directory */
5088 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5089 if (IS_ERR(kn)) {
5090 ret = PTR_ERR(kn);
5091 goto out_destroy;
5092 }
5093 cgrp->kn = kn;
5094
5095 /*
5096 * This extra ref will be put in cgroup_free_fn() and guarantees
5097 * that @cgrp->kn is always accessible.
5098 */
5099 kernfs_get(kn);
5100
5101 ret = cgroup_kn_set_ugid(kn);
5102 if (ret)
5103 goto out_destroy;
5104
Tejun Heo334c3672016-03-03 09:58:01 -05005105 ret = css_populate_dir(&cgrp->self);
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005106 if (ret)
5107 goto out_destroy;
5108
Tejun Heo03970d32016-03-03 09:58:00 -05005109 ret = cgroup_apply_control_enable(cgrp);
5110 if (ret)
5111 goto out_destroy;
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005112
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04005113 TRACE_CGROUP_PATH(mkdir, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005114
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005115 /* let's create and online css's */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005116 kernfs_activate(kn);
5117
Tejun Heoba0f4d72014-05-13 12:19:22 -04005118 ret = 0;
5119 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005120
Tejun Heoa5bca212016-03-03 09:57:58 -05005121out_destroy:
5122 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005123out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005124 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005125 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005126}
5127
Tejun Heo223dbc32013-08-13 20:22:50 -04005128/*
5129 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005130 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5131 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005132 */
5133static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005134{
Tejun Heo223dbc32013-08-13 20:22:50 -04005135 struct cgroup_subsys_state *css =
5136 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005137
Tejun Heof20104d2013-08-13 20:22:50 -04005138 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005139
Tejun Heoaa226ff2016-01-21 15:31:11 -05005140 do {
5141 offline_css(css);
5142 css_put(css);
5143 /* @css can't go away while we're holding cgroup_mutex */
5144 css = css->parent;
5145 } while (css && atomic_dec_and_test(&css->online_cnt));
5146
5147 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005148}
5149
Tejun Heo223dbc32013-08-13 20:22:50 -04005150/* css kill confirmation processing requires process context, bounce */
5151static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005152{
5153 struct cgroup_subsys_state *css =
5154 container_of(ref, struct cgroup_subsys_state, refcnt);
5155
Tejun Heoaa226ff2016-01-21 15:31:11 -05005156 if (atomic_dec_and_test(&css->online_cnt)) {
5157 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5158 queue_work(cgroup_destroy_wq, &css->destroy_work);
5159 }
Tejun Heod3daf282013-06-13 19:39:16 -07005160}
5161
Tejun Heof392e512014-04-23 11:13:14 -04005162/**
5163 * kill_css - destroy a css
5164 * @css: css to destroy
5165 *
5166 * This function initiates destruction of @css by removing cgroup interface
5167 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005168 * asynchronously once css_tryget_online() is guaranteed to fail and when
5169 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005170 */
5171static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005172{
Tejun Heo01f64742014-05-13 12:19:23 -04005173 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005174
Waiman Long33c35aa2017-05-15 09:34:06 -04005175 if (css->flags & CSS_DYING)
5176 return;
5177
5178 css->flags |= CSS_DYING;
5179
Tejun Heo2bd59d42014-02-11 11:52:49 -05005180 /*
5181 * This must happen before css is disassociated with its cgroup.
5182 * See seq_css() for details.
5183 */
Tejun Heo334c3672016-03-03 09:58:01 -05005184 css_clear_dir(css);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005185
Tejun Heoedae0c32013-08-13 20:22:51 -04005186 /*
5187 * Killing would put the base ref, but we need to keep it alive
5188 * until after ->css_offline().
5189 */
5190 css_get(css);
5191
5192 /*
5193 * cgroup core guarantees that, by the time ->css_offline() is
5194 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005195 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005196 * proceed to offlining css's because percpu_ref_kill() doesn't
5197 * guarantee that the ref is seen as killed on all CPUs on return.
5198 *
5199 * Use percpu_ref_kill_and_confirm() to get notifications as each
5200 * css is confirmed to be seen as killed on all CPUs.
5201 */
5202 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005203}
5204
5205/**
5206 * cgroup_destroy_locked - the first stage of cgroup destruction
5207 * @cgrp: cgroup to be destroyed
5208 *
5209 * css's make use of percpu refcnts whose killing latency shouldn't be
5210 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005211 * guarantee that css_tryget_online() won't succeed by the time
5212 * ->css_offline() is invoked. To satisfy all the requirements,
5213 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005214 *
5215 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5216 * userland visible parts and start killing the percpu refcnts of
5217 * css's. Set up so that the next stage will be kicked off once all
5218 * the percpu refcnts are confirmed to be killed.
5219 *
5220 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5221 * rest of destruction. Once all cgroup references are gone, the
5222 * cgroup is RCU-freed.
5223 *
5224 * This function implements s1. After this step, @cgrp is gone as far as
5225 * the userland is concerned and a new cgroup with the same name may be
5226 * created. As cgroup doesn't care about the names internally, this
5227 * doesn't cause any problem.
5228 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005229static int cgroup_destroy_locked(struct cgroup *cgrp)
5230 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005231{
Roman Gushchin0679dee2017-08-02 17:55:29 +01005232 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005233 struct cgroup_subsys_state *css;
Tejun Heo2b021cb2016-03-15 20:43:04 -04005234 struct cgrp_cset_link *link;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005235 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005236
Tejun Heo42809dd2012-11-19 08:13:37 -08005237 lockdep_assert_held(&cgroup_mutex);
5238
Tejun Heo91486f62015-10-15 16:41:51 -04005239 /*
5240 * Only migration can raise populated from zero and we're already
5241 * holding cgroup_mutex.
5242 */
5243 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005244 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005245
Tejun Heo1a90dd52012-11-05 09:16:59 -08005246 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005247 * Make sure there's no live children. We can't test emptiness of
5248 * ->self.children as dead children linger on it while being
5249 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005250 */
Tejun Heof3d46502014-05-16 13:22:52 -04005251 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005252 return -EBUSY;
5253
5254 /*
Tejun Heo2b021cb2016-03-15 20:43:04 -04005255 * Mark @cgrp and the associated csets dead. The former prevents
5256 * further task migration and child creation by disabling
5257 * cgroup_lock_live_group(). The latter makes the csets ignored by
5258 * the migration path.
Tejun Heo455050d2013-06-13 19:27:41 -07005259 */
Tejun Heo184faf32014-05-16 13:22:51 -04005260 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005261
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005262 spin_lock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005263 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5264 link->cset->dead = true;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005265 spin_unlock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005266
Tejun Heo249f3462014-05-14 09:15:01 -04005267 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005268 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005269 kill_css(css);
5270
Tejun Heo5faaf052018-04-26 14:29:04 -07005271 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5272 css_clear_dir(&cgrp->self);
Tejun Heo01f64742014-05-13 12:19:23 -04005273 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005274
Tejun Heo454000a2017-05-15 09:34:02 -04005275 if (parent && cgroup_is_threaded(cgrp))
5276 parent->nr_threaded_children--;
5277
Roman Gushchin0679dee2017-08-02 17:55:29 +01005278 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
5279 tcgrp->nr_descendants--;
5280 tcgrp->nr_dying_descendants++;
5281 }
5282
Roman Gushchin5a621e62017-08-02 17:55:32 +01005283 cgroup1_check_for_release(parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005284
Tejun Heo249f3462014-05-14 09:15:01 -04005285 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005286 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005287
Tejun Heoea15f8c2013-06-13 19:27:42 -07005288 return 0;
5289};
5290
Tejun Heo1592c9b2016-12-27 14:49:08 -05005291int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005292{
Tejun Heoa9746d82014-05-13 12:19:22 -04005293 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005294 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005295
Tejun Heo945ba192016-03-03 09:58:00 -05005296 cgrp = cgroup_kn_lock_live(kn, false);
Tejun Heoa9746d82014-05-13 12:19:22 -04005297 if (!cgrp)
5298 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005299
Tejun Heoa9746d82014-05-13 12:19:22 -04005300 ret = cgroup_destroy_locked(cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005301 if (!ret)
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04005302 TRACE_CGROUP_PATH(rmdir, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005303
Tejun Heoa9746d82014-05-13 12:19:22 -04005304 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005305 return ret;
5306}
5307
Tejun Heo2bd59d42014-02-11 11:52:49 -05005308static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
Tejun Heo5136f632017-06-27 14:30:28 -04005309 .show_options = cgroup_show_options,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005310 .mkdir = cgroup_mkdir,
5311 .rmdir = cgroup_rmdir,
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05005312 .show_path = cgroup_show_path,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005313};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005314
Tejun Heo15a4c832014-05-04 15:09:14 -04005315static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005316{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005317 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005318
Tejun Heoa5ae9892015-12-29 14:53:56 -05005319 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005320
Tejun Heo648bb562012-11-19 08:13:36 -08005321 mutex_lock(&cgroup_mutex);
5322
Tejun Heo15a4c832014-05-04 15:09:14 -04005323 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005324 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005325
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005326 /* Create the root cgroup state for this subsystem */
5327 ss->root = &cgrp_dfl_root;
5328 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005329 /* We don't handle early failures gracefully */
5330 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005331 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005332
5333 /*
5334 * Root csses are never destroyed and we can't initialize
5335 * percpu_ref during early init. Disable refcnting.
5336 */
5337 css->flags |= CSS_NO_REF;
5338
Tejun Heo15a4c832014-05-04 15:09:14 -04005339 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005340 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005341 css->id = 1;
5342 } else {
5343 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5344 BUG_ON(css->id < 0);
5345 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005346
Li Zefane8d55fd2008-04-29 01:00:13 -07005347 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005348 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005349 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005350 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005351 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005352
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005353 have_fork_callback |= (bool)ss->fork << ss->id;
5354 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005355 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005356 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005357
Li Zefane8d55fd2008-04-29 01:00:13 -07005358 /* At system boot, before all subsystems have been
5359 * registered, no tasks have been forked, so we don't
5360 * need to invoke fork callbacks here. */
5361 BUG_ON(!list_empty(&init_task.tasks));
5362
Tejun Heoae7f1642013-08-13 20:22:50 -04005363 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005364
Tejun Heo648bb562012-11-19 08:13:36 -08005365 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005366}
5367
5368/**
Li Zefana043e3b2008-02-23 15:24:09 -08005369 * cgroup_init_early - cgroup initialization at system boot
5370 *
5371 * Initialize cgroups at system boot, and initialize any
5372 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005373 */
5374int __init cgroup_init_early(void)
5375{
Al Virof5dfb532019-01-16 23:42:38 -05005376 static struct cgroup_fs_context __initdata ctx;
Tejun Heo30159ec2013-06-25 11:53:37 -07005377 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005378 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005379
Al Virocf6299b12019-01-17 02:25:51 -05005380 ctx.root = &cgrp_dfl_root;
5381 init_cgroup_root(&ctx);
Tejun Heo3b514d22014-05-16 13:22:47 -04005382 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5383
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005384 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005385
Tejun Heo3ed80a62014-02-08 10:36:58 -05005386 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005387 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005388 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005389 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005390 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005391 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5392 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005393
Tejun Heoaec25022014-02-08 10:36:58 -05005394 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005395 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005396 if (!ss->legacy_name)
5397 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005398
5399 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005400 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005401 }
5402 return 0;
5403}
5404
Tejun Heo6e5c8302016-02-22 22:25:47 -05005405static u16 cgroup_disable_mask __initdata;
Tejun Heoa3e72732015-09-25 16:24:27 -04005406
Paul Menageddbcc7e2007-10-18 23:39:30 -07005407/**
Li Zefana043e3b2008-02-23 15:24:09 -08005408 * cgroup_init - cgroup initialization
5409 *
5410 * Register cgroup filesystem and /proc file, and initialize
5411 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005412 */
5413int __init cgroup_init(void)
5414{
Tejun Heo30159ec2013-06-25 11:53:37 -07005415 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005416 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005417
Tejun Heo6e5c8302016-02-22 22:25:47 -05005418 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005419 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heod62beb72016-12-27 14:49:08 -05005420 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
5421 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005422
Tejun Heoc58632b2018-04-26 14:29:04 -07005423 cgroup_rstat_boot();
Tejun Heo041cd642017-09-25 08:12:05 -07005424
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005425 /*
Paul E. McKenney2af30242018-11-07 14:11:40 -08005426 * The latency of the synchronize_rcu() is too high for cgroups,
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005427 * avoid it at the cost of forcing all readers into the slow path.
5428 */
5429 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5430
Aditya Kalia79a9082016-01-29 02:54:06 -06005431 get_user_ns(init_cgroup_ns.user_ns);
5432
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005433 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005434
Tejun Heo2378d8b2016-03-03 09:57:57 -05005435 /*
5436 * Add init_css_set to the hash table so that dfl_root can link to
5437 * it during init.
5438 */
5439 hash_add(css_set_table, &init_css_set.hlist,
5440 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005441
Al Viro35ac1182019-01-12 00:20:54 -05005442 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005443
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005444 mutex_unlock(&cgroup_mutex);
5445
Tejun Heo172a2c062014-03-19 10:23:53 -04005446 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005447 if (ss->early_init) {
5448 struct cgroup_subsys_state *css =
5449 init_css_set.subsys[ss->id];
5450
5451 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5452 GFP_KERNEL);
5453 BUG_ON(css->id < 0);
5454 } else {
5455 cgroup_init_subsys(ss, false);
5456 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005457
Tejun Heo2d8f2432014-04-23 11:13:15 -04005458 list_add_tail(&init_css_set.e_cset_node[ssid],
5459 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005460
5461 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005462 * Setting dfl_root subsys_mask needs to consider the
5463 * disabled flag and cftype registration needs kmalloc,
5464 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005465 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005466 if (cgroup_disable_mask & (1 << ssid)) {
5467 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5468 printk(KERN_INFO "Disabling %s control group subsystem\n",
5469 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005470 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005471 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005472
Tejun Heod62beb72016-12-27 14:49:08 -05005473 if (cgroup1_ssid_disabled(ssid))
Johannes Weiner223ffb22016-02-11 13:34:49 -05005474 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5475 ss->name);
5476
Tejun Heoa8ddc822014-07-15 11:05:10 -04005477 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5478
Tejun Heo8cfd8142017-07-21 11:14:51 -04005479 /* implicit controllers must be threaded too */
5480 WARN_ON(ss->implicit_on_dfl && !ss->threaded);
5481
Tejun Heof6d635ad2016-03-08 11:51:26 -05005482 if (ss->implicit_on_dfl)
5483 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5484 else if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005485 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005486
Tejun Heo8cfd8142017-07-21 11:14:51 -04005487 if (ss->threaded)
5488 cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
5489
Tejun Heoa8ddc822014-07-15 11:05:10 -04005490 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5491 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5492 } else {
5493 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5494 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005495 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005496
5497 if (ss->bind)
5498 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo7af608e2017-07-18 17:57:46 -04005499
5500 mutex_lock(&cgroup_mutex);
5501 css_populate_dir(init_css_set.subsys[ssid]);
5502 mutex_unlock(&cgroup_mutex);
Tejun Heo172a2c062014-03-19 10:23:53 -04005503 }
Greg KH676db4a2010-08-05 13:53:35 -07005504
Tejun Heo2378d8b2016-03-03 09:57:57 -05005505 /* init_css_set.subsys[] has been updated, re-hash */
5506 hash_del(&init_css_set.hlist);
5507 hash_add(css_set_table, &init_css_set.hlist,
5508 css_set_hash(init_css_set.subsys));
5509
Tejun Heo035f4f52015-10-15 17:00:43 -04005510 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5511 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005512 WARN_ON(register_filesystem(&cgroup2_fs_type));
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02005513 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
Paul Menagea4243162007-10-18 23:39:35 -07005514
Tejun Heo2bd59d42014-02-11 11:52:49 -05005515 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005516}
Paul Menageb4f48b62007-10-18 23:39:33 -07005517
Tejun Heoe5fca242013-11-22 17:14:39 -05005518static int __init cgroup_wq_init(void)
5519{
5520 /*
5521 * There isn't much point in executing destruction path in
5522 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005523 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005524 *
5525 * We would prefer to do this in cgroup_init() above, but that
5526 * is called before init_workqueues(): so leave this until after.
5527 */
Tejun Heo1a115332014-02-12 19:06:19 -05005528 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005529 BUG_ON(!cgroup_destroy_wq);
5530 return 0;
5531}
5532core_initcall(cgroup_wq_init);
5533
Shaohua Li69fd5c32017-07-12 11:49:55 -07005534void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
5535 char *buf, size_t buflen)
5536{
5537 struct kernfs_node *kn;
5538
5539 kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id);
5540 if (!kn)
5541 return;
5542 kernfs_path(kn, buf, buflen);
5543 kernfs_put(kn);
5544}
5545
Paul Menagea4243162007-10-18 23:39:35 -07005546/*
5547 * proc_cgroup_show()
5548 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5549 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005550 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005551int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5552 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005553{
Tejun Heo4c737b42016-08-10 11:23:44 -04005554 char *buf;
Paul Menagea4243162007-10-18 23:39:35 -07005555 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005556 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005557
5558 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005559 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005560 if (!buf)
5561 goto out;
5562
Paul Menagea4243162007-10-18 23:39:35 -07005563 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005564 spin_lock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005565
Tejun Heo985ed672014-03-19 10:23:53 -04005566 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005567 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005568 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005569 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005570
Tejun Heoa7165262016-02-23 10:00:50 -05005571 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005572 continue;
5573
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005574 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005575 if (root != &cgrp_dfl_root)
5576 for_each_subsys(ss, ssid)
5577 if (root->subsys_mask & (1 << ssid))
5578 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005579 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005580 if (strlen(root->name))
5581 seq_printf(m, "%sname=%s", count ? "," : "",
5582 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005583 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005584
Paul Menage7717f7b2009-09-23 15:56:22 -07005585 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005586
5587 /*
5588 * On traditional hierarchies, all zombie tasks show up as
5589 * belonging to the root cgroup. On the default hierarchy,
5590 * while a zombie doesn't show up in "cgroup.procs" and
5591 * thus can't be migrated, its /proc/PID/cgroup keeps
5592 * reporting the cgroup it belonged to before exiting. If
5593 * the cgroup is removed before the zombie is reaped,
5594 * " (deleted)" is appended to the cgroup path.
5595 */
5596 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
Tejun Heo4c737b42016-08-10 11:23:44 -04005597 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
Aditya Kalia79a9082016-01-29 02:54:06 -06005598 current->nsproxy->cgroup_ns);
Tejun Heoe0223002016-09-29 15:49:40 +02005599 if (retval >= PATH_MAX)
Tejun Heo2e91fa72015-10-15 16:41:53 -04005600 retval = -ENAMETOOLONG;
Tejun Heoe0223002016-09-29 15:49:40 +02005601 if (retval < 0)
Tejun Heo2e91fa72015-10-15 16:41:53 -04005602 goto out_unlock;
Tejun Heo2e91fa72015-10-15 16:41:53 -04005603
Tejun Heo4c737b42016-08-10 11:23:44 -04005604 seq_puts(m, buf);
5605 } else {
5606 seq_puts(m, "/");
5607 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005608
5609 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5610 seq_puts(m, " (deleted)\n");
5611 else
5612 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005613 }
5614
Zefan Li006f4ac2014-09-18 16:03:15 +08005615 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005616out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005617 spin_unlock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005618 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005619 kfree(buf);
5620out:
5621 return retval;
5622}
5623
Paul Menageb4f48b62007-10-18 23:39:33 -07005624/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005625 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005626 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005627 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005628 * A task is associated with the init_css_set until cgroup_post_fork()
5629 * attaches it to the parent's css_set. Empty cg_list indicates that
5630 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005631 */
5632void cgroup_fork(struct task_struct *child)
5633{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005634 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005635 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005636}
5637
5638/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005639 * cgroup_can_fork - called on a new task before the process is exposed
5640 * @child: the task in question.
5641 *
5642 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5643 * returns an error, the fork aborts with that error code. This allows for
5644 * a cgroup subsystem to conditionally allow or deny new forks.
5645 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005646int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005647{
5648 struct cgroup_subsys *ss;
5649 int i, j, ret;
5650
Tejun Heob4e0eea2016-02-22 22:25:46 -05005651 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005652 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005653 if (ret)
5654 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05005655 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10005656
5657 return 0;
5658
5659out_revert:
5660 for_each_subsys(ss, j) {
5661 if (j >= i)
5662 break;
5663 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005664 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005665 }
5666
5667 return ret;
5668}
5669
5670/**
5671 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5672 * @child: the task in question
5673 *
5674 * This calls the cancel_fork() callbacks if a fork failed *after*
5675 * cgroup_can_fork() succeded.
5676 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005677void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005678{
5679 struct cgroup_subsys *ss;
5680 int i;
5681
5682 for_each_subsys(ss, i)
5683 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005684 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005685}
5686
5687/**
Li Zefana043e3b2008-02-23 15:24:09 -08005688 * cgroup_post_fork - called on a new task after adding it to the task list
5689 * @child: the task in question
5690 *
Tejun Heo5edee612012-10-16 15:03:14 -07005691 * Adds the task to the list running through its css_set if necessary and
5692 * call the subsystem fork() callbacks. Has to be after the task is
5693 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005694 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005695 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005696 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005697void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005698{
Tejun Heo30159ec2013-06-25 11:53:37 -07005699 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005700 int i;
5701
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005702 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005703 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005704 * function sets use_task_css_set_links before grabbing
5705 * tasklist_lock and we just went through tasklist_lock to add
5706 * @child, it's guaranteed that either we see the set
5707 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5708 * @child during its iteration.
5709 *
5710 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005711 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005712 * association is stable, and, on completion of the parent's
5713 * migration, @child is visible in the source of migration or
5714 * already in the destination cgroup. This guarantee is necessary
5715 * when implementing operations which need to migrate all tasks of
5716 * a cgroup to another.
5717 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005718 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005719 * will remain in init_css_set. This is safe because all tasks are
5720 * in the init_css_set before cg_links is enabled and there's no
5721 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005722 */
Paul Menage817929e2007-10-18 23:39:36 -07005723 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005724 struct css_set *cset;
5725
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005726 spin_lock_irq(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005727 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005728 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005729 get_css_set(cset);
Waiman Long73a72422017-06-13 17:18:01 -04005730 cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04005731 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005732 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005733 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005734 }
Tejun Heo5edee612012-10-16 15:03:14 -07005735
5736 /*
5737 * Call ss->fork(). This must happen after @child is linked on
5738 * css_set; otherwise, @child might change state between ->fork()
5739 * and addition to css_set.
5740 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005741 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005742 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005743 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07005744}
Tejun Heo5edee612012-10-16 15:03:14 -07005745
Paul Menage817929e2007-10-18 23:39:36 -07005746/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005747 * cgroup_exit - detach cgroup from exiting task
5748 * @tsk: pointer to task_struct of exiting process
5749 *
5750 * Description: Detach cgroup from @tsk and release it.
5751 *
5752 * Note that cgroups marked notify_on_release force every task in
5753 * them to take the global cgroup_mutex mutex when exiting.
5754 * This could impact scaling on very large systems. Be reluctant to
5755 * use notify_on_release cgroups where very high task exit scaling
5756 * is required on large systems.
5757 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005758 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5759 * call cgroup_exit() while the task is still competent to handle
5760 * notify_on_release(), then leave the task attached to the root cgroup in
5761 * each hierarchy for the remainder of its exit. No need to bother with
5762 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005763 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005764 */
Li Zefan1ec41832014-03-28 15:22:19 +08005765void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005766{
Tejun Heo30159ec2013-06-25 11:53:37 -07005767 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005768 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005769 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005770
5771 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005772 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005773 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005774 */
Tejun Heo0de09422015-10-15 16:41:49 -04005775 cset = task_css_set(tsk);
5776
Paul Menage817929e2007-10-18 23:39:36 -07005777 if (!list_empty(&tsk->cg_list)) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005778 spin_lock_irq(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005779 css_set_move_task(tsk, cset, NULL, false);
Waiman Long73a72422017-06-13 17:18:01 -04005780 cset->nr_tasks--;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005781 spin_unlock_irq(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005782 } else {
5783 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005784 }
5785
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005786 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005787 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04005788 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005789 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04005790}
Tejun Heo30159ec2013-06-25 11:53:37 -07005791
Tejun Heo2e91fa72015-10-15 16:41:53 -04005792void cgroup_free(struct task_struct *task)
5793{
5794 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005795 struct cgroup_subsys *ss;
5796 int ssid;
5797
Tejun Heob4e0eea2016-02-22 22:25:46 -05005798 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04005799 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005800 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005801
Tejun Heo2e91fa72015-10-15 16:41:53 -04005802 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005803}
Paul Menage697f4162007-10-18 23:39:34 -07005804
Paul Menage8bab8dd2008-04-04 14:29:57 -07005805static int __init cgroup_disable(char *str)
5806{
Tejun Heo30159ec2013-06-25 11:53:37 -07005807 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005808 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005809 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005810
5811 while ((token = strsep(&str, ",")) != NULL) {
5812 if (!*token)
5813 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005814
Tejun Heo3ed80a62014-02-08 10:36:58 -05005815 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005816 if (strcmp(token, ss->name) &&
5817 strcmp(token, ss->legacy_name))
5818 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005819 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005820 }
5821 }
5822 return 1;
5823}
5824__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005825
Waiman Long5cf81142018-11-08 10:08:46 -05005826void __init __weak enable_debug_cgroup(void) { }
5827
5828static int __init enable_cgroup_debug(char *str)
5829{
5830 cgroup_debug = true;
5831 enable_debug_cgroup();
5832 return 1;
5833}
5834__setup("cgroup_debug", enable_cgroup_debug);
5835
Tejun Heob77d7b62013-08-13 11:01:54 -04005836/**
Tejun Heoec903c02014-05-13 12:11:01 -04005837 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005838 * @dentry: directory dentry of interest
5839 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005840 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005841 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5842 * to get the corresponding css and return it. If such css doesn't exist
5843 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005844 */
Tejun Heoec903c02014-05-13 12:11:01 -04005845struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5846 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005847{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005848 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05005849 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005850 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005851 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005852
Tejun Heo35cf0832013-08-26 18:40:56 -04005853 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05005854 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5855 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005856 return ERR_PTR(-EBADF);
5857
Tejun Heo5a17f542014-02-11 11:52:47 -05005858 rcu_read_lock();
5859
Tejun Heo2bd59d42014-02-11 11:52:49 -05005860 /*
5861 * This path doesn't originate from kernfs and @kn could already
5862 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005863 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005864 */
Tejun Heoe0aed7c2016-12-27 14:49:09 -05005865 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005866 if (cgrp)
5867 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005868
Tejun Heoec903c02014-05-13 12:11:01 -04005869 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005870 css = ERR_PTR(-ENOENT);
5871
5872 rcu_read_unlock();
5873 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005874}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005875
Li Zefan1cb650b2013-08-19 10:05:24 +08005876/**
5877 * css_from_id - lookup css by id
5878 * @id: the cgroup id
5879 * @ss: cgroup subsys to be looked into
5880 *
5881 * Returns the css if there's valid one with @id, otherwise returns NULL.
5882 * Should be called under rcu_read_lock().
5883 */
5884struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5885{
Tejun Heo6fa49182014-05-04 15:09:13 -04005886 WARN_ON_ONCE(!rcu_read_lock_held());
Johannes Weinerd6ccc552016-06-17 12:24:27 -04005887 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005888}
5889
Tejun Heo16af4392015-11-20 15:55:52 -05005890/**
5891 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5892 * @path: path on the default hierarchy
5893 *
5894 * Find the cgroup at @path on the default hierarchy, increment its
5895 * reference count and return it. Returns pointer to the found cgroup on
5896 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5897 * if @path points to a non-directory.
5898 */
5899struct cgroup *cgroup_get_from_path(const char *path)
5900{
5901 struct kernfs_node *kn;
5902 struct cgroup *cgrp;
5903
5904 mutex_lock(&cgroup_mutex);
5905
5906 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5907 if (kn) {
5908 if (kernfs_type(kn) == KERNFS_DIR) {
5909 cgrp = kn->priv;
Tejun Heoa590b902017-04-28 15:14:55 -04005910 cgroup_get_live(cgrp);
Tejun Heo16af4392015-11-20 15:55:52 -05005911 } else {
5912 cgrp = ERR_PTR(-ENOTDIR);
5913 }
5914 kernfs_put(kn);
5915 } else {
5916 cgrp = ERR_PTR(-ENOENT);
5917 }
5918
5919 mutex_unlock(&cgroup_mutex);
5920 return cgrp;
5921}
5922EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5923
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07005924/**
5925 * cgroup_get_from_fd - get a cgroup pointer from a fd
5926 * @fd: fd obtained by open(cgroup2_dir)
5927 *
5928 * Find the cgroup from a fd which should be obtained
5929 * by opening a cgroup directory. Returns a pointer to the
5930 * cgroup on success. ERR_PTR is returned if the cgroup
5931 * cannot be found.
5932 */
5933struct cgroup *cgroup_get_from_fd(int fd)
5934{
5935 struct cgroup_subsys_state *css;
5936 struct cgroup *cgrp;
5937 struct file *f;
5938
5939 f = fget_raw(fd);
5940 if (!f)
5941 return ERR_PTR(-EBADF);
5942
5943 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
5944 fput(f);
5945 if (IS_ERR(css))
5946 return ERR_CAST(css);
5947
5948 cgrp = css->cgroup;
5949 if (!cgroup_on_dfl(cgrp)) {
5950 cgroup_put(cgrp);
5951 return ERR_PTR(-EBADF);
5952 }
5953
5954 return cgrp;
5955}
5956EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
5957
Tejun Heobd1060a2015-12-07 17:38:53 -05005958/*
5959 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5960 * definition in cgroup-defs.h.
5961 */
5962#ifdef CONFIG_SOCK_CGROUP_DATA
5963
5964#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5965
Tejun Heo3fa4cc92015-12-14 11:24:06 -05005966DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05005967static bool cgroup_sk_alloc_disabled __read_mostly;
5968
5969void cgroup_sk_alloc_disable(void)
5970{
5971 if (cgroup_sk_alloc_disabled)
5972 return;
5973 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5974 cgroup_sk_alloc_disabled = true;
5975}
5976
5977#else
5978
5979#define cgroup_sk_alloc_disabled false
5980
5981#endif
5982
5983void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5984{
5985 if (cgroup_sk_alloc_disabled)
5986 return;
5987
Johannes Weinerd979a392016-09-19 14:44:38 -07005988 /* Socket clone path */
5989 if (skcd->val) {
Tejun Heoa590b902017-04-28 15:14:55 -04005990 /*
5991 * We might be cloning a socket which is left in an empty
5992 * cgroup and the cgroup might have already been rmdir'd.
5993 * Don't use cgroup_get_live().
5994 */
Johannes Weinerd979a392016-09-19 14:44:38 -07005995 cgroup_get(sock_cgroup_ptr(skcd));
5996 return;
5997 }
5998
Tejun Heobd1060a2015-12-07 17:38:53 -05005999 rcu_read_lock();
6000
6001 while (true) {
6002 struct css_set *cset;
6003
6004 cset = task_css_set(current);
6005 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
6006 skcd->val = (unsigned long)cset->dfl_cgrp;
6007 break;
6008 }
6009 cpu_relax();
6010 }
6011
6012 rcu_read_unlock();
6013}
6014
6015void cgroup_sk_free(struct sock_cgroup_data *skcd)
6016{
6017 cgroup_put(sock_cgroup_ptr(skcd));
6018}
6019
6020#endif /* CONFIG_SOCK_CGROUP_DATA */
6021
Daniel Mack30070982016-11-23 16:52:26 +01006022#ifdef CONFIG_CGROUP_BPF
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07006023int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
6024 enum bpf_attach_type type, u32 flags)
Daniel Mack30070982016-11-23 16:52:26 +01006025{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08006026 int ret;
Daniel Mack30070982016-11-23 16:52:26 +01006027
6028 mutex_lock(&cgroup_mutex);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07006029 ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
6030 mutex_unlock(&cgroup_mutex);
6031 return ret;
6032}
6033int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
6034 enum bpf_attach_type type, u32 flags)
6035{
6036 int ret;
6037
6038 mutex_lock(&cgroup_mutex);
6039 ret = __cgroup_bpf_detach(cgrp, prog, type, flags);
Daniel Mack30070982016-11-23 16:52:26 +01006040 mutex_unlock(&cgroup_mutex);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08006041 return ret;
Daniel Mack30070982016-11-23 16:52:26 +01006042}
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07006043int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
6044 union bpf_attr __user *uattr)
6045{
6046 int ret;
6047
6048 mutex_lock(&cgroup_mutex);
6049 ret = __cgroup_bpf_query(cgrp, attr, uattr);
6050 mutex_unlock(&cgroup_mutex);
6051 return ret;
6052}
Daniel Mack30070982016-11-23 16:52:26 +01006053#endif /* CONFIG_CGROUP_BPF */
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006054
6055#ifdef CONFIG_SYSFS
6056static ssize_t show_delegatable_files(struct cftype *files, char *buf,
6057 ssize_t size, const char *prefix)
6058{
6059 struct cftype *cft;
6060 ssize_t ret = 0;
6061
6062 for (cft = files; cft && cft->name[0] != '\0'; cft++) {
6063 if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
6064 continue;
6065
6066 if (prefix)
6067 ret += snprintf(buf + ret, size - ret, "%s.", prefix);
6068
6069 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
6070
Yangtao Li4d9ebbe2018-11-03 22:27:41 -04006071 if (WARN_ON(ret >= size))
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006072 break;
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006073 }
6074
6075 return ret;
6076}
6077
6078static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
6079 char *buf)
6080{
6081 struct cgroup_subsys *ss;
6082 int ssid;
6083 ssize_t ret = 0;
6084
6085 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret,
6086 NULL);
6087
6088 for_each_subsys(ss, ssid)
6089 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
6090 PAGE_SIZE - ret,
6091 cgroup_subsys_name[ssid]);
6092
6093 return ret;
6094}
6095static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
6096
Roman Gushchin5f2e6732017-11-06 13:30:29 -05006097static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
6098 char *buf)
6099{
6100 return snprintf(buf, PAGE_SIZE, "nsdelegate\n");
6101}
6102static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
6103
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006104static struct attribute *cgroup_sysfs_attrs[] = {
6105 &cgroup_delegate_attr.attr,
Roman Gushchin5f2e6732017-11-06 13:30:29 -05006106 &cgroup_features_attr.attr,
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006107 NULL,
6108};
6109
6110static const struct attribute_group cgroup_sysfs_attr_group = {
6111 .attrs = cgroup_sysfs_attrs,
6112 .name = "cgroup",
6113};
6114
6115static int __init cgroup_sysfs_init(void)
6116{
6117 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
6118}
6119subsys_initcall(cgroup_sysfs_init);
6120#endif /* CONFIG_SYSFS */