blob: d27904c193daa1d8a8680522093254cfde376177 [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 */
181static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700182
Tejun Heo5533e012014-05-14 19:33:07 -0400183/* some controllers are not supported in the default hierarchy */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000184static unsigned long cgrp_dfl_root_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400185
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186/* The list of hierarchy roots */
187
Tejun Heo9871bf92013-06-24 15:21:47 -0700188static LIST_HEAD(cgroup_roots);
189static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700190
Tejun Heo3417ae12014-02-08 10:37:01 -0500191/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700192static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700193
Li Zefan794611a2013-06-18 18:53:53 +0800194/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400195 * Assign a monotonically increasing serial number to csses. It guarantees
196 * cgroups with bigger numbers are newer than those with smaller numbers.
197 * Also, as csses are always appended to the parent's ->children list, it
198 * guarantees that sibling csses are always sorted in the ascending serial
199 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800200 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400201static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800202
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000203/*
204 * These bitmask flags indicate whether tasks in the fork and exit paths have
205 * fork/exit handlers to call. This avoids us having to do extra work in the
206 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000208static unsigned long have_fork_callback __read_mostly;
209static unsigned long have_exit_callback __read_mostly;
Tejun Heoafcf6c82015-10-15 16:41:53 -0400210static unsigned long have_free_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211
Aleksa Sarai7e476822015-06-09 21:32:09 +1000212/* Ditto for the can_fork callback. */
213static unsigned long have_canfork_callback __read_mostly;
214
Tejun Heo67e9c742015-11-16 11:13:34 -0500215static struct file_system_type cgroup2_fs_type;
Tejun Heoa14c6872014-07-15 11:05:09 -0400216static struct cftype cgroup_dfl_base_files[];
217static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700218
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400219static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000220 unsigned long ss_mask);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400221static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400223static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
224 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400225static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400226static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400227static int cgroup_addrm_files(struct cgroup_subsys_state *css,
228 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400229 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800230
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400231/**
232 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
233 * @ssid: subsys ID of interest
234 *
235 * cgroup_subsys_enabled() can only be used with literal subsys names which
236 * is fine for individual subsystems but unsuitable for cgroup core. This
237 * is slower static_key_enabled() based test indexed by @ssid.
238 */
239static bool cgroup_ssid_enabled(int ssid)
240{
241 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
242}
243
Tejun Heo9e10a132015-09-18 11:56:28 -0400244/**
245 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
246 * @cgrp: the cgroup of interest
247 *
248 * The default hierarchy is the v2 interface of cgroup and this function
249 * can be used to test whether a cgroup is on the default hierarchy for
250 * cases where a subsystem should behave differnetly depending on the
251 * interface version.
252 *
253 * The set of behaviors which change on the default hierarchy are still
254 * being determined and the mount option is prefixed with __DEVEL__.
255 *
256 * List of changed behaviors:
257 *
258 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
259 * and "name" are disallowed.
260 *
261 * - When mounting an existing superblock, mount options should match.
262 *
263 * - Remount is disallowed.
264 *
265 * - rename(2) is disallowed.
266 *
267 * - "tasks" is removed. Everything should be at process granularity. Use
268 * "cgroup.procs" instead.
269 *
270 * - "cgroup.procs" is not sorted. pids will be unique unless they got
271 * recycled inbetween reads.
272 *
273 * - "release_agent" and "notify_on_release" are removed. Replacement
274 * notification mechanism will be implemented.
275 *
276 * - "cgroup.clone_children" is removed.
277 *
278 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
279 * and its descendants contain no task; otherwise, 1. The file also
280 * generates kernfs notification which can be monitored through poll and
281 * [di]notify when the value of the file changes.
282 *
283 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
284 * take masks of ancestors with non-empty cpus/mems, instead of being
285 * moved to an ancestor.
286 *
287 * - cpuset: a task can be moved into an empty cpuset, and again it takes
288 * masks of ancestors.
289 *
290 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
291 * is not created.
292 *
293 * - blkcg: blk-throttle becomes properly hierarchical.
294 *
295 * - debug: disallowed on the default hierarchy.
296 */
297static bool cgroup_on_dfl(const struct cgroup *cgrp)
298{
299 return cgrp->root == &cgrp_dfl_root;
300}
301
Tejun Heo6fa49182014-05-04 15:09:13 -0400302/* IDR wrappers which synchronize using cgroup_idr_lock */
303static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
304 gfp_t gfp_mask)
305{
306 int ret;
307
308 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400309 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800310 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400311 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400312 idr_preload_end();
313 return ret;
314}
315
316static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
317{
318 void *ret;
319
Tejun Heo54504e92014-05-13 12:10:59 -0400320 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400321 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400322 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400323 return ret;
324}
325
326static void cgroup_idr_remove(struct idr *idr, int id)
327{
Tejun Heo54504e92014-05-13 12:10:59 -0400328 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400329 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400330 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400331}
332
Tejun Heod51f39b2014-05-16 13:22:48 -0400333static struct cgroup *cgroup_parent(struct cgroup *cgrp)
334{
335 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
336
337 if (parent_css)
338 return container_of(parent_css, struct cgroup, self);
339 return NULL;
340}
341
Tejun Heo95109b62013-08-08 20:11:27 -0400342/**
343 * cgroup_css - obtain a cgroup's css for the specified subsystem
344 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400345 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400346 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400347 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
348 * function must be called either under cgroup_mutex or rcu_read_lock() and
349 * the caller is responsible for pinning the returned css if it wants to
350 * keep accessing it outside the said locks. This function may return
351 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400352 */
353static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400354 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400355{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400356 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500357 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500358 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400359 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400360 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400361}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700362
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400363/**
364 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
365 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400366 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400367 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400368 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400369 * as the matching css of the nearest ancestor including self which has @ss
370 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
371 * function is guaranteed to return non-NULL css.
372 */
373static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
374 struct cgroup_subsys *ss)
375{
376 lockdep_assert_held(&cgroup_mutex);
377
378 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400379 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400380
381 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
382 return NULL;
383
Tejun Heoeeecbd12014-11-18 02:49:52 -0500384 /*
385 * This function is used while updating css associations and thus
386 * can't test the csses directly. Use ->child_subsys_mask.
387 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400388 while (cgroup_parent(cgrp) &&
389 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
390 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400391
392 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700393}
394
Tejun Heoeeecbd12014-11-18 02:49:52 -0500395/**
396 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
397 * @cgrp: the cgroup of interest
398 * @ss: the subsystem of interest
399 *
400 * Find and get the effective css of @cgrp for @ss. The effective css is
401 * defined as the matching css of the nearest ancestor including self which
402 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
403 * the root css is returned, so this function always returns a valid css.
404 * The returned css must be put using css_put().
405 */
406struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
407 struct cgroup_subsys *ss)
408{
409 struct cgroup_subsys_state *css;
410
411 rcu_read_lock();
412
413 do {
414 css = cgroup_css(cgrp, ss);
415
416 if (css && css_tryget_online(css))
417 goto out_unlock;
418 cgrp = cgroup_parent(cgrp);
419 } while (cgrp);
420
421 css = init_css_set.subsys[ss->id];
422 css_get(css);
423out_unlock:
424 rcu_read_unlock();
425 return css;
426}
427
Paul Menageddbcc7e2007-10-18 23:39:30 -0700428/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700429static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700430{
Tejun Heo184faf32014-05-16 13:22:51 -0400431 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700432}
433
Tejun Heo052c3f32015-10-15 16:41:50 -0400434static void cgroup_get(struct cgroup *cgrp)
435{
436 WARN_ON_ONCE(cgroup_is_dead(cgrp));
437 css_get(&cgrp->self);
438}
439
440static bool cgroup_tryget(struct cgroup *cgrp)
441{
442 return css_tryget(&cgrp->self);
443}
444
Tejun Heob4168642014-05-13 12:16:21 -0400445struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500446{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500447 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400448 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500449
450 /*
451 * This is open and unprotected implementation of cgroup_css().
452 * seq_css() is only called from a kernfs file operation which has
453 * an active reference on the file. Because all the subsystem
454 * files are drained before a css is disassociated with a cgroup,
455 * the matching css from the cgroup's subsys table is guaranteed to
456 * be and stay valid until the enclosing operation is complete.
457 */
458 if (cft->ss)
459 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
460 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400461 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500462}
Tejun Heob4168642014-05-13 12:16:21 -0400463EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500464
Adrian Bunke9685a02008-02-07 00:13:46 -0800465static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700466{
Paul Menagebd89aab2007-10-18 23:40:44 -0700467 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700468}
469
Tejun Heo30159ec2013-06-25 11:53:37 -0700470/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500471 * for_each_css - iterate all css's of a cgroup
472 * @css: the iteration cursor
473 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
474 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700475 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400476 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700477 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500478#define for_each_css(css, ssid, cgrp) \
479 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
480 if (!((css) = rcu_dereference_check( \
481 (cgrp)->subsys[(ssid)], \
482 lockdep_is_held(&cgroup_mutex)))) { } \
483 else
484
485/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400486 * for_each_e_css - iterate all effective css's of a cgroup
487 * @css: the iteration cursor
488 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
489 * @cgrp: the target cgroup to iterate css's of
490 *
491 * Should be called under cgroup_[tree_]mutex.
492 */
493#define for_each_e_css(css, ssid, cgrp) \
494 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
495 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
496 ; \
497 else
498
499/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500500 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700501 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500502 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700503 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500504#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500505 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
506 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700507
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000508/**
509 * for_each_subsys_which - filter for_each_subsys with a bitmask
510 * @ss: the iteration cursor
511 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
512 * @ss_maskp: a pointer to the bitmask
513 *
514 * The block will only run for cases where the ssid-th bit (1 << ssid) of
515 * mask is set to 1.
516 */
517#define for_each_subsys_which(ss, ssid, ss_maskp) \
518 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000519 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000520 else \
521 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
522 if (((ss) = cgroup_subsys[ssid]) && false) \
523 break; \
524 else
525
Tejun Heo985ed672014-03-19 10:23:53 -0400526/* iterate across the hierarchies */
527#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700528 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700529
Tejun Heof8f22e52014-04-23 11:13:16 -0400530/* iterate over child cgrps, lock should be held throughout iteration */
531#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400532 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400533 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400534 cgroup_is_dead(child); })) \
535 ; \
536 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700537
Paul Menage81a6a5c2007-10-18 23:39:38 -0700538static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700539static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700540
Tejun Heo69d02062013-06-12 21:04:50 -0700541/*
542 * A cgroup can be associated with multiple css_sets as different tasks may
543 * belong to different cgroups on different hierarchies. In the other
544 * direction, a css_set is naturally associated with multiple cgroups.
545 * This M:N relationship is represented by the following link structure
546 * which exists for each association and allows traversing the associations
547 * from both sides.
548 */
549struct cgrp_cset_link {
550 /* the cgroup and css_set this link associates */
551 struct cgroup *cgrp;
552 struct css_set *cset;
553
554 /* list of cgrp_cset_links anchored at cgrp->cset_links */
555 struct list_head cset_link;
556
557 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
558 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700559};
560
Tejun Heo172a2c062014-03-19 10:23:53 -0400561/*
562 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700563 * hierarchies being mounted. It contains a pointer to the root state
564 * for each subsystem. Also used to anchor the list of css_sets. Not
565 * reference-counted, to improve performance when child cgroups
566 * haven't been created.
567 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400568struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400569 .refcount = ATOMIC_INIT(1),
570 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
571 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
572 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
573 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
574 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400575 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400576};
Paul Menage817929e2007-10-18 23:39:36 -0700577
Tejun Heo172a2c062014-03-19 10:23:53 -0400578static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700579
Tejun Heo842b5972014-04-25 18:28:02 -0400580/**
Tejun Heo0de09422015-10-15 16:41:49 -0400581 * css_set_populated - does a css_set contain any tasks?
582 * @cset: target css_set
583 */
584static bool css_set_populated(struct css_set *cset)
585{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400586 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400587
588 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
589}
590
591/**
Tejun Heo842b5972014-04-25 18:28:02 -0400592 * cgroup_update_populated - updated populated count of a cgroup
593 * @cgrp: the target cgroup
594 * @populated: inc or dec populated count
595 *
Tejun Heo0de09422015-10-15 16:41:49 -0400596 * One of the css_sets associated with @cgrp is either getting its first
597 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
598 * count is propagated towards root so that a given cgroup's populated_cnt
599 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400600 *
601 * @cgrp's interface file "cgroup.populated" is zero if
602 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
603 * changes from or to zero, userland is notified that the content of the
604 * interface file has changed. This can be used to detect when @cgrp and
605 * its descendants become populated or empty.
606 */
607static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
608{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400609 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400610
611 do {
612 bool trigger;
613
614 if (populated)
615 trigger = !cgrp->populated_cnt++;
616 else
617 trigger = !--cgrp->populated_cnt;
618
619 if (!trigger)
620 break;
621
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400622 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400623 cgroup_file_notify(&cgrp->events_file);
624
Tejun Heod51f39b2014-05-16 13:22:48 -0400625 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400626 } while (cgrp);
627}
628
Tejun Heo0de09422015-10-15 16:41:49 -0400629/**
630 * css_set_update_populated - update populated state of a css_set
631 * @cset: target css_set
632 * @populated: whether @cset is populated or depopulated
633 *
634 * @cset is either getting the first task or losing the last. Update the
635 * ->populated_cnt of all associated cgroups accordingly.
636 */
637static void css_set_update_populated(struct css_set *cset, bool populated)
638{
639 struct cgrp_cset_link *link;
640
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400641 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400642
643 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
644 cgroup_update_populated(link->cgrp, populated);
645}
646
Tejun Heof6d7d042015-10-15 16:41:52 -0400647/**
648 * css_set_move_task - move a task from one css_set to another
649 * @task: task being moved
650 * @from_cset: css_set @task currently belongs to (may be NULL)
651 * @to_cset: new css_set @task is being moved to (may be NULL)
652 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
653 *
654 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
655 * css_set, @from_cset can be NULL. If @task is being disassociated
656 * instead of moved, @to_cset can be NULL.
657 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400658 * This function automatically handles populated_cnt updates and
659 * css_task_iter adjustments but the caller is responsible for managing
660 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400661 */
662static void css_set_move_task(struct task_struct *task,
663 struct css_set *from_cset, struct css_set *to_cset,
664 bool use_mg_tasks)
665{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400666 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400667
668 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400669 struct css_task_iter *it, *pos;
670
Tejun Heof6d7d042015-10-15 16:41:52 -0400671 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400672
673 /*
674 * @task is leaving, advance task iterators which are
675 * pointing to it so that they can resume at the next
676 * position. Advancing an iterator might remove it from
677 * the list, use safe walk. See css_task_iter_advance*()
678 * for details.
679 */
680 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
681 iters_node)
682 if (it->task_pos == &task->cg_list)
683 css_task_iter_advance(it);
684
Tejun Heof6d7d042015-10-15 16:41:52 -0400685 list_del_init(&task->cg_list);
686 if (!css_set_populated(from_cset))
687 css_set_update_populated(from_cset, false);
688 } else {
689 WARN_ON_ONCE(!list_empty(&task->cg_list));
690 }
691
692 if (to_cset) {
693 /*
694 * We are synchronized through cgroup_threadgroup_rwsem
695 * against PF_EXITING setting such that we can't race
696 * against cgroup_exit() changing the css_set to
697 * init_css_set and dropping the old one.
698 */
699 WARN_ON_ONCE(task->flags & PF_EXITING);
700
701 if (!css_set_populated(to_cset))
702 css_set_update_populated(to_cset, true);
703 rcu_assign_pointer(task->cgroups, to_cset);
704 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
705 &to_cset->tasks);
706 }
707}
708
Paul Menage7717f7b2009-09-23 15:56:22 -0700709/*
710 * hash table for cgroup groups. This improves the performance to find
711 * an existing css_set. This hash doesn't (currently) take into
712 * account cgroups in empty hierarchies.
713 */
Li Zefan472b1052008-04-29 01:00:11 -0700714#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800715static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700716
Li Zefan0ac801f2013-01-10 11:49:27 +0800717static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700718{
Li Zefan0ac801f2013-01-10 11:49:27 +0800719 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700720 struct cgroup_subsys *ss;
721 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700722
Tejun Heo30159ec2013-06-25 11:53:37 -0700723 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800724 key += (unsigned long)css[i];
725 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700726
Li Zefan0ac801f2013-01-10 11:49:27 +0800727 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700728}
729
Zefan Lia25eb522014-09-19 16:51:00 +0800730static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700731{
Tejun Heo69d02062013-06-12 21:04:50 -0700732 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400733 struct cgroup_subsys *ss;
734 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700735
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400736 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500737
738 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700739 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700740
Tejun Heo53254f92015-11-23 14:55:41 -0500741 /* This css_set is dead. unlink it and release cgroup and css refs */
742 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400743 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500744 css_put(cset->subsys[ssid]);
745 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700746 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700747 css_set_count--;
748
Tejun Heo69d02062013-06-12 21:04:50 -0700749 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700750 list_del(&link->cset_link);
751 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400752 if (cgroup_parent(link->cgrp))
753 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700754 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700755 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700756
Tejun Heo5abb8852013-06-12 21:04:49 -0700757 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700758}
759
Zefan Lia25eb522014-09-19 16:51:00 +0800760static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500761{
762 /*
763 * Ensure that the refcount doesn't hit zero while any readers
764 * can see it. Similar to atomic_dec_and_lock(), but for an
765 * rwlock
766 */
767 if (atomic_add_unless(&cset->refcount, -1, 1))
768 return;
769
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400770 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800771 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400772 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500773}
774
Paul Menage817929e2007-10-18 23:39:36 -0700775/*
776 * refcounted get/put for css_set objects
777 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700778static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700779{
Tejun Heo5abb8852013-06-12 21:04:49 -0700780 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700781}
782
Tejun Heob326f9d2013-06-24 15:21:48 -0700783/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700784 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700785 * @cset: candidate css_set being tested
786 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700787 * @new_cgrp: cgroup that's being entered by the task
788 * @template: desired set of css pointers in css_set (pre-calculated)
789 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800790 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700791 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
792 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700793static bool compare_css_sets(struct css_set *cset,
794 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700795 struct cgroup *new_cgrp,
796 struct cgroup_subsys_state *template[])
797{
798 struct list_head *l1, *l2;
799
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400800 /*
801 * On the default hierarchy, there can be csets which are
802 * associated with the same set of cgroups but different csses.
803 * Let's first ensure that csses match.
804 */
805 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700806 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700807
808 /*
809 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400810 * different cgroups in hierarchies. As different cgroups may
811 * share the same effective css, this comparison is always
812 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700813 */
Tejun Heo69d02062013-06-12 21:04:50 -0700814 l1 = &cset->cgrp_links;
815 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700816 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700817 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700818 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700819
820 l1 = l1->next;
821 l2 = l2->next;
822 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700823 if (l1 == &cset->cgrp_links) {
824 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700825 break;
826 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700827 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700828 }
829 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700830 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
831 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
832 cgrp1 = link1->cgrp;
833 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700834 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700835 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700836
837 /*
838 * If this hierarchy is the hierarchy of the cgroup
839 * that's changing, then we need to check that this
840 * css_set points to the new cgroup; if it's any other
841 * hierarchy, then this css_set should point to the
842 * same cgroup as the old css_set.
843 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700844 if (cgrp1->root == new_cgrp->root) {
845 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700846 return false;
847 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700848 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700849 return false;
850 }
851 }
852 return true;
853}
854
Tejun Heob326f9d2013-06-24 15:21:48 -0700855/**
856 * find_existing_css_set - init css array and find the matching css_set
857 * @old_cset: the css_set that we're using before the cgroup transition
858 * @cgrp: the cgroup that we're moving into
859 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700860 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700861static struct css_set *find_existing_css_set(struct css_set *old_cset,
862 struct cgroup *cgrp,
863 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700864{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400865 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700866 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700867 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800868 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700869 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700870
Ben Blumaae8aab2010-03-10 15:22:07 -0800871 /*
872 * Build the set of subsystem state objects that we want to see in the
873 * new css_set. while subsystems can change globally, the entries here
874 * won't change, so no need for locking.
875 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700876 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400877 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400878 /*
879 * @ss is in this hierarchy, so we want the
880 * effective css from @cgrp.
881 */
882 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700883 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400884 /*
885 * @ss is not in this hierarchy, so we don't want
886 * to change the css.
887 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700888 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700889 }
890 }
891
Li Zefan0ac801f2013-01-10 11:49:27 +0800892 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700893 hash_for_each_possible(css_set_table, cset, hlist, key) {
894 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700895 continue;
896
897 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700898 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700899 }
Paul Menage817929e2007-10-18 23:39:36 -0700900
901 /* No existing cgroup group matched */
902 return NULL;
903}
904
Tejun Heo69d02062013-06-12 21:04:50 -0700905static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700906{
Tejun Heo69d02062013-06-12 21:04:50 -0700907 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700908
Tejun Heo69d02062013-06-12 21:04:50 -0700909 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
910 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700911 kfree(link);
912 }
913}
914
Tejun Heo69d02062013-06-12 21:04:50 -0700915/**
916 * allocate_cgrp_cset_links - allocate cgrp_cset_links
917 * @count: the number of links to allocate
918 * @tmp_links: list_head the allocated links are put on
919 *
920 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
921 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700922 */
Tejun Heo69d02062013-06-12 21:04:50 -0700923static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700924{
Tejun Heo69d02062013-06-12 21:04:50 -0700925 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700926 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700927
928 INIT_LIST_HEAD(tmp_links);
929
Li Zefan36553432008-07-29 22:33:19 -0700930 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700931 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700932 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700933 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700934 return -ENOMEM;
935 }
Tejun Heo69d02062013-06-12 21:04:50 -0700936 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700937 }
938 return 0;
939}
940
Li Zefanc12f65d2009-01-07 18:07:42 -0800941/**
942 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700943 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700944 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800945 * @cgrp: the destination cgroup
946 */
Tejun Heo69d02062013-06-12 21:04:50 -0700947static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
948 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800949{
Tejun Heo69d02062013-06-12 21:04:50 -0700950 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800951
Tejun Heo69d02062013-06-12 21:04:50 -0700952 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400953
954 if (cgroup_on_dfl(cgrp))
955 cset->dfl_cgrp = cgrp;
956
Tejun Heo69d02062013-06-12 21:04:50 -0700957 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
958 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700959 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400960
Paul Menage7717f7b2009-09-23 15:56:22 -0700961 /*
Tejun Heo389b9c12015-10-15 16:41:51 -0400962 * Always add links to the tail of the lists so that the lists are
963 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -0700964 */
Tejun Heo389b9c12015-10-15 16:41:51 -0400965 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -0700966 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400967
968 if (cgroup_parent(cgrp))
969 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -0800970}
971
Tejun Heob326f9d2013-06-24 15:21:48 -0700972/**
973 * find_css_set - return a new css_set with one cgroup updated
974 * @old_cset: the baseline css_set
975 * @cgrp: the cgroup to be updated
976 *
977 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
978 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700979 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700980static struct css_set *find_css_set(struct css_set *old_cset,
981 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700982{
Tejun Heob326f9d2013-06-24 15:21:48 -0700983 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700984 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700985 struct list_head tmp_links;
986 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400987 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800988 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400989 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700990
Tejun Heob326f9d2013-06-24 15:21:48 -0700991 lockdep_assert_held(&cgroup_mutex);
992
Paul Menage817929e2007-10-18 23:39:36 -0700993 /* First see if we already have a cgroup group that matches
994 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400995 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700996 cset = find_existing_css_set(old_cset, cgrp, template);
997 if (cset)
998 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400999 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001000
Tejun Heo5abb8852013-06-12 21:04:49 -07001001 if (cset)
1002 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001003
Tejun Heof4f4be22013-06-12 21:04:51 -07001004 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001005 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001006 return NULL;
1007
Tejun Heo69d02062013-06-12 21:04:50 -07001008 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001009 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001010 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001011 return NULL;
1012 }
1013
Tejun Heo5abb8852013-06-12 21:04:49 -07001014 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001015 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001016 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001017 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001018 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001019 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001020 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001021 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001022
1023 /* Copy the set of subsystem state objects generated in
1024 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001025 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001026
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001027 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001028 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001029 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001030 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001031
Paul Menage7717f7b2009-09-23 15:56:22 -07001032 if (c->root == cgrp->root)
1033 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001034 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001035 }
Paul Menage817929e2007-10-18 23:39:36 -07001036
Tejun Heo69d02062013-06-12 21:04:50 -07001037 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001038
Paul Menage817929e2007-10-18 23:39:36 -07001039 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001040
Tejun Heo2d8f2432014-04-23 11:13:15 -04001041 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001042 key = css_set_hash(cset->subsys);
1043 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001044
Tejun Heo53254f92015-11-23 14:55:41 -05001045 for_each_subsys(ss, ssid) {
1046 struct cgroup_subsys_state *css = cset->subsys[ssid];
1047
Tejun Heo2d8f2432014-04-23 11:13:15 -04001048 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001049 &css->cgroup->e_csets[ssid]);
1050 css_get(css);
1051 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001052
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001053 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001054
Tejun Heo5abb8852013-06-12 21:04:49 -07001055 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001056}
1057
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001058static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001059{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001060 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001061
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001062 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001063}
1064
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001065static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001066{
1067 int id;
1068
1069 lockdep_assert_held(&cgroup_mutex);
1070
Tejun Heo985ed672014-03-19 10:23:53 -04001071 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001072 if (id < 0)
1073 return id;
1074
1075 root->hierarchy_id = id;
1076 return 0;
1077}
1078
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001079static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001080{
1081 lockdep_assert_held(&cgroup_mutex);
1082
1083 if (root->hierarchy_id) {
1084 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1085 root->hierarchy_id = 0;
1086 }
1087}
1088
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001089static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001090{
1091 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001092 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001093 WARN_ON_ONCE(root->hierarchy_id);
1094
1095 idr_destroy(&root->cgroup_idr);
1096 kfree(root);
1097 }
1098}
1099
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001100static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001101{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001102 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001103 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001104
Tejun Heo2bd59d42014-02-11 11:52:49 -05001105 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001106
Tejun Heo776f02f2014-02-12 09:29:50 -05001107 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001108 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001109
Tejun Heof2e85d52014-02-11 11:52:49 -05001110 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001111 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001112
1113 /*
1114 * Release all the links from cset_links to this hierarchy's
1115 * root cgroup
1116 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001117 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001118
1119 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1120 list_del(&link->cset_link);
1121 list_del(&link->cgrp_link);
1122 kfree(link);
1123 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001124
1125 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001126
1127 if (!list_empty(&root->root_list)) {
1128 list_del(&root->root_list);
1129 cgroup_root_count--;
1130 }
1131
1132 cgroup_exit_root_id(root);
1133
1134 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001135
Tejun Heo2bd59d42014-02-11 11:52:49 -05001136 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001137 cgroup_free_root(root);
1138}
1139
Tejun Heoceb6a082014-02-25 10:04:02 -05001140/* look up cgroup associated with given css_set on the specified hierarchy */
1141static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001142 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001143{
Paul Menage7717f7b2009-09-23 15:56:22 -07001144 struct cgroup *res = NULL;
1145
Tejun Heo96d365e2014-02-13 06:58:40 -05001146 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001147 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001148
Tejun Heo5abb8852013-06-12 21:04:49 -07001149 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001150 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001151 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001152 struct cgrp_cset_link *link;
1153
1154 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001155 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001156
Paul Menage7717f7b2009-09-23 15:56:22 -07001157 if (c->root == root) {
1158 res = c;
1159 break;
1160 }
1161 }
1162 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001163
Paul Menage7717f7b2009-09-23 15:56:22 -07001164 BUG_ON(!res);
1165 return res;
1166}
1167
1168/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001169 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001170 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001171 */
1172static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001173 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001174{
1175 /*
1176 * No need to lock the task - since we hold cgroup_mutex the
1177 * task can't change groups, so the only thing that can happen
1178 * is that it exits and its css is set back to init_css_set.
1179 */
1180 return cset_cgroup_from_root(task_css_set(task), root);
1181}
1182
1183/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184 * A task must hold cgroup_mutex to modify cgroups.
1185 *
1186 * Any task can increment and decrement the count field without lock.
1187 * So in general, code holding cgroup_mutex can't rely on the count
1188 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001189 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001190 * means that no tasks are currently attached, therefore there is no
1191 * way a task attached to that cgroup can fork (the other way to
1192 * increment the count). So code holding cgroup_mutex can safely
1193 * assume that if the count is zero, it will stay zero. Similarly, if
1194 * a task holds cgroup_mutex on a cgroup with zero count, it
1195 * knows that the cgroup won't be removed, as cgroup_rmdir()
1196 * needs that mutex.
1197 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001198 * A cgroup can only be deleted if both its 'count' of using tasks
1199 * is zero, and its list of 'children' cgroups is empty. Since all
1200 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001201 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001202 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001203 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001204 *
1205 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001206 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207 */
1208
Tejun Heo2bd59d42014-02-11 11:52:49 -05001209static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001210static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001211
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001212static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1213 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001214{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001215 struct cgroup_subsys *ss = cft->ss;
1216
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001217 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1218 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1219 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001220 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1221 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001222 else
1223 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1224 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001225}
1226
Tejun Heof2e85d52014-02-11 11:52:49 -05001227/**
1228 * cgroup_file_mode - deduce file mode of a control file
1229 * @cft: the control file in question
1230 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001231 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001232 */
1233static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001234{
Tejun Heof2e85d52014-02-11 11:52:49 -05001235 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001236
Tejun Heof2e85d52014-02-11 11:52:49 -05001237 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1238 mode |= S_IRUGO;
1239
Tejun Heo7dbdb192015-09-18 17:54:23 -04001240 if (cft->write_u64 || cft->write_s64 || cft->write) {
1241 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1242 mode |= S_IWUGO;
1243 else
1244 mode |= S_IWUSR;
1245 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001246
1247 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001248}
1249
Tejun Heoa9746d82014-05-13 12:19:22 -04001250/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001251 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001252 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001253 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001254 *
1255 * On the default hierarchy, a subsystem may request other subsystems to be
1256 * enabled together through its ->depends_on mask. In such cases, more
1257 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1258 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001259 * This function calculates which subsystems need to be enabled if
1260 * @subtree_control is to be applied to @cgrp. The returned mask is always
1261 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001262 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001263static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1264 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001265{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001266 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001267 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001268 struct cgroup_subsys *ss;
1269 int ssid;
1270
1271 lockdep_assert_held(&cgroup_mutex);
1272
Tejun Heo0f060de2014-11-18 02:49:50 -05001273 if (!cgroup_on_dfl(cgrp))
1274 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001275
1276 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001277 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001278
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001279 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1280 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001281
1282 /*
1283 * Mask out subsystems which aren't available. This can
1284 * happen only if some depended-upon subsystems were bound
1285 * to non-default hierarchies.
1286 */
1287 if (parent)
1288 new_ss_mask &= parent->child_subsys_mask;
1289 else
1290 new_ss_mask &= cgrp->root->subsys_mask;
1291
1292 if (new_ss_mask == cur_ss_mask)
1293 break;
1294 cur_ss_mask = new_ss_mask;
1295 }
1296
Tejun Heo0f060de2014-11-18 02:49:50 -05001297 return cur_ss_mask;
1298}
1299
1300/**
1301 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1302 * @cgrp: the target cgroup
1303 *
1304 * Update @cgrp->child_subsys_mask according to the current
1305 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1306 */
1307static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1308{
1309 cgrp->child_subsys_mask =
1310 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001311}
1312
Tejun Heoa9746d82014-05-13 12:19:22 -04001313/**
1314 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1315 * @kn: the kernfs_node being serviced
1316 *
1317 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1318 * the method finishes if locking succeeded. Note that once this function
1319 * returns the cgroup returned by cgroup_kn_lock_live() may become
1320 * inaccessible any time. If the caller intends to continue to access the
1321 * cgroup, it should pin it before invoking this function.
1322 */
1323static void cgroup_kn_unlock(struct kernfs_node *kn)
1324{
1325 struct cgroup *cgrp;
1326
1327 if (kernfs_type(kn) == KERNFS_DIR)
1328 cgrp = kn->priv;
1329 else
1330 cgrp = kn->parent->priv;
1331
1332 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001333
1334 kernfs_unbreak_active_protection(kn);
1335 cgroup_put(cgrp);
1336}
1337
1338/**
1339 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1340 * @kn: the kernfs_node being serviced
1341 *
1342 * This helper is to be used by a cgroup kernfs method currently servicing
1343 * @kn. It breaks the active protection, performs cgroup locking and
1344 * verifies that the associated cgroup is alive. Returns the cgroup if
1345 * alive; otherwise, %NULL. A successful return should be undone by a
1346 * matching cgroup_kn_unlock() invocation.
1347 *
1348 * Any cgroup kernfs method implementation which requires locking the
1349 * associated cgroup should use this helper. It avoids nesting cgroup
1350 * locking under kernfs active protection and allows all kernfs operations
1351 * including self-removal.
1352 */
1353static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1354{
1355 struct cgroup *cgrp;
1356
1357 if (kernfs_type(kn) == KERNFS_DIR)
1358 cgrp = kn->priv;
1359 else
1360 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361
Tejun Heo2bd59d42014-02-11 11:52:49 -05001362 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001363 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001364 * active_ref. cgroup liveliness check alone provides enough
1365 * protection against removal. Ensure @cgrp stays accessible and
1366 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001367 */
Li Zefanaa323622014-09-04 14:43:38 +08001368 if (!cgroup_tryget(cgrp))
1369 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001370 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371
Tejun Heoa9746d82014-05-13 12:19:22 -04001372 mutex_lock(&cgroup_mutex);
1373
1374 if (!cgroup_is_dead(cgrp))
1375 return cgrp;
1376
1377 cgroup_kn_unlock(kn);
1378 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379}
1380
Li Zefan2739d3c2013-01-21 18:18:33 +08001381static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001383 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384
Tejun Heo01f64742014-05-13 12:19:23 -04001385 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001386
1387 if (cft->file_offset) {
1388 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1389 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1390
1391 spin_lock_irq(&cgroup_file_kn_lock);
1392 cfile->kn = NULL;
1393 spin_unlock_irq(&cgroup_file_kn_lock);
1394 }
1395
Tejun Heo2bd59d42014-02-11 11:52:49 -05001396 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001397}
1398
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001399/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001400 * css_clear_dir - remove subsys files in a cgroup directory
1401 * @css: taget css
1402 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001403 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001404static void css_clear_dir(struct cgroup_subsys_state *css,
1405 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001406{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001407 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1408 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001409
Tejun Heo4df8dc92015-09-18 17:54:23 -04001410 list_for_each_entry(cfts, &css->ss->cfts, node)
1411 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412}
1413
Tejun Heoccdca212015-09-18 17:54:23 -04001414/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001415 * css_populate_dir - create subsys files in a cgroup directory
1416 * @css: target css
1417 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001418 *
1419 * On failure, no file is added.
1420 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001421static int css_populate_dir(struct cgroup_subsys_state *css,
1422 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001423{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001424 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1425 struct cftype *cfts, *failed_cfts;
1426 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001427
Tejun Heo4df8dc92015-09-18 17:54:23 -04001428 if (!css->ss) {
1429 if (cgroup_on_dfl(cgrp))
1430 cfts = cgroup_dfl_base_files;
1431 else
1432 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001433
Tejun Heo4df8dc92015-09-18 17:54:23 -04001434 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1435 }
Tejun Heoccdca212015-09-18 17:54:23 -04001436
Tejun Heo4df8dc92015-09-18 17:54:23 -04001437 list_for_each_entry(cfts, &css->ss->cfts, node) {
1438 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1439 if (ret < 0) {
1440 failed_cfts = cfts;
1441 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001442 }
1443 }
1444 return 0;
1445err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001446 list_for_each_entry(cfts, &css->ss->cfts, node) {
1447 if (cfts == failed_cfts)
1448 break;
1449 cgroup_addrm_files(css, cgrp, cfts, false);
1450 }
Tejun Heoccdca212015-09-18 17:54:23 -04001451 return ret;
1452}
1453
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001454static int rebind_subsystems(struct cgroup_root *dst_root,
1455 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456{
Tejun Heo1ada4832015-09-18 17:54:23 -04001457 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001458 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001459 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001460 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461
Tejun Heoace2bee2014-02-11 11:52:47 -05001462 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001463
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001464 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001465 /* if @ss has non-root csses attached to it, can't move */
1466 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001467 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001468
Tejun Heo5df36032014-03-19 10:23:54 -04001469 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001470 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001471 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 }
1473
Tejun Heo5533e012014-05-14 19:33:07 -04001474 /* skip creating root files on dfl_root for inhibited subsystems */
1475 tmp_ss_mask = ss_mask;
1476 if (dst_root == &cgrp_dfl_root)
1477 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1478
Tejun Heo4df8dc92015-09-18 17:54:23 -04001479 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
1480 struct cgroup *scgrp = &ss->root->cgrp;
1481 int tssid;
1482
1483 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1484 if (!ret)
1485 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486
Tejun Heoa2dd4242014-03-19 10:23:55 -04001487 /*
1488 * Rebinding back to the default root is not allowed to
1489 * fail. Using both default and non-default roots should
1490 * be rare. Moving subsystems back and forth even more so.
1491 * Just warn about it and continue.
1492 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001493 if (dst_root == &cgrp_dfl_root) {
1494 if (cgrp_dfl_root_visible) {
1495 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
1496 ret, ss_mask);
1497 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1498 }
1499 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001500 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001501
1502 for_each_subsys_which(ss, tssid, &tmp_ss_mask) {
1503 if (tssid == ssid)
1504 break;
1505 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
1506 }
1507 return ret;
Tejun Heo5df36032014-03-19 10:23:54 -04001508 }
Tejun Heo31261212013-06-28 17:07:30 -07001509
1510 /*
1511 * Nothing can fail from this point on. Remove files for the
1512 * removed subsystems and rebind each subsystem.
1513 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001514 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001515 struct cgroup_root *src_root = ss->root;
1516 struct cgroup *scgrp = &src_root->cgrp;
1517 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001518 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001519
Tejun Heo1ada4832015-09-18 17:54:23 -04001520 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001521
Tejun Heo4df8dc92015-09-18 17:54:23 -04001522 css_clear_dir(css, NULL);
1523
Tejun Heo1ada4832015-09-18 17:54:23 -04001524 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1525 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001526 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001527 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001528
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001529 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001530 hash_for_each(css_set_table, i, cset, hlist)
1531 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001532 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001533 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001534
Tejun Heof392e512014-04-23 11:13:14 -04001535 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001536 scgrp->subtree_control &= ~(1 << ssid);
1537 cgroup_refresh_child_subsys_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001538
Tejun Heobd53d612014-04-23 11:13:16 -04001539 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001540 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001541 if (dst_root == &cgrp_dfl_root) {
1542 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1543 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001544 dcgrp->subtree_control |= 1 << ssid;
1545 cgroup_refresh_child_subsys_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001546 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001547 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001548
Tejun Heo5df36032014-03-19 10:23:54 -04001549 if (ss->bind)
1550 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
Tejun Heo1ada4832015-09-18 17:54:23 -04001553 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554 return 0;
1555}
1556
Tejun Heo2bd59d42014-02-11 11:52:49 -05001557static int cgroup_show_options(struct seq_file *seq,
1558 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001560 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001562 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563
Tejun Heod98817d2015-08-18 13:58:16 -07001564 if (root != &cgrp_dfl_root)
1565 for_each_subsys(ss, ssid)
1566 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001567 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001568 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001570 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001571 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001572
1573 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001574 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001575 seq_show_option(seq, "release_agent",
1576 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001577 spin_unlock(&release_agent_path_lock);
1578
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001579 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001580 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001582 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 return 0;
1584}
1585
1586struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001587 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001588 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001589 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001590 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001592 /* User explicitly requested empty subsystem */
1593 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594};
1595
Ben Blumcf5d5942010-03-10 15:22:09 -08001596static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001598 char *token, *o = data;
1599 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001600 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001601 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001602 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001603 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001604
1605#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001606 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001607#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610
1611 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001612 nr_opts++;
1613
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614 if (!*token)
1615 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001616 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001617 /* Explicitly have no subsystems */
1618 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001619 continue;
1620 }
1621 if (!strcmp(token, "all")) {
1622 /* Mutually exclusive option 'all' + subsystem name */
1623 if (one_ss)
1624 return -EINVAL;
1625 all_ss = true;
1626 continue;
1627 }
1628 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001629 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001630 continue;
1631 }
1632 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001633 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001634 continue;
1635 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001636 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001637 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001638 continue;
1639 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001640 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001641 /* Specifying two release agents is forbidden */
1642 if (opts->release_agent)
1643 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001644 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001645 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001646 if (!opts->release_agent)
1647 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001648 continue;
1649 }
1650 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001651 const char *name = token + 5;
1652 /* Can't specify an empty name */
1653 if (!strlen(name))
1654 return -EINVAL;
1655 /* Must match [\w.-]+ */
1656 for (i = 0; i < strlen(name); i++) {
1657 char c = name[i];
1658 if (isalnum(c))
1659 continue;
1660 if ((c == '.') || (c == '-') || (c == '_'))
1661 continue;
1662 return -EINVAL;
1663 }
1664 /* Specifying two names is forbidden */
1665 if (opts->name)
1666 return -EINVAL;
1667 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001668 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001669 GFP_KERNEL);
1670 if (!opts->name)
1671 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001672
1673 continue;
1674 }
1675
Tejun Heo30159ec2013-06-25 11:53:37 -07001676 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001677 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001678 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001679 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001680 continue;
1681
1682 /* Mutually exclusive option 'all' + subsystem name */
1683 if (all_ss)
1684 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001685 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001686 one_ss = true;
1687
1688 break;
1689 }
1690 if (i == CGROUP_SUBSYS_COUNT)
1691 return -ENOENT;
1692 }
1693
Li Zefanf9ab5b52009-06-17 16:26:33 -07001694 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001695 * If the 'all' option was specified select all the subsystems,
1696 * otherwise if 'none', 'name=' and a subsystem name options were
1697 * not specified, let's default to 'all'
1698 */
1699 if (all_ss || (!one_ss && !opts->none && !opts->name))
1700 for_each_subsys(ss, i)
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001701 if (cgroup_ssid_enabled(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001702 opts->subsys_mask |= (1 << i);
1703
1704 /*
1705 * We either have to specify by name or by subsystems. (So all
1706 * empty hierarchies must have a name).
1707 */
1708 if (!opts->subsys_mask && !opts->name)
1709 return -EINVAL;
1710
1711 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001712 * Option noprefix was introduced just for backward compatibility
1713 * with the old cpuset, so we allow noprefix only if mounting just
1714 * the cpuset subsystem.
1715 */
Tejun Heo93438622013-04-14 20:15:25 -07001716 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001717 return -EINVAL;
1718
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001719 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001720 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001721 return -EINVAL;
1722
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 return 0;
1724}
1725
Tejun Heo2bd59d42014-02-11 11:52:49 -05001726static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727{
1728 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001729 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001731 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
Tejun Heoaa6ec292014-07-09 10:08:08 -04001733 if (root == &cgrp_dfl_root) {
1734 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001735 return -EINVAL;
1736 }
1737
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 mutex_lock(&cgroup_mutex);
1739
1740 /* See what subsystems are wanted */
1741 ret = parse_cgroupfs_options(data, &opts);
1742 if (ret)
1743 goto out_unlock;
1744
Tejun Heof392e512014-04-23 11:13:14 -04001745 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001746 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001747 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001748
Tejun Heof392e512014-04-23 11:13:14 -04001749 added_mask = opts.subsys_mask & ~root->subsys_mask;
1750 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001751
Ben Blumcf5d5942010-03-10 15:22:09 -08001752 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001753 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001754 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001755 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001756 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001758 goto out_unlock;
1759 }
1760
Tejun Heof172e672013-06-28 17:07:30 -07001761 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001762 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001763 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001764 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001765 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766
Tejun Heo5df36032014-03-19 10:23:54 -04001767 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001768 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001771 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001772
Tejun Heo69e943b2014-02-08 10:36:58 -05001773 if (opts.release_agent) {
1774 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001775 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001776 spin_unlock(&release_agent_path_lock);
1777 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001779 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001780 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 return ret;
1783}
1784
Tejun Heoafeb0f92014-02-13 06:58:39 -05001785/*
1786 * To reduce the fork() overhead for systems that are not actually using
1787 * their cgroups capability, we don't maintain the lists running through
1788 * each css_set to its tasks until we see the list actually used - in other
1789 * words after the first mount.
1790 */
1791static bool use_task_css_set_links __read_mostly;
1792
1793static void cgroup_enable_task_cg_lists(void)
1794{
1795 struct task_struct *p, *g;
1796
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001797 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001798
1799 if (use_task_css_set_links)
1800 goto out_unlock;
1801
1802 use_task_css_set_links = true;
1803
1804 /*
1805 * We need tasklist_lock because RCU is not safe against
1806 * while_each_thread(). Besides, a forking task that has passed
1807 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1808 * is not guaranteed to have its child immediately visible in the
1809 * tasklist if we walk through it with RCU.
1810 */
1811 read_lock(&tasklist_lock);
1812 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001813 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1814 task_css_set(p) != &init_css_set);
1815
1816 /*
1817 * We should check if the process is exiting, otherwise
1818 * it will race with cgroup_exit() in that the list
1819 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001820 * Do it while holding siglock so that we don't end up
1821 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001822 */
Tejun Heof153ad12014-02-25 09:56:49 -05001823 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001824 if (!(p->flags & PF_EXITING)) {
1825 struct css_set *cset = task_css_set(p);
1826
Tejun Heo0de09422015-10-15 16:41:49 -04001827 if (!css_set_populated(cset))
1828 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001829 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001830 get_css_set(cset);
1831 }
Tejun Heof153ad12014-02-25 09:56:49 -05001832 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001833 } while_each_thread(g, p);
1834 read_unlock(&tasklist_lock);
1835out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001836 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001837}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001838
Paul Menagecc31edc2008-10-18 20:28:04 -07001839static void init_cgroup_housekeeping(struct cgroup *cgrp)
1840{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001841 struct cgroup_subsys *ss;
1842 int ssid;
1843
Tejun Heod5c419b2014-05-16 13:22:48 -04001844 INIT_LIST_HEAD(&cgrp->self.sibling);
1845 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001846 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001847 INIT_LIST_HEAD(&cgrp->pidlists);
1848 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001849 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001850 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001851
1852 for_each_subsys(ss, ssid)
1853 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001854
1855 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001856 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001857}
Paul Menagec6d57f32009-09-23 15:56:19 -07001858
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001859static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001860 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001861{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001862 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001863
Paul Menageddbcc7e2007-10-18 23:39:30 -07001864 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001865 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001866 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001867 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001868 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001869
Paul Menagec6d57f32009-09-23 15:56:19 -07001870 root->flags = opts->flags;
1871 if (opts->release_agent)
1872 strcpy(root->release_agent_path, opts->release_agent);
1873 if (opts->name)
1874 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001875 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001876 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001877}
1878
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001879static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001880{
Tejun Heod427dfe2014-02-11 11:52:48 -05001881 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001882 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001883 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001884 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001885
Tejun Heod427dfe2014-02-11 11:52:48 -05001886 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001887
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001888 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001889 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001890 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001891 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001892 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001893
Tejun Heo2aad2a82014-09-24 13:31:50 -04001894 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1895 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001896 if (ret)
1897 goto out;
1898
Tejun Heod427dfe2014-02-11 11:52:48 -05001899 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001900 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001901 * but that's OK - it can only be increased by someone holding
1902 * cgroup_lock, and that's us. The worst that can happen is that we
1903 * have some link structures left over
1904 */
1905 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001906 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001907 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001908
Tejun Heo985ed672014-03-19 10:23:53 -04001909 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001910 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001911 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001912
Tejun Heo2bd59d42014-02-11 11:52:49 -05001913 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1914 KERNFS_ROOT_CREATE_DEACTIVATED,
1915 root_cgrp);
1916 if (IS_ERR(root->kf_root)) {
1917 ret = PTR_ERR(root->kf_root);
1918 goto exit_root_id;
1919 }
1920 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001921
Tejun Heo4df8dc92015-09-18 17:54:23 -04001922 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001923 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001924 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001925
Tejun Heo5df36032014-03-19 10:23:54 -04001926 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001927 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001928 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001929
Tejun Heod427dfe2014-02-11 11:52:48 -05001930 /*
1931 * There must be no failure case after here, since rebinding takes
1932 * care of subsystems' refcounts, which are explicitly dropped in
1933 * the failure exit path.
1934 */
1935 list_add(&root->root_list, &cgroup_roots);
1936 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001937
Tejun Heod427dfe2014-02-11 11:52:48 -05001938 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001939 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001940 * objects.
1941 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001942 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001943 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001944 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001945 if (css_set_populated(cset))
1946 cgroup_update_populated(root_cgrp, true);
1947 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001948 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001949
Tejun Heod5c419b2014-05-16 13:22:48 -04001950 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001951 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001952
Tejun Heo2bd59d42014-02-11 11:52:49 -05001953 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001954 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001955 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001956
Tejun Heo2bd59d42014-02-11 11:52:49 -05001957destroy_root:
1958 kernfs_destroy_root(root->kf_root);
1959 root->kf_root = NULL;
1960exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001961 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001962cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001963 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001964out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001965 free_cgrp_cset_links(&tmp_links);
1966 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001967}
1968
Al Virof7e83572010-07-26 13:23:11 +04001969static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001970 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001971 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001972{
Tejun Heo67e9c742015-11-16 11:13:34 -05001973 bool is_v2 = fs_type == &cgroup2_fs_type;
Li Zefan3a32bd72014-06-30 11:50:59 +08001974 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001975 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001976 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001977 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001978 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001979 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001980 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001981 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001982
1983 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001984 * The first time anyone tries to mount a cgroup, enable the list
1985 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001986 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001987 if (!use_task_css_set_links)
1988 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001989
Tejun Heo67e9c742015-11-16 11:13:34 -05001990 if (is_v2) {
1991 if (data) {
1992 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
1993 return ERR_PTR(-EINVAL);
1994 }
1995 cgrp_dfl_root_visible = true;
1996 root = &cgrp_dfl_root;
1997 cgroup_get(&root->cgrp);
1998 goto out_mount;
1999 }
2000
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002001 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002002
Paul Menageddbcc7e2007-10-18 23:39:30 -07002003 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002004 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002005 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002006 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002007
Li Zefan970317a2014-06-30 11:49:58 +08002008 /*
2009 * Destruction of cgroup root is asynchronous, so subsystems may
2010 * still be dying after the previous unmount. Let's drain the
2011 * dying subsystems. We just need to ensure that the ones
2012 * unmounted previously finish dying and don't care about new ones
2013 * starting. Testing ref liveliness is good enough.
2014 */
2015 for_each_subsys(ss, i) {
2016 if (!(opts.subsys_mask & (1 << i)) ||
2017 ss->root == &cgrp_dfl_root)
2018 continue;
2019
2020 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2021 mutex_unlock(&cgroup_mutex);
2022 msleep(10);
2023 ret = restart_syscall();
2024 goto out_free;
2025 }
2026 cgroup_put(&ss->root->cgrp);
2027 }
2028
Tejun Heo985ed672014-03-19 10:23:53 -04002029 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002030 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002031
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002032 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002033 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002034
Paul Menage817929e2007-10-18 23:39:36 -07002035 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002036 * If we asked for a name then it must match. Also, if
2037 * name matches but sybsys_mask doesn't, we should fail.
2038 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002039 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002040 if (opts.name) {
2041 if (strcmp(opts.name, root->name))
2042 continue;
2043 name_match = true;
2044 }
Tejun Heo31261212013-06-28 17:07:30 -07002045
2046 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002047 * If we asked for subsystems (or explicitly for no
2048 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002049 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002050 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002051 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002052 if (!name_match)
2053 continue;
2054 ret = -EBUSY;
2055 goto out_unlock;
2056 }
Tejun Heo873fe092013-04-14 20:15:26 -07002057
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002058 if (root->flags ^ opts.flags)
2059 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002060
Tejun Heo776f02f2014-02-12 09:29:50 -05002061 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002062 * We want to reuse @root whose lifetime is governed by its
2063 * ->cgrp. Let's check whether @root is alive and keep it
2064 * that way. As cgroup_kill_sb() can happen anytime, we
2065 * want to block it by pinning the sb so that @root doesn't
2066 * get killed before mount is complete.
2067 *
2068 * With the sb pinned, tryget_live can reliably indicate
2069 * whether @root can be reused. If it's being killed,
2070 * drain it. We can use wait_queue for the wait but this
2071 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002072 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002073 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2074 if (IS_ERR(pinned_sb) ||
2075 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002076 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002077 if (!IS_ERR_OR_NULL(pinned_sb))
2078 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002079 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002080 ret = restart_syscall();
2081 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002082 }
2083
2084 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002085 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002086 }
2087
Tejun Heo172a2c062014-03-19 10:23:53 -04002088 /*
2089 * No such thing, create a new one. name= matching without subsys
2090 * specification is allowed for already existing hierarchies but we
2091 * can't create new one without subsys specification.
2092 */
2093 if (!opts.subsys_mask && !opts.none) {
2094 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002095 goto out_unlock;
2096 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002097
Tejun Heo172a2c062014-03-19 10:23:53 -04002098 root = kzalloc(sizeof(*root), GFP_KERNEL);
2099 if (!root) {
2100 ret = -ENOMEM;
2101 goto out_unlock;
2102 }
2103
2104 init_cgroup_root(root, &opts);
2105
Tejun Heo35585572014-02-13 06:58:38 -05002106 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002107 if (ret)
2108 cgroup_free_root(root);
2109
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002110out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002111 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002112out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002113 kfree(opts.release_agent);
2114 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002115
Tejun Heo2bd59d42014-02-11 11:52:49 -05002116 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002117 return ERR_PTR(ret);
Tejun Heo67e9c742015-11-16 11:13:34 -05002118out_mount:
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002119 dentry = kernfs_mount(fs_type, flags, root->kf_root,
Tejun Heo67e9c742015-11-16 11:13:34 -05002120 is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
2121 &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002122 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002123 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002124
2125 /*
2126 * If @pinned_sb, we're reusing an existing root and holding an
2127 * extra ref on its sb. Mount is complete. Put the extra ref.
2128 */
2129 if (pinned_sb) {
2130 WARN_ON(new_sb);
2131 deactivate_super(pinned_sb);
2132 }
2133
Tejun Heo2bd59d42014-02-11 11:52:49 -05002134 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002135}
2136
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002137static void cgroup_kill_sb(struct super_block *sb)
2138{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002139 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002140 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002141
Tejun Heo9d755d32014-05-14 09:15:02 -04002142 /*
2143 * If @root doesn't have any mounts or children, start killing it.
2144 * This prevents new mounts by disabling percpu_ref_tryget_live().
2145 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002146 *
2147 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002148 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002149 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002150 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002151 cgroup_put(&root->cgrp);
2152 else
2153 percpu_ref_kill(&root->cgrp.self.refcnt);
2154
Tejun Heo2bd59d42014-02-11 11:52:49 -05002155 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002156}
2157
2158static struct file_system_type cgroup_fs_type = {
2159 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002160 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002161 .kill_sb = cgroup_kill_sb,
2162};
2163
Tejun Heo67e9c742015-11-16 11:13:34 -05002164static struct file_system_type cgroup2_fs_type = {
2165 .name = "cgroup2",
2166 .mount = cgroup_mount,
2167 .kill_sb = cgroup_kill_sb,
2168};
2169
Li Zefana043e3b2008-02-23 15:24:09 -08002170/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002171 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002172 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002173 * @buf: the buffer to write the path into
2174 * @buflen: the length of the buffer
2175 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002176 * Determine @task's cgroup on the first (the one with the lowest non-zero
2177 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2178 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2179 * cgroup controller callbacks.
2180 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002181 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002182 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002183char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002184{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002185 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002186 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002187 int hierarchy_id = 1;
2188 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002189
2190 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002191 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002192
Tejun Heo913ffdb2013-07-11 16:34:48 -07002193 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2194
Tejun Heo857a2be2013-04-14 20:50:08 -07002195 if (root) {
2196 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002197 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002198 } else {
2199 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002200 if (strlcpy(buf, "/", buflen) < buflen)
2201 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002202 }
2203
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002204 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002205 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002206 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002207}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002208EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002209
Tejun Heob3dc0942014-02-25 10:04:01 -05002210/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002211struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002212 /* the src and dst cset list running through cset->mg_node */
2213 struct list_head src_csets;
2214 struct list_head dst_csets;
2215
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002216 /* the subsys currently being processed */
2217 int ssid;
2218
Tejun Heob3dc0942014-02-25 10:04:01 -05002219 /*
2220 * Fields for cgroup_taskset_*() iteration.
2221 *
2222 * Before migration is committed, the target migration tasks are on
2223 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2224 * the csets on ->dst_csets. ->csets point to either ->src_csets
2225 * or ->dst_csets depending on whether migration is committed.
2226 *
2227 * ->cur_csets and ->cur_task point to the current task position
2228 * during iteration.
2229 */
2230 struct list_head *csets;
2231 struct css_set *cur_cset;
2232 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002233};
2234
Tejun Heoadaae5d2015-09-11 15:00:21 -04002235#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2236 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2237 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2238 .csets = &tset.src_csets, \
2239}
2240
2241/**
2242 * cgroup_taskset_add - try to add a migration target task to a taskset
2243 * @task: target task
2244 * @tset: target taskset
2245 *
2246 * Add @task, which is a migration target, to @tset. This function becomes
2247 * noop if @task doesn't need to be migrated. @task's css_set should have
2248 * been added as a migration source and @task->cg_list will be moved from
2249 * the css_set's tasks list to mg_tasks one.
2250 */
2251static void cgroup_taskset_add(struct task_struct *task,
2252 struct cgroup_taskset *tset)
2253{
2254 struct css_set *cset;
2255
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002256 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002257
2258 /* @task either already exited or can't exit until the end */
2259 if (task->flags & PF_EXITING)
2260 return;
2261
2262 /* leave @task alone if post_fork() hasn't linked it yet */
2263 if (list_empty(&task->cg_list))
2264 return;
2265
2266 cset = task_css_set(task);
2267 if (!cset->mg_src_cgrp)
2268 return;
2269
2270 list_move_tail(&task->cg_list, &cset->mg_tasks);
2271 if (list_empty(&cset->mg_node))
2272 list_add_tail(&cset->mg_node, &tset->src_csets);
2273 if (list_empty(&cset->mg_dst_cset->mg_node))
2274 list_move_tail(&cset->mg_dst_cset->mg_node,
2275 &tset->dst_csets);
2276}
2277
Tejun Heo2f7ee562011-12-12 18:12:21 -08002278/**
2279 * cgroup_taskset_first - reset taskset and return the first task
2280 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002281 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002282 *
2283 * @tset iteration is initialized and the first task is returned.
2284 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002285struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2286 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002287{
Tejun Heob3dc0942014-02-25 10:04:01 -05002288 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2289 tset->cur_task = NULL;
2290
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002291 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002292}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002293
2294/**
2295 * cgroup_taskset_next - iterate to the next task in taskset
2296 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002297 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002298 *
2299 * Return the next task in @tset. Iteration must have been initialized
2300 * with cgroup_taskset_first().
2301 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002302struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2303 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002304{
Tejun Heob3dc0942014-02-25 10:04:01 -05002305 struct css_set *cset = tset->cur_cset;
2306 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002307
Tejun Heob3dc0942014-02-25 10:04:01 -05002308 while (&cset->mg_node != tset->csets) {
2309 if (!task)
2310 task = list_first_entry(&cset->mg_tasks,
2311 struct task_struct, cg_list);
2312 else
2313 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002314
Tejun Heob3dc0942014-02-25 10:04:01 -05002315 if (&task->cg_list != &cset->mg_tasks) {
2316 tset->cur_cset = cset;
2317 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002318
2319 /*
2320 * This function may be called both before and
2321 * after cgroup_taskset_migrate(). The two cases
2322 * can be distinguished by looking at whether @cset
2323 * has its ->mg_dst_cset set.
2324 */
2325 if (cset->mg_dst_cset)
2326 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2327 else
2328 *dst_cssp = cset->subsys[tset->ssid];
2329
Tejun Heob3dc0942014-02-25 10:04:01 -05002330 return task;
2331 }
2332
2333 cset = list_next_entry(cset, mg_node);
2334 task = NULL;
2335 }
2336
2337 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002338}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002339
2340/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002341 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2342 * @tset: taget taskset
2343 * @dst_cgrp: destination cgroup
2344 *
2345 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2346 * ->can_attach callbacks fails and guarantees that either all or none of
2347 * the tasks in @tset are migrated. @tset is consumed regardless of
2348 * success.
2349 */
2350static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2351 struct cgroup *dst_cgrp)
2352{
2353 struct cgroup_subsys_state *css, *failed_css = NULL;
2354 struct task_struct *task, *tmp_task;
2355 struct css_set *cset, *tmp_cset;
2356 int i, ret;
2357
2358 /* methods shouldn't be called if no task is actually migrating */
2359 if (list_empty(&tset->src_csets))
2360 return 0;
2361
2362 /* check that we can legitimately attach to the cgroup */
2363 for_each_e_css(css, i, dst_cgrp) {
2364 if (css->ss->can_attach) {
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002365 tset->ssid = i;
2366 ret = css->ss->can_attach(tset);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002367 if (ret) {
2368 failed_css = css;
2369 goto out_cancel_attach;
2370 }
2371 }
2372 }
2373
2374 /*
2375 * Now that we're guaranteed success, proceed to move all tasks to
2376 * the new cgroup. There are no failure cases after here, so this
2377 * is the commit point.
2378 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002379 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002380 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002381 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2382 struct css_set *from_cset = task_css_set(task);
2383 struct css_set *to_cset = cset->mg_dst_cset;
2384
2385 get_css_set(to_cset);
2386 css_set_move_task(task, from_cset, to_cset, true);
2387 put_css_set_locked(from_cset);
2388 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002389 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002390 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002391
2392 /*
2393 * Migration is committed, all target tasks are now on dst_csets.
2394 * Nothing is sensitive to fork() after this point. Notify
2395 * controllers that migration is complete.
2396 */
2397 tset->csets = &tset->dst_csets;
2398
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002399 for_each_e_css(css, i, dst_cgrp) {
2400 if (css->ss->attach) {
2401 tset->ssid = i;
2402 css->ss->attach(tset);
2403 }
2404 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002405
2406 ret = 0;
2407 goto out_release_tset;
2408
2409out_cancel_attach:
2410 for_each_e_css(css, i, dst_cgrp) {
2411 if (css == failed_css)
2412 break;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002413 if (css->ss->cancel_attach) {
2414 tset->ssid = i;
2415 css->ss->cancel_attach(tset);
2416 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002417 }
2418out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002419 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002420 list_splice_init(&tset->dst_csets, &tset->src_csets);
2421 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2422 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2423 list_del_init(&cset->mg_node);
2424 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002425 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002426 return ret;
2427}
2428
2429/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002430 * cgroup_migrate_finish - cleanup after attach
2431 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002432 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002433 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2434 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002435 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002436static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002437{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002438 struct css_set *cset, *tmp_cset;
2439
2440 lockdep_assert_held(&cgroup_mutex);
2441
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002442 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002443 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2444 cset->mg_src_cgrp = NULL;
2445 cset->mg_dst_cset = NULL;
2446 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002447 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002448 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002449 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002450}
2451
2452/**
2453 * cgroup_migrate_add_src - add a migration source css_set
2454 * @src_cset: the source css_set to add
2455 * @dst_cgrp: the destination cgroup
2456 * @preloaded_csets: list of preloaded css_sets
2457 *
2458 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2459 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2460 * up by cgroup_migrate_finish().
2461 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002462 * This function may be called without holding cgroup_threadgroup_rwsem
2463 * even if the target is a process. Threads may be created and destroyed
2464 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2465 * into play and the preloaded css_sets are guaranteed to cover all
2466 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002467 */
2468static void cgroup_migrate_add_src(struct css_set *src_cset,
2469 struct cgroup *dst_cgrp,
2470 struct list_head *preloaded_csets)
2471{
2472 struct cgroup *src_cgrp;
2473
2474 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002475 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002476
2477 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2478
Tejun Heo1958d2d2014-02-25 10:04:03 -05002479 if (!list_empty(&src_cset->mg_preload_node))
2480 return;
2481
2482 WARN_ON(src_cset->mg_src_cgrp);
2483 WARN_ON(!list_empty(&src_cset->mg_tasks));
2484 WARN_ON(!list_empty(&src_cset->mg_node));
2485
2486 src_cset->mg_src_cgrp = src_cgrp;
2487 get_css_set(src_cset);
2488 list_add(&src_cset->mg_preload_node, preloaded_csets);
2489}
2490
2491/**
2492 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002493 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002494 * @preloaded_csets: list of preloaded source css_sets
2495 *
2496 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2497 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002498 * pins all destination css_sets, links each to its source, and append them
2499 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2500 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002501 *
2502 * This function must be called after cgroup_migrate_add_src() has been
2503 * called on each migration source css_set. After migration is performed
2504 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2505 * @preloaded_csets.
2506 */
2507static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2508 struct list_head *preloaded_csets)
2509{
2510 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002511 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002512
2513 lockdep_assert_held(&cgroup_mutex);
2514
Tejun Heof8f22e52014-04-23 11:13:16 -04002515 /*
2516 * Except for the root, child_subsys_mask must be zero for a cgroup
2517 * with tasks so that child cgroups don't compete against tasks.
2518 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002519 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002520 dst_cgrp->child_subsys_mask)
2521 return -EBUSY;
2522
Tejun Heo1958d2d2014-02-25 10:04:03 -05002523 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002524 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002525 struct css_set *dst_cset;
2526
Tejun Heof817de92014-04-23 11:13:16 -04002527 dst_cset = find_css_set(src_cset,
2528 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002529 if (!dst_cset)
2530 goto err;
2531
2532 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002533
2534 /*
2535 * If src cset equals dst, it's noop. Drop the src.
2536 * cgroup_migrate() will skip the cset too. Note that we
2537 * can't handle src == dst as some nodes are used by both.
2538 */
2539 if (src_cset == dst_cset) {
2540 src_cset->mg_src_cgrp = NULL;
2541 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002542 put_css_set(src_cset);
2543 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002544 continue;
2545 }
2546
Tejun Heo1958d2d2014-02-25 10:04:03 -05002547 src_cset->mg_dst_cset = dst_cset;
2548
2549 if (list_empty(&dst_cset->mg_preload_node))
2550 list_add(&dst_cset->mg_preload_node, &csets);
2551 else
Zefan Lia25eb522014-09-19 16:51:00 +08002552 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002553 }
2554
Tejun Heof817de92014-04-23 11:13:16 -04002555 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002556 return 0;
2557err:
2558 cgroup_migrate_finish(&csets);
2559 return -ENOMEM;
2560}
2561
2562/**
2563 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002564 * @leader: the leader of the process or the task to migrate
2565 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002566 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002567 *
2568 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002569 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002570 * caller is also responsible for invoking cgroup_migrate_add_src() and
2571 * cgroup_migrate_prepare_dst() on the targets before invoking this
2572 * function and following up with cgroup_migrate_finish().
2573 *
2574 * As long as a controller's ->can_attach() doesn't fail, this function is
2575 * guaranteed to succeed. This means that, excluding ->can_attach()
2576 * failure, when migrating multiple targets, the success or failure can be
2577 * decided for all targets by invoking group_migrate_prepare_dst() before
2578 * actually starting migrating.
2579 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002580static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2581 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002582{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002583 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2584 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002585
2586 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002587 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2588 * already PF_EXITING could be freed from underneath us unless we
2589 * take an rcu_read_lock.
2590 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002591 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002592 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002593 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002594 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002595 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002596 if (!threadgroup)
2597 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002598 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002599 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002600 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002601
Tejun Heoadaae5d2015-09-11 15:00:21 -04002602 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002603}
2604
Tejun Heo1958d2d2014-02-25 10:04:03 -05002605/**
2606 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2607 * @dst_cgrp: the cgroup to attach to
2608 * @leader: the task or the leader of the threadgroup to be attached
2609 * @threadgroup: attach the whole threadgroup?
2610 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002611 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002612 */
2613static int cgroup_attach_task(struct cgroup *dst_cgrp,
2614 struct task_struct *leader, bool threadgroup)
2615{
2616 LIST_HEAD(preloaded_csets);
2617 struct task_struct *task;
2618 int ret;
2619
2620 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002621 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002622 rcu_read_lock();
2623 task = leader;
2624 do {
2625 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2626 &preloaded_csets);
2627 if (!threadgroup)
2628 break;
2629 } while_each_thread(leader, task);
2630 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002631 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002632
2633 /* prepare dst csets and commit */
2634 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2635 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002636 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002637
2638 cgroup_migrate_finish(&preloaded_csets);
2639 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002640}
2641
Tejun Heo187fe842015-06-18 16:54:28 -04002642static int cgroup_procs_write_permission(struct task_struct *task,
2643 struct cgroup *dst_cgrp,
2644 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002645{
2646 const struct cred *cred = current_cred();
2647 const struct cred *tcred = get_task_cred(task);
2648 int ret = 0;
2649
2650 /*
2651 * even if we're attaching all tasks in the thread group, we only
2652 * need to check permissions on one of them.
2653 */
2654 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2655 !uid_eq(cred->euid, tcred->uid) &&
2656 !uid_eq(cred->euid, tcred->suid))
2657 ret = -EACCES;
2658
Tejun Heo187fe842015-06-18 16:54:28 -04002659 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2660 struct super_block *sb = of->file->f_path.dentry->d_sb;
2661 struct cgroup *cgrp;
2662 struct inode *inode;
2663
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002664 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002665 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002666 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002667
2668 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2669 cgrp = cgroup_parent(cgrp);
2670
2671 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002672 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002673 if (inode) {
2674 ret = inode_permission(inode, MAY_WRITE);
2675 iput(inode);
2676 }
2677 }
2678
Tejun Heodedf22e2015-06-18 16:54:28 -04002679 put_cred(tcred);
2680 return ret;
2681}
2682
Ben Blum74a11662011-05-26 16:25:20 -07002683/*
2684 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002685 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002686 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002687 */
Tejun Heoacbef752014-05-13 12:16:22 -04002688static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2689 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002690{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002691 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002692 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002693 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002694 int ret;
2695
Tejun Heoacbef752014-05-13 12:16:22 -04002696 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2697 return -EINVAL;
2698
Tejun Heoe76ecae2014-05-13 12:19:23 -04002699 cgrp = cgroup_kn_lock_live(of->kn);
2700 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002701 return -ENODEV;
2702
Tejun Heo3014dde2015-09-16 13:03:02 -04002703 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002704 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002705 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002706 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002707 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002708 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002709 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002710 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002711 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002712 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002713 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002714
2715 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002716 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002717
2718 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002719 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002720 * trapped in a cpuset, or RT worker may be born in a cgroup
2721 * with no rt_runtime allocated. Just say no.
2722 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002723 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002724 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002725 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002726 }
2727
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002728 get_task_struct(tsk);
2729 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002730
Tejun Heo187fe842015-06-18 16:54:28 -04002731 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002732 if (!ret)
2733 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002734
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002735 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002736 goto out_unlock_threadgroup;
2737
2738out_unlock_rcu:
2739 rcu_read_unlock();
2740out_unlock_threadgroup:
2741 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002742 cgroup_kn_unlock(of->kn);
Tejun Heoe93ad192016-01-19 12:18:41 -05002743 cpuset_post_attach_flush();
Tejun Heoacbef752014-05-13 12:16:22 -04002744 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002745}
2746
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002747/**
2748 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2749 * @from: attach to all cgroups of a given task
2750 * @tsk: the task to be attached
2751 */
2752int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2753{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002754 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002755 int retval = 0;
2756
Tejun Heo47cfcd02013-04-07 09:29:51 -07002757 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002758 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002759 struct cgroup *from_cgrp;
2760
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002761 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002762 continue;
2763
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002764 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002765 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002766 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002767
Li Zefan6f4b7e62013-07-31 16:18:36 +08002768 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002769 if (retval)
2770 break;
2771 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002772 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002773
2774 return retval;
2775}
2776EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2777
Tejun Heoacbef752014-05-13 12:16:22 -04002778static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2779 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002780{
Tejun Heoacbef752014-05-13 12:16:22 -04002781 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002782}
2783
Tejun Heoacbef752014-05-13 12:16:22 -04002784static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2785 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002786{
Tejun Heoacbef752014-05-13 12:16:22 -04002787 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002788}
2789
Tejun Heo451af502014-05-13 12:16:21 -04002790static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2791 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e0662008-07-25 01:46:59 -07002792{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002793 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002794
Tejun Heoe76ecae2014-05-13 12:19:23 -04002795 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2796
2797 cgrp = cgroup_kn_lock_live(of->kn);
2798 if (!cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07002799 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002800 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002801 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2802 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002803 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002804 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002805 return nbytes;
Paul Menagee788e0662008-07-25 01:46:59 -07002806}
2807
Tejun Heo2da8ca82013-12-05 12:28:04 -05002808static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002809{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002810 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002811
Tejun Heo46cfeb02014-05-13 12:11:00 -04002812 spin_lock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002813 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002814 spin_unlock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002815 seq_putc(seq, '\n');
Paul Menagee788e0662008-07-25 01:46:59 -07002816 return 0;
2817}
2818
Tejun Heo2da8ca82013-12-05 12:28:04 -05002819static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002820{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002821 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002822 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002823}
2824
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002825static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002826{
2827 struct cgroup_subsys *ss;
2828 bool printed = false;
2829 int ssid;
2830
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002831 for_each_subsys_which(ss, ssid, &ss_mask) {
2832 if (printed)
2833 seq_putc(seq, ' ');
2834 seq_printf(seq, "%s", ss->name);
2835 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002836 }
2837 if (printed)
2838 seq_putc(seq, '\n');
2839}
2840
2841/* show controllers which are currently attached to the default hierarchy */
2842static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2843{
2844 struct cgroup *cgrp = seq_css(seq)->cgroup;
2845
Tejun Heo5533e012014-05-14 19:33:07 -04002846 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2847 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002848 return 0;
2849}
2850
2851/* show controllers which are enabled from the parent */
2852static int cgroup_controllers_show(struct seq_file *seq, void *v)
2853{
2854 struct cgroup *cgrp = seq_css(seq)->cgroup;
2855
Tejun Heo667c2492014-07-08 18:02:56 -04002856 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002857 return 0;
2858}
2859
2860/* show controllers which are enabled for a given cgroup's children */
2861static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2862{
2863 struct cgroup *cgrp = seq_css(seq)->cgroup;
2864
Tejun Heo667c2492014-07-08 18:02:56 -04002865 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002866 return 0;
2867}
2868
2869/**
2870 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2871 * @cgrp: root of the subtree to update csses for
2872 *
2873 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2874 * css associations need to be updated accordingly. This function looks up
2875 * all css_sets which are attached to the subtree, creates the matching
2876 * updated css_sets and migrates the tasks to the new ones.
2877 */
2878static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2879{
2880 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002881 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002882 struct cgroup_subsys_state *css;
2883 struct css_set *src_cset;
2884 int ret;
2885
Tejun Heof8f22e52014-04-23 11:13:16 -04002886 lockdep_assert_held(&cgroup_mutex);
2887
Tejun Heo3014dde2015-09-16 13:03:02 -04002888 percpu_down_write(&cgroup_threadgroup_rwsem);
2889
Tejun Heof8f22e52014-04-23 11:13:16 -04002890 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002891 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002892 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2893 struct cgrp_cset_link *link;
2894
2895 /* self is not affected by child_subsys_mask change */
2896 if (css->cgroup == cgrp)
2897 continue;
2898
2899 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2900 cgroup_migrate_add_src(link->cset, cgrp,
2901 &preloaded_csets);
2902 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002903 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002904
2905 /* NULL dst indicates self on default hierarchy */
2906 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2907 if (ret)
2908 goto out_finish;
2909
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002910 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002911 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002912 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002913
2914 /* src_csets precede dst_csets, break on the first dst_cset */
2915 if (!src_cset->mg_src_cgrp)
2916 break;
2917
Tejun Heo10265072015-09-11 15:00:22 -04002918 /* all tasks in src_csets need to be migrated */
2919 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2920 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002921 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002922 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002923
Tejun Heo10265072015-09-11 15:00:22 -04002924 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002925out_finish:
2926 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002927 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002928 return ret;
2929}
2930
2931/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002932static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2933 char *buf, size_t nbytes,
2934 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002935{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002936 unsigned long enable = 0, disable = 0;
2937 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002938 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002939 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002940 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002941 int ssid, ret;
2942
2943 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002944 * Parse input - space separated list of subsystem names prefixed
2945 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002946 */
Tejun Heo451af502014-05-13 12:16:21 -04002947 buf = strstrip(buf);
2948 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002949 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2950
Tejun Heod37167a2014-05-13 12:10:59 -04002951 if (tok[0] == '\0')
2952 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002953 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04002954 if (!cgroup_ssid_enabled(ssid) ||
2955 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002956 continue;
2957
2958 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002959 enable |= 1 << ssid;
2960 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002961 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002962 disable |= 1 << ssid;
2963 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002964 } else {
2965 return -EINVAL;
2966 }
2967 break;
2968 }
2969 if (ssid == CGROUP_SUBSYS_COUNT)
2970 return -EINVAL;
2971 }
2972
Tejun Heoa9746d82014-05-13 12:19:22 -04002973 cgrp = cgroup_kn_lock_live(of->kn);
2974 if (!cgrp)
2975 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002976
2977 for_each_subsys(ss, ssid) {
2978 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002979 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002980 enable &= ~(1 << ssid);
2981 continue;
2982 }
2983
Tejun Heoc29adf22014-07-08 18:02:56 -04002984 /* unavailable or not enabled on the parent? */
2985 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2986 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002987 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002988 ret = -ENOENT;
2989 goto out_unlock;
2990 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002991 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002992 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002993 disable &= ~(1 << ssid);
2994 continue;
2995 }
2996
2997 /* a child has it enabled? */
2998 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002999 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003000 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003001 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003002 }
3003 }
3004 }
3005 }
3006
3007 if (!enable && !disable) {
3008 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003009 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003010 }
3011
3012 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003013 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003014 * with tasks so that child cgroups don't compete against tasks.
3015 */
Tejun Heod51f39b2014-05-16 13:22:48 -04003016 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003017 ret = -EBUSY;
3018 goto out_unlock;
3019 }
3020
3021 /*
Tejun Heof63070d2014-07-08 18:02:57 -04003022 * Update subsys masks and calculate what needs to be done. More
3023 * subsystems than specified may need to be enabled or disabled
3024 * depending on subsystem dependencies.
3025 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05003026 old_sc = cgrp->subtree_control;
3027 old_ss = cgrp->child_subsys_mask;
3028 new_sc = (old_sc | enable) & ~disable;
3029 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04003030
Tejun Heo755bf5e2014-11-18 02:49:50 -05003031 css_enable = ~old_ss & new_ss;
3032 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04003033 enable |= css_enable;
3034 disable |= css_disable;
3035
Tejun Heodb6e3052014-11-18 02:49:51 -05003036 /*
3037 * Because css offlining is asynchronous, userland might try to
3038 * re-enable the same controller while the previous instance is
3039 * still around. In such cases, wait till it's gone using
3040 * offline_waitq.
3041 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10003042 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05003043 cgroup_for_each_live_child(child, cgrp) {
3044 DEFINE_WAIT(wait);
3045
3046 if (!cgroup_css(child, ss))
3047 continue;
3048
3049 cgroup_get(child);
3050 prepare_to_wait(&child->offline_waitq, &wait,
3051 TASK_UNINTERRUPTIBLE);
3052 cgroup_kn_unlock(of->kn);
3053 schedule();
3054 finish_wait(&child->offline_waitq, &wait);
3055 cgroup_put(child);
3056
3057 return restart_syscall();
3058 }
3059 }
3060
Tejun Heo755bf5e2014-11-18 02:49:50 -05003061 cgrp->subtree_control = new_sc;
3062 cgrp->child_subsys_mask = new_ss;
3063
Tejun Heof63070d2014-07-08 18:02:57 -04003064 /*
3065 * Create new csses or make the existing ones visible. A css is
3066 * created invisible if it's being implicitly enabled through
3067 * dependency. An invisible css is made visible when the userland
3068 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04003069 */
3070 for_each_subsys(ss, ssid) {
3071 if (!(enable & (1 << ssid)))
3072 continue;
3073
3074 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04003075 if (css_enable & (1 << ssid))
3076 ret = create_css(child, ss,
3077 cgrp->subtree_control & (1 << ssid));
3078 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003079 ret = css_populate_dir(cgroup_css(child, ss),
3080 NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003081 if (ret)
3082 goto err_undo_css;
3083 }
3084 }
3085
Tejun Heoc29adf22014-07-08 18:02:56 -04003086 /*
3087 * At this point, cgroup_e_css() results reflect the new csses
3088 * making the following cgroup_update_dfl_csses() properly update
3089 * css associations of all tasks in the subtree.
3090 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003091 ret = cgroup_update_dfl_csses(cgrp);
3092 if (ret)
3093 goto err_undo_css;
3094
Tejun Heof63070d2014-07-08 18:02:57 -04003095 /*
3096 * All tasks are migrated out of disabled csses. Kill or hide
3097 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003098 * disabled while other subsystems are still depending on it. The
3099 * css must not actively control resources and be in the vanilla
3100 * state if it's made visible again later. Controllers which may
3101 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003102 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003103 for_each_subsys(ss, ssid) {
3104 if (!(disable & (1 << ssid)))
3105 continue;
3106
Tejun Heof63070d2014-07-08 18:02:57 -04003107 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003108 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3109
3110 if (css_disable & (1 << ssid)) {
3111 kill_css(css);
3112 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003113 css_clear_dir(css, NULL);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003114 if (ss->css_reset)
3115 ss->css_reset(css);
3116 }
Tejun Heof63070d2014-07-08 18:02:57 -04003117 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003118 }
3119
Tejun Heo56c807b2014-11-18 02:49:51 -05003120 /*
3121 * The effective csses of all the descendants (excluding @cgrp) may
3122 * have changed. Subsystems can optionally subscribe to this event
3123 * by implementing ->css_e_css_changed() which is invoked if any of
3124 * the effective csses seen from the css's cgroup may have changed.
3125 */
3126 for_each_subsys(ss, ssid) {
3127 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
3128 struct cgroup_subsys_state *css;
3129
3130 if (!ss->css_e_css_changed || !this_css)
3131 continue;
3132
3133 css_for_each_descendant_pre(css, this_css)
3134 if (css != this_css)
3135 ss->css_e_css_changed(css);
3136 }
3137
Tejun Heof8f22e52014-04-23 11:13:16 -04003138 kernfs_activate(cgrp->kn);
3139 ret = 0;
3140out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003141 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003142 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003143
3144err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003145 cgrp->subtree_control = old_sc;
3146 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003147
3148 for_each_subsys(ss, ssid) {
3149 if (!(enable & (1 << ssid)))
3150 continue;
3151
3152 cgroup_for_each_live_child(child, cgrp) {
3153 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003154
3155 if (!css)
3156 continue;
3157
3158 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003159 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003160 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003161 css_clear_dir(css, NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003162 }
3163 }
3164 goto out_unlock;
3165}
3166
Tejun Heo4a07c222015-09-18 17:54:22 -04003167static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003168{
Tejun Heo4a07c222015-09-18 17:54:22 -04003169 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003170 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003171 return 0;
3172}
3173
Tejun Heo2bd59d42014-02-11 11:52:49 -05003174static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3175 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003176{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003177 struct cgroup *cgrp = of->kn->parent->priv;
3178 struct cftype *cft = of->kn->priv;
3179 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003180 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003181
Tejun Heob4168642014-05-13 12:16:21 -04003182 if (cft->write)
3183 return cft->write(of, buf, nbytes, off);
3184
Tejun Heo2bd59d42014-02-11 11:52:49 -05003185 /*
3186 * kernfs guarantees that a file isn't deleted with operations in
3187 * flight, which means that the matching css is and stays alive and
3188 * doesn't need to be pinned. The RCU locking is not necessary
3189 * either. It's just for the convenience of using cgroup_css().
3190 */
3191 rcu_read_lock();
3192 css = cgroup_css(cgrp, cft->ss);
3193 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003194
Tejun Heo451af502014-05-13 12:16:21 -04003195 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003196 unsigned long long v;
3197 ret = kstrtoull(buf, 0, &v);
3198 if (!ret)
3199 ret = cft->write_u64(css, cft, v);
3200 } else if (cft->write_s64) {
3201 long long v;
3202 ret = kstrtoll(buf, 0, &v);
3203 if (!ret)
3204 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003205 } else {
3206 ret = -EINVAL;
3207 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003208
Tejun Heoa742c592013-12-05 12:28:03 -05003209 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003210}
3211
Tejun Heo6612f052013-12-05 12:28:04 -05003212static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003213{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003214 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003215}
3216
3217static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3218{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003219 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003220}
3221
3222static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3223{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003224 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003225}
3226
3227static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3228{
Tejun Heo7da11272013-12-05 12:28:04 -05003229 struct cftype *cft = seq_cft(m);
3230 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003231
Tejun Heo2da8ca82013-12-05 12:28:04 -05003232 if (cft->seq_show)
3233 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003234
Tejun Heo896f5192013-12-05 12:28:04 -05003235 if (cft->read_u64)
3236 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3237 else if (cft->read_s64)
3238 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3239 else
3240 return -EINVAL;
3241 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003242}
3243
Tejun Heo2bd59d42014-02-11 11:52:49 -05003244static struct kernfs_ops cgroup_kf_single_ops = {
3245 .atomic_write_len = PAGE_SIZE,
3246 .write = cgroup_file_write,
3247 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003248};
3249
Tejun Heo2bd59d42014-02-11 11:52:49 -05003250static struct kernfs_ops cgroup_kf_ops = {
3251 .atomic_write_len = PAGE_SIZE,
3252 .write = cgroup_file_write,
3253 .seq_start = cgroup_seqfile_start,
3254 .seq_next = cgroup_seqfile_next,
3255 .seq_stop = cgroup_seqfile_stop,
3256 .seq_show = cgroup_seqfile_show,
3257};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003258
3259/*
3260 * cgroup_rename - Only allow simple rename of directories in place.
3261 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003262static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3263 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003264{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003265 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003266 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003267
Tejun Heo2bd59d42014-02-11 11:52:49 -05003268 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003269 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003270 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003271 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003272
Tejun Heo6db8e852013-06-14 11:18:22 -07003273 /*
3274 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003275 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003276 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003277 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003278 return -EPERM;
3279
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003280 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003281 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003282 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003283 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003284 */
3285 kernfs_break_active_protection(new_parent);
3286 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003287
Tejun Heo2bd59d42014-02-11 11:52:49 -05003288 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003289
Tejun Heo2bd59d42014-02-11 11:52:49 -05003290 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003291
Tejun Heo2bd59d42014-02-11 11:52:49 -05003292 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003293
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003294 kernfs_unbreak_active_protection(kn);
3295 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003296 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003297}
3298
Tejun Heo49957f82014-04-07 16:44:47 -04003299/* set uid and gid of cgroup dirs and files to that of the creator */
3300static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3301{
3302 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3303 .ia_uid = current_fsuid(),
3304 .ia_gid = current_fsgid(), };
3305
3306 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3307 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3308 return 0;
3309
3310 return kernfs_setattr(kn, &iattr);
3311}
3312
Tejun Heo4df8dc92015-09-18 17:54:23 -04003313static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3314 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003315{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003316 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003317 struct kernfs_node *kn;
3318 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003319 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003320
Tejun Heo2bd59d42014-02-11 11:52:49 -05003321#ifdef CONFIG_DEBUG_LOCK_ALLOC
3322 key = &cft->lockdep_key;
3323#endif
3324 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3325 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003326 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003327 if (IS_ERR(kn))
3328 return PTR_ERR(kn);
3329
3330 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003331 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003332 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003333 return ret;
3334 }
3335
Tejun Heo6f60ead2015-09-18 17:54:23 -04003336 if (cft->file_offset) {
3337 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3338
Tejun Heo34c06252015-11-05 00:12:24 -05003339 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003340 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003341 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003342 }
3343
Tejun Heof8f22e52014-04-23 11:13:16 -04003344 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003345}
3346
Tejun Heob1f28d32013-06-28 16:24:10 -07003347/**
3348 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003349 * @css: the target css
3350 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003351 * @cfts: array of cftypes to be added
3352 * @is_add: whether to add or remove
3353 *
3354 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003355 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003356 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003357static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3358 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003359 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003360{
Tejun Heo6732ed82015-09-18 17:54:23 -04003361 struct cftype *cft, *cft_end = NULL;
Tejun Heob1f28d32013-06-28 16:24:10 -07003362 int ret;
3363
Tejun Heo01f64742014-05-13 12:19:23 -04003364 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003365
Tejun Heo6732ed82015-09-18 17:54:23 -04003366restart:
3367 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003368 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003369 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003370 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003371 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003372 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003373 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003374 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003375 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003376 continue;
3377
Li Zefan2739d3c2013-01-21 18:18:33 +08003378 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003379 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003380 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003381 pr_warn("%s: failed to add %s, err=%d\n",
3382 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003383 cft_end = cft;
3384 is_add = false;
3385 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003386 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003387 } else {
3388 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003389 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003390 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003391 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003392}
3393
Tejun Heo21a2d342014-02-12 09:29:49 -05003394static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003395{
3396 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003397 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003398 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003399 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003400 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003401
Tejun Heo01f64742014-05-13 12:19:23 -04003402 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003403
Li Zefane8c82d22013-06-18 18:48:37 +08003404 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003405 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003406 struct cgroup *cgrp = css->cgroup;
3407
Li Zefane8c82d22013-06-18 18:48:37 +08003408 if (cgroup_is_dead(cgrp))
3409 continue;
3410
Tejun Heo4df8dc92015-09-18 17:54:23 -04003411 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003412 if (ret)
3413 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003414 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003415
3416 if (is_add && !ret)
3417 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003418 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003419}
3420
Tejun Heo2da440a2014-02-11 11:52:48 -05003421static void cgroup_exit_cftypes(struct cftype *cfts)
3422{
3423 struct cftype *cft;
3424
Tejun Heo2bd59d42014-02-11 11:52:49 -05003425 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3426 /* free copy for custom atomic_write_len, see init_cftypes() */
3427 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3428 kfree(cft->kf_ops);
3429 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003430 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003431
3432 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003433 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003434 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003435}
3436
Tejun Heo2bd59d42014-02-11 11:52:49 -05003437static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003438{
3439 struct cftype *cft;
3440
Tejun Heo2bd59d42014-02-11 11:52:49 -05003441 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3442 struct kernfs_ops *kf_ops;
3443
Tejun Heo0adb0702014-02-12 09:29:48 -05003444 WARN_ON(cft->ss || cft->kf_ops);
3445
Tejun Heo2bd59d42014-02-11 11:52:49 -05003446 if (cft->seq_start)
3447 kf_ops = &cgroup_kf_ops;
3448 else
3449 kf_ops = &cgroup_kf_single_ops;
3450
3451 /*
3452 * Ugh... if @cft wants a custom max_write_len, we need to
3453 * make a copy of kf_ops to set its atomic_write_len.
3454 */
3455 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3456 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3457 if (!kf_ops) {
3458 cgroup_exit_cftypes(cfts);
3459 return -ENOMEM;
3460 }
3461 kf_ops->atomic_write_len = cft->max_write_len;
3462 }
3463
3464 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003465 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003466 }
3467
3468 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003469}
3470
Tejun Heo21a2d342014-02-12 09:29:49 -05003471static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3472{
Tejun Heo01f64742014-05-13 12:19:23 -04003473 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003474
3475 if (!cfts || !cfts[0].ss)
3476 return -ENOENT;
3477
3478 list_del(&cfts->node);
3479 cgroup_apply_cftypes(cfts, false);
3480 cgroup_exit_cftypes(cfts);
3481 return 0;
3482}
3483
Tejun Heo8e3f6542012-04-01 12:09:55 -07003484/**
Tejun Heo80b13582014-02-12 09:29:48 -05003485 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3486 * @cfts: zero-length name terminated array of cftypes
3487 *
3488 * Unregister @cfts. Files described by @cfts are removed from all
3489 * existing cgroups and all future cgroups won't have them either. This
3490 * function can be called anytime whether @cfts' subsys is attached or not.
3491 *
3492 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3493 * registered.
3494 */
3495int cgroup_rm_cftypes(struct cftype *cfts)
3496{
Tejun Heo21a2d342014-02-12 09:29:49 -05003497 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003498
Tejun Heo01f64742014-05-13 12:19:23 -04003499 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003500 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003501 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003502 return ret;
3503}
3504
3505/**
3506 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3507 * @ss: target cgroup subsystem
3508 * @cfts: zero-length name terminated array of cftypes
3509 *
3510 * Register @cfts to @ss. Files described by @cfts are created for all
3511 * existing cgroups to which @ss is attached and all future cgroups will
3512 * have them too. This function can be called anytime whether @ss is
3513 * attached or not.
3514 *
3515 * Returns 0 on successful registration, -errno on failure. Note that this
3516 * function currently returns 0 as long as @cfts registration is successful
3517 * even if some file creation attempts on existing cgroups fail.
3518 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003519static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003520{
Tejun Heo9ccece82013-06-28 16:24:11 -07003521 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003522
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003523 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003524 return 0;
3525
Li Zefandc5736e2014-02-17 10:41:50 +08003526 if (!cfts || cfts[0].name[0] == '\0')
3527 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003528
Tejun Heo2bd59d42014-02-11 11:52:49 -05003529 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003530 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003531 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003532
Tejun Heo01f64742014-05-13 12:19:23 -04003533 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003534
Tejun Heo0adb0702014-02-12 09:29:48 -05003535 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003536 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003537 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003538 cgroup_rm_cftypes_locked(cfts);
3539
Tejun Heo01f64742014-05-13 12:19:23 -04003540 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003541 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003542}
Tejun Heo79578622012-04-01 12:09:56 -07003543
3544/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003545 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3546 * @ss: target cgroup subsystem
3547 * @cfts: zero-length name terminated array of cftypes
3548 *
3549 * Similar to cgroup_add_cftypes() but the added files are only used for
3550 * the default hierarchy.
3551 */
3552int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3553{
3554 struct cftype *cft;
3555
3556 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003557 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003558 return cgroup_add_cftypes(ss, cfts);
3559}
3560
3561/**
3562 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3563 * @ss: target cgroup subsystem
3564 * @cfts: zero-length name terminated array of cftypes
3565 *
3566 * Similar to cgroup_add_cftypes() but the added files are only used for
3567 * the legacy hierarchies.
3568 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003569int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3570{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003571 struct cftype *cft;
3572
Tejun Heoe4b70372015-10-15 17:00:43 -04003573 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3574 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003575 return cgroup_add_cftypes(ss, cfts);
3576}
3577
Li Zefana043e3b2008-02-23 15:24:09 -08003578/**
Tejun Heo34c06252015-11-05 00:12:24 -05003579 * cgroup_file_notify - generate a file modified event for a cgroup_file
3580 * @cfile: target cgroup_file
3581 *
3582 * @cfile must have been obtained by setting cftype->file_offset.
3583 */
3584void cgroup_file_notify(struct cgroup_file *cfile)
3585{
3586 unsigned long flags;
3587
3588 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3589 if (cfile->kn)
3590 kernfs_notify(cfile->kn);
3591 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3592}
3593
3594/**
Li Zefana043e3b2008-02-23 15:24:09 -08003595 * cgroup_task_count - count the number of tasks in a cgroup.
3596 * @cgrp: the cgroup in question
3597 *
3598 * Return the number of tasks in the cgroup.
3599 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003600static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003601{
3602 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003603 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003604
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003605 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003606 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3607 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003608 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003609 return count;
3610}
3611
Tejun Heo574bd9f2012-11-09 09:12:29 -08003612/**
Tejun Heo492eb212013-08-08 20:11:25 -04003613 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003614 * @pos: the current position (%NULL to initiate traversal)
3615 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003616 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003617 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003618 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003619 * that @parent and @pos are accessible. The next sibling is guaranteed to
3620 * be returned regardless of their states.
3621 *
3622 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3623 * css which finished ->css_online() is guaranteed to be visible in the
3624 * future iterations and will stay visible until the last reference is put.
3625 * A css which hasn't finished ->css_online() or already finished
3626 * ->css_offline() may show up during traversal. It's each subsystem's
3627 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003628 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003629struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3630 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003631{
Tejun Heoc2931b72014-05-16 13:22:51 -04003632 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003633
Tejun Heo8353da12014-05-13 12:19:23 -04003634 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003635
3636 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003637 * @pos could already have been unlinked from the sibling list.
3638 * Once a cgroup is removed, its ->sibling.next is no longer
3639 * updated when its next sibling changes. CSS_RELEASED is set when
3640 * @pos is taken off list, at which time its next pointer is valid,
3641 * and, as releases are serialized, the one pointed to by the next
3642 * pointer is guaranteed to not have started release yet. This
3643 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3644 * critical section, the one pointed to by its next pointer is
3645 * guaranteed to not have finished its RCU grace period even if we
3646 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003647 *
Tejun Heode3f0342014-05-16 13:22:49 -04003648 * If @pos has CSS_RELEASED set, its next pointer can't be
3649 * dereferenced; however, as each css is given a monotonically
3650 * increasing unique serial number and always appended to the
3651 * sibling list, the next one can be found by walking the parent's
3652 * children until the first css with higher serial number than
3653 * @pos's. While this path can be slower, it happens iff iteration
3654 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003655 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003656 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003657 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3658 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3659 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003660 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003661 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003662 if (next->serial_nr > pos->serial_nr)
3663 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003664 }
3665
Tejun Heo3b281af2014-04-23 11:13:15 -04003666 /*
3667 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003668 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003669 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003670 if (&next->sibling != &parent->children)
3671 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003672 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003673}
Tejun Heo53fa5262013-05-24 10:55:38 +09003674
3675/**
Tejun Heo492eb212013-08-08 20:11:25 -04003676 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003677 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003678 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003679 *
Tejun Heo492eb212013-08-08 20:11:25 -04003680 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003681 * to visit for pre-order traversal of @root's descendants. @root is
3682 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003683 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003684 * While this function requires cgroup_mutex or RCU read locking, it
3685 * doesn't require the whole traversal to be contained in a single critical
3686 * section. This function will return the correct next descendant as long
3687 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003688 *
3689 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3690 * css which finished ->css_online() is guaranteed to be visible in the
3691 * future iterations and will stay visible until the last reference is put.
3692 * A css which hasn't finished ->css_online() or already finished
3693 * ->css_offline() may show up during traversal. It's each subsystem's
3694 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003695 */
Tejun Heo492eb212013-08-08 20:11:25 -04003696struct cgroup_subsys_state *
3697css_next_descendant_pre(struct cgroup_subsys_state *pos,
3698 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003699{
Tejun Heo492eb212013-08-08 20:11:25 -04003700 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003701
Tejun Heo8353da12014-05-13 12:19:23 -04003702 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003703
Tejun Heobd8815a2013-08-08 20:11:27 -04003704 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003705 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003706 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003707
3708 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003709 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003710 if (next)
3711 return next;
3712
3713 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003714 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003715 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003716 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003717 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003718 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003719 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003720
3721 return NULL;
3722}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003723
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003724/**
Tejun Heo492eb212013-08-08 20:11:25 -04003725 * css_rightmost_descendant - return the rightmost descendant of a css
3726 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003727 *
Tejun Heo492eb212013-08-08 20:11:25 -04003728 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3729 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003730 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003731 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003732 * While this function requires cgroup_mutex or RCU read locking, it
3733 * doesn't require the whole traversal to be contained in a single critical
3734 * section. This function will return the correct rightmost descendant as
3735 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003736 */
Tejun Heo492eb212013-08-08 20:11:25 -04003737struct cgroup_subsys_state *
3738css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003739{
Tejun Heo492eb212013-08-08 20:11:25 -04003740 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003741
Tejun Heo8353da12014-05-13 12:19:23 -04003742 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003743
3744 do {
3745 last = pos;
3746 /* ->prev isn't RCU safe, walk ->next till the end */
3747 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003748 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003749 pos = tmp;
3750 } while (pos);
3751
3752 return last;
3753}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003754
Tejun Heo492eb212013-08-08 20:11:25 -04003755static struct cgroup_subsys_state *
3756css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003757{
Tejun Heo492eb212013-08-08 20:11:25 -04003758 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003759
3760 do {
3761 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003762 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003763 } while (pos);
3764
3765 return last;
3766}
3767
3768/**
Tejun Heo492eb212013-08-08 20:11:25 -04003769 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003770 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003771 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003772 *
Tejun Heo492eb212013-08-08 20:11:25 -04003773 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003774 * to visit for post-order traversal of @root's descendants. @root is
3775 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003776 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003777 * While this function requires cgroup_mutex or RCU read locking, it
3778 * doesn't require the whole traversal to be contained in a single critical
3779 * section. This function will return the correct next descendant as long
3780 * as both @pos and @cgroup are accessible and @pos is a descendant of
3781 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003782 *
3783 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3784 * css which finished ->css_online() is guaranteed to be visible in the
3785 * future iterations and will stay visible until the last reference is put.
3786 * A css which hasn't finished ->css_online() or already finished
3787 * ->css_offline() may show up during traversal. It's each subsystem's
3788 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003789 */
Tejun Heo492eb212013-08-08 20:11:25 -04003790struct cgroup_subsys_state *
3791css_next_descendant_post(struct cgroup_subsys_state *pos,
3792 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003793{
Tejun Heo492eb212013-08-08 20:11:25 -04003794 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003795
Tejun Heo8353da12014-05-13 12:19:23 -04003796 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003797
Tejun Heo58b79a92013-09-06 15:31:08 -04003798 /* if first iteration, visit leftmost descendant which may be @root */
3799 if (!pos)
3800 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003801
Tejun Heobd8815a2013-08-08 20:11:27 -04003802 /* if we visited @root, we're done */
3803 if (pos == root)
3804 return NULL;
3805
Tejun Heo574bd9f2012-11-09 09:12:29 -08003806 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003807 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003808 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003809 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003810
3811 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003812 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003813}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003814
Tejun Heof3d46502014-05-16 13:22:52 -04003815/**
3816 * css_has_online_children - does a css have online children
3817 * @css: the target css
3818 *
3819 * Returns %true if @css has any online children; otherwise, %false. This
3820 * function can be called from any context but the caller is responsible
3821 * for synchronizing against on/offlining as necessary.
3822 */
3823bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003824{
Tejun Heof3d46502014-05-16 13:22:52 -04003825 struct cgroup_subsys_state *child;
3826 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003827
3828 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003829 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003830 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003831 ret = true;
3832 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003833 }
3834 }
3835 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003836 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003837}
3838
Tejun Heo0942eee2013-08-08 20:11:26 -04003839/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003840 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003841 * @it: the iterator to advance
3842 *
3843 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003844 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003845static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003846{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003847 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003848 struct cgrp_cset_link *link;
3849 struct css_set *cset;
3850
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003851 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003852
Tejun Heod5158762013-08-08 20:11:26 -04003853 /* Advance to the next non-empty css_set */
3854 do {
3855 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003856 if (l == it->cset_head) {
3857 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003858 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003859 return;
3860 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003861
3862 if (it->ss) {
3863 cset = container_of(l, struct css_set,
3864 e_cset_node[it->ss->id]);
3865 } else {
3866 link = list_entry(l, struct cgrp_cset_link, cset_link);
3867 cset = link->cset;
3868 }
Tejun Heo0de09422015-10-15 16:41:49 -04003869 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05003870
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003871 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003872
3873 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003874 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003875 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003876 it->task_pos = cset->mg_tasks.next;
3877
3878 it->tasks_head = &cset->tasks;
3879 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04003880
3881 /*
3882 * We don't keep css_sets locked across iteration steps and thus
3883 * need to take steps to ensure that iteration can be resumed after
3884 * the lock is re-acquired. Iteration is performed at two levels -
3885 * css_sets and tasks in them.
3886 *
3887 * Once created, a css_set never leaves its cgroup lists, so a
3888 * pinned css_set is guaranteed to stay put and we can resume
3889 * iteration afterwards.
3890 *
3891 * Tasks may leave @cset across iteration steps. This is resolved
3892 * by registering each iterator with the css_set currently being
3893 * walked and making css_set_move_task() advance iterators whose
3894 * next task is leaving.
3895 */
3896 if (it->cur_cset) {
3897 list_del(&it->iters_node);
3898 put_css_set_locked(it->cur_cset);
3899 }
3900 get_css_set(cset);
3901 it->cur_cset = cset;
3902 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04003903}
3904
Tejun Heoecb9d532015-10-15 16:41:52 -04003905static void css_task_iter_advance(struct css_task_iter *it)
3906{
3907 struct list_head *l = it->task_pos;
3908
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003909 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04003910 WARN_ON_ONCE(!l);
3911
3912 /*
3913 * Advance iterator to find next entry. cset->tasks is consumed
3914 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3915 * next cset.
3916 */
3917 l = l->next;
3918
3919 if (l == it->tasks_head)
3920 l = it->mg_tasks_head->next;
3921
3922 if (l == it->mg_tasks_head)
3923 css_task_iter_advance_css_set(it);
3924 else
3925 it->task_pos = l;
3926}
3927
Tejun Heo0942eee2013-08-08 20:11:26 -04003928/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003929 * css_task_iter_start - initiate task iteration
3930 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003931 * @it: the task iterator to use
3932 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003933 * Initiate iteration through the tasks of @css. The caller can call
3934 * css_task_iter_next() to walk through the tasks until the function
3935 * returns NULL. On completion of iteration, css_task_iter_end() must be
3936 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003937 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003938void css_task_iter_start(struct cgroup_subsys_state *css,
3939 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003940{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003941 /* no one should try to iterate before mounting cgroups */
3942 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003943
Tejun Heoed27b9f2015-10-15 16:41:52 -04003944 memset(it, 0, sizeof(*it));
3945
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003946 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003947
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003948 it->ss = css->ss;
3949
3950 if (it->ss)
3951 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3952 else
3953 it->cset_pos = &css->cgroup->cset_links;
3954
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003955 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003956
Tejun Heoecb9d532015-10-15 16:41:52 -04003957 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003958
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003959 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003960}
Paul Menage817929e2007-10-18 23:39:36 -07003961
Tejun Heo0942eee2013-08-08 20:11:26 -04003962/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003963 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003964 * @it: the task iterator being iterated
3965 *
3966 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003967 * initialized via css_task_iter_start(). Returns NULL when the iteration
3968 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003969 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003970struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003971{
Tejun Heod5745672015-10-29 11:43:05 +09003972 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04003973 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09003974 it->cur_task = NULL;
3975 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003976
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003977 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003978
Tejun Heod5745672015-10-29 11:43:05 +09003979 if (it->task_pos) {
3980 it->cur_task = list_entry(it->task_pos, struct task_struct,
3981 cg_list);
3982 get_task_struct(it->cur_task);
3983 css_task_iter_advance(it);
3984 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003985
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003986 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003987
3988 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07003989}
3990
Tejun Heo0942eee2013-08-08 20:11:26 -04003991/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003992 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003993 * @it: the task iterator to finish
3994 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003995 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003996 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003997void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003998{
Tejun Heoed27b9f2015-10-15 16:41:52 -04003999 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004000 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004001 list_del(&it->iters_node);
4002 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004003 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004004 }
4005
4006 if (it->cur_task)
4007 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004008}
4009
4010/**
4011 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4012 * @to: cgroup to which the tasks will be moved
4013 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004014 *
4015 * Locking rules between cgroup_post_fork() and the migration path
4016 * guarantee that, if a task is forking while being migrated, the new child
4017 * is guaranteed to be either visible in the source cgroup after the
4018 * parent's migration is complete or put into the target cgroup. No task
4019 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004020 */
4021int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4022{
Tejun Heo952aaa12014-02-25 10:04:03 -05004023 LIST_HEAD(preloaded_csets);
4024 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004025 struct css_task_iter it;
4026 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004027 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004028
Tejun Heo952aaa12014-02-25 10:04:03 -05004029 mutex_lock(&cgroup_mutex);
4030
4031 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004032 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004033 list_for_each_entry(link, &from->cset_links, cset_link)
4034 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004035 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004036
4037 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4038 if (ret)
4039 goto out_err;
4040
4041 /*
Rami Rosen2cfa2b12016-01-09 23:33:06 +02004042 * Migrate tasks one-by-one until @from is empty. This fails iff
Tejun Heo952aaa12014-02-25 10:04:03 -05004043 * ->can_attach() fails.
4044 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004045 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004046 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004047 task = css_task_iter_next(&it);
4048 if (task)
4049 get_task_struct(task);
4050 css_task_iter_end(&it);
4051
4052 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004053 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004054 put_task_struct(task);
4055 }
4056 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004057out_err:
4058 cgroup_migrate_finish(&preloaded_csets);
4059 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004060 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004061}
4062
Paul Menage817929e2007-10-18 23:39:36 -07004063/*
Ben Blum102a7752009-09-23 15:56:26 -07004064 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004065 *
4066 * Reading this file can return large amounts of data if a cgroup has
4067 * *lots* of attached tasks. So it may need several calls to read(),
4068 * but we cannot guarantee that the information we produce is correct
4069 * unless we produce it entirely atomically.
4070 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004071 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004072
Li Zefan24528252012-01-20 11:58:43 +08004073/* which pidlist file are we talking about? */
4074enum cgroup_filetype {
4075 CGROUP_FILE_PROCS,
4076 CGROUP_FILE_TASKS,
4077};
4078
4079/*
4080 * A pidlist is a list of pids that virtually represents the contents of one
4081 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4082 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4083 * to the cgroup.
4084 */
4085struct cgroup_pidlist {
4086 /*
4087 * used to find which pidlist is wanted. doesn't change as long as
4088 * this particular list stays in the list.
4089 */
4090 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4091 /* array of xids */
4092 pid_t *list;
4093 /* how many elements the above list has */
4094 int length;
Li Zefan24528252012-01-20 11:58:43 +08004095 /* each of these stored in a list by its cgroup */
4096 struct list_head links;
4097 /* pointer to the cgroup we belong to, for list removal purposes */
4098 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004099 /* for delayed destruction */
4100 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004101};
4102
Paul Menagebbcb81d2007-10-18 23:39:32 -07004103/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004104 * The following two functions "fix" the issue where there are more pids
4105 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4106 * TODO: replace with a kernel-wide solution to this problem
4107 */
4108#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4109static void *pidlist_allocate(int count)
4110{
4111 if (PIDLIST_TOO_LARGE(count))
4112 return vmalloc(count * sizeof(pid_t));
4113 else
4114 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4115}
Tejun Heob1a21362013-11-29 10:42:58 -05004116
Ben Blumd1d9fd32009-09-23 15:56:28 -07004117static void pidlist_free(void *p)
4118{
Bandan Das58794512015-03-02 17:51:10 -05004119 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004120}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004121
4122/*
Tejun Heob1a21362013-11-29 10:42:58 -05004123 * Used to destroy all pidlists lingering waiting for destroy timer. None
4124 * should be left afterwards.
4125 */
4126static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4127{
4128 struct cgroup_pidlist *l, *tmp_l;
4129
4130 mutex_lock(&cgrp->pidlist_mutex);
4131 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4132 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4133 mutex_unlock(&cgrp->pidlist_mutex);
4134
4135 flush_workqueue(cgroup_pidlist_destroy_wq);
4136 BUG_ON(!list_empty(&cgrp->pidlists));
4137}
4138
4139static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4140{
4141 struct delayed_work *dwork = to_delayed_work(work);
4142 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4143 destroy_dwork);
4144 struct cgroup_pidlist *tofree = NULL;
4145
4146 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004147
4148 /*
Tejun Heo04502362013-11-29 10:42:59 -05004149 * Destroy iff we didn't get queued again. The state won't change
4150 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004151 */
Tejun Heo04502362013-11-29 10:42:59 -05004152 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004153 list_del(&l->links);
4154 pidlist_free(l->list);
4155 put_pid_ns(l->key.ns);
4156 tofree = l;
4157 }
4158
Tejun Heob1a21362013-11-29 10:42:58 -05004159 mutex_unlock(&l->owner->pidlist_mutex);
4160 kfree(tofree);
4161}
4162
4163/*
Ben Blum102a7752009-09-23 15:56:26 -07004164 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004165 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004166 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004167static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004168{
Ben Blum102a7752009-09-23 15:56:26 -07004169 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004170
4171 /*
4172 * we presume the 0th element is unique, so i starts at 1. trivial
4173 * edge cases first; no work needs to be done for either
4174 */
4175 if (length == 0 || length == 1)
4176 return length;
4177 /* src and dest walk down the list; dest counts unique elements */
4178 for (src = 1; src < length; src++) {
4179 /* find next unique element */
4180 while (list[src] == list[src-1]) {
4181 src++;
4182 if (src == length)
4183 goto after;
4184 }
4185 /* dest always points to where the next unique element goes */
4186 list[dest] = list[src];
4187 dest++;
4188 }
4189after:
Ben Blum102a7752009-09-23 15:56:26 -07004190 return dest;
4191}
4192
Tejun Heoafb2bc12013-11-29 10:42:59 -05004193/*
4194 * The two pid files - task and cgroup.procs - guaranteed that the result
4195 * is sorted, which forced this whole pidlist fiasco. As pid order is
4196 * different per namespace, each namespace needs differently sorted list,
4197 * making it impossible to use, for example, single rbtree of member tasks
4198 * sorted by task pointer. As pidlists can be fairly large, allocating one
4199 * per open file is dangerous, so cgroup had to implement shared pool of
4200 * pidlists keyed by cgroup and namespace.
4201 *
4202 * All this extra complexity was caused by the original implementation
4203 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004204 * want to do away with it. Explicitly scramble sort order if on the
4205 * default hierarchy so that no such expectation exists in the new
4206 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004207 *
4208 * Scrambling is done by swapping every two consecutive bits, which is
4209 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4210 */
4211static pid_t pid_fry(pid_t pid)
4212{
4213 unsigned a = pid & 0x55555555;
4214 unsigned b = pid & 0xAAAAAAAA;
4215
4216 return (a << 1) | (b >> 1);
4217}
4218
4219static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4220{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004221 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004222 return pid_fry(pid);
4223 else
4224 return pid;
4225}
4226
Ben Blum102a7752009-09-23 15:56:26 -07004227static int cmppid(const void *a, const void *b)
4228{
4229 return *(pid_t *)a - *(pid_t *)b;
4230}
4231
Tejun Heoafb2bc12013-11-29 10:42:59 -05004232static int fried_cmppid(const void *a, const void *b)
4233{
4234 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4235}
4236
Ben Blum72a8cb32009-09-23 15:56:27 -07004237static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4238 enum cgroup_filetype type)
4239{
4240 struct cgroup_pidlist *l;
4241 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004242 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004243
Tejun Heoe6b81712013-11-29 10:42:59 -05004244 lockdep_assert_held(&cgrp->pidlist_mutex);
4245
4246 list_for_each_entry(l, &cgrp->pidlists, links)
4247 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004248 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004249 return NULL;
4250}
4251
Ben Blum72a8cb32009-09-23 15:56:27 -07004252/*
4253 * find the appropriate pidlist for our purpose (given procs vs tasks)
4254 * returns with the lock on that pidlist already held, and takes care
4255 * of the use count, or returns NULL with no locks held if we're out of
4256 * memory.
4257 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004258static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4259 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004260{
4261 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004262
Tejun Heoe6b81712013-11-29 10:42:59 -05004263 lockdep_assert_held(&cgrp->pidlist_mutex);
4264
4265 l = cgroup_pidlist_find(cgrp, type);
4266 if (l)
4267 return l;
4268
Ben Blum72a8cb32009-09-23 15:56:27 -07004269 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004270 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004271 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004272 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004273
Tejun Heob1a21362013-11-29 10:42:58 -05004274 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004275 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004276 /* don't need task_nsproxy() if we're looking at ourself */
4277 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004278 l->owner = cgrp;
4279 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004280 return l;
4281}
4282
4283/*
Ben Blum102a7752009-09-23 15:56:26 -07004284 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4285 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004286static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4287 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004288{
4289 pid_t *array;
4290 int length;
4291 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004292 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004293 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004294 struct cgroup_pidlist *l;
4295
Tejun Heo4bac00d2013-11-29 10:42:59 -05004296 lockdep_assert_held(&cgrp->pidlist_mutex);
4297
Ben Blum102a7752009-09-23 15:56:26 -07004298 /*
4299 * If cgroup gets more users after we read count, we won't have
4300 * enough space - tough. This race is indistinguishable to the
4301 * caller from the case that the additional cgroup users didn't
4302 * show up until sometime later on.
4303 */
4304 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004305 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004306 if (!array)
4307 return -ENOMEM;
4308 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004309 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004310 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004311 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004312 break;
Ben Blum102a7752009-09-23 15:56:26 -07004313 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004314 if (type == CGROUP_FILE_PROCS)
4315 pid = task_tgid_vnr(tsk);
4316 else
4317 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004318 if (pid > 0) /* make sure to only use valid results */
4319 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004320 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004321 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004322 length = n;
4323 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004324 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004325 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4326 else
4327 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004328 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004329 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004330
Tejun Heoe6b81712013-11-29 10:42:59 -05004331 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004332 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004333 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004334 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004335 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004336
4337 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004338 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004339 l->list = array;
4340 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004341 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004342 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004343}
4344
Balbir Singh846c7bb2007-10-18 23:39:44 -07004345/**
Li Zefana043e3b2008-02-23 15:24:09 -08004346 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004347 * @stats: cgroupstats to fill information into
4348 * @dentry: A dentry entry belonging to the cgroup for which stats have
4349 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004350 *
4351 * Build and fill cgroupstats so that taskstats can export it to user
4352 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004353 */
4354int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4355{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004356 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004357 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004358 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004359 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004360
Tejun Heo2bd59d42014-02-11 11:52:49 -05004361 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4362 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4363 kernfs_type(kn) != KERNFS_DIR)
4364 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004365
Li Zefanbad34662014-02-14 16:54:28 +08004366 mutex_lock(&cgroup_mutex);
4367
Tejun Heo2bd59d42014-02-11 11:52:49 -05004368 /*
4369 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004370 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004371 * @kn->priv is RCU safe. Let's do the RCU dancing.
4372 */
4373 rcu_read_lock();
4374 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004375 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004376 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004377 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004378 return -ENOENT;
4379 }
Li Zefanbad34662014-02-14 16:54:28 +08004380 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004381
Tejun Heo9d800df2014-05-14 09:15:00 -04004382 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004383 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004384 switch (tsk->state) {
4385 case TASK_RUNNING:
4386 stats->nr_running++;
4387 break;
4388 case TASK_INTERRUPTIBLE:
4389 stats->nr_sleeping++;
4390 break;
4391 case TASK_UNINTERRUPTIBLE:
4392 stats->nr_uninterruptible++;
4393 break;
4394 case TASK_STOPPED:
4395 stats->nr_stopped++;
4396 break;
4397 default:
4398 if (delayacct_is_task_waiting_on_io(tsk))
4399 stats->nr_io_wait++;
4400 break;
4401 }
4402 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004403 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004404
Li Zefanbad34662014-02-14 16:54:28 +08004405 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004406 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004407}
4408
Paul Menage8f3ff202009-09-23 15:56:25 -07004409
Paul Menagecc31edc2008-10-18 20:28:04 -07004410/*
Ben Blum102a7752009-09-23 15:56:26 -07004411 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004412 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004413 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004414 */
4415
Ben Blum102a7752009-09-23 15:56:26 -07004416static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004417{
4418 /*
4419 * Initially we receive a position value that corresponds to
4420 * one more than the last pid shown (or 0 on the first call or
4421 * after a seek to the start). Use a binary-search to find the
4422 * next pid to display, if any
4423 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004424 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004425 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004426 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004427 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004428 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004429 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004430
Tejun Heo4bac00d2013-11-29 10:42:59 -05004431 mutex_lock(&cgrp->pidlist_mutex);
4432
4433 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004434 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004435 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004436 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004437 * could already have been destroyed.
4438 */
Tejun Heo5d224442013-12-05 12:28:04 -05004439 if (of->priv)
4440 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004441
4442 /*
4443 * Either this is the first start() after open or the matching
4444 * pidlist has been destroyed inbetween. Create a new one.
4445 */
Tejun Heo5d224442013-12-05 12:28:04 -05004446 if (!of->priv) {
4447 ret = pidlist_array_load(cgrp, type,
4448 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004449 if (ret)
4450 return ERR_PTR(ret);
4451 }
Tejun Heo5d224442013-12-05 12:28:04 -05004452 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004453
Paul Menagecc31edc2008-10-18 20:28:04 -07004454 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004455 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004456
Paul Menagecc31edc2008-10-18 20:28:04 -07004457 while (index < end) {
4458 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004459 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004460 index = mid;
4461 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004462 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004463 index = mid + 1;
4464 else
4465 end = mid;
4466 }
4467 }
4468 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004469 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004470 return NULL;
4471 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004472 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004473 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004474 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004475}
4476
Ben Blum102a7752009-09-23 15:56:26 -07004477static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004478{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004479 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004480 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004481
Tejun Heo5d224442013-12-05 12:28:04 -05004482 if (l)
4483 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004484 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004485 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004486}
4487
Ben Blum102a7752009-09-23 15:56:26 -07004488static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004489{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004490 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004491 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004492 pid_t *p = v;
4493 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004494 /*
4495 * Advance to the next pid in the array. If this goes off the
4496 * end, we're done
4497 */
4498 p++;
4499 if (p >= end) {
4500 return NULL;
4501 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004502 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004503 return p;
4504 }
4505}
4506
Ben Blum102a7752009-09-23 15:56:26 -07004507static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004508{
Joe Perches94ff2122015-04-15 16:18:20 -07004509 seq_printf(s, "%d\n", *(int *)v);
4510
4511 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004512}
4513
Tejun Heo182446d2013-08-08 20:11:24 -04004514static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4515 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004516{
Tejun Heo182446d2013-08-08 20:11:24 -04004517 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004518}
4519
Tejun Heo182446d2013-08-08 20:11:24 -04004520static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4521 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004522{
Paul Menage6379c102008-07-25 01:47:01 -07004523 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004524 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004525 else
Tejun Heo182446d2013-08-08 20:11:24 -04004526 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004527 return 0;
4528}
4529
Tejun Heo182446d2013-08-08 20:11:24 -04004530static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4531 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004532{
Tejun Heo182446d2013-08-08 20:11:24 -04004533 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004534}
4535
Tejun Heo182446d2013-08-08 20:11:24 -04004536static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4537 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004538{
4539 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004540 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004541 else
Tejun Heo182446d2013-08-08 20:11:24 -04004542 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004543 return 0;
4544}
4545
Tejun Heoa14c6872014-07-15 11:05:09 -04004546/* cgroup core interface files for the default hierarchy */
4547static struct cftype cgroup_dfl_base_files[] = {
4548 {
4549 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004550 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004551 .seq_start = cgroup_pidlist_start,
4552 .seq_next = cgroup_pidlist_next,
4553 .seq_stop = cgroup_pidlist_stop,
4554 .seq_show = cgroup_pidlist_show,
4555 .private = CGROUP_FILE_PROCS,
4556 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004557 },
4558 {
4559 .name = "cgroup.controllers",
4560 .flags = CFTYPE_ONLY_ON_ROOT,
4561 .seq_show = cgroup_root_controllers_show,
4562 },
4563 {
4564 .name = "cgroup.controllers",
4565 .flags = CFTYPE_NOT_ON_ROOT,
4566 .seq_show = cgroup_controllers_show,
4567 },
4568 {
4569 .name = "cgroup.subtree_control",
4570 .seq_show = cgroup_subtree_control_show,
4571 .write = cgroup_subtree_control_write,
4572 },
4573 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004574 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004575 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004576 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004577 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004578 },
4579 { } /* terminate */
4580};
4581
4582/* cgroup core interface files for the legacy hierarchies */
4583static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004584 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004585 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004586 .seq_start = cgroup_pidlist_start,
4587 .seq_next = cgroup_pidlist_next,
4588 .seq_stop = cgroup_pidlist_stop,
4589 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004590 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004591 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004592 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004593 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004594 .name = "cgroup.clone_children",
4595 .read_u64 = cgroup_clone_children_read,
4596 .write_u64 = cgroup_clone_children_write,
4597 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004598 {
Tejun Heo873fe092013-04-14 20:15:26 -07004599 .name = "cgroup.sane_behavior",
4600 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004601 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004602 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004603 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004604 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004605 .seq_start = cgroup_pidlist_start,
4606 .seq_next = cgroup_pidlist_next,
4607 .seq_stop = cgroup_pidlist_stop,
4608 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004609 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004610 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004611 },
4612 {
4613 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004614 .read_u64 = cgroup_read_notify_on_release,
4615 .write_u64 = cgroup_write_notify_on_release,
4616 },
Tejun Heo873fe092013-04-14 20:15:26 -07004617 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004618 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004619 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004620 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004621 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004622 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004623 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004624 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004625};
4626
Tejun Heo0c21ead2013-08-13 20:22:51 -04004627/*
4628 * css destruction is four-stage process.
4629 *
4630 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4631 * Implemented in kill_css().
4632 *
4633 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004634 * and thus css_tryget_online() is guaranteed to fail, the css can be
4635 * offlined by invoking offline_css(). After offlining, the base ref is
4636 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004637 *
4638 * 3. When the percpu_ref reaches zero, the only possible remaining
4639 * accessors are inside RCU read sections. css_release() schedules the
4640 * RCU callback.
4641 *
4642 * 4. After the grace period, the css can be freed. Implemented in
4643 * css_free_work_fn().
4644 *
4645 * It is actually hairier because both step 2 and 4 require process context
4646 * and thus involve punting to css->destroy_work adding two additional
4647 * steps to the already complex sequence.
4648 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004649static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004650{
4651 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004652 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004653 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004654 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004655
Tejun Heo9a1049d2014-06-28 08:10:14 -04004656 percpu_ref_exit(&css->refcnt);
4657
Vladimir Davydov01e58652015-02-12 14:59:26 -08004658 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004659 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004660 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004661 int id = css->id;
4662
Vladimir Davydov01e58652015-02-12 14:59:26 -08004663 ss->css_free(css);
4664 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004665 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004666
4667 if (parent)
4668 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004669 } else {
4670 /* cgroup free path */
4671 atomic_dec(&cgrp->root->nr_cgrps);
4672 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004673 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004674
Tejun Heod51f39b2014-05-16 13:22:48 -04004675 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004676 /*
4677 * We get a ref to the parent, and put the ref when
4678 * this cgroup is being freed, so it's guaranteed
4679 * that the parent won't be destroyed before its
4680 * children.
4681 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004682 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004683 kernfs_put(cgrp->kn);
4684 kfree(cgrp);
4685 } else {
4686 /*
4687 * This is root cgroup's refcnt reaching zero,
4688 * which indicates that the root should be
4689 * released.
4690 */
4691 cgroup_destroy_root(cgrp->root);
4692 }
4693 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004694}
4695
4696static void css_free_rcu_fn(struct rcu_head *rcu_head)
4697{
4698 struct cgroup_subsys_state *css =
4699 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4700
Tejun Heo0c21ead2013-08-13 20:22:51 -04004701 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004702 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004703}
4704
Tejun Heo25e15d82014-05-14 09:15:02 -04004705static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004706{
4707 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004708 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004709 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004710 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004711
Tejun Heo1fed1b22014-05-16 13:22:49 -04004712 mutex_lock(&cgroup_mutex);
4713
Tejun Heode3f0342014-05-16 13:22:49 -04004714 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004715 list_del_rcu(&css->sibling);
4716
Tejun Heo9d755d32014-05-14 09:15:02 -04004717 if (ss) {
4718 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004719 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004720 if (ss->css_released)
4721 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004722 } else {
4723 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004724 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4725 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004726
4727 /*
4728 * There are two control paths which try to determine
4729 * cgroup from dentry without going through kernfs -
4730 * cgroupstats_build() and css_tryget_online_from_dir().
4731 * Those are supported by RCU protecting clearing of
4732 * cgrp->kn->priv backpointer.
4733 */
4734 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, 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
Li Zefand7eeac12013-03-12 15:35:59 -07004805 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004806 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004807
Tejun Heoeb954192013-08-08 20:11:23 -04004808 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004809 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004810
4811 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004812}
4813
Tejun Heoc81c925a2013-12-06 15:11:56 -05004814/**
4815 * create_css - create a cgroup_subsys_state
4816 * @cgrp: the cgroup new css will be associated with
4817 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004818 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004819 *
4820 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004821 * css is online and installed in @cgrp with all interface files created if
4822 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004823 */
Tejun Heof63070d2014-07-08 18:02:57 -04004824static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4825 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004826{
Tejun Heod51f39b2014-05-16 13:22:48 -04004827 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004828 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004829 struct cgroup_subsys_state *css;
4830 int err;
4831
Tejun Heoc81c925a2013-12-06 15:11:56 -05004832 lockdep_assert_held(&cgroup_mutex);
4833
Tejun Heo1fed1b22014-05-16 13:22:49 -04004834 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004835 if (IS_ERR(css))
4836 return PTR_ERR(css);
4837
Tejun Heoddfcada2014-05-04 15:09:14 -04004838 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004839
Tejun Heo2aad2a82014-09-24 13:31:50 -04004840 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004841 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004842 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004843
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004844 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004845 if (err < 0)
4846 goto err_free_percpu_ref;
4847 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004848
Tejun Heof63070d2014-07-08 18:02:57 -04004849 if (visible) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04004850 err = css_populate_dir(css, NULL);
Tejun Heof63070d2014-07-08 18:02:57 -04004851 if (err)
4852 goto err_free_id;
4853 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004854
4855 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004856 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004857 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004858
4859 err = online_css(css);
4860 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004861 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004862
Tejun Heoc81c925a2013-12-06 15:11:56 -05004863 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004864 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004865 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 -04004866 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004867 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004868 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004869 ss->warned_broken_hierarchy = true;
4870 }
4871
4872 return 0;
4873
Tejun Heo1fed1b22014-05-16 13:22:49 -04004874err_list_del:
4875 list_del_rcu(&css->sibling);
Tejun Heo4df8dc92015-09-18 17:54:23 -04004876 css_clear_dir(css, NULL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004877err_free_id:
4878 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004879err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004880 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004881err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004882 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004883 return err;
4884}
4885
Tejun Heob3bfd982014-05-13 12:19:22 -04004886static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4887 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004888{
Tejun Heob11cfb52015-11-20 15:55:52 -05004889 struct cgroup *parent, *cgrp, *tcgrp;
Tejun Heoa9746d82014-05-13 12:19:22 -04004890 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004891 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004892 struct kernfs_node *kn;
Tejun Heob11cfb52015-11-20 15:55:52 -05004893 int level, ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004894
Alban Crequy71b1fb52014-08-18 12:20:20 +01004895 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4896 */
4897 if (strchr(name, '\n'))
4898 return -EINVAL;
4899
Tejun Heoa9746d82014-05-13 12:19:22 -04004900 parent = cgroup_kn_lock_live(parent_kn);
4901 if (!parent)
4902 return -ENODEV;
4903 root = parent->root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004904 level = parent->level + 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004905
Tejun Heo0a950f62012-11-19 09:02:12 -08004906 /* allocate the cgroup and its ID, 0 is reserved for the root */
Tejun Heob11cfb52015-11-20 15:55:52 -05004907 cgrp = kzalloc(sizeof(*cgrp) +
4908 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004909 if (!cgrp) {
4910 ret = -ENOMEM;
4911 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004912 }
4913
Tejun Heo2aad2a82014-09-24 13:31:50 -04004914 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004915 if (ret)
4916 goto out_free_cgrp;
4917
Li Zefan0ab02ca2014-02-11 16:05:46 +08004918 /*
4919 * Temporarily set the pointer to NULL, so idr_find() won't return
4920 * a half-baked cgroup.
4921 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004922 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004923 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004924 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004925 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004926 }
4927
Paul Menagecc31edc2008-10-18 20:28:04 -07004928 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004929
Tejun Heo9d800df2014-05-14 09:15:00 -04004930 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004931 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004932 cgrp->level = level;
4933
4934 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
4935 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004936
Li Zefanb6abdb02008-03-04 14:28:19 -08004937 if (notify_on_release(parent))
4938 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4939
Tejun Heo2260e7f2012-11-19 08:13:38 -08004940 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4941 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004942
Tejun Heo2bd59d42014-02-11 11:52:49 -05004943 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004944 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004945 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004946 ret = PTR_ERR(kn);
4947 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004948 }
4949 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950
Tejun Heo6f305582014-02-12 09:29:50 -05004951 /*
4952 * This extra ref will be put in cgroup_free_fn() and guarantees
4953 * that @cgrp->kn is always accessible.
4954 */
4955 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004956
Tejun Heo0cb51d72014-05-16 13:22:49 -04004957 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004958
Tejun Heo4e139af2012-11-19 08:13:36 -08004959 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004960 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004961 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004962 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004963
Tejun Heo0d802552013-12-06 15:11:56 -05004964 /*
4965 * @cgrp is now fully operational. If something fails after this
4966 * point, it'll be released via the normal destruction path.
4967 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004968 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004969
Tejun Heoba0f4d72014-05-13 12:19:22 -04004970 ret = cgroup_kn_set_ugid(kn);
4971 if (ret)
4972 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004973
Tejun Heo4df8dc92015-09-18 17:54:23 -04004974 ret = css_populate_dir(&cgrp->self, NULL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004975 if (ret)
4976 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004977
Tejun Heo9d403e92013-12-06 15:11:56 -05004978 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004979 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004980 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004981 ret = create_css(cgrp, ss,
4982 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004983 if (ret)
4984 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004985 }
Tejun Heoa8638032012-11-09 09:12:29 -08004986 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004987
Tejun Heobd53d612014-04-23 11:13:16 -04004988 /*
4989 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004990 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004991 */
Tejun Heo667c2492014-07-08 18:02:56 -04004992 if (!cgroup_on_dfl(cgrp)) {
4993 cgrp->subtree_control = parent->subtree_control;
4994 cgroup_refresh_child_subsys_mask(cgrp);
4995 }
Tejun Heof392e512014-04-23 11:13:14 -04004996
Tejun Heo2bd59d42014-02-11 11:52:49 -05004997 kernfs_activate(kn);
4998
Tejun Heoba0f4d72014-05-13 12:19:22 -04004999 ret = 0;
5000 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005001
Tejun Heoba0f4d72014-05-13 12:19:22 -04005002out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04005003 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04005004out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04005005 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005006out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07005007 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005008out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005009 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005010 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04005011
5012out_destroy:
5013 cgroup_destroy_locked(cgrp);
5014 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005015}
5016
Tejun Heo223dbc32013-08-13 20:22:50 -04005017/*
5018 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005019 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5020 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005021 */
5022static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005023{
Tejun Heo223dbc32013-08-13 20:22:50 -04005024 struct cgroup_subsys_state *css =
5025 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005026
Tejun Heof20104d2013-08-13 20:22:50 -04005027 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005028
Tejun Heoaa226ff2016-01-21 15:31:11 -05005029 do {
5030 offline_css(css);
5031 css_put(css);
5032 /* @css can't go away while we're holding cgroup_mutex */
5033 css = css->parent;
5034 } while (css && atomic_dec_and_test(&css->online_cnt));
5035
5036 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005037}
5038
Tejun Heo223dbc32013-08-13 20:22:50 -04005039/* css kill confirmation processing requires process context, bounce */
5040static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005041{
5042 struct cgroup_subsys_state *css =
5043 container_of(ref, struct cgroup_subsys_state, refcnt);
5044
Tejun Heoaa226ff2016-01-21 15:31:11 -05005045 if (atomic_dec_and_test(&css->online_cnt)) {
5046 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5047 queue_work(cgroup_destroy_wq, &css->destroy_work);
5048 }
Tejun Heod3daf282013-06-13 19:39:16 -07005049}
5050
Tejun Heof392e512014-04-23 11:13:14 -04005051/**
5052 * kill_css - destroy a css
5053 * @css: css to destroy
5054 *
5055 * This function initiates destruction of @css by removing cgroup interface
5056 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005057 * asynchronously once css_tryget_online() is guaranteed to fail and when
5058 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005059 */
5060static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005061{
Tejun Heo01f64742014-05-13 12:19:23 -04005062 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005063
Tejun Heo2bd59d42014-02-11 11:52:49 -05005064 /*
5065 * This must happen before css is disassociated with its cgroup.
5066 * See seq_css() for details.
5067 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005068 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005069
Tejun Heoedae0c32013-08-13 20:22:51 -04005070 /*
5071 * Killing would put the base ref, but we need to keep it alive
5072 * until after ->css_offline().
5073 */
5074 css_get(css);
5075
5076 /*
5077 * cgroup core guarantees that, by the time ->css_offline() is
5078 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005079 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005080 * proceed to offlining css's because percpu_ref_kill() doesn't
5081 * guarantee that the ref is seen as killed on all CPUs on return.
5082 *
5083 * Use percpu_ref_kill_and_confirm() to get notifications as each
5084 * css is confirmed to be seen as killed on all CPUs.
5085 */
5086 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005087}
5088
5089/**
5090 * cgroup_destroy_locked - the first stage of cgroup destruction
5091 * @cgrp: cgroup to be destroyed
5092 *
5093 * css's make use of percpu refcnts whose killing latency shouldn't be
5094 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005095 * guarantee that css_tryget_online() won't succeed by the time
5096 * ->css_offline() is invoked. To satisfy all the requirements,
5097 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005098 *
5099 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5100 * userland visible parts and start killing the percpu refcnts of
5101 * css's. Set up so that the next stage will be kicked off once all
5102 * the percpu refcnts are confirmed to be killed.
5103 *
5104 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5105 * rest of destruction. Once all cgroup references are gone, the
5106 * cgroup is RCU-freed.
5107 *
5108 * This function implements s1. After this step, @cgrp is gone as far as
5109 * the userland is concerned and a new cgroup with the same name may be
5110 * created. As cgroup doesn't care about the names internally, this
5111 * doesn't cause any problem.
5112 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005113static int cgroup_destroy_locked(struct cgroup *cgrp)
5114 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005115{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005116 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005117 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005118
Tejun Heo42809dd2012-11-19 08:13:37 -08005119 lockdep_assert_held(&cgroup_mutex);
5120
Tejun Heo91486f62015-10-15 16:41:51 -04005121 /*
5122 * Only migration can raise populated from zero and we're already
5123 * holding cgroup_mutex.
5124 */
5125 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005126 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005127
Tejun Heo1a90dd52012-11-05 09:16:59 -08005128 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005129 * Make sure there's no live children. We can't test emptiness of
5130 * ->self.children as dead children linger on it while being
5131 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005132 */
Tejun Heof3d46502014-05-16 13:22:52 -04005133 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005134 return -EBUSY;
5135
5136 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005137 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005138 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005139 */
Tejun Heo184faf32014-05-16 13:22:51 -04005140 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005141
Tejun Heo249f3462014-05-14 09:15:01 -04005142 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005143 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005144 kill_css(css);
5145
Tejun Heo455050d2013-06-13 19:27:41 -07005146 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005147 * Remove @cgrp directory along with the base files. @cgrp has an
5148 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005149 */
Tejun Heo01f64742014-05-13 12:19:23 -04005150 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005151
Tejun Heod51f39b2014-05-16 13:22:48 -04005152 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005153
Tejun Heo249f3462014-05-14 09:15:01 -04005154 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005155 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005156
Tejun Heoea15f8c2013-06-13 19:27:42 -07005157 return 0;
5158};
5159
Tejun Heo2bd59d42014-02-11 11:52:49 -05005160static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005161{
Tejun Heoa9746d82014-05-13 12:19:22 -04005162 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005163 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005164
Tejun Heoa9746d82014-05-13 12:19:22 -04005165 cgrp = cgroup_kn_lock_live(kn);
5166 if (!cgrp)
5167 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005168
Tejun Heoa9746d82014-05-13 12:19:22 -04005169 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005170
Tejun Heoa9746d82014-05-13 12:19:22 -04005171 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005172 return ret;
5173}
5174
Tejun Heo2bd59d42014-02-11 11:52:49 -05005175static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5176 .remount_fs = cgroup_remount,
5177 .show_options = cgroup_show_options,
5178 .mkdir = cgroup_mkdir,
5179 .rmdir = cgroup_rmdir,
5180 .rename = cgroup_rename,
5181};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005182
Tejun Heo15a4c832014-05-04 15:09:14 -04005183static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005184{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005185 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005186
Tejun Heoa5ae9892015-12-29 14:53:56 -05005187 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005188
Tejun Heo648bb562012-11-19 08:13:36 -08005189 mutex_lock(&cgroup_mutex);
5190
Tejun Heo15a4c832014-05-04 15:09:14 -04005191 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005192 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005193
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005194 /* Create the root cgroup state for this subsystem */
5195 ss->root = &cgrp_dfl_root;
5196 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005197 /* We don't handle early failures gracefully */
5198 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005199 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005200
5201 /*
5202 * Root csses are never destroyed and we can't initialize
5203 * percpu_ref during early init. Disable refcnting.
5204 */
5205 css->flags |= CSS_NO_REF;
5206
Tejun Heo15a4c832014-05-04 15:09:14 -04005207 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005208 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005209 css->id = 1;
5210 } else {
5211 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5212 BUG_ON(css->id < 0);
5213 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005214
Li Zefane8d55fd2008-04-29 01:00:13 -07005215 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005216 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005217 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005218 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005219 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005220
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005221 have_fork_callback |= (bool)ss->fork << ss->id;
5222 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005223 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005224 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005225
Li Zefane8d55fd2008-04-29 01:00:13 -07005226 /* At system boot, before all subsystems have been
5227 * registered, no tasks have been forked, so we don't
5228 * need to invoke fork callbacks here. */
5229 BUG_ON(!list_empty(&init_task.tasks));
5230
Tejun Heoae7f1642013-08-13 20:22:50 -04005231 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005232
Tejun Heo648bb562012-11-19 08:13:36 -08005233 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005234}
5235
5236/**
Li Zefana043e3b2008-02-23 15:24:09 -08005237 * cgroup_init_early - cgroup initialization at system boot
5238 *
5239 * Initialize cgroups at system boot, and initialize any
5240 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005241 */
5242int __init cgroup_init_early(void)
5243{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005244 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005245 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005246 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005247
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005248 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005249 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5250
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005251 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005252
Tejun Heo3ed80a62014-02-08 10:36:58 -05005253 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005254 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05005255 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5256 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005257 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005258 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5259 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005260
Tejun Heoaec25022014-02-08 10:36:58 -05005261 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005262 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005263 if (!ss->legacy_name)
5264 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005265
5266 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005267 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005268 }
5269 return 0;
5270}
5271
Tejun Heoa3e72732015-09-25 16:24:27 -04005272static unsigned long cgroup_disable_mask __initdata;
5273
Paul Menageddbcc7e2007-10-18 23:39:30 -07005274/**
Li Zefana043e3b2008-02-23 15:24:09 -08005275 * cgroup_init - cgroup initialization
5276 *
5277 * Register cgroup filesystem and /proc file, and initialize
5278 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005279 */
5280int __init cgroup_init(void)
5281{
Tejun Heo30159ec2013-06-25 11:53:37 -07005282 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005283 unsigned long key;
Tejun Heo035f4f52015-10-15 17:00:43 -04005284 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005285
Tejun Heo1ed13282015-09-16 12:53:17 -04005286 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005287 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5288 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005289
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005290 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005291
Tejun Heo82fe9b02013-06-25 11:53:37 -07005292 /* Add init_css_set to the hash table */
5293 key = css_set_hash(init_css_set.subsys);
5294 hash_add(css_set_table, &init_css_set.hlist, key);
5295
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005296 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005297
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005298 mutex_unlock(&cgroup_mutex);
5299
Tejun Heo172a2c062014-03-19 10:23:53 -04005300 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005301 if (ss->early_init) {
5302 struct cgroup_subsys_state *css =
5303 init_css_set.subsys[ss->id];
5304
5305 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5306 GFP_KERNEL);
5307 BUG_ON(css->id < 0);
5308 } else {
5309 cgroup_init_subsys(ss, false);
5310 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005311
Tejun Heo2d8f2432014-04-23 11:13:15 -04005312 list_add_tail(&init_css_set.e_cset_node[ssid],
5313 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005314
5315 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005316 * Setting dfl_root subsys_mask needs to consider the
5317 * disabled flag and cftype registration needs kmalloc,
5318 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005319 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005320 if (cgroup_disable_mask & (1 << ssid)) {
5321 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5322 printk(KERN_INFO "Disabling %s control group subsystem\n",
5323 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005324 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005325 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005326
5327 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5328
Tejun Heo5de4fa12014-07-15 11:05:10 -04005329 if (!ss->dfl_cftypes)
5330 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5331
Tejun Heoa8ddc822014-07-15 11:05:10 -04005332 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5333 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5334 } else {
5335 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5336 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005337 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005338
5339 if (ss->bind)
5340 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005341 }
Greg KH676db4a2010-08-05 13:53:35 -07005342
Tejun Heo035f4f52015-10-15 17:00:43 -04005343 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5344 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005345 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005346 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005347
Tejun Heo2bd59d42014-02-11 11:52:49 -05005348 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005349}
Paul Menageb4f48b62007-10-18 23:39:33 -07005350
Tejun Heoe5fca242013-11-22 17:14:39 -05005351static int __init cgroup_wq_init(void)
5352{
5353 /*
5354 * There isn't much point in executing destruction path in
5355 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005356 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005357 *
5358 * We would prefer to do this in cgroup_init() above, but that
5359 * is called before init_workqueues(): so leave this until after.
5360 */
Tejun Heo1a115332014-02-12 19:06:19 -05005361 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005362 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005363
5364 /*
5365 * Used to destroy pidlists and separate to serve as flush domain.
5366 * Cap @max_active to 1 too.
5367 */
5368 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5369 0, 1);
5370 BUG_ON(!cgroup_pidlist_destroy_wq);
5371
Tejun Heoe5fca242013-11-22 17:14:39 -05005372 return 0;
5373}
5374core_initcall(cgroup_wq_init);
5375
Paul Menagea4243162007-10-18 23:39:35 -07005376/*
5377 * proc_cgroup_show()
5378 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5379 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005380 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005381int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5382 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005383{
Tejun Heoe61734c2014-02-12 09:29:50 -05005384 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005385 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005386 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005387
5388 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005389 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005390 if (!buf)
5391 goto out;
5392
Paul Menagea4243162007-10-18 23:39:35 -07005393 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005394 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005395
Tejun Heo985ed672014-03-19 10:23:53 -04005396 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005397 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005398 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005399 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005400
Tejun Heoa2dd4242014-03-19 10:23:55 -04005401 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005402 continue;
5403
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005404 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005405 if (root != &cgrp_dfl_root)
5406 for_each_subsys(ss, ssid)
5407 if (root->subsys_mask & (1 << ssid))
5408 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005409 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005410 if (strlen(root->name))
5411 seq_printf(m, "%sname=%s", count ? "," : "",
5412 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005413 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005414
Paul Menage7717f7b2009-09-23 15:56:22 -07005415 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005416
5417 /*
5418 * On traditional hierarchies, all zombie tasks show up as
5419 * belonging to the root cgroup. On the default hierarchy,
5420 * while a zombie doesn't show up in "cgroup.procs" and
5421 * thus can't be migrated, its /proc/PID/cgroup keeps
5422 * reporting the cgroup it belonged to before exiting. If
5423 * the cgroup is removed before the zombie is reaped,
5424 * " (deleted)" is appended to the cgroup path.
5425 */
5426 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5427 path = cgroup_path(cgrp, buf, PATH_MAX);
5428 if (!path) {
5429 retval = -ENAMETOOLONG;
5430 goto out_unlock;
5431 }
5432 } else {
5433 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005434 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005435
Tejun Heoe61734c2014-02-12 09:29:50 -05005436 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005437
5438 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5439 seq_puts(m, " (deleted)\n");
5440 else
5441 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005442 }
5443
Zefan Li006f4ac2014-09-18 16:03:15 +08005444 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005445out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005446 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005447 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005448 kfree(buf);
5449out:
5450 return retval;
5451}
5452
Paul Menagea4243162007-10-18 23:39:35 -07005453/* Display information about each subsystem and each hierarchy */
5454static int proc_cgroupstats_show(struct seq_file *m, void *v)
5455{
Tejun Heo30159ec2013-06-25 11:53:37 -07005456 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005457 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005458
Paul Menage8bab8dd2008-04-04 14:29:57 -07005459 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005460 /*
5461 * ideally we don't want subsystems moving around while we do this.
5462 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5463 * subsys/hierarchy state.
5464 */
Paul Menagea4243162007-10-18 23:39:35 -07005465 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005466
5467 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005468 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005469 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005470 atomic_read(&ss->root->nr_cgrps),
5471 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005472
Paul Menagea4243162007-10-18 23:39:35 -07005473 mutex_unlock(&cgroup_mutex);
5474 return 0;
5475}
5476
5477static int cgroupstats_open(struct inode *inode, struct file *file)
5478{
Al Viro9dce07f2008-03-29 03:07:28 +00005479 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005480}
5481
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005482static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005483 .open = cgroupstats_open,
5484 .read = seq_read,
5485 .llseek = seq_lseek,
5486 .release = single_release,
5487};
5488
Paul Menageb4f48b62007-10-18 23:39:33 -07005489/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005490 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005491 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005492 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005493 * A task is associated with the init_css_set until cgroup_post_fork()
5494 * attaches it to the parent's css_set. Empty cg_list indicates that
5495 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005496 */
5497void cgroup_fork(struct task_struct *child)
5498{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005499 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005500 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005501}
5502
5503/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005504 * cgroup_can_fork - called on a new task before the process is exposed
5505 * @child: the task in question.
5506 *
5507 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5508 * returns an error, the fork aborts with that error code. This allows for
5509 * a cgroup subsystem to conditionally allow or deny new forks.
5510 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005511int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005512{
5513 struct cgroup_subsys *ss;
5514 int i, j, ret;
5515
5516 for_each_subsys_which(ss, i, &have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005517 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005518 if (ret)
5519 goto out_revert;
5520 }
5521
5522 return 0;
5523
5524out_revert:
5525 for_each_subsys(ss, j) {
5526 if (j >= i)
5527 break;
5528 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005529 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005530 }
5531
5532 return ret;
5533}
5534
5535/**
5536 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5537 * @child: the task in question
5538 *
5539 * This calls the cancel_fork() callbacks if a fork failed *after*
5540 * cgroup_can_fork() succeded.
5541 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005542void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005543{
5544 struct cgroup_subsys *ss;
5545 int i;
5546
5547 for_each_subsys(ss, i)
5548 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005549 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005550}
5551
5552/**
Li Zefana043e3b2008-02-23 15:24:09 -08005553 * cgroup_post_fork - called on a new task after adding it to the task list
5554 * @child: the task in question
5555 *
Tejun Heo5edee612012-10-16 15:03:14 -07005556 * Adds the task to the list running through its css_set if necessary and
5557 * call the subsystem fork() callbacks. Has to be after the task is
5558 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005559 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005560 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005561 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005562void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005563{
Tejun Heo30159ec2013-06-25 11:53:37 -07005564 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005565 int i;
5566
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005567 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005568 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005569 * function sets use_task_css_set_links before grabbing
5570 * tasklist_lock and we just went through tasklist_lock to add
5571 * @child, it's guaranteed that either we see the set
5572 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5573 * @child during its iteration.
5574 *
5575 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005576 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005577 * association is stable, and, on completion of the parent's
5578 * migration, @child is visible in the source of migration or
5579 * already in the destination cgroup. This guarantee is necessary
5580 * when implementing operations which need to migrate all tasks of
5581 * a cgroup to another.
5582 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005583 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005584 * will remain in init_css_set. This is safe because all tasks are
5585 * in the init_css_set before cg_links is enabled and there's no
5586 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005587 */
Paul Menage817929e2007-10-18 23:39:36 -07005588 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005589 struct css_set *cset;
5590
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005591 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005592 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005593 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005594 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005595 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005596 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005597 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005598 }
Tejun Heo5edee612012-10-16 15:03:14 -07005599
5600 /*
5601 * Call ss->fork(). This must happen after @child is linked on
5602 * css_set; otherwise, @child might change state between ->fork()
5603 * and addition to css_set.
5604 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005605 for_each_subsys_which(ss, i, &have_fork_callback)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005606 ss->fork(child);
Paul Menage817929e2007-10-18 23:39:36 -07005607}
Tejun Heo5edee612012-10-16 15:03:14 -07005608
Paul Menage817929e2007-10-18 23:39:36 -07005609/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005610 * cgroup_exit - detach cgroup from exiting task
5611 * @tsk: pointer to task_struct of exiting process
5612 *
5613 * Description: Detach cgroup from @tsk and release it.
5614 *
5615 * Note that cgroups marked notify_on_release force every task in
5616 * them to take the global cgroup_mutex mutex when exiting.
5617 * This could impact scaling on very large systems. Be reluctant to
5618 * use notify_on_release cgroups where very high task exit scaling
5619 * is required on large systems.
5620 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005621 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5622 * call cgroup_exit() while the task is still competent to handle
5623 * notify_on_release(), then leave the task attached to the root cgroup in
5624 * each hierarchy for the remainder of its exit. No need to bother with
5625 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005626 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005627 */
Li Zefan1ec41832014-03-28 15:22:19 +08005628void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005629{
Tejun Heo30159ec2013-06-25 11:53:37 -07005630 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005631 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005632 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005633
5634 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005635 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005636 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005637 */
Tejun Heo0de09422015-10-15 16:41:49 -04005638 cset = task_css_set(tsk);
5639
Paul Menage817929e2007-10-18 23:39:36 -07005640 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005641 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005642 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005643 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005644 } else {
5645 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005646 }
5647
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005648 /* see cgroup_post_fork() for details */
Tejun Heo2e91fa72015-10-15 16:41:53 -04005649 for_each_subsys_which(ss, i, &have_exit_callback)
5650 ss->exit(tsk);
5651}
Tejun Heo30159ec2013-06-25 11:53:37 -07005652
Tejun Heo2e91fa72015-10-15 16:41:53 -04005653void cgroup_free(struct task_struct *task)
5654{
5655 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005656 struct cgroup_subsys *ss;
5657 int ssid;
5658
5659 for_each_subsys_which(ss, ssid, &have_free_callback)
5660 ss->free(task);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005661
Tejun Heo2e91fa72015-10-15 16:41:53 -04005662 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005663}
Paul Menage697f4162007-10-18 23:39:34 -07005664
Paul Menagebd89aab2007-10-18 23:40:44 -07005665static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005666{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005667 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005668 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5669 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005670}
5671
Paul Menage81a6a5c2007-10-18 23:39:38 -07005672/*
5673 * Notify userspace when a cgroup is released, by running the
5674 * configured release agent with the name of the cgroup (path
5675 * relative to the root of cgroup file system) as the argument.
5676 *
5677 * Most likely, this user command will try to rmdir this cgroup.
5678 *
5679 * This races with the possibility that some other task will be
5680 * attached to this cgroup before it is removed, or that some other
5681 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5682 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5683 * unused, and this cgroup will be reprieved from its death sentence,
5684 * to continue to serve a useful existence. Next time it's released,
5685 * we will get notified again, if it still has 'notify_on_release' set.
5686 *
5687 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5688 * means only wait until the task is successfully execve()'d. The
5689 * separate release agent task is forked by call_usermodehelper(),
5690 * then control in this thread returns here, without waiting for the
5691 * release agent task. We don't bother to wait because the caller of
5692 * this routine has no use for the exit status of the release agent
5693 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005694 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005695static void cgroup_release_agent(struct work_struct *work)
5696{
Zefan Li971ff492014-09-18 16:06:19 +08005697 struct cgroup *cgrp =
5698 container_of(work, struct cgroup, release_agent_work);
5699 char *pathbuf = NULL, *agentbuf = NULL, *path;
5700 char *argv[3], *envp[3];
5701
Paul Menage81a6a5c2007-10-18 23:39:38 -07005702 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005703
Zefan Li971ff492014-09-18 16:06:19 +08005704 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5705 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5706 if (!pathbuf || !agentbuf)
5707 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005708
Zefan Li971ff492014-09-18 16:06:19 +08005709 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5710 if (!path)
5711 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005712
Zefan Li971ff492014-09-18 16:06:19 +08005713 argv[0] = agentbuf;
5714 argv[1] = path;
5715 argv[2] = NULL;
5716
5717 /* minimal command environment */
5718 envp[0] = "HOME=/";
5719 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5720 envp[2] = NULL;
5721
Paul Menage81a6a5c2007-10-18 23:39:38 -07005722 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005723 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005724 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005725out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005726 mutex_unlock(&cgroup_mutex);
5727out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005728 kfree(agentbuf);
5729 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005730}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005731
5732static int __init cgroup_disable(char *str)
5733{
Tejun Heo30159ec2013-06-25 11:53:37 -07005734 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005735 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005736 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005737
5738 while ((token = strsep(&str, ",")) != NULL) {
5739 if (!*token)
5740 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005741
Tejun Heo3ed80a62014-02-08 10:36:58 -05005742 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005743 if (strcmp(token, ss->name) &&
5744 strcmp(token, ss->legacy_name))
5745 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005746 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005747 }
5748 }
5749 return 1;
5750}
5751__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005752
Tejun Heob77d7b62013-08-13 11:01:54 -04005753/**
Tejun Heoec903c02014-05-13 12:11:01 -04005754 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005755 * @dentry: directory dentry of interest
5756 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005757 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005758 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5759 * to get the corresponding css and return it. If such css doesn't exist
5760 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005761 */
Tejun Heoec903c02014-05-13 12:11:01 -04005762struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5763 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005764{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005765 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5766 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005767 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005768
Tejun Heo35cf0832013-08-26 18:40:56 -04005769 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005770 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5771 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005772 return ERR_PTR(-EBADF);
5773
Tejun Heo5a17f542014-02-11 11:52:47 -05005774 rcu_read_lock();
5775
Tejun Heo2bd59d42014-02-11 11:52:49 -05005776 /*
5777 * This path doesn't originate from kernfs and @kn could already
5778 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005779 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005780 */
5781 cgrp = rcu_dereference(kn->priv);
5782 if (cgrp)
5783 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005784
Tejun Heoec903c02014-05-13 12:11:01 -04005785 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005786 css = ERR_PTR(-ENOENT);
5787
5788 rcu_read_unlock();
5789 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005790}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005791
Li Zefan1cb650b2013-08-19 10:05:24 +08005792/**
5793 * css_from_id - lookup css by id
5794 * @id: the cgroup id
5795 * @ss: cgroup subsys to be looked into
5796 *
5797 * Returns the css if there's valid one with @id, otherwise returns NULL.
5798 * Should be called under rcu_read_lock().
5799 */
5800struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5801{
Tejun Heo6fa49182014-05-04 15:09:13 -04005802 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005803 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005804}
5805
Tejun Heo16af4392015-11-20 15:55:52 -05005806/**
5807 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5808 * @path: path on the default hierarchy
5809 *
5810 * Find the cgroup at @path on the default hierarchy, increment its
5811 * reference count and return it. Returns pointer to the found cgroup on
5812 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5813 * if @path points to a non-directory.
5814 */
5815struct cgroup *cgroup_get_from_path(const char *path)
5816{
5817 struct kernfs_node *kn;
5818 struct cgroup *cgrp;
5819
5820 mutex_lock(&cgroup_mutex);
5821
5822 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5823 if (kn) {
5824 if (kernfs_type(kn) == KERNFS_DIR) {
5825 cgrp = kn->priv;
5826 cgroup_get(cgrp);
5827 } else {
5828 cgrp = ERR_PTR(-ENOTDIR);
5829 }
5830 kernfs_put(kn);
5831 } else {
5832 cgrp = ERR_PTR(-ENOENT);
5833 }
5834
5835 mutex_unlock(&cgroup_mutex);
5836 return cgrp;
5837}
5838EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5839
Tejun Heobd1060a2015-12-07 17:38:53 -05005840/*
5841 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5842 * definition in cgroup-defs.h.
5843 */
5844#ifdef CONFIG_SOCK_CGROUP_DATA
5845
5846#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5847
Tejun Heo3fa4cc92015-12-14 11:24:06 -05005848DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05005849static bool cgroup_sk_alloc_disabled __read_mostly;
5850
5851void cgroup_sk_alloc_disable(void)
5852{
5853 if (cgroup_sk_alloc_disabled)
5854 return;
5855 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5856 cgroup_sk_alloc_disabled = true;
5857}
5858
5859#else
5860
5861#define cgroup_sk_alloc_disabled false
5862
5863#endif
5864
5865void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5866{
5867 if (cgroup_sk_alloc_disabled)
5868 return;
5869
5870 rcu_read_lock();
5871
5872 while (true) {
5873 struct css_set *cset;
5874
5875 cset = task_css_set(current);
5876 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5877 skcd->val = (unsigned long)cset->dfl_cgrp;
5878 break;
5879 }
5880 cpu_relax();
5881 }
5882
5883 rcu_read_unlock();
5884}
5885
5886void cgroup_sk_free(struct sock_cgroup_data *skcd)
5887{
5888 cgroup_put(sock_cgroup_ptr(skcd));
5889}
5890
5891#endif /* CONFIG_SOCK_CGROUP_DATA */
5892
Paul Menagefe693432009-09-23 15:56:20 -07005893#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005894static struct cgroup_subsys_state *
5895debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005896{
5897 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5898
5899 if (!css)
5900 return ERR_PTR(-ENOMEM);
5901
5902 return css;
5903}
5904
Tejun Heoeb954192013-08-08 20:11:23 -04005905static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005906{
Tejun Heoeb954192013-08-08 20:11:23 -04005907 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005908}
5909
Tejun Heo182446d2013-08-08 20:11:24 -04005910static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5911 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005912{
Tejun Heo182446d2013-08-08 20:11:24 -04005913 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005914}
5915
Tejun Heo182446d2013-08-08 20:11:24 -04005916static u64 current_css_set_read(struct cgroup_subsys_state *css,
5917 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005918{
5919 return (u64)(unsigned long)current->cgroups;
5920}
5921
Tejun Heo182446d2013-08-08 20:11:24 -04005922static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005923 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005924{
5925 u64 count;
5926
5927 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005928 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005929 rcu_read_unlock();
5930 return count;
5931}
5932
Tejun Heo2da8ca82013-12-05 12:28:04 -05005933static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005934{
Tejun Heo69d02062013-06-12 21:04:50 -07005935 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005936 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005937 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005938
Tejun Heoe61734c2014-02-12 09:29:50 -05005939 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5940 if (!name_buf)
5941 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005942
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005943 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005944 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005945 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005946 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005947 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005948
Tejun Heoa2dd4242014-03-19 10:23:55 -04005949 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005950 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005951 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005952 }
5953 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005954 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005955 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005956 return 0;
5957}
5958
5959#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005960static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005961{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005962 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005963 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005964
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005965 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005966 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005967 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005968 struct task_struct *task;
5969 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005970
Tejun Heo5abb8852013-06-12 21:04:49 -07005971 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005972
Tejun Heo5abb8852013-06-12 21:04:49 -07005973 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005974 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5975 goto overflow;
5976 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005977 }
Tejun Heoc7561122014-02-25 10:04:01 -05005978
5979 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5980 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5981 goto overflow;
5982 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5983 }
5984 continue;
5985 overflow:
5986 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005987 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005988 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005989 return 0;
5990}
5991
Tejun Heo182446d2013-08-08 20:11:24 -04005992static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005993{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005994 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08005995 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005996}
5997
5998static struct cftype debug_files[] = {
5999 {
Paul Menagefe693432009-09-23 15:56:20 -07006000 .name = "taskcount",
6001 .read_u64 = debug_taskcount_read,
6002 },
6003
6004 {
6005 .name = "current_css_set",
6006 .read_u64 = current_css_set_read,
6007 },
6008
6009 {
6010 .name = "current_css_set_refcount",
6011 .read_u64 = current_css_set_refcount_read,
6012 },
6013
6014 {
Paul Menage7717f7b2009-09-23 15:56:22 -07006015 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006016 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006017 },
6018
6019 {
6020 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006021 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006022 },
6023
6024 {
Paul Menagefe693432009-09-23 15:56:20 -07006025 .name = "releasable",
6026 .read_u64 = releasable_read,
6027 },
Paul Menagefe693432009-09-23 15:56:20 -07006028
Tejun Heo4baf6e32012-04-01 12:09:55 -07006029 { } /* terminate */
6030};
Paul Menagefe693432009-09-23 15:56:20 -07006031
Tejun Heo073219e2014-02-08 10:36:58 -05006032struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006033 .css_alloc = debug_css_alloc,
6034 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04006035 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07006036};
6037#endif /* CONFIG_CGROUP_DEBUG */