blob: af0886262f58d2dea076e06195dcef5f082787e3 [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 Heo67e9c742015-11-16 11:13:34 -0500214static struct file_system_type cgroup2_fs_type;
Tejun Heoa14c6872014-07-15 11:05:09 -0400215static struct cftype cgroup_dfl_base_files[];
216static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700217
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400218static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000219 unsigned long ss_mask);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400220static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400222static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
223 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400224static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400225static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400226static int cgroup_addrm_files(struct cgroup_subsys_state *css,
227 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400228 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800229
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400230/**
231 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
232 * @ssid: subsys ID of interest
233 *
234 * cgroup_subsys_enabled() can only be used with literal subsys names which
235 * is fine for individual subsystems but unsuitable for cgroup core. This
236 * is slower static_key_enabled() based test indexed by @ssid.
237 */
238static bool cgroup_ssid_enabled(int ssid)
239{
240 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
241}
242
Tejun Heo9e10a132015-09-18 11:56:28 -0400243/**
244 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
245 * @cgrp: the cgroup of interest
246 *
247 * The default hierarchy is the v2 interface of cgroup and this function
248 * can be used to test whether a cgroup is on the default hierarchy for
249 * cases where a subsystem should behave differnetly depending on the
250 * interface version.
251 *
252 * The set of behaviors which change on the default hierarchy are still
253 * being determined and the mount option is prefixed with __DEVEL__.
254 *
255 * List of changed behaviors:
256 *
257 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
258 * and "name" are disallowed.
259 *
260 * - When mounting an existing superblock, mount options should match.
261 *
262 * - Remount is disallowed.
263 *
264 * - rename(2) is disallowed.
265 *
266 * - "tasks" is removed. Everything should be at process granularity. Use
267 * "cgroup.procs" instead.
268 *
269 * - "cgroup.procs" is not sorted. pids will be unique unless they got
270 * recycled inbetween reads.
271 *
272 * - "release_agent" and "notify_on_release" are removed. Replacement
273 * notification mechanism will be implemented.
274 *
275 * - "cgroup.clone_children" is removed.
276 *
277 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
278 * and its descendants contain no task; otherwise, 1. The file also
279 * generates kernfs notification which can be monitored through poll and
280 * [di]notify when the value of the file changes.
281 *
282 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
283 * take masks of ancestors with non-empty cpus/mems, instead of being
284 * moved to an ancestor.
285 *
286 * - cpuset: a task can be moved into an empty cpuset, and again it takes
287 * masks of ancestors.
288 *
289 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
290 * is not created.
291 *
292 * - blkcg: blk-throttle becomes properly hierarchical.
293 *
294 * - debug: disallowed on the default hierarchy.
295 */
296static bool cgroup_on_dfl(const struct cgroup *cgrp)
297{
298 return cgrp->root == &cgrp_dfl_root;
299}
300
Tejun Heo6fa49182014-05-04 15:09:13 -0400301/* IDR wrappers which synchronize using cgroup_idr_lock */
302static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
303 gfp_t gfp_mask)
304{
305 int ret;
306
307 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400308 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800309 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400310 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400311 idr_preload_end();
312 return ret;
313}
314
315static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
316{
317 void *ret;
318
Tejun Heo54504e92014-05-13 12:10:59 -0400319 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400320 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400321 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400322 return ret;
323}
324
325static void cgroup_idr_remove(struct idr *idr, int id)
326{
Tejun Heo54504e92014-05-13 12:10:59 -0400327 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400328 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400329 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400330}
331
Tejun Heod51f39b2014-05-16 13:22:48 -0400332static struct cgroup *cgroup_parent(struct cgroup *cgrp)
333{
334 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
335
336 if (parent_css)
337 return container_of(parent_css, struct cgroup, self);
338 return NULL;
339}
340
Tejun Heo95109b62013-08-08 20:11:27 -0400341/**
342 * cgroup_css - obtain a cgroup's css for the specified subsystem
343 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400344 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400345 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400346 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
347 * function must be called either under cgroup_mutex or rcu_read_lock() and
348 * the caller is responsible for pinning the returned css if it wants to
349 * keep accessing it outside the said locks. This function may return
350 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400351 */
352static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400353 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400354{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400355 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500356 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500357 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400358 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400359 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400360}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700361
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400362/**
363 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
364 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400365 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400366 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400367 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400368 * as the matching css of the nearest ancestor including self which has @ss
369 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
370 * function is guaranteed to return non-NULL css.
371 */
372static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
373 struct cgroup_subsys *ss)
374{
375 lockdep_assert_held(&cgroup_mutex);
376
377 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400378 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400379
380 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
381 return NULL;
382
Tejun Heoeeecbd12014-11-18 02:49:52 -0500383 /*
384 * This function is used while updating css associations and thus
385 * can't test the csses directly. Use ->child_subsys_mask.
386 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400387 while (cgroup_parent(cgrp) &&
388 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
389 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400390
391 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700392}
393
Tejun Heoeeecbd12014-11-18 02:49:52 -0500394/**
395 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
396 * @cgrp: the cgroup of interest
397 * @ss: the subsystem of interest
398 *
399 * Find and get the effective css of @cgrp for @ss. The effective css is
400 * defined as the matching css of the nearest ancestor including self which
401 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
402 * the root css is returned, so this function always returns a valid css.
403 * The returned css must be put using css_put().
404 */
405struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
406 struct cgroup_subsys *ss)
407{
408 struct cgroup_subsys_state *css;
409
410 rcu_read_lock();
411
412 do {
413 css = cgroup_css(cgrp, ss);
414
415 if (css && css_tryget_online(css))
416 goto out_unlock;
417 cgrp = cgroup_parent(cgrp);
418 } while (cgrp);
419
420 css = init_css_set.subsys[ss->id];
421 css_get(css);
422out_unlock:
423 rcu_read_unlock();
424 return css;
425}
426
Paul Menageddbcc7e2007-10-18 23:39:30 -0700427/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700428static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700429{
Tejun Heo184faf32014-05-16 13:22:51 -0400430 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700431}
432
Tejun Heo052c3f32015-10-15 16:41:50 -0400433static void cgroup_get(struct cgroup *cgrp)
434{
435 WARN_ON_ONCE(cgroup_is_dead(cgrp));
436 css_get(&cgrp->self);
437}
438
439static bool cgroup_tryget(struct cgroup *cgrp)
440{
441 return css_tryget(&cgrp->self);
442}
443
444static void cgroup_put(struct cgroup *cgrp)
445{
446 css_put(&cgrp->self);
447}
448
Tejun Heob4168642014-05-13 12:16:21 -0400449struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500450{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500451 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400452 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500453
454 /*
455 * This is open and unprotected implementation of cgroup_css().
456 * seq_css() is only called from a kernfs file operation which has
457 * an active reference on the file. Because all the subsystem
458 * files are drained before a css is disassociated with a cgroup,
459 * the matching css from the cgroup's subsys table is guaranteed to
460 * be and stay valid until the enclosing operation is complete.
461 */
462 if (cft->ss)
463 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
464 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400465 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500466}
Tejun Heob4168642014-05-13 12:16:21 -0400467EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500468
Li Zefan78574cf2013-04-08 19:00:38 -0700469/**
470 * cgroup_is_descendant - test ancestry
471 * @cgrp: the cgroup to be tested
472 * @ancestor: possible ancestor of @cgrp
473 *
474 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
475 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
476 * and @ancestor are accessible.
477 */
478bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
479{
480 while (cgrp) {
481 if (cgrp == ancestor)
482 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400483 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700484 }
485 return false;
486}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700487
Adrian Bunke9685a02008-02-07 00:13:46 -0800488static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700489{
Paul Menagebd89aab2007-10-18 23:40:44 -0700490 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700491}
492
Tejun Heo30159ec2013-06-25 11:53:37 -0700493/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500494 * for_each_css - iterate all css's of a cgroup
495 * @css: the iteration cursor
496 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
497 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700498 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400499 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700500 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500501#define for_each_css(css, ssid, cgrp) \
502 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
503 if (!((css) = rcu_dereference_check( \
504 (cgrp)->subsys[(ssid)], \
505 lockdep_is_held(&cgroup_mutex)))) { } \
506 else
507
508/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400509 * for_each_e_css - iterate all effective css's of a cgroup
510 * @css: the iteration cursor
511 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
512 * @cgrp: the target cgroup to iterate css's of
513 *
514 * Should be called under cgroup_[tree_]mutex.
515 */
516#define for_each_e_css(css, ssid, cgrp) \
517 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
518 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
519 ; \
520 else
521
522/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500523 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700524 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500525 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700526 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500527#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500528 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
529 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700530
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000531/**
532 * for_each_subsys_which - filter for_each_subsys with a bitmask
533 * @ss: the iteration cursor
534 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
535 * @ss_maskp: a pointer to the bitmask
536 *
537 * The block will only run for cases where the ssid-th bit (1 << ssid) of
538 * mask is set to 1.
539 */
540#define for_each_subsys_which(ss, ssid, ss_maskp) \
541 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000542 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000543 else \
544 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
545 if (((ss) = cgroup_subsys[ssid]) && false) \
546 break; \
547 else
548
Tejun Heo985ed672014-03-19 10:23:53 -0400549/* iterate across the hierarchies */
550#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700551 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700552
Tejun Heof8f22e52014-04-23 11:13:16 -0400553/* iterate over child cgrps, lock should be held throughout iteration */
554#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400555 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400556 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400557 cgroup_is_dead(child); })) \
558 ; \
559 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700560
Paul Menage81a6a5c2007-10-18 23:39:38 -0700561static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700562static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700563
Tejun Heo69d02062013-06-12 21:04:50 -0700564/*
565 * A cgroup can be associated with multiple css_sets as different tasks may
566 * belong to different cgroups on different hierarchies. In the other
567 * direction, a css_set is naturally associated with multiple cgroups.
568 * This M:N relationship is represented by the following link structure
569 * which exists for each association and allows traversing the associations
570 * from both sides.
571 */
572struct cgrp_cset_link {
573 /* the cgroup and css_set this link associates */
574 struct cgroup *cgrp;
575 struct css_set *cset;
576
577 /* list of cgrp_cset_links anchored at cgrp->cset_links */
578 struct list_head cset_link;
579
580 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
581 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700582};
583
Tejun Heo172a2c062014-03-19 10:23:53 -0400584/*
585 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700586 * hierarchies being mounted. It contains a pointer to the root state
587 * for each subsystem. Also used to anchor the list of css_sets. Not
588 * reference-counted, to improve performance when child cgroups
589 * haven't been created.
590 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400591struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400592 .refcount = ATOMIC_INIT(1),
593 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
594 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
595 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
596 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
597 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400598 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400599};
Paul Menage817929e2007-10-18 23:39:36 -0700600
Tejun Heo172a2c062014-03-19 10:23:53 -0400601static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700602
Tejun Heo842b5972014-04-25 18:28:02 -0400603/**
Tejun Heo0de09422015-10-15 16:41:49 -0400604 * css_set_populated - does a css_set contain any tasks?
605 * @cset: target css_set
606 */
607static bool css_set_populated(struct css_set *cset)
608{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400609 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400610
611 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
612}
613
614/**
Tejun Heo842b5972014-04-25 18:28:02 -0400615 * cgroup_update_populated - updated populated count of a cgroup
616 * @cgrp: the target cgroup
617 * @populated: inc or dec populated count
618 *
Tejun Heo0de09422015-10-15 16:41:49 -0400619 * One of the css_sets associated with @cgrp is either getting its first
620 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
621 * count is propagated towards root so that a given cgroup's populated_cnt
622 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400623 *
624 * @cgrp's interface file "cgroup.populated" is zero if
625 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
626 * changes from or to zero, userland is notified that the content of the
627 * interface file has changed. This can be used to detect when @cgrp and
628 * its descendants become populated or empty.
629 */
630static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
631{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400632 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400633
634 do {
635 bool trigger;
636
637 if (populated)
638 trigger = !cgrp->populated_cnt++;
639 else
640 trigger = !--cgrp->populated_cnt;
641
642 if (!trigger)
643 break;
644
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400645 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400646 cgroup_file_notify(&cgrp->events_file);
647
Tejun Heod51f39b2014-05-16 13:22:48 -0400648 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400649 } while (cgrp);
650}
651
Tejun Heo0de09422015-10-15 16:41:49 -0400652/**
653 * css_set_update_populated - update populated state of a css_set
654 * @cset: target css_set
655 * @populated: whether @cset is populated or depopulated
656 *
657 * @cset is either getting the first task or losing the last. Update the
658 * ->populated_cnt of all associated cgroups accordingly.
659 */
660static void css_set_update_populated(struct css_set *cset, bool populated)
661{
662 struct cgrp_cset_link *link;
663
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400664 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400665
666 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
667 cgroup_update_populated(link->cgrp, populated);
668}
669
Tejun Heof6d7d042015-10-15 16:41:52 -0400670/**
671 * css_set_move_task - move a task from one css_set to another
672 * @task: task being moved
673 * @from_cset: css_set @task currently belongs to (may be NULL)
674 * @to_cset: new css_set @task is being moved to (may be NULL)
675 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
676 *
677 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
678 * css_set, @from_cset can be NULL. If @task is being disassociated
679 * instead of moved, @to_cset can be NULL.
680 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400681 * This function automatically handles populated_cnt updates and
682 * css_task_iter adjustments but the caller is responsible for managing
683 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400684 */
685static void css_set_move_task(struct task_struct *task,
686 struct css_set *from_cset, struct css_set *to_cset,
687 bool use_mg_tasks)
688{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400689 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400690
691 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400692 struct css_task_iter *it, *pos;
693
Tejun Heof6d7d042015-10-15 16:41:52 -0400694 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400695
696 /*
697 * @task is leaving, advance task iterators which are
698 * pointing to it so that they can resume at the next
699 * position. Advancing an iterator might remove it from
700 * the list, use safe walk. See css_task_iter_advance*()
701 * for details.
702 */
703 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
704 iters_node)
705 if (it->task_pos == &task->cg_list)
706 css_task_iter_advance(it);
707
Tejun Heof6d7d042015-10-15 16:41:52 -0400708 list_del_init(&task->cg_list);
709 if (!css_set_populated(from_cset))
710 css_set_update_populated(from_cset, false);
711 } else {
712 WARN_ON_ONCE(!list_empty(&task->cg_list));
713 }
714
715 if (to_cset) {
716 /*
717 * We are synchronized through cgroup_threadgroup_rwsem
718 * against PF_EXITING setting such that we can't race
719 * against cgroup_exit() changing the css_set to
720 * init_css_set and dropping the old one.
721 */
722 WARN_ON_ONCE(task->flags & PF_EXITING);
723
724 if (!css_set_populated(to_cset))
725 css_set_update_populated(to_cset, true);
726 rcu_assign_pointer(task->cgroups, to_cset);
727 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
728 &to_cset->tasks);
729 }
730}
731
Paul Menage7717f7b2009-09-23 15:56:22 -0700732/*
733 * hash table for cgroup groups. This improves the performance to find
734 * an existing css_set. This hash doesn't (currently) take into
735 * account cgroups in empty hierarchies.
736 */
Li Zefan472b1052008-04-29 01:00:11 -0700737#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800738static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700739
Li Zefan0ac801f2013-01-10 11:49:27 +0800740static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700741{
Li Zefan0ac801f2013-01-10 11:49:27 +0800742 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700743 struct cgroup_subsys *ss;
744 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700745
Tejun Heo30159ec2013-06-25 11:53:37 -0700746 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800747 key += (unsigned long)css[i];
748 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700749
Li Zefan0ac801f2013-01-10 11:49:27 +0800750 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700751}
752
Zefan Lia25eb522014-09-19 16:51:00 +0800753static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700754{
Tejun Heo69d02062013-06-12 21:04:50 -0700755 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400756 struct cgroup_subsys *ss;
757 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700758
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400759 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500760
761 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700762 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700763
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700764 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400765 for_each_subsys(ss, ssid)
766 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700767 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700768 css_set_count--;
769
Tejun Heo69d02062013-06-12 21:04:50 -0700770 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700771 list_del(&link->cset_link);
772 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400773 if (cgroup_parent(link->cgrp))
774 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700775 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700776 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700777
Tejun Heo5abb8852013-06-12 21:04:49 -0700778 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700779}
780
Zefan Lia25eb522014-09-19 16:51:00 +0800781static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500782{
783 /*
784 * Ensure that the refcount doesn't hit zero while any readers
785 * can see it. Similar to atomic_dec_and_lock(), but for an
786 * rwlock
787 */
788 if (atomic_add_unless(&cset->refcount, -1, 1))
789 return;
790
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400791 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800792 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400793 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500794}
795
Paul Menage817929e2007-10-18 23:39:36 -0700796/*
797 * refcounted get/put for css_set objects
798 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700799static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700800{
Tejun Heo5abb8852013-06-12 21:04:49 -0700801 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700802}
803
Tejun Heob326f9d2013-06-24 15:21:48 -0700804/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700805 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700806 * @cset: candidate css_set being tested
807 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700808 * @new_cgrp: cgroup that's being entered by the task
809 * @template: desired set of css pointers in css_set (pre-calculated)
810 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800811 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700812 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
813 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700814static bool compare_css_sets(struct css_set *cset,
815 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700816 struct cgroup *new_cgrp,
817 struct cgroup_subsys_state *template[])
818{
819 struct list_head *l1, *l2;
820
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400821 /*
822 * On the default hierarchy, there can be csets which are
823 * associated with the same set of cgroups but different csses.
824 * Let's first ensure that csses match.
825 */
826 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700827 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700828
829 /*
830 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400831 * different cgroups in hierarchies. As different cgroups may
832 * share the same effective css, this comparison is always
833 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700834 */
Tejun Heo69d02062013-06-12 21:04:50 -0700835 l1 = &cset->cgrp_links;
836 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700837 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700838 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700839 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700840
841 l1 = l1->next;
842 l2 = l2->next;
843 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700844 if (l1 == &cset->cgrp_links) {
845 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700846 break;
847 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700848 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700849 }
850 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700851 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
852 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
853 cgrp1 = link1->cgrp;
854 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700855 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700856 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700857
858 /*
859 * If this hierarchy is the hierarchy of the cgroup
860 * that's changing, then we need to check that this
861 * css_set points to the new cgroup; if it's any other
862 * hierarchy, then this css_set should point to the
863 * same cgroup as the old css_set.
864 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700865 if (cgrp1->root == new_cgrp->root) {
866 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700867 return false;
868 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700869 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700870 return false;
871 }
872 }
873 return true;
874}
875
Tejun Heob326f9d2013-06-24 15:21:48 -0700876/**
877 * find_existing_css_set - init css array and find the matching css_set
878 * @old_cset: the css_set that we're using before the cgroup transition
879 * @cgrp: the cgroup that we're moving into
880 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700881 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700882static struct css_set *find_existing_css_set(struct css_set *old_cset,
883 struct cgroup *cgrp,
884 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700885{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400886 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700887 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700888 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800889 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700890 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700891
Ben Blumaae8aab2010-03-10 15:22:07 -0800892 /*
893 * Build the set of subsystem state objects that we want to see in the
894 * new css_set. while subsystems can change globally, the entries here
895 * won't change, so no need for locking.
896 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700897 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400898 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400899 /*
900 * @ss is in this hierarchy, so we want the
901 * effective css from @cgrp.
902 */
903 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700904 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400905 /*
906 * @ss is not in this hierarchy, so we don't want
907 * to change the css.
908 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700909 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700910 }
911 }
912
Li Zefan0ac801f2013-01-10 11:49:27 +0800913 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700914 hash_for_each_possible(css_set_table, cset, hlist, key) {
915 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700916 continue;
917
918 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700919 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700920 }
Paul Menage817929e2007-10-18 23:39:36 -0700921
922 /* No existing cgroup group matched */
923 return NULL;
924}
925
Tejun Heo69d02062013-06-12 21:04:50 -0700926static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700927{
Tejun Heo69d02062013-06-12 21:04:50 -0700928 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700929
Tejun Heo69d02062013-06-12 21:04:50 -0700930 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
931 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700932 kfree(link);
933 }
934}
935
Tejun Heo69d02062013-06-12 21:04:50 -0700936/**
937 * allocate_cgrp_cset_links - allocate cgrp_cset_links
938 * @count: the number of links to allocate
939 * @tmp_links: list_head the allocated links are put on
940 *
941 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
942 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700943 */
Tejun Heo69d02062013-06-12 21:04:50 -0700944static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700945{
Tejun Heo69d02062013-06-12 21:04:50 -0700946 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700947 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700948
949 INIT_LIST_HEAD(tmp_links);
950
Li Zefan36553432008-07-29 22:33:19 -0700951 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700952 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700953 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700954 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700955 return -ENOMEM;
956 }
Tejun Heo69d02062013-06-12 21:04:50 -0700957 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700958 }
959 return 0;
960}
961
Li Zefanc12f65d2009-01-07 18:07:42 -0800962/**
963 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700964 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700965 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800966 * @cgrp: the destination cgroup
967 */
Tejun Heo69d02062013-06-12 21:04:50 -0700968static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
969 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800970{
Tejun Heo69d02062013-06-12 21:04:50 -0700971 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800972
Tejun Heo69d02062013-06-12 21:04:50 -0700973 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400974
975 if (cgroup_on_dfl(cgrp))
976 cset->dfl_cgrp = cgrp;
977
Tejun Heo69d02062013-06-12 21:04:50 -0700978 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
979 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700980 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400981
Paul Menage7717f7b2009-09-23 15:56:22 -0700982 /*
Tejun Heo389b9c12015-10-15 16:41:51 -0400983 * Always add links to the tail of the lists so that the lists are
984 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -0700985 */
Tejun Heo389b9c12015-10-15 16:41:51 -0400986 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -0700987 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400988
989 if (cgroup_parent(cgrp))
990 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -0800991}
992
Tejun Heob326f9d2013-06-24 15:21:48 -0700993/**
994 * find_css_set - return a new css_set with one cgroup updated
995 * @old_cset: the baseline css_set
996 * @cgrp: the cgroup to be updated
997 *
998 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
999 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001000 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001001static struct css_set *find_css_set(struct css_set *old_cset,
1002 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001003{
Tejun Heob326f9d2013-06-24 15:21:48 -07001004 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001005 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001006 struct list_head tmp_links;
1007 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001008 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001009 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001010 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001011
Tejun Heob326f9d2013-06-24 15:21:48 -07001012 lockdep_assert_held(&cgroup_mutex);
1013
Paul Menage817929e2007-10-18 23:39:36 -07001014 /* First see if we already have a cgroup group that matches
1015 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001016 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001017 cset = find_existing_css_set(old_cset, cgrp, template);
1018 if (cset)
1019 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001020 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001021
Tejun Heo5abb8852013-06-12 21:04:49 -07001022 if (cset)
1023 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001024
Tejun Heof4f4be22013-06-12 21:04:51 -07001025 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001026 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001027 return NULL;
1028
Tejun Heo69d02062013-06-12 21:04:50 -07001029 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001030 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001031 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001032 return NULL;
1033 }
1034
Tejun Heo5abb8852013-06-12 21:04:49 -07001035 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001036 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001037 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001038 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001039 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001040 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001041 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001042 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001043
1044 /* Copy the set of subsystem state objects generated in
1045 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001046 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001047
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001048 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001049 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001050 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001051 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001052
Paul Menage7717f7b2009-09-23 15:56:22 -07001053 if (c->root == cgrp->root)
1054 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001055 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001056 }
Paul Menage817929e2007-10-18 23:39:36 -07001057
Tejun Heo69d02062013-06-12 21:04:50 -07001058 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001059
Paul Menage817929e2007-10-18 23:39:36 -07001060 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001061
Tejun Heo2d8f2432014-04-23 11:13:15 -04001062 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001063 key = css_set_hash(cset->subsys);
1064 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001065
Tejun Heo2d8f2432014-04-23 11:13:15 -04001066 for_each_subsys(ss, ssid)
1067 list_add_tail(&cset->e_cset_node[ssid],
1068 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
1069
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001070 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001071
Tejun Heo5abb8852013-06-12 21:04:49 -07001072 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001073}
1074
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001075static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001076{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001077 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001078
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001079 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001080}
1081
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001082static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001083{
1084 int id;
1085
1086 lockdep_assert_held(&cgroup_mutex);
1087
Tejun Heo985ed672014-03-19 10:23:53 -04001088 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001089 if (id < 0)
1090 return id;
1091
1092 root->hierarchy_id = id;
1093 return 0;
1094}
1095
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001096static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001097{
1098 lockdep_assert_held(&cgroup_mutex);
1099
1100 if (root->hierarchy_id) {
1101 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1102 root->hierarchy_id = 0;
1103 }
1104}
1105
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001106static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001107{
1108 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001109 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001110 WARN_ON_ONCE(root->hierarchy_id);
1111
1112 idr_destroy(&root->cgroup_idr);
1113 kfree(root);
1114 }
1115}
1116
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001117static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001118{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001119 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001120 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001121
Tejun Heo2bd59d42014-02-11 11:52:49 -05001122 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001123
Tejun Heo776f02f2014-02-12 09:29:50 -05001124 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001125 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001126
Tejun Heof2e85d52014-02-11 11:52:49 -05001127 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001128 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001129
1130 /*
1131 * Release all the links from cset_links to this hierarchy's
1132 * root cgroup
1133 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001134 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001135
1136 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1137 list_del(&link->cset_link);
1138 list_del(&link->cgrp_link);
1139 kfree(link);
1140 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001141
1142 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001143
1144 if (!list_empty(&root->root_list)) {
1145 list_del(&root->root_list);
1146 cgroup_root_count--;
1147 }
1148
1149 cgroup_exit_root_id(root);
1150
1151 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001152
Tejun Heo2bd59d42014-02-11 11:52:49 -05001153 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001154 cgroup_free_root(root);
1155}
1156
Tejun Heoceb6a082014-02-25 10:04:02 -05001157/* look up cgroup associated with given css_set on the specified hierarchy */
1158static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001159 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001160{
Paul Menage7717f7b2009-09-23 15:56:22 -07001161 struct cgroup *res = NULL;
1162
Tejun Heo96d365e2014-02-13 06:58:40 -05001163 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001164 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001165
Tejun Heo5abb8852013-06-12 21:04:49 -07001166 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001167 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001168 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001169 struct cgrp_cset_link *link;
1170
1171 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001172 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001173
Paul Menage7717f7b2009-09-23 15:56:22 -07001174 if (c->root == root) {
1175 res = c;
1176 break;
1177 }
1178 }
1179 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001180
Paul Menage7717f7b2009-09-23 15:56:22 -07001181 BUG_ON(!res);
1182 return res;
1183}
1184
1185/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001186 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001187 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001188 */
1189static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001190 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001191{
1192 /*
1193 * No need to lock the task - since we hold cgroup_mutex the
1194 * task can't change groups, so the only thing that can happen
1195 * is that it exits and its css is set back to init_css_set.
1196 */
1197 return cset_cgroup_from_root(task_css_set(task), root);
1198}
1199
1200/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001201 * A task must hold cgroup_mutex to modify cgroups.
1202 *
1203 * Any task can increment and decrement the count field without lock.
1204 * So in general, code holding cgroup_mutex can't rely on the count
1205 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001206 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207 * means that no tasks are currently attached, therefore there is no
1208 * way a task attached to that cgroup can fork (the other way to
1209 * increment the count). So code holding cgroup_mutex can safely
1210 * assume that if the count is zero, it will stay zero. Similarly, if
1211 * a task holds cgroup_mutex on a cgroup with zero count, it
1212 * knows that the cgroup won't be removed, as cgroup_rmdir()
1213 * needs that mutex.
1214 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215 * A cgroup can only be deleted if both its 'count' of using tasks
1216 * is zero, and its list of 'children' cgroups is empty. Since all
1217 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001218 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001219 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001220 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001221 *
1222 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001223 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001224 */
1225
Tejun Heo2bd59d42014-02-11 11:52:49 -05001226static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001227static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001228
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001229static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1230 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001231{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001232 struct cgroup_subsys *ss = cft->ss;
1233
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001234 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1235 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1236 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001237 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1238 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001239 else
1240 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1241 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242}
1243
Tejun Heof2e85d52014-02-11 11:52:49 -05001244/**
1245 * cgroup_file_mode - deduce file mode of a control file
1246 * @cft: the control file in question
1247 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001248 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001249 */
1250static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001251{
Tejun Heof2e85d52014-02-11 11:52:49 -05001252 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001253
Tejun Heof2e85d52014-02-11 11:52:49 -05001254 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1255 mode |= S_IRUGO;
1256
Tejun Heo7dbdb192015-09-18 17:54:23 -04001257 if (cft->write_u64 || cft->write_s64 || cft->write) {
1258 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1259 mode |= S_IWUGO;
1260 else
1261 mode |= S_IWUSR;
1262 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001263
1264 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001265}
1266
Tejun Heoa9746d82014-05-13 12:19:22 -04001267/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001268 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001269 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001270 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001271 *
1272 * On the default hierarchy, a subsystem may request other subsystems to be
1273 * enabled together through its ->depends_on mask. In such cases, more
1274 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1275 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001276 * This function calculates which subsystems need to be enabled if
1277 * @subtree_control is to be applied to @cgrp. The returned mask is always
1278 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001279 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001280static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1281 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001282{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001283 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001284 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001285 struct cgroup_subsys *ss;
1286 int ssid;
1287
1288 lockdep_assert_held(&cgroup_mutex);
1289
Tejun Heo0f060de2014-11-18 02:49:50 -05001290 if (!cgroup_on_dfl(cgrp))
1291 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001292
1293 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001294 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001295
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001296 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1297 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001298
1299 /*
1300 * Mask out subsystems which aren't available. This can
1301 * happen only if some depended-upon subsystems were bound
1302 * to non-default hierarchies.
1303 */
1304 if (parent)
1305 new_ss_mask &= parent->child_subsys_mask;
1306 else
1307 new_ss_mask &= cgrp->root->subsys_mask;
1308
1309 if (new_ss_mask == cur_ss_mask)
1310 break;
1311 cur_ss_mask = new_ss_mask;
1312 }
1313
Tejun Heo0f060de2014-11-18 02:49:50 -05001314 return cur_ss_mask;
1315}
1316
1317/**
1318 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1319 * @cgrp: the target cgroup
1320 *
1321 * Update @cgrp->child_subsys_mask according to the current
1322 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1323 */
1324static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1325{
1326 cgrp->child_subsys_mask =
1327 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001328}
1329
Tejun Heoa9746d82014-05-13 12:19:22 -04001330/**
1331 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1332 * @kn: the kernfs_node being serviced
1333 *
1334 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1335 * the method finishes if locking succeeded. Note that once this function
1336 * returns the cgroup returned by cgroup_kn_lock_live() may become
1337 * inaccessible any time. If the caller intends to continue to access the
1338 * cgroup, it should pin it before invoking this function.
1339 */
1340static void cgroup_kn_unlock(struct kernfs_node *kn)
1341{
1342 struct cgroup *cgrp;
1343
1344 if (kernfs_type(kn) == KERNFS_DIR)
1345 cgrp = kn->priv;
1346 else
1347 cgrp = kn->parent->priv;
1348
1349 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001350
1351 kernfs_unbreak_active_protection(kn);
1352 cgroup_put(cgrp);
1353}
1354
1355/**
1356 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1357 * @kn: the kernfs_node being serviced
1358 *
1359 * This helper is to be used by a cgroup kernfs method currently servicing
1360 * @kn. It breaks the active protection, performs cgroup locking and
1361 * verifies that the associated cgroup is alive. Returns the cgroup if
1362 * alive; otherwise, %NULL. A successful return should be undone by a
1363 * matching cgroup_kn_unlock() invocation.
1364 *
1365 * Any cgroup kernfs method implementation which requires locking the
1366 * associated cgroup should use this helper. It avoids nesting cgroup
1367 * locking under kernfs active protection and allows all kernfs operations
1368 * including self-removal.
1369 */
1370static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1371{
1372 struct cgroup *cgrp;
1373
1374 if (kernfs_type(kn) == KERNFS_DIR)
1375 cgrp = kn->priv;
1376 else
1377 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378
Tejun Heo2bd59d42014-02-11 11:52:49 -05001379 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001380 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001381 * active_ref. cgroup liveliness check alone provides enough
1382 * protection against removal. Ensure @cgrp stays accessible and
1383 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001384 */
Li Zefanaa323622014-09-04 14:43:38 +08001385 if (!cgroup_tryget(cgrp))
1386 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001387 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388
Tejun Heoa9746d82014-05-13 12:19:22 -04001389 mutex_lock(&cgroup_mutex);
1390
1391 if (!cgroup_is_dead(cgrp))
1392 return cgrp;
1393
1394 cgroup_kn_unlock(kn);
1395 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396}
1397
Li Zefan2739d3c2013-01-21 18:18:33 +08001398static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001400 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401
Tejun Heo01f64742014-05-13 12:19:23 -04001402 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001403
1404 if (cft->file_offset) {
1405 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1406 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1407
1408 spin_lock_irq(&cgroup_file_kn_lock);
1409 cfile->kn = NULL;
1410 spin_unlock_irq(&cgroup_file_kn_lock);
1411 }
1412
Tejun Heo2bd59d42014-02-11 11:52:49 -05001413 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001414}
1415
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001416/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001417 * css_clear_dir - remove subsys files in a cgroup directory
1418 * @css: taget css
1419 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001420 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001421static void css_clear_dir(struct cgroup_subsys_state *css,
1422 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001423{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001424 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1425 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001426
Tejun Heo4df8dc92015-09-18 17:54:23 -04001427 list_for_each_entry(cfts, &css->ss->cfts, node)
1428 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429}
1430
Tejun Heoccdca212015-09-18 17:54:23 -04001431/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001432 * css_populate_dir - create subsys files in a cgroup directory
1433 * @css: target css
1434 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001435 *
1436 * On failure, no file is added.
1437 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001438static int css_populate_dir(struct cgroup_subsys_state *css,
1439 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001440{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001441 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1442 struct cftype *cfts, *failed_cfts;
1443 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001444
Tejun Heo4df8dc92015-09-18 17:54:23 -04001445 if (!css->ss) {
1446 if (cgroup_on_dfl(cgrp))
1447 cfts = cgroup_dfl_base_files;
1448 else
1449 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001450
Tejun Heo4df8dc92015-09-18 17:54:23 -04001451 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1452 }
Tejun Heoccdca212015-09-18 17:54:23 -04001453
Tejun Heo4df8dc92015-09-18 17:54:23 -04001454 list_for_each_entry(cfts, &css->ss->cfts, node) {
1455 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1456 if (ret < 0) {
1457 failed_cfts = cfts;
1458 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001459 }
1460 }
1461 return 0;
1462err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001463 list_for_each_entry(cfts, &css->ss->cfts, node) {
1464 if (cfts == failed_cfts)
1465 break;
1466 cgroup_addrm_files(css, cgrp, cfts, false);
1467 }
Tejun Heoccdca212015-09-18 17:54:23 -04001468 return ret;
1469}
1470
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001471static int rebind_subsystems(struct cgroup_root *dst_root,
1472 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473{
Tejun Heo1ada4832015-09-18 17:54:23 -04001474 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001475 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001476 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001477 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478
Tejun Heoace2bee2014-02-11 11:52:47 -05001479 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001480
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001481 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001482 /* if @ss has non-root csses attached to it, can't move */
1483 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001484 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485
Tejun Heo5df36032014-03-19 10:23:54 -04001486 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001487 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001488 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001489 }
1490
Tejun Heo5533e012014-05-14 19:33:07 -04001491 /* skip creating root files on dfl_root for inhibited subsystems */
1492 tmp_ss_mask = ss_mask;
1493 if (dst_root == &cgrp_dfl_root)
1494 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1495
Tejun Heo4df8dc92015-09-18 17:54:23 -04001496 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
1497 struct cgroup *scgrp = &ss->root->cgrp;
1498 int tssid;
1499
1500 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1501 if (!ret)
1502 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503
Tejun Heoa2dd4242014-03-19 10:23:55 -04001504 /*
1505 * Rebinding back to the default root is not allowed to
1506 * fail. Using both default and non-default roots should
1507 * be rare. Moving subsystems back and forth even more so.
1508 * Just warn about it and continue.
1509 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001510 if (dst_root == &cgrp_dfl_root) {
1511 if (cgrp_dfl_root_visible) {
1512 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
1513 ret, ss_mask);
1514 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1515 }
1516 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001517 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001518
1519 for_each_subsys_which(ss, tssid, &tmp_ss_mask) {
1520 if (tssid == ssid)
1521 break;
1522 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
1523 }
1524 return ret;
Tejun Heo5df36032014-03-19 10:23:54 -04001525 }
Tejun Heo31261212013-06-28 17:07:30 -07001526
1527 /*
1528 * Nothing can fail from this point on. Remove files for the
1529 * removed subsystems and rebind each subsystem.
1530 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001531 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001532 struct cgroup_root *src_root = ss->root;
1533 struct cgroup *scgrp = &src_root->cgrp;
1534 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001535 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001536
Tejun Heo1ada4832015-09-18 17:54:23 -04001537 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001538
Tejun Heo4df8dc92015-09-18 17:54:23 -04001539 css_clear_dir(css, NULL);
1540
Tejun Heo1ada4832015-09-18 17:54:23 -04001541 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1542 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001543 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001544 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001545
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001546 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001547 hash_for_each(css_set_table, i, cset, hlist)
1548 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001549 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001550 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001551
Tejun Heof392e512014-04-23 11:13:14 -04001552 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001553 scgrp->subtree_control &= ~(1 << ssid);
1554 cgroup_refresh_child_subsys_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001555
Tejun Heobd53d612014-04-23 11:13:16 -04001556 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001557 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001558 if (dst_root == &cgrp_dfl_root) {
1559 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1560 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001561 dcgrp->subtree_control |= 1 << ssid;
1562 cgroup_refresh_child_subsys_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001563 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001564 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001565
Tejun Heo5df36032014-03-19 10:23:54 -04001566 if (ss->bind)
1567 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569
Tejun Heo1ada4832015-09-18 17:54:23 -04001570 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 return 0;
1572}
1573
Tejun Heo2bd59d42014-02-11 11:52:49 -05001574static int cgroup_show_options(struct seq_file *seq,
1575 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001577 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001579 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580
Tejun Heod98817d2015-08-18 13:58:16 -07001581 if (root != &cgrp_dfl_root)
1582 for_each_subsys(ss, ssid)
1583 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001584 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001585 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001587 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001588 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001589
1590 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001591 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001592 seq_show_option(seq, "release_agent",
1593 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001594 spin_unlock(&release_agent_path_lock);
1595
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001596 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001597 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001599 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 return 0;
1601}
1602
1603struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001604 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001605 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001606 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001607 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001609 /* User explicitly requested empty subsystem */
1610 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611};
1612
Ben Blumcf5d5942010-03-10 15:22:09 -08001613static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001615 char *token, *o = data;
1616 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001617 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001618 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001619 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001620 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001621
1622#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001623 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001624#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625
Paul Menagec6d57f32009-09-23 15:56:19 -07001626 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627
1628 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001629 nr_opts++;
1630
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 if (!*token)
1632 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001633 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001634 /* Explicitly have no subsystems */
1635 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001636 continue;
1637 }
1638 if (!strcmp(token, "all")) {
1639 /* Mutually exclusive option 'all' + subsystem name */
1640 if (one_ss)
1641 return -EINVAL;
1642 all_ss = true;
1643 continue;
1644 }
1645 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001646 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001647 continue;
1648 }
1649 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001650 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001651 continue;
1652 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001653 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001654 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001655 continue;
1656 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001657 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001658 /* Specifying two release agents is forbidden */
1659 if (opts->release_agent)
1660 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001661 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001662 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001663 if (!opts->release_agent)
1664 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001665 continue;
1666 }
1667 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001668 const char *name = token + 5;
1669 /* Can't specify an empty name */
1670 if (!strlen(name))
1671 return -EINVAL;
1672 /* Must match [\w.-]+ */
1673 for (i = 0; i < strlen(name); i++) {
1674 char c = name[i];
1675 if (isalnum(c))
1676 continue;
1677 if ((c == '.') || (c == '-') || (c == '_'))
1678 continue;
1679 return -EINVAL;
1680 }
1681 /* Specifying two names is forbidden */
1682 if (opts->name)
1683 return -EINVAL;
1684 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001685 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001686 GFP_KERNEL);
1687 if (!opts->name)
1688 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001689
1690 continue;
1691 }
1692
Tejun Heo30159ec2013-06-25 11:53:37 -07001693 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001694 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001695 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001696 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001697 continue;
1698
1699 /* Mutually exclusive option 'all' + subsystem name */
1700 if (all_ss)
1701 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001702 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001703 one_ss = true;
1704
1705 break;
1706 }
1707 if (i == CGROUP_SUBSYS_COUNT)
1708 return -ENOENT;
1709 }
1710
Li Zefanf9ab5b52009-06-17 16:26:33 -07001711 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001712 * If the 'all' option was specified select all the subsystems,
1713 * otherwise if 'none', 'name=' and a subsystem name options were
1714 * not specified, let's default to 'all'
1715 */
1716 if (all_ss || (!one_ss && !opts->none && !opts->name))
1717 for_each_subsys(ss, i)
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001718 if (cgroup_ssid_enabled(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001719 opts->subsys_mask |= (1 << i);
1720
1721 /*
1722 * We either have to specify by name or by subsystems. (So all
1723 * empty hierarchies must have a name).
1724 */
1725 if (!opts->subsys_mask && !opts->name)
1726 return -EINVAL;
1727
1728 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001729 * Option noprefix was introduced just for backward compatibility
1730 * with the old cpuset, so we allow noprefix only if mounting just
1731 * the cpuset subsystem.
1732 */
Tejun Heo93438622013-04-14 20:15:25 -07001733 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001734 return -EINVAL;
1735
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001736 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001737 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001738 return -EINVAL;
1739
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740 return 0;
1741}
1742
Tejun Heo2bd59d42014-02-11 11:52:49 -05001743static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744{
1745 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001746 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001748 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749
Tejun Heoaa6ec292014-07-09 10:08:08 -04001750 if (root == &cgrp_dfl_root) {
1751 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001752 return -EINVAL;
1753 }
1754
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755 mutex_lock(&cgroup_mutex);
1756
1757 /* See what subsystems are wanted */
1758 ret = parse_cgroupfs_options(data, &opts);
1759 if (ret)
1760 goto out_unlock;
1761
Tejun Heof392e512014-04-23 11:13:14 -04001762 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001763 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001764 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001765
Tejun Heof392e512014-04-23 11:13:14 -04001766 added_mask = opts.subsys_mask & ~root->subsys_mask;
1767 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001768
Ben Blumcf5d5942010-03-10 15:22:09 -08001769 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001770 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001771 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001772 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001773 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001775 goto out_unlock;
1776 }
1777
Tejun Heof172e672013-06-28 17:07:30 -07001778 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001779 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001780 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001781 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001782 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783
Tejun Heo5df36032014-03-19 10:23:54 -04001784 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001785 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001788 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001789
Tejun Heo69e943b2014-02-08 10:36:58 -05001790 if (opts.release_agent) {
1791 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001792 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001793 spin_unlock(&release_agent_path_lock);
1794 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001796 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001797 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 return ret;
1800}
1801
Tejun Heoafeb0f92014-02-13 06:58:39 -05001802/*
1803 * To reduce the fork() overhead for systems that are not actually using
1804 * their cgroups capability, we don't maintain the lists running through
1805 * each css_set to its tasks until we see the list actually used - in other
1806 * words after the first mount.
1807 */
1808static bool use_task_css_set_links __read_mostly;
1809
1810static void cgroup_enable_task_cg_lists(void)
1811{
1812 struct task_struct *p, *g;
1813
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001814 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001815
1816 if (use_task_css_set_links)
1817 goto out_unlock;
1818
1819 use_task_css_set_links = true;
1820
1821 /*
1822 * We need tasklist_lock because RCU is not safe against
1823 * while_each_thread(). Besides, a forking task that has passed
1824 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1825 * is not guaranteed to have its child immediately visible in the
1826 * tasklist if we walk through it with RCU.
1827 */
1828 read_lock(&tasklist_lock);
1829 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001830 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1831 task_css_set(p) != &init_css_set);
1832
1833 /*
1834 * We should check if the process is exiting, otherwise
1835 * it will race with cgroup_exit() in that the list
1836 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001837 * Do it while holding siglock so that we don't end up
1838 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001839 */
Tejun Heof153ad12014-02-25 09:56:49 -05001840 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001841 if (!(p->flags & PF_EXITING)) {
1842 struct css_set *cset = task_css_set(p);
1843
Tejun Heo0de09422015-10-15 16:41:49 -04001844 if (!css_set_populated(cset))
1845 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001846 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001847 get_css_set(cset);
1848 }
Tejun Heof153ad12014-02-25 09:56:49 -05001849 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001850 } while_each_thread(g, p);
1851 read_unlock(&tasklist_lock);
1852out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001853 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001854}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001855
Paul Menagecc31edc2008-10-18 20:28:04 -07001856static void init_cgroup_housekeeping(struct cgroup *cgrp)
1857{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001858 struct cgroup_subsys *ss;
1859 int ssid;
1860
Tejun Heod5c419b2014-05-16 13:22:48 -04001861 INIT_LIST_HEAD(&cgrp->self.sibling);
1862 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001863 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001864 INIT_LIST_HEAD(&cgrp->pidlists);
1865 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001866 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001867 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001868
1869 for_each_subsys(ss, ssid)
1870 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001871
1872 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001873 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001874}
Paul Menagec6d57f32009-09-23 15:56:19 -07001875
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001876static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001877 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001878{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001879 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001880
Paul Menageddbcc7e2007-10-18 23:39:30 -07001881 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001882 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001883 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001884 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001885 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001886
Paul Menagec6d57f32009-09-23 15:56:19 -07001887 root->flags = opts->flags;
1888 if (opts->release_agent)
1889 strcpy(root->release_agent_path, opts->release_agent);
1890 if (opts->name)
1891 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001892 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001893 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001894}
1895
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001896static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001897{
Tejun Heod427dfe2014-02-11 11:52:48 -05001898 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001899 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001900 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001901 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001902
Tejun Heod427dfe2014-02-11 11:52:48 -05001903 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001904
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001905 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001906 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001907 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001908 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001909
Tejun Heo2aad2a82014-09-24 13:31:50 -04001910 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1911 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001912 if (ret)
1913 goto out;
1914
Tejun Heod427dfe2014-02-11 11:52:48 -05001915 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001916 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001917 * but that's OK - it can only be increased by someone holding
1918 * cgroup_lock, and that's us. The worst that can happen is that we
1919 * have some link structures left over
1920 */
1921 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001922 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001923 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001924
Tejun Heo985ed672014-03-19 10:23:53 -04001925 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001926 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001927 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001928
Tejun Heo2bd59d42014-02-11 11:52:49 -05001929 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1930 KERNFS_ROOT_CREATE_DEACTIVATED,
1931 root_cgrp);
1932 if (IS_ERR(root->kf_root)) {
1933 ret = PTR_ERR(root->kf_root);
1934 goto exit_root_id;
1935 }
1936 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001937
Tejun Heo4df8dc92015-09-18 17:54:23 -04001938 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001939 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001940 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001941
Tejun Heo5df36032014-03-19 10:23:54 -04001942 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001943 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001944 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001945
Tejun Heod427dfe2014-02-11 11:52:48 -05001946 /*
1947 * There must be no failure case after here, since rebinding takes
1948 * care of subsystems' refcounts, which are explicitly dropped in
1949 * the failure exit path.
1950 */
1951 list_add(&root->root_list, &cgroup_roots);
1952 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001953
Tejun Heod427dfe2014-02-11 11:52:48 -05001954 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001955 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001956 * objects.
1957 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001958 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001959 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001960 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001961 if (css_set_populated(cset))
1962 cgroup_update_populated(root_cgrp, true);
1963 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001964 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001965
Tejun Heod5c419b2014-05-16 13:22:48 -04001966 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001967 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001968
Tejun Heo2bd59d42014-02-11 11:52:49 -05001969 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001970 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001971 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001972
Tejun Heo2bd59d42014-02-11 11:52:49 -05001973destroy_root:
1974 kernfs_destroy_root(root->kf_root);
1975 root->kf_root = NULL;
1976exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001977 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001978cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001979 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001980out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001981 free_cgrp_cset_links(&tmp_links);
1982 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001983}
1984
Al Virof7e83572010-07-26 13:23:11 +04001985static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001986 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001987 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001988{
Tejun Heo67e9c742015-11-16 11:13:34 -05001989 bool is_v2 = fs_type == &cgroup2_fs_type;
Li Zefan3a32bd72014-06-30 11:50:59 +08001990 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001991 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001992 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001993 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001994 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001995 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001996 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001997 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001998
1999 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05002000 * The first time anyone tries to mount a cgroup, enable the list
2001 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07002002 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05002003 if (!use_task_css_set_links)
2004 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002005
Tejun Heo67e9c742015-11-16 11:13:34 -05002006 if (is_v2) {
2007 if (data) {
2008 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
2009 return ERR_PTR(-EINVAL);
2010 }
2011 cgrp_dfl_root_visible = true;
2012 root = &cgrp_dfl_root;
2013 cgroup_get(&root->cgrp);
2014 goto out_mount;
2015 }
2016
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002017 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002018
Paul Menageddbcc7e2007-10-18 23:39:30 -07002019 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002020 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002021 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002022 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002023
Li Zefan970317a2014-06-30 11:49:58 +08002024 /*
2025 * Destruction of cgroup root is asynchronous, so subsystems may
2026 * still be dying after the previous unmount. Let's drain the
2027 * dying subsystems. We just need to ensure that the ones
2028 * unmounted previously finish dying and don't care about new ones
2029 * starting. Testing ref liveliness is good enough.
2030 */
2031 for_each_subsys(ss, i) {
2032 if (!(opts.subsys_mask & (1 << i)) ||
2033 ss->root == &cgrp_dfl_root)
2034 continue;
2035
2036 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2037 mutex_unlock(&cgroup_mutex);
2038 msleep(10);
2039 ret = restart_syscall();
2040 goto out_free;
2041 }
2042 cgroup_put(&ss->root->cgrp);
2043 }
2044
Tejun Heo985ed672014-03-19 10:23:53 -04002045 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002046 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002047
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002048 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002049 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002050
Paul Menage817929e2007-10-18 23:39:36 -07002051 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002052 * If we asked for a name then it must match. Also, if
2053 * name matches but sybsys_mask doesn't, we should fail.
2054 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002055 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002056 if (opts.name) {
2057 if (strcmp(opts.name, root->name))
2058 continue;
2059 name_match = true;
2060 }
Tejun Heo31261212013-06-28 17:07:30 -07002061
2062 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002063 * If we asked for subsystems (or explicitly for no
2064 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002065 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002066 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002067 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002068 if (!name_match)
2069 continue;
2070 ret = -EBUSY;
2071 goto out_unlock;
2072 }
Tejun Heo873fe092013-04-14 20:15:26 -07002073
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002074 if (root->flags ^ opts.flags)
2075 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002076
Tejun Heo776f02f2014-02-12 09:29:50 -05002077 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002078 * We want to reuse @root whose lifetime is governed by its
2079 * ->cgrp. Let's check whether @root is alive and keep it
2080 * that way. As cgroup_kill_sb() can happen anytime, we
2081 * want to block it by pinning the sb so that @root doesn't
2082 * get killed before mount is complete.
2083 *
2084 * With the sb pinned, tryget_live can reliably indicate
2085 * whether @root can be reused. If it's being killed,
2086 * drain it. We can use wait_queue for the wait but this
2087 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002088 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002089 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2090 if (IS_ERR(pinned_sb) ||
2091 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002092 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002093 if (!IS_ERR_OR_NULL(pinned_sb))
2094 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002095 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002096 ret = restart_syscall();
2097 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002098 }
2099
2100 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002101 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002102 }
2103
Tejun Heo172a2c062014-03-19 10:23:53 -04002104 /*
2105 * No such thing, create a new one. name= matching without subsys
2106 * specification is allowed for already existing hierarchies but we
2107 * can't create new one without subsys specification.
2108 */
2109 if (!opts.subsys_mask && !opts.none) {
2110 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002111 goto out_unlock;
2112 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002113
Tejun Heo172a2c062014-03-19 10:23:53 -04002114 root = kzalloc(sizeof(*root), GFP_KERNEL);
2115 if (!root) {
2116 ret = -ENOMEM;
2117 goto out_unlock;
2118 }
2119
2120 init_cgroup_root(root, &opts);
2121
Tejun Heo35585572014-02-13 06:58:38 -05002122 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002123 if (ret)
2124 cgroup_free_root(root);
2125
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002126out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002127 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002128out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002129 kfree(opts.release_agent);
2130 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002131
Tejun Heo2bd59d42014-02-11 11:52:49 -05002132 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002133 return ERR_PTR(ret);
Tejun Heo67e9c742015-11-16 11:13:34 -05002134out_mount:
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002135 dentry = kernfs_mount(fs_type, flags, root->kf_root,
Tejun Heo67e9c742015-11-16 11:13:34 -05002136 is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
2137 &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002138 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002139 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002140
2141 /*
2142 * If @pinned_sb, we're reusing an existing root and holding an
2143 * extra ref on its sb. Mount is complete. Put the extra ref.
2144 */
2145 if (pinned_sb) {
2146 WARN_ON(new_sb);
2147 deactivate_super(pinned_sb);
2148 }
2149
Tejun Heo2bd59d42014-02-11 11:52:49 -05002150 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002151}
2152
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002153static void cgroup_kill_sb(struct super_block *sb)
2154{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002155 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002156 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002157
Tejun Heo9d755d32014-05-14 09:15:02 -04002158 /*
2159 * If @root doesn't have any mounts or children, start killing it.
2160 * This prevents new mounts by disabling percpu_ref_tryget_live().
2161 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002162 *
2163 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002164 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002165 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002166 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002167 cgroup_put(&root->cgrp);
2168 else
2169 percpu_ref_kill(&root->cgrp.self.refcnt);
2170
Tejun Heo2bd59d42014-02-11 11:52:49 -05002171 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002172}
2173
2174static struct file_system_type cgroup_fs_type = {
2175 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002176 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002177 .kill_sb = cgroup_kill_sb,
2178};
2179
Tejun Heo67e9c742015-11-16 11:13:34 -05002180static struct file_system_type cgroup2_fs_type = {
2181 .name = "cgroup2",
2182 .mount = cgroup_mount,
2183 .kill_sb = cgroup_kill_sb,
2184};
2185
Li Zefana043e3b2008-02-23 15:24:09 -08002186/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002187 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002188 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002189 * @buf: the buffer to write the path into
2190 * @buflen: the length of the buffer
2191 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002192 * Determine @task's cgroup on the first (the one with the lowest non-zero
2193 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2194 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2195 * cgroup controller callbacks.
2196 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002197 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002198 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002199char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002200{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002201 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002202 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002203 int hierarchy_id = 1;
2204 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002205
2206 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002207 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002208
Tejun Heo913ffdb2013-07-11 16:34:48 -07002209 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2210
Tejun Heo857a2be2013-04-14 20:50:08 -07002211 if (root) {
2212 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002213 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002214 } else {
2215 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002216 if (strlcpy(buf, "/", buflen) < buflen)
2217 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002218 }
2219
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002220 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002221 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002222 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002223}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002224EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002225
Tejun Heob3dc0942014-02-25 10:04:01 -05002226/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002227struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002228 /* the src and dst cset list running through cset->mg_node */
2229 struct list_head src_csets;
2230 struct list_head dst_csets;
2231
2232 /*
2233 * Fields for cgroup_taskset_*() iteration.
2234 *
2235 * Before migration is committed, the target migration tasks are on
2236 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2237 * the csets on ->dst_csets. ->csets point to either ->src_csets
2238 * or ->dst_csets depending on whether migration is committed.
2239 *
2240 * ->cur_csets and ->cur_task point to the current task position
2241 * during iteration.
2242 */
2243 struct list_head *csets;
2244 struct css_set *cur_cset;
2245 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002246};
2247
Tejun Heoadaae5d2015-09-11 15:00:21 -04002248#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2249 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2250 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2251 .csets = &tset.src_csets, \
2252}
2253
2254/**
2255 * cgroup_taskset_add - try to add a migration target task to a taskset
2256 * @task: target task
2257 * @tset: target taskset
2258 *
2259 * Add @task, which is a migration target, to @tset. This function becomes
2260 * noop if @task doesn't need to be migrated. @task's css_set should have
2261 * been added as a migration source and @task->cg_list will be moved from
2262 * the css_set's tasks list to mg_tasks one.
2263 */
2264static void cgroup_taskset_add(struct task_struct *task,
2265 struct cgroup_taskset *tset)
2266{
2267 struct css_set *cset;
2268
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002269 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002270
2271 /* @task either already exited or can't exit until the end */
2272 if (task->flags & PF_EXITING)
2273 return;
2274
2275 /* leave @task alone if post_fork() hasn't linked it yet */
2276 if (list_empty(&task->cg_list))
2277 return;
2278
2279 cset = task_css_set(task);
2280 if (!cset->mg_src_cgrp)
2281 return;
2282
2283 list_move_tail(&task->cg_list, &cset->mg_tasks);
2284 if (list_empty(&cset->mg_node))
2285 list_add_tail(&cset->mg_node, &tset->src_csets);
2286 if (list_empty(&cset->mg_dst_cset->mg_node))
2287 list_move_tail(&cset->mg_dst_cset->mg_node,
2288 &tset->dst_csets);
2289}
2290
Tejun Heo2f7ee562011-12-12 18:12:21 -08002291/**
2292 * cgroup_taskset_first - reset taskset and return the first task
2293 * @tset: taskset of interest
2294 *
2295 * @tset iteration is initialized and the first task is returned.
2296 */
2297struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2298{
Tejun Heob3dc0942014-02-25 10:04:01 -05002299 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2300 tset->cur_task = NULL;
2301
2302 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002303}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002304
2305/**
2306 * cgroup_taskset_next - iterate to the next task in taskset
2307 * @tset: taskset of interest
2308 *
2309 * Return the next task in @tset. Iteration must have been initialized
2310 * with cgroup_taskset_first().
2311 */
2312struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2313{
Tejun Heob3dc0942014-02-25 10:04:01 -05002314 struct css_set *cset = tset->cur_cset;
2315 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002316
Tejun Heob3dc0942014-02-25 10:04:01 -05002317 while (&cset->mg_node != tset->csets) {
2318 if (!task)
2319 task = list_first_entry(&cset->mg_tasks,
2320 struct task_struct, cg_list);
2321 else
2322 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002323
Tejun Heob3dc0942014-02-25 10:04:01 -05002324 if (&task->cg_list != &cset->mg_tasks) {
2325 tset->cur_cset = cset;
2326 tset->cur_task = task;
2327 return task;
2328 }
2329
2330 cset = list_next_entry(cset, mg_node);
2331 task = NULL;
2332 }
2333
2334 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002335}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002336
2337/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002338 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2339 * @tset: taget taskset
2340 * @dst_cgrp: destination cgroup
2341 *
2342 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2343 * ->can_attach callbacks fails and guarantees that either all or none of
2344 * the tasks in @tset are migrated. @tset is consumed regardless of
2345 * success.
2346 */
2347static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2348 struct cgroup *dst_cgrp)
2349{
2350 struct cgroup_subsys_state *css, *failed_css = NULL;
2351 struct task_struct *task, *tmp_task;
2352 struct css_set *cset, *tmp_cset;
2353 int i, ret;
2354
2355 /* methods shouldn't be called if no task is actually migrating */
2356 if (list_empty(&tset->src_csets))
2357 return 0;
2358
2359 /* check that we can legitimately attach to the cgroup */
2360 for_each_e_css(css, i, dst_cgrp) {
2361 if (css->ss->can_attach) {
2362 ret = css->ss->can_attach(css, tset);
2363 if (ret) {
2364 failed_css = css;
2365 goto out_cancel_attach;
2366 }
2367 }
2368 }
2369
2370 /*
2371 * Now that we're guaranteed success, proceed to move all tasks to
2372 * the new cgroup. There are no failure cases after here, so this
2373 * is the commit point.
2374 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002375 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002376 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002377 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2378 struct css_set *from_cset = task_css_set(task);
2379 struct css_set *to_cset = cset->mg_dst_cset;
2380
2381 get_css_set(to_cset);
2382 css_set_move_task(task, from_cset, to_cset, true);
2383 put_css_set_locked(from_cset);
2384 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002385 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002386 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002387
2388 /*
2389 * Migration is committed, all target tasks are now on dst_csets.
2390 * Nothing is sensitive to fork() after this point. Notify
2391 * controllers that migration is complete.
2392 */
2393 tset->csets = &tset->dst_csets;
2394
2395 for_each_e_css(css, i, dst_cgrp)
2396 if (css->ss->attach)
2397 css->ss->attach(css, tset);
2398
2399 ret = 0;
2400 goto out_release_tset;
2401
2402out_cancel_attach:
2403 for_each_e_css(css, i, dst_cgrp) {
2404 if (css == failed_css)
2405 break;
2406 if (css->ss->cancel_attach)
2407 css->ss->cancel_attach(css, tset);
2408 }
2409out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002410 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002411 list_splice_init(&tset->dst_csets, &tset->src_csets);
2412 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2413 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2414 list_del_init(&cset->mg_node);
2415 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002416 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002417 return ret;
2418}
2419
2420/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002421 * cgroup_migrate_finish - cleanup after attach
2422 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002423 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002424 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2425 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002426 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002427static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002428{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002429 struct css_set *cset, *tmp_cset;
2430
2431 lockdep_assert_held(&cgroup_mutex);
2432
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002433 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002434 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2435 cset->mg_src_cgrp = NULL;
2436 cset->mg_dst_cset = NULL;
2437 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002438 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002439 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002440 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002441}
2442
2443/**
2444 * cgroup_migrate_add_src - add a migration source css_set
2445 * @src_cset: the source css_set to add
2446 * @dst_cgrp: the destination cgroup
2447 * @preloaded_csets: list of preloaded css_sets
2448 *
2449 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2450 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2451 * up by cgroup_migrate_finish().
2452 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002453 * This function may be called without holding cgroup_threadgroup_rwsem
2454 * even if the target is a process. Threads may be created and destroyed
2455 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2456 * into play and the preloaded css_sets are guaranteed to cover all
2457 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002458 */
2459static void cgroup_migrate_add_src(struct css_set *src_cset,
2460 struct cgroup *dst_cgrp,
2461 struct list_head *preloaded_csets)
2462{
2463 struct cgroup *src_cgrp;
2464
2465 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002466 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002467
2468 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2469
Tejun Heo1958d2d2014-02-25 10:04:03 -05002470 if (!list_empty(&src_cset->mg_preload_node))
2471 return;
2472
2473 WARN_ON(src_cset->mg_src_cgrp);
2474 WARN_ON(!list_empty(&src_cset->mg_tasks));
2475 WARN_ON(!list_empty(&src_cset->mg_node));
2476
2477 src_cset->mg_src_cgrp = src_cgrp;
2478 get_css_set(src_cset);
2479 list_add(&src_cset->mg_preload_node, preloaded_csets);
2480}
2481
2482/**
2483 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002484 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002485 * @preloaded_csets: list of preloaded source css_sets
2486 *
2487 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2488 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002489 * pins all destination css_sets, links each to its source, and append them
2490 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2491 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002492 *
2493 * This function must be called after cgroup_migrate_add_src() has been
2494 * called on each migration source css_set. After migration is performed
2495 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2496 * @preloaded_csets.
2497 */
2498static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2499 struct list_head *preloaded_csets)
2500{
2501 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002502 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002503
2504 lockdep_assert_held(&cgroup_mutex);
2505
Tejun Heof8f22e52014-04-23 11:13:16 -04002506 /*
2507 * Except for the root, child_subsys_mask must be zero for a cgroup
2508 * with tasks so that child cgroups don't compete against tasks.
2509 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002510 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002511 dst_cgrp->child_subsys_mask)
2512 return -EBUSY;
2513
Tejun Heo1958d2d2014-02-25 10:04:03 -05002514 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002515 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002516 struct css_set *dst_cset;
2517
Tejun Heof817de92014-04-23 11:13:16 -04002518 dst_cset = find_css_set(src_cset,
2519 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002520 if (!dst_cset)
2521 goto err;
2522
2523 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002524
2525 /*
2526 * If src cset equals dst, it's noop. Drop the src.
2527 * cgroup_migrate() will skip the cset too. Note that we
2528 * can't handle src == dst as some nodes are used by both.
2529 */
2530 if (src_cset == dst_cset) {
2531 src_cset->mg_src_cgrp = NULL;
2532 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002533 put_css_set(src_cset);
2534 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002535 continue;
2536 }
2537
Tejun Heo1958d2d2014-02-25 10:04:03 -05002538 src_cset->mg_dst_cset = dst_cset;
2539
2540 if (list_empty(&dst_cset->mg_preload_node))
2541 list_add(&dst_cset->mg_preload_node, &csets);
2542 else
Zefan Lia25eb522014-09-19 16:51:00 +08002543 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002544 }
2545
Tejun Heof817de92014-04-23 11:13:16 -04002546 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002547 return 0;
2548err:
2549 cgroup_migrate_finish(&csets);
2550 return -ENOMEM;
2551}
2552
2553/**
2554 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002555 * @leader: the leader of the process or the task to migrate
2556 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002557 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002558 *
2559 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002560 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002561 * caller is also responsible for invoking cgroup_migrate_add_src() and
2562 * cgroup_migrate_prepare_dst() on the targets before invoking this
2563 * function and following up with cgroup_migrate_finish().
2564 *
2565 * As long as a controller's ->can_attach() doesn't fail, this function is
2566 * guaranteed to succeed. This means that, excluding ->can_attach()
2567 * failure, when migrating multiple targets, the success or failure can be
2568 * decided for all targets by invoking group_migrate_prepare_dst() before
2569 * actually starting migrating.
2570 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002571static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2572 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002573{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002574 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2575 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002576
2577 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002578 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2579 * already PF_EXITING could be freed from underneath us unless we
2580 * take an rcu_read_lock.
2581 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002582 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002583 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002584 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002585 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002586 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002587 if (!threadgroup)
2588 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002589 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002590 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002591 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002592
Tejun Heoadaae5d2015-09-11 15:00:21 -04002593 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002594}
2595
Tejun Heo1958d2d2014-02-25 10:04:03 -05002596/**
2597 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2598 * @dst_cgrp: the cgroup to attach to
2599 * @leader: the task or the leader of the threadgroup to be attached
2600 * @threadgroup: attach the whole threadgroup?
2601 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002602 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002603 */
2604static int cgroup_attach_task(struct cgroup *dst_cgrp,
2605 struct task_struct *leader, bool threadgroup)
2606{
2607 LIST_HEAD(preloaded_csets);
2608 struct task_struct *task;
2609 int ret;
2610
2611 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002612 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002613 rcu_read_lock();
2614 task = leader;
2615 do {
2616 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2617 &preloaded_csets);
2618 if (!threadgroup)
2619 break;
2620 } while_each_thread(leader, task);
2621 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002622 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002623
2624 /* prepare dst csets and commit */
2625 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2626 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002627 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002628
2629 cgroup_migrate_finish(&preloaded_csets);
2630 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002631}
2632
Tejun Heo187fe842015-06-18 16:54:28 -04002633static int cgroup_procs_write_permission(struct task_struct *task,
2634 struct cgroup *dst_cgrp,
2635 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002636{
2637 const struct cred *cred = current_cred();
2638 const struct cred *tcred = get_task_cred(task);
2639 int ret = 0;
2640
2641 /*
2642 * even if we're attaching all tasks in the thread group, we only
2643 * need to check permissions on one of them.
2644 */
2645 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2646 !uid_eq(cred->euid, tcred->uid) &&
2647 !uid_eq(cred->euid, tcred->suid))
2648 ret = -EACCES;
2649
Tejun Heo187fe842015-06-18 16:54:28 -04002650 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2651 struct super_block *sb = of->file->f_path.dentry->d_sb;
2652 struct cgroup *cgrp;
2653 struct inode *inode;
2654
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002655 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002656 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002657 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002658
2659 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2660 cgrp = cgroup_parent(cgrp);
2661
2662 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002663 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002664 if (inode) {
2665 ret = inode_permission(inode, MAY_WRITE);
2666 iput(inode);
2667 }
2668 }
2669
Tejun Heodedf22e2015-06-18 16:54:28 -04002670 put_cred(tcred);
2671 return ret;
2672}
2673
Ben Blum74a11662011-05-26 16:25:20 -07002674/*
2675 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002676 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002677 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002678 */
Tejun Heoacbef752014-05-13 12:16:22 -04002679static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2680 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002681{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002682 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002683 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002684 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002685 int ret;
2686
Tejun Heoacbef752014-05-13 12:16:22 -04002687 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2688 return -EINVAL;
2689
Tejun Heoe76ecae2014-05-13 12:19:23 -04002690 cgrp = cgroup_kn_lock_live(of->kn);
2691 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002692 return -ENODEV;
2693
Tejun Heo3014dde2015-09-16 13:03:02 -04002694 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002695 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002696 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002697 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002698 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002699 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002700 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002701 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002702 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002703 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002704 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002705
2706 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002707 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002708
2709 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002710 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002711 * trapped in a cpuset, or RT worker may be born in a cgroup
2712 * with no rt_runtime allocated. Just say no.
2713 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002714 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002715 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002716 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002717 }
2718
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002719 get_task_struct(tsk);
2720 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002721
Tejun Heo187fe842015-06-18 16:54:28 -04002722 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002723 if (!ret)
2724 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002725
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002726 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002727 goto out_unlock_threadgroup;
2728
2729out_unlock_rcu:
2730 rcu_read_unlock();
2731out_unlock_threadgroup:
2732 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002733 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002734 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002735}
2736
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002737/**
2738 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2739 * @from: attach to all cgroups of a given task
2740 * @tsk: the task to be attached
2741 */
2742int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2743{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002744 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002745 int retval = 0;
2746
Tejun Heo47cfcd02013-04-07 09:29:51 -07002747 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002748 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002749 struct cgroup *from_cgrp;
2750
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002751 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002752 continue;
2753
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002754 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002755 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002756 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002757
Li Zefan6f4b7e62013-07-31 16:18:36 +08002758 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002759 if (retval)
2760 break;
2761 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002762 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002763
2764 return retval;
2765}
2766EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2767
Tejun Heoacbef752014-05-13 12:16:22 -04002768static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2769 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002770{
Tejun Heoacbef752014-05-13 12:16:22 -04002771 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002772}
2773
Tejun Heoacbef752014-05-13 12:16:22 -04002774static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2775 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002776{
Tejun Heoacbef752014-05-13 12:16:22 -04002777 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002778}
2779
Tejun Heo451af502014-05-13 12:16:21 -04002780static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2781 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002782{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002783 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002784
Tejun Heoe76ecae2014-05-13 12:19:23 -04002785 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2786
2787 cgrp = cgroup_kn_lock_live(of->kn);
2788 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002789 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002790 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002791 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2792 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002793 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002794 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002795 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002796}
2797
Tejun Heo2da8ca82013-12-05 12:28:04 -05002798static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002799{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002800 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002801
Tejun Heo46cfeb02014-05-13 12:11:00 -04002802 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002803 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002804 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002805 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002806 return 0;
2807}
2808
Tejun Heo2da8ca82013-12-05 12:28:04 -05002809static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002810{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002811 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002812 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002813}
2814
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002815static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002816{
2817 struct cgroup_subsys *ss;
2818 bool printed = false;
2819 int ssid;
2820
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002821 for_each_subsys_which(ss, ssid, &ss_mask) {
2822 if (printed)
2823 seq_putc(seq, ' ');
2824 seq_printf(seq, "%s", ss->name);
2825 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002826 }
2827 if (printed)
2828 seq_putc(seq, '\n');
2829}
2830
2831/* show controllers which are currently attached to the default hierarchy */
2832static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2833{
2834 struct cgroup *cgrp = seq_css(seq)->cgroup;
2835
Tejun Heo5533e012014-05-14 19:33:07 -04002836 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2837 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002838 return 0;
2839}
2840
2841/* show controllers which are enabled from the parent */
2842static int cgroup_controllers_show(struct seq_file *seq, void *v)
2843{
2844 struct cgroup *cgrp = seq_css(seq)->cgroup;
2845
Tejun Heo667c2492014-07-08 18:02:56 -04002846 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002847 return 0;
2848}
2849
2850/* show controllers which are enabled for a given cgroup's children */
2851static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2852{
2853 struct cgroup *cgrp = seq_css(seq)->cgroup;
2854
Tejun Heo667c2492014-07-08 18:02:56 -04002855 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002856 return 0;
2857}
2858
2859/**
2860 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2861 * @cgrp: root of the subtree to update csses for
2862 *
2863 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2864 * css associations need to be updated accordingly. This function looks up
2865 * all css_sets which are attached to the subtree, creates the matching
2866 * updated css_sets and migrates the tasks to the new ones.
2867 */
2868static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2869{
2870 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002871 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002872 struct cgroup_subsys_state *css;
2873 struct css_set *src_cset;
2874 int ret;
2875
Tejun Heof8f22e52014-04-23 11:13:16 -04002876 lockdep_assert_held(&cgroup_mutex);
2877
Tejun Heo3014dde2015-09-16 13:03:02 -04002878 percpu_down_write(&cgroup_threadgroup_rwsem);
2879
Tejun Heof8f22e52014-04-23 11:13:16 -04002880 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002881 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002882 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2883 struct cgrp_cset_link *link;
2884
2885 /* self is not affected by child_subsys_mask change */
2886 if (css->cgroup == cgrp)
2887 continue;
2888
2889 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2890 cgroup_migrate_add_src(link->cset, cgrp,
2891 &preloaded_csets);
2892 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002893 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002894
2895 /* NULL dst indicates self on default hierarchy */
2896 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2897 if (ret)
2898 goto out_finish;
2899
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002900 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002901 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002902 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002903
2904 /* src_csets precede dst_csets, break on the first dst_cset */
2905 if (!src_cset->mg_src_cgrp)
2906 break;
2907
Tejun Heo10265072015-09-11 15:00:22 -04002908 /* all tasks in src_csets need to be migrated */
2909 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2910 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002911 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002912 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002913
Tejun Heo10265072015-09-11 15:00:22 -04002914 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002915out_finish:
2916 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002917 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002918 return ret;
2919}
2920
2921/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002922static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2923 char *buf, size_t nbytes,
2924 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002925{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002926 unsigned long enable = 0, disable = 0;
2927 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002928 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002929 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002930 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002931 int ssid, ret;
2932
2933 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002934 * Parse input - space separated list of subsystem names prefixed
2935 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002936 */
Tejun Heo451af502014-05-13 12:16:21 -04002937 buf = strstrip(buf);
2938 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002939 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2940
Tejun Heod37167a2014-05-13 12:10:59 -04002941 if (tok[0] == '\0')
2942 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002943 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04002944 if (!cgroup_ssid_enabled(ssid) ||
2945 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002946 continue;
2947
2948 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002949 enable |= 1 << ssid;
2950 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002951 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002952 disable |= 1 << ssid;
2953 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002954 } else {
2955 return -EINVAL;
2956 }
2957 break;
2958 }
2959 if (ssid == CGROUP_SUBSYS_COUNT)
2960 return -EINVAL;
2961 }
2962
Tejun Heoa9746d82014-05-13 12:19:22 -04002963 cgrp = cgroup_kn_lock_live(of->kn);
2964 if (!cgrp)
2965 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002966
2967 for_each_subsys(ss, ssid) {
2968 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002969 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002970 enable &= ~(1 << ssid);
2971 continue;
2972 }
2973
Tejun Heoc29adf22014-07-08 18:02:56 -04002974 /* unavailable or not enabled on the parent? */
2975 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2976 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002977 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002978 ret = -ENOENT;
2979 goto out_unlock;
2980 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002981 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002982 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002983 disable &= ~(1 << ssid);
2984 continue;
2985 }
2986
2987 /* a child has it enabled? */
2988 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002989 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002990 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002991 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002992 }
2993 }
2994 }
2995 }
2996
2997 if (!enable && !disable) {
2998 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002999 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003000 }
3001
3002 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003003 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003004 * with tasks so that child cgroups don't compete against tasks.
3005 */
Tejun Heod51f39b2014-05-16 13:22:48 -04003006 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003007 ret = -EBUSY;
3008 goto out_unlock;
3009 }
3010
3011 /*
Tejun Heof63070d2014-07-08 18:02:57 -04003012 * Update subsys masks and calculate what needs to be done. More
3013 * subsystems than specified may need to be enabled or disabled
3014 * depending on subsystem dependencies.
3015 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05003016 old_sc = cgrp->subtree_control;
3017 old_ss = cgrp->child_subsys_mask;
3018 new_sc = (old_sc | enable) & ~disable;
3019 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04003020
Tejun Heo755bf5e2014-11-18 02:49:50 -05003021 css_enable = ~old_ss & new_ss;
3022 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04003023 enable |= css_enable;
3024 disable |= css_disable;
3025
Tejun Heodb6e3052014-11-18 02:49:51 -05003026 /*
3027 * Because css offlining is asynchronous, userland might try to
3028 * re-enable the same controller while the previous instance is
3029 * still around. In such cases, wait till it's gone using
3030 * offline_waitq.
3031 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10003032 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05003033 cgroup_for_each_live_child(child, cgrp) {
3034 DEFINE_WAIT(wait);
3035
3036 if (!cgroup_css(child, ss))
3037 continue;
3038
3039 cgroup_get(child);
3040 prepare_to_wait(&child->offline_waitq, &wait,
3041 TASK_UNINTERRUPTIBLE);
3042 cgroup_kn_unlock(of->kn);
3043 schedule();
3044 finish_wait(&child->offline_waitq, &wait);
3045 cgroup_put(child);
3046
3047 return restart_syscall();
3048 }
3049 }
3050
Tejun Heo755bf5e2014-11-18 02:49:50 -05003051 cgrp->subtree_control = new_sc;
3052 cgrp->child_subsys_mask = new_ss;
3053
Tejun Heof63070d2014-07-08 18:02:57 -04003054 /*
3055 * Create new csses or make the existing ones visible. A css is
3056 * created invisible if it's being implicitly enabled through
3057 * dependency. An invisible css is made visible when the userland
3058 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04003059 */
3060 for_each_subsys(ss, ssid) {
3061 if (!(enable & (1 << ssid)))
3062 continue;
3063
3064 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04003065 if (css_enable & (1 << ssid))
3066 ret = create_css(child, ss,
3067 cgrp->subtree_control & (1 << ssid));
3068 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003069 ret = css_populate_dir(cgroup_css(child, ss),
3070 NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003071 if (ret)
3072 goto err_undo_css;
3073 }
3074 }
3075
Tejun Heoc29adf22014-07-08 18:02:56 -04003076 /*
3077 * At this point, cgroup_e_css() results reflect the new csses
3078 * making the following cgroup_update_dfl_csses() properly update
3079 * css associations of all tasks in the subtree.
3080 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003081 ret = cgroup_update_dfl_csses(cgrp);
3082 if (ret)
3083 goto err_undo_css;
3084
Tejun Heof63070d2014-07-08 18:02:57 -04003085 /*
3086 * All tasks are migrated out of disabled csses. Kill or hide
3087 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003088 * disabled while other subsystems are still depending on it. The
3089 * css must not actively control resources and be in the vanilla
3090 * state if it's made visible again later. Controllers which may
3091 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003092 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003093 for_each_subsys(ss, ssid) {
3094 if (!(disable & (1 << ssid)))
3095 continue;
3096
Tejun Heof63070d2014-07-08 18:02:57 -04003097 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003098 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3099
3100 if (css_disable & (1 << ssid)) {
3101 kill_css(css);
3102 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003103 css_clear_dir(css, NULL);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003104 if (ss->css_reset)
3105 ss->css_reset(css);
3106 }
Tejun Heof63070d2014-07-08 18:02:57 -04003107 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003108 }
3109
Tejun Heo56c807b2014-11-18 02:49:51 -05003110 /*
3111 * The effective csses of all the descendants (excluding @cgrp) may
3112 * have changed. Subsystems can optionally subscribe to this event
3113 * by implementing ->css_e_css_changed() which is invoked if any of
3114 * the effective csses seen from the css's cgroup may have changed.
3115 */
3116 for_each_subsys(ss, ssid) {
3117 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
3118 struct cgroup_subsys_state *css;
3119
3120 if (!ss->css_e_css_changed || !this_css)
3121 continue;
3122
3123 css_for_each_descendant_pre(css, this_css)
3124 if (css != this_css)
3125 ss->css_e_css_changed(css);
3126 }
3127
Tejun Heof8f22e52014-04-23 11:13:16 -04003128 kernfs_activate(cgrp->kn);
3129 ret = 0;
3130out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003131 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003132 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003133
3134err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003135 cgrp->subtree_control = old_sc;
3136 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003137
3138 for_each_subsys(ss, ssid) {
3139 if (!(enable & (1 << ssid)))
3140 continue;
3141
3142 cgroup_for_each_live_child(child, cgrp) {
3143 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003144
3145 if (!css)
3146 continue;
3147
3148 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003149 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003150 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003151 css_clear_dir(css, NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003152 }
3153 }
3154 goto out_unlock;
3155}
3156
Tejun Heo4a07c222015-09-18 17:54:22 -04003157static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003158{
Tejun Heo4a07c222015-09-18 17:54:22 -04003159 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003160 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003161 return 0;
3162}
3163
Tejun Heo2bd59d42014-02-11 11:52:49 -05003164static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3165 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003166{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003167 struct cgroup *cgrp = of->kn->parent->priv;
3168 struct cftype *cft = of->kn->priv;
3169 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003170 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003171
Tejun Heob4168642014-05-13 12:16:21 -04003172 if (cft->write)
3173 return cft->write(of, buf, nbytes, off);
3174
Tejun Heo2bd59d42014-02-11 11:52:49 -05003175 /*
3176 * kernfs guarantees that a file isn't deleted with operations in
3177 * flight, which means that the matching css is and stays alive and
3178 * doesn't need to be pinned. The RCU locking is not necessary
3179 * either. It's just for the convenience of using cgroup_css().
3180 */
3181 rcu_read_lock();
3182 css = cgroup_css(cgrp, cft->ss);
3183 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003184
Tejun Heo451af502014-05-13 12:16:21 -04003185 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003186 unsigned long long v;
3187 ret = kstrtoull(buf, 0, &v);
3188 if (!ret)
3189 ret = cft->write_u64(css, cft, v);
3190 } else if (cft->write_s64) {
3191 long long v;
3192 ret = kstrtoll(buf, 0, &v);
3193 if (!ret)
3194 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003195 } else {
3196 ret = -EINVAL;
3197 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003198
Tejun Heoa742c592013-12-05 12:28:03 -05003199 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003200}
3201
Tejun Heo6612f052013-12-05 12:28:04 -05003202static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003203{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003204 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003205}
3206
3207static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3208{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003209 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003210}
3211
3212static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3213{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003214 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003215}
3216
3217static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3218{
Tejun Heo7da11272013-12-05 12:28:04 -05003219 struct cftype *cft = seq_cft(m);
3220 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003221
Tejun Heo2da8ca82013-12-05 12:28:04 -05003222 if (cft->seq_show)
3223 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003224
Tejun Heo896f5192013-12-05 12:28:04 -05003225 if (cft->read_u64)
3226 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3227 else if (cft->read_s64)
3228 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3229 else
3230 return -EINVAL;
3231 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003232}
3233
Tejun Heo2bd59d42014-02-11 11:52:49 -05003234static struct kernfs_ops cgroup_kf_single_ops = {
3235 .atomic_write_len = PAGE_SIZE,
3236 .write = cgroup_file_write,
3237 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003238};
3239
Tejun Heo2bd59d42014-02-11 11:52:49 -05003240static struct kernfs_ops cgroup_kf_ops = {
3241 .atomic_write_len = PAGE_SIZE,
3242 .write = cgroup_file_write,
3243 .seq_start = cgroup_seqfile_start,
3244 .seq_next = cgroup_seqfile_next,
3245 .seq_stop = cgroup_seqfile_stop,
3246 .seq_show = cgroup_seqfile_show,
3247};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003248
3249/*
3250 * cgroup_rename - Only allow simple rename of directories in place.
3251 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003252static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3253 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003254{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003255 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003256 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003257
Tejun Heo2bd59d42014-02-11 11:52:49 -05003258 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003259 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003260 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003261 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003262
Tejun Heo6db8e852013-06-14 11:18:22 -07003263 /*
3264 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003265 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003266 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003267 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003268 return -EPERM;
3269
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003270 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003271 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003272 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003273 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003274 */
3275 kernfs_break_active_protection(new_parent);
3276 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003277
Tejun Heo2bd59d42014-02-11 11:52:49 -05003278 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003279
Tejun Heo2bd59d42014-02-11 11:52:49 -05003280 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003281
Tejun Heo2bd59d42014-02-11 11:52:49 -05003282 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003283
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003284 kernfs_unbreak_active_protection(kn);
3285 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003286 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003287}
3288
Tejun Heo49957f82014-04-07 16:44:47 -04003289/* set uid and gid of cgroup dirs and files to that of the creator */
3290static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3291{
3292 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3293 .ia_uid = current_fsuid(),
3294 .ia_gid = current_fsgid(), };
3295
3296 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3297 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3298 return 0;
3299
3300 return kernfs_setattr(kn, &iattr);
3301}
3302
Tejun Heo4df8dc92015-09-18 17:54:23 -04003303static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3304 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003305{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003306 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003307 struct kernfs_node *kn;
3308 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003309 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003310
Tejun Heo2bd59d42014-02-11 11:52:49 -05003311#ifdef CONFIG_DEBUG_LOCK_ALLOC
3312 key = &cft->lockdep_key;
3313#endif
3314 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3315 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003316 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003317 if (IS_ERR(kn))
3318 return PTR_ERR(kn);
3319
3320 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003321 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003322 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003323 return ret;
3324 }
3325
Tejun Heo6f60ead2015-09-18 17:54:23 -04003326 if (cft->file_offset) {
3327 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3328
Tejun Heo34c06252015-11-05 00:12:24 -05003329 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003330 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003331 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003332 }
3333
Tejun Heof8f22e52014-04-23 11:13:16 -04003334 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003335}
3336
Tejun Heob1f28d32013-06-28 16:24:10 -07003337/**
3338 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003339 * @css: the target css
3340 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003341 * @cfts: array of cftypes to be added
3342 * @is_add: whether to add or remove
3343 *
3344 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003345 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003346 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003347static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3348 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003349 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003350{
Tejun Heo6732ed82015-09-18 17:54:23 -04003351 struct cftype *cft, *cft_end = NULL;
Tejun Heob1f28d32013-06-28 16:24:10 -07003352 int ret;
3353
Tejun Heo01f64742014-05-13 12:19:23 -04003354 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003355
Tejun Heo6732ed82015-09-18 17:54:23 -04003356restart:
3357 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003358 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003359 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003360 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003361 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003362 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003363 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003364 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003365 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003366 continue;
3367
Li Zefan2739d3c2013-01-21 18:18:33 +08003368 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003369 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003370 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003371 pr_warn("%s: failed to add %s, err=%d\n",
3372 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003373 cft_end = cft;
3374 is_add = false;
3375 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003376 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003377 } else {
3378 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003379 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003380 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003381 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003382}
3383
Tejun Heo21a2d342014-02-12 09:29:49 -05003384static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003385{
3386 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003387 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003388 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003389 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003390 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003391
Tejun Heo01f64742014-05-13 12:19:23 -04003392 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003393
Li Zefane8c82d22013-06-18 18:48:37 +08003394 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003395 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003396 struct cgroup *cgrp = css->cgroup;
3397
Li Zefane8c82d22013-06-18 18:48:37 +08003398 if (cgroup_is_dead(cgrp))
3399 continue;
3400
Tejun Heo4df8dc92015-09-18 17:54:23 -04003401 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003402 if (ret)
3403 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003404 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003405
3406 if (is_add && !ret)
3407 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003408 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003409}
3410
Tejun Heo2da440a2014-02-11 11:52:48 -05003411static void cgroup_exit_cftypes(struct cftype *cfts)
3412{
3413 struct cftype *cft;
3414
Tejun Heo2bd59d42014-02-11 11:52:49 -05003415 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3416 /* free copy for custom atomic_write_len, see init_cftypes() */
3417 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3418 kfree(cft->kf_ops);
3419 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003420 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003421
3422 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003423 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003424 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003425}
3426
Tejun Heo2bd59d42014-02-11 11:52:49 -05003427static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003428{
3429 struct cftype *cft;
3430
Tejun Heo2bd59d42014-02-11 11:52:49 -05003431 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3432 struct kernfs_ops *kf_ops;
3433
Tejun Heo0adb0702014-02-12 09:29:48 -05003434 WARN_ON(cft->ss || cft->kf_ops);
3435
Tejun Heo2bd59d42014-02-11 11:52:49 -05003436 if (cft->seq_start)
3437 kf_ops = &cgroup_kf_ops;
3438 else
3439 kf_ops = &cgroup_kf_single_ops;
3440
3441 /*
3442 * Ugh... if @cft wants a custom max_write_len, we need to
3443 * make a copy of kf_ops to set its atomic_write_len.
3444 */
3445 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3446 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3447 if (!kf_ops) {
3448 cgroup_exit_cftypes(cfts);
3449 return -ENOMEM;
3450 }
3451 kf_ops->atomic_write_len = cft->max_write_len;
3452 }
3453
3454 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003455 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003456 }
3457
3458 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003459}
3460
Tejun Heo21a2d342014-02-12 09:29:49 -05003461static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3462{
Tejun Heo01f64742014-05-13 12:19:23 -04003463 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003464
3465 if (!cfts || !cfts[0].ss)
3466 return -ENOENT;
3467
3468 list_del(&cfts->node);
3469 cgroup_apply_cftypes(cfts, false);
3470 cgroup_exit_cftypes(cfts);
3471 return 0;
3472}
3473
Tejun Heo8e3f6542012-04-01 12:09:55 -07003474/**
Tejun Heo80b13582014-02-12 09:29:48 -05003475 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3476 * @cfts: zero-length name terminated array of cftypes
3477 *
3478 * Unregister @cfts. Files described by @cfts are removed from all
3479 * existing cgroups and all future cgroups won't have them either. This
3480 * function can be called anytime whether @cfts' subsys is attached or not.
3481 *
3482 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3483 * registered.
3484 */
3485int cgroup_rm_cftypes(struct cftype *cfts)
3486{
Tejun Heo21a2d342014-02-12 09:29:49 -05003487 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003488
Tejun Heo01f64742014-05-13 12:19:23 -04003489 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003490 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003491 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003492 return ret;
3493}
3494
3495/**
3496 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3497 * @ss: target cgroup subsystem
3498 * @cfts: zero-length name terminated array of cftypes
3499 *
3500 * Register @cfts to @ss. Files described by @cfts are created for all
3501 * existing cgroups to which @ss is attached and all future cgroups will
3502 * have them too. This function can be called anytime whether @ss is
3503 * attached or not.
3504 *
3505 * Returns 0 on successful registration, -errno on failure. Note that this
3506 * function currently returns 0 as long as @cfts registration is successful
3507 * even if some file creation attempts on existing cgroups fail.
3508 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003509static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003510{
Tejun Heo9ccece82013-06-28 16:24:11 -07003511 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003512
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003513 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003514 return 0;
3515
Li Zefandc5736e2014-02-17 10:41:50 +08003516 if (!cfts || cfts[0].name[0] == '\0')
3517 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003518
Tejun Heo2bd59d42014-02-11 11:52:49 -05003519 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003520 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003521 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003522
Tejun Heo01f64742014-05-13 12:19:23 -04003523 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003524
Tejun Heo0adb0702014-02-12 09:29:48 -05003525 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003526 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003527 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003528 cgroup_rm_cftypes_locked(cfts);
3529
Tejun Heo01f64742014-05-13 12:19:23 -04003530 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003531 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003532}
Tejun Heo79578622012-04-01 12:09:56 -07003533
3534/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003535 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3536 * @ss: target cgroup subsystem
3537 * @cfts: zero-length name terminated array of cftypes
3538 *
3539 * Similar to cgroup_add_cftypes() but the added files are only used for
3540 * the default hierarchy.
3541 */
3542int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3543{
3544 struct cftype *cft;
3545
3546 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003547 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003548 return cgroup_add_cftypes(ss, cfts);
3549}
3550
3551/**
3552 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3553 * @ss: target cgroup subsystem
3554 * @cfts: zero-length name terminated array of cftypes
3555 *
3556 * Similar to cgroup_add_cftypes() but the added files are only used for
3557 * the legacy hierarchies.
3558 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003559int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3560{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003561 struct cftype *cft;
3562
Tejun Heoe4b70372015-10-15 17:00:43 -04003563 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3564 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003565 return cgroup_add_cftypes(ss, cfts);
3566}
3567
Li Zefana043e3b2008-02-23 15:24:09 -08003568/**
Tejun Heo34c06252015-11-05 00:12:24 -05003569 * cgroup_file_notify - generate a file modified event for a cgroup_file
3570 * @cfile: target cgroup_file
3571 *
3572 * @cfile must have been obtained by setting cftype->file_offset.
3573 */
3574void cgroup_file_notify(struct cgroup_file *cfile)
3575{
3576 unsigned long flags;
3577
3578 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3579 if (cfile->kn)
3580 kernfs_notify(cfile->kn);
3581 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3582}
3583
3584/**
Li Zefana043e3b2008-02-23 15:24:09 -08003585 * cgroup_task_count - count the number of tasks in a cgroup.
3586 * @cgrp: the cgroup in question
3587 *
3588 * Return the number of tasks in the cgroup.
3589 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003590static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003591{
3592 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003593 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003594
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003595 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003596 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3597 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003598 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003599 return count;
3600}
3601
Tejun Heo574bd9f2012-11-09 09:12:29 -08003602/**
Tejun Heo492eb212013-08-08 20:11:25 -04003603 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003604 * @pos: the current position (%NULL to initiate traversal)
3605 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003606 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003607 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003608 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003609 * that @parent and @pos are accessible. The next sibling is guaranteed to
3610 * be returned regardless of their states.
3611 *
3612 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3613 * css which finished ->css_online() is guaranteed to be visible in the
3614 * future iterations and will stay visible until the last reference is put.
3615 * A css which hasn't finished ->css_online() or already finished
3616 * ->css_offline() may show up during traversal. It's each subsystem's
3617 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003618 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003619struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3620 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003621{
Tejun Heoc2931b72014-05-16 13:22:51 -04003622 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003623
Tejun Heo8353da12014-05-13 12:19:23 -04003624 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003625
3626 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003627 * @pos could already have been unlinked from the sibling list.
3628 * Once a cgroup is removed, its ->sibling.next is no longer
3629 * updated when its next sibling changes. CSS_RELEASED is set when
3630 * @pos is taken off list, at which time its next pointer is valid,
3631 * and, as releases are serialized, the one pointed to by the next
3632 * pointer is guaranteed to not have started release yet. This
3633 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3634 * critical section, the one pointed to by its next pointer is
3635 * guaranteed to not have finished its RCU grace period even if we
3636 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003637 *
Tejun Heode3f0342014-05-16 13:22:49 -04003638 * If @pos has CSS_RELEASED set, its next pointer can't be
3639 * dereferenced; however, as each css is given a monotonically
3640 * increasing unique serial number and always appended to the
3641 * sibling list, the next one can be found by walking the parent's
3642 * children until the first css with higher serial number than
3643 * @pos's. While this path can be slower, it happens iff iteration
3644 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003645 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003646 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003647 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3648 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3649 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003650 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003651 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003652 if (next->serial_nr > pos->serial_nr)
3653 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003654 }
3655
Tejun Heo3b281af2014-04-23 11:13:15 -04003656 /*
3657 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003658 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003659 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003660 if (&next->sibling != &parent->children)
3661 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003662 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003663}
Tejun Heo53fa5262013-05-24 10:55:38 +09003664
3665/**
Tejun Heo492eb212013-08-08 20:11:25 -04003666 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003667 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003668 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003669 *
Tejun Heo492eb212013-08-08 20:11:25 -04003670 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003671 * to visit for pre-order traversal of @root's descendants. @root is
3672 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003673 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003674 * While this function requires cgroup_mutex or RCU read locking, it
3675 * doesn't require the whole traversal to be contained in a single critical
3676 * section. This function will return the correct next descendant as long
3677 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003678 *
3679 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3680 * css which finished ->css_online() is guaranteed to be visible in the
3681 * future iterations and will stay visible until the last reference is put.
3682 * A css which hasn't finished ->css_online() or already finished
3683 * ->css_offline() may show up during traversal. It's each subsystem's
3684 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003685 */
Tejun Heo492eb212013-08-08 20:11:25 -04003686struct cgroup_subsys_state *
3687css_next_descendant_pre(struct cgroup_subsys_state *pos,
3688 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003689{
Tejun Heo492eb212013-08-08 20:11:25 -04003690 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003691
Tejun Heo8353da12014-05-13 12:19:23 -04003692 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003693
Tejun Heobd8815a2013-08-08 20:11:27 -04003694 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003695 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003696 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003697
3698 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003699 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003700 if (next)
3701 return next;
3702
3703 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003704 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003705 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003706 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003707 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003708 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003709 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003710
3711 return NULL;
3712}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003713
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003714/**
Tejun Heo492eb212013-08-08 20:11:25 -04003715 * css_rightmost_descendant - return the rightmost descendant of a css
3716 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003717 *
Tejun Heo492eb212013-08-08 20:11:25 -04003718 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3719 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003720 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003721 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003722 * While this function requires cgroup_mutex or RCU read locking, it
3723 * doesn't require the whole traversal to be contained in a single critical
3724 * section. This function will return the correct rightmost descendant as
3725 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003726 */
Tejun Heo492eb212013-08-08 20:11:25 -04003727struct cgroup_subsys_state *
3728css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003729{
Tejun Heo492eb212013-08-08 20:11:25 -04003730 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003731
Tejun Heo8353da12014-05-13 12:19:23 -04003732 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003733
3734 do {
3735 last = pos;
3736 /* ->prev isn't RCU safe, walk ->next till the end */
3737 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003738 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003739 pos = tmp;
3740 } while (pos);
3741
3742 return last;
3743}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003744
Tejun Heo492eb212013-08-08 20:11:25 -04003745static struct cgroup_subsys_state *
3746css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003747{
Tejun Heo492eb212013-08-08 20:11:25 -04003748 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003749
3750 do {
3751 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003752 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003753 } while (pos);
3754
3755 return last;
3756}
3757
3758/**
Tejun Heo492eb212013-08-08 20:11:25 -04003759 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003760 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003761 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003762 *
Tejun Heo492eb212013-08-08 20:11:25 -04003763 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003764 * to visit for post-order traversal of @root's descendants. @root is
3765 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003766 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003767 * While this function requires cgroup_mutex or RCU read locking, it
3768 * doesn't require the whole traversal to be contained in a single critical
3769 * section. This function will return the correct next descendant as long
3770 * as both @pos and @cgroup are accessible and @pos is a descendant of
3771 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003772 *
3773 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3774 * css which finished ->css_online() is guaranteed to be visible in the
3775 * future iterations and will stay visible until the last reference is put.
3776 * A css which hasn't finished ->css_online() or already finished
3777 * ->css_offline() may show up during traversal. It's each subsystem's
3778 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003779 */
Tejun Heo492eb212013-08-08 20:11:25 -04003780struct cgroup_subsys_state *
3781css_next_descendant_post(struct cgroup_subsys_state *pos,
3782 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003783{
Tejun Heo492eb212013-08-08 20:11:25 -04003784 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003785
Tejun Heo8353da12014-05-13 12:19:23 -04003786 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003787
Tejun Heo58b79a92013-09-06 15:31:08 -04003788 /* if first iteration, visit leftmost descendant which may be @root */
3789 if (!pos)
3790 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003791
Tejun Heobd8815a2013-08-08 20:11:27 -04003792 /* if we visited @root, we're done */
3793 if (pos == root)
3794 return NULL;
3795
Tejun Heo574bd9f2012-11-09 09:12:29 -08003796 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003797 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003798 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003799 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003800
3801 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003802 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003803}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003804
Tejun Heof3d46502014-05-16 13:22:52 -04003805/**
3806 * css_has_online_children - does a css have online children
3807 * @css: the target css
3808 *
3809 * Returns %true if @css has any online children; otherwise, %false. This
3810 * function can be called from any context but the caller is responsible
3811 * for synchronizing against on/offlining as necessary.
3812 */
3813bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003814{
Tejun Heof3d46502014-05-16 13:22:52 -04003815 struct cgroup_subsys_state *child;
3816 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003817
3818 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003819 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003820 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003821 ret = true;
3822 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003823 }
3824 }
3825 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003826 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003827}
3828
Tejun Heo0942eee2013-08-08 20:11:26 -04003829/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003830 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003831 * @it: the iterator to advance
3832 *
3833 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003834 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003835static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003836{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003837 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003838 struct cgrp_cset_link *link;
3839 struct css_set *cset;
3840
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003841 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003842
Tejun Heod5158762013-08-08 20:11:26 -04003843 /* Advance to the next non-empty css_set */
3844 do {
3845 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003846 if (l == it->cset_head) {
3847 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003848 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003849 return;
3850 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003851
3852 if (it->ss) {
3853 cset = container_of(l, struct css_set,
3854 e_cset_node[it->ss->id]);
3855 } else {
3856 link = list_entry(l, struct cgrp_cset_link, cset_link);
3857 cset = link->cset;
3858 }
Tejun Heo0de09422015-10-15 16:41:49 -04003859 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05003860
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003861 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003862
3863 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003864 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003865 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003866 it->task_pos = cset->mg_tasks.next;
3867
3868 it->tasks_head = &cset->tasks;
3869 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04003870
3871 /*
3872 * We don't keep css_sets locked across iteration steps and thus
3873 * need to take steps to ensure that iteration can be resumed after
3874 * the lock is re-acquired. Iteration is performed at two levels -
3875 * css_sets and tasks in them.
3876 *
3877 * Once created, a css_set never leaves its cgroup lists, so a
3878 * pinned css_set is guaranteed to stay put and we can resume
3879 * iteration afterwards.
3880 *
3881 * Tasks may leave @cset across iteration steps. This is resolved
3882 * by registering each iterator with the css_set currently being
3883 * walked and making css_set_move_task() advance iterators whose
3884 * next task is leaving.
3885 */
3886 if (it->cur_cset) {
3887 list_del(&it->iters_node);
3888 put_css_set_locked(it->cur_cset);
3889 }
3890 get_css_set(cset);
3891 it->cur_cset = cset;
3892 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04003893}
3894
Tejun Heoecb9d532015-10-15 16:41:52 -04003895static void css_task_iter_advance(struct css_task_iter *it)
3896{
3897 struct list_head *l = it->task_pos;
3898
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003899 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04003900 WARN_ON_ONCE(!l);
3901
3902 /*
3903 * Advance iterator to find next entry. cset->tasks is consumed
3904 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3905 * next cset.
3906 */
3907 l = l->next;
3908
3909 if (l == it->tasks_head)
3910 l = it->mg_tasks_head->next;
3911
3912 if (l == it->mg_tasks_head)
3913 css_task_iter_advance_css_set(it);
3914 else
3915 it->task_pos = l;
3916}
3917
Tejun Heo0942eee2013-08-08 20:11:26 -04003918/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003919 * css_task_iter_start - initiate task iteration
3920 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003921 * @it: the task iterator to use
3922 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003923 * Initiate iteration through the tasks of @css. The caller can call
3924 * css_task_iter_next() to walk through the tasks until the function
3925 * returns NULL. On completion of iteration, css_task_iter_end() must be
3926 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003927 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003928void css_task_iter_start(struct cgroup_subsys_state *css,
3929 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003930{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003931 /* no one should try to iterate before mounting cgroups */
3932 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003933
Tejun Heoed27b9f2015-10-15 16:41:52 -04003934 memset(it, 0, sizeof(*it));
3935
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003936 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003937
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003938 it->ss = css->ss;
3939
3940 if (it->ss)
3941 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3942 else
3943 it->cset_pos = &css->cgroup->cset_links;
3944
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003945 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003946
Tejun Heoecb9d532015-10-15 16:41:52 -04003947 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003948
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003949 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003950}
Paul Menage817929e2007-10-18 23:39:36 -07003951
Tejun Heo0942eee2013-08-08 20:11:26 -04003952/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003953 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003954 * @it: the task iterator being iterated
3955 *
3956 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003957 * initialized via css_task_iter_start(). Returns NULL when the iteration
3958 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003959 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003960struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003961{
Tejun Heod5745672015-10-29 11:43:05 +09003962 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04003963 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09003964 it->cur_task = NULL;
3965 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003966
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003967 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003968
Tejun Heod5745672015-10-29 11:43:05 +09003969 if (it->task_pos) {
3970 it->cur_task = list_entry(it->task_pos, struct task_struct,
3971 cg_list);
3972 get_task_struct(it->cur_task);
3973 css_task_iter_advance(it);
3974 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003975
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003976 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003977
3978 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07003979}
3980
Tejun Heo0942eee2013-08-08 20:11:26 -04003981/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003982 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003983 * @it: the task iterator to finish
3984 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003985 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003986 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003987void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003988{
Tejun Heoed27b9f2015-10-15 16:41:52 -04003989 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003990 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003991 list_del(&it->iters_node);
3992 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003993 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003994 }
3995
3996 if (it->cur_task)
3997 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07003998}
3999
4000/**
4001 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4002 * @to: cgroup to which the tasks will be moved
4003 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004004 *
4005 * Locking rules between cgroup_post_fork() and the migration path
4006 * guarantee that, if a task is forking while being migrated, the new child
4007 * is guaranteed to be either visible in the source cgroup after the
4008 * parent's migration is complete or put into the target cgroup. No task
4009 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004010 */
4011int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4012{
Tejun Heo952aaa12014-02-25 10:04:03 -05004013 LIST_HEAD(preloaded_csets);
4014 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004015 struct css_task_iter it;
4016 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004017 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004018
Tejun Heo952aaa12014-02-25 10:04:03 -05004019 mutex_lock(&cgroup_mutex);
4020
4021 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004022 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004023 list_for_each_entry(link, &from->cset_links, cset_link)
4024 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004025 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004026
4027 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4028 if (ret)
4029 goto out_err;
4030
4031 /*
4032 * Migrate tasks one-by-one until @form is empty. This fails iff
4033 * ->can_attach() fails.
4034 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004035 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004036 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004037 task = css_task_iter_next(&it);
4038 if (task)
4039 get_task_struct(task);
4040 css_task_iter_end(&it);
4041
4042 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004043 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004044 put_task_struct(task);
4045 }
4046 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004047out_err:
4048 cgroup_migrate_finish(&preloaded_csets);
4049 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004050 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004051}
4052
Paul Menage817929e2007-10-18 23:39:36 -07004053/*
Ben Blum102a7752009-09-23 15:56:26 -07004054 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004055 *
4056 * Reading this file can return large amounts of data if a cgroup has
4057 * *lots* of attached tasks. So it may need several calls to read(),
4058 * but we cannot guarantee that the information we produce is correct
4059 * unless we produce it entirely atomically.
4060 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004061 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004062
Li Zefan24528252012-01-20 11:58:43 +08004063/* which pidlist file are we talking about? */
4064enum cgroup_filetype {
4065 CGROUP_FILE_PROCS,
4066 CGROUP_FILE_TASKS,
4067};
4068
4069/*
4070 * A pidlist is a list of pids that virtually represents the contents of one
4071 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4072 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4073 * to the cgroup.
4074 */
4075struct cgroup_pidlist {
4076 /*
4077 * used to find which pidlist is wanted. doesn't change as long as
4078 * this particular list stays in the list.
4079 */
4080 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4081 /* array of xids */
4082 pid_t *list;
4083 /* how many elements the above list has */
4084 int length;
Li Zefan24528252012-01-20 11:58:43 +08004085 /* each of these stored in a list by its cgroup */
4086 struct list_head links;
4087 /* pointer to the cgroup we belong to, for list removal purposes */
4088 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004089 /* for delayed destruction */
4090 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004091};
4092
Paul Menagebbcb81d2007-10-18 23:39:32 -07004093/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004094 * The following two functions "fix" the issue where there are more pids
4095 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4096 * TODO: replace with a kernel-wide solution to this problem
4097 */
4098#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4099static void *pidlist_allocate(int count)
4100{
4101 if (PIDLIST_TOO_LARGE(count))
4102 return vmalloc(count * sizeof(pid_t));
4103 else
4104 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4105}
Tejun Heob1a21362013-11-29 10:42:58 -05004106
Ben Blumd1d9fd32009-09-23 15:56:28 -07004107static void pidlist_free(void *p)
4108{
Bandan Das58794512015-03-02 17:51:10 -05004109 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004110}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004111
4112/*
Tejun Heob1a21362013-11-29 10:42:58 -05004113 * Used to destroy all pidlists lingering waiting for destroy timer. None
4114 * should be left afterwards.
4115 */
4116static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4117{
4118 struct cgroup_pidlist *l, *tmp_l;
4119
4120 mutex_lock(&cgrp->pidlist_mutex);
4121 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4122 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4123 mutex_unlock(&cgrp->pidlist_mutex);
4124
4125 flush_workqueue(cgroup_pidlist_destroy_wq);
4126 BUG_ON(!list_empty(&cgrp->pidlists));
4127}
4128
4129static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4130{
4131 struct delayed_work *dwork = to_delayed_work(work);
4132 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4133 destroy_dwork);
4134 struct cgroup_pidlist *tofree = NULL;
4135
4136 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004137
4138 /*
Tejun Heo04502362013-11-29 10:42:59 -05004139 * Destroy iff we didn't get queued again. The state won't change
4140 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004141 */
Tejun Heo04502362013-11-29 10:42:59 -05004142 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004143 list_del(&l->links);
4144 pidlist_free(l->list);
4145 put_pid_ns(l->key.ns);
4146 tofree = l;
4147 }
4148
Tejun Heob1a21362013-11-29 10:42:58 -05004149 mutex_unlock(&l->owner->pidlist_mutex);
4150 kfree(tofree);
4151}
4152
4153/*
Ben Blum102a7752009-09-23 15:56:26 -07004154 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004155 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004156 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004157static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004158{
Ben Blum102a7752009-09-23 15:56:26 -07004159 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004160
4161 /*
4162 * we presume the 0th element is unique, so i starts at 1. trivial
4163 * edge cases first; no work needs to be done for either
4164 */
4165 if (length == 0 || length == 1)
4166 return length;
4167 /* src and dest walk down the list; dest counts unique elements */
4168 for (src = 1; src < length; src++) {
4169 /* find next unique element */
4170 while (list[src] == list[src-1]) {
4171 src++;
4172 if (src == length)
4173 goto after;
4174 }
4175 /* dest always points to where the next unique element goes */
4176 list[dest] = list[src];
4177 dest++;
4178 }
4179after:
Ben Blum102a7752009-09-23 15:56:26 -07004180 return dest;
4181}
4182
Tejun Heoafb2bc12013-11-29 10:42:59 -05004183/*
4184 * The two pid files - task and cgroup.procs - guaranteed that the result
4185 * is sorted, which forced this whole pidlist fiasco. As pid order is
4186 * different per namespace, each namespace needs differently sorted list,
4187 * making it impossible to use, for example, single rbtree of member tasks
4188 * sorted by task pointer. As pidlists can be fairly large, allocating one
4189 * per open file is dangerous, so cgroup had to implement shared pool of
4190 * pidlists keyed by cgroup and namespace.
4191 *
4192 * All this extra complexity was caused by the original implementation
4193 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004194 * want to do away with it. Explicitly scramble sort order if on the
4195 * default hierarchy so that no such expectation exists in the new
4196 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004197 *
4198 * Scrambling is done by swapping every two consecutive bits, which is
4199 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4200 */
4201static pid_t pid_fry(pid_t pid)
4202{
4203 unsigned a = pid & 0x55555555;
4204 unsigned b = pid & 0xAAAAAAAA;
4205
4206 return (a << 1) | (b >> 1);
4207}
4208
4209static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4210{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004211 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004212 return pid_fry(pid);
4213 else
4214 return pid;
4215}
4216
Ben Blum102a7752009-09-23 15:56:26 -07004217static int cmppid(const void *a, const void *b)
4218{
4219 return *(pid_t *)a - *(pid_t *)b;
4220}
4221
Tejun Heoafb2bc12013-11-29 10:42:59 -05004222static int fried_cmppid(const void *a, const void *b)
4223{
4224 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4225}
4226
Ben Blum72a8cb32009-09-23 15:56:27 -07004227static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4228 enum cgroup_filetype type)
4229{
4230 struct cgroup_pidlist *l;
4231 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004232 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004233
Tejun Heoe6b81712013-11-29 10:42:59 -05004234 lockdep_assert_held(&cgrp->pidlist_mutex);
4235
4236 list_for_each_entry(l, &cgrp->pidlists, links)
4237 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004238 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004239 return NULL;
4240}
4241
Ben Blum72a8cb32009-09-23 15:56:27 -07004242/*
4243 * find the appropriate pidlist for our purpose (given procs vs tasks)
4244 * returns with the lock on that pidlist already held, and takes care
4245 * of the use count, or returns NULL with no locks held if we're out of
4246 * memory.
4247 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004248static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4249 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004250{
4251 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004252
Tejun Heoe6b81712013-11-29 10:42:59 -05004253 lockdep_assert_held(&cgrp->pidlist_mutex);
4254
4255 l = cgroup_pidlist_find(cgrp, type);
4256 if (l)
4257 return l;
4258
Ben Blum72a8cb32009-09-23 15:56:27 -07004259 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004260 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004261 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004262 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004263
Tejun Heob1a21362013-11-29 10:42:58 -05004264 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004265 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004266 /* don't need task_nsproxy() if we're looking at ourself */
4267 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004268 l->owner = cgrp;
4269 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004270 return l;
4271}
4272
4273/*
Ben Blum102a7752009-09-23 15:56:26 -07004274 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4275 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004276static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4277 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004278{
4279 pid_t *array;
4280 int length;
4281 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004282 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004283 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004284 struct cgroup_pidlist *l;
4285
Tejun Heo4bac00d2013-11-29 10:42:59 -05004286 lockdep_assert_held(&cgrp->pidlist_mutex);
4287
Ben Blum102a7752009-09-23 15:56:26 -07004288 /*
4289 * If cgroup gets more users after we read count, we won't have
4290 * enough space - tough. This race is indistinguishable to the
4291 * caller from the case that the additional cgroup users didn't
4292 * show up until sometime later on.
4293 */
4294 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004295 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004296 if (!array)
4297 return -ENOMEM;
4298 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004299 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004300 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004301 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004302 break;
Ben Blum102a7752009-09-23 15:56:26 -07004303 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004304 if (type == CGROUP_FILE_PROCS)
4305 pid = task_tgid_vnr(tsk);
4306 else
4307 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004308 if (pid > 0) /* make sure to only use valid results */
4309 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004310 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004311 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004312 length = n;
4313 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004314 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004315 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4316 else
4317 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004318 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004319 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004320
Tejun Heoe6b81712013-11-29 10:42:59 -05004321 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004322 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004323 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004324 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004325 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004326
4327 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004328 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004329 l->list = array;
4330 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004331 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004332 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004333}
4334
Balbir Singh846c7bb2007-10-18 23:39:44 -07004335/**
Li Zefana043e3b2008-02-23 15:24:09 -08004336 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004337 * @stats: cgroupstats to fill information into
4338 * @dentry: A dentry entry belonging to the cgroup for which stats have
4339 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004340 *
4341 * Build and fill cgroupstats so that taskstats can export it to user
4342 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004343 */
4344int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4345{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004346 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004347 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004348 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004349 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004350
Tejun Heo2bd59d42014-02-11 11:52:49 -05004351 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4352 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4353 kernfs_type(kn) != KERNFS_DIR)
4354 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004355
Li Zefanbad34662014-02-14 16:54:28 +08004356 mutex_lock(&cgroup_mutex);
4357
Tejun Heo2bd59d42014-02-11 11:52:49 -05004358 /*
4359 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004360 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004361 * @kn->priv is RCU safe. Let's do the RCU dancing.
4362 */
4363 rcu_read_lock();
4364 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004365 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004366 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004367 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004368 return -ENOENT;
4369 }
Li Zefanbad34662014-02-14 16:54:28 +08004370 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004371
Tejun Heo9d800df2014-05-14 09:15:00 -04004372 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004373 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004374 switch (tsk->state) {
4375 case TASK_RUNNING:
4376 stats->nr_running++;
4377 break;
4378 case TASK_INTERRUPTIBLE:
4379 stats->nr_sleeping++;
4380 break;
4381 case TASK_UNINTERRUPTIBLE:
4382 stats->nr_uninterruptible++;
4383 break;
4384 case TASK_STOPPED:
4385 stats->nr_stopped++;
4386 break;
4387 default:
4388 if (delayacct_is_task_waiting_on_io(tsk))
4389 stats->nr_io_wait++;
4390 break;
4391 }
4392 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004393 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004394
Li Zefanbad34662014-02-14 16:54:28 +08004395 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004396 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004397}
4398
Paul Menage8f3ff202009-09-23 15:56:25 -07004399
Paul Menagecc31edc2008-10-18 20:28:04 -07004400/*
Ben Blum102a7752009-09-23 15:56:26 -07004401 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004402 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004403 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004404 */
4405
Ben Blum102a7752009-09-23 15:56:26 -07004406static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004407{
4408 /*
4409 * Initially we receive a position value that corresponds to
4410 * one more than the last pid shown (or 0 on the first call or
4411 * after a seek to the start). Use a binary-search to find the
4412 * next pid to display, if any
4413 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004414 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004415 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004416 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004417 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004418 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004419 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004420
Tejun Heo4bac00d2013-11-29 10:42:59 -05004421 mutex_lock(&cgrp->pidlist_mutex);
4422
4423 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004424 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004425 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004426 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004427 * could already have been destroyed.
4428 */
Tejun Heo5d224442013-12-05 12:28:04 -05004429 if (of->priv)
4430 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004431
4432 /*
4433 * Either this is the first start() after open or the matching
4434 * pidlist has been destroyed inbetween. Create a new one.
4435 */
Tejun Heo5d224442013-12-05 12:28:04 -05004436 if (!of->priv) {
4437 ret = pidlist_array_load(cgrp, type,
4438 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004439 if (ret)
4440 return ERR_PTR(ret);
4441 }
Tejun Heo5d224442013-12-05 12:28:04 -05004442 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004443
Paul Menagecc31edc2008-10-18 20:28:04 -07004444 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004445 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004446
Paul Menagecc31edc2008-10-18 20:28:04 -07004447 while (index < end) {
4448 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004449 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004450 index = mid;
4451 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004452 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004453 index = mid + 1;
4454 else
4455 end = mid;
4456 }
4457 }
4458 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004459 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004460 return NULL;
4461 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004462 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004463 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004464 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004465}
4466
Ben Blum102a7752009-09-23 15:56:26 -07004467static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004468{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004469 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004470 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004471
Tejun Heo5d224442013-12-05 12:28:04 -05004472 if (l)
4473 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004474 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004475 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004476}
4477
Ben Blum102a7752009-09-23 15:56:26 -07004478static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004479{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004480 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004481 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004482 pid_t *p = v;
4483 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004484 /*
4485 * Advance to the next pid in the array. If this goes off the
4486 * end, we're done
4487 */
4488 p++;
4489 if (p >= end) {
4490 return NULL;
4491 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004492 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004493 return p;
4494 }
4495}
4496
Ben Blum102a7752009-09-23 15:56:26 -07004497static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004498{
Joe Perches94ff2122015-04-15 16:18:20 -07004499 seq_printf(s, "%d\n", *(int *)v);
4500
4501 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004502}
4503
Tejun Heo182446d2013-08-08 20:11:24 -04004504static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4505 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004506{
Tejun Heo182446d2013-08-08 20:11:24 -04004507 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004508}
4509
Tejun Heo182446d2013-08-08 20:11:24 -04004510static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4511 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004512{
Paul Menage6379c102008-07-25 01:47:01 -07004513 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004514 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004515 else
Tejun Heo182446d2013-08-08 20:11:24 -04004516 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004517 return 0;
4518}
4519
Tejun Heo182446d2013-08-08 20:11:24 -04004520static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4521 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004522{
Tejun Heo182446d2013-08-08 20:11:24 -04004523 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004524}
4525
Tejun Heo182446d2013-08-08 20:11:24 -04004526static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4527 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004528{
4529 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004530 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004531 else
Tejun Heo182446d2013-08-08 20:11:24 -04004532 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004533 return 0;
4534}
4535
Tejun Heoa14c6872014-07-15 11:05:09 -04004536/* cgroup core interface files for the default hierarchy */
4537static struct cftype cgroup_dfl_base_files[] = {
4538 {
4539 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004540 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004541 .seq_start = cgroup_pidlist_start,
4542 .seq_next = cgroup_pidlist_next,
4543 .seq_stop = cgroup_pidlist_stop,
4544 .seq_show = cgroup_pidlist_show,
4545 .private = CGROUP_FILE_PROCS,
4546 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004547 },
4548 {
4549 .name = "cgroup.controllers",
4550 .flags = CFTYPE_ONLY_ON_ROOT,
4551 .seq_show = cgroup_root_controllers_show,
4552 },
4553 {
4554 .name = "cgroup.controllers",
4555 .flags = CFTYPE_NOT_ON_ROOT,
4556 .seq_show = cgroup_controllers_show,
4557 },
4558 {
4559 .name = "cgroup.subtree_control",
4560 .seq_show = cgroup_subtree_control_show,
4561 .write = cgroup_subtree_control_write,
4562 },
4563 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004564 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004565 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004566 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004567 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004568 },
4569 { } /* terminate */
4570};
4571
4572/* cgroup core interface files for the legacy hierarchies */
4573static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004574 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004575 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004576 .seq_start = cgroup_pidlist_start,
4577 .seq_next = cgroup_pidlist_next,
4578 .seq_stop = cgroup_pidlist_stop,
4579 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004580 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004581 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004582 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004583 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004584 .name = "cgroup.clone_children",
4585 .read_u64 = cgroup_clone_children_read,
4586 .write_u64 = cgroup_clone_children_write,
4587 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004588 {
Tejun Heo873fe092013-04-14 20:15:26 -07004589 .name = "cgroup.sane_behavior",
4590 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004591 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004592 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004593 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004594 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004595 .seq_start = cgroup_pidlist_start,
4596 .seq_next = cgroup_pidlist_next,
4597 .seq_stop = cgroup_pidlist_stop,
4598 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004599 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004600 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004601 },
4602 {
4603 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004604 .read_u64 = cgroup_read_notify_on_release,
4605 .write_u64 = cgroup_write_notify_on_release,
4606 },
Tejun Heo873fe092013-04-14 20:15:26 -07004607 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004608 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004609 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004610 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004611 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004612 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004613 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004614 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004615};
4616
Tejun Heo0c21ead2013-08-13 20:22:51 -04004617/*
4618 * css destruction is four-stage process.
4619 *
4620 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4621 * Implemented in kill_css().
4622 *
4623 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004624 * and thus css_tryget_online() is guaranteed to fail, the css can be
4625 * offlined by invoking offline_css(). After offlining, the base ref is
4626 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004627 *
4628 * 3. When the percpu_ref reaches zero, the only possible remaining
4629 * accessors are inside RCU read sections. css_release() schedules the
4630 * RCU callback.
4631 *
4632 * 4. After the grace period, the css can be freed. Implemented in
4633 * css_free_work_fn().
4634 *
4635 * It is actually hairier because both step 2 and 4 require process context
4636 * and thus involve punting to css->destroy_work adding two additional
4637 * steps to the already complex sequence.
4638 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004639static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004640{
4641 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004642 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004643 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004644 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004645
Tejun Heo9a1049d2014-06-28 08:10:14 -04004646 percpu_ref_exit(&css->refcnt);
4647
Vladimir Davydov01e58652015-02-12 14:59:26 -08004648 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004649 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004650 int id = css->id;
4651
Tejun Heo9d755d32014-05-14 09:15:02 -04004652 if (css->parent)
4653 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004654
Vladimir Davydov01e58652015-02-12 14:59:26 -08004655 ss->css_free(css);
4656 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004657 cgroup_put(cgrp);
4658 } else {
4659 /* cgroup free path */
4660 atomic_dec(&cgrp->root->nr_cgrps);
4661 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004662 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004663
Tejun Heod51f39b2014-05-16 13:22:48 -04004664 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004665 /*
4666 * We get a ref to the parent, and put the ref when
4667 * this cgroup is being freed, so it's guaranteed
4668 * that the parent won't be destroyed before its
4669 * children.
4670 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004671 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004672 kernfs_put(cgrp->kn);
4673 kfree(cgrp);
4674 } else {
4675 /*
4676 * This is root cgroup's refcnt reaching zero,
4677 * which indicates that the root should be
4678 * released.
4679 */
4680 cgroup_destroy_root(cgrp->root);
4681 }
4682 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004683}
4684
4685static void css_free_rcu_fn(struct rcu_head *rcu_head)
4686{
4687 struct cgroup_subsys_state *css =
4688 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4689
Tejun Heo0c21ead2013-08-13 20:22:51 -04004690 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004691 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004692}
4693
Tejun Heo25e15d82014-05-14 09:15:02 -04004694static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004695{
4696 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004697 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004698 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004699 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004700
Tejun Heo1fed1b22014-05-16 13:22:49 -04004701 mutex_lock(&cgroup_mutex);
4702
Tejun Heode3f0342014-05-16 13:22:49 -04004703 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004704 list_del_rcu(&css->sibling);
4705
Tejun Heo9d755d32014-05-14 09:15:02 -04004706 if (ss) {
4707 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004708 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004709 if (ss->css_released)
4710 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004711 } else {
4712 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004713 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4714 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004715
4716 /*
4717 * There are two control paths which try to determine
4718 * cgroup from dentry without going through kernfs -
4719 * cgroupstats_build() and css_tryget_online_from_dir().
4720 * Those are supported by RCU protecting clearing of
4721 * cgrp->kn->priv backpointer.
4722 */
4723 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004724 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004725
Tejun Heo1fed1b22014-05-16 13:22:49 -04004726 mutex_unlock(&cgroup_mutex);
4727
Tejun Heo0c21ead2013-08-13 20:22:51 -04004728 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004729}
4730
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004731static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004732{
4733 struct cgroup_subsys_state *css =
4734 container_of(ref, struct cgroup_subsys_state, refcnt);
4735
Tejun Heo25e15d82014-05-14 09:15:02 -04004736 INIT_WORK(&css->destroy_work, css_release_work_fn);
4737 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004738}
4739
Tejun Heoddfcada2014-05-04 15:09:14 -04004740static void init_and_link_css(struct cgroup_subsys_state *css,
4741 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004743 lockdep_assert_held(&cgroup_mutex);
4744
Tejun Heoddfcada2014-05-04 15:09:14 -04004745 cgroup_get(cgrp);
4746
Tejun Heod5c419b2014-05-16 13:22:48 -04004747 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004748 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004749 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004750 INIT_LIST_HEAD(&css->sibling);
4751 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004752 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004753
Tejun Heod51f39b2014-05-16 13:22:48 -04004754 if (cgroup_parent(cgrp)) {
4755 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004756 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004757 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004758
Tejun Heoca8bdca2013-08-26 18:40:56 -04004759 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004760}
4761
Li Zefan2a4ac632013-07-31 16:16:40 +08004762/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004763static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004764{
Tejun Heo623f9262013-08-13 11:01:55 -04004765 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004766 int ret = 0;
4767
Tejun Heoa31f2d32012-11-19 08:13:37 -08004768 lockdep_assert_held(&cgroup_mutex);
4769
Tejun Heo92fb9742012-11-19 08:13:38 -08004770 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004771 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004772 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004773 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004774 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004775 }
Tejun Heob1929db2012-11-19 08:13:38 -08004776 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004777}
4778
Li Zefan2a4ac632013-07-31 16:16:40 +08004779/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004780static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004781{
Tejun Heo623f9262013-08-13 11:01:55 -04004782 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004783
4784 lockdep_assert_held(&cgroup_mutex);
4785
4786 if (!(css->flags & CSS_ONLINE))
4787 return;
4788
Li Zefand7eeac12013-03-12 15:35:59 -07004789 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004790 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004791
Tejun Heoeb954192013-08-08 20:11:23 -04004792 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004793 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004794
4795 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004796}
4797
Tejun Heoc81c925a2013-12-06 15:11:56 -05004798/**
4799 * create_css - create a cgroup_subsys_state
4800 * @cgrp: the cgroup new css will be associated with
4801 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004802 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004803 *
4804 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004805 * css is online and installed in @cgrp with all interface files created if
4806 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004807 */
Tejun Heof63070d2014-07-08 18:02:57 -04004808static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4809 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004810{
Tejun Heod51f39b2014-05-16 13:22:48 -04004811 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004812 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004813 struct cgroup_subsys_state *css;
4814 int err;
4815
Tejun Heoc81c925a2013-12-06 15:11:56 -05004816 lockdep_assert_held(&cgroup_mutex);
4817
Tejun Heo1fed1b22014-05-16 13:22:49 -04004818 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004819 if (IS_ERR(css))
4820 return PTR_ERR(css);
4821
Tejun Heoddfcada2014-05-04 15:09:14 -04004822 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004823
Tejun Heo2aad2a82014-09-24 13:31:50 -04004824 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004825 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004826 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004827
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004828 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004829 if (err < 0)
4830 goto err_free_percpu_ref;
4831 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004832
Tejun Heof63070d2014-07-08 18:02:57 -04004833 if (visible) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04004834 err = css_populate_dir(css, NULL);
Tejun Heof63070d2014-07-08 18:02:57 -04004835 if (err)
4836 goto err_free_id;
4837 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004838
4839 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004840 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004841 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004842
4843 err = online_css(css);
4844 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004845 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004846
Tejun Heoc81c925a2013-12-06 15:11:56 -05004847 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004848 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004849 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 -04004850 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004851 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004852 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004853 ss->warned_broken_hierarchy = true;
4854 }
4855
4856 return 0;
4857
Tejun Heo1fed1b22014-05-16 13:22:49 -04004858err_list_del:
4859 list_del_rcu(&css->sibling);
Tejun Heo4df8dc92015-09-18 17:54:23 -04004860 css_clear_dir(css, NULL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004861err_free_id:
4862 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004863err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004864 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004865err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004866 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004867 return err;
4868}
4869
Tejun Heob3bfd982014-05-13 12:19:22 -04004870static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4871 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004872{
Tejun Heoa9746d82014-05-13 12:19:22 -04004873 struct cgroup *parent, *cgrp;
4874 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004875 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004876 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004877 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004878
Alban Crequy71b1fb52014-08-18 12:20:20 +01004879 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4880 */
4881 if (strchr(name, '\n'))
4882 return -EINVAL;
4883
Tejun Heoa9746d82014-05-13 12:19:22 -04004884 parent = cgroup_kn_lock_live(parent_kn);
4885 if (!parent)
4886 return -ENODEV;
4887 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004888
Tejun Heo0a950f62012-11-19 09:02:12 -08004889 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004890 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004891 if (!cgrp) {
4892 ret = -ENOMEM;
4893 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004894 }
4895
Tejun Heo2aad2a82014-09-24 13:31:50 -04004896 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004897 if (ret)
4898 goto out_free_cgrp;
4899
Li Zefan0ab02ca2014-02-11 16:05:46 +08004900 /*
4901 * Temporarily set the pointer to NULL, so idr_find() won't return
4902 * a half-baked cgroup.
4903 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004904 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004905 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004906 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004907 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004908 }
4909
Paul Menagecc31edc2008-10-18 20:28:04 -07004910 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004911
Tejun Heo9d800df2014-05-14 09:15:00 -04004912 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004913 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004914
Li Zefanb6abdb02008-03-04 14:28:19 -08004915 if (notify_on_release(parent))
4916 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4917
Tejun Heo2260e7f2012-11-19 08:13:38 -08004918 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4919 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004920
Tejun Heo2bd59d42014-02-11 11:52:49 -05004921 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004922 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004923 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004924 ret = PTR_ERR(kn);
4925 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004926 }
4927 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004928
Tejun Heo6f305582014-02-12 09:29:50 -05004929 /*
4930 * This extra ref will be put in cgroup_free_fn() and guarantees
4931 * that @cgrp->kn is always accessible.
4932 */
4933 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004934
Tejun Heo0cb51d72014-05-16 13:22:49 -04004935 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004936
Tejun Heo4e139af2012-11-19 08:13:36 -08004937 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004938 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004939 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004940 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004941
Tejun Heo0d802552013-12-06 15:11:56 -05004942 /*
4943 * @cgrp is now fully operational. If something fails after this
4944 * point, it'll be released via the normal destruction path.
4945 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004946 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004947
Tejun Heoba0f4d72014-05-13 12:19:22 -04004948 ret = cgroup_kn_set_ugid(kn);
4949 if (ret)
4950 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004951
Tejun Heo4df8dc92015-09-18 17:54:23 -04004952 ret = css_populate_dir(&cgrp->self, NULL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004953 if (ret)
4954 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004955
Tejun Heo9d403e92013-12-06 15:11:56 -05004956 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004957 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004958 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004959 ret = create_css(cgrp, ss,
4960 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004961 if (ret)
4962 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004963 }
Tejun Heoa8638032012-11-09 09:12:29 -08004964 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004965
Tejun Heobd53d612014-04-23 11:13:16 -04004966 /*
4967 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004968 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004969 */
Tejun Heo667c2492014-07-08 18:02:56 -04004970 if (!cgroup_on_dfl(cgrp)) {
4971 cgrp->subtree_control = parent->subtree_control;
4972 cgroup_refresh_child_subsys_mask(cgrp);
4973 }
Tejun Heof392e512014-04-23 11:13:14 -04004974
Tejun Heo2bd59d42014-02-11 11:52:49 -05004975 kernfs_activate(kn);
4976
Tejun Heoba0f4d72014-05-13 12:19:22 -04004977 ret = 0;
4978 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004979
Tejun Heoba0f4d72014-05-13 12:19:22 -04004980out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004981 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004982out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004983 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004984out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004985 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004986out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004987 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004988 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004989
4990out_destroy:
4991 cgroup_destroy_locked(cgrp);
4992 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004993}
4994
Tejun Heo223dbc32013-08-13 20:22:50 -04004995/*
4996 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004997 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4998 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004999 */
5000static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005001{
Tejun Heo223dbc32013-08-13 20:22:50 -04005002 struct cgroup_subsys_state *css =
5003 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005004
Tejun Heof20104d2013-08-13 20:22:50 -04005005 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005006 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04005007 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005008
Tejun Heo09a503ea2013-08-13 20:22:50 -04005009 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07005010}
5011
Tejun Heo223dbc32013-08-13 20:22:50 -04005012/* css kill confirmation processing requires process context, bounce */
5013static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005014{
5015 struct cgroup_subsys_state *css =
5016 container_of(ref, struct cgroup_subsys_state, refcnt);
5017
Tejun Heo223dbc32013-08-13 20:22:50 -04005018 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05005019 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005020}
5021
Tejun Heof392e512014-04-23 11:13:14 -04005022/**
5023 * kill_css - destroy a css
5024 * @css: css to destroy
5025 *
5026 * This function initiates destruction of @css by removing cgroup interface
5027 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005028 * asynchronously once css_tryget_online() is guaranteed to fail and when
5029 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005030 */
5031static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005032{
Tejun Heo01f64742014-05-13 12:19:23 -04005033 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005034
Tejun Heo2bd59d42014-02-11 11:52:49 -05005035 /*
5036 * This must happen before css is disassociated with its cgroup.
5037 * See seq_css() for details.
5038 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005039 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005040
Tejun Heoedae0c32013-08-13 20:22:51 -04005041 /*
5042 * Killing would put the base ref, but we need to keep it alive
5043 * until after ->css_offline().
5044 */
5045 css_get(css);
5046
5047 /*
5048 * cgroup core guarantees that, by the time ->css_offline() is
5049 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005050 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005051 * proceed to offlining css's because percpu_ref_kill() doesn't
5052 * guarantee that the ref is seen as killed on all CPUs on return.
5053 *
5054 * Use percpu_ref_kill_and_confirm() to get notifications as each
5055 * css is confirmed to be seen as killed on all CPUs.
5056 */
5057 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005058}
5059
5060/**
5061 * cgroup_destroy_locked - the first stage of cgroup destruction
5062 * @cgrp: cgroup to be destroyed
5063 *
5064 * css's make use of percpu refcnts whose killing latency shouldn't be
5065 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005066 * guarantee that css_tryget_online() won't succeed by the time
5067 * ->css_offline() is invoked. To satisfy all the requirements,
5068 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005069 *
5070 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5071 * userland visible parts and start killing the percpu refcnts of
5072 * css's. Set up so that the next stage will be kicked off once all
5073 * the percpu refcnts are confirmed to be killed.
5074 *
5075 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5076 * rest of destruction. Once all cgroup references are gone, the
5077 * cgroup is RCU-freed.
5078 *
5079 * This function implements s1. After this step, @cgrp is gone as far as
5080 * the userland is concerned and a new cgroup with the same name may be
5081 * created. As cgroup doesn't care about the names internally, this
5082 * doesn't cause any problem.
5083 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005084static int cgroup_destroy_locked(struct cgroup *cgrp)
5085 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005086{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005087 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005088 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005089
Tejun Heo42809dd2012-11-19 08:13:37 -08005090 lockdep_assert_held(&cgroup_mutex);
5091
Tejun Heo91486f62015-10-15 16:41:51 -04005092 /*
5093 * Only migration can raise populated from zero and we're already
5094 * holding cgroup_mutex.
5095 */
5096 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005097 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005098
Tejun Heo1a90dd52012-11-05 09:16:59 -08005099 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005100 * Make sure there's no live children. We can't test emptiness of
5101 * ->self.children as dead children linger on it while being
5102 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005103 */
Tejun Heof3d46502014-05-16 13:22:52 -04005104 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005105 return -EBUSY;
5106
5107 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005108 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005109 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005110 */
Tejun Heo184faf32014-05-16 13:22:51 -04005111 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005112
Tejun Heo249f3462014-05-14 09:15:01 -04005113 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005114 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005115 kill_css(css);
5116
Tejun Heo455050d2013-06-13 19:27:41 -07005117 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005118 * Remove @cgrp directory along with the base files. @cgrp has an
5119 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005120 */
Tejun Heo01f64742014-05-13 12:19:23 -04005121 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005122
Tejun Heod51f39b2014-05-16 13:22:48 -04005123 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005124
Tejun Heo249f3462014-05-14 09:15:01 -04005125 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005126 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005127
Tejun Heoea15f8c2013-06-13 19:27:42 -07005128 return 0;
5129};
5130
Tejun Heo2bd59d42014-02-11 11:52:49 -05005131static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005132{
Tejun Heoa9746d82014-05-13 12:19:22 -04005133 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005134 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005135
Tejun Heoa9746d82014-05-13 12:19:22 -04005136 cgrp = cgroup_kn_lock_live(kn);
5137 if (!cgrp)
5138 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005139
Tejun Heoa9746d82014-05-13 12:19:22 -04005140 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005141
Tejun Heoa9746d82014-05-13 12:19:22 -04005142 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005143 return ret;
5144}
5145
Tejun Heo2bd59d42014-02-11 11:52:49 -05005146static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5147 .remount_fs = cgroup_remount,
5148 .show_options = cgroup_show_options,
5149 .mkdir = cgroup_mkdir,
5150 .rmdir = cgroup_rmdir,
5151 .rename = cgroup_rename,
5152};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005153
Tejun Heo15a4c832014-05-04 15:09:14 -04005154static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005155{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005156 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005157
5158 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005159
Tejun Heo648bb562012-11-19 08:13:36 -08005160 mutex_lock(&cgroup_mutex);
5161
Tejun Heo15a4c832014-05-04 15:09:14 -04005162 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005163 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005164
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005165 /* Create the root cgroup state for this subsystem */
5166 ss->root = &cgrp_dfl_root;
5167 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005168 /* We don't handle early failures gracefully */
5169 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005170 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005171
5172 /*
5173 * Root csses are never destroyed and we can't initialize
5174 * percpu_ref during early init. Disable refcnting.
5175 */
5176 css->flags |= CSS_NO_REF;
5177
Tejun Heo15a4c832014-05-04 15:09:14 -04005178 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005179 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005180 css->id = 1;
5181 } else {
5182 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5183 BUG_ON(css->id < 0);
5184 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005185
Li Zefane8d55fd2008-04-29 01:00:13 -07005186 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005187 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005188 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005189 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005190 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005191
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005192 have_fork_callback |= (bool)ss->fork << ss->id;
5193 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005194 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005195 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005196
Li Zefane8d55fd2008-04-29 01:00:13 -07005197 /* At system boot, before all subsystems have been
5198 * registered, no tasks have been forked, so we don't
5199 * need to invoke fork callbacks here. */
5200 BUG_ON(!list_empty(&init_task.tasks));
5201
Tejun Heoae7f1642013-08-13 20:22:50 -04005202 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005203
Tejun Heo648bb562012-11-19 08:13:36 -08005204 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005205}
5206
5207/**
Li Zefana043e3b2008-02-23 15:24:09 -08005208 * cgroup_init_early - cgroup initialization at system boot
5209 *
5210 * Initialize cgroups at system boot, and initialize any
5211 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005212 */
5213int __init cgroup_init_early(void)
5214{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005215 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005216 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005217 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005218
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005219 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005220 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5221
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005222 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005223
Tejun Heo3ed80a62014-02-08 10:36:58 -05005224 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005225 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05005226 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5227 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005228 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005229 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5230 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005231
Tejun Heoaec25022014-02-08 10:36:58 -05005232 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005233 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005234 if (!ss->legacy_name)
5235 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005236
5237 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005238 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005239 }
5240 return 0;
5241}
5242
Tejun Heoa3e72732015-09-25 16:24:27 -04005243static unsigned long cgroup_disable_mask __initdata;
5244
Paul Menageddbcc7e2007-10-18 23:39:30 -07005245/**
Li Zefana043e3b2008-02-23 15:24:09 -08005246 * cgroup_init - cgroup initialization
5247 *
5248 * Register cgroup filesystem and /proc file, and initialize
5249 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005250 */
5251int __init cgroup_init(void)
5252{
Tejun Heo30159ec2013-06-25 11:53:37 -07005253 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005254 unsigned long key;
Tejun Heo035f4f52015-10-15 17:00:43 -04005255 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005256
Tejun Heo1ed13282015-09-16 12:53:17 -04005257 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005258 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5259 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005260
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005261 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005262
Tejun Heo82fe9b02013-06-25 11:53:37 -07005263 /* Add init_css_set to the hash table */
5264 key = css_set_hash(init_css_set.subsys);
5265 hash_add(css_set_table, &init_css_set.hlist, key);
5266
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005267 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005268
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005269 mutex_unlock(&cgroup_mutex);
5270
Tejun Heo172a2c062014-03-19 10:23:53 -04005271 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005272 if (ss->early_init) {
5273 struct cgroup_subsys_state *css =
5274 init_css_set.subsys[ss->id];
5275
5276 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5277 GFP_KERNEL);
5278 BUG_ON(css->id < 0);
5279 } else {
5280 cgroup_init_subsys(ss, false);
5281 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005282
Tejun Heo2d8f2432014-04-23 11:13:15 -04005283 list_add_tail(&init_css_set.e_cset_node[ssid],
5284 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005285
5286 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005287 * Setting dfl_root subsys_mask needs to consider the
5288 * disabled flag and cftype registration needs kmalloc,
5289 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005290 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005291 if (cgroup_disable_mask & (1 << ssid)) {
5292 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5293 printk(KERN_INFO "Disabling %s control group subsystem\n",
5294 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005295 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005296 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005297
5298 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5299
Tejun Heo5de4fa12014-07-15 11:05:10 -04005300 if (!ss->dfl_cftypes)
5301 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5302
Tejun Heoa8ddc822014-07-15 11:05:10 -04005303 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5304 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5305 } else {
5306 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5307 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005308 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005309
5310 if (ss->bind)
5311 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005312 }
Greg KH676db4a2010-08-05 13:53:35 -07005313
Tejun Heo035f4f52015-10-15 17:00:43 -04005314 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5315 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005316 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005317 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005318
Tejun Heo2bd59d42014-02-11 11:52:49 -05005319 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005320}
Paul Menageb4f48b62007-10-18 23:39:33 -07005321
Tejun Heoe5fca242013-11-22 17:14:39 -05005322static int __init cgroup_wq_init(void)
5323{
5324 /*
5325 * There isn't much point in executing destruction path in
5326 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005327 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005328 *
5329 * We would prefer to do this in cgroup_init() above, but that
5330 * is called before init_workqueues(): so leave this until after.
5331 */
Tejun Heo1a115332014-02-12 19:06:19 -05005332 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005333 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005334
5335 /*
5336 * Used to destroy pidlists and separate to serve as flush domain.
5337 * Cap @max_active to 1 too.
5338 */
5339 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5340 0, 1);
5341 BUG_ON(!cgroup_pidlist_destroy_wq);
5342
Tejun Heoe5fca242013-11-22 17:14:39 -05005343 return 0;
5344}
5345core_initcall(cgroup_wq_init);
5346
Paul Menagea4243162007-10-18 23:39:35 -07005347/*
5348 * proc_cgroup_show()
5349 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5350 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005351 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005352int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5353 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005354{
Tejun Heoe61734c2014-02-12 09:29:50 -05005355 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005356 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005357 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005358
5359 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005360 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005361 if (!buf)
5362 goto out;
5363
Paul Menagea4243162007-10-18 23:39:35 -07005364 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005365 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005366
Tejun Heo985ed672014-03-19 10:23:53 -04005367 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005368 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005369 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005370 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005371
Tejun Heoa2dd4242014-03-19 10:23:55 -04005372 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005373 continue;
5374
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005375 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005376 if (root != &cgrp_dfl_root)
5377 for_each_subsys(ss, ssid)
5378 if (root->subsys_mask & (1 << ssid))
5379 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005380 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005381 if (strlen(root->name))
5382 seq_printf(m, "%sname=%s", count ? "," : "",
5383 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005384 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005385
Paul Menage7717f7b2009-09-23 15:56:22 -07005386 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005387
5388 /*
5389 * On traditional hierarchies, all zombie tasks show up as
5390 * belonging to the root cgroup. On the default hierarchy,
5391 * while a zombie doesn't show up in "cgroup.procs" and
5392 * thus can't be migrated, its /proc/PID/cgroup keeps
5393 * reporting the cgroup it belonged to before exiting. If
5394 * the cgroup is removed before the zombie is reaped,
5395 * " (deleted)" is appended to the cgroup path.
5396 */
5397 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5398 path = cgroup_path(cgrp, buf, PATH_MAX);
5399 if (!path) {
5400 retval = -ENAMETOOLONG;
5401 goto out_unlock;
5402 }
5403 } else {
5404 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005405 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005406
Tejun Heoe61734c2014-02-12 09:29:50 -05005407 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005408
5409 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5410 seq_puts(m, " (deleted)\n");
5411 else
5412 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005413 }
5414
Zefan Li006f4ac2014-09-18 16:03:15 +08005415 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005416out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005417 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005418 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005419 kfree(buf);
5420out:
5421 return retval;
5422}
5423
Paul Menagea4243162007-10-18 23:39:35 -07005424/* Display information about each subsystem and each hierarchy */
5425static int proc_cgroupstats_show(struct seq_file *m, void *v)
5426{
Tejun Heo30159ec2013-06-25 11:53:37 -07005427 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005428 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005429
Paul Menage8bab8dd2008-04-04 14:29:57 -07005430 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005431 /*
5432 * ideally we don't want subsystems moving around while we do this.
5433 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5434 * subsys/hierarchy state.
5435 */
Paul Menagea4243162007-10-18 23:39:35 -07005436 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005437
5438 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005439 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005440 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005441 atomic_read(&ss->root->nr_cgrps),
5442 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005443
Paul Menagea4243162007-10-18 23:39:35 -07005444 mutex_unlock(&cgroup_mutex);
5445 return 0;
5446}
5447
5448static int cgroupstats_open(struct inode *inode, struct file *file)
5449{
Al Viro9dce07f2008-03-29 03:07:28 +00005450 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005451}
5452
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005453static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005454 .open = cgroupstats_open,
5455 .read = seq_read,
5456 .llseek = seq_lseek,
5457 .release = single_release,
5458};
5459
Aleksa Sarai7e476822015-06-09 21:32:09 +10005460static void **subsys_canfork_priv_p(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5461{
5462 if (CGROUP_CANFORK_START <= i && i < CGROUP_CANFORK_END)
5463 return &ss_priv[i - CGROUP_CANFORK_START];
5464 return NULL;
5465}
5466
5467static void *subsys_canfork_priv(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5468{
5469 void **private = subsys_canfork_priv_p(ss_priv, i);
5470 return private ? *private : NULL;
5471}
5472
Paul Menageb4f48b62007-10-18 23:39:33 -07005473/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005474 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005475 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005476 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005477 * A task is associated with the init_css_set until cgroup_post_fork()
5478 * attaches it to the parent's css_set. Empty cg_list indicates that
5479 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005480 */
5481void cgroup_fork(struct task_struct *child)
5482{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005483 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005484 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005485}
5486
5487/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005488 * cgroup_can_fork - called on a new task before the process is exposed
5489 * @child: the task in question.
5490 *
5491 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5492 * returns an error, the fork aborts with that error code. This allows for
5493 * a cgroup subsystem to conditionally allow or deny new forks.
5494 */
5495int cgroup_can_fork(struct task_struct *child,
5496 void *ss_priv[CGROUP_CANFORK_COUNT])
5497{
5498 struct cgroup_subsys *ss;
5499 int i, j, ret;
5500
5501 for_each_subsys_which(ss, i, &have_canfork_callback) {
5502 ret = ss->can_fork(child, subsys_canfork_priv_p(ss_priv, i));
5503 if (ret)
5504 goto out_revert;
5505 }
5506
5507 return 0;
5508
5509out_revert:
5510 for_each_subsys(ss, j) {
5511 if (j >= i)
5512 break;
5513 if (ss->cancel_fork)
5514 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, j));
5515 }
5516
5517 return ret;
5518}
5519
5520/**
5521 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5522 * @child: the task in question
5523 *
5524 * This calls the cancel_fork() callbacks if a fork failed *after*
5525 * cgroup_can_fork() succeded.
5526 */
5527void cgroup_cancel_fork(struct task_struct *child,
5528 void *ss_priv[CGROUP_CANFORK_COUNT])
5529{
5530 struct cgroup_subsys *ss;
5531 int i;
5532
5533 for_each_subsys(ss, i)
5534 if (ss->cancel_fork)
5535 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, i));
5536}
5537
5538/**
Li Zefana043e3b2008-02-23 15:24:09 -08005539 * cgroup_post_fork - called on a new task after adding it to the task list
5540 * @child: the task in question
5541 *
Tejun Heo5edee612012-10-16 15:03:14 -07005542 * Adds the task to the list running through its css_set if necessary and
5543 * call the subsystem fork() callbacks. Has to be after the task is
5544 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005545 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005546 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005547 */
Aleksa Sarai7e476822015-06-09 21:32:09 +10005548void cgroup_post_fork(struct task_struct *child,
5549 void *old_ss_priv[CGROUP_CANFORK_COUNT])
Paul Menage817929e2007-10-18 23:39:36 -07005550{
Tejun Heo30159ec2013-06-25 11:53:37 -07005551 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005552 int i;
5553
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005554 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005555 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005556 * function sets use_task_css_set_links before grabbing
5557 * tasklist_lock and we just went through tasklist_lock to add
5558 * @child, it's guaranteed that either we see the set
5559 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5560 * @child during its iteration.
5561 *
5562 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005563 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005564 * association is stable, and, on completion of the parent's
5565 * migration, @child is visible in the source of migration or
5566 * already in the destination cgroup. This guarantee is necessary
5567 * when implementing operations which need to migrate all tasks of
5568 * a cgroup to another.
5569 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005570 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005571 * will remain in init_css_set. This is safe because all tasks are
5572 * in the init_css_set before cg_links is enabled and there's no
5573 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005574 */
Paul Menage817929e2007-10-18 23:39:36 -07005575 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005576 struct css_set *cset;
5577
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005578 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005579 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005580 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005581 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005582 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005583 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005584 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005585 }
Tejun Heo5edee612012-10-16 15:03:14 -07005586
5587 /*
5588 * Call ss->fork(). This must happen after @child is linked on
5589 * css_set; otherwise, @child might change state between ->fork()
5590 * and addition to css_set.
5591 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005592 for_each_subsys_which(ss, i, &have_fork_callback)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005593 ss->fork(child, subsys_canfork_priv(old_ss_priv, i));
Paul Menage817929e2007-10-18 23:39:36 -07005594}
Tejun Heo5edee612012-10-16 15:03:14 -07005595
Paul Menage817929e2007-10-18 23:39:36 -07005596/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005597 * cgroup_exit - detach cgroup from exiting task
5598 * @tsk: pointer to task_struct of exiting process
5599 *
5600 * Description: Detach cgroup from @tsk and release it.
5601 *
5602 * Note that cgroups marked notify_on_release force every task in
5603 * them to take the global cgroup_mutex mutex when exiting.
5604 * This could impact scaling on very large systems. Be reluctant to
5605 * use notify_on_release cgroups where very high task exit scaling
5606 * is required on large systems.
5607 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005608 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5609 * call cgroup_exit() while the task is still competent to handle
5610 * notify_on_release(), then leave the task attached to the root cgroup in
5611 * each hierarchy for the remainder of its exit. No need to bother with
5612 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005613 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005614 */
Li Zefan1ec41832014-03-28 15:22:19 +08005615void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005616{
Tejun Heo30159ec2013-06-25 11:53:37 -07005617 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005618 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005619 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005620
5621 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005622 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005623 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005624 */
Tejun Heo0de09422015-10-15 16:41:49 -04005625 cset = task_css_set(tsk);
5626
Paul Menage817929e2007-10-18 23:39:36 -07005627 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005628 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005629 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005630 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005631 } else {
5632 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005633 }
5634
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005635 /* see cgroup_post_fork() for details */
Tejun Heo2e91fa72015-10-15 16:41:53 -04005636 for_each_subsys_which(ss, i, &have_exit_callback)
5637 ss->exit(tsk);
5638}
Tejun Heo30159ec2013-06-25 11:53:37 -07005639
Tejun Heo2e91fa72015-10-15 16:41:53 -04005640void cgroup_free(struct task_struct *task)
5641{
5642 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005643 struct cgroup_subsys *ss;
5644 int ssid;
5645
5646 for_each_subsys_which(ss, ssid, &have_free_callback)
5647 ss->free(task);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005648
Tejun Heo2e91fa72015-10-15 16:41:53 -04005649 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005650}
Paul Menage697f4162007-10-18 23:39:34 -07005651
Paul Menagebd89aab2007-10-18 23:40:44 -07005652static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005653{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005654 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005655 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5656 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005657}
5658
Paul Menage81a6a5c2007-10-18 23:39:38 -07005659/*
5660 * Notify userspace when a cgroup is released, by running the
5661 * configured release agent with the name of the cgroup (path
5662 * relative to the root of cgroup file system) as the argument.
5663 *
5664 * Most likely, this user command will try to rmdir this cgroup.
5665 *
5666 * This races with the possibility that some other task will be
5667 * attached to this cgroup before it is removed, or that some other
5668 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5669 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5670 * unused, and this cgroup will be reprieved from its death sentence,
5671 * to continue to serve a useful existence. Next time it's released,
5672 * we will get notified again, if it still has 'notify_on_release' set.
5673 *
5674 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5675 * means only wait until the task is successfully execve()'d. The
5676 * separate release agent task is forked by call_usermodehelper(),
5677 * then control in this thread returns here, without waiting for the
5678 * release agent task. We don't bother to wait because the caller of
5679 * this routine has no use for the exit status of the release agent
5680 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005681 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005682static void cgroup_release_agent(struct work_struct *work)
5683{
Zefan Li971ff492014-09-18 16:06:19 +08005684 struct cgroup *cgrp =
5685 container_of(work, struct cgroup, release_agent_work);
5686 char *pathbuf = NULL, *agentbuf = NULL, *path;
5687 char *argv[3], *envp[3];
5688
Paul Menage81a6a5c2007-10-18 23:39:38 -07005689 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005690
Zefan Li971ff492014-09-18 16:06:19 +08005691 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5692 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5693 if (!pathbuf || !agentbuf)
5694 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005695
Zefan Li971ff492014-09-18 16:06:19 +08005696 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5697 if (!path)
5698 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005699
Zefan Li971ff492014-09-18 16:06:19 +08005700 argv[0] = agentbuf;
5701 argv[1] = path;
5702 argv[2] = NULL;
5703
5704 /* minimal command environment */
5705 envp[0] = "HOME=/";
5706 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5707 envp[2] = NULL;
5708
Paul Menage81a6a5c2007-10-18 23:39:38 -07005709 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005710 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005711 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005712out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005713 mutex_unlock(&cgroup_mutex);
5714out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005715 kfree(agentbuf);
5716 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005717}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005718
5719static int __init cgroup_disable(char *str)
5720{
Tejun Heo30159ec2013-06-25 11:53:37 -07005721 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005722 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005723 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005724
5725 while ((token = strsep(&str, ",")) != NULL) {
5726 if (!*token)
5727 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005728
Tejun Heo3ed80a62014-02-08 10:36:58 -05005729 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005730 if (strcmp(token, ss->name) &&
5731 strcmp(token, ss->legacy_name))
5732 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005733 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005734 }
5735 }
5736 return 1;
5737}
5738__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005739
Tejun Heob77d7b62013-08-13 11:01:54 -04005740/**
Tejun Heoec903c02014-05-13 12:11:01 -04005741 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005742 * @dentry: directory dentry of interest
5743 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005744 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005745 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5746 * to get the corresponding css and return it. If such css doesn't exist
5747 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005748 */
Tejun Heoec903c02014-05-13 12:11:01 -04005749struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5750 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005751{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005752 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5753 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005754 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005755
Tejun Heo35cf0832013-08-26 18:40:56 -04005756 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005757 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5758 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005759 return ERR_PTR(-EBADF);
5760
Tejun Heo5a17f542014-02-11 11:52:47 -05005761 rcu_read_lock();
5762
Tejun Heo2bd59d42014-02-11 11:52:49 -05005763 /*
5764 * This path doesn't originate from kernfs and @kn could already
5765 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005766 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005767 */
5768 cgrp = rcu_dereference(kn->priv);
5769 if (cgrp)
5770 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005771
Tejun Heoec903c02014-05-13 12:11:01 -04005772 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005773 css = ERR_PTR(-ENOENT);
5774
5775 rcu_read_unlock();
5776 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005777}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005778
Li Zefan1cb650b2013-08-19 10:05:24 +08005779/**
5780 * css_from_id - lookup css by id
5781 * @id: the cgroup id
5782 * @ss: cgroup subsys to be looked into
5783 *
5784 * Returns the css if there's valid one with @id, otherwise returns NULL.
5785 * Should be called under rcu_read_lock().
5786 */
5787struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5788{
Tejun Heo6fa49182014-05-04 15:09:13 -04005789 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005790 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005791}
5792
Paul Menagefe693432009-09-23 15:56:20 -07005793#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005794static struct cgroup_subsys_state *
5795debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005796{
5797 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5798
5799 if (!css)
5800 return ERR_PTR(-ENOMEM);
5801
5802 return css;
5803}
5804
Tejun Heoeb954192013-08-08 20:11:23 -04005805static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005806{
Tejun Heoeb954192013-08-08 20:11:23 -04005807 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005808}
5809
Tejun Heo182446d2013-08-08 20:11:24 -04005810static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5811 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005812{
Tejun Heo182446d2013-08-08 20:11:24 -04005813 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005814}
5815
Tejun Heo182446d2013-08-08 20:11:24 -04005816static u64 current_css_set_read(struct cgroup_subsys_state *css,
5817 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005818{
5819 return (u64)(unsigned long)current->cgroups;
5820}
5821
Tejun Heo182446d2013-08-08 20:11:24 -04005822static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005823 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005824{
5825 u64 count;
5826
5827 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005828 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005829 rcu_read_unlock();
5830 return count;
5831}
5832
Tejun Heo2da8ca82013-12-05 12:28:04 -05005833static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005834{
Tejun Heo69d02062013-06-12 21:04:50 -07005835 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005836 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005837 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005838
Tejun Heoe61734c2014-02-12 09:29:50 -05005839 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5840 if (!name_buf)
5841 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005842
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005843 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005844 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005845 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005846 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005847 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005848
Tejun Heoa2dd4242014-03-19 10:23:55 -04005849 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005850 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005851 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005852 }
5853 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005854 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005855 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005856 return 0;
5857}
5858
5859#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005860static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005861{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005862 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005863 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005864
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005865 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005866 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005867 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005868 struct task_struct *task;
5869 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005870
Tejun Heo5abb8852013-06-12 21:04:49 -07005871 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005872
Tejun Heo5abb8852013-06-12 21:04:49 -07005873 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005874 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5875 goto overflow;
5876 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005877 }
Tejun Heoc7561122014-02-25 10:04:01 -05005878
5879 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5880 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5881 goto overflow;
5882 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5883 }
5884 continue;
5885 overflow:
5886 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005887 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005888 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005889 return 0;
5890}
5891
Tejun Heo182446d2013-08-08 20:11:24 -04005892static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005893{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005894 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08005895 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005896}
5897
5898static struct cftype debug_files[] = {
5899 {
Paul Menagefe693432009-09-23 15:56:20 -07005900 .name = "taskcount",
5901 .read_u64 = debug_taskcount_read,
5902 },
5903
5904 {
5905 .name = "current_css_set",
5906 .read_u64 = current_css_set_read,
5907 },
5908
5909 {
5910 .name = "current_css_set_refcount",
5911 .read_u64 = current_css_set_refcount_read,
5912 },
5913
5914 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005915 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005916 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005917 },
5918
5919 {
5920 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005921 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005922 },
5923
5924 {
Paul Menagefe693432009-09-23 15:56:20 -07005925 .name = "releasable",
5926 .read_u64 = releasable_read,
5927 },
Paul Menagefe693432009-09-23 15:56:20 -07005928
Tejun Heo4baf6e32012-04-01 12:09:55 -07005929 { } /* terminate */
5930};
Paul Menagefe693432009-09-23 15:56:20 -07005931
Tejun Heo073219e2014-02-08 10:36:58 -05005932struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005933 .css_alloc = debug_css_alloc,
5934 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005935 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005936};
5937#endif /* CONFIG_CGROUP_DEBUG */