blob: f1603c153890d2b9dbd37a5c687fd297c6137f24 [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 Heo69e943b2014-02-08 10:36:58 -0500101 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
102 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
103 */
104static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700105
Tejun Heo1ed13282015-09-16 12:53:17 -0400106struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
107
Tejun Heo8353da12014-05-13 12:19:23 -0400108#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700109 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
110 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400111 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500112
Ben Blumaae8aab2010-03-10 15:22:07 -0800113/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500114 * cgroup destruction makes heavy use of work items and there can be a lot
115 * of concurrent destructions. Use a separate workqueue so that cgroup
116 * destruction work items don't end up filling up max_active of system_wq
117 * which may lead to deadlock.
118 */
119static struct workqueue_struct *cgroup_destroy_wq;
120
121/*
Tejun Heob1a21362013-11-29 10:42:58 -0500122 * pidlist destructions need to be flushed on cgroup destruction. Use a
123 * separate workqueue as flush domain.
124 */
125static struct workqueue_struct *cgroup_pidlist_destroy_wq;
126
Tejun Heo3ed80a62014-02-08 10:36:58 -0500127/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500128#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500129static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700130#include <linux/cgroup_subsys.h>
131};
Tejun Heo073219e2014-02-08 10:36:58 -0500132#undef SUBSYS
133
134/* array of cgroup subsystem names */
135#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
136static const char *cgroup_subsys_name[] = {
137#include <linux/cgroup_subsys.h>
138};
139#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700140
Tejun Heo49d1dc42015-09-18 11:56:28 -0400141/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
142#define SUBSYS(_x) \
143 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
144 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
145 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
146 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
147#include <linux/cgroup_subsys.h>
148#undef SUBSYS
149
150#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
151static struct static_key_true *cgroup_subsys_enabled_key[] = {
152#include <linux/cgroup_subsys.h>
153};
154#undef SUBSYS
155
156#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
157static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
158#include <linux/cgroup_subsys.h>
159};
160#undef SUBSYS
161
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400163 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700164 * unattached - it never has more than a single cgroup, and all tasks are
165 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400167struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400168EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700169
Tejun Heoa2dd4242014-03-19 10:23:55 -0400170/*
171 * The default hierarchy always exists but is hidden until mounted for the
172 * first time. This is for backward compatibility.
173 */
174static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700175
Tejun Heo5533e012014-05-14 19:33:07 -0400176/* some controllers are not supported in the default hierarchy */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000177static unsigned long cgrp_dfl_root_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400178
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179/* The list of hierarchy roots */
180
Tejun Heo9871bf92013-06-24 15:21:47 -0700181static LIST_HEAD(cgroup_roots);
182static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183
Tejun Heo3417ae12014-02-08 10:37:01 -0500184/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700185static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700186
Li Zefan794611a2013-06-18 18:53:53 +0800187/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400188 * Assign a monotonically increasing serial number to csses. It guarantees
189 * cgroups with bigger numbers are newer than those with smaller numbers.
190 * Also, as csses are always appended to the parent's ->children list, it
191 * guarantees that sibling csses are always sorted in the ascending serial
192 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800193 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400194static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800195
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000196/*
197 * These bitmask flags indicate whether tasks in the fork and exit paths have
198 * fork/exit handlers to call. This avoids us having to do extra work in the
199 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000201static unsigned long have_fork_callback __read_mostly;
202static unsigned long have_exit_callback __read_mostly;
Tejun Heoafcf6c82015-10-15 16:41:53 -0400203static unsigned long have_free_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204
Aleksa Sarai7e476822015-06-09 21:32:09 +1000205/* Ditto for the can_fork callback. */
206static unsigned long have_canfork_callback __read_mostly;
207
Tejun Heoa14c6872014-07-15 11:05:09 -0400208static struct cftype cgroup_dfl_base_files[];
209static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700210
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400211static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000212 unsigned long ss_mask);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400213static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800214static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400215static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
216 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400217static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400218static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400219static int cgroup_addrm_files(struct cgroup_subsys_state *css,
220 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400221 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400223/**
224 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
225 * @ssid: subsys ID of interest
226 *
227 * cgroup_subsys_enabled() can only be used with literal subsys names which
228 * is fine for individual subsystems but unsuitable for cgroup core. This
229 * is slower static_key_enabled() based test indexed by @ssid.
230 */
231static bool cgroup_ssid_enabled(int ssid)
232{
233 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
234}
235
Tejun Heo9e10a132015-09-18 11:56:28 -0400236/**
237 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
238 * @cgrp: the cgroup of interest
239 *
240 * The default hierarchy is the v2 interface of cgroup and this function
241 * can be used to test whether a cgroup is on the default hierarchy for
242 * cases where a subsystem should behave differnetly depending on the
243 * interface version.
244 *
245 * The set of behaviors which change on the default hierarchy are still
246 * being determined and the mount option is prefixed with __DEVEL__.
247 *
248 * List of changed behaviors:
249 *
250 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
251 * and "name" are disallowed.
252 *
253 * - When mounting an existing superblock, mount options should match.
254 *
255 * - Remount is disallowed.
256 *
257 * - rename(2) is disallowed.
258 *
259 * - "tasks" is removed. Everything should be at process granularity. Use
260 * "cgroup.procs" instead.
261 *
262 * - "cgroup.procs" is not sorted. pids will be unique unless they got
263 * recycled inbetween reads.
264 *
265 * - "release_agent" and "notify_on_release" are removed. Replacement
266 * notification mechanism will be implemented.
267 *
268 * - "cgroup.clone_children" is removed.
269 *
270 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
271 * and its descendants contain no task; otherwise, 1. The file also
272 * generates kernfs notification which can be monitored through poll and
273 * [di]notify when the value of the file changes.
274 *
275 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
276 * take masks of ancestors with non-empty cpus/mems, instead of being
277 * moved to an ancestor.
278 *
279 * - cpuset: a task can be moved into an empty cpuset, and again it takes
280 * masks of ancestors.
281 *
282 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
283 * is not created.
284 *
285 * - blkcg: blk-throttle becomes properly hierarchical.
286 *
287 * - debug: disallowed on the default hierarchy.
288 */
289static bool cgroup_on_dfl(const struct cgroup *cgrp)
290{
291 return cgrp->root == &cgrp_dfl_root;
292}
293
Tejun Heo6fa49182014-05-04 15:09:13 -0400294/* IDR wrappers which synchronize using cgroup_idr_lock */
295static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
296 gfp_t gfp_mask)
297{
298 int ret;
299
300 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400301 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800302 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400303 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400304 idr_preload_end();
305 return ret;
306}
307
308static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
309{
310 void *ret;
311
Tejun Heo54504e92014-05-13 12:10:59 -0400312 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400313 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400314 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400315 return ret;
316}
317
318static void cgroup_idr_remove(struct idr *idr, int id)
319{
Tejun Heo54504e92014-05-13 12:10:59 -0400320 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400321 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400322 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400323}
324
Tejun Heod51f39b2014-05-16 13:22:48 -0400325static struct cgroup *cgroup_parent(struct cgroup *cgrp)
326{
327 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
328
329 if (parent_css)
330 return container_of(parent_css, struct cgroup, self);
331 return NULL;
332}
333
Tejun Heo95109b62013-08-08 20:11:27 -0400334/**
335 * cgroup_css - obtain a cgroup's css for the specified subsystem
336 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400337 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400338 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400339 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
340 * function must be called either under cgroup_mutex or rcu_read_lock() and
341 * the caller is responsible for pinning the returned css if it wants to
342 * keep accessing it outside the said locks. This function may return
343 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400344 */
345static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400346 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400347{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400348 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500349 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500350 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400351 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400352 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400353}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700354
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400355/**
356 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
357 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400358 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400359 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400360 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400361 * as the matching css of the nearest ancestor including self which has @ss
362 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
363 * function is guaranteed to return non-NULL css.
364 */
365static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
366 struct cgroup_subsys *ss)
367{
368 lockdep_assert_held(&cgroup_mutex);
369
370 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400371 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400372
373 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
374 return NULL;
375
Tejun Heoeeecbd12014-11-18 02:49:52 -0500376 /*
377 * This function is used while updating css associations and thus
378 * can't test the csses directly. Use ->child_subsys_mask.
379 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400380 while (cgroup_parent(cgrp) &&
381 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
382 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400383
384 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700385}
386
Tejun Heoeeecbd12014-11-18 02:49:52 -0500387/**
388 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
389 * @cgrp: the cgroup of interest
390 * @ss: the subsystem of interest
391 *
392 * Find and get the effective css of @cgrp for @ss. The effective css is
393 * defined as the matching css of the nearest ancestor including self which
394 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
395 * the root css is returned, so this function always returns a valid css.
396 * The returned css must be put using css_put().
397 */
398struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
399 struct cgroup_subsys *ss)
400{
401 struct cgroup_subsys_state *css;
402
403 rcu_read_lock();
404
405 do {
406 css = cgroup_css(cgrp, ss);
407
408 if (css && css_tryget_online(css))
409 goto out_unlock;
410 cgrp = cgroup_parent(cgrp);
411 } while (cgrp);
412
413 css = init_css_set.subsys[ss->id];
414 css_get(css);
415out_unlock:
416 rcu_read_unlock();
417 return css;
418}
419
Paul Menageddbcc7e2007-10-18 23:39:30 -0700420/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700421static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700422{
Tejun Heo184faf32014-05-16 13:22:51 -0400423 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700424}
425
Tejun Heo052c3f32015-10-15 16:41:50 -0400426static void cgroup_get(struct cgroup *cgrp)
427{
428 WARN_ON_ONCE(cgroup_is_dead(cgrp));
429 css_get(&cgrp->self);
430}
431
432static bool cgroup_tryget(struct cgroup *cgrp)
433{
434 return css_tryget(&cgrp->self);
435}
436
437static void cgroup_put(struct cgroup *cgrp)
438{
439 css_put(&cgrp->self);
440}
441
Tejun Heob4168642014-05-13 12:16:21 -0400442struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500443{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500444 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400445 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500446
447 /*
448 * This is open and unprotected implementation of cgroup_css().
449 * seq_css() is only called from a kernfs file operation which has
450 * an active reference on the file. Because all the subsystem
451 * files are drained before a css is disassociated with a cgroup,
452 * the matching css from the cgroup's subsys table is guaranteed to
453 * be and stay valid until the enclosing operation is complete.
454 */
455 if (cft->ss)
456 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
457 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400458 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500459}
Tejun Heob4168642014-05-13 12:16:21 -0400460EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500461
Li Zefan78574cf2013-04-08 19:00:38 -0700462/**
463 * cgroup_is_descendant - test ancestry
464 * @cgrp: the cgroup to be tested
465 * @ancestor: possible ancestor of @cgrp
466 *
467 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
468 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
469 * and @ancestor are accessible.
470 */
471bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
472{
473 while (cgrp) {
474 if (cgrp == ancestor)
475 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400476 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700477 }
478 return false;
479}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700480
Adrian Bunke9685a02008-02-07 00:13:46 -0800481static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700482{
Paul Menagebd89aab2007-10-18 23:40:44 -0700483 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700484}
485
Tejun Heo30159ec2013-06-25 11:53:37 -0700486/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500487 * for_each_css - iterate all css's of a cgroup
488 * @css: the iteration cursor
489 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
490 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700491 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400492 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700493 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500494#define for_each_css(css, ssid, cgrp) \
495 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
496 if (!((css) = rcu_dereference_check( \
497 (cgrp)->subsys[(ssid)], \
498 lockdep_is_held(&cgroup_mutex)))) { } \
499 else
500
501/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400502 * for_each_e_css - iterate all effective css's of a cgroup
503 * @css: the iteration cursor
504 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
505 * @cgrp: the target cgroup to iterate css's of
506 *
507 * Should be called under cgroup_[tree_]mutex.
508 */
509#define for_each_e_css(css, ssid, cgrp) \
510 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
511 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
512 ; \
513 else
514
515/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500516 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700517 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500518 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700519 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500520#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500521 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
522 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700523
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000524/**
525 * for_each_subsys_which - filter for_each_subsys with a bitmask
526 * @ss: the iteration cursor
527 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
528 * @ss_maskp: a pointer to the bitmask
529 *
530 * The block will only run for cases where the ssid-th bit (1 << ssid) of
531 * mask is set to 1.
532 */
533#define for_each_subsys_which(ss, ssid, ss_maskp) \
534 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000535 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000536 else \
537 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
538 if (((ss) = cgroup_subsys[ssid]) && false) \
539 break; \
540 else
541
Tejun Heo985ed672014-03-19 10:23:53 -0400542/* iterate across the hierarchies */
543#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700544 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700545
Tejun Heof8f22e52014-04-23 11:13:16 -0400546/* iterate over child cgrps, lock should be held throughout iteration */
547#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400548 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400549 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400550 cgroup_is_dead(child); })) \
551 ; \
552 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700553
Paul Menage81a6a5c2007-10-18 23:39:38 -0700554static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700555static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700556
Tejun Heo69d02062013-06-12 21:04:50 -0700557/*
558 * A cgroup can be associated with multiple css_sets as different tasks may
559 * belong to different cgroups on different hierarchies. In the other
560 * direction, a css_set is naturally associated with multiple cgroups.
561 * This M:N relationship is represented by the following link structure
562 * which exists for each association and allows traversing the associations
563 * from both sides.
564 */
565struct cgrp_cset_link {
566 /* the cgroup and css_set this link associates */
567 struct cgroup *cgrp;
568 struct css_set *cset;
569
570 /* list of cgrp_cset_links anchored at cgrp->cset_links */
571 struct list_head cset_link;
572
573 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
574 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700575};
576
Tejun Heo172a2c062014-03-19 10:23:53 -0400577/*
578 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700579 * hierarchies being mounted. It contains a pointer to the root state
580 * for each subsystem. Also used to anchor the list of css_sets. Not
581 * reference-counted, to improve performance when child cgroups
582 * haven't been created.
583 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400584struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400585 .refcount = ATOMIC_INIT(1),
586 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
587 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
588 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
589 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
590 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400591 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400592};
Paul Menage817929e2007-10-18 23:39:36 -0700593
Tejun Heo172a2c062014-03-19 10:23:53 -0400594static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700595
Tejun Heo842b5972014-04-25 18:28:02 -0400596/**
Tejun Heo0de09422015-10-15 16:41:49 -0400597 * css_set_populated - does a css_set contain any tasks?
598 * @cset: target css_set
599 */
600static bool css_set_populated(struct css_set *cset)
601{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400602 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400603
604 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
605}
606
607/**
Tejun Heo842b5972014-04-25 18:28:02 -0400608 * cgroup_update_populated - updated populated count of a cgroup
609 * @cgrp: the target cgroup
610 * @populated: inc or dec populated count
611 *
Tejun Heo0de09422015-10-15 16:41:49 -0400612 * One of the css_sets associated with @cgrp is either getting its first
613 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
614 * count is propagated towards root so that a given cgroup's populated_cnt
615 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400616 *
617 * @cgrp's interface file "cgroup.populated" is zero if
618 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
619 * changes from or to zero, userland is notified that the content of the
620 * interface file has changed. This can be used to detect when @cgrp and
621 * its descendants become populated or empty.
622 */
623static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
624{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400625 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400626
627 do {
628 bool trigger;
629
630 if (populated)
631 trigger = !cgrp->populated_cnt++;
632 else
633 trigger = !--cgrp->populated_cnt;
634
635 if (!trigger)
636 break;
637
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400638 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400639 cgroup_file_notify(&cgrp->events_file);
640
Tejun Heod51f39b2014-05-16 13:22:48 -0400641 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400642 } while (cgrp);
643}
644
Tejun Heo0de09422015-10-15 16:41:49 -0400645/**
646 * css_set_update_populated - update populated state of a css_set
647 * @cset: target css_set
648 * @populated: whether @cset is populated or depopulated
649 *
650 * @cset is either getting the first task or losing the last. Update the
651 * ->populated_cnt of all associated cgroups accordingly.
652 */
653static void css_set_update_populated(struct css_set *cset, bool populated)
654{
655 struct cgrp_cset_link *link;
656
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400657 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400658
659 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
660 cgroup_update_populated(link->cgrp, populated);
661}
662
Tejun Heof6d7d042015-10-15 16:41:52 -0400663/**
664 * css_set_move_task - move a task from one css_set to another
665 * @task: task being moved
666 * @from_cset: css_set @task currently belongs to (may be NULL)
667 * @to_cset: new css_set @task is being moved to (may be NULL)
668 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
669 *
670 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
671 * css_set, @from_cset can be NULL. If @task is being disassociated
672 * instead of moved, @to_cset can be NULL.
673 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400674 * This function automatically handles populated_cnt updates and
675 * css_task_iter adjustments but the caller is responsible for managing
676 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400677 */
678static void css_set_move_task(struct task_struct *task,
679 struct css_set *from_cset, struct css_set *to_cset,
680 bool use_mg_tasks)
681{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400682 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400683
684 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400685 struct css_task_iter *it, *pos;
686
Tejun Heof6d7d042015-10-15 16:41:52 -0400687 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400688
689 /*
690 * @task is leaving, advance task iterators which are
691 * pointing to it so that they can resume at the next
692 * position. Advancing an iterator might remove it from
693 * the list, use safe walk. See css_task_iter_advance*()
694 * for details.
695 */
696 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
697 iters_node)
698 if (it->task_pos == &task->cg_list)
699 css_task_iter_advance(it);
700
Tejun Heof6d7d042015-10-15 16:41:52 -0400701 list_del_init(&task->cg_list);
702 if (!css_set_populated(from_cset))
703 css_set_update_populated(from_cset, false);
704 } else {
705 WARN_ON_ONCE(!list_empty(&task->cg_list));
706 }
707
708 if (to_cset) {
709 /*
710 * We are synchronized through cgroup_threadgroup_rwsem
711 * against PF_EXITING setting such that we can't race
712 * against cgroup_exit() changing the css_set to
713 * init_css_set and dropping the old one.
714 */
715 WARN_ON_ONCE(task->flags & PF_EXITING);
716
717 if (!css_set_populated(to_cset))
718 css_set_update_populated(to_cset, true);
719 rcu_assign_pointer(task->cgroups, to_cset);
720 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
721 &to_cset->tasks);
722 }
723}
724
Paul Menage7717f7b2009-09-23 15:56:22 -0700725/*
726 * hash table for cgroup groups. This improves the performance to find
727 * an existing css_set. This hash doesn't (currently) take into
728 * account cgroups in empty hierarchies.
729 */
Li Zefan472b1052008-04-29 01:00:11 -0700730#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800731static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700732
Li Zefan0ac801f2013-01-10 11:49:27 +0800733static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700734{
Li Zefan0ac801f2013-01-10 11:49:27 +0800735 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700736 struct cgroup_subsys *ss;
737 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700738
Tejun Heo30159ec2013-06-25 11:53:37 -0700739 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800740 key += (unsigned long)css[i];
741 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700742
Li Zefan0ac801f2013-01-10 11:49:27 +0800743 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700744}
745
Zefan Lia25eb522014-09-19 16:51:00 +0800746static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700747{
Tejun Heo69d02062013-06-12 21:04:50 -0700748 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400749 struct cgroup_subsys *ss;
750 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700751
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400752 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500753
754 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700755 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700756
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700757 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400758 for_each_subsys(ss, ssid)
759 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700760 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700761 css_set_count--;
762
Tejun Heo69d02062013-06-12 21:04:50 -0700763 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700764 list_del(&link->cset_link);
765 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400766 if (cgroup_parent(link->cgrp))
767 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700768 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700769 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700770
Tejun Heo5abb8852013-06-12 21:04:49 -0700771 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700772}
773
Zefan Lia25eb522014-09-19 16:51:00 +0800774static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500775{
776 /*
777 * Ensure that the refcount doesn't hit zero while any readers
778 * can see it. Similar to atomic_dec_and_lock(), but for an
779 * rwlock
780 */
781 if (atomic_add_unless(&cset->refcount, -1, 1))
782 return;
783
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400784 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800785 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400786 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500787}
788
Paul Menage817929e2007-10-18 23:39:36 -0700789/*
790 * refcounted get/put for css_set objects
791 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700792static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700793{
Tejun Heo5abb8852013-06-12 21:04:49 -0700794 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700795}
796
Tejun Heob326f9d2013-06-24 15:21:48 -0700797/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700798 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700799 * @cset: candidate css_set being tested
800 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700801 * @new_cgrp: cgroup that's being entered by the task
802 * @template: desired set of css pointers in css_set (pre-calculated)
803 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800804 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700805 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
806 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700807static bool compare_css_sets(struct css_set *cset,
808 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700809 struct cgroup *new_cgrp,
810 struct cgroup_subsys_state *template[])
811{
812 struct list_head *l1, *l2;
813
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400814 /*
815 * On the default hierarchy, there can be csets which are
816 * associated with the same set of cgroups but different csses.
817 * Let's first ensure that csses match.
818 */
819 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700820 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700821
822 /*
823 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400824 * different cgroups in hierarchies. As different cgroups may
825 * share the same effective css, this comparison is always
826 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700827 */
Tejun Heo69d02062013-06-12 21:04:50 -0700828 l1 = &cset->cgrp_links;
829 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700830 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700831 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700832 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700833
834 l1 = l1->next;
835 l2 = l2->next;
836 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700837 if (l1 == &cset->cgrp_links) {
838 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700839 break;
840 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700841 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700842 }
843 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700844 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
845 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
846 cgrp1 = link1->cgrp;
847 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700848 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700849 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700850
851 /*
852 * If this hierarchy is the hierarchy of the cgroup
853 * that's changing, then we need to check that this
854 * css_set points to the new cgroup; if it's any other
855 * hierarchy, then this css_set should point to the
856 * same cgroup as the old css_set.
857 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700858 if (cgrp1->root == new_cgrp->root) {
859 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700860 return false;
861 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700862 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700863 return false;
864 }
865 }
866 return true;
867}
868
Tejun Heob326f9d2013-06-24 15:21:48 -0700869/**
870 * find_existing_css_set - init css array and find the matching css_set
871 * @old_cset: the css_set that we're using before the cgroup transition
872 * @cgrp: the cgroup that we're moving into
873 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700874 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700875static struct css_set *find_existing_css_set(struct css_set *old_cset,
876 struct cgroup *cgrp,
877 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700878{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400879 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700880 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700881 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800882 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700883 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700884
Ben Blumaae8aab2010-03-10 15:22:07 -0800885 /*
886 * Build the set of subsystem state objects that we want to see in the
887 * new css_set. while subsystems can change globally, the entries here
888 * won't change, so no need for locking.
889 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700890 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400891 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400892 /*
893 * @ss is in this hierarchy, so we want the
894 * effective css from @cgrp.
895 */
896 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700897 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400898 /*
899 * @ss is not in this hierarchy, so we don't want
900 * to change the css.
901 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700902 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700903 }
904 }
905
Li Zefan0ac801f2013-01-10 11:49:27 +0800906 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700907 hash_for_each_possible(css_set_table, cset, hlist, key) {
908 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700909 continue;
910
911 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700912 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700913 }
Paul Menage817929e2007-10-18 23:39:36 -0700914
915 /* No existing cgroup group matched */
916 return NULL;
917}
918
Tejun Heo69d02062013-06-12 21:04:50 -0700919static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700920{
Tejun Heo69d02062013-06-12 21:04:50 -0700921 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700922
Tejun Heo69d02062013-06-12 21:04:50 -0700923 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
924 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700925 kfree(link);
926 }
927}
928
Tejun Heo69d02062013-06-12 21:04:50 -0700929/**
930 * allocate_cgrp_cset_links - allocate cgrp_cset_links
931 * @count: the number of links to allocate
932 * @tmp_links: list_head the allocated links are put on
933 *
934 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
935 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700936 */
Tejun Heo69d02062013-06-12 21:04:50 -0700937static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700938{
Tejun Heo69d02062013-06-12 21:04:50 -0700939 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700940 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700941
942 INIT_LIST_HEAD(tmp_links);
943
Li Zefan36553432008-07-29 22:33:19 -0700944 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700945 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700946 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700947 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700948 return -ENOMEM;
949 }
Tejun Heo69d02062013-06-12 21:04:50 -0700950 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700951 }
952 return 0;
953}
954
Li Zefanc12f65d2009-01-07 18:07:42 -0800955/**
956 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700957 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700958 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800959 * @cgrp: the destination cgroup
960 */
Tejun Heo69d02062013-06-12 21:04:50 -0700961static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
962 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800963{
Tejun Heo69d02062013-06-12 21:04:50 -0700964 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800965
Tejun Heo69d02062013-06-12 21:04:50 -0700966 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400967
968 if (cgroup_on_dfl(cgrp))
969 cset->dfl_cgrp = cgrp;
970
Tejun Heo69d02062013-06-12 21:04:50 -0700971 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
972 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700973 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400974
Paul Menage7717f7b2009-09-23 15:56:22 -0700975 /*
Tejun Heo389b9c12015-10-15 16:41:51 -0400976 * Always add links to the tail of the lists so that the lists are
977 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -0700978 */
Tejun Heo389b9c12015-10-15 16:41:51 -0400979 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -0700980 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400981
982 if (cgroup_parent(cgrp))
983 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -0800984}
985
Tejun Heob326f9d2013-06-24 15:21:48 -0700986/**
987 * find_css_set - return a new css_set with one cgroup updated
988 * @old_cset: the baseline css_set
989 * @cgrp: the cgroup to be updated
990 *
991 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
992 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700993 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700994static struct css_set *find_css_set(struct css_set *old_cset,
995 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700996{
Tejun Heob326f9d2013-06-24 15:21:48 -0700997 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700998 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700999 struct list_head tmp_links;
1000 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001001 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001002 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001003 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001004
Tejun Heob326f9d2013-06-24 15:21:48 -07001005 lockdep_assert_held(&cgroup_mutex);
1006
Paul Menage817929e2007-10-18 23:39:36 -07001007 /* First see if we already have a cgroup group that matches
1008 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001009 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001010 cset = find_existing_css_set(old_cset, cgrp, template);
1011 if (cset)
1012 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001013 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001014
Tejun Heo5abb8852013-06-12 21:04:49 -07001015 if (cset)
1016 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001017
Tejun Heof4f4be22013-06-12 21:04:51 -07001018 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001019 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001020 return NULL;
1021
Tejun Heo69d02062013-06-12 21:04:50 -07001022 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001023 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001024 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001025 return NULL;
1026 }
1027
Tejun Heo5abb8852013-06-12 21:04:49 -07001028 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001029 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001030 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001031 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001032 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001033 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001034 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001035 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001036
1037 /* Copy the set of subsystem state objects generated in
1038 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001039 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001040
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001041 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001042 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001043 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001044 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001045
Paul Menage7717f7b2009-09-23 15:56:22 -07001046 if (c->root == cgrp->root)
1047 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001048 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001049 }
Paul Menage817929e2007-10-18 23:39:36 -07001050
Tejun Heo69d02062013-06-12 21:04:50 -07001051 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001052
Paul Menage817929e2007-10-18 23:39:36 -07001053 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001054
Tejun Heo2d8f2432014-04-23 11:13:15 -04001055 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001056 key = css_set_hash(cset->subsys);
1057 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001058
Tejun Heo2d8f2432014-04-23 11:13:15 -04001059 for_each_subsys(ss, ssid)
1060 list_add_tail(&cset->e_cset_node[ssid],
1061 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
1062
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001063 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001064
Tejun Heo5abb8852013-06-12 21:04:49 -07001065 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001066}
1067
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001068static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001069{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001070 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001071
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001072 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001073}
1074
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001075static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001076{
1077 int id;
1078
1079 lockdep_assert_held(&cgroup_mutex);
1080
Tejun Heo985ed672014-03-19 10:23:53 -04001081 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001082 if (id < 0)
1083 return id;
1084
1085 root->hierarchy_id = id;
1086 return 0;
1087}
1088
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001089static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001090{
1091 lockdep_assert_held(&cgroup_mutex);
1092
1093 if (root->hierarchy_id) {
1094 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1095 root->hierarchy_id = 0;
1096 }
1097}
1098
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001099static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001100{
1101 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001102 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001103 WARN_ON_ONCE(root->hierarchy_id);
1104
1105 idr_destroy(&root->cgroup_idr);
1106 kfree(root);
1107 }
1108}
1109
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001110static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001111{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001112 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001113 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001114
Tejun Heo2bd59d42014-02-11 11:52:49 -05001115 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001116
Tejun Heo776f02f2014-02-12 09:29:50 -05001117 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001118 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001119
Tejun Heof2e85d52014-02-11 11:52:49 -05001120 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001121 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001122
1123 /*
1124 * Release all the links from cset_links to this hierarchy's
1125 * root cgroup
1126 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001127 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001128
1129 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1130 list_del(&link->cset_link);
1131 list_del(&link->cgrp_link);
1132 kfree(link);
1133 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001134
1135 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001136
1137 if (!list_empty(&root->root_list)) {
1138 list_del(&root->root_list);
1139 cgroup_root_count--;
1140 }
1141
1142 cgroup_exit_root_id(root);
1143
1144 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001145
Tejun Heo2bd59d42014-02-11 11:52:49 -05001146 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001147 cgroup_free_root(root);
1148}
1149
Tejun Heoceb6a082014-02-25 10:04:02 -05001150/* look up cgroup associated with given css_set on the specified hierarchy */
1151static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001152 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001153{
Paul Menage7717f7b2009-09-23 15:56:22 -07001154 struct cgroup *res = NULL;
1155
Tejun Heo96d365e2014-02-13 06:58:40 -05001156 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001157 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001158
Tejun Heo5abb8852013-06-12 21:04:49 -07001159 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001160 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001161 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001162 struct cgrp_cset_link *link;
1163
1164 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001165 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001166
Paul Menage7717f7b2009-09-23 15:56:22 -07001167 if (c->root == root) {
1168 res = c;
1169 break;
1170 }
1171 }
1172 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001173
Paul Menage7717f7b2009-09-23 15:56:22 -07001174 BUG_ON(!res);
1175 return res;
1176}
1177
1178/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001179 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001180 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001181 */
1182static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001183 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001184{
1185 /*
1186 * No need to lock the task - since we hold cgroup_mutex the
1187 * task can't change groups, so the only thing that can happen
1188 * is that it exits and its css is set back to init_css_set.
1189 */
1190 return cset_cgroup_from_root(task_css_set(task), root);
1191}
1192
1193/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001194 * A task must hold cgroup_mutex to modify cgroups.
1195 *
1196 * Any task can increment and decrement the count field without lock.
1197 * So in general, code holding cgroup_mutex can't rely on the count
1198 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001199 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001200 * means that no tasks are currently attached, therefore there is no
1201 * way a task attached to that cgroup can fork (the other way to
1202 * increment the count). So code holding cgroup_mutex can safely
1203 * assume that if the count is zero, it will stay zero. Similarly, if
1204 * a task holds cgroup_mutex on a cgroup with zero count, it
1205 * knows that the cgroup won't be removed, as cgroup_rmdir()
1206 * needs that mutex.
1207 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001208 * A cgroup can only be deleted if both its 'count' of using tasks
1209 * is zero, and its list of 'children' cgroups is empty. Since all
1210 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001211 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001213 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001214 *
1215 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001216 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217 */
1218
Tejun Heo2bd59d42014-02-11 11:52:49 -05001219static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001220static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001221
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001222static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1223 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001224{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001225 struct cgroup_subsys *ss = cft->ss;
1226
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001227 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1228 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1229 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001230 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1231 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001232 else
1233 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1234 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001235}
1236
Tejun Heof2e85d52014-02-11 11:52:49 -05001237/**
1238 * cgroup_file_mode - deduce file mode of a control file
1239 * @cft: the control file in question
1240 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001241 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001242 */
1243static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001244{
Tejun Heof2e85d52014-02-11 11:52:49 -05001245 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001246
Tejun Heof2e85d52014-02-11 11:52:49 -05001247 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1248 mode |= S_IRUGO;
1249
Tejun Heo7dbdb192015-09-18 17:54:23 -04001250 if (cft->write_u64 || cft->write_s64 || cft->write) {
1251 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1252 mode |= S_IWUGO;
1253 else
1254 mode |= S_IWUSR;
1255 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001256
1257 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001258}
1259
Tejun Heoa9746d82014-05-13 12:19:22 -04001260/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001261 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001262 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001263 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001264 *
1265 * On the default hierarchy, a subsystem may request other subsystems to be
1266 * enabled together through its ->depends_on mask. In such cases, more
1267 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1268 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001269 * This function calculates which subsystems need to be enabled if
1270 * @subtree_control is to be applied to @cgrp. The returned mask is always
1271 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001272 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001273static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1274 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001275{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001276 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001277 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001278 struct cgroup_subsys *ss;
1279 int ssid;
1280
1281 lockdep_assert_held(&cgroup_mutex);
1282
Tejun Heo0f060de2014-11-18 02:49:50 -05001283 if (!cgroup_on_dfl(cgrp))
1284 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001285
1286 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001287 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001288
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001289 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1290 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001291
1292 /*
1293 * Mask out subsystems which aren't available. This can
1294 * happen only if some depended-upon subsystems were bound
1295 * to non-default hierarchies.
1296 */
1297 if (parent)
1298 new_ss_mask &= parent->child_subsys_mask;
1299 else
1300 new_ss_mask &= cgrp->root->subsys_mask;
1301
1302 if (new_ss_mask == cur_ss_mask)
1303 break;
1304 cur_ss_mask = new_ss_mask;
1305 }
1306
Tejun Heo0f060de2014-11-18 02:49:50 -05001307 return cur_ss_mask;
1308}
1309
1310/**
1311 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1312 * @cgrp: the target cgroup
1313 *
1314 * Update @cgrp->child_subsys_mask according to the current
1315 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1316 */
1317static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1318{
1319 cgrp->child_subsys_mask =
1320 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001321}
1322
Tejun Heoa9746d82014-05-13 12:19:22 -04001323/**
1324 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1325 * @kn: the kernfs_node being serviced
1326 *
1327 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1328 * the method finishes if locking succeeded. Note that once this function
1329 * returns the cgroup returned by cgroup_kn_lock_live() may become
1330 * inaccessible any time. If the caller intends to continue to access the
1331 * cgroup, it should pin it before invoking this function.
1332 */
1333static void cgroup_kn_unlock(struct kernfs_node *kn)
1334{
1335 struct cgroup *cgrp;
1336
1337 if (kernfs_type(kn) == KERNFS_DIR)
1338 cgrp = kn->priv;
1339 else
1340 cgrp = kn->parent->priv;
1341
1342 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001343
1344 kernfs_unbreak_active_protection(kn);
1345 cgroup_put(cgrp);
1346}
1347
1348/**
1349 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1350 * @kn: the kernfs_node being serviced
1351 *
1352 * This helper is to be used by a cgroup kernfs method currently servicing
1353 * @kn. It breaks the active protection, performs cgroup locking and
1354 * verifies that the associated cgroup is alive. Returns the cgroup if
1355 * alive; otherwise, %NULL. A successful return should be undone by a
1356 * matching cgroup_kn_unlock() invocation.
1357 *
1358 * Any cgroup kernfs method implementation which requires locking the
1359 * associated cgroup should use this helper. It avoids nesting cgroup
1360 * locking under kernfs active protection and allows all kernfs operations
1361 * including self-removal.
1362 */
1363static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1364{
1365 struct cgroup *cgrp;
1366
1367 if (kernfs_type(kn) == KERNFS_DIR)
1368 cgrp = kn->priv;
1369 else
1370 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371
Tejun Heo2bd59d42014-02-11 11:52:49 -05001372 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001373 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001374 * active_ref. cgroup liveliness check alone provides enough
1375 * protection against removal. Ensure @cgrp stays accessible and
1376 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001377 */
Li Zefanaa323622014-09-04 14:43:38 +08001378 if (!cgroup_tryget(cgrp))
1379 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001380 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381
Tejun Heoa9746d82014-05-13 12:19:22 -04001382 mutex_lock(&cgroup_mutex);
1383
1384 if (!cgroup_is_dead(cgrp))
1385 return cgrp;
1386
1387 cgroup_kn_unlock(kn);
1388 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389}
1390
Li Zefan2739d3c2013-01-21 18:18:33 +08001391static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001393 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394
Tejun Heo01f64742014-05-13 12:19:23 -04001395 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001396 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001397}
1398
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001399/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001400 * css_clear_dir - remove subsys files in a cgroup directory
1401 * @css: taget css
1402 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001403 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001404static void css_clear_dir(struct cgroup_subsys_state *css,
1405 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001406{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001407 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1408 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001409
Tejun Heo4df8dc92015-09-18 17:54:23 -04001410 list_for_each_entry(cfts, &css->ss->cfts, node)
1411 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412}
1413
Tejun Heoccdca212015-09-18 17:54:23 -04001414/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001415 * css_populate_dir - create subsys files in a cgroup directory
1416 * @css: target css
1417 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001418 *
1419 * On failure, no file is added.
1420 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001421static int css_populate_dir(struct cgroup_subsys_state *css,
1422 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001423{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001424 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1425 struct cftype *cfts, *failed_cfts;
1426 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001427
Tejun Heo4df8dc92015-09-18 17:54:23 -04001428 if (!css->ss) {
1429 if (cgroup_on_dfl(cgrp))
1430 cfts = cgroup_dfl_base_files;
1431 else
1432 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001433
Tejun Heo4df8dc92015-09-18 17:54:23 -04001434 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1435 }
Tejun Heoccdca212015-09-18 17:54:23 -04001436
Tejun Heo4df8dc92015-09-18 17:54:23 -04001437 list_for_each_entry(cfts, &css->ss->cfts, node) {
1438 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1439 if (ret < 0) {
1440 failed_cfts = cfts;
1441 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001442 }
1443 }
1444 return 0;
1445err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001446 list_for_each_entry(cfts, &css->ss->cfts, node) {
1447 if (cfts == failed_cfts)
1448 break;
1449 cgroup_addrm_files(css, cgrp, cfts, false);
1450 }
Tejun Heoccdca212015-09-18 17:54:23 -04001451 return ret;
1452}
1453
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001454static int rebind_subsystems(struct cgroup_root *dst_root,
1455 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456{
Tejun Heo1ada4832015-09-18 17:54:23 -04001457 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001458 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001459 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001460 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461
Tejun Heoace2bee2014-02-11 11:52:47 -05001462 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001463
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001464 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001465 /* if @ss has non-root csses attached to it, can't move */
1466 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001467 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001468
Tejun Heo5df36032014-03-19 10:23:54 -04001469 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001470 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001471 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 }
1473
Tejun Heo5533e012014-05-14 19:33:07 -04001474 /* skip creating root files on dfl_root for inhibited subsystems */
1475 tmp_ss_mask = ss_mask;
1476 if (dst_root == &cgrp_dfl_root)
1477 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1478
Tejun Heo4df8dc92015-09-18 17:54:23 -04001479 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
1480 struct cgroup *scgrp = &ss->root->cgrp;
1481 int tssid;
1482
1483 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1484 if (!ret)
1485 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486
Tejun Heoa2dd4242014-03-19 10:23:55 -04001487 /*
1488 * Rebinding back to the default root is not allowed to
1489 * fail. Using both default and non-default roots should
1490 * be rare. Moving subsystems back and forth even more so.
1491 * Just warn about it and continue.
1492 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001493 if (dst_root == &cgrp_dfl_root) {
1494 if (cgrp_dfl_root_visible) {
1495 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
1496 ret, ss_mask);
1497 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1498 }
1499 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001500 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001501
1502 for_each_subsys_which(ss, tssid, &tmp_ss_mask) {
1503 if (tssid == ssid)
1504 break;
1505 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
1506 }
1507 return ret;
Tejun Heo5df36032014-03-19 10:23:54 -04001508 }
Tejun Heo31261212013-06-28 17:07:30 -07001509
1510 /*
1511 * Nothing can fail from this point on. Remove files for the
1512 * removed subsystems and rebind each subsystem.
1513 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001514 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001515 struct cgroup_root *src_root = ss->root;
1516 struct cgroup *scgrp = &src_root->cgrp;
1517 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001518 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001519
Tejun Heo1ada4832015-09-18 17:54:23 -04001520 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001521
Tejun Heo4df8dc92015-09-18 17:54:23 -04001522 css_clear_dir(css, NULL);
1523
Tejun Heo1ada4832015-09-18 17:54:23 -04001524 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1525 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001526 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001527 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001528
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001529 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001530 hash_for_each(css_set_table, i, cset, hlist)
1531 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001532 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001533 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001534
Tejun Heof392e512014-04-23 11:13:14 -04001535 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001536 scgrp->subtree_control &= ~(1 << ssid);
1537 cgroup_refresh_child_subsys_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001538
Tejun Heobd53d612014-04-23 11:13:16 -04001539 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001540 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001541 if (dst_root == &cgrp_dfl_root) {
1542 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1543 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001544 dcgrp->subtree_control |= 1 << ssid;
1545 cgroup_refresh_child_subsys_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001546 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001547 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001548
Tejun Heo5df36032014-03-19 10:23:54 -04001549 if (ss->bind)
1550 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
Tejun Heo1ada4832015-09-18 17:54:23 -04001553 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554 return 0;
1555}
1556
Tejun Heo2bd59d42014-02-11 11:52:49 -05001557static int cgroup_show_options(struct seq_file *seq,
1558 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001560 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001562 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563
Tejun Heod98817d2015-08-18 13:58:16 -07001564 if (root != &cgrp_dfl_root)
1565 for_each_subsys(ss, ssid)
1566 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001567 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001568 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001570 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001571 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001572
1573 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001574 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001575 seq_show_option(seq, "release_agent",
1576 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001577 spin_unlock(&release_agent_path_lock);
1578
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001579 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001580 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001582 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 return 0;
1584}
1585
1586struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001587 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001588 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001589 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001590 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001592 /* User explicitly requested empty subsystem */
1593 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594};
1595
Ben Blumcf5d5942010-03-10 15:22:09 -08001596static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001598 char *token, *o = data;
1599 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001600 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001601 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001602 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001603 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001604
1605#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001606 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001607#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610
1611 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001612 nr_opts++;
1613
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614 if (!*token)
1615 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001616 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001617 /* Explicitly have no subsystems */
1618 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001619 continue;
1620 }
1621 if (!strcmp(token, "all")) {
1622 /* Mutually exclusive option 'all' + subsystem name */
1623 if (one_ss)
1624 return -EINVAL;
1625 all_ss = true;
1626 continue;
1627 }
Tejun Heo873fe092013-04-14 20:15:26 -07001628 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1629 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1630 continue;
1631 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001632 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001633 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001634 continue;
1635 }
1636 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001637 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001638 continue;
1639 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001640 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001641 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001642 continue;
1643 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001644 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001645 /* Specifying two release agents is forbidden */
1646 if (opts->release_agent)
1647 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001648 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001649 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001650 if (!opts->release_agent)
1651 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001652 continue;
1653 }
1654 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001655 const char *name = token + 5;
1656 /* Can't specify an empty name */
1657 if (!strlen(name))
1658 return -EINVAL;
1659 /* Must match [\w.-]+ */
1660 for (i = 0; i < strlen(name); i++) {
1661 char c = name[i];
1662 if (isalnum(c))
1663 continue;
1664 if ((c == '.') || (c == '-') || (c == '_'))
1665 continue;
1666 return -EINVAL;
1667 }
1668 /* Specifying two names is forbidden */
1669 if (opts->name)
1670 return -EINVAL;
1671 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001672 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001673 GFP_KERNEL);
1674 if (!opts->name)
1675 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001676
1677 continue;
1678 }
1679
Tejun Heo30159ec2013-06-25 11:53:37 -07001680 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001681 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001682 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001683 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001684 continue;
1685
1686 /* Mutually exclusive option 'all' + subsystem name */
1687 if (all_ss)
1688 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001689 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001690 one_ss = true;
1691
1692 break;
1693 }
1694 if (i == CGROUP_SUBSYS_COUNT)
1695 return -ENOENT;
1696 }
1697
Tejun Heo873fe092013-04-14 20:15:26 -07001698 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001699 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001700 if (nr_opts != 1) {
1701 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001702 return -EINVAL;
1703 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001704 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001705 }
1706
Li Zefanf9ab5b52009-06-17 16:26:33 -07001707 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001708 * If the 'all' option was specified select all the subsystems,
1709 * otherwise if 'none', 'name=' and a subsystem name options were
1710 * not specified, let's default to 'all'
1711 */
1712 if (all_ss || (!one_ss && !opts->none && !opts->name))
1713 for_each_subsys(ss, i)
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001714 if (cgroup_ssid_enabled(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001715 opts->subsys_mask |= (1 << i);
1716
1717 /*
1718 * We either have to specify by name or by subsystems. (So all
1719 * empty hierarchies must have a name).
1720 */
1721 if (!opts->subsys_mask && !opts->name)
1722 return -EINVAL;
1723
1724 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001725 * Option noprefix was introduced just for backward compatibility
1726 * with the old cpuset, so we allow noprefix only if mounting just
1727 * the cpuset subsystem.
1728 */
Tejun Heo93438622013-04-14 20:15:25 -07001729 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001730 return -EINVAL;
1731
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001732 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001733 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001734 return -EINVAL;
1735
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 return 0;
1737}
1738
Tejun Heo2bd59d42014-02-11 11:52:49 -05001739static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740{
1741 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001742 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001744 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745
Tejun Heoaa6ec292014-07-09 10:08:08 -04001746 if (root == &cgrp_dfl_root) {
1747 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001748 return -EINVAL;
1749 }
1750
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751 mutex_lock(&cgroup_mutex);
1752
1753 /* See what subsystems are wanted */
1754 ret = parse_cgroupfs_options(data, &opts);
1755 if (ret)
1756 goto out_unlock;
1757
Tejun Heof392e512014-04-23 11:13:14 -04001758 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001759 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001760 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001761
Tejun Heof392e512014-04-23 11:13:14 -04001762 added_mask = opts.subsys_mask & ~root->subsys_mask;
1763 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001764
Ben Blumcf5d5942010-03-10 15:22:09 -08001765 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001766 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001767 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001768 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001769 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001771 goto out_unlock;
1772 }
1773
Tejun Heof172e672013-06-28 17:07:30 -07001774 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001775 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001776 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001777 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001778 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779
Tejun Heo5df36032014-03-19 10:23:54 -04001780 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001781 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001784 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001785
Tejun Heo69e943b2014-02-08 10:36:58 -05001786 if (opts.release_agent) {
1787 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001788 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001789 spin_unlock(&release_agent_path_lock);
1790 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001792 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001793 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 return ret;
1796}
1797
Tejun Heoafeb0f92014-02-13 06:58:39 -05001798/*
1799 * To reduce the fork() overhead for systems that are not actually using
1800 * their cgroups capability, we don't maintain the lists running through
1801 * each css_set to its tasks until we see the list actually used - in other
1802 * words after the first mount.
1803 */
1804static bool use_task_css_set_links __read_mostly;
1805
1806static void cgroup_enable_task_cg_lists(void)
1807{
1808 struct task_struct *p, *g;
1809
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001810 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001811
1812 if (use_task_css_set_links)
1813 goto out_unlock;
1814
1815 use_task_css_set_links = true;
1816
1817 /*
1818 * We need tasklist_lock because RCU is not safe against
1819 * while_each_thread(). Besides, a forking task that has passed
1820 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1821 * is not guaranteed to have its child immediately visible in the
1822 * tasklist if we walk through it with RCU.
1823 */
1824 read_lock(&tasklist_lock);
1825 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001826 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1827 task_css_set(p) != &init_css_set);
1828
1829 /*
1830 * We should check if the process is exiting, otherwise
1831 * it will race with cgroup_exit() in that the list
1832 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001833 * Do it while holding siglock so that we don't end up
1834 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001835 */
Tejun Heof153ad12014-02-25 09:56:49 -05001836 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001837 if (!(p->flags & PF_EXITING)) {
1838 struct css_set *cset = task_css_set(p);
1839
Tejun Heo0de09422015-10-15 16:41:49 -04001840 if (!css_set_populated(cset))
1841 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001842 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001843 get_css_set(cset);
1844 }
Tejun Heof153ad12014-02-25 09:56:49 -05001845 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001846 } while_each_thread(g, p);
1847 read_unlock(&tasklist_lock);
1848out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001849 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001850}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001851
Paul Menagecc31edc2008-10-18 20:28:04 -07001852static void init_cgroup_housekeeping(struct cgroup *cgrp)
1853{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001854 struct cgroup_subsys *ss;
1855 int ssid;
1856
Tejun Heod5c419b2014-05-16 13:22:48 -04001857 INIT_LIST_HEAD(&cgrp->self.sibling);
1858 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo6f60ead2015-09-18 17:54:23 -04001859 INIT_LIST_HEAD(&cgrp->self.files);
Tejun Heo69d02062013-06-12 21:04:50 -07001860 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001861 INIT_LIST_HEAD(&cgrp->pidlists);
1862 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001863 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001864 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001865
1866 for_each_subsys(ss, ssid)
1867 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001868
1869 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001870 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001871}
Paul Menagec6d57f32009-09-23 15:56:19 -07001872
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001873static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001874 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001875{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001876 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001877
Paul Menageddbcc7e2007-10-18 23:39:30 -07001878 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001879 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001880 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001881 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001882 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001883
Paul Menagec6d57f32009-09-23 15:56:19 -07001884 root->flags = opts->flags;
1885 if (opts->release_agent)
1886 strcpy(root->release_agent_path, opts->release_agent);
1887 if (opts->name)
1888 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001889 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001890 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001891}
1892
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001893static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001894{
Tejun Heod427dfe2014-02-11 11:52:48 -05001895 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001896 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001897 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001898 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001899
Tejun Heod427dfe2014-02-11 11:52:48 -05001900 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001901
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001902 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001903 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001904 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001905 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001906
Tejun Heo2aad2a82014-09-24 13:31:50 -04001907 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1908 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001909 if (ret)
1910 goto out;
1911
Tejun Heod427dfe2014-02-11 11:52:48 -05001912 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001913 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001914 * but that's OK - it can only be increased by someone holding
1915 * cgroup_lock, and that's us. The worst that can happen is that we
1916 * have some link structures left over
1917 */
1918 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001919 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001920 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001921
Tejun Heo985ed672014-03-19 10:23:53 -04001922 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001923 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001924 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001925
Tejun Heo2bd59d42014-02-11 11:52:49 -05001926 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1927 KERNFS_ROOT_CREATE_DEACTIVATED,
1928 root_cgrp);
1929 if (IS_ERR(root->kf_root)) {
1930 ret = PTR_ERR(root->kf_root);
1931 goto exit_root_id;
1932 }
1933 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001934
Tejun Heo4df8dc92015-09-18 17:54:23 -04001935 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001936 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001937 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001938
Tejun Heo5df36032014-03-19 10:23:54 -04001939 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001940 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001941 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001942
Tejun Heod427dfe2014-02-11 11:52:48 -05001943 /*
1944 * There must be no failure case after here, since rebinding takes
1945 * care of subsystems' refcounts, which are explicitly dropped in
1946 * the failure exit path.
1947 */
1948 list_add(&root->root_list, &cgroup_roots);
1949 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001950
Tejun Heod427dfe2014-02-11 11:52:48 -05001951 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001952 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001953 * objects.
1954 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001955 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001956 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001957 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001958 if (css_set_populated(cset))
1959 cgroup_update_populated(root_cgrp, true);
1960 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001961 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001962
Tejun Heod5c419b2014-05-16 13:22:48 -04001963 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001964 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001965
Tejun Heo2bd59d42014-02-11 11:52:49 -05001966 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001967 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001968 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001969
Tejun Heo2bd59d42014-02-11 11:52:49 -05001970destroy_root:
1971 kernfs_destroy_root(root->kf_root);
1972 root->kf_root = NULL;
1973exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001974 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001975cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001976 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001977out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001978 free_cgrp_cset_links(&tmp_links);
1979 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001980}
1981
Al Virof7e83572010-07-26 13:23:11 +04001982static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001983 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001984 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001985{
Li Zefan3a32bd72014-06-30 11:50:59 +08001986 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001987 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001988 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001989 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001990 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001991 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001992 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001993 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001994
1995 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001996 * The first time anyone tries to mount a cgroup, enable the list
1997 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001998 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001999 if (!use_task_css_set_links)
2000 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002001
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002002 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002003
Paul Menageddbcc7e2007-10-18 23:39:30 -07002004 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002005 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002006 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002007 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002008
Tejun Heo2bd59d42014-02-11 11:52:49 -05002009 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002010 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04002011 cgrp_dfl_root_visible = true;
2012 root = &cgrp_dfl_root;
2013 cgroup_get(&root->cgrp);
2014 ret = 0;
2015 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002016 }
2017
Li Zefan970317a2014-06-30 11:49:58 +08002018 /*
2019 * Destruction of cgroup root is asynchronous, so subsystems may
2020 * still be dying after the previous unmount. Let's drain the
2021 * dying subsystems. We just need to ensure that the ones
2022 * unmounted previously finish dying and don't care about new ones
2023 * starting. Testing ref liveliness is good enough.
2024 */
2025 for_each_subsys(ss, i) {
2026 if (!(opts.subsys_mask & (1 << i)) ||
2027 ss->root == &cgrp_dfl_root)
2028 continue;
2029
2030 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2031 mutex_unlock(&cgroup_mutex);
2032 msleep(10);
2033 ret = restart_syscall();
2034 goto out_free;
2035 }
2036 cgroup_put(&ss->root->cgrp);
2037 }
2038
Tejun Heo985ed672014-03-19 10:23:53 -04002039 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002040 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002041
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002042 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002043 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002044
Paul Menage817929e2007-10-18 23:39:36 -07002045 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002046 * If we asked for a name then it must match. Also, if
2047 * name matches but sybsys_mask doesn't, we should fail.
2048 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002049 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002050 if (opts.name) {
2051 if (strcmp(opts.name, root->name))
2052 continue;
2053 name_match = true;
2054 }
Tejun Heo31261212013-06-28 17:07:30 -07002055
2056 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002057 * If we asked for subsystems (or explicitly for no
2058 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002059 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002060 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002061 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002062 if (!name_match)
2063 continue;
2064 ret = -EBUSY;
2065 goto out_unlock;
2066 }
Tejun Heo873fe092013-04-14 20:15:26 -07002067
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002068 if (root->flags ^ opts.flags)
2069 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002070
Tejun Heo776f02f2014-02-12 09:29:50 -05002071 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002072 * We want to reuse @root whose lifetime is governed by its
2073 * ->cgrp. Let's check whether @root is alive and keep it
2074 * that way. As cgroup_kill_sb() can happen anytime, we
2075 * want to block it by pinning the sb so that @root doesn't
2076 * get killed before mount is complete.
2077 *
2078 * With the sb pinned, tryget_live can reliably indicate
2079 * whether @root can be reused. If it's being killed,
2080 * drain it. We can use wait_queue for the wait but this
2081 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002082 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002083 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2084 if (IS_ERR(pinned_sb) ||
2085 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002086 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002087 if (!IS_ERR_OR_NULL(pinned_sb))
2088 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002089 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002090 ret = restart_syscall();
2091 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002092 }
2093
2094 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002095 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002096 }
2097
Tejun Heo172a2c062014-03-19 10:23:53 -04002098 /*
2099 * No such thing, create a new one. name= matching without subsys
2100 * specification is allowed for already existing hierarchies but we
2101 * can't create new one without subsys specification.
2102 */
2103 if (!opts.subsys_mask && !opts.none) {
2104 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002105 goto out_unlock;
2106 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002107
Tejun Heo172a2c062014-03-19 10:23:53 -04002108 root = kzalloc(sizeof(*root), GFP_KERNEL);
2109 if (!root) {
2110 ret = -ENOMEM;
2111 goto out_unlock;
2112 }
2113
2114 init_cgroup_root(root, &opts);
2115
Tejun Heo35585572014-02-13 06:58:38 -05002116 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002117 if (ret)
2118 cgroup_free_root(root);
2119
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002120out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002121 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002122out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002123 kfree(opts.release_agent);
2124 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002125
Tejun Heo2bd59d42014-02-11 11:52:49 -05002126 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002127 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002128
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002129 dentry = kernfs_mount(fs_type, flags, root->kf_root,
2130 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002131 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002132 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002133
2134 /*
2135 * If @pinned_sb, we're reusing an existing root and holding an
2136 * extra ref on its sb. Mount is complete. Put the extra ref.
2137 */
2138 if (pinned_sb) {
2139 WARN_ON(new_sb);
2140 deactivate_super(pinned_sb);
2141 }
2142
Tejun Heo2bd59d42014-02-11 11:52:49 -05002143 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002144}
2145
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002146static void cgroup_kill_sb(struct super_block *sb)
2147{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002148 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002149 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002150
Tejun Heo9d755d32014-05-14 09:15:02 -04002151 /*
2152 * If @root doesn't have any mounts or children, start killing it.
2153 * This prevents new mounts by disabling percpu_ref_tryget_live().
2154 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002155 *
2156 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002157 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002158 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002159 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002160 cgroup_put(&root->cgrp);
2161 else
2162 percpu_ref_kill(&root->cgrp.self.refcnt);
2163
Tejun Heo2bd59d42014-02-11 11:52:49 -05002164 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002165}
2166
2167static struct file_system_type cgroup_fs_type = {
2168 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002169 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002170 .kill_sb = cgroup_kill_sb,
2171};
2172
Li Zefana043e3b2008-02-23 15:24:09 -08002173/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002174 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002175 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002176 * @buf: the buffer to write the path into
2177 * @buflen: the length of the buffer
2178 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002179 * Determine @task's cgroup on the first (the one with the lowest non-zero
2180 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2181 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2182 * cgroup controller callbacks.
2183 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002184 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002185 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002186char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002187{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002188 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002189 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002190 int hierarchy_id = 1;
2191 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002192
2193 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002194 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002195
Tejun Heo913ffdb2013-07-11 16:34:48 -07002196 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2197
Tejun Heo857a2be2013-04-14 20:50:08 -07002198 if (root) {
2199 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002200 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002201 } else {
2202 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002203 if (strlcpy(buf, "/", buflen) < buflen)
2204 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002205 }
2206
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002207 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002208 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002209 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002210}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002211EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002212
Tejun Heob3dc0942014-02-25 10:04:01 -05002213/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002214struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002215 /* the src and dst cset list running through cset->mg_node */
2216 struct list_head src_csets;
2217 struct list_head dst_csets;
2218
2219 /*
2220 * Fields for cgroup_taskset_*() iteration.
2221 *
2222 * Before migration is committed, the target migration tasks are on
2223 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2224 * the csets on ->dst_csets. ->csets point to either ->src_csets
2225 * or ->dst_csets depending on whether migration is committed.
2226 *
2227 * ->cur_csets and ->cur_task point to the current task position
2228 * during iteration.
2229 */
2230 struct list_head *csets;
2231 struct css_set *cur_cset;
2232 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002233};
2234
Tejun Heoadaae5d2015-09-11 15:00:21 -04002235#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2236 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2237 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2238 .csets = &tset.src_csets, \
2239}
2240
2241/**
2242 * cgroup_taskset_add - try to add a migration target task to a taskset
2243 * @task: target task
2244 * @tset: target taskset
2245 *
2246 * Add @task, which is a migration target, to @tset. This function becomes
2247 * noop if @task doesn't need to be migrated. @task's css_set should have
2248 * been added as a migration source and @task->cg_list will be moved from
2249 * the css_set's tasks list to mg_tasks one.
2250 */
2251static void cgroup_taskset_add(struct task_struct *task,
2252 struct cgroup_taskset *tset)
2253{
2254 struct css_set *cset;
2255
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002256 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002257
2258 /* @task either already exited or can't exit until the end */
2259 if (task->flags & PF_EXITING)
2260 return;
2261
2262 /* leave @task alone if post_fork() hasn't linked it yet */
2263 if (list_empty(&task->cg_list))
2264 return;
2265
2266 cset = task_css_set(task);
2267 if (!cset->mg_src_cgrp)
2268 return;
2269
2270 list_move_tail(&task->cg_list, &cset->mg_tasks);
2271 if (list_empty(&cset->mg_node))
2272 list_add_tail(&cset->mg_node, &tset->src_csets);
2273 if (list_empty(&cset->mg_dst_cset->mg_node))
2274 list_move_tail(&cset->mg_dst_cset->mg_node,
2275 &tset->dst_csets);
2276}
2277
Tejun Heo2f7ee562011-12-12 18:12:21 -08002278/**
2279 * cgroup_taskset_first - reset taskset and return the first task
2280 * @tset: taskset of interest
2281 *
2282 * @tset iteration is initialized and the first task is returned.
2283 */
2284struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2285{
Tejun Heob3dc0942014-02-25 10:04:01 -05002286 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2287 tset->cur_task = NULL;
2288
2289 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002290}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002291
2292/**
2293 * cgroup_taskset_next - iterate to the next task in taskset
2294 * @tset: taskset of interest
2295 *
2296 * Return the next task in @tset. Iteration must have been initialized
2297 * with cgroup_taskset_first().
2298 */
2299struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2300{
Tejun Heob3dc0942014-02-25 10:04:01 -05002301 struct css_set *cset = tset->cur_cset;
2302 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002303
Tejun Heob3dc0942014-02-25 10:04:01 -05002304 while (&cset->mg_node != tset->csets) {
2305 if (!task)
2306 task = list_first_entry(&cset->mg_tasks,
2307 struct task_struct, cg_list);
2308 else
2309 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002310
Tejun Heob3dc0942014-02-25 10:04:01 -05002311 if (&task->cg_list != &cset->mg_tasks) {
2312 tset->cur_cset = cset;
2313 tset->cur_task = task;
2314 return task;
2315 }
2316
2317 cset = list_next_entry(cset, mg_node);
2318 task = NULL;
2319 }
2320
2321 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002322}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002323
2324/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002325 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2326 * @tset: taget taskset
2327 * @dst_cgrp: destination cgroup
2328 *
2329 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2330 * ->can_attach callbacks fails and guarantees that either all or none of
2331 * the tasks in @tset are migrated. @tset is consumed regardless of
2332 * success.
2333 */
2334static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2335 struct cgroup *dst_cgrp)
2336{
2337 struct cgroup_subsys_state *css, *failed_css = NULL;
2338 struct task_struct *task, *tmp_task;
2339 struct css_set *cset, *tmp_cset;
2340 int i, ret;
2341
2342 /* methods shouldn't be called if no task is actually migrating */
2343 if (list_empty(&tset->src_csets))
2344 return 0;
2345
2346 /* check that we can legitimately attach to the cgroup */
2347 for_each_e_css(css, i, dst_cgrp) {
2348 if (css->ss->can_attach) {
2349 ret = css->ss->can_attach(css, tset);
2350 if (ret) {
2351 failed_css = css;
2352 goto out_cancel_attach;
2353 }
2354 }
2355 }
2356
2357 /*
2358 * Now that we're guaranteed success, proceed to move all tasks to
2359 * the new cgroup. There are no failure cases after here, so this
2360 * is the commit point.
2361 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002362 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002363 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002364 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2365 struct css_set *from_cset = task_css_set(task);
2366 struct css_set *to_cset = cset->mg_dst_cset;
2367
2368 get_css_set(to_cset);
2369 css_set_move_task(task, from_cset, to_cset, true);
2370 put_css_set_locked(from_cset);
2371 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002372 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002373 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002374
2375 /*
2376 * Migration is committed, all target tasks are now on dst_csets.
2377 * Nothing is sensitive to fork() after this point. Notify
2378 * controllers that migration is complete.
2379 */
2380 tset->csets = &tset->dst_csets;
2381
2382 for_each_e_css(css, i, dst_cgrp)
2383 if (css->ss->attach)
2384 css->ss->attach(css, tset);
2385
2386 ret = 0;
2387 goto out_release_tset;
2388
2389out_cancel_attach:
2390 for_each_e_css(css, i, dst_cgrp) {
2391 if (css == failed_css)
2392 break;
2393 if (css->ss->cancel_attach)
2394 css->ss->cancel_attach(css, tset);
2395 }
2396out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002397 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002398 list_splice_init(&tset->dst_csets, &tset->src_csets);
2399 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2400 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2401 list_del_init(&cset->mg_node);
2402 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002403 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002404 return ret;
2405}
2406
2407/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002408 * cgroup_migrate_finish - cleanup after attach
2409 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002410 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002411 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2412 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002413 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002414static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002415{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002416 struct css_set *cset, *tmp_cset;
2417
2418 lockdep_assert_held(&cgroup_mutex);
2419
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002420 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002421 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2422 cset->mg_src_cgrp = NULL;
2423 cset->mg_dst_cset = NULL;
2424 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002425 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002426 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002427 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002428}
2429
2430/**
2431 * cgroup_migrate_add_src - add a migration source css_set
2432 * @src_cset: the source css_set to add
2433 * @dst_cgrp: the destination cgroup
2434 * @preloaded_csets: list of preloaded css_sets
2435 *
2436 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2437 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2438 * up by cgroup_migrate_finish().
2439 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002440 * This function may be called without holding cgroup_threadgroup_rwsem
2441 * even if the target is a process. Threads may be created and destroyed
2442 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2443 * into play and the preloaded css_sets are guaranteed to cover all
2444 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002445 */
2446static void cgroup_migrate_add_src(struct css_set *src_cset,
2447 struct cgroup *dst_cgrp,
2448 struct list_head *preloaded_csets)
2449{
2450 struct cgroup *src_cgrp;
2451
2452 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002453 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002454
2455 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2456
Tejun Heo1958d2d2014-02-25 10:04:03 -05002457 if (!list_empty(&src_cset->mg_preload_node))
2458 return;
2459
2460 WARN_ON(src_cset->mg_src_cgrp);
2461 WARN_ON(!list_empty(&src_cset->mg_tasks));
2462 WARN_ON(!list_empty(&src_cset->mg_node));
2463
2464 src_cset->mg_src_cgrp = src_cgrp;
2465 get_css_set(src_cset);
2466 list_add(&src_cset->mg_preload_node, preloaded_csets);
2467}
2468
2469/**
2470 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002471 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002472 * @preloaded_csets: list of preloaded source css_sets
2473 *
2474 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2475 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002476 * pins all destination css_sets, links each to its source, and append them
2477 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2478 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002479 *
2480 * This function must be called after cgroup_migrate_add_src() has been
2481 * called on each migration source css_set. After migration is performed
2482 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2483 * @preloaded_csets.
2484 */
2485static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2486 struct list_head *preloaded_csets)
2487{
2488 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002489 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002490
2491 lockdep_assert_held(&cgroup_mutex);
2492
Tejun Heof8f22e52014-04-23 11:13:16 -04002493 /*
2494 * Except for the root, child_subsys_mask must be zero for a cgroup
2495 * with tasks so that child cgroups don't compete against tasks.
2496 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002497 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002498 dst_cgrp->child_subsys_mask)
2499 return -EBUSY;
2500
Tejun Heo1958d2d2014-02-25 10:04:03 -05002501 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002502 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002503 struct css_set *dst_cset;
2504
Tejun Heof817de92014-04-23 11:13:16 -04002505 dst_cset = find_css_set(src_cset,
2506 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002507 if (!dst_cset)
2508 goto err;
2509
2510 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002511
2512 /*
2513 * If src cset equals dst, it's noop. Drop the src.
2514 * cgroup_migrate() will skip the cset too. Note that we
2515 * can't handle src == dst as some nodes are used by both.
2516 */
2517 if (src_cset == dst_cset) {
2518 src_cset->mg_src_cgrp = NULL;
2519 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002520 put_css_set(src_cset);
2521 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002522 continue;
2523 }
2524
Tejun Heo1958d2d2014-02-25 10:04:03 -05002525 src_cset->mg_dst_cset = dst_cset;
2526
2527 if (list_empty(&dst_cset->mg_preload_node))
2528 list_add(&dst_cset->mg_preload_node, &csets);
2529 else
Zefan Lia25eb522014-09-19 16:51:00 +08002530 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002531 }
2532
Tejun Heof817de92014-04-23 11:13:16 -04002533 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002534 return 0;
2535err:
2536 cgroup_migrate_finish(&csets);
2537 return -ENOMEM;
2538}
2539
2540/**
2541 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002542 * @leader: the leader of the process or the task to migrate
2543 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002544 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002545 *
2546 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002547 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002548 * caller is also responsible for invoking cgroup_migrate_add_src() and
2549 * cgroup_migrate_prepare_dst() on the targets before invoking this
2550 * function and following up with cgroup_migrate_finish().
2551 *
2552 * As long as a controller's ->can_attach() doesn't fail, this function is
2553 * guaranteed to succeed. This means that, excluding ->can_attach()
2554 * failure, when migrating multiple targets, the success or failure can be
2555 * decided for all targets by invoking group_migrate_prepare_dst() before
2556 * actually starting migrating.
2557 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002558static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2559 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002560{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002561 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2562 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002563
2564 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002565 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2566 * already PF_EXITING could be freed from underneath us unless we
2567 * take an rcu_read_lock.
2568 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002569 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002570 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002571 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002572 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002573 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002574 if (!threadgroup)
2575 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002576 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002577 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002578 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002579
Tejun Heoadaae5d2015-09-11 15:00:21 -04002580 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002581}
2582
Tejun Heo1958d2d2014-02-25 10:04:03 -05002583/**
2584 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2585 * @dst_cgrp: the cgroup to attach to
2586 * @leader: the task or the leader of the threadgroup to be attached
2587 * @threadgroup: attach the whole threadgroup?
2588 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002589 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002590 */
2591static int cgroup_attach_task(struct cgroup *dst_cgrp,
2592 struct task_struct *leader, bool threadgroup)
2593{
2594 LIST_HEAD(preloaded_csets);
2595 struct task_struct *task;
2596 int ret;
2597
2598 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002599 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002600 rcu_read_lock();
2601 task = leader;
2602 do {
2603 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2604 &preloaded_csets);
2605 if (!threadgroup)
2606 break;
2607 } while_each_thread(leader, task);
2608 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002609 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002610
2611 /* prepare dst csets and commit */
2612 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2613 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002614 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002615
2616 cgroup_migrate_finish(&preloaded_csets);
2617 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002618}
2619
Tejun Heo187fe842015-06-18 16:54:28 -04002620static int cgroup_procs_write_permission(struct task_struct *task,
2621 struct cgroup *dst_cgrp,
2622 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002623{
2624 const struct cred *cred = current_cred();
2625 const struct cred *tcred = get_task_cred(task);
2626 int ret = 0;
2627
2628 /*
2629 * even if we're attaching all tasks in the thread group, we only
2630 * need to check permissions on one of them.
2631 */
2632 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2633 !uid_eq(cred->euid, tcred->uid) &&
2634 !uid_eq(cred->euid, tcred->suid))
2635 ret = -EACCES;
2636
Tejun Heo187fe842015-06-18 16:54:28 -04002637 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2638 struct super_block *sb = of->file->f_path.dentry->d_sb;
2639 struct cgroup *cgrp;
2640 struct inode *inode;
2641
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002642 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002643 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002644 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002645
2646 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2647 cgrp = cgroup_parent(cgrp);
2648
2649 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002650 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002651 if (inode) {
2652 ret = inode_permission(inode, MAY_WRITE);
2653 iput(inode);
2654 }
2655 }
2656
Tejun Heodedf22e2015-06-18 16:54:28 -04002657 put_cred(tcred);
2658 return ret;
2659}
2660
Ben Blum74a11662011-05-26 16:25:20 -07002661/*
2662 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002663 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002664 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002665 */
Tejun Heoacbef752014-05-13 12:16:22 -04002666static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2667 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002668{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002669 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002670 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002671 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002672 int ret;
2673
Tejun Heoacbef752014-05-13 12:16:22 -04002674 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2675 return -EINVAL;
2676
Tejun Heoe76ecae2014-05-13 12:19:23 -04002677 cgrp = cgroup_kn_lock_live(of->kn);
2678 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002679 return -ENODEV;
2680
Tejun Heo3014dde2015-09-16 13:03:02 -04002681 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002682 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002683 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002684 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002685 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002686 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002687 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002688 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002689 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002690 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002691 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002692
2693 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002694 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002695
2696 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002697 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002698 * trapped in a cpuset, or RT worker may be born in a cgroup
2699 * with no rt_runtime allocated. Just say no.
2700 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002701 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002702 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002703 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002704 }
2705
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002706 get_task_struct(tsk);
2707 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002708
Tejun Heo187fe842015-06-18 16:54:28 -04002709 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002710 if (!ret)
2711 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002712
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002713 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002714 goto out_unlock_threadgroup;
2715
2716out_unlock_rcu:
2717 rcu_read_unlock();
2718out_unlock_threadgroup:
2719 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002720 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002721 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002722}
2723
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002724/**
2725 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2726 * @from: attach to all cgroups of a given task
2727 * @tsk: the task to be attached
2728 */
2729int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2730{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002731 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002732 int retval = 0;
2733
Tejun Heo47cfcd02013-04-07 09:29:51 -07002734 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002735 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002736 struct cgroup *from_cgrp;
2737
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002738 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002739 continue;
2740
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002741 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002742 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002743 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002744
Li Zefan6f4b7e62013-07-31 16:18:36 +08002745 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002746 if (retval)
2747 break;
2748 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002749 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002750
2751 return retval;
2752}
2753EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2754
Tejun Heoacbef752014-05-13 12:16:22 -04002755static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2756 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002757{
Tejun Heoacbef752014-05-13 12:16:22 -04002758 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002759}
2760
Tejun Heoacbef752014-05-13 12:16:22 -04002761static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2762 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002763{
Tejun Heoacbef752014-05-13 12:16:22 -04002764 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002765}
2766
Tejun Heo451af502014-05-13 12:16:21 -04002767static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2768 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002769{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002770 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002771
Tejun Heoe76ecae2014-05-13 12:19:23 -04002772 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2773
2774 cgrp = cgroup_kn_lock_live(of->kn);
2775 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002776 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002777 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002778 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2779 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002780 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002781 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002782 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002783}
2784
Tejun Heo2da8ca82013-12-05 12:28:04 -05002785static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002786{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002787 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002788
Tejun Heo46cfeb02014-05-13 12:11:00 -04002789 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002790 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002791 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002792 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002793 return 0;
2794}
2795
Tejun Heo2da8ca82013-12-05 12:28:04 -05002796static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002797{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002798 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002799 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002800}
2801
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002802static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002803{
2804 struct cgroup_subsys *ss;
2805 bool printed = false;
2806 int ssid;
2807
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002808 for_each_subsys_which(ss, ssid, &ss_mask) {
2809 if (printed)
2810 seq_putc(seq, ' ');
2811 seq_printf(seq, "%s", ss->name);
2812 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002813 }
2814 if (printed)
2815 seq_putc(seq, '\n');
2816}
2817
2818/* show controllers which are currently attached to the default hierarchy */
2819static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2820{
2821 struct cgroup *cgrp = seq_css(seq)->cgroup;
2822
Tejun Heo5533e012014-05-14 19:33:07 -04002823 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2824 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002825 return 0;
2826}
2827
2828/* show controllers which are enabled from the parent */
2829static int cgroup_controllers_show(struct seq_file *seq, void *v)
2830{
2831 struct cgroup *cgrp = seq_css(seq)->cgroup;
2832
Tejun Heo667c2492014-07-08 18:02:56 -04002833 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002834 return 0;
2835}
2836
2837/* show controllers which are enabled for a given cgroup's children */
2838static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2839{
2840 struct cgroup *cgrp = seq_css(seq)->cgroup;
2841
Tejun Heo667c2492014-07-08 18:02:56 -04002842 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002843 return 0;
2844}
2845
2846/**
2847 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2848 * @cgrp: root of the subtree to update csses for
2849 *
2850 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2851 * css associations need to be updated accordingly. This function looks up
2852 * all css_sets which are attached to the subtree, creates the matching
2853 * updated css_sets and migrates the tasks to the new ones.
2854 */
2855static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2856{
2857 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002858 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002859 struct cgroup_subsys_state *css;
2860 struct css_set *src_cset;
2861 int ret;
2862
Tejun Heof8f22e52014-04-23 11:13:16 -04002863 lockdep_assert_held(&cgroup_mutex);
2864
Tejun Heo3014dde2015-09-16 13:03:02 -04002865 percpu_down_write(&cgroup_threadgroup_rwsem);
2866
Tejun Heof8f22e52014-04-23 11:13:16 -04002867 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002868 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002869 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2870 struct cgrp_cset_link *link;
2871
2872 /* self is not affected by child_subsys_mask change */
2873 if (css->cgroup == cgrp)
2874 continue;
2875
2876 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2877 cgroup_migrate_add_src(link->cset, cgrp,
2878 &preloaded_csets);
2879 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002880 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002881
2882 /* NULL dst indicates self on default hierarchy */
2883 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2884 if (ret)
2885 goto out_finish;
2886
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002887 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002888 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002889 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002890
2891 /* src_csets precede dst_csets, break on the first dst_cset */
2892 if (!src_cset->mg_src_cgrp)
2893 break;
2894
Tejun Heo10265072015-09-11 15:00:22 -04002895 /* all tasks in src_csets need to be migrated */
2896 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2897 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002898 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002899 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002900
Tejun Heo10265072015-09-11 15:00:22 -04002901 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002902out_finish:
2903 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002904 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002905 return ret;
2906}
2907
2908/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002909static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2910 char *buf, size_t nbytes,
2911 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002912{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002913 unsigned long enable = 0, disable = 0;
2914 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002915 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002916 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002917 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002918 int ssid, ret;
2919
2920 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002921 * Parse input - space separated list of subsystem names prefixed
2922 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002923 */
Tejun Heo451af502014-05-13 12:16:21 -04002924 buf = strstrip(buf);
2925 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002926 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2927
Tejun Heod37167a2014-05-13 12:10:59 -04002928 if (tok[0] == '\0')
2929 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002930 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04002931 if (!cgroup_ssid_enabled(ssid) ||
2932 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002933 continue;
2934
2935 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002936 enable |= 1 << ssid;
2937 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002938 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002939 disable |= 1 << ssid;
2940 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002941 } else {
2942 return -EINVAL;
2943 }
2944 break;
2945 }
2946 if (ssid == CGROUP_SUBSYS_COUNT)
2947 return -EINVAL;
2948 }
2949
Tejun Heoa9746d82014-05-13 12:19:22 -04002950 cgrp = cgroup_kn_lock_live(of->kn);
2951 if (!cgrp)
2952 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002953
2954 for_each_subsys(ss, ssid) {
2955 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002956 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002957 enable &= ~(1 << ssid);
2958 continue;
2959 }
2960
Tejun Heoc29adf22014-07-08 18:02:56 -04002961 /* unavailable or not enabled on the parent? */
2962 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2963 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002964 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002965 ret = -ENOENT;
2966 goto out_unlock;
2967 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002968 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002969 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002970 disable &= ~(1 << ssid);
2971 continue;
2972 }
2973
2974 /* a child has it enabled? */
2975 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002976 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002977 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002978 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002979 }
2980 }
2981 }
2982 }
2983
2984 if (!enable && !disable) {
2985 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002986 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002987 }
2988
2989 /*
Tejun Heo667c2492014-07-08 18:02:56 -04002990 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002991 * with tasks so that child cgroups don't compete against tasks.
2992 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002993 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002994 ret = -EBUSY;
2995 goto out_unlock;
2996 }
2997
2998 /*
Tejun Heof63070d2014-07-08 18:02:57 -04002999 * Update subsys masks and calculate what needs to be done. More
3000 * subsystems than specified may need to be enabled or disabled
3001 * depending on subsystem dependencies.
3002 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05003003 old_sc = cgrp->subtree_control;
3004 old_ss = cgrp->child_subsys_mask;
3005 new_sc = (old_sc | enable) & ~disable;
3006 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04003007
Tejun Heo755bf5e2014-11-18 02:49:50 -05003008 css_enable = ~old_ss & new_ss;
3009 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04003010 enable |= css_enable;
3011 disable |= css_disable;
3012
Tejun Heodb6e3052014-11-18 02:49:51 -05003013 /*
3014 * Because css offlining is asynchronous, userland might try to
3015 * re-enable the same controller while the previous instance is
3016 * still around. In such cases, wait till it's gone using
3017 * offline_waitq.
3018 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10003019 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05003020 cgroup_for_each_live_child(child, cgrp) {
3021 DEFINE_WAIT(wait);
3022
3023 if (!cgroup_css(child, ss))
3024 continue;
3025
3026 cgroup_get(child);
3027 prepare_to_wait(&child->offline_waitq, &wait,
3028 TASK_UNINTERRUPTIBLE);
3029 cgroup_kn_unlock(of->kn);
3030 schedule();
3031 finish_wait(&child->offline_waitq, &wait);
3032 cgroup_put(child);
3033
3034 return restart_syscall();
3035 }
3036 }
3037
Tejun Heo755bf5e2014-11-18 02:49:50 -05003038 cgrp->subtree_control = new_sc;
3039 cgrp->child_subsys_mask = new_ss;
3040
Tejun Heof63070d2014-07-08 18:02:57 -04003041 /*
3042 * Create new csses or make the existing ones visible. A css is
3043 * created invisible if it's being implicitly enabled through
3044 * dependency. An invisible css is made visible when the userland
3045 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04003046 */
3047 for_each_subsys(ss, ssid) {
3048 if (!(enable & (1 << ssid)))
3049 continue;
3050
3051 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04003052 if (css_enable & (1 << ssid))
3053 ret = create_css(child, ss,
3054 cgrp->subtree_control & (1 << ssid));
3055 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003056 ret = css_populate_dir(cgroup_css(child, ss),
3057 NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003058 if (ret)
3059 goto err_undo_css;
3060 }
3061 }
3062
Tejun Heoc29adf22014-07-08 18:02:56 -04003063 /*
3064 * At this point, cgroup_e_css() results reflect the new csses
3065 * making the following cgroup_update_dfl_csses() properly update
3066 * css associations of all tasks in the subtree.
3067 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003068 ret = cgroup_update_dfl_csses(cgrp);
3069 if (ret)
3070 goto err_undo_css;
3071
Tejun Heof63070d2014-07-08 18:02:57 -04003072 /*
3073 * All tasks are migrated out of disabled csses. Kill or hide
3074 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003075 * disabled while other subsystems are still depending on it. The
3076 * css must not actively control resources and be in the vanilla
3077 * state if it's made visible again later. Controllers which may
3078 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003079 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003080 for_each_subsys(ss, ssid) {
3081 if (!(disable & (1 << ssid)))
3082 continue;
3083
Tejun Heof63070d2014-07-08 18:02:57 -04003084 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003085 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3086
3087 if (css_disable & (1 << ssid)) {
3088 kill_css(css);
3089 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003090 css_clear_dir(css, NULL);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003091 if (ss->css_reset)
3092 ss->css_reset(css);
3093 }
Tejun Heof63070d2014-07-08 18:02:57 -04003094 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003095 }
3096
Tejun Heo56c807b2014-11-18 02:49:51 -05003097 /*
3098 * The effective csses of all the descendants (excluding @cgrp) may
3099 * have changed. Subsystems can optionally subscribe to this event
3100 * by implementing ->css_e_css_changed() which is invoked if any of
3101 * the effective csses seen from the css's cgroup may have changed.
3102 */
3103 for_each_subsys(ss, ssid) {
3104 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
3105 struct cgroup_subsys_state *css;
3106
3107 if (!ss->css_e_css_changed || !this_css)
3108 continue;
3109
3110 css_for_each_descendant_pre(css, this_css)
3111 if (css != this_css)
3112 ss->css_e_css_changed(css);
3113 }
3114
Tejun Heof8f22e52014-04-23 11:13:16 -04003115 kernfs_activate(cgrp->kn);
3116 ret = 0;
3117out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003118 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003119 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003120
3121err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003122 cgrp->subtree_control = old_sc;
3123 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003124
3125 for_each_subsys(ss, ssid) {
3126 if (!(enable & (1 << ssid)))
3127 continue;
3128
3129 cgroup_for_each_live_child(child, cgrp) {
3130 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003131
3132 if (!css)
3133 continue;
3134
3135 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003136 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003137 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003138 css_clear_dir(css, NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003139 }
3140 }
3141 goto out_unlock;
3142}
3143
Tejun Heo4a07c222015-09-18 17:54:22 -04003144static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003145{
Tejun Heo4a07c222015-09-18 17:54:22 -04003146 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003147 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003148 return 0;
3149}
3150
Tejun Heo2bd59d42014-02-11 11:52:49 -05003151static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3152 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003153{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003154 struct cgroup *cgrp = of->kn->parent->priv;
3155 struct cftype *cft = of->kn->priv;
3156 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003157 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003158
Tejun Heob4168642014-05-13 12:16:21 -04003159 if (cft->write)
3160 return cft->write(of, buf, nbytes, off);
3161
Tejun Heo2bd59d42014-02-11 11:52:49 -05003162 /*
3163 * kernfs guarantees that a file isn't deleted with operations in
3164 * flight, which means that the matching css is and stays alive and
3165 * doesn't need to be pinned. The RCU locking is not necessary
3166 * either. It's just for the convenience of using cgroup_css().
3167 */
3168 rcu_read_lock();
3169 css = cgroup_css(cgrp, cft->ss);
3170 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003171
Tejun Heo451af502014-05-13 12:16:21 -04003172 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003173 unsigned long long v;
3174 ret = kstrtoull(buf, 0, &v);
3175 if (!ret)
3176 ret = cft->write_u64(css, cft, v);
3177 } else if (cft->write_s64) {
3178 long long v;
3179 ret = kstrtoll(buf, 0, &v);
3180 if (!ret)
3181 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003182 } else {
3183 ret = -EINVAL;
3184 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003185
Tejun Heoa742c592013-12-05 12:28:03 -05003186 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003187}
3188
Tejun Heo6612f052013-12-05 12:28:04 -05003189static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003190{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003191 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003192}
3193
3194static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3195{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003196 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003197}
3198
3199static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3200{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003201 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003202}
3203
3204static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3205{
Tejun Heo7da11272013-12-05 12:28:04 -05003206 struct cftype *cft = seq_cft(m);
3207 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003208
Tejun Heo2da8ca82013-12-05 12:28:04 -05003209 if (cft->seq_show)
3210 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003211
Tejun Heo896f5192013-12-05 12:28:04 -05003212 if (cft->read_u64)
3213 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3214 else if (cft->read_s64)
3215 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3216 else
3217 return -EINVAL;
3218 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003219}
3220
Tejun Heo2bd59d42014-02-11 11:52:49 -05003221static struct kernfs_ops cgroup_kf_single_ops = {
3222 .atomic_write_len = PAGE_SIZE,
3223 .write = cgroup_file_write,
3224 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003225};
3226
Tejun Heo2bd59d42014-02-11 11:52:49 -05003227static struct kernfs_ops cgroup_kf_ops = {
3228 .atomic_write_len = PAGE_SIZE,
3229 .write = cgroup_file_write,
3230 .seq_start = cgroup_seqfile_start,
3231 .seq_next = cgroup_seqfile_next,
3232 .seq_stop = cgroup_seqfile_stop,
3233 .seq_show = cgroup_seqfile_show,
3234};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003235
3236/*
3237 * cgroup_rename - Only allow simple rename of directories in place.
3238 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003239static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3240 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003241{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003242 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003243 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003244
Tejun Heo2bd59d42014-02-11 11:52:49 -05003245 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003246 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003247 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003248 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003249
Tejun Heo6db8e852013-06-14 11:18:22 -07003250 /*
3251 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003252 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003253 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003254 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003255 return -EPERM;
3256
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003257 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003258 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003259 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003260 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003261 */
3262 kernfs_break_active_protection(new_parent);
3263 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003264
Tejun Heo2bd59d42014-02-11 11:52:49 -05003265 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003266
Tejun Heo2bd59d42014-02-11 11:52:49 -05003267 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003268
Tejun Heo2bd59d42014-02-11 11:52:49 -05003269 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003270
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003271 kernfs_unbreak_active_protection(kn);
3272 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003273 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003274}
3275
Tejun Heo49957f82014-04-07 16:44:47 -04003276/* set uid and gid of cgroup dirs and files to that of the creator */
3277static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3278{
3279 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3280 .ia_uid = current_fsuid(),
3281 .ia_gid = current_fsgid(), };
3282
3283 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3284 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3285 return 0;
3286
3287 return kernfs_setattr(kn, &iattr);
3288}
3289
Tejun Heo4df8dc92015-09-18 17:54:23 -04003290static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3291 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003292{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003293 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003294 struct kernfs_node *kn;
3295 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003296 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003297
Tejun Heo2bd59d42014-02-11 11:52:49 -05003298#ifdef CONFIG_DEBUG_LOCK_ALLOC
3299 key = &cft->lockdep_key;
3300#endif
3301 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3302 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003303 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003304 if (IS_ERR(kn))
3305 return PTR_ERR(kn);
3306
3307 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003308 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003309 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003310 return ret;
3311 }
3312
Tejun Heo6f60ead2015-09-18 17:54:23 -04003313 if (cft->file_offset) {
3314 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3315
3316 kernfs_get(kn);
3317 cfile->kn = kn;
3318 list_add(&cfile->node, &css->files);
3319 }
3320
Tejun Heof8f22e52014-04-23 11:13:16 -04003321 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003322}
3323
Tejun Heob1f28d32013-06-28 16:24:10 -07003324/**
3325 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003326 * @css: the target css
3327 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003328 * @cfts: array of cftypes to be added
3329 * @is_add: whether to add or remove
3330 *
3331 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003332 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003333 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003334static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3335 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003336 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003337{
Tejun Heo6732ed82015-09-18 17:54:23 -04003338 struct cftype *cft, *cft_end = NULL;
Tejun Heob1f28d32013-06-28 16:24:10 -07003339 int ret;
3340
Tejun Heo01f64742014-05-13 12:19:23 -04003341 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003342
Tejun Heo6732ed82015-09-18 17:54:23 -04003343restart:
3344 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003345 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003346 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003347 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003348 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003349 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003350 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003351 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003352 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003353 continue;
3354
Li Zefan2739d3c2013-01-21 18:18:33 +08003355 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003356 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003357 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003358 pr_warn("%s: failed to add %s, err=%d\n",
3359 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003360 cft_end = cft;
3361 is_add = false;
3362 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003363 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003364 } else {
3365 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003366 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003367 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003368 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003369}
3370
Tejun Heo21a2d342014-02-12 09:29:49 -05003371static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003372{
3373 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003374 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003375 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003376 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003377 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003378
Tejun Heo01f64742014-05-13 12:19:23 -04003379 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003380
Li Zefane8c82d22013-06-18 18:48:37 +08003381 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003382 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003383 struct cgroup *cgrp = css->cgroup;
3384
Li Zefane8c82d22013-06-18 18:48:37 +08003385 if (cgroup_is_dead(cgrp))
3386 continue;
3387
Tejun Heo4df8dc92015-09-18 17:54:23 -04003388 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003389 if (ret)
3390 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003391 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003392
3393 if (is_add && !ret)
3394 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003395 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003396}
3397
Tejun Heo2da440a2014-02-11 11:52:48 -05003398static void cgroup_exit_cftypes(struct cftype *cfts)
3399{
3400 struct cftype *cft;
3401
Tejun Heo2bd59d42014-02-11 11:52:49 -05003402 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3403 /* free copy for custom atomic_write_len, see init_cftypes() */
3404 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3405 kfree(cft->kf_ops);
3406 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003407 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003408
3409 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003410 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003411 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003412}
3413
Tejun Heo2bd59d42014-02-11 11:52:49 -05003414static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003415{
3416 struct cftype *cft;
3417
Tejun Heo2bd59d42014-02-11 11:52:49 -05003418 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3419 struct kernfs_ops *kf_ops;
3420
Tejun Heo0adb0702014-02-12 09:29:48 -05003421 WARN_ON(cft->ss || cft->kf_ops);
3422
Tejun Heo2bd59d42014-02-11 11:52:49 -05003423 if (cft->seq_start)
3424 kf_ops = &cgroup_kf_ops;
3425 else
3426 kf_ops = &cgroup_kf_single_ops;
3427
3428 /*
3429 * Ugh... if @cft wants a custom max_write_len, we need to
3430 * make a copy of kf_ops to set its atomic_write_len.
3431 */
3432 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3433 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3434 if (!kf_ops) {
3435 cgroup_exit_cftypes(cfts);
3436 return -ENOMEM;
3437 }
3438 kf_ops->atomic_write_len = cft->max_write_len;
3439 }
3440
3441 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003442 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003443 }
3444
3445 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003446}
3447
Tejun Heo21a2d342014-02-12 09:29:49 -05003448static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3449{
Tejun Heo01f64742014-05-13 12:19:23 -04003450 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003451
3452 if (!cfts || !cfts[0].ss)
3453 return -ENOENT;
3454
3455 list_del(&cfts->node);
3456 cgroup_apply_cftypes(cfts, false);
3457 cgroup_exit_cftypes(cfts);
3458 return 0;
3459}
3460
Tejun Heo8e3f6542012-04-01 12:09:55 -07003461/**
Tejun Heo80b13582014-02-12 09:29:48 -05003462 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3463 * @cfts: zero-length name terminated array of cftypes
3464 *
3465 * Unregister @cfts. Files described by @cfts are removed from all
3466 * existing cgroups and all future cgroups won't have them either. This
3467 * function can be called anytime whether @cfts' subsys is attached or not.
3468 *
3469 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3470 * registered.
3471 */
3472int cgroup_rm_cftypes(struct cftype *cfts)
3473{
Tejun Heo21a2d342014-02-12 09:29:49 -05003474 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003475
Tejun Heo01f64742014-05-13 12:19:23 -04003476 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003477 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003478 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003479 return ret;
3480}
3481
3482/**
3483 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3484 * @ss: target cgroup subsystem
3485 * @cfts: zero-length name terminated array of cftypes
3486 *
3487 * Register @cfts to @ss. Files described by @cfts are created for all
3488 * existing cgroups to which @ss is attached and all future cgroups will
3489 * have them too. This function can be called anytime whether @ss is
3490 * attached or not.
3491 *
3492 * Returns 0 on successful registration, -errno on failure. Note that this
3493 * function currently returns 0 as long as @cfts registration is successful
3494 * even if some file creation attempts on existing cgroups fail.
3495 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003496static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003497{
Tejun Heo9ccece82013-06-28 16:24:11 -07003498 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003499
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003500 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003501 return 0;
3502
Li Zefandc5736e2014-02-17 10:41:50 +08003503 if (!cfts || cfts[0].name[0] == '\0')
3504 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003505
Tejun Heo2bd59d42014-02-11 11:52:49 -05003506 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003507 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003508 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003509
Tejun Heo01f64742014-05-13 12:19:23 -04003510 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003511
Tejun Heo0adb0702014-02-12 09:29:48 -05003512 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003513 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003514 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003515 cgroup_rm_cftypes_locked(cfts);
3516
Tejun Heo01f64742014-05-13 12:19:23 -04003517 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003518 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003519}
Tejun Heo79578622012-04-01 12:09:56 -07003520
3521/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003522 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3523 * @ss: target cgroup subsystem
3524 * @cfts: zero-length name terminated array of cftypes
3525 *
3526 * Similar to cgroup_add_cftypes() but the added files are only used for
3527 * the default hierarchy.
3528 */
3529int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3530{
3531 struct cftype *cft;
3532
3533 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003534 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003535 return cgroup_add_cftypes(ss, cfts);
3536}
3537
3538/**
3539 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3540 * @ss: target cgroup subsystem
3541 * @cfts: zero-length name terminated array of cftypes
3542 *
3543 * Similar to cgroup_add_cftypes() but the added files are only used for
3544 * the legacy hierarchies.
3545 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003546int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3547{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003548 struct cftype *cft;
3549
Tejun Heoe4b70372015-10-15 17:00:43 -04003550 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3551 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003552 return cgroup_add_cftypes(ss, cfts);
3553}
3554
Li Zefana043e3b2008-02-23 15:24:09 -08003555/**
3556 * cgroup_task_count - count the number of tasks in a cgroup.
3557 * @cgrp: the cgroup in question
3558 *
3559 * Return the number of tasks in the cgroup.
3560 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003561static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003562{
3563 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003564 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003565
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003566 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003567 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3568 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003569 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003570 return count;
3571}
3572
Tejun Heo574bd9f2012-11-09 09:12:29 -08003573/**
Tejun Heo492eb212013-08-08 20:11:25 -04003574 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003575 * @pos: the current position (%NULL to initiate traversal)
3576 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003577 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003578 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003579 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003580 * that @parent and @pos are accessible. The next sibling is guaranteed to
3581 * be returned regardless of their states.
3582 *
3583 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3584 * css which finished ->css_online() is guaranteed to be visible in the
3585 * future iterations and will stay visible until the last reference is put.
3586 * A css which hasn't finished ->css_online() or already finished
3587 * ->css_offline() may show up during traversal. It's each subsystem's
3588 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003589 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003590struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3591 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003592{
Tejun Heoc2931b72014-05-16 13:22:51 -04003593 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003594
Tejun Heo8353da12014-05-13 12:19:23 -04003595 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003596
3597 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003598 * @pos could already have been unlinked from the sibling list.
3599 * Once a cgroup is removed, its ->sibling.next is no longer
3600 * updated when its next sibling changes. CSS_RELEASED is set when
3601 * @pos is taken off list, at which time its next pointer is valid,
3602 * and, as releases are serialized, the one pointed to by the next
3603 * pointer is guaranteed to not have started release yet. This
3604 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3605 * critical section, the one pointed to by its next pointer is
3606 * guaranteed to not have finished its RCU grace period even if we
3607 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003608 *
Tejun Heode3f0342014-05-16 13:22:49 -04003609 * If @pos has CSS_RELEASED set, its next pointer can't be
3610 * dereferenced; however, as each css is given a monotonically
3611 * increasing unique serial number and always appended to the
3612 * sibling list, the next one can be found by walking the parent's
3613 * children until the first css with higher serial number than
3614 * @pos's. While this path can be slower, it happens iff iteration
3615 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003616 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003617 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003618 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3619 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3620 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003621 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003622 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003623 if (next->serial_nr > pos->serial_nr)
3624 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003625 }
3626
Tejun Heo3b281af2014-04-23 11:13:15 -04003627 /*
3628 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003629 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003630 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003631 if (&next->sibling != &parent->children)
3632 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003633 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003634}
Tejun Heo53fa5262013-05-24 10:55:38 +09003635
3636/**
Tejun Heo492eb212013-08-08 20:11:25 -04003637 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003638 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003639 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003640 *
Tejun Heo492eb212013-08-08 20:11:25 -04003641 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003642 * to visit for pre-order traversal of @root's descendants. @root is
3643 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003644 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003645 * While this function requires cgroup_mutex or RCU read locking, it
3646 * doesn't require the whole traversal to be contained in a single critical
3647 * section. This function will return the correct next descendant as long
3648 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003649 *
3650 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3651 * css which finished ->css_online() is guaranteed to be visible in the
3652 * future iterations and will stay visible until the last reference is put.
3653 * A css which hasn't finished ->css_online() or already finished
3654 * ->css_offline() may show up during traversal. It's each subsystem's
3655 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003656 */
Tejun Heo492eb212013-08-08 20:11:25 -04003657struct cgroup_subsys_state *
3658css_next_descendant_pre(struct cgroup_subsys_state *pos,
3659 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003660{
Tejun Heo492eb212013-08-08 20:11:25 -04003661 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003662
Tejun Heo8353da12014-05-13 12:19:23 -04003663 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003664
Tejun Heobd8815a2013-08-08 20:11:27 -04003665 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003666 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003667 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003668
3669 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003670 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003671 if (next)
3672 return next;
3673
3674 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003675 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003676 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003677 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003678 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003679 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003680 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003681
3682 return NULL;
3683}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003684
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003685/**
Tejun Heo492eb212013-08-08 20:11:25 -04003686 * css_rightmost_descendant - return the rightmost descendant of a css
3687 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003688 *
Tejun Heo492eb212013-08-08 20:11:25 -04003689 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3690 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003691 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003692 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003693 * While this function requires cgroup_mutex or RCU read locking, it
3694 * doesn't require the whole traversal to be contained in a single critical
3695 * section. This function will return the correct rightmost descendant as
3696 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003697 */
Tejun Heo492eb212013-08-08 20:11:25 -04003698struct cgroup_subsys_state *
3699css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003700{
Tejun Heo492eb212013-08-08 20:11:25 -04003701 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003702
Tejun Heo8353da12014-05-13 12:19:23 -04003703 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003704
3705 do {
3706 last = pos;
3707 /* ->prev isn't RCU safe, walk ->next till the end */
3708 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003709 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003710 pos = tmp;
3711 } while (pos);
3712
3713 return last;
3714}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003715
Tejun Heo492eb212013-08-08 20:11:25 -04003716static struct cgroup_subsys_state *
3717css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003718{
Tejun Heo492eb212013-08-08 20:11:25 -04003719 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003720
3721 do {
3722 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003723 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003724 } while (pos);
3725
3726 return last;
3727}
3728
3729/**
Tejun Heo492eb212013-08-08 20:11:25 -04003730 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003731 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003732 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003733 *
Tejun Heo492eb212013-08-08 20:11:25 -04003734 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003735 * to visit for post-order traversal of @root's descendants. @root is
3736 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003737 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003738 * While this function requires cgroup_mutex or RCU read locking, it
3739 * doesn't require the whole traversal to be contained in a single critical
3740 * section. This function will return the correct next descendant as long
3741 * as both @pos and @cgroup are accessible and @pos is a descendant of
3742 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003743 *
3744 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3745 * css which finished ->css_online() is guaranteed to be visible in the
3746 * future iterations and will stay visible until the last reference is put.
3747 * A css which hasn't finished ->css_online() or already finished
3748 * ->css_offline() may show up during traversal. It's each subsystem's
3749 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003750 */
Tejun Heo492eb212013-08-08 20:11:25 -04003751struct cgroup_subsys_state *
3752css_next_descendant_post(struct cgroup_subsys_state *pos,
3753 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003754{
Tejun Heo492eb212013-08-08 20:11:25 -04003755 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003756
Tejun Heo8353da12014-05-13 12:19:23 -04003757 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003758
Tejun Heo58b79a92013-09-06 15:31:08 -04003759 /* if first iteration, visit leftmost descendant which may be @root */
3760 if (!pos)
3761 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003762
Tejun Heobd8815a2013-08-08 20:11:27 -04003763 /* if we visited @root, we're done */
3764 if (pos == root)
3765 return NULL;
3766
Tejun Heo574bd9f2012-11-09 09:12:29 -08003767 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003768 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003769 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003770 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003771
3772 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003773 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003774}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003775
Tejun Heof3d46502014-05-16 13:22:52 -04003776/**
3777 * css_has_online_children - does a css have online children
3778 * @css: the target css
3779 *
3780 * Returns %true if @css has any online children; otherwise, %false. This
3781 * function can be called from any context but the caller is responsible
3782 * for synchronizing against on/offlining as necessary.
3783 */
3784bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003785{
Tejun Heof3d46502014-05-16 13:22:52 -04003786 struct cgroup_subsys_state *child;
3787 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003788
3789 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003790 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003791 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003792 ret = true;
3793 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003794 }
3795 }
3796 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003797 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003798}
3799
Tejun Heo0942eee2013-08-08 20:11:26 -04003800/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003801 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003802 * @it: the iterator to advance
3803 *
3804 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003805 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003806static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003807{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003808 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003809 struct cgrp_cset_link *link;
3810 struct css_set *cset;
3811
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003812 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003813
Tejun Heod5158762013-08-08 20:11:26 -04003814 /* Advance to the next non-empty css_set */
3815 do {
3816 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003817 if (l == it->cset_head) {
3818 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003819 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003820 return;
3821 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003822
3823 if (it->ss) {
3824 cset = container_of(l, struct css_set,
3825 e_cset_node[it->ss->id]);
3826 } else {
3827 link = list_entry(l, struct cgrp_cset_link, cset_link);
3828 cset = link->cset;
3829 }
Tejun Heo0de09422015-10-15 16:41:49 -04003830 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05003831
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003832 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003833
3834 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003835 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003836 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003837 it->task_pos = cset->mg_tasks.next;
3838
3839 it->tasks_head = &cset->tasks;
3840 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04003841
3842 /*
3843 * We don't keep css_sets locked across iteration steps and thus
3844 * need to take steps to ensure that iteration can be resumed after
3845 * the lock is re-acquired. Iteration is performed at two levels -
3846 * css_sets and tasks in them.
3847 *
3848 * Once created, a css_set never leaves its cgroup lists, so a
3849 * pinned css_set is guaranteed to stay put and we can resume
3850 * iteration afterwards.
3851 *
3852 * Tasks may leave @cset across iteration steps. This is resolved
3853 * by registering each iterator with the css_set currently being
3854 * walked and making css_set_move_task() advance iterators whose
3855 * next task is leaving.
3856 */
3857 if (it->cur_cset) {
3858 list_del(&it->iters_node);
3859 put_css_set_locked(it->cur_cset);
3860 }
3861 get_css_set(cset);
3862 it->cur_cset = cset;
3863 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04003864}
3865
Tejun Heoecb9d532015-10-15 16:41:52 -04003866static void css_task_iter_advance(struct css_task_iter *it)
3867{
3868 struct list_head *l = it->task_pos;
3869
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003870 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04003871 WARN_ON_ONCE(!l);
3872
3873 /*
3874 * Advance iterator to find next entry. cset->tasks is consumed
3875 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3876 * next cset.
3877 */
3878 l = l->next;
3879
3880 if (l == it->tasks_head)
3881 l = it->mg_tasks_head->next;
3882
3883 if (l == it->mg_tasks_head)
3884 css_task_iter_advance_css_set(it);
3885 else
3886 it->task_pos = l;
3887}
3888
Tejun Heo0942eee2013-08-08 20:11:26 -04003889/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003890 * css_task_iter_start - initiate task iteration
3891 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003892 * @it: the task iterator to use
3893 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003894 * Initiate iteration through the tasks of @css. The caller can call
3895 * css_task_iter_next() to walk through the tasks until the function
3896 * returns NULL. On completion of iteration, css_task_iter_end() must be
3897 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003898 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003899void css_task_iter_start(struct cgroup_subsys_state *css,
3900 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003901{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003902 /* no one should try to iterate before mounting cgroups */
3903 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003904
Tejun Heoed27b9f2015-10-15 16:41:52 -04003905 memset(it, 0, sizeof(*it));
3906
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003907 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003908
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003909 it->ss = css->ss;
3910
3911 if (it->ss)
3912 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3913 else
3914 it->cset_pos = &css->cgroup->cset_links;
3915
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003916 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003917
Tejun Heoecb9d532015-10-15 16:41:52 -04003918 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003919
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003920 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003921}
Paul Menage817929e2007-10-18 23:39:36 -07003922
Tejun Heo0942eee2013-08-08 20:11:26 -04003923/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003924 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003925 * @it: the task iterator being iterated
3926 *
3927 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003928 * initialized via css_task_iter_start(). Returns NULL when the iteration
3929 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003930 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003931struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003932{
Tejun Heod5745672015-10-29 11:43:05 +09003933 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04003934 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09003935 it->cur_task = NULL;
3936 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003937
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003938 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003939
Tejun Heod5745672015-10-29 11:43:05 +09003940 if (it->task_pos) {
3941 it->cur_task = list_entry(it->task_pos, struct task_struct,
3942 cg_list);
3943 get_task_struct(it->cur_task);
3944 css_task_iter_advance(it);
3945 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04003946
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003947 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003948
3949 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07003950}
3951
Tejun Heo0942eee2013-08-08 20:11:26 -04003952/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003953 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003954 * @it: the task iterator to finish
3955 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003956 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003957 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003958void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003959{
Tejun Heoed27b9f2015-10-15 16:41:52 -04003960 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003961 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003962 list_del(&it->iters_node);
3963 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003964 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003965 }
3966
3967 if (it->cur_task)
3968 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07003969}
3970
3971/**
3972 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3973 * @to: cgroup to which the tasks will be moved
3974 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003975 *
3976 * Locking rules between cgroup_post_fork() and the migration path
3977 * guarantee that, if a task is forking while being migrated, the new child
3978 * is guaranteed to be either visible in the source cgroup after the
3979 * parent's migration is complete or put into the target cgroup. No task
3980 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003981 */
3982int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3983{
Tejun Heo952aaa12014-02-25 10:04:03 -05003984 LIST_HEAD(preloaded_csets);
3985 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003986 struct css_task_iter it;
3987 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003988 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003989
Tejun Heo952aaa12014-02-25 10:04:03 -05003990 mutex_lock(&cgroup_mutex);
3991
3992 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003993 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05003994 list_for_each_entry(link, &from->cset_links, cset_link)
3995 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003996 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05003997
3998 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3999 if (ret)
4000 goto out_err;
4001
4002 /*
4003 * Migrate tasks one-by-one until @form is empty. This fails iff
4004 * ->can_attach() fails.
4005 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004006 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004007 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004008 task = css_task_iter_next(&it);
4009 if (task)
4010 get_task_struct(task);
4011 css_task_iter_end(&it);
4012
4013 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004014 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004015 put_task_struct(task);
4016 }
4017 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004018out_err:
4019 cgroup_migrate_finish(&preloaded_csets);
4020 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004021 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004022}
4023
Paul Menage817929e2007-10-18 23:39:36 -07004024/*
Ben Blum102a7752009-09-23 15:56:26 -07004025 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004026 *
4027 * Reading this file can return large amounts of data if a cgroup has
4028 * *lots* of attached tasks. So it may need several calls to read(),
4029 * but we cannot guarantee that the information we produce is correct
4030 * unless we produce it entirely atomically.
4031 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004032 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004033
Li Zefan24528252012-01-20 11:58:43 +08004034/* which pidlist file are we talking about? */
4035enum cgroup_filetype {
4036 CGROUP_FILE_PROCS,
4037 CGROUP_FILE_TASKS,
4038};
4039
4040/*
4041 * A pidlist is a list of pids that virtually represents the contents of one
4042 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4043 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4044 * to the cgroup.
4045 */
4046struct cgroup_pidlist {
4047 /*
4048 * used to find which pidlist is wanted. doesn't change as long as
4049 * this particular list stays in the list.
4050 */
4051 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4052 /* array of xids */
4053 pid_t *list;
4054 /* how many elements the above list has */
4055 int length;
Li Zefan24528252012-01-20 11:58:43 +08004056 /* each of these stored in a list by its cgroup */
4057 struct list_head links;
4058 /* pointer to the cgroup we belong to, for list removal purposes */
4059 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004060 /* for delayed destruction */
4061 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004062};
4063
Paul Menagebbcb81d2007-10-18 23:39:32 -07004064/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004065 * The following two functions "fix" the issue where there are more pids
4066 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4067 * TODO: replace with a kernel-wide solution to this problem
4068 */
4069#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4070static void *pidlist_allocate(int count)
4071{
4072 if (PIDLIST_TOO_LARGE(count))
4073 return vmalloc(count * sizeof(pid_t));
4074 else
4075 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4076}
Tejun Heob1a21362013-11-29 10:42:58 -05004077
Ben Blumd1d9fd32009-09-23 15:56:28 -07004078static void pidlist_free(void *p)
4079{
Bandan Das58794512015-03-02 17:51:10 -05004080 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004081}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004082
4083/*
Tejun Heob1a21362013-11-29 10:42:58 -05004084 * Used to destroy all pidlists lingering waiting for destroy timer. None
4085 * should be left afterwards.
4086 */
4087static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4088{
4089 struct cgroup_pidlist *l, *tmp_l;
4090
4091 mutex_lock(&cgrp->pidlist_mutex);
4092 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4093 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4094 mutex_unlock(&cgrp->pidlist_mutex);
4095
4096 flush_workqueue(cgroup_pidlist_destroy_wq);
4097 BUG_ON(!list_empty(&cgrp->pidlists));
4098}
4099
4100static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4101{
4102 struct delayed_work *dwork = to_delayed_work(work);
4103 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4104 destroy_dwork);
4105 struct cgroup_pidlist *tofree = NULL;
4106
4107 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004108
4109 /*
Tejun Heo04502362013-11-29 10:42:59 -05004110 * Destroy iff we didn't get queued again. The state won't change
4111 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004112 */
Tejun Heo04502362013-11-29 10:42:59 -05004113 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004114 list_del(&l->links);
4115 pidlist_free(l->list);
4116 put_pid_ns(l->key.ns);
4117 tofree = l;
4118 }
4119
Tejun Heob1a21362013-11-29 10:42:58 -05004120 mutex_unlock(&l->owner->pidlist_mutex);
4121 kfree(tofree);
4122}
4123
4124/*
Ben Blum102a7752009-09-23 15:56:26 -07004125 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004126 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004127 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004128static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004129{
Ben Blum102a7752009-09-23 15:56:26 -07004130 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004131
4132 /*
4133 * we presume the 0th element is unique, so i starts at 1. trivial
4134 * edge cases first; no work needs to be done for either
4135 */
4136 if (length == 0 || length == 1)
4137 return length;
4138 /* src and dest walk down the list; dest counts unique elements */
4139 for (src = 1; src < length; src++) {
4140 /* find next unique element */
4141 while (list[src] == list[src-1]) {
4142 src++;
4143 if (src == length)
4144 goto after;
4145 }
4146 /* dest always points to where the next unique element goes */
4147 list[dest] = list[src];
4148 dest++;
4149 }
4150after:
Ben Blum102a7752009-09-23 15:56:26 -07004151 return dest;
4152}
4153
Tejun Heoafb2bc12013-11-29 10:42:59 -05004154/*
4155 * The two pid files - task and cgroup.procs - guaranteed that the result
4156 * is sorted, which forced this whole pidlist fiasco. As pid order is
4157 * different per namespace, each namespace needs differently sorted list,
4158 * making it impossible to use, for example, single rbtree of member tasks
4159 * sorted by task pointer. As pidlists can be fairly large, allocating one
4160 * per open file is dangerous, so cgroup had to implement shared pool of
4161 * pidlists keyed by cgroup and namespace.
4162 *
4163 * All this extra complexity was caused by the original implementation
4164 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004165 * want to do away with it. Explicitly scramble sort order if on the
4166 * default hierarchy so that no such expectation exists in the new
4167 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004168 *
4169 * Scrambling is done by swapping every two consecutive bits, which is
4170 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4171 */
4172static pid_t pid_fry(pid_t pid)
4173{
4174 unsigned a = pid & 0x55555555;
4175 unsigned b = pid & 0xAAAAAAAA;
4176
4177 return (a << 1) | (b >> 1);
4178}
4179
4180static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4181{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004182 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004183 return pid_fry(pid);
4184 else
4185 return pid;
4186}
4187
Ben Blum102a7752009-09-23 15:56:26 -07004188static int cmppid(const void *a, const void *b)
4189{
4190 return *(pid_t *)a - *(pid_t *)b;
4191}
4192
Tejun Heoafb2bc12013-11-29 10:42:59 -05004193static int fried_cmppid(const void *a, const void *b)
4194{
4195 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4196}
4197
Ben Blum72a8cb32009-09-23 15:56:27 -07004198static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4199 enum cgroup_filetype type)
4200{
4201 struct cgroup_pidlist *l;
4202 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004203 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004204
Tejun Heoe6b81712013-11-29 10:42:59 -05004205 lockdep_assert_held(&cgrp->pidlist_mutex);
4206
4207 list_for_each_entry(l, &cgrp->pidlists, links)
4208 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004209 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004210 return NULL;
4211}
4212
Ben Blum72a8cb32009-09-23 15:56:27 -07004213/*
4214 * find the appropriate pidlist for our purpose (given procs vs tasks)
4215 * returns with the lock on that pidlist already held, and takes care
4216 * of the use count, or returns NULL with no locks held if we're out of
4217 * memory.
4218 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004219static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4220 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004221{
4222 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004223
Tejun Heoe6b81712013-11-29 10:42:59 -05004224 lockdep_assert_held(&cgrp->pidlist_mutex);
4225
4226 l = cgroup_pidlist_find(cgrp, type);
4227 if (l)
4228 return l;
4229
Ben Blum72a8cb32009-09-23 15:56:27 -07004230 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004231 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004232 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004233 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004234
Tejun Heob1a21362013-11-29 10:42:58 -05004235 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004236 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004237 /* don't need task_nsproxy() if we're looking at ourself */
4238 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004239 l->owner = cgrp;
4240 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004241 return l;
4242}
4243
4244/*
Ben Blum102a7752009-09-23 15:56:26 -07004245 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4246 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004247static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4248 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004249{
4250 pid_t *array;
4251 int length;
4252 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004253 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004254 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004255 struct cgroup_pidlist *l;
4256
Tejun Heo4bac00d2013-11-29 10:42:59 -05004257 lockdep_assert_held(&cgrp->pidlist_mutex);
4258
Ben Blum102a7752009-09-23 15:56:26 -07004259 /*
4260 * If cgroup gets more users after we read count, we won't have
4261 * enough space - tough. This race is indistinguishable to the
4262 * caller from the case that the additional cgroup users didn't
4263 * show up until sometime later on.
4264 */
4265 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004266 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004267 if (!array)
4268 return -ENOMEM;
4269 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004270 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004271 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004272 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004273 break;
Ben Blum102a7752009-09-23 15:56:26 -07004274 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004275 if (type == CGROUP_FILE_PROCS)
4276 pid = task_tgid_vnr(tsk);
4277 else
4278 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004279 if (pid > 0) /* make sure to only use valid results */
4280 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004281 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004282 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004283 length = n;
4284 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004285 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004286 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4287 else
4288 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004289 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004290 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004291
Tejun Heoe6b81712013-11-29 10:42:59 -05004292 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004293 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004294 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004295 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004296 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004297
4298 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004299 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004300 l->list = array;
4301 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004302 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004303 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004304}
4305
Balbir Singh846c7bb2007-10-18 23:39:44 -07004306/**
Li Zefana043e3b2008-02-23 15:24:09 -08004307 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004308 * @stats: cgroupstats to fill information into
4309 * @dentry: A dentry entry belonging to the cgroup for which stats have
4310 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004311 *
4312 * Build and fill cgroupstats so that taskstats can export it to user
4313 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004314 */
4315int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4316{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004317 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004318 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004319 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004320 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004321
Tejun Heo2bd59d42014-02-11 11:52:49 -05004322 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4323 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4324 kernfs_type(kn) != KERNFS_DIR)
4325 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004326
Li Zefanbad34662014-02-14 16:54:28 +08004327 mutex_lock(&cgroup_mutex);
4328
Tejun Heo2bd59d42014-02-11 11:52:49 -05004329 /*
4330 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004331 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004332 * @kn->priv is RCU safe. Let's do the RCU dancing.
4333 */
4334 rcu_read_lock();
4335 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004336 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004337 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004338 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004339 return -ENOENT;
4340 }
Li Zefanbad34662014-02-14 16:54:28 +08004341 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004342
Tejun Heo9d800df2014-05-14 09:15:00 -04004343 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004344 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004345 switch (tsk->state) {
4346 case TASK_RUNNING:
4347 stats->nr_running++;
4348 break;
4349 case TASK_INTERRUPTIBLE:
4350 stats->nr_sleeping++;
4351 break;
4352 case TASK_UNINTERRUPTIBLE:
4353 stats->nr_uninterruptible++;
4354 break;
4355 case TASK_STOPPED:
4356 stats->nr_stopped++;
4357 break;
4358 default:
4359 if (delayacct_is_task_waiting_on_io(tsk))
4360 stats->nr_io_wait++;
4361 break;
4362 }
4363 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004364 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004365
Li Zefanbad34662014-02-14 16:54:28 +08004366 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004367 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004368}
4369
Paul Menage8f3ff202009-09-23 15:56:25 -07004370
Paul Menagecc31edc2008-10-18 20:28:04 -07004371/*
Ben Blum102a7752009-09-23 15:56:26 -07004372 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004373 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004374 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004375 */
4376
Ben Blum102a7752009-09-23 15:56:26 -07004377static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004378{
4379 /*
4380 * Initially we receive a position value that corresponds to
4381 * one more than the last pid shown (or 0 on the first call or
4382 * after a seek to the start). Use a binary-search to find the
4383 * next pid to display, if any
4384 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004385 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004386 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004387 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004388 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004389 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004390 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004391
Tejun Heo4bac00d2013-11-29 10:42:59 -05004392 mutex_lock(&cgrp->pidlist_mutex);
4393
4394 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004395 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004396 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004397 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004398 * could already have been destroyed.
4399 */
Tejun Heo5d224442013-12-05 12:28:04 -05004400 if (of->priv)
4401 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004402
4403 /*
4404 * Either this is the first start() after open or the matching
4405 * pidlist has been destroyed inbetween. Create a new one.
4406 */
Tejun Heo5d224442013-12-05 12:28:04 -05004407 if (!of->priv) {
4408 ret = pidlist_array_load(cgrp, type,
4409 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004410 if (ret)
4411 return ERR_PTR(ret);
4412 }
Tejun Heo5d224442013-12-05 12:28:04 -05004413 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004414
Paul Menagecc31edc2008-10-18 20:28:04 -07004415 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004416 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004417
Paul Menagecc31edc2008-10-18 20:28:04 -07004418 while (index < end) {
4419 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004420 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004421 index = mid;
4422 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004423 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004424 index = mid + 1;
4425 else
4426 end = mid;
4427 }
4428 }
4429 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004430 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004431 return NULL;
4432 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004433 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004434 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004435 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004436}
4437
Ben Blum102a7752009-09-23 15:56:26 -07004438static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004439{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004440 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004441 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004442
Tejun Heo5d224442013-12-05 12:28:04 -05004443 if (l)
4444 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004445 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004446 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004447}
4448
Ben Blum102a7752009-09-23 15:56:26 -07004449static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004450{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004451 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004452 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004453 pid_t *p = v;
4454 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004455 /*
4456 * Advance to the next pid in the array. If this goes off the
4457 * end, we're done
4458 */
4459 p++;
4460 if (p >= end) {
4461 return NULL;
4462 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004463 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004464 return p;
4465 }
4466}
4467
Ben Blum102a7752009-09-23 15:56:26 -07004468static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004469{
Joe Perches94ff2122015-04-15 16:18:20 -07004470 seq_printf(s, "%d\n", *(int *)v);
4471
4472 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004473}
4474
Tejun Heo182446d2013-08-08 20:11:24 -04004475static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4476 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004477{
Tejun Heo182446d2013-08-08 20:11:24 -04004478 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004479}
4480
Tejun Heo182446d2013-08-08 20:11:24 -04004481static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4482 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004483{
Paul Menage6379c102008-07-25 01:47:01 -07004484 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004485 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004486 else
Tejun Heo182446d2013-08-08 20:11:24 -04004487 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004488 return 0;
4489}
4490
Tejun Heo182446d2013-08-08 20:11:24 -04004491static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4492 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004493{
Tejun Heo182446d2013-08-08 20:11:24 -04004494 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004495}
4496
Tejun Heo182446d2013-08-08 20:11:24 -04004497static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4498 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004499{
4500 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004501 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004502 else
Tejun Heo182446d2013-08-08 20:11:24 -04004503 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004504 return 0;
4505}
4506
Tejun Heoa14c6872014-07-15 11:05:09 -04004507/* cgroup core interface files for the default hierarchy */
4508static struct cftype cgroup_dfl_base_files[] = {
4509 {
4510 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004511 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004512 .seq_start = cgroup_pidlist_start,
4513 .seq_next = cgroup_pidlist_next,
4514 .seq_stop = cgroup_pidlist_stop,
4515 .seq_show = cgroup_pidlist_show,
4516 .private = CGROUP_FILE_PROCS,
4517 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004518 },
4519 {
4520 .name = "cgroup.controllers",
4521 .flags = CFTYPE_ONLY_ON_ROOT,
4522 .seq_show = cgroup_root_controllers_show,
4523 },
4524 {
4525 .name = "cgroup.controllers",
4526 .flags = CFTYPE_NOT_ON_ROOT,
4527 .seq_show = cgroup_controllers_show,
4528 },
4529 {
4530 .name = "cgroup.subtree_control",
4531 .seq_show = cgroup_subtree_control_show,
4532 .write = cgroup_subtree_control_write,
4533 },
4534 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004535 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004536 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004537 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004538 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004539 },
4540 { } /* terminate */
4541};
4542
4543/* cgroup core interface files for the legacy hierarchies */
4544static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004545 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004546 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004547 .seq_start = cgroup_pidlist_start,
4548 .seq_next = cgroup_pidlist_next,
4549 .seq_stop = cgroup_pidlist_stop,
4550 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004551 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004552 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004553 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004554 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004555 .name = "cgroup.clone_children",
4556 .read_u64 = cgroup_clone_children_read,
4557 .write_u64 = cgroup_clone_children_write,
4558 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004559 {
Tejun Heo873fe092013-04-14 20:15:26 -07004560 .name = "cgroup.sane_behavior",
4561 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004562 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004563 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004564 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004565 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004566 .seq_start = cgroup_pidlist_start,
4567 .seq_next = cgroup_pidlist_next,
4568 .seq_stop = cgroup_pidlist_stop,
4569 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004570 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004571 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004572 },
4573 {
4574 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004575 .read_u64 = cgroup_read_notify_on_release,
4576 .write_u64 = cgroup_write_notify_on_release,
4577 },
Tejun Heo873fe092013-04-14 20:15:26 -07004578 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004579 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004580 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004581 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004582 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004583 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004584 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004585 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004586};
4587
Tejun Heo0c21ead2013-08-13 20:22:51 -04004588/*
4589 * css destruction is four-stage process.
4590 *
4591 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4592 * Implemented in kill_css().
4593 *
4594 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004595 * and thus css_tryget_online() is guaranteed to fail, the css can be
4596 * offlined by invoking offline_css(). After offlining, the base ref is
4597 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004598 *
4599 * 3. When the percpu_ref reaches zero, the only possible remaining
4600 * accessors are inside RCU read sections. css_release() schedules the
4601 * RCU callback.
4602 *
4603 * 4. After the grace period, the css can be freed. Implemented in
4604 * css_free_work_fn().
4605 *
4606 * It is actually hairier because both step 2 and 4 require process context
4607 * and thus involve punting to css->destroy_work adding two additional
4608 * steps to the already complex sequence.
4609 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004610static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004611{
4612 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004613 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004614 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004615 struct cgroup *cgrp = css->cgroup;
Tejun Heo6f60ead2015-09-18 17:54:23 -04004616 struct cgroup_file *cfile;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004617
Tejun Heo9a1049d2014-06-28 08:10:14 -04004618 percpu_ref_exit(&css->refcnt);
4619
Tejun Heo6f60ead2015-09-18 17:54:23 -04004620 list_for_each_entry(cfile, &css->files, node)
4621 kernfs_put(cfile->kn);
4622
Vladimir Davydov01e58652015-02-12 14:59:26 -08004623 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004624 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004625 int id = css->id;
4626
Tejun Heo9d755d32014-05-14 09:15:02 -04004627 if (css->parent)
4628 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004629
Vladimir Davydov01e58652015-02-12 14:59:26 -08004630 ss->css_free(css);
4631 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004632 cgroup_put(cgrp);
4633 } else {
4634 /* cgroup free path */
4635 atomic_dec(&cgrp->root->nr_cgrps);
4636 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004637 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004638
Tejun Heod51f39b2014-05-16 13:22:48 -04004639 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004640 /*
4641 * We get a ref to the parent, and put the ref when
4642 * this cgroup is being freed, so it's guaranteed
4643 * that the parent won't be destroyed before its
4644 * children.
4645 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004646 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004647 kernfs_put(cgrp->kn);
4648 kfree(cgrp);
4649 } else {
4650 /*
4651 * This is root cgroup's refcnt reaching zero,
4652 * which indicates that the root should be
4653 * released.
4654 */
4655 cgroup_destroy_root(cgrp->root);
4656 }
4657 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004658}
4659
4660static void css_free_rcu_fn(struct rcu_head *rcu_head)
4661{
4662 struct cgroup_subsys_state *css =
4663 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4664
Tejun Heo0c21ead2013-08-13 20:22:51 -04004665 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004666 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004667}
4668
Tejun Heo25e15d82014-05-14 09:15:02 -04004669static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004670{
4671 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004672 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004673 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004674 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004675
Tejun Heo1fed1b22014-05-16 13:22:49 -04004676 mutex_lock(&cgroup_mutex);
4677
Tejun Heode3f0342014-05-16 13:22:49 -04004678 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004679 list_del_rcu(&css->sibling);
4680
Tejun Heo9d755d32014-05-14 09:15:02 -04004681 if (ss) {
4682 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004683 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004684 if (ss->css_released)
4685 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004686 } else {
4687 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004688 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4689 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004690
4691 /*
4692 * There are two control paths which try to determine
4693 * cgroup from dentry without going through kernfs -
4694 * cgroupstats_build() and css_tryget_online_from_dir().
4695 * Those are supported by RCU protecting clearing of
4696 * cgrp->kn->priv backpointer.
4697 */
4698 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004699 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004700
Tejun Heo1fed1b22014-05-16 13:22:49 -04004701 mutex_unlock(&cgroup_mutex);
4702
Tejun Heo0c21ead2013-08-13 20:22:51 -04004703 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004704}
4705
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004706static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004707{
4708 struct cgroup_subsys_state *css =
4709 container_of(ref, struct cgroup_subsys_state, refcnt);
4710
Tejun Heo25e15d82014-05-14 09:15:02 -04004711 INIT_WORK(&css->destroy_work, css_release_work_fn);
4712 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004713}
4714
Tejun Heoddfcada2014-05-04 15:09:14 -04004715static void init_and_link_css(struct cgroup_subsys_state *css,
4716 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004717{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004718 lockdep_assert_held(&cgroup_mutex);
4719
Tejun Heoddfcada2014-05-04 15:09:14 -04004720 cgroup_get(cgrp);
4721
Tejun Heod5c419b2014-05-16 13:22:48 -04004722 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004723 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004724 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004725 INIT_LIST_HEAD(&css->sibling);
4726 INIT_LIST_HEAD(&css->children);
Tejun Heo6f60ead2015-09-18 17:54:23 -04004727 INIT_LIST_HEAD(&css->files);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004728 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004729
Tejun Heod51f39b2014-05-16 13:22:48 -04004730 if (cgroup_parent(cgrp)) {
4731 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004732 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004733 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004734
Tejun Heoca8bdca2013-08-26 18:40:56 -04004735 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004736}
4737
Li Zefan2a4ac632013-07-31 16:16:40 +08004738/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004739static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004740{
Tejun Heo623f9262013-08-13 11:01:55 -04004741 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004742 int ret = 0;
4743
Tejun Heoa31f2d32012-11-19 08:13:37 -08004744 lockdep_assert_held(&cgroup_mutex);
4745
Tejun Heo92fb9742012-11-19 08:13:38 -08004746 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004747 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004748 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004749 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004750 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004751 }
Tejun Heob1929db2012-11-19 08:13:38 -08004752 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004753}
4754
Li Zefan2a4ac632013-07-31 16:16:40 +08004755/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004756static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004757{
Tejun Heo623f9262013-08-13 11:01:55 -04004758 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004759
4760 lockdep_assert_held(&cgroup_mutex);
4761
4762 if (!(css->flags & CSS_ONLINE))
4763 return;
4764
Li Zefand7eeac12013-03-12 15:35:59 -07004765 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004766 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004767
Tejun Heoeb954192013-08-08 20:11:23 -04004768 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004769 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004770
4771 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004772}
4773
Tejun Heoc81c925a2013-12-06 15:11:56 -05004774/**
4775 * create_css - create a cgroup_subsys_state
4776 * @cgrp: the cgroup new css will be associated with
4777 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004778 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004779 *
4780 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004781 * css is online and installed in @cgrp with all interface files created if
4782 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004783 */
Tejun Heof63070d2014-07-08 18:02:57 -04004784static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4785 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004786{
Tejun Heod51f39b2014-05-16 13:22:48 -04004787 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004788 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004789 struct cgroup_subsys_state *css;
4790 int err;
4791
Tejun Heoc81c925a2013-12-06 15:11:56 -05004792 lockdep_assert_held(&cgroup_mutex);
4793
Tejun Heo1fed1b22014-05-16 13:22:49 -04004794 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004795 if (IS_ERR(css))
4796 return PTR_ERR(css);
4797
Tejun Heoddfcada2014-05-04 15:09:14 -04004798 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004799
Tejun Heo2aad2a82014-09-24 13:31:50 -04004800 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004801 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004802 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004803
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004804 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004805 if (err < 0)
4806 goto err_free_percpu_ref;
4807 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004808
Tejun Heof63070d2014-07-08 18:02:57 -04004809 if (visible) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04004810 err = css_populate_dir(css, NULL);
Tejun Heof63070d2014-07-08 18:02:57 -04004811 if (err)
4812 goto err_free_id;
4813 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004814
4815 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004816 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004817 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004818
4819 err = online_css(css);
4820 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004821 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004822
Tejun Heoc81c925a2013-12-06 15:11:56 -05004823 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004824 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004825 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 -04004826 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004827 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004828 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004829 ss->warned_broken_hierarchy = true;
4830 }
4831
4832 return 0;
4833
Tejun Heo1fed1b22014-05-16 13:22:49 -04004834err_list_del:
4835 list_del_rcu(&css->sibling);
Tejun Heo4df8dc92015-09-18 17:54:23 -04004836 css_clear_dir(css, NULL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004837err_free_id:
4838 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004839err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004840 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004841err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004842 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004843 return err;
4844}
4845
Tejun Heob3bfd982014-05-13 12:19:22 -04004846static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4847 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004848{
Tejun Heoa9746d82014-05-13 12:19:22 -04004849 struct cgroup *parent, *cgrp;
4850 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004851 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004852 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004853 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004854
Alban Crequy71b1fb52014-08-18 12:20:20 +01004855 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4856 */
4857 if (strchr(name, '\n'))
4858 return -EINVAL;
4859
Tejun Heoa9746d82014-05-13 12:19:22 -04004860 parent = cgroup_kn_lock_live(parent_kn);
4861 if (!parent)
4862 return -ENODEV;
4863 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004864
Tejun Heo0a950f62012-11-19 09:02:12 -08004865 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004866 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004867 if (!cgrp) {
4868 ret = -ENOMEM;
4869 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004870 }
4871
Tejun Heo2aad2a82014-09-24 13:31:50 -04004872 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004873 if (ret)
4874 goto out_free_cgrp;
4875
Li Zefan0ab02ca2014-02-11 16:05:46 +08004876 /*
4877 * Temporarily set the pointer to NULL, so idr_find() won't return
4878 * a half-baked cgroup.
4879 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004880 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004881 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004882 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004883 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004884 }
4885
Paul Menagecc31edc2008-10-18 20:28:04 -07004886 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004887
Tejun Heo9d800df2014-05-14 09:15:00 -04004888 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004889 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004890
Li Zefanb6abdb02008-03-04 14:28:19 -08004891 if (notify_on_release(parent))
4892 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4893
Tejun Heo2260e7f2012-11-19 08:13:38 -08004894 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4895 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004896
Tejun Heo2bd59d42014-02-11 11:52:49 -05004897 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004898 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004899 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004900 ret = PTR_ERR(kn);
4901 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004902 }
4903 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004904
Tejun Heo6f305582014-02-12 09:29:50 -05004905 /*
4906 * This extra ref will be put in cgroup_free_fn() and guarantees
4907 * that @cgrp->kn is always accessible.
4908 */
4909 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004910
Tejun Heo0cb51d72014-05-16 13:22:49 -04004911 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004912
Tejun Heo4e139af2012-11-19 08:13:36 -08004913 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004914 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004915 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004916 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004917
Tejun Heo0d802552013-12-06 15:11:56 -05004918 /*
4919 * @cgrp is now fully operational. If something fails after this
4920 * point, it'll be released via the normal destruction path.
4921 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004922 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004923
Tejun Heoba0f4d72014-05-13 12:19:22 -04004924 ret = cgroup_kn_set_ugid(kn);
4925 if (ret)
4926 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004927
Tejun Heo4df8dc92015-09-18 17:54:23 -04004928 ret = css_populate_dir(&cgrp->self, NULL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004929 if (ret)
4930 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004931
Tejun Heo9d403e92013-12-06 15:11:56 -05004932 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004933 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004934 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004935 ret = create_css(cgrp, ss,
4936 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004937 if (ret)
4938 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004939 }
Tejun Heoa8638032012-11-09 09:12:29 -08004940 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004941
Tejun Heobd53d612014-04-23 11:13:16 -04004942 /*
4943 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004944 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004945 */
Tejun Heo667c2492014-07-08 18:02:56 -04004946 if (!cgroup_on_dfl(cgrp)) {
4947 cgrp->subtree_control = parent->subtree_control;
4948 cgroup_refresh_child_subsys_mask(cgrp);
4949 }
Tejun Heof392e512014-04-23 11:13:14 -04004950
Tejun Heo2bd59d42014-02-11 11:52:49 -05004951 kernfs_activate(kn);
4952
Tejun Heoba0f4d72014-05-13 12:19:22 -04004953 ret = 0;
4954 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004955
Tejun Heoba0f4d72014-05-13 12:19:22 -04004956out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004957 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004958out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004959 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004960out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004961 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004962out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004963 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004964 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004965
4966out_destroy:
4967 cgroup_destroy_locked(cgrp);
4968 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004969}
4970
Tejun Heo223dbc32013-08-13 20:22:50 -04004971/*
4972 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004973 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4974 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004975 */
4976static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004977{
Tejun Heo223dbc32013-08-13 20:22:50 -04004978 struct cgroup_subsys_state *css =
4979 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004980
Tejun Heof20104d2013-08-13 20:22:50 -04004981 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004982 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004983 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004984
Tejun Heo09a503ea2013-08-13 20:22:50 -04004985 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004986}
4987
Tejun Heo223dbc32013-08-13 20:22:50 -04004988/* css kill confirmation processing requires process context, bounce */
4989static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004990{
4991 struct cgroup_subsys_state *css =
4992 container_of(ref, struct cgroup_subsys_state, refcnt);
4993
Tejun Heo223dbc32013-08-13 20:22:50 -04004994 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004995 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004996}
4997
Tejun Heof392e512014-04-23 11:13:14 -04004998/**
4999 * kill_css - destroy a css
5000 * @css: css to destroy
5001 *
5002 * This function initiates destruction of @css by removing cgroup interface
5003 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005004 * asynchronously once css_tryget_online() is guaranteed to fail and when
5005 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005006 */
5007static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005008{
Tejun Heo01f64742014-05-13 12:19:23 -04005009 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005010
Tejun Heo2bd59d42014-02-11 11:52:49 -05005011 /*
5012 * This must happen before css is disassociated with its cgroup.
5013 * See seq_css() for details.
5014 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005015 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005016
Tejun Heoedae0c32013-08-13 20:22:51 -04005017 /*
5018 * Killing would put the base ref, but we need to keep it alive
5019 * until after ->css_offline().
5020 */
5021 css_get(css);
5022
5023 /*
5024 * cgroup core guarantees that, by the time ->css_offline() is
5025 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005026 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005027 * proceed to offlining css's because percpu_ref_kill() doesn't
5028 * guarantee that the ref is seen as killed on all CPUs on return.
5029 *
5030 * Use percpu_ref_kill_and_confirm() to get notifications as each
5031 * css is confirmed to be seen as killed on all CPUs.
5032 */
5033 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005034}
5035
5036/**
5037 * cgroup_destroy_locked - the first stage of cgroup destruction
5038 * @cgrp: cgroup to be destroyed
5039 *
5040 * css's make use of percpu refcnts whose killing latency shouldn't be
5041 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005042 * guarantee that css_tryget_online() won't succeed by the time
5043 * ->css_offline() is invoked. To satisfy all the requirements,
5044 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005045 *
5046 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5047 * userland visible parts and start killing the percpu refcnts of
5048 * css's. Set up so that the next stage will be kicked off once all
5049 * the percpu refcnts are confirmed to be killed.
5050 *
5051 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5052 * rest of destruction. Once all cgroup references are gone, the
5053 * cgroup is RCU-freed.
5054 *
5055 * This function implements s1. After this step, @cgrp is gone as far as
5056 * the userland is concerned and a new cgroup with the same name may be
5057 * created. As cgroup doesn't care about the names internally, this
5058 * doesn't cause any problem.
5059 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005060static int cgroup_destroy_locked(struct cgroup *cgrp)
5061 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005062{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005063 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005064 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005065
Tejun Heo42809dd2012-11-19 08:13:37 -08005066 lockdep_assert_held(&cgroup_mutex);
5067
Tejun Heo91486f62015-10-15 16:41:51 -04005068 /*
5069 * Only migration can raise populated from zero and we're already
5070 * holding cgroup_mutex.
5071 */
5072 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005073 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005074
Tejun Heo1a90dd52012-11-05 09:16:59 -08005075 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005076 * Make sure there's no live children. We can't test emptiness of
5077 * ->self.children as dead children linger on it while being
5078 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005079 */
Tejun Heof3d46502014-05-16 13:22:52 -04005080 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005081 return -EBUSY;
5082
5083 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005084 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005085 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005086 */
Tejun Heo184faf32014-05-16 13:22:51 -04005087 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005088
Tejun Heo249f3462014-05-14 09:15:01 -04005089 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005090 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005091 kill_css(css);
5092
Tejun Heo455050d2013-06-13 19:27:41 -07005093 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005094 * Remove @cgrp directory along with the base files. @cgrp has an
5095 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005096 */
Tejun Heo01f64742014-05-13 12:19:23 -04005097 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005098
Tejun Heod51f39b2014-05-16 13:22:48 -04005099 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005100
Tejun Heo249f3462014-05-14 09:15:01 -04005101 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005102 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005103
Tejun Heoea15f8c2013-06-13 19:27:42 -07005104 return 0;
5105};
5106
Tejun Heo2bd59d42014-02-11 11:52:49 -05005107static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005108{
Tejun Heoa9746d82014-05-13 12:19:22 -04005109 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005110 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005111
Tejun Heoa9746d82014-05-13 12:19:22 -04005112 cgrp = cgroup_kn_lock_live(kn);
5113 if (!cgrp)
5114 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005115
Tejun Heoa9746d82014-05-13 12:19:22 -04005116 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005117
Tejun Heoa9746d82014-05-13 12:19:22 -04005118 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005119 return ret;
5120}
5121
Tejun Heo2bd59d42014-02-11 11:52:49 -05005122static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5123 .remount_fs = cgroup_remount,
5124 .show_options = cgroup_show_options,
5125 .mkdir = cgroup_mkdir,
5126 .rmdir = cgroup_rmdir,
5127 .rename = cgroup_rename,
5128};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005129
Tejun Heo15a4c832014-05-04 15:09:14 -04005130static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005131{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005132 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005133
5134 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005135
Tejun Heo648bb562012-11-19 08:13:36 -08005136 mutex_lock(&cgroup_mutex);
5137
Tejun Heo15a4c832014-05-04 15:09:14 -04005138 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005139 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005140
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005141 /* Create the root cgroup state for this subsystem */
5142 ss->root = &cgrp_dfl_root;
5143 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005144 /* We don't handle early failures gracefully */
5145 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005146 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005147
5148 /*
5149 * Root csses are never destroyed and we can't initialize
5150 * percpu_ref during early init. Disable refcnting.
5151 */
5152 css->flags |= CSS_NO_REF;
5153
Tejun Heo15a4c832014-05-04 15:09:14 -04005154 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005155 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005156 css->id = 1;
5157 } else {
5158 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5159 BUG_ON(css->id < 0);
5160 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005161
Li Zefane8d55fd2008-04-29 01:00:13 -07005162 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005163 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005164 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005165 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005166 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005167
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005168 have_fork_callback |= (bool)ss->fork << ss->id;
5169 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005170 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005171 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005172
Li Zefane8d55fd2008-04-29 01:00:13 -07005173 /* At system boot, before all subsystems have been
5174 * registered, no tasks have been forked, so we don't
5175 * need to invoke fork callbacks here. */
5176 BUG_ON(!list_empty(&init_task.tasks));
5177
Tejun Heoae7f1642013-08-13 20:22:50 -04005178 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005179
Tejun Heo648bb562012-11-19 08:13:36 -08005180 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005181}
5182
5183/**
Li Zefana043e3b2008-02-23 15:24:09 -08005184 * cgroup_init_early - cgroup initialization at system boot
5185 *
5186 * Initialize cgroups at system boot, and initialize any
5187 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005188 */
5189int __init cgroup_init_early(void)
5190{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005191 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005192 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005193 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005194
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005195 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005196 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5197
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005198 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005199
Tejun Heo3ed80a62014-02-08 10:36:58 -05005200 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005201 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05005202 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5203 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005204 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005205 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5206 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005207
Tejun Heoaec25022014-02-08 10:36:58 -05005208 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005209 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005210 if (!ss->legacy_name)
5211 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005212
5213 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005214 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005215 }
5216 return 0;
5217}
5218
Tejun Heoa3e72732015-09-25 16:24:27 -04005219static unsigned long cgroup_disable_mask __initdata;
5220
Paul Menageddbcc7e2007-10-18 23:39:30 -07005221/**
Li Zefana043e3b2008-02-23 15:24:09 -08005222 * cgroup_init - cgroup initialization
5223 *
5224 * Register cgroup filesystem and /proc file, and initialize
5225 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005226 */
5227int __init cgroup_init(void)
5228{
Tejun Heo30159ec2013-06-25 11:53:37 -07005229 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005230 unsigned long key;
Tejun Heo035f4f52015-10-15 17:00:43 -04005231 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005232
Tejun Heo1ed13282015-09-16 12:53:17 -04005233 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005234 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5235 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005236
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005237 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005238
Tejun Heo82fe9b02013-06-25 11:53:37 -07005239 /* Add init_css_set to the hash table */
5240 key = css_set_hash(init_css_set.subsys);
5241 hash_add(css_set_table, &init_css_set.hlist, key);
5242
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005243 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005244
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005245 mutex_unlock(&cgroup_mutex);
5246
Tejun Heo172a2c062014-03-19 10:23:53 -04005247 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005248 if (ss->early_init) {
5249 struct cgroup_subsys_state *css =
5250 init_css_set.subsys[ss->id];
5251
5252 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5253 GFP_KERNEL);
5254 BUG_ON(css->id < 0);
5255 } else {
5256 cgroup_init_subsys(ss, false);
5257 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005258
Tejun Heo2d8f2432014-04-23 11:13:15 -04005259 list_add_tail(&init_css_set.e_cset_node[ssid],
5260 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005261
5262 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005263 * Setting dfl_root subsys_mask needs to consider the
5264 * disabled flag and cftype registration needs kmalloc,
5265 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005266 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005267 if (cgroup_disable_mask & (1 << ssid)) {
5268 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5269 printk(KERN_INFO "Disabling %s control group subsystem\n",
5270 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005271 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005272 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005273
5274 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5275
Tejun Heo5de4fa12014-07-15 11:05:10 -04005276 if (!ss->dfl_cftypes)
5277 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5278
Tejun Heoa8ddc822014-07-15 11:05:10 -04005279 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5280 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5281 } else {
5282 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5283 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005284 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005285
5286 if (ss->bind)
5287 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005288 }
Greg KH676db4a2010-08-05 13:53:35 -07005289
Tejun Heo035f4f52015-10-15 17:00:43 -04005290 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5291 WARN_ON(register_filesystem(&cgroup_fs_type));
5292 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005293
Tejun Heo2bd59d42014-02-11 11:52:49 -05005294 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005295}
Paul Menageb4f48b62007-10-18 23:39:33 -07005296
Tejun Heoe5fca242013-11-22 17:14:39 -05005297static int __init cgroup_wq_init(void)
5298{
5299 /*
5300 * There isn't much point in executing destruction path in
5301 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005302 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005303 *
5304 * We would prefer to do this in cgroup_init() above, but that
5305 * is called before init_workqueues(): so leave this until after.
5306 */
Tejun Heo1a115332014-02-12 19:06:19 -05005307 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005308 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005309
5310 /*
5311 * Used to destroy pidlists and separate to serve as flush domain.
5312 * Cap @max_active to 1 too.
5313 */
5314 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5315 0, 1);
5316 BUG_ON(!cgroup_pidlist_destroy_wq);
5317
Tejun Heoe5fca242013-11-22 17:14:39 -05005318 return 0;
5319}
5320core_initcall(cgroup_wq_init);
5321
Paul Menagea4243162007-10-18 23:39:35 -07005322/*
5323 * proc_cgroup_show()
5324 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5325 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005326 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005327int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5328 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005329{
Tejun Heoe61734c2014-02-12 09:29:50 -05005330 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005331 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005332 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005333
5334 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005335 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005336 if (!buf)
5337 goto out;
5338
Paul Menagea4243162007-10-18 23:39:35 -07005339 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005340 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005341
Tejun Heo985ed672014-03-19 10:23:53 -04005342 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005343 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005344 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005345 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005346
Tejun Heoa2dd4242014-03-19 10:23:55 -04005347 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005348 continue;
5349
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005350 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005351 if (root != &cgrp_dfl_root)
5352 for_each_subsys(ss, ssid)
5353 if (root->subsys_mask & (1 << ssid))
5354 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005355 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005356 if (strlen(root->name))
5357 seq_printf(m, "%sname=%s", count ? "," : "",
5358 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005359 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005360
Paul Menage7717f7b2009-09-23 15:56:22 -07005361 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005362
5363 /*
5364 * On traditional hierarchies, all zombie tasks show up as
5365 * belonging to the root cgroup. On the default hierarchy,
5366 * while a zombie doesn't show up in "cgroup.procs" and
5367 * thus can't be migrated, its /proc/PID/cgroup keeps
5368 * reporting the cgroup it belonged to before exiting. If
5369 * the cgroup is removed before the zombie is reaped,
5370 * " (deleted)" is appended to the cgroup path.
5371 */
5372 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5373 path = cgroup_path(cgrp, buf, PATH_MAX);
5374 if (!path) {
5375 retval = -ENAMETOOLONG;
5376 goto out_unlock;
5377 }
5378 } else {
5379 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005380 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005381
Tejun Heoe61734c2014-02-12 09:29:50 -05005382 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005383
5384 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5385 seq_puts(m, " (deleted)\n");
5386 else
5387 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005388 }
5389
Zefan Li006f4ac2014-09-18 16:03:15 +08005390 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005391out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005392 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005393 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005394 kfree(buf);
5395out:
5396 return retval;
5397}
5398
Paul Menagea4243162007-10-18 23:39:35 -07005399/* Display information about each subsystem and each hierarchy */
5400static int proc_cgroupstats_show(struct seq_file *m, void *v)
5401{
Tejun Heo30159ec2013-06-25 11:53:37 -07005402 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005403 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005404
Paul Menage8bab8dd2008-04-04 14:29:57 -07005405 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005406 /*
5407 * ideally we don't want subsystems moving around while we do this.
5408 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5409 * subsys/hierarchy state.
5410 */
Paul Menagea4243162007-10-18 23:39:35 -07005411 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005412
5413 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005414 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005415 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005416 atomic_read(&ss->root->nr_cgrps),
5417 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005418
Paul Menagea4243162007-10-18 23:39:35 -07005419 mutex_unlock(&cgroup_mutex);
5420 return 0;
5421}
5422
5423static int cgroupstats_open(struct inode *inode, struct file *file)
5424{
Al Viro9dce07f2008-03-29 03:07:28 +00005425 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005426}
5427
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005428static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005429 .open = cgroupstats_open,
5430 .read = seq_read,
5431 .llseek = seq_lseek,
5432 .release = single_release,
5433};
5434
Aleksa Sarai7e476822015-06-09 21:32:09 +10005435static void **subsys_canfork_priv_p(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5436{
5437 if (CGROUP_CANFORK_START <= i && i < CGROUP_CANFORK_END)
5438 return &ss_priv[i - CGROUP_CANFORK_START];
5439 return NULL;
5440}
5441
5442static void *subsys_canfork_priv(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5443{
5444 void **private = subsys_canfork_priv_p(ss_priv, i);
5445 return private ? *private : NULL;
5446}
5447
Paul Menageb4f48b62007-10-18 23:39:33 -07005448/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005449 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005450 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005451 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005452 * A task is associated with the init_css_set until cgroup_post_fork()
5453 * attaches it to the parent's css_set. Empty cg_list indicates that
5454 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005455 */
5456void cgroup_fork(struct task_struct *child)
5457{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005458 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005459 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005460}
5461
5462/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005463 * cgroup_can_fork - called on a new task before the process is exposed
5464 * @child: the task in question.
5465 *
5466 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5467 * returns an error, the fork aborts with that error code. This allows for
5468 * a cgroup subsystem to conditionally allow or deny new forks.
5469 */
5470int cgroup_can_fork(struct task_struct *child,
5471 void *ss_priv[CGROUP_CANFORK_COUNT])
5472{
5473 struct cgroup_subsys *ss;
5474 int i, j, ret;
5475
5476 for_each_subsys_which(ss, i, &have_canfork_callback) {
5477 ret = ss->can_fork(child, subsys_canfork_priv_p(ss_priv, i));
5478 if (ret)
5479 goto out_revert;
5480 }
5481
5482 return 0;
5483
5484out_revert:
5485 for_each_subsys(ss, j) {
5486 if (j >= i)
5487 break;
5488 if (ss->cancel_fork)
5489 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, j));
5490 }
5491
5492 return ret;
5493}
5494
5495/**
5496 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5497 * @child: the task in question
5498 *
5499 * This calls the cancel_fork() callbacks if a fork failed *after*
5500 * cgroup_can_fork() succeded.
5501 */
5502void cgroup_cancel_fork(struct task_struct *child,
5503 void *ss_priv[CGROUP_CANFORK_COUNT])
5504{
5505 struct cgroup_subsys *ss;
5506 int i;
5507
5508 for_each_subsys(ss, i)
5509 if (ss->cancel_fork)
5510 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, i));
5511}
5512
5513/**
Li Zefana043e3b2008-02-23 15:24:09 -08005514 * cgroup_post_fork - called on a new task after adding it to the task list
5515 * @child: the task in question
5516 *
Tejun Heo5edee612012-10-16 15:03:14 -07005517 * Adds the task to the list running through its css_set if necessary and
5518 * call the subsystem fork() callbacks. Has to be after the task is
5519 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005520 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005521 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005522 */
Aleksa Sarai7e476822015-06-09 21:32:09 +10005523void cgroup_post_fork(struct task_struct *child,
5524 void *old_ss_priv[CGROUP_CANFORK_COUNT])
Paul Menage817929e2007-10-18 23:39:36 -07005525{
Tejun Heo30159ec2013-06-25 11:53:37 -07005526 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005527 int i;
5528
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005529 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005530 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005531 * function sets use_task_css_set_links before grabbing
5532 * tasklist_lock and we just went through tasklist_lock to add
5533 * @child, it's guaranteed that either we see the set
5534 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5535 * @child during its iteration.
5536 *
5537 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005538 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005539 * association is stable, and, on completion of the parent's
5540 * migration, @child is visible in the source of migration or
5541 * already in the destination cgroup. This guarantee is necessary
5542 * when implementing operations which need to migrate all tasks of
5543 * a cgroup to another.
5544 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005545 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005546 * will remain in init_css_set. This is safe because all tasks are
5547 * in the init_css_set before cg_links is enabled and there's no
5548 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005549 */
Paul Menage817929e2007-10-18 23:39:36 -07005550 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005551 struct css_set *cset;
5552
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005553 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005554 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005555 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005556 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005557 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005558 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005559 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005560 }
Tejun Heo5edee612012-10-16 15:03:14 -07005561
5562 /*
5563 * Call ss->fork(). This must happen after @child is linked on
5564 * css_set; otherwise, @child might change state between ->fork()
5565 * and addition to css_set.
5566 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005567 for_each_subsys_which(ss, i, &have_fork_callback)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005568 ss->fork(child, subsys_canfork_priv(old_ss_priv, i));
Paul Menage817929e2007-10-18 23:39:36 -07005569}
Tejun Heo5edee612012-10-16 15:03:14 -07005570
Paul Menage817929e2007-10-18 23:39:36 -07005571/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005572 * cgroup_exit - detach cgroup from exiting task
5573 * @tsk: pointer to task_struct of exiting process
5574 *
5575 * Description: Detach cgroup from @tsk and release it.
5576 *
5577 * Note that cgroups marked notify_on_release force every task in
5578 * them to take the global cgroup_mutex mutex when exiting.
5579 * This could impact scaling on very large systems. Be reluctant to
5580 * use notify_on_release cgroups where very high task exit scaling
5581 * is required on large systems.
5582 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005583 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5584 * call cgroup_exit() while the task is still competent to handle
5585 * notify_on_release(), then leave the task attached to the root cgroup in
5586 * each hierarchy for the remainder of its exit. No need to bother with
5587 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005588 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005589 */
Li Zefan1ec41832014-03-28 15:22:19 +08005590void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005591{
Tejun Heo30159ec2013-06-25 11:53:37 -07005592 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005593 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005594 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005595
5596 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005597 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005598 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005599 */
Tejun Heo0de09422015-10-15 16:41:49 -04005600 cset = task_css_set(tsk);
5601
Paul Menage817929e2007-10-18 23:39:36 -07005602 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005603 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005604 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005605 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005606 } else {
5607 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005608 }
5609
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005610 /* see cgroup_post_fork() for details */
Tejun Heo2e91fa72015-10-15 16:41:53 -04005611 for_each_subsys_which(ss, i, &have_exit_callback)
5612 ss->exit(tsk);
5613}
Tejun Heo30159ec2013-06-25 11:53:37 -07005614
Tejun Heo2e91fa72015-10-15 16:41:53 -04005615void cgroup_free(struct task_struct *task)
5616{
5617 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005618 struct cgroup_subsys *ss;
5619 int ssid;
5620
5621 for_each_subsys_which(ss, ssid, &have_free_callback)
5622 ss->free(task);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005623
Tejun Heo2e91fa72015-10-15 16:41:53 -04005624 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005625}
Paul Menage697f4162007-10-18 23:39:34 -07005626
Paul Menagebd89aab2007-10-18 23:40:44 -07005627static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005628{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005629 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005630 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5631 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005632}
5633
Paul Menage81a6a5c2007-10-18 23:39:38 -07005634/*
5635 * Notify userspace when a cgroup is released, by running the
5636 * configured release agent with the name of the cgroup (path
5637 * relative to the root of cgroup file system) as the argument.
5638 *
5639 * Most likely, this user command will try to rmdir this cgroup.
5640 *
5641 * This races with the possibility that some other task will be
5642 * attached to this cgroup before it is removed, or that some other
5643 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5644 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5645 * unused, and this cgroup will be reprieved from its death sentence,
5646 * to continue to serve a useful existence. Next time it's released,
5647 * we will get notified again, if it still has 'notify_on_release' set.
5648 *
5649 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5650 * means only wait until the task is successfully execve()'d. The
5651 * separate release agent task is forked by call_usermodehelper(),
5652 * then control in this thread returns here, without waiting for the
5653 * release agent task. We don't bother to wait because the caller of
5654 * this routine has no use for the exit status of the release agent
5655 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005656 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005657static void cgroup_release_agent(struct work_struct *work)
5658{
Zefan Li971ff492014-09-18 16:06:19 +08005659 struct cgroup *cgrp =
5660 container_of(work, struct cgroup, release_agent_work);
5661 char *pathbuf = NULL, *agentbuf = NULL, *path;
5662 char *argv[3], *envp[3];
5663
Paul Menage81a6a5c2007-10-18 23:39:38 -07005664 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005665
Zefan Li971ff492014-09-18 16:06:19 +08005666 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5667 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5668 if (!pathbuf || !agentbuf)
5669 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005670
Zefan Li971ff492014-09-18 16:06:19 +08005671 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5672 if (!path)
5673 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005674
Zefan Li971ff492014-09-18 16:06:19 +08005675 argv[0] = agentbuf;
5676 argv[1] = path;
5677 argv[2] = NULL;
5678
5679 /* minimal command environment */
5680 envp[0] = "HOME=/";
5681 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5682 envp[2] = NULL;
5683
Paul Menage81a6a5c2007-10-18 23:39:38 -07005684 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005685 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005686 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005687out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005688 mutex_unlock(&cgroup_mutex);
5689out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005690 kfree(agentbuf);
5691 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005692}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005693
5694static int __init cgroup_disable(char *str)
5695{
Tejun Heo30159ec2013-06-25 11:53:37 -07005696 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005697 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005698 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005699
5700 while ((token = strsep(&str, ",")) != NULL) {
5701 if (!*token)
5702 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005703
Tejun Heo3ed80a62014-02-08 10:36:58 -05005704 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005705 if (strcmp(token, ss->name) &&
5706 strcmp(token, ss->legacy_name))
5707 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005708 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005709 }
5710 }
5711 return 1;
5712}
5713__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005714
Tejun Heob77d7b62013-08-13 11:01:54 -04005715/**
Tejun Heoec903c02014-05-13 12:11:01 -04005716 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005717 * @dentry: directory dentry of interest
5718 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005719 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005720 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5721 * to get the corresponding css and return it. If such css doesn't exist
5722 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005723 */
Tejun Heoec903c02014-05-13 12:11:01 -04005724struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5725 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005726{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005727 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5728 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005729 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005730
Tejun Heo35cf0832013-08-26 18:40:56 -04005731 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005732 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5733 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005734 return ERR_PTR(-EBADF);
5735
Tejun Heo5a17f542014-02-11 11:52:47 -05005736 rcu_read_lock();
5737
Tejun Heo2bd59d42014-02-11 11:52:49 -05005738 /*
5739 * This path doesn't originate from kernfs and @kn could already
5740 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005741 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005742 */
5743 cgrp = rcu_dereference(kn->priv);
5744 if (cgrp)
5745 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005746
Tejun Heoec903c02014-05-13 12:11:01 -04005747 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005748 css = ERR_PTR(-ENOENT);
5749
5750 rcu_read_unlock();
5751 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005752}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005753
Li Zefan1cb650b2013-08-19 10:05:24 +08005754/**
5755 * css_from_id - lookup css by id
5756 * @id: the cgroup id
5757 * @ss: cgroup subsys to be looked into
5758 *
5759 * Returns the css if there's valid one with @id, otherwise returns NULL.
5760 * Should be called under rcu_read_lock().
5761 */
5762struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5763{
Tejun Heo6fa49182014-05-04 15:09:13 -04005764 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005765 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005766}
5767
Paul Menagefe693432009-09-23 15:56:20 -07005768#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005769static struct cgroup_subsys_state *
5770debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005771{
5772 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5773
5774 if (!css)
5775 return ERR_PTR(-ENOMEM);
5776
5777 return css;
5778}
5779
Tejun Heoeb954192013-08-08 20:11:23 -04005780static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005781{
Tejun Heoeb954192013-08-08 20:11:23 -04005782 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005783}
5784
Tejun Heo182446d2013-08-08 20:11:24 -04005785static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5786 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005787{
Tejun Heo182446d2013-08-08 20:11:24 -04005788 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005789}
5790
Tejun Heo182446d2013-08-08 20:11:24 -04005791static u64 current_css_set_read(struct cgroup_subsys_state *css,
5792 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005793{
5794 return (u64)(unsigned long)current->cgroups;
5795}
5796
Tejun Heo182446d2013-08-08 20:11:24 -04005797static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005798 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005799{
5800 u64 count;
5801
5802 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005803 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005804 rcu_read_unlock();
5805 return count;
5806}
5807
Tejun Heo2da8ca82013-12-05 12:28:04 -05005808static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005809{
Tejun Heo69d02062013-06-12 21:04:50 -07005810 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005811 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005812 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005813
Tejun Heoe61734c2014-02-12 09:29:50 -05005814 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5815 if (!name_buf)
5816 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005817
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005818 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005819 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005820 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005821 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005822 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005823
Tejun Heoa2dd4242014-03-19 10:23:55 -04005824 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005825 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005826 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005827 }
5828 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005829 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005830 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005831 return 0;
5832}
5833
5834#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005835static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005836{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005837 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005838 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005839
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005840 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005841 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005842 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005843 struct task_struct *task;
5844 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005845
Tejun Heo5abb8852013-06-12 21:04:49 -07005846 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005847
Tejun Heo5abb8852013-06-12 21:04:49 -07005848 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005849 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5850 goto overflow;
5851 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005852 }
Tejun Heoc7561122014-02-25 10:04:01 -05005853
5854 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5855 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5856 goto overflow;
5857 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5858 }
5859 continue;
5860 overflow:
5861 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005862 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005863 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07005864 return 0;
5865}
5866
Tejun Heo182446d2013-08-08 20:11:24 -04005867static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005868{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005869 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08005870 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005871}
5872
5873static struct cftype debug_files[] = {
5874 {
Paul Menagefe693432009-09-23 15:56:20 -07005875 .name = "taskcount",
5876 .read_u64 = debug_taskcount_read,
5877 },
5878
5879 {
5880 .name = "current_css_set",
5881 .read_u64 = current_css_set_read,
5882 },
5883
5884 {
5885 .name = "current_css_set_refcount",
5886 .read_u64 = current_css_set_refcount_read,
5887 },
5888
5889 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005890 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005891 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005892 },
5893
5894 {
5895 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005896 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005897 },
5898
5899 {
Paul Menagefe693432009-09-23 15:56:20 -07005900 .name = "releasable",
5901 .read_u64 = releasable_read,
5902 },
Paul Menagefe693432009-09-23 15:56:20 -07005903
Tejun Heo4baf6e32012-04-01 12:09:55 -07005904 { } /* terminate */
5905};
Paul Menagefe693432009-09-23 15:56:20 -07005906
Tejun Heo073219e2014-02-08 10:36:58 -05005907struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005908 .css_alloc = debug_css_alloc,
5909 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005910 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005911};
5912#endif /* CONFIG_CGROUP_DEBUG */