blob: 31af98996692718ef9a9272b4fdbf6e17377ba42 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Tejun Heo0a268db2016-12-27 14:49:06 -050031#include "cgroup-internal.h"
32
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/cred.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080037#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010044#include <linux/sched/task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040047#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070048#include <linux/string.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080049#include <linux/hashtable.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070050#include <linux/idr.h>
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020051#include <linux/kthread.h>
Arun Sharma600634972011-07-26 16:09:06 -070052#include <linux/atomic.h>
Tejun Heoe93ad192016-01-19 12:18:41 -050053#include <linux/cpuset.h>
Aditya Kalia79a9082016-01-29 02:54:06 -060054#include <linux/proc_ns.h>
55#include <linux/nsproxy.h>
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -070056#include <linux/file.h>
Tejun Heod4ff7492018-04-26 14:29:04 -070057#include <linux/sched/cputime.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050058#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070059
Tejun Heoed1777d2016-08-10 11:23:44 -040060#define CREATE_TRACE_POINTS
61#include <trace/events/cgroup.h>
62
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050063#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
64 MAX_CFTYPE_NAME + 2)
Tejun Heob12e3582018-04-26 14:29:04 -070065/* let's not notify more than 100 times per second */
66#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050067
Tejun Heob1a21362013-11-29 10:42:58 -050068/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069 * cgroup_mutex is the master lock. Any modification to cgroup or its
70 * hierarchy must be performed while holding it.
71 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040072 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050073 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080074 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050075 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
76 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080077 */
Tejun Heo22194492013-04-07 09:29:51 -070078DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040079DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0a268db2016-12-27 14:49:06 -050080
81#ifdef CONFIG_PROVE_RCU
Tejun Heo0e1d7682014-02-25 10:04:03 -050082EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040083EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070084#endif
85
Tejun Heo69e943b2014-02-08 10:36:58 -050086/*
Tejun Heo15a4c832014-05-04 15:09:14 -040087 * Protects cgroup_idr and css_idr so that IDs can be released without
88 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040089 */
90static DEFINE_SPINLOCK(cgroup_idr_lock);
91
92/*
Tejun Heo34c06252015-11-05 00:12:24 -050093 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
94 * against file removal/re-creation across css hiding.
95 */
96static DEFINE_SPINLOCK(cgroup_file_kn_lock);
97
Tejun Heo1ed13282015-09-16 12:53:17 -040098struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
99
Tejun Heo8353da12014-05-13 12:19:23 -0400100#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700101 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
102 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400103 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500104
Ben Blumaae8aab2010-03-10 15:22:07 -0800105/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500106 * cgroup destruction makes heavy use of work items and there can be a lot
107 * of concurrent destructions. Use a separate workqueue so that cgroup
108 * destruction work items don't end up filling up max_active of system_wq
109 * which may lead to deadlock.
110 */
111static struct workqueue_struct *cgroup_destroy_wq;
112
Tejun Heo3ed80a62014-02-08 10:36:58 -0500113/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500114#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo0a268db2016-12-27 14:49:06 -0500115struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116#include <linux/cgroup_subsys.h>
117};
Tejun Heo073219e2014-02-08 10:36:58 -0500118#undef SUBSYS
119
120/* array of cgroup subsystem names */
121#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
122static const char *cgroup_subsys_name[] = {
123#include <linux/cgroup_subsys.h>
124};
125#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700126
Tejun Heo49d1dc42015-09-18 11:56:28 -0400127/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
128#define SUBSYS(_x) \
129 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
130 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
131 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
132 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
133#include <linux/cgroup_subsys.h>
134#undef SUBSYS
135
136#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
137static struct static_key_true *cgroup_subsys_enabled_key[] = {
138#include <linux/cgroup_subsys.h>
139};
140#undef SUBSYS
141
142#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
143static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
147
Tejun Heoc58632b2018-04-26 14:29:04 -0700148static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
Tejun Heo041cd642017-09-25 08:12:05 -0700149
Paul Menageddbcc7e2007-10-18 23:39:30 -0700150/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400151 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700152 * unattached - it never has more than a single cgroup, and all tasks are
153 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154 */
Tejun Heoc58632b2018-04-26 14:29:04 -0700155struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
Tejun Heod0ec4232015-08-05 16:03:19 -0400156EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700157
Tejun Heoa2dd4242014-03-19 10:23:55 -0400158/*
159 * The default hierarchy always exists but is hidden until mounted for the
160 * first time. This is for backward compatibility.
161 */
Tejun Heoa7165262016-02-23 10:00:50 -0500162static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700163
Tejun Heo5533e012014-05-14 19:33:07 -0400164/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500165static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400166
Tejun Heof6d635ad2016-03-08 11:51:26 -0500167/* some controllers are implicitly enabled on the default hierarchy */
Tejun Heob8074212017-01-20 12:06:08 -0500168static u16 cgrp_dfl_implicit_ss_mask;
Tejun Heof6d635ad2016-03-08 11:51:26 -0500169
Tejun Heo8cfd8142017-07-21 11:14:51 -0400170/* some controllers can be threaded on the default hierarchy */
171static u16 cgrp_dfl_threaded_ss_mask;
172
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173/* The list of hierarchy roots */
Tejun Heo0a268db2016-12-27 14:49:06 -0500174LIST_HEAD(cgroup_roots);
Tejun Heo9871bf92013-06-24 15:21:47 -0700175static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176
Tejun Heo3417ae12014-02-08 10:37:01 -0500177/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700178static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700179
Li Zefan794611a2013-06-18 18:53:53 +0800180/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400181 * Assign a monotonically increasing serial number to csses. It guarantees
182 * cgroups with bigger numbers are newer than those with smaller numbers.
183 * Also, as csses are always appended to the parent's ->children list, it
184 * guarantees that sibling csses are always sorted in the ascending serial
185 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800186 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400187static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800188
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000189/*
Tejun Heob8074212017-01-20 12:06:08 -0500190 * These bitmasks identify subsystems with specific features to avoid
191 * having to do iterative checks repeatedly.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500193static u16 have_fork_callback __read_mostly;
194static u16 have_exit_callback __read_mostly;
195static u16 have_free_callback __read_mostly;
Tejun Heob8074212017-01-20 12:06:08 -0500196static u16 have_canfork_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700197
Aditya Kalia79a9082016-01-29 02:54:06 -0600198/* cgroup namespace for init task */
199struct cgroup_namespace init_cgroup_ns = {
Elena Reshetova387ad962017-02-20 12:19:00 +0200200 .count = REFCOUNT_INIT(2),
Aditya Kalia79a9082016-01-29 02:54:06 -0600201 .user_ns = &init_user_ns,
202 .ns.ops = &cgroupns_operations,
203 .ns.inum = PROC_CGROUP_INIT_INO,
204 .root_cset = &init_css_set,
205};
206
Tejun Heo67e9c742015-11-16 11:13:34 -0500207static struct file_system_type cgroup2_fs_type;
Tejun Heod62beb72016-12-27 14:49:08 -0500208static struct cftype cgroup_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700209
Tejun Heo334c3672016-03-03 09:58:01 -0500210static int cgroup_apply_control(struct cgroup *cgrp);
211static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400212static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800213static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500214static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
215 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400216static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400217static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400218static int cgroup_addrm_files(struct cgroup_subsys_state *css,
219 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400220 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400222/**
223 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
224 * @ssid: subsys ID of interest
225 *
226 * cgroup_subsys_enabled() can only be used with literal subsys names which
227 * is fine for individual subsystems but unsuitable for cgroup core. This
228 * is slower static_key_enabled() based test indexed by @ssid.
229 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500230bool cgroup_ssid_enabled(int ssid)
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400231{
Arnd Bergmanncfe02a82016-03-15 00:21:06 +0100232 if (CGROUP_SUBSYS_COUNT == 0)
233 return false;
234
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400235 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
236}
237
Tejun Heo9e10a132015-09-18 11:56:28 -0400238/**
239 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
240 * @cgrp: the cgroup of interest
241 *
242 * The default hierarchy is the v2 interface of cgroup and this function
243 * can be used to test whether a cgroup is on the default hierarchy for
244 * cases where a subsystem should behave differnetly depending on the
245 * interface version.
246 *
247 * The set of behaviors which change on the default hierarchy are still
248 * being determined and the mount option is prefixed with __DEVEL__.
249 *
250 * List of changed behaviors:
251 *
252 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
253 * and "name" are disallowed.
254 *
255 * - When mounting an existing superblock, mount options should match.
256 *
257 * - Remount is disallowed.
258 *
259 * - rename(2) is disallowed.
260 *
261 * - "tasks" is removed. Everything should be at process granularity. Use
262 * "cgroup.procs" instead.
263 *
264 * - "cgroup.procs" is not sorted. pids will be unique unless they got
265 * recycled inbetween reads.
266 *
267 * - "release_agent" and "notify_on_release" are removed. Replacement
268 * notification mechanism will be implemented.
269 *
270 * - "cgroup.clone_children" is removed.
271 *
272 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
273 * and its descendants contain no task; otherwise, 1. The file also
274 * generates kernfs notification which can be monitored through poll and
275 * [di]notify when the value of the file changes.
276 *
277 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
278 * take masks of ancestors with non-empty cpus/mems, instead of being
279 * moved to an ancestor.
280 *
281 * - cpuset: a task can be moved into an empty cpuset, and again it takes
282 * masks of ancestors.
283 *
284 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
285 * is not created.
286 *
287 * - blkcg: blk-throttle becomes properly hierarchical.
288 *
289 * - debug: disallowed on the default hierarchy.
290 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500291bool cgroup_on_dfl(const struct cgroup *cgrp)
Tejun Heo9e10a132015-09-18 11:56:28 -0400292{
293 return cgrp->root == &cgrp_dfl_root;
294}
295
Tejun Heo6fa49182014-05-04 15:09:13 -0400296/* IDR wrappers which synchronize using cgroup_idr_lock */
297static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
298 gfp_t gfp_mask)
299{
300 int ret;
301
302 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400303 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800304 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400305 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400306 idr_preload_end();
307 return ret;
308}
309
310static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
311{
312 void *ret;
313
Tejun Heo54504e92014-05-13 12:10:59 -0400314 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400315 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400316 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400317 return ret;
318}
319
320static void cgroup_idr_remove(struct idr *idr, int id)
321{
Tejun Heo54504e92014-05-13 12:10:59 -0400322 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400323 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400324 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400325}
326
Tejun Heo27f26752017-07-16 21:44:18 -0400327static bool cgroup_has_tasks(struct cgroup *cgrp)
Tejun Heod51f39b2014-05-16 13:22:48 -0400328{
Tejun Heo27f26752017-07-16 21:44:18 -0400329 return cgrp->nr_populated_csets;
330}
Tejun Heod51f39b2014-05-16 13:22:48 -0400331
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400332bool cgroup_is_threaded(struct cgroup *cgrp)
Tejun Heo454000a2017-05-15 09:34:02 -0400333{
334 return cgrp->dom_cgrp != cgrp;
335}
336
Tejun Heo8cfd8142017-07-21 11:14:51 -0400337/* can @cgrp host both domain and threaded children? */
338static bool cgroup_is_mixable(struct cgroup *cgrp)
339{
340 /*
341 * Root isn't under domain level resource control exempting it from
342 * the no-internal-process constraint, so it can serve as a thread
343 * root and a parent of resource domains at the same time.
344 */
345 return !cgroup_parent(cgrp);
346}
347
348/* can @cgrp become a thread root? should always be true for a thread root */
349static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
350{
351 /* mixables don't care */
352 if (cgroup_is_mixable(cgrp))
353 return true;
354
355 /* domain roots can't be nested under threaded */
356 if (cgroup_is_threaded(cgrp))
357 return false;
358
359 /* can only have either domain or threaded children */
360 if (cgrp->nr_populated_domain_children)
361 return false;
362
363 /* and no domain controllers can be enabled */
364 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
365 return false;
366
367 return true;
368}
369
370/* is @cgrp root of a threaded subtree? */
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400371bool cgroup_is_thread_root(struct cgroup *cgrp)
Tejun Heo8cfd8142017-07-21 11:14:51 -0400372{
373 /* thread root should be a domain */
374 if (cgroup_is_threaded(cgrp))
375 return false;
376
377 /* a domain w/ threaded children is a thread root */
378 if (cgrp->nr_threaded_children)
379 return true;
380
381 /*
382 * A domain which has tasks and explicit threaded controllers
383 * enabled is a thread root.
384 */
385 if (cgroup_has_tasks(cgrp) &&
386 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
387 return true;
388
389 return false;
390}
391
392/* a domain which isn't connected to the root w/o brekage can't be used */
393static bool cgroup_is_valid_domain(struct cgroup *cgrp)
394{
395 /* the cgroup itself can be a thread root */
396 if (cgroup_is_threaded(cgrp))
397 return false;
398
399 /* but the ancestors can't be unless mixable */
400 while ((cgrp = cgroup_parent(cgrp))) {
401 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
402 return false;
403 if (cgroup_is_threaded(cgrp))
404 return false;
405 }
406
407 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400408}
409
Tejun Heo5531dc92016-03-03 09:57:58 -0500410/* subsystems visibly enabled on a cgroup */
411static u16 cgroup_control(struct cgroup *cgrp)
412{
413 struct cgroup *parent = cgroup_parent(cgrp);
414 u16 root_ss_mask = cgrp->root->subsys_mask;
415
Tejun Heo8cfd8142017-07-21 11:14:51 -0400416 if (parent) {
417 u16 ss_mask = parent->subtree_control;
418
419 /* threaded cgroups can only have threaded controllers */
420 if (cgroup_is_threaded(cgrp))
421 ss_mask &= cgrp_dfl_threaded_ss_mask;
422 return ss_mask;
423 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500424
425 if (cgroup_on_dfl(cgrp))
Tejun Heof6d635ad2016-03-08 11:51:26 -0500426 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
427 cgrp_dfl_implicit_ss_mask);
Tejun Heo5531dc92016-03-03 09:57:58 -0500428 return root_ss_mask;
429}
430
431/* subsystems enabled on a cgroup */
432static u16 cgroup_ss_mask(struct cgroup *cgrp)
433{
434 struct cgroup *parent = cgroup_parent(cgrp);
435
Tejun Heo8cfd8142017-07-21 11:14:51 -0400436 if (parent) {
437 u16 ss_mask = parent->subtree_ss_mask;
438
439 /* threaded cgroups can only have threaded controllers */
440 if (cgroup_is_threaded(cgrp))
441 ss_mask &= cgrp_dfl_threaded_ss_mask;
442 return ss_mask;
443 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500444
445 return cgrp->root->subsys_mask;
446}
447
Tejun Heo95109b62013-08-08 20:11:27 -0400448/**
449 * cgroup_css - obtain a cgroup's css for the specified subsystem
450 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400451 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400452 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400453 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
454 * function must be called either under cgroup_mutex or rcu_read_lock() and
455 * the caller is responsible for pinning the returned css if it wants to
456 * keep accessing it outside the said locks. This function may return
457 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400458 */
459static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400460 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400461{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400462 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500463 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500464 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400465 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400466 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400467}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700468
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400469/**
Tejun Heod41bf8c2017-10-23 16:18:27 -0700470 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
471 * @cgrp: the cgroup of interest
472 * @ss: the subsystem of interest
473 *
474 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist
475 * or is offline, %NULL is returned.
476 */
477static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
478 struct cgroup_subsys *ss)
479{
480 struct cgroup_subsys_state *css;
481
482 rcu_read_lock();
483 css = cgroup_css(cgrp, ss);
484 if (!css || !css_tryget_online(css))
485 css = NULL;
486 rcu_read_unlock();
487
488 return css;
489}
490
491/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400492 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
493 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400494 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400495 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400496 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400497 * as the matching css of the nearest ancestor including self which has @ss
498 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
499 * function is guaranteed to return non-NULL css.
500 */
501static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
502 struct cgroup_subsys *ss)
503{
504 lockdep_assert_held(&cgroup_mutex);
505
506 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400507 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400508
Tejun Heoeeecbd12014-11-18 02:49:52 -0500509 /*
510 * This function is used while updating css associations and thus
Tejun Heo5531dc92016-03-03 09:57:58 -0500511 * can't test the csses directly. Test ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500512 */
Tejun Heo5531dc92016-03-03 09:57:58 -0500513 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
Tejun Heod51f39b2014-05-16 13:22:48 -0400514 cgrp = cgroup_parent(cgrp);
Tejun Heo5531dc92016-03-03 09:57:58 -0500515 if (!cgrp)
516 return NULL;
517 }
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400518
519 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700520}
521
Tejun Heoeeecbd12014-11-18 02:49:52 -0500522/**
523 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
524 * @cgrp: the cgroup of interest
525 * @ss: the subsystem of interest
526 *
527 * Find and get the effective css of @cgrp for @ss. The effective css is
528 * defined as the matching css of the nearest ancestor including self which
529 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
530 * the root css is returned, so this function always returns a valid css.
531 * The returned css must be put using css_put().
532 */
533struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
534 struct cgroup_subsys *ss)
535{
536 struct cgroup_subsys_state *css;
537
538 rcu_read_lock();
539
540 do {
541 css = cgroup_css(cgrp, ss);
542
543 if (css && css_tryget_online(css))
544 goto out_unlock;
545 cgrp = cgroup_parent(cgrp);
546 } while (cgrp);
547
548 css = init_css_set.subsys[ss->id];
549 css_get(css);
550out_unlock:
551 rcu_read_unlock();
552 return css;
553}
554
Tejun Heoa590b902017-04-28 15:14:55 -0400555static void cgroup_get_live(struct cgroup *cgrp)
Tejun Heo052c3f32015-10-15 16:41:50 -0400556{
557 WARN_ON_ONCE(cgroup_is_dead(cgrp));
558 css_get(&cgrp->self);
559}
560
Tejun Heob4168642014-05-13 12:16:21 -0400561struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500562{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500563 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400564 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500565
566 /*
567 * This is open and unprotected implementation of cgroup_css().
568 * seq_css() is only called from a kernfs file operation which has
569 * an active reference on the file. Because all the subsystem
570 * files are drained before a css is disassociated with a cgroup,
571 * the matching css from the cgroup's subsys table is guaranteed to
572 * be and stay valid until the enclosing operation is complete.
573 */
574 if (cft->ss)
575 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
576 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400577 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500578}
Tejun Heob4168642014-05-13 12:16:21 -0400579EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500580
Tejun Heo30159ec2013-06-25 11:53:37 -0700581/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500582 * for_each_css - iterate all css's of a cgroup
583 * @css: the iteration cursor
584 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
585 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700586 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400587 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700588 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500589#define for_each_css(css, ssid, cgrp) \
590 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
591 if (!((css) = rcu_dereference_check( \
592 (cgrp)->subsys[(ssid)], \
593 lockdep_is_held(&cgroup_mutex)))) { } \
594 else
595
596/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400597 * for_each_e_css - iterate all effective css's of a cgroup
598 * @css: the iteration cursor
599 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
600 * @cgrp: the target cgroup to iterate css's of
601 *
602 * Should be called under cgroup_[tree_]mutex.
603 */
604#define for_each_e_css(css, ssid, cgrp) \
605 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
606 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
607 ; \
608 else
609
610/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500611 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000612 * @ss: the iteration cursor
613 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500614 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000615 *
616 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500617 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000618 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500619#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
620 unsigned long __ss_mask = (ss_mask); \
621 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000622 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500623 break; \
624 } \
625 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
626 (ss) = cgroup_subsys[ssid]; \
627 {
628
629#define while_each_subsys_mask() \
630 } \
631 } \
632} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000633
Tejun Heof8f22e52014-04-23 11:13:16 -0400634/* iterate over child cgrps, lock should be held throughout iteration */
635#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400636 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400637 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400638 cgroup_is_dead(child); })) \
639 ; \
640 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700641
Tejun Heoce3f1d92016-03-03 09:57:59 -0500642/* walk live descendants in preorder */
643#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
644 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
645 if (({ lockdep_assert_held(&cgroup_mutex); \
646 (dsct) = (d_css)->cgroup; \
647 cgroup_is_dead(dsct); })) \
648 ; \
649 else
650
651/* walk live descendants in postorder */
652#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
653 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
654 if (({ lockdep_assert_held(&cgroup_mutex); \
655 (dsct) = (d_css)->cgroup; \
656 cgroup_is_dead(dsct); })) \
657 ; \
658 else
659
Tejun Heo172a2c062014-03-19 10:23:53 -0400660/*
661 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700662 * hierarchies being mounted. It contains a pointer to the root state
663 * for each subsystem. Also used to anchor the list of css_sets. Not
664 * reference-counted, to improve performance when child cgroups
665 * haven't been created.
666 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400667struct css_set init_css_set = {
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200668 .refcount = REFCOUNT_INIT(1),
Tejun Heo454000a2017-05-15 09:34:02 -0400669 .dom_cset = &init_css_set,
Tejun Heo172a2c062014-03-19 10:23:53 -0400670 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
671 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500672 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo454000a2017-05-15 09:34:02 -0400673 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500674 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
Tejun Heo172a2c062014-03-19 10:23:53 -0400675 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
676 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heo38683142017-09-25 13:50:20 -0700677
678 /*
679 * The following field is re-initialized when this cset gets linked
680 * in cgroup_init(). However, let's initialize the field
681 * statically too so that the default cgroup can be accessed safely
682 * early during boot.
683 */
684 .dfl_cgrp = &cgrp_dfl_root.cgrp,
Tejun Heo172a2c062014-03-19 10:23:53 -0400685};
Paul Menage817929e2007-10-18 23:39:36 -0700686
Tejun Heo172a2c062014-03-19 10:23:53 -0400687static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700688
Tejun Heo454000a2017-05-15 09:34:02 -0400689static bool css_set_threaded(struct css_set *cset)
690{
691 return cset->dom_cset != cset;
692}
693
Tejun Heo842b5972014-04-25 18:28:02 -0400694/**
Tejun Heo0de09422015-10-15 16:41:49 -0400695 * css_set_populated - does a css_set contain any tasks?
696 * @cset: target css_set
Waiman Long73a72422017-06-13 17:18:01 -0400697 *
698 * css_set_populated() should be the same as !!cset->nr_tasks at steady
699 * state. However, css_set_populated() can be called while a task is being
700 * added to or removed from the linked list before the nr_tasks is
701 * properly updated. Hence, we can't just look at ->nr_tasks here.
Tejun Heo0de09422015-10-15 16:41:49 -0400702 */
703static bool css_set_populated(struct css_set *cset)
704{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400705 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400706
707 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
708}
709
710/**
Tejun Heo788b9502017-07-16 21:43:33 -0400711 * cgroup_update_populated - update the populated count of a cgroup
Tejun Heo842b5972014-04-25 18:28:02 -0400712 * @cgrp: the target cgroup
713 * @populated: inc or dec populated count
714 *
Tejun Heo0de09422015-10-15 16:41:49 -0400715 * One of the css_sets associated with @cgrp is either getting its first
Tejun Heo788b9502017-07-16 21:43:33 -0400716 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
717 * count is propagated towards root so that a given cgroup's
718 * nr_populated_children is zero iff none of its descendants contain any
719 * tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400720 *
Tejun Heo788b9502017-07-16 21:43:33 -0400721 * @cgrp's interface file "cgroup.populated" is zero if both
722 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
723 * 1 otherwise. When the sum changes from or to zero, userland is notified
724 * that the content of the interface file has changed. This can be used to
725 * detect when @cgrp and its descendants become populated or empty.
Tejun Heo842b5972014-04-25 18:28:02 -0400726 */
727static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
728{
Tejun Heo788b9502017-07-16 21:43:33 -0400729 struct cgroup *child = NULL;
730 int adj = populated ? 1 : -1;
731
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400732 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400733
734 do {
Tejun Heo788b9502017-07-16 21:43:33 -0400735 bool was_populated = cgroup_is_populated(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400736
Tejun Heo454000a2017-05-15 09:34:02 -0400737 if (!child) {
Tejun Heo788b9502017-07-16 21:43:33 -0400738 cgrp->nr_populated_csets += adj;
Tejun Heo454000a2017-05-15 09:34:02 -0400739 } else {
740 if (cgroup_is_threaded(child))
741 cgrp->nr_populated_threaded_children += adj;
742 else
743 cgrp->nr_populated_domain_children += adj;
744 }
Tejun Heo842b5972014-04-25 18:28:02 -0400745
Tejun Heo788b9502017-07-16 21:43:33 -0400746 if (was_populated == cgroup_is_populated(cgrp))
Tejun Heo842b5972014-04-25 18:28:02 -0400747 break;
748
Tejun Heod62beb72016-12-27 14:49:08 -0500749 cgroup1_check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400750 cgroup_file_notify(&cgrp->events_file);
751
Tejun Heo788b9502017-07-16 21:43:33 -0400752 child = cgrp;
Tejun Heod51f39b2014-05-16 13:22:48 -0400753 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400754 } while (cgrp);
755}
756
Tejun Heo0de09422015-10-15 16:41:49 -0400757/**
758 * css_set_update_populated - update populated state of a css_set
759 * @cset: target css_set
760 * @populated: whether @cset is populated or depopulated
761 *
762 * @cset is either getting the first task or losing the last. Update the
Tejun Heo788b9502017-07-16 21:43:33 -0400763 * populated counters of all associated cgroups accordingly.
Tejun Heo0de09422015-10-15 16:41:49 -0400764 */
765static void css_set_update_populated(struct css_set *cset, bool populated)
766{
767 struct cgrp_cset_link *link;
768
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400769 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400770
771 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
772 cgroup_update_populated(link->cgrp, populated);
773}
774
Tejun Heof6d7d042015-10-15 16:41:52 -0400775/**
776 * css_set_move_task - move a task from one css_set to another
777 * @task: task being moved
778 * @from_cset: css_set @task currently belongs to (may be NULL)
779 * @to_cset: new css_set @task is being moved to (may be NULL)
780 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
781 *
782 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
783 * css_set, @from_cset can be NULL. If @task is being disassociated
784 * instead of moved, @to_cset can be NULL.
785 *
Tejun Heo788b9502017-07-16 21:43:33 -0400786 * This function automatically handles populated counter updates and
Tejun Heoed27b9f2015-10-15 16:41:52 -0400787 * css_task_iter adjustments but the caller is responsible for managing
788 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400789 */
790static void css_set_move_task(struct task_struct *task,
791 struct css_set *from_cset, struct css_set *to_cset,
792 bool use_mg_tasks)
793{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400794 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400795
Tejun Heo20b454a2016-03-03 09:57:57 -0500796 if (to_cset && !css_set_populated(to_cset))
797 css_set_update_populated(to_cset, true);
798
Tejun Heof6d7d042015-10-15 16:41:52 -0400799 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400800 struct css_task_iter *it, *pos;
801
Tejun Heof6d7d042015-10-15 16:41:52 -0400802 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400803
804 /*
805 * @task is leaving, advance task iterators which are
806 * pointing to it so that they can resume at the next
807 * position. Advancing an iterator might remove it from
808 * the list, use safe walk. See css_task_iter_advance*()
809 * for details.
810 */
811 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
812 iters_node)
813 if (it->task_pos == &task->cg_list)
814 css_task_iter_advance(it);
815
Tejun Heof6d7d042015-10-15 16:41:52 -0400816 list_del_init(&task->cg_list);
817 if (!css_set_populated(from_cset))
818 css_set_update_populated(from_cset, false);
819 } else {
820 WARN_ON_ONCE(!list_empty(&task->cg_list));
821 }
822
823 if (to_cset) {
824 /*
825 * We are synchronized through cgroup_threadgroup_rwsem
826 * against PF_EXITING setting such that we can't race
827 * against cgroup_exit() changing the css_set to
828 * init_css_set and dropping the old one.
829 */
830 WARN_ON_ONCE(task->flags & PF_EXITING);
831
Tejun Heof6d7d042015-10-15 16:41:52 -0400832 rcu_assign_pointer(task->cgroups, to_cset);
833 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
834 &to_cset->tasks);
835 }
836}
837
Paul Menage7717f7b2009-09-23 15:56:22 -0700838/*
839 * hash table for cgroup groups. This improves the performance to find
840 * an existing css_set. This hash doesn't (currently) take into
841 * account cgroups in empty hierarchies.
842 */
Li Zefan472b1052008-04-29 01:00:11 -0700843#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800844static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700845
Li Zefan0ac801f2013-01-10 11:49:27 +0800846static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700847{
Li Zefan0ac801f2013-01-10 11:49:27 +0800848 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700849 struct cgroup_subsys *ss;
850 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700851
Tejun Heo30159ec2013-06-25 11:53:37 -0700852 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800853 key += (unsigned long)css[i];
854 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700855
Li Zefan0ac801f2013-01-10 11:49:27 +0800856 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700857}
858
Tejun Heodcfe1492016-12-27 14:49:09 -0500859void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700860{
Tejun Heo69d02062013-06-12 21:04:50 -0700861 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400862 struct cgroup_subsys *ss;
863 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700864
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400865 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500866
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200867 if (!refcount_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700868 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700869
Tejun Heo454000a2017-05-15 09:34:02 -0400870 WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
871
Tejun Heo53254f92015-11-23 14:55:41 -0500872 /* This css_set is dead. unlink it and release cgroup and css refs */
873 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400874 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500875 css_put(cset->subsys[ssid]);
876 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700877 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700878 css_set_count--;
879
Tejun Heo69d02062013-06-12 21:04:50 -0700880 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700881 list_del(&link->cset_link);
882 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400883 if (cgroup_parent(link->cgrp))
884 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700885 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700886 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700887
Tejun Heo454000a2017-05-15 09:34:02 -0400888 if (css_set_threaded(cset)) {
889 list_del(&cset->threaded_csets_node);
890 put_css_set_locked(cset->dom_cset);
891 }
892
Tejun Heo5abb8852013-06-12 21:04:49 -0700893 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700894}
895
Tejun Heob326f9d2013-06-24 15:21:48 -0700896/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700897 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700898 * @cset: candidate css_set being tested
899 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700900 * @new_cgrp: cgroup that's being entered by the task
901 * @template: desired set of css pointers in css_set (pre-calculated)
902 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800903 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700904 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
905 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700906static bool compare_css_sets(struct css_set *cset,
907 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700908 struct cgroup *new_cgrp,
909 struct cgroup_subsys_state *template[])
910{
Tejun Heo454000a2017-05-15 09:34:02 -0400911 struct cgroup *new_dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700912 struct list_head *l1, *l2;
913
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400914 /*
915 * On the default hierarchy, there can be csets which are
916 * associated with the same set of cgroups but different csses.
917 * Let's first ensure that csses match.
918 */
919 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700920 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700921
Tejun Heo454000a2017-05-15 09:34:02 -0400922
923 /* @cset's domain should match the default cgroup's */
924 if (cgroup_on_dfl(new_cgrp))
925 new_dfl_cgrp = new_cgrp;
926 else
927 new_dfl_cgrp = old_cset->dfl_cgrp;
928
929 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
930 return false;
931
Paul Menage7717f7b2009-09-23 15:56:22 -0700932 /*
933 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400934 * different cgroups in hierarchies. As different cgroups may
935 * share the same effective css, this comparison is always
936 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700937 */
Tejun Heo69d02062013-06-12 21:04:50 -0700938 l1 = &cset->cgrp_links;
939 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700940 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700941 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700942 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700943
944 l1 = l1->next;
945 l2 = l2->next;
946 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700947 if (l1 == &cset->cgrp_links) {
948 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700949 break;
950 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700951 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700952 }
953 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700954 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
955 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
956 cgrp1 = link1->cgrp;
957 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700958 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700959 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700960
961 /*
962 * If this hierarchy is the hierarchy of the cgroup
963 * that's changing, then we need to check that this
964 * css_set points to the new cgroup; if it's any other
965 * hierarchy, then this css_set should point to the
966 * same cgroup as the old css_set.
967 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700968 if (cgrp1->root == new_cgrp->root) {
969 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700970 return false;
971 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700972 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700973 return false;
974 }
975 }
976 return true;
977}
978
Tejun Heob326f9d2013-06-24 15:21:48 -0700979/**
980 * find_existing_css_set - init css array and find the matching css_set
981 * @old_cset: the css_set that we're using before the cgroup transition
982 * @cgrp: the cgroup that we're moving into
983 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700984 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700985static struct css_set *find_existing_css_set(struct css_set *old_cset,
986 struct cgroup *cgrp,
987 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700988{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400989 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700990 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700991 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800992 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700993 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700994
Ben Blumaae8aab2010-03-10 15:22:07 -0800995 /*
996 * Build the set of subsystem state objects that we want to see in the
997 * new css_set. while subsystems can change globally, the entries here
998 * won't change, so no need for locking.
999 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001000 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -04001001 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001002 /*
1003 * @ss is in this hierarchy, so we want the
1004 * effective css from @cgrp.
1005 */
1006 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -07001007 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001008 /*
1009 * @ss is not in this hierarchy, so we don't want
1010 * to change the css.
1011 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001012 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -07001013 }
1014 }
1015
Li Zefan0ac801f2013-01-10 11:49:27 +08001016 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -07001017 hash_for_each_possible(css_set_table, cset, hlist, key) {
1018 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -07001019 continue;
1020
1021 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -07001022 return cset;
Li Zefan472b1052008-04-29 01:00:11 -07001023 }
Paul Menage817929e2007-10-18 23:39:36 -07001024
1025 /* No existing cgroup group matched */
1026 return NULL;
1027}
1028
Tejun Heo69d02062013-06-12 21:04:50 -07001029static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -07001030{
Tejun Heo69d02062013-06-12 21:04:50 -07001031 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001032
Tejun Heo69d02062013-06-12 21:04:50 -07001033 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1034 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07001035 kfree(link);
1036 }
1037}
1038
Tejun Heo69d02062013-06-12 21:04:50 -07001039/**
1040 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1041 * @count: the number of links to allocate
1042 * @tmp_links: list_head the allocated links are put on
1043 *
1044 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1045 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -07001046 */
Tejun Heo69d02062013-06-12 21:04:50 -07001047static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -07001048{
Tejun Heo69d02062013-06-12 21:04:50 -07001049 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -07001050 int i;
Tejun Heo69d02062013-06-12 21:04:50 -07001051
1052 INIT_LIST_HEAD(tmp_links);
1053
Li Zefan36553432008-07-29 22:33:19 -07001054 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -07001055 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -07001056 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -07001057 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001058 return -ENOMEM;
1059 }
Tejun Heo69d02062013-06-12 21:04:50 -07001060 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001061 }
1062 return 0;
1063}
1064
Li Zefanc12f65d2009-01-07 18:07:42 -08001065/**
1066 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -07001067 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -07001068 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -08001069 * @cgrp: the destination cgroup
1070 */
Tejun Heo69d02062013-06-12 21:04:50 -07001071static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1072 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -08001073{
Tejun Heo69d02062013-06-12 21:04:50 -07001074 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -08001075
Tejun Heo69d02062013-06-12 21:04:50 -07001076 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -04001077
1078 if (cgroup_on_dfl(cgrp))
1079 cset->dfl_cgrp = cgrp;
1080
Tejun Heo69d02062013-06-12 21:04:50 -07001081 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1082 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07001083 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -04001084
Paul Menage7717f7b2009-09-23 15:56:22 -07001085 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001086 * Always add links to the tail of the lists so that the lists are
1087 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001088 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001089 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001090 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001091
1092 if (cgroup_parent(cgrp))
Tejun Heoa590b902017-04-28 15:14:55 -04001093 cgroup_get_live(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001094}
1095
Tejun Heob326f9d2013-06-24 15:21:48 -07001096/**
1097 * find_css_set - return a new css_set with one cgroup updated
1098 * @old_cset: the baseline css_set
1099 * @cgrp: the cgroup to be updated
1100 *
1101 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1102 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001103 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001104static struct css_set *find_css_set(struct css_set *old_cset,
1105 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001106{
Tejun Heob326f9d2013-06-24 15:21:48 -07001107 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001108 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001109 struct list_head tmp_links;
1110 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001111 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001112 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001113 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001114
Tejun Heob326f9d2013-06-24 15:21:48 -07001115 lockdep_assert_held(&cgroup_mutex);
1116
Paul Menage817929e2007-10-18 23:39:36 -07001117 /* First see if we already have a cgroup group that matches
1118 * the desired set */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001119 spin_lock_irq(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001120 cset = find_existing_css_set(old_cset, cgrp, template);
1121 if (cset)
1122 get_css_set(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001123 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001124
Tejun Heo5abb8852013-06-12 21:04:49 -07001125 if (cset)
1126 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001127
Tejun Heof4f4be22013-06-12 21:04:51 -07001128 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001129 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001130 return NULL;
1131
Tejun Heo69d02062013-06-12 21:04:50 -07001132 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001133 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001134 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001135 return NULL;
1136 }
1137
Elena Reshetova4b9502e62017-03-08 10:00:40 +02001138 refcount_set(&cset->refcount, 1);
Tejun Heo454000a2017-05-15 09:34:02 -04001139 cset->dom_cset = cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07001140 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001141 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001142 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo454000a2017-05-15 09:34:02 -04001143 INIT_LIST_HEAD(&cset->threaded_csets);
Tejun Heo5abb8852013-06-12 21:04:49 -07001144 INIT_HLIST_NODE(&cset->hlist);
Tejun Heo5f617ebb2016-12-27 14:49:05 -05001145 INIT_LIST_HEAD(&cset->cgrp_links);
1146 INIT_LIST_HEAD(&cset->mg_preload_node);
1147 INIT_LIST_HEAD(&cset->mg_node);
Paul Menage817929e2007-10-18 23:39:36 -07001148
1149 /* Copy the set of subsystem state objects generated in
1150 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001151 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001152
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001153 spin_lock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001154 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001155 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001156 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001157
Paul Menage7717f7b2009-09-23 15:56:22 -07001158 if (c->root == cgrp->root)
1159 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001160 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001161 }
Paul Menage817929e2007-10-18 23:39:36 -07001162
Tejun Heo69d02062013-06-12 21:04:50 -07001163 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001164
Paul Menage817929e2007-10-18 23:39:36 -07001165 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001166
Tejun Heo2d8f2432014-04-23 11:13:15 -04001167 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001168 key = css_set_hash(cset->subsys);
1169 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001170
Tejun Heo53254f92015-11-23 14:55:41 -05001171 for_each_subsys(ss, ssid) {
1172 struct cgroup_subsys_state *css = cset->subsys[ssid];
1173
Tejun Heo2d8f2432014-04-23 11:13:15 -04001174 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001175 &css->cgroup->e_csets[ssid]);
1176 css_get(css);
1177 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001178
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001179 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001180
Tejun Heo454000a2017-05-15 09:34:02 -04001181 /*
1182 * If @cset should be threaded, look up the matching dom_cset and
1183 * link them up. We first fully initialize @cset then look for the
1184 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1185 * to stay empty until we return.
1186 */
1187 if (cgroup_is_threaded(cset->dfl_cgrp)) {
1188 struct css_set *dcset;
1189
1190 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1191 if (!dcset) {
1192 put_css_set(cset);
1193 return NULL;
1194 }
1195
1196 spin_lock_irq(&css_set_lock);
1197 cset->dom_cset = dcset;
1198 list_add_tail(&cset->threaded_csets_node,
1199 &dcset->threaded_csets);
1200 spin_unlock_irq(&css_set_lock);
1201 }
1202
Tejun Heo5abb8852013-06-12 21:04:49 -07001203 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001204}
1205
Tejun Heo0a268db2016-12-27 14:49:06 -05001206struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001207{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001208 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001209
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001210 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001211}
1212
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001213static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001214{
1215 int id;
1216
1217 lockdep_assert_held(&cgroup_mutex);
1218
Tejun Heo985ed672014-03-19 10:23:53 -04001219 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001220 if (id < 0)
1221 return id;
1222
1223 root->hierarchy_id = id;
1224 return 0;
1225}
1226
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001227static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001228{
1229 lockdep_assert_held(&cgroup_mutex);
1230
Johannes Weiner8c8a5502016-06-17 12:23:59 -04001231 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heof2e85d52014-02-11 11:52:49 -05001232}
1233
Tejun Heo1592c9b2016-12-27 14:49:08 -05001234void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001235{
1236 if (root) {
Tejun Heof2e85d52014-02-11 11:52:49 -05001237 idr_destroy(&root->cgroup_idr);
1238 kfree(root);
1239 }
1240}
1241
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001242static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001243{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001244 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001245 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001246
Tejun Heoed1777d2016-08-10 11:23:44 -04001247 trace_cgroup_destroy_root(root);
1248
Tejun Heo334c3672016-03-03 09:58:01 -05001249 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -05001250
Tejun Heo776f02f2014-02-12 09:29:50 -05001251 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001252 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001253
Tejun Heof2e85d52014-02-11 11:52:49 -05001254 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo334c3672016-03-03 09:58:01 -05001255 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -05001256
1257 /*
1258 * Release all the links from cset_links to this hierarchy's
1259 * root cgroup
1260 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001261 spin_lock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001262
1263 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1264 list_del(&link->cset_link);
1265 list_del(&link->cgrp_link);
1266 kfree(link);
1267 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001268
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001269 spin_unlock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001270
1271 if (!list_empty(&root->root_list)) {
1272 list_del(&root->root_list);
1273 cgroup_root_count--;
1274 }
1275
1276 cgroup_exit_root_id(root);
1277
1278 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001279
Tejun Heo2bd59d42014-02-11 11:52:49 -05001280 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001281 cgroup_free_root(root);
1282}
1283
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001284/*
1285 * look up cgroup associated with current task's cgroup namespace on the
1286 * specified hierarchy
1287 */
1288static struct cgroup *
1289current_cgns_cgroup_from_root(struct cgroup_root *root)
1290{
1291 struct cgroup *res = NULL;
1292 struct css_set *cset;
1293
1294 lockdep_assert_held(&css_set_lock);
1295
1296 rcu_read_lock();
1297
1298 cset = current->nsproxy->cgroup_ns->root_cset;
1299 if (cset == &init_css_set) {
1300 res = &root->cgrp;
1301 } else {
1302 struct cgrp_cset_link *link;
1303
1304 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1305 struct cgroup *c = link->cgrp;
1306
1307 if (c->root == root) {
1308 res = c;
1309 break;
1310 }
1311 }
1312 }
1313 rcu_read_unlock();
1314
1315 BUG_ON(!res);
1316 return res;
1317}
1318
Tejun Heoceb6a082014-02-25 10:04:02 -05001319/* look up cgroup associated with given css_set on the specified hierarchy */
1320static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001321 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001322{
Paul Menage7717f7b2009-09-23 15:56:22 -07001323 struct cgroup *res = NULL;
1324
Tejun Heo96d365e2014-02-13 06:58:40 -05001325 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001326 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001327
Tejun Heo5abb8852013-06-12 21:04:49 -07001328 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001329 res = &root->cgrp;
Tejun Heo13d82fb2017-08-02 15:39:38 -07001330 } else if (root == &cgrp_dfl_root) {
1331 res = cset->dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001332 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001333 struct cgrp_cset_link *link;
1334
1335 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001336 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001337
Paul Menage7717f7b2009-09-23 15:56:22 -07001338 if (c->root == root) {
1339 res = c;
1340 break;
1341 }
1342 }
1343 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001344
Paul Menage7717f7b2009-09-23 15:56:22 -07001345 BUG_ON(!res);
1346 return res;
1347}
1348
1349/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001350 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001351 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001352 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001353struct cgroup *task_cgroup_from_root(struct task_struct *task,
1354 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001355{
1356 /*
1357 * No need to lock the task - since we hold cgroup_mutex the
1358 * task can't change groups, so the only thing that can happen
1359 * is that it exits and its css is set back to init_css_set.
1360 */
1361 return cset_cgroup_from_root(task_css_set(task), root);
1362}
1363
1364/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 * A task must hold cgroup_mutex to modify cgroups.
1366 *
1367 * Any task can increment and decrement the count field without lock.
1368 * So in general, code holding cgroup_mutex can't rely on the count
1369 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001370 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371 * means that no tasks are currently attached, therefore there is no
1372 * way a task attached to that cgroup can fork (the other way to
1373 * increment the count). So code holding cgroup_mutex can safely
1374 * assume that if the count is zero, it will stay zero. Similarly, if
1375 * a task holds cgroup_mutex on a cgroup with zero count, it
1376 * knows that the cgroup won't be removed, as cgroup_rmdir()
1377 * needs that mutex.
1378 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379 * A cgroup can only be deleted if both its 'count' of using tasks
1380 * is zero, and its list of 'children' cgroups is empty. Since all
1381 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001382 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001384 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385 *
1386 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001387 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388 */
1389
Tejun Heo2bd59d42014-02-11 11:52:49 -05001390static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Paul Menagea4243162007-10-18 23:39:35 -07001391
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001392static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1393 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001395 struct cgroup_subsys *ss = cft->ss;
1396
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001397 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1398 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1399 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001400 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1401 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001402 else
Tejun Heo08a77672018-01-09 07:21:15 -08001403 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001404 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001405}
1406
Tejun Heof2e85d52014-02-11 11:52:49 -05001407/**
1408 * cgroup_file_mode - deduce file mode of a control file
1409 * @cft: the control file in question
1410 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001411 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001412 */
1413static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001414{
Tejun Heof2e85d52014-02-11 11:52:49 -05001415 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001416
Tejun Heof2e85d52014-02-11 11:52:49 -05001417 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1418 mode |= S_IRUGO;
1419
Tejun Heo7dbdb192015-09-18 17:54:23 -04001420 if (cft->write_u64 || cft->write_s64 || cft->write) {
1421 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1422 mode |= S_IWUGO;
1423 else
1424 mode |= S_IWUSR;
1425 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001426
1427 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001428}
1429
Tejun Heoa9746d82014-05-13 12:19:22 -04001430/**
Tejun Heo8699b772016-02-22 22:25:46 -05001431 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001432 * @subtree_control: the new subtree_control mask to consider
Tejun Heo5ced2512016-03-03 09:58:01 -05001433 * @this_ss_mask: available subsystems
Tejun Heoaf0ba672014-07-08 18:02:57 -04001434 *
1435 * On the default hierarchy, a subsystem may request other subsystems to be
1436 * enabled together through its ->depends_on mask. In such cases, more
1437 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1438 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001439 * This function calculates which subsystems need to be enabled if
Tejun Heo5ced2512016-03-03 09:58:01 -05001440 * @subtree_control is to be applied while restricted to @this_ss_mask.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001441 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001442static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
Tejun Heo667c2492014-07-08 18:02:56 -04001443{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001444 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001445 struct cgroup_subsys *ss;
1446 int ssid;
1447
1448 lockdep_assert_held(&cgroup_mutex);
1449
Tejun Heof6d635ad2016-03-08 11:51:26 -05001450 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1451
Tejun Heoaf0ba672014-07-08 18:02:57 -04001452 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001453 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001454
Tejun Heob4e0eea2016-02-22 22:25:46 -05001455 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001456 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001457 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001458
1459 /*
1460 * Mask out subsystems which aren't available. This can
1461 * happen only if some depended-upon subsystems were bound
1462 * to non-default hierarchies.
1463 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001464 new_ss_mask &= this_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001465
1466 if (new_ss_mask == cur_ss_mask)
1467 break;
1468 cur_ss_mask = new_ss_mask;
1469 }
1470
Tejun Heo0f060de2014-11-18 02:49:50 -05001471 return cur_ss_mask;
1472}
1473
1474/**
Tejun Heoa9746d82014-05-13 12:19:22 -04001475 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1476 * @kn: the kernfs_node being serviced
1477 *
1478 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1479 * the method finishes if locking succeeded. Note that once this function
1480 * returns the cgroup returned by cgroup_kn_lock_live() may become
1481 * inaccessible any time. If the caller intends to continue to access the
1482 * cgroup, it should pin it before invoking this function.
1483 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001484void cgroup_kn_unlock(struct kernfs_node *kn)
Tejun Heoa9746d82014-05-13 12:19:22 -04001485{
1486 struct cgroup *cgrp;
1487
1488 if (kernfs_type(kn) == KERNFS_DIR)
1489 cgrp = kn->priv;
1490 else
1491 cgrp = kn->parent->priv;
1492
1493 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001494
1495 kernfs_unbreak_active_protection(kn);
1496 cgroup_put(cgrp);
1497}
1498
1499/**
1500 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1501 * @kn: the kernfs_node being serviced
Tejun Heo945ba192016-03-03 09:58:00 -05001502 * @drain_offline: perform offline draining on the cgroup
Tejun Heoa9746d82014-05-13 12:19:22 -04001503 *
1504 * This helper is to be used by a cgroup kernfs method currently servicing
1505 * @kn. It breaks the active protection, performs cgroup locking and
1506 * verifies that the associated cgroup is alive. Returns the cgroup if
1507 * alive; otherwise, %NULL. A successful return should be undone by a
Tejun Heo945ba192016-03-03 09:58:00 -05001508 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1509 * cgroup is drained of offlining csses before return.
Tejun Heoa9746d82014-05-13 12:19:22 -04001510 *
1511 * Any cgroup kernfs method implementation which requires locking the
1512 * associated cgroup should use this helper. It avoids nesting cgroup
1513 * locking under kernfs active protection and allows all kernfs operations
1514 * including self-removal.
1515 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001516struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
Tejun Heoa9746d82014-05-13 12:19:22 -04001517{
1518 struct cgroup *cgrp;
1519
1520 if (kernfs_type(kn) == KERNFS_DIR)
1521 cgrp = kn->priv;
1522 else
1523 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
Tejun Heo2bd59d42014-02-11 11:52:49 -05001525 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001526 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001527 * active_ref. cgroup liveliness check alone provides enough
1528 * protection against removal. Ensure @cgrp stays accessible and
1529 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001530 */
Li Zefanaa323622014-09-04 14:43:38 +08001531 if (!cgroup_tryget(cgrp))
1532 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001533 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534
Tejun Heo945ba192016-03-03 09:58:00 -05001535 if (drain_offline)
1536 cgroup_lock_and_drain_offline(cgrp);
1537 else
1538 mutex_lock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001539
1540 if (!cgroup_is_dead(cgrp))
1541 return cgrp;
1542
1543 cgroup_kn_unlock(kn);
1544 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545}
1546
Li Zefan2739d3c2013-01-21 18:18:33 +08001547static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001549 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550
Tejun Heo01f64742014-05-13 12:19:23 -04001551 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001552
1553 if (cft->file_offset) {
1554 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1555 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1556
1557 spin_lock_irq(&cgroup_file_kn_lock);
1558 cfile->kn = NULL;
1559 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heob12e3582018-04-26 14:29:04 -07001560
1561 del_timer_sync(&cfile->notify_timer);
Tejun Heo34c06252015-11-05 00:12:24 -05001562 }
1563
Tejun Heo2bd59d42014-02-11 11:52:49 -05001564 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001565}
1566
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001567/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001568 * css_clear_dir - remove subsys files in a cgroup directory
1569 * @css: taget css
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001570 */
Tejun Heo334c3672016-03-03 09:58:01 -05001571static void css_clear_dir(struct cgroup_subsys_state *css)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001572{
Tejun Heo334c3672016-03-03 09:58:01 -05001573 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001574 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001575
Tejun Heo88cb04b2016-03-03 09:57:58 -05001576 if (!(css->flags & CSS_VISIBLE))
1577 return;
1578
1579 css->flags &= ~CSS_VISIBLE;
1580
Tejun Heo5faaf052018-04-26 14:29:04 -07001581 if (!css->ss) {
1582 if (cgroup_on_dfl(cgrp))
1583 cfts = cgroup_base_files;
1584 else
1585 cfts = cgroup1_base_files;
1586
Tejun Heo4df8dc92015-09-18 17:54:23 -04001587 cgroup_addrm_files(css, cgrp, cfts, false);
Tejun Heo5faaf052018-04-26 14:29:04 -07001588 } else {
1589 list_for_each_entry(cfts, &css->ss->cfts, node)
1590 cgroup_addrm_files(css, cgrp, cfts, false);
1591 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592}
1593
Tejun Heoccdca212015-09-18 17:54:23 -04001594/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001595 * css_populate_dir - create subsys files in a cgroup directory
1596 * @css: target css
Tejun Heoccdca212015-09-18 17:54:23 -04001597 *
1598 * On failure, no file is added.
1599 */
Tejun Heo334c3672016-03-03 09:58:01 -05001600static int css_populate_dir(struct cgroup_subsys_state *css)
Tejun Heoccdca212015-09-18 17:54:23 -04001601{
Tejun Heo334c3672016-03-03 09:58:01 -05001602 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001603 struct cftype *cfts, *failed_cfts;
1604 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001605
Tejun Heo03970d32016-03-03 09:58:00 -05001606 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
Tejun Heo88cb04b2016-03-03 09:57:58 -05001607 return 0;
1608
Tejun Heo4df8dc92015-09-18 17:54:23 -04001609 if (!css->ss) {
1610 if (cgroup_on_dfl(cgrp))
Tejun Heod62beb72016-12-27 14:49:08 -05001611 cfts = cgroup_base_files;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001612 else
Tejun Heod62beb72016-12-27 14:49:08 -05001613 cfts = cgroup1_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001614
Tejun Heo5faaf052018-04-26 14:29:04 -07001615 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1616 if (ret < 0)
1617 return ret;
1618 } else {
1619 list_for_each_entry(cfts, &css->ss->cfts, node) {
1620 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1621 if (ret < 0) {
1622 failed_cfts = cfts;
1623 goto err;
1624 }
Tejun Heoccdca212015-09-18 17:54:23 -04001625 }
1626 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001627
1628 css->flags |= CSS_VISIBLE;
1629
Tejun Heoccdca212015-09-18 17:54:23 -04001630 return 0;
1631err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001632 list_for_each_entry(cfts, &css->ss->cfts, node) {
1633 if (cfts == failed_cfts)
1634 break;
1635 cgroup_addrm_files(css, cgrp, cfts, false);
1636 }
Tejun Heoccdca212015-09-18 17:54:23 -04001637 return ret;
1638}
1639
Tejun Heo0a268db2016-12-27 14:49:06 -05001640int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641{
Tejun Heo1ada4832015-09-18 17:54:23 -04001642 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001643 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001644 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645
Tejun Heoace2bee2014-02-11 11:52:47 -05001646 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001647
Tejun Heob4e0eea2016-02-22 22:25:46 -05001648 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heof6d635ad2016-03-08 11:51:26 -05001649 /*
1650 * If @ss has non-root csses attached to it, can't move.
1651 * If @ss is an implicit controller, it is exempt from this
1652 * rule and can be stolen.
1653 */
1654 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1655 !ss->implicit_on_dfl)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001656 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657
Tejun Heo5df36032014-03-19 10:23:54 -04001658 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001659 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001660 return -EBUSY;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001661 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
Tejun Heob4e0eea2016-02-22 22:25:46 -05001663 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001664 struct cgroup_root *src_root = ss->root;
1665 struct cgroup *scgrp = &src_root->cgrp;
1666 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001667 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001668
Tejun Heo1ada4832015-09-18 17:54:23 -04001669 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001670
Tejun Heo334c3672016-03-03 09:58:01 -05001671 /* disable from the source */
1672 src_root->subsys_mask &= ~(1 << ssid);
1673 WARN_ON(cgroup_apply_control(scgrp));
1674 cgroup_finalize_control(scgrp, 0);
Tejun Heo4df8dc92015-09-18 17:54:23 -04001675
Tejun Heo334c3672016-03-03 09:58:01 -05001676 /* rebind */
Tejun Heo1ada4832015-09-18 17:54:23 -04001677 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1678 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001679 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001680 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001681
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001682 spin_lock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001683 hash_for_each(css_set_table, i, cset, hlist)
1684 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001685 &dcgrp->e_csets[ss->id]);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001686 spin_unlock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001687
Tejun Heobd53d612014-04-23 11:13:16 -04001688 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001689 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001690 if (dst_root == &cgrp_dfl_root) {
1691 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1692 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001693 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001694 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001695 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001696
Tejun Heo334c3672016-03-03 09:58:01 -05001697 ret = cgroup_apply_control(dcgrp);
1698 if (ret)
1699 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1700 ss->name, ret);
1701
Tejun Heo5df36032014-03-19 10:23:54 -04001702 if (ss->bind)
1703 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001704 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705
Tejun Heo1ada4832015-09-18 17:54:23 -04001706 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707 return 0;
1708}
1709
Tejun Heo1592c9b2016-12-27 14:49:08 -05001710int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1711 struct kernfs_root *kf_root)
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001712{
Felipe Balbi09be4c82016-05-12 12:34:38 +03001713 int len = 0;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001714 char *buf = NULL;
1715 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1716 struct cgroup *ns_cgroup;
1717
1718 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1719 if (!buf)
1720 return -ENOMEM;
1721
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001722 spin_lock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001723 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1724 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001725 spin_unlock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001726
1727 if (len >= PATH_MAX)
1728 len = -ERANGE;
1729 else if (len > 0) {
1730 seq_escape(sf, buf, " \t\n\\");
1731 len = 0;
1732 }
1733 kfree(buf);
1734 return len;
1735}
1736
Tejun Heo5136f632017-06-27 14:30:28 -04001737static int parse_cgroup_root_flags(char *data, unsigned int *root_flags)
1738{
1739 char *token;
1740
1741 *root_flags = 0;
1742
1743 if (!data)
1744 return 0;
1745
1746 while ((token = strsep(&data, ",")) != NULL) {
1747 if (!strcmp(token, "nsdelegate")) {
1748 *root_flags |= CGRP_ROOT_NS_DELEGATE;
1749 continue;
1750 }
1751
1752 pr_err("cgroup2: unknown option \"%s\"\n", token);
1753 return -EINVAL;
1754 }
1755
1756 return 0;
1757}
1758
1759static void apply_cgroup_root_flags(unsigned int root_flags)
1760{
1761 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
1762 if (root_flags & CGRP_ROOT_NS_DELEGATE)
1763 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
1764 else
1765 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
1766 }
1767}
1768
1769static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
1770{
1771 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
1772 seq_puts(seq, ",nsdelegate");
1773 return 0;
1774}
1775
Tejun Heo2bd59d42014-02-11 11:52:49 -05001776static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777{
Tejun Heo5136f632017-06-27 14:30:28 -04001778 unsigned int root_flags;
1779 int ret;
1780
1781 ret = parse_cgroup_root_flags(data, &root_flags);
1782 if (ret)
1783 return ret;
1784
1785 apply_cgroup_root_flags(root_flags);
1786 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787}
1788
Tejun Heoafeb0f92014-02-13 06:58:39 -05001789/*
1790 * To reduce the fork() overhead for systems that are not actually using
1791 * their cgroups capability, we don't maintain the lists running through
1792 * each css_set to its tasks until we see the list actually used - in other
1793 * words after the first mount.
1794 */
1795static bool use_task_css_set_links __read_mostly;
1796
1797static void cgroup_enable_task_cg_lists(void)
1798{
1799 struct task_struct *p, *g;
1800
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001801 spin_lock_irq(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001802
1803 if (use_task_css_set_links)
1804 goto out_unlock;
1805
1806 use_task_css_set_links = true;
1807
1808 /*
1809 * We need tasklist_lock because RCU is not safe against
1810 * while_each_thread(). Besides, a forking task that has passed
1811 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1812 * is not guaranteed to have its child immediately visible in the
1813 * tasklist if we walk through it with RCU.
1814 */
1815 read_lock(&tasklist_lock);
1816 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001817 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1818 task_css_set(p) != &init_css_set);
1819
1820 /*
1821 * We should check if the process is exiting, otherwise
1822 * it will race with cgroup_exit() in that the list
1823 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001824 * Do it while holding siglock so that we don't end up
1825 * racing against cgroup_exit().
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001826 *
1827 * Interrupts were already disabled while acquiring
1828 * the css_set_lock, so we do not need to disable it
1829 * again when acquiring the sighand->siglock here.
Tejun Heoafeb0f92014-02-13 06:58:39 -05001830 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001831 spin_lock(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001832 if (!(p->flags & PF_EXITING)) {
1833 struct css_set *cset = task_css_set(p);
1834
Tejun Heo0de09422015-10-15 16:41:49 -04001835 if (!css_set_populated(cset))
1836 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001837 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001838 get_css_set(cset);
Waiman Long73a72422017-06-13 17:18:01 -04001839 cset->nr_tasks++;
Tejun Heoeaf797a2014-02-25 10:04:03 -05001840 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001841 spin_unlock(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001842 } while_each_thread(g, p);
1843 read_unlock(&tasklist_lock);
1844out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001845 spin_unlock_irq(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001846}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001847
Paul Menagecc31edc2008-10-18 20:28:04 -07001848static void init_cgroup_housekeeping(struct cgroup *cgrp)
1849{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001850 struct cgroup_subsys *ss;
1851 int ssid;
1852
Tejun Heod5c419b2014-05-16 13:22:48 -04001853 INIT_LIST_HEAD(&cgrp->self.sibling);
1854 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001855 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001856 INIT_LIST_HEAD(&cgrp->pidlists);
1857 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001858 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001859 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo454000a2017-05-15 09:34:02 -04001860 cgrp->dom_cgrp = cgrp;
Roman Gushchin1a926e02017-07-28 18:28:44 +01001861 cgrp->max_descendants = INT_MAX;
1862 cgrp->max_depth = INT_MAX;
Tejun Heod4ff7492018-04-26 14:29:04 -07001863 prev_cputime_init(&cgrp->prev_cputime);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001864
1865 for_each_subsys(ss, ssid)
1866 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001867
1868 init_waitqueue_head(&cgrp->offline_waitq);
Tejun Heod62beb72016-12-27 14:49:08 -05001869 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001870}
Paul Menagec6d57f32009-09-23 15:56:19 -07001871
Tejun Heo1592c9b2016-12-27 14:49:08 -05001872void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001873{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001874 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001875
Paul Menageddbcc7e2007-10-18 23:39:30 -07001876 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001877 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001878 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001879 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001880 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001881
Paul Menagec6d57f32009-09-23 15:56:19 -07001882 root->flags = opts->flags;
1883 if (opts->release_agent)
Tejun Heo08a77672018-01-09 07:21:15 -08001884 strscpy(root->release_agent_path, opts->release_agent, PATH_MAX);
Paul Menagec6d57f32009-09-23 15:56:19 -07001885 if (opts->name)
Tejun Heo08a77672018-01-09 07:21:15 -08001886 strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001887 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001888 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001889}
1890
Zefan Li9732adc2017-04-19 10:15:59 +08001891int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001892{
Tejun Heod427dfe2014-02-11 11:52:48 -05001893 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001894 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heofa069902016-12-27 14:49:07 -05001895 struct kernfs_syscall_ops *kf_sops;
Tejun Heod427dfe2014-02-11 11:52:48 -05001896 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001897 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001898
Tejun Heod427dfe2014-02-11 11:52:48 -05001899 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001900
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001901 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001902 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001903 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001904 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001905 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001906
Zefan Li9732adc2017-04-19 10:15:59 +08001907 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
1908 ref_flags, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001909 if (ret)
1910 goto out;
1911
Tejun Heod427dfe2014-02-11 11:52:48 -05001912 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001913 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001914 * but that's OK - it can only be increased by someone holding
Tejun Heo04313592016-03-03 09:58:01 -05001915 * cgroup_lock, and that's us. Later rebinding may disable
1916 * controllers on the default hierarchy and thus create new csets,
1917 * which can't be more than the existing ones. Allocate 2x.
Tejun Heod427dfe2014-02-11 11:52:48 -05001918 */
Tejun Heo04313592016-03-03 09:58:01 -05001919 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001920 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001921 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001922
Tejun Heo985ed672014-03-19 10:23:53 -04001923 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001924 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001925 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001926
Tejun Heofa069902016-12-27 14:49:07 -05001927 kf_sops = root == &cgrp_dfl_root ?
1928 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1929
1930 root->kf_root = kernfs_create_root(kf_sops,
Shaohua Liaa818822017-07-12 11:49:51 -07001931 KERNFS_ROOT_CREATE_DEACTIVATED |
1932 KERNFS_ROOT_SUPPORT_EXPORTOP,
Tejun Heo2bd59d42014-02-11 11:52:49 -05001933 root_cgrp);
1934 if (IS_ERR(root->kf_root)) {
1935 ret = PTR_ERR(root->kf_root);
1936 goto exit_root_id;
1937 }
1938 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001939
Tejun Heo334c3672016-03-03 09:58:01 -05001940 ret = css_populate_dir(&root_cgrp->self);
Tejun Heod427dfe2014-02-11 11:52:48 -05001941 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001942 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001943
Tejun Heo5df36032014-03-19 10:23:54 -04001944 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001945 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001946 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001947
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001948 ret = cgroup_bpf_inherit(root_cgrp);
1949 WARN_ON_ONCE(ret);
1950
Tejun Heoed1777d2016-08-10 11:23:44 -04001951 trace_cgroup_setup_root(root);
1952
Tejun Heod427dfe2014-02-11 11:52:48 -05001953 /*
1954 * There must be no failure case after here, since rebinding takes
1955 * care of subsystems' refcounts, which are explicitly dropped in
1956 * the failure exit path.
1957 */
1958 list_add(&root->root_list, &cgroup_roots);
1959 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001960
Tejun Heod427dfe2014-02-11 11:52:48 -05001961 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001962 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001963 * objects.
1964 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001965 spin_lock_irq(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001966 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001967 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001968 if (css_set_populated(cset))
1969 cgroup_update_populated(root_cgrp, true);
1970 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001971 spin_unlock_irq(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001972
Tejun Heod5c419b2014-05-16 13:22:48 -04001973 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001974 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001975
Tejun Heo2bd59d42014-02-11 11:52:49 -05001976 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001977 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001978 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001979
Tejun Heo2bd59d42014-02-11 11:52:49 -05001980destroy_root:
1981 kernfs_destroy_root(root->kf_root);
1982 root->kf_root = NULL;
1983exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001984 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001985cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001986 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001987out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001988 free_cgrp_cset_links(&tmp_links);
1989 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001990}
1991
Tejun Heo1592c9b2016-12-27 14:49:08 -05001992struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
1993 struct cgroup_root *root, unsigned long magic,
1994 struct cgroup_namespace *ns)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001995{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001996 struct dentry *dentry;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001997 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001998
Tejun Heo633feee32016-12-27 14:49:07 -05001999 dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
Serge Hallyned825712016-01-29 02:54:09 -06002000
Paul Menagec6d57f32009-09-23 15:56:19 -07002001 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002002 * In non-init cgroup namespace, instead of root cgroup's dentry,
2003 * we return the dentry corresponding to the cgroupns->root_cgrp.
Serge Hallyned825712016-01-29 02:54:09 -06002004 */
2005 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
2006 struct dentry *nsdentry;
2007 struct cgroup *cgrp;
2008
2009 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002010 spin_lock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002011
2012 cgrp = cset_cgroup_from_root(ns->root_cset, root);
2013
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002014 spin_unlock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002015 mutex_unlock(&cgroup_mutex);
2016
2017 nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
2018 dput(dentry);
2019 dentry = nsdentry;
2020 }
2021
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002022 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002023 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002024
Tejun Heo633feee32016-12-27 14:49:07 -05002025 return dentry;
2026}
2027
Tejun Heo633feee32016-12-27 14:49:07 -05002028static struct dentry *cgroup_mount(struct file_system_type *fs_type,
2029 int flags, const char *unused_dev_name,
2030 void *data)
2031{
2032 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
2033 struct dentry *dentry;
Tejun Heo5136f632017-06-27 14:30:28 -04002034 int ret;
Tejun Heo633feee32016-12-27 14:49:07 -05002035
2036 get_cgroup_ns(ns);
2037
2038 /* Check if the caller has permission to mount. */
2039 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
2040 put_cgroup_ns(ns);
2041 return ERR_PTR(-EPERM);
2042 }
2043
Li Zefan3a32bd72014-06-30 11:50:59 +08002044 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002045 * The first time anyone tries to mount a cgroup, enable the list
2046 * linking each css_set to its tasks and fix up all existing tasks.
Li Zefan3a32bd72014-06-30 11:50:59 +08002047 */
Tejun Heo633feee32016-12-27 14:49:07 -05002048 if (!use_task_css_set_links)
2049 cgroup_enable_task_cg_lists();
2050
2051 if (fs_type == &cgroup2_fs_type) {
Tejun Heo5136f632017-06-27 14:30:28 -04002052 unsigned int root_flags;
2053
2054 ret = parse_cgroup_root_flags(data, &root_flags);
2055 if (ret) {
Tejun Heo633feee32016-12-27 14:49:07 -05002056 put_cgroup_ns(ns);
Tejun Heo5136f632017-06-27 14:30:28 -04002057 return ERR_PTR(ret);
Tejun Heo633feee32016-12-27 14:49:07 -05002058 }
Tejun Heo5136f632017-06-27 14:30:28 -04002059
Tejun Heo633feee32016-12-27 14:49:07 -05002060 cgrp_dfl_visible = true;
Tejun Heoa590b902017-04-28 15:14:55 -04002061 cgroup_get_live(&cgrp_dfl_root.cgrp);
Tejun Heo633feee32016-12-27 14:49:07 -05002062
2063 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
2064 CGROUP2_SUPER_MAGIC, ns);
Tejun Heo5136f632017-06-27 14:30:28 -04002065 if (!IS_ERR(dentry))
2066 apply_cgroup_root_flags(root_flags);
Tejun Heo633feee32016-12-27 14:49:07 -05002067 } else {
2068 dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
2069 CGROUP_SUPER_MAGIC, ns);
Li Zefan3a32bd72014-06-30 11:50:59 +08002070 }
2071
Serge Hallyned825712016-01-29 02:54:09 -06002072 put_cgroup_ns(ns);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002073 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002074}
2075
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002076static void cgroup_kill_sb(struct super_block *sb)
2077{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002078 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002079 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002080
Tejun Heo9d755d32014-05-14 09:15:02 -04002081 /*
2082 * If @root doesn't have any mounts or children, start killing it.
2083 * This prevents new mounts by disabling percpu_ref_tryget_live().
2084 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002085 *
2086 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002087 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002088 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002089 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002090 cgroup_put(&root->cgrp);
2091 else
2092 percpu_ref_kill(&root->cgrp.self.refcnt);
2093
Tejun Heo2bd59d42014-02-11 11:52:49 -05002094 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002095}
2096
Tejun Heo0a268db2016-12-27 14:49:06 -05002097struct file_system_type cgroup_fs_type = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002098 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002099 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002100 .kill_sb = cgroup_kill_sb,
Serge Hallyn1c537532016-01-29 02:54:11 -06002101 .fs_flags = FS_USERNS_MOUNT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002102};
2103
Tejun Heo67e9c742015-11-16 11:13:34 -05002104static struct file_system_type cgroup2_fs_type = {
2105 .name = "cgroup2",
2106 .mount = cgroup_mount,
2107 .kill_sb = cgroup_kill_sb,
Serge Hallyn1c537532016-01-29 02:54:11 -06002108 .fs_flags = FS_USERNS_MOUNT,
Tejun Heo67e9c742015-11-16 11:13:34 -05002109};
2110
Tejun Heo0a268db2016-12-27 14:49:06 -05002111int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2112 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002113{
2114 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
Aditya Kalia79a9082016-01-29 02:54:06 -06002115
Tejun Heo4c737b42016-08-10 11:23:44 -04002116 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
Aditya Kalia79a9082016-01-29 02:54:06 -06002117}
2118
Tejun Heo4c737b42016-08-10 11:23:44 -04002119int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2120 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002121{
Tejun Heo4c737b42016-08-10 11:23:44 -04002122 int ret;
Aditya Kalia79a9082016-01-29 02:54:06 -06002123
2124 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002125 spin_lock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002126
2127 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2128
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002129 spin_unlock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002130 mutex_unlock(&cgroup_mutex);
2131
2132 return ret;
2133}
2134EXPORT_SYMBOL_GPL(cgroup_path_ns);
2135
Li Zefana043e3b2008-02-23 15:24:09 -08002136/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002137 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002138 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002139 * @buf: the buffer to write the path into
2140 * @buflen: the length of the buffer
2141 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002142 * Determine @task's cgroup on the first (the one with the lowest non-zero
2143 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2144 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2145 * cgroup controller callbacks.
2146 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002147 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002148 */
Tejun Heo4c737b42016-08-10 11:23:44 -04002149int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002150{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002151 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002152 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002153 int hierarchy_id = 1;
Tejun Heo4c737b42016-08-10 11:23:44 -04002154 int ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002155
2156 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002157 spin_lock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002158
Tejun Heo913ffdb2013-07-11 16:34:48 -07002159 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2160
Tejun Heo857a2be2013-04-14 20:50:08 -07002161 if (root) {
2162 cgrp = task_cgroup_from_root(task, root);
Tejun Heo4c737b42016-08-10 11:23:44 -04002163 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002164 } else {
2165 /* if no hierarchy exists, everyone is in "/" */
Tejun Heo4c737b42016-08-10 11:23:44 -04002166 ret = strlcpy(buf, "/", buflen);
Tejun Heo857a2be2013-04-14 20:50:08 -07002167 }
2168
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002169 spin_unlock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002170 mutex_unlock(&cgroup_mutex);
Tejun Heo4c737b42016-08-10 11:23:44 -04002171 return ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002172}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002173EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002174
Tejun Heoadaae5d2015-09-11 15:00:21 -04002175/**
Tejun Heoe595cd72017-01-15 19:03:41 -05002176 * cgroup_migrate_add_task - add a migration target task to a migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002177 * @task: target task
Tejun Heoe595cd72017-01-15 19:03:41 -05002178 * @mgctx: target migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002179 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002180 * Add @task, which is a migration target, to @mgctx->tset. This function
2181 * becomes noop if @task doesn't need to be migrated. @task's css_set
2182 * should have been added as a migration source and @task->cg_list will be
2183 * moved from the css_set's tasks list to mg_tasks one.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002184 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002185static void cgroup_migrate_add_task(struct task_struct *task,
2186 struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002187{
2188 struct css_set *cset;
2189
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002190 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002191
2192 /* @task either already exited or can't exit until the end */
2193 if (task->flags & PF_EXITING)
2194 return;
2195
2196 /* leave @task alone if post_fork() hasn't linked it yet */
2197 if (list_empty(&task->cg_list))
2198 return;
2199
2200 cset = task_css_set(task);
2201 if (!cset->mg_src_cgrp)
2202 return;
2203
Tejun Heo61046722017-07-08 07:17:02 -04002204 mgctx->tset.nr_tasks++;
2205
Tejun Heoadaae5d2015-09-11 15:00:21 -04002206 list_move_tail(&task->cg_list, &cset->mg_tasks);
2207 if (list_empty(&cset->mg_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002208 list_add_tail(&cset->mg_node,
2209 &mgctx->tset.src_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002210 if (list_empty(&cset->mg_dst_cset->mg_node))
Tejun Heod8ebf512017-01-15 19:03:40 -05002211 list_add_tail(&cset->mg_dst_cset->mg_node,
Tejun Heoe595cd72017-01-15 19:03:41 -05002212 &mgctx->tset.dst_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002213}
2214
Tejun Heo2f7ee562011-12-12 18:12:21 -08002215/**
2216 * cgroup_taskset_first - reset taskset and return the first task
2217 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002218 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002219 *
2220 * @tset iteration is initialized and the first task is returned.
2221 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002222struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2223 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002224{
Tejun Heob3dc0942014-02-25 10:04:01 -05002225 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2226 tset->cur_task = NULL;
2227
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002228 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002229}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002230
2231/**
2232 * cgroup_taskset_next - iterate to the next task in taskset
2233 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002234 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002235 *
2236 * Return the next task in @tset. Iteration must have been initialized
2237 * with cgroup_taskset_first().
2238 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002239struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2240 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002241{
Tejun Heob3dc0942014-02-25 10:04:01 -05002242 struct css_set *cset = tset->cur_cset;
2243 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002244
Tejun Heob3dc0942014-02-25 10:04:01 -05002245 while (&cset->mg_node != tset->csets) {
2246 if (!task)
2247 task = list_first_entry(&cset->mg_tasks,
2248 struct task_struct, cg_list);
2249 else
2250 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002251
Tejun Heob3dc0942014-02-25 10:04:01 -05002252 if (&task->cg_list != &cset->mg_tasks) {
2253 tset->cur_cset = cset;
2254 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002255
2256 /*
2257 * This function may be called both before and
2258 * after cgroup_taskset_migrate(). The two cases
2259 * can be distinguished by looking at whether @cset
2260 * has its ->mg_dst_cset set.
2261 */
2262 if (cset->mg_dst_cset)
2263 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2264 else
2265 *dst_cssp = cset->subsys[tset->ssid];
2266
Tejun Heob3dc0942014-02-25 10:04:01 -05002267 return task;
2268 }
2269
2270 cset = list_next_entry(cset, mg_node);
2271 task = NULL;
2272 }
2273
2274 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002275}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002276
2277/**
Tejun Heo37ff9f82016-03-08 11:51:26 -05002278 * cgroup_taskset_migrate - migrate a taskset
Tejun Heoe595cd72017-01-15 19:03:41 -05002279 * @mgctx: migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002280 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002281 * Migrate tasks in @mgctx as setup by migration preparation functions.
Tejun Heo37ff9f82016-03-08 11:51:26 -05002282 * This function fails iff one of the ->can_attach callbacks fails and
Tejun Heoe595cd72017-01-15 19:03:41 -05002283 * guarantees that either all or none of the tasks in @mgctx are migrated.
2284 * @mgctx is consumed regardless of success.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002285 */
Tejun Heobfc2cf62017-01-15 19:03:41 -05002286static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002287{
Tejun Heoe595cd72017-01-15 19:03:41 -05002288 struct cgroup_taskset *tset = &mgctx->tset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002289 struct cgroup_subsys *ss;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002290 struct task_struct *task, *tmp_task;
2291 struct css_set *cset, *tmp_cset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002292 int ssid, failed_ssid, ret;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002293
Tejun Heoadaae5d2015-09-11 15:00:21 -04002294 /* check that we can legitimately attach to the cgroup */
Tejun Heo61046722017-07-08 07:17:02 -04002295 if (tset->nr_tasks) {
2296 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2297 if (ss->can_attach) {
2298 tset->ssid = ssid;
2299 ret = ss->can_attach(tset);
2300 if (ret) {
2301 failed_ssid = ssid;
2302 goto out_cancel_attach;
2303 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002304 }
Tejun Heo61046722017-07-08 07:17:02 -04002305 } while_each_subsys_mask();
2306 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002307
2308 /*
2309 * Now that we're guaranteed success, proceed to move all tasks to
2310 * the new cgroup. There are no failure cases after here, so this
2311 * is the commit point.
2312 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002313 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002314 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002315 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2316 struct css_set *from_cset = task_css_set(task);
2317 struct css_set *to_cset = cset->mg_dst_cset;
2318
2319 get_css_set(to_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002320 to_cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04002321 css_set_move_task(task, from_cset, to_cset, true);
2322 put_css_set_locked(from_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002323 from_cset->nr_tasks--;
Tejun Heof6d7d042015-10-15 16:41:52 -04002324 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002325 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002326 spin_unlock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002327
2328 /*
2329 * Migration is committed, all target tasks are now on dst_csets.
2330 * Nothing is sensitive to fork() after this point. Notify
2331 * controllers that migration is complete.
2332 */
2333 tset->csets = &tset->dst_csets;
2334
Tejun Heo61046722017-07-08 07:17:02 -04002335 if (tset->nr_tasks) {
2336 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2337 if (ss->attach) {
2338 tset->ssid = ssid;
2339 ss->attach(tset);
2340 }
2341 } while_each_subsys_mask();
2342 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002343
2344 ret = 0;
2345 goto out_release_tset;
2346
2347out_cancel_attach:
Tejun Heo61046722017-07-08 07:17:02 -04002348 if (tset->nr_tasks) {
2349 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2350 if (ssid == failed_ssid)
2351 break;
2352 if (ss->cancel_attach) {
2353 tset->ssid = ssid;
2354 ss->cancel_attach(tset);
2355 }
2356 } while_each_subsys_mask();
2357 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002358out_release_tset:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002359 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002360 list_splice_init(&tset->dst_csets, &tset->src_csets);
2361 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2362 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2363 list_del_init(&cset->mg_node);
2364 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002365 spin_unlock_irq(&css_set_lock);
Waiman Longc4fa6c42017-09-21 09:54:13 -04002366
2367 /*
2368 * Re-initialize the cgroup_taskset structure in case it is reused
2369 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2370 * iteration.
2371 */
2372 tset->nr_tasks = 0;
2373 tset->csets = &tset->src_csets;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002374 return ret;
2375}
2376
2377/**
Tejun Heo8cfd8142017-07-21 11:14:51 -04002378 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
Tejun Heo6c694c82016-03-08 11:51:25 -05002379 * @dst_cgrp: destination cgroup to test
2380 *
Tejun Heo8cfd8142017-07-21 11:14:51 -04002381 * On the default hierarchy, except for the mixable, (possible) thread root
2382 * and threaded cgroups, subtree_control must be zero for migration
2383 * destination cgroups with tasks so that child cgroups don't compete
2384 * against tasks.
Tejun Heo6c694c82016-03-08 11:51:25 -05002385 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04002386int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
Tejun Heo6c694c82016-03-08 11:51:25 -05002387{
Tejun Heo8cfd8142017-07-21 11:14:51 -04002388 /* v1 doesn't have any restriction */
2389 if (!cgroup_on_dfl(dst_cgrp))
2390 return 0;
2391
2392 /* verify @dst_cgrp can host resources */
2393 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
2394 return -EOPNOTSUPP;
2395
2396 /* mixables don't care */
2397 if (cgroup_is_mixable(dst_cgrp))
2398 return 0;
2399
2400 /*
2401 * If @dst_cgrp is already or can become a thread root or is
2402 * threaded, it doesn't matter.
2403 */
2404 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
2405 return 0;
2406
2407 /* apply no-internal-process constraint */
2408 if (dst_cgrp->subtree_control)
2409 return -EBUSY;
2410
2411 return 0;
Tejun Heo6c694c82016-03-08 11:51:25 -05002412}
2413
2414/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002415 * cgroup_migrate_finish - cleanup after attach
Tejun Heoe595cd72017-01-15 19:03:41 -05002416 * @mgctx: migration context
Ben Blum74a11662011-05-26 16:25:20 -07002417 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002418 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2419 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002420 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002421void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002422{
Tejun Heoe595cd72017-01-15 19:03:41 -05002423 LIST_HEAD(preloaded);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002424 struct css_set *cset, *tmp_cset;
2425
2426 lockdep_assert_held(&cgroup_mutex);
2427
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002428 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002429
2430 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2431 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2432
2433 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002434 cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002435 cset->mg_dst_cgrp = NULL;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002436 cset->mg_dst_cset = NULL;
2437 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002438 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002439 }
Tejun Heoe595cd72017-01-15 19:03:41 -05002440
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002441 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002442}
2443
2444/**
2445 * cgroup_migrate_add_src - add a migration source css_set
2446 * @src_cset: the source css_set to add
2447 * @dst_cgrp: the destination cgroup
Tejun Heoe595cd72017-01-15 19:03:41 -05002448 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002449 *
2450 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
Tejun Heoe595cd72017-01-15 19:03:41 -05002451 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
Tejun Heo1958d2d2014-02-25 10:04:03 -05002452 * up by cgroup_migrate_finish().
2453 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002454 * This function may be called without holding cgroup_threadgroup_rwsem
2455 * even if the target is a process. Threads may be created and destroyed
2456 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2457 * into play and the preloaded css_sets are guaranteed to cover all
2458 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002459 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002460void cgroup_migrate_add_src(struct css_set *src_cset,
2461 struct cgroup *dst_cgrp,
Tejun Heoe595cd72017-01-15 19:03:41 -05002462 struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002463{
2464 struct cgroup *src_cgrp;
2465
2466 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002467 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002468
Tejun Heo2b021cb2016-03-15 20:43:04 -04002469 /*
2470 * If ->dead, @src_set is associated with one or more dead cgroups
2471 * and doesn't contain any migratable tasks. Ignore it early so
2472 * that the rest of migration path doesn't get confused by it.
2473 */
2474 if (src_cset->dead)
2475 return;
2476
Tejun Heo1958d2d2014-02-25 10:04:03 -05002477 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2478
Tejun Heo1958d2d2014-02-25 10:04:03 -05002479 if (!list_empty(&src_cset->mg_preload_node))
2480 return;
2481
2482 WARN_ON(src_cset->mg_src_cgrp);
Tejun Heoe4857982016-03-08 11:51:26 -05002483 WARN_ON(src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002484 WARN_ON(!list_empty(&src_cset->mg_tasks));
2485 WARN_ON(!list_empty(&src_cset->mg_node));
2486
2487 src_cset->mg_src_cgrp = src_cgrp;
Tejun Heoe4857982016-03-08 11:51:26 -05002488 src_cset->mg_dst_cgrp = dst_cgrp;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002489 get_css_set(src_cset);
Tejun Heoe595cd72017-01-15 19:03:41 -05002490 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002491}
2492
2493/**
2494 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heoe595cd72017-01-15 19:03:41 -05002495 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002496 *
Tejun Heoe4857982016-03-08 11:51:26 -05002497 * Tasks are about to be moved and all the source css_sets have been
Tejun Heoe595cd72017-01-15 19:03:41 -05002498 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2499 * pins all destination css_sets, links each to its source, and append them
2500 * to @mgctx->preloaded_dst_csets.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002501 *
2502 * This function must be called after cgroup_migrate_add_src() has been
2503 * called on each migration source css_set. After migration is performed
2504 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
Tejun Heoe595cd72017-01-15 19:03:41 -05002505 * @mgctx.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002506 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002507int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002508{
Tejun Heof817de92014-04-23 11:13:16 -04002509 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002510
2511 lockdep_assert_held(&cgroup_mutex);
2512
2513 /* look up the dst cset for each src cset and link it to src */
Tejun Heoe595cd72017-01-15 19:03:41 -05002514 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2515 mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002516 struct css_set *dst_cset;
Tejun Heobfc2cf62017-01-15 19:03:41 -05002517 struct cgroup_subsys *ss;
2518 int ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002519
Tejun Heoe4857982016-03-08 11:51:26 -05002520 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002521 if (!dst_cset)
2522 goto err;
2523
2524 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002525
2526 /*
2527 * If src cset equals dst, it's noop. Drop the src.
2528 * cgroup_migrate() will skip the cset too. Note that we
2529 * can't handle src == dst as some nodes are used by both.
2530 */
2531 if (src_cset == dst_cset) {
2532 src_cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002533 src_cset->mg_dst_cgrp = NULL;
Tejun Heof817de92014-04-23 11:13:16 -04002534 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002535 put_css_set(src_cset);
2536 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002537 continue;
2538 }
2539
Tejun Heo1958d2d2014-02-25 10:04:03 -05002540 src_cset->mg_dst_cset = dst_cset;
2541
2542 if (list_empty(&dst_cset->mg_preload_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002543 list_add_tail(&dst_cset->mg_preload_node,
2544 &mgctx->preloaded_dst_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002545 else
Zefan Lia25eb522014-09-19 16:51:00 +08002546 put_css_set(dst_cset);
Tejun Heobfc2cf62017-01-15 19:03:41 -05002547
2548 for_each_subsys(ss, ssid)
2549 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2550 mgctx->ss_mask |= 1 << ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002551 }
2552
Tejun Heo1958d2d2014-02-25 10:04:03 -05002553 return 0;
2554err:
Tejun Heoe595cd72017-01-15 19:03:41 -05002555 cgroup_migrate_finish(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002556 return -ENOMEM;
2557}
2558
2559/**
2560 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002561 * @leader: the leader of the process or the task to migrate
2562 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heoe595cd72017-01-15 19:03:41 -05002563 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002564 *
Tejun Heo37ff9f82016-03-08 11:51:26 -05002565 * Migrate a process or task denoted by @leader. If migrating a process,
2566 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2567 * responsible for invoking cgroup_migrate_add_src() and
Tejun Heo1958d2d2014-02-25 10:04:03 -05002568 * cgroup_migrate_prepare_dst() on the targets before invoking this
2569 * function and following up with cgroup_migrate_finish().
2570 *
2571 * As long as a controller's ->can_attach() doesn't fail, this function is
2572 * guaranteed to succeed. This means that, excluding ->can_attach()
2573 * failure, when migrating multiple targets, the success or failure can be
2574 * decided for all targets by invoking group_migrate_prepare_dst() before
2575 * actually starting migrating.
2576 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002577int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heobfc2cf62017-01-15 19:03:41 -05002578 struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002579{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002580 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002581
2582 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002583 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2584 * already PF_EXITING could be freed from underneath us unless we
2585 * take an rcu_read_lock.
2586 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002587 spin_lock_irq(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002588 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002589 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002590 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002591 cgroup_migrate_add_task(task, mgctx);
Li Zefan081aa452013-03-13 09:17:09 +08002592 if (!threadgroup)
2593 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002594 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002595 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002596 spin_unlock_irq(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002597
Tejun Heobfc2cf62017-01-15 19:03:41 -05002598 return cgroup_migrate_execute(mgctx);
Ben Blum74a11662011-05-26 16:25:20 -07002599}
2600
Tejun Heo1958d2d2014-02-25 10:04:03 -05002601/**
2602 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2603 * @dst_cgrp: the cgroup to attach to
2604 * @leader: the task or the leader of the threadgroup to be attached
2605 * @threadgroup: attach the whole threadgroup?
2606 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002607 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002608 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002609int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2610 bool threadgroup)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002611{
Tejun Heoe595cd72017-01-15 19:03:41 -05002612 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002613 struct task_struct *task;
2614 int ret;
2615
Tejun Heo8cfd8142017-07-21 11:14:51 -04002616 ret = cgroup_migrate_vet_dst(dst_cgrp);
2617 if (ret)
2618 return ret;
Tejun Heo6c694c82016-03-08 11:51:25 -05002619
Tejun Heo1958d2d2014-02-25 10:04:03 -05002620 /* look up all src csets */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002621 spin_lock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002622 rcu_read_lock();
2623 task = leader;
2624 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002625 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002626 if (!threadgroup)
2627 break;
2628 } while_each_thread(leader, task);
2629 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002630 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002631
2632 /* prepare dst csets and commit */
Tejun Heoe595cd72017-01-15 19:03:41 -05002633 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002634 if (!ret)
Tejun Heobfc2cf62017-01-15 19:03:41 -05002635 ret = cgroup_migrate(leader, threadgroup, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002636
Tejun Heoe595cd72017-01-15 19:03:41 -05002637 cgroup_migrate_finish(&mgctx);
Tejun Heoed1777d2016-08-10 11:23:44 -04002638
2639 if (!ret)
2640 trace_cgroup_attach_task(dst_cgrp, leader, threadgroup);
2641
Tejun Heo1958d2d2014-02-25 10:04:03 -05002642 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002643}
2644
Tejun Heo715c8092017-05-15 09:34:00 -04002645struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
2646 __acquires(&cgroup_threadgroup_rwsem)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002647{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002648 struct task_struct *tsk;
Tejun Heoacbef752014-05-13 12:16:22 -04002649 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002650
Tejun Heoacbef752014-05-13 12:16:22 -04002651 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
Tejun Heo715c8092017-05-15 09:34:00 -04002652 return ERR_PTR(-EINVAL);
Ben Blum74a11662011-05-26 16:25:20 -07002653
Tejun Heo3014dde2015-09-16 13:03:02 -04002654 percpu_down_write(&cgroup_threadgroup_rwsem);
Tejun Heo715c8092017-05-15 09:34:00 -04002655
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002656 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002657 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002658 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002659 if (!tsk) {
Tejun Heo715c8092017-05-15 09:34:00 -04002660 tsk = ERR_PTR(-ESRCH);
2661 goto out_unlock_threadgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002662 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002663 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002664 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002665 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002666
2667 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002668 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002669
2670 /*
Tejun Heo77f88792017-03-16 16:54:24 -04002671 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2672 * If userland migrates such a kthread to a non-root cgroup, it can
2673 * become trapped in a cpuset, or RT kthread may be born in a
2674 * cgroup with no rt_runtime allocated. Just say no.
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002675 */
Tejun Heo77f88792017-03-16 16:54:24 -04002676 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
Tejun Heo715c8092017-05-15 09:34:00 -04002677 tsk = ERR_PTR(-EINVAL);
2678 goto out_unlock_threadgroup;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002679 }
2680
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002681 get_task_struct(tsk);
Tejun Heo715c8092017-05-15 09:34:00 -04002682 goto out_unlock_rcu;
Tejun Heocd3d0952011-12-12 18:12:21 -08002683
Tejun Heo715c8092017-05-15 09:34:00 -04002684out_unlock_threadgroup:
2685 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo3014dde2015-09-16 13:03:02 -04002686out_unlock_rcu:
2687 rcu_read_unlock();
Tejun Heo715c8092017-05-15 09:34:00 -04002688 return tsk;
2689}
2690
2691void cgroup_procs_write_finish(struct task_struct *task)
2692 __releases(&cgroup_threadgroup_rwsem)
2693{
2694 struct cgroup_subsys *ss;
2695 int ssid;
2696
2697 /* release reference from cgroup_procs_write_start() */
2698 put_task_struct(task);
2699
Tejun Heo3014dde2015-09-16 13:03:02 -04002700 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002701 for_each_subsys(ss, ssid)
2702 if (ss->post_attach)
2703 ss->post_attach();
Paul Menageaf351022008-07-25 01:47:01 -07002704}
2705
Tejun Heo6e5c8302016-02-22 22:25:47 -05002706static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002707{
2708 struct cgroup_subsys *ss;
2709 bool printed = false;
2710 int ssid;
2711
Tejun Heob4e0eea2016-02-22 22:25:46 -05002712 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002713 if (printed)
2714 seq_putc(seq, ' ');
2715 seq_printf(seq, "%s", ss->name);
2716 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002717 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002718 if (printed)
2719 seq_putc(seq, '\n');
2720}
2721
Tejun Heof8f22e52014-04-23 11:13:16 -04002722/* show controllers which are enabled from the parent */
2723static int cgroup_controllers_show(struct seq_file *seq, void *v)
2724{
2725 struct cgroup *cgrp = seq_css(seq)->cgroup;
2726
Tejun Heo5531dc92016-03-03 09:57:58 -05002727 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04002728 return 0;
2729}
2730
2731/* show controllers which are enabled for a given cgroup's children */
2732static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2733{
2734 struct cgroup *cgrp = seq_css(seq)->cgroup;
2735
Tejun Heo667c2492014-07-08 18:02:56 -04002736 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002737 return 0;
2738}
2739
2740/**
2741 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2742 * @cgrp: root of the subtree to update csses for
2743 *
Tejun Heo54962602016-03-03 09:58:01 -05002744 * @cgrp's control masks have changed and its subtree's css associations
2745 * need to be updated accordingly. This function looks up all css_sets
2746 * which are attached to the subtree, creates the matching updated css_sets
2747 * and migrates the tasks to the new ones.
Tejun Heof8f22e52014-04-23 11:13:16 -04002748 */
2749static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2750{
Tejun Heoe595cd72017-01-15 19:03:41 -05002751 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo54962602016-03-03 09:58:01 -05002752 struct cgroup_subsys_state *d_css;
2753 struct cgroup *dsct;
Tejun Heof8f22e52014-04-23 11:13:16 -04002754 struct css_set *src_cset;
2755 int ret;
2756
Tejun Heof8f22e52014-04-23 11:13:16 -04002757 lockdep_assert_held(&cgroup_mutex);
2758
Tejun Heo3014dde2015-09-16 13:03:02 -04002759 percpu_down_write(&cgroup_threadgroup_rwsem);
2760
Tejun Heof8f22e52014-04-23 11:13:16 -04002761 /* look up all csses currently attached to @cgrp's subtree */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002762 spin_lock_irq(&css_set_lock);
Tejun Heo54962602016-03-03 09:58:01 -05002763 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002764 struct cgrp_cset_link *link;
2765
Tejun Heo54962602016-03-03 09:58:01 -05002766 list_for_each_entry(link, &dsct->cset_links, cset_link)
Tejun Heoe595cd72017-01-15 19:03:41 -05002767 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002768 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002769 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002770
2771 /* NULL dst indicates self on default hierarchy */
Tejun Heoe595cd72017-01-15 19:03:41 -05002772 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002773 if (ret)
2774 goto out_finish;
2775
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002776 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002777 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002778 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002779
Tejun Heo10265072015-09-11 15:00:22 -04002780 /* all tasks in src_csets need to be migrated */
2781 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
Tejun Heoe595cd72017-01-15 19:03:41 -05002782 cgroup_migrate_add_task(task, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002783 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002784 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002785
Tejun Heobfc2cf62017-01-15 19:03:41 -05002786 ret = cgroup_migrate_execute(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002787out_finish:
Tejun Heoe595cd72017-01-15 19:03:41 -05002788 cgroup_migrate_finish(&mgctx);
Tejun Heo3014dde2015-09-16 13:03:02 -04002789 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002790 return ret;
2791}
2792
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002793/**
Tejun Heo945ba192016-03-03 09:58:00 -05002794 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
Tejun Heoce3f1d92016-03-03 09:57:59 -05002795 * @cgrp: root of the target subtree
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002796 *
2797 * Because css offlining is asynchronous, userland may try to re-enable a
Tejun Heo945ba192016-03-03 09:58:00 -05002798 * controller while the previous css is still around. This function grabs
2799 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002800 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002801void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
Tejun Heo945ba192016-03-03 09:58:00 -05002802 __acquires(&cgroup_mutex)
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002803{
2804 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002805 struct cgroup_subsys_state *d_css;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002806 struct cgroup_subsys *ss;
2807 int ssid;
2808
Tejun Heo945ba192016-03-03 09:58:00 -05002809restart:
2810 mutex_lock(&cgroup_mutex);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002811
Tejun Heoce3f1d92016-03-03 09:57:59 -05002812 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002813 for_each_subsys(ss, ssid) {
2814 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2815 DEFINE_WAIT(wait);
2816
Tejun Heoce3f1d92016-03-03 09:57:59 -05002817 if (!css || !percpu_ref_is_dying(&css->refcnt))
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002818 continue;
2819
Tejun Heoa590b902017-04-28 15:14:55 -04002820 cgroup_get_live(dsct);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002821 prepare_to_wait(&dsct->offline_waitq, &wait,
2822 TASK_UNINTERRUPTIBLE);
2823
2824 mutex_unlock(&cgroup_mutex);
2825 schedule();
2826 finish_wait(&dsct->offline_waitq, &wait);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002827
2828 cgroup_put(dsct);
Tejun Heo945ba192016-03-03 09:58:00 -05002829 goto restart;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002830 }
2831 }
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002832}
2833
Tejun Heo12b3bb62016-03-03 09:57:59 -05002834/**
Tejun Heo15a27c32016-03-03 09:57:59 -05002835 * cgroup_save_control - save control masks of a subtree
2836 * @cgrp: root of the target subtree
2837 *
2838 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2839 * prefixed fields for @cgrp's subtree including @cgrp itself.
2840 */
2841static void cgroup_save_control(struct cgroup *cgrp)
2842{
2843 struct cgroup *dsct;
2844 struct cgroup_subsys_state *d_css;
2845
2846 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2847 dsct->old_subtree_control = dsct->subtree_control;
2848 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
2849 }
2850}
2851
2852/**
2853 * cgroup_propagate_control - refresh control masks of a subtree
2854 * @cgrp: root of the target subtree
2855 *
2856 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2857 * ->subtree_control and propagate controller availability through the
2858 * subtree so that descendants don't have unavailable controllers enabled.
2859 */
2860static void cgroup_propagate_control(struct cgroup *cgrp)
2861{
2862 struct cgroup *dsct;
2863 struct cgroup_subsys_state *d_css;
2864
2865 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2866 dsct->subtree_control &= cgroup_control(dsct);
Tejun Heo5ced2512016-03-03 09:58:01 -05002867 dsct->subtree_ss_mask =
2868 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2869 cgroup_ss_mask(dsct));
Tejun Heo15a27c32016-03-03 09:57:59 -05002870 }
2871}
2872
2873/**
2874 * cgroup_restore_control - restore control masks of a subtree
2875 * @cgrp: root of the target subtree
2876 *
2877 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2878 * prefixed fields for @cgrp's subtree including @cgrp itself.
2879 */
2880static void cgroup_restore_control(struct cgroup *cgrp)
2881{
2882 struct cgroup *dsct;
2883 struct cgroup_subsys_state *d_css;
2884
2885 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2886 dsct->subtree_control = dsct->old_subtree_control;
2887 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
2888 }
2889}
2890
Tejun Heof6d635ad2016-03-08 11:51:26 -05002891static bool css_visible(struct cgroup_subsys_state *css)
2892{
2893 struct cgroup_subsys *ss = css->ss;
2894 struct cgroup *cgrp = css->cgroup;
2895
2896 if (cgroup_control(cgrp) & (1 << ss->id))
2897 return true;
2898 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2899 return false;
2900 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2901}
2902
Tejun Heo15a27c32016-03-03 09:57:59 -05002903/**
Tejun Heobdb53bd2016-03-03 09:57:59 -05002904 * cgroup_apply_control_enable - enable or show csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05002905 * @cgrp: root of the target subtree
Tejun Heobdb53bd2016-03-03 09:57:59 -05002906 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05002907 * Walk @cgrp's subtree and create new csses or make the existing ones
Tejun Heobdb53bd2016-03-03 09:57:59 -05002908 * visible. A css is created invisible if it's being implicitly enabled
2909 * through dependency. An invisible css is made visible when the userland
2910 * explicitly enables it.
2911 *
2912 * Returns 0 on success, -errno on failure. On failure, csses which have
2913 * been processed already aren't cleaned up. The caller is responsible for
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08002914 * cleaning up with cgroup_apply_control_disable().
Tejun Heobdb53bd2016-03-03 09:57:59 -05002915 */
2916static int cgroup_apply_control_enable(struct cgroup *cgrp)
2917{
2918 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002919 struct cgroup_subsys_state *d_css;
Tejun Heobdb53bd2016-03-03 09:57:59 -05002920 struct cgroup_subsys *ss;
2921 int ssid, ret;
2922
Tejun Heoce3f1d92016-03-03 09:57:59 -05002923 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heobdb53bd2016-03-03 09:57:59 -05002924 for_each_subsys(ss, ssid) {
2925 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2926
Tejun Heo945ba192016-03-03 09:58:00 -05002927 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2928
Tejun Heobdb53bd2016-03-03 09:57:59 -05002929 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
2930 continue;
2931
2932 if (!css) {
2933 css = css_create(dsct, ss);
2934 if (IS_ERR(css))
2935 return PTR_ERR(css);
2936 }
2937
Tejun Heof6d635ad2016-03-08 11:51:26 -05002938 if (css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05002939 ret = css_populate_dir(css);
Tejun Heobdb53bd2016-03-03 09:57:59 -05002940 if (ret)
2941 return ret;
2942 }
2943 }
2944 }
2945
2946 return 0;
2947}
2948
2949/**
Tejun Heo12b3bb62016-03-03 09:57:59 -05002950 * cgroup_apply_control_disable - kill or hide csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05002951 * @cgrp: root of the target subtree
Tejun Heo12b3bb62016-03-03 09:57:59 -05002952 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05002953 * Walk @cgrp's subtree and kill and hide csses so that they match
Tejun Heo12b3bb62016-03-03 09:57:59 -05002954 * cgroup_ss_mask() and cgroup_visible_mask().
2955 *
2956 * A css is hidden when the userland requests it to be disabled while other
2957 * subsystems are still depending on it. The css must not actively control
2958 * resources and be in the vanilla state if it's made visible again later.
2959 * Controllers which may be depended upon should provide ->css_reset() for
2960 * this purpose.
2961 */
2962static void cgroup_apply_control_disable(struct cgroup *cgrp)
2963{
2964 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002965 struct cgroup_subsys_state *d_css;
Tejun Heo12b3bb62016-03-03 09:57:59 -05002966 struct cgroup_subsys *ss;
2967 int ssid;
2968
Tejun Heoce3f1d92016-03-03 09:57:59 -05002969 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05002970 for_each_subsys(ss, ssid) {
2971 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2972
Tejun Heo945ba192016-03-03 09:58:00 -05002973 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2974
Tejun Heo12b3bb62016-03-03 09:57:59 -05002975 if (!css)
2976 continue;
2977
Tejun Heo334c3672016-03-03 09:58:01 -05002978 if (css->parent &&
2979 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05002980 kill_css(css);
Tejun Heof6d635ad2016-03-08 11:51:26 -05002981 } else if (!css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05002982 css_clear_dir(css);
Tejun Heo12b3bb62016-03-03 09:57:59 -05002983 if (ss->css_reset)
2984 ss->css_reset(css);
2985 }
2986 }
2987 }
2988}
2989
Tejun Heof7b28142016-03-03 09:58:00 -05002990/**
2991 * cgroup_apply_control - apply control mask updates to the subtree
2992 * @cgrp: root of the target subtree
2993 *
2994 * subsystems can be enabled and disabled in a subtree using the following
2995 * steps.
2996 *
2997 * 1. Call cgroup_save_control() to stash the current state.
2998 * 2. Update ->subtree_control masks in the subtree as desired.
2999 * 3. Call cgroup_apply_control() to apply the changes.
3000 * 4. Optionally perform other related operations.
3001 * 5. Call cgroup_finalize_control() to finish up.
3002 *
3003 * This function implements step 3 and propagates the mask changes
3004 * throughout @cgrp's subtree, updates csses accordingly and perform
3005 * process migrations.
3006 */
3007static int cgroup_apply_control(struct cgroup *cgrp)
3008{
3009 int ret;
3010
3011 cgroup_propagate_control(cgrp);
3012
3013 ret = cgroup_apply_control_enable(cgrp);
3014 if (ret)
3015 return ret;
3016
3017 /*
3018 * At this point, cgroup_e_css() results reflect the new csses
3019 * making the following cgroup_update_dfl_csses() properly update
3020 * css associations of all tasks in the subtree.
3021 */
3022 ret = cgroup_update_dfl_csses(cgrp);
3023 if (ret)
3024 return ret;
3025
3026 return 0;
3027}
3028
3029/**
3030 * cgroup_finalize_control - finalize control mask update
3031 * @cgrp: root of the target subtree
3032 * @ret: the result of the update
3033 *
3034 * Finalize control mask update. See cgroup_apply_control() for more info.
3035 */
3036static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3037{
3038 if (ret) {
3039 cgroup_restore_control(cgrp);
3040 cgroup_propagate_control(cgrp);
3041 }
3042
3043 cgroup_apply_control_disable(cgrp);
3044}
3045
Tejun Heo8cfd8142017-07-21 11:14:51 -04003046static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
3047{
3048 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
3049
3050 /* if nothing is getting enabled, nothing to worry about */
3051 if (!enable)
3052 return 0;
3053
3054 /* can @cgrp host any resources? */
3055 if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
3056 return -EOPNOTSUPP;
3057
3058 /* mixables don't care */
3059 if (cgroup_is_mixable(cgrp))
3060 return 0;
3061
3062 if (domain_enable) {
3063 /* can't enable domain controllers inside a thread subtree */
3064 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3065 return -EOPNOTSUPP;
3066 } else {
3067 /*
3068 * Threaded controllers can handle internal competitions
3069 * and are always allowed inside a (prospective) thread
3070 * subtree.
3071 */
3072 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3073 return 0;
3074 }
3075
3076 /*
3077 * Controllers can't be enabled for a cgroup with tasks to avoid
3078 * child cgroups competing against tasks.
3079 */
3080 if (cgroup_has_tasks(cgrp))
3081 return -EBUSY;
3082
3083 return 0;
3084}
3085
Tejun Heof8f22e52014-04-23 11:13:16 -04003086/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003087static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3088 char *buf, size_t nbytes,
3089 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003090{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003091 u16 enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04003092 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003093 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003094 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003095 int ssid, ret;
3096
3097 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003098 * Parse input - space separated list of subsystem names prefixed
3099 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003100 */
Tejun Heo451af502014-05-13 12:16:21 -04003101 buf = strstrip(buf);
3102 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003103 if (tok[0] == '\0')
3104 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003105 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003106 if (!cgroup_ssid_enabled(ssid) ||
3107 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003108 continue;
3109
3110 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003111 enable |= 1 << ssid;
3112 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003113 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003114 disable |= 1 << ssid;
3115 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003116 } else {
3117 return -EINVAL;
3118 }
3119 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003120 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003121 if (ssid == CGROUP_SUBSYS_COUNT)
3122 return -EINVAL;
3123 }
3124
Tejun Heo945ba192016-03-03 09:58:00 -05003125 cgrp = cgroup_kn_lock_live(of->kn, true);
Tejun Heoa9746d82014-05-13 12:19:22 -04003126 if (!cgrp)
3127 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003128
3129 for_each_subsys(ss, ssid) {
3130 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003131 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003132 enable &= ~(1 << ssid);
3133 continue;
3134 }
3135
Tejun Heo5531dc92016-03-03 09:57:58 -05003136 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003137 ret = -ENOENT;
3138 goto out_unlock;
3139 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003140 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003141 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003142 disable &= ~(1 << ssid);
3143 continue;
3144 }
3145
3146 /* a child has it enabled? */
3147 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003148 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003149 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003150 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003151 }
3152 }
3153 }
3154 }
3155
3156 if (!enable && !disable) {
3157 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003158 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003159 }
3160
Tejun Heo8cfd8142017-07-21 11:14:51 -04003161 ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3162 if (ret)
Tejun Heo27f26752017-07-16 21:44:18 -04003163 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003164
Tejun Heo15a27c32016-03-03 09:57:59 -05003165 /* save and update control masks and prepare csses */
3166 cgroup_save_control(cgrp);
Tejun Heoc29adf22014-07-08 18:02:56 -04003167
Tejun Heo15a27c32016-03-03 09:57:59 -05003168 cgrp->subtree_control |= enable;
3169 cgrp->subtree_control &= ~disable;
Tejun Heof63070d2014-07-08 18:02:57 -04003170
Tejun Heof7b28142016-03-03 09:58:00 -05003171 ret = cgroup_apply_control(cgrp);
Tejun Heof7b28142016-03-03 09:58:00 -05003172 cgroup_finalize_control(cgrp, ret);
Tejun Heo3c745412017-07-23 08:14:15 -04003173 if (ret)
3174 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003175
3176 kernfs_activate(cgrp->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003177out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003178 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003179 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003180}
3181
Tejun Heoc705a002017-07-25 13:20:18 -04003182/**
3183 * cgroup_enable_threaded - make @cgrp threaded
3184 * @cgrp: the target cgroup
3185 *
3186 * Called when "threaded" is written to the cgroup.type interface file and
3187 * tries to make @cgrp threaded and join the parent's resource domain.
3188 * This function is never called on the root cgroup as cgroup.type doesn't
3189 * exist on it.
3190 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04003191static int cgroup_enable_threaded(struct cgroup *cgrp)
3192{
3193 struct cgroup *parent = cgroup_parent(cgrp);
3194 struct cgroup *dom_cgrp = parent->dom_cgrp;
3195 int ret;
3196
3197 lockdep_assert_held(&cgroup_mutex);
3198
3199 /* noop if already threaded */
3200 if (cgroup_is_threaded(cgrp))
3201 return 0;
3202
Tejun Heod1897c92018-02-21 11:39:22 -08003203 /*
3204 * If @cgroup is populated or has domain controllers enabled, it
3205 * can't be switched. While the below cgroup_can_be_thread_root()
3206 * test can catch the same conditions, that's only when @parent is
3207 * not mixable, so let's check it explicitly.
3208 */
3209 if (cgroup_is_populated(cgrp) ||
3210 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3211 return -EOPNOTSUPP;
3212
Tejun Heo8cfd8142017-07-21 11:14:51 -04003213 /* we're joining the parent's domain, ensure its validity */
3214 if (!cgroup_is_valid_domain(dom_cgrp) ||
3215 !cgroup_can_be_thread_root(dom_cgrp))
3216 return -EOPNOTSUPP;
3217
3218 /*
Tejun Heo8cfd8142017-07-21 11:14:51 -04003219 * The following shouldn't cause actual migrations and should
3220 * always succeed.
3221 */
3222 cgroup_save_control(cgrp);
3223
3224 cgrp->dom_cgrp = dom_cgrp;
3225 ret = cgroup_apply_control(cgrp);
3226 if (!ret)
3227 parent->nr_threaded_children++;
3228 else
3229 cgrp->dom_cgrp = cgrp;
3230
3231 cgroup_finalize_control(cgrp, ret);
3232 return ret;
3233}
3234
3235static int cgroup_type_show(struct seq_file *seq, void *v)
3236{
3237 struct cgroup *cgrp = seq_css(seq)->cgroup;
3238
3239 if (cgroup_is_threaded(cgrp))
3240 seq_puts(seq, "threaded\n");
3241 else if (!cgroup_is_valid_domain(cgrp))
3242 seq_puts(seq, "domain invalid\n");
3243 else if (cgroup_is_thread_root(cgrp))
3244 seq_puts(seq, "domain threaded\n");
3245 else
3246 seq_puts(seq, "domain\n");
3247
3248 return 0;
3249}
3250
3251static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
3252 size_t nbytes, loff_t off)
3253{
3254 struct cgroup *cgrp;
3255 int ret;
3256
3257 /* only switching to threaded mode is supported */
3258 if (strcmp(strstrip(buf), "threaded"))
3259 return -EINVAL;
3260
3261 cgrp = cgroup_kn_lock_live(of->kn, false);
3262 if (!cgrp)
3263 return -ENOENT;
3264
3265 /* threaded can only be enabled */
3266 ret = cgroup_enable_threaded(cgrp);
3267
3268 cgroup_kn_unlock(of->kn);
3269 return ret ?: nbytes;
3270}
3271
Roman Gushchin1a926e02017-07-28 18:28:44 +01003272static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
3273{
3274 struct cgroup *cgrp = seq_css(seq)->cgroup;
3275 int descendants = READ_ONCE(cgrp->max_descendants);
3276
3277 if (descendants == INT_MAX)
3278 seq_puts(seq, "max\n");
3279 else
3280 seq_printf(seq, "%d\n", descendants);
3281
3282 return 0;
3283}
3284
3285static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
3286 char *buf, size_t nbytes, loff_t off)
3287{
3288 struct cgroup *cgrp;
3289 int descendants;
3290 ssize_t ret;
3291
3292 buf = strstrip(buf);
3293 if (!strcmp(buf, "max")) {
3294 descendants = INT_MAX;
3295 } else {
3296 ret = kstrtoint(buf, 0, &descendants);
3297 if (ret)
3298 return ret;
3299 }
3300
Dan Carpenter696b98f2017-08-09 13:25:21 +03003301 if (descendants < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003302 return -ERANGE;
3303
3304 cgrp = cgroup_kn_lock_live(of->kn, false);
3305 if (!cgrp)
3306 return -ENOENT;
3307
3308 cgrp->max_descendants = descendants;
3309
3310 cgroup_kn_unlock(of->kn);
3311
3312 return nbytes;
3313}
3314
3315static int cgroup_max_depth_show(struct seq_file *seq, void *v)
3316{
3317 struct cgroup *cgrp = seq_css(seq)->cgroup;
3318 int depth = READ_ONCE(cgrp->max_depth);
3319
3320 if (depth == INT_MAX)
3321 seq_puts(seq, "max\n");
3322 else
3323 seq_printf(seq, "%d\n", depth);
3324
3325 return 0;
3326}
3327
3328static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
3329 char *buf, size_t nbytes, loff_t off)
3330{
3331 struct cgroup *cgrp;
3332 ssize_t ret;
3333 int depth;
3334
3335 buf = strstrip(buf);
3336 if (!strcmp(buf, "max")) {
3337 depth = INT_MAX;
3338 } else {
3339 ret = kstrtoint(buf, 0, &depth);
3340 if (ret)
3341 return ret;
3342 }
3343
Dan Carpenter696b98f2017-08-09 13:25:21 +03003344 if (depth < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003345 return -ERANGE;
3346
3347 cgrp = cgroup_kn_lock_live(of->kn, false);
3348 if (!cgrp)
3349 return -ENOENT;
3350
3351 cgrp->max_depth = depth;
3352
3353 cgroup_kn_unlock(of->kn);
3354
3355 return nbytes;
3356}
3357
Tejun Heo4a07c222015-09-18 17:54:22 -04003358static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003359{
Tejun Heo4a07c222015-09-18 17:54:22 -04003360 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003361 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003362 return 0;
3363}
3364
Tejun Heo3e489302017-08-11 05:49:01 -07003365static int cgroup_stat_show(struct seq_file *seq, void *v)
Roman Gushchinec392252017-08-02 17:55:31 +01003366{
3367 struct cgroup *cgroup = seq_css(seq)->cgroup;
3368
3369 seq_printf(seq, "nr_descendants %d\n",
3370 cgroup->nr_descendants);
3371 seq_printf(seq, "nr_dying_descendants %d\n",
3372 cgroup->nr_dying_descendants);
3373
3374 return 0;
3375}
3376
Tejun Heod41bf8c2017-10-23 16:18:27 -07003377static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
3378 struct cgroup *cgrp, int ssid)
3379{
3380 struct cgroup_subsys *ss = cgroup_subsys[ssid];
3381 struct cgroup_subsys_state *css;
3382 int ret;
3383
3384 if (!ss->css_extra_stat_show)
3385 return 0;
3386
3387 css = cgroup_tryget_css(cgrp, ss);
3388 if (!css)
3389 return 0;
3390
3391 ret = ss->css_extra_stat_show(seq, css);
3392 css_put(css);
3393 return ret;
3394}
3395
3396static int cpu_stat_show(struct seq_file *seq, void *v)
3397{
Tejun Heoc3ba1322017-10-30 08:13:14 -07003398 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
Tejun Heod41bf8c2017-10-23 16:18:27 -07003399 int ret = 0;
3400
Tejun Heod4ff7492018-04-26 14:29:04 -07003401 cgroup_base_stat_cputime_show(seq);
Tejun Heod41bf8c2017-10-23 16:18:27 -07003402#ifdef CONFIG_CGROUP_SCHED
3403 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
3404#endif
3405 return ret;
3406}
3407
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003408static int cgroup_file_open(struct kernfs_open_file *of)
3409{
3410 struct cftype *cft = of->kn->priv;
3411
3412 if (cft->open)
3413 return cft->open(of);
3414 return 0;
3415}
3416
3417static void cgroup_file_release(struct kernfs_open_file *of)
3418{
3419 struct cftype *cft = of->kn->priv;
3420
3421 if (cft->release)
3422 cft->release(of);
3423}
3424
Tejun Heo2bd59d42014-02-11 11:52:49 -05003425static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3426 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003427{
Tejun Heo5136f632017-06-27 14:30:28 -04003428 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003429 struct cgroup *cgrp = of->kn->parent->priv;
3430 struct cftype *cft = of->kn->priv;
3431 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003432 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003433
Tejun Heo5136f632017-06-27 14:30:28 -04003434 /*
3435 * If namespaces are delegation boundaries, disallow writes to
3436 * files in an non-init namespace root from inside the namespace
3437 * except for the files explicitly marked delegatable -
3438 * cgroup.procs and cgroup.subtree_control.
3439 */
3440 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
3441 !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
3442 ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp)
3443 return -EPERM;
3444
Tejun Heob4168642014-05-13 12:16:21 -04003445 if (cft->write)
3446 return cft->write(of, buf, nbytes, off);
3447
Tejun Heo2bd59d42014-02-11 11:52:49 -05003448 /*
3449 * kernfs guarantees that a file isn't deleted with operations in
3450 * flight, which means that the matching css is and stays alive and
3451 * doesn't need to be pinned. The RCU locking is not necessary
3452 * either. It's just for the convenience of using cgroup_css().
3453 */
3454 rcu_read_lock();
3455 css = cgroup_css(cgrp, cft->ss);
3456 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003457
Tejun Heo451af502014-05-13 12:16:21 -04003458 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003459 unsigned long long v;
3460 ret = kstrtoull(buf, 0, &v);
3461 if (!ret)
3462 ret = cft->write_u64(css, cft, v);
3463 } else if (cft->write_s64) {
3464 long long v;
3465 ret = kstrtoll(buf, 0, &v);
3466 if (!ret)
3467 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003468 } else {
3469 ret = -EINVAL;
3470 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003471
Tejun Heoa742c592013-12-05 12:28:03 -05003472 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003473}
3474
Tejun Heo6612f052013-12-05 12:28:04 -05003475static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003476{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003477 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003478}
3479
3480static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3481{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003482 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003483}
3484
3485static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3486{
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003487 if (seq_cft(seq)->seq_stop)
3488 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003489}
3490
3491static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3492{
Tejun Heo7da11272013-12-05 12:28:04 -05003493 struct cftype *cft = seq_cft(m);
3494 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003495
Tejun Heo2da8ca82013-12-05 12:28:04 -05003496 if (cft->seq_show)
3497 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003498
Tejun Heo896f5192013-12-05 12:28:04 -05003499 if (cft->read_u64)
3500 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3501 else if (cft->read_s64)
3502 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3503 else
3504 return -EINVAL;
3505 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003506}
3507
Tejun Heo2bd59d42014-02-11 11:52:49 -05003508static struct kernfs_ops cgroup_kf_single_ops = {
3509 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003510 .open = cgroup_file_open,
3511 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003512 .write = cgroup_file_write,
3513 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003514};
3515
Tejun Heo2bd59d42014-02-11 11:52:49 -05003516static struct kernfs_ops cgroup_kf_ops = {
3517 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003518 .open = cgroup_file_open,
3519 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003520 .write = cgroup_file_write,
3521 .seq_start = cgroup_seqfile_start,
3522 .seq_next = cgroup_seqfile_next,
3523 .seq_stop = cgroup_seqfile_stop,
3524 .seq_show = cgroup_seqfile_show,
3525};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003526
Tejun Heo49957f82014-04-07 16:44:47 -04003527/* set uid and gid of cgroup dirs and files to that of the creator */
3528static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3529{
3530 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3531 .ia_uid = current_fsuid(),
3532 .ia_gid = current_fsgid(), };
3533
3534 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3535 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3536 return 0;
3537
3538 return kernfs_setattr(kn, &iattr);
3539}
3540
Tejun Heob12e3582018-04-26 14:29:04 -07003541static void cgroup_file_notify_timer(struct timer_list *timer)
3542{
3543 cgroup_file_notify(container_of(timer, struct cgroup_file,
3544 notify_timer));
3545}
3546
Tejun Heo4df8dc92015-09-18 17:54:23 -04003547static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3548 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003549{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003550 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003551 struct kernfs_node *kn;
3552 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003553 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003554
Tejun Heo2bd59d42014-02-11 11:52:49 -05003555#ifdef CONFIG_DEBUG_LOCK_ALLOC
3556 key = &cft->lockdep_key;
3557#endif
3558 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3559 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003560 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003561 if (IS_ERR(kn))
3562 return PTR_ERR(kn);
3563
3564 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003565 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003566 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003567 return ret;
3568 }
3569
Tejun Heo6f60ead2015-09-18 17:54:23 -04003570 if (cft->file_offset) {
3571 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3572
Tejun Heob12e3582018-04-26 14:29:04 -07003573 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
3574
Tejun Heo34c06252015-11-05 00:12:24 -05003575 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003576 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003577 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003578 }
3579
Tejun Heof8f22e52014-04-23 11:13:16 -04003580 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003581}
3582
Tejun Heob1f28d32013-06-28 16:24:10 -07003583/**
3584 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003585 * @css: the target css
3586 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003587 * @cfts: array of cftypes to be added
3588 * @is_add: whether to add or remove
3589 *
3590 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003591 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003592 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003593static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3594 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003595 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003596{
Tejun Heo6732ed82015-09-18 17:54:23 -04003597 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003598 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003599
Tejun Heo01f64742014-05-13 12:19:23 -04003600 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003601
Tejun Heo6732ed82015-09-18 17:54:23 -04003602restart:
3603 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003604 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003605 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003606 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003607 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003608 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003609 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003610 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003611 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003612 continue;
3613
Li Zefan2739d3c2013-01-21 18:18:33 +08003614 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003615 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003616 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003617 pr_warn("%s: failed to add %s, err=%d\n",
3618 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003619 cft_end = cft;
3620 is_add = false;
3621 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003622 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003623 } else {
3624 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003625 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003626 }
Tejun Heob598dde2016-02-22 22:25:45 -05003627 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003628}
3629
Tejun Heo21a2d342014-02-12 09:29:49 -05003630static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003631{
Tejun Heo2bb566c2013-08-08 20:11:23 -04003632 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003633 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003634 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003635 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003636
Tejun Heo01f64742014-05-13 12:19:23 -04003637 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003638
Li Zefane8c82d22013-06-18 18:48:37 +08003639 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003640 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003641 struct cgroup *cgrp = css->cgroup;
3642
Tejun Heo88cb04b2016-03-03 09:57:58 -05003643 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08003644 continue;
3645
Tejun Heo4df8dc92015-09-18 17:54:23 -04003646 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003647 if (ret)
3648 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003649 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003650
3651 if (is_add && !ret)
3652 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003653 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003654}
3655
Tejun Heo2da440a2014-02-11 11:52:48 -05003656static void cgroup_exit_cftypes(struct cftype *cfts)
3657{
3658 struct cftype *cft;
3659
Tejun Heo2bd59d42014-02-11 11:52:49 -05003660 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3661 /* free copy for custom atomic_write_len, see init_cftypes() */
3662 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3663 kfree(cft->kf_ops);
3664 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003665 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003666
3667 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003668 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003669 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003670}
3671
Tejun Heo2bd59d42014-02-11 11:52:49 -05003672static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003673{
3674 struct cftype *cft;
3675
Tejun Heo2bd59d42014-02-11 11:52:49 -05003676 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3677 struct kernfs_ops *kf_ops;
3678
Tejun Heo0adb0702014-02-12 09:29:48 -05003679 WARN_ON(cft->ss || cft->kf_ops);
3680
Tejun Heo2bd59d42014-02-11 11:52:49 -05003681 if (cft->seq_start)
3682 kf_ops = &cgroup_kf_ops;
3683 else
3684 kf_ops = &cgroup_kf_single_ops;
3685
3686 /*
3687 * Ugh... if @cft wants a custom max_write_len, we need to
3688 * make a copy of kf_ops to set its atomic_write_len.
3689 */
3690 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3691 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3692 if (!kf_ops) {
3693 cgroup_exit_cftypes(cfts);
3694 return -ENOMEM;
3695 }
3696 kf_ops->atomic_write_len = cft->max_write_len;
3697 }
3698
3699 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003700 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003701 }
3702
3703 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003704}
3705
Tejun Heo21a2d342014-02-12 09:29:49 -05003706static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3707{
Tejun Heo01f64742014-05-13 12:19:23 -04003708 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003709
3710 if (!cfts || !cfts[0].ss)
3711 return -ENOENT;
3712
3713 list_del(&cfts->node);
3714 cgroup_apply_cftypes(cfts, false);
3715 cgroup_exit_cftypes(cfts);
3716 return 0;
3717}
3718
Tejun Heo8e3f6542012-04-01 12:09:55 -07003719/**
Tejun Heo80b13582014-02-12 09:29:48 -05003720 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3721 * @cfts: zero-length name terminated array of cftypes
3722 *
3723 * Unregister @cfts. Files described by @cfts are removed from all
3724 * existing cgroups and all future cgroups won't have them either. This
3725 * function can be called anytime whether @cfts' subsys is attached or not.
3726 *
3727 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3728 * registered.
3729 */
3730int cgroup_rm_cftypes(struct cftype *cfts)
3731{
Tejun Heo21a2d342014-02-12 09:29:49 -05003732 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003733
Tejun Heo01f64742014-05-13 12:19:23 -04003734 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003735 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003736 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003737 return ret;
3738}
3739
3740/**
3741 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3742 * @ss: target cgroup subsystem
3743 * @cfts: zero-length name terminated array of cftypes
3744 *
3745 * Register @cfts to @ss. Files described by @cfts are created for all
3746 * existing cgroups to which @ss is attached and all future cgroups will
3747 * have them too. This function can be called anytime whether @ss is
3748 * attached or not.
3749 *
3750 * Returns 0 on successful registration, -errno on failure. Note that this
3751 * function currently returns 0 as long as @cfts registration is successful
3752 * even if some file creation attempts on existing cgroups fail.
3753 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003754static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003755{
Tejun Heo9ccece82013-06-28 16:24:11 -07003756 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003757
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003758 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003759 return 0;
3760
Li Zefandc5736e2014-02-17 10:41:50 +08003761 if (!cfts || cfts[0].name[0] == '\0')
3762 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003763
Tejun Heo2bd59d42014-02-11 11:52:49 -05003764 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003765 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003766 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003767
Tejun Heo01f64742014-05-13 12:19:23 -04003768 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003769
Tejun Heo0adb0702014-02-12 09:29:48 -05003770 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003771 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003772 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003773 cgroup_rm_cftypes_locked(cfts);
3774
Tejun Heo01f64742014-05-13 12:19:23 -04003775 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003776 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003777}
Tejun Heo79578622012-04-01 12:09:56 -07003778
3779/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003780 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3781 * @ss: target cgroup subsystem
3782 * @cfts: zero-length name terminated array of cftypes
3783 *
3784 * Similar to cgroup_add_cftypes() but the added files are only used for
3785 * the default hierarchy.
3786 */
3787int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3788{
3789 struct cftype *cft;
3790
3791 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003792 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003793 return cgroup_add_cftypes(ss, cfts);
3794}
3795
3796/**
3797 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3798 * @ss: target cgroup subsystem
3799 * @cfts: zero-length name terminated array of cftypes
3800 *
3801 * Similar to cgroup_add_cftypes() but the added files are only used for
3802 * the legacy hierarchies.
3803 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003804int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3805{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003806 struct cftype *cft;
3807
Tejun Heoe4b70372015-10-15 17:00:43 -04003808 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3809 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003810 return cgroup_add_cftypes(ss, cfts);
3811}
3812
Li Zefana043e3b2008-02-23 15:24:09 -08003813/**
Tejun Heo34c06252015-11-05 00:12:24 -05003814 * cgroup_file_notify - generate a file modified event for a cgroup_file
3815 * @cfile: target cgroup_file
3816 *
3817 * @cfile must have been obtained by setting cftype->file_offset.
3818 */
3819void cgroup_file_notify(struct cgroup_file *cfile)
3820{
3821 unsigned long flags;
3822
3823 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
Tejun Heob12e3582018-04-26 14:29:04 -07003824 if (cfile->kn) {
3825 unsigned long last = cfile->notified_at;
3826 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
3827
3828 if (time_in_range(jiffies, last, next)) {
3829 timer_reduce(&cfile->notify_timer, next);
3830 } else {
3831 kernfs_notify(cfile->kn);
3832 cfile->notified_at = jiffies;
3833 }
3834 }
Tejun Heo34c06252015-11-05 00:12:24 -05003835 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3836}
3837
3838/**
Tejun Heo492eb212013-08-08 20:11:25 -04003839 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003840 * @pos: the current position (%NULL to initiate traversal)
3841 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003842 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003843 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003844 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003845 * that @parent and @pos are accessible. The next sibling is guaranteed to
3846 * be returned regardless of their states.
3847 *
3848 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3849 * css which finished ->css_online() is guaranteed to be visible in the
3850 * future iterations and will stay visible until the last reference is put.
3851 * A css which hasn't finished ->css_online() or already finished
3852 * ->css_offline() may show up during traversal. It's each subsystem's
3853 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003854 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003855struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3856 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003857{
Tejun Heoc2931b72014-05-16 13:22:51 -04003858 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003859
Tejun Heo8353da12014-05-13 12:19:23 -04003860 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003861
3862 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003863 * @pos could already have been unlinked from the sibling list.
3864 * Once a cgroup is removed, its ->sibling.next is no longer
3865 * updated when its next sibling changes. CSS_RELEASED is set when
3866 * @pos is taken off list, at which time its next pointer is valid,
3867 * and, as releases are serialized, the one pointed to by the next
3868 * pointer is guaranteed to not have started release yet. This
3869 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3870 * critical section, the one pointed to by its next pointer is
3871 * guaranteed to not have finished its RCU grace period even if we
3872 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003873 *
Tejun Heode3f0342014-05-16 13:22:49 -04003874 * If @pos has CSS_RELEASED set, its next pointer can't be
3875 * dereferenced; however, as each css is given a monotonically
3876 * increasing unique serial number and always appended to the
3877 * sibling list, the next one can be found by walking the parent's
3878 * children until the first css with higher serial number than
3879 * @pos's. While this path can be slower, it happens iff iteration
3880 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003881 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003882 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003883 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3884 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3885 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003886 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003887 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003888 if (next->serial_nr > pos->serial_nr)
3889 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003890 }
3891
Tejun Heo3b281af2014-04-23 11:13:15 -04003892 /*
3893 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003894 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003895 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003896 if (&next->sibling != &parent->children)
3897 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003898 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003899}
Tejun Heo53fa5262013-05-24 10:55:38 +09003900
3901/**
Tejun Heo492eb212013-08-08 20:11:25 -04003902 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003903 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003904 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003905 *
Tejun Heo492eb212013-08-08 20:11:25 -04003906 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003907 * to visit for pre-order traversal of @root's descendants. @root is
3908 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003909 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003910 * While this function requires cgroup_mutex or RCU read locking, it
3911 * doesn't require the whole traversal to be contained in a single critical
3912 * section. This function will return the correct next descendant as long
3913 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003914 *
3915 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3916 * css which finished ->css_online() is guaranteed to be visible in the
3917 * future iterations and will stay visible until the last reference is put.
3918 * A css which hasn't finished ->css_online() or already finished
3919 * ->css_offline() may show up during traversal. It's each subsystem's
3920 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003921 */
Tejun Heo492eb212013-08-08 20:11:25 -04003922struct cgroup_subsys_state *
3923css_next_descendant_pre(struct cgroup_subsys_state *pos,
3924 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003925{
Tejun Heo492eb212013-08-08 20:11:25 -04003926 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003927
Tejun Heo8353da12014-05-13 12:19:23 -04003928 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003929
Tejun Heobd8815a2013-08-08 20:11:27 -04003930 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003931 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003932 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003933
3934 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003935 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003936 if (next)
3937 return next;
3938
3939 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003940 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003941 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003942 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003943 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003944 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003945 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003946
3947 return NULL;
3948}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003949
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003950/**
Tejun Heo492eb212013-08-08 20:11:25 -04003951 * css_rightmost_descendant - return the rightmost descendant of a css
3952 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003953 *
Tejun Heo492eb212013-08-08 20:11:25 -04003954 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3955 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003956 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003957 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003958 * While this function requires cgroup_mutex or RCU read locking, it
3959 * doesn't require the whole traversal to be contained in a single critical
3960 * section. This function will return the correct rightmost descendant as
3961 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003962 */
Tejun Heo492eb212013-08-08 20:11:25 -04003963struct cgroup_subsys_state *
3964css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003965{
Tejun Heo492eb212013-08-08 20:11:25 -04003966 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003967
Tejun Heo8353da12014-05-13 12:19:23 -04003968 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003969
3970 do {
3971 last = pos;
3972 /* ->prev isn't RCU safe, walk ->next till the end */
3973 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003974 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003975 pos = tmp;
3976 } while (pos);
3977
3978 return last;
3979}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003980
Tejun Heo492eb212013-08-08 20:11:25 -04003981static struct cgroup_subsys_state *
3982css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003983{
Tejun Heo492eb212013-08-08 20:11:25 -04003984 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003985
3986 do {
3987 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003988 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003989 } while (pos);
3990
3991 return last;
3992}
3993
3994/**
Tejun Heo492eb212013-08-08 20:11:25 -04003995 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003996 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003997 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003998 *
Tejun Heo492eb212013-08-08 20:11:25 -04003999 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004000 * to visit for post-order traversal of @root's descendants. @root is
4001 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004002 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004003 * While this function requires cgroup_mutex or RCU read locking, it
4004 * doesn't require the whole traversal to be contained in a single critical
4005 * section. This function will return the correct next descendant as long
4006 * as both @pos and @cgroup are accessible and @pos is a descendant of
4007 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04004008 *
4009 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4010 * css which finished ->css_online() is guaranteed to be visible in the
4011 * future iterations and will stay visible until the last reference is put.
4012 * A css which hasn't finished ->css_online() or already finished
4013 * ->css_offline() may show up during traversal. It's each subsystem's
4014 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004015 */
Tejun Heo492eb212013-08-08 20:11:25 -04004016struct cgroup_subsys_state *
4017css_next_descendant_post(struct cgroup_subsys_state *pos,
4018 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004019{
Tejun Heo492eb212013-08-08 20:11:25 -04004020 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004021
Tejun Heo8353da12014-05-13 12:19:23 -04004022 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004023
Tejun Heo58b79a92013-09-06 15:31:08 -04004024 /* if first iteration, visit leftmost descendant which may be @root */
4025 if (!pos)
4026 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004027
Tejun Heobd8815a2013-08-08 20:11:27 -04004028 /* if we visited @root, we're done */
4029 if (pos == root)
4030 return NULL;
4031
Tejun Heo574bd9f2012-11-09 09:12:29 -08004032 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004033 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004034 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04004035 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004036
4037 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004038 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004039}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004040
Tejun Heof3d46502014-05-16 13:22:52 -04004041/**
4042 * css_has_online_children - does a css have online children
4043 * @css: the target css
4044 *
4045 * Returns %true if @css has any online children; otherwise, %false. This
4046 * function can be called from any context but the caller is responsible
4047 * for synchronizing against on/offlining as necessary.
4048 */
4049bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04004050{
Tejun Heof3d46502014-05-16 13:22:52 -04004051 struct cgroup_subsys_state *child;
4052 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04004053
4054 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04004055 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08004056 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04004057 ret = true;
4058 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04004059 }
4060 }
4061 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04004062 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08004063}
4064
Tejun Heo450ee0c2017-05-15 09:34:03 -04004065static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4066{
4067 struct list_head *l;
4068 struct cgrp_cset_link *link;
4069 struct css_set *cset;
4070
4071 lockdep_assert_held(&css_set_lock);
4072
4073 /* find the next threaded cset */
4074 if (it->tcset_pos) {
4075 l = it->tcset_pos->next;
4076
4077 if (l != it->tcset_head) {
4078 it->tcset_pos = l;
4079 return container_of(l, struct css_set,
4080 threaded_csets_node);
4081 }
4082
4083 it->tcset_pos = NULL;
4084 }
4085
4086 /* find the next cset */
4087 l = it->cset_pos;
4088 l = l->next;
4089 if (l == it->cset_head) {
4090 it->cset_pos = NULL;
4091 return NULL;
4092 }
4093
4094 if (it->ss) {
4095 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4096 } else {
4097 link = list_entry(l, struct cgrp_cset_link, cset_link);
4098 cset = link->cset;
4099 }
4100
4101 it->cset_pos = l;
4102
4103 /* initialize threaded css_set walking */
4104 if (it->flags & CSS_TASK_ITER_THREADED) {
4105 if (it->cur_dcset)
4106 put_css_set_locked(it->cur_dcset);
4107 it->cur_dcset = cset;
4108 get_css_set(cset);
4109
4110 it->tcset_head = &cset->threaded_csets;
4111 it->tcset_pos = &cset->threaded_csets;
4112 }
4113
4114 return cset;
4115}
4116
Tejun Heo0942eee2013-08-08 20:11:26 -04004117/**
Tejun Heoecb9d532015-10-15 16:41:52 -04004118 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04004119 * @it: the iterator to advance
4120 *
4121 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04004122 */
Tejun Heoecb9d532015-10-15 16:41:52 -04004123static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04004124{
Tejun Heod5158762013-08-08 20:11:26 -04004125 struct css_set *cset;
4126
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004127 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004128
Tejun Heod5158762013-08-08 20:11:26 -04004129 /* Advance to the next non-empty css_set */
4130 do {
Tejun Heo450ee0c2017-05-15 09:34:03 -04004131 cset = css_task_iter_next_css_set(it);
4132 if (!cset) {
Tejun Heoecb9d532015-10-15 16:41:52 -04004133 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04004134 return;
4135 }
Tejun Heo0de09422015-10-15 16:41:49 -04004136 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05004137
Tejun Heoc7561122014-02-25 10:04:01 -05004138 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004139 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05004140 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004141 it->task_pos = cset->mg_tasks.next;
4142
4143 it->tasks_head = &cset->tasks;
4144 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04004145
4146 /*
4147 * We don't keep css_sets locked across iteration steps and thus
4148 * need to take steps to ensure that iteration can be resumed after
4149 * the lock is re-acquired. Iteration is performed at two levels -
4150 * css_sets and tasks in them.
4151 *
4152 * Once created, a css_set never leaves its cgroup lists, so a
4153 * pinned css_set is guaranteed to stay put and we can resume
4154 * iteration afterwards.
4155 *
4156 * Tasks may leave @cset across iteration steps. This is resolved
4157 * by registering each iterator with the css_set currently being
4158 * walked and making css_set_move_task() advance iterators whose
4159 * next task is leaving.
4160 */
4161 if (it->cur_cset) {
4162 list_del(&it->iters_node);
4163 put_css_set_locked(it->cur_cset);
4164 }
4165 get_css_set(cset);
4166 it->cur_cset = cset;
4167 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04004168}
4169
Tejun Heoecb9d532015-10-15 16:41:52 -04004170static void css_task_iter_advance(struct css_task_iter *it)
4171{
Tejun Heo74d08332017-12-20 07:09:19 -08004172 struct list_head *next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004173
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004174 lockdep_assert_held(&css_set_lock);
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004175repeat:
Tejun Heoecb9d532015-10-15 16:41:52 -04004176 /*
4177 * Advance iterator to find next entry. cset->tasks is consumed
4178 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
4179 * next cset.
4180 */
Tejun Heo74d08332017-12-20 07:09:19 -08004181 next = it->task_pos->next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004182
Tejun Heo74d08332017-12-20 07:09:19 -08004183 if (next == it->tasks_head)
4184 next = it->mg_tasks_head->next;
Tejun Heoecb9d532015-10-15 16:41:52 -04004185
Tejun Heo74d08332017-12-20 07:09:19 -08004186 if (next == it->mg_tasks_head)
Tejun Heoecb9d532015-10-15 16:41:52 -04004187 css_task_iter_advance_css_set(it);
4188 else
Tejun Heo74d08332017-12-20 07:09:19 -08004189 it->task_pos = next;
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004190
4191 /* if PROCS, skip over tasks which aren't group leaders */
4192 if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos &&
4193 !thread_group_leader(list_entry(it->task_pos, struct task_struct,
4194 cg_list)))
4195 goto repeat;
Tejun Heoecb9d532015-10-15 16:41:52 -04004196}
4197
Tejun Heo0942eee2013-08-08 20:11:26 -04004198/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004199 * css_task_iter_start - initiate task iteration
4200 * @css: the css to walk tasks of
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004201 * @flags: CSS_TASK_ITER_* flags
Tejun Heo0942eee2013-08-08 20:11:26 -04004202 * @it: the task iterator to use
4203 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004204 * Initiate iteration through the tasks of @css. The caller can call
4205 * css_task_iter_next() to walk through the tasks until the function
4206 * returns NULL. On completion of iteration, css_task_iter_end() must be
4207 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04004208 */
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004209void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
Tejun Heo72ec7022013-08-08 20:11:26 -04004210 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004211{
Tejun Heo56fde9e2014-02-13 06:58:38 -05004212 /* no one should try to iterate before mounting cgroups */
4213 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07004214
Tejun Heoed27b9f2015-10-15 16:41:52 -04004215 memset(it, 0, sizeof(*it));
4216
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004217 spin_lock_irq(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004218
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004219 it->ss = css->ss;
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004220 it->flags = flags;
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004221
4222 if (it->ss)
4223 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4224 else
4225 it->cset_pos = &css->cgroup->cset_links;
4226
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004227 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004228
Tejun Heoecb9d532015-10-15 16:41:52 -04004229 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004230
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004231 spin_unlock_irq(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004232}
Paul Menage817929e2007-10-18 23:39:36 -07004233
Tejun Heo0942eee2013-08-08 20:11:26 -04004234/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004235 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004236 * @it: the task iterator being iterated
4237 *
4238 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004239 * initialized via css_task_iter_start(). Returns NULL when the iteration
4240 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004241 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004242struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004243{
Tejun Heod5745672015-10-29 11:43:05 +09004244 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004245 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004246 it->cur_task = NULL;
4247 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004248
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004249 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004250
Tejun Heod5745672015-10-29 11:43:05 +09004251 if (it->task_pos) {
4252 it->cur_task = list_entry(it->task_pos, struct task_struct,
4253 cg_list);
4254 get_task_struct(it->cur_task);
4255 css_task_iter_advance(it);
4256 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004257
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004258 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004259
4260 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004261}
4262
Tejun Heo0942eee2013-08-08 20:11:26 -04004263/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004264 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004265 * @it: the task iterator to finish
4266 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004267 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004268 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004269void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004270{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004271 if (it->cur_cset) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004272 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004273 list_del(&it->iters_node);
4274 put_css_set_locked(it->cur_cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004275 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004276 }
4277
Tejun Heo450ee0c2017-05-15 09:34:03 -04004278 if (it->cur_dcset)
4279 put_css_set(it->cur_dcset);
4280
Tejun Heoed27b9f2015-10-15 16:41:52 -04004281 if (it->cur_task)
4282 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004283}
4284
Tejun Heob4b90a82016-12-27 14:49:04 -05004285static void cgroup_procs_release(struct kernfs_open_file *of)
Tejun Heo8cc99342013-04-07 09:29:50 -07004286{
Tejun Heob4b90a82016-12-27 14:49:04 -05004287 if (of->priv) {
4288 css_task_iter_end(of->priv);
4289 kfree(of->priv);
4290 }
4291}
4292
4293static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4294{
4295 struct kernfs_open_file *of = s->private;
4296 struct css_task_iter *it = of->priv;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004297
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004298 return css_task_iter_next(it);
Tejun Heo8cc99342013-04-07 09:29:50 -07004299}
4300
Tejun Heo8cfd8142017-07-21 11:14:51 -04004301static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
4302 unsigned int iter_flags)
Ben Blumd1d9fd32009-09-23 15:56:28 -07004303{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004304 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004305 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heob4b90a82016-12-27 14:49:04 -05004306 struct css_task_iter *it = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004307
4308 /*
Tejun Heob4b90a82016-12-27 14:49:04 -05004309 * When a seq_file is seeked, it's always traversed sequentially
4310 * from position 0, so we can simply keep iterating on !0 *pos.
Tejun Heo4bac00d2013-11-29 10:42:59 -05004311 */
Tejun Heob4b90a82016-12-27 14:49:04 -05004312 if (!it) {
4313 if (WARN_ON_ONCE((*pos)++))
4314 return ERR_PTR(-EINVAL);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004315
Tejun Heob4b90a82016-12-27 14:49:04 -05004316 it = kzalloc(sizeof(*it), GFP_KERNEL);
4317 if (!it)
4318 return ERR_PTR(-ENOMEM);
4319 of->priv = it;
Tejun Heo450ee0c2017-05-15 09:34:03 -04004320 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heob4b90a82016-12-27 14:49:04 -05004321 } else if (!(*pos)++) {
4322 css_task_iter_end(it);
Tejun Heo450ee0c2017-05-15 09:34:03 -04004323 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004324 }
Tejun Heo4bac00d2013-11-29 10:42:59 -05004325
Tejun Heob4b90a82016-12-27 14:49:04 -05004326 return cgroup_procs_next(s, NULL, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07004327}
4328
Tejun Heo8cfd8142017-07-21 11:14:51 -04004329static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
4330{
4331 struct cgroup *cgrp = seq_css(s)->cgroup;
4332
4333 /*
4334 * All processes of a threaded subtree belong to the domain cgroup
4335 * of the subtree. Only threads can be distributed across the
4336 * subtree. Reject reads on cgroup.procs in the subtree proper.
4337 * They're always empty anyway.
4338 */
4339 if (cgroup_is_threaded(cgrp))
4340 return ERR_PTR(-EOPNOTSUPP);
4341
4342 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
4343 CSS_TASK_ITER_THREADED);
4344}
4345
Tejun Heob4b90a82016-12-27 14:49:04 -05004346static int cgroup_procs_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004347{
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004348 seq_printf(s, "%d\n", task_pid_vnr(v));
Daniel Lezcano97978e62010-10-27 15:33:35 -07004349 return 0;
4350}
4351
Tejun Heo715c8092017-05-15 09:34:00 -04004352static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
4353 struct cgroup *dst_cgrp,
4354 struct super_block *sb)
4355{
4356 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
4357 struct cgroup *com_cgrp = src_cgrp;
4358 struct inode *inode;
4359 int ret;
4360
4361 lockdep_assert_held(&cgroup_mutex);
4362
4363 /* find the common ancestor */
4364 while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
4365 com_cgrp = cgroup_parent(com_cgrp);
4366
4367 /* %current should be authorized to migrate to the common ancestor */
4368 inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn);
4369 if (!inode)
4370 return -ENOMEM;
4371
4372 ret = inode_permission(inode, MAY_WRITE);
4373 iput(inode);
4374 if (ret)
4375 return ret;
4376
4377 /*
4378 * If namespaces are delegation boundaries, %current must be able
4379 * to see both source and destination cgroups from its namespace.
4380 */
4381 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
4382 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
4383 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
4384 return -ENOENT;
4385
4386 return 0;
4387}
4388
4389static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
4390 char *buf, size_t nbytes, loff_t off)
4391{
4392 struct cgroup *src_cgrp, *dst_cgrp;
4393 struct task_struct *task;
4394 ssize_t ret;
4395
4396 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4397 if (!dst_cgrp)
4398 return -ENODEV;
4399
4400 task = cgroup_procs_write_start(buf, true);
4401 ret = PTR_ERR_OR_ZERO(task);
4402 if (ret)
4403 goto out_unlock;
4404
4405 /* find the source cgroup */
4406 spin_lock_irq(&css_set_lock);
4407 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4408 spin_unlock_irq(&css_set_lock);
4409
4410 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4411 of->file->f_path.dentry->d_sb);
4412 if (ret)
4413 goto out_finish;
4414
4415 ret = cgroup_attach_task(dst_cgrp, task, true);
4416
4417out_finish:
4418 cgroup_procs_write_finish(task);
4419out_unlock:
4420 cgroup_kn_unlock(of->kn);
4421
4422 return ret ?: nbytes;
4423}
4424
Tejun Heo8cfd8142017-07-21 11:14:51 -04004425static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
4426{
4427 return __cgroup_procs_start(s, pos, 0);
4428}
4429
4430static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
4431 char *buf, size_t nbytes, loff_t off)
4432{
4433 struct cgroup *src_cgrp, *dst_cgrp;
4434 struct task_struct *task;
4435 ssize_t ret;
4436
4437 buf = strstrip(buf);
4438
4439 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4440 if (!dst_cgrp)
4441 return -ENODEV;
4442
4443 task = cgroup_procs_write_start(buf, false);
4444 ret = PTR_ERR_OR_ZERO(task);
4445 if (ret)
4446 goto out_unlock;
4447
4448 /* find the source cgroup */
4449 spin_lock_irq(&css_set_lock);
4450 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4451 spin_unlock_irq(&css_set_lock);
4452
4453 /* thread migrations follow the cgroup.procs delegation rule */
4454 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4455 of->file->f_path.dentry->d_sb);
4456 if (ret)
4457 goto out_finish;
4458
4459 /* and must be contained in the same domain */
4460 ret = -EOPNOTSUPP;
4461 if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)
4462 goto out_finish;
4463
4464 ret = cgroup_attach_task(dst_cgrp, task, false);
4465
4466out_finish:
4467 cgroup_procs_write_finish(task);
4468out_unlock:
4469 cgroup_kn_unlock(of->kn);
4470
4471 return ret ?: nbytes;
4472}
4473
Tejun Heoa14c6872014-07-15 11:05:09 -04004474/* cgroup core interface files for the default hierarchy */
Tejun Heod62beb72016-12-27 14:49:08 -05004475static struct cftype cgroup_base_files[] = {
Tejun Heoa14c6872014-07-15 11:05:09 -04004476 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004477 .name = "cgroup.type",
4478 .flags = CFTYPE_NOT_ON_ROOT,
4479 .seq_show = cgroup_type_show,
4480 .write = cgroup_type_write,
4481 },
4482 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004483 .name = "cgroup.procs",
Tejun Heo5136f632017-06-27 14:30:28 -04004484 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004485 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heob4b90a82016-12-27 14:49:04 -05004486 .release = cgroup_procs_release,
4487 .seq_start = cgroup_procs_start,
4488 .seq_next = cgroup_procs_next,
4489 .seq_show = cgroup_procs_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004490 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004491 },
4492 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004493 .name = "cgroup.threads",
Roman Gushchin4f584242018-01-10 04:35:12 -08004494 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo8cfd8142017-07-21 11:14:51 -04004495 .release = cgroup_procs_release,
4496 .seq_start = cgroup_threads_start,
4497 .seq_next = cgroup_procs_next,
4498 .seq_show = cgroup_procs_show,
4499 .write = cgroup_threads_write,
4500 },
4501 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004502 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04004503 .seq_show = cgroup_controllers_show,
4504 },
4505 {
4506 .name = "cgroup.subtree_control",
Tejun Heo5136f632017-06-27 14:30:28 -04004507 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heoa14c6872014-07-15 11:05:09 -04004508 .seq_show = cgroup_subtree_control_show,
4509 .write = cgroup_subtree_control_write,
4510 },
4511 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004512 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004513 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004514 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004515 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004516 },
Roman Gushchin1a926e02017-07-28 18:28:44 +01004517 {
4518 .name = "cgroup.max.descendants",
4519 .seq_show = cgroup_max_descendants_show,
4520 .write = cgroup_max_descendants_write,
4521 },
4522 {
4523 .name = "cgroup.max.depth",
4524 .seq_show = cgroup_max_depth_show,
4525 .write = cgroup_max_depth_write,
4526 },
Roman Gushchinec392252017-08-02 17:55:31 +01004527 {
4528 .name = "cgroup.stat",
Tejun Heo3e489302017-08-11 05:49:01 -07004529 .seq_show = cgroup_stat_show,
Roman Gushchinec392252017-08-02 17:55:31 +01004530 },
Tejun Heod41bf8c2017-10-23 16:18:27 -07004531 {
4532 .name = "cpu.stat",
4533 .flags = CFTYPE_NOT_ON_ROOT,
4534 .seq_show = cpu_stat_show,
4535 },
Tejun Heoa14c6872014-07-15 11:05:09 -04004536 { } /* terminate */
4537};
4538
Tejun Heo0c21ead2013-08-13 20:22:51 -04004539/*
4540 * css destruction is four-stage process.
4541 *
4542 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4543 * Implemented in kill_css().
4544 *
4545 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004546 * and thus css_tryget_online() is guaranteed to fail, the css can be
4547 * offlined by invoking offline_css(). After offlining, the base ref is
4548 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004549 *
4550 * 3. When the percpu_ref reaches zero, the only possible remaining
4551 * accessors are inside RCU read sections. css_release() schedules the
4552 * RCU callback.
4553 *
4554 * 4. After the grace period, the css can be freed. Implemented in
4555 * css_free_work_fn().
4556 *
4557 * It is actually hairier because both step 2 and 4 require process context
4558 * and thus involve punting to css->destroy_work adding two additional
4559 * steps to the already complex sequence.
4560 */
Tejun Heo8f36aae2018-03-14 12:45:14 -07004561static void css_free_rwork_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004562{
Tejun Heo8f36aae2018-03-14 12:45:14 -07004563 struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
4564 struct cgroup_subsys_state, destroy_rwork);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004565 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004566 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004567
Tejun Heo9a1049d2014-06-28 08:10:14 -04004568 percpu_ref_exit(&css->refcnt);
4569
Vladimir Davydov01e58652015-02-12 14:59:26 -08004570 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004571 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004572 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004573 int id = css->id;
4574
Vladimir Davydov01e58652015-02-12 14:59:26 -08004575 ss->css_free(css);
4576 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004577 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004578
4579 if (parent)
4580 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004581 } else {
4582 /* cgroup free path */
4583 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heod62beb72016-12-27 14:49:08 -05004584 cgroup1_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004585 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004586
Tejun Heod51f39b2014-05-16 13:22:48 -04004587 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004588 /*
4589 * We get a ref to the parent, and put the ref when
4590 * this cgroup is being freed, so it's guaranteed
4591 * that the parent won't be destroyed before its
4592 * children.
4593 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004594 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004595 kernfs_put(cgrp->kn);
Tejun Heo041cd642017-09-25 08:12:05 -07004596 if (cgroup_on_dfl(cgrp))
Tejun Heoc58632b2018-04-26 14:29:04 -07004597 cgroup_rstat_exit(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04004598 kfree(cgrp);
4599 } else {
4600 /*
4601 * This is root cgroup's refcnt reaching zero,
4602 * which indicates that the root should be
4603 * released.
4604 */
4605 cgroup_destroy_root(cgrp->root);
4606 }
4607 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004608}
4609
Tejun Heo25e15d82014-05-14 09:15:02 -04004610static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004611{
4612 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004613 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004614 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004615 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004616
Tejun Heo1fed1b22014-05-16 13:22:49 -04004617 mutex_lock(&cgroup_mutex);
4618
Tejun Heode3f0342014-05-16 13:22:49 -04004619 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004620 list_del_rcu(&css->sibling);
4621
Tejun Heo9d755d32014-05-14 09:15:02 -04004622 if (ss) {
4623 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004624 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004625 if (ss->css_released)
4626 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004627 } else {
Roman Gushchin0679dee2017-08-02 17:55:29 +01004628 struct cgroup *tcgrp;
4629
Tejun Heo9d755d32014-05-14 09:15:02 -04004630 /* cgroup release path */
Tejun Heoed1777d2016-08-10 11:23:44 -04004631 trace_cgroup_release(cgrp);
4632
Tejun Heo041cd642017-09-25 08:12:05 -07004633 if (cgroup_on_dfl(cgrp))
Tejun Heoc58632b2018-04-26 14:29:04 -07004634 cgroup_rstat_flush(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004635
Roman Gushchin0679dee2017-08-02 17:55:29 +01004636 for (tcgrp = cgroup_parent(cgrp); tcgrp;
4637 tcgrp = cgroup_parent(tcgrp))
4638 tcgrp->nr_dying_descendants--;
4639
Tejun Heo9d755d32014-05-14 09:15:02 -04004640 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4641 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004642
4643 /*
4644 * There are two control paths which try to determine
4645 * cgroup from dentry without going through kernfs -
4646 * cgroupstats_build() and css_tryget_online_from_dir().
4647 * Those are supported by RCU protecting clearing of
4648 * cgrp->kn->priv backpointer.
4649 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004650 if (cgrp->kn)
4651 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4652 NULL);
Daniel Mack30070982016-11-23 16:52:26 +01004653
4654 cgroup_bpf_put(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04004655 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004656
Tejun Heo1fed1b22014-05-16 13:22:49 -04004657 mutex_unlock(&cgroup_mutex);
4658
Tejun Heo8f36aae2018-03-14 12:45:14 -07004659 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4660 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heod3daf282013-06-13 19:39:16 -07004661}
4662
Tejun Heo48ddbe12012-04-01 12:09:56 -07004663static void css_release(struct percpu_ref *ref)
4664{
4665 struct cgroup_subsys_state *css =
4666 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004667
Tejun Heo25e15d82014-05-14 09:15:02 -04004668 INIT_WORK(&css->destroy_work, css_release_work_fn);
4669 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004670}
4671
Tejun Heoddfcada2014-05-04 15:09:14 -04004672static void init_and_link_css(struct cgroup_subsys_state *css,
4673 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004675 lockdep_assert_held(&cgroup_mutex);
4676
Tejun Heoa590b902017-04-28 15:14:55 -04004677 cgroup_get_live(cgrp);
Tejun Heoddfcada2014-05-04 15:09:14 -04004678
Tejun Heod5c419b2014-05-16 13:22:48 -04004679 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004680 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004681 css->ss = ss;
Tejun Heo8fa3b8d2016-05-26 15:42:13 -04004682 css->id = -1;
Tejun Heod5c419b2014-05-16 13:22:48 -04004683 INIT_LIST_HEAD(&css->sibling);
4684 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004685 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05004686 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004687
Tejun Heod51f39b2014-05-16 13:22:48 -04004688 if (cgroup_parent(cgrp)) {
4689 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004690 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004691 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004692
Tejun Heoca8bdca2013-08-26 18:40:56 -04004693 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694}
4695
Li Zefan2a4ac632013-07-31 16:16:40 +08004696/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004697static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004698{
Tejun Heo623f9262013-08-13 11:01:55 -04004699 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004700 int ret = 0;
4701
Tejun Heoa31f2d32012-11-19 08:13:37 -08004702 lockdep_assert_held(&cgroup_mutex);
4703
Tejun Heo92fb9742012-11-19 08:13:38 -08004704 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004705 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004706 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004707 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004708 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05004709
4710 atomic_inc(&css->online_cnt);
4711 if (css->parent)
4712 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04004713 }
Tejun Heob1929db2012-11-19 08:13:38 -08004714 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004715}
4716
Li Zefan2a4ac632013-07-31 16:16:40 +08004717/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004718static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004719{
Tejun Heo623f9262013-08-13 11:01:55 -04004720 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004721
4722 lockdep_assert_held(&cgroup_mutex);
4723
4724 if (!(css->flags & CSS_ONLINE))
4725 return;
4726
Li Zefand7eeac12013-03-12 15:35:59 -07004727 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004728 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004729
Tejun Heoeb954192013-08-08 20:11:23 -04004730 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004731 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004732
4733 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004734}
4735
Tejun Heoc81c925a2013-12-06 15:11:56 -05004736/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004737 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05004738 * @cgrp: the cgroup new css will be associated with
4739 * @ss: the subsys of new css
4740 *
4741 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004742 * css is online and installed in @cgrp. This function doesn't create the
4743 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004744 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004745static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4746 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004747{
Tejun Heod51f39b2014-05-16 13:22:48 -04004748 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004749 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004750 struct cgroup_subsys_state *css;
4751 int err;
4752
Tejun Heoc81c925a2013-12-06 15:11:56 -05004753 lockdep_assert_held(&cgroup_mutex);
4754
Tejun Heo1fed1b22014-05-16 13:22:49 -04004755 css = ss->css_alloc(parent_css);
Tejun Heoe7e15b82016-06-21 13:06:24 -04004756 if (!css)
4757 css = ERR_PTR(-ENOMEM);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004758 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004759 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004760
Tejun Heoddfcada2014-05-04 15:09:14 -04004761 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004762
Tejun Heo2aad2a82014-09-24 13:31:50 -04004763 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004764 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004765 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004766
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004767 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004768 if (err < 0)
Wenwei Taob00c52d2016-05-13 22:59:20 +08004769 goto err_free_css;
Tejun Heo15a4c832014-05-04 15:09:14 -04004770 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004771
Tejun Heo15a4c832014-05-04 15:09:14 -04004772 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004773 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004774 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004775
4776 err = online_css(css);
4777 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004778 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004779
Tejun Heoc81c925a2013-12-06 15:11:56 -05004780 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004781 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004782 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 -04004783 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004784 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004785 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004786 ss->warned_broken_hierarchy = true;
4787 }
4788
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004789 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004790
Tejun Heo1fed1b22014-05-16 13:22:49 -04004791err_list_del:
4792 list_del_rcu(&css->sibling);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004793err_free_css:
Tejun Heo8f36aae2018-03-14 12:45:14 -07004794 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4795 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004796 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004797}
4798
Tejun Heo07cd1292017-01-26 16:47:28 -05004799/*
4800 * The returned cgroup is fully initialized including its control mask, but
4801 * it isn't associated with its kernfs_node and doesn't have the control
4802 * mask applied.
4803 */
Tejun Heoa5bca212016-03-03 09:57:58 -05004804static struct cgroup *cgroup_create(struct cgroup *parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004805{
Tejun Heoa5bca212016-03-03 09:57:58 -05004806 struct cgroup_root *root = parent->root;
Tejun Heoa5bca212016-03-03 09:57:58 -05004807 struct cgroup *cgrp, *tcgrp;
4808 int level = parent->level + 1;
Tejun Heo03970d32016-03-03 09:58:00 -05004809 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004810
Tejun Heo0a950f62012-11-19 09:02:12 -08004811 /* allocate the cgroup and its ID, 0 is reserved for the root */
Tejun Heob11cfb52015-11-20 15:55:52 -05004812 cgrp = kzalloc(sizeof(*cgrp) +
4813 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05004814 if (!cgrp)
4815 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004816
Tejun Heo2aad2a82014-09-24 13:31:50 -04004817 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004818 if (ret)
4819 goto out_free_cgrp;
4820
Tejun Heo041cd642017-09-25 08:12:05 -07004821 if (cgroup_on_dfl(parent)) {
Tejun Heoc58632b2018-04-26 14:29:04 -07004822 ret = cgroup_rstat_init(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07004823 if (ret)
4824 goto out_cancel_ref;
4825 }
4826
Li Zefan0ab02ca2014-02-11 16:05:46 +08004827 /*
4828 * Temporarily set the pointer to NULL, so idr_find() won't return
4829 * a half-baked cgroup.
4830 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004831 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004832 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004833 ret = -ENOMEM;
Tejun Heo041cd642017-09-25 08:12:05 -07004834 goto out_stat_exit;
Tejun Heo976c06b2012-11-05 09:16:59 -08004835 }
4836
Paul Menagecc31edc2008-10-18 20:28:04 -07004837 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004838
Tejun Heo9d800df2014-05-14 09:15:00 -04004839 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004840 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004841 cgrp->level = level;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07004842 ret = cgroup_bpf_inherit(cgrp);
4843 if (ret)
4844 goto out_idr_free;
Tejun Heob11cfb52015-11-20 15:55:52 -05004845
Roman Gushchin0679dee2017-08-02 17:55:29 +01004846 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
Tejun Heob11cfb52015-11-20 15:55:52 -05004847 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004848
Roman Gushchin0679dee2017-08-02 17:55:29 +01004849 if (tcgrp != cgrp)
4850 tcgrp->nr_descendants++;
4851 }
4852
Li Zefanb6abdb02008-03-04 14:28:19 -08004853 if (notify_on_release(parent))
4854 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4855
Tejun Heo2260e7f2012-11-19 08:13:38 -08004856 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4857 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004858
Tejun Heo0cb51d72014-05-16 13:22:49 -04004859 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004860
Tejun Heo4e139af2012-11-19 08:13:36 -08004861 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004862 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004863 atomic_inc(&root->nr_cgrps);
Tejun Heoa590b902017-04-28 15:14:55 -04004864 cgroup_get_live(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004865
Tejun Heo0d802552013-12-06 15:11:56 -05004866 /*
4867 * @cgrp is now fully operational. If something fails after this
4868 * point, it'll be released via the normal destruction path.
4869 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004870 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004871
Tejun Heobd53d612014-04-23 11:13:16 -04004872 /*
4873 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004874 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004875 */
Tejun Heo03970d32016-03-03 09:58:00 -05004876 if (!cgroup_on_dfl(cgrp))
Tejun Heo5531dc92016-03-03 09:57:58 -05004877 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo03970d32016-03-03 09:58:00 -05004878
4879 cgroup_propagate_control(cgrp);
4880
Tejun Heoa5bca212016-03-03 09:57:58 -05004881 return cgrp;
4882
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07004883out_idr_free:
4884 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo041cd642017-09-25 08:12:05 -07004885out_stat_exit:
4886 if (cgroup_on_dfl(parent))
Tejun Heoc58632b2018-04-26 14:29:04 -07004887 cgroup_rstat_exit(cgrp);
Tejun Heoa5bca212016-03-03 09:57:58 -05004888out_cancel_ref:
4889 percpu_ref_exit(&cgrp->self.refcnt);
4890out_free_cgrp:
4891 kfree(cgrp);
4892 return ERR_PTR(ret);
Tejun Heoa5bca212016-03-03 09:57:58 -05004893}
4894
Roman Gushchin1a926e02017-07-28 18:28:44 +01004895static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
4896{
4897 struct cgroup *cgroup;
4898 int ret = false;
4899 int level = 1;
4900
4901 lockdep_assert_held(&cgroup_mutex);
4902
4903 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
4904 if (cgroup->nr_descendants >= cgroup->max_descendants)
4905 goto fail;
4906
4907 if (level > cgroup->max_depth)
4908 goto fail;
4909
4910 level++;
4911 }
4912
4913 ret = true;
4914fail:
4915 return ret;
4916}
4917
Tejun Heo1592c9b2016-12-27 14:49:08 -05004918int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
Tejun Heoa5bca212016-03-03 09:57:58 -05004919{
4920 struct cgroup *parent, *cgrp;
Tejun Heoa5bca212016-03-03 09:57:58 -05004921 struct kernfs_node *kn;
Tejun Heo03970d32016-03-03 09:58:00 -05004922 int ret;
Tejun Heoa5bca212016-03-03 09:57:58 -05004923
4924 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4925 if (strchr(name, '\n'))
4926 return -EINVAL;
4927
Tejun Heo945ba192016-03-03 09:58:00 -05004928 parent = cgroup_kn_lock_live(parent_kn, false);
Tejun Heoa5bca212016-03-03 09:57:58 -05004929 if (!parent)
4930 return -ENODEV;
4931
Roman Gushchin1a926e02017-07-28 18:28:44 +01004932 if (!cgroup_check_hierarchy_limits(parent)) {
4933 ret = -EAGAIN;
4934 goto out_unlock;
4935 }
4936
Tejun Heoa5bca212016-03-03 09:57:58 -05004937 cgrp = cgroup_create(parent);
4938 if (IS_ERR(cgrp)) {
4939 ret = PTR_ERR(cgrp);
4940 goto out_unlock;
4941 }
4942
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004943 /* create the directory */
4944 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
4945 if (IS_ERR(kn)) {
4946 ret = PTR_ERR(kn);
4947 goto out_destroy;
4948 }
4949 cgrp->kn = kn;
4950
4951 /*
4952 * This extra ref will be put in cgroup_free_fn() and guarantees
4953 * that @cgrp->kn is always accessible.
4954 */
4955 kernfs_get(kn);
4956
4957 ret = cgroup_kn_set_ugid(kn);
4958 if (ret)
4959 goto out_destroy;
4960
Tejun Heo334c3672016-03-03 09:58:01 -05004961 ret = css_populate_dir(&cgrp->self);
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004962 if (ret)
4963 goto out_destroy;
4964
Tejun Heo03970d32016-03-03 09:58:00 -05004965 ret = cgroup_apply_control_enable(cgrp);
4966 if (ret)
4967 goto out_destroy;
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004968
Tejun Heoed1777d2016-08-10 11:23:44 -04004969 trace_cgroup_mkdir(cgrp);
4970
Tejun Heo195e9b6c2016-03-03 09:57:58 -05004971 /* let's create and online css's */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004972 kernfs_activate(kn);
4973
Tejun Heoba0f4d72014-05-13 12:19:22 -04004974 ret = 0;
4975 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004976
Tejun Heoa5bca212016-03-03 09:57:58 -05004977out_destroy:
4978 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004979out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004980 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004981 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004982}
4983
Tejun Heo223dbc32013-08-13 20:22:50 -04004984/*
4985 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004986 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4987 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004988 */
4989static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004990{
Tejun Heo223dbc32013-08-13 20:22:50 -04004991 struct cgroup_subsys_state *css =
4992 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004993
Tejun Heof20104d2013-08-13 20:22:50 -04004994 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004995
Tejun Heoaa226ff2016-01-21 15:31:11 -05004996 do {
4997 offline_css(css);
4998 css_put(css);
4999 /* @css can't go away while we're holding cgroup_mutex */
5000 css = css->parent;
5001 } while (css && atomic_dec_and_test(&css->online_cnt));
5002
5003 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005004}
5005
Tejun Heo223dbc32013-08-13 20:22:50 -04005006/* css kill confirmation processing requires process context, bounce */
5007static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005008{
5009 struct cgroup_subsys_state *css =
5010 container_of(ref, struct cgroup_subsys_state, refcnt);
5011
Tejun Heoaa226ff2016-01-21 15:31:11 -05005012 if (atomic_dec_and_test(&css->online_cnt)) {
5013 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5014 queue_work(cgroup_destroy_wq, &css->destroy_work);
5015 }
Tejun Heod3daf282013-06-13 19:39:16 -07005016}
5017
Tejun Heof392e512014-04-23 11:13:14 -04005018/**
5019 * kill_css - destroy a css
5020 * @css: css to destroy
5021 *
5022 * This function initiates destruction of @css by removing cgroup interface
5023 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005024 * asynchronously once css_tryget_online() is guaranteed to fail and when
5025 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005026 */
5027static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005028{
Tejun Heo01f64742014-05-13 12:19:23 -04005029 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005030
Waiman Long33c35aa2017-05-15 09:34:06 -04005031 if (css->flags & CSS_DYING)
5032 return;
5033
5034 css->flags |= CSS_DYING;
5035
Tejun Heo2bd59d42014-02-11 11:52:49 -05005036 /*
5037 * This must happen before css is disassociated with its cgroup.
5038 * See seq_css() for details.
5039 */
Tejun Heo334c3672016-03-03 09:58:01 -05005040 css_clear_dir(css);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005041
Tejun Heoedae0c32013-08-13 20:22:51 -04005042 /*
5043 * Killing would put the base ref, but we need to keep it alive
5044 * until after ->css_offline().
5045 */
5046 css_get(css);
5047
5048 /*
5049 * cgroup core guarantees that, by the time ->css_offline() is
5050 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005051 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005052 * proceed to offlining css's because percpu_ref_kill() doesn't
5053 * guarantee that the ref is seen as killed on all CPUs on return.
5054 *
5055 * Use percpu_ref_kill_and_confirm() to get notifications as each
5056 * css is confirmed to be seen as killed on all CPUs.
5057 */
5058 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005059}
5060
5061/**
5062 * cgroup_destroy_locked - the first stage of cgroup destruction
5063 * @cgrp: cgroup to be destroyed
5064 *
5065 * css's make use of percpu refcnts whose killing latency shouldn't be
5066 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005067 * guarantee that css_tryget_online() won't succeed by the time
5068 * ->css_offline() is invoked. To satisfy all the requirements,
5069 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005070 *
5071 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5072 * userland visible parts and start killing the percpu refcnts of
5073 * css's. Set up so that the next stage will be kicked off once all
5074 * the percpu refcnts are confirmed to be killed.
5075 *
5076 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5077 * rest of destruction. Once all cgroup references are gone, the
5078 * cgroup is RCU-freed.
5079 *
5080 * This function implements s1. After this step, @cgrp is gone as far as
5081 * the userland is concerned and a new cgroup with the same name may be
5082 * created. As cgroup doesn't care about the names internally, this
5083 * doesn't cause any problem.
5084 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005085static int cgroup_destroy_locked(struct cgroup *cgrp)
5086 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005087{
Roman Gushchin0679dee2017-08-02 17:55:29 +01005088 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005089 struct cgroup_subsys_state *css;
Tejun Heo2b021cb2016-03-15 20:43:04 -04005090 struct cgrp_cset_link *link;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005091 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005092
Tejun Heo42809dd2012-11-19 08:13:37 -08005093 lockdep_assert_held(&cgroup_mutex);
5094
Tejun Heo91486f62015-10-15 16:41:51 -04005095 /*
5096 * Only migration can raise populated from zero and we're already
5097 * holding cgroup_mutex.
5098 */
5099 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005100 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005101
Tejun Heo1a90dd52012-11-05 09:16:59 -08005102 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005103 * Make sure there's no live children. We can't test emptiness of
5104 * ->self.children as dead children linger on it while being
5105 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005106 */
Tejun Heof3d46502014-05-16 13:22:52 -04005107 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005108 return -EBUSY;
5109
5110 /*
Tejun Heo2b021cb2016-03-15 20:43:04 -04005111 * Mark @cgrp and the associated csets dead. The former prevents
5112 * further task migration and child creation by disabling
5113 * cgroup_lock_live_group(). The latter makes the csets ignored by
5114 * the migration path.
Tejun Heo455050d2013-06-13 19:27:41 -07005115 */
Tejun Heo184faf32014-05-16 13:22:51 -04005116 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005117
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005118 spin_lock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005119 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5120 link->cset->dead = true;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005121 spin_unlock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005122
Tejun Heo249f3462014-05-14 09:15:01 -04005123 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005124 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005125 kill_css(css);
5126
Tejun Heo5faaf052018-04-26 14:29:04 -07005127 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5128 css_clear_dir(&cgrp->self);
Tejun Heo01f64742014-05-13 12:19:23 -04005129 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005130
Tejun Heo454000a2017-05-15 09:34:02 -04005131 if (parent && cgroup_is_threaded(cgrp))
5132 parent->nr_threaded_children--;
5133
Roman Gushchin0679dee2017-08-02 17:55:29 +01005134 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
5135 tcgrp->nr_descendants--;
5136 tcgrp->nr_dying_descendants++;
5137 }
5138
Roman Gushchin5a621e62017-08-02 17:55:32 +01005139 cgroup1_check_for_release(parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005140
Tejun Heo249f3462014-05-14 09:15:01 -04005141 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005142 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005143
Tejun Heoea15f8c2013-06-13 19:27:42 -07005144 return 0;
5145};
5146
Tejun Heo1592c9b2016-12-27 14:49:08 -05005147int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005148{
Tejun Heoa9746d82014-05-13 12:19:22 -04005149 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005150 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005151
Tejun Heo945ba192016-03-03 09:58:00 -05005152 cgrp = cgroup_kn_lock_live(kn, false);
Tejun Heoa9746d82014-05-13 12:19:22 -04005153 if (!cgrp)
5154 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005155
Tejun Heoa9746d82014-05-13 12:19:22 -04005156 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005157
Tejun Heoed1777d2016-08-10 11:23:44 -04005158 if (!ret)
5159 trace_cgroup_rmdir(cgrp);
5160
Tejun Heoa9746d82014-05-13 12:19:22 -04005161 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005162 return ret;
5163}
5164
Tejun Heo2bd59d42014-02-11 11:52:49 -05005165static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
Tejun Heo5136f632017-06-27 14:30:28 -04005166 .show_options = cgroup_show_options,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005167 .remount_fs = cgroup_remount,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005168 .mkdir = cgroup_mkdir,
5169 .rmdir = cgroup_rmdir,
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05005170 .show_path = cgroup_show_path,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005171};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005172
Tejun Heo15a4c832014-05-04 15:09:14 -04005173static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005174{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005175 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005176
Tejun Heoa5ae9892015-12-29 14:53:56 -05005177 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005178
Tejun Heo648bb562012-11-19 08:13:36 -08005179 mutex_lock(&cgroup_mutex);
5180
Tejun Heo15a4c832014-05-04 15:09:14 -04005181 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005182 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005183
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005184 /* Create the root cgroup state for this subsystem */
5185 ss->root = &cgrp_dfl_root;
5186 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005187 /* We don't handle early failures gracefully */
5188 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005189 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005190
5191 /*
5192 * Root csses are never destroyed and we can't initialize
5193 * percpu_ref during early init. Disable refcnting.
5194 */
5195 css->flags |= CSS_NO_REF;
5196
Tejun Heo15a4c832014-05-04 15:09:14 -04005197 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005198 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005199 css->id = 1;
5200 } else {
5201 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5202 BUG_ON(css->id < 0);
5203 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005204
Li Zefane8d55fd2008-04-29 01:00:13 -07005205 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005206 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005207 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005208 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005209 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005210
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005211 have_fork_callback |= (bool)ss->fork << ss->id;
5212 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005213 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005214 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005215
Li Zefane8d55fd2008-04-29 01:00:13 -07005216 /* At system boot, before all subsystems have been
5217 * registered, no tasks have been forked, so we don't
5218 * need to invoke fork callbacks here. */
5219 BUG_ON(!list_empty(&init_task.tasks));
5220
Tejun Heoae7f1642013-08-13 20:22:50 -04005221 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005222
Tejun Heo648bb562012-11-19 08:13:36 -08005223 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005224}
5225
5226/**
Li Zefana043e3b2008-02-23 15:24:09 -08005227 * cgroup_init_early - cgroup initialization at system boot
5228 *
5229 * Initialize cgroups at system boot, and initialize any
5230 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005231 */
5232int __init cgroup_init_early(void)
5233{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005234 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005235 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005236 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005237
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005238 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005239 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5240
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005241 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005242
Tejun Heo3ed80a62014-02-08 10:36:58 -05005243 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005244 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005245 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005246 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005247 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005248 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5249 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005250
Tejun Heoaec25022014-02-08 10:36:58 -05005251 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005252 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005253 if (!ss->legacy_name)
5254 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005255
5256 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005257 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005258 }
5259 return 0;
5260}
5261
Tejun Heo6e5c8302016-02-22 22:25:47 -05005262static u16 cgroup_disable_mask __initdata;
Tejun Heoa3e72732015-09-25 16:24:27 -04005263
Paul Menageddbcc7e2007-10-18 23:39:30 -07005264/**
Li Zefana043e3b2008-02-23 15:24:09 -08005265 * cgroup_init - cgroup initialization
5266 *
5267 * Register cgroup filesystem and /proc file, and initialize
5268 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005269 */
5270int __init cgroup_init(void)
5271{
Tejun Heo30159ec2013-06-25 11:53:37 -07005272 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005273 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005274
Tejun Heo6e5c8302016-02-22 22:25:47 -05005275 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005276 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heod62beb72016-12-27 14:49:08 -05005277 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
5278 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005279
Tejun Heoc58632b2018-04-26 14:29:04 -07005280 cgroup_rstat_boot();
Tejun Heo041cd642017-09-25 08:12:05 -07005281
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005282 /*
5283 * The latency of the synchronize_sched() is too high for cgroups,
5284 * avoid it at the cost of forcing all readers into the slow path.
5285 */
5286 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5287
Aditya Kalia79a9082016-01-29 02:54:06 -06005288 get_user_ns(init_cgroup_ns.user_ns);
5289
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005290 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005291
Tejun Heo2378d8b2016-03-03 09:57:57 -05005292 /*
5293 * Add init_css_set to the hash table so that dfl_root can link to
5294 * it during init.
5295 */
5296 hash_add(css_set_table, &init_css_set.hlist,
5297 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005298
Zefan Li9732adc2017-04-19 10:15:59 +08005299 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005300
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005301 mutex_unlock(&cgroup_mutex);
5302
Tejun Heo172a2c062014-03-19 10:23:53 -04005303 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005304 if (ss->early_init) {
5305 struct cgroup_subsys_state *css =
5306 init_css_set.subsys[ss->id];
5307
5308 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5309 GFP_KERNEL);
5310 BUG_ON(css->id < 0);
5311 } else {
5312 cgroup_init_subsys(ss, false);
5313 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005314
Tejun Heo2d8f2432014-04-23 11:13:15 -04005315 list_add_tail(&init_css_set.e_cset_node[ssid],
5316 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005317
5318 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005319 * Setting dfl_root subsys_mask needs to consider the
5320 * disabled flag and cftype registration needs kmalloc,
5321 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005322 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005323 if (cgroup_disable_mask & (1 << ssid)) {
5324 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5325 printk(KERN_INFO "Disabling %s control group subsystem\n",
5326 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005327 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005328 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005329
Tejun Heod62beb72016-12-27 14:49:08 -05005330 if (cgroup1_ssid_disabled(ssid))
Johannes Weiner223ffb22016-02-11 13:34:49 -05005331 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5332 ss->name);
5333
Tejun Heoa8ddc822014-07-15 11:05:10 -04005334 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5335
Tejun Heo8cfd8142017-07-21 11:14:51 -04005336 /* implicit controllers must be threaded too */
5337 WARN_ON(ss->implicit_on_dfl && !ss->threaded);
5338
Tejun Heof6d635ad2016-03-08 11:51:26 -05005339 if (ss->implicit_on_dfl)
5340 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5341 else if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005342 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005343
Tejun Heo8cfd8142017-07-21 11:14:51 -04005344 if (ss->threaded)
5345 cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
5346
Tejun Heoa8ddc822014-07-15 11:05:10 -04005347 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5348 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5349 } else {
5350 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5351 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005352 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005353
5354 if (ss->bind)
5355 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo7af608e2017-07-18 17:57:46 -04005356
5357 mutex_lock(&cgroup_mutex);
5358 css_populate_dir(init_css_set.subsys[ssid]);
5359 mutex_unlock(&cgroup_mutex);
Tejun Heo172a2c062014-03-19 10:23:53 -04005360 }
Greg KH676db4a2010-08-05 13:53:35 -07005361
Tejun Heo2378d8b2016-03-03 09:57:57 -05005362 /* init_css_set.subsys[] has been updated, re-hash */
5363 hash_del(&init_css_set.hlist);
5364 hash_add(css_set_table, &init_css_set.hlist,
5365 css_set_hash(init_css_set.subsys));
5366
Tejun Heo035f4f52015-10-15 17:00:43 -04005367 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5368 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005369 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005370 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005371
Tejun Heo2bd59d42014-02-11 11:52:49 -05005372 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005373}
Paul Menageb4f48b62007-10-18 23:39:33 -07005374
Tejun Heoe5fca242013-11-22 17:14:39 -05005375static int __init cgroup_wq_init(void)
5376{
5377 /*
5378 * There isn't much point in executing destruction path in
5379 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005380 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005381 *
5382 * We would prefer to do this in cgroup_init() above, but that
5383 * is called before init_workqueues(): so leave this until after.
5384 */
Tejun Heo1a115332014-02-12 19:06:19 -05005385 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005386 BUG_ON(!cgroup_destroy_wq);
5387 return 0;
5388}
5389core_initcall(cgroup_wq_init);
5390
Shaohua Li69fd5c32017-07-12 11:49:55 -07005391void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
5392 char *buf, size_t buflen)
5393{
5394 struct kernfs_node *kn;
5395
5396 kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id);
5397 if (!kn)
5398 return;
5399 kernfs_path(kn, buf, buflen);
5400 kernfs_put(kn);
5401}
5402
Paul Menagea4243162007-10-18 23:39:35 -07005403/*
5404 * proc_cgroup_show()
5405 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5406 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005407 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005408int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5409 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005410{
Tejun Heo4c737b42016-08-10 11:23:44 -04005411 char *buf;
Paul Menagea4243162007-10-18 23:39:35 -07005412 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005413 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005414
5415 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005416 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005417 if (!buf)
5418 goto out;
5419
Paul Menagea4243162007-10-18 23:39:35 -07005420 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005421 spin_lock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005422
Tejun Heo985ed672014-03-19 10:23:53 -04005423 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005424 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005425 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005426 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005427
Tejun Heoa7165262016-02-23 10:00:50 -05005428 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005429 continue;
5430
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005431 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005432 if (root != &cgrp_dfl_root)
5433 for_each_subsys(ss, ssid)
5434 if (root->subsys_mask & (1 << ssid))
5435 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005436 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005437 if (strlen(root->name))
5438 seq_printf(m, "%sname=%s", count ? "," : "",
5439 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005440 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005441
Paul Menage7717f7b2009-09-23 15:56:22 -07005442 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005443
5444 /*
5445 * On traditional hierarchies, all zombie tasks show up as
5446 * belonging to the root cgroup. On the default hierarchy,
5447 * while a zombie doesn't show up in "cgroup.procs" and
5448 * thus can't be migrated, its /proc/PID/cgroup keeps
5449 * reporting the cgroup it belonged to before exiting. If
5450 * the cgroup is removed before the zombie is reaped,
5451 * " (deleted)" is appended to the cgroup path.
5452 */
5453 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
Tejun Heo4c737b42016-08-10 11:23:44 -04005454 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
Aditya Kalia79a9082016-01-29 02:54:06 -06005455 current->nsproxy->cgroup_ns);
Tejun Heoe0223002016-09-29 15:49:40 +02005456 if (retval >= PATH_MAX)
Tejun Heo2e91fa72015-10-15 16:41:53 -04005457 retval = -ENAMETOOLONG;
Tejun Heoe0223002016-09-29 15:49:40 +02005458 if (retval < 0)
Tejun Heo2e91fa72015-10-15 16:41:53 -04005459 goto out_unlock;
Tejun Heo2e91fa72015-10-15 16:41:53 -04005460
Tejun Heo4c737b42016-08-10 11:23:44 -04005461 seq_puts(m, buf);
5462 } else {
5463 seq_puts(m, "/");
5464 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005465
5466 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5467 seq_puts(m, " (deleted)\n");
5468 else
5469 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005470 }
5471
Zefan Li006f4ac2014-09-18 16:03:15 +08005472 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005473out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005474 spin_unlock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005475 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005476 kfree(buf);
5477out:
5478 return retval;
5479}
5480
Paul Menageb4f48b62007-10-18 23:39:33 -07005481/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005482 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005483 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005484 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005485 * A task is associated with the init_css_set until cgroup_post_fork()
5486 * attaches it to the parent's css_set. Empty cg_list indicates that
5487 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005488 */
5489void cgroup_fork(struct task_struct *child)
5490{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005491 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005492 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005493}
5494
5495/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005496 * cgroup_can_fork - called on a new task before the process is exposed
5497 * @child: the task in question.
5498 *
5499 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5500 * returns an error, the fork aborts with that error code. This allows for
5501 * a cgroup subsystem to conditionally allow or deny new forks.
5502 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005503int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005504{
5505 struct cgroup_subsys *ss;
5506 int i, j, ret;
5507
Tejun Heob4e0eea2016-02-22 22:25:46 -05005508 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005509 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005510 if (ret)
5511 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05005512 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10005513
5514 return 0;
5515
5516out_revert:
5517 for_each_subsys(ss, j) {
5518 if (j >= i)
5519 break;
5520 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005521 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005522 }
5523
5524 return ret;
5525}
5526
5527/**
5528 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5529 * @child: the task in question
5530 *
5531 * This calls the cancel_fork() callbacks if a fork failed *after*
5532 * cgroup_can_fork() succeded.
5533 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005534void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005535{
5536 struct cgroup_subsys *ss;
5537 int i;
5538
5539 for_each_subsys(ss, i)
5540 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005541 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005542}
5543
5544/**
Li Zefana043e3b2008-02-23 15:24:09 -08005545 * cgroup_post_fork - called on a new task after adding it to the task list
5546 * @child: the task in question
5547 *
Tejun Heo5edee612012-10-16 15:03:14 -07005548 * Adds the task to the list running through its css_set if necessary and
5549 * call the subsystem fork() callbacks. Has to be after the task is
5550 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005551 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005552 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005553 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005554void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005555{
Tejun Heo30159ec2013-06-25 11:53:37 -07005556 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005557 int i;
5558
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005559 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005560 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005561 * function sets use_task_css_set_links before grabbing
5562 * tasklist_lock and we just went through tasklist_lock to add
5563 * @child, it's guaranteed that either we see the set
5564 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5565 * @child during its iteration.
5566 *
5567 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005568 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005569 * association is stable, and, on completion of the parent's
5570 * migration, @child is visible in the source of migration or
5571 * already in the destination cgroup. This guarantee is necessary
5572 * when implementing operations which need to migrate all tasks of
5573 * a cgroup to another.
5574 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005575 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005576 * will remain in init_css_set. This is safe because all tasks are
5577 * in the init_css_set before cg_links is enabled and there's no
5578 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005579 */
Paul Menage817929e2007-10-18 23:39:36 -07005580 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005581 struct css_set *cset;
5582
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005583 spin_lock_irq(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005584 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005585 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005586 get_css_set(cset);
Waiman Long73a72422017-06-13 17:18:01 -04005587 cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04005588 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005589 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005590 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005591 }
Tejun Heo5edee612012-10-16 15:03:14 -07005592
5593 /*
5594 * Call ss->fork(). This must happen after @child is linked on
5595 * css_set; otherwise, @child might change state between ->fork()
5596 * and addition to css_set.
5597 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005598 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005599 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005600 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07005601}
Tejun Heo5edee612012-10-16 15:03:14 -07005602
Paul Menage817929e2007-10-18 23:39:36 -07005603/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005604 * cgroup_exit - detach cgroup from exiting task
5605 * @tsk: pointer to task_struct of exiting process
5606 *
5607 * Description: Detach cgroup from @tsk and release it.
5608 *
5609 * Note that cgroups marked notify_on_release force every task in
5610 * them to take the global cgroup_mutex mutex when exiting.
5611 * This could impact scaling on very large systems. Be reluctant to
5612 * use notify_on_release cgroups where very high task exit scaling
5613 * is required on large systems.
5614 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005615 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5616 * call cgroup_exit() while the task is still competent to handle
5617 * notify_on_release(), then leave the task attached to the root cgroup in
5618 * each hierarchy for the remainder of its exit. No need to bother with
5619 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005620 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005621 */
Li Zefan1ec41832014-03-28 15:22:19 +08005622void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005623{
Tejun Heo30159ec2013-06-25 11:53:37 -07005624 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005625 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005626 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005627
5628 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005629 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005630 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005631 */
Tejun Heo0de09422015-10-15 16:41:49 -04005632 cset = task_css_set(tsk);
5633
Paul Menage817929e2007-10-18 23:39:36 -07005634 if (!list_empty(&tsk->cg_list)) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005635 spin_lock_irq(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005636 css_set_move_task(tsk, cset, NULL, false);
Waiman Long73a72422017-06-13 17:18:01 -04005637 cset->nr_tasks--;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005638 spin_unlock_irq(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005639 } else {
5640 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005641 }
5642
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005643 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005644 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04005645 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005646 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04005647}
Tejun Heo30159ec2013-06-25 11:53:37 -07005648
Tejun Heo2e91fa72015-10-15 16:41:53 -04005649void cgroup_free(struct task_struct *task)
5650{
5651 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005652 struct cgroup_subsys *ss;
5653 int ssid;
5654
Tejun Heob4e0eea2016-02-22 22:25:46 -05005655 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04005656 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005657 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005658
Tejun Heo2e91fa72015-10-15 16:41:53 -04005659 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005660}
Paul Menage697f4162007-10-18 23:39:34 -07005661
Paul Menage8bab8dd2008-04-04 14:29:57 -07005662static int __init cgroup_disable(char *str)
5663{
Tejun Heo30159ec2013-06-25 11:53:37 -07005664 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005665 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005666 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005667
5668 while ((token = strsep(&str, ",")) != NULL) {
5669 if (!*token)
5670 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005671
Tejun Heo3ed80a62014-02-08 10:36:58 -05005672 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005673 if (strcmp(token, ss->name) &&
5674 strcmp(token, ss->legacy_name))
5675 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005676 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005677 }
5678 }
5679 return 1;
5680}
5681__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005682
Tejun Heob77d7b62013-08-13 11:01:54 -04005683/**
Tejun Heoec903c02014-05-13 12:11:01 -04005684 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005685 * @dentry: directory dentry of interest
5686 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005687 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005688 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5689 * to get the corresponding css and return it. If such css doesn't exist
5690 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005691 */
Tejun Heoec903c02014-05-13 12:11:01 -04005692struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5693 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005694{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005695 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05005696 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005697 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005698 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005699
Tejun Heo35cf0832013-08-26 18:40:56 -04005700 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05005701 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5702 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005703 return ERR_PTR(-EBADF);
5704
Tejun Heo5a17f542014-02-11 11:52:47 -05005705 rcu_read_lock();
5706
Tejun Heo2bd59d42014-02-11 11:52:49 -05005707 /*
5708 * This path doesn't originate from kernfs and @kn could already
5709 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005710 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005711 */
Tejun Heoe0aed7c2016-12-27 14:49:09 -05005712 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005713 if (cgrp)
5714 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005715
Tejun Heoec903c02014-05-13 12:11:01 -04005716 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005717 css = ERR_PTR(-ENOENT);
5718
5719 rcu_read_unlock();
5720 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005721}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005722
Li Zefan1cb650b2013-08-19 10:05:24 +08005723/**
5724 * css_from_id - lookup css by id
5725 * @id: the cgroup id
5726 * @ss: cgroup subsys to be looked into
5727 *
5728 * Returns the css if there's valid one with @id, otherwise returns NULL.
5729 * Should be called under rcu_read_lock().
5730 */
5731struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5732{
Tejun Heo6fa49182014-05-04 15:09:13 -04005733 WARN_ON_ONCE(!rcu_read_lock_held());
Johannes Weinerd6ccc552016-06-17 12:24:27 -04005734 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005735}
5736
Tejun Heo16af4392015-11-20 15:55:52 -05005737/**
5738 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5739 * @path: path on the default hierarchy
5740 *
5741 * Find the cgroup at @path on the default hierarchy, increment its
5742 * reference count and return it. Returns pointer to the found cgroup on
5743 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5744 * if @path points to a non-directory.
5745 */
5746struct cgroup *cgroup_get_from_path(const char *path)
5747{
5748 struct kernfs_node *kn;
5749 struct cgroup *cgrp;
5750
5751 mutex_lock(&cgroup_mutex);
5752
5753 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5754 if (kn) {
5755 if (kernfs_type(kn) == KERNFS_DIR) {
5756 cgrp = kn->priv;
Tejun Heoa590b902017-04-28 15:14:55 -04005757 cgroup_get_live(cgrp);
Tejun Heo16af4392015-11-20 15:55:52 -05005758 } else {
5759 cgrp = ERR_PTR(-ENOTDIR);
5760 }
5761 kernfs_put(kn);
5762 } else {
5763 cgrp = ERR_PTR(-ENOENT);
5764 }
5765
5766 mutex_unlock(&cgroup_mutex);
5767 return cgrp;
5768}
5769EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5770
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07005771/**
5772 * cgroup_get_from_fd - get a cgroup pointer from a fd
5773 * @fd: fd obtained by open(cgroup2_dir)
5774 *
5775 * Find the cgroup from a fd which should be obtained
5776 * by opening a cgroup directory. Returns a pointer to the
5777 * cgroup on success. ERR_PTR is returned if the cgroup
5778 * cannot be found.
5779 */
5780struct cgroup *cgroup_get_from_fd(int fd)
5781{
5782 struct cgroup_subsys_state *css;
5783 struct cgroup *cgrp;
5784 struct file *f;
5785
5786 f = fget_raw(fd);
5787 if (!f)
5788 return ERR_PTR(-EBADF);
5789
5790 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
5791 fput(f);
5792 if (IS_ERR(css))
5793 return ERR_CAST(css);
5794
5795 cgrp = css->cgroup;
5796 if (!cgroup_on_dfl(cgrp)) {
5797 cgroup_put(cgrp);
5798 return ERR_PTR(-EBADF);
5799 }
5800
5801 return cgrp;
5802}
5803EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
5804
Tejun Heobd1060a2015-12-07 17:38:53 -05005805/*
5806 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5807 * definition in cgroup-defs.h.
5808 */
5809#ifdef CONFIG_SOCK_CGROUP_DATA
5810
5811#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5812
Tejun Heo3fa4cc92015-12-14 11:24:06 -05005813DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05005814static bool cgroup_sk_alloc_disabled __read_mostly;
5815
5816void cgroup_sk_alloc_disable(void)
5817{
5818 if (cgroup_sk_alloc_disabled)
5819 return;
5820 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5821 cgroup_sk_alloc_disabled = true;
5822}
5823
5824#else
5825
5826#define cgroup_sk_alloc_disabled false
5827
5828#endif
5829
5830void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5831{
5832 if (cgroup_sk_alloc_disabled)
5833 return;
5834
Johannes Weinerd979a392016-09-19 14:44:38 -07005835 /* Socket clone path */
5836 if (skcd->val) {
Tejun Heoa590b902017-04-28 15:14:55 -04005837 /*
5838 * We might be cloning a socket which is left in an empty
5839 * cgroup and the cgroup might have already been rmdir'd.
5840 * Don't use cgroup_get_live().
5841 */
Johannes Weinerd979a392016-09-19 14:44:38 -07005842 cgroup_get(sock_cgroup_ptr(skcd));
5843 return;
5844 }
5845
Tejun Heobd1060a2015-12-07 17:38:53 -05005846 rcu_read_lock();
5847
5848 while (true) {
5849 struct css_set *cset;
5850
5851 cset = task_css_set(current);
5852 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5853 skcd->val = (unsigned long)cset->dfl_cgrp;
5854 break;
5855 }
5856 cpu_relax();
5857 }
5858
5859 rcu_read_unlock();
5860}
5861
5862void cgroup_sk_free(struct sock_cgroup_data *skcd)
5863{
5864 cgroup_put(sock_cgroup_ptr(skcd));
5865}
5866
5867#endif /* CONFIG_SOCK_CGROUP_DATA */
5868
Daniel Mack30070982016-11-23 16:52:26 +01005869#ifdef CONFIG_CGROUP_BPF
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07005870int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
5871 enum bpf_attach_type type, u32 flags)
Daniel Mack30070982016-11-23 16:52:26 +01005872{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08005873 int ret;
Daniel Mack30070982016-11-23 16:52:26 +01005874
5875 mutex_lock(&cgroup_mutex);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07005876 ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
5877 mutex_unlock(&cgroup_mutex);
5878 return ret;
5879}
5880int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
5881 enum bpf_attach_type type, u32 flags)
5882{
5883 int ret;
5884
5885 mutex_lock(&cgroup_mutex);
5886 ret = __cgroup_bpf_detach(cgrp, prog, type, flags);
Daniel Mack30070982016-11-23 16:52:26 +01005887 mutex_unlock(&cgroup_mutex);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08005888 return ret;
Daniel Mack30070982016-11-23 16:52:26 +01005889}
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07005890int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
5891 union bpf_attr __user *uattr)
5892{
5893 int ret;
5894
5895 mutex_lock(&cgroup_mutex);
5896 ret = __cgroup_bpf_query(cgrp, attr, uattr);
5897 mutex_unlock(&cgroup_mutex);
5898 return ret;
5899}
Daniel Mack30070982016-11-23 16:52:26 +01005900#endif /* CONFIG_CGROUP_BPF */
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05005901
5902#ifdef CONFIG_SYSFS
5903static ssize_t show_delegatable_files(struct cftype *files, char *buf,
5904 ssize_t size, const char *prefix)
5905{
5906 struct cftype *cft;
5907 ssize_t ret = 0;
5908
5909 for (cft = files; cft && cft->name[0] != '\0'; cft++) {
5910 if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
5911 continue;
5912
5913 if (prefix)
5914 ret += snprintf(buf + ret, size - ret, "%s.", prefix);
5915
5916 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
5917
5918 if (unlikely(ret >= size)) {
5919 WARN_ON(1);
5920 break;
5921 }
5922 }
5923
5924 return ret;
5925}
5926
5927static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
5928 char *buf)
5929{
5930 struct cgroup_subsys *ss;
5931 int ssid;
5932 ssize_t ret = 0;
5933
5934 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret,
5935 NULL);
5936
5937 for_each_subsys(ss, ssid)
5938 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
5939 PAGE_SIZE - ret,
5940 cgroup_subsys_name[ssid]);
5941
5942 return ret;
5943}
5944static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
5945
Roman Gushchin5f2e6732017-11-06 13:30:29 -05005946static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
5947 char *buf)
5948{
5949 return snprintf(buf, PAGE_SIZE, "nsdelegate\n");
5950}
5951static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
5952
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05005953static struct attribute *cgroup_sysfs_attrs[] = {
5954 &cgroup_delegate_attr.attr,
Roman Gushchin5f2e6732017-11-06 13:30:29 -05005955 &cgroup_features_attr.attr,
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05005956 NULL,
5957};
5958
5959static const struct attribute_group cgroup_sysfs_attr_group = {
5960 .attrs = cgroup_sysfs_attrs,
5961 .name = "cgroup",
5962};
5963
5964static int __init cgroup_sysfs_init(void)
5965{
5966 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
5967}
5968subsys_initcall(cgroup_sysfs_init);
5969#endif /* CONFIG_SYSFS */