blob: 470f6536b9e8cfb029eedb5ecdb3c3e8c3c341be [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mm.h>
40#include <linux/mutex.h>
41#include <linux/mount.h>
42#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070043#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/rcupdate.h>
45#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040048#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050059#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070060
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070062
Tejun Heoe25e2cb2011-12-12 18:12:21 -080063/*
Tejun Heob1a21362013-11-29 10:42:58 -050064 * pidlists linger the following amount before being destroyed. The goal
65 * is avoiding frequent destruction in the middle of consecutive read calls
66 * Expiring in the middle is a performance problem not a correctness one.
67 * 1 sec should be enough.
68 */
69#define CGROUP_PIDLIST_DESTROY_DELAY HZ
70
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050071#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
72 MAX_CFTYPE_NAME + 2)
73
Tejun Heob1a21362013-11-29 10:42:58 -050074/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080075 * cgroup_mutex is the master lock. Any modification to cgroup or its
76 * hierarchy must be performed while holding it.
77 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040078 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050079 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050081 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
82 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 */
Tejun Heo22194492013-04-07 09:29:51 -070084#ifdef CONFIG_PROVE_RCU
85DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040086DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -050087EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040088EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070089#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070090static DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040091static DEFINE_SPINLOCK(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070092#endif
93
Tejun Heo69e943b2014-02-08 10:36:58 -050094/*
Tejun Heo15a4c832014-05-04 15:09:14 -040095 * Protects cgroup_idr and css_idr so that IDs can be released without
96 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040097 */
98static DEFINE_SPINLOCK(cgroup_idr_lock);
99
100/*
Tejun Heo34c06252015-11-05 00:12:24 -0500101 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
102 * against file removal/re-creation across css hiding.
103 */
104static DEFINE_SPINLOCK(cgroup_file_kn_lock);
105
106/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500107 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
108 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
109 */
110static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700111
Tejun Heo1ed13282015-09-16 12:53:17 -0400112struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
113
Tejun Heo8353da12014-05-13 12:19:23 -0400114#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700115 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
116 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400117 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500118
Ben Blumaae8aab2010-03-10 15:22:07 -0800119/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500120 * cgroup destruction makes heavy use of work items and there can be a lot
121 * of concurrent destructions. Use a separate workqueue so that cgroup
122 * destruction work items don't end up filling up max_active of system_wq
123 * which may lead to deadlock.
124 */
125static struct workqueue_struct *cgroup_destroy_wq;
126
127/*
Tejun Heob1a21362013-11-29 10:42:58 -0500128 * pidlist destructions need to be flushed on cgroup destruction. Use a
129 * separate workqueue as flush domain.
130 */
131static struct workqueue_struct *cgroup_pidlist_destroy_wq;
132
Tejun Heo3ed80a62014-02-08 10:36:58 -0500133/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500134#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500135static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700136#include <linux/cgroup_subsys.h>
137};
Tejun Heo073219e2014-02-08 10:36:58 -0500138#undef SUBSYS
139
140/* array of cgroup subsystem names */
141#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
142static const char *cgroup_subsys_name[] = {
143#include <linux/cgroup_subsys.h>
144};
145#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146
Tejun Heo49d1dc42015-09-18 11:56:28 -0400147/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
148#define SUBSYS(_x) \
149 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
150 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
151 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
152 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
153#include <linux/cgroup_subsys.h>
154#undef SUBSYS
155
156#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
157static struct static_key_true *cgroup_subsys_enabled_key[] = {
158#include <linux/cgroup_subsys.h>
159};
160#undef SUBSYS
161
162#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
163static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
164#include <linux/cgroup_subsys.h>
165};
166#undef SUBSYS
167
Paul Menageddbcc7e2007-10-18 23:39:30 -0700168/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400169 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700170 * unattached - it never has more than a single cgroup, and all tasks are
171 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700172 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400173struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400174EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700175
Tejun Heoa2dd4242014-03-19 10:23:55 -0400176/*
177 * The default hierarchy always exists but is hidden until mounted for the
178 * first time. This is for backward compatibility.
179 */
180static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700181
Tejun Heo5533e012014-05-14 19:33:07 -0400182/* some controllers are not supported in the default hierarchy */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000183static unsigned long cgrp_dfl_root_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400184
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185/* The list of hierarchy roots */
186
Tejun Heo9871bf92013-06-24 15:21:47 -0700187static LIST_HEAD(cgroup_roots);
188static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189
Tejun Heo3417ae12014-02-08 10:37:01 -0500190/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700191static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700192
Li Zefan794611a2013-06-18 18:53:53 +0800193/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400194 * Assign a monotonically increasing serial number to csses. It guarantees
195 * cgroups with bigger numbers are newer than those with smaller numbers.
196 * Also, as csses are always appended to the parent's ->children list, it
197 * guarantees that sibling csses are always sorted in the ascending serial
198 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800199 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400200static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800201
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000202/*
203 * These bitmask flags indicate whether tasks in the fork and exit paths have
204 * fork/exit handlers to call. This avoids us having to do extra work in the
205 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700206 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000207static unsigned long have_fork_callback __read_mostly;
208static unsigned long have_exit_callback __read_mostly;
Tejun Heoafcf6c82015-10-15 16:41:53 -0400209static unsigned long have_free_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 Heoed27b9f2015-10-15 16:41:52 -0400219static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800220static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400221static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
222 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400223static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400224static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400225static int cgroup_addrm_files(struct cgroup_subsys_state *css,
226 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400227 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800228
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400229/**
230 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
231 * @ssid: subsys ID of interest
232 *
233 * cgroup_subsys_enabled() can only be used with literal subsys names which
234 * is fine for individual subsystems but unsuitable for cgroup core. This
235 * is slower static_key_enabled() based test indexed by @ssid.
236 */
237static bool cgroup_ssid_enabled(int ssid)
238{
239 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
240}
241
Tejun Heo9e10a132015-09-18 11:56:28 -0400242/**
243 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
244 * @cgrp: the cgroup of interest
245 *
246 * The default hierarchy is the v2 interface of cgroup and this function
247 * can be used to test whether a cgroup is on the default hierarchy for
248 * cases where a subsystem should behave differnetly depending on the
249 * interface version.
250 *
251 * The set of behaviors which change on the default hierarchy are still
252 * being determined and the mount option is prefixed with __DEVEL__.
253 *
254 * List of changed behaviors:
255 *
256 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
257 * and "name" are disallowed.
258 *
259 * - When mounting an existing superblock, mount options should match.
260 *
261 * - Remount is disallowed.
262 *
263 * - rename(2) is disallowed.
264 *
265 * - "tasks" is removed. Everything should be at process granularity. Use
266 * "cgroup.procs" instead.
267 *
268 * - "cgroup.procs" is not sorted. pids will be unique unless they got
269 * recycled inbetween reads.
270 *
271 * - "release_agent" and "notify_on_release" are removed. Replacement
272 * notification mechanism will be implemented.
273 *
274 * - "cgroup.clone_children" is removed.
275 *
276 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
277 * and its descendants contain no task; otherwise, 1. The file also
278 * generates kernfs notification which can be monitored through poll and
279 * [di]notify when the value of the file changes.
280 *
281 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
282 * take masks of ancestors with non-empty cpus/mems, instead of being
283 * moved to an ancestor.
284 *
285 * - cpuset: a task can be moved into an empty cpuset, and again it takes
286 * masks of ancestors.
287 *
288 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
289 * is not created.
290 *
291 * - blkcg: blk-throttle becomes properly hierarchical.
292 *
293 * - debug: disallowed on the default hierarchy.
294 */
295static bool cgroup_on_dfl(const struct cgroup *cgrp)
296{
297 return cgrp->root == &cgrp_dfl_root;
298}
299
Tejun Heo6fa49182014-05-04 15:09:13 -0400300/* IDR wrappers which synchronize using cgroup_idr_lock */
301static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
302 gfp_t gfp_mask)
303{
304 int ret;
305
306 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400307 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800308 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400309 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400310 idr_preload_end();
311 return ret;
312}
313
314static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
315{
316 void *ret;
317
Tejun Heo54504e92014-05-13 12:10:59 -0400318 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400319 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400320 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400321 return ret;
322}
323
324static void cgroup_idr_remove(struct idr *idr, int id)
325{
Tejun Heo54504e92014-05-13 12:10:59 -0400326 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400327 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400328 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400329}
330
Tejun Heod51f39b2014-05-16 13:22:48 -0400331static struct cgroup *cgroup_parent(struct cgroup *cgrp)
332{
333 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
334
335 if (parent_css)
336 return container_of(parent_css, struct cgroup, self);
337 return NULL;
338}
339
Tejun Heo95109b62013-08-08 20:11:27 -0400340/**
341 * cgroup_css - obtain a cgroup's css for the specified subsystem
342 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400343 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400344 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400345 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
346 * function must be called either under cgroup_mutex or rcu_read_lock() and
347 * the caller is responsible for pinning the returned css if it wants to
348 * keep accessing it outside the said locks. This function may return
349 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400350 */
351static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400352 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400353{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400354 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500355 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500356 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400357 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400358 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400359}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700360
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400361/**
362 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
363 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400364 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400365 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400366 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400367 * as the matching css of the nearest ancestor including self which has @ss
368 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
369 * function is guaranteed to return non-NULL css.
370 */
371static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
372 struct cgroup_subsys *ss)
373{
374 lockdep_assert_held(&cgroup_mutex);
375
376 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400377 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400378
379 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
380 return NULL;
381
Tejun Heoeeecbd12014-11-18 02:49:52 -0500382 /*
383 * This function is used while updating css associations and thus
384 * can't test the csses directly. Use ->child_subsys_mask.
385 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400386 while (cgroup_parent(cgrp) &&
387 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
388 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400389
390 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700391}
392
Tejun Heoeeecbd12014-11-18 02:49:52 -0500393/**
394 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
395 * @cgrp: the cgroup of interest
396 * @ss: the subsystem of interest
397 *
398 * Find and get the effective css of @cgrp for @ss. The effective css is
399 * defined as the matching css of the nearest ancestor including self which
400 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
401 * the root css is returned, so this function always returns a valid css.
402 * The returned css must be put using css_put().
403 */
404struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
405 struct cgroup_subsys *ss)
406{
407 struct cgroup_subsys_state *css;
408
409 rcu_read_lock();
410
411 do {
412 css = cgroup_css(cgrp, ss);
413
414 if (css && css_tryget_online(css))
415 goto out_unlock;
416 cgrp = cgroup_parent(cgrp);
417 } while (cgrp);
418
419 css = init_css_set.subsys[ss->id];
420 css_get(css);
421out_unlock:
422 rcu_read_unlock();
423 return css;
424}
425
Paul Menageddbcc7e2007-10-18 23:39:30 -0700426/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700427static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700428{
Tejun Heo184faf32014-05-16 13:22:51 -0400429 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700430}
431
Tejun Heo052c3f32015-10-15 16:41:50 -0400432static void cgroup_get(struct cgroup *cgrp)
433{
434 WARN_ON_ONCE(cgroup_is_dead(cgrp));
435 css_get(&cgrp->self);
436}
437
438static bool cgroup_tryget(struct cgroup *cgrp)
439{
440 return css_tryget(&cgrp->self);
441}
442
443static void cgroup_put(struct cgroup *cgrp)
444{
445 css_put(&cgrp->self);
446}
447
Tejun Heob4168642014-05-13 12:16:21 -0400448struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500449{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500450 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400451 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500452
453 /*
454 * This is open and unprotected implementation of cgroup_css().
455 * seq_css() is only called from a kernfs file operation which has
456 * an active reference on the file. Because all the subsystem
457 * files are drained before a css is disassociated with a cgroup,
458 * the matching css from the cgroup's subsys table is guaranteed to
459 * be and stay valid until the enclosing operation is complete.
460 */
461 if (cft->ss)
462 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
463 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400464 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500465}
Tejun Heob4168642014-05-13 12:16:21 -0400466EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500467
Li Zefan78574cf2013-04-08 19:00:38 -0700468/**
469 * cgroup_is_descendant - test ancestry
470 * @cgrp: the cgroup to be tested
471 * @ancestor: possible ancestor of @cgrp
472 *
473 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
474 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
475 * and @ancestor are accessible.
476 */
477bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
478{
479 while (cgrp) {
480 if (cgrp == ancestor)
481 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400482 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700483 }
484 return false;
485}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700486
Adrian Bunke9685a02008-02-07 00:13:46 -0800487static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700488{
Paul Menagebd89aab2007-10-18 23:40:44 -0700489 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700490}
491
Tejun Heo30159ec2013-06-25 11:53:37 -0700492/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500493 * for_each_css - iterate all css's of a cgroup
494 * @css: the iteration cursor
495 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
496 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700497 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400498 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700499 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500500#define for_each_css(css, ssid, cgrp) \
501 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
502 if (!((css) = rcu_dereference_check( \
503 (cgrp)->subsys[(ssid)], \
504 lockdep_is_held(&cgroup_mutex)))) { } \
505 else
506
507/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400508 * for_each_e_css - iterate all effective css's of a cgroup
509 * @css: the iteration cursor
510 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
511 * @cgrp: the target cgroup to iterate css's of
512 *
513 * Should be called under cgroup_[tree_]mutex.
514 */
515#define for_each_e_css(css, ssid, cgrp) \
516 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
517 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
518 ; \
519 else
520
521/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500522 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700523 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500524 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700525 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500526#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500527 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
528 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700529
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000530/**
531 * for_each_subsys_which - filter for_each_subsys with a bitmask
532 * @ss: the iteration cursor
533 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
534 * @ss_maskp: a pointer to the bitmask
535 *
536 * The block will only run for cases where the ssid-th bit (1 << ssid) of
537 * mask is set to 1.
538 */
539#define for_each_subsys_which(ss, ssid, ss_maskp) \
540 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000541 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000542 else \
543 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
544 if (((ss) = cgroup_subsys[ssid]) && false) \
545 break; \
546 else
547
Tejun Heo985ed672014-03-19 10:23:53 -0400548/* iterate across the hierarchies */
549#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700550 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700551
Tejun Heof8f22e52014-04-23 11:13:16 -0400552/* iterate over child cgrps, lock should be held throughout iteration */
553#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400554 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400555 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400556 cgroup_is_dead(child); })) \
557 ; \
558 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700559
Paul Menage81a6a5c2007-10-18 23:39:38 -0700560static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700561static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700562
Tejun Heo69d02062013-06-12 21:04:50 -0700563/*
564 * A cgroup can be associated with multiple css_sets as different tasks may
565 * belong to different cgroups on different hierarchies. In the other
566 * direction, a css_set is naturally associated with multiple cgroups.
567 * This M:N relationship is represented by the following link structure
568 * which exists for each association and allows traversing the associations
569 * from both sides.
570 */
571struct cgrp_cset_link {
572 /* the cgroup and css_set this link associates */
573 struct cgroup *cgrp;
574 struct css_set *cset;
575
576 /* list of cgrp_cset_links anchored at cgrp->cset_links */
577 struct list_head cset_link;
578
579 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
580 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700581};
582
Tejun Heo172a2c062014-03-19 10:23:53 -0400583/*
584 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700585 * hierarchies being mounted. It contains a pointer to the root state
586 * for each subsystem. Also used to anchor the list of css_sets. Not
587 * reference-counted, to improve performance when child cgroups
588 * haven't been created.
589 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400590struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400591 .refcount = ATOMIC_INIT(1),
592 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
593 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
594 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
595 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
596 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400597 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400598};
Paul Menage817929e2007-10-18 23:39:36 -0700599
Tejun Heo172a2c062014-03-19 10:23:53 -0400600static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700601
Tejun Heo842b5972014-04-25 18:28:02 -0400602/**
Tejun Heo0de09422015-10-15 16:41:49 -0400603 * css_set_populated - does a css_set contain any tasks?
604 * @cset: target css_set
605 */
606static bool css_set_populated(struct css_set *cset)
607{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400608 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400609
610 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
611}
612
613/**
Tejun Heo842b5972014-04-25 18:28:02 -0400614 * cgroup_update_populated - updated populated count of a cgroup
615 * @cgrp: the target cgroup
616 * @populated: inc or dec populated count
617 *
Tejun Heo0de09422015-10-15 16:41:49 -0400618 * One of the css_sets associated with @cgrp is either getting its first
619 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
620 * count is propagated towards root so that a given cgroup's populated_cnt
621 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400622 *
623 * @cgrp's interface file "cgroup.populated" is zero if
624 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
625 * changes from or to zero, userland is notified that the content of the
626 * interface file has changed. This can be used to detect when @cgrp and
627 * its descendants become populated or empty.
628 */
629static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
630{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400631 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400632
633 do {
634 bool trigger;
635
636 if (populated)
637 trigger = !cgrp->populated_cnt++;
638 else
639 trigger = !--cgrp->populated_cnt;
640
641 if (!trigger)
642 break;
643
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400644 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400645 cgroup_file_notify(&cgrp->events_file);
646
Tejun Heod51f39b2014-05-16 13:22:48 -0400647 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400648 } while (cgrp);
649}
650
Tejun Heo0de09422015-10-15 16:41:49 -0400651/**
652 * css_set_update_populated - update populated state of a css_set
653 * @cset: target css_set
654 * @populated: whether @cset is populated or depopulated
655 *
656 * @cset is either getting the first task or losing the last. Update the
657 * ->populated_cnt of all associated cgroups accordingly.
658 */
659static void css_set_update_populated(struct css_set *cset, bool populated)
660{
661 struct cgrp_cset_link *link;
662
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400663 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400664
665 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
666 cgroup_update_populated(link->cgrp, populated);
667}
668
Tejun Heof6d7d042015-10-15 16:41:52 -0400669/**
670 * css_set_move_task - move a task from one css_set to another
671 * @task: task being moved
672 * @from_cset: css_set @task currently belongs to (may be NULL)
673 * @to_cset: new css_set @task is being moved to (may be NULL)
674 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
675 *
676 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
677 * css_set, @from_cset can be NULL. If @task is being disassociated
678 * instead of moved, @to_cset can be NULL.
679 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400680 * This function automatically handles populated_cnt updates and
681 * css_task_iter adjustments but the caller is responsible for managing
682 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400683 */
684static void css_set_move_task(struct task_struct *task,
685 struct css_set *from_cset, struct css_set *to_cset,
686 bool use_mg_tasks)
687{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400688 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400689
690 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400691 struct css_task_iter *it, *pos;
692
Tejun Heof6d7d042015-10-15 16:41:52 -0400693 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400694
695 /*
696 * @task is leaving, advance task iterators which are
697 * pointing to it so that they can resume at the next
698 * position. Advancing an iterator might remove it from
699 * the list, use safe walk. See css_task_iter_advance*()
700 * for details.
701 */
702 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
703 iters_node)
704 if (it->task_pos == &task->cg_list)
705 css_task_iter_advance(it);
706
Tejun Heof6d7d042015-10-15 16:41:52 -0400707 list_del_init(&task->cg_list);
708 if (!css_set_populated(from_cset))
709 css_set_update_populated(from_cset, false);
710 } else {
711 WARN_ON_ONCE(!list_empty(&task->cg_list));
712 }
713
714 if (to_cset) {
715 /*
716 * We are synchronized through cgroup_threadgroup_rwsem
717 * against PF_EXITING setting such that we can't race
718 * against cgroup_exit() changing the css_set to
719 * init_css_set and dropping the old one.
720 */
721 WARN_ON_ONCE(task->flags & PF_EXITING);
722
723 if (!css_set_populated(to_cset))
724 css_set_update_populated(to_cset, true);
725 rcu_assign_pointer(task->cgroups, to_cset);
726 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
727 &to_cset->tasks);
728 }
729}
730
Paul Menage7717f7b2009-09-23 15:56:22 -0700731/*
732 * hash table for cgroup groups. This improves the performance to find
733 * an existing css_set. This hash doesn't (currently) take into
734 * account cgroups in empty hierarchies.
735 */
Li Zefan472b1052008-04-29 01:00:11 -0700736#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800737static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700738
Li Zefan0ac801f2013-01-10 11:49:27 +0800739static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700740{
Li Zefan0ac801f2013-01-10 11:49:27 +0800741 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700742 struct cgroup_subsys *ss;
743 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700744
Tejun Heo30159ec2013-06-25 11:53:37 -0700745 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800746 key += (unsigned long)css[i];
747 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700748
Li Zefan0ac801f2013-01-10 11:49:27 +0800749 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700750}
751
Zefan Lia25eb522014-09-19 16:51:00 +0800752static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700753{
Tejun Heo69d02062013-06-12 21:04:50 -0700754 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400755 struct cgroup_subsys *ss;
756 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700757
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400758 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500759
760 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700761 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700762
Tejun Heo53254f92015-11-23 14:55:41 -0500763 /* This css_set is dead. unlink it and release cgroup and css refs */
764 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400765 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500766 css_put(cset->subsys[ssid]);
767 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700768 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700769 css_set_count--;
770
Tejun Heo69d02062013-06-12 21:04:50 -0700771 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700772 list_del(&link->cset_link);
773 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400774 if (cgroup_parent(link->cgrp))
775 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700776 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700777 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700778
Tejun Heo5abb8852013-06-12 21:04:49 -0700779 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700780}
781
Zefan Lia25eb522014-09-19 16:51:00 +0800782static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500783{
784 /*
785 * Ensure that the refcount doesn't hit zero while any readers
786 * can see it. Similar to atomic_dec_and_lock(), but for an
787 * rwlock
788 */
789 if (atomic_add_unless(&cset->refcount, -1, 1))
790 return;
791
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400792 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800793 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400794 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500795}
796
Paul Menage817929e2007-10-18 23:39:36 -0700797/*
798 * refcounted get/put for css_set objects
799 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700800static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700801{
Tejun Heo5abb8852013-06-12 21:04:49 -0700802 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700803}
804
Tejun Heob326f9d2013-06-24 15:21:48 -0700805/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700806 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700807 * @cset: candidate css_set being tested
808 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700809 * @new_cgrp: cgroup that's being entered by the task
810 * @template: desired set of css pointers in css_set (pre-calculated)
811 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800812 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700813 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
814 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700815static bool compare_css_sets(struct css_set *cset,
816 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700817 struct cgroup *new_cgrp,
818 struct cgroup_subsys_state *template[])
819{
820 struct list_head *l1, *l2;
821
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400822 /*
823 * On the default hierarchy, there can be csets which are
824 * associated with the same set of cgroups but different csses.
825 * Let's first ensure that csses match.
826 */
827 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700828 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700829
830 /*
831 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400832 * different cgroups in hierarchies. As different cgroups may
833 * share the same effective css, this comparison is always
834 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700835 */
Tejun Heo69d02062013-06-12 21:04:50 -0700836 l1 = &cset->cgrp_links;
837 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700838 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700839 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700840 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700841
842 l1 = l1->next;
843 l2 = l2->next;
844 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700845 if (l1 == &cset->cgrp_links) {
846 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700847 break;
848 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700849 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700850 }
851 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700852 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
853 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
854 cgrp1 = link1->cgrp;
855 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700856 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700857 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700858
859 /*
860 * If this hierarchy is the hierarchy of the cgroup
861 * that's changing, then we need to check that this
862 * css_set points to the new cgroup; if it's any other
863 * hierarchy, then this css_set should point to the
864 * same cgroup as the old css_set.
865 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700866 if (cgrp1->root == new_cgrp->root) {
867 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700868 return false;
869 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700870 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700871 return false;
872 }
873 }
874 return true;
875}
876
Tejun Heob326f9d2013-06-24 15:21:48 -0700877/**
878 * find_existing_css_set - init css array and find the matching css_set
879 * @old_cset: the css_set that we're using before the cgroup transition
880 * @cgrp: the cgroup that we're moving into
881 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700882 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700883static struct css_set *find_existing_css_set(struct css_set *old_cset,
884 struct cgroup *cgrp,
885 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700886{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400887 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700888 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700889 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800890 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700891 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700892
Ben Blumaae8aab2010-03-10 15:22:07 -0800893 /*
894 * Build the set of subsystem state objects that we want to see in the
895 * new css_set. while subsystems can change globally, the entries here
896 * won't change, so no need for locking.
897 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700898 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400899 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400900 /*
901 * @ss is in this hierarchy, so we want the
902 * effective css from @cgrp.
903 */
904 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700905 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400906 /*
907 * @ss is not in this hierarchy, so we don't want
908 * to change the css.
909 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700910 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700911 }
912 }
913
Li Zefan0ac801f2013-01-10 11:49:27 +0800914 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700915 hash_for_each_possible(css_set_table, cset, hlist, key) {
916 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700917 continue;
918
919 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700920 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700921 }
Paul Menage817929e2007-10-18 23:39:36 -0700922
923 /* No existing cgroup group matched */
924 return NULL;
925}
926
Tejun Heo69d02062013-06-12 21:04:50 -0700927static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700928{
Tejun Heo69d02062013-06-12 21:04:50 -0700929 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700930
Tejun Heo69d02062013-06-12 21:04:50 -0700931 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
932 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700933 kfree(link);
934 }
935}
936
Tejun Heo69d02062013-06-12 21:04:50 -0700937/**
938 * allocate_cgrp_cset_links - allocate cgrp_cset_links
939 * @count: the number of links to allocate
940 * @tmp_links: list_head the allocated links are put on
941 *
942 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
943 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700944 */
Tejun Heo69d02062013-06-12 21:04:50 -0700945static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700946{
Tejun Heo69d02062013-06-12 21:04:50 -0700947 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700948 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700949
950 INIT_LIST_HEAD(tmp_links);
951
Li Zefan36553432008-07-29 22:33:19 -0700952 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700953 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700954 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700955 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700956 return -ENOMEM;
957 }
Tejun Heo69d02062013-06-12 21:04:50 -0700958 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700959 }
960 return 0;
961}
962
Li Zefanc12f65d2009-01-07 18:07:42 -0800963/**
964 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700965 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700966 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800967 * @cgrp: the destination cgroup
968 */
Tejun Heo69d02062013-06-12 21:04:50 -0700969static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
970 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800971{
Tejun Heo69d02062013-06-12 21:04:50 -0700972 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800973
Tejun Heo69d02062013-06-12 21:04:50 -0700974 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400975
976 if (cgroup_on_dfl(cgrp))
977 cset->dfl_cgrp = cgrp;
978
Tejun Heo69d02062013-06-12 21:04:50 -0700979 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
980 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700981 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400982
Paul Menage7717f7b2009-09-23 15:56:22 -0700983 /*
Tejun Heo389b9c12015-10-15 16:41:51 -0400984 * Always add links to the tail of the lists so that the lists are
985 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -0700986 */
Tejun Heo389b9c12015-10-15 16:41:51 -0400987 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -0700988 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400989
990 if (cgroup_parent(cgrp))
991 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -0800992}
993
Tejun Heob326f9d2013-06-24 15:21:48 -0700994/**
995 * find_css_set - return a new css_set with one cgroup updated
996 * @old_cset: the baseline css_set
997 * @cgrp: the cgroup to be updated
998 *
999 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1000 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001001 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001002static struct css_set *find_css_set(struct css_set *old_cset,
1003 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001004{
Tejun Heob326f9d2013-06-24 15:21:48 -07001005 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001006 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001007 struct list_head tmp_links;
1008 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001009 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001010 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001011 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001012
Tejun Heob326f9d2013-06-24 15:21:48 -07001013 lockdep_assert_held(&cgroup_mutex);
1014
Paul Menage817929e2007-10-18 23:39:36 -07001015 /* First see if we already have a cgroup group that matches
1016 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001017 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001018 cset = find_existing_css_set(old_cset, cgrp, template);
1019 if (cset)
1020 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001021 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001022
Tejun Heo5abb8852013-06-12 21:04:49 -07001023 if (cset)
1024 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001025
Tejun Heof4f4be22013-06-12 21:04:51 -07001026 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001027 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001028 return NULL;
1029
Tejun Heo69d02062013-06-12 21:04:50 -07001030 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001031 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001032 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001033 return NULL;
1034 }
1035
Tejun Heo5abb8852013-06-12 21:04:49 -07001036 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001037 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001038 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001039 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001040 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001041 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001042 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001043 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001044
1045 /* Copy the set of subsystem state objects generated in
1046 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001047 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001048
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001049 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001050 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001051 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001052 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001053
Paul Menage7717f7b2009-09-23 15:56:22 -07001054 if (c->root == cgrp->root)
1055 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001056 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001057 }
Paul Menage817929e2007-10-18 23:39:36 -07001058
Tejun Heo69d02062013-06-12 21:04:50 -07001059 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001060
Paul Menage817929e2007-10-18 23:39:36 -07001061 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001062
Tejun Heo2d8f2432014-04-23 11:13:15 -04001063 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001064 key = css_set_hash(cset->subsys);
1065 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001066
Tejun Heo53254f92015-11-23 14:55:41 -05001067 for_each_subsys(ss, ssid) {
1068 struct cgroup_subsys_state *css = cset->subsys[ssid];
1069
Tejun Heo2d8f2432014-04-23 11:13:15 -04001070 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001071 &css->cgroup->e_csets[ssid]);
1072 css_get(css);
1073 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001074
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001075 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001076
Tejun Heo5abb8852013-06-12 21:04:49 -07001077 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001078}
1079
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001080static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001081{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001082 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001083
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001084 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001085}
1086
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001087static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001088{
1089 int id;
1090
1091 lockdep_assert_held(&cgroup_mutex);
1092
Tejun Heo985ed672014-03-19 10:23:53 -04001093 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001094 if (id < 0)
1095 return id;
1096
1097 root->hierarchy_id = id;
1098 return 0;
1099}
1100
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001101static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001102{
1103 lockdep_assert_held(&cgroup_mutex);
1104
1105 if (root->hierarchy_id) {
1106 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1107 root->hierarchy_id = 0;
1108 }
1109}
1110
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001111static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001112{
1113 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001114 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001115 WARN_ON_ONCE(root->hierarchy_id);
1116
1117 idr_destroy(&root->cgroup_idr);
1118 kfree(root);
1119 }
1120}
1121
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001122static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001123{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001124 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001125 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001126
Tejun Heo2bd59d42014-02-11 11:52:49 -05001127 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001128
Tejun Heo776f02f2014-02-12 09:29:50 -05001129 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001130 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001131
Tejun Heof2e85d52014-02-11 11:52:49 -05001132 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001133 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001134
1135 /*
1136 * Release all the links from cset_links to this hierarchy's
1137 * root cgroup
1138 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001139 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001140
1141 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1142 list_del(&link->cset_link);
1143 list_del(&link->cgrp_link);
1144 kfree(link);
1145 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001146
1147 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001148
1149 if (!list_empty(&root->root_list)) {
1150 list_del(&root->root_list);
1151 cgroup_root_count--;
1152 }
1153
1154 cgroup_exit_root_id(root);
1155
1156 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001157
Tejun Heo2bd59d42014-02-11 11:52:49 -05001158 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001159 cgroup_free_root(root);
1160}
1161
Tejun Heoceb6a082014-02-25 10:04:02 -05001162/* look up cgroup associated with given css_set on the specified hierarchy */
1163static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001164 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001165{
Paul Menage7717f7b2009-09-23 15:56:22 -07001166 struct cgroup *res = NULL;
1167
Tejun Heo96d365e2014-02-13 06:58:40 -05001168 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001169 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001170
Tejun Heo5abb8852013-06-12 21:04:49 -07001171 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001172 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001173 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001174 struct cgrp_cset_link *link;
1175
1176 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001177 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001178
Paul Menage7717f7b2009-09-23 15:56:22 -07001179 if (c->root == root) {
1180 res = c;
1181 break;
1182 }
1183 }
1184 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001185
Paul Menage7717f7b2009-09-23 15:56:22 -07001186 BUG_ON(!res);
1187 return res;
1188}
1189
1190/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001191 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001192 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001193 */
1194static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001195 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001196{
1197 /*
1198 * No need to lock the task - since we hold cgroup_mutex the
1199 * task can't change groups, so the only thing that can happen
1200 * is that it exits and its css is set back to init_css_set.
1201 */
1202 return cset_cgroup_from_root(task_css_set(task), root);
1203}
1204
1205/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001206 * A task must hold cgroup_mutex to modify cgroups.
1207 *
1208 * Any task can increment and decrement the count field without lock.
1209 * So in general, code holding cgroup_mutex can't rely on the count
1210 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001211 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 * means that no tasks are currently attached, therefore there is no
1213 * way a task attached to that cgroup can fork (the other way to
1214 * increment the count). So code holding cgroup_mutex can safely
1215 * assume that if the count is zero, it will stay zero. Similarly, if
1216 * a task holds cgroup_mutex on a cgroup with zero count, it
1217 * knows that the cgroup won't be removed, as cgroup_rmdir()
1218 * needs that mutex.
1219 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001220 * A cgroup can only be deleted if both its 'count' of using tasks
1221 * is zero, and its list of 'children' cgroups is empty. Since all
1222 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001223 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001224 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001225 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001226 *
1227 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001228 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229 */
1230
Tejun Heo2bd59d42014-02-11 11:52:49 -05001231static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001232static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001233
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001234static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1235 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001236{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001237 struct cgroup_subsys *ss = cft->ss;
1238
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001239 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1240 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1241 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001242 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1243 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001244 else
1245 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1246 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247}
1248
Tejun Heof2e85d52014-02-11 11:52:49 -05001249/**
1250 * cgroup_file_mode - deduce file mode of a control file
1251 * @cft: the control file in question
1252 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001253 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001254 */
1255static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001256{
Tejun Heof2e85d52014-02-11 11:52:49 -05001257 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001258
Tejun Heof2e85d52014-02-11 11:52:49 -05001259 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1260 mode |= S_IRUGO;
1261
Tejun Heo7dbdb192015-09-18 17:54:23 -04001262 if (cft->write_u64 || cft->write_s64 || cft->write) {
1263 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1264 mode |= S_IWUGO;
1265 else
1266 mode |= S_IWUSR;
1267 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001268
1269 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001270}
1271
Tejun Heoa9746d82014-05-13 12:19:22 -04001272/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001273 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001274 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001275 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001276 *
1277 * On the default hierarchy, a subsystem may request other subsystems to be
1278 * enabled together through its ->depends_on mask. In such cases, more
1279 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1280 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001281 * This function calculates which subsystems need to be enabled if
1282 * @subtree_control is to be applied to @cgrp. The returned mask is always
1283 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001284 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001285static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1286 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001287{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001288 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001289 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001290 struct cgroup_subsys *ss;
1291 int ssid;
1292
1293 lockdep_assert_held(&cgroup_mutex);
1294
Tejun Heo0f060de2014-11-18 02:49:50 -05001295 if (!cgroup_on_dfl(cgrp))
1296 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001297
1298 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001299 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001300
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001301 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1302 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001303
1304 /*
1305 * Mask out subsystems which aren't available. This can
1306 * happen only if some depended-upon subsystems were bound
1307 * to non-default hierarchies.
1308 */
1309 if (parent)
1310 new_ss_mask &= parent->child_subsys_mask;
1311 else
1312 new_ss_mask &= cgrp->root->subsys_mask;
1313
1314 if (new_ss_mask == cur_ss_mask)
1315 break;
1316 cur_ss_mask = new_ss_mask;
1317 }
1318
Tejun Heo0f060de2014-11-18 02:49:50 -05001319 return cur_ss_mask;
1320}
1321
1322/**
1323 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1324 * @cgrp: the target cgroup
1325 *
1326 * Update @cgrp->child_subsys_mask according to the current
1327 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1328 */
1329static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1330{
1331 cgrp->child_subsys_mask =
1332 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001333}
1334
Tejun Heoa9746d82014-05-13 12:19:22 -04001335/**
1336 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1337 * @kn: the kernfs_node being serviced
1338 *
1339 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1340 * the method finishes if locking succeeded. Note that once this function
1341 * returns the cgroup returned by cgroup_kn_lock_live() may become
1342 * inaccessible any time. If the caller intends to continue to access the
1343 * cgroup, it should pin it before invoking this function.
1344 */
1345static void cgroup_kn_unlock(struct kernfs_node *kn)
1346{
1347 struct cgroup *cgrp;
1348
1349 if (kernfs_type(kn) == KERNFS_DIR)
1350 cgrp = kn->priv;
1351 else
1352 cgrp = kn->parent->priv;
1353
1354 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001355
1356 kernfs_unbreak_active_protection(kn);
1357 cgroup_put(cgrp);
1358}
1359
1360/**
1361 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1362 * @kn: the kernfs_node being serviced
1363 *
1364 * This helper is to be used by a cgroup kernfs method currently servicing
1365 * @kn. It breaks the active protection, performs cgroup locking and
1366 * verifies that the associated cgroup is alive. Returns the cgroup if
1367 * alive; otherwise, %NULL. A successful return should be undone by a
1368 * matching cgroup_kn_unlock() invocation.
1369 *
1370 * Any cgroup kernfs method implementation which requires locking the
1371 * associated cgroup should use this helper. It avoids nesting cgroup
1372 * locking under kernfs active protection and allows all kernfs operations
1373 * including self-removal.
1374 */
1375static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1376{
1377 struct cgroup *cgrp;
1378
1379 if (kernfs_type(kn) == KERNFS_DIR)
1380 cgrp = kn->priv;
1381 else
1382 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383
Tejun Heo2bd59d42014-02-11 11:52:49 -05001384 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001385 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001386 * active_ref. cgroup liveliness check alone provides enough
1387 * protection against removal. Ensure @cgrp stays accessible and
1388 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001389 */
Li Zefanaa323622014-09-04 14:43:38 +08001390 if (!cgroup_tryget(cgrp))
1391 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001392 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393
Tejun Heoa9746d82014-05-13 12:19:22 -04001394 mutex_lock(&cgroup_mutex);
1395
1396 if (!cgroup_is_dead(cgrp))
1397 return cgrp;
1398
1399 cgroup_kn_unlock(kn);
1400 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401}
1402
Li Zefan2739d3c2013-01-21 18:18:33 +08001403static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001405 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406
Tejun Heo01f64742014-05-13 12:19:23 -04001407 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001408
1409 if (cft->file_offset) {
1410 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1411 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1412
1413 spin_lock_irq(&cgroup_file_kn_lock);
1414 cfile->kn = NULL;
1415 spin_unlock_irq(&cgroup_file_kn_lock);
1416 }
1417
Tejun Heo2bd59d42014-02-11 11:52:49 -05001418 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001419}
1420
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001421/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001422 * css_clear_dir - remove subsys files in a cgroup directory
1423 * @css: taget css
1424 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001425 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001426static void css_clear_dir(struct cgroup_subsys_state *css,
1427 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001428{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001429 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1430 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001431
Tejun Heo4df8dc92015-09-18 17:54:23 -04001432 list_for_each_entry(cfts, &css->ss->cfts, node)
1433 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434}
1435
Tejun Heoccdca212015-09-18 17:54:23 -04001436/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001437 * css_populate_dir - create subsys files in a cgroup directory
1438 * @css: target css
1439 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001440 *
1441 * On failure, no file is added.
1442 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001443static int css_populate_dir(struct cgroup_subsys_state *css,
1444 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001445{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001446 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1447 struct cftype *cfts, *failed_cfts;
1448 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001449
Tejun Heo4df8dc92015-09-18 17:54:23 -04001450 if (!css->ss) {
1451 if (cgroup_on_dfl(cgrp))
1452 cfts = cgroup_dfl_base_files;
1453 else
1454 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001455
Tejun Heo4df8dc92015-09-18 17:54:23 -04001456 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1457 }
Tejun Heoccdca212015-09-18 17:54:23 -04001458
Tejun Heo4df8dc92015-09-18 17:54:23 -04001459 list_for_each_entry(cfts, &css->ss->cfts, node) {
1460 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1461 if (ret < 0) {
1462 failed_cfts = cfts;
1463 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001464 }
1465 }
1466 return 0;
1467err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001468 list_for_each_entry(cfts, &css->ss->cfts, node) {
1469 if (cfts == failed_cfts)
1470 break;
1471 cgroup_addrm_files(css, cgrp, cfts, false);
1472 }
Tejun Heoccdca212015-09-18 17:54:23 -04001473 return ret;
1474}
1475
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001476static int rebind_subsystems(struct cgroup_root *dst_root,
1477 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478{
Tejun Heo1ada4832015-09-18 17:54:23 -04001479 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001480 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001481 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001482 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483
Tejun Heoace2bee2014-02-11 11:52:47 -05001484 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001485
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001486 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001487 /* if @ss has non-root csses attached to it, can't move */
1488 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001489 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490
Tejun Heo5df36032014-03-19 10:23:54 -04001491 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001492 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001493 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494 }
1495
Tejun Heo5533e012014-05-14 19:33:07 -04001496 /* skip creating root files on dfl_root for inhibited subsystems */
1497 tmp_ss_mask = ss_mask;
1498 if (dst_root == &cgrp_dfl_root)
1499 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1500
Tejun Heo4df8dc92015-09-18 17:54:23 -04001501 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
1502 struct cgroup *scgrp = &ss->root->cgrp;
1503 int tssid;
1504
1505 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1506 if (!ret)
1507 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
Tejun Heoa2dd4242014-03-19 10:23:55 -04001509 /*
1510 * Rebinding back to the default root is not allowed to
1511 * fail. Using both default and non-default roots should
1512 * be rare. Moving subsystems back and forth even more so.
1513 * Just warn about it and continue.
1514 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001515 if (dst_root == &cgrp_dfl_root) {
1516 if (cgrp_dfl_root_visible) {
1517 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
1518 ret, ss_mask);
1519 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1520 }
1521 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001522 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001523
1524 for_each_subsys_which(ss, tssid, &tmp_ss_mask) {
1525 if (tssid == ssid)
1526 break;
1527 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
1528 }
1529 return ret;
Tejun Heo5df36032014-03-19 10:23:54 -04001530 }
Tejun Heo31261212013-06-28 17:07:30 -07001531
1532 /*
1533 * Nothing can fail from this point on. Remove files for the
1534 * removed subsystems and rebind each subsystem.
1535 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001536 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001537 struct cgroup_root *src_root = ss->root;
1538 struct cgroup *scgrp = &src_root->cgrp;
1539 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001540 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001541
Tejun Heo1ada4832015-09-18 17:54:23 -04001542 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001543
Tejun Heo4df8dc92015-09-18 17:54:23 -04001544 css_clear_dir(css, NULL);
1545
Tejun Heo1ada4832015-09-18 17:54:23 -04001546 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1547 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001548 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001549 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001550
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001551 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001552 hash_for_each(css_set_table, i, cset, hlist)
1553 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001554 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001555 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001556
Tejun Heof392e512014-04-23 11:13:14 -04001557 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001558 scgrp->subtree_control &= ~(1 << ssid);
1559 cgroup_refresh_child_subsys_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001560
Tejun Heobd53d612014-04-23 11:13:16 -04001561 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001562 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001563 if (dst_root == &cgrp_dfl_root) {
1564 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1565 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001566 dcgrp->subtree_control |= 1 << ssid;
1567 cgroup_refresh_child_subsys_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001568 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001569 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001570
Tejun Heo5df36032014-03-19 10:23:54 -04001571 if (ss->bind)
1572 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
Tejun Heo1ada4832015-09-18 17:54:23 -04001575 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 return 0;
1577}
1578
Tejun Heo2bd59d42014-02-11 11:52:49 -05001579static int cgroup_show_options(struct seq_file *seq,
1580 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001582 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001584 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585
Tejun Heod98817d2015-08-18 13:58:16 -07001586 if (root != &cgrp_dfl_root)
1587 for_each_subsys(ss, ssid)
1588 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001589 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001590 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001592 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001593 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001594
1595 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001596 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001597 seq_show_option(seq, "release_agent",
1598 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001599 spin_unlock(&release_agent_path_lock);
1600
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001601 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001602 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001603 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001604 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 return 0;
1606}
1607
1608struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001609 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001610 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001611 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001612 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001613 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001614 /* User explicitly requested empty subsystem */
1615 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616};
1617
Ben Blumcf5d5942010-03-10 15:22:09 -08001618static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001620 char *token, *o = data;
1621 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001622 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001623 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001624 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001625 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001626
1627#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001628 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001629#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Paul Menagec6d57f32009-09-23 15:56:19 -07001631 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
1633 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001634 nr_opts++;
1635
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 if (!*token)
1637 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001638 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001639 /* Explicitly have no subsystems */
1640 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001641 continue;
1642 }
1643 if (!strcmp(token, "all")) {
1644 /* Mutually exclusive option 'all' + subsystem name */
1645 if (one_ss)
1646 return -EINVAL;
1647 all_ss = true;
1648 continue;
1649 }
Tejun Heo873fe092013-04-14 20:15:26 -07001650 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1651 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1652 continue;
1653 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001654 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001655 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001656 continue;
1657 }
1658 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001659 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001660 continue;
1661 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001662 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001663 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001664 continue;
1665 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001666 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001667 /* Specifying two release agents is forbidden */
1668 if (opts->release_agent)
1669 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001670 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001671 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001672 if (!opts->release_agent)
1673 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001674 continue;
1675 }
1676 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001677 const char *name = token + 5;
1678 /* Can't specify an empty name */
1679 if (!strlen(name))
1680 return -EINVAL;
1681 /* Must match [\w.-]+ */
1682 for (i = 0; i < strlen(name); i++) {
1683 char c = name[i];
1684 if (isalnum(c))
1685 continue;
1686 if ((c == '.') || (c == '-') || (c == '_'))
1687 continue;
1688 return -EINVAL;
1689 }
1690 /* Specifying two names is forbidden */
1691 if (opts->name)
1692 return -EINVAL;
1693 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001694 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001695 GFP_KERNEL);
1696 if (!opts->name)
1697 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001698
1699 continue;
1700 }
1701
Tejun Heo30159ec2013-06-25 11:53:37 -07001702 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001703 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001704 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001705 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001706 continue;
1707
1708 /* Mutually exclusive option 'all' + subsystem name */
1709 if (all_ss)
1710 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001711 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001712 one_ss = true;
1713
1714 break;
1715 }
1716 if (i == CGROUP_SUBSYS_COUNT)
1717 return -ENOENT;
1718 }
1719
Tejun Heo873fe092013-04-14 20:15:26 -07001720 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001721 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 -04001722 if (nr_opts != 1) {
1723 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001724 return -EINVAL;
1725 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001726 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001727 }
1728
Li Zefanf9ab5b52009-06-17 16:26:33 -07001729 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001730 * If the 'all' option was specified select all the subsystems,
1731 * otherwise if 'none', 'name=' and a subsystem name options were
1732 * not specified, let's default to 'all'
1733 */
1734 if (all_ss || (!one_ss && !opts->none && !opts->name))
1735 for_each_subsys(ss, i)
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001736 if (cgroup_ssid_enabled(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001737 opts->subsys_mask |= (1 << i);
1738
1739 /*
1740 * We either have to specify by name or by subsystems. (So all
1741 * empty hierarchies must have a name).
1742 */
1743 if (!opts->subsys_mask && !opts->name)
1744 return -EINVAL;
1745
1746 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001747 * Option noprefix was introduced just for backward compatibility
1748 * with the old cpuset, so we allow noprefix only if mounting just
1749 * the cpuset subsystem.
1750 */
Tejun Heo93438622013-04-14 20:15:25 -07001751 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001752 return -EINVAL;
1753
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001754 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001755 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001756 return -EINVAL;
1757
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 return 0;
1759}
1760
Tejun Heo2bd59d42014-02-11 11:52:49 -05001761static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762{
1763 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001764 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001766 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767
Tejun Heoaa6ec292014-07-09 10:08:08 -04001768 if (root == &cgrp_dfl_root) {
1769 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001770 return -EINVAL;
1771 }
1772
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 mutex_lock(&cgroup_mutex);
1774
1775 /* See what subsystems are wanted */
1776 ret = parse_cgroupfs_options(data, &opts);
1777 if (ret)
1778 goto out_unlock;
1779
Tejun Heof392e512014-04-23 11:13:14 -04001780 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001781 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001782 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001783
Tejun Heof392e512014-04-23 11:13:14 -04001784 added_mask = opts.subsys_mask & ~root->subsys_mask;
1785 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001786
Ben Blumcf5d5942010-03-10 15:22:09 -08001787 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001788 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001789 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001790 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001791 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001793 goto out_unlock;
1794 }
1795
Tejun Heof172e672013-06-28 17:07:30 -07001796 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001797 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001798 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001799 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001800 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801
Tejun Heo5df36032014-03-19 10:23:54 -04001802 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001803 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001806 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001807
Tejun Heo69e943b2014-02-08 10:36:58 -05001808 if (opts.release_agent) {
1809 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001810 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001811 spin_unlock(&release_agent_path_lock);
1812 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001814 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001815 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 return ret;
1818}
1819
Tejun Heoafeb0f92014-02-13 06:58:39 -05001820/*
1821 * To reduce the fork() overhead for systems that are not actually using
1822 * their cgroups capability, we don't maintain the lists running through
1823 * each css_set to its tasks until we see the list actually used - in other
1824 * words after the first mount.
1825 */
1826static bool use_task_css_set_links __read_mostly;
1827
1828static void cgroup_enable_task_cg_lists(void)
1829{
1830 struct task_struct *p, *g;
1831
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001832 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001833
1834 if (use_task_css_set_links)
1835 goto out_unlock;
1836
1837 use_task_css_set_links = true;
1838
1839 /*
1840 * We need tasklist_lock because RCU is not safe against
1841 * while_each_thread(). Besides, a forking task that has passed
1842 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1843 * is not guaranteed to have its child immediately visible in the
1844 * tasklist if we walk through it with RCU.
1845 */
1846 read_lock(&tasklist_lock);
1847 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001848 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1849 task_css_set(p) != &init_css_set);
1850
1851 /*
1852 * We should check if the process is exiting, otherwise
1853 * it will race with cgroup_exit() in that the list
1854 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001855 * Do it while holding siglock so that we don't end up
1856 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001857 */
Tejun Heof153ad12014-02-25 09:56:49 -05001858 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001859 if (!(p->flags & PF_EXITING)) {
1860 struct css_set *cset = task_css_set(p);
1861
Tejun Heo0de09422015-10-15 16:41:49 -04001862 if (!css_set_populated(cset))
1863 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001864 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001865 get_css_set(cset);
1866 }
Tejun Heof153ad12014-02-25 09:56:49 -05001867 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001868 } while_each_thread(g, p);
1869 read_unlock(&tasklist_lock);
1870out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001871 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001872}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001873
Paul Menagecc31edc2008-10-18 20:28:04 -07001874static void init_cgroup_housekeeping(struct cgroup *cgrp)
1875{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001876 struct cgroup_subsys *ss;
1877 int ssid;
1878
Tejun Heod5c419b2014-05-16 13:22:48 -04001879 INIT_LIST_HEAD(&cgrp->self.sibling);
1880 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001881 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001882 INIT_LIST_HEAD(&cgrp->pidlists);
1883 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001884 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001885 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001886
1887 for_each_subsys(ss, ssid)
1888 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001889
1890 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001891 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001892}
Paul Menagec6d57f32009-09-23 15:56:19 -07001893
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001894static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001895 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001896{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001897 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001898
Paul Menageddbcc7e2007-10-18 23:39:30 -07001899 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001900 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001901 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001902 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001903 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001904
Paul Menagec6d57f32009-09-23 15:56:19 -07001905 root->flags = opts->flags;
1906 if (opts->release_agent)
1907 strcpy(root->release_agent_path, opts->release_agent);
1908 if (opts->name)
1909 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001910 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001911 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001912}
1913
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001914static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001915{
Tejun Heod427dfe2014-02-11 11:52:48 -05001916 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001917 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001918 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001919 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001920
Tejun Heod427dfe2014-02-11 11:52:48 -05001921 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001922
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001923 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001924 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001925 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001926 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001927
Tejun Heo2aad2a82014-09-24 13:31:50 -04001928 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1929 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001930 if (ret)
1931 goto out;
1932
Tejun Heod427dfe2014-02-11 11:52:48 -05001933 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001934 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001935 * but that's OK - it can only be increased by someone holding
1936 * cgroup_lock, and that's us. The worst that can happen is that we
1937 * have some link structures left over
1938 */
1939 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001940 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001941 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001942
Tejun Heo985ed672014-03-19 10:23:53 -04001943 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001944 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001945 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001946
Tejun Heo2bd59d42014-02-11 11:52:49 -05001947 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1948 KERNFS_ROOT_CREATE_DEACTIVATED,
1949 root_cgrp);
1950 if (IS_ERR(root->kf_root)) {
1951 ret = PTR_ERR(root->kf_root);
1952 goto exit_root_id;
1953 }
1954 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001955
Tejun Heo4df8dc92015-09-18 17:54:23 -04001956 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001957 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001958 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001959
Tejun Heo5df36032014-03-19 10:23:54 -04001960 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001961 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001962 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001963
Tejun Heod427dfe2014-02-11 11:52:48 -05001964 /*
1965 * There must be no failure case after here, since rebinding takes
1966 * care of subsystems' refcounts, which are explicitly dropped in
1967 * the failure exit path.
1968 */
1969 list_add(&root->root_list, &cgroup_roots);
1970 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001971
Tejun Heod427dfe2014-02-11 11:52:48 -05001972 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001973 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001974 * objects.
1975 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001976 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001977 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001978 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001979 if (css_set_populated(cset))
1980 cgroup_update_populated(root_cgrp, true);
1981 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001982 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001983
Tejun Heod5c419b2014-05-16 13:22:48 -04001984 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001985 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001986
Tejun Heo2bd59d42014-02-11 11:52:49 -05001987 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001988 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001989 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001990
Tejun Heo2bd59d42014-02-11 11:52:49 -05001991destroy_root:
1992 kernfs_destroy_root(root->kf_root);
1993 root->kf_root = NULL;
1994exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001995 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001996cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001997 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001998out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001999 free_cgrp_cset_links(&tmp_links);
2000 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002001}
2002
Al Virof7e83572010-07-26 13:23:11 +04002003static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002004 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04002005 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002006{
Li Zefan3a32bd72014-06-30 11:50:59 +08002007 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08002008 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002009 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002010 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002011 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002012 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08002013 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002014 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07002015
2016 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05002017 * The first time anyone tries to mount a cgroup, enable the list
2018 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07002019 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05002020 if (!use_task_css_set_links)
2021 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002022
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002023 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002024
Paul Menageddbcc7e2007-10-18 23:39:30 -07002025 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002026 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002027 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002028 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002029
Tejun Heo2bd59d42014-02-11 11:52:49 -05002030 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002031 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04002032 cgrp_dfl_root_visible = true;
2033 root = &cgrp_dfl_root;
2034 cgroup_get(&root->cgrp);
2035 ret = 0;
2036 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002037 }
2038
Li Zefan970317a2014-06-30 11:49:58 +08002039 /*
2040 * Destruction of cgroup root is asynchronous, so subsystems may
2041 * still be dying after the previous unmount. Let's drain the
2042 * dying subsystems. We just need to ensure that the ones
2043 * unmounted previously finish dying and don't care about new ones
2044 * starting. Testing ref liveliness is good enough.
2045 */
2046 for_each_subsys(ss, i) {
2047 if (!(opts.subsys_mask & (1 << i)) ||
2048 ss->root == &cgrp_dfl_root)
2049 continue;
2050
2051 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2052 mutex_unlock(&cgroup_mutex);
2053 msleep(10);
2054 ret = restart_syscall();
2055 goto out_free;
2056 }
2057 cgroup_put(&ss->root->cgrp);
2058 }
2059
Tejun Heo985ed672014-03-19 10:23:53 -04002060 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002061 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002062
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002063 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002064 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002065
Paul Menage817929e2007-10-18 23:39:36 -07002066 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002067 * If we asked for a name then it must match. Also, if
2068 * name matches but sybsys_mask doesn't, we should fail.
2069 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002070 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002071 if (opts.name) {
2072 if (strcmp(opts.name, root->name))
2073 continue;
2074 name_match = true;
2075 }
Tejun Heo31261212013-06-28 17:07:30 -07002076
2077 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002078 * If we asked for subsystems (or explicitly for no
2079 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002080 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002081 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002082 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002083 if (!name_match)
2084 continue;
2085 ret = -EBUSY;
2086 goto out_unlock;
2087 }
Tejun Heo873fe092013-04-14 20:15:26 -07002088
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002089 if (root->flags ^ opts.flags)
2090 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002091
Tejun Heo776f02f2014-02-12 09:29:50 -05002092 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002093 * We want to reuse @root whose lifetime is governed by its
2094 * ->cgrp. Let's check whether @root is alive and keep it
2095 * that way. As cgroup_kill_sb() can happen anytime, we
2096 * want to block it by pinning the sb so that @root doesn't
2097 * get killed before mount is complete.
2098 *
2099 * With the sb pinned, tryget_live can reliably indicate
2100 * whether @root can be reused. If it's being killed,
2101 * drain it. We can use wait_queue for the wait but this
2102 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002103 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002104 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2105 if (IS_ERR(pinned_sb) ||
2106 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002107 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002108 if (!IS_ERR_OR_NULL(pinned_sb))
2109 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002110 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002111 ret = restart_syscall();
2112 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002113 }
2114
2115 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002116 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002117 }
2118
Tejun Heo172a2c062014-03-19 10:23:53 -04002119 /*
2120 * No such thing, create a new one. name= matching without subsys
2121 * specification is allowed for already existing hierarchies but we
2122 * can't create new one without subsys specification.
2123 */
2124 if (!opts.subsys_mask && !opts.none) {
2125 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002126 goto out_unlock;
2127 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002128
Tejun Heo172a2c062014-03-19 10:23:53 -04002129 root = kzalloc(sizeof(*root), GFP_KERNEL);
2130 if (!root) {
2131 ret = -ENOMEM;
2132 goto out_unlock;
2133 }
2134
2135 init_cgroup_root(root, &opts);
2136
Tejun Heo35585572014-02-13 06:58:38 -05002137 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002138 if (ret)
2139 cgroup_free_root(root);
2140
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002141out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002142 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002143out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002144 kfree(opts.release_agent);
2145 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002146
Tejun Heo2bd59d42014-02-11 11:52:49 -05002147 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002148 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002149
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002150 dentry = kernfs_mount(fs_type, flags, root->kf_root,
2151 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002152 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002153 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002154
2155 /*
2156 * If @pinned_sb, we're reusing an existing root and holding an
2157 * extra ref on its sb. Mount is complete. Put the extra ref.
2158 */
2159 if (pinned_sb) {
2160 WARN_ON(new_sb);
2161 deactivate_super(pinned_sb);
2162 }
2163
Tejun Heo2bd59d42014-02-11 11:52:49 -05002164 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002165}
2166
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002167static void cgroup_kill_sb(struct super_block *sb)
2168{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002169 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002170 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002171
Tejun Heo9d755d32014-05-14 09:15:02 -04002172 /*
2173 * If @root doesn't have any mounts or children, start killing it.
2174 * This prevents new mounts by disabling percpu_ref_tryget_live().
2175 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002176 *
2177 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002178 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002179 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002180 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002181 cgroup_put(&root->cgrp);
2182 else
2183 percpu_ref_kill(&root->cgrp.self.refcnt);
2184
Tejun Heo2bd59d42014-02-11 11:52:49 -05002185 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002186}
2187
2188static struct file_system_type cgroup_fs_type = {
2189 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002190 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002191 .kill_sb = cgroup_kill_sb,
2192};
2193
Li Zefana043e3b2008-02-23 15:24:09 -08002194/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002195 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002196 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002197 * @buf: the buffer to write the path into
2198 * @buflen: the length of the buffer
2199 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002200 * Determine @task's cgroup on the first (the one with the lowest non-zero
2201 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2202 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2203 * cgroup controller callbacks.
2204 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002205 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002206 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002207char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002208{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002209 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002210 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002211 int hierarchy_id = 1;
2212 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002213
2214 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002215 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002216
Tejun Heo913ffdb2013-07-11 16:34:48 -07002217 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2218
Tejun Heo857a2be2013-04-14 20:50:08 -07002219 if (root) {
2220 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002221 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002222 } else {
2223 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002224 if (strlcpy(buf, "/", buflen) < buflen)
2225 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002226 }
2227
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002228 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002229 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002230 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002231}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002232EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002233
Tejun Heob3dc0942014-02-25 10:04:01 -05002234/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002235struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002236 /* the src and dst cset list running through cset->mg_node */
2237 struct list_head src_csets;
2238 struct list_head dst_csets;
2239
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002240 /* the subsys currently being processed */
2241 int ssid;
2242
Tejun Heob3dc0942014-02-25 10:04:01 -05002243 /*
2244 * Fields for cgroup_taskset_*() iteration.
2245 *
2246 * Before migration is committed, the target migration tasks are on
2247 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2248 * the csets on ->dst_csets. ->csets point to either ->src_csets
2249 * or ->dst_csets depending on whether migration is committed.
2250 *
2251 * ->cur_csets and ->cur_task point to the current task position
2252 * during iteration.
2253 */
2254 struct list_head *csets;
2255 struct css_set *cur_cset;
2256 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002257};
2258
Tejun Heoadaae5d2015-09-11 15:00:21 -04002259#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2260 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2261 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2262 .csets = &tset.src_csets, \
2263}
2264
2265/**
2266 * cgroup_taskset_add - try to add a migration target task to a taskset
2267 * @task: target task
2268 * @tset: target taskset
2269 *
2270 * Add @task, which is a migration target, to @tset. This function becomes
2271 * noop if @task doesn't need to be migrated. @task's css_set should have
2272 * been added as a migration source and @task->cg_list will be moved from
2273 * the css_set's tasks list to mg_tasks one.
2274 */
2275static void cgroup_taskset_add(struct task_struct *task,
2276 struct cgroup_taskset *tset)
2277{
2278 struct css_set *cset;
2279
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002280 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002281
2282 /* @task either already exited or can't exit until the end */
2283 if (task->flags & PF_EXITING)
2284 return;
2285
2286 /* leave @task alone if post_fork() hasn't linked it yet */
2287 if (list_empty(&task->cg_list))
2288 return;
2289
2290 cset = task_css_set(task);
2291 if (!cset->mg_src_cgrp)
2292 return;
2293
2294 list_move_tail(&task->cg_list, &cset->mg_tasks);
2295 if (list_empty(&cset->mg_node))
2296 list_add_tail(&cset->mg_node, &tset->src_csets);
2297 if (list_empty(&cset->mg_dst_cset->mg_node))
2298 list_move_tail(&cset->mg_dst_cset->mg_node,
2299 &tset->dst_csets);
2300}
2301
Tejun Heo2f7ee562011-12-12 18:12:21 -08002302/**
2303 * cgroup_taskset_first - reset taskset and return the first task
2304 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002305 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002306 *
2307 * @tset iteration is initialized and the first task is returned.
2308 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002309struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2310 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002311{
Tejun Heob3dc0942014-02-25 10:04:01 -05002312 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2313 tset->cur_task = NULL;
2314
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002315 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002316}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002317
2318/**
2319 * cgroup_taskset_next - iterate to the next task in taskset
2320 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002321 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002322 *
2323 * Return the next task in @tset. Iteration must have been initialized
2324 * with cgroup_taskset_first().
2325 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002326struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2327 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002328{
Tejun Heob3dc0942014-02-25 10:04:01 -05002329 struct css_set *cset = tset->cur_cset;
2330 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002331
Tejun Heob3dc0942014-02-25 10:04:01 -05002332 while (&cset->mg_node != tset->csets) {
2333 if (!task)
2334 task = list_first_entry(&cset->mg_tasks,
2335 struct task_struct, cg_list);
2336 else
2337 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002338
Tejun Heob3dc0942014-02-25 10:04:01 -05002339 if (&task->cg_list != &cset->mg_tasks) {
2340 tset->cur_cset = cset;
2341 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002342
2343 /*
2344 * This function may be called both before and
2345 * after cgroup_taskset_migrate(). The two cases
2346 * can be distinguished by looking at whether @cset
2347 * has its ->mg_dst_cset set.
2348 */
2349 if (cset->mg_dst_cset)
2350 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2351 else
2352 *dst_cssp = cset->subsys[tset->ssid];
2353
Tejun Heob3dc0942014-02-25 10:04:01 -05002354 return task;
2355 }
2356
2357 cset = list_next_entry(cset, mg_node);
2358 task = NULL;
2359 }
2360
2361 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002362}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002363
2364/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002365 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2366 * @tset: taget taskset
2367 * @dst_cgrp: destination cgroup
2368 *
2369 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2370 * ->can_attach callbacks fails and guarantees that either all or none of
2371 * the tasks in @tset are migrated. @tset is consumed regardless of
2372 * success.
2373 */
2374static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2375 struct cgroup *dst_cgrp)
2376{
2377 struct cgroup_subsys_state *css, *failed_css = NULL;
2378 struct task_struct *task, *tmp_task;
2379 struct css_set *cset, *tmp_cset;
2380 int i, ret;
2381
2382 /* methods shouldn't be called if no task is actually migrating */
2383 if (list_empty(&tset->src_csets))
2384 return 0;
2385
2386 /* check that we can legitimately attach to the cgroup */
2387 for_each_e_css(css, i, dst_cgrp) {
2388 if (css->ss->can_attach) {
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002389 tset->ssid = i;
2390 ret = css->ss->can_attach(tset);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002391 if (ret) {
2392 failed_css = css;
2393 goto out_cancel_attach;
2394 }
2395 }
2396 }
2397
2398 /*
2399 * Now that we're guaranteed success, proceed to move all tasks to
2400 * the new cgroup. There are no failure cases after here, so this
2401 * is the commit point.
2402 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002403 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002404 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002405 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2406 struct css_set *from_cset = task_css_set(task);
2407 struct css_set *to_cset = cset->mg_dst_cset;
2408
2409 get_css_set(to_cset);
2410 css_set_move_task(task, from_cset, to_cset, true);
2411 put_css_set_locked(from_cset);
2412 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002413 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002414 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002415
2416 /*
2417 * Migration is committed, all target tasks are now on dst_csets.
2418 * Nothing is sensitive to fork() after this point. Notify
2419 * controllers that migration is complete.
2420 */
2421 tset->csets = &tset->dst_csets;
2422
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002423 for_each_e_css(css, i, dst_cgrp) {
2424 if (css->ss->attach) {
2425 tset->ssid = i;
2426 css->ss->attach(tset);
2427 }
2428 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002429
2430 ret = 0;
2431 goto out_release_tset;
2432
2433out_cancel_attach:
2434 for_each_e_css(css, i, dst_cgrp) {
2435 if (css == failed_css)
2436 break;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002437 if (css->ss->cancel_attach) {
2438 tset->ssid = i;
2439 css->ss->cancel_attach(tset);
2440 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002441 }
2442out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002443 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002444 list_splice_init(&tset->dst_csets, &tset->src_csets);
2445 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2446 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2447 list_del_init(&cset->mg_node);
2448 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002449 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002450 return ret;
2451}
2452
2453/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002454 * cgroup_migrate_finish - cleanup after attach
2455 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002456 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002457 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2458 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002459 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002460static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002461{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002462 struct css_set *cset, *tmp_cset;
2463
2464 lockdep_assert_held(&cgroup_mutex);
2465
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002466 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002467 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2468 cset->mg_src_cgrp = NULL;
2469 cset->mg_dst_cset = NULL;
2470 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002471 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002472 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002473 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002474}
2475
2476/**
2477 * cgroup_migrate_add_src - add a migration source css_set
2478 * @src_cset: the source css_set to add
2479 * @dst_cgrp: the destination cgroup
2480 * @preloaded_csets: list of preloaded css_sets
2481 *
2482 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2483 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2484 * up by cgroup_migrate_finish().
2485 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002486 * This function may be called without holding cgroup_threadgroup_rwsem
2487 * even if the target is a process. Threads may be created and destroyed
2488 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2489 * into play and the preloaded css_sets are guaranteed to cover all
2490 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002491 */
2492static void cgroup_migrate_add_src(struct css_set *src_cset,
2493 struct cgroup *dst_cgrp,
2494 struct list_head *preloaded_csets)
2495{
2496 struct cgroup *src_cgrp;
2497
2498 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002499 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002500
2501 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2502
Tejun Heo1958d2d2014-02-25 10:04:03 -05002503 if (!list_empty(&src_cset->mg_preload_node))
2504 return;
2505
2506 WARN_ON(src_cset->mg_src_cgrp);
2507 WARN_ON(!list_empty(&src_cset->mg_tasks));
2508 WARN_ON(!list_empty(&src_cset->mg_node));
2509
2510 src_cset->mg_src_cgrp = src_cgrp;
2511 get_css_set(src_cset);
2512 list_add(&src_cset->mg_preload_node, preloaded_csets);
2513}
2514
2515/**
2516 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002517 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002518 * @preloaded_csets: list of preloaded source css_sets
2519 *
2520 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2521 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002522 * pins all destination css_sets, links each to its source, and append them
2523 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2524 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002525 *
2526 * This function must be called after cgroup_migrate_add_src() has been
2527 * called on each migration source css_set. After migration is performed
2528 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2529 * @preloaded_csets.
2530 */
2531static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2532 struct list_head *preloaded_csets)
2533{
2534 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002535 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002536
2537 lockdep_assert_held(&cgroup_mutex);
2538
Tejun Heof8f22e52014-04-23 11:13:16 -04002539 /*
2540 * Except for the root, child_subsys_mask must be zero for a cgroup
2541 * with tasks so that child cgroups don't compete against tasks.
2542 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002543 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002544 dst_cgrp->child_subsys_mask)
2545 return -EBUSY;
2546
Tejun Heo1958d2d2014-02-25 10:04:03 -05002547 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002548 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002549 struct css_set *dst_cset;
2550
Tejun Heof817de92014-04-23 11:13:16 -04002551 dst_cset = find_css_set(src_cset,
2552 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002553 if (!dst_cset)
2554 goto err;
2555
2556 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002557
2558 /*
2559 * If src cset equals dst, it's noop. Drop the src.
2560 * cgroup_migrate() will skip the cset too. Note that we
2561 * can't handle src == dst as some nodes are used by both.
2562 */
2563 if (src_cset == dst_cset) {
2564 src_cset->mg_src_cgrp = NULL;
2565 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002566 put_css_set(src_cset);
2567 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002568 continue;
2569 }
2570
Tejun Heo1958d2d2014-02-25 10:04:03 -05002571 src_cset->mg_dst_cset = dst_cset;
2572
2573 if (list_empty(&dst_cset->mg_preload_node))
2574 list_add(&dst_cset->mg_preload_node, &csets);
2575 else
Zefan Lia25eb522014-09-19 16:51:00 +08002576 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002577 }
2578
Tejun Heof817de92014-04-23 11:13:16 -04002579 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002580 return 0;
2581err:
2582 cgroup_migrate_finish(&csets);
2583 return -ENOMEM;
2584}
2585
2586/**
2587 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002588 * @leader: the leader of the process or the task to migrate
2589 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002590 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002591 *
2592 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002593 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002594 * caller is also responsible for invoking cgroup_migrate_add_src() and
2595 * cgroup_migrate_prepare_dst() on the targets before invoking this
2596 * function and following up with cgroup_migrate_finish().
2597 *
2598 * As long as a controller's ->can_attach() doesn't fail, this function is
2599 * guaranteed to succeed. This means that, excluding ->can_attach()
2600 * failure, when migrating multiple targets, the success or failure can be
2601 * decided for all targets by invoking group_migrate_prepare_dst() before
2602 * actually starting migrating.
2603 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002604static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2605 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002606{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002607 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2608 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002609
2610 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002611 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2612 * already PF_EXITING could be freed from underneath us unless we
2613 * take an rcu_read_lock.
2614 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002615 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002616 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002617 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002618 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002619 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002620 if (!threadgroup)
2621 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002622 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002623 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002624 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002625
Tejun Heoadaae5d2015-09-11 15:00:21 -04002626 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002627}
2628
Tejun Heo1958d2d2014-02-25 10:04:03 -05002629/**
2630 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2631 * @dst_cgrp: the cgroup to attach to
2632 * @leader: the task or the leader of the threadgroup to be attached
2633 * @threadgroup: attach the whole threadgroup?
2634 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002635 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002636 */
2637static int cgroup_attach_task(struct cgroup *dst_cgrp,
2638 struct task_struct *leader, bool threadgroup)
2639{
2640 LIST_HEAD(preloaded_csets);
2641 struct task_struct *task;
2642 int ret;
2643
2644 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002645 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002646 rcu_read_lock();
2647 task = leader;
2648 do {
2649 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2650 &preloaded_csets);
2651 if (!threadgroup)
2652 break;
2653 } while_each_thread(leader, task);
2654 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002655 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002656
2657 /* prepare dst csets and commit */
2658 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2659 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002660 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002661
2662 cgroup_migrate_finish(&preloaded_csets);
2663 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002664}
2665
Tejun Heo187fe842015-06-18 16:54:28 -04002666static int cgroup_procs_write_permission(struct task_struct *task,
2667 struct cgroup *dst_cgrp,
2668 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002669{
2670 const struct cred *cred = current_cred();
2671 const struct cred *tcred = get_task_cred(task);
2672 int ret = 0;
2673
2674 /*
2675 * even if we're attaching all tasks in the thread group, we only
2676 * need to check permissions on one of them.
2677 */
2678 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2679 !uid_eq(cred->euid, tcred->uid) &&
2680 !uid_eq(cred->euid, tcred->suid))
2681 ret = -EACCES;
2682
Tejun Heo187fe842015-06-18 16:54:28 -04002683 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2684 struct super_block *sb = of->file->f_path.dentry->d_sb;
2685 struct cgroup *cgrp;
2686 struct inode *inode;
2687
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002688 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002689 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002690 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002691
2692 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2693 cgrp = cgroup_parent(cgrp);
2694
2695 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002696 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002697 if (inode) {
2698 ret = inode_permission(inode, MAY_WRITE);
2699 iput(inode);
2700 }
2701 }
2702
Tejun Heodedf22e2015-06-18 16:54:28 -04002703 put_cred(tcred);
2704 return ret;
2705}
2706
Ben Blum74a11662011-05-26 16:25:20 -07002707/*
2708 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002709 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002710 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002711 */
Tejun Heoacbef752014-05-13 12:16:22 -04002712static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2713 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002714{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002715 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002716 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002717 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002718 int ret;
2719
Tejun Heoacbef752014-05-13 12:16:22 -04002720 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2721 return -EINVAL;
2722
Tejun Heoe76ecae2014-05-13 12:19:23 -04002723 cgrp = cgroup_kn_lock_live(of->kn);
2724 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002725 return -ENODEV;
2726
Tejun Heo3014dde2015-09-16 13:03:02 -04002727 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002728 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002729 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002730 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002731 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002732 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002733 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002734 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002735 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002736 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002737 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002738
2739 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002740 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002741
2742 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002743 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002744 * trapped in a cpuset, or RT worker may be born in a cgroup
2745 * with no rt_runtime allocated. Just say no.
2746 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002747 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002748 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002749 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002750 }
2751
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002752 get_task_struct(tsk);
2753 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002754
Tejun Heo187fe842015-06-18 16:54:28 -04002755 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002756 if (!ret)
2757 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002758
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002759 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002760 goto out_unlock_threadgroup;
2761
2762out_unlock_rcu:
2763 rcu_read_unlock();
2764out_unlock_threadgroup:
2765 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002766 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002767 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002768}
2769
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002770/**
2771 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2772 * @from: attach to all cgroups of a given task
2773 * @tsk: the task to be attached
2774 */
2775int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2776{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002777 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002778 int retval = 0;
2779
Tejun Heo47cfcd02013-04-07 09:29:51 -07002780 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002781 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002782 struct cgroup *from_cgrp;
2783
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002784 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002785 continue;
2786
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002787 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002788 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002789 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002790
Li Zefan6f4b7e62013-07-31 16:18:36 +08002791 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002792 if (retval)
2793 break;
2794 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002795 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002796
2797 return retval;
2798}
2799EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2800
Tejun Heoacbef752014-05-13 12:16:22 -04002801static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2802 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002803{
Tejun Heoacbef752014-05-13 12:16:22 -04002804 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002805}
2806
Tejun Heoacbef752014-05-13 12:16:22 -04002807static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2808 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002809{
Tejun Heoacbef752014-05-13 12:16:22 -04002810 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002811}
2812
Tejun Heo451af502014-05-13 12:16:21 -04002813static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2814 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e0662008-07-25 01:46:59 -07002815{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002816 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002817
Tejun Heoe76ecae2014-05-13 12:19:23 -04002818 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2819
2820 cgrp = cgroup_kn_lock_live(of->kn);
2821 if (!cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07002822 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002823 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002824 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2825 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002826 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002827 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002828 return nbytes;
Paul Menagee788e0662008-07-25 01:46:59 -07002829}
2830
Tejun Heo2da8ca82013-12-05 12:28:04 -05002831static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002832{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002833 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002834
Tejun Heo46cfeb02014-05-13 12:11:00 -04002835 spin_lock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002836 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002837 spin_unlock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002838 seq_putc(seq, '\n');
Paul Menagee788e0662008-07-25 01:46:59 -07002839 return 0;
2840}
2841
Tejun Heo2da8ca82013-12-05 12:28:04 -05002842static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002843{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002844 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002845 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002846}
2847
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002848static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002849{
2850 struct cgroup_subsys *ss;
2851 bool printed = false;
2852 int ssid;
2853
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002854 for_each_subsys_which(ss, ssid, &ss_mask) {
2855 if (printed)
2856 seq_putc(seq, ' ');
2857 seq_printf(seq, "%s", ss->name);
2858 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002859 }
2860 if (printed)
2861 seq_putc(seq, '\n');
2862}
2863
2864/* show controllers which are currently attached to the default hierarchy */
2865static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2866{
2867 struct cgroup *cgrp = seq_css(seq)->cgroup;
2868
Tejun Heo5533e012014-05-14 19:33:07 -04002869 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2870 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002871 return 0;
2872}
2873
2874/* show controllers which are enabled from the parent */
2875static int cgroup_controllers_show(struct seq_file *seq, void *v)
2876{
2877 struct cgroup *cgrp = seq_css(seq)->cgroup;
2878
Tejun Heo667c2492014-07-08 18:02:56 -04002879 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002880 return 0;
2881}
2882
2883/* show controllers which are enabled for a given cgroup's children */
2884static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2885{
2886 struct cgroup *cgrp = seq_css(seq)->cgroup;
2887
Tejun Heo667c2492014-07-08 18:02:56 -04002888 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002889 return 0;
2890}
2891
2892/**
2893 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2894 * @cgrp: root of the subtree to update csses for
2895 *
2896 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2897 * css associations need to be updated accordingly. This function looks up
2898 * all css_sets which are attached to the subtree, creates the matching
2899 * updated css_sets and migrates the tasks to the new ones.
2900 */
2901static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2902{
2903 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002904 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002905 struct cgroup_subsys_state *css;
2906 struct css_set *src_cset;
2907 int ret;
2908
Tejun Heof8f22e52014-04-23 11:13:16 -04002909 lockdep_assert_held(&cgroup_mutex);
2910
Tejun Heo3014dde2015-09-16 13:03:02 -04002911 percpu_down_write(&cgroup_threadgroup_rwsem);
2912
Tejun Heof8f22e52014-04-23 11:13:16 -04002913 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002914 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002915 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2916 struct cgrp_cset_link *link;
2917
2918 /* self is not affected by child_subsys_mask change */
2919 if (css->cgroup == cgrp)
2920 continue;
2921
2922 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2923 cgroup_migrate_add_src(link->cset, cgrp,
2924 &preloaded_csets);
2925 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002926 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002927
2928 /* NULL dst indicates self on default hierarchy */
2929 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2930 if (ret)
2931 goto out_finish;
2932
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002933 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002934 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002935 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002936
2937 /* src_csets precede dst_csets, break on the first dst_cset */
2938 if (!src_cset->mg_src_cgrp)
2939 break;
2940
Tejun Heo10265072015-09-11 15:00:22 -04002941 /* all tasks in src_csets need to be migrated */
2942 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2943 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002944 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002945 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002946
Tejun Heo10265072015-09-11 15:00:22 -04002947 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002948out_finish:
2949 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002950 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002951 return ret;
2952}
2953
2954/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002955static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2956 char *buf, size_t nbytes,
2957 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002958{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002959 unsigned long enable = 0, disable = 0;
2960 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002961 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002962 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002963 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002964 int ssid, ret;
2965
2966 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002967 * Parse input - space separated list of subsystem names prefixed
2968 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002969 */
Tejun Heo451af502014-05-13 12:16:21 -04002970 buf = strstrip(buf);
2971 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002972 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2973
Tejun Heod37167a2014-05-13 12:10:59 -04002974 if (tok[0] == '\0')
2975 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002976 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04002977 if (!cgroup_ssid_enabled(ssid) ||
2978 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002979 continue;
2980
2981 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002982 enable |= 1 << ssid;
2983 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002984 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002985 disable |= 1 << ssid;
2986 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002987 } else {
2988 return -EINVAL;
2989 }
2990 break;
2991 }
2992 if (ssid == CGROUP_SUBSYS_COUNT)
2993 return -EINVAL;
2994 }
2995
Tejun Heoa9746d82014-05-13 12:19:22 -04002996 cgrp = cgroup_kn_lock_live(of->kn);
2997 if (!cgrp)
2998 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002999
3000 for_each_subsys(ss, ssid) {
3001 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003002 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003003 enable &= ~(1 << ssid);
3004 continue;
3005 }
3006
Tejun Heoc29adf22014-07-08 18:02:56 -04003007 /* unavailable or not enabled on the parent? */
3008 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
3009 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04003010 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003011 ret = -ENOENT;
3012 goto out_unlock;
3013 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003014 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003015 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003016 disable &= ~(1 << ssid);
3017 continue;
3018 }
3019
3020 /* a child has it enabled? */
3021 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003022 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003023 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003024 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003025 }
3026 }
3027 }
3028 }
3029
3030 if (!enable && !disable) {
3031 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003032 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003033 }
3034
3035 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003036 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003037 * with tasks so that child cgroups don't compete against tasks.
3038 */
Tejun Heod51f39b2014-05-16 13:22:48 -04003039 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003040 ret = -EBUSY;
3041 goto out_unlock;
3042 }
3043
3044 /*
Tejun Heof63070d2014-07-08 18:02:57 -04003045 * Update subsys masks and calculate what needs to be done. More
3046 * subsystems than specified may need to be enabled or disabled
3047 * depending on subsystem dependencies.
3048 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05003049 old_sc = cgrp->subtree_control;
3050 old_ss = cgrp->child_subsys_mask;
3051 new_sc = (old_sc | enable) & ~disable;
3052 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04003053
Tejun Heo755bf5e2014-11-18 02:49:50 -05003054 css_enable = ~old_ss & new_ss;
3055 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04003056 enable |= css_enable;
3057 disable |= css_disable;
3058
Tejun Heodb6e3052014-11-18 02:49:51 -05003059 /*
3060 * Because css offlining is asynchronous, userland might try to
3061 * re-enable the same controller while the previous instance is
3062 * still around. In such cases, wait till it's gone using
3063 * offline_waitq.
3064 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10003065 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05003066 cgroup_for_each_live_child(child, cgrp) {
3067 DEFINE_WAIT(wait);
3068
3069 if (!cgroup_css(child, ss))
3070 continue;
3071
3072 cgroup_get(child);
3073 prepare_to_wait(&child->offline_waitq, &wait,
3074 TASK_UNINTERRUPTIBLE);
3075 cgroup_kn_unlock(of->kn);
3076 schedule();
3077 finish_wait(&child->offline_waitq, &wait);
3078 cgroup_put(child);
3079
3080 return restart_syscall();
3081 }
3082 }
3083
Tejun Heo755bf5e2014-11-18 02:49:50 -05003084 cgrp->subtree_control = new_sc;
3085 cgrp->child_subsys_mask = new_ss;
3086
Tejun Heof63070d2014-07-08 18:02:57 -04003087 /*
3088 * Create new csses or make the existing ones visible. A css is
3089 * created invisible if it's being implicitly enabled through
3090 * dependency. An invisible css is made visible when the userland
3091 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04003092 */
3093 for_each_subsys(ss, ssid) {
3094 if (!(enable & (1 << ssid)))
3095 continue;
3096
3097 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04003098 if (css_enable & (1 << ssid))
3099 ret = create_css(child, ss,
3100 cgrp->subtree_control & (1 << ssid));
3101 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003102 ret = css_populate_dir(cgroup_css(child, ss),
3103 NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003104 if (ret)
3105 goto err_undo_css;
3106 }
3107 }
3108
Tejun Heoc29adf22014-07-08 18:02:56 -04003109 /*
3110 * At this point, cgroup_e_css() results reflect the new csses
3111 * making the following cgroup_update_dfl_csses() properly update
3112 * css associations of all tasks in the subtree.
3113 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003114 ret = cgroup_update_dfl_csses(cgrp);
3115 if (ret)
3116 goto err_undo_css;
3117
Tejun Heof63070d2014-07-08 18:02:57 -04003118 /*
3119 * All tasks are migrated out of disabled csses. Kill or hide
3120 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003121 * disabled while other subsystems are still depending on it. The
3122 * css must not actively control resources and be in the vanilla
3123 * state if it's made visible again later. Controllers which may
3124 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003125 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003126 for_each_subsys(ss, ssid) {
3127 if (!(disable & (1 << ssid)))
3128 continue;
3129
Tejun Heof63070d2014-07-08 18:02:57 -04003130 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003131 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3132
3133 if (css_disable & (1 << ssid)) {
3134 kill_css(css);
3135 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003136 css_clear_dir(css, NULL);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003137 if (ss->css_reset)
3138 ss->css_reset(css);
3139 }
Tejun Heof63070d2014-07-08 18:02:57 -04003140 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003141 }
3142
Tejun Heo56c807b2014-11-18 02:49:51 -05003143 /*
3144 * The effective csses of all the descendants (excluding @cgrp) may
3145 * have changed. Subsystems can optionally subscribe to this event
3146 * by implementing ->css_e_css_changed() which is invoked if any of
3147 * the effective csses seen from the css's cgroup may have changed.
3148 */
3149 for_each_subsys(ss, ssid) {
3150 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
3151 struct cgroup_subsys_state *css;
3152
3153 if (!ss->css_e_css_changed || !this_css)
3154 continue;
3155
3156 css_for_each_descendant_pre(css, this_css)
3157 if (css != this_css)
3158 ss->css_e_css_changed(css);
3159 }
3160
Tejun Heof8f22e52014-04-23 11:13:16 -04003161 kernfs_activate(cgrp->kn);
3162 ret = 0;
3163out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003164 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003165 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003166
3167err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003168 cgrp->subtree_control = old_sc;
3169 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003170
3171 for_each_subsys(ss, ssid) {
3172 if (!(enable & (1 << ssid)))
3173 continue;
3174
3175 cgroup_for_each_live_child(child, cgrp) {
3176 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003177
3178 if (!css)
3179 continue;
3180
3181 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003182 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003183 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003184 css_clear_dir(css, NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003185 }
3186 }
3187 goto out_unlock;
3188}
3189
Tejun Heo4a07c222015-09-18 17:54:22 -04003190static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003191{
Tejun Heo4a07c222015-09-18 17:54:22 -04003192 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003193 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003194 return 0;
3195}
3196
Tejun Heo2bd59d42014-02-11 11:52:49 -05003197static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3198 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003199{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003200 struct cgroup *cgrp = of->kn->parent->priv;
3201 struct cftype *cft = of->kn->priv;
3202 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003203 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003204
Tejun Heob4168642014-05-13 12:16:21 -04003205 if (cft->write)
3206 return cft->write(of, buf, nbytes, off);
3207
Tejun Heo2bd59d42014-02-11 11:52:49 -05003208 /*
3209 * kernfs guarantees that a file isn't deleted with operations in
3210 * flight, which means that the matching css is and stays alive and
3211 * doesn't need to be pinned. The RCU locking is not necessary
3212 * either. It's just for the convenience of using cgroup_css().
3213 */
3214 rcu_read_lock();
3215 css = cgroup_css(cgrp, cft->ss);
3216 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003217
Tejun Heo451af502014-05-13 12:16:21 -04003218 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003219 unsigned long long v;
3220 ret = kstrtoull(buf, 0, &v);
3221 if (!ret)
3222 ret = cft->write_u64(css, cft, v);
3223 } else if (cft->write_s64) {
3224 long long v;
3225 ret = kstrtoll(buf, 0, &v);
3226 if (!ret)
3227 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003228 } else {
3229 ret = -EINVAL;
3230 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003231
Tejun Heoa742c592013-12-05 12:28:03 -05003232 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003233}
3234
Tejun Heo6612f052013-12-05 12:28:04 -05003235static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003236{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003237 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003238}
3239
3240static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3241{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003242 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003243}
3244
3245static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3246{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003247 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003248}
3249
3250static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3251{
Tejun Heo7da11272013-12-05 12:28:04 -05003252 struct cftype *cft = seq_cft(m);
3253 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003254
Tejun Heo2da8ca82013-12-05 12:28:04 -05003255 if (cft->seq_show)
3256 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003257
Tejun Heo896f5192013-12-05 12:28:04 -05003258 if (cft->read_u64)
3259 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3260 else if (cft->read_s64)
3261 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3262 else
3263 return -EINVAL;
3264 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003265}
3266
Tejun Heo2bd59d42014-02-11 11:52:49 -05003267static struct kernfs_ops cgroup_kf_single_ops = {
3268 .atomic_write_len = PAGE_SIZE,
3269 .write = cgroup_file_write,
3270 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003271};
3272
Tejun Heo2bd59d42014-02-11 11:52:49 -05003273static struct kernfs_ops cgroup_kf_ops = {
3274 .atomic_write_len = PAGE_SIZE,
3275 .write = cgroup_file_write,
3276 .seq_start = cgroup_seqfile_start,
3277 .seq_next = cgroup_seqfile_next,
3278 .seq_stop = cgroup_seqfile_stop,
3279 .seq_show = cgroup_seqfile_show,
3280};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003281
3282/*
3283 * cgroup_rename - Only allow simple rename of directories in place.
3284 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003285static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3286 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003287{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003288 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003289 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003290
Tejun Heo2bd59d42014-02-11 11:52:49 -05003291 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003292 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003293 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003294 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003295
Tejun Heo6db8e852013-06-14 11:18:22 -07003296 /*
3297 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003298 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003299 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003300 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003301 return -EPERM;
3302
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003303 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003304 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003305 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003306 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003307 */
3308 kernfs_break_active_protection(new_parent);
3309 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003310
Tejun Heo2bd59d42014-02-11 11:52:49 -05003311 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003312
Tejun Heo2bd59d42014-02-11 11:52:49 -05003313 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003314
Tejun Heo2bd59d42014-02-11 11:52:49 -05003315 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003316
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003317 kernfs_unbreak_active_protection(kn);
3318 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003319 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003320}
3321
Tejun Heo49957f82014-04-07 16:44:47 -04003322/* set uid and gid of cgroup dirs and files to that of the creator */
3323static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3324{
3325 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3326 .ia_uid = current_fsuid(),
3327 .ia_gid = current_fsgid(), };
3328
3329 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3330 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3331 return 0;
3332
3333 return kernfs_setattr(kn, &iattr);
3334}
3335
Tejun Heo4df8dc92015-09-18 17:54:23 -04003336static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3337 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003338{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003339 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003340 struct kernfs_node *kn;
3341 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003342 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003343
Tejun Heo2bd59d42014-02-11 11:52:49 -05003344#ifdef CONFIG_DEBUG_LOCK_ALLOC
3345 key = &cft->lockdep_key;
3346#endif
3347 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3348 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003349 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003350 if (IS_ERR(kn))
3351 return PTR_ERR(kn);
3352
3353 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003354 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003355 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003356 return ret;
3357 }
3358
Tejun Heo6f60ead2015-09-18 17:54:23 -04003359 if (cft->file_offset) {
3360 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3361
Tejun Heo34c06252015-11-05 00:12:24 -05003362 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003363 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003364 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003365 }
3366
Tejun Heof8f22e52014-04-23 11:13:16 -04003367 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003368}
3369
Tejun Heob1f28d32013-06-28 16:24:10 -07003370/**
3371 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003372 * @css: the target css
3373 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003374 * @cfts: array of cftypes to be added
3375 * @is_add: whether to add or remove
3376 *
3377 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003378 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003379 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003380static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3381 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003382 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003383{
Tejun Heo6732ed82015-09-18 17:54:23 -04003384 struct cftype *cft, *cft_end = NULL;
Tejun Heob1f28d32013-06-28 16:24:10 -07003385 int ret;
3386
Tejun Heo01f64742014-05-13 12:19:23 -04003387 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003388
Tejun Heo6732ed82015-09-18 17:54:23 -04003389restart:
3390 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003391 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003392 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003393 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003394 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003395 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003396 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003397 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003398 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003399 continue;
3400
Li Zefan2739d3c2013-01-21 18:18:33 +08003401 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003402 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003403 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003404 pr_warn("%s: failed to add %s, err=%d\n",
3405 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003406 cft_end = cft;
3407 is_add = false;
3408 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003409 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003410 } else {
3411 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003412 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003413 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003414 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003415}
3416
Tejun Heo21a2d342014-02-12 09:29:49 -05003417static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003418{
3419 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003420 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003421 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003422 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003423 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003424
Tejun Heo01f64742014-05-13 12:19:23 -04003425 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003426
Li Zefane8c82d22013-06-18 18:48:37 +08003427 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003428 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003429 struct cgroup *cgrp = css->cgroup;
3430
Li Zefane8c82d22013-06-18 18:48:37 +08003431 if (cgroup_is_dead(cgrp))
3432 continue;
3433
Tejun Heo4df8dc92015-09-18 17:54:23 -04003434 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003435 if (ret)
3436 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003437 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003438
3439 if (is_add && !ret)
3440 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003441 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003442}
3443
Tejun Heo2da440a2014-02-11 11:52:48 -05003444static void cgroup_exit_cftypes(struct cftype *cfts)
3445{
3446 struct cftype *cft;
3447
Tejun Heo2bd59d42014-02-11 11:52:49 -05003448 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3449 /* free copy for custom atomic_write_len, see init_cftypes() */
3450 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3451 kfree(cft->kf_ops);
3452 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003453 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003454
3455 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003456 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003457 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003458}
3459
Tejun Heo2bd59d42014-02-11 11:52:49 -05003460static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003461{
3462 struct cftype *cft;
3463
Tejun Heo2bd59d42014-02-11 11:52:49 -05003464 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3465 struct kernfs_ops *kf_ops;
3466
Tejun Heo0adb0702014-02-12 09:29:48 -05003467 WARN_ON(cft->ss || cft->kf_ops);
3468
Tejun Heo2bd59d42014-02-11 11:52:49 -05003469 if (cft->seq_start)
3470 kf_ops = &cgroup_kf_ops;
3471 else
3472 kf_ops = &cgroup_kf_single_ops;
3473
3474 /*
3475 * Ugh... if @cft wants a custom max_write_len, we need to
3476 * make a copy of kf_ops to set its atomic_write_len.
3477 */
3478 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3479 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3480 if (!kf_ops) {
3481 cgroup_exit_cftypes(cfts);
3482 return -ENOMEM;
3483 }
3484 kf_ops->atomic_write_len = cft->max_write_len;
3485 }
3486
3487 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003488 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003489 }
3490
3491 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003492}
3493
Tejun Heo21a2d342014-02-12 09:29:49 -05003494static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3495{
Tejun Heo01f64742014-05-13 12:19:23 -04003496 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003497
3498 if (!cfts || !cfts[0].ss)
3499 return -ENOENT;
3500
3501 list_del(&cfts->node);
3502 cgroup_apply_cftypes(cfts, false);
3503 cgroup_exit_cftypes(cfts);
3504 return 0;
3505}
3506
Tejun Heo8e3f6542012-04-01 12:09:55 -07003507/**
Tejun Heo80b13582014-02-12 09:29:48 -05003508 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3509 * @cfts: zero-length name terminated array of cftypes
3510 *
3511 * Unregister @cfts. Files described by @cfts are removed from all
3512 * existing cgroups and all future cgroups won't have them either. This
3513 * function can be called anytime whether @cfts' subsys is attached or not.
3514 *
3515 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3516 * registered.
3517 */
3518int cgroup_rm_cftypes(struct cftype *cfts)
3519{
Tejun Heo21a2d342014-02-12 09:29:49 -05003520 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003521
Tejun Heo01f64742014-05-13 12:19:23 -04003522 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003523 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003524 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003525 return ret;
3526}
3527
3528/**
3529 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3530 * @ss: target cgroup subsystem
3531 * @cfts: zero-length name terminated array of cftypes
3532 *
3533 * Register @cfts to @ss. Files described by @cfts are created for all
3534 * existing cgroups to which @ss is attached and all future cgroups will
3535 * have them too. This function can be called anytime whether @ss is
3536 * attached or not.
3537 *
3538 * Returns 0 on successful registration, -errno on failure. Note that this
3539 * function currently returns 0 as long as @cfts registration is successful
3540 * even if some file creation attempts on existing cgroups fail.
3541 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003542static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003543{
Tejun Heo9ccece82013-06-28 16:24:11 -07003544 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003545
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003546 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003547 return 0;
3548
Li Zefandc5736e2014-02-17 10:41:50 +08003549 if (!cfts || cfts[0].name[0] == '\0')
3550 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003551
Tejun Heo2bd59d42014-02-11 11:52:49 -05003552 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003553 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003554 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003555
Tejun Heo01f64742014-05-13 12:19:23 -04003556 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003557
Tejun Heo0adb0702014-02-12 09:29:48 -05003558 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003559 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003560 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003561 cgroup_rm_cftypes_locked(cfts);
3562
Tejun Heo01f64742014-05-13 12:19:23 -04003563 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003564 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003565}
Tejun Heo79578622012-04-01 12:09:56 -07003566
3567/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003568 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3569 * @ss: target cgroup subsystem
3570 * @cfts: zero-length name terminated array of cftypes
3571 *
3572 * Similar to cgroup_add_cftypes() but the added files are only used for
3573 * the default hierarchy.
3574 */
3575int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3576{
3577 struct cftype *cft;
3578
3579 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003580 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003581 return cgroup_add_cftypes(ss, cfts);
3582}
3583
3584/**
3585 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3586 * @ss: target cgroup subsystem
3587 * @cfts: zero-length name terminated array of cftypes
3588 *
3589 * Similar to cgroup_add_cftypes() but the added files are only used for
3590 * the legacy hierarchies.
3591 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003592int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3593{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003594 struct cftype *cft;
3595
Tejun Heoe4b70372015-10-15 17:00:43 -04003596 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3597 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003598 return cgroup_add_cftypes(ss, cfts);
3599}
3600
Li Zefana043e3b2008-02-23 15:24:09 -08003601/**
Tejun Heo34c06252015-11-05 00:12:24 -05003602 * cgroup_file_notify - generate a file modified event for a cgroup_file
3603 * @cfile: target cgroup_file
3604 *
3605 * @cfile must have been obtained by setting cftype->file_offset.
3606 */
3607void cgroup_file_notify(struct cgroup_file *cfile)
3608{
3609 unsigned long flags;
3610
3611 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3612 if (cfile->kn)
3613 kernfs_notify(cfile->kn);
3614 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3615}
3616
3617/**
Li Zefana043e3b2008-02-23 15:24:09 -08003618 * cgroup_task_count - count the number of tasks in a cgroup.
3619 * @cgrp: the cgroup in question
3620 *
3621 * Return the number of tasks in the cgroup.
3622 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003623static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003624{
3625 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003626 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003627
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003628 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003629 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3630 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003631 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003632 return count;
3633}
3634
Tejun Heo574bd9f2012-11-09 09:12:29 -08003635/**
Tejun Heo492eb212013-08-08 20:11:25 -04003636 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003637 * @pos: the current position (%NULL to initiate traversal)
3638 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003639 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003640 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003641 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003642 * that @parent and @pos are accessible. The next sibling is guaranteed to
3643 * be returned regardless of their states.
3644 *
3645 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3646 * css which finished ->css_online() is guaranteed to be visible in the
3647 * future iterations and will stay visible until the last reference is put.
3648 * A css which hasn't finished ->css_online() or already finished
3649 * ->css_offline() may show up during traversal. It's each subsystem's
3650 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003651 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003652struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3653 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003654{
Tejun Heoc2931b72014-05-16 13:22:51 -04003655 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003656
Tejun Heo8353da12014-05-13 12:19:23 -04003657 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003658
3659 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003660 * @pos could already have been unlinked from the sibling list.
3661 * Once a cgroup is removed, its ->sibling.next is no longer
3662 * updated when its next sibling changes. CSS_RELEASED is set when
3663 * @pos is taken off list, at which time its next pointer is valid,
3664 * and, as releases are serialized, the one pointed to by the next
3665 * pointer is guaranteed to not have started release yet. This
3666 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3667 * critical section, the one pointed to by its next pointer is
3668 * guaranteed to not have finished its RCU grace period even if we
3669 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003670 *
Tejun Heode3f0342014-05-16 13:22:49 -04003671 * If @pos has CSS_RELEASED set, its next pointer can't be
3672 * dereferenced; however, as each css is given a monotonically
3673 * increasing unique serial number and always appended to the
3674 * sibling list, the next one can be found by walking the parent's
3675 * children until the first css with higher serial number than
3676 * @pos's. While this path can be slower, it happens iff iteration
3677 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003678 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003679 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003680 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3681 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3682 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003683 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003684 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003685 if (next->serial_nr > pos->serial_nr)
3686 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003687 }
3688
Tejun Heo3b281af2014-04-23 11:13:15 -04003689 /*
3690 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003691 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003692 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003693 if (&next->sibling != &parent->children)
3694 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003695 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003696}
Tejun Heo53fa5262013-05-24 10:55:38 +09003697
3698/**
Tejun Heo492eb212013-08-08 20:11:25 -04003699 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003700 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003701 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003702 *
Tejun Heo492eb212013-08-08 20:11:25 -04003703 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003704 * to visit for pre-order traversal of @root's descendants. @root is
3705 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003706 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003707 * While this function requires cgroup_mutex or RCU read locking, it
3708 * doesn't require the whole traversal to be contained in a single critical
3709 * section. This function will return the correct next descendant as long
3710 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003711 *
3712 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3713 * css which finished ->css_online() is guaranteed to be visible in the
3714 * future iterations and will stay visible until the last reference is put.
3715 * A css which hasn't finished ->css_online() or already finished
3716 * ->css_offline() may show up during traversal. It's each subsystem's
3717 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003718 */
Tejun Heo492eb212013-08-08 20:11:25 -04003719struct cgroup_subsys_state *
3720css_next_descendant_pre(struct cgroup_subsys_state *pos,
3721 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003722{
Tejun Heo492eb212013-08-08 20:11:25 -04003723 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003724
Tejun Heo8353da12014-05-13 12:19:23 -04003725 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003726
Tejun Heobd8815a2013-08-08 20:11:27 -04003727 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003728 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003729 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003730
3731 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003732 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003733 if (next)
3734 return next;
3735
3736 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003737 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003738 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003739 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003740 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003741 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003742 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003743
3744 return NULL;
3745}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003746
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003747/**
Tejun Heo492eb212013-08-08 20:11:25 -04003748 * css_rightmost_descendant - return the rightmost descendant of a css
3749 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003750 *
Tejun Heo492eb212013-08-08 20:11:25 -04003751 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3752 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003753 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003754 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003755 * While this function requires cgroup_mutex or RCU read locking, it
3756 * doesn't require the whole traversal to be contained in a single critical
3757 * section. This function will return the correct rightmost descendant as
3758 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003759 */
Tejun Heo492eb212013-08-08 20:11:25 -04003760struct cgroup_subsys_state *
3761css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003762{
Tejun Heo492eb212013-08-08 20:11:25 -04003763 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003764
Tejun Heo8353da12014-05-13 12:19:23 -04003765 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003766
3767 do {
3768 last = pos;
3769 /* ->prev isn't RCU safe, walk ->next till the end */
3770 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003771 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003772 pos = tmp;
3773 } while (pos);
3774
3775 return last;
3776}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003777
Tejun Heo492eb212013-08-08 20:11:25 -04003778static struct cgroup_subsys_state *
3779css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003780{
Tejun Heo492eb212013-08-08 20:11:25 -04003781 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003782
3783 do {
3784 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003785 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003786 } while (pos);
3787
3788 return last;
3789}
3790
3791/**
Tejun Heo492eb212013-08-08 20:11:25 -04003792 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003793 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003794 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003795 *
Tejun Heo492eb212013-08-08 20:11:25 -04003796 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003797 * to visit for post-order traversal of @root's descendants. @root is
3798 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003799 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003800 * While this function requires cgroup_mutex or RCU read locking, it
3801 * doesn't require the whole traversal to be contained in a single critical
3802 * section. This function will return the correct next descendant as long
3803 * as both @pos and @cgroup are accessible and @pos is a descendant of
3804 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003805 *
3806 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3807 * css which finished ->css_online() is guaranteed to be visible in the
3808 * future iterations and will stay visible until the last reference is put.
3809 * A css which hasn't finished ->css_online() or already finished
3810 * ->css_offline() may show up during traversal. It's each subsystem's
3811 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003812 */
Tejun Heo492eb212013-08-08 20:11:25 -04003813struct cgroup_subsys_state *
3814css_next_descendant_post(struct cgroup_subsys_state *pos,
3815 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003816{
Tejun Heo492eb212013-08-08 20:11:25 -04003817 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003818
Tejun Heo8353da12014-05-13 12:19:23 -04003819 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003820
Tejun Heo58b79a92013-09-06 15:31:08 -04003821 /* if first iteration, visit leftmost descendant which may be @root */
3822 if (!pos)
3823 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003824
Tejun Heobd8815a2013-08-08 20:11:27 -04003825 /* if we visited @root, we're done */
3826 if (pos == root)
3827 return NULL;
3828
Tejun Heo574bd9f2012-11-09 09:12:29 -08003829 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003830 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003831 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003832 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003833
3834 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003835 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003836}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003837
Tejun Heof3d46502014-05-16 13:22:52 -04003838/**
3839 * css_has_online_children - does a css have online children
3840 * @css: the target css
3841 *
3842 * Returns %true if @css has any online children; otherwise, %false. This
3843 * function can be called from any context but the caller is responsible
3844 * for synchronizing against on/offlining as necessary.
3845 */
3846bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003847{
Tejun Heof3d46502014-05-16 13:22:52 -04003848 struct cgroup_subsys_state *child;
3849 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003850
3851 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003852 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003853 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003854 ret = true;
3855 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003856 }
3857 }
3858 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003859 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003860}
3861
Tejun Heo0942eee2013-08-08 20:11:26 -04003862/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003863 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003864 * @it: the iterator to advance
3865 *
3866 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003867 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003868static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003869{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003870 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003871 struct cgrp_cset_link *link;
3872 struct css_set *cset;
3873
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003874 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003875
Tejun Heod5158762013-08-08 20:11:26 -04003876 /* Advance to the next non-empty css_set */
3877 do {
3878 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003879 if (l == it->cset_head) {
3880 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003881 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003882 return;
3883 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003884
3885 if (it->ss) {
3886 cset = container_of(l, struct css_set,
3887 e_cset_node[it->ss->id]);
3888 } else {
3889 link = list_entry(l, struct cgrp_cset_link, cset_link);
3890 cset = link->cset;
3891 }
Tejun Heo0de09422015-10-15 16:41:49 -04003892 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05003893
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003894 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003895
3896 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003897 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003898 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003899 it->task_pos = cset->mg_tasks.next;
3900
3901 it->tasks_head = &cset->tasks;
3902 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04003903
3904 /*
3905 * We don't keep css_sets locked across iteration steps and thus
3906 * need to take steps to ensure that iteration can be resumed after
3907 * the lock is re-acquired. Iteration is performed at two levels -
3908 * css_sets and tasks in them.
3909 *
3910 * Once created, a css_set never leaves its cgroup lists, so a
3911 * pinned css_set is guaranteed to stay put and we can resume
3912 * iteration afterwards.
3913 *
3914 * Tasks may leave @cset across iteration steps. This is resolved
3915 * by registering each iterator with the css_set currently being
3916 * walked and making css_set_move_task() advance iterators whose
3917 * next task is leaving.
3918 */
3919 if (it->cur_cset) {
3920 list_del(&it->iters_node);
3921 put_css_set_locked(it->cur_cset);
3922 }
3923 get_css_set(cset);
3924 it->cur_cset = cset;
3925 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04003926}
3927
Tejun Heoecb9d532015-10-15 16:41:52 -04003928static void css_task_iter_advance(struct css_task_iter *it)
3929{
3930 struct list_head *l = it->task_pos;
3931
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003932 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04003933 WARN_ON_ONCE(!l);
3934
3935 /*
3936 * Advance iterator to find next entry. cset->tasks is consumed
3937 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3938 * next cset.
3939 */
3940 l = l->next;
3941
3942 if (l == it->tasks_head)
3943 l = it->mg_tasks_head->next;
3944
3945 if (l == it->mg_tasks_head)
3946 css_task_iter_advance_css_set(it);
3947 else
3948 it->task_pos = l;
3949}
3950
Tejun Heo0942eee2013-08-08 20:11:26 -04003951/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003952 * css_task_iter_start - initiate task iteration
3953 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003954 * @it: the task iterator to use
3955 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003956 * Initiate iteration through the tasks of @css. The caller can call
3957 * css_task_iter_next() to walk through the tasks until the function
3958 * returns NULL. On completion of iteration, css_task_iter_end() must be
3959 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003960 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003961void css_task_iter_start(struct cgroup_subsys_state *css,
3962 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003963{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003964 /* no one should try to iterate before mounting cgroups */
3965 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003966
Tejun Heoed27b9f2015-10-15 16:41:52 -04003967 memset(it, 0, sizeof(*it));
3968
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003969 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003970
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003971 it->ss = css->ss;
3972
3973 if (it->ss)
3974 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3975 else
3976 it->cset_pos = &css->cgroup->cset_links;
3977
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003978 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003979
Tejun Heoecb9d532015-10-15 16:41:52 -04003980 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003981
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003982 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003983}
Paul Menage817929e2007-10-18 23:39:36 -07003984
Tejun Heo0942eee2013-08-08 20:11:26 -04003985/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003986 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003987 * @it: the task iterator being iterated
3988 *
3989 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003990 * initialized via css_task_iter_start(). Returns NULL when the iteration
3991 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003992 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003993struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003994{
Tejun Heod5745672015-10-29 11:43:05 +09003995 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04003996 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09003997 it->cur_task = NULL;
3998 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003999
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004000 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004001
Tejun Heod5745672015-10-29 11:43:05 +09004002 if (it->task_pos) {
4003 it->cur_task = list_entry(it->task_pos, struct task_struct,
4004 cg_list);
4005 get_task_struct(it->cur_task);
4006 css_task_iter_advance(it);
4007 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004008
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004009 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004010
4011 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004012}
4013
Tejun Heo0942eee2013-08-08 20:11:26 -04004014/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004015 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004016 * @it: the task iterator to finish
4017 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004018 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004019 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004020void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004021{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004022 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004023 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004024 list_del(&it->iters_node);
4025 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004026 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004027 }
4028
4029 if (it->cur_task)
4030 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004031}
4032
4033/**
4034 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4035 * @to: cgroup to which the tasks will be moved
4036 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004037 *
4038 * Locking rules between cgroup_post_fork() and the migration path
4039 * guarantee that, if a task is forking while being migrated, the new child
4040 * is guaranteed to be either visible in the source cgroup after the
4041 * parent's migration is complete or put into the target cgroup. No task
4042 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004043 */
4044int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4045{
Tejun Heo952aaa12014-02-25 10:04:03 -05004046 LIST_HEAD(preloaded_csets);
4047 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004048 struct css_task_iter it;
4049 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004050 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004051
Tejun Heo952aaa12014-02-25 10:04:03 -05004052 mutex_lock(&cgroup_mutex);
4053
4054 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004055 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004056 list_for_each_entry(link, &from->cset_links, cset_link)
4057 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004058 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004059
4060 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4061 if (ret)
4062 goto out_err;
4063
4064 /*
4065 * Migrate tasks one-by-one until @form is empty. This fails iff
4066 * ->can_attach() fails.
4067 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004068 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004069 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004070 task = css_task_iter_next(&it);
4071 if (task)
4072 get_task_struct(task);
4073 css_task_iter_end(&it);
4074
4075 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004076 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004077 put_task_struct(task);
4078 }
4079 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004080out_err:
4081 cgroup_migrate_finish(&preloaded_csets);
4082 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004083 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004084}
4085
Paul Menage817929e2007-10-18 23:39:36 -07004086/*
Ben Blum102a7752009-09-23 15:56:26 -07004087 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004088 *
4089 * Reading this file can return large amounts of data if a cgroup has
4090 * *lots* of attached tasks. So it may need several calls to read(),
4091 * but we cannot guarantee that the information we produce is correct
4092 * unless we produce it entirely atomically.
4093 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004094 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004095
Li Zefan24528252012-01-20 11:58:43 +08004096/* which pidlist file are we talking about? */
4097enum cgroup_filetype {
4098 CGROUP_FILE_PROCS,
4099 CGROUP_FILE_TASKS,
4100};
4101
4102/*
4103 * A pidlist is a list of pids that virtually represents the contents of one
4104 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4105 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4106 * to the cgroup.
4107 */
4108struct cgroup_pidlist {
4109 /*
4110 * used to find which pidlist is wanted. doesn't change as long as
4111 * this particular list stays in the list.
4112 */
4113 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4114 /* array of xids */
4115 pid_t *list;
4116 /* how many elements the above list has */
4117 int length;
Li Zefan24528252012-01-20 11:58:43 +08004118 /* each of these stored in a list by its cgroup */
4119 struct list_head links;
4120 /* pointer to the cgroup we belong to, for list removal purposes */
4121 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004122 /* for delayed destruction */
4123 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004124};
4125
Paul Menagebbcb81d2007-10-18 23:39:32 -07004126/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004127 * The following two functions "fix" the issue where there are more pids
4128 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4129 * TODO: replace with a kernel-wide solution to this problem
4130 */
4131#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4132static void *pidlist_allocate(int count)
4133{
4134 if (PIDLIST_TOO_LARGE(count))
4135 return vmalloc(count * sizeof(pid_t));
4136 else
4137 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4138}
Tejun Heob1a21362013-11-29 10:42:58 -05004139
Ben Blumd1d9fd32009-09-23 15:56:28 -07004140static void pidlist_free(void *p)
4141{
Bandan Das58794512015-03-02 17:51:10 -05004142 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004143}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004144
4145/*
Tejun Heob1a21362013-11-29 10:42:58 -05004146 * Used to destroy all pidlists lingering waiting for destroy timer. None
4147 * should be left afterwards.
4148 */
4149static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4150{
4151 struct cgroup_pidlist *l, *tmp_l;
4152
4153 mutex_lock(&cgrp->pidlist_mutex);
4154 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4155 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4156 mutex_unlock(&cgrp->pidlist_mutex);
4157
4158 flush_workqueue(cgroup_pidlist_destroy_wq);
4159 BUG_ON(!list_empty(&cgrp->pidlists));
4160}
4161
4162static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4163{
4164 struct delayed_work *dwork = to_delayed_work(work);
4165 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4166 destroy_dwork);
4167 struct cgroup_pidlist *tofree = NULL;
4168
4169 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004170
4171 /*
Tejun Heo04502362013-11-29 10:42:59 -05004172 * Destroy iff we didn't get queued again. The state won't change
4173 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004174 */
Tejun Heo04502362013-11-29 10:42:59 -05004175 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004176 list_del(&l->links);
4177 pidlist_free(l->list);
4178 put_pid_ns(l->key.ns);
4179 tofree = l;
4180 }
4181
Tejun Heob1a21362013-11-29 10:42:58 -05004182 mutex_unlock(&l->owner->pidlist_mutex);
4183 kfree(tofree);
4184}
4185
4186/*
Ben Blum102a7752009-09-23 15:56:26 -07004187 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004188 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004189 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004190static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004191{
Ben Blum102a7752009-09-23 15:56:26 -07004192 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004193
4194 /*
4195 * we presume the 0th element is unique, so i starts at 1. trivial
4196 * edge cases first; no work needs to be done for either
4197 */
4198 if (length == 0 || length == 1)
4199 return length;
4200 /* src and dest walk down the list; dest counts unique elements */
4201 for (src = 1; src < length; src++) {
4202 /* find next unique element */
4203 while (list[src] == list[src-1]) {
4204 src++;
4205 if (src == length)
4206 goto after;
4207 }
4208 /* dest always points to where the next unique element goes */
4209 list[dest] = list[src];
4210 dest++;
4211 }
4212after:
Ben Blum102a7752009-09-23 15:56:26 -07004213 return dest;
4214}
4215
Tejun Heoafb2bc12013-11-29 10:42:59 -05004216/*
4217 * The two pid files - task and cgroup.procs - guaranteed that the result
4218 * is sorted, which forced this whole pidlist fiasco. As pid order is
4219 * different per namespace, each namespace needs differently sorted list,
4220 * making it impossible to use, for example, single rbtree of member tasks
4221 * sorted by task pointer. As pidlists can be fairly large, allocating one
4222 * per open file is dangerous, so cgroup had to implement shared pool of
4223 * pidlists keyed by cgroup and namespace.
4224 *
4225 * All this extra complexity was caused by the original implementation
4226 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004227 * want to do away with it. Explicitly scramble sort order if on the
4228 * default hierarchy so that no such expectation exists in the new
4229 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004230 *
4231 * Scrambling is done by swapping every two consecutive bits, which is
4232 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4233 */
4234static pid_t pid_fry(pid_t pid)
4235{
4236 unsigned a = pid & 0x55555555;
4237 unsigned b = pid & 0xAAAAAAAA;
4238
4239 return (a << 1) | (b >> 1);
4240}
4241
4242static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4243{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004244 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004245 return pid_fry(pid);
4246 else
4247 return pid;
4248}
4249
Ben Blum102a7752009-09-23 15:56:26 -07004250static int cmppid(const void *a, const void *b)
4251{
4252 return *(pid_t *)a - *(pid_t *)b;
4253}
4254
Tejun Heoafb2bc12013-11-29 10:42:59 -05004255static int fried_cmppid(const void *a, const void *b)
4256{
4257 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4258}
4259
Ben Blum72a8cb32009-09-23 15:56:27 -07004260static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4261 enum cgroup_filetype type)
4262{
4263 struct cgroup_pidlist *l;
4264 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004265 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004266
Tejun Heoe6b81712013-11-29 10:42:59 -05004267 lockdep_assert_held(&cgrp->pidlist_mutex);
4268
4269 list_for_each_entry(l, &cgrp->pidlists, links)
4270 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004271 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004272 return NULL;
4273}
4274
Ben Blum72a8cb32009-09-23 15:56:27 -07004275/*
4276 * find the appropriate pidlist for our purpose (given procs vs tasks)
4277 * returns with the lock on that pidlist already held, and takes care
4278 * of the use count, or returns NULL with no locks held if we're out of
4279 * memory.
4280 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004281static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4282 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004283{
4284 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004285
Tejun Heoe6b81712013-11-29 10:42:59 -05004286 lockdep_assert_held(&cgrp->pidlist_mutex);
4287
4288 l = cgroup_pidlist_find(cgrp, type);
4289 if (l)
4290 return l;
4291
Ben Blum72a8cb32009-09-23 15:56:27 -07004292 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004293 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004294 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004295 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004296
Tejun Heob1a21362013-11-29 10:42:58 -05004297 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004298 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004299 /* don't need task_nsproxy() if we're looking at ourself */
4300 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004301 l->owner = cgrp;
4302 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004303 return l;
4304}
4305
4306/*
Ben Blum102a7752009-09-23 15:56:26 -07004307 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4308 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004309static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4310 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004311{
4312 pid_t *array;
4313 int length;
4314 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004315 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004316 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004317 struct cgroup_pidlist *l;
4318
Tejun Heo4bac00d2013-11-29 10:42:59 -05004319 lockdep_assert_held(&cgrp->pidlist_mutex);
4320
Ben Blum102a7752009-09-23 15:56:26 -07004321 /*
4322 * If cgroup gets more users after we read count, we won't have
4323 * enough space - tough. This race is indistinguishable to the
4324 * caller from the case that the additional cgroup users didn't
4325 * show up until sometime later on.
4326 */
4327 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004328 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004329 if (!array)
4330 return -ENOMEM;
4331 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004332 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004333 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004334 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004335 break;
Ben Blum102a7752009-09-23 15:56:26 -07004336 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004337 if (type == CGROUP_FILE_PROCS)
4338 pid = task_tgid_vnr(tsk);
4339 else
4340 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004341 if (pid > 0) /* make sure to only use valid results */
4342 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004343 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004344 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004345 length = n;
4346 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004347 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004348 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4349 else
4350 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004351 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004352 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004353
Tejun Heoe6b81712013-11-29 10:42:59 -05004354 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004355 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004356 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004357 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004358 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004359
4360 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004361 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004362 l->list = array;
4363 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004364 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004365 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004366}
4367
Balbir Singh846c7bb2007-10-18 23:39:44 -07004368/**
Li Zefana043e3b2008-02-23 15:24:09 -08004369 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004370 * @stats: cgroupstats to fill information into
4371 * @dentry: A dentry entry belonging to the cgroup for which stats have
4372 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004373 *
4374 * Build and fill cgroupstats so that taskstats can export it to user
4375 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004376 */
4377int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4378{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004379 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004380 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004381 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004382 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004383
Tejun Heo2bd59d42014-02-11 11:52:49 -05004384 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4385 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4386 kernfs_type(kn) != KERNFS_DIR)
4387 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004388
Li Zefanbad34662014-02-14 16:54:28 +08004389 mutex_lock(&cgroup_mutex);
4390
Tejun Heo2bd59d42014-02-11 11:52:49 -05004391 /*
4392 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004393 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004394 * @kn->priv is RCU safe. Let's do the RCU dancing.
4395 */
4396 rcu_read_lock();
4397 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004398 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004399 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004400 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004401 return -ENOENT;
4402 }
Li Zefanbad34662014-02-14 16:54:28 +08004403 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004404
Tejun Heo9d800df2014-05-14 09:15:00 -04004405 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004406 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004407 switch (tsk->state) {
4408 case TASK_RUNNING:
4409 stats->nr_running++;
4410 break;
4411 case TASK_INTERRUPTIBLE:
4412 stats->nr_sleeping++;
4413 break;
4414 case TASK_UNINTERRUPTIBLE:
4415 stats->nr_uninterruptible++;
4416 break;
4417 case TASK_STOPPED:
4418 stats->nr_stopped++;
4419 break;
4420 default:
4421 if (delayacct_is_task_waiting_on_io(tsk))
4422 stats->nr_io_wait++;
4423 break;
4424 }
4425 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004426 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004427
Li Zefanbad34662014-02-14 16:54:28 +08004428 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004429 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004430}
4431
Paul Menage8f3ff202009-09-23 15:56:25 -07004432
Paul Menagecc31edc2008-10-18 20:28:04 -07004433/*
Ben Blum102a7752009-09-23 15:56:26 -07004434 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004435 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004436 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004437 */
4438
Ben Blum102a7752009-09-23 15:56:26 -07004439static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004440{
4441 /*
4442 * Initially we receive a position value that corresponds to
4443 * one more than the last pid shown (or 0 on the first call or
4444 * after a seek to the start). Use a binary-search to find the
4445 * next pid to display, if any
4446 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004447 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004448 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004449 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004450 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004451 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004452 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004453
Tejun Heo4bac00d2013-11-29 10:42:59 -05004454 mutex_lock(&cgrp->pidlist_mutex);
4455
4456 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004457 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004458 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004459 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004460 * could already have been destroyed.
4461 */
Tejun Heo5d224442013-12-05 12:28:04 -05004462 if (of->priv)
4463 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004464
4465 /*
4466 * Either this is the first start() after open or the matching
4467 * pidlist has been destroyed inbetween. Create a new one.
4468 */
Tejun Heo5d224442013-12-05 12:28:04 -05004469 if (!of->priv) {
4470 ret = pidlist_array_load(cgrp, type,
4471 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004472 if (ret)
4473 return ERR_PTR(ret);
4474 }
Tejun Heo5d224442013-12-05 12:28:04 -05004475 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004476
Paul Menagecc31edc2008-10-18 20:28:04 -07004477 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004478 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004479
Paul Menagecc31edc2008-10-18 20:28:04 -07004480 while (index < end) {
4481 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004482 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004483 index = mid;
4484 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004485 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004486 index = mid + 1;
4487 else
4488 end = mid;
4489 }
4490 }
4491 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004492 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004493 return NULL;
4494 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004495 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004496 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004497 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004498}
4499
Ben Blum102a7752009-09-23 15:56:26 -07004500static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004501{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004502 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004503 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004504
Tejun Heo5d224442013-12-05 12:28:04 -05004505 if (l)
4506 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004507 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004508 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004509}
4510
Ben Blum102a7752009-09-23 15:56:26 -07004511static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004512{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004513 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004514 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004515 pid_t *p = v;
4516 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004517 /*
4518 * Advance to the next pid in the array. If this goes off the
4519 * end, we're done
4520 */
4521 p++;
4522 if (p >= end) {
4523 return NULL;
4524 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004525 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004526 return p;
4527 }
4528}
4529
Ben Blum102a7752009-09-23 15:56:26 -07004530static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004531{
Joe Perches94ff2122015-04-15 16:18:20 -07004532 seq_printf(s, "%d\n", *(int *)v);
4533
4534 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004535}
4536
Tejun Heo182446d2013-08-08 20:11:24 -04004537static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4538 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004539{
Tejun Heo182446d2013-08-08 20:11:24 -04004540 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004541}
4542
Tejun Heo182446d2013-08-08 20:11:24 -04004543static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4544 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004545{
Paul Menage6379c102008-07-25 01:47:01 -07004546 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004547 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004548 else
Tejun Heo182446d2013-08-08 20:11:24 -04004549 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004550 return 0;
4551}
4552
Tejun Heo182446d2013-08-08 20:11:24 -04004553static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4554 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004555{
Tejun Heo182446d2013-08-08 20:11:24 -04004556 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004557}
4558
Tejun Heo182446d2013-08-08 20:11:24 -04004559static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4560 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004561{
4562 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004563 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004564 else
Tejun Heo182446d2013-08-08 20:11:24 -04004565 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004566 return 0;
4567}
4568
Tejun Heoa14c6872014-07-15 11:05:09 -04004569/* cgroup core interface files for the default hierarchy */
4570static struct cftype cgroup_dfl_base_files[] = {
4571 {
4572 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004573 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004574 .seq_start = cgroup_pidlist_start,
4575 .seq_next = cgroup_pidlist_next,
4576 .seq_stop = cgroup_pidlist_stop,
4577 .seq_show = cgroup_pidlist_show,
4578 .private = CGROUP_FILE_PROCS,
4579 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004580 },
4581 {
4582 .name = "cgroup.controllers",
4583 .flags = CFTYPE_ONLY_ON_ROOT,
4584 .seq_show = cgroup_root_controllers_show,
4585 },
4586 {
4587 .name = "cgroup.controllers",
4588 .flags = CFTYPE_NOT_ON_ROOT,
4589 .seq_show = cgroup_controllers_show,
4590 },
4591 {
4592 .name = "cgroup.subtree_control",
4593 .seq_show = cgroup_subtree_control_show,
4594 .write = cgroup_subtree_control_write,
4595 },
4596 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004597 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004598 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004599 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004600 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004601 },
4602 { } /* terminate */
4603};
4604
4605/* cgroup core interface files for the legacy hierarchies */
4606static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004607 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004608 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004609 .seq_start = cgroup_pidlist_start,
4610 .seq_next = cgroup_pidlist_next,
4611 .seq_stop = cgroup_pidlist_stop,
4612 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004613 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004614 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004615 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004616 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004617 .name = "cgroup.clone_children",
4618 .read_u64 = cgroup_clone_children_read,
4619 .write_u64 = cgroup_clone_children_write,
4620 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004621 {
Tejun Heo873fe092013-04-14 20:15:26 -07004622 .name = "cgroup.sane_behavior",
4623 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004624 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004625 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004626 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004627 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004628 .seq_start = cgroup_pidlist_start,
4629 .seq_next = cgroup_pidlist_next,
4630 .seq_stop = cgroup_pidlist_stop,
4631 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004632 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004633 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004634 },
4635 {
4636 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004637 .read_u64 = cgroup_read_notify_on_release,
4638 .write_u64 = cgroup_write_notify_on_release,
4639 },
Tejun Heo873fe092013-04-14 20:15:26 -07004640 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004641 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004642 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004643 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004644 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004645 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004646 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004647 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004648};
4649
Tejun Heo0c21ead2013-08-13 20:22:51 -04004650/*
4651 * css destruction is four-stage process.
4652 *
4653 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4654 * Implemented in kill_css().
4655 *
4656 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004657 * and thus css_tryget_online() is guaranteed to fail, the css can be
4658 * offlined by invoking offline_css(). After offlining, the base ref is
4659 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004660 *
4661 * 3. When the percpu_ref reaches zero, the only possible remaining
4662 * accessors are inside RCU read sections. css_release() schedules the
4663 * RCU callback.
4664 *
4665 * 4. After the grace period, the css can be freed. Implemented in
4666 * css_free_work_fn().
4667 *
4668 * It is actually hairier because both step 2 and 4 require process context
4669 * and thus involve punting to css->destroy_work adding two additional
4670 * steps to the already complex sequence.
4671 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004672static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004673{
4674 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004675 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004676 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004677 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004678
Tejun Heo9a1049d2014-06-28 08:10:14 -04004679 percpu_ref_exit(&css->refcnt);
4680
Vladimir Davydov01e58652015-02-12 14:59:26 -08004681 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004682 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004683 int id = css->id;
4684
Tejun Heo9d755d32014-05-14 09:15:02 -04004685 if (css->parent)
4686 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004687
Vladimir Davydov01e58652015-02-12 14:59:26 -08004688 ss->css_free(css);
4689 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004690 cgroup_put(cgrp);
4691 } else {
4692 /* cgroup free path */
4693 atomic_dec(&cgrp->root->nr_cgrps);
4694 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004695 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004696
Tejun Heod51f39b2014-05-16 13:22:48 -04004697 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004698 /*
4699 * We get a ref to the parent, and put the ref when
4700 * this cgroup is being freed, so it's guaranteed
4701 * that the parent won't be destroyed before its
4702 * children.
4703 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004704 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004705 kernfs_put(cgrp->kn);
4706 kfree(cgrp);
4707 } else {
4708 /*
4709 * This is root cgroup's refcnt reaching zero,
4710 * which indicates that the root should be
4711 * released.
4712 */
4713 cgroup_destroy_root(cgrp->root);
4714 }
4715 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004716}
4717
4718static void css_free_rcu_fn(struct rcu_head *rcu_head)
4719{
4720 struct cgroup_subsys_state *css =
4721 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4722
Tejun Heo0c21ead2013-08-13 20:22:51 -04004723 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004724 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004725}
4726
Tejun Heo25e15d82014-05-14 09:15:02 -04004727static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004728{
4729 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004730 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004731 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004732 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004733
Tejun Heo1fed1b22014-05-16 13:22:49 -04004734 mutex_lock(&cgroup_mutex);
4735
Tejun Heode3f0342014-05-16 13:22:49 -04004736 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004737 list_del_rcu(&css->sibling);
4738
Tejun Heo9d755d32014-05-14 09:15:02 -04004739 if (ss) {
4740 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004741 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004742 if (ss->css_released)
4743 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004744 } else {
4745 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004746 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4747 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004748
4749 /*
4750 * There are two control paths which try to determine
4751 * cgroup from dentry without going through kernfs -
4752 * cgroupstats_build() and css_tryget_online_from_dir().
4753 * Those are supported by RCU protecting clearing of
4754 * cgrp->kn->priv backpointer.
4755 */
4756 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004757 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004758
Tejun Heo1fed1b22014-05-16 13:22:49 -04004759 mutex_unlock(&cgroup_mutex);
4760
Tejun Heo0c21ead2013-08-13 20:22:51 -04004761 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004762}
4763
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004764static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004765{
4766 struct cgroup_subsys_state *css =
4767 container_of(ref, struct cgroup_subsys_state, refcnt);
4768
Tejun Heo25e15d82014-05-14 09:15:02 -04004769 INIT_WORK(&css->destroy_work, css_release_work_fn);
4770 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004771}
4772
Tejun Heoddfcada2014-05-04 15:09:14 -04004773static void init_and_link_css(struct cgroup_subsys_state *css,
4774 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004775{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004776 lockdep_assert_held(&cgroup_mutex);
4777
Tejun Heoddfcada2014-05-04 15:09:14 -04004778 cgroup_get(cgrp);
4779
Tejun Heod5c419b2014-05-16 13:22:48 -04004780 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004781 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004782 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004783 INIT_LIST_HEAD(&css->sibling);
4784 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004785 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004786
Tejun Heod51f39b2014-05-16 13:22:48 -04004787 if (cgroup_parent(cgrp)) {
4788 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004789 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004790 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004791
Tejun Heoca8bdca2013-08-26 18:40:56 -04004792 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004793}
4794
Li Zefan2a4ac632013-07-31 16:16:40 +08004795/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004796static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004797{
Tejun Heo623f9262013-08-13 11:01:55 -04004798 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004799 int ret = 0;
4800
Tejun Heoa31f2d32012-11-19 08:13:37 -08004801 lockdep_assert_held(&cgroup_mutex);
4802
Tejun Heo92fb9742012-11-19 08:13:38 -08004803 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004804 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004805 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004806 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004807 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004808 }
Tejun Heob1929db2012-11-19 08:13:38 -08004809 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004810}
4811
Li Zefan2a4ac632013-07-31 16:16:40 +08004812/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004813static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004814{
Tejun Heo623f9262013-08-13 11:01:55 -04004815 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004816
4817 lockdep_assert_held(&cgroup_mutex);
4818
4819 if (!(css->flags & CSS_ONLINE))
4820 return;
4821
Li Zefand7eeac12013-03-12 15:35:59 -07004822 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004823 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004824
Tejun Heoeb954192013-08-08 20:11:23 -04004825 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004826 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004827
4828 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004829}
4830
Tejun Heoc81c925a2013-12-06 15:11:56 -05004831/**
4832 * create_css - create a cgroup_subsys_state
4833 * @cgrp: the cgroup new css will be associated with
4834 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004835 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004836 *
4837 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004838 * css is online and installed in @cgrp with all interface files created if
4839 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004840 */
Tejun Heof63070d2014-07-08 18:02:57 -04004841static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4842 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004843{
Tejun Heod51f39b2014-05-16 13:22:48 -04004844 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004845 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004846 struct cgroup_subsys_state *css;
4847 int err;
4848
Tejun Heoc81c925a2013-12-06 15:11:56 -05004849 lockdep_assert_held(&cgroup_mutex);
4850
Tejun Heo1fed1b22014-05-16 13:22:49 -04004851 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004852 if (IS_ERR(css))
4853 return PTR_ERR(css);
4854
Tejun Heoddfcada2014-05-04 15:09:14 -04004855 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004856
Tejun Heo2aad2a82014-09-24 13:31:50 -04004857 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004858 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004859 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004860
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004861 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004862 if (err < 0)
4863 goto err_free_percpu_ref;
4864 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004865
Tejun Heof63070d2014-07-08 18:02:57 -04004866 if (visible) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04004867 err = css_populate_dir(css, NULL);
Tejun Heof63070d2014-07-08 18:02:57 -04004868 if (err)
4869 goto err_free_id;
4870 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004871
4872 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004873 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004874 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004875
4876 err = online_css(css);
4877 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004878 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004879
Tejun Heoc81c925a2013-12-06 15:11:56 -05004880 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004881 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004882 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 -04004883 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004884 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004885 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004886 ss->warned_broken_hierarchy = true;
4887 }
4888
4889 return 0;
4890
Tejun Heo1fed1b22014-05-16 13:22:49 -04004891err_list_del:
4892 list_del_rcu(&css->sibling);
Tejun Heo4df8dc92015-09-18 17:54:23 -04004893 css_clear_dir(css, NULL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004894err_free_id:
4895 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004896err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004897 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004898err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004899 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004900 return err;
4901}
4902
Tejun Heob3bfd982014-05-13 12:19:22 -04004903static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4904 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004905{
Tejun Heoa9746d82014-05-13 12:19:22 -04004906 struct cgroup *parent, *cgrp;
4907 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004908 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004909 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004910 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004911
Alban Crequy71b1fb52014-08-18 12:20:20 +01004912 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4913 */
4914 if (strchr(name, '\n'))
4915 return -EINVAL;
4916
Tejun Heoa9746d82014-05-13 12:19:22 -04004917 parent = cgroup_kn_lock_live(parent_kn);
4918 if (!parent)
4919 return -ENODEV;
4920 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921
Tejun Heo0a950f62012-11-19 09:02:12 -08004922 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004923 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004924 if (!cgrp) {
4925 ret = -ENOMEM;
4926 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004927 }
4928
Tejun Heo2aad2a82014-09-24 13:31:50 -04004929 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004930 if (ret)
4931 goto out_free_cgrp;
4932
Li Zefan0ab02ca2014-02-11 16:05:46 +08004933 /*
4934 * Temporarily set the pointer to NULL, so idr_find() won't return
4935 * a half-baked cgroup.
4936 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004937 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004938 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004939 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004940 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004941 }
4942
Paul Menagecc31edc2008-10-18 20:28:04 -07004943 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004944
Tejun Heo9d800df2014-05-14 09:15:00 -04004945 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004946 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004947
Li Zefanb6abdb02008-03-04 14:28:19 -08004948 if (notify_on_release(parent))
4949 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4950
Tejun Heo2260e7f2012-11-19 08:13:38 -08004951 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4952 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004953
Tejun Heo2bd59d42014-02-11 11:52:49 -05004954 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004955 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004956 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004957 ret = PTR_ERR(kn);
4958 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004959 }
4960 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004961
Tejun Heo6f305582014-02-12 09:29:50 -05004962 /*
4963 * This extra ref will be put in cgroup_free_fn() and guarantees
4964 * that @cgrp->kn is always accessible.
4965 */
4966 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004967
Tejun Heo0cb51d72014-05-16 13:22:49 -04004968 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004969
Tejun Heo4e139af2012-11-19 08:13:36 -08004970 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004971 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004972 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004973 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004974
Tejun Heo0d802552013-12-06 15:11:56 -05004975 /*
4976 * @cgrp is now fully operational. If something fails after this
4977 * point, it'll be released via the normal destruction path.
4978 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004979 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004980
Tejun Heoba0f4d72014-05-13 12:19:22 -04004981 ret = cgroup_kn_set_ugid(kn);
4982 if (ret)
4983 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004984
Tejun Heo4df8dc92015-09-18 17:54:23 -04004985 ret = css_populate_dir(&cgrp->self, NULL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004986 if (ret)
4987 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004988
Tejun Heo9d403e92013-12-06 15:11:56 -05004989 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004990 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004991 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004992 ret = create_css(cgrp, ss,
4993 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004994 if (ret)
4995 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004996 }
Tejun Heoa8638032012-11-09 09:12:29 -08004997 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004998
Tejun Heobd53d612014-04-23 11:13:16 -04004999 /*
5000 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04005001 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04005002 */
Tejun Heo667c2492014-07-08 18:02:56 -04005003 if (!cgroup_on_dfl(cgrp)) {
5004 cgrp->subtree_control = parent->subtree_control;
5005 cgroup_refresh_child_subsys_mask(cgrp);
5006 }
Tejun Heof392e512014-04-23 11:13:14 -04005007
Tejun Heo2bd59d42014-02-11 11:52:49 -05005008 kernfs_activate(kn);
5009
Tejun Heoba0f4d72014-05-13 12:19:22 -04005010 ret = 0;
5011 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005012
Tejun Heoba0f4d72014-05-13 12:19:22 -04005013out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04005014 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04005015out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04005016 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005017out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07005018 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005019out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005020 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005021 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04005022
5023out_destroy:
5024 cgroup_destroy_locked(cgrp);
5025 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005026}
5027
Tejun Heo223dbc32013-08-13 20:22:50 -04005028/*
5029 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005030 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5031 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005032 */
5033static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005034{
Tejun Heo223dbc32013-08-13 20:22:50 -04005035 struct cgroup_subsys_state *css =
5036 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005037
Tejun Heof20104d2013-08-13 20:22:50 -04005038 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005039 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04005040 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005041
Tejun Heo09a503ea2013-08-13 20:22:50 -04005042 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07005043}
5044
Tejun Heo223dbc32013-08-13 20:22:50 -04005045/* css kill confirmation processing requires process context, bounce */
5046static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005047{
5048 struct cgroup_subsys_state *css =
5049 container_of(ref, struct cgroup_subsys_state, refcnt);
5050
Tejun Heo223dbc32013-08-13 20:22:50 -04005051 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05005052 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005053}
5054
Tejun Heof392e512014-04-23 11:13:14 -04005055/**
5056 * kill_css - destroy a css
5057 * @css: css to destroy
5058 *
5059 * This function initiates destruction of @css by removing cgroup interface
5060 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005061 * asynchronously once css_tryget_online() is guaranteed to fail and when
5062 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005063 */
5064static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005065{
Tejun Heo01f64742014-05-13 12:19:23 -04005066 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005067
Tejun Heo2bd59d42014-02-11 11:52:49 -05005068 /*
5069 * This must happen before css is disassociated with its cgroup.
5070 * See seq_css() for details.
5071 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005072 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005073
Tejun Heoedae0c32013-08-13 20:22:51 -04005074 /*
5075 * Killing would put the base ref, but we need to keep it alive
5076 * until after ->css_offline().
5077 */
5078 css_get(css);
5079
5080 /*
5081 * cgroup core guarantees that, by the time ->css_offline() is
5082 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005083 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005084 * proceed to offlining css's because percpu_ref_kill() doesn't
5085 * guarantee that the ref is seen as killed on all CPUs on return.
5086 *
5087 * Use percpu_ref_kill_and_confirm() to get notifications as each
5088 * css is confirmed to be seen as killed on all CPUs.
5089 */
5090 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005091}
5092
5093/**
5094 * cgroup_destroy_locked - the first stage of cgroup destruction
5095 * @cgrp: cgroup to be destroyed
5096 *
5097 * css's make use of percpu refcnts whose killing latency shouldn't be
5098 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005099 * guarantee that css_tryget_online() won't succeed by the time
5100 * ->css_offline() is invoked. To satisfy all the requirements,
5101 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005102 *
5103 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5104 * userland visible parts and start killing the percpu refcnts of
5105 * css's. Set up so that the next stage will be kicked off once all
5106 * the percpu refcnts are confirmed to be killed.
5107 *
5108 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5109 * rest of destruction. Once all cgroup references are gone, the
5110 * cgroup is RCU-freed.
5111 *
5112 * This function implements s1. After this step, @cgrp is gone as far as
5113 * the userland is concerned and a new cgroup with the same name may be
5114 * created. As cgroup doesn't care about the names internally, this
5115 * doesn't cause any problem.
5116 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005117static int cgroup_destroy_locked(struct cgroup *cgrp)
5118 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005119{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005120 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005121 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005122
Tejun Heo42809dd2012-11-19 08:13:37 -08005123 lockdep_assert_held(&cgroup_mutex);
5124
Tejun Heo91486f62015-10-15 16:41:51 -04005125 /*
5126 * Only migration can raise populated from zero and we're already
5127 * holding cgroup_mutex.
5128 */
5129 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005130 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005131
Tejun Heo1a90dd52012-11-05 09:16:59 -08005132 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005133 * Make sure there's no live children. We can't test emptiness of
5134 * ->self.children as dead children linger on it while being
5135 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005136 */
Tejun Heof3d46502014-05-16 13:22:52 -04005137 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005138 return -EBUSY;
5139
5140 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005141 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005142 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005143 */
Tejun Heo184faf32014-05-16 13:22:51 -04005144 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005145
Tejun Heo249f3462014-05-14 09:15:01 -04005146 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005147 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005148 kill_css(css);
5149
Tejun Heo455050d2013-06-13 19:27:41 -07005150 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005151 * Remove @cgrp directory along with the base files. @cgrp has an
5152 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005153 */
Tejun Heo01f64742014-05-13 12:19:23 -04005154 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005155
Tejun Heod51f39b2014-05-16 13:22:48 -04005156 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005157
Tejun Heo249f3462014-05-14 09:15:01 -04005158 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005159 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005160
Tejun Heoea15f8c2013-06-13 19:27:42 -07005161 return 0;
5162};
5163
Tejun Heo2bd59d42014-02-11 11:52:49 -05005164static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005165{
Tejun Heoa9746d82014-05-13 12:19:22 -04005166 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005167 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005168
Tejun Heoa9746d82014-05-13 12:19:22 -04005169 cgrp = cgroup_kn_lock_live(kn);
5170 if (!cgrp)
5171 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005172
Tejun Heoa9746d82014-05-13 12:19:22 -04005173 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005174
Tejun Heoa9746d82014-05-13 12:19:22 -04005175 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005176 return ret;
5177}
5178
Tejun Heo2bd59d42014-02-11 11:52:49 -05005179static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5180 .remount_fs = cgroup_remount,
5181 .show_options = cgroup_show_options,
5182 .mkdir = cgroup_mkdir,
5183 .rmdir = cgroup_rmdir,
5184 .rename = cgroup_rename,
5185};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005186
Tejun Heo15a4c832014-05-04 15:09:14 -04005187static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005188{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005189 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005190
5191 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005192
Tejun Heo648bb562012-11-19 08:13:36 -08005193 mutex_lock(&cgroup_mutex);
5194
Tejun Heo15a4c832014-05-04 15:09:14 -04005195 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005196 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005197
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005198 /* Create the root cgroup state for this subsystem */
5199 ss->root = &cgrp_dfl_root;
5200 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005201 /* We don't handle early failures gracefully */
5202 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005203 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005204
5205 /*
5206 * Root csses are never destroyed and we can't initialize
5207 * percpu_ref during early init. Disable refcnting.
5208 */
5209 css->flags |= CSS_NO_REF;
5210
Tejun Heo15a4c832014-05-04 15:09:14 -04005211 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005212 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005213 css->id = 1;
5214 } else {
5215 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5216 BUG_ON(css->id < 0);
5217 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005218
Li Zefane8d55fd2008-04-29 01:00:13 -07005219 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005220 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005221 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005222 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005223 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005224
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005225 have_fork_callback |= (bool)ss->fork << ss->id;
5226 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005227 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005228 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005229
Li Zefane8d55fd2008-04-29 01:00:13 -07005230 /* At system boot, before all subsystems have been
5231 * registered, no tasks have been forked, so we don't
5232 * need to invoke fork callbacks here. */
5233 BUG_ON(!list_empty(&init_task.tasks));
5234
Tejun Heoae7f1642013-08-13 20:22:50 -04005235 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005236
Tejun Heo648bb562012-11-19 08:13:36 -08005237 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005238}
5239
5240/**
Li Zefana043e3b2008-02-23 15:24:09 -08005241 * cgroup_init_early - cgroup initialization at system boot
5242 *
5243 * Initialize cgroups at system boot, and initialize any
5244 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005245 */
5246int __init cgroup_init_early(void)
5247{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005248 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005249 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005250 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005251
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005252 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005253 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5254
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005255 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005256
Tejun Heo3ed80a62014-02-08 10:36:58 -05005257 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005258 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05005259 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5260 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005261 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005262 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5263 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005264
Tejun Heoaec25022014-02-08 10:36:58 -05005265 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005266 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005267 if (!ss->legacy_name)
5268 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005269
5270 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005271 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005272 }
5273 return 0;
5274}
5275
Tejun Heoa3e72732015-09-25 16:24:27 -04005276static unsigned long cgroup_disable_mask __initdata;
5277
Paul Menageddbcc7e2007-10-18 23:39:30 -07005278/**
Li Zefana043e3b2008-02-23 15:24:09 -08005279 * cgroup_init - cgroup initialization
5280 *
5281 * Register cgroup filesystem and /proc file, and initialize
5282 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005283 */
5284int __init cgroup_init(void)
5285{
Tejun Heo30159ec2013-06-25 11:53:37 -07005286 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005287 unsigned long key;
Tejun Heo035f4f52015-10-15 17:00:43 -04005288 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005289
Tejun Heo1ed13282015-09-16 12:53:17 -04005290 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005291 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5292 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005293
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005294 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005295
Tejun Heo82fe9b02013-06-25 11:53:37 -07005296 /* Add init_css_set to the hash table */
5297 key = css_set_hash(init_css_set.subsys);
5298 hash_add(css_set_table, &init_css_set.hlist, key);
5299
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005300 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005301
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005302 mutex_unlock(&cgroup_mutex);
5303
Tejun Heo172a2c062014-03-19 10:23:53 -04005304 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005305 if (ss->early_init) {
5306 struct cgroup_subsys_state *css =
5307 init_css_set.subsys[ss->id];
5308
5309 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5310 GFP_KERNEL);
5311 BUG_ON(css->id < 0);
5312 } else {
5313 cgroup_init_subsys(ss, false);
5314 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005315
Tejun Heo2d8f2432014-04-23 11:13:15 -04005316 list_add_tail(&init_css_set.e_cset_node[ssid],
5317 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005318
5319 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005320 * Setting dfl_root subsys_mask needs to consider the
5321 * disabled flag and cftype registration needs kmalloc,
5322 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005323 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005324 if (cgroup_disable_mask & (1 << ssid)) {
5325 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5326 printk(KERN_INFO "Disabling %s control group subsystem\n",
5327 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005328 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005329 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005330
5331 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5332
Tejun Heo5de4fa12014-07-15 11:05:10 -04005333 if (!ss->dfl_cftypes)
5334 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5335
Tejun Heoa8ddc822014-07-15 11:05:10 -04005336 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5337 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5338 } else {
5339 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5340 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005341 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005342
5343 if (ss->bind)
5344 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005345 }
Greg KH676db4a2010-08-05 13:53:35 -07005346
Tejun Heo035f4f52015-10-15 17:00:43 -04005347 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5348 WARN_ON(register_filesystem(&cgroup_fs_type));
5349 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005350
Tejun Heo2bd59d42014-02-11 11:52:49 -05005351 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005352}
Paul Menageb4f48b62007-10-18 23:39:33 -07005353
Tejun Heoe5fca242013-11-22 17:14:39 -05005354static int __init cgroup_wq_init(void)
5355{
5356 /*
5357 * There isn't much point in executing destruction path in
5358 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005359 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005360 *
5361 * We would prefer to do this in cgroup_init() above, but that
5362 * is called before init_workqueues(): so leave this until after.
5363 */
Tejun Heo1a115332014-02-12 19:06:19 -05005364 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005365 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005366
5367 /*
5368 * Used to destroy pidlists and separate to serve as flush domain.
5369 * Cap @max_active to 1 too.
5370 */
5371 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5372 0, 1);
5373 BUG_ON(!cgroup_pidlist_destroy_wq);
5374
Tejun Heoe5fca242013-11-22 17:14:39 -05005375 return 0;
5376}
5377core_initcall(cgroup_wq_init);
5378
Paul Menagea4243162007-10-18 23:39:35 -07005379/*
5380 * proc_cgroup_show()
5381 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5382 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005383 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005384int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5385 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005386{
Tejun Heoe61734c2014-02-12 09:29:50 -05005387 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005388 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005389 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005390
5391 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005392 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005393 if (!buf)
5394 goto out;
5395
Paul Menagea4243162007-10-18 23:39:35 -07005396 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005397 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005398
Tejun Heo985ed672014-03-19 10:23:53 -04005399 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005400 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005401 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005402 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005403
Tejun Heoa2dd4242014-03-19 10:23:55 -04005404 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005405 continue;
5406
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005407 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005408 if (root != &cgrp_dfl_root)
5409 for_each_subsys(ss, ssid)
5410 if (root->subsys_mask & (1 << ssid))
5411 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005412 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005413 if (strlen(root->name))
5414 seq_printf(m, "%sname=%s", count ? "," : "",
5415 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005416 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005417
Paul Menage7717f7b2009-09-23 15:56:22 -07005418 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005419
5420 /*
5421 * On traditional hierarchies, all zombie tasks show up as
5422 * belonging to the root cgroup. On the default hierarchy,
5423 * while a zombie doesn't show up in "cgroup.procs" and
5424 * thus can't be migrated, its /proc/PID/cgroup keeps
5425 * reporting the cgroup it belonged to before exiting. If
5426 * the cgroup is removed before the zombie is reaped,
5427 * " (deleted)" is appended to the cgroup path.
5428 */
5429 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5430 path = cgroup_path(cgrp, buf, PATH_MAX);
5431 if (!path) {
5432 retval = -ENAMETOOLONG;
5433 goto out_unlock;
5434 }
5435 } else {
5436 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005437 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005438
Tejun Heoe61734c2014-02-12 09:29:50 -05005439 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005440
5441 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5442 seq_puts(m, " (deleted)\n");
5443 else
5444 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005445 }
5446
Zefan Li006f4ac2014-09-18 16:03:15 +08005447 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005448out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005449 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005450 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005451 kfree(buf);
5452out:
5453 return retval;
5454}
5455
Paul Menagea4243162007-10-18 23:39:35 -07005456/* Display information about each subsystem and each hierarchy */
5457static int proc_cgroupstats_show(struct seq_file *m, void *v)
5458{
Tejun Heo30159ec2013-06-25 11:53:37 -07005459 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005460 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005461
Paul Menage8bab8dd2008-04-04 14:29:57 -07005462 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005463 /*
5464 * ideally we don't want subsystems moving around while we do this.
5465 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5466 * subsys/hierarchy state.
5467 */
Paul Menagea4243162007-10-18 23:39:35 -07005468 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005469
5470 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005471 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005472 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005473 atomic_read(&ss->root->nr_cgrps),
5474 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005475
Paul Menagea4243162007-10-18 23:39:35 -07005476 mutex_unlock(&cgroup_mutex);
5477 return 0;
5478}
5479
5480static int cgroupstats_open(struct inode *inode, struct file *file)
5481{
Al Viro9dce07f2008-03-29 03:07:28 +00005482 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005483}
5484
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005485static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005486 .open = cgroupstats_open,
5487 .read = seq_read,
5488 .llseek = seq_lseek,
5489 .release = single_release,
5490};
5491
Aleksa Sarai7e476822015-06-09 21:32:09 +10005492static void **subsys_canfork_priv_p(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5493{
5494 if (CGROUP_CANFORK_START <= i && i < CGROUP_CANFORK_END)
5495 return &ss_priv[i - CGROUP_CANFORK_START];
5496 return NULL;
5497}
5498
5499static void *subsys_canfork_priv(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5500{
5501 void **private = subsys_canfork_priv_p(ss_priv, i);
5502 return private ? *private : NULL;
5503}
5504
Paul Menageb4f48b62007-10-18 23:39:33 -07005505/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005506 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005507 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005508 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005509 * A task is associated with the init_css_set until cgroup_post_fork()
5510 * attaches it to the parent's css_set. Empty cg_list indicates that
5511 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005512 */
5513void cgroup_fork(struct task_struct *child)
5514{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005515 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005516 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005517}
5518
5519/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005520 * cgroup_can_fork - called on a new task before the process is exposed
5521 * @child: the task in question.
5522 *
5523 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5524 * returns an error, the fork aborts with that error code. This allows for
5525 * a cgroup subsystem to conditionally allow or deny new forks.
5526 */
5527int cgroup_can_fork(struct task_struct *child,
5528 void *ss_priv[CGROUP_CANFORK_COUNT])
5529{
5530 struct cgroup_subsys *ss;
5531 int i, j, ret;
5532
5533 for_each_subsys_which(ss, i, &have_canfork_callback) {
5534 ret = ss->can_fork(child, subsys_canfork_priv_p(ss_priv, i));
5535 if (ret)
5536 goto out_revert;
5537 }
5538
5539 return 0;
5540
5541out_revert:
5542 for_each_subsys(ss, j) {
5543 if (j >= i)
5544 break;
5545 if (ss->cancel_fork)
5546 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, j));
5547 }
5548
5549 return ret;
5550}
5551
5552/**
5553 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5554 * @child: the task in question
5555 *
5556 * This calls the cancel_fork() callbacks if a fork failed *after*
5557 * cgroup_can_fork() succeded.
5558 */
5559void cgroup_cancel_fork(struct task_struct *child,
5560 void *ss_priv[CGROUP_CANFORK_COUNT])
5561{
5562 struct cgroup_subsys *ss;
5563 int i;
5564
5565 for_each_subsys(ss, i)
5566 if (ss->cancel_fork)
5567 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, i));
5568}
5569
5570/**
Li Zefana043e3b2008-02-23 15:24:09 -08005571 * cgroup_post_fork - called on a new task after adding it to the task list
5572 * @child: the task in question
5573 *
Tejun Heo5edee612012-10-16 15:03:14 -07005574 * Adds the task to the list running through its css_set if necessary and
5575 * call the subsystem fork() callbacks. Has to be after the task is
5576 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005577 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005578 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005579 */
Aleksa Sarai7e476822015-06-09 21:32:09 +10005580void cgroup_post_fork(struct task_struct *child,
5581 void *old_ss_priv[CGROUP_CANFORK_COUNT])
Paul Menage817929e2007-10-18 23:39:36 -07005582{
Tejun Heo30159ec2013-06-25 11:53:37 -07005583 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005584 int i;
5585
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005586 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005587 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005588 * function sets use_task_css_set_links before grabbing
5589 * tasklist_lock and we just went through tasklist_lock to add
5590 * @child, it's guaranteed that either we see the set
5591 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5592 * @child during its iteration.
5593 *
5594 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005595 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005596 * association is stable, and, on completion of the parent's
5597 * migration, @child is visible in the source of migration or
5598 * already in the destination cgroup. This guarantee is necessary
5599 * when implementing operations which need to migrate all tasks of
5600 * a cgroup to another.
5601 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005602 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005603 * will remain in init_css_set. This is safe because all tasks are
5604 * in the init_css_set before cg_links is enabled and there's no
5605 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005606 */
Paul Menage817929e2007-10-18 23:39:36 -07005607 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005608 struct css_set *cset;
5609
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005610 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005611 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005612 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005613 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005614 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005615 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005616 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005617 }
Tejun Heo5edee612012-10-16 15:03:14 -07005618
5619 /*
5620 * Call ss->fork(). This must happen after @child is linked on
5621 * css_set; otherwise, @child might change state between ->fork()
5622 * and addition to css_set.
5623 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005624 for_each_subsys_which(ss, i, &have_fork_callback)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005625 ss->fork(child, subsys_canfork_priv(old_ss_priv, i));
Paul Menage817929e2007-10-18 23:39:36 -07005626}
Tejun Heo5edee612012-10-16 15:03:14 -07005627
Paul Menage817929e2007-10-18 23:39:36 -07005628/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005629 * cgroup_exit - detach cgroup from exiting task
5630 * @tsk: pointer to task_struct of exiting process
5631 *
5632 * Description: Detach cgroup from @tsk and release it.
5633 *
5634 * Note that cgroups marked notify_on_release force every task in
5635 * them to take the global cgroup_mutex mutex when exiting.
5636 * This could impact scaling on very large systems. Be reluctant to
5637 * use notify_on_release cgroups where very high task exit scaling
5638 * is required on large systems.
5639 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005640 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5641 * call cgroup_exit() while the task is still competent to handle
5642 * notify_on_release(), then leave the task attached to the root cgroup in
5643 * each hierarchy for the remainder of its exit. No need to bother with
5644 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005645 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005646 */
Li Zefan1ec41832014-03-28 15:22:19 +08005647void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005648{
Tejun Heo30159ec2013-06-25 11:53:37 -07005649 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005650 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005651 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005652
5653 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005654 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005655 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005656 */
Tejun Heo0de09422015-10-15 16:41:49 -04005657 cset = task_css_set(tsk);
5658
Paul Menage817929e2007-10-18 23:39:36 -07005659 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005660 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005661 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005662 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005663 } else {
5664 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005665 }
5666
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005667 /* see cgroup_post_fork() for details */
Tejun Heo2e91fa72015-10-15 16:41:53 -04005668 for_each_subsys_which(ss, i, &have_exit_callback)
5669 ss->exit(tsk);
5670}
Tejun Heo30159ec2013-06-25 11:53:37 -07005671
Tejun Heo2e91fa72015-10-15 16:41:53 -04005672void cgroup_free(struct task_struct *task)
5673{
5674 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005675 struct cgroup_subsys *ss;
5676 int ssid;
5677
5678 for_each_subsys_which(ss, ssid, &have_free_callback)
5679 ss->free(task);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005680
Tejun Heo2e91fa72015-10-15 16:41:53 -04005681 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005682}
Paul Menage697f4162007-10-18 23:39:34 -07005683
Paul Menagebd89aab2007-10-18 23:40:44 -07005684static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005685{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005686 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005687 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5688 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005689}
5690
Paul Menage81a6a5c2007-10-18 23:39:38 -07005691/*
5692 * Notify userspace when a cgroup is released, by running the
5693 * configured release agent with the name of the cgroup (path
5694 * relative to the root of cgroup file system) as the argument.
5695 *
5696 * Most likely, this user command will try to rmdir this cgroup.
5697 *
5698 * This races with the possibility that some other task will be
5699 * attached to this cgroup before it is removed, or that some other
5700 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5701 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5702 * unused, and this cgroup will be reprieved from its death sentence,
5703 * to continue to serve a useful existence. Next time it's released,
5704 * we will get notified again, if it still has 'notify_on_release' set.
5705 *
5706 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5707 * means only wait until the task is successfully execve()'d. The
5708 * separate release agent task is forked by call_usermodehelper(),
5709 * then control in this thread returns here, without waiting for the
5710 * release agent task. We don't bother to wait because the caller of
5711 * this routine has no use for the exit status of the release agent
5712 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005713 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005714static void cgroup_release_agent(struct work_struct *work)
5715{
Zefan Li971ff492014-09-18 16:06:19 +08005716 struct cgroup *cgrp =
5717 container_of(work, struct cgroup, release_agent_work);
5718 char *pathbuf = NULL, *agentbuf = NULL, *path;
5719 char *argv[3], *envp[3];
5720
Paul Menage81a6a5c2007-10-18 23:39:38 -07005721 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005722
Zefan Li971ff492014-09-18 16:06:19 +08005723 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5724 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5725 if (!pathbuf || !agentbuf)
5726 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005727
Zefan Li971ff492014-09-18 16:06:19 +08005728 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5729 if (!path)
5730 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005731
Zefan Li971ff492014-09-18 16:06:19 +08005732 argv[0] = agentbuf;
5733 argv[1] = path;
5734 argv[2] = NULL;
5735
5736 /* minimal command environment */
5737 envp[0] = "HOME=/";
5738 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5739 envp[2] = NULL;
5740
Paul Menage81a6a5c2007-10-18 23:39:38 -07005741 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005742 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005743 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005744out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005745 mutex_unlock(&cgroup_mutex);
5746out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005747 kfree(agentbuf);
5748 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005749}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005750
5751static int __init cgroup_disable(char *str)
5752{
Tejun Heo30159ec2013-06-25 11:53:37 -07005753 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005754 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005755 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005756
5757 while ((token = strsep(&str, ",")) != NULL) {
5758 if (!*token)
5759 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005760
Tejun Heo3ed80a62014-02-08 10:36:58 -05005761 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005762 if (strcmp(token, ss->name) &&
5763 strcmp(token, ss->legacy_name))
5764 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005765 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005766 }
5767 }
5768 return 1;
5769}
5770__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005771
Tejun Heob77d7b62013-08-13 11:01:54 -04005772/**
Tejun Heoec903c02014-05-13 12:11:01 -04005773 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005774 * @dentry: directory dentry of interest
5775 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005776 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005777 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5778 * to get the corresponding css and return it. If such css doesn't exist
5779 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005780 */
Tejun Heoec903c02014-05-13 12:11:01 -04005781struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5782 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005783{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005784 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5785 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005786 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005787
Tejun Heo35cf0832013-08-26 18:40:56 -04005788 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005789 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5790 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005791 return ERR_PTR(-EBADF);
5792
Tejun Heo5a17f542014-02-11 11:52:47 -05005793 rcu_read_lock();
5794
Tejun Heo2bd59d42014-02-11 11:52:49 -05005795 /*
5796 * This path doesn't originate from kernfs and @kn could already
5797 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005798 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005799 */
5800 cgrp = rcu_dereference(kn->priv);
5801 if (cgrp)
5802 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005803
Tejun Heoec903c02014-05-13 12:11:01 -04005804 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005805 css = ERR_PTR(-ENOENT);
5806
5807 rcu_read_unlock();
5808 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005809}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005810
Li Zefan1cb650b2013-08-19 10:05:24 +08005811/**
5812 * css_from_id - lookup css by id
5813 * @id: the cgroup id
5814 * @ss: cgroup subsys to be looked into
5815 *
5816 * Returns the css if there's valid one with @id, otherwise returns NULL.
5817 * Should be called under rcu_read_lock().
5818 */
5819struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5820{
Tejun Heo6fa49182014-05-04 15:09:13 -04005821 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005822 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005823}
5824
Paul Menagefe693432009-09-23 15:56:20 -07005825#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005826static struct cgroup_subsys_state *
5827debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005828{
5829 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5830
5831 if (!css)
5832 return ERR_PTR(-ENOMEM);
5833
5834 return css;
5835}
5836
Tejun Heoeb954192013-08-08 20:11:23 -04005837static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005838{
Tejun Heoeb954192013-08-08 20:11:23 -04005839 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005840}
5841
Tejun Heo182446d2013-08-08 20:11:24 -04005842static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5843 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005844{
Tejun Heo182446d2013-08-08 20:11:24 -04005845 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005846}
5847
Tejun Heo182446d2013-08-08 20:11:24 -04005848static u64 current_css_set_read(struct cgroup_subsys_state *css,
5849 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005850{
5851 return (u64)(unsigned long)current->cgroups;
5852}
5853
Tejun Heo182446d2013-08-08 20:11:24 -04005854static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005855 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005856{
5857 u64 count;
5858
5859 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005860 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005861 rcu_read_unlock();
5862 return count;
5863}
5864
Tejun Heo2da8ca82013-12-05 12:28:04 -05005865static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005866{
Tejun Heo69d02062013-06-12 21:04:50 -07005867 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005868 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005869 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005870
Tejun Heoe61734c2014-02-12 09:29:50 -05005871 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5872 if (!name_buf)
5873 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005874
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005875 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005876 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005877 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005878 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005879 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005880
Tejun Heoa2dd4242014-03-19 10:23:55 -04005881 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005882 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005883 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005884 }
5885 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005886 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005887 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005888 return 0;
5889}
5890
5891#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005892static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005893{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005894 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005895 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005896
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005897 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005898 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005899 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005900 struct task_struct *task;
5901 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005902
Tejun Heo5abb8852013-06-12 21:04:49 -07005903 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005904
Tejun Heo5abb8852013-06-12 21:04:49 -07005905 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005906 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5907 goto overflow;
5908 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005909 }
Tejun Heoc7561122014-02-25 10:04:01 -05005910
5911 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5912 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5913 goto overflow;
5914 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5915 }
5916 continue;
5917 overflow:
5918 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005919 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005920 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005921 return 0;
5922}
5923
Tejun Heo182446d2013-08-08 20:11:24 -04005924static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005925{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005926 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08005927 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005928}
5929
5930static struct cftype debug_files[] = {
5931 {
Paul Menagefe693432009-09-23 15:56:20 -07005932 .name = "taskcount",
5933 .read_u64 = debug_taskcount_read,
5934 },
5935
5936 {
5937 .name = "current_css_set",
5938 .read_u64 = current_css_set_read,
5939 },
5940
5941 {
5942 .name = "current_css_set_refcount",
5943 .read_u64 = current_css_set_refcount_read,
5944 },
5945
5946 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005947 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005948 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005949 },
5950
5951 {
5952 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005953 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005954 },
5955
5956 {
Paul Menagefe693432009-09-23 15:56:20 -07005957 .name = "releasable",
5958 .read_u64 = releasable_read,
5959 },
Paul Menagefe693432009-09-23 15:56:20 -07005960
Tejun Heo4baf6e32012-04-01 12:09:55 -07005961 { } /* terminate */
5962};
Paul Menagefe693432009-09-23 15:56:20 -07005963
Tejun Heo073219e2014-02-08 10:36:58 -05005964struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005965 .css_alloc = debug_css_alloc,
5966 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005967 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005968};
5969#endif /* CONFIG_CGROUP_DEBUG */