blob: c38c3a229d14df6350f1183b6ea239fb7833fcb9 [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 Heo96d365e2014-02-13 06:58:40 -050048#include <linux/rwsem.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040049#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070050#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070051#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070052#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080055#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020059#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050060#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070061
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.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 Heo0e1d7682014-02-25 10:04:03 -050079 * css_set_rwsem protects task->cgroups pointer, the list of css_set
80 * 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 Heo0e1d7682014-02-25 10:04:03 -050087DECLARE_RWSEM(css_set_rwsem);
88EXPORT_SYMBOL_GPL(cgroup_mutex);
89EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070090#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070091static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050092static DECLARE_RWSEM(css_set_rwsem);
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 Heo69e943b2014-02-08 10:36:58 -0500102 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
103 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
104 */
105static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700106
Tejun Heo1ed13282015-09-16 12:53:17 -0400107struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
108
Tejun Heo8353da12014-05-13 12:19:23 -0400109#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700110 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
111 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400112 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500113
Ben Blumaae8aab2010-03-10 15:22:07 -0800114/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500115 * cgroup destruction makes heavy use of work items and there can be a lot
116 * of concurrent destructions. Use a separate workqueue so that cgroup
117 * destruction work items don't end up filling up max_active of system_wq
118 * which may lead to deadlock.
119 */
120static struct workqueue_struct *cgroup_destroy_wq;
121
122/*
Tejun Heob1a21362013-11-29 10:42:58 -0500123 * pidlist destructions need to be flushed on cgroup destruction. Use a
124 * separate workqueue as flush domain.
125 */
126static struct workqueue_struct *cgroup_pidlist_destroy_wq;
127
Tejun Heo3ed80a62014-02-08 10:36:58 -0500128/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500129#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500130static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131#include <linux/cgroup_subsys.h>
132};
Tejun Heo073219e2014-02-08 10:36:58 -0500133#undef SUBSYS
134
135/* array of cgroup subsystem names */
136#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
137static const char *cgroup_subsys_name[] = {
138#include <linux/cgroup_subsys.h>
139};
140#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
Tejun Heo49d1dc42015-09-18 11:56:28 -0400142/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
143#define SUBSYS(_x) \
144 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
145 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
146 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
147 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
148#include <linux/cgroup_subsys.h>
149#undef SUBSYS
150
151#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
152static struct static_key_true *cgroup_subsys_enabled_key[] = {
153#include <linux/cgroup_subsys.h>
154};
155#undef SUBSYS
156
157#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
158static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
159#include <linux/cgroup_subsys.h>
160};
161#undef SUBSYS
162
Paul Menageddbcc7e2007-10-18 23:39:30 -0700163/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400164 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700165 * unattached - it never has more than a single cgroup, and all tasks are
166 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700167 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400168struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400169EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700170
Tejun Heoa2dd4242014-03-19 10:23:55 -0400171/*
172 * The default hierarchy always exists but is hidden until mounted for the
173 * first time. This is for backward compatibility.
174 */
175static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176
Tejun Heoa8ddc822014-07-15 11:05:10 -0400177/*
178 * Set by the boot param of the same name and makes subsystems with NULL
179 * ->dfl_files to use ->legacy_files on the default hierarchy.
180 */
181static bool cgroup_legacy_files_on_dfl;
182
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;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210
Aleksa Sarai7e476822015-06-09 21:32:09 +1000211/* Ditto for the can_fork callback. */
212static unsigned long have_canfork_callback __read_mostly;
213
Tejun Heoa14c6872014-07-15 11:05:09 -0400214static struct cftype cgroup_dfl_base_files[];
215static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700216
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400217static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000218 unsigned long ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800219static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400220static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
221 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400222static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400223static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400224static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
225 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800226
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400227/**
228 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
229 * @ssid: subsys ID of interest
230 *
231 * cgroup_subsys_enabled() can only be used with literal subsys names which
232 * is fine for individual subsystems but unsuitable for cgroup core. This
233 * is slower static_key_enabled() based test indexed by @ssid.
234 */
235static bool cgroup_ssid_enabled(int ssid)
236{
237 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
238}
239
Tejun Heo9e10a132015-09-18 11:56:28 -0400240/**
241 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
242 * @cgrp: the cgroup of interest
243 *
244 * The default hierarchy is the v2 interface of cgroup and this function
245 * can be used to test whether a cgroup is on the default hierarchy for
246 * cases where a subsystem should behave differnetly depending on the
247 * interface version.
248 *
249 * The set of behaviors which change on the default hierarchy are still
250 * being determined and the mount option is prefixed with __DEVEL__.
251 *
252 * List of changed behaviors:
253 *
254 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
255 * and "name" are disallowed.
256 *
257 * - When mounting an existing superblock, mount options should match.
258 *
259 * - Remount is disallowed.
260 *
261 * - rename(2) is disallowed.
262 *
263 * - "tasks" is removed. Everything should be at process granularity. Use
264 * "cgroup.procs" instead.
265 *
266 * - "cgroup.procs" is not sorted. pids will be unique unless they got
267 * recycled inbetween reads.
268 *
269 * - "release_agent" and "notify_on_release" are removed. Replacement
270 * notification mechanism will be implemented.
271 *
272 * - "cgroup.clone_children" is removed.
273 *
274 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
275 * and its descendants contain no task; otherwise, 1. The file also
276 * generates kernfs notification which can be monitored through poll and
277 * [di]notify when the value of the file changes.
278 *
279 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
280 * take masks of ancestors with non-empty cpus/mems, instead of being
281 * moved to an ancestor.
282 *
283 * - cpuset: a task can be moved into an empty cpuset, and again it takes
284 * masks of ancestors.
285 *
286 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
287 * is not created.
288 *
289 * - blkcg: blk-throttle becomes properly hierarchical.
290 *
291 * - debug: disallowed on the default hierarchy.
292 */
293static bool cgroup_on_dfl(const struct cgroup *cgrp)
294{
295 return cgrp->root == &cgrp_dfl_root;
296}
297
Tejun Heo6fa49182014-05-04 15:09:13 -0400298/* IDR wrappers which synchronize using cgroup_idr_lock */
299static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
300 gfp_t gfp_mask)
301{
302 int ret;
303
304 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400305 spin_lock_bh(&cgroup_idr_lock);
Vladimir Davydovcf780b72015-08-03 15:32:26 +0300306 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_WAIT);
Tejun Heo54504e92014-05-13 12:10:59 -0400307 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400308 idr_preload_end();
309 return ret;
310}
311
312static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
313{
314 void *ret;
315
Tejun Heo54504e92014-05-13 12:10:59 -0400316 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400317 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400318 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400319 return ret;
320}
321
322static void cgroup_idr_remove(struct idr *idr, int id)
323{
Tejun Heo54504e92014-05-13 12:10:59 -0400324 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400325 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400326 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400327}
328
Tejun Heod51f39b2014-05-16 13:22:48 -0400329static struct cgroup *cgroup_parent(struct cgroup *cgrp)
330{
331 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
332
333 if (parent_css)
334 return container_of(parent_css, struct cgroup, self);
335 return NULL;
336}
337
Tejun Heo95109b62013-08-08 20:11:27 -0400338/**
339 * cgroup_css - obtain a cgroup's css for the specified subsystem
340 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400341 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400342 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400343 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
344 * function must be called either under cgroup_mutex or rcu_read_lock() and
345 * the caller is responsible for pinning the returned css if it wants to
346 * keep accessing it outside the said locks. This function may return
347 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400348 */
349static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400350 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400351{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400352 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500353 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500354 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400355 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400356 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400357}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700358
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400359/**
360 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
361 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400362 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400363 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400364 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400365 * as the matching css of the nearest ancestor including self which has @ss
366 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
367 * function is guaranteed to return non-NULL css.
368 */
369static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
370 struct cgroup_subsys *ss)
371{
372 lockdep_assert_held(&cgroup_mutex);
373
374 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400375 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400376
377 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
378 return NULL;
379
Tejun Heoeeecbd12014-11-18 02:49:52 -0500380 /*
381 * This function is used while updating css associations and thus
382 * can't test the csses directly. Use ->child_subsys_mask.
383 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400384 while (cgroup_parent(cgrp) &&
385 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
386 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400387
388 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700389}
390
Tejun Heoeeecbd12014-11-18 02:49:52 -0500391/**
392 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
393 * @cgrp: the cgroup of interest
394 * @ss: the subsystem of interest
395 *
396 * Find and get the effective css of @cgrp for @ss. The effective css is
397 * defined as the matching css of the nearest ancestor including self which
398 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
399 * the root css is returned, so this function always returns a valid css.
400 * The returned css must be put using css_put().
401 */
402struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
403 struct cgroup_subsys *ss)
404{
405 struct cgroup_subsys_state *css;
406
407 rcu_read_lock();
408
409 do {
410 css = cgroup_css(cgrp, ss);
411
412 if (css && css_tryget_online(css))
413 goto out_unlock;
414 cgrp = cgroup_parent(cgrp);
415 } while (cgrp);
416
417 css = init_css_set.subsys[ss->id];
418 css_get(css);
419out_unlock:
420 rcu_read_unlock();
421 return css;
422}
423
Paul Menageddbcc7e2007-10-18 23:39:30 -0700424/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700425static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700426{
Tejun Heo184faf32014-05-16 13:22:51 -0400427 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700428}
429
Tejun Heob4168642014-05-13 12:16:21 -0400430struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500431{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500432 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400433 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500434
435 /*
436 * This is open and unprotected implementation of cgroup_css().
437 * seq_css() is only called from a kernfs file operation which has
438 * an active reference on the file. Because all the subsystem
439 * files are drained before a css is disassociated with a cgroup,
440 * the matching css from the cgroup's subsys table is guaranteed to
441 * be and stay valid until the enclosing operation is complete.
442 */
443 if (cft->ss)
444 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
445 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400446 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500447}
Tejun Heob4168642014-05-13 12:16:21 -0400448EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500449
Li Zefan78574cf2013-04-08 19:00:38 -0700450/**
451 * cgroup_is_descendant - test ancestry
452 * @cgrp: the cgroup to be tested
453 * @ancestor: possible ancestor of @cgrp
454 *
455 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
456 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
457 * and @ancestor are accessible.
458 */
459bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
460{
461 while (cgrp) {
462 if (cgrp == ancestor)
463 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400464 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700465 }
466 return false;
467}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700468
Adrian Bunke9685a02008-02-07 00:13:46 -0800469static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700470{
Paul Menagebd89aab2007-10-18 23:40:44 -0700471 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700472}
473
Tejun Heo30159ec2013-06-25 11:53:37 -0700474/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500475 * for_each_css - iterate all css's of a cgroup
476 * @css: the iteration cursor
477 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
478 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700479 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400480 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700481 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500482#define for_each_css(css, ssid, cgrp) \
483 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
484 if (!((css) = rcu_dereference_check( \
485 (cgrp)->subsys[(ssid)], \
486 lockdep_is_held(&cgroup_mutex)))) { } \
487 else
488
489/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400490 * for_each_e_css - iterate all effective css's of a cgroup
491 * @css: the iteration cursor
492 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
493 * @cgrp: the target cgroup to iterate css's of
494 *
495 * Should be called under cgroup_[tree_]mutex.
496 */
497#define for_each_e_css(css, ssid, cgrp) \
498 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
499 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
500 ; \
501 else
502
503/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500504 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700505 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500506 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700507 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500508#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500509 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
510 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700511
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000512/**
513 * for_each_subsys_which - filter for_each_subsys with a bitmask
514 * @ss: the iteration cursor
515 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
516 * @ss_maskp: a pointer to the bitmask
517 *
518 * The block will only run for cases where the ssid-th bit (1 << ssid) of
519 * mask is set to 1.
520 */
521#define for_each_subsys_which(ss, ssid, ss_maskp) \
522 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000523 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000524 else \
525 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
526 if (((ss) = cgroup_subsys[ssid]) && false) \
527 break; \
528 else
529
Tejun Heo985ed672014-03-19 10:23:53 -0400530/* iterate across the hierarchies */
531#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700532 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700533
Tejun Heof8f22e52014-04-23 11:13:16 -0400534/* iterate over child cgrps, lock should be held throughout iteration */
535#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400536 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400537 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400538 cgroup_is_dead(child); })) \
539 ; \
540 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700541
Paul Menage81a6a5c2007-10-18 23:39:38 -0700542static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700543static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700544
Tejun Heo69d02062013-06-12 21:04:50 -0700545/*
546 * A cgroup can be associated with multiple css_sets as different tasks may
547 * belong to different cgroups on different hierarchies. In the other
548 * direction, a css_set is naturally associated with multiple cgroups.
549 * This M:N relationship is represented by the following link structure
550 * which exists for each association and allows traversing the associations
551 * from both sides.
552 */
553struct cgrp_cset_link {
554 /* the cgroup and css_set this link associates */
555 struct cgroup *cgrp;
556 struct css_set *cset;
557
558 /* list of cgrp_cset_links anchored at cgrp->cset_links */
559 struct list_head cset_link;
560
561 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
562 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700563};
564
Tejun Heo172a2c062014-03-19 10:23:53 -0400565/*
566 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700567 * hierarchies being mounted. It contains a pointer to the root state
568 * for each subsystem. Also used to anchor the list of css_sets. Not
569 * reference-counted, to improve performance when child cgroups
570 * haven't been created.
571 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400572struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400573 .refcount = ATOMIC_INIT(1),
574 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
575 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
576 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
577 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
578 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
579};
Paul Menage817929e2007-10-18 23:39:36 -0700580
Tejun Heo172a2c062014-03-19 10:23:53 -0400581static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700582
Tejun Heo842b5972014-04-25 18:28:02 -0400583/**
584 * cgroup_update_populated - updated populated count of a cgroup
585 * @cgrp: the target cgroup
586 * @populated: inc or dec populated count
587 *
588 * @cgrp is either getting the first task (css_set) or losing the last.
589 * Update @cgrp->populated_cnt accordingly. The count is propagated
590 * towards root so that a given cgroup's populated_cnt is zero iff the
591 * cgroup and all its descendants are empty.
592 *
593 * @cgrp's interface file "cgroup.populated" is zero if
594 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
595 * changes from or to zero, userland is notified that the content of the
596 * interface file has changed. This can be used to detect when @cgrp and
597 * its descendants become populated or empty.
598 */
599static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
600{
601 lockdep_assert_held(&css_set_rwsem);
602
603 do {
604 bool trigger;
605
606 if (populated)
607 trigger = !cgrp->populated_cnt++;
608 else
609 trigger = !--cgrp->populated_cnt;
610
611 if (!trigger)
612 break;
613
Tejun Heo4a07c222015-09-18 17:54:22 -0400614 if (cgrp->events_kn)
615 kernfs_notify(cgrp->events_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400616 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400617 } while (cgrp);
618}
619
Paul Menage7717f7b2009-09-23 15:56:22 -0700620/*
621 * hash table for cgroup groups. This improves the performance to find
622 * an existing css_set. This hash doesn't (currently) take into
623 * account cgroups in empty hierarchies.
624 */
Li Zefan472b1052008-04-29 01:00:11 -0700625#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800626static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700627
Li Zefan0ac801f2013-01-10 11:49:27 +0800628static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700629{
Li Zefan0ac801f2013-01-10 11:49:27 +0800630 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700631 struct cgroup_subsys *ss;
632 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700633
Tejun Heo30159ec2013-06-25 11:53:37 -0700634 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800635 key += (unsigned long)css[i];
636 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700637
Li Zefan0ac801f2013-01-10 11:49:27 +0800638 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700639}
640
Zefan Lia25eb522014-09-19 16:51:00 +0800641static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700642{
Tejun Heo69d02062013-06-12 21:04:50 -0700643 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400644 struct cgroup_subsys *ss;
645 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700646
Tejun Heo89c55092014-02-13 06:58:40 -0500647 lockdep_assert_held(&css_set_rwsem);
648
649 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700650 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700651
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700652 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400653 for_each_subsys(ss, ssid)
654 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700655 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700656 css_set_count--;
657
Tejun Heo69d02062013-06-12 21:04:50 -0700658 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700659 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700660
Tejun Heo69d02062013-06-12 21:04:50 -0700661 list_del(&link->cset_link);
662 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800663
Tejun Heo96d365e2014-02-13 06:58:40 -0500664 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400665 if (list_empty(&cgrp->cset_links)) {
666 cgroup_update_populated(cgrp, false);
Zefan Lia25eb522014-09-19 16:51:00 +0800667 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700668 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700669
670 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700671 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700672
Tejun Heo5abb8852013-06-12 21:04:49 -0700673 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700674}
675
Zefan Lia25eb522014-09-19 16:51:00 +0800676static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500677{
678 /*
679 * Ensure that the refcount doesn't hit zero while any readers
680 * can see it. Similar to atomic_dec_and_lock(), but for an
681 * rwlock
682 */
683 if (atomic_add_unless(&cset->refcount, -1, 1))
684 return;
685
686 down_write(&css_set_rwsem);
Zefan Lia25eb522014-09-19 16:51:00 +0800687 put_css_set_locked(cset);
Tejun Heo89c55092014-02-13 06:58:40 -0500688 up_write(&css_set_rwsem);
689}
690
Paul Menage817929e2007-10-18 23:39:36 -0700691/*
692 * refcounted get/put for css_set objects
693 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700694static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700695{
Tejun Heo5abb8852013-06-12 21:04:49 -0700696 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700697}
698
Tejun Heob326f9d2013-06-24 15:21:48 -0700699/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700700 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700701 * @cset: candidate css_set being tested
702 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700703 * @new_cgrp: cgroup that's being entered by the task
704 * @template: desired set of css pointers in css_set (pre-calculated)
705 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800706 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700707 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
708 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700709static bool compare_css_sets(struct css_set *cset,
710 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 struct cgroup *new_cgrp,
712 struct cgroup_subsys_state *template[])
713{
714 struct list_head *l1, *l2;
715
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400716 /*
717 * On the default hierarchy, there can be csets which are
718 * associated with the same set of cgroups but different csses.
719 * Let's first ensure that csses match.
720 */
721 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700722 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700723
724 /*
725 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400726 * different cgroups in hierarchies. As different cgroups may
727 * share the same effective css, this comparison is always
728 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 */
Tejun Heo69d02062013-06-12 21:04:50 -0700730 l1 = &cset->cgrp_links;
731 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700732 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700733 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700734 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700735
736 l1 = l1->next;
737 l2 = l2->next;
738 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700739 if (l1 == &cset->cgrp_links) {
740 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700741 break;
742 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700743 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700744 }
745 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700746 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
747 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
748 cgrp1 = link1->cgrp;
749 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700750 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700751 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700752
753 /*
754 * If this hierarchy is the hierarchy of the cgroup
755 * that's changing, then we need to check that this
756 * css_set points to the new cgroup; if it's any other
757 * hierarchy, then this css_set should point to the
758 * same cgroup as the old css_set.
759 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700760 if (cgrp1->root == new_cgrp->root) {
761 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700762 return false;
763 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700764 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700765 return false;
766 }
767 }
768 return true;
769}
770
Tejun Heob326f9d2013-06-24 15:21:48 -0700771/**
772 * find_existing_css_set - init css array and find the matching css_set
773 * @old_cset: the css_set that we're using before the cgroup transition
774 * @cgrp: the cgroup that we're moving into
775 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700776 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700777static struct css_set *find_existing_css_set(struct css_set *old_cset,
778 struct cgroup *cgrp,
779 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700780{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400781 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700782 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700783 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800784 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700785 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700786
Ben Blumaae8aab2010-03-10 15:22:07 -0800787 /*
788 * Build the set of subsystem state objects that we want to see in the
789 * new css_set. while subsystems can change globally, the entries here
790 * won't change, so no need for locking.
791 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700792 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400793 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400794 /*
795 * @ss is in this hierarchy, so we want the
796 * effective css from @cgrp.
797 */
798 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700799 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400800 /*
801 * @ss is not in this hierarchy, so we don't want
802 * to change the css.
803 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700804 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700805 }
806 }
807
Li Zefan0ac801f2013-01-10 11:49:27 +0800808 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700809 hash_for_each_possible(css_set_table, cset, hlist, key) {
810 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700811 continue;
812
813 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700814 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700815 }
Paul Menage817929e2007-10-18 23:39:36 -0700816
817 /* No existing cgroup group matched */
818 return NULL;
819}
820
Tejun Heo69d02062013-06-12 21:04:50 -0700821static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700822{
Tejun Heo69d02062013-06-12 21:04:50 -0700823 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700824
Tejun Heo69d02062013-06-12 21:04:50 -0700825 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
826 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700827 kfree(link);
828 }
829}
830
Tejun Heo69d02062013-06-12 21:04:50 -0700831/**
832 * allocate_cgrp_cset_links - allocate cgrp_cset_links
833 * @count: the number of links to allocate
834 * @tmp_links: list_head the allocated links are put on
835 *
836 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
837 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700838 */
Tejun Heo69d02062013-06-12 21:04:50 -0700839static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700840{
Tejun Heo69d02062013-06-12 21:04:50 -0700841 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700842 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700843
844 INIT_LIST_HEAD(tmp_links);
845
Li Zefan36553432008-07-29 22:33:19 -0700846 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700847 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700848 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700849 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700850 return -ENOMEM;
851 }
Tejun Heo69d02062013-06-12 21:04:50 -0700852 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700853 }
854 return 0;
855}
856
Li Zefanc12f65d2009-01-07 18:07:42 -0800857/**
858 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700859 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700860 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800861 * @cgrp: the destination cgroup
862 */
Tejun Heo69d02062013-06-12 21:04:50 -0700863static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
864 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800865{
Tejun Heo69d02062013-06-12 21:04:50 -0700866 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800867
Tejun Heo69d02062013-06-12 21:04:50 -0700868 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400869
870 if (cgroup_on_dfl(cgrp))
871 cset->dfl_cgrp = cgrp;
872
Tejun Heo69d02062013-06-12 21:04:50 -0700873 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
874 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700875 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400876
877 if (list_empty(&cgrp->cset_links))
878 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700879 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400880
Paul Menage7717f7b2009-09-23 15:56:22 -0700881 /*
882 * Always add links to the tail of the list so that the list
883 * is sorted by order of hierarchy creation
884 */
Tejun Heo69d02062013-06-12 21:04:50 -0700885 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800886}
887
Tejun Heob326f9d2013-06-24 15:21:48 -0700888/**
889 * find_css_set - return a new css_set with one cgroup updated
890 * @old_cset: the baseline css_set
891 * @cgrp: the cgroup to be updated
892 *
893 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
894 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700895 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700896static struct css_set *find_css_set(struct css_set *old_cset,
897 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700898{
Tejun Heob326f9d2013-06-24 15:21:48 -0700899 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700900 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700901 struct list_head tmp_links;
902 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400903 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800904 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400905 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700906
Tejun Heob326f9d2013-06-24 15:21:48 -0700907 lockdep_assert_held(&cgroup_mutex);
908
Paul Menage817929e2007-10-18 23:39:36 -0700909 /* First see if we already have a cgroup group that matches
910 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500911 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700912 cset = find_existing_css_set(old_cset, cgrp, template);
913 if (cset)
914 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500915 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700916
Tejun Heo5abb8852013-06-12 21:04:49 -0700917 if (cset)
918 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700919
Tejun Heof4f4be22013-06-12 21:04:51 -0700920 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700921 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700922 return NULL;
923
Tejun Heo69d02062013-06-12 21:04:50 -0700924 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700925 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700926 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700927 return NULL;
928 }
929
Tejun Heo5abb8852013-06-12 21:04:49 -0700930 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700931 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700932 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500933 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500934 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500935 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700936 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700937
938 /* Copy the set of subsystem state objects generated in
939 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700940 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700941
Tejun Heo96d365e2014-02-13 06:58:40 -0500942 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700943 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700944 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700945 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700946
Paul Menage7717f7b2009-09-23 15:56:22 -0700947 if (c->root == cgrp->root)
948 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700949 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700950 }
Paul Menage817929e2007-10-18 23:39:36 -0700951
Tejun Heo69d02062013-06-12 21:04:50 -0700952 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700953
Paul Menage817929e2007-10-18 23:39:36 -0700954 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700955
Tejun Heo2d8f2432014-04-23 11:13:15 -0400956 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700957 key = css_set_hash(cset->subsys);
958 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700959
Tejun Heo2d8f2432014-04-23 11:13:15 -0400960 for_each_subsys(ss, ssid)
961 list_add_tail(&cset->e_cset_node[ssid],
962 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
963
Tejun Heo96d365e2014-02-13 06:58:40 -0500964 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700965
Tejun Heo5abb8852013-06-12 21:04:49 -0700966 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700967}
968
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400969static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700970{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400971 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500972
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400973 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500974}
975
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400976static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500977{
978 int id;
979
980 lockdep_assert_held(&cgroup_mutex);
981
Tejun Heo985ed672014-03-19 10:23:53 -0400982 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500983 if (id < 0)
984 return id;
985
986 root->hierarchy_id = id;
987 return 0;
988}
989
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400990static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500991{
992 lockdep_assert_held(&cgroup_mutex);
993
994 if (root->hierarchy_id) {
995 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
996 root->hierarchy_id = 0;
997 }
998}
999
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001000static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001001{
1002 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001003 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001004 WARN_ON_ONCE(root->hierarchy_id);
1005
1006 idr_destroy(&root->cgroup_idr);
1007 kfree(root);
1008 }
1009}
1010
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001011static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001012{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001013 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001014 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001015
Tejun Heo2bd59d42014-02-11 11:52:49 -05001016 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001017
Tejun Heo776f02f2014-02-12 09:29:50 -05001018 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001019 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001020
Tejun Heof2e85d52014-02-11 11:52:49 -05001021 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001022 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001023
1024 /*
1025 * Release all the links from cset_links to this hierarchy's
1026 * root cgroup
1027 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001028 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -05001029
1030 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1031 list_del(&link->cset_link);
1032 list_del(&link->cgrp_link);
1033 kfree(link);
1034 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001035 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -05001036
1037 if (!list_empty(&root->root_list)) {
1038 list_del(&root->root_list);
1039 cgroup_root_count--;
1040 }
1041
1042 cgroup_exit_root_id(root);
1043
1044 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001045
Tejun Heo2bd59d42014-02-11 11:52:49 -05001046 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001047 cgroup_free_root(root);
1048}
1049
Tejun Heoceb6a082014-02-25 10:04:02 -05001050/* look up cgroup associated with given css_set on the specified hierarchy */
1051static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001052 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001053{
Paul Menage7717f7b2009-09-23 15:56:22 -07001054 struct cgroup *res = NULL;
1055
Tejun Heo96d365e2014-02-13 06:58:40 -05001056 lockdep_assert_held(&cgroup_mutex);
1057 lockdep_assert_held(&css_set_rwsem);
1058
Tejun Heo5abb8852013-06-12 21:04:49 -07001059 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001060 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001061 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001062 struct cgrp_cset_link *link;
1063
1064 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001065 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001066
Paul Menage7717f7b2009-09-23 15:56:22 -07001067 if (c->root == root) {
1068 res = c;
1069 break;
1070 }
1071 }
1072 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001073
Paul Menage7717f7b2009-09-23 15:56:22 -07001074 BUG_ON(!res);
1075 return res;
1076}
1077
1078/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001079 * Return the cgroup for "task" from the given hierarchy. Must be
1080 * called with cgroup_mutex and css_set_rwsem held.
1081 */
1082static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001083 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001084{
1085 /*
1086 * No need to lock the task - since we hold cgroup_mutex the
1087 * task can't change groups, so the only thing that can happen
1088 * is that it exits and its css is set back to init_css_set.
1089 */
1090 return cset_cgroup_from_root(task_css_set(task), root);
1091}
1092
1093/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 * A task must hold cgroup_mutex to modify cgroups.
1095 *
1096 * Any task can increment and decrement the count field without lock.
1097 * So in general, code holding cgroup_mutex can't rely on the count
1098 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001099 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 * means that no tasks are currently attached, therefore there is no
1101 * way a task attached to that cgroup can fork (the other way to
1102 * increment the count). So code holding cgroup_mutex can safely
1103 * assume that if the count is zero, it will stay zero. Similarly, if
1104 * a task holds cgroup_mutex on a cgroup with zero count, it
1105 * knows that the cgroup won't be removed, as cgroup_rmdir()
1106 * needs that mutex.
1107 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 * A cgroup can only be deleted if both its 'count' of using tasks
1109 * is zero, and its list of 'children' cgroups is empty. Since all
1110 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001111 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001113 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114 *
1115 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001116 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117 */
1118
Tejun Heo2bd59d42014-02-11 11:52:49 -05001119static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001120static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001121
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001122static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1123 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001125 struct cgroup_subsys *ss = cft->ss;
1126
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001127 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1128 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1129 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001130 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1131 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001132 else
1133 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1134 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135}
1136
Tejun Heof2e85d52014-02-11 11:52:49 -05001137/**
1138 * cgroup_file_mode - deduce file mode of a control file
1139 * @cft: the control file in question
1140 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001141 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001142 */
1143static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001144{
Tejun Heof2e85d52014-02-11 11:52:49 -05001145 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001146
Tejun Heof2e85d52014-02-11 11:52:49 -05001147 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1148 mode |= S_IRUGO;
1149
Tejun Heo7dbdb192015-09-18 17:54:23 -04001150 if (cft->write_u64 || cft->write_s64 || cft->write) {
1151 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1152 mode |= S_IWUGO;
1153 else
1154 mode |= S_IWUSR;
1155 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001156
1157 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001158}
1159
Tejun Heo59f52962014-02-11 11:52:49 -05001160static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001162 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001163 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164}
1165
Li Zefanaa323622014-09-04 14:43:38 +08001166static bool cgroup_tryget(struct cgroup *cgrp)
1167{
1168 return css_tryget(&cgrp->self);
1169}
1170
Tejun Heo59f52962014-02-11 11:52:49 -05001171static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001172{
Tejun Heo9d755d32014-05-14 09:15:02 -04001173 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001174}
1175
Tejun Heoa9746d82014-05-13 12:19:22 -04001176/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001177 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001178 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001179 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001180 *
1181 * On the default hierarchy, a subsystem may request other subsystems to be
1182 * enabled together through its ->depends_on mask. In such cases, more
1183 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1184 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001185 * This function calculates which subsystems need to be enabled if
1186 * @subtree_control is to be applied to @cgrp. The returned mask is always
1187 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001188 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001189static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1190 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001191{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001192 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001193 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001194 struct cgroup_subsys *ss;
1195 int ssid;
1196
1197 lockdep_assert_held(&cgroup_mutex);
1198
Tejun Heo0f060de2014-11-18 02:49:50 -05001199 if (!cgroup_on_dfl(cgrp))
1200 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001201
1202 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001203 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001204
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001205 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1206 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001207
1208 /*
1209 * Mask out subsystems which aren't available. This can
1210 * happen only if some depended-upon subsystems were bound
1211 * to non-default hierarchies.
1212 */
1213 if (parent)
1214 new_ss_mask &= parent->child_subsys_mask;
1215 else
1216 new_ss_mask &= cgrp->root->subsys_mask;
1217
1218 if (new_ss_mask == cur_ss_mask)
1219 break;
1220 cur_ss_mask = new_ss_mask;
1221 }
1222
Tejun Heo0f060de2014-11-18 02:49:50 -05001223 return cur_ss_mask;
1224}
1225
1226/**
1227 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1228 * @cgrp: the target cgroup
1229 *
1230 * Update @cgrp->child_subsys_mask according to the current
1231 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1232 */
1233static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1234{
1235 cgrp->child_subsys_mask =
1236 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001237}
1238
Tejun Heoa9746d82014-05-13 12:19:22 -04001239/**
1240 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1241 * @kn: the kernfs_node being serviced
1242 *
1243 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1244 * the method finishes if locking succeeded. Note that once this function
1245 * returns the cgroup returned by cgroup_kn_lock_live() may become
1246 * inaccessible any time. If the caller intends to continue to access the
1247 * cgroup, it should pin it before invoking this function.
1248 */
1249static void cgroup_kn_unlock(struct kernfs_node *kn)
1250{
1251 struct cgroup *cgrp;
1252
1253 if (kernfs_type(kn) == KERNFS_DIR)
1254 cgrp = kn->priv;
1255 else
1256 cgrp = kn->parent->priv;
1257
1258 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001259
1260 kernfs_unbreak_active_protection(kn);
1261 cgroup_put(cgrp);
1262}
1263
1264/**
1265 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1266 * @kn: the kernfs_node being serviced
1267 *
1268 * This helper is to be used by a cgroup kernfs method currently servicing
1269 * @kn. It breaks the active protection, performs cgroup locking and
1270 * verifies that the associated cgroup is alive. Returns the cgroup if
1271 * alive; otherwise, %NULL. A successful return should be undone by a
1272 * matching cgroup_kn_unlock() invocation.
1273 *
1274 * Any cgroup kernfs method implementation which requires locking the
1275 * associated cgroup should use this helper. It avoids nesting cgroup
1276 * locking under kernfs active protection and allows all kernfs operations
1277 * including self-removal.
1278 */
1279static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1280{
1281 struct cgroup *cgrp;
1282
1283 if (kernfs_type(kn) == KERNFS_DIR)
1284 cgrp = kn->priv;
1285 else
1286 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287
1288 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001289 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001290 * active_ref. cgroup liveliness check alone provides enough
1291 * protection against removal. Ensure @cgrp stays accessible and
1292 * break the active_ref protection.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293 */
Li Zefanaa323622014-09-04 14:43:38 +08001294 if (!cgroup_tryget(cgrp))
1295 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001296 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001297
Tejun Heoa9746d82014-05-13 12:19:22 -04001298 mutex_lock(&cgroup_mutex);
1299
1300 if (!cgroup_is_dead(cgrp))
1301 return cgrp;
1302
1303 cgroup_kn_unlock(kn);
1304 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305}
1306
Li Zefan2739d3c2013-01-21 18:18:33 +08001307static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001309 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001310
Tejun Heo01f64742014-05-13 12:19:23 -04001311 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001312 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001313}
1314
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001315/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001316 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001317 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001318 * @subsys_mask: mask of the subsystem ids whose files should be removed
1319 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001320static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001321{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001322 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001323 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001324
Tejun Heob420ba72013-07-12 12:34:02 -07001325 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001326 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001327
Tejun Heo69dfa002014-05-04 15:09:13 -04001328 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001329 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001330 list_for_each_entry(cfts, &ss->cfts, node)
1331 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001332 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333}
1334
Tejun Heoccdca212015-09-18 17:54:23 -04001335/**
1336 * cgroup_populate_dir - create subsys files in a cgroup directory
1337 * @cgrp: target cgroup
1338 * @subsys_mask: mask of the subsystem ids whose files should be added
1339 *
1340 * On failure, no file is added.
1341 */
1342static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
1343{
1344 struct cgroup_subsys *ss;
1345 int i, ret = 0;
1346
1347 /* process cftsets of each subsystem */
1348 for_each_subsys(ss, i) {
1349 struct cftype *cfts;
1350
1351 if (!(subsys_mask & (1 << i)))
1352 continue;
1353
1354 list_for_each_entry(cfts, &ss->cfts, node) {
1355 ret = cgroup_addrm_files(cgrp, cfts, true);
1356 if (ret < 0)
1357 goto err;
1358 }
1359 }
1360 return 0;
1361err:
1362 cgroup_clear_dir(cgrp, subsys_mask);
1363 return ret;
1364}
1365
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001366static int rebind_subsystems(struct cgroup_root *dst_root,
1367 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368{
Tejun Heo30159ec2013-06-25 11:53:37 -07001369 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001370 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001371 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372
Tejun Heoace2bee2014-02-11 11:52:47 -05001373 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001374
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001375 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001376 /* if @ss has non-root csses attached to it, can't move */
1377 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001378 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379
Tejun Heo5df36032014-03-19 10:23:54 -04001380 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001381 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001382 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 }
1384
Tejun Heo5533e012014-05-14 19:33:07 -04001385 /* skip creating root files on dfl_root for inhibited subsystems */
1386 tmp_ss_mask = ss_mask;
1387 if (dst_root == &cgrp_dfl_root)
1388 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1389
1390 ret = cgroup_populate_dir(&dst_root->cgrp, tmp_ss_mask);
Tejun Heoa2dd4242014-03-19 10:23:55 -04001391 if (ret) {
1392 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001393 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394
Tejun Heoa2dd4242014-03-19 10:23:55 -04001395 /*
1396 * Rebinding back to the default root is not allowed to
1397 * fail. Using both default and non-default roots should
1398 * be rare. Moving subsystems back and forth even more so.
1399 * Just warn about it and continue.
1400 */
1401 if (cgrp_dfl_root_visible) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001402 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001403 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001404 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001405 }
Tejun Heo5df36032014-03-19 10:23:54 -04001406 }
Tejun Heo31261212013-06-28 17:07:30 -07001407
1408 /*
1409 * Nothing can fail from this point on. Remove files for the
1410 * removed subsystems and rebind each subsystem.
1411 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001412 for_each_subsys_which(ss, ssid, &ss_mask)
1413 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001414
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001415 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001416 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001417 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001418 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001419
Tejun Heo5df36032014-03-19 10:23:54 -04001420 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001421 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001422
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001423 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001424
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001425 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1426 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001427 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001428 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001429
Tejun Heo2d8f2432014-04-23 11:13:15 -04001430 down_write(&css_set_rwsem);
1431 hash_for_each(css_set_table, i, cset, hlist)
1432 list_move_tail(&cset->e_cset_node[ss->id],
1433 &dst_root->cgrp.e_csets[ss->id]);
1434 up_write(&css_set_rwsem);
1435
Tejun Heof392e512014-04-23 11:13:14 -04001436 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo667c2492014-07-08 18:02:56 -04001437 src_root->cgrp.subtree_control &= ~(1 << ssid);
1438 cgroup_refresh_child_subsys_mask(&src_root->cgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001439
Tejun Heobd53d612014-04-23 11:13:16 -04001440 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001441 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001442 if (dst_root == &cgrp_dfl_root) {
1443 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1444 } else {
Tejun Heo667c2492014-07-08 18:02:56 -04001445 dst_root->cgrp.subtree_control |= 1 << ssid;
1446 cgroup_refresh_child_subsys_mask(&dst_root->cgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001447 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001448 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001449
Tejun Heo5df36032014-03-19 10:23:54 -04001450 if (ss->bind)
1451 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453
Tejun Heoa2dd4242014-03-19 10:23:55 -04001454 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 return 0;
1456}
1457
Tejun Heo2bd59d42014-02-11 11:52:49 -05001458static int cgroup_show_options(struct seq_file *seq,
1459 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001461 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001463 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464
Tejun Heod98817d2015-08-18 13:58:16 -07001465 if (root != &cgrp_dfl_root)
1466 for_each_subsys(ss, ssid)
1467 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001468 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001469 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001471 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001472 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001473
1474 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001475 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001476 seq_show_option(seq, "release_agent",
1477 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001478 spin_unlock(&release_agent_path_lock);
1479
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001480 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001481 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001483 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484 return 0;
1485}
1486
1487struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001488 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001489 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001490 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001491 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001493 /* User explicitly requested empty subsystem */
1494 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495};
1496
Ben Blumcf5d5942010-03-10 15:22:09 -08001497static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001499 char *token, *o = data;
1500 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001501 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001502 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001503 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001504 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001505
1506#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001507 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001508#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511
1512 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001513 nr_opts++;
1514
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515 if (!*token)
1516 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001517 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001518 /* Explicitly have no subsystems */
1519 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001520 continue;
1521 }
1522 if (!strcmp(token, "all")) {
1523 /* Mutually exclusive option 'all' + subsystem name */
1524 if (one_ss)
1525 return -EINVAL;
1526 all_ss = true;
1527 continue;
1528 }
Tejun Heo873fe092013-04-14 20:15:26 -07001529 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1530 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1531 continue;
1532 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001533 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001534 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001535 continue;
1536 }
1537 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001538 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001539 continue;
1540 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001541 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001542 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001543 continue;
1544 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001545 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001546 /* Specifying two release agents is forbidden */
1547 if (opts->release_agent)
1548 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001549 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001550 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001551 if (!opts->release_agent)
1552 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001553 continue;
1554 }
1555 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001556 const char *name = token + 5;
1557 /* Can't specify an empty name */
1558 if (!strlen(name))
1559 return -EINVAL;
1560 /* Must match [\w.-]+ */
1561 for (i = 0; i < strlen(name); i++) {
1562 char c = name[i];
1563 if (isalnum(c))
1564 continue;
1565 if ((c == '.') || (c == '-') || (c == '_'))
1566 continue;
1567 return -EINVAL;
1568 }
1569 /* Specifying two names is forbidden */
1570 if (opts->name)
1571 return -EINVAL;
1572 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001573 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001574 GFP_KERNEL);
1575 if (!opts->name)
1576 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001577
1578 continue;
1579 }
1580
Tejun Heo30159ec2013-06-25 11:53:37 -07001581 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001582 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001583 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001584 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001585 continue;
1586
1587 /* Mutually exclusive option 'all' + subsystem name */
1588 if (all_ss)
1589 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001590 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001591 one_ss = true;
1592
1593 break;
1594 }
1595 if (i == CGROUP_SUBSYS_COUNT)
1596 return -ENOENT;
1597 }
1598
Tejun Heo873fe092013-04-14 20:15:26 -07001599 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001600 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001601 if (nr_opts != 1) {
1602 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001603 return -EINVAL;
1604 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001605 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001606 }
1607
Li Zefanf9ab5b52009-06-17 16:26:33 -07001608 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001609 * If the 'all' option was specified select all the subsystems,
1610 * otherwise if 'none', 'name=' and a subsystem name options were
1611 * not specified, let's default to 'all'
1612 */
1613 if (all_ss || (!one_ss && !opts->none && !opts->name))
1614 for_each_subsys(ss, i)
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001615 if (cgroup_ssid_enabled(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001616 opts->subsys_mask |= (1 << i);
1617
1618 /*
1619 * We either have to specify by name or by subsystems. (So all
1620 * empty hierarchies must have a name).
1621 */
1622 if (!opts->subsys_mask && !opts->name)
1623 return -EINVAL;
1624
1625 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001626 * Option noprefix was introduced just for backward compatibility
1627 * with the old cpuset, so we allow noprefix only if mounting just
1628 * the cpuset subsystem.
1629 */
Tejun Heo93438622013-04-14 20:15:25 -07001630 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001631 return -EINVAL;
1632
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001633 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001634 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001635 return -EINVAL;
1636
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637 return 0;
1638}
1639
Tejun Heo2bd59d42014-02-11 11:52:49 -05001640static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641{
1642 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001643 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001645 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646
Tejun Heoaa6ec292014-07-09 10:08:08 -04001647 if (root == &cgrp_dfl_root) {
1648 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001649 return -EINVAL;
1650 }
1651
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 mutex_lock(&cgroup_mutex);
1653
1654 /* See what subsystems are wanted */
1655 ret = parse_cgroupfs_options(data, &opts);
1656 if (ret)
1657 goto out_unlock;
1658
Tejun Heof392e512014-04-23 11:13:14 -04001659 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001660 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001661 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001662
Tejun Heof392e512014-04-23 11:13:14 -04001663 added_mask = opts.subsys_mask & ~root->subsys_mask;
1664 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001665
Ben Blumcf5d5942010-03-10 15:22:09 -08001666 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001667 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001668 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001669 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001670 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001672 goto out_unlock;
1673 }
1674
Tejun Heof172e672013-06-28 17:07:30 -07001675 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001676 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001677 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001678 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001679 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680
Tejun Heo5df36032014-03-19 10:23:54 -04001681 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001682 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001685 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001686
Tejun Heo69e943b2014-02-08 10:36:58 -05001687 if (opts.release_agent) {
1688 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001689 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001690 spin_unlock(&release_agent_path_lock);
1691 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001693 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001694 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 return ret;
1697}
1698
Tejun Heoafeb0f92014-02-13 06:58:39 -05001699/*
1700 * To reduce the fork() overhead for systems that are not actually using
1701 * their cgroups capability, we don't maintain the lists running through
1702 * each css_set to its tasks until we see the list actually used - in other
1703 * words after the first mount.
1704 */
1705static bool use_task_css_set_links __read_mostly;
1706
1707static void cgroup_enable_task_cg_lists(void)
1708{
1709 struct task_struct *p, *g;
1710
Tejun Heo96d365e2014-02-13 06:58:40 -05001711 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001712
1713 if (use_task_css_set_links)
1714 goto out_unlock;
1715
1716 use_task_css_set_links = true;
1717
1718 /*
1719 * We need tasklist_lock because RCU is not safe against
1720 * while_each_thread(). Besides, a forking task that has passed
1721 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1722 * is not guaranteed to have its child immediately visible in the
1723 * tasklist if we walk through it with RCU.
1724 */
1725 read_lock(&tasklist_lock);
1726 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001727 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1728 task_css_set(p) != &init_css_set);
1729
1730 /*
1731 * We should check if the process is exiting, otherwise
1732 * it will race with cgroup_exit() in that the list
1733 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001734 * Do it while holding siglock so that we don't end up
1735 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001736 */
Tejun Heof153ad12014-02-25 09:56:49 -05001737 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001738 if (!(p->flags & PF_EXITING)) {
1739 struct css_set *cset = task_css_set(p);
1740
1741 list_add(&p->cg_list, &cset->tasks);
1742 get_css_set(cset);
1743 }
Tejun Heof153ad12014-02-25 09:56:49 -05001744 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001745 } while_each_thread(g, p);
1746 read_unlock(&tasklist_lock);
1747out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001748 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001749}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750
Paul Menagecc31edc2008-10-18 20:28:04 -07001751static void init_cgroup_housekeeping(struct cgroup *cgrp)
1752{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001753 struct cgroup_subsys *ss;
1754 int ssid;
1755
Tejun Heod5c419b2014-05-16 13:22:48 -04001756 INIT_LIST_HEAD(&cgrp->self.sibling);
1757 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001758 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001759 INIT_LIST_HEAD(&cgrp->pidlists);
1760 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001761 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001762 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001763
1764 for_each_subsys(ss, ssid)
1765 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001766
1767 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001768 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001769}
Paul Menagec6d57f32009-09-23 15:56:19 -07001770
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001771static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001772 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001774 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001775
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001777 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001778 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001779 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001780 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781
Paul Menagec6d57f32009-09-23 15:56:19 -07001782 root->flags = opts->flags;
1783 if (opts->release_agent)
1784 strcpy(root->release_agent_path, opts->release_agent);
1785 if (opts->name)
1786 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001787 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001788 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001789}
1790
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001791static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001792{
Tejun Heod427dfe2014-02-11 11:52:48 -05001793 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001794 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heoa14c6872014-07-15 11:05:09 -04001795 struct cftype *base_files;
Tejun Heod427dfe2014-02-11 11:52:48 -05001796 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001797 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001798
Tejun Heod427dfe2014-02-11 11:52:48 -05001799 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001800
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001801 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001802 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001803 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001804 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001805
Tejun Heo2aad2a82014-09-24 13:31:50 -04001806 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1807 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001808 if (ret)
1809 goto out;
1810
Tejun Heod427dfe2014-02-11 11:52:48 -05001811 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001812 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001813 * but that's OK - it can only be increased by someone holding
1814 * cgroup_lock, and that's us. The worst that can happen is that we
1815 * have some link structures left over
1816 */
1817 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001819 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820
Tejun Heo985ed672014-03-19 10:23:53 -04001821 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001822 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001823 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824
Tejun Heo2bd59d42014-02-11 11:52:49 -05001825 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1826 KERNFS_ROOT_CREATE_DEACTIVATED,
1827 root_cgrp);
1828 if (IS_ERR(root->kf_root)) {
1829 ret = PTR_ERR(root->kf_root);
1830 goto exit_root_id;
1831 }
1832 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833
Tejun Heoa14c6872014-07-15 11:05:09 -04001834 if (root == &cgrp_dfl_root)
1835 base_files = cgroup_dfl_base_files;
1836 else
1837 base_files = cgroup_legacy_base_files;
1838
1839 ret = cgroup_addrm_files(root_cgrp, base_files, true);
Tejun Heod427dfe2014-02-11 11:52:48 -05001840 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001841 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001842
Tejun Heo5df36032014-03-19 10:23:54 -04001843 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001844 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001845 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001846
Tejun Heod427dfe2014-02-11 11:52:48 -05001847 /*
1848 * There must be no failure case after here, since rebinding takes
1849 * care of subsystems' refcounts, which are explicitly dropped in
1850 * the failure exit path.
1851 */
1852 list_add(&root->root_list, &cgroup_roots);
1853 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001854
Tejun Heod427dfe2014-02-11 11:52:48 -05001855 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001856 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001857 * objects.
1858 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001859 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001860 hash_for_each(css_set_table, i, cset, hlist)
1861 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001862 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001863
Tejun Heod5c419b2014-05-16 13:22:48 -04001864 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001865 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001866
Tejun Heo2bd59d42014-02-11 11:52:49 -05001867 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001868 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001869 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001870
Tejun Heo2bd59d42014-02-11 11:52:49 -05001871destroy_root:
1872 kernfs_destroy_root(root->kf_root);
1873 root->kf_root = NULL;
1874exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001875 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001876cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001877 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001878out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001879 free_cgrp_cset_links(&tmp_links);
1880 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001881}
1882
Al Virof7e83572010-07-26 13:23:11 +04001883static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001884 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001885 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001886{
Li Zefan3a32bd72014-06-30 11:50:59 +08001887 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001888 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001889 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001890 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001891 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001892 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001893 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001894 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001895
1896 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001897 * The first time anyone tries to mount a cgroup, enable the list
1898 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001899 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001900 if (!use_task_css_set_links)
1901 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001902
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001903 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001904
Paul Menageddbcc7e2007-10-18 23:39:30 -07001905 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001906 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001907 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001908 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001909
Tejun Heo2bd59d42014-02-11 11:52:49 -05001910 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001911 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04001912 cgrp_dfl_root_visible = true;
1913 root = &cgrp_dfl_root;
1914 cgroup_get(&root->cgrp);
1915 ret = 0;
1916 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001917 }
1918
Li Zefan970317a2014-06-30 11:49:58 +08001919 /*
1920 * Destruction of cgroup root is asynchronous, so subsystems may
1921 * still be dying after the previous unmount. Let's drain the
1922 * dying subsystems. We just need to ensure that the ones
1923 * unmounted previously finish dying and don't care about new ones
1924 * starting. Testing ref liveliness is good enough.
1925 */
1926 for_each_subsys(ss, i) {
1927 if (!(opts.subsys_mask & (1 << i)) ||
1928 ss->root == &cgrp_dfl_root)
1929 continue;
1930
1931 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1932 mutex_unlock(&cgroup_mutex);
1933 msleep(10);
1934 ret = restart_syscall();
1935 goto out_free;
1936 }
1937 cgroup_put(&ss->root->cgrp);
1938 }
1939
Tejun Heo985ed672014-03-19 10:23:53 -04001940 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001941 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001942
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001943 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001944 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001945
Paul Menage817929e2007-10-18 23:39:36 -07001946 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001947 * If we asked for a name then it must match. Also, if
1948 * name matches but sybsys_mask doesn't, we should fail.
1949 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001950 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001951 if (opts.name) {
1952 if (strcmp(opts.name, root->name))
1953 continue;
1954 name_match = true;
1955 }
Tejun Heo31261212013-06-28 17:07:30 -07001956
1957 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001958 * If we asked for subsystems (or explicitly for no
1959 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001960 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001961 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001962 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001963 if (!name_match)
1964 continue;
1965 ret = -EBUSY;
1966 goto out_unlock;
1967 }
Tejun Heo873fe092013-04-14 20:15:26 -07001968
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001969 if (root->flags ^ opts.flags)
1970 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05001971
Tejun Heo776f02f2014-02-12 09:29:50 -05001972 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08001973 * We want to reuse @root whose lifetime is governed by its
1974 * ->cgrp. Let's check whether @root is alive and keep it
1975 * that way. As cgroup_kill_sb() can happen anytime, we
1976 * want to block it by pinning the sb so that @root doesn't
1977 * get killed before mount is complete.
1978 *
1979 * With the sb pinned, tryget_live can reliably indicate
1980 * whether @root can be reused. If it's being killed,
1981 * drain it. We can use wait_queue for the wait but this
1982 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001983 */
Li Zefan3a32bd72014-06-30 11:50:59 +08001984 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1985 if (IS_ERR(pinned_sb) ||
1986 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001987 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08001988 if (!IS_ERR_OR_NULL(pinned_sb))
1989 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05001990 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001991 ret = restart_syscall();
1992 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001993 }
1994
1995 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001996 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001997 }
1998
Tejun Heo172a2c062014-03-19 10:23:53 -04001999 /*
2000 * No such thing, create a new one. name= matching without subsys
2001 * specification is allowed for already existing hierarchies but we
2002 * can't create new one without subsys specification.
2003 */
2004 if (!opts.subsys_mask && !opts.none) {
2005 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002006 goto out_unlock;
2007 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002008
Tejun Heo172a2c062014-03-19 10:23:53 -04002009 root = kzalloc(sizeof(*root), GFP_KERNEL);
2010 if (!root) {
2011 ret = -ENOMEM;
2012 goto out_unlock;
2013 }
2014
2015 init_cgroup_root(root, &opts);
2016
Tejun Heo35585572014-02-13 06:58:38 -05002017 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002018 if (ret)
2019 cgroup_free_root(root);
2020
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002021out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002022 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002023out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002024 kfree(opts.release_agent);
2025 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002026
Tejun Heo2bd59d42014-02-11 11:52:49 -05002027 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002028 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002029
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002030 dentry = kernfs_mount(fs_type, flags, root->kf_root,
2031 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002032 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002033 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002034
2035 /*
2036 * If @pinned_sb, we're reusing an existing root and holding an
2037 * extra ref on its sb. Mount is complete. Put the extra ref.
2038 */
2039 if (pinned_sb) {
2040 WARN_ON(new_sb);
2041 deactivate_super(pinned_sb);
2042 }
2043
Tejun Heo2bd59d42014-02-11 11:52:49 -05002044 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002045}
2046
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002047static void cgroup_kill_sb(struct super_block *sb)
2048{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002049 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002050 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002051
Tejun Heo9d755d32014-05-14 09:15:02 -04002052 /*
2053 * If @root doesn't have any mounts or children, start killing it.
2054 * This prevents new mounts by disabling percpu_ref_tryget_live().
2055 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002056 *
2057 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002058 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002059 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002060 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002061 cgroup_put(&root->cgrp);
2062 else
2063 percpu_ref_kill(&root->cgrp.self.refcnt);
2064
Tejun Heo2bd59d42014-02-11 11:52:49 -05002065 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002066}
2067
2068static struct file_system_type cgroup_fs_type = {
2069 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002070 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002071 .kill_sb = cgroup_kill_sb,
2072};
2073
Li Zefana043e3b2008-02-23 15:24:09 -08002074/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002075 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002076 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002077 * @buf: the buffer to write the path into
2078 * @buflen: the length of the buffer
2079 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002080 * Determine @task's cgroup on the first (the one with the lowest non-zero
2081 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2082 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2083 * cgroup controller callbacks.
2084 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002085 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002086 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002087char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002088{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002089 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002090 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002091 int hierarchy_id = 1;
2092 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002093
2094 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05002095 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07002096
Tejun Heo913ffdb2013-07-11 16:34:48 -07002097 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2098
Tejun Heo857a2be2013-04-14 20:50:08 -07002099 if (root) {
2100 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002101 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002102 } else {
2103 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002104 if (strlcpy(buf, "/", buflen) < buflen)
2105 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002106 }
2107
Tejun Heo96d365e2014-02-13 06:58:40 -05002108 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07002109 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002110 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002111}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002112EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002113
Tejun Heob3dc0942014-02-25 10:04:01 -05002114/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002115struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002116 /* the src and dst cset list running through cset->mg_node */
2117 struct list_head src_csets;
2118 struct list_head dst_csets;
2119
2120 /*
2121 * Fields for cgroup_taskset_*() iteration.
2122 *
2123 * Before migration is committed, the target migration tasks are on
2124 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2125 * the csets on ->dst_csets. ->csets point to either ->src_csets
2126 * or ->dst_csets depending on whether migration is committed.
2127 *
2128 * ->cur_csets and ->cur_task point to the current task position
2129 * during iteration.
2130 */
2131 struct list_head *csets;
2132 struct css_set *cur_cset;
2133 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002134};
2135
2136/**
2137 * cgroup_taskset_first - reset taskset and return the first task
2138 * @tset: taskset of interest
2139 *
2140 * @tset iteration is initialized and the first task is returned.
2141 */
2142struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2143{
Tejun Heob3dc0942014-02-25 10:04:01 -05002144 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2145 tset->cur_task = NULL;
2146
2147 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002148}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002149
2150/**
2151 * cgroup_taskset_next - iterate to the next task in taskset
2152 * @tset: taskset of interest
2153 *
2154 * Return the next task in @tset. Iteration must have been initialized
2155 * with cgroup_taskset_first().
2156 */
2157struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2158{
Tejun Heob3dc0942014-02-25 10:04:01 -05002159 struct css_set *cset = tset->cur_cset;
2160 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002161
Tejun Heob3dc0942014-02-25 10:04:01 -05002162 while (&cset->mg_node != tset->csets) {
2163 if (!task)
2164 task = list_first_entry(&cset->mg_tasks,
2165 struct task_struct, cg_list);
2166 else
2167 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002168
Tejun Heob3dc0942014-02-25 10:04:01 -05002169 if (&task->cg_list != &cset->mg_tasks) {
2170 tset->cur_cset = cset;
2171 tset->cur_task = task;
2172 return task;
2173 }
2174
2175 cset = list_next_entry(cset, mg_node);
2176 task = NULL;
2177 }
2178
2179 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002180}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002181
2182/**
Ben Blum74a11662011-05-26 16:25:20 -07002183 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02002184 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002185 * @tsk: the task being migrated
2186 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07002187 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002188 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07002189 */
Tejun Heo5abb8852013-06-12 21:04:49 -07002190static void cgroup_task_migrate(struct cgroup *old_cgrp,
2191 struct task_struct *tsk,
2192 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07002193{
Tejun Heo5abb8852013-06-12 21:04:49 -07002194 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07002195
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002196 lockdep_assert_held(&cgroup_mutex);
2197 lockdep_assert_held(&css_set_rwsem);
2198
Ben Blum74a11662011-05-26 16:25:20 -07002199 /*
Tejun Heo1ed13282015-09-16 12:53:17 -04002200 * We are synchronized through cgroup_threadgroup_rwsem against
2201 * PF_EXITING setting such that we can't race against cgroup_exit()
2202 * changing the css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07002203 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01002204 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002205 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002206
Tejun Heob3dc0942014-02-25 10:04:01 -05002207 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07002208 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002209
Tejun Heo1b9aba42014-03-19 17:43:21 -04002210 /*
2211 * Use move_tail so that cgroup_taskset_first() still returns the
2212 * leader after migration. This works because cgroup_migrate()
2213 * ensures that the dst_cset of the leader is the first on the
2214 * tset's dst_csets list.
2215 */
2216 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07002217
2218 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07002219 * We just gained a reference on old_cset by taking it from the
2220 * task. As trading it for new_cset is protected by cgroup_mutex,
2221 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07002222 */
Zefan Lia25eb522014-09-19 16:51:00 +08002223 put_css_set_locked(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002224}
2225
Li Zefana043e3b2008-02-23 15:24:09 -08002226/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002227 * cgroup_migrate_finish - cleanup after attach
2228 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002229 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002230 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2231 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002232 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002233static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002234{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002235 struct css_set *cset, *tmp_cset;
2236
2237 lockdep_assert_held(&cgroup_mutex);
2238
2239 down_write(&css_set_rwsem);
2240 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2241 cset->mg_src_cgrp = NULL;
2242 cset->mg_dst_cset = NULL;
2243 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002244 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002245 }
2246 up_write(&css_set_rwsem);
2247}
2248
2249/**
2250 * cgroup_migrate_add_src - add a migration source css_set
2251 * @src_cset: the source css_set to add
2252 * @dst_cgrp: the destination cgroup
2253 * @preloaded_csets: list of preloaded css_sets
2254 *
2255 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2256 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2257 * up by cgroup_migrate_finish().
2258 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002259 * This function may be called without holding cgroup_threadgroup_rwsem
2260 * even if the target is a process. Threads may be created and destroyed
2261 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2262 * into play and the preloaded css_sets are guaranteed to cover all
2263 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002264 */
2265static void cgroup_migrate_add_src(struct css_set *src_cset,
2266 struct cgroup *dst_cgrp,
2267 struct list_head *preloaded_csets)
2268{
2269 struct cgroup *src_cgrp;
2270
2271 lockdep_assert_held(&cgroup_mutex);
2272 lockdep_assert_held(&css_set_rwsem);
2273
2274 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2275
Tejun Heo1958d2d2014-02-25 10:04:03 -05002276 if (!list_empty(&src_cset->mg_preload_node))
2277 return;
2278
2279 WARN_ON(src_cset->mg_src_cgrp);
2280 WARN_ON(!list_empty(&src_cset->mg_tasks));
2281 WARN_ON(!list_empty(&src_cset->mg_node));
2282
2283 src_cset->mg_src_cgrp = src_cgrp;
2284 get_css_set(src_cset);
2285 list_add(&src_cset->mg_preload_node, preloaded_csets);
2286}
2287
2288/**
2289 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002290 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002291 * @preloaded_csets: list of preloaded source css_sets
2292 *
2293 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2294 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002295 * pins all destination css_sets, links each to its source, and append them
2296 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2297 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002298 *
2299 * This function must be called after cgroup_migrate_add_src() has been
2300 * called on each migration source css_set. After migration is performed
2301 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2302 * @preloaded_csets.
2303 */
2304static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2305 struct list_head *preloaded_csets)
2306{
2307 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002308 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002309
2310 lockdep_assert_held(&cgroup_mutex);
2311
Tejun Heof8f22e52014-04-23 11:13:16 -04002312 /*
2313 * Except for the root, child_subsys_mask must be zero for a cgroup
2314 * with tasks so that child cgroups don't compete against tasks.
2315 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002316 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002317 dst_cgrp->child_subsys_mask)
2318 return -EBUSY;
2319
Tejun Heo1958d2d2014-02-25 10:04:03 -05002320 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002321 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002322 struct css_set *dst_cset;
2323
Tejun Heof817de92014-04-23 11:13:16 -04002324 dst_cset = find_css_set(src_cset,
2325 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002326 if (!dst_cset)
2327 goto err;
2328
2329 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002330
2331 /*
2332 * If src cset equals dst, it's noop. Drop the src.
2333 * cgroup_migrate() will skip the cset too. Note that we
2334 * can't handle src == dst as some nodes are used by both.
2335 */
2336 if (src_cset == dst_cset) {
2337 src_cset->mg_src_cgrp = NULL;
2338 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002339 put_css_set(src_cset);
2340 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002341 continue;
2342 }
2343
Tejun Heo1958d2d2014-02-25 10:04:03 -05002344 src_cset->mg_dst_cset = dst_cset;
2345
2346 if (list_empty(&dst_cset->mg_preload_node))
2347 list_add(&dst_cset->mg_preload_node, &csets);
2348 else
Zefan Lia25eb522014-09-19 16:51:00 +08002349 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002350 }
2351
Tejun Heof817de92014-04-23 11:13:16 -04002352 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002353 return 0;
2354err:
2355 cgroup_migrate_finish(&csets);
2356 return -ENOMEM;
2357}
2358
2359/**
2360 * cgroup_migrate - migrate a process or task to a cgroup
2361 * @cgrp: the destination cgroup
2362 * @leader: the leader of the process or the task to migrate
2363 * @threadgroup: whether @leader points to the whole process or a single task
2364 *
2365 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002366 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002367 * caller is also responsible for invoking cgroup_migrate_add_src() and
2368 * cgroup_migrate_prepare_dst() on the targets before invoking this
2369 * function and following up with cgroup_migrate_finish().
2370 *
2371 * As long as a controller's ->can_attach() doesn't fail, this function is
2372 * guaranteed to succeed. This means that, excluding ->can_attach()
2373 * failure, when migrating multiple targets, the success or failure can be
2374 * decided for all targets by invoking group_migrate_prepare_dst() before
2375 * actually starting migrating.
2376 */
2377static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2378 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002379{
Tejun Heob3dc0942014-02-25 10:04:01 -05002380 struct cgroup_taskset tset = {
2381 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2382 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2383 .csets = &tset.src_csets,
2384 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002385 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002386 struct css_set *cset, *tmp_cset;
2387 struct task_struct *task, *tmp_task;
2388 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002389
2390 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002391 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2392 * already PF_EXITING could be freed from underneath us unless we
2393 * take an rcu_read_lock.
2394 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002395 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002396 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002397 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002398 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002399 /* @task either already exited or can't exit until the end */
2400 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002401 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002402
Tejun Heoeaf797a2014-02-25 10:04:03 -05002403 /* leave @task alone if post_fork() hasn't linked it yet */
2404 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002405 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002406
Tejun Heob3dc0942014-02-25 10:04:01 -05002407 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002408 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002409 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002410
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002411 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002412 * cgroup_taskset_first() must always return the leader.
2413 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002414 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002415 list_move_tail(&task->cg_list, &cset->mg_tasks);
2416 if (list_empty(&cset->mg_node))
2417 list_add_tail(&cset->mg_node, &tset.src_csets);
2418 if (list_empty(&cset->mg_dst_cset->mg_node))
2419 list_move_tail(&cset->mg_dst_cset->mg_node,
2420 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002421 next:
Li Zefan081aa452013-03-13 09:17:09 +08002422 if (!threadgroup)
2423 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002424 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002425 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002426 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002427
Tejun Heo134d3372011-12-12 18:12:21 -08002428 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002429 if (list_empty(&tset.src_csets))
2430 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002431
Tejun Heo1958d2d2014-02-25 10:04:03 -05002432 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002433 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002434 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002435 ret = css->ss->can_attach(css, &tset);
2436 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002437 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002438 goto out_cancel_attach;
2439 }
2440 }
Ben Blum74a11662011-05-26 16:25:20 -07002441 }
2442
2443 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002444 * Now that we're guaranteed success, proceed to move all tasks to
2445 * the new cgroup. There are no failure cases after here, so this
2446 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002447 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002448 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002449 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2450 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2451 cgroup_task_migrate(cset->mg_src_cgrp, task,
2452 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002453 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002454 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002455
2456 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002457 * Migration is committed, all target tasks are now on dst_csets.
2458 * Nothing is sensitive to fork() after this point. Notify
2459 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002460 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002461 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002462
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002463 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002464 if (css->ss->attach)
2465 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002466
Tejun Heo9db8de32014-02-13 06:58:43 -05002467 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002468 goto out_release_tset;
2469
Ben Blum74a11662011-05-26 16:25:20 -07002470out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002471 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002472 if (css == failed_css)
2473 break;
2474 if (css->ss->cancel_attach)
2475 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002476 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002477out_release_tset:
2478 down_write(&css_set_rwsem);
2479 list_splice_init(&tset.dst_csets, &tset.src_csets);
2480 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002481 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002482 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002483 }
2484 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002485 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002486}
2487
Tejun Heo1958d2d2014-02-25 10:04:03 -05002488/**
2489 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2490 * @dst_cgrp: the cgroup to attach to
2491 * @leader: the task or the leader of the threadgroup to be attached
2492 * @threadgroup: attach the whole threadgroup?
2493 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002494 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002495 */
2496static int cgroup_attach_task(struct cgroup *dst_cgrp,
2497 struct task_struct *leader, bool threadgroup)
2498{
2499 LIST_HEAD(preloaded_csets);
2500 struct task_struct *task;
2501 int ret;
2502
2503 /* look up all src csets */
2504 down_read(&css_set_rwsem);
2505 rcu_read_lock();
2506 task = leader;
2507 do {
2508 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2509 &preloaded_csets);
2510 if (!threadgroup)
2511 break;
2512 } while_each_thread(leader, task);
2513 rcu_read_unlock();
2514 up_read(&css_set_rwsem);
2515
2516 /* prepare dst csets and commit */
2517 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2518 if (!ret)
2519 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2520
2521 cgroup_migrate_finish(&preloaded_csets);
2522 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002523}
2524
Tejun Heo187fe842015-06-18 16:54:28 -04002525static int cgroup_procs_write_permission(struct task_struct *task,
2526 struct cgroup *dst_cgrp,
2527 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002528{
2529 const struct cred *cred = current_cred();
2530 const struct cred *tcred = get_task_cred(task);
2531 int ret = 0;
2532
2533 /*
2534 * even if we're attaching all tasks in the thread group, we only
2535 * need to check permissions on one of them.
2536 */
2537 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2538 !uid_eq(cred->euid, tcred->uid) &&
2539 !uid_eq(cred->euid, tcred->suid))
2540 ret = -EACCES;
2541
Tejun Heo187fe842015-06-18 16:54:28 -04002542 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2543 struct super_block *sb = of->file->f_path.dentry->d_sb;
2544 struct cgroup *cgrp;
2545 struct inode *inode;
2546
2547 down_read(&css_set_rwsem);
2548 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
2549 up_read(&css_set_rwsem);
2550
2551 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2552 cgrp = cgroup_parent(cgrp);
2553
2554 ret = -ENOMEM;
2555 inode = kernfs_get_inode(sb, cgrp->procs_kn);
2556 if (inode) {
2557 ret = inode_permission(inode, MAY_WRITE);
2558 iput(inode);
2559 }
2560 }
2561
Tejun Heodedf22e2015-06-18 16:54:28 -04002562 put_cred(tcred);
2563 return ret;
2564}
2565
Ben Blum74a11662011-05-26 16:25:20 -07002566/*
2567 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002568 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002569 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002570 */
Tejun Heoacbef752014-05-13 12:16:22 -04002571static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2572 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002573{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002574 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002575 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002576 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002577 int ret;
2578
Tejun Heoacbef752014-05-13 12:16:22 -04002579 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2580 return -EINVAL;
2581
Tejun Heoe76ecae2014-05-13 12:19:23 -04002582 cgrp = cgroup_kn_lock_live(of->kn);
2583 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002584 return -ENODEV;
2585
Tejun Heo3014dde2015-09-16 13:03:02 -04002586 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002587 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002588 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002589 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002590 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002591 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002592 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002593 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002594 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002595 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002596 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002597
2598 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002599 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002600
2601 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002602 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002603 * trapped in a cpuset, or RT worker may be born in a cgroup
2604 * with no rt_runtime allocated. Just say no.
2605 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002606 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002607 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002608 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002609 }
2610
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002611 get_task_struct(tsk);
2612 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002613
Tejun Heo187fe842015-06-18 16:54:28 -04002614 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002615 if (!ret)
2616 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002617
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002618 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002619 goto out_unlock_threadgroup;
2620
2621out_unlock_rcu:
2622 rcu_read_unlock();
2623out_unlock_threadgroup:
2624 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002625 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002626 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002627}
2628
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002629/**
2630 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2631 * @from: attach to all cgroups of a given task
2632 * @tsk: the task to be attached
2633 */
2634int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2635{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002636 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002637 int retval = 0;
2638
Tejun Heo47cfcd02013-04-07 09:29:51 -07002639 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002640 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002641 struct cgroup *from_cgrp;
2642
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002643 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002644 continue;
2645
Tejun Heo96d365e2014-02-13 06:58:40 -05002646 down_read(&css_set_rwsem);
2647 from_cgrp = task_cgroup_from_root(from, root);
2648 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002649
Li Zefan6f4b7e62013-07-31 16:18:36 +08002650 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002651 if (retval)
2652 break;
2653 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002654 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002655
2656 return retval;
2657}
2658EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2659
Tejun Heoacbef752014-05-13 12:16:22 -04002660static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2661 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002662{
Tejun Heoacbef752014-05-13 12:16:22 -04002663 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002664}
2665
Tejun Heoacbef752014-05-13 12:16:22 -04002666static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2667 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002668{
Tejun Heoacbef752014-05-13 12:16:22 -04002669 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002670}
2671
Tejun Heo451af502014-05-13 12:16:21 -04002672static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2673 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002674{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002675 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002676
Tejun Heoe76ecae2014-05-13 12:19:23 -04002677 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2678
2679 cgrp = cgroup_kn_lock_live(of->kn);
2680 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002681 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002682 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002683 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2684 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002685 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002686 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002687 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002688}
2689
Tejun Heo2da8ca82013-12-05 12:28:04 -05002690static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002691{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002692 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002693
Tejun Heo46cfeb02014-05-13 12:11:00 -04002694 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002695 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002696 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002697 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002698 return 0;
2699}
2700
Tejun Heo2da8ca82013-12-05 12:28:04 -05002701static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002702{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002703 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002704 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705}
2706
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002707static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002708{
2709 struct cgroup_subsys *ss;
2710 bool printed = false;
2711 int ssid;
2712
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002713 for_each_subsys_which(ss, ssid, &ss_mask) {
2714 if (printed)
2715 seq_putc(seq, ' ');
2716 seq_printf(seq, "%s", ss->name);
2717 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002718 }
2719 if (printed)
2720 seq_putc(seq, '\n');
2721}
2722
2723/* show controllers which are currently attached to the default hierarchy */
2724static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2725{
2726 struct cgroup *cgrp = seq_css(seq)->cgroup;
2727
Tejun Heo5533e012014-05-14 19:33:07 -04002728 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2729 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002730 return 0;
2731}
2732
2733/* show controllers which are enabled from the parent */
2734static int cgroup_controllers_show(struct seq_file *seq, void *v)
2735{
2736 struct cgroup *cgrp = seq_css(seq)->cgroup;
2737
Tejun Heo667c2492014-07-08 18:02:56 -04002738 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002739 return 0;
2740}
2741
2742/* show controllers which are enabled for a given cgroup's children */
2743static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2744{
2745 struct cgroup *cgrp = seq_css(seq)->cgroup;
2746
Tejun Heo667c2492014-07-08 18:02:56 -04002747 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002748 return 0;
2749}
2750
2751/**
2752 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2753 * @cgrp: root of the subtree to update csses for
2754 *
2755 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2756 * css associations need to be updated accordingly. This function looks up
2757 * all css_sets which are attached to the subtree, creates the matching
2758 * updated css_sets and migrates the tasks to the new ones.
2759 */
2760static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2761{
2762 LIST_HEAD(preloaded_csets);
2763 struct cgroup_subsys_state *css;
2764 struct css_set *src_cset;
2765 int ret;
2766
Tejun Heof8f22e52014-04-23 11:13:16 -04002767 lockdep_assert_held(&cgroup_mutex);
2768
Tejun Heo3014dde2015-09-16 13:03:02 -04002769 percpu_down_write(&cgroup_threadgroup_rwsem);
2770
Tejun Heof8f22e52014-04-23 11:13:16 -04002771 /* look up all csses currently attached to @cgrp's subtree */
2772 down_read(&css_set_rwsem);
2773 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2774 struct cgrp_cset_link *link;
2775
2776 /* self is not affected by child_subsys_mask change */
2777 if (css->cgroup == cgrp)
2778 continue;
2779
2780 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2781 cgroup_migrate_add_src(link->cset, cgrp,
2782 &preloaded_csets);
2783 }
2784 up_read(&css_set_rwsem);
2785
2786 /* NULL dst indicates self on default hierarchy */
2787 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2788 if (ret)
2789 goto out_finish;
2790
2791 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2792 struct task_struct *last_task = NULL, *task;
2793
2794 /* src_csets precede dst_csets, break on the first dst_cset */
2795 if (!src_cset->mg_src_cgrp)
2796 break;
2797
2798 /*
2799 * All tasks in src_cset need to be migrated to the
2800 * matching dst_cset. Empty it process by process. We
2801 * walk tasks but migrate processes. The leader might even
2802 * belong to a different cset but such src_cset would also
2803 * be among the target src_csets because the default
2804 * hierarchy enforces per-process membership.
2805 */
2806 while (true) {
2807 down_read(&css_set_rwsem);
2808 task = list_first_entry_or_null(&src_cset->tasks,
2809 struct task_struct, cg_list);
2810 if (task) {
2811 task = task->group_leader;
2812 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2813 get_task_struct(task);
2814 }
2815 up_read(&css_set_rwsem);
2816
2817 if (!task)
2818 break;
2819
2820 /* guard against possible infinite loop */
2821 if (WARN(last_task == task,
2822 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2823 goto out_finish;
2824 last_task = task;
2825
Tejun Heof8f22e52014-04-23 11:13:16 -04002826 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2827
Tejun Heof8f22e52014-04-23 11:13:16 -04002828 put_task_struct(task);
2829
2830 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2831 goto out_finish;
2832 }
2833 }
2834
2835out_finish:
2836 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002837 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002838 return ret;
2839}
2840
2841/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002842static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2843 char *buf, size_t nbytes,
2844 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002845{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002846 unsigned long enable = 0, disable = 0;
2847 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002848 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002849 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002850 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002851 int ssid, ret;
2852
2853 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002854 * Parse input - space separated list of subsystem names prefixed
2855 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002856 */
Tejun Heo451af502014-05-13 12:16:21 -04002857 buf = strstrip(buf);
2858 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002859 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2860
Tejun Heod37167a2014-05-13 12:10:59 -04002861 if (tok[0] == '\0')
2862 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002863 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04002864 if (!cgroup_ssid_enabled(ssid) ||
2865 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002866 continue;
2867
2868 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002869 enable |= 1 << ssid;
2870 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002871 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002872 disable |= 1 << ssid;
2873 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002874 } else {
2875 return -EINVAL;
2876 }
2877 break;
2878 }
2879 if (ssid == CGROUP_SUBSYS_COUNT)
2880 return -EINVAL;
2881 }
2882
Tejun Heoa9746d82014-05-13 12:19:22 -04002883 cgrp = cgroup_kn_lock_live(of->kn);
2884 if (!cgrp)
2885 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002886
2887 for_each_subsys(ss, ssid) {
2888 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002889 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002890 enable &= ~(1 << ssid);
2891 continue;
2892 }
2893
Tejun Heoc29adf22014-07-08 18:02:56 -04002894 /* unavailable or not enabled on the parent? */
2895 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2896 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002897 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002898 ret = -ENOENT;
2899 goto out_unlock;
2900 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002901 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002902 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002903 disable &= ~(1 << ssid);
2904 continue;
2905 }
2906
2907 /* a child has it enabled? */
2908 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002909 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002910 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002911 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002912 }
2913 }
2914 }
2915 }
2916
2917 if (!enable && !disable) {
2918 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002919 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002920 }
2921
2922 /*
Tejun Heo667c2492014-07-08 18:02:56 -04002923 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002924 * with tasks so that child cgroups don't compete against tasks.
2925 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002926 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002927 ret = -EBUSY;
2928 goto out_unlock;
2929 }
2930
2931 /*
Tejun Heof63070d2014-07-08 18:02:57 -04002932 * Update subsys masks and calculate what needs to be done. More
2933 * subsystems than specified may need to be enabled or disabled
2934 * depending on subsystem dependencies.
2935 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05002936 old_sc = cgrp->subtree_control;
2937 old_ss = cgrp->child_subsys_mask;
2938 new_sc = (old_sc | enable) & ~disable;
2939 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04002940
Tejun Heo755bf5e2014-11-18 02:49:50 -05002941 css_enable = ~old_ss & new_ss;
2942 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04002943 enable |= css_enable;
2944 disable |= css_disable;
2945
Tejun Heodb6e3052014-11-18 02:49:51 -05002946 /*
2947 * Because css offlining is asynchronous, userland might try to
2948 * re-enable the same controller while the previous instance is
2949 * still around. In such cases, wait till it's gone using
2950 * offline_waitq.
2951 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002952 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05002953 cgroup_for_each_live_child(child, cgrp) {
2954 DEFINE_WAIT(wait);
2955
2956 if (!cgroup_css(child, ss))
2957 continue;
2958
2959 cgroup_get(child);
2960 prepare_to_wait(&child->offline_waitq, &wait,
2961 TASK_UNINTERRUPTIBLE);
2962 cgroup_kn_unlock(of->kn);
2963 schedule();
2964 finish_wait(&child->offline_waitq, &wait);
2965 cgroup_put(child);
2966
2967 return restart_syscall();
2968 }
2969 }
2970
Tejun Heo755bf5e2014-11-18 02:49:50 -05002971 cgrp->subtree_control = new_sc;
2972 cgrp->child_subsys_mask = new_ss;
2973
Tejun Heof63070d2014-07-08 18:02:57 -04002974 /*
2975 * Create new csses or make the existing ones visible. A css is
2976 * created invisible if it's being implicitly enabled through
2977 * dependency. An invisible css is made visible when the userland
2978 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04002979 */
2980 for_each_subsys(ss, ssid) {
2981 if (!(enable & (1 << ssid)))
2982 continue;
2983
2984 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04002985 if (css_enable & (1 << ssid))
2986 ret = create_css(child, ss,
2987 cgrp->subtree_control & (1 << ssid));
2988 else
2989 ret = cgroup_populate_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002990 if (ret)
2991 goto err_undo_css;
2992 }
2993 }
2994
Tejun Heoc29adf22014-07-08 18:02:56 -04002995 /*
2996 * At this point, cgroup_e_css() results reflect the new csses
2997 * making the following cgroup_update_dfl_csses() properly update
2998 * css associations of all tasks in the subtree.
2999 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003000 ret = cgroup_update_dfl_csses(cgrp);
3001 if (ret)
3002 goto err_undo_css;
3003
Tejun Heof63070d2014-07-08 18:02:57 -04003004 /*
3005 * All tasks are migrated out of disabled csses. Kill or hide
3006 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003007 * disabled while other subsystems are still depending on it. The
3008 * css must not actively control resources and be in the vanilla
3009 * state if it's made visible again later. Controllers which may
3010 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003011 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003012 for_each_subsys(ss, ssid) {
3013 if (!(disable & (1 << ssid)))
3014 continue;
3015
Tejun Heof63070d2014-07-08 18:02:57 -04003016 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003017 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3018
3019 if (css_disable & (1 << ssid)) {
3020 kill_css(css);
3021 } else {
Tejun Heof63070d2014-07-08 18:02:57 -04003022 cgroup_clear_dir(child, 1 << ssid);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003023 if (ss->css_reset)
3024 ss->css_reset(css);
3025 }
Tejun Heof63070d2014-07-08 18:02:57 -04003026 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003027 }
3028
Tejun Heo56c807b2014-11-18 02:49:51 -05003029 /*
3030 * The effective csses of all the descendants (excluding @cgrp) may
3031 * have changed. Subsystems can optionally subscribe to this event
3032 * by implementing ->css_e_css_changed() which is invoked if any of
3033 * the effective csses seen from the css's cgroup may have changed.
3034 */
3035 for_each_subsys(ss, ssid) {
3036 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
3037 struct cgroup_subsys_state *css;
3038
3039 if (!ss->css_e_css_changed || !this_css)
3040 continue;
3041
3042 css_for_each_descendant_pre(css, this_css)
3043 if (css != this_css)
3044 ss->css_e_css_changed(css);
3045 }
3046
Tejun Heof8f22e52014-04-23 11:13:16 -04003047 kernfs_activate(cgrp->kn);
3048 ret = 0;
3049out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003050 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003051 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003052
3053err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003054 cgrp->subtree_control = old_sc;
3055 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003056
3057 for_each_subsys(ss, ssid) {
3058 if (!(enable & (1 << ssid)))
3059 continue;
3060
3061 cgroup_for_each_live_child(child, cgrp) {
3062 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003063
3064 if (!css)
3065 continue;
3066
3067 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003068 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003069 else
3070 cgroup_clear_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003071 }
3072 }
3073 goto out_unlock;
3074}
3075
Tejun Heo4a07c222015-09-18 17:54:22 -04003076static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003077{
Tejun Heo4a07c222015-09-18 17:54:22 -04003078 seq_printf(seq, "populated %d\n",
3079 (bool)seq_css(seq)->cgroup->populated_cnt);
Tejun Heo842b5972014-04-25 18:28:02 -04003080 return 0;
3081}
3082
Tejun Heo2bd59d42014-02-11 11:52:49 -05003083static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3084 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003085{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003086 struct cgroup *cgrp = of->kn->parent->priv;
3087 struct cftype *cft = of->kn->priv;
3088 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003089 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003090
Tejun Heob4168642014-05-13 12:16:21 -04003091 if (cft->write)
3092 return cft->write(of, buf, nbytes, off);
3093
Tejun Heo2bd59d42014-02-11 11:52:49 -05003094 /*
3095 * kernfs guarantees that a file isn't deleted with operations in
3096 * flight, which means that the matching css is and stays alive and
3097 * doesn't need to be pinned. The RCU locking is not necessary
3098 * either. It's just for the convenience of using cgroup_css().
3099 */
3100 rcu_read_lock();
3101 css = cgroup_css(cgrp, cft->ss);
3102 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003103
Tejun Heo451af502014-05-13 12:16:21 -04003104 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003105 unsigned long long v;
3106 ret = kstrtoull(buf, 0, &v);
3107 if (!ret)
3108 ret = cft->write_u64(css, cft, v);
3109 } else if (cft->write_s64) {
3110 long long v;
3111 ret = kstrtoll(buf, 0, &v);
3112 if (!ret)
3113 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003114 } else {
3115 ret = -EINVAL;
3116 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003117
Tejun Heoa742c592013-12-05 12:28:03 -05003118 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003119}
3120
Tejun Heo6612f052013-12-05 12:28:04 -05003121static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003122{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003123 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003124}
3125
3126static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3127{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003128 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003129}
3130
3131static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3132{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003133 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003134}
3135
3136static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3137{
Tejun Heo7da11272013-12-05 12:28:04 -05003138 struct cftype *cft = seq_cft(m);
3139 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003140
Tejun Heo2da8ca82013-12-05 12:28:04 -05003141 if (cft->seq_show)
3142 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003143
Tejun Heo896f5192013-12-05 12:28:04 -05003144 if (cft->read_u64)
3145 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3146 else if (cft->read_s64)
3147 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3148 else
3149 return -EINVAL;
3150 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003151}
3152
Tejun Heo2bd59d42014-02-11 11:52:49 -05003153static struct kernfs_ops cgroup_kf_single_ops = {
3154 .atomic_write_len = PAGE_SIZE,
3155 .write = cgroup_file_write,
3156 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003157};
3158
Tejun Heo2bd59d42014-02-11 11:52:49 -05003159static struct kernfs_ops cgroup_kf_ops = {
3160 .atomic_write_len = PAGE_SIZE,
3161 .write = cgroup_file_write,
3162 .seq_start = cgroup_seqfile_start,
3163 .seq_next = cgroup_seqfile_next,
3164 .seq_stop = cgroup_seqfile_stop,
3165 .seq_show = cgroup_seqfile_show,
3166};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003167
3168/*
3169 * cgroup_rename - Only allow simple rename of directories in place.
3170 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003171static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3172 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003173{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003174 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003175 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003176
Tejun Heo2bd59d42014-02-11 11:52:49 -05003177 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003178 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003179 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003180 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003181
Tejun Heo6db8e852013-06-14 11:18:22 -07003182 /*
3183 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003184 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003185 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003186 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003187 return -EPERM;
3188
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003189 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003190 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003191 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003192 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003193 */
3194 kernfs_break_active_protection(new_parent);
3195 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003196
Tejun Heo2bd59d42014-02-11 11:52:49 -05003197 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003198
Tejun Heo2bd59d42014-02-11 11:52:49 -05003199 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003200
Tejun Heo2bd59d42014-02-11 11:52:49 -05003201 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003202
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003203 kernfs_unbreak_active_protection(kn);
3204 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003205 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003206}
3207
Tejun Heo49957f82014-04-07 16:44:47 -04003208/* set uid and gid of cgroup dirs and files to that of the creator */
3209static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3210{
3211 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3212 .ia_uid = current_fsuid(),
3213 .ia_gid = current_fsgid(), };
3214
3215 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3216 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3217 return 0;
3218
3219 return kernfs_setattr(kn, &iattr);
3220}
3221
Tejun Heo2bb566c2013-08-08 20:11:23 -04003222static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003223{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003224 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003225 struct kernfs_node *kn;
3226 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003227 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003228
Tejun Heo2bd59d42014-02-11 11:52:49 -05003229#ifdef CONFIG_DEBUG_LOCK_ALLOC
3230 key = &cft->lockdep_key;
3231#endif
3232 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3233 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003234 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003235 if (IS_ERR(kn))
3236 return PTR_ERR(kn);
3237
3238 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003239 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003240 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003241 return ret;
3242 }
3243
Tejun Heo187fe842015-06-18 16:54:28 -04003244 if (cft->write == cgroup_procs_write)
3245 cgrp->procs_kn = kn;
Tejun Heo4a07c222015-09-18 17:54:22 -04003246 else if (cft->seq_show == cgroup_events_show)
3247 cgrp->events_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04003248 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003249}
3250
Tejun Heob1f28d32013-06-28 16:24:10 -07003251/**
3252 * cgroup_addrm_files - add or remove files to a cgroup directory
3253 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07003254 * @cfts: array of cftypes to be added
3255 * @is_add: whether to add or remove
3256 *
3257 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04003258 * For removals, this function never fails. If addition fails, this
3259 * function doesn't remove files already added. The caller is responsible
3260 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07003261 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04003262static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
3263 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003264{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003265 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07003266 int ret;
3267
Tejun Heo01f64742014-05-13 12:19:23 -04003268 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003269
3270 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003271 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003272 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003273 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003274 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003275 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003276 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003277 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003278 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003279 continue;
3280
Li Zefan2739d3c2013-01-21 18:18:33 +08003281 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003282 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003283 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003284 pr_warn("%s: failed to add %s, err=%d\n",
3285 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07003286 return ret;
3287 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003288 } else {
3289 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003290 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003291 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003292 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003293}
3294
Tejun Heo21a2d342014-02-12 09:29:49 -05003295static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003296{
3297 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003298 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003299 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003300 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003301 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003302
Tejun Heo01f64742014-05-13 12:19:23 -04003303 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003304
Li Zefane8c82d22013-06-18 18:48:37 +08003305 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003306 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003307 struct cgroup *cgrp = css->cgroup;
3308
Li Zefane8c82d22013-06-18 18:48:37 +08003309 if (cgroup_is_dead(cgrp))
3310 continue;
3311
Tejun Heo21a2d342014-02-12 09:29:49 -05003312 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003313 if (ret)
3314 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003315 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003316
3317 if (is_add && !ret)
3318 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003319 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003320}
3321
Tejun Heo2da440a2014-02-11 11:52:48 -05003322static void cgroup_exit_cftypes(struct cftype *cfts)
3323{
3324 struct cftype *cft;
3325
Tejun Heo2bd59d42014-02-11 11:52:49 -05003326 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3327 /* free copy for custom atomic_write_len, see init_cftypes() */
3328 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3329 kfree(cft->kf_ops);
3330 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003331 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003332
3333 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003334 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003335 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003336}
3337
Tejun Heo2bd59d42014-02-11 11:52:49 -05003338static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003339{
3340 struct cftype *cft;
3341
Tejun Heo2bd59d42014-02-11 11:52:49 -05003342 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3343 struct kernfs_ops *kf_ops;
3344
Tejun Heo0adb0702014-02-12 09:29:48 -05003345 WARN_ON(cft->ss || cft->kf_ops);
3346
Tejun Heo2bd59d42014-02-11 11:52:49 -05003347 if (cft->seq_start)
3348 kf_ops = &cgroup_kf_ops;
3349 else
3350 kf_ops = &cgroup_kf_single_ops;
3351
3352 /*
3353 * Ugh... if @cft wants a custom max_write_len, we need to
3354 * make a copy of kf_ops to set its atomic_write_len.
3355 */
3356 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3357 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3358 if (!kf_ops) {
3359 cgroup_exit_cftypes(cfts);
3360 return -ENOMEM;
3361 }
3362 kf_ops->atomic_write_len = cft->max_write_len;
3363 }
3364
3365 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003366 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003367 }
3368
3369 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003370}
3371
Tejun Heo21a2d342014-02-12 09:29:49 -05003372static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3373{
Tejun Heo01f64742014-05-13 12:19:23 -04003374 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003375
3376 if (!cfts || !cfts[0].ss)
3377 return -ENOENT;
3378
3379 list_del(&cfts->node);
3380 cgroup_apply_cftypes(cfts, false);
3381 cgroup_exit_cftypes(cfts);
3382 return 0;
3383}
3384
Tejun Heo8e3f6542012-04-01 12:09:55 -07003385/**
Tejun Heo80b13582014-02-12 09:29:48 -05003386 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3387 * @cfts: zero-length name terminated array of cftypes
3388 *
3389 * Unregister @cfts. Files described by @cfts are removed from all
3390 * existing cgroups and all future cgroups won't have them either. This
3391 * function can be called anytime whether @cfts' subsys is attached or not.
3392 *
3393 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3394 * registered.
3395 */
3396int cgroup_rm_cftypes(struct cftype *cfts)
3397{
Tejun Heo21a2d342014-02-12 09:29:49 -05003398 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003399
Tejun Heo01f64742014-05-13 12:19:23 -04003400 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003401 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003402 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003403 return ret;
3404}
3405
3406/**
3407 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3408 * @ss: target cgroup subsystem
3409 * @cfts: zero-length name terminated array of cftypes
3410 *
3411 * Register @cfts to @ss. Files described by @cfts are created for all
3412 * existing cgroups to which @ss is attached and all future cgroups will
3413 * have them too. This function can be called anytime whether @ss is
3414 * attached or not.
3415 *
3416 * Returns 0 on successful registration, -errno on failure. Note that this
3417 * function currently returns 0 as long as @cfts registration is successful
3418 * even if some file creation attempts on existing cgroups fail.
3419 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003420static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003421{
Tejun Heo9ccece82013-06-28 16:24:11 -07003422 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003423
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003424 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003425 return 0;
3426
Li Zefandc5736e2014-02-17 10:41:50 +08003427 if (!cfts || cfts[0].name[0] == '\0')
3428 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003429
Tejun Heo2bd59d42014-02-11 11:52:49 -05003430 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003431 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003432 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003433
Tejun Heo01f64742014-05-13 12:19:23 -04003434 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003435
Tejun Heo0adb0702014-02-12 09:29:48 -05003436 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003437 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003438 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003439 cgroup_rm_cftypes_locked(cfts);
3440
Tejun Heo01f64742014-05-13 12:19:23 -04003441 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003442 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003443}
Tejun Heo79578622012-04-01 12:09:56 -07003444
3445/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003446 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3447 * @ss: target cgroup subsystem
3448 * @cfts: zero-length name terminated array of cftypes
3449 *
3450 * Similar to cgroup_add_cftypes() but the added files are only used for
3451 * the default hierarchy.
3452 */
3453int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3454{
3455 struct cftype *cft;
3456
3457 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003458 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003459 return cgroup_add_cftypes(ss, cfts);
3460}
3461
3462/**
3463 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3464 * @ss: target cgroup subsystem
3465 * @cfts: zero-length name terminated array of cftypes
3466 *
3467 * Similar to cgroup_add_cftypes() but the added files are only used for
3468 * the legacy hierarchies.
3469 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003470int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3471{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003472 struct cftype *cft;
3473
Vivek Goyalfa8137b2014-08-08 11:44:03 -04003474 /*
3475 * If legacy_flies_on_dfl, we want to show the legacy files on the
3476 * dfl hierarchy but iff the target subsystem hasn't been updated
3477 * for the dfl hierarchy yet.
3478 */
3479 if (!cgroup_legacy_files_on_dfl ||
3480 ss->dfl_cftypes != ss->legacy_cftypes) {
3481 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3482 cft->flags |= __CFTYPE_NOT_ON_DFL;
3483 }
3484
Tejun Heo2cf669a2014-07-15 11:05:09 -04003485 return cgroup_add_cftypes(ss, cfts);
3486}
3487
Li Zefana043e3b2008-02-23 15:24:09 -08003488/**
3489 * cgroup_task_count - count the number of tasks in a cgroup.
3490 * @cgrp: the cgroup in question
3491 *
3492 * Return the number of tasks in the cgroup.
3493 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003494static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003495{
3496 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003497 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003498
Tejun Heo96d365e2014-02-13 06:58:40 -05003499 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003500 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3501 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003502 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003503 return count;
3504}
3505
Tejun Heo574bd9f2012-11-09 09:12:29 -08003506/**
Tejun Heo492eb212013-08-08 20:11:25 -04003507 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003508 * @pos: the current position (%NULL to initiate traversal)
3509 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003510 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003511 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003512 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003513 * that @parent and @pos are accessible. The next sibling is guaranteed to
3514 * be returned regardless of their states.
3515 *
3516 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3517 * css which finished ->css_online() is guaranteed to be visible in the
3518 * future iterations and will stay visible until the last reference is put.
3519 * A css which hasn't finished ->css_online() or already finished
3520 * ->css_offline() may show up during traversal. It's each subsystem's
3521 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003522 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003523struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3524 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003525{
Tejun Heoc2931b72014-05-16 13:22:51 -04003526 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003527
Tejun Heo8353da12014-05-13 12:19:23 -04003528 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003529
3530 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003531 * @pos could already have been unlinked from the sibling list.
3532 * Once a cgroup is removed, its ->sibling.next is no longer
3533 * updated when its next sibling changes. CSS_RELEASED is set when
3534 * @pos is taken off list, at which time its next pointer is valid,
3535 * and, as releases are serialized, the one pointed to by the next
3536 * pointer is guaranteed to not have started release yet. This
3537 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3538 * critical section, the one pointed to by its next pointer is
3539 * guaranteed to not have finished its RCU grace period even if we
3540 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003541 *
Tejun Heode3f0342014-05-16 13:22:49 -04003542 * If @pos has CSS_RELEASED set, its next pointer can't be
3543 * dereferenced; however, as each css is given a monotonically
3544 * increasing unique serial number and always appended to the
3545 * sibling list, the next one can be found by walking the parent's
3546 * children until the first css with higher serial number than
3547 * @pos's. While this path can be slower, it happens iff iteration
3548 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003549 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003550 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003551 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3552 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3553 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003554 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003555 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003556 if (next->serial_nr > pos->serial_nr)
3557 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003558 }
3559
Tejun Heo3b281af2014-04-23 11:13:15 -04003560 /*
3561 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003562 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003563 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003564 if (&next->sibling != &parent->children)
3565 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003566 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003567}
Tejun Heo53fa5262013-05-24 10:55:38 +09003568
3569/**
Tejun Heo492eb212013-08-08 20:11:25 -04003570 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003571 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003572 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003573 *
Tejun Heo492eb212013-08-08 20:11:25 -04003574 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003575 * to visit for pre-order traversal of @root's descendants. @root is
3576 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003577 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003578 * While this function requires cgroup_mutex or RCU read locking, it
3579 * doesn't require the whole traversal to be contained in a single critical
3580 * section. This function will return the correct next descendant as long
3581 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003582 *
3583 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3584 * css which finished ->css_online() is guaranteed to be visible in the
3585 * future iterations and will stay visible until the last reference is put.
3586 * A css which hasn't finished ->css_online() or already finished
3587 * ->css_offline() may show up during traversal. It's each subsystem's
3588 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003589 */
Tejun Heo492eb212013-08-08 20:11:25 -04003590struct cgroup_subsys_state *
3591css_next_descendant_pre(struct cgroup_subsys_state *pos,
3592 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003593{
Tejun Heo492eb212013-08-08 20:11:25 -04003594 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003595
Tejun Heo8353da12014-05-13 12:19:23 -04003596 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003597
Tejun Heobd8815a2013-08-08 20:11:27 -04003598 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003599 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003600 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003601
3602 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003603 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003604 if (next)
3605 return next;
3606
3607 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003608 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003609 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003610 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003611 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003612 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003613 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003614
3615 return NULL;
3616}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003617
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003618/**
Tejun Heo492eb212013-08-08 20:11:25 -04003619 * css_rightmost_descendant - return the rightmost descendant of a css
3620 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003621 *
Tejun Heo492eb212013-08-08 20:11:25 -04003622 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3623 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003624 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003625 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003626 * While this function requires cgroup_mutex or RCU read locking, it
3627 * doesn't require the whole traversal to be contained in a single critical
3628 * section. This function will return the correct rightmost descendant as
3629 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003630 */
Tejun Heo492eb212013-08-08 20:11:25 -04003631struct cgroup_subsys_state *
3632css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003633{
Tejun Heo492eb212013-08-08 20:11:25 -04003634 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003635
Tejun Heo8353da12014-05-13 12:19:23 -04003636 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003637
3638 do {
3639 last = pos;
3640 /* ->prev isn't RCU safe, walk ->next till the end */
3641 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003642 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003643 pos = tmp;
3644 } while (pos);
3645
3646 return last;
3647}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003648
Tejun Heo492eb212013-08-08 20:11:25 -04003649static struct cgroup_subsys_state *
3650css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003651{
Tejun Heo492eb212013-08-08 20:11:25 -04003652 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003653
3654 do {
3655 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003656 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003657 } while (pos);
3658
3659 return last;
3660}
3661
3662/**
Tejun Heo492eb212013-08-08 20:11:25 -04003663 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003664 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003665 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003666 *
Tejun Heo492eb212013-08-08 20:11:25 -04003667 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003668 * to visit for post-order traversal of @root's descendants. @root is
3669 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003670 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003671 * While this function requires cgroup_mutex or RCU read locking, it
3672 * doesn't require the whole traversal to be contained in a single critical
3673 * section. This function will return the correct next descendant as long
3674 * as both @pos and @cgroup are accessible and @pos is a descendant of
3675 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003676 *
3677 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3678 * css which finished ->css_online() is guaranteed to be visible in the
3679 * future iterations and will stay visible until the last reference is put.
3680 * A css which hasn't finished ->css_online() or already finished
3681 * ->css_offline() may show up during traversal. It's each subsystem's
3682 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003683 */
Tejun Heo492eb212013-08-08 20:11:25 -04003684struct cgroup_subsys_state *
3685css_next_descendant_post(struct cgroup_subsys_state *pos,
3686 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003687{
Tejun Heo492eb212013-08-08 20:11:25 -04003688 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003689
Tejun Heo8353da12014-05-13 12:19:23 -04003690 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003691
Tejun Heo58b79a92013-09-06 15:31:08 -04003692 /* if first iteration, visit leftmost descendant which may be @root */
3693 if (!pos)
3694 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003695
Tejun Heobd8815a2013-08-08 20:11:27 -04003696 /* if we visited @root, we're done */
3697 if (pos == root)
3698 return NULL;
3699
Tejun Heo574bd9f2012-11-09 09:12:29 -08003700 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003701 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003702 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003703 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003704
3705 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003706 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003707}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003708
Tejun Heof3d46502014-05-16 13:22:52 -04003709/**
3710 * css_has_online_children - does a css have online children
3711 * @css: the target css
3712 *
3713 * Returns %true if @css has any online children; otherwise, %false. This
3714 * function can be called from any context but the caller is responsible
3715 * for synchronizing against on/offlining as necessary.
3716 */
3717bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003718{
Tejun Heof3d46502014-05-16 13:22:52 -04003719 struct cgroup_subsys_state *child;
3720 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003721
3722 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003723 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003724 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003725 ret = true;
3726 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003727 }
3728 }
3729 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003730 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003731}
3732
Tejun Heo0942eee2013-08-08 20:11:26 -04003733/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003734 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003735 * @it: the iterator to advance
3736 *
3737 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003738 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003739static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003740{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003741 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003742 struct cgrp_cset_link *link;
3743 struct css_set *cset;
3744
3745 /* Advance to the next non-empty css_set */
3746 do {
3747 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003748 if (l == it->cset_head) {
3749 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003750 return;
3751 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003752
3753 if (it->ss) {
3754 cset = container_of(l, struct css_set,
3755 e_cset_node[it->ss->id]);
3756 } else {
3757 link = list_entry(l, struct cgrp_cset_link, cset_link);
3758 cset = link->cset;
3759 }
Tejun Heoc7561122014-02-25 10:04:01 -05003760 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3761
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003762 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003763
3764 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003765 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003766 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003767 it->task_pos = cset->mg_tasks.next;
3768
3769 it->tasks_head = &cset->tasks;
3770 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003771}
3772
Tejun Heo0942eee2013-08-08 20:11:26 -04003773/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003774 * css_task_iter_start - initiate task iteration
3775 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003776 * @it: the task iterator to use
3777 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003778 * Initiate iteration through the tasks of @css. The caller can call
3779 * css_task_iter_next() to walk through the tasks until the function
3780 * returns NULL. On completion of iteration, css_task_iter_end() must be
3781 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003782 *
3783 * Note that this function acquires a lock which is released when the
3784 * iteration finishes. The caller can't sleep while iteration is in
3785 * progress.
3786 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003787void css_task_iter_start(struct cgroup_subsys_state *css,
3788 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003789 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003790{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003791 /* no one should try to iterate before mounting cgroups */
3792 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003793
Tejun Heo96d365e2014-02-13 06:58:40 -05003794 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003795
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003796 it->ss = css->ss;
3797
3798 if (it->ss)
3799 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3800 else
3801 it->cset_pos = &css->cgroup->cset_links;
3802
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003803 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003804
Tejun Heo72ec7022013-08-08 20:11:26 -04003805 css_advance_task_iter(it);
Paul Menagebd89aab2007-10-18 23:40:44 -07003806}
Paul Menage817929e2007-10-18 23:39:36 -07003807
Tejun Heo0942eee2013-08-08 20:11:26 -04003808/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003809 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003810 * @it: the task iterator being iterated
3811 *
3812 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003813 * initialized via css_task_iter_start(). Returns NULL when the iteration
3814 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003815 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003816struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003817{
3818 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003819 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003820
3821 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003822 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003823 return NULL;
3824 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003825
3826 /*
3827 * Advance iterator to find next entry. cset->tasks is consumed
3828 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3829 * next cset.
3830 */
Paul Menage817929e2007-10-18 23:39:36 -07003831 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003832
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003833 if (l == it->tasks_head)
3834 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003835
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003836 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003837 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003838 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003839 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003840
Paul Menage817929e2007-10-18 23:39:36 -07003841 return res;
3842}
3843
Tejun Heo0942eee2013-08-08 20:11:26 -04003844/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003845 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003846 * @it: the task iterator to finish
3847 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003848 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003849 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003850void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003851 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003852{
Tejun Heo96d365e2014-02-13 06:58:40 -05003853 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003854}
3855
3856/**
3857 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3858 * @to: cgroup to which the tasks will be moved
3859 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003860 *
3861 * Locking rules between cgroup_post_fork() and the migration path
3862 * guarantee that, if a task is forking while being migrated, the new child
3863 * is guaranteed to be either visible in the source cgroup after the
3864 * parent's migration is complete or put into the target cgroup. No task
3865 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003866 */
3867int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3868{
Tejun Heo952aaa12014-02-25 10:04:03 -05003869 LIST_HEAD(preloaded_csets);
3870 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003871 struct css_task_iter it;
3872 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003873 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003874
Tejun Heo952aaa12014-02-25 10:04:03 -05003875 mutex_lock(&cgroup_mutex);
3876
3877 /* all tasks in @from are being moved, all csets are source */
3878 down_read(&css_set_rwsem);
3879 list_for_each_entry(link, &from->cset_links, cset_link)
3880 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3881 up_read(&css_set_rwsem);
3882
3883 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3884 if (ret)
3885 goto out_err;
3886
3887 /*
3888 * Migrate tasks one-by-one until @form is empty. This fails iff
3889 * ->can_attach() fails.
3890 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003891 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003892 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003893 task = css_task_iter_next(&it);
3894 if (task)
3895 get_task_struct(task);
3896 css_task_iter_end(&it);
3897
3898 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003899 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003900 put_task_struct(task);
3901 }
3902 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003903out_err:
3904 cgroup_migrate_finish(&preloaded_csets);
3905 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003906 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003907}
3908
Paul Menage817929e2007-10-18 23:39:36 -07003909/*
Ben Blum102a7752009-09-23 15:56:26 -07003910 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003911 *
3912 * Reading this file can return large amounts of data if a cgroup has
3913 * *lots* of attached tasks. So it may need several calls to read(),
3914 * but we cannot guarantee that the information we produce is correct
3915 * unless we produce it entirely atomically.
3916 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003917 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003918
Li Zefan24528252012-01-20 11:58:43 +08003919/* which pidlist file are we talking about? */
3920enum cgroup_filetype {
3921 CGROUP_FILE_PROCS,
3922 CGROUP_FILE_TASKS,
3923};
3924
3925/*
3926 * A pidlist is a list of pids that virtually represents the contents of one
3927 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3928 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3929 * to the cgroup.
3930 */
3931struct cgroup_pidlist {
3932 /*
3933 * used to find which pidlist is wanted. doesn't change as long as
3934 * this particular list stays in the list.
3935 */
3936 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3937 /* array of xids */
3938 pid_t *list;
3939 /* how many elements the above list has */
3940 int length;
Li Zefan24528252012-01-20 11:58:43 +08003941 /* each of these stored in a list by its cgroup */
3942 struct list_head links;
3943 /* pointer to the cgroup we belong to, for list removal purposes */
3944 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003945 /* for delayed destruction */
3946 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003947};
3948
Paul Menagebbcb81d2007-10-18 23:39:32 -07003949/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003950 * The following two functions "fix" the issue where there are more pids
3951 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3952 * TODO: replace with a kernel-wide solution to this problem
3953 */
3954#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3955static void *pidlist_allocate(int count)
3956{
3957 if (PIDLIST_TOO_LARGE(count))
3958 return vmalloc(count * sizeof(pid_t));
3959 else
3960 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3961}
Tejun Heob1a21362013-11-29 10:42:58 -05003962
Ben Blumd1d9fd32009-09-23 15:56:28 -07003963static void pidlist_free(void *p)
3964{
Bandan Das58794512015-03-02 17:51:10 -05003965 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003966}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003967
3968/*
Tejun Heob1a21362013-11-29 10:42:58 -05003969 * Used to destroy all pidlists lingering waiting for destroy timer. None
3970 * should be left afterwards.
3971 */
3972static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3973{
3974 struct cgroup_pidlist *l, *tmp_l;
3975
3976 mutex_lock(&cgrp->pidlist_mutex);
3977 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3978 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3979 mutex_unlock(&cgrp->pidlist_mutex);
3980
3981 flush_workqueue(cgroup_pidlist_destroy_wq);
3982 BUG_ON(!list_empty(&cgrp->pidlists));
3983}
3984
3985static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3986{
3987 struct delayed_work *dwork = to_delayed_work(work);
3988 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3989 destroy_dwork);
3990 struct cgroup_pidlist *tofree = NULL;
3991
3992 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003993
3994 /*
Tejun Heo04502362013-11-29 10:42:59 -05003995 * Destroy iff we didn't get queued again. The state won't change
3996 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003997 */
Tejun Heo04502362013-11-29 10:42:59 -05003998 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003999 list_del(&l->links);
4000 pidlist_free(l->list);
4001 put_pid_ns(l->key.ns);
4002 tofree = l;
4003 }
4004
Tejun Heob1a21362013-11-29 10:42:58 -05004005 mutex_unlock(&l->owner->pidlist_mutex);
4006 kfree(tofree);
4007}
4008
4009/*
Ben Blum102a7752009-09-23 15:56:26 -07004010 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004011 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004012 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004013static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004014{
Ben Blum102a7752009-09-23 15:56:26 -07004015 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004016
4017 /*
4018 * we presume the 0th element is unique, so i starts at 1. trivial
4019 * edge cases first; no work needs to be done for either
4020 */
4021 if (length == 0 || length == 1)
4022 return length;
4023 /* src and dest walk down the list; dest counts unique elements */
4024 for (src = 1; src < length; src++) {
4025 /* find next unique element */
4026 while (list[src] == list[src-1]) {
4027 src++;
4028 if (src == length)
4029 goto after;
4030 }
4031 /* dest always points to where the next unique element goes */
4032 list[dest] = list[src];
4033 dest++;
4034 }
4035after:
Ben Blum102a7752009-09-23 15:56:26 -07004036 return dest;
4037}
4038
Tejun Heoafb2bc12013-11-29 10:42:59 -05004039/*
4040 * The two pid files - task and cgroup.procs - guaranteed that the result
4041 * is sorted, which forced this whole pidlist fiasco. As pid order is
4042 * different per namespace, each namespace needs differently sorted list,
4043 * making it impossible to use, for example, single rbtree of member tasks
4044 * sorted by task pointer. As pidlists can be fairly large, allocating one
4045 * per open file is dangerous, so cgroup had to implement shared pool of
4046 * pidlists keyed by cgroup and namespace.
4047 *
4048 * All this extra complexity was caused by the original implementation
4049 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004050 * want to do away with it. Explicitly scramble sort order if on the
4051 * default hierarchy so that no such expectation exists in the new
4052 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004053 *
4054 * Scrambling is done by swapping every two consecutive bits, which is
4055 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4056 */
4057static pid_t pid_fry(pid_t pid)
4058{
4059 unsigned a = pid & 0x55555555;
4060 unsigned b = pid & 0xAAAAAAAA;
4061
4062 return (a << 1) | (b >> 1);
4063}
4064
4065static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4066{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004067 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004068 return pid_fry(pid);
4069 else
4070 return pid;
4071}
4072
Ben Blum102a7752009-09-23 15:56:26 -07004073static int cmppid(const void *a, const void *b)
4074{
4075 return *(pid_t *)a - *(pid_t *)b;
4076}
4077
Tejun Heoafb2bc12013-11-29 10:42:59 -05004078static int fried_cmppid(const void *a, const void *b)
4079{
4080 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4081}
4082
Ben Blum72a8cb32009-09-23 15:56:27 -07004083static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4084 enum cgroup_filetype type)
4085{
4086 struct cgroup_pidlist *l;
4087 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004088 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004089
Tejun Heoe6b81712013-11-29 10:42:59 -05004090 lockdep_assert_held(&cgrp->pidlist_mutex);
4091
4092 list_for_each_entry(l, &cgrp->pidlists, links)
4093 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004094 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004095 return NULL;
4096}
4097
Ben Blum72a8cb32009-09-23 15:56:27 -07004098/*
4099 * find the appropriate pidlist for our purpose (given procs vs tasks)
4100 * returns with the lock on that pidlist already held, and takes care
4101 * of the use count, or returns NULL with no locks held if we're out of
4102 * memory.
4103 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004104static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4105 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004106{
4107 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004108
Tejun Heoe6b81712013-11-29 10:42:59 -05004109 lockdep_assert_held(&cgrp->pidlist_mutex);
4110
4111 l = cgroup_pidlist_find(cgrp, type);
4112 if (l)
4113 return l;
4114
Ben Blum72a8cb32009-09-23 15:56:27 -07004115 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004116 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004117 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004118 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004119
Tejun Heob1a21362013-11-29 10:42:58 -05004120 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004121 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004122 /* don't need task_nsproxy() if we're looking at ourself */
4123 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004124 l->owner = cgrp;
4125 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004126 return l;
4127}
4128
4129/*
Ben Blum102a7752009-09-23 15:56:26 -07004130 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4131 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004132static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4133 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004134{
4135 pid_t *array;
4136 int length;
4137 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004138 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004139 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004140 struct cgroup_pidlist *l;
4141
Tejun Heo4bac00d2013-11-29 10:42:59 -05004142 lockdep_assert_held(&cgrp->pidlist_mutex);
4143
Ben Blum102a7752009-09-23 15:56:26 -07004144 /*
4145 * If cgroup gets more users after we read count, we won't have
4146 * enough space - tough. This race is indistinguishable to the
4147 * caller from the case that the additional cgroup users didn't
4148 * show up until sometime later on.
4149 */
4150 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004151 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004152 if (!array)
4153 return -ENOMEM;
4154 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004155 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004156 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004157 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004158 break;
Ben Blum102a7752009-09-23 15:56:26 -07004159 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004160 if (type == CGROUP_FILE_PROCS)
4161 pid = task_tgid_vnr(tsk);
4162 else
4163 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004164 if (pid > 0) /* make sure to only use valid results */
4165 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004166 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004167 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004168 length = n;
4169 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004170 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004171 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4172 else
4173 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004174 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004175 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004176
Tejun Heoe6b81712013-11-29 10:42:59 -05004177 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004178 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004179 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004180 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004181 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004182
4183 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004184 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004185 l->list = array;
4186 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004187 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004188 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004189}
4190
Balbir Singh846c7bb2007-10-18 23:39:44 -07004191/**
Li Zefana043e3b2008-02-23 15:24:09 -08004192 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004193 * @stats: cgroupstats to fill information into
4194 * @dentry: A dentry entry belonging to the cgroup for which stats have
4195 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004196 *
4197 * Build and fill cgroupstats so that taskstats can export it to user
4198 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004199 */
4200int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4201{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004202 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004203 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004204 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004205 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004206
Tejun Heo2bd59d42014-02-11 11:52:49 -05004207 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4208 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4209 kernfs_type(kn) != KERNFS_DIR)
4210 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004211
Li Zefanbad34662014-02-14 16:54:28 +08004212 mutex_lock(&cgroup_mutex);
4213
Tejun Heo2bd59d42014-02-11 11:52:49 -05004214 /*
4215 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004216 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004217 * @kn->priv is RCU safe. Let's do the RCU dancing.
4218 */
4219 rcu_read_lock();
4220 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004221 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004222 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004223 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004224 return -ENOENT;
4225 }
Li Zefanbad34662014-02-14 16:54:28 +08004226 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004227
Tejun Heo9d800df2014-05-14 09:15:00 -04004228 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004229 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004230 switch (tsk->state) {
4231 case TASK_RUNNING:
4232 stats->nr_running++;
4233 break;
4234 case TASK_INTERRUPTIBLE:
4235 stats->nr_sleeping++;
4236 break;
4237 case TASK_UNINTERRUPTIBLE:
4238 stats->nr_uninterruptible++;
4239 break;
4240 case TASK_STOPPED:
4241 stats->nr_stopped++;
4242 break;
4243 default:
4244 if (delayacct_is_task_waiting_on_io(tsk))
4245 stats->nr_io_wait++;
4246 break;
4247 }
4248 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004249 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004250
Li Zefanbad34662014-02-14 16:54:28 +08004251 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004252 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004253}
4254
Paul Menage8f3ff202009-09-23 15:56:25 -07004255
Paul Menagecc31edc2008-10-18 20:28:04 -07004256/*
Ben Blum102a7752009-09-23 15:56:26 -07004257 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004258 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004259 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004260 */
4261
Ben Blum102a7752009-09-23 15:56:26 -07004262static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004263{
4264 /*
4265 * Initially we receive a position value that corresponds to
4266 * one more than the last pid shown (or 0 on the first call or
4267 * after a seek to the start). Use a binary-search to find the
4268 * next pid to display, if any
4269 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004270 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004271 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004272 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004273 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004274 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004275 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004276
Tejun Heo4bac00d2013-11-29 10:42:59 -05004277 mutex_lock(&cgrp->pidlist_mutex);
4278
4279 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004280 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004281 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004282 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004283 * could already have been destroyed.
4284 */
Tejun Heo5d224442013-12-05 12:28:04 -05004285 if (of->priv)
4286 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004287
4288 /*
4289 * Either this is the first start() after open or the matching
4290 * pidlist has been destroyed inbetween. Create a new one.
4291 */
Tejun Heo5d224442013-12-05 12:28:04 -05004292 if (!of->priv) {
4293 ret = pidlist_array_load(cgrp, type,
4294 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004295 if (ret)
4296 return ERR_PTR(ret);
4297 }
Tejun Heo5d224442013-12-05 12:28:04 -05004298 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004299
Paul Menagecc31edc2008-10-18 20:28:04 -07004300 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004301 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004302
Paul Menagecc31edc2008-10-18 20:28:04 -07004303 while (index < end) {
4304 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004305 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004306 index = mid;
4307 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004308 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004309 index = mid + 1;
4310 else
4311 end = mid;
4312 }
4313 }
4314 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004315 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004316 return NULL;
4317 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004318 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004319 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004320 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004321}
4322
Ben Blum102a7752009-09-23 15:56:26 -07004323static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004324{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004325 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004326 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004327
Tejun Heo5d224442013-12-05 12:28:04 -05004328 if (l)
4329 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004330 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004331 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004332}
4333
Ben Blum102a7752009-09-23 15:56:26 -07004334static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004335{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004336 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004337 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004338 pid_t *p = v;
4339 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004340 /*
4341 * Advance to the next pid in the array. If this goes off the
4342 * end, we're done
4343 */
4344 p++;
4345 if (p >= end) {
4346 return NULL;
4347 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004348 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004349 return p;
4350 }
4351}
4352
Ben Blum102a7752009-09-23 15:56:26 -07004353static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004354{
Joe Perches94ff2122015-04-15 16:18:20 -07004355 seq_printf(s, "%d\n", *(int *)v);
4356
4357 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004358}
4359
Tejun Heo182446d2013-08-08 20:11:24 -04004360static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4361 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004362{
Tejun Heo182446d2013-08-08 20:11:24 -04004363 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004364}
4365
Tejun Heo182446d2013-08-08 20:11:24 -04004366static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4367 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004368{
Paul Menage6379c102008-07-25 01:47:01 -07004369 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004370 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004371 else
Tejun Heo182446d2013-08-08 20:11:24 -04004372 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004373 return 0;
4374}
4375
Tejun Heo182446d2013-08-08 20:11:24 -04004376static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4377 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004378{
Tejun Heo182446d2013-08-08 20:11:24 -04004379 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004380}
4381
Tejun Heo182446d2013-08-08 20:11:24 -04004382static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4383 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004384{
4385 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004386 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004387 else
Tejun Heo182446d2013-08-08 20:11:24 -04004388 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004389 return 0;
4390}
4391
Tejun Heoa14c6872014-07-15 11:05:09 -04004392/* cgroup core interface files for the default hierarchy */
4393static struct cftype cgroup_dfl_base_files[] = {
4394 {
4395 .name = "cgroup.procs",
4396 .seq_start = cgroup_pidlist_start,
4397 .seq_next = cgroup_pidlist_next,
4398 .seq_stop = cgroup_pidlist_stop,
4399 .seq_show = cgroup_pidlist_show,
4400 .private = CGROUP_FILE_PROCS,
4401 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004402 },
4403 {
4404 .name = "cgroup.controllers",
4405 .flags = CFTYPE_ONLY_ON_ROOT,
4406 .seq_show = cgroup_root_controllers_show,
4407 },
4408 {
4409 .name = "cgroup.controllers",
4410 .flags = CFTYPE_NOT_ON_ROOT,
4411 .seq_show = cgroup_controllers_show,
4412 },
4413 {
4414 .name = "cgroup.subtree_control",
4415 .seq_show = cgroup_subtree_control_show,
4416 .write = cgroup_subtree_control_write,
4417 },
4418 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004419 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004420 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo4a07c222015-09-18 17:54:22 -04004421 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004422 },
4423 { } /* terminate */
4424};
4425
4426/* cgroup core interface files for the legacy hierarchies */
4427static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004428 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004429 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004430 .seq_start = cgroup_pidlist_start,
4431 .seq_next = cgroup_pidlist_next,
4432 .seq_stop = cgroup_pidlist_stop,
4433 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004434 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004435 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004436 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004437 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004438 .name = "cgroup.clone_children",
4439 .read_u64 = cgroup_clone_children_read,
4440 .write_u64 = cgroup_clone_children_write,
4441 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004442 {
Tejun Heo873fe092013-04-14 20:15:26 -07004443 .name = "cgroup.sane_behavior",
4444 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004445 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004446 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004447 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004448 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004449 .seq_start = cgroup_pidlist_start,
4450 .seq_next = cgroup_pidlist_next,
4451 .seq_stop = cgroup_pidlist_stop,
4452 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004453 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004454 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004455 },
4456 {
4457 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004458 .read_u64 = cgroup_read_notify_on_release,
4459 .write_u64 = cgroup_write_notify_on_release,
4460 },
Tejun Heo873fe092013-04-14 20:15:26 -07004461 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004462 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004463 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004464 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004465 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004466 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004467 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004468 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004469};
4470
Tejun Heo0c21ead2013-08-13 20:22:51 -04004471/*
4472 * css destruction is four-stage process.
4473 *
4474 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4475 * Implemented in kill_css().
4476 *
4477 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004478 * and thus css_tryget_online() is guaranteed to fail, the css can be
4479 * offlined by invoking offline_css(). After offlining, the base ref is
4480 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004481 *
4482 * 3. When the percpu_ref reaches zero, the only possible remaining
4483 * accessors are inside RCU read sections. css_release() schedules the
4484 * RCU callback.
4485 *
4486 * 4. After the grace period, the css can be freed. Implemented in
4487 * css_free_work_fn().
4488 *
4489 * It is actually hairier because both step 2 and 4 require process context
4490 * and thus involve punting to css->destroy_work adding two additional
4491 * steps to the already complex sequence.
4492 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004493static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004494{
4495 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004496 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004497 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004498 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004499
Tejun Heo9a1049d2014-06-28 08:10:14 -04004500 percpu_ref_exit(&css->refcnt);
4501
Vladimir Davydov01e58652015-02-12 14:59:26 -08004502 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004503 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004504 int id = css->id;
4505
Tejun Heo9d755d32014-05-14 09:15:02 -04004506 if (css->parent)
4507 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004508
Vladimir Davydov01e58652015-02-12 14:59:26 -08004509 ss->css_free(css);
4510 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004511 cgroup_put(cgrp);
4512 } else {
4513 /* cgroup free path */
4514 atomic_dec(&cgrp->root->nr_cgrps);
4515 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004516 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004517
Tejun Heod51f39b2014-05-16 13:22:48 -04004518 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004519 /*
4520 * We get a ref to the parent, and put the ref when
4521 * this cgroup is being freed, so it's guaranteed
4522 * that the parent won't be destroyed before its
4523 * children.
4524 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004525 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004526 kernfs_put(cgrp->kn);
4527 kfree(cgrp);
4528 } else {
4529 /*
4530 * This is root cgroup's refcnt reaching zero,
4531 * which indicates that the root should be
4532 * released.
4533 */
4534 cgroup_destroy_root(cgrp->root);
4535 }
4536 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004537}
4538
4539static void css_free_rcu_fn(struct rcu_head *rcu_head)
4540{
4541 struct cgroup_subsys_state *css =
4542 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4543
Tejun Heo0c21ead2013-08-13 20:22:51 -04004544 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004545 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004546}
4547
Tejun Heo25e15d82014-05-14 09:15:02 -04004548static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004549{
4550 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004551 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004552 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004553 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004554
Tejun Heo1fed1b22014-05-16 13:22:49 -04004555 mutex_lock(&cgroup_mutex);
4556
Tejun Heode3f0342014-05-16 13:22:49 -04004557 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004558 list_del_rcu(&css->sibling);
4559
Tejun Heo9d755d32014-05-14 09:15:02 -04004560 if (ss) {
4561 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004562 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004563 if (ss->css_released)
4564 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004565 } else {
4566 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004567 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4568 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004569
4570 /*
4571 * There are two control paths which try to determine
4572 * cgroup from dentry without going through kernfs -
4573 * cgroupstats_build() and css_tryget_online_from_dir().
4574 * Those are supported by RCU protecting clearing of
4575 * cgrp->kn->priv backpointer.
4576 */
4577 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004578 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004579
Tejun Heo1fed1b22014-05-16 13:22:49 -04004580 mutex_unlock(&cgroup_mutex);
4581
Tejun Heo0c21ead2013-08-13 20:22:51 -04004582 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004583}
4584
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004585static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004586{
4587 struct cgroup_subsys_state *css =
4588 container_of(ref, struct cgroup_subsys_state, refcnt);
4589
Tejun Heo25e15d82014-05-14 09:15:02 -04004590 INIT_WORK(&css->destroy_work, css_release_work_fn);
4591 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004592}
4593
Tejun Heoddfcada2014-05-04 15:09:14 -04004594static void init_and_link_css(struct cgroup_subsys_state *css,
4595 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004597 lockdep_assert_held(&cgroup_mutex);
4598
Tejun Heoddfcada2014-05-04 15:09:14 -04004599 cgroup_get(cgrp);
4600
Tejun Heod5c419b2014-05-16 13:22:48 -04004601 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004602 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004603 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004604 INIT_LIST_HEAD(&css->sibling);
4605 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004606 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004607
Tejun Heod51f39b2014-05-16 13:22:48 -04004608 if (cgroup_parent(cgrp)) {
4609 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004610 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004611 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004612
Tejun Heoca8bdca2013-08-26 18:40:56 -04004613 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004614}
4615
Li Zefan2a4ac632013-07-31 16:16:40 +08004616/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004617static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004618{
Tejun Heo623f9262013-08-13 11:01:55 -04004619 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004620 int ret = 0;
4621
Tejun Heoa31f2d32012-11-19 08:13:37 -08004622 lockdep_assert_held(&cgroup_mutex);
4623
Tejun Heo92fb9742012-11-19 08:13:38 -08004624 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004625 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004626 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004627 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004628 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004629 }
Tejun Heob1929db2012-11-19 08:13:38 -08004630 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004631}
4632
Li Zefan2a4ac632013-07-31 16:16:40 +08004633/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004634static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004635{
Tejun Heo623f9262013-08-13 11:01:55 -04004636 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004637
4638 lockdep_assert_held(&cgroup_mutex);
4639
4640 if (!(css->flags & CSS_ONLINE))
4641 return;
4642
Li Zefand7eeac12013-03-12 15:35:59 -07004643 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004644 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004645
Tejun Heoeb954192013-08-08 20:11:23 -04004646 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004647 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004648
4649 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004650}
4651
Tejun Heoc81c925a2013-12-06 15:11:56 -05004652/**
4653 * create_css - create a cgroup_subsys_state
4654 * @cgrp: the cgroup new css will be associated with
4655 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004656 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004657 *
4658 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004659 * css is online and installed in @cgrp with all interface files created if
4660 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004661 */
Tejun Heof63070d2014-07-08 18:02:57 -04004662static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4663 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004664{
Tejun Heod51f39b2014-05-16 13:22:48 -04004665 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004666 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004667 struct cgroup_subsys_state *css;
4668 int err;
4669
Tejun Heoc81c925a2013-12-06 15:11:56 -05004670 lockdep_assert_held(&cgroup_mutex);
4671
Tejun Heo1fed1b22014-05-16 13:22:49 -04004672 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004673 if (IS_ERR(css))
4674 return PTR_ERR(css);
4675
Tejun Heoddfcada2014-05-04 15:09:14 -04004676 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004677
Tejun Heo2aad2a82014-09-24 13:31:50 -04004678 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004679 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004680 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004681
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004682 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004683 if (err < 0)
4684 goto err_free_percpu_ref;
4685 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004686
Tejun Heof63070d2014-07-08 18:02:57 -04004687 if (visible) {
4688 err = cgroup_populate_dir(cgrp, 1 << ss->id);
4689 if (err)
4690 goto err_free_id;
4691 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004692
4693 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004694 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004695 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004696
4697 err = online_css(css);
4698 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004699 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004700
Tejun Heoc81c925a2013-12-06 15:11:56 -05004701 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004702 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004703 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 -04004704 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004705 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004706 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004707 ss->warned_broken_hierarchy = true;
4708 }
4709
4710 return 0;
4711
Tejun Heo1fed1b22014-05-16 13:22:49 -04004712err_list_del:
4713 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004714 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004715err_free_id:
4716 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004717err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004718 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004719err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004720 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004721 return err;
4722}
4723
Tejun Heob3bfd982014-05-13 12:19:22 -04004724static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4725 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004726{
Tejun Heoa9746d82014-05-13 12:19:22 -04004727 struct cgroup *parent, *cgrp;
4728 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004729 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004730 struct kernfs_node *kn;
Tejun Heoa14c6872014-07-15 11:05:09 -04004731 struct cftype *base_files;
Tejun Heob3bfd982014-05-13 12:19:22 -04004732 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733
Alban Crequy71b1fb52014-08-18 12:20:20 +01004734 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4735 */
4736 if (strchr(name, '\n'))
4737 return -EINVAL;
4738
Tejun Heoa9746d82014-05-13 12:19:22 -04004739 parent = cgroup_kn_lock_live(parent_kn);
4740 if (!parent)
4741 return -ENODEV;
4742 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004743
Tejun Heo0a950f62012-11-19 09:02:12 -08004744 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004745 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004746 if (!cgrp) {
4747 ret = -ENOMEM;
4748 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004749 }
4750
Tejun Heo2aad2a82014-09-24 13:31:50 -04004751 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004752 if (ret)
4753 goto out_free_cgrp;
4754
Li Zefan0ab02ca2014-02-11 16:05:46 +08004755 /*
4756 * Temporarily set the pointer to NULL, so idr_find() won't return
4757 * a half-baked cgroup.
4758 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004759 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004760 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004761 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004762 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004763 }
4764
Paul Menagecc31edc2008-10-18 20:28:04 -07004765 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004766
Tejun Heo9d800df2014-05-14 09:15:00 -04004767 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004768 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769
Li Zefanb6abdb02008-03-04 14:28:19 -08004770 if (notify_on_release(parent))
4771 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4772
Tejun Heo2260e7f2012-11-19 08:13:38 -08004773 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4774 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004775
Tejun Heo2bd59d42014-02-11 11:52:49 -05004776 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004777 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004778 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004779 ret = PTR_ERR(kn);
4780 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004781 }
4782 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004783
Tejun Heo6f305582014-02-12 09:29:50 -05004784 /*
4785 * This extra ref will be put in cgroup_free_fn() and guarantees
4786 * that @cgrp->kn is always accessible.
4787 */
4788 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789
Tejun Heo0cb51d72014-05-16 13:22:49 -04004790 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004791
Tejun Heo4e139af2012-11-19 08:13:36 -08004792 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004793 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004794 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004795 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004796
Tejun Heo0d802552013-12-06 15:11:56 -05004797 /*
4798 * @cgrp is now fully operational. If something fails after this
4799 * point, it'll be released via the normal destruction path.
4800 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004801 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004802
Tejun Heoba0f4d72014-05-13 12:19:22 -04004803 ret = cgroup_kn_set_ugid(kn);
4804 if (ret)
4805 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004806
Tejun Heoa14c6872014-07-15 11:05:09 -04004807 if (cgroup_on_dfl(cgrp))
4808 base_files = cgroup_dfl_base_files;
4809 else
4810 base_files = cgroup_legacy_base_files;
4811
4812 ret = cgroup_addrm_files(cgrp, base_files, true);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004813 if (ret)
4814 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004815
Tejun Heo9d403e92013-12-06 15:11:56 -05004816 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004817 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004818 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004819 ret = create_css(cgrp, ss,
4820 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004821 if (ret)
4822 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004823 }
Tejun Heoa8638032012-11-09 09:12:29 -08004824 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004825
Tejun Heobd53d612014-04-23 11:13:16 -04004826 /*
4827 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004828 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004829 */
Tejun Heo667c2492014-07-08 18:02:56 -04004830 if (!cgroup_on_dfl(cgrp)) {
4831 cgrp->subtree_control = parent->subtree_control;
4832 cgroup_refresh_child_subsys_mask(cgrp);
4833 }
Tejun Heof392e512014-04-23 11:13:14 -04004834
Tejun Heo2bd59d42014-02-11 11:52:49 -05004835 kernfs_activate(kn);
4836
Tejun Heoba0f4d72014-05-13 12:19:22 -04004837 ret = 0;
4838 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839
Tejun Heoba0f4d72014-05-13 12:19:22 -04004840out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004841 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004842out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004843 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004844out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004845 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004846out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004847 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004848 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004849
4850out_destroy:
4851 cgroup_destroy_locked(cgrp);
4852 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004853}
4854
Tejun Heo223dbc32013-08-13 20:22:50 -04004855/*
4856 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004857 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4858 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004859 */
4860static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004861{
Tejun Heo223dbc32013-08-13 20:22:50 -04004862 struct cgroup_subsys_state *css =
4863 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004864
Tejun Heof20104d2013-08-13 20:22:50 -04004865 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004866 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004867 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004868
Tejun Heo09a503ea2013-08-13 20:22:50 -04004869 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004870}
4871
Tejun Heo223dbc32013-08-13 20:22:50 -04004872/* css kill confirmation processing requires process context, bounce */
4873static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004874{
4875 struct cgroup_subsys_state *css =
4876 container_of(ref, struct cgroup_subsys_state, refcnt);
4877
Tejun Heo223dbc32013-08-13 20:22:50 -04004878 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004879 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004880}
4881
Tejun Heof392e512014-04-23 11:13:14 -04004882/**
4883 * kill_css - destroy a css
4884 * @css: css to destroy
4885 *
4886 * This function initiates destruction of @css by removing cgroup interface
4887 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004888 * asynchronously once css_tryget_online() is guaranteed to fail and when
4889 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004890 */
4891static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004892{
Tejun Heo01f64742014-05-13 12:19:23 -04004893 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004894
Tejun Heo2bd59d42014-02-11 11:52:49 -05004895 /*
4896 * This must happen before css is disassociated with its cgroup.
4897 * See seq_css() for details.
4898 */
Tejun Heoaec25022014-02-08 10:36:58 -05004899 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004900
Tejun Heoedae0c32013-08-13 20:22:51 -04004901 /*
4902 * Killing would put the base ref, but we need to keep it alive
4903 * until after ->css_offline().
4904 */
4905 css_get(css);
4906
4907 /*
4908 * cgroup core guarantees that, by the time ->css_offline() is
4909 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004910 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004911 * proceed to offlining css's because percpu_ref_kill() doesn't
4912 * guarantee that the ref is seen as killed on all CPUs on return.
4913 *
4914 * Use percpu_ref_kill_and_confirm() to get notifications as each
4915 * css is confirmed to be seen as killed on all CPUs.
4916 */
4917 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004918}
4919
4920/**
4921 * cgroup_destroy_locked - the first stage of cgroup destruction
4922 * @cgrp: cgroup to be destroyed
4923 *
4924 * css's make use of percpu refcnts whose killing latency shouldn't be
4925 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004926 * guarantee that css_tryget_online() won't succeed by the time
4927 * ->css_offline() is invoked. To satisfy all the requirements,
4928 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004929 *
4930 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4931 * userland visible parts and start killing the percpu refcnts of
4932 * css's. Set up so that the next stage will be kicked off once all
4933 * the percpu refcnts are confirmed to be killed.
4934 *
4935 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4936 * rest of destruction. Once all cgroup references are gone, the
4937 * cgroup is RCU-freed.
4938 *
4939 * This function implements s1. After this step, @cgrp is gone as far as
4940 * the userland is concerned and a new cgroup with the same name may be
4941 * created. As cgroup doesn't care about the names internally, this
4942 * doesn't cause any problem.
4943 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004944static int cgroup_destroy_locked(struct cgroup *cgrp)
4945 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004946{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004947 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004948 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004949 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950
Tejun Heo42809dd2012-11-19 08:13:37 -08004951 lockdep_assert_held(&cgroup_mutex);
4952
Tejun Heoddd69142013-06-12 21:04:54 -07004953 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004954 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004955 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004956 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004957 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004958 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004959 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004960 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004961 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004962
Tejun Heo1a90dd52012-11-05 09:16:59 -08004963 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004964 * Make sure there's no live children. We can't test emptiness of
4965 * ->self.children as dead children linger on it while being
4966 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004967 */
Tejun Heof3d46502014-05-16 13:22:52 -04004968 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004969 return -EBUSY;
4970
4971 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004972 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004973 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004974 */
Tejun Heo184faf32014-05-16 13:22:51 -04004975 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004976
Tejun Heo249f3462014-05-14 09:15:01 -04004977 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004978 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004979 kill_css(css);
4980
Tejun Heo455050d2013-06-13 19:27:41 -07004981 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004982 * Remove @cgrp directory along with the base files. @cgrp has an
4983 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07004984 */
Tejun Heo01f64742014-05-13 12:19:23 -04004985 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04004986
Tejun Heod51f39b2014-05-16 13:22:48 -04004987 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05004988
Tejun Heo249f3462014-05-14 09:15:01 -04004989 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004990 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07004991
Tejun Heoea15f8c2013-06-13 19:27:42 -07004992 return 0;
4993};
4994
Tejun Heo2bd59d42014-02-11 11:52:49 -05004995static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004996{
Tejun Heoa9746d82014-05-13 12:19:22 -04004997 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004998 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004999
Tejun Heoa9746d82014-05-13 12:19:22 -04005000 cgrp = cgroup_kn_lock_live(kn);
5001 if (!cgrp)
5002 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005003
Tejun Heoa9746d82014-05-13 12:19:22 -04005004 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005005
Tejun Heoa9746d82014-05-13 12:19:22 -04005006 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005007 return ret;
5008}
5009
Tejun Heo2bd59d42014-02-11 11:52:49 -05005010static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5011 .remount_fs = cgroup_remount,
5012 .show_options = cgroup_show_options,
5013 .mkdir = cgroup_mkdir,
5014 .rmdir = cgroup_rmdir,
5015 .rename = cgroup_rename,
5016};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005017
Tejun Heo15a4c832014-05-04 15:09:14 -04005018static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005019{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005020 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005021
5022 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005023
Tejun Heo648bb562012-11-19 08:13:36 -08005024 mutex_lock(&cgroup_mutex);
5025
Tejun Heo15a4c832014-05-04 15:09:14 -04005026 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005027 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005028
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005029 /* Create the root cgroup state for this subsystem */
5030 ss->root = &cgrp_dfl_root;
5031 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005032 /* We don't handle early failures gracefully */
5033 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005034 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005035
5036 /*
5037 * Root csses are never destroyed and we can't initialize
5038 * percpu_ref during early init. Disable refcnting.
5039 */
5040 css->flags |= CSS_NO_REF;
5041
Tejun Heo15a4c832014-05-04 15:09:14 -04005042 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005043 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005044 css->id = 1;
5045 } else {
5046 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5047 BUG_ON(css->id < 0);
5048 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005049
Li Zefane8d55fd2008-04-29 01:00:13 -07005050 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005051 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005052 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005053 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005054 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005055
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005056 have_fork_callback |= (bool)ss->fork << ss->id;
5057 have_exit_callback |= (bool)ss->exit << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005058 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005059
Li Zefane8d55fd2008-04-29 01:00:13 -07005060 /* At system boot, before all subsystems have been
5061 * registered, no tasks have been forked, so we don't
5062 * need to invoke fork callbacks here. */
5063 BUG_ON(!list_empty(&init_task.tasks));
5064
Tejun Heoae7f1642013-08-13 20:22:50 -04005065 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005066
Tejun Heo648bb562012-11-19 08:13:36 -08005067 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005068}
5069
5070/**
Li Zefana043e3b2008-02-23 15:24:09 -08005071 * cgroup_init_early - cgroup initialization at system boot
5072 *
5073 * Initialize cgroups at system boot, and initialize any
5074 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005075 */
5076int __init cgroup_init_early(void)
5077{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005078 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005079 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005080 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005081
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005082 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005083 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5084
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005085 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005086
Tejun Heo3ed80a62014-02-08 10:36:58 -05005087 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005088 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05005089 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5090 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005091 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005092 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5093 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005094
Tejun Heoaec25022014-02-08 10:36:58 -05005095 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005096 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005097 if (!ss->legacy_name)
5098 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005099
5100 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005101 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005102 }
5103 return 0;
5104}
5105
5106/**
Li Zefana043e3b2008-02-23 15:24:09 -08005107 * cgroup_init - cgroup initialization
5108 *
5109 * Register cgroup filesystem and /proc file, and initialize
5110 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005111 */
5112int __init cgroup_init(void)
5113{
Tejun Heo30159ec2013-06-25 11:53:37 -07005114 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005115 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04005116 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07005117
Tejun Heo1ed13282015-09-16 12:53:17 -04005118 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005119 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5120 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005121
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005122 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005123
Tejun Heo82fe9b02013-06-25 11:53:37 -07005124 /* Add init_css_set to the hash table */
5125 key = css_set_hash(init_css_set.subsys);
5126 hash_add(css_set_table, &init_css_set.hlist, key);
5127
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005128 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005129
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005130 mutex_unlock(&cgroup_mutex);
5131
Tejun Heo172a2c062014-03-19 10:23:53 -04005132 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005133 if (ss->early_init) {
5134 struct cgroup_subsys_state *css =
5135 init_css_set.subsys[ss->id];
5136
5137 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5138 GFP_KERNEL);
5139 BUG_ON(css->id < 0);
5140 } else {
5141 cgroup_init_subsys(ss, false);
5142 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005143
Tejun Heo2d8f2432014-04-23 11:13:15 -04005144 list_add_tail(&init_css_set.e_cset_node[ssid],
5145 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005146
5147 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005148 * Setting dfl_root subsys_mask needs to consider the
5149 * disabled flag and cftype registration needs kmalloc,
5150 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005151 */
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005152 if (!cgroup_ssid_enabled(ssid))
Tejun Heoa8ddc822014-07-15 11:05:10 -04005153 continue;
5154
5155 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5156
5157 if (cgroup_legacy_files_on_dfl && !ss->dfl_cftypes)
5158 ss->dfl_cftypes = ss->legacy_cftypes;
5159
Tejun Heo5de4fa12014-07-15 11:05:10 -04005160 if (!ss->dfl_cftypes)
5161 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5162
Tejun Heoa8ddc822014-07-15 11:05:10 -04005163 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5164 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5165 } else {
5166 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5167 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005168 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005169
5170 if (ss->bind)
5171 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005172 }
Greg KH676db4a2010-08-05 13:53:35 -07005173
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005174 err = sysfs_create_mount_point(fs_kobj, "cgroup");
5175 if (err)
5176 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005177
5178 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005179 if (err < 0) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005180 sysfs_remove_mount_point(fs_kobj, "cgroup");
Tejun Heo2bd59d42014-02-11 11:52:49 -05005181 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005182 }
5183
5184 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005185 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005186}
Paul Menageb4f48b62007-10-18 23:39:33 -07005187
Tejun Heoe5fca242013-11-22 17:14:39 -05005188static int __init cgroup_wq_init(void)
5189{
5190 /*
5191 * There isn't much point in executing destruction path in
5192 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005193 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005194 *
5195 * We would prefer to do this in cgroup_init() above, but that
5196 * is called before init_workqueues(): so leave this until after.
5197 */
Tejun Heo1a115332014-02-12 19:06:19 -05005198 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005199 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005200
5201 /*
5202 * Used to destroy pidlists and separate to serve as flush domain.
5203 * Cap @max_active to 1 too.
5204 */
5205 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5206 0, 1);
5207 BUG_ON(!cgroup_pidlist_destroy_wq);
5208
Tejun Heoe5fca242013-11-22 17:14:39 -05005209 return 0;
5210}
5211core_initcall(cgroup_wq_init);
5212
Paul Menagea4243162007-10-18 23:39:35 -07005213/*
5214 * proc_cgroup_show()
5215 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5216 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005217 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005218int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5219 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005220{
Tejun Heoe61734c2014-02-12 09:29:50 -05005221 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005222 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005223 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005224
5225 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005226 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005227 if (!buf)
5228 goto out;
5229
Paul Menagea4243162007-10-18 23:39:35 -07005230 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05005231 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005232
Tejun Heo985ed672014-03-19 10:23:53 -04005233 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005234 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005235 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005236 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005237
Tejun Heoa2dd4242014-03-19 10:23:55 -04005238 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005239 continue;
5240
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005241 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005242 if (root != &cgrp_dfl_root)
5243 for_each_subsys(ss, ssid)
5244 if (root->subsys_mask & (1 << ssid))
5245 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005246 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005247 if (strlen(root->name))
5248 seq_printf(m, "%sname=%s", count ? "," : "",
5249 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005250 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005251 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05005252 path = cgroup_path(cgrp, buf, PATH_MAX);
5253 if (!path) {
5254 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07005255 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05005256 }
5257 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07005258 seq_putc(m, '\n');
5259 }
5260
Zefan Li006f4ac2014-09-18 16:03:15 +08005261 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005262out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05005263 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005264 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005265 kfree(buf);
5266out:
5267 return retval;
5268}
5269
Paul Menagea4243162007-10-18 23:39:35 -07005270/* Display information about each subsystem and each hierarchy */
5271static int proc_cgroupstats_show(struct seq_file *m, void *v)
5272{
Tejun Heo30159ec2013-06-25 11:53:37 -07005273 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005274 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005275
Paul Menage8bab8dd2008-04-04 14:29:57 -07005276 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005277 /*
5278 * ideally we don't want subsystems moving around while we do this.
5279 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5280 * subsys/hierarchy state.
5281 */
Paul Menagea4243162007-10-18 23:39:35 -07005282 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005283
5284 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005285 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005286 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005287 atomic_read(&ss->root->nr_cgrps),
5288 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005289
Paul Menagea4243162007-10-18 23:39:35 -07005290 mutex_unlock(&cgroup_mutex);
5291 return 0;
5292}
5293
5294static int cgroupstats_open(struct inode *inode, struct file *file)
5295{
Al Viro9dce07f2008-03-29 03:07:28 +00005296 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005297}
5298
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005299static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005300 .open = cgroupstats_open,
5301 .read = seq_read,
5302 .llseek = seq_lseek,
5303 .release = single_release,
5304};
5305
Aleksa Sarai7e476822015-06-09 21:32:09 +10005306static void **subsys_canfork_priv_p(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5307{
5308 if (CGROUP_CANFORK_START <= i && i < CGROUP_CANFORK_END)
5309 return &ss_priv[i - CGROUP_CANFORK_START];
5310 return NULL;
5311}
5312
5313static void *subsys_canfork_priv(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5314{
5315 void **private = subsys_canfork_priv_p(ss_priv, i);
5316 return private ? *private : NULL;
5317}
5318
Paul Menageb4f48b62007-10-18 23:39:33 -07005319/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005320 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005321 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005322 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005323 * A task is associated with the init_css_set until cgroup_post_fork()
5324 * attaches it to the parent's css_set. Empty cg_list indicates that
5325 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005326 */
5327void cgroup_fork(struct task_struct *child)
5328{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005329 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005330 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005331}
5332
5333/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005334 * cgroup_can_fork - called on a new task before the process is exposed
5335 * @child: the task in question.
5336 *
5337 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5338 * returns an error, the fork aborts with that error code. This allows for
5339 * a cgroup subsystem to conditionally allow or deny new forks.
5340 */
5341int cgroup_can_fork(struct task_struct *child,
5342 void *ss_priv[CGROUP_CANFORK_COUNT])
5343{
5344 struct cgroup_subsys *ss;
5345 int i, j, ret;
5346
5347 for_each_subsys_which(ss, i, &have_canfork_callback) {
5348 ret = ss->can_fork(child, subsys_canfork_priv_p(ss_priv, i));
5349 if (ret)
5350 goto out_revert;
5351 }
5352
5353 return 0;
5354
5355out_revert:
5356 for_each_subsys(ss, j) {
5357 if (j >= i)
5358 break;
5359 if (ss->cancel_fork)
5360 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, j));
5361 }
5362
5363 return ret;
5364}
5365
5366/**
5367 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5368 * @child: the task in question
5369 *
5370 * This calls the cancel_fork() callbacks if a fork failed *after*
5371 * cgroup_can_fork() succeded.
5372 */
5373void cgroup_cancel_fork(struct task_struct *child,
5374 void *ss_priv[CGROUP_CANFORK_COUNT])
5375{
5376 struct cgroup_subsys *ss;
5377 int i;
5378
5379 for_each_subsys(ss, i)
5380 if (ss->cancel_fork)
5381 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, i));
5382}
5383
5384/**
Li Zefana043e3b2008-02-23 15:24:09 -08005385 * cgroup_post_fork - called on a new task after adding it to the task list
5386 * @child: the task in question
5387 *
Tejun Heo5edee612012-10-16 15:03:14 -07005388 * Adds the task to the list running through its css_set if necessary and
5389 * call the subsystem fork() callbacks. Has to be after the task is
5390 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005391 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005392 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005393 */
Aleksa Sarai7e476822015-06-09 21:32:09 +10005394void cgroup_post_fork(struct task_struct *child,
5395 void *old_ss_priv[CGROUP_CANFORK_COUNT])
Paul Menage817929e2007-10-18 23:39:36 -07005396{
Tejun Heo30159ec2013-06-25 11:53:37 -07005397 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005398 int i;
5399
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005400 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005401 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005402 * function sets use_task_css_set_links before grabbing
5403 * tasklist_lock and we just went through tasklist_lock to add
5404 * @child, it's guaranteed that either we see the set
5405 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5406 * @child during its iteration.
5407 *
5408 * If we won the race, @child is associated with %current's
5409 * css_set. Grabbing css_set_rwsem guarantees both that the
5410 * association is stable, and, on completion of the parent's
5411 * migration, @child is visible in the source of migration or
5412 * already in the destination cgroup. This guarantee is necessary
5413 * when implementing operations which need to migrate all tasks of
5414 * a cgroup to another.
5415 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005416 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005417 * will remain in init_css_set. This is safe because all tasks are
5418 * in the init_css_set before cg_links is enabled and there's no
5419 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005420 */
Paul Menage817929e2007-10-18 23:39:36 -07005421 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005422 struct css_set *cset;
5423
Tejun Heo96d365e2014-02-13 06:58:40 -05005424 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005425 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005426 if (list_empty(&child->cg_list)) {
5427 rcu_assign_pointer(child->cgroups, cset);
5428 list_add(&child->cg_list, &cset->tasks);
5429 get_css_set(cset);
5430 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005431 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07005432 }
Tejun Heo5edee612012-10-16 15:03:14 -07005433
5434 /*
5435 * Call ss->fork(). This must happen after @child is linked on
5436 * css_set; otherwise, @child might change state between ->fork()
5437 * and addition to css_set.
5438 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005439 for_each_subsys_which(ss, i, &have_fork_callback)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005440 ss->fork(child, subsys_canfork_priv(old_ss_priv, i));
Paul Menage817929e2007-10-18 23:39:36 -07005441}
Tejun Heo5edee612012-10-16 15:03:14 -07005442
Paul Menage817929e2007-10-18 23:39:36 -07005443/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005444 * cgroup_exit - detach cgroup from exiting task
5445 * @tsk: pointer to task_struct of exiting process
5446 *
5447 * Description: Detach cgroup from @tsk and release it.
5448 *
5449 * Note that cgroups marked notify_on_release force every task in
5450 * them to take the global cgroup_mutex mutex when exiting.
5451 * This could impact scaling on very large systems. Be reluctant to
5452 * use notify_on_release cgroups where very high task exit scaling
5453 * is required on large systems.
5454 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005455 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5456 * call cgroup_exit() while the task is still competent to handle
5457 * notify_on_release(), then leave the task attached to the root cgroup in
5458 * each hierarchy for the remainder of its exit. No need to bother with
5459 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005460 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005461 */
Li Zefan1ec41832014-03-28 15:22:19 +08005462void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005463{
Tejun Heo30159ec2013-06-25 11:53:37 -07005464 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005465 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005466 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005467 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005468
5469 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005470 * Unlink from @tsk from its css_set. As migration path can't race
5471 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005472 */
5473 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005474 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005475 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005476 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005477 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005478 }
5479
Paul Menageb4f48b62007-10-18 23:39:33 -07005480 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005481 cset = task_css_set(tsk);
5482 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005483
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005484 /* see cgroup_post_fork() for details */
5485 for_each_subsys_which(ss, i, &have_exit_callback) {
5486 struct cgroup_subsys_state *old_css = cset->subsys[i];
5487 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005488
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005489 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005490 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005491
Tejun Heoeaf797a2014-02-25 10:04:03 -05005492 if (put_cset)
Zefan Lia25eb522014-09-19 16:51:00 +08005493 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005494}
Paul Menage697f4162007-10-18 23:39:34 -07005495
Paul Menagebd89aab2007-10-18 23:40:44 -07005496static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005497{
Zefan Lia25eb522014-09-19 16:51:00 +08005498 if (notify_on_release(cgrp) && !cgroup_has_tasks(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005499 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5500 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005501}
5502
Paul Menage81a6a5c2007-10-18 23:39:38 -07005503/*
5504 * Notify userspace when a cgroup is released, by running the
5505 * configured release agent with the name of the cgroup (path
5506 * relative to the root of cgroup file system) as the argument.
5507 *
5508 * Most likely, this user command will try to rmdir this cgroup.
5509 *
5510 * This races with the possibility that some other task will be
5511 * attached to this cgroup before it is removed, or that some other
5512 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5513 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5514 * unused, and this cgroup will be reprieved from its death sentence,
5515 * to continue to serve a useful existence. Next time it's released,
5516 * we will get notified again, if it still has 'notify_on_release' set.
5517 *
5518 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5519 * means only wait until the task is successfully execve()'d. The
5520 * separate release agent task is forked by call_usermodehelper(),
5521 * then control in this thread returns here, without waiting for the
5522 * release agent task. We don't bother to wait because the caller of
5523 * this routine has no use for the exit status of the release agent
5524 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005525 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005526static void cgroup_release_agent(struct work_struct *work)
5527{
Zefan Li971ff492014-09-18 16:06:19 +08005528 struct cgroup *cgrp =
5529 container_of(work, struct cgroup, release_agent_work);
5530 char *pathbuf = NULL, *agentbuf = NULL, *path;
5531 char *argv[3], *envp[3];
5532
Paul Menage81a6a5c2007-10-18 23:39:38 -07005533 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005534
Zefan Li971ff492014-09-18 16:06:19 +08005535 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5536 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5537 if (!pathbuf || !agentbuf)
5538 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005539
Zefan Li971ff492014-09-18 16:06:19 +08005540 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5541 if (!path)
5542 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005543
Zefan Li971ff492014-09-18 16:06:19 +08005544 argv[0] = agentbuf;
5545 argv[1] = path;
5546 argv[2] = NULL;
5547
5548 /* minimal command environment */
5549 envp[0] = "HOME=/";
5550 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5551 envp[2] = NULL;
5552
Paul Menage81a6a5c2007-10-18 23:39:38 -07005553 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005554 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005555 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005556out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005557 mutex_unlock(&cgroup_mutex);
5558out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005559 kfree(agentbuf);
5560 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005561}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005562
5563static int __init cgroup_disable(char *str)
5564{
Tejun Heo30159ec2013-06-25 11:53:37 -07005565 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005566 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005567 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005568
5569 while ((token = strsep(&str, ",")) != NULL) {
5570 if (!*token)
5571 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005572
Tejun Heo3ed80a62014-02-08 10:36:58 -05005573 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005574 if (strcmp(token, ss->name) &&
5575 strcmp(token, ss->legacy_name))
5576 continue;
5577
Tejun Heo49d1dc42015-09-18 11:56:28 -04005578 static_branch_disable(cgroup_subsys_enabled_key[i]);
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005579 printk(KERN_INFO "Disabling %s control group subsystem\n",
5580 ss->name);
5581 break;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005582 }
5583 }
5584 return 1;
5585}
5586__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005587
Tejun Heoa8ddc822014-07-15 11:05:10 -04005588static int __init cgroup_set_legacy_files_on_dfl(char *str)
5589{
5590 printk("cgroup: using legacy files on the default hierarchy\n");
5591 cgroup_legacy_files_on_dfl = true;
5592 return 0;
5593}
5594__setup("cgroup__DEVEL__legacy_files_on_dfl", cgroup_set_legacy_files_on_dfl);
5595
Tejun Heob77d7b62013-08-13 11:01:54 -04005596/**
Tejun Heoec903c02014-05-13 12:11:01 -04005597 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005598 * @dentry: directory dentry of interest
5599 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005600 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005601 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5602 * to get the corresponding css and return it. If such css doesn't exist
5603 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005604 */
Tejun Heoec903c02014-05-13 12:11:01 -04005605struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5606 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005607{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005608 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5609 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005610 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005611
Tejun Heo35cf0832013-08-26 18:40:56 -04005612 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005613 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5614 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005615 return ERR_PTR(-EBADF);
5616
Tejun Heo5a17f542014-02-11 11:52:47 -05005617 rcu_read_lock();
5618
Tejun Heo2bd59d42014-02-11 11:52:49 -05005619 /*
5620 * This path doesn't originate from kernfs and @kn could already
5621 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005622 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005623 */
5624 cgrp = rcu_dereference(kn->priv);
5625 if (cgrp)
5626 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005627
Tejun Heoec903c02014-05-13 12:11:01 -04005628 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005629 css = ERR_PTR(-ENOENT);
5630
5631 rcu_read_unlock();
5632 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005633}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005634
Li Zefan1cb650b2013-08-19 10:05:24 +08005635/**
5636 * css_from_id - lookup css by id
5637 * @id: the cgroup id
5638 * @ss: cgroup subsys to be looked into
5639 *
5640 * Returns the css if there's valid one with @id, otherwise returns NULL.
5641 * Should be called under rcu_read_lock().
5642 */
5643struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5644{
Tejun Heo6fa49182014-05-04 15:09:13 -04005645 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005646 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005647}
5648
Paul Menagefe693432009-09-23 15:56:20 -07005649#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005650static struct cgroup_subsys_state *
5651debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005652{
5653 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5654
5655 if (!css)
5656 return ERR_PTR(-ENOMEM);
5657
5658 return css;
5659}
5660
Tejun Heoeb954192013-08-08 20:11:23 -04005661static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005662{
Tejun Heoeb954192013-08-08 20:11:23 -04005663 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005664}
5665
Tejun Heo182446d2013-08-08 20:11:24 -04005666static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5667 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005668{
Tejun Heo182446d2013-08-08 20:11:24 -04005669 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005670}
5671
Tejun Heo182446d2013-08-08 20:11:24 -04005672static u64 current_css_set_read(struct cgroup_subsys_state *css,
5673 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005674{
5675 return (u64)(unsigned long)current->cgroups;
5676}
5677
Tejun Heo182446d2013-08-08 20:11:24 -04005678static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005679 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005680{
5681 u64 count;
5682
5683 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005684 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005685 rcu_read_unlock();
5686 return count;
5687}
5688
Tejun Heo2da8ca82013-12-05 12:28:04 -05005689static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005690{
Tejun Heo69d02062013-06-12 21:04:50 -07005691 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005692 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005693 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005694
Tejun Heoe61734c2014-02-12 09:29:50 -05005695 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5696 if (!name_buf)
5697 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005698
Tejun Heo96d365e2014-02-13 06:58:40 -05005699 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005700 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005701 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005702 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005703 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005704
Tejun Heoa2dd4242014-03-19 10:23:55 -04005705 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005706 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005707 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005708 }
5709 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005710 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005711 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005712 return 0;
5713}
5714
5715#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005716static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005717{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005718 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005719 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005720
Tejun Heo96d365e2014-02-13 06:58:40 -05005721 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005722 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005723 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005724 struct task_struct *task;
5725 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005726
Tejun Heo5abb8852013-06-12 21:04:49 -07005727 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005728
Tejun Heo5abb8852013-06-12 21:04:49 -07005729 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005730 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5731 goto overflow;
5732 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005733 }
Tejun Heoc7561122014-02-25 10:04:01 -05005734
5735 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5736 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5737 goto overflow;
5738 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5739 }
5740 continue;
5741 overflow:
5742 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005743 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005744 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005745 return 0;
5746}
5747
Tejun Heo182446d2013-08-08 20:11:24 -04005748static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005749{
Zefan Lia25eb522014-09-19 16:51:00 +08005750 return (!cgroup_has_tasks(css->cgroup) &&
5751 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005752}
5753
5754static struct cftype debug_files[] = {
5755 {
Paul Menagefe693432009-09-23 15:56:20 -07005756 .name = "taskcount",
5757 .read_u64 = debug_taskcount_read,
5758 },
5759
5760 {
5761 .name = "current_css_set",
5762 .read_u64 = current_css_set_read,
5763 },
5764
5765 {
5766 .name = "current_css_set_refcount",
5767 .read_u64 = current_css_set_refcount_read,
5768 },
5769
5770 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005771 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005772 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005773 },
5774
5775 {
5776 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005777 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005778 },
5779
5780 {
Paul Menagefe693432009-09-23 15:56:20 -07005781 .name = "releasable",
5782 .read_u64 = releasable_read,
5783 },
Paul Menagefe693432009-09-23 15:56:20 -07005784
Tejun Heo4baf6e32012-04-01 12:09:55 -07005785 { } /* terminate */
5786};
Paul Menagefe693432009-09-23 15:56:20 -07005787
Tejun Heo073219e2014-02-08 10:36:58 -05005788struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005789 .css_alloc = debug_css_alloc,
5790 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005791 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005792};
5793#endif /* CONFIG_CGROUP_DEBUG */