blob: 4178e45becb45de4ac81191e432d536d3b196267 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mm.h>
40#include <linux/mutex.h>
41#include <linux/mount.h>
42#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070043#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/rcupdate.h>
45#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040048#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050059#include <linux/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Tejun Heoe93ad192016-01-19 12:18:41 -050061#include <linux/cpuset.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050062#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070063
Tejun Heoe25e2cb2011-12-12 18:12:21 -080064/*
Tejun Heob1a21362013-11-29 10:42:58 -050065 * pidlists linger the following amount before being destroyed. The goal
66 * is avoiding frequent destruction in the middle of consecutive read calls
67 * Expiring in the middle is a performance problem not a correctness one.
68 * 1 sec should be enough.
69 */
70#define CGROUP_PIDLIST_DESTROY_DELAY HZ
71
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050072#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
73 MAX_CFTYPE_NAME + 2)
74
Tejun Heob1a21362013-11-29 10:42:58 -050075/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080076 * cgroup_mutex is the master lock. Any modification to cgroup or its
77 * hierarchy must be performed while holding it.
78 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040079 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050080 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080081 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050082 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
83 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080084 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040087DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -050088EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040089EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070090#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070091static DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040092static DEFINE_SPINLOCK(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070093#endif
94
Tejun Heo69e943b2014-02-08 10:36:58 -050095/*
Tejun Heo15a4c832014-05-04 15:09:14 -040096 * Protects cgroup_idr and css_idr so that IDs can be released without
97 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040098 */
99static DEFINE_SPINLOCK(cgroup_idr_lock);
100
101/*
Tejun Heo34c06252015-11-05 00:12:24 -0500102 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
103 * against file removal/re-creation across css hiding.
104 */
105static DEFINE_SPINLOCK(cgroup_file_kn_lock);
106
107/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500108 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
109 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
110 */
111static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700112
Tejun Heo1ed13282015-09-16 12:53:17 -0400113struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
114
Tejun Heo8353da12014-05-13 12:19:23 -0400115#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700116 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
117 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400118 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500119
Ben Blumaae8aab2010-03-10 15:22:07 -0800120/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500121 * cgroup destruction makes heavy use of work items and there can be a lot
122 * of concurrent destructions. Use a separate workqueue so that cgroup
123 * destruction work items don't end up filling up max_active of system_wq
124 * which may lead to deadlock.
125 */
126static struct workqueue_struct *cgroup_destroy_wq;
127
128/*
Tejun Heob1a21362013-11-29 10:42:58 -0500129 * pidlist destructions need to be flushed on cgroup destruction. Use a
130 * separate workqueue as flush domain.
131 */
132static struct workqueue_struct *cgroup_pidlist_destroy_wq;
133
Tejun Heo3ed80a62014-02-08 10:36:58 -0500134/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500135#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500136static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137#include <linux/cgroup_subsys.h>
138};
Tejun Heo073219e2014-02-08 10:36:58 -0500139#undef SUBSYS
140
141/* array of cgroup subsystem names */
142#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
143static const char *cgroup_subsys_name[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Tejun Heo49d1dc42015-09-18 11:56:28 -0400148/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
149#define SUBSYS(_x) \
150 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
151 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
152 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
153 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
154#include <linux/cgroup_subsys.h>
155#undef SUBSYS
156
157#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
158static struct static_key_true *cgroup_subsys_enabled_key[] = {
159#include <linux/cgroup_subsys.h>
160};
161#undef SUBSYS
162
163#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
164static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
165#include <linux/cgroup_subsys.h>
166};
167#undef SUBSYS
168
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400170 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700171 * unattached - it never has more than a single cgroup, and all tasks are
172 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400174struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400175EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700176
Tejun Heoa2dd4242014-03-19 10:23:55 -0400177/*
178 * The default hierarchy always exists but is hidden until mounted for the
179 * first time. This is for backward compatibility.
180 */
Tejun Heoa7165262016-02-23 10:00:50 -0500181static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700182
Johannes Weiner223ffb22016-02-11 13:34:49 -0500183/* Controllers blocked by the commandline in v1 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500184static u16 cgroup_no_v1_mask;
Johannes Weiner223ffb22016-02-11 13:34:49 -0500185
Tejun Heo5533e012014-05-14 19:33:07 -0400186/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500187static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400188
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189/* The list of hierarchy roots */
190
Tejun Heo9871bf92013-06-24 15:21:47 -0700191static LIST_HEAD(cgroup_roots);
192static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700193
Tejun Heo3417ae12014-02-08 10:37:01 -0500194/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700195static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700196
Li Zefan794611a2013-06-18 18:53:53 +0800197/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400198 * Assign a monotonically increasing serial number to csses. It guarantees
199 * cgroups with bigger numbers are newer than those with smaller numbers.
200 * Also, as csses are always appended to the parent's ->children list, it
201 * guarantees that sibling csses are always sorted in the ascending serial
202 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800203 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400204static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800205
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000206/*
207 * These bitmask flags indicate whether tasks in the fork and exit paths have
208 * fork/exit handlers to call. This avoids us having to do extra work in the
209 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500211static u16 have_fork_callback __read_mostly;
212static u16 have_exit_callback __read_mostly;
213static u16 have_free_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700214
Aleksa Sarai7e476822015-06-09 21:32:09 +1000215/* Ditto for the can_fork callback. */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500216static u16 have_canfork_callback __read_mostly;
Aleksa Sarai7e476822015-06-09 21:32:09 +1000217
Tejun Heo67e9c742015-11-16 11:13:34 -0500218static struct file_system_type cgroup2_fs_type;
Tejun Heoa14c6872014-07-15 11:05:09 -0400219static struct cftype cgroup_dfl_base_files[];
220static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700221
Tejun Heo6e5c8302016-02-22 22:25:47 -0500222static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400223static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500225static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
226 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400227static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400228static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400229static int cgroup_addrm_files(struct cgroup_subsys_state *css,
230 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400231 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800232
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400233/**
234 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
235 * @ssid: subsys ID of interest
236 *
237 * cgroup_subsys_enabled() can only be used with literal subsys names which
238 * is fine for individual subsystems but unsuitable for cgroup core. This
239 * is slower static_key_enabled() based test indexed by @ssid.
240 */
241static bool cgroup_ssid_enabled(int ssid)
242{
243 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
244}
245
Johannes Weiner223ffb22016-02-11 13:34:49 -0500246static bool cgroup_ssid_no_v1(int ssid)
247{
248 return cgroup_no_v1_mask & (1 << ssid);
249}
250
Tejun Heo9e10a132015-09-18 11:56:28 -0400251/**
252 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
253 * @cgrp: the cgroup of interest
254 *
255 * The default hierarchy is the v2 interface of cgroup and this function
256 * can be used to test whether a cgroup is on the default hierarchy for
257 * cases where a subsystem should behave differnetly depending on the
258 * interface version.
259 *
260 * The set of behaviors which change on the default hierarchy are still
261 * being determined and the mount option is prefixed with __DEVEL__.
262 *
263 * List of changed behaviors:
264 *
265 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
266 * and "name" are disallowed.
267 *
268 * - When mounting an existing superblock, mount options should match.
269 *
270 * - Remount is disallowed.
271 *
272 * - rename(2) is disallowed.
273 *
274 * - "tasks" is removed. Everything should be at process granularity. Use
275 * "cgroup.procs" instead.
276 *
277 * - "cgroup.procs" is not sorted. pids will be unique unless they got
278 * recycled inbetween reads.
279 *
280 * - "release_agent" and "notify_on_release" are removed. Replacement
281 * notification mechanism will be implemented.
282 *
283 * - "cgroup.clone_children" is removed.
284 *
285 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
286 * and its descendants contain no task; otherwise, 1. The file also
287 * generates kernfs notification which can be monitored through poll and
288 * [di]notify when the value of the file changes.
289 *
290 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
291 * take masks of ancestors with non-empty cpus/mems, instead of being
292 * moved to an ancestor.
293 *
294 * - cpuset: a task can be moved into an empty cpuset, and again it takes
295 * masks of ancestors.
296 *
297 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
298 * is not created.
299 *
300 * - blkcg: blk-throttle becomes properly hierarchical.
301 *
302 * - debug: disallowed on the default hierarchy.
303 */
304static bool cgroup_on_dfl(const struct cgroup *cgrp)
305{
306 return cgrp->root == &cgrp_dfl_root;
307}
308
Tejun Heo6fa49182014-05-04 15:09:13 -0400309/* IDR wrappers which synchronize using cgroup_idr_lock */
310static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
311 gfp_t gfp_mask)
312{
313 int ret;
314
315 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400316 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800317 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400318 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400319 idr_preload_end();
320 return ret;
321}
322
323static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
324{
325 void *ret;
326
Tejun Heo54504e92014-05-13 12:10:59 -0400327 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400328 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400329 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400330 return ret;
331}
332
333static void cgroup_idr_remove(struct idr *idr, int id)
334{
Tejun Heo54504e92014-05-13 12:10:59 -0400335 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400336 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400337 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400338}
339
Tejun Heod51f39b2014-05-16 13:22:48 -0400340static struct cgroup *cgroup_parent(struct cgroup *cgrp)
341{
342 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
343
344 if (parent_css)
345 return container_of(parent_css, struct cgroup, self);
346 return NULL;
347}
348
Tejun Heo95109b62013-08-08 20:11:27 -0400349/**
350 * cgroup_css - obtain a cgroup's css for the specified subsystem
351 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400352 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400353 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400354 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
355 * function must be called either under cgroup_mutex or rcu_read_lock() and
356 * the caller is responsible for pinning the returned css if it wants to
357 * keep accessing it outside the said locks. This function may return
358 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400359 */
360static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400361 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400362{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400363 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500364 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500365 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400366 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400367 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400368}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700369
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400370/**
371 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
372 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400373 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400374 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400375 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400376 * as the matching css of the nearest ancestor including self which has @ss
377 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
378 * function is guaranteed to return non-NULL css.
379 */
380static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
381 struct cgroup_subsys *ss)
382{
383 lockdep_assert_held(&cgroup_mutex);
384
385 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400386 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400387
388 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
389 return NULL;
390
Tejun Heoeeecbd12014-11-18 02:49:52 -0500391 /*
392 * This function is used while updating css associations and thus
Tejun Heo8699b772016-02-22 22:25:46 -0500393 * can't test the csses directly. Use ->subtree_ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500394 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400395 while (cgroup_parent(cgrp) &&
Tejun Heo8699b772016-02-22 22:25:46 -0500396 !(cgroup_parent(cgrp)->subtree_ss_mask & (1 << ss->id)))
Tejun Heod51f39b2014-05-16 13:22:48 -0400397 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400398
399 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700400}
401
Tejun Heoeeecbd12014-11-18 02:49:52 -0500402/**
403 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
404 * @cgrp: the cgroup of interest
405 * @ss: the subsystem of interest
406 *
407 * Find and get the effective css of @cgrp for @ss. The effective css is
408 * defined as the matching css of the nearest ancestor including self which
409 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
410 * the root css is returned, so this function always returns a valid css.
411 * The returned css must be put using css_put().
412 */
413struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
414 struct cgroup_subsys *ss)
415{
416 struct cgroup_subsys_state *css;
417
418 rcu_read_lock();
419
420 do {
421 css = cgroup_css(cgrp, ss);
422
423 if (css && css_tryget_online(css))
424 goto out_unlock;
425 cgrp = cgroup_parent(cgrp);
426 } while (cgrp);
427
428 css = init_css_set.subsys[ss->id];
429 css_get(css);
430out_unlock:
431 rcu_read_unlock();
432 return css;
433}
434
Paul Menageddbcc7e2007-10-18 23:39:30 -0700435/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700436static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700437{
Tejun Heo184faf32014-05-16 13:22:51 -0400438 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700439}
440
Tejun Heo052c3f32015-10-15 16:41:50 -0400441static void cgroup_get(struct cgroup *cgrp)
442{
443 WARN_ON_ONCE(cgroup_is_dead(cgrp));
444 css_get(&cgrp->self);
445}
446
447static bool cgroup_tryget(struct cgroup *cgrp)
448{
449 return css_tryget(&cgrp->self);
450}
451
Tejun Heob4168642014-05-13 12:16:21 -0400452struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500453{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500454 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400455 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500456
457 /*
458 * This is open and unprotected implementation of cgroup_css().
459 * seq_css() is only called from a kernfs file operation which has
460 * an active reference on the file. Because all the subsystem
461 * files are drained before a css is disassociated with a cgroup,
462 * the matching css from the cgroup's subsys table is guaranteed to
463 * be and stay valid until the enclosing operation is complete.
464 */
465 if (cft->ss)
466 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
467 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400468 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500469}
Tejun Heob4168642014-05-13 12:16:21 -0400470EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500471
Adrian Bunke9685a02008-02-07 00:13:46 -0800472static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700473{
Paul Menagebd89aab2007-10-18 23:40:44 -0700474 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700475}
476
Tejun Heo30159ec2013-06-25 11:53:37 -0700477/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500478 * for_each_css - iterate all css's of a cgroup
479 * @css: the iteration cursor
480 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
481 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700482 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400483 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700484 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500485#define for_each_css(css, ssid, cgrp) \
486 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
487 if (!((css) = rcu_dereference_check( \
488 (cgrp)->subsys[(ssid)], \
489 lockdep_is_held(&cgroup_mutex)))) { } \
490 else
491
492/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400493 * for_each_e_css - iterate all effective css's of a cgroup
494 * @css: the iteration cursor
495 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
496 * @cgrp: the target cgroup to iterate css's of
497 *
498 * Should be called under cgroup_[tree_]mutex.
499 */
500#define for_each_e_css(css, ssid, cgrp) \
501 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
502 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
503 ; \
504 else
505
506/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500507 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700508 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500509 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700510 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500511#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500512 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
513 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700514
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000515/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500516 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000517 * @ss: the iteration cursor
518 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500519 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000520 *
521 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500522 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000523 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500524#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
525 unsigned long __ss_mask = (ss_mask); \
526 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000527 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500528 break; \
529 } \
530 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
531 (ss) = cgroup_subsys[ssid]; \
532 {
533
534#define while_each_subsys_mask() \
535 } \
536 } \
537} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000538
Tejun Heo985ed672014-03-19 10:23:53 -0400539/* iterate across the hierarchies */
540#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700541 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700542
Tejun Heof8f22e52014-04-23 11:13:16 -0400543/* iterate over child cgrps, lock should be held throughout iteration */
544#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400545 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400546 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400547 cgroup_is_dead(child); })) \
548 ; \
549 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700550
Paul Menage81a6a5c2007-10-18 23:39:38 -0700551static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700552static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700553
Tejun Heo69d02062013-06-12 21:04:50 -0700554/*
555 * A cgroup can be associated with multiple css_sets as different tasks may
556 * belong to different cgroups on different hierarchies. In the other
557 * direction, a css_set is naturally associated with multiple cgroups.
558 * This M:N relationship is represented by the following link structure
559 * which exists for each association and allows traversing the associations
560 * from both sides.
561 */
562struct cgrp_cset_link {
563 /* the cgroup and css_set this link associates */
564 struct cgroup *cgrp;
565 struct css_set *cset;
566
567 /* list of cgrp_cset_links anchored at cgrp->cset_links */
568 struct list_head cset_link;
569
570 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
571 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700572};
573
Tejun Heo172a2c062014-03-19 10:23:53 -0400574/*
575 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700576 * hierarchies being mounted. It contains a pointer to the root state
577 * for each subsystem. Also used to anchor the list of css_sets. Not
578 * reference-counted, to improve performance when child cgroups
579 * haven't been created.
580 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400581struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400582 .refcount = ATOMIC_INIT(1),
583 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
584 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
585 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
586 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
587 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400588 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400589};
Paul Menage817929e2007-10-18 23:39:36 -0700590
Tejun Heo172a2c062014-03-19 10:23:53 -0400591static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700592
Tejun Heo842b5972014-04-25 18:28:02 -0400593/**
Tejun Heo0de09422015-10-15 16:41:49 -0400594 * css_set_populated - does a css_set contain any tasks?
595 * @cset: target css_set
596 */
597static bool css_set_populated(struct css_set *cset)
598{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400599 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400600
601 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
602}
603
604/**
Tejun Heo842b5972014-04-25 18:28:02 -0400605 * cgroup_update_populated - updated populated count of a cgroup
606 * @cgrp: the target cgroup
607 * @populated: inc or dec populated count
608 *
Tejun Heo0de09422015-10-15 16:41:49 -0400609 * One of the css_sets associated with @cgrp is either getting its first
610 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
611 * count is propagated towards root so that a given cgroup's populated_cnt
612 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400613 *
614 * @cgrp's interface file "cgroup.populated" is zero if
615 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
616 * changes from or to zero, userland is notified that the content of the
617 * interface file has changed. This can be used to detect when @cgrp and
618 * its descendants become populated or empty.
619 */
620static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
621{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400622 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400623
624 do {
625 bool trigger;
626
627 if (populated)
628 trigger = !cgrp->populated_cnt++;
629 else
630 trigger = !--cgrp->populated_cnt;
631
632 if (!trigger)
633 break;
634
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400635 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400636 cgroup_file_notify(&cgrp->events_file);
637
Tejun Heod51f39b2014-05-16 13:22:48 -0400638 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400639 } while (cgrp);
640}
641
Tejun Heo0de09422015-10-15 16:41:49 -0400642/**
643 * css_set_update_populated - update populated state of a css_set
644 * @cset: target css_set
645 * @populated: whether @cset is populated or depopulated
646 *
647 * @cset is either getting the first task or losing the last. Update the
648 * ->populated_cnt of all associated cgroups accordingly.
649 */
650static void css_set_update_populated(struct css_set *cset, bool populated)
651{
652 struct cgrp_cset_link *link;
653
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400654 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400655
656 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
657 cgroup_update_populated(link->cgrp, populated);
658}
659
Tejun Heof6d7d042015-10-15 16:41:52 -0400660/**
661 * css_set_move_task - move a task from one css_set to another
662 * @task: task being moved
663 * @from_cset: css_set @task currently belongs to (may be NULL)
664 * @to_cset: new css_set @task is being moved to (may be NULL)
665 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
666 *
667 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
668 * css_set, @from_cset can be NULL. If @task is being disassociated
669 * instead of moved, @to_cset can be NULL.
670 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400671 * This function automatically handles populated_cnt updates and
672 * css_task_iter adjustments but the caller is responsible for managing
673 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400674 */
675static void css_set_move_task(struct task_struct *task,
676 struct css_set *from_cset, struct css_set *to_cset,
677 bool use_mg_tasks)
678{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400679 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400680
Tejun Heo20b454a2016-03-03 09:57:57 -0500681 if (to_cset && !css_set_populated(to_cset))
682 css_set_update_populated(to_cset, true);
683
Tejun Heof6d7d042015-10-15 16:41:52 -0400684 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400685 struct css_task_iter *it, *pos;
686
Tejun Heof6d7d042015-10-15 16:41:52 -0400687 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400688
689 /*
690 * @task is leaving, advance task iterators which are
691 * pointing to it so that they can resume at the next
692 * position. Advancing an iterator might remove it from
693 * the list, use safe walk. See css_task_iter_advance*()
694 * for details.
695 */
696 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
697 iters_node)
698 if (it->task_pos == &task->cg_list)
699 css_task_iter_advance(it);
700
Tejun Heof6d7d042015-10-15 16:41:52 -0400701 list_del_init(&task->cg_list);
702 if (!css_set_populated(from_cset))
703 css_set_update_populated(from_cset, false);
704 } else {
705 WARN_ON_ONCE(!list_empty(&task->cg_list));
706 }
707
708 if (to_cset) {
709 /*
710 * We are synchronized through cgroup_threadgroup_rwsem
711 * against PF_EXITING setting such that we can't race
712 * against cgroup_exit() changing the css_set to
713 * init_css_set and dropping the old one.
714 */
715 WARN_ON_ONCE(task->flags & PF_EXITING);
716
Tejun Heof6d7d042015-10-15 16:41:52 -0400717 rcu_assign_pointer(task->cgroups, to_cset);
718 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
719 &to_cset->tasks);
720 }
721}
722
Paul Menage7717f7b2009-09-23 15:56:22 -0700723/*
724 * hash table for cgroup groups. This improves the performance to find
725 * an existing css_set. This hash doesn't (currently) take into
726 * account cgroups in empty hierarchies.
727 */
Li Zefan472b1052008-04-29 01:00:11 -0700728#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800729static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700730
Li Zefan0ac801f2013-01-10 11:49:27 +0800731static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700732{
Li Zefan0ac801f2013-01-10 11:49:27 +0800733 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700734 struct cgroup_subsys *ss;
735 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700736
Tejun Heo30159ec2013-06-25 11:53:37 -0700737 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800738 key += (unsigned long)css[i];
739 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700740
Li Zefan0ac801f2013-01-10 11:49:27 +0800741 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700742}
743
Zefan Lia25eb522014-09-19 16:51:00 +0800744static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700745{
Tejun Heo69d02062013-06-12 21:04:50 -0700746 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400747 struct cgroup_subsys *ss;
748 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700749
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400750 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500751
752 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700753 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700754
Tejun Heo53254f92015-11-23 14:55:41 -0500755 /* This css_set is dead. unlink it and release cgroup and css refs */
756 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400757 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500758 css_put(cset->subsys[ssid]);
759 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700760 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700761 css_set_count--;
762
Tejun Heo69d02062013-06-12 21:04:50 -0700763 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700764 list_del(&link->cset_link);
765 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400766 if (cgroup_parent(link->cgrp))
767 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700768 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700769 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700770
Tejun Heo5abb8852013-06-12 21:04:49 -0700771 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700772}
773
Zefan Lia25eb522014-09-19 16:51:00 +0800774static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500775{
776 /*
777 * Ensure that the refcount doesn't hit zero while any readers
778 * can see it. Similar to atomic_dec_and_lock(), but for an
779 * rwlock
780 */
781 if (atomic_add_unless(&cset->refcount, -1, 1))
782 return;
783
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400784 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800785 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400786 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500787}
788
Paul Menage817929e2007-10-18 23:39:36 -0700789/*
790 * refcounted get/put for css_set objects
791 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700792static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700793{
Tejun Heo5abb8852013-06-12 21:04:49 -0700794 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700795}
796
Tejun Heob326f9d2013-06-24 15:21:48 -0700797/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700798 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700799 * @cset: candidate css_set being tested
800 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700801 * @new_cgrp: cgroup that's being entered by the task
802 * @template: desired set of css pointers in css_set (pre-calculated)
803 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800804 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700805 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
806 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700807static bool compare_css_sets(struct css_set *cset,
808 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700809 struct cgroup *new_cgrp,
810 struct cgroup_subsys_state *template[])
811{
812 struct list_head *l1, *l2;
813
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400814 /*
815 * On the default hierarchy, there can be csets which are
816 * associated with the same set of cgroups but different csses.
817 * Let's first ensure that csses match.
818 */
819 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700820 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700821
822 /*
823 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400824 * different cgroups in hierarchies. As different cgroups may
825 * share the same effective css, this comparison is always
826 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700827 */
Tejun Heo69d02062013-06-12 21:04:50 -0700828 l1 = &cset->cgrp_links;
829 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700830 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700831 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700832 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700833
834 l1 = l1->next;
835 l2 = l2->next;
836 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700837 if (l1 == &cset->cgrp_links) {
838 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700839 break;
840 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700841 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700842 }
843 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700844 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
845 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
846 cgrp1 = link1->cgrp;
847 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700848 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700849 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700850
851 /*
852 * If this hierarchy is the hierarchy of the cgroup
853 * that's changing, then we need to check that this
854 * css_set points to the new cgroup; if it's any other
855 * hierarchy, then this css_set should point to the
856 * same cgroup as the old css_set.
857 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700858 if (cgrp1->root == new_cgrp->root) {
859 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700860 return false;
861 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700862 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700863 return false;
864 }
865 }
866 return true;
867}
868
Tejun Heob326f9d2013-06-24 15:21:48 -0700869/**
870 * find_existing_css_set - init css array and find the matching css_set
871 * @old_cset: the css_set that we're using before the cgroup transition
872 * @cgrp: the cgroup that we're moving into
873 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700874 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700875static struct css_set *find_existing_css_set(struct css_set *old_cset,
876 struct cgroup *cgrp,
877 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700878{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400879 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700880 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700881 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800882 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700883 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700884
Ben Blumaae8aab2010-03-10 15:22:07 -0800885 /*
886 * Build the set of subsystem state objects that we want to see in the
887 * new css_set. while subsystems can change globally, the entries here
888 * won't change, so no need for locking.
889 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700890 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400891 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400892 /*
893 * @ss is in this hierarchy, so we want the
894 * effective css from @cgrp.
895 */
896 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700897 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400898 /*
899 * @ss is not in this hierarchy, so we don't want
900 * to change the css.
901 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700902 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700903 }
904 }
905
Li Zefan0ac801f2013-01-10 11:49:27 +0800906 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700907 hash_for_each_possible(css_set_table, cset, hlist, key) {
908 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700909 continue;
910
911 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700912 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700913 }
Paul Menage817929e2007-10-18 23:39:36 -0700914
915 /* No existing cgroup group matched */
916 return NULL;
917}
918
Tejun Heo69d02062013-06-12 21:04:50 -0700919static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700920{
Tejun Heo69d02062013-06-12 21:04:50 -0700921 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700922
Tejun Heo69d02062013-06-12 21:04:50 -0700923 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
924 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700925 kfree(link);
926 }
927}
928
Tejun Heo69d02062013-06-12 21:04:50 -0700929/**
930 * allocate_cgrp_cset_links - allocate cgrp_cset_links
931 * @count: the number of links to allocate
932 * @tmp_links: list_head the allocated links are put on
933 *
934 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
935 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700936 */
Tejun Heo69d02062013-06-12 21:04:50 -0700937static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700938{
Tejun Heo69d02062013-06-12 21:04:50 -0700939 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700940 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700941
942 INIT_LIST_HEAD(tmp_links);
943
Li Zefan36553432008-07-29 22:33:19 -0700944 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700945 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700946 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700947 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700948 return -ENOMEM;
949 }
Tejun Heo69d02062013-06-12 21:04:50 -0700950 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700951 }
952 return 0;
953}
954
Li Zefanc12f65d2009-01-07 18:07:42 -0800955/**
956 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700957 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700958 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800959 * @cgrp: the destination cgroup
960 */
Tejun Heo69d02062013-06-12 21:04:50 -0700961static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
962 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800963{
Tejun Heo69d02062013-06-12 21:04:50 -0700964 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800965
Tejun Heo69d02062013-06-12 21:04:50 -0700966 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400967
968 if (cgroup_on_dfl(cgrp))
969 cset->dfl_cgrp = cgrp;
970
Tejun Heo69d02062013-06-12 21:04:50 -0700971 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
972 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700973 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400974
Paul Menage7717f7b2009-09-23 15:56:22 -0700975 /*
Tejun Heo389b9c12015-10-15 16:41:51 -0400976 * Always add links to the tail of the lists so that the lists are
977 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -0700978 */
Tejun Heo389b9c12015-10-15 16:41:51 -0400979 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -0700980 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400981
982 if (cgroup_parent(cgrp))
983 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -0800984}
985
Tejun Heob326f9d2013-06-24 15:21:48 -0700986/**
987 * find_css_set - return a new css_set with one cgroup updated
988 * @old_cset: the baseline css_set
989 * @cgrp: the cgroup to be updated
990 *
991 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
992 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700993 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700994static struct css_set *find_css_set(struct css_set *old_cset,
995 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700996{
Tejun Heob326f9d2013-06-24 15:21:48 -0700997 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700998 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700999 struct list_head tmp_links;
1000 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001001 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001002 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001003 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001004
Tejun Heob326f9d2013-06-24 15:21:48 -07001005 lockdep_assert_held(&cgroup_mutex);
1006
Paul Menage817929e2007-10-18 23:39:36 -07001007 /* First see if we already have a cgroup group that matches
1008 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001009 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001010 cset = find_existing_css_set(old_cset, cgrp, template);
1011 if (cset)
1012 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001013 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001014
Tejun Heo5abb8852013-06-12 21:04:49 -07001015 if (cset)
1016 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001017
Tejun Heof4f4be22013-06-12 21:04:51 -07001018 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001019 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001020 return NULL;
1021
Tejun Heo69d02062013-06-12 21:04:50 -07001022 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001023 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001024 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001025 return NULL;
1026 }
1027
Tejun Heo5abb8852013-06-12 21:04:49 -07001028 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001029 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001030 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001031 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001032 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001033 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001034 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001035 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001036
1037 /* Copy the set of subsystem state objects generated in
1038 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001039 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001040
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001041 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001042 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001043 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001044 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001045
Paul Menage7717f7b2009-09-23 15:56:22 -07001046 if (c->root == cgrp->root)
1047 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001048 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001049 }
Paul Menage817929e2007-10-18 23:39:36 -07001050
Tejun Heo69d02062013-06-12 21:04:50 -07001051 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001052
Paul Menage817929e2007-10-18 23:39:36 -07001053 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001054
Tejun Heo2d8f2432014-04-23 11:13:15 -04001055 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001056 key = css_set_hash(cset->subsys);
1057 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001058
Tejun Heo53254f92015-11-23 14:55:41 -05001059 for_each_subsys(ss, ssid) {
1060 struct cgroup_subsys_state *css = cset->subsys[ssid];
1061
Tejun Heo2d8f2432014-04-23 11:13:15 -04001062 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001063 &css->cgroup->e_csets[ssid]);
1064 css_get(css);
1065 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001066
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001067 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001068
Tejun Heo5abb8852013-06-12 21:04:49 -07001069 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001070}
1071
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001072static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001073{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001074 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001075
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001076 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001077}
1078
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001079static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001080{
1081 int id;
1082
1083 lockdep_assert_held(&cgroup_mutex);
1084
Tejun Heo985ed672014-03-19 10:23:53 -04001085 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001086 if (id < 0)
1087 return id;
1088
1089 root->hierarchy_id = id;
1090 return 0;
1091}
1092
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001093static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001094{
1095 lockdep_assert_held(&cgroup_mutex);
1096
1097 if (root->hierarchy_id) {
1098 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1099 root->hierarchy_id = 0;
1100 }
1101}
1102
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001103static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001104{
1105 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001106 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001107 WARN_ON_ONCE(root->hierarchy_id);
1108
1109 idr_destroy(&root->cgroup_idr);
1110 kfree(root);
1111 }
1112}
1113
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001114static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001115{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001116 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001117 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001118
Tejun Heo2bd59d42014-02-11 11:52:49 -05001119 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001120
Tejun Heo776f02f2014-02-12 09:29:50 -05001121 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001122 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001123
Tejun Heof2e85d52014-02-11 11:52:49 -05001124 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001125 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001126
1127 /*
1128 * Release all the links from cset_links to this hierarchy's
1129 * root cgroup
1130 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001131 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001132
1133 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1134 list_del(&link->cset_link);
1135 list_del(&link->cgrp_link);
1136 kfree(link);
1137 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001138
1139 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001140
1141 if (!list_empty(&root->root_list)) {
1142 list_del(&root->root_list);
1143 cgroup_root_count--;
1144 }
1145
1146 cgroup_exit_root_id(root);
1147
1148 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001149
Tejun Heo2bd59d42014-02-11 11:52:49 -05001150 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001151 cgroup_free_root(root);
1152}
1153
Tejun Heoceb6a082014-02-25 10:04:02 -05001154/* look up cgroup associated with given css_set on the specified hierarchy */
1155static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001156 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001157{
Paul Menage7717f7b2009-09-23 15:56:22 -07001158 struct cgroup *res = NULL;
1159
Tejun Heo96d365e2014-02-13 06:58:40 -05001160 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001161 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001162
Tejun Heo5abb8852013-06-12 21:04:49 -07001163 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001164 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001165 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001166 struct cgrp_cset_link *link;
1167
1168 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001169 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001170
Paul Menage7717f7b2009-09-23 15:56:22 -07001171 if (c->root == root) {
1172 res = c;
1173 break;
1174 }
1175 }
1176 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001177
Paul Menage7717f7b2009-09-23 15:56:22 -07001178 BUG_ON(!res);
1179 return res;
1180}
1181
1182/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001183 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001184 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001185 */
1186static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001187 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001188{
1189 /*
1190 * No need to lock the task - since we hold cgroup_mutex the
1191 * task can't change groups, so the only thing that can happen
1192 * is that it exits and its css is set back to init_css_set.
1193 */
1194 return cset_cgroup_from_root(task_css_set(task), root);
1195}
1196
1197/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001198 * A task must hold cgroup_mutex to modify cgroups.
1199 *
1200 * Any task can increment and decrement the count field without lock.
1201 * So in general, code holding cgroup_mutex can't rely on the count
1202 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001203 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001204 * means that no tasks are currently attached, therefore there is no
1205 * way a task attached to that cgroup can fork (the other way to
1206 * increment the count). So code holding cgroup_mutex can safely
1207 * assume that if the count is zero, it will stay zero. Similarly, if
1208 * a task holds cgroup_mutex on a cgroup with zero count, it
1209 * knows that the cgroup won't be removed, as cgroup_rmdir()
1210 * needs that mutex.
1211 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 * A cgroup can only be deleted if both its 'count' of using tasks
1213 * is zero, and its list of 'children' cgroups is empty. Since all
1214 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001215 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001216 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001217 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001218 *
1219 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001220 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001221 */
1222
Tejun Heo2bd59d42014-02-11 11:52:49 -05001223static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001224static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001225
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001226static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1227 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001228{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001229 struct cgroup_subsys *ss = cft->ss;
1230
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001231 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1232 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1233 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001234 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1235 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001236 else
1237 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1238 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239}
1240
Tejun Heof2e85d52014-02-11 11:52:49 -05001241/**
1242 * cgroup_file_mode - deduce file mode of a control file
1243 * @cft: the control file in question
1244 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001245 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001246 */
1247static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001248{
Tejun Heof2e85d52014-02-11 11:52:49 -05001249 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001250
Tejun Heof2e85d52014-02-11 11:52:49 -05001251 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1252 mode |= S_IRUGO;
1253
Tejun Heo7dbdb192015-09-18 17:54:23 -04001254 if (cft->write_u64 || cft->write_s64 || cft->write) {
1255 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1256 mode |= S_IWUGO;
1257 else
1258 mode |= S_IWUSR;
1259 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001260
1261 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001262}
1263
Tejun Heoa9746d82014-05-13 12:19:22 -04001264/**
Tejun Heo8699b772016-02-22 22:25:46 -05001265 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001266 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001267 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001268 *
1269 * On the default hierarchy, a subsystem may request other subsystems to be
1270 * enabled together through its ->depends_on mask. In such cases, more
1271 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1272 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001273 * This function calculates which subsystems need to be enabled if
1274 * @subtree_control is to be applied to @cgrp. The returned mask is always
1275 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001276 */
Tejun Heo6e5c8302016-02-22 22:25:47 -05001277static u16 cgroup_calc_subtree_ss_mask(struct cgroup *cgrp, u16 subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001278{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001279 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo6e5c8302016-02-22 22:25:47 -05001280 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001281 struct cgroup_subsys *ss;
1282 int ssid;
1283
1284 lockdep_assert_held(&cgroup_mutex);
1285
Tejun Heo0f060de2014-11-18 02:49:50 -05001286 if (!cgroup_on_dfl(cgrp))
1287 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001288
1289 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001290 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001291
Tejun Heob4e0eea2016-02-22 22:25:46 -05001292 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001293 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001294 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001295
1296 /*
1297 * Mask out subsystems which aren't available. This can
1298 * happen only if some depended-upon subsystems were bound
1299 * to non-default hierarchies.
1300 */
1301 if (parent)
Tejun Heo8699b772016-02-22 22:25:46 -05001302 new_ss_mask &= parent->subtree_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001303 else
1304 new_ss_mask &= cgrp->root->subsys_mask;
1305
1306 if (new_ss_mask == cur_ss_mask)
1307 break;
1308 cur_ss_mask = new_ss_mask;
1309 }
1310
Tejun Heo0f060de2014-11-18 02:49:50 -05001311 return cur_ss_mask;
1312}
1313
1314/**
Tejun Heo8699b772016-02-22 22:25:46 -05001315 * cgroup_refresh_subtree_ss_mask - update subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001316 * @cgrp: the target cgroup
1317 *
Tejun Heo8699b772016-02-22 22:25:46 -05001318 * Update @cgrp->subtree_ss_mask according to the current
1319 * @cgrp->subtree_control using cgroup_calc_subtree_ss_mask().
Tejun Heo0f060de2014-11-18 02:49:50 -05001320 */
Tejun Heo8699b772016-02-22 22:25:46 -05001321static void cgroup_refresh_subtree_ss_mask(struct cgroup *cgrp)
Tejun Heo0f060de2014-11-18 02:49:50 -05001322{
Tejun Heo8699b772016-02-22 22:25:46 -05001323 cgrp->subtree_ss_mask =
1324 cgroup_calc_subtree_ss_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001325}
1326
Tejun Heoa9746d82014-05-13 12:19:22 -04001327/**
1328 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1329 * @kn: the kernfs_node being serviced
1330 *
1331 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1332 * the method finishes if locking succeeded. Note that once this function
1333 * returns the cgroup returned by cgroup_kn_lock_live() may become
1334 * inaccessible any time. If the caller intends to continue to access the
1335 * cgroup, it should pin it before invoking this function.
1336 */
1337static void cgroup_kn_unlock(struct kernfs_node *kn)
1338{
1339 struct cgroup *cgrp;
1340
1341 if (kernfs_type(kn) == KERNFS_DIR)
1342 cgrp = kn->priv;
1343 else
1344 cgrp = kn->parent->priv;
1345
1346 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001347
1348 kernfs_unbreak_active_protection(kn);
1349 cgroup_put(cgrp);
1350}
1351
1352/**
1353 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1354 * @kn: the kernfs_node being serviced
1355 *
1356 * This helper is to be used by a cgroup kernfs method currently servicing
1357 * @kn. It breaks the active protection, performs cgroup locking and
1358 * verifies that the associated cgroup is alive. Returns the cgroup if
1359 * alive; otherwise, %NULL. A successful return should be undone by a
1360 * matching cgroup_kn_unlock() invocation.
1361 *
1362 * Any cgroup kernfs method implementation which requires locking the
1363 * associated cgroup should use this helper. It avoids nesting cgroup
1364 * locking under kernfs active protection and allows all kernfs operations
1365 * including self-removal.
1366 */
1367static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1368{
1369 struct cgroup *cgrp;
1370
1371 if (kernfs_type(kn) == KERNFS_DIR)
1372 cgrp = kn->priv;
1373 else
1374 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001375
Tejun Heo2bd59d42014-02-11 11:52:49 -05001376 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001377 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001378 * active_ref. cgroup liveliness check alone provides enough
1379 * protection against removal. Ensure @cgrp stays accessible and
1380 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001381 */
Li Zefanaa323622014-09-04 14:43:38 +08001382 if (!cgroup_tryget(cgrp))
1383 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001384 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385
Tejun Heoa9746d82014-05-13 12:19:22 -04001386 mutex_lock(&cgroup_mutex);
1387
1388 if (!cgroup_is_dead(cgrp))
1389 return cgrp;
1390
1391 cgroup_kn_unlock(kn);
1392 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393}
1394
Li Zefan2739d3c2013-01-21 18:18:33 +08001395static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001397 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398
Tejun Heo01f64742014-05-13 12:19:23 -04001399 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001400
1401 if (cft->file_offset) {
1402 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1403 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1404
1405 spin_lock_irq(&cgroup_file_kn_lock);
1406 cfile->kn = NULL;
1407 spin_unlock_irq(&cgroup_file_kn_lock);
1408 }
1409
Tejun Heo2bd59d42014-02-11 11:52:49 -05001410 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001411}
1412
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001413/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001414 * css_clear_dir - remove subsys files in a cgroup directory
1415 * @css: taget css
1416 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001417 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001418static void css_clear_dir(struct cgroup_subsys_state *css,
1419 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001420{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001421 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1422 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001423
Tejun Heo4df8dc92015-09-18 17:54:23 -04001424 list_for_each_entry(cfts, &css->ss->cfts, node)
1425 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426}
1427
Tejun Heoccdca212015-09-18 17:54:23 -04001428/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001429 * css_populate_dir - create subsys files in a cgroup directory
1430 * @css: target css
1431 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001432 *
1433 * On failure, no file is added.
1434 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001435static int css_populate_dir(struct cgroup_subsys_state *css,
1436 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001437{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001438 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1439 struct cftype *cfts, *failed_cfts;
1440 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001441
Tejun Heo4df8dc92015-09-18 17:54:23 -04001442 if (!css->ss) {
1443 if (cgroup_on_dfl(cgrp))
1444 cfts = cgroup_dfl_base_files;
1445 else
1446 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001447
Tejun Heo4df8dc92015-09-18 17:54:23 -04001448 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1449 }
Tejun Heoccdca212015-09-18 17:54:23 -04001450
Tejun Heo4df8dc92015-09-18 17:54:23 -04001451 list_for_each_entry(cfts, &css->ss->cfts, node) {
1452 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1453 if (ret < 0) {
1454 failed_cfts = cfts;
1455 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001456 }
1457 }
1458 return 0;
1459err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001460 list_for_each_entry(cfts, &css->ss->cfts, node) {
1461 if (cfts == failed_cfts)
1462 break;
1463 cgroup_addrm_files(css, cgrp, cfts, false);
1464 }
Tejun Heoccdca212015-09-18 17:54:23 -04001465 return ret;
1466}
1467
Tejun Heo6e5c8302016-02-22 22:25:47 -05001468static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469{
Tejun Heo1ada4832015-09-18 17:54:23 -04001470 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001471 struct cgroup_subsys *ss;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001472 u16 tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001473 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474
Tejun Heoace2bee2014-02-11 11:52:47 -05001475 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001476
Tejun Heob4e0eea2016-02-22 22:25:46 -05001477 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001478 /* if @ss has non-root csses attached to it, can't move */
1479 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001480 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481
Tejun Heo5df36032014-03-19 10:23:54 -04001482 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001483 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001484 return -EBUSY;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001485 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486
Tejun Heo5533e012014-05-14 19:33:07 -04001487 /* skip creating root files on dfl_root for inhibited subsystems */
1488 tmp_ss_mask = ss_mask;
1489 if (dst_root == &cgrp_dfl_root)
Tejun Heoa7165262016-02-23 10:00:50 -05001490 tmp_ss_mask &= ~cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -04001491
Tejun Heob4e0eea2016-02-22 22:25:46 -05001492 do_each_subsys_mask(ss, ssid, tmp_ss_mask) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04001493 struct cgroup *scgrp = &ss->root->cgrp;
1494 int tssid;
1495
1496 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1497 if (!ret)
1498 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001499
Tejun Heoa2dd4242014-03-19 10:23:55 -04001500 /*
1501 * Rebinding back to the default root is not allowed to
1502 * fail. Using both default and non-default roots should
1503 * be rare. Moving subsystems back and forth even more so.
1504 * Just warn about it and continue.
1505 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001506 if (dst_root == &cgrp_dfl_root) {
Tejun Heoa7165262016-02-23 10:00:50 -05001507 if (cgrp_dfl_visible) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001508 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Tejun Heo4df8dc92015-09-18 17:54:23 -04001509 ret, ss_mask);
1510 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1511 }
1512 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001513 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001514
Tejun Heob4e0eea2016-02-22 22:25:46 -05001515 do_each_subsys_mask(ss, tssid, tmp_ss_mask) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04001516 if (tssid == ssid)
1517 break;
1518 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001519 } while_each_subsys_mask();
Tejun Heo4df8dc92015-09-18 17:54:23 -04001520 return ret;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001521 } while_each_subsys_mask();
Tejun Heo31261212013-06-28 17:07:30 -07001522
1523 /*
1524 * Nothing can fail from this point on. Remove files for the
1525 * removed subsystems and rebind each subsystem.
1526 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05001527 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001528 struct cgroup_root *src_root = ss->root;
1529 struct cgroup *scgrp = &src_root->cgrp;
1530 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001531 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001532
Tejun Heo1ada4832015-09-18 17:54:23 -04001533 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001534
Tejun Heo4df8dc92015-09-18 17:54:23 -04001535 css_clear_dir(css, NULL);
1536
Tejun Heo1ada4832015-09-18 17:54:23 -04001537 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1538 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001539 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001540 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001541
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001542 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001543 hash_for_each(css_set_table, i, cset, hlist)
1544 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001545 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001546 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001547
Tejun Heof392e512014-04-23 11:13:14 -04001548 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001549 scgrp->subtree_control &= ~(1 << ssid);
Tejun Heo8699b772016-02-22 22:25:46 -05001550 cgroup_refresh_subtree_ss_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001551
Tejun Heobd53d612014-04-23 11:13:16 -04001552 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001553 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001554 if (dst_root == &cgrp_dfl_root) {
1555 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1556 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001557 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo8699b772016-02-22 22:25:46 -05001558 cgroup_refresh_subtree_ss_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001559 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001560 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001561
Tejun Heo5df36032014-03-19 10:23:54 -04001562 if (ss->bind)
1563 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001564 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565
Tejun Heo1ada4832015-09-18 17:54:23 -04001566 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 return 0;
1568}
1569
Tejun Heo2bd59d42014-02-11 11:52:49 -05001570static int cgroup_show_options(struct seq_file *seq,
1571 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001573 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001575 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576
Tejun Heod98817d2015-08-18 13:58:16 -07001577 if (root != &cgrp_dfl_root)
1578 for_each_subsys(ss, ssid)
1579 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001580 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001581 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001583 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001584 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001585
1586 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001587 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001588 seq_show_option(seq, "release_agent",
1589 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001590 spin_unlock(&release_agent_path_lock);
1591
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001592 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001593 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001595 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596 return 0;
1597}
1598
1599struct cgroup_sb_opts {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001600 u16 subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001601 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001602 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001603 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001605 /* User explicitly requested empty subsystem */
1606 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607};
1608
Ben Blumcf5d5942010-03-10 15:22:09 -08001609static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001611 char *token, *o = data;
1612 bool all_ss = false, one_ss = false;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001613 u16 mask = U16_MAX;
Tejun Heo30159ec2013-06-25 11:53:37 -07001614 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001615 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001616 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001617
1618#ifdef CONFIG_CPUSETS
Tejun Heo6e5c8302016-02-22 22:25:47 -05001619 mask = ~((u16)1 << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001620#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Paul Menagec6d57f32009-09-23 15:56:19 -07001622 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
1624 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001625 nr_opts++;
1626
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627 if (!*token)
1628 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001629 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001630 /* Explicitly have no subsystems */
1631 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001632 continue;
1633 }
1634 if (!strcmp(token, "all")) {
1635 /* Mutually exclusive option 'all' + subsystem name */
1636 if (one_ss)
1637 return -EINVAL;
1638 all_ss = true;
1639 continue;
1640 }
1641 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001642 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001643 continue;
1644 }
1645 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001646 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001647 continue;
1648 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001649 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001650 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001651 continue;
1652 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001653 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001654 /* Specifying two release agents is forbidden */
1655 if (opts->release_agent)
1656 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001657 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001658 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001659 if (!opts->release_agent)
1660 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001661 continue;
1662 }
1663 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001664 const char *name = token + 5;
1665 /* Can't specify an empty name */
1666 if (!strlen(name))
1667 return -EINVAL;
1668 /* Must match [\w.-]+ */
1669 for (i = 0; i < strlen(name); i++) {
1670 char c = name[i];
1671 if (isalnum(c))
1672 continue;
1673 if ((c == '.') || (c == '-') || (c == '_'))
1674 continue;
1675 return -EINVAL;
1676 }
1677 /* Specifying two names is forbidden */
1678 if (opts->name)
1679 return -EINVAL;
1680 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001681 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001682 GFP_KERNEL);
1683 if (!opts->name)
1684 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001685
1686 continue;
1687 }
1688
Tejun Heo30159ec2013-06-25 11:53:37 -07001689 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001690 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001691 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001692 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001693 continue;
Johannes Weiner223ffb22016-02-11 13:34:49 -05001694 if (cgroup_ssid_no_v1(i))
1695 continue;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001696
1697 /* Mutually exclusive option 'all' + subsystem name */
1698 if (all_ss)
1699 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001700 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001701 one_ss = true;
1702
1703 break;
1704 }
1705 if (i == CGROUP_SUBSYS_COUNT)
1706 return -ENOENT;
1707 }
1708
Li Zefanf9ab5b52009-06-17 16:26:33 -07001709 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001710 * If the 'all' option was specified select all the subsystems,
1711 * otherwise if 'none', 'name=' and a subsystem name options were
1712 * not specified, let's default to 'all'
1713 */
1714 if (all_ss || (!one_ss && !opts->none && !opts->name))
1715 for_each_subsys(ss, i)
Johannes Weiner223ffb22016-02-11 13:34:49 -05001716 if (cgroup_ssid_enabled(i) && !cgroup_ssid_no_v1(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001717 opts->subsys_mask |= (1 << i);
1718
1719 /*
1720 * We either have to specify by name or by subsystems. (So all
1721 * empty hierarchies must have a name).
1722 */
1723 if (!opts->subsys_mask && !opts->name)
1724 return -EINVAL;
1725
1726 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001727 * Option noprefix was introduced just for backward compatibility
1728 * with the old cpuset, so we allow noprefix only if mounting just
1729 * the cpuset subsystem.
1730 */
Tejun Heo93438622013-04-14 20:15:25 -07001731 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001732 return -EINVAL;
1733
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001734 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001735 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001736 return -EINVAL;
1737
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 return 0;
1739}
1740
Tejun Heo2bd59d42014-02-11 11:52:49 -05001741static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742{
1743 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001744 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745 struct cgroup_sb_opts opts;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001746 u16 added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747
Tejun Heoaa6ec292014-07-09 10:08:08 -04001748 if (root == &cgrp_dfl_root) {
1749 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001750 return -EINVAL;
1751 }
1752
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 mutex_lock(&cgroup_mutex);
1754
1755 /* See what subsystems are wanted */
1756 ret = parse_cgroupfs_options(data, &opts);
1757 if (ret)
1758 goto out_unlock;
1759
Tejun Heof392e512014-04-23 11:13:14 -04001760 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001761 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001762 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001763
Tejun Heof392e512014-04-23 11:13:14 -04001764 added_mask = opts.subsys_mask & ~root->subsys_mask;
1765 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001766
Ben Blumcf5d5942010-03-10 15:22:09 -08001767 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001768 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001769 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001770 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001771 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001773 goto out_unlock;
1774 }
1775
Tejun Heof172e672013-06-28 17:07:30 -07001776 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001777 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001778 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001779 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001780 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781
Tejun Heo5df36032014-03-19 10:23:54 -04001782 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001783 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001786 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001787
Tejun Heo69e943b2014-02-08 10:36:58 -05001788 if (opts.release_agent) {
1789 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001790 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001791 spin_unlock(&release_agent_path_lock);
1792 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001794 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001795 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 return ret;
1798}
1799
Tejun Heoafeb0f92014-02-13 06:58:39 -05001800/*
1801 * To reduce the fork() overhead for systems that are not actually using
1802 * their cgroups capability, we don't maintain the lists running through
1803 * each css_set to its tasks until we see the list actually used - in other
1804 * words after the first mount.
1805 */
1806static bool use_task_css_set_links __read_mostly;
1807
1808static void cgroup_enable_task_cg_lists(void)
1809{
1810 struct task_struct *p, *g;
1811
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001812 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001813
1814 if (use_task_css_set_links)
1815 goto out_unlock;
1816
1817 use_task_css_set_links = true;
1818
1819 /*
1820 * We need tasklist_lock because RCU is not safe against
1821 * while_each_thread(). Besides, a forking task that has passed
1822 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1823 * is not guaranteed to have its child immediately visible in the
1824 * tasklist if we walk through it with RCU.
1825 */
1826 read_lock(&tasklist_lock);
1827 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001828 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1829 task_css_set(p) != &init_css_set);
1830
1831 /*
1832 * We should check if the process is exiting, otherwise
1833 * it will race with cgroup_exit() in that the list
1834 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001835 * Do it while holding siglock so that we don't end up
1836 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001837 */
Tejun Heof153ad12014-02-25 09:56:49 -05001838 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001839 if (!(p->flags & PF_EXITING)) {
1840 struct css_set *cset = task_css_set(p);
1841
Tejun Heo0de09422015-10-15 16:41:49 -04001842 if (!css_set_populated(cset))
1843 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001844 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001845 get_css_set(cset);
1846 }
Tejun Heof153ad12014-02-25 09:56:49 -05001847 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001848 } while_each_thread(g, p);
1849 read_unlock(&tasklist_lock);
1850out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001851 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001852}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001853
Paul Menagecc31edc2008-10-18 20:28:04 -07001854static void init_cgroup_housekeeping(struct cgroup *cgrp)
1855{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001856 struct cgroup_subsys *ss;
1857 int ssid;
1858
Tejun Heod5c419b2014-05-16 13:22:48 -04001859 INIT_LIST_HEAD(&cgrp->self.sibling);
1860 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001861 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001862 INIT_LIST_HEAD(&cgrp->pidlists);
1863 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001864 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001865 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001866
1867 for_each_subsys(ss, ssid)
1868 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001869
1870 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001871 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001872}
Paul Menagec6d57f32009-09-23 15:56:19 -07001873
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001874static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001875 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001876{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001877 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001878
Paul Menageddbcc7e2007-10-18 23:39:30 -07001879 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001880 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001881 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001882 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001883 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001884
Paul Menagec6d57f32009-09-23 15:56:19 -07001885 root->flags = opts->flags;
1886 if (opts->release_agent)
1887 strcpy(root->release_agent_path, opts->release_agent);
1888 if (opts->name)
1889 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001890 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001891 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001892}
1893
Tejun Heo6e5c8302016-02-22 22:25:47 -05001894static int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001895{
Tejun Heod427dfe2014-02-11 11:52:48 -05001896 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001897 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001898 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001899 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001900
Tejun Heod427dfe2014-02-11 11:52:48 -05001901 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001902
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001903 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001904 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001905 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001906 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001907 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001908
Tejun Heo2aad2a82014-09-24 13:31:50 -04001909 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1910 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001911 if (ret)
1912 goto out;
1913
Tejun Heod427dfe2014-02-11 11:52:48 -05001914 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001915 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001916 * but that's OK - it can only be increased by someone holding
1917 * cgroup_lock, and that's us. The worst that can happen is that we
1918 * have some link structures left over
1919 */
1920 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001921 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001922 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001923
Tejun Heo985ed672014-03-19 10:23:53 -04001924 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001925 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001926 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001927
Tejun Heo2bd59d42014-02-11 11:52:49 -05001928 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1929 KERNFS_ROOT_CREATE_DEACTIVATED,
1930 root_cgrp);
1931 if (IS_ERR(root->kf_root)) {
1932 ret = PTR_ERR(root->kf_root);
1933 goto exit_root_id;
1934 }
1935 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001936
Tejun Heo4df8dc92015-09-18 17:54:23 -04001937 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001938 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001939 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001940
Tejun Heo5df36032014-03-19 10:23:54 -04001941 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001942 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001943 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001944
Tejun Heod427dfe2014-02-11 11:52:48 -05001945 /*
1946 * There must be no failure case after here, since rebinding takes
1947 * care of subsystems' refcounts, which are explicitly dropped in
1948 * the failure exit path.
1949 */
1950 list_add(&root->root_list, &cgroup_roots);
1951 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001952
Tejun Heod427dfe2014-02-11 11:52:48 -05001953 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001954 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001955 * objects.
1956 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001957 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001958 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001959 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001960 if (css_set_populated(cset))
1961 cgroup_update_populated(root_cgrp, true);
1962 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001963 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001964
Tejun Heod5c419b2014-05-16 13:22:48 -04001965 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001966 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001967
Tejun Heo2bd59d42014-02-11 11:52:49 -05001968 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001969 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001970 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001971
Tejun Heo2bd59d42014-02-11 11:52:49 -05001972destroy_root:
1973 kernfs_destroy_root(root->kf_root);
1974 root->kf_root = NULL;
1975exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001976 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001977cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001978 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001979out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001980 free_cgrp_cset_links(&tmp_links);
1981 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001982}
1983
Al Virof7e83572010-07-26 13:23:11 +04001984static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001985 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001986 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001987{
Tejun Heo67e9c742015-11-16 11:13:34 -05001988 bool is_v2 = fs_type == &cgroup2_fs_type;
Li Zefan3a32bd72014-06-30 11:50:59 +08001989 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001990 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001991 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001992 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001993 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001994 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001995 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001996 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001997
1998 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001999 * The first time anyone tries to mount a cgroup, enable the list
2000 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07002001 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05002002 if (!use_task_css_set_links)
2003 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002004
Tejun Heo67e9c742015-11-16 11:13:34 -05002005 if (is_v2) {
2006 if (data) {
2007 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
2008 return ERR_PTR(-EINVAL);
2009 }
Tejun Heoa7165262016-02-23 10:00:50 -05002010 cgrp_dfl_visible = true;
Tejun Heo67e9c742015-11-16 11:13:34 -05002011 root = &cgrp_dfl_root;
2012 cgroup_get(&root->cgrp);
2013 goto out_mount;
2014 }
2015
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002016 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002017
Paul Menageddbcc7e2007-10-18 23:39:30 -07002018 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002019 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002020 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002021 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002022
Li Zefan970317a2014-06-30 11:49:58 +08002023 /*
2024 * Destruction of cgroup root is asynchronous, so subsystems may
2025 * still be dying after the previous unmount. Let's drain the
2026 * dying subsystems. We just need to ensure that the ones
2027 * unmounted previously finish dying and don't care about new ones
2028 * starting. Testing ref liveliness is good enough.
2029 */
2030 for_each_subsys(ss, i) {
2031 if (!(opts.subsys_mask & (1 << i)) ||
2032 ss->root == &cgrp_dfl_root)
2033 continue;
2034
2035 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2036 mutex_unlock(&cgroup_mutex);
2037 msleep(10);
2038 ret = restart_syscall();
2039 goto out_free;
2040 }
2041 cgroup_put(&ss->root->cgrp);
2042 }
2043
Tejun Heo985ed672014-03-19 10:23:53 -04002044 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002045 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002046
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002047 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002048 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002049
Paul Menage817929e2007-10-18 23:39:36 -07002050 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002051 * If we asked for a name then it must match. Also, if
2052 * name matches but sybsys_mask doesn't, we should fail.
2053 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002054 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002055 if (opts.name) {
2056 if (strcmp(opts.name, root->name))
2057 continue;
2058 name_match = true;
2059 }
Tejun Heo31261212013-06-28 17:07:30 -07002060
2061 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002062 * If we asked for subsystems (or explicitly for no
2063 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002064 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002065 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002066 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002067 if (!name_match)
2068 continue;
2069 ret = -EBUSY;
2070 goto out_unlock;
2071 }
Tejun Heo873fe092013-04-14 20:15:26 -07002072
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002073 if (root->flags ^ opts.flags)
2074 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002075
Tejun Heo776f02f2014-02-12 09:29:50 -05002076 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002077 * We want to reuse @root whose lifetime is governed by its
2078 * ->cgrp. Let's check whether @root is alive and keep it
2079 * that way. As cgroup_kill_sb() can happen anytime, we
2080 * want to block it by pinning the sb so that @root doesn't
2081 * get killed before mount is complete.
2082 *
2083 * With the sb pinned, tryget_live can reliably indicate
2084 * whether @root can be reused. If it's being killed,
2085 * drain it. We can use wait_queue for the wait but this
2086 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002087 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002088 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2089 if (IS_ERR(pinned_sb) ||
2090 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002091 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002092 if (!IS_ERR_OR_NULL(pinned_sb))
2093 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002094 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002095 ret = restart_syscall();
2096 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002097 }
2098
2099 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002100 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002101 }
2102
Tejun Heo172a2c062014-03-19 10:23:53 -04002103 /*
2104 * No such thing, create a new one. name= matching without subsys
2105 * specification is allowed for already existing hierarchies but we
2106 * can't create new one without subsys specification.
2107 */
2108 if (!opts.subsys_mask && !opts.none) {
2109 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002110 goto out_unlock;
2111 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002112
Tejun Heo172a2c062014-03-19 10:23:53 -04002113 root = kzalloc(sizeof(*root), GFP_KERNEL);
2114 if (!root) {
2115 ret = -ENOMEM;
2116 goto out_unlock;
2117 }
2118
2119 init_cgroup_root(root, &opts);
2120
Tejun Heo35585572014-02-13 06:58:38 -05002121 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002122 if (ret)
2123 cgroup_free_root(root);
2124
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002125out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002126 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002127out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002128 kfree(opts.release_agent);
2129 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002130
Tejun Heo2bd59d42014-02-11 11:52:49 -05002131 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002132 return ERR_PTR(ret);
Tejun Heo67e9c742015-11-16 11:13:34 -05002133out_mount:
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002134 dentry = kernfs_mount(fs_type, flags, root->kf_root,
Tejun Heo67e9c742015-11-16 11:13:34 -05002135 is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
2136 &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002137 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002138 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002139
2140 /*
2141 * If @pinned_sb, we're reusing an existing root and holding an
2142 * extra ref on its sb. Mount is complete. Put the extra ref.
2143 */
2144 if (pinned_sb) {
2145 WARN_ON(new_sb);
2146 deactivate_super(pinned_sb);
2147 }
2148
Tejun Heo2bd59d42014-02-11 11:52:49 -05002149 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002150}
2151
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002152static void cgroup_kill_sb(struct super_block *sb)
2153{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002154 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002155 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002156
Tejun Heo9d755d32014-05-14 09:15:02 -04002157 /*
2158 * If @root doesn't have any mounts or children, start killing it.
2159 * This prevents new mounts by disabling percpu_ref_tryget_live().
2160 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002161 *
2162 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002163 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002164 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002165 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002166 cgroup_put(&root->cgrp);
2167 else
2168 percpu_ref_kill(&root->cgrp.self.refcnt);
2169
Tejun Heo2bd59d42014-02-11 11:52:49 -05002170 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002171}
2172
2173static struct file_system_type cgroup_fs_type = {
2174 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002175 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002176 .kill_sb = cgroup_kill_sb,
2177};
2178
Tejun Heo67e9c742015-11-16 11:13:34 -05002179static struct file_system_type cgroup2_fs_type = {
2180 .name = "cgroup2",
2181 .mount = cgroup_mount,
2182 .kill_sb = cgroup_kill_sb,
2183};
2184
Li Zefana043e3b2008-02-23 15:24:09 -08002185/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002186 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002187 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002188 * @buf: the buffer to write the path into
2189 * @buflen: the length of the buffer
2190 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002191 * Determine @task's cgroup on the first (the one with the lowest non-zero
2192 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2193 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2194 * cgroup controller callbacks.
2195 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002196 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002197 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002198char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002199{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002200 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002201 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002202 int hierarchy_id = 1;
2203 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002204
2205 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002206 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002207
Tejun Heo913ffdb2013-07-11 16:34:48 -07002208 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2209
Tejun Heo857a2be2013-04-14 20:50:08 -07002210 if (root) {
2211 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002212 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002213 } else {
2214 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002215 if (strlcpy(buf, "/", buflen) < buflen)
2216 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002217 }
2218
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002219 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002220 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002221 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002222}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002223EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002224
Tejun Heob3dc0942014-02-25 10:04:01 -05002225/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002226struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002227 /* the src and dst cset list running through cset->mg_node */
2228 struct list_head src_csets;
2229 struct list_head dst_csets;
2230
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002231 /* the subsys currently being processed */
2232 int ssid;
2233
Tejun Heob3dc0942014-02-25 10:04:01 -05002234 /*
2235 * Fields for cgroup_taskset_*() iteration.
2236 *
2237 * Before migration is committed, the target migration tasks are on
2238 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2239 * the csets on ->dst_csets. ->csets point to either ->src_csets
2240 * or ->dst_csets depending on whether migration is committed.
2241 *
2242 * ->cur_csets and ->cur_task point to the current task position
2243 * during iteration.
2244 */
2245 struct list_head *csets;
2246 struct css_set *cur_cset;
2247 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002248};
2249
Tejun Heoadaae5d2015-09-11 15:00:21 -04002250#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2251 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2252 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2253 .csets = &tset.src_csets, \
2254}
2255
2256/**
2257 * cgroup_taskset_add - try to add a migration target task to a taskset
2258 * @task: target task
2259 * @tset: target taskset
2260 *
2261 * Add @task, which is a migration target, to @tset. This function becomes
2262 * noop if @task doesn't need to be migrated. @task's css_set should have
2263 * been added as a migration source and @task->cg_list will be moved from
2264 * the css_set's tasks list to mg_tasks one.
2265 */
2266static void cgroup_taskset_add(struct task_struct *task,
2267 struct cgroup_taskset *tset)
2268{
2269 struct css_set *cset;
2270
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002271 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002272
2273 /* @task either already exited or can't exit until the end */
2274 if (task->flags & PF_EXITING)
2275 return;
2276
2277 /* leave @task alone if post_fork() hasn't linked it yet */
2278 if (list_empty(&task->cg_list))
2279 return;
2280
2281 cset = task_css_set(task);
2282 if (!cset->mg_src_cgrp)
2283 return;
2284
2285 list_move_tail(&task->cg_list, &cset->mg_tasks);
2286 if (list_empty(&cset->mg_node))
2287 list_add_tail(&cset->mg_node, &tset->src_csets);
2288 if (list_empty(&cset->mg_dst_cset->mg_node))
2289 list_move_tail(&cset->mg_dst_cset->mg_node,
2290 &tset->dst_csets);
2291}
2292
Tejun Heo2f7ee562011-12-12 18:12:21 -08002293/**
2294 * cgroup_taskset_first - reset taskset and return the first task
2295 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002296 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002297 *
2298 * @tset iteration is initialized and the first task is returned.
2299 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002300struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2301 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002302{
Tejun Heob3dc0942014-02-25 10:04:01 -05002303 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2304 tset->cur_task = NULL;
2305
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002306 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002307}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002308
2309/**
2310 * cgroup_taskset_next - iterate to the next task in taskset
2311 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002312 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002313 *
2314 * Return the next task in @tset. Iteration must have been initialized
2315 * with cgroup_taskset_first().
2316 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002317struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2318 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002319{
Tejun Heob3dc0942014-02-25 10:04:01 -05002320 struct css_set *cset = tset->cur_cset;
2321 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002322
Tejun Heob3dc0942014-02-25 10:04:01 -05002323 while (&cset->mg_node != tset->csets) {
2324 if (!task)
2325 task = list_first_entry(&cset->mg_tasks,
2326 struct task_struct, cg_list);
2327 else
2328 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002329
Tejun Heob3dc0942014-02-25 10:04:01 -05002330 if (&task->cg_list != &cset->mg_tasks) {
2331 tset->cur_cset = cset;
2332 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002333
2334 /*
2335 * This function may be called both before and
2336 * after cgroup_taskset_migrate(). The two cases
2337 * can be distinguished by looking at whether @cset
2338 * has its ->mg_dst_cset set.
2339 */
2340 if (cset->mg_dst_cset)
2341 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2342 else
2343 *dst_cssp = cset->subsys[tset->ssid];
2344
Tejun Heob3dc0942014-02-25 10:04:01 -05002345 return task;
2346 }
2347
2348 cset = list_next_entry(cset, mg_node);
2349 task = NULL;
2350 }
2351
2352 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002353}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002354
2355/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002356 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2357 * @tset: taget taskset
2358 * @dst_cgrp: destination cgroup
2359 *
2360 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2361 * ->can_attach callbacks fails and guarantees that either all or none of
2362 * the tasks in @tset are migrated. @tset is consumed regardless of
2363 * success.
2364 */
2365static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2366 struct cgroup *dst_cgrp)
2367{
2368 struct cgroup_subsys_state *css, *failed_css = NULL;
2369 struct task_struct *task, *tmp_task;
2370 struct css_set *cset, *tmp_cset;
2371 int i, ret;
2372
2373 /* methods shouldn't be called if no task is actually migrating */
2374 if (list_empty(&tset->src_csets))
2375 return 0;
2376
2377 /* check that we can legitimately attach to the cgroup */
2378 for_each_e_css(css, i, dst_cgrp) {
2379 if (css->ss->can_attach) {
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002380 tset->ssid = i;
2381 ret = css->ss->can_attach(tset);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002382 if (ret) {
2383 failed_css = css;
2384 goto out_cancel_attach;
2385 }
2386 }
2387 }
2388
2389 /*
2390 * Now that we're guaranteed success, proceed to move all tasks to
2391 * the new cgroup. There are no failure cases after here, so this
2392 * is the commit point.
2393 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002394 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002395 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002396 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2397 struct css_set *from_cset = task_css_set(task);
2398 struct css_set *to_cset = cset->mg_dst_cset;
2399
2400 get_css_set(to_cset);
2401 css_set_move_task(task, from_cset, to_cset, true);
2402 put_css_set_locked(from_cset);
2403 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002404 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002405 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002406
2407 /*
2408 * Migration is committed, all target tasks are now on dst_csets.
2409 * Nothing is sensitive to fork() after this point. Notify
2410 * controllers that migration is complete.
2411 */
2412 tset->csets = &tset->dst_csets;
2413
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002414 for_each_e_css(css, i, dst_cgrp) {
2415 if (css->ss->attach) {
2416 tset->ssid = i;
2417 css->ss->attach(tset);
2418 }
2419 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002420
2421 ret = 0;
2422 goto out_release_tset;
2423
2424out_cancel_attach:
2425 for_each_e_css(css, i, dst_cgrp) {
2426 if (css == failed_css)
2427 break;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002428 if (css->ss->cancel_attach) {
2429 tset->ssid = i;
2430 css->ss->cancel_attach(tset);
2431 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002432 }
2433out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002434 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002435 list_splice_init(&tset->dst_csets, &tset->src_csets);
2436 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2437 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2438 list_del_init(&cset->mg_node);
2439 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002440 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002441 return ret;
2442}
2443
2444/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002445 * cgroup_migrate_finish - cleanup after attach
2446 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002447 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002448 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2449 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002450 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002451static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002452{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002453 struct css_set *cset, *tmp_cset;
2454
2455 lockdep_assert_held(&cgroup_mutex);
2456
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002457 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002458 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2459 cset->mg_src_cgrp = NULL;
2460 cset->mg_dst_cset = NULL;
2461 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002462 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002463 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002464 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002465}
2466
2467/**
2468 * cgroup_migrate_add_src - add a migration source css_set
2469 * @src_cset: the source css_set to add
2470 * @dst_cgrp: the destination cgroup
2471 * @preloaded_csets: list of preloaded css_sets
2472 *
2473 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2474 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2475 * up by cgroup_migrate_finish().
2476 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002477 * This function may be called without holding cgroup_threadgroup_rwsem
2478 * even if the target is a process. Threads may be created and destroyed
2479 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2480 * into play and the preloaded css_sets are guaranteed to cover all
2481 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002482 */
2483static void cgroup_migrate_add_src(struct css_set *src_cset,
2484 struct cgroup *dst_cgrp,
2485 struct list_head *preloaded_csets)
2486{
2487 struct cgroup *src_cgrp;
2488
2489 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002490 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002491
2492 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2493
Tejun Heo1958d2d2014-02-25 10:04:03 -05002494 if (!list_empty(&src_cset->mg_preload_node))
2495 return;
2496
2497 WARN_ON(src_cset->mg_src_cgrp);
2498 WARN_ON(!list_empty(&src_cset->mg_tasks));
2499 WARN_ON(!list_empty(&src_cset->mg_node));
2500
2501 src_cset->mg_src_cgrp = src_cgrp;
2502 get_css_set(src_cset);
2503 list_add(&src_cset->mg_preload_node, preloaded_csets);
2504}
2505
2506/**
2507 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002508 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002509 * @preloaded_csets: list of preloaded source css_sets
2510 *
2511 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2512 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002513 * pins all destination css_sets, links each to its source, and append them
2514 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2515 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002516 *
2517 * This function must be called after cgroup_migrate_add_src() has been
2518 * called on each migration source css_set. After migration is performed
2519 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2520 * @preloaded_csets.
2521 */
2522static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2523 struct list_head *preloaded_csets)
2524{
2525 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002526 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002527
2528 lockdep_assert_held(&cgroup_mutex);
2529
Tejun Heof8f22e52014-04-23 11:13:16 -04002530 /*
Tejun Heo62716ea2016-02-23 10:00:51 -05002531 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002532 * with tasks so that child cgroups don't compete against tasks.
2533 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002534 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heo62716ea2016-02-23 10:00:51 -05002535 dst_cgrp->subtree_control)
Tejun Heof8f22e52014-04-23 11:13:16 -04002536 return -EBUSY;
2537
Tejun Heo1958d2d2014-02-25 10:04:03 -05002538 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002539 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002540 struct css_set *dst_cset;
2541
Tejun Heof817de92014-04-23 11:13:16 -04002542 dst_cset = find_css_set(src_cset,
2543 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002544 if (!dst_cset)
2545 goto err;
2546
2547 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002548
2549 /*
2550 * If src cset equals dst, it's noop. Drop the src.
2551 * cgroup_migrate() will skip the cset too. Note that we
2552 * can't handle src == dst as some nodes are used by both.
2553 */
2554 if (src_cset == dst_cset) {
2555 src_cset->mg_src_cgrp = NULL;
2556 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002557 put_css_set(src_cset);
2558 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002559 continue;
2560 }
2561
Tejun Heo1958d2d2014-02-25 10:04:03 -05002562 src_cset->mg_dst_cset = dst_cset;
2563
2564 if (list_empty(&dst_cset->mg_preload_node))
2565 list_add(&dst_cset->mg_preload_node, &csets);
2566 else
Zefan Lia25eb522014-09-19 16:51:00 +08002567 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002568 }
2569
Tejun Heof817de92014-04-23 11:13:16 -04002570 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002571 return 0;
2572err:
2573 cgroup_migrate_finish(&csets);
2574 return -ENOMEM;
2575}
2576
2577/**
2578 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002579 * @leader: the leader of the process or the task to migrate
2580 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002581 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002582 *
2583 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002584 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002585 * caller is also responsible for invoking cgroup_migrate_add_src() and
2586 * cgroup_migrate_prepare_dst() on the targets before invoking this
2587 * function and following up with cgroup_migrate_finish().
2588 *
2589 * As long as a controller's ->can_attach() doesn't fail, this function is
2590 * guaranteed to succeed. This means that, excluding ->can_attach()
2591 * failure, when migrating multiple targets, the success or failure can be
2592 * decided for all targets by invoking group_migrate_prepare_dst() before
2593 * actually starting migrating.
2594 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002595static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2596 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002597{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002598 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2599 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002600
2601 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002602 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2603 * already PF_EXITING could be freed from underneath us unless we
2604 * take an rcu_read_lock.
2605 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002606 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002607 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002608 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002609 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002610 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002611 if (!threadgroup)
2612 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002613 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002614 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002615 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002616
Tejun Heoadaae5d2015-09-11 15:00:21 -04002617 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002618}
2619
Tejun Heo1958d2d2014-02-25 10:04:03 -05002620/**
2621 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2622 * @dst_cgrp: the cgroup to attach to
2623 * @leader: the task or the leader of the threadgroup to be attached
2624 * @threadgroup: attach the whole threadgroup?
2625 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002626 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002627 */
2628static int cgroup_attach_task(struct cgroup *dst_cgrp,
2629 struct task_struct *leader, bool threadgroup)
2630{
2631 LIST_HEAD(preloaded_csets);
2632 struct task_struct *task;
2633 int ret;
2634
2635 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002636 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002637 rcu_read_lock();
2638 task = leader;
2639 do {
2640 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2641 &preloaded_csets);
2642 if (!threadgroup)
2643 break;
2644 } while_each_thread(leader, task);
2645 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002646 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002647
2648 /* prepare dst csets and commit */
2649 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2650 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002651 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002652
2653 cgroup_migrate_finish(&preloaded_csets);
2654 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002655}
2656
Tejun Heo187fe842015-06-18 16:54:28 -04002657static int cgroup_procs_write_permission(struct task_struct *task,
2658 struct cgroup *dst_cgrp,
2659 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002660{
2661 const struct cred *cred = current_cred();
2662 const struct cred *tcred = get_task_cred(task);
2663 int ret = 0;
2664
2665 /*
2666 * even if we're attaching all tasks in the thread group, we only
2667 * need to check permissions on one of them.
2668 */
2669 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2670 !uid_eq(cred->euid, tcred->uid) &&
2671 !uid_eq(cred->euid, tcred->suid))
2672 ret = -EACCES;
2673
Tejun Heo187fe842015-06-18 16:54:28 -04002674 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2675 struct super_block *sb = of->file->f_path.dentry->d_sb;
2676 struct cgroup *cgrp;
2677 struct inode *inode;
2678
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002679 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002680 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002681 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002682
2683 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2684 cgrp = cgroup_parent(cgrp);
2685
2686 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002687 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002688 if (inode) {
2689 ret = inode_permission(inode, MAY_WRITE);
2690 iput(inode);
2691 }
2692 }
2693
Tejun Heodedf22e2015-06-18 16:54:28 -04002694 put_cred(tcred);
2695 return ret;
2696}
2697
Ben Blum74a11662011-05-26 16:25:20 -07002698/*
2699 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002700 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002701 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002702 */
Tejun Heoacbef752014-05-13 12:16:22 -04002703static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2704 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002705{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002706 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002707 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002708 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002709 int ret;
2710
Tejun Heoacbef752014-05-13 12:16:22 -04002711 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2712 return -EINVAL;
2713
Tejun Heoe76ecae2014-05-13 12:19:23 -04002714 cgrp = cgroup_kn_lock_live(of->kn);
2715 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002716 return -ENODEV;
2717
Tejun Heo3014dde2015-09-16 13:03:02 -04002718 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002719 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002720 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002721 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002722 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002723 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002724 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002725 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002726 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002727 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002728 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002729
2730 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002731 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002732
2733 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002734 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002735 * trapped in a cpuset, or RT worker may be born in a cgroup
2736 * with no rt_runtime allocated. Just say no.
2737 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002738 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002739 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002740 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002741 }
2742
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002743 get_task_struct(tsk);
2744 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002745
Tejun Heo187fe842015-06-18 16:54:28 -04002746 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002747 if (!ret)
2748 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002749
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002750 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002751 goto out_unlock_threadgroup;
2752
2753out_unlock_rcu:
2754 rcu_read_unlock();
2755out_unlock_threadgroup:
2756 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002757 cgroup_kn_unlock(of->kn);
Tejun Heoe93ad192016-01-19 12:18:41 -05002758 cpuset_post_attach_flush();
Tejun Heoacbef752014-05-13 12:16:22 -04002759 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002760}
2761
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002762/**
2763 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2764 * @from: attach to all cgroups of a given task
2765 * @tsk: the task to be attached
2766 */
2767int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2768{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002769 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002770 int retval = 0;
2771
Tejun Heo47cfcd02013-04-07 09:29:51 -07002772 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002773 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002774 struct cgroup *from_cgrp;
2775
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002776 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002777 continue;
2778
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002779 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002780 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002781 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002782
Li Zefan6f4b7e62013-07-31 16:18:36 +08002783 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002784 if (retval)
2785 break;
2786 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002787 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002788
2789 return retval;
2790}
2791EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2792
Tejun Heoacbef752014-05-13 12:16:22 -04002793static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2794 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002795{
Tejun Heoacbef752014-05-13 12:16:22 -04002796 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002797}
2798
Tejun Heoacbef752014-05-13 12:16:22 -04002799static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2800 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002801{
Tejun Heoacbef752014-05-13 12:16:22 -04002802 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002803}
2804
Tejun Heo451af502014-05-13 12:16:21 -04002805static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2806 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e0662008-07-25 01:46:59 -07002807{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002808 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002809
Tejun Heoe76ecae2014-05-13 12:19:23 -04002810 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2811
2812 cgrp = cgroup_kn_lock_live(of->kn);
2813 if (!cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07002814 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002815 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002816 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2817 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002818 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002819 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002820 return nbytes;
Paul Menagee788e0662008-07-25 01:46:59 -07002821}
2822
Tejun Heo2da8ca82013-12-05 12:28:04 -05002823static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002824{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002825 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002826
Tejun Heo46cfeb02014-05-13 12:11:00 -04002827 spin_lock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002828 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002829 spin_unlock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002830 seq_putc(seq, '\n');
Paul Menagee788e0662008-07-25 01:46:59 -07002831 return 0;
2832}
2833
Tejun Heo2da8ca82013-12-05 12:28:04 -05002834static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002835{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002836 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002837 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002838}
2839
Tejun Heo6e5c8302016-02-22 22:25:47 -05002840static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002841{
2842 struct cgroup_subsys *ss;
2843 bool printed = false;
2844 int ssid;
2845
Tejun Heob4e0eea2016-02-22 22:25:46 -05002846 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002847 if (printed)
2848 seq_putc(seq, ' ');
2849 seq_printf(seq, "%s", ss->name);
2850 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002851 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002852 if (printed)
2853 seq_putc(seq, '\n');
2854}
2855
2856/* show controllers which are currently attached to the default hierarchy */
2857static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2858{
2859 struct cgroup *cgrp = seq_css(seq)->cgroup;
2860
Tejun Heo5533e012014-05-14 19:33:07 -04002861 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
Tejun Heoa7165262016-02-23 10:00:50 -05002862 ~cgrp_dfl_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002863 return 0;
2864}
2865
2866/* show controllers which are enabled from the parent */
2867static int cgroup_controllers_show(struct seq_file *seq, void *v)
2868{
2869 struct cgroup *cgrp = seq_css(seq)->cgroup;
2870
Tejun Heo667c2492014-07-08 18:02:56 -04002871 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002872 return 0;
2873}
2874
2875/* show controllers which are enabled for a given cgroup's children */
2876static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2877{
2878 struct cgroup *cgrp = seq_css(seq)->cgroup;
2879
Tejun Heo667c2492014-07-08 18:02:56 -04002880 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002881 return 0;
2882}
2883
2884/**
2885 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2886 * @cgrp: root of the subtree to update csses for
2887 *
Tejun Heo8699b772016-02-22 22:25:46 -05002888 * @cgrp's subtree_ss_mask has changed and its subtree's (self excluded)
Tejun Heof8f22e52014-04-23 11:13:16 -04002889 * css associations need to be updated accordingly. This function looks up
2890 * all css_sets which are attached to the subtree, creates the matching
2891 * updated css_sets and migrates the tasks to the new ones.
2892 */
2893static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2894{
2895 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002896 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002897 struct cgroup_subsys_state *css;
2898 struct css_set *src_cset;
2899 int ret;
2900
Tejun Heof8f22e52014-04-23 11:13:16 -04002901 lockdep_assert_held(&cgroup_mutex);
2902
Tejun Heo3014dde2015-09-16 13:03:02 -04002903 percpu_down_write(&cgroup_threadgroup_rwsem);
2904
Tejun Heof8f22e52014-04-23 11:13:16 -04002905 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002906 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002907 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2908 struct cgrp_cset_link *link;
2909
Tejun Heo8699b772016-02-22 22:25:46 -05002910 /* self is not affected by subtree_ss_mask change */
Tejun Heof8f22e52014-04-23 11:13:16 -04002911 if (css->cgroup == cgrp)
2912 continue;
2913
2914 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2915 cgroup_migrate_add_src(link->cset, cgrp,
2916 &preloaded_csets);
2917 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002918 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002919
2920 /* NULL dst indicates self on default hierarchy */
2921 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2922 if (ret)
2923 goto out_finish;
2924
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002925 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002926 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002927 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002928
2929 /* src_csets precede dst_csets, break on the first dst_cset */
2930 if (!src_cset->mg_src_cgrp)
2931 break;
2932
Tejun Heo10265072015-09-11 15:00:22 -04002933 /* all tasks in src_csets need to be migrated */
2934 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2935 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002936 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002937 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002938
Tejun Heo10265072015-09-11 15:00:22 -04002939 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002940out_finish:
2941 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002942 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002943 return ret;
2944}
2945
2946/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002947static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2948 char *buf, size_t nbytes,
2949 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002950{
Tejun Heo6e5c8302016-02-22 22:25:47 -05002951 u16 enable = 0, disable = 0;
2952 u16 css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002953 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002954 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002955 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002956 int ssid, ret;
2957
2958 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002959 * Parse input - space separated list of subsystem names prefixed
2960 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002961 */
Tejun Heo451af502014-05-13 12:16:21 -04002962 buf = strstrip(buf);
2963 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002964 if (tok[0] == '\0')
2965 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05002966 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04002967 if (!cgroup_ssid_enabled(ssid) ||
2968 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002969 continue;
2970
2971 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002972 enable |= 1 << ssid;
2973 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002974 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002975 disable |= 1 << ssid;
2976 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002977 } else {
2978 return -EINVAL;
2979 }
2980 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002981 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002982 if (ssid == CGROUP_SUBSYS_COUNT)
2983 return -EINVAL;
2984 }
2985
Tejun Heoa9746d82014-05-13 12:19:22 -04002986 cgrp = cgroup_kn_lock_live(of->kn);
2987 if (!cgrp)
2988 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002989
2990 for_each_subsys(ss, ssid) {
2991 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002992 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002993 enable &= ~(1 << ssid);
2994 continue;
2995 }
2996
Tejun Heoc29adf22014-07-08 18:02:56 -04002997 /* unavailable or not enabled on the parent? */
2998 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2999 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04003000 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003001 ret = -ENOENT;
3002 goto out_unlock;
3003 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003004 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003005 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003006 disable &= ~(1 << ssid);
3007 continue;
3008 }
3009
3010 /* a child has it enabled? */
3011 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003012 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003013 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003014 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003015 }
3016 }
3017 }
3018 }
3019
3020 if (!enable && !disable) {
3021 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003022 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003023 }
3024
3025 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003026 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003027 * with tasks so that child cgroups don't compete against tasks.
3028 */
Tejun Heod51f39b2014-05-16 13:22:48 -04003029 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003030 ret = -EBUSY;
3031 goto out_unlock;
3032 }
3033
3034 /*
Tejun Heof63070d2014-07-08 18:02:57 -04003035 * Update subsys masks and calculate what needs to be done. More
3036 * subsystems than specified may need to be enabled or disabled
3037 * depending on subsystem dependencies.
3038 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05003039 old_sc = cgrp->subtree_control;
Tejun Heo8699b772016-02-22 22:25:46 -05003040 old_ss = cgrp->subtree_ss_mask;
Tejun Heo755bf5e2014-11-18 02:49:50 -05003041 new_sc = (old_sc | enable) & ~disable;
Tejun Heo8699b772016-02-22 22:25:46 -05003042 new_ss = cgroup_calc_subtree_ss_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04003043
Tejun Heo755bf5e2014-11-18 02:49:50 -05003044 css_enable = ~old_ss & new_ss;
3045 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04003046 enable |= css_enable;
3047 disable |= css_disable;
3048
Tejun Heodb6e3052014-11-18 02:49:51 -05003049 /*
3050 * Because css offlining is asynchronous, userland might try to
3051 * re-enable the same controller while the previous instance is
3052 * still around. In such cases, wait till it's gone using
3053 * offline_waitq.
3054 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05003055 do_each_subsys_mask(ss, ssid, css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05003056 cgroup_for_each_live_child(child, cgrp) {
3057 DEFINE_WAIT(wait);
3058
3059 if (!cgroup_css(child, ss))
3060 continue;
3061
3062 cgroup_get(child);
3063 prepare_to_wait(&child->offline_waitq, &wait,
3064 TASK_UNINTERRUPTIBLE);
3065 cgroup_kn_unlock(of->kn);
3066 schedule();
3067 finish_wait(&child->offline_waitq, &wait);
3068 cgroup_put(child);
3069
3070 return restart_syscall();
3071 }
Tejun Heob4e0eea2016-02-22 22:25:46 -05003072 } while_each_subsys_mask();
Tejun Heodb6e3052014-11-18 02:49:51 -05003073
Tejun Heo755bf5e2014-11-18 02:49:50 -05003074 cgrp->subtree_control = new_sc;
Tejun Heo8699b772016-02-22 22:25:46 -05003075 cgrp->subtree_ss_mask = new_ss;
Tejun Heo755bf5e2014-11-18 02:49:50 -05003076
Tejun Heof63070d2014-07-08 18:02:57 -04003077 /*
3078 * Create new csses or make the existing ones visible. A css is
3079 * created invisible if it's being implicitly enabled through
3080 * dependency. An invisible css is made visible when the userland
3081 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04003082 */
Tejun Heo996cd1f2016-02-22 22:25:46 -05003083 do_each_subsys_mask(ss, ssid, enable) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003084 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05003085 if (css_enable & (1 << ssid)) {
3086 struct cgroup_subsys_state *css;
3087
3088 css = css_create(child, ss);
3089 if (IS_ERR(css)) {
3090 ret = PTR_ERR(css);
3091 goto err_undo_css;
3092 }
3093
3094 if (cgrp->subtree_control & (1 << ssid)) {
3095 ret = css_populate_dir(css, NULL);
3096 if (ret)
3097 goto err_undo_css;
3098 }
3099 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003100 ret = css_populate_dir(cgroup_css(child, ss),
3101 NULL);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05003102 if (ret)
3103 goto err_undo_css;
3104 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003105 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05003106 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003107
Tejun Heoc29adf22014-07-08 18:02:56 -04003108 /*
3109 * At this point, cgroup_e_css() results reflect the new csses
3110 * making the following cgroup_update_dfl_csses() properly update
3111 * css associations of all tasks in the subtree.
3112 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003113 ret = cgroup_update_dfl_csses(cgrp);
3114 if (ret)
3115 goto err_undo_css;
3116
Tejun Heof63070d2014-07-08 18:02:57 -04003117 /*
3118 * All tasks are migrated out of disabled csses. Kill or hide
3119 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003120 * disabled while other subsystems are still depending on it. The
3121 * css must not actively control resources and be in the vanilla
3122 * state if it's made visible again later. Controllers which may
3123 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003124 */
Tejun Heo996cd1f2016-02-22 22:25:46 -05003125 do_each_subsys_mask(ss, ssid, disable) {
Tejun Heof63070d2014-07-08 18:02:57 -04003126 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003127 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3128
3129 if (css_disable & (1 << ssid)) {
3130 kill_css(css);
3131 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003132 css_clear_dir(css, NULL);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003133 if (ss->css_reset)
3134 ss->css_reset(css);
3135 }
Tejun Heof63070d2014-07-08 18:02:57 -04003136 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05003137 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003138
3139 kernfs_activate(cgrp->kn);
3140 ret = 0;
3141out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003142 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003143 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003144
3145err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003146 cgrp->subtree_control = old_sc;
Tejun Heo8699b772016-02-22 22:25:46 -05003147 cgrp->subtree_ss_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003148
Tejun Heo996cd1f2016-02-22 22:25:46 -05003149 do_each_subsys_mask(ss, ssid, enable) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003150 cgroup_for_each_live_child(child, cgrp) {
3151 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003152
3153 if (!css)
3154 continue;
3155
3156 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003157 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003158 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003159 css_clear_dir(css, NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003160 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05003161 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003162 goto out_unlock;
3163}
3164
Tejun Heo4a07c222015-09-18 17:54:22 -04003165static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003166{
Tejun Heo4a07c222015-09-18 17:54:22 -04003167 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003168 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003169 return 0;
3170}
3171
Tejun Heo2bd59d42014-02-11 11:52:49 -05003172static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3173 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003174{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003175 struct cgroup *cgrp = of->kn->parent->priv;
3176 struct cftype *cft = of->kn->priv;
3177 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003178 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003179
Tejun Heob4168642014-05-13 12:16:21 -04003180 if (cft->write)
3181 return cft->write(of, buf, nbytes, off);
3182
Tejun Heo2bd59d42014-02-11 11:52:49 -05003183 /*
3184 * kernfs guarantees that a file isn't deleted with operations in
3185 * flight, which means that the matching css is and stays alive and
3186 * doesn't need to be pinned. The RCU locking is not necessary
3187 * either. It's just for the convenience of using cgroup_css().
3188 */
3189 rcu_read_lock();
3190 css = cgroup_css(cgrp, cft->ss);
3191 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003192
Tejun Heo451af502014-05-13 12:16:21 -04003193 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003194 unsigned long long v;
3195 ret = kstrtoull(buf, 0, &v);
3196 if (!ret)
3197 ret = cft->write_u64(css, cft, v);
3198 } else if (cft->write_s64) {
3199 long long v;
3200 ret = kstrtoll(buf, 0, &v);
3201 if (!ret)
3202 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003203 } else {
3204 ret = -EINVAL;
3205 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003206
Tejun Heoa742c592013-12-05 12:28:03 -05003207 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003208}
3209
Tejun Heo6612f052013-12-05 12:28:04 -05003210static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003211{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003212 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003213}
3214
3215static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3216{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003217 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003218}
3219
3220static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3221{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003222 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003223}
3224
3225static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3226{
Tejun Heo7da11272013-12-05 12:28:04 -05003227 struct cftype *cft = seq_cft(m);
3228 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003229
Tejun Heo2da8ca82013-12-05 12:28:04 -05003230 if (cft->seq_show)
3231 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003232
Tejun Heo896f5192013-12-05 12:28:04 -05003233 if (cft->read_u64)
3234 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3235 else if (cft->read_s64)
3236 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3237 else
3238 return -EINVAL;
3239 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003240}
3241
Tejun Heo2bd59d42014-02-11 11:52:49 -05003242static struct kernfs_ops cgroup_kf_single_ops = {
3243 .atomic_write_len = PAGE_SIZE,
3244 .write = cgroup_file_write,
3245 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003246};
3247
Tejun Heo2bd59d42014-02-11 11:52:49 -05003248static struct kernfs_ops cgroup_kf_ops = {
3249 .atomic_write_len = PAGE_SIZE,
3250 .write = cgroup_file_write,
3251 .seq_start = cgroup_seqfile_start,
3252 .seq_next = cgroup_seqfile_next,
3253 .seq_stop = cgroup_seqfile_stop,
3254 .seq_show = cgroup_seqfile_show,
3255};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003256
3257/*
3258 * cgroup_rename - Only allow simple rename of directories in place.
3259 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003260static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3261 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003262{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003263 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003264 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003265
Tejun Heo2bd59d42014-02-11 11:52:49 -05003266 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003267 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003268 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003269 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003270
Tejun Heo6db8e852013-06-14 11:18:22 -07003271 /*
3272 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003273 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003274 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003275 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003276 return -EPERM;
3277
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003278 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003279 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003280 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003281 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003282 */
3283 kernfs_break_active_protection(new_parent);
3284 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003285
Tejun Heo2bd59d42014-02-11 11:52:49 -05003286 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003287
Tejun Heo2bd59d42014-02-11 11:52:49 -05003288 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003289
Tejun Heo2bd59d42014-02-11 11:52:49 -05003290 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003291
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003292 kernfs_unbreak_active_protection(kn);
3293 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003294 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003295}
3296
Tejun Heo49957f82014-04-07 16:44:47 -04003297/* set uid and gid of cgroup dirs and files to that of the creator */
3298static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3299{
3300 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3301 .ia_uid = current_fsuid(),
3302 .ia_gid = current_fsgid(), };
3303
3304 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3305 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3306 return 0;
3307
3308 return kernfs_setattr(kn, &iattr);
3309}
3310
Tejun Heo4df8dc92015-09-18 17:54:23 -04003311static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3312 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003313{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003314 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003315 struct kernfs_node *kn;
3316 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003317 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003318
Tejun Heo2bd59d42014-02-11 11:52:49 -05003319#ifdef CONFIG_DEBUG_LOCK_ALLOC
3320 key = &cft->lockdep_key;
3321#endif
3322 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3323 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003324 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003325 if (IS_ERR(kn))
3326 return PTR_ERR(kn);
3327
3328 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003329 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003330 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003331 return ret;
3332 }
3333
Tejun Heo6f60ead2015-09-18 17:54:23 -04003334 if (cft->file_offset) {
3335 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3336
Tejun Heo34c06252015-11-05 00:12:24 -05003337 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003338 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003339 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003340 }
3341
Tejun Heof8f22e52014-04-23 11:13:16 -04003342 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003343}
3344
Tejun Heob1f28d32013-06-28 16:24:10 -07003345/**
3346 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003347 * @css: the target css
3348 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003349 * @cfts: array of cftypes to be added
3350 * @is_add: whether to add or remove
3351 *
3352 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003353 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003354 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003355static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3356 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003357 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003358{
Tejun Heo6732ed82015-09-18 17:54:23 -04003359 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003360 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003361
Tejun Heo01f64742014-05-13 12:19:23 -04003362 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003363
Tejun Heo6732ed82015-09-18 17:54:23 -04003364restart:
3365 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003366 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003367 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003368 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003369 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003370 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003371 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003372 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003373 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003374 continue;
3375
Li Zefan2739d3c2013-01-21 18:18:33 +08003376 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003377 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003378 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003379 pr_warn("%s: failed to add %s, err=%d\n",
3380 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003381 cft_end = cft;
3382 is_add = false;
3383 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003384 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003385 } else {
3386 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003387 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003388 }
Tejun Heob598dde2016-02-22 22:25:45 -05003389 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003390}
3391
Tejun Heo21a2d342014-02-12 09:29:49 -05003392static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003393{
3394 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003395 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003396 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003397 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003398 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003399
Tejun Heo01f64742014-05-13 12:19:23 -04003400 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003401
Li Zefane8c82d22013-06-18 18:48:37 +08003402 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003403 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003404 struct cgroup *cgrp = css->cgroup;
3405
Li Zefane8c82d22013-06-18 18:48:37 +08003406 if (cgroup_is_dead(cgrp))
3407 continue;
3408
Tejun Heo4df8dc92015-09-18 17:54:23 -04003409 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003410 if (ret)
3411 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003412 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003413
3414 if (is_add && !ret)
3415 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003416 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003417}
3418
Tejun Heo2da440a2014-02-11 11:52:48 -05003419static void cgroup_exit_cftypes(struct cftype *cfts)
3420{
3421 struct cftype *cft;
3422
Tejun Heo2bd59d42014-02-11 11:52:49 -05003423 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3424 /* free copy for custom atomic_write_len, see init_cftypes() */
3425 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3426 kfree(cft->kf_ops);
3427 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003428 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003429
3430 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003431 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003432 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003433}
3434
Tejun Heo2bd59d42014-02-11 11:52:49 -05003435static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003436{
3437 struct cftype *cft;
3438
Tejun Heo2bd59d42014-02-11 11:52:49 -05003439 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3440 struct kernfs_ops *kf_ops;
3441
Tejun Heo0adb0702014-02-12 09:29:48 -05003442 WARN_ON(cft->ss || cft->kf_ops);
3443
Tejun Heo2bd59d42014-02-11 11:52:49 -05003444 if (cft->seq_start)
3445 kf_ops = &cgroup_kf_ops;
3446 else
3447 kf_ops = &cgroup_kf_single_ops;
3448
3449 /*
3450 * Ugh... if @cft wants a custom max_write_len, we need to
3451 * make a copy of kf_ops to set its atomic_write_len.
3452 */
3453 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3454 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3455 if (!kf_ops) {
3456 cgroup_exit_cftypes(cfts);
3457 return -ENOMEM;
3458 }
3459 kf_ops->atomic_write_len = cft->max_write_len;
3460 }
3461
3462 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003463 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003464 }
3465
3466 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003467}
3468
Tejun Heo21a2d342014-02-12 09:29:49 -05003469static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3470{
Tejun Heo01f64742014-05-13 12:19:23 -04003471 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003472
3473 if (!cfts || !cfts[0].ss)
3474 return -ENOENT;
3475
3476 list_del(&cfts->node);
3477 cgroup_apply_cftypes(cfts, false);
3478 cgroup_exit_cftypes(cfts);
3479 return 0;
3480}
3481
Tejun Heo8e3f6542012-04-01 12:09:55 -07003482/**
Tejun Heo80b13582014-02-12 09:29:48 -05003483 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3484 * @cfts: zero-length name terminated array of cftypes
3485 *
3486 * Unregister @cfts. Files described by @cfts are removed from all
3487 * existing cgroups and all future cgroups won't have them either. This
3488 * function can be called anytime whether @cfts' subsys is attached or not.
3489 *
3490 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3491 * registered.
3492 */
3493int cgroup_rm_cftypes(struct cftype *cfts)
3494{
Tejun Heo21a2d342014-02-12 09:29:49 -05003495 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003496
Tejun Heo01f64742014-05-13 12:19:23 -04003497 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003498 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003499 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003500 return ret;
3501}
3502
3503/**
3504 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3505 * @ss: target cgroup subsystem
3506 * @cfts: zero-length name terminated array of cftypes
3507 *
3508 * Register @cfts to @ss. Files described by @cfts are created for all
3509 * existing cgroups to which @ss is attached and all future cgroups will
3510 * have them too. This function can be called anytime whether @ss is
3511 * attached or not.
3512 *
3513 * Returns 0 on successful registration, -errno on failure. Note that this
3514 * function currently returns 0 as long as @cfts registration is successful
3515 * even if some file creation attempts on existing cgroups fail.
3516 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003517static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003518{
Tejun Heo9ccece82013-06-28 16:24:11 -07003519 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003520
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003521 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003522 return 0;
3523
Li Zefandc5736e2014-02-17 10:41:50 +08003524 if (!cfts || cfts[0].name[0] == '\0')
3525 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003526
Tejun Heo2bd59d42014-02-11 11:52:49 -05003527 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003528 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003529 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003530
Tejun Heo01f64742014-05-13 12:19:23 -04003531 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003532
Tejun Heo0adb0702014-02-12 09:29:48 -05003533 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003534 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003535 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003536 cgroup_rm_cftypes_locked(cfts);
3537
Tejun Heo01f64742014-05-13 12:19:23 -04003538 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003539 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003540}
Tejun Heo79578622012-04-01 12:09:56 -07003541
3542/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003543 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3544 * @ss: target cgroup subsystem
3545 * @cfts: zero-length name terminated array of cftypes
3546 *
3547 * Similar to cgroup_add_cftypes() but the added files are only used for
3548 * the default hierarchy.
3549 */
3550int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3551{
3552 struct cftype *cft;
3553
3554 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003555 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003556 return cgroup_add_cftypes(ss, cfts);
3557}
3558
3559/**
3560 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3561 * @ss: target cgroup subsystem
3562 * @cfts: zero-length name terminated array of cftypes
3563 *
3564 * Similar to cgroup_add_cftypes() but the added files are only used for
3565 * the legacy hierarchies.
3566 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003567int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3568{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003569 struct cftype *cft;
3570
Tejun Heoe4b70372015-10-15 17:00:43 -04003571 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3572 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003573 return cgroup_add_cftypes(ss, cfts);
3574}
3575
Li Zefana043e3b2008-02-23 15:24:09 -08003576/**
Tejun Heo34c06252015-11-05 00:12:24 -05003577 * cgroup_file_notify - generate a file modified event for a cgroup_file
3578 * @cfile: target cgroup_file
3579 *
3580 * @cfile must have been obtained by setting cftype->file_offset.
3581 */
3582void cgroup_file_notify(struct cgroup_file *cfile)
3583{
3584 unsigned long flags;
3585
3586 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3587 if (cfile->kn)
3588 kernfs_notify(cfile->kn);
3589 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3590}
3591
3592/**
Li Zefana043e3b2008-02-23 15:24:09 -08003593 * cgroup_task_count - count the number of tasks in a cgroup.
3594 * @cgrp: the cgroup in question
3595 *
3596 * Return the number of tasks in the cgroup.
3597 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003598static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003599{
3600 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003601 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003602
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003603 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003604 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3605 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003606 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003607 return count;
3608}
3609
Tejun Heo574bd9f2012-11-09 09:12:29 -08003610/**
Tejun Heo492eb212013-08-08 20:11:25 -04003611 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003612 * @pos: the current position (%NULL to initiate traversal)
3613 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003614 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003615 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003616 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003617 * that @parent and @pos are accessible. The next sibling is guaranteed to
3618 * be returned regardless of their states.
3619 *
3620 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3621 * css which finished ->css_online() is guaranteed to be visible in the
3622 * future iterations and will stay visible until the last reference is put.
3623 * A css which hasn't finished ->css_online() or already finished
3624 * ->css_offline() may show up during traversal. It's each subsystem's
3625 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003626 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003627struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3628 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003629{
Tejun Heoc2931b72014-05-16 13:22:51 -04003630 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003631
Tejun Heo8353da12014-05-13 12:19:23 -04003632 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003633
3634 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003635 * @pos could already have been unlinked from the sibling list.
3636 * Once a cgroup is removed, its ->sibling.next is no longer
3637 * updated when its next sibling changes. CSS_RELEASED is set when
3638 * @pos is taken off list, at which time its next pointer is valid,
3639 * and, as releases are serialized, the one pointed to by the next
3640 * pointer is guaranteed to not have started release yet. This
3641 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3642 * critical section, the one pointed to by its next pointer is
3643 * guaranteed to not have finished its RCU grace period even if we
3644 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003645 *
Tejun Heode3f0342014-05-16 13:22:49 -04003646 * If @pos has CSS_RELEASED set, its next pointer can't be
3647 * dereferenced; however, as each css is given a monotonically
3648 * increasing unique serial number and always appended to the
3649 * sibling list, the next one can be found by walking the parent's
3650 * children until the first css with higher serial number than
3651 * @pos's. While this path can be slower, it happens iff iteration
3652 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003653 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003654 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003655 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3656 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3657 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003658 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003659 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003660 if (next->serial_nr > pos->serial_nr)
3661 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003662 }
3663
Tejun Heo3b281af2014-04-23 11:13:15 -04003664 /*
3665 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003666 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003667 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003668 if (&next->sibling != &parent->children)
3669 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003670 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003671}
Tejun Heo53fa5262013-05-24 10:55:38 +09003672
3673/**
Tejun Heo492eb212013-08-08 20:11:25 -04003674 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003675 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003676 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003677 *
Tejun Heo492eb212013-08-08 20:11:25 -04003678 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003679 * to visit for pre-order traversal of @root's descendants. @root is
3680 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003681 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003682 * While this function requires cgroup_mutex or RCU read locking, it
3683 * doesn't require the whole traversal to be contained in a single critical
3684 * section. This function will return the correct next descendant as long
3685 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003686 *
3687 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3688 * css which finished ->css_online() is guaranteed to be visible in the
3689 * future iterations and will stay visible until the last reference is put.
3690 * A css which hasn't finished ->css_online() or already finished
3691 * ->css_offline() may show up during traversal. It's each subsystem's
3692 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003693 */
Tejun Heo492eb212013-08-08 20:11:25 -04003694struct cgroup_subsys_state *
3695css_next_descendant_pre(struct cgroup_subsys_state *pos,
3696 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003697{
Tejun Heo492eb212013-08-08 20:11:25 -04003698 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003699
Tejun Heo8353da12014-05-13 12:19:23 -04003700 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003701
Tejun Heobd8815a2013-08-08 20:11:27 -04003702 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003703 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003704 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003705
3706 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003707 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003708 if (next)
3709 return next;
3710
3711 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003712 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003713 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003714 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003715 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003716 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003717 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003718
3719 return NULL;
3720}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003721
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003722/**
Tejun Heo492eb212013-08-08 20:11:25 -04003723 * css_rightmost_descendant - return the rightmost descendant of a css
3724 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003725 *
Tejun Heo492eb212013-08-08 20:11:25 -04003726 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3727 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003728 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003729 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003730 * While this function requires cgroup_mutex or RCU read locking, it
3731 * doesn't require the whole traversal to be contained in a single critical
3732 * section. This function will return the correct rightmost descendant as
3733 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003734 */
Tejun Heo492eb212013-08-08 20:11:25 -04003735struct cgroup_subsys_state *
3736css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003737{
Tejun Heo492eb212013-08-08 20:11:25 -04003738 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003739
Tejun Heo8353da12014-05-13 12:19:23 -04003740 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003741
3742 do {
3743 last = pos;
3744 /* ->prev isn't RCU safe, walk ->next till the end */
3745 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003746 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003747 pos = tmp;
3748 } while (pos);
3749
3750 return last;
3751}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003752
Tejun Heo492eb212013-08-08 20:11:25 -04003753static struct cgroup_subsys_state *
3754css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003755{
Tejun Heo492eb212013-08-08 20:11:25 -04003756 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003757
3758 do {
3759 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003760 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003761 } while (pos);
3762
3763 return last;
3764}
3765
3766/**
Tejun Heo492eb212013-08-08 20:11:25 -04003767 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003768 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003769 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003770 *
Tejun Heo492eb212013-08-08 20:11:25 -04003771 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003772 * to visit for post-order traversal of @root's descendants. @root is
3773 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003774 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003775 * While this function requires cgroup_mutex or RCU read locking, it
3776 * doesn't require the whole traversal to be contained in a single critical
3777 * section. This function will return the correct next descendant as long
3778 * as both @pos and @cgroup are accessible and @pos is a descendant of
3779 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003780 *
3781 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3782 * css which finished ->css_online() is guaranteed to be visible in the
3783 * future iterations and will stay visible until the last reference is put.
3784 * A css which hasn't finished ->css_online() or already finished
3785 * ->css_offline() may show up during traversal. It's each subsystem's
3786 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003787 */
Tejun Heo492eb212013-08-08 20:11:25 -04003788struct cgroup_subsys_state *
3789css_next_descendant_post(struct cgroup_subsys_state *pos,
3790 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003791{
Tejun Heo492eb212013-08-08 20:11:25 -04003792 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003793
Tejun Heo8353da12014-05-13 12:19:23 -04003794 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003795
Tejun Heo58b79a92013-09-06 15:31:08 -04003796 /* if first iteration, visit leftmost descendant which may be @root */
3797 if (!pos)
3798 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003799
Tejun Heobd8815a2013-08-08 20:11:27 -04003800 /* if we visited @root, we're done */
3801 if (pos == root)
3802 return NULL;
3803
Tejun Heo574bd9f2012-11-09 09:12:29 -08003804 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003805 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003806 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003807 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003808
3809 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003810 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003811}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003812
Tejun Heof3d46502014-05-16 13:22:52 -04003813/**
3814 * css_has_online_children - does a css have online children
3815 * @css: the target css
3816 *
3817 * Returns %true if @css has any online children; otherwise, %false. This
3818 * function can be called from any context but the caller is responsible
3819 * for synchronizing against on/offlining as necessary.
3820 */
3821bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003822{
Tejun Heof3d46502014-05-16 13:22:52 -04003823 struct cgroup_subsys_state *child;
3824 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003825
3826 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003827 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003828 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003829 ret = true;
3830 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003831 }
3832 }
3833 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003834 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003835}
3836
Tejun Heo0942eee2013-08-08 20:11:26 -04003837/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003838 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003839 * @it: the iterator to advance
3840 *
3841 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003842 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003843static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003844{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003845 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003846 struct cgrp_cset_link *link;
3847 struct css_set *cset;
3848
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003849 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003850
Tejun Heod5158762013-08-08 20:11:26 -04003851 /* Advance to the next non-empty css_set */
3852 do {
3853 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003854 if (l == it->cset_head) {
3855 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003856 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003857 return;
3858 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003859
3860 if (it->ss) {
3861 cset = container_of(l, struct css_set,
3862 e_cset_node[it->ss->id]);
3863 } else {
3864 link = list_entry(l, struct cgrp_cset_link, cset_link);
3865 cset = link->cset;
3866 }
Tejun Heo0de09422015-10-15 16:41:49 -04003867 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05003868
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003869 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003870
3871 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003872 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003873 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003874 it->task_pos = cset->mg_tasks.next;
3875
3876 it->tasks_head = &cset->tasks;
3877 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04003878
3879 /*
3880 * We don't keep css_sets locked across iteration steps and thus
3881 * need to take steps to ensure that iteration can be resumed after
3882 * the lock is re-acquired. Iteration is performed at two levels -
3883 * css_sets and tasks in them.
3884 *
3885 * Once created, a css_set never leaves its cgroup lists, so a
3886 * pinned css_set is guaranteed to stay put and we can resume
3887 * iteration afterwards.
3888 *
3889 * Tasks may leave @cset across iteration steps. This is resolved
3890 * by registering each iterator with the css_set currently being
3891 * walked and making css_set_move_task() advance iterators whose
3892 * next task is leaving.
3893 */
3894 if (it->cur_cset) {
3895 list_del(&it->iters_node);
3896 put_css_set_locked(it->cur_cset);
3897 }
3898 get_css_set(cset);
3899 it->cur_cset = cset;
3900 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04003901}
3902
Tejun Heoecb9d532015-10-15 16:41:52 -04003903static void css_task_iter_advance(struct css_task_iter *it)
3904{
3905 struct list_head *l = it->task_pos;
3906
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003907 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04003908 WARN_ON_ONCE(!l);
3909
3910 /*
3911 * Advance iterator to find next entry. cset->tasks is consumed
3912 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3913 * next cset.
3914 */
3915 l = l->next;
3916
3917 if (l == it->tasks_head)
3918 l = it->mg_tasks_head->next;
3919
3920 if (l == it->mg_tasks_head)
3921 css_task_iter_advance_css_set(it);
3922 else
3923 it->task_pos = l;
3924}
3925
Tejun Heo0942eee2013-08-08 20:11:26 -04003926/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003927 * css_task_iter_start - initiate task iteration
3928 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003929 * @it: the task iterator to use
3930 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003931 * Initiate iteration through the tasks of @css. The caller can call
3932 * css_task_iter_next() to walk through the tasks until the function
3933 * returns NULL. On completion of iteration, css_task_iter_end() must be
3934 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003935 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003936void css_task_iter_start(struct cgroup_subsys_state *css,
3937 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003938{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003939 /* no one should try to iterate before mounting cgroups */
3940 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003941
Tejun Heoed27b9f2015-10-15 16:41:52 -04003942 memset(it, 0, sizeof(*it));
3943
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003944 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003945
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003946 it->ss = css->ss;
3947
3948 if (it->ss)
3949 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3950 else
3951 it->cset_pos = &css->cgroup->cset_links;
3952
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003953 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003954
Tejun Heoecb9d532015-10-15 16:41:52 -04003955 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003956
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003957 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003958}
Paul Menage817929e2007-10-18 23:39:36 -07003959
Tejun Heo0942eee2013-08-08 20:11:26 -04003960/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003961 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003962 * @it: the task iterator being iterated
3963 *
3964 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003965 * initialized via css_task_iter_start(). Returns NULL when the iteration
3966 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003967 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003968struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003969{
Tejun Heod5745672015-10-29 11:43:05 +09003970 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04003971 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09003972 it->cur_task = NULL;
3973 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003974
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003975 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003976
Tejun Heod5745672015-10-29 11:43:05 +09003977 if (it->task_pos) {
3978 it->cur_task = list_entry(it->task_pos, struct task_struct,
3979 cg_list);
3980 get_task_struct(it->cur_task);
3981 css_task_iter_advance(it);
3982 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003983
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003984 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003985
3986 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07003987}
3988
Tejun Heo0942eee2013-08-08 20:11:26 -04003989/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003990 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003991 * @it: the task iterator to finish
3992 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003993 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003994 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003995void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003996{
Tejun Heoed27b9f2015-10-15 16:41:52 -04003997 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003998 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003999 list_del(&it->iters_node);
4000 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004001 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004002 }
4003
4004 if (it->cur_task)
4005 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004006}
4007
4008/**
4009 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4010 * @to: cgroup to which the tasks will be moved
4011 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004012 *
4013 * Locking rules between cgroup_post_fork() and the migration path
4014 * guarantee that, if a task is forking while being migrated, the new child
4015 * is guaranteed to be either visible in the source cgroup after the
4016 * parent's migration is complete or put into the target cgroup. No task
4017 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004018 */
4019int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4020{
Tejun Heo952aaa12014-02-25 10:04:03 -05004021 LIST_HEAD(preloaded_csets);
4022 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004023 struct css_task_iter it;
4024 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004025 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004026
Tejun Heo952aaa12014-02-25 10:04:03 -05004027 mutex_lock(&cgroup_mutex);
4028
4029 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004030 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004031 list_for_each_entry(link, &from->cset_links, cset_link)
4032 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004033 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004034
4035 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4036 if (ret)
4037 goto out_err;
4038
4039 /*
Rami Rosen2cfa2b12016-01-09 23:33:06 +02004040 * Migrate tasks one-by-one until @from is empty. This fails iff
Tejun Heo952aaa12014-02-25 10:04:03 -05004041 * ->can_attach() fails.
4042 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004043 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004044 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004045 task = css_task_iter_next(&it);
4046 if (task)
4047 get_task_struct(task);
4048 css_task_iter_end(&it);
4049
4050 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004051 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004052 put_task_struct(task);
4053 }
4054 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004055out_err:
4056 cgroup_migrate_finish(&preloaded_csets);
4057 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004058 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004059}
4060
Paul Menage817929e2007-10-18 23:39:36 -07004061/*
Ben Blum102a7752009-09-23 15:56:26 -07004062 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004063 *
4064 * Reading this file can return large amounts of data if a cgroup has
4065 * *lots* of attached tasks. So it may need several calls to read(),
4066 * but we cannot guarantee that the information we produce is correct
4067 * unless we produce it entirely atomically.
4068 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004069 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004070
Li Zefan24528252012-01-20 11:58:43 +08004071/* which pidlist file are we talking about? */
4072enum cgroup_filetype {
4073 CGROUP_FILE_PROCS,
4074 CGROUP_FILE_TASKS,
4075};
4076
4077/*
4078 * A pidlist is a list of pids that virtually represents the contents of one
4079 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4080 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4081 * to the cgroup.
4082 */
4083struct cgroup_pidlist {
4084 /*
4085 * used to find which pidlist is wanted. doesn't change as long as
4086 * this particular list stays in the list.
4087 */
4088 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4089 /* array of xids */
4090 pid_t *list;
4091 /* how many elements the above list has */
4092 int length;
Li Zefan24528252012-01-20 11:58:43 +08004093 /* each of these stored in a list by its cgroup */
4094 struct list_head links;
4095 /* pointer to the cgroup we belong to, for list removal purposes */
4096 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004097 /* for delayed destruction */
4098 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004099};
4100
Paul Menagebbcb81d2007-10-18 23:39:32 -07004101/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004102 * The following two functions "fix" the issue where there are more pids
4103 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4104 * TODO: replace with a kernel-wide solution to this problem
4105 */
4106#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4107static void *pidlist_allocate(int count)
4108{
4109 if (PIDLIST_TOO_LARGE(count))
4110 return vmalloc(count * sizeof(pid_t));
4111 else
4112 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4113}
Tejun Heob1a21362013-11-29 10:42:58 -05004114
Ben Blumd1d9fd32009-09-23 15:56:28 -07004115static void pidlist_free(void *p)
4116{
Bandan Das58794512015-03-02 17:51:10 -05004117 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004118}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004119
4120/*
Tejun Heob1a21362013-11-29 10:42:58 -05004121 * Used to destroy all pidlists lingering waiting for destroy timer. None
4122 * should be left afterwards.
4123 */
4124static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4125{
4126 struct cgroup_pidlist *l, *tmp_l;
4127
4128 mutex_lock(&cgrp->pidlist_mutex);
4129 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4130 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4131 mutex_unlock(&cgrp->pidlist_mutex);
4132
4133 flush_workqueue(cgroup_pidlist_destroy_wq);
4134 BUG_ON(!list_empty(&cgrp->pidlists));
4135}
4136
4137static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4138{
4139 struct delayed_work *dwork = to_delayed_work(work);
4140 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4141 destroy_dwork);
4142 struct cgroup_pidlist *tofree = NULL;
4143
4144 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004145
4146 /*
Tejun Heo04502362013-11-29 10:42:59 -05004147 * Destroy iff we didn't get queued again. The state won't change
4148 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004149 */
Tejun Heo04502362013-11-29 10:42:59 -05004150 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004151 list_del(&l->links);
4152 pidlist_free(l->list);
4153 put_pid_ns(l->key.ns);
4154 tofree = l;
4155 }
4156
Tejun Heob1a21362013-11-29 10:42:58 -05004157 mutex_unlock(&l->owner->pidlist_mutex);
4158 kfree(tofree);
4159}
4160
4161/*
Ben Blum102a7752009-09-23 15:56:26 -07004162 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004163 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004164 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004165static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004166{
Ben Blum102a7752009-09-23 15:56:26 -07004167 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004168
4169 /*
4170 * we presume the 0th element is unique, so i starts at 1. trivial
4171 * edge cases first; no work needs to be done for either
4172 */
4173 if (length == 0 || length == 1)
4174 return length;
4175 /* src and dest walk down the list; dest counts unique elements */
4176 for (src = 1; src < length; src++) {
4177 /* find next unique element */
4178 while (list[src] == list[src-1]) {
4179 src++;
4180 if (src == length)
4181 goto after;
4182 }
4183 /* dest always points to where the next unique element goes */
4184 list[dest] = list[src];
4185 dest++;
4186 }
4187after:
Ben Blum102a7752009-09-23 15:56:26 -07004188 return dest;
4189}
4190
Tejun Heoafb2bc12013-11-29 10:42:59 -05004191/*
4192 * The two pid files - task and cgroup.procs - guaranteed that the result
4193 * is sorted, which forced this whole pidlist fiasco. As pid order is
4194 * different per namespace, each namespace needs differently sorted list,
4195 * making it impossible to use, for example, single rbtree of member tasks
4196 * sorted by task pointer. As pidlists can be fairly large, allocating one
4197 * per open file is dangerous, so cgroup had to implement shared pool of
4198 * pidlists keyed by cgroup and namespace.
4199 *
4200 * All this extra complexity was caused by the original implementation
4201 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004202 * want to do away with it. Explicitly scramble sort order if on the
4203 * default hierarchy so that no such expectation exists in the new
4204 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004205 *
4206 * Scrambling is done by swapping every two consecutive bits, which is
4207 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4208 */
4209static pid_t pid_fry(pid_t pid)
4210{
4211 unsigned a = pid & 0x55555555;
4212 unsigned b = pid & 0xAAAAAAAA;
4213
4214 return (a << 1) | (b >> 1);
4215}
4216
4217static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4218{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004219 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004220 return pid_fry(pid);
4221 else
4222 return pid;
4223}
4224
Ben Blum102a7752009-09-23 15:56:26 -07004225static int cmppid(const void *a, const void *b)
4226{
4227 return *(pid_t *)a - *(pid_t *)b;
4228}
4229
Tejun Heoafb2bc12013-11-29 10:42:59 -05004230static int fried_cmppid(const void *a, const void *b)
4231{
4232 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4233}
4234
Ben Blum72a8cb32009-09-23 15:56:27 -07004235static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4236 enum cgroup_filetype type)
4237{
4238 struct cgroup_pidlist *l;
4239 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004240 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004241
Tejun Heoe6b81712013-11-29 10:42:59 -05004242 lockdep_assert_held(&cgrp->pidlist_mutex);
4243
4244 list_for_each_entry(l, &cgrp->pidlists, links)
4245 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004246 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004247 return NULL;
4248}
4249
Ben Blum72a8cb32009-09-23 15:56:27 -07004250/*
4251 * find the appropriate pidlist for our purpose (given procs vs tasks)
4252 * returns with the lock on that pidlist already held, and takes care
4253 * of the use count, or returns NULL with no locks held if we're out of
4254 * memory.
4255 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004256static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4257 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004258{
4259 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004260
Tejun Heoe6b81712013-11-29 10:42:59 -05004261 lockdep_assert_held(&cgrp->pidlist_mutex);
4262
4263 l = cgroup_pidlist_find(cgrp, type);
4264 if (l)
4265 return l;
4266
Ben Blum72a8cb32009-09-23 15:56:27 -07004267 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004268 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004269 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004270 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004271
Tejun Heob1a21362013-11-29 10:42:58 -05004272 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004273 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004274 /* don't need task_nsproxy() if we're looking at ourself */
4275 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004276 l->owner = cgrp;
4277 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004278 return l;
4279}
4280
4281/*
Ben Blum102a7752009-09-23 15:56:26 -07004282 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4283 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004284static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4285 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004286{
4287 pid_t *array;
4288 int length;
4289 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004290 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004291 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004292 struct cgroup_pidlist *l;
4293
Tejun Heo4bac00d2013-11-29 10:42:59 -05004294 lockdep_assert_held(&cgrp->pidlist_mutex);
4295
Ben Blum102a7752009-09-23 15:56:26 -07004296 /*
4297 * If cgroup gets more users after we read count, we won't have
4298 * enough space - tough. This race is indistinguishable to the
4299 * caller from the case that the additional cgroup users didn't
4300 * show up until sometime later on.
4301 */
4302 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004303 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004304 if (!array)
4305 return -ENOMEM;
4306 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004307 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004308 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004309 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004310 break;
Ben Blum102a7752009-09-23 15:56:26 -07004311 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004312 if (type == CGROUP_FILE_PROCS)
4313 pid = task_tgid_vnr(tsk);
4314 else
4315 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004316 if (pid > 0) /* make sure to only use valid results */
4317 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004318 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004319 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004320 length = n;
4321 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004322 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004323 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4324 else
4325 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004326 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004327 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004328
Tejun Heoe6b81712013-11-29 10:42:59 -05004329 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004330 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004331 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004332 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004333 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004334
4335 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004336 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004337 l->list = array;
4338 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004339 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004340 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004341}
4342
Balbir Singh846c7bb2007-10-18 23:39:44 -07004343/**
Li Zefana043e3b2008-02-23 15:24:09 -08004344 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004345 * @stats: cgroupstats to fill information into
4346 * @dentry: A dentry entry belonging to the cgroup for which stats have
4347 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004348 *
4349 * Build and fill cgroupstats so that taskstats can export it to user
4350 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004351 */
4352int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4353{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004354 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004355 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004356 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004357 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004358
Tejun Heo2bd59d42014-02-11 11:52:49 -05004359 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4360 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4361 kernfs_type(kn) != KERNFS_DIR)
4362 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004363
Li Zefanbad34662014-02-14 16:54:28 +08004364 mutex_lock(&cgroup_mutex);
4365
Tejun Heo2bd59d42014-02-11 11:52:49 -05004366 /*
4367 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004368 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004369 * @kn->priv is RCU safe. Let's do the RCU dancing.
4370 */
4371 rcu_read_lock();
4372 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004373 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004374 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004375 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004376 return -ENOENT;
4377 }
Li Zefanbad34662014-02-14 16:54:28 +08004378 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004379
Tejun Heo9d800df2014-05-14 09:15:00 -04004380 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004381 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004382 switch (tsk->state) {
4383 case TASK_RUNNING:
4384 stats->nr_running++;
4385 break;
4386 case TASK_INTERRUPTIBLE:
4387 stats->nr_sleeping++;
4388 break;
4389 case TASK_UNINTERRUPTIBLE:
4390 stats->nr_uninterruptible++;
4391 break;
4392 case TASK_STOPPED:
4393 stats->nr_stopped++;
4394 break;
4395 default:
4396 if (delayacct_is_task_waiting_on_io(tsk))
4397 stats->nr_io_wait++;
4398 break;
4399 }
4400 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004401 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004402
Li Zefanbad34662014-02-14 16:54:28 +08004403 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004404 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004405}
4406
Paul Menage8f3ff202009-09-23 15:56:25 -07004407
Paul Menagecc31edc2008-10-18 20:28:04 -07004408/*
Ben Blum102a7752009-09-23 15:56:26 -07004409 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004410 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004411 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004412 */
4413
Ben Blum102a7752009-09-23 15:56:26 -07004414static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004415{
4416 /*
4417 * Initially we receive a position value that corresponds to
4418 * one more than the last pid shown (or 0 on the first call or
4419 * after a seek to the start). Use a binary-search to find the
4420 * next pid to display, if any
4421 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004422 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004423 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004424 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004425 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004426 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004427 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004428
Tejun Heo4bac00d2013-11-29 10:42:59 -05004429 mutex_lock(&cgrp->pidlist_mutex);
4430
4431 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004432 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004433 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004434 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004435 * could already have been destroyed.
4436 */
Tejun Heo5d224442013-12-05 12:28:04 -05004437 if (of->priv)
4438 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004439
4440 /*
4441 * Either this is the first start() after open or the matching
4442 * pidlist has been destroyed inbetween. Create a new one.
4443 */
Tejun Heo5d224442013-12-05 12:28:04 -05004444 if (!of->priv) {
4445 ret = pidlist_array_load(cgrp, type,
4446 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004447 if (ret)
4448 return ERR_PTR(ret);
4449 }
Tejun Heo5d224442013-12-05 12:28:04 -05004450 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004451
Paul Menagecc31edc2008-10-18 20:28:04 -07004452 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004453 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004454
Paul Menagecc31edc2008-10-18 20:28:04 -07004455 while (index < end) {
4456 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004457 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004458 index = mid;
4459 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004460 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004461 index = mid + 1;
4462 else
4463 end = mid;
4464 }
4465 }
4466 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004467 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004468 return NULL;
4469 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004470 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004471 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004472 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004473}
4474
Ben Blum102a7752009-09-23 15:56:26 -07004475static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004476{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004477 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004478 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004479
Tejun Heo5d224442013-12-05 12:28:04 -05004480 if (l)
4481 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004482 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004483 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004484}
4485
Ben Blum102a7752009-09-23 15:56:26 -07004486static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004487{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004488 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004489 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004490 pid_t *p = v;
4491 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004492 /*
4493 * Advance to the next pid in the array. If this goes off the
4494 * end, we're done
4495 */
4496 p++;
4497 if (p >= end) {
4498 return NULL;
4499 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004500 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004501 return p;
4502 }
4503}
4504
Ben Blum102a7752009-09-23 15:56:26 -07004505static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004506{
Joe Perches94ff2122015-04-15 16:18:20 -07004507 seq_printf(s, "%d\n", *(int *)v);
4508
4509 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004510}
4511
Tejun Heo182446d2013-08-08 20:11:24 -04004512static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4513 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004514{
Tejun Heo182446d2013-08-08 20:11:24 -04004515 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004516}
4517
Tejun Heo182446d2013-08-08 20:11:24 -04004518static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4519 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004520{
Paul Menage6379c102008-07-25 01:47:01 -07004521 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004522 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004523 else
Tejun Heo182446d2013-08-08 20:11:24 -04004524 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004525 return 0;
4526}
4527
Tejun Heo182446d2013-08-08 20:11:24 -04004528static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4529 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004530{
Tejun Heo182446d2013-08-08 20:11:24 -04004531 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004532}
4533
Tejun Heo182446d2013-08-08 20:11:24 -04004534static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4535 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004536{
4537 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004538 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004539 else
Tejun Heo182446d2013-08-08 20:11:24 -04004540 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004541 return 0;
4542}
4543
Tejun Heoa14c6872014-07-15 11:05:09 -04004544/* cgroup core interface files for the default hierarchy */
4545static struct cftype cgroup_dfl_base_files[] = {
4546 {
4547 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004548 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004549 .seq_start = cgroup_pidlist_start,
4550 .seq_next = cgroup_pidlist_next,
4551 .seq_stop = cgroup_pidlist_stop,
4552 .seq_show = cgroup_pidlist_show,
4553 .private = CGROUP_FILE_PROCS,
4554 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004555 },
4556 {
4557 .name = "cgroup.controllers",
4558 .flags = CFTYPE_ONLY_ON_ROOT,
4559 .seq_show = cgroup_root_controllers_show,
4560 },
4561 {
4562 .name = "cgroup.controllers",
4563 .flags = CFTYPE_NOT_ON_ROOT,
4564 .seq_show = cgroup_controllers_show,
4565 },
4566 {
4567 .name = "cgroup.subtree_control",
4568 .seq_show = cgroup_subtree_control_show,
4569 .write = cgroup_subtree_control_write,
4570 },
4571 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004572 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004573 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004574 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004575 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004576 },
4577 { } /* terminate */
4578};
4579
4580/* cgroup core interface files for the legacy hierarchies */
4581static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004582 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004583 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004584 .seq_start = cgroup_pidlist_start,
4585 .seq_next = cgroup_pidlist_next,
4586 .seq_stop = cgroup_pidlist_stop,
4587 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004588 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004589 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004590 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004591 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004592 .name = "cgroup.clone_children",
4593 .read_u64 = cgroup_clone_children_read,
4594 .write_u64 = cgroup_clone_children_write,
4595 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004596 {
Tejun Heo873fe092013-04-14 20:15:26 -07004597 .name = "cgroup.sane_behavior",
4598 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004599 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004600 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004601 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004602 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004603 .seq_start = cgroup_pidlist_start,
4604 .seq_next = cgroup_pidlist_next,
4605 .seq_stop = cgroup_pidlist_stop,
4606 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004607 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004608 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004609 },
4610 {
4611 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004612 .read_u64 = cgroup_read_notify_on_release,
4613 .write_u64 = cgroup_write_notify_on_release,
4614 },
Tejun Heo873fe092013-04-14 20:15:26 -07004615 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004616 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004617 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004618 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004619 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004620 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004621 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004622 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004623};
4624
Tejun Heo0c21ead2013-08-13 20:22:51 -04004625/*
4626 * css destruction is four-stage process.
4627 *
4628 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4629 * Implemented in kill_css().
4630 *
4631 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004632 * and thus css_tryget_online() is guaranteed to fail, the css can be
4633 * offlined by invoking offline_css(). After offlining, the base ref is
4634 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004635 *
4636 * 3. When the percpu_ref reaches zero, the only possible remaining
4637 * accessors are inside RCU read sections. css_release() schedules the
4638 * RCU callback.
4639 *
4640 * 4. After the grace period, the css can be freed. Implemented in
4641 * css_free_work_fn().
4642 *
4643 * It is actually hairier because both step 2 and 4 require process context
4644 * and thus involve punting to css->destroy_work adding two additional
4645 * steps to the already complex sequence.
4646 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004647static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004648{
4649 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004650 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004651 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004652 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004653
Tejun Heo9a1049d2014-06-28 08:10:14 -04004654 percpu_ref_exit(&css->refcnt);
4655
Vladimir Davydov01e58652015-02-12 14:59:26 -08004656 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004657 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004658 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004659 int id = css->id;
4660
Vladimir Davydov01e58652015-02-12 14:59:26 -08004661 ss->css_free(css);
4662 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004663 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004664
4665 if (parent)
4666 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004667 } else {
4668 /* cgroup free path */
4669 atomic_dec(&cgrp->root->nr_cgrps);
4670 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004671 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004672
Tejun Heod51f39b2014-05-16 13:22:48 -04004673 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004674 /*
4675 * We get a ref to the parent, and put the ref when
4676 * this cgroup is being freed, so it's guaranteed
4677 * that the parent won't be destroyed before its
4678 * children.
4679 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004680 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004681 kernfs_put(cgrp->kn);
4682 kfree(cgrp);
4683 } else {
4684 /*
4685 * This is root cgroup's refcnt reaching zero,
4686 * which indicates that the root should be
4687 * released.
4688 */
4689 cgroup_destroy_root(cgrp->root);
4690 }
4691 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004692}
4693
4694static void css_free_rcu_fn(struct rcu_head *rcu_head)
4695{
4696 struct cgroup_subsys_state *css =
4697 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4698
Tejun Heo0c21ead2013-08-13 20:22:51 -04004699 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004700 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004701}
4702
Tejun Heo25e15d82014-05-14 09:15:02 -04004703static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004704{
4705 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004706 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004707 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004708 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004709
Tejun Heo1fed1b22014-05-16 13:22:49 -04004710 mutex_lock(&cgroup_mutex);
4711
Tejun Heode3f0342014-05-16 13:22:49 -04004712 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004713 list_del_rcu(&css->sibling);
4714
Tejun Heo9d755d32014-05-14 09:15:02 -04004715 if (ss) {
4716 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004717 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004718 if (ss->css_released)
4719 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004720 } else {
4721 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004722 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4723 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004724
4725 /*
4726 * There are two control paths which try to determine
4727 * cgroup from dentry without going through kernfs -
4728 * cgroupstats_build() and css_tryget_online_from_dir().
4729 * Those are supported by RCU protecting clearing of
4730 * cgrp->kn->priv backpointer.
4731 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004732 if (cgrp->kn)
4733 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4734 NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004735 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004736
Tejun Heo1fed1b22014-05-16 13:22:49 -04004737 mutex_unlock(&cgroup_mutex);
4738
Tejun Heo0c21ead2013-08-13 20:22:51 -04004739 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004740}
4741
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004742static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004743{
4744 struct cgroup_subsys_state *css =
4745 container_of(ref, struct cgroup_subsys_state, refcnt);
4746
Tejun Heo25e15d82014-05-14 09:15:02 -04004747 INIT_WORK(&css->destroy_work, css_release_work_fn);
4748 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004749}
4750
Tejun Heoddfcada2014-05-04 15:09:14 -04004751static void init_and_link_css(struct cgroup_subsys_state *css,
4752 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004753{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004754 lockdep_assert_held(&cgroup_mutex);
4755
Tejun Heoddfcada2014-05-04 15:09:14 -04004756 cgroup_get(cgrp);
4757
Tejun Heod5c419b2014-05-16 13:22:48 -04004758 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004759 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004760 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004761 INIT_LIST_HEAD(&css->sibling);
4762 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004763 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05004764 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004765
Tejun Heod51f39b2014-05-16 13:22:48 -04004766 if (cgroup_parent(cgrp)) {
4767 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004768 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004769 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004770
Tejun Heoca8bdca2013-08-26 18:40:56 -04004771 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004772}
4773
Li Zefan2a4ac632013-07-31 16:16:40 +08004774/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004775static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004776{
Tejun Heo623f9262013-08-13 11:01:55 -04004777 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004778 int ret = 0;
4779
Tejun Heoa31f2d32012-11-19 08:13:37 -08004780 lockdep_assert_held(&cgroup_mutex);
4781
Tejun Heo92fb9742012-11-19 08:13:38 -08004782 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004783 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004784 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004785 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004786 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05004787
4788 atomic_inc(&css->online_cnt);
4789 if (css->parent)
4790 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04004791 }
Tejun Heob1929db2012-11-19 08:13:38 -08004792 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004793}
4794
Li Zefan2a4ac632013-07-31 16:16:40 +08004795/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004796static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004797{
Tejun Heo623f9262013-08-13 11:01:55 -04004798 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004799
4800 lockdep_assert_held(&cgroup_mutex);
4801
4802 if (!(css->flags & CSS_ONLINE))
4803 return;
4804
Vladimir Davydovfa062352016-03-01 19:56:30 +03004805 if (ss->css_reset)
4806 ss->css_reset(css);
4807
Li Zefand7eeac12013-03-12 15:35:59 -07004808 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004809 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004810
Tejun Heoeb954192013-08-08 20:11:23 -04004811 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004812 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004813
4814 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004815}
4816
Tejun Heoc81c925a2013-12-06 15:11:56 -05004817/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004818 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05004819 * @cgrp: the cgroup new css will be associated with
4820 * @ss: the subsys of new css
4821 *
4822 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004823 * css is online and installed in @cgrp. This function doesn't create the
4824 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004825 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004826static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4827 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004828{
Tejun Heod51f39b2014-05-16 13:22:48 -04004829 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004830 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004831 struct cgroup_subsys_state *css;
4832 int err;
4833
Tejun Heoc81c925a2013-12-06 15:11:56 -05004834 lockdep_assert_held(&cgroup_mutex);
4835
Tejun Heo1fed1b22014-05-16 13:22:49 -04004836 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004837 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004838 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004839
Tejun Heoddfcada2014-05-04 15:09:14 -04004840 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004841
Tejun Heo2aad2a82014-09-24 13:31:50 -04004842 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004843 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004844 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004845
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004846 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004847 if (err < 0)
4848 goto err_free_percpu_ref;
4849 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004850
Tejun Heo15a4c832014-05-04 15:09:14 -04004851 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004852 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004853 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004854
4855 err = online_css(css);
4856 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004857 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004858
Tejun Heoc81c925a2013-12-06 15:11:56 -05004859 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004860 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004861 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 -04004862 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004863 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004864 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004865 ss->warned_broken_hierarchy = true;
4866 }
4867
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004868 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004869
Tejun Heo1fed1b22014-05-16 13:22:49 -04004870err_list_del:
4871 list_del_rcu(&css->sibling);
Tejun Heo15a4c832014-05-04 15:09:14 -04004872 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004873err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004874 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004875err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004876 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004877 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004878}
4879
Tejun Heob3bfd982014-05-13 12:19:22 -04004880static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4881 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004882{
Tejun Heob11cfb52015-11-20 15:55:52 -05004883 struct cgroup *parent, *cgrp, *tcgrp;
Tejun Heoa9746d82014-05-13 12:19:22 -04004884 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004885 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004886 struct kernfs_node *kn;
Tejun Heob11cfb52015-11-20 15:55:52 -05004887 int level, ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004888
Alban Crequy71b1fb52014-08-18 12:20:20 +01004889 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4890 */
4891 if (strchr(name, '\n'))
4892 return -EINVAL;
4893
Tejun Heoa9746d82014-05-13 12:19:22 -04004894 parent = cgroup_kn_lock_live(parent_kn);
4895 if (!parent)
4896 return -ENODEV;
4897 root = parent->root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004898 level = parent->level + 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899
Tejun Heo0a950f62012-11-19 09:02:12 -08004900 /* allocate the cgroup and its ID, 0 is reserved for the root */
Tejun Heob11cfb52015-11-20 15:55:52 -05004901 cgrp = kzalloc(sizeof(*cgrp) +
4902 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004903 if (!cgrp) {
4904 ret = -ENOMEM;
4905 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004906 }
4907
Tejun Heo2aad2a82014-09-24 13:31:50 -04004908 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004909 if (ret)
4910 goto out_free_cgrp;
4911
Li Zefan0ab02ca2014-02-11 16:05:46 +08004912 /*
4913 * Temporarily set the pointer to NULL, so idr_find() won't return
4914 * a half-baked cgroup.
4915 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004916 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004917 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004918 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004919 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004920 }
4921
Paul Menagecc31edc2008-10-18 20:28:04 -07004922 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004923
Tejun Heo9d800df2014-05-14 09:15:00 -04004924 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004925 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004926 cgrp->level = level;
4927
4928 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
4929 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004930
Li Zefanb6abdb02008-03-04 14:28:19 -08004931 if (notify_on_release(parent))
4932 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4933
Tejun Heo2260e7f2012-11-19 08:13:38 -08004934 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4935 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004936
Tejun Heo2bd59d42014-02-11 11:52:49 -05004937 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004938 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004939 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004940 ret = PTR_ERR(kn);
4941 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004942 }
4943 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004944
Tejun Heo6f305582014-02-12 09:29:50 -05004945 /*
4946 * This extra ref will be put in cgroup_free_fn() and guarantees
4947 * that @cgrp->kn is always accessible.
4948 */
4949 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950
Tejun Heo0cb51d72014-05-16 13:22:49 -04004951 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004952
Tejun Heo4e139af2012-11-19 08:13:36 -08004953 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004954 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004955 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004956 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004957
Tejun Heo0d802552013-12-06 15:11:56 -05004958 /*
4959 * @cgrp is now fully operational. If something fails after this
4960 * point, it'll be released via the normal destruction path.
4961 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004962 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004963
Tejun Heoba0f4d72014-05-13 12:19:22 -04004964 ret = cgroup_kn_set_ugid(kn);
4965 if (ret)
4966 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004967
Tejun Heo4df8dc92015-09-18 17:54:23 -04004968 ret = css_populate_dir(&cgrp->self, NULL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004969 if (ret)
4970 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004971
Tejun Heo9d403e92013-12-06 15:11:56 -05004972 /* let's create and online css's */
Tejun Heo996cd1f2016-02-22 22:25:46 -05004973 do_each_subsys_mask(ss, ssid, parent->subtree_ss_mask) {
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004974 struct cgroup_subsys_state *css;
4975
4976 css = css_create(cgrp, ss);
4977 if (IS_ERR(css)) {
4978 ret = PTR_ERR(css);
Tejun Heo996cd1f2016-02-22 22:25:46 -05004979 goto out_destroy;
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004980 }
4981
4982 if (parent->subtree_control & (1 << ssid)) {
4983 ret = css_populate_dir(css, NULL);
4984 if (ret)
4985 goto out_destroy;
4986 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05004987 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07004988
Tejun Heobd53d612014-04-23 11:13:16 -04004989 /*
4990 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004991 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004992 */
Tejun Heo667c2492014-07-08 18:02:56 -04004993 if (!cgroup_on_dfl(cgrp)) {
4994 cgrp->subtree_control = parent->subtree_control;
Tejun Heo8699b772016-02-22 22:25:46 -05004995 cgroup_refresh_subtree_ss_mask(cgrp);
Tejun Heo667c2492014-07-08 18:02:56 -04004996 }
Tejun Heof392e512014-04-23 11:13:14 -04004997
Tejun Heo2bd59d42014-02-11 11:52:49 -05004998 kernfs_activate(kn);
4999
Tejun Heoba0f4d72014-05-13 12:19:22 -04005000 ret = 0;
5001 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005002
Tejun Heoba0f4d72014-05-13 12:19:22 -04005003out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04005004 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04005005out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04005006 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005007out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07005008 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005009out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005010 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005011 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04005012
5013out_destroy:
5014 cgroup_destroy_locked(cgrp);
5015 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005016}
5017
Tejun Heo223dbc32013-08-13 20:22:50 -04005018/*
5019 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005020 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5021 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005022 */
5023static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005024{
Tejun Heo223dbc32013-08-13 20:22:50 -04005025 struct cgroup_subsys_state *css =
5026 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005027
Tejun Heof20104d2013-08-13 20:22:50 -04005028 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005029
Tejun Heoaa226ff2016-01-21 15:31:11 -05005030 do {
5031 offline_css(css);
5032 css_put(css);
5033 /* @css can't go away while we're holding cgroup_mutex */
5034 css = css->parent;
5035 } while (css && atomic_dec_and_test(&css->online_cnt));
5036
5037 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005038}
5039
Tejun Heo223dbc32013-08-13 20:22:50 -04005040/* css kill confirmation processing requires process context, bounce */
5041static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005042{
5043 struct cgroup_subsys_state *css =
5044 container_of(ref, struct cgroup_subsys_state, refcnt);
5045
Tejun Heoaa226ff2016-01-21 15:31:11 -05005046 if (atomic_dec_and_test(&css->online_cnt)) {
5047 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5048 queue_work(cgroup_destroy_wq, &css->destroy_work);
5049 }
Tejun Heod3daf282013-06-13 19:39:16 -07005050}
5051
Tejun Heof392e512014-04-23 11:13:14 -04005052/**
5053 * kill_css - destroy a css
5054 * @css: css to destroy
5055 *
5056 * This function initiates destruction of @css by removing cgroup interface
5057 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005058 * asynchronously once css_tryget_online() is guaranteed to fail and when
5059 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005060 */
5061static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005062{
Tejun Heo01f64742014-05-13 12:19:23 -04005063 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005064
Tejun Heo2bd59d42014-02-11 11:52:49 -05005065 /*
5066 * This must happen before css is disassociated with its cgroup.
5067 * See seq_css() for details.
5068 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005069 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005070
Tejun Heoedae0c32013-08-13 20:22:51 -04005071 /*
5072 * Killing would put the base ref, but we need to keep it alive
5073 * until after ->css_offline().
5074 */
5075 css_get(css);
5076
5077 /*
5078 * cgroup core guarantees that, by the time ->css_offline() is
5079 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005080 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005081 * proceed to offlining css's because percpu_ref_kill() doesn't
5082 * guarantee that the ref is seen as killed on all CPUs on return.
5083 *
5084 * Use percpu_ref_kill_and_confirm() to get notifications as each
5085 * css is confirmed to be seen as killed on all CPUs.
5086 */
5087 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005088}
5089
5090/**
5091 * cgroup_destroy_locked - the first stage of cgroup destruction
5092 * @cgrp: cgroup to be destroyed
5093 *
5094 * css's make use of percpu refcnts whose killing latency shouldn't be
5095 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005096 * guarantee that css_tryget_online() won't succeed by the time
5097 * ->css_offline() is invoked. To satisfy all the requirements,
5098 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005099 *
5100 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5101 * userland visible parts and start killing the percpu refcnts of
5102 * css's. Set up so that the next stage will be kicked off once all
5103 * the percpu refcnts are confirmed to be killed.
5104 *
5105 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5106 * rest of destruction. Once all cgroup references are gone, the
5107 * cgroup is RCU-freed.
5108 *
5109 * This function implements s1. After this step, @cgrp is gone as far as
5110 * the userland is concerned and a new cgroup with the same name may be
5111 * created. As cgroup doesn't care about the names internally, this
5112 * doesn't cause any problem.
5113 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005114static int cgroup_destroy_locked(struct cgroup *cgrp)
5115 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005116{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005117 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005118 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005119
Tejun Heo42809dd2012-11-19 08:13:37 -08005120 lockdep_assert_held(&cgroup_mutex);
5121
Tejun Heo91486f62015-10-15 16:41:51 -04005122 /*
5123 * Only migration can raise populated from zero and we're already
5124 * holding cgroup_mutex.
5125 */
5126 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005127 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005128
Tejun Heo1a90dd52012-11-05 09:16:59 -08005129 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005130 * Make sure there's no live children. We can't test emptiness of
5131 * ->self.children as dead children linger on it while being
5132 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005133 */
Tejun Heof3d46502014-05-16 13:22:52 -04005134 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005135 return -EBUSY;
5136
5137 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005138 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005139 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005140 */
Tejun Heo184faf32014-05-16 13:22:51 -04005141 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005142
Tejun Heo249f3462014-05-14 09:15:01 -04005143 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005144 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005145 kill_css(css);
5146
Tejun Heo455050d2013-06-13 19:27:41 -07005147 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005148 * Remove @cgrp directory along with the base files. @cgrp has an
5149 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005150 */
Tejun Heo01f64742014-05-13 12:19:23 -04005151 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005152
Tejun Heod51f39b2014-05-16 13:22:48 -04005153 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005154
Tejun Heo249f3462014-05-14 09:15:01 -04005155 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005156 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005157
Tejun Heoea15f8c2013-06-13 19:27:42 -07005158 return 0;
5159};
5160
Tejun Heo2bd59d42014-02-11 11:52:49 -05005161static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005162{
Tejun Heoa9746d82014-05-13 12:19:22 -04005163 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005164 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005165
Tejun Heoa9746d82014-05-13 12:19:22 -04005166 cgrp = cgroup_kn_lock_live(kn);
5167 if (!cgrp)
5168 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005169
Tejun Heoa9746d82014-05-13 12:19:22 -04005170 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005171
Tejun Heoa9746d82014-05-13 12:19:22 -04005172 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005173 return ret;
5174}
5175
Tejun Heo2bd59d42014-02-11 11:52:49 -05005176static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5177 .remount_fs = cgroup_remount,
5178 .show_options = cgroup_show_options,
5179 .mkdir = cgroup_mkdir,
5180 .rmdir = cgroup_rmdir,
5181 .rename = cgroup_rename,
5182};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005183
Tejun Heo15a4c832014-05-04 15:09:14 -04005184static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005185{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005186 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005187
Tejun Heoa5ae9892015-12-29 14:53:56 -05005188 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005189
Tejun Heo648bb562012-11-19 08:13:36 -08005190 mutex_lock(&cgroup_mutex);
5191
Tejun Heo15a4c832014-05-04 15:09:14 -04005192 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005193 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005194
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005195 /* Create the root cgroup state for this subsystem */
5196 ss->root = &cgrp_dfl_root;
5197 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005198 /* We don't handle early failures gracefully */
5199 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005200 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005201
5202 /*
5203 * Root csses are never destroyed and we can't initialize
5204 * percpu_ref during early init. Disable refcnting.
5205 */
5206 css->flags |= CSS_NO_REF;
5207
Tejun Heo15a4c832014-05-04 15:09:14 -04005208 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005209 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005210 css->id = 1;
5211 } else {
5212 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5213 BUG_ON(css->id < 0);
5214 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005215
Li Zefane8d55fd2008-04-29 01:00:13 -07005216 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005217 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005218 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005219 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005220 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005221
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005222 have_fork_callback |= (bool)ss->fork << ss->id;
5223 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005224 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005225 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005226
Li Zefane8d55fd2008-04-29 01:00:13 -07005227 /* At system boot, before all subsystems have been
5228 * registered, no tasks have been forked, so we don't
5229 * need to invoke fork callbacks here. */
5230 BUG_ON(!list_empty(&init_task.tasks));
5231
Tejun Heoae7f1642013-08-13 20:22:50 -04005232 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005233
Tejun Heo648bb562012-11-19 08:13:36 -08005234 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005235}
5236
5237/**
Li Zefana043e3b2008-02-23 15:24:09 -08005238 * cgroup_init_early - cgroup initialization at system boot
5239 *
5240 * Initialize cgroups at system boot, and initialize any
5241 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005242 */
5243int __init cgroup_init_early(void)
5244{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005245 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005246 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005247 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005248
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005249 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005250 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5251
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005252 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005253
Tejun Heo3ed80a62014-02-08 10:36:58 -05005254 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005255 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005256 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005257 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005258 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005259 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5260 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005261
Tejun Heoaec25022014-02-08 10:36:58 -05005262 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005263 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005264 if (!ss->legacy_name)
5265 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005266
5267 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005268 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005269 }
5270 return 0;
5271}
5272
Tejun Heo6e5c8302016-02-22 22:25:47 -05005273static u16 cgroup_disable_mask __initdata;
Tejun Heoa3e72732015-09-25 16:24:27 -04005274
Paul Menageddbcc7e2007-10-18 23:39:30 -07005275/**
Li Zefana043e3b2008-02-23 15:24:09 -08005276 * cgroup_init - cgroup initialization
5277 *
5278 * Register cgroup filesystem and /proc file, and initialize
5279 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005280 */
5281int __init cgroup_init(void)
5282{
Tejun Heo30159ec2013-06-25 11:53:37 -07005283 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005284 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005285
Tejun Heo6e5c8302016-02-22 22:25:47 -05005286 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005287 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005288 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5289 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005290
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005291 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005292
Tejun Heo2378d8b2016-03-03 09:57:57 -05005293 /*
5294 * Add init_css_set to the hash table so that dfl_root can link to
5295 * it during init.
5296 */
5297 hash_add(css_set_table, &init_css_set.hlist,
5298 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005299
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005300 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005301
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005302 mutex_unlock(&cgroup_mutex);
5303
Tejun Heo172a2c062014-03-19 10:23:53 -04005304 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005305 if (ss->early_init) {
5306 struct cgroup_subsys_state *css =
5307 init_css_set.subsys[ss->id];
5308
5309 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5310 GFP_KERNEL);
5311 BUG_ON(css->id < 0);
5312 } else {
5313 cgroup_init_subsys(ss, false);
5314 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005315
Tejun Heo2d8f2432014-04-23 11:13:15 -04005316 list_add_tail(&init_css_set.e_cset_node[ssid],
5317 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005318
5319 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005320 * Setting dfl_root subsys_mask needs to consider the
5321 * disabled flag and cftype registration needs kmalloc,
5322 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005323 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005324 if (cgroup_disable_mask & (1 << ssid)) {
5325 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5326 printk(KERN_INFO "Disabling %s control group subsystem\n",
5327 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005328 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005329 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005330
Johannes Weiner223ffb22016-02-11 13:34:49 -05005331 if (cgroup_ssid_no_v1(ssid))
5332 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5333 ss->name);
5334
Tejun Heoa8ddc822014-07-15 11:05:10 -04005335 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5336
Tejun Heo5de4fa12014-07-15 11:05:10 -04005337 if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005338 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005339
Tejun Heoa8ddc822014-07-15 11:05:10 -04005340 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5341 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5342 } else {
5343 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5344 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005345 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005346
5347 if (ss->bind)
5348 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005349 }
Greg KH676db4a2010-08-05 13:53:35 -07005350
Tejun Heo2378d8b2016-03-03 09:57:57 -05005351 /* init_css_set.subsys[] has been updated, re-hash */
5352 hash_del(&init_css_set.hlist);
5353 hash_add(css_set_table, &init_css_set.hlist,
5354 css_set_hash(init_css_set.subsys));
5355
Tejun Heo035f4f52015-10-15 17:00:43 -04005356 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5357 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005358 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005359 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005360
Tejun Heo2bd59d42014-02-11 11:52:49 -05005361 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005362}
Paul Menageb4f48b62007-10-18 23:39:33 -07005363
Tejun Heoe5fca242013-11-22 17:14:39 -05005364static int __init cgroup_wq_init(void)
5365{
5366 /*
5367 * There isn't much point in executing destruction path in
5368 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005369 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005370 *
5371 * We would prefer to do this in cgroup_init() above, but that
5372 * is called before init_workqueues(): so leave this until after.
5373 */
Tejun Heo1a115332014-02-12 19:06:19 -05005374 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005375 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005376
5377 /*
5378 * Used to destroy pidlists and separate to serve as flush domain.
5379 * Cap @max_active to 1 too.
5380 */
5381 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5382 0, 1);
5383 BUG_ON(!cgroup_pidlist_destroy_wq);
5384
Tejun Heoe5fca242013-11-22 17:14:39 -05005385 return 0;
5386}
5387core_initcall(cgroup_wq_init);
5388
Paul Menagea4243162007-10-18 23:39:35 -07005389/*
5390 * proc_cgroup_show()
5391 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5392 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005393 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005394int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5395 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005396{
Tejun Heoe61734c2014-02-12 09:29:50 -05005397 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005398 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005399 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005400
5401 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005402 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005403 if (!buf)
5404 goto out;
5405
Paul Menagea4243162007-10-18 23:39:35 -07005406 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005407 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005408
Tejun Heo985ed672014-03-19 10:23:53 -04005409 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005410 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005411 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005412 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005413
Tejun Heoa7165262016-02-23 10:00:50 -05005414 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005415 continue;
5416
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005417 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005418 if (root != &cgrp_dfl_root)
5419 for_each_subsys(ss, ssid)
5420 if (root->subsys_mask & (1 << ssid))
5421 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005422 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005423 if (strlen(root->name))
5424 seq_printf(m, "%sname=%s", count ? "," : "",
5425 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005426 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005427
Paul Menage7717f7b2009-09-23 15:56:22 -07005428 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005429
5430 /*
5431 * On traditional hierarchies, all zombie tasks show up as
5432 * belonging to the root cgroup. On the default hierarchy,
5433 * while a zombie doesn't show up in "cgroup.procs" and
5434 * thus can't be migrated, its /proc/PID/cgroup keeps
5435 * reporting the cgroup it belonged to before exiting. If
5436 * the cgroup is removed before the zombie is reaped,
5437 * " (deleted)" is appended to the cgroup path.
5438 */
5439 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5440 path = cgroup_path(cgrp, buf, PATH_MAX);
5441 if (!path) {
5442 retval = -ENAMETOOLONG;
5443 goto out_unlock;
5444 }
5445 } else {
5446 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005447 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005448
Tejun Heoe61734c2014-02-12 09:29:50 -05005449 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005450
5451 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5452 seq_puts(m, " (deleted)\n");
5453 else
5454 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005455 }
5456
Zefan Li006f4ac2014-09-18 16:03:15 +08005457 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005458out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005459 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005460 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005461 kfree(buf);
5462out:
5463 return retval;
5464}
5465
Paul Menagea4243162007-10-18 23:39:35 -07005466/* Display information about each subsystem and each hierarchy */
5467static int proc_cgroupstats_show(struct seq_file *m, void *v)
5468{
Tejun Heo30159ec2013-06-25 11:53:37 -07005469 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005470 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005471
Paul Menage8bab8dd2008-04-04 14:29:57 -07005472 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005473 /*
5474 * ideally we don't want subsystems moving around while we do this.
5475 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5476 * subsys/hierarchy state.
5477 */
Paul Menagea4243162007-10-18 23:39:35 -07005478 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005479
5480 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005481 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005482 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005483 atomic_read(&ss->root->nr_cgrps),
5484 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005485
Paul Menagea4243162007-10-18 23:39:35 -07005486 mutex_unlock(&cgroup_mutex);
5487 return 0;
5488}
5489
5490static int cgroupstats_open(struct inode *inode, struct file *file)
5491{
Al Viro9dce07f2008-03-29 03:07:28 +00005492 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005493}
5494
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005495static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005496 .open = cgroupstats_open,
5497 .read = seq_read,
5498 .llseek = seq_lseek,
5499 .release = single_release,
5500};
5501
Paul Menageb4f48b62007-10-18 23:39:33 -07005502/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005503 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005504 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005505 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005506 * A task is associated with the init_css_set until cgroup_post_fork()
5507 * attaches it to the parent's css_set. Empty cg_list indicates that
5508 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005509 */
5510void cgroup_fork(struct task_struct *child)
5511{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005512 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005513 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005514}
5515
5516/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005517 * cgroup_can_fork - called on a new task before the process is exposed
5518 * @child: the task in question.
5519 *
5520 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5521 * returns an error, the fork aborts with that error code. This allows for
5522 * a cgroup subsystem to conditionally allow or deny new forks.
5523 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005524int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005525{
5526 struct cgroup_subsys *ss;
5527 int i, j, ret;
5528
Tejun Heob4e0eea2016-02-22 22:25:46 -05005529 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005530 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005531 if (ret)
5532 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05005533 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10005534
5535 return 0;
5536
5537out_revert:
5538 for_each_subsys(ss, j) {
5539 if (j >= i)
5540 break;
5541 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005542 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005543 }
5544
5545 return ret;
5546}
5547
5548/**
5549 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5550 * @child: the task in question
5551 *
5552 * This calls the cancel_fork() callbacks if a fork failed *after*
5553 * cgroup_can_fork() succeded.
5554 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005555void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005556{
5557 struct cgroup_subsys *ss;
5558 int i;
5559
5560 for_each_subsys(ss, i)
5561 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005562 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005563}
5564
5565/**
Li Zefana043e3b2008-02-23 15:24:09 -08005566 * cgroup_post_fork - called on a new task after adding it to the task list
5567 * @child: the task in question
5568 *
Tejun Heo5edee612012-10-16 15:03:14 -07005569 * Adds the task to the list running through its css_set if necessary and
5570 * call the subsystem fork() callbacks. Has to be after the task is
5571 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005572 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005573 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005574 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005575void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005576{
Tejun Heo30159ec2013-06-25 11:53:37 -07005577 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005578 int i;
5579
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005580 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005581 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005582 * function sets use_task_css_set_links before grabbing
5583 * tasklist_lock and we just went through tasklist_lock to add
5584 * @child, it's guaranteed that either we see the set
5585 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5586 * @child during its iteration.
5587 *
5588 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005589 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005590 * association is stable, and, on completion of the parent's
5591 * migration, @child is visible in the source of migration or
5592 * already in the destination cgroup. This guarantee is necessary
5593 * when implementing operations which need to migrate all tasks of
5594 * a cgroup to another.
5595 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005596 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005597 * will remain in init_css_set. This is safe because all tasks are
5598 * in the init_css_set before cg_links is enabled and there's no
5599 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005600 */
Paul Menage817929e2007-10-18 23:39:36 -07005601 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005602 struct css_set *cset;
5603
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005604 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005605 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005606 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005607 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005608 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005609 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005610 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005611 }
Tejun Heo5edee612012-10-16 15:03:14 -07005612
5613 /*
5614 * Call ss->fork(). This must happen after @child is linked on
5615 * css_set; otherwise, @child might change state between ->fork()
5616 * and addition to css_set.
5617 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005618 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005619 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005620 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07005621}
Tejun Heo5edee612012-10-16 15:03:14 -07005622
Paul Menage817929e2007-10-18 23:39:36 -07005623/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005624 * cgroup_exit - detach cgroup from exiting task
5625 * @tsk: pointer to task_struct of exiting process
5626 *
5627 * Description: Detach cgroup from @tsk and release it.
5628 *
5629 * Note that cgroups marked notify_on_release force every task in
5630 * them to take the global cgroup_mutex mutex when exiting.
5631 * This could impact scaling on very large systems. Be reluctant to
5632 * use notify_on_release cgroups where very high task exit scaling
5633 * is required on large systems.
5634 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005635 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5636 * call cgroup_exit() while the task is still competent to handle
5637 * notify_on_release(), then leave the task attached to the root cgroup in
5638 * each hierarchy for the remainder of its exit. No need to bother with
5639 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005640 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005641 */
Li Zefan1ec41832014-03-28 15:22:19 +08005642void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005643{
Tejun Heo30159ec2013-06-25 11:53:37 -07005644 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005645 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005646 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005647
5648 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005649 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005650 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005651 */
Tejun Heo0de09422015-10-15 16:41:49 -04005652 cset = task_css_set(tsk);
5653
Paul Menage817929e2007-10-18 23:39:36 -07005654 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005655 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005656 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005657 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005658 } else {
5659 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005660 }
5661
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005662 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005663 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04005664 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005665 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04005666}
Tejun Heo30159ec2013-06-25 11:53:37 -07005667
Tejun Heo2e91fa72015-10-15 16:41:53 -04005668void cgroup_free(struct task_struct *task)
5669{
5670 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005671 struct cgroup_subsys *ss;
5672 int ssid;
5673
Tejun Heob4e0eea2016-02-22 22:25:46 -05005674 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04005675 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005676 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005677
Tejun Heo2e91fa72015-10-15 16:41:53 -04005678 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005679}
Paul Menage697f4162007-10-18 23:39:34 -07005680
Paul Menagebd89aab2007-10-18 23:40:44 -07005681static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005682{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005683 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005684 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5685 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005686}
5687
Paul Menage81a6a5c2007-10-18 23:39:38 -07005688/*
5689 * Notify userspace when a cgroup is released, by running the
5690 * configured release agent with the name of the cgroup (path
5691 * relative to the root of cgroup file system) as the argument.
5692 *
5693 * Most likely, this user command will try to rmdir this cgroup.
5694 *
5695 * This races with the possibility that some other task will be
5696 * attached to this cgroup before it is removed, or that some other
5697 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5698 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5699 * unused, and this cgroup will be reprieved from its death sentence,
5700 * to continue to serve a useful existence. Next time it's released,
5701 * we will get notified again, if it still has 'notify_on_release' set.
5702 *
5703 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5704 * means only wait until the task is successfully execve()'d. The
5705 * separate release agent task is forked by call_usermodehelper(),
5706 * then control in this thread returns here, without waiting for the
5707 * release agent task. We don't bother to wait because the caller of
5708 * this routine has no use for the exit status of the release agent
5709 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005710 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005711static void cgroup_release_agent(struct work_struct *work)
5712{
Zefan Li971ff492014-09-18 16:06:19 +08005713 struct cgroup *cgrp =
5714 container_of(work, struct cgroup, release_agent_work);
5715 char *pathbuf = NULL, *agentbuf = NULL, *path;
5716 char *argv[3], *envp[3];
5717
Paul Menage81a6a5c2007-10-18 23:39:38 -07005718 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005719
Zefan Li971ff492014-09-18 16:06:19 +08005720 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5721 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5722 if (!pathbuf || !agentbuf)
5723 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005724
Zefan Li971ff492014-09-18 16:06:19 +08005725 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5726 if (!path)
5727 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005728
Zefan Li971ff492014-09-18 16:06:19 +08005729 argv[0] = agentbuf;
5730 argv[1] = path;
5731 argv[2] = NULL;
5732
5733 /* minimal command environment */
5734 envp[0] = "HOME=/";
5735 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5736 envp[2] = NULL;
5737
Paul Menage81a6a5c2007-10-18 23:39:38 -07005738 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005739 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005740 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005741out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005742 mutex_unlock(&cgroup_mutex);
5743out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005744 kfree(agentbuf);
5745 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005746}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005747
5748static int __init cgroup_disable(char *str)
5749{
Tejun Heo30159ec2013-06-25 11:53:37 -07005750 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005751 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005752 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005753
5754 while ((token = strsep(&str, ",")) != NULL) {
5755 if (!*token)
5756 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005757
Tejun Heo3ed80a62014-02-08 10:36:58 -05005758 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005759 if (strcmp(token, ss->name) &&
5760 strcmp(token, ss->legacy_name))
5761 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005762 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005763 }
5764 }
5765 return 1;
5766}
5767__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005768
Johannes Weiner223ffb22016-02-11 13:34:49 -05005769static int __init cgroup_no_v1(char *str)
5770{
5771 struct cgroup_subsys *ss;
5772 char *token;
5773 int i;
5774
5775 while ((token = strsep(&str, ",")) != NULL) {
5776 if (!*token)
5777 continue;
5778
5779 if (!strcmp(token, "all")) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05005780 cgroup_no_v1_mask = U16_MAX;
Johannes Weiner223ffb22016-02-11 13:34:49 -05005781 break;
5782 }
5783
5784 for_each_subsys(ss, i) {
5785 if (strcmp(token, ss->name) &&
5786 strcmp(token, ss->legacy_name))
5787 continue;
5788
5789 cgroup_no_v1_mask |= 1 << i;
5790 }
5791 }
5792 return 1;
5793}
5794__setup("cgroup_no_v1=", cgroup_no_v1);
5795
Tejun Heob77d7b62013-08-13 11:01:54 -04005796/**
Tejun Heoec903c02014-05-13 12:11:01 -04005797 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005798 * @dentry: directory dentry of interest
5799 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005800 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005801 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5802 * to get the corresponding css and return it. If such css doesn't exist
5803 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005804 */
Tejun Heoec903c02014-05-13 12:11:01 -04005805struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5806 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005807{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005808 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05005809 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005810 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005811 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005812
Tejun Heo35cf0832013-08-26 18:40:56 -04005813 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05005814 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5815 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005816 return ERR_PTR(-EBADF);
5817
Tejun Heo5a17f542014-02-11 11:52:47 -05005818 rcu_read_lock();
5819
Tejun Heo2bd59d42014-02-11 11:52:49 -05005820 /*
5821 * This path doesn't originate from kernfs and @kn could already
5822 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005823 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005824 */
5825 cgrp = rcu_dereference(kn->priv);
5826 if (cgrp)
5827 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005828
Tejun Heoec903c02014-05-13 12:11:01 -04005829 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005830 css = ERR_PTR(-ENOENT);
5831
5832 rcu_read_unlock();
5833 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005834}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005835
Li Zefan1cb650b2013-08-19 10:05:24 +08005836/**
5837 * css_from_id - lookup css by id
5838 * @id: the cgroup id
5839 * @ss: cgroup subsys to be looked into
5840 *
5841 * Returns the css if there's valid one with @id, otherwise returns NULL.
5842 * Should be called under rcu_read_lock().
5843 */
5844struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5845{
Tejun Heo6fa49182014-05-04 15:09:13 -04005846 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005847 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005848}
5849
Tejun Heo16af4392015-11-20 15:55:52 -05005850/**
5851 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5852 * @path: path on the default hierarchy
5853 *
5854 * Find the cgroup at @path on the default hierarchy, increment its
5855 * reference count and return it. Returns pointer to the found cgroup on
5856 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5857 * if @path points to a non-directory.
5858 */
5859struct cgroup *cgroup_get_from_path(const char *path)
5860{
5861 struct kernfs_node *kn;
5862 struct cgroup *cgrp;
5863
5864 mutex_lock(&cgroup_mutex);
5865
5866 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5867 if (kn) {
5868 if (kernfs_type(kn) == KERNFS_DIR) {
5869 cgrp = kn->priv;
5870 cgroup_get(cgrp);
5871 } else {
5872 cgrp = ERR_PTR(-ENOTDIR);
5873 }
5874 kernfs_put(kn);
5875 } else {
5876 cgrp = ERR_PTR(-ENOENT);
5877 }
5878
5879 mutex_unlock(&cgroup_mutex);
5880 return cgrp;
5881}
5882EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5883
Tejun Heobd1060a2015-12-07 17:38:53 -05005884/*
5885 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5886 * definition in cgroup-defs.h.
5887 */
5888#ifdef CONFIG_SOCK_CGROUP_DATA
5889
5890#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5891
Tejun Heo3fa4cc92015-12-14 11:24:06 -05005892DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05005893static bool cgroup_sk_alloc_disabled __read_mostly;
5894
5895void cgroup_sk_alloc_disable(void)
5896{
5897 if (cgroup_sk_alloc_disabled)
5898 return;
5899 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5900 cgroup_sk_alloc_disabled = true;
5901}
5902
5903#else
5904
5905#define cgroup_sk_alloc_disabled false
5906
5907#endif
5908
5909void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5910{
5911 if (cgroup_sk_alloc_disabled)
5912 return;
5913
5914 rcu_read_lock();
5915
5916 while (true) {
5917 struct css_set *cset;
5918
5919 cset = task_css_set(current);
5920 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5921 skcd->val = (unsigned long)cset->dfl_cgrp;
5922 break;
5923 }
5924 cpu_relax();
5925 }
5926
5927 rcu_read_unlock();
5928}
5929
5930void cgroup_sk_free(struct sock_cgroup_data *skcd)
5931{
5932 cgroup_put(sock_cgroup_ptr(skcd));
5933}
5934
5935#endif /* CONFIG_SOCK_CGROUP_DATA */
5936
Paul Menagefe693432009-09-23 15:56:20 -07005937#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005938static struct cgroup_subsys_state *
5939debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005940{
5941 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5942
5943 if (!css)
5944 return ERR_PTR(-ENOMEM);
5945
5946 return css;
5947}
5948
Tejun Heoeb954192013-08-08 20:11:23 -04005949static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005950{
Tejun Heoeb954192013-08-08 20:11:23 -04005951 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005952}
5953
Tejun Heo182446d2013-08-08 20:11:24 -04005954static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5955 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005956{
Tejun Heo182446d2013-08-08 20:11:24 -04005957 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005958}
5959
Tejun Heo182446d2013-08-08 20:11:24 -04005960static u64 current_css_set_read(struct cgroup_subsys_state *css,
5961 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005962{
5963 return (u64)(unsigned long)current->cgroups;
5964}
5965
Tejun Heo182446d2013-08-08 20:11:24 -04005966static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005967 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005968{
5969 u64 count;
5970
5971 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005972 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005973 rcu_read_unlock();
5974 return count;
5975}
5976
Tejun Heo2da8ca82013-12-05 12:28:04 -05005977static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005978{
Tejun Heo69d02062013-06-12 21:04:50 -07005979 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005980 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005981 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005982
Tejun Heoe61734c2014-02-12 09:29:50 -05005983 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5984 if (!name_buf)
5985 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005986
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005987 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005988 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005989 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005990 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005991 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005992
Tejun Heoa2dd4242014-03-19 10:23:55 -04005993 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005994 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005995 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005996 }
5997 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005998 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005999 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006000 return 0;
6001}
6002
6003#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05006004static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006005{
Tejun Heo2da8ca82013-12-05 12:28:04 -05006006 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07006007 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07006008
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006009 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04006010 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07006011 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07006012 struct task_struct *task;
6013 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05006014
Tejun Heo5abb8852013-06-12 21:04:49 -07006015 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05006016
Tejun Heo5abb8852013-06-12 21:04:49 -07006017 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05006018 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6019 goto overflow;
6020 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07006021 }
Tejun Heoc7561122014-02-25 10:04:01 -05006022
6023 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
6024 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6025 goto overflow;
6026 seq_printf(seq, " task %d\n", task_pid_vnr(task));
6027 }
6028 continue;
6029 overflow:
6030 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07006031 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006032 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006033 return 0;
6034}
6035
Tejun Heo182446d2013-08-08 20:11:24 -04006036static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006037{
Tejun Heo27bd4db2015-10-15 16:41:50 -04006038 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08006039 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07006040}
6041
6042static struct cftype debug_files[] = {
6043 {
Paul Menagefe693432009-09-23 15:56:20 -07006044 .name = "taskcount",
6045 .read_u64 = debug_taskcount_read,
6046 },
6047
6048 {
6049 .name = "current_css_set",
6050 .read_u64 = current_css_set_read,
6051 },
6052
6053 {
6054 .name = "current_css_set_refcount",
6055 .read_u64 = current_css_set_refcount_read,
6056 },
6057
6058 {
Paul Menage7717f7b2009-09-23 15:56:22 -07006059 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006060 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006061 },
6062
6063 {
6064 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006065 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006066 },
6067
6068 {
Paul Menagefe693432009-09-23 15:56:20 -07006069 .name = "releasable",
6070 .read_u64 = releasable_read,
6071 },
Paul Menagefe693432009-09-23 15:56:20 -07006072
Tejun Heo4baf6e32012-04-01 12:09:55 -07006073 { } /* terminate */
6074};
Paul Menagefe693432009-09-23 15:56:20 -07006075
Tejun Heo073219e2014-02-08 10:36:58 -05006076struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006077 .css_alloc = debug_css_alloc,
6078 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04006079 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07006080};
6081#endif /* CONFIG_CGROUP_DEBUG */