blob: d295e6a91cdc55fd5a2db7454eda921e051ebbfc [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mm.h>
40#include <linux/mutex.h>
41#include <linux/mount.h>
42#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070043#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/rcupdate.h>
45#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040048#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050059#include <linux/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Tejun Heoe93ad192016-01-19 12:18:41 -050061#include <linux/cpuset.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050062#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070063
Tejun Heoe25e2cb2011-12-12 18:12:21 -080064/*
Tejun Heob1a21362013-11-29 10:42:58 -050065 * pidlists linger the following amount before being destroyed. The goal
66 * is avoiding frequent destruction in the middle of consecutive read calls
67 * Expiring in the middle is a performance problem not a correctness one.
68 * 1 sec should be enough.
69 */
70#define CGROUP_PIDLIST_DESTROY_DELAY HZ
71
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050072#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
73 MAX_CFTYPE_NAME + 2)
74
Tejun Heob1a21362013-11-29 10:42:58 -050075/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080076 * cgroup_mutex is the master lock. Any modification to cgroup or its
77 * hierarchy must be performed while holding it.
78 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040079 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050080 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080081 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050082 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
83 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080084 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040087DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -050088EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040089EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070090#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070091static DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040092static DEFINE_SPINLOCK(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070093#endif
94
Tejun Heo69e943b2014-02-08 10:36:58 -050095/*
Tejun Heo15a4c832014-05-04 15:09:14 -040096 * Protects cgroup_idr and css_idr so that IDs can be released without
97 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040098 */
99static DEFINE_SPINLOCK(cgroup_idr_lock);
100
101/*
Tejun Heo34c06252015-11-05 00:12:24 -0500102 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
103 * against file removal/re-creation across css hiding.
104 */
105static DEFINE_SPINLOCK(cgroup_file_kn_lock);
106
107/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500108 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
109 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
110 */
111static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700112
Tejun Heo1ed13282015-09-16 12:53:17 -0400113struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
114
Tejun Heo8353da12014-05-13 12:19:23 -0400115#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700116 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
117 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400118 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500119
Ben Blumaae8aab2010-03-10 15:22:07 -0800120/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500121 * cgroup destruction makes heavy use of work items and there can be a lot
122 * of concurrent destructions. Use a separate workqueue so that cgroup
123 * destruction work items don't end up filling up max_active of system_wq
124 * which may lead to deadlock.
125 */
126static struct workqueue_struct *cgroup_destroy_wq;
127
128/*
Tejun Heob1a21362013-11-29 10:42:58 -0500129 * pidlist destructions need to be flushed on cgroup destruction. Use a
130 * separate workqueue as flush domain.
131 */
132static struct workqueue_struct *cgroup_pidlist_destroy_wq;
133
Tejun Heo3ed80a62014-02-08 10:36:58 -0500134/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500135#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500136static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137#include <linux/cgroup_subsys.h>
138};
Tejun Heo073219e2014-02-08 10:36:58 -0500139#undef SUBSYS
140
141/* array of cgroup subsystem names */
142#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
143static const char *cgroup_subsys_name[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Tejun Heo49d1dc42015-09-18 11:56:28 -0400148/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
149#define SUBSYS(_x) \
150 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
151 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
152 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
153 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
154#include <linux/cgroup_subsys.h>
155#undef SUBSYS
156
157#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
158static struct static_key_true *cgroup_subsys_enabled_key[] = {
159#include <linux/cgroup_subsys.h>
160};
161#undef SUBSYS
162
163#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
164static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
165#include <linux/cgroup_subsys.h>
166};
167#undef SUBSYS
168
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400170 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700171 * unattached - it never has more than a single cgroup, and all tasks are
172 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400174struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400175EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700176
Tejun Heoa2dd4242014-03-19 10:23:55 -0400177/*
178 * The default hierarchy always exists but is hidden until mounted for the
179 * first time. This is for backward compatibility.
180 */
Tejun Heoa7165262016-02-23 10:00:50 -0500181static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700182
Johannes Weiner223ffb22016-02-11 13:34:49 -0500183/* Controllers blocked by the commandline in v1 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500184static u16 cgroup_no_v1_mask;
Johannes Weiner223ffb22016-02-11 13:34:49 -0500185
Tejun Heo5533e012014-05-14 19:33:07 -0400186/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500187static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400188
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189/* The list of hierarchy roots */
190
Tejun Heo9871bf92013-06-24 15:21:47 -0700191static LIST_HEAD(cgroup_roots);
192static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700193
Tejun Heo3417ae12014-02-08 10:37:01 -0500194/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700195static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700196
Li Zefan794611a2013-06-18 18:53:53 +0800197/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400198 * Assign a monotonically increasing serial number to csses. It guarantees
199 * cgroups with bigger numbers are newer than those with smaller numbers.
200 * Also, as csses are always appended to the parent's ->children list, it
201 * guarantees that sibling csses are always sorted in the ascending serial
202 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800203 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400204static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800205
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000206/*
207 * These bitmask flags indicate whether tasks in the fork and exit paths have
208 * fork/exit handlers to call. This avoids us having to do extra work in the
209 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500211static u16 have_fork_callback __read_mostly;
212static u16 have_exit_callback __read_mostly;
213static u16 have_free_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700214
Aleksa Sarai7e476822015-06-09 21:32:09 +1000215/* Ditto for the can_fork callback. */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500216static u16 have_canfork_callback __read_mostly;
Aleksa Sarai7e476822015-06-09 21:32:09 +1000217
Tejun Heo67e9c742015-11-16 11:13:34 -0500218static struct file_system_type cgroup2_fs_type;
Tejun Heoa14c6872014-07-15 11:05:09 -0400219static struct cftype cgroup_dfl_base_files[];
220static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700221
Tejun Heo6e5c8302016-02-22 22:25:47 -0500222static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
Tejun Heoed27b9f2015-10-15 16:41:52 -0400223static void css_task_iter_advance(struct css_task_iter *it);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500225static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
226 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400227static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400228static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400229static int cgroup_addrm_files(struct cgroup_subsys_state *css,
230 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400231 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800232
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400233/**
234 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
235 * @ssid: subsys ID of interest
236 *
237 * cgroup_subsys_enabled() can only be used with literal subsys names which
238 * is fine for individual subsystems but unsuitable for cgroup core. This
239 * is slower static_key_enabled() based test indexed by @ssid.
240 */
241static bool cgroup_ssid_enabled(int ssid)
242{
243 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
244}
245
Johannes Weiner223ffb22016-02-11 13:34:49 -0500246static bool cgroup_ssid_no_v1(int ssid)
247{
248 return cgroup_no_v1_mask & (1 << ssid);
249}
250
Tejun Heo9e10a132015-09-18 11:56:28 -0400251/**
252 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
253 * @cgrp: the cgroup of interest
254 *
255 * The default hierarchy is the v2 interface of cgroup and this function
256 * can be used to test whether a cgroup is on the default hierarchy for
257 * cases where a subsystem should behave differnetly depending on the
258 * interface version.
259 *
260 * The set of behaviors which change on the default hierarchy are still
261 * being determined and the mount option is prefixed with __DEVEL__.
262 *
263 * List of changed behaviors:
264 *
265 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
266 * and "name" are disallowed.
267 *
268 * - When mounting an existing superblock, mount options should match.
269 *
270 * - Remount is disallowed.
271 *
272 * - rename(2) is disallowed.
273 *
274 * - "tasks" is removed. Everything should be at process granularity. Use
275 * "cgroup.procs" instead.
276 *
277 * - "cgroup.procs" is not sorted. pids will be unique unless they got
278 * recycled inbetween reads.
279 *
280 * - "release_agent" and "notify_on_release" are removed. Replacement
281 * notification mechanism will be implemented.
282 *
283 * - "cgroup.clone_children" is removed.
284 *
285 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
286 * and its descendants contain no task; otherwise, 1. The file also
287 * generates kernfs notification which can be monitored through poll and
288 * [di]notify when the value of the file changes.
289 *
290 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
291 * take masks of ancestors with non-empty cpus/mems, instead of being
292 * moved to an ancestor.
293 *
294 * - cpuset: a task can be moved into an empty cpuset, and again it takes
295 * masks of ancestors.
296 *
297 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
298 * is not created.
299 *
300 * - blkcg: blk-throttle becomes properly hierarchical.
301 *
302 * - debug: disallowed on the default hierarchy.
303 */
304static bool cgroup_on_dfl(const struct cgroup *cgrp)
305{
306 return cgrp->root == &cgrp_dfl_root;
307}
308
Tejun Heo6fa49182014-05-04 15:09:13 -0400309/* IDR wrappers which synchronize using cgroup_idr_lock */
310static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
311 gfp_t gfp_mask)
312{
313 int ret;
314
315 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400316 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800317 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400318 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400319 idr_preload_end();
320 return ret;
321}
322
323static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
324{
325 void *ret;
326
Tejun Heo54504e92014-05-13 12:10:59 -0400327 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400328 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400329 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400330 return ret;
331}
332
333static void cgroup_idr_remove(struct idr *idr, int id)
334{
Tejun Heo54504e92014-05-13 12:10:59 -0400335 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400336 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400337 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400338}
339
Tejun Heod51f39b2014-05-16 13:22:48 -0400340static struct cgroup *cgroup_parent(struct cgroup *cgrp)
341{
342 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
343
344 if (parent_css)
345 return container_of(parent_css, struct cgroup, self);
346 return NULL;
347}
348
Tejun Heo5531dc92016-03-03 09:57:58 -0500349/* subsystems visibly enabled on a cgroup */
350static u16 cgroup_control(struct cgroup *cgrp)
351{
352 struct cgroup *parent = cgroup_parent(cgrp);
353 u16 root_ss_mask = cgrp->root->subsys_mask;
354
355 if (parent)
356 return parent->subtree_control;
357
358 if (cgroup_on_dfl(cgrp))
359 root_ss_mask &= ~cgrp_dfl_inhibit_ss_mask;
360
361 return root_ss_mask;
362}
363
364/* subsystems enabled on a cgroup */
365static u16 cgroup_ss_mask(struct cgroup *cgrp)
366{
367 struct cgroup *parent = cgroup_parent(cgrp);
368
369 if (parent)
370 return parent->subtree_ss_mask;
371
372 return cgrp->root->subsys_mask;
373}
374
Tejun Heo95109b62013-08-08 20:11:27 -0400375/**
376 * cgroup_css - obtain a cgroup's css for the specified subsystem
377 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400378 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400379 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400380 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
381 * function must be called either under cgroup_mutex or rcu_read_lock() and
382 * the caller is responsible for pinning the returned css if it wants to
383 * keep accessing it outside the said locks. This function may return
384 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400385 */
386static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400387 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400388{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400389 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500390 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500391 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400392 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400393 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400394}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700395
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400396/**
397 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
398 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400399 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400400 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400401 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400402 * as the matching css of the nearest ancestor including self which has @ss
403 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
404 * function is guaranteed to return non-NULL css.
405 */
406static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
407 struct cgroup_subsys *ss)
408{
409 lockdep_assert_held(&cgroup_mutex);
410
411 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400412 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400413
Tejun Heoeeecbd12014-11-18 02:49:52 -0500414 /*
415 * This function is used while updating css associations and thus
Tejun Heo5531dc92016-03-03 09:57:58 -0500416 * can't test the csses directly. Test ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500417 */
Tejun Heo5531dc92016-03-03 09:57:58 -0500418 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
Tejun Heod51f39b2014-05-16 13:22:48 -0400419 cgrp = cgroup_parent(cgrp);
Tejun Heo5531dc92016-03-03 09:57:58 -0500420 if (!cgrp)
421 return NULL;
422 }
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400423
424 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700425}
426
Tejun Heoeeecbd12014-11-18 02:49:52 -0500427/**
428 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
429 * @cgrp: the cgroup of interest
430 * @ss: the subsystem of interest
431 *
432 * Find and get the effective css of @cgrp for @ss. The effective css is
433 * defined as the matching css of the nearest ancestor including self which
434 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
435 * the root css is returned, so this function always returns a valid css.
436 * The returned css must be put using css_put().
437 */
438struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
439 struct cgroup_subsys *ss)
440{
441 struct cgroup_subsys_state *css;
442
443 rcu_read_lock();
444
445 do {
446 css = cgroup_css(cgrp, ss);
447
448 if (css && css_tryget_online(css))
449 goto out_unlock;
450 cgrp = cgroup_parent(cgrp);
451 } while (cgrp);
452
453 css = init_css_set.subsys[ss->id];
454 css_get(css);
455out_unlock:
456 rcu_read_unlock();
457 return css;
458}
459
Paul Menageddbcc7e2007-10-18 23:39:30 -0700460/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700461static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700462{
Tejun Heo184faf32014-05-16 13:22:51 -0400463 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700464}
465
Tejun Heo052c3f32015-10-15 16:41:50 -0400466static void cgroup_get(struct cgroup *cgrp)
467{
468 WARN_ON_ONCE(cgroup_is_dead(cgrp));
469 css_get(&cgrp->self);
470}
471
472static bool cgroup_tryget(struct cgroup *cgrp)
473{
474 return css_tryget(&cgrp->self);
475}
476
Tejun Heob4168642014-05-13 12:16:21 -0400477struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500478{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500479 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400480 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500481
482 /*
483 * This is open and unprotected implementation of cgroup_css().
484 * seq_css() is only called from a kernfs file operation which has
485 * an active reference on the file. Because all the subsystem
486 * files are drained before a css is disassociated with a cgroup,
487 * the matching css from the cgroup's subsys table is guaranteed to
488 * be and stay valid until the enclosing operation is complete.
489 */
490 if (cft->ss)
491 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
492 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400493 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500494}
Tejun Heob4168642014-05-13 12:16:21 -0400495EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500496
Adrian Bunke9685a02008-02-07 00:13:46 -0800497static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700498{
Paul Menagebd89aab2007-10-18 23:40:44 -0700499 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700500}
501
Tejun Heo30159ec2013-06-25 11:53:37 -0700502/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500503 * for_each_css - iterate all css's of a cgroup
504 * @css: the iteration cursor
505 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
506 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700507 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400508 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700509 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500510#define for_each_css(css, ssid, cgrp) \
511 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
512 if (!((css) = rcu_dereference_check( \
513 (cgrp)->subsys[(ssid)], \
514 lockdep_is_held(&cgroup_mutex)))) { } \
515 else
516
517/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400518 * for_each_e_css - iterate all effective css's of a cgroup
519 * @css: the iteration cursor
520 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
521 * @cgrp: the target cgroup to iterate css's of
522 *
523 * Should be called under cgroup_[tree_]mutex.
524 */
525#define for_each_e_css(css, ssid, cgrp) \
526 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
527 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
528 ; \
529 else
530
531/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500532 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700533 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500534 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700535 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500536#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500537 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
538 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700539
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000540/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500541 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000542 * @ss: the iteration cursor
543 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500544 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000545 *
546 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500547 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000548 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500549#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
550 unsigned long __ss_mask = (ss_mask); \
551 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000552 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500553 break; \
554 } \
555 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
556 (ss) = cgroup_subsys[ssid]; \
557 {
558
559#define while_each_subsys_mask() \
560 } \
561 } \
562} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000563
Tejun Heo985ed672014-03-19 10:23:53 -0400564/* iterate across the hierarchies */
565#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700566 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700567
Tejun Heof8f22e52014-04-23 11:13:16 -0400568/* iterate over child cgrps, lock should be held throughout iteration */
569#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400570 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400571 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400572 cgroup_is_dead(child); })) \
573 ; \
574 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700575
Paul Menage81a6a5c2007-10-18 23:39:38 -0700576static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700577static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700578
Tejun Heo69d02062013-06-12 21:04:50 -0700579/*
580 * A cgroup can be associated with multiple css_sets as different tasks may
581 * belong to different cgroups on different hierarchies. In the other
582 * direction, a css_set is naturally associated with multiple cgroups.
583 * This M:N relationship is represented by the following link structure
584 * which exists for each association and allows traversing the associations
585 * from both sides.
586 */
587struct cgrp_cset_link {
588 /* the cgroup and css_set this link associates */
589 struct cgroup *cgrp;
590 struct css_set *cset;
591
592 /* list of cgrp_cset_links anchored at cgrp->cset_links */
593 struct list_head cset_link;
594
595 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
596 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700597};
598
Tejun Heo172a2c062014-03-19 10:23:53 -0400599/*
600 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700601 * hierarchies being mounted. It contains a pointer to the root state
602 * for each subsystem. Also used to anchor the list of css_sets. Not
603 * reference-counted, to improve performance when child cgroups
604 * haven't been created.
605 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400606struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400607 .refcount = ATOMIC_INIT(1),
608 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
609 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
610 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
611 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
612 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400613 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400614};
Paul Menage817929e2007-10-18 23:39:36 -0700615
Tejun Heo172a2c062014-03-19 10:23:53 -0400616static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700617
Tejun Heo842b5972014-04-25 18:28:02 -0400618/**
Tejun Heo0de09422015-10-15 16:41:49 -0400619 * css_set_populated - does a css_set contain any tasks?
620 * @cset: target css_set
621 */
622static bool css_set_populated(struct css_set *cset)
623{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400624 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400625
626 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
627}
628
629/**
Tejun Heo842b5972014-04-25 18:28:02 -0400630 * cgroup_update_populated - updated populated count of a cgroup
631 * @cgrp: the target cgroup
632 * @populated: inc or dec populated count
633 *
Tejun Heo0de09422015-10-15 16:41:49 -0400634 * One of the css_sets associated with @cgrp is either getting its first
635 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
636 * count is propagated towards root so that a given cgroup's populated_cnt
637 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400638 *
639 * @cgrp's interface file "cgroup.populated" is zero if
640 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
641 * changes from or to zero, userland is notified that the content of the
642 * interface file has changed. This can be used to detect when @cgrp and
643 * its descendants become populated or empty.
644 */
645static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
646{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400647 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400648
649 do {
650 bool trigger;
651
652 if (populated)
653 trigger = !cgrp->populated_cnt++;
654 else
655 trigger = !--cgrp->populated_cnt;
656
657 if (!trigger)
658 break;
659
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400660 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400661 cgroup_file_notify(&cgrp->events_file);
662
Tejun Heod51f39b2014-05-16 13:22:48 -0400663 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400664 } while (cgrp);
665}
666
Tejun Heo0de09422015-10-15 16:41:49 -0400667/**
668 * css_set_update_populated - update populated state of a css_set
669 * @cset: target css_set
670 * @populated: whether @cset is populated or depopulated
671 *
672 * @cset is either getting the first task or losing the last. Update the
673 * ->populated_cnt of all associated cgroups accordingly.
674 */
675static void css_set_update_populated(struct css_set *cset, bool populated)
676{
677 struct cgrp_cset_link *link;
678
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400679 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400680
681 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
682 cgroup_update_populated(link->cgrp, populated);
683}
684
Tejun Heof6d7d042015-10-15 16:41:52 -0400685/**
686 * css_set_move_task - move a task from one css_set to another
687 * @task: task being moved
688 * @from_cset: css_set @task currently belongs to (may be NULL)
689 * @to_cset: new css_set @task is being moved to (may be NULL)
690 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
691 *
692 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
693 * css_set, @from_cset can be NULL. If @task is being disassociated
694 * instead of moved, @to_cset can be NULL.
695 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400696 * This function automatically handles populated_cnt updates and
697 * css_task_iter adjustments but the caller is responsible for managing
698 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400699 */
700static void css_set_move_task(struct task_struct *task,
701 struct css_set *from_cset, struct css_set *to_cset,
702 bool use_mg_tasks)
703{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400704 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400705
Tejun Heo20b454a2016-03-03 09:57:57 -0500706 if (to_cset && !css_set_populated(to_cset))
707 css_set_update_populated(to_cset, true);
708
Tejun Heof6d7d042015-10-15 16:41:52 -0400709 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400710 struct css_task_iter *it, *pos;
711
Tejun Heof6d7d042015-10-15 16:41:52 -0400712 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400713
714 /*
715 * @task is leaving, advance task iterators which are
716 * pointing to it so that they can resume at the next
717 * position. Advancing an iterator might remove it from
718 * the list, use safe walk. See css_task_iter_advance*()
719 * for details.
720 */
721 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
722 iters_node)
723 if (it->task_pos == &task->cg_list)
724 css_task_iter_advance(it);
725
Tejun Heof6d7d042015-10-15 16:41:52 -0400726 list_del_init(&task->cg_list);
727 if (!css_set_populated(from_cset))
728 css_set_update_populated(from_cset, false);
729 } else {
730 WARN_ON_ONCE(!list_empty(&task->cg_list));
731 }
732
733 if (to_cset) {
734 /*
735 * We are synchronized through cgroup_threadgroup_rwsem
736 * against PF_EXITING setting such that we can't race
737 * against cgroup_exit() changing the css_set to
738 * init_css_set and dropping the old one.
739 */
740 WARN_ON_ONCE(task->flags & PF_EXITING);
741
Tejun Heof6d7d042015-10-15 16:41:52 -0400742 rcu_assign_pointer(task->cgroups, to_cset);
743 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
744 &to_cset->tasks);
745 }
746}
747
Paul Menage7717f7b2009-09-23 15:56:22 -0700748/*
749 * hash table for cgroup groups. This improves the performance to find
750 * an existing css_set. This hash doesn't (currently) take into
751 * account cgroups in empty hierarchies.
752 */
Li Zefan472b1052008-04-29 01:00:11 -0700753#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800754static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700755
Li Zefan0ac801f2013-01-10 11:49:27 +0800756static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700757{
Li Zefan0ac801f2013-01-10 11:49:27 +0800758 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700759 struct cgroup_subsys *ss;
760 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700761
Tejun Heo30159ec2013-06-25 11:53:37 -0700762 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800763 key += (unsigned long)css[i];
764 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700765
Li Zefan0ac801f2013-01-10 11:49:27 +0800766 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700767}
768
Zefan Lia25eb522014-09-19 16:51:00 +0800769static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700770{
Tejun Heo69d02062013-06-12 21:04:50 -0700771 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400772 struct cgroup_subsys *ss;
773 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700774
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400775 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500776
777 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700778 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700779
Tejun Heo53254f92015-11-23 14:55:41 -0500780 /* This css_set is dead. unlink it and release cgroup and css refs */
781 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400782 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500783 css_put(cset->subsys[ssid]);
784 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700785 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700786 css_set_count--;
787
Tejun Heo69d02062013-06-12 21:04:50 -0700788 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700789 list_del(&link->cset_link);
790 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400791 if (cgroup_parent(link->cgrp))
792 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700793 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700794 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700795
Tejun Heo5abb8852013-06-12 21:04:49 -0700796 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700797}
798
Zefan Lia25eb522014-09-19 16:51:00 +0800799static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500800{
801 /*
802 * Ensure that the refcount doesn't hit zero while any readers
803 * can see it. Similar to atomic_dec_and_lock(), but for an
804 * rwlock
805 */
806 if (atomic_add_unless(&cset->refcount, -1, 1))
807 return;
808
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400809 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800810 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400811 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500812}
813
Paul Menage817929e2007-10-18 23:39:36 -0700814/*
815 * refcounted get/put for css_set objects
816 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700817static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700818{
Tejun Heo5abb8852013-06-12 21:04:49 -0700819 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700820}
821
Tejun Heob326f9d2013-06-24 15:21:48 -0700822/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700823 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700824 * @cset: candidate css_set being tested
825 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700826 * @new_cgrp: cgroup that's being entered by the task
827 * @template: desired set of css pointers in css_set (pre-calculated)
828 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800829 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700830 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
831 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700832static bool compare_css_sets(struct css_set *cset,
833 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700834 struct cgroup *new_cgrp,
835 struct cgroup_subsys_state *template[])
836{
837 struct list_head *l1, *l2;
838
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400839 /*
840 * On the default hierarchy, there can be csets which are
841 * associated with the same set of cgroups but different csses.
842 * Let's first ensure that csses match.
843 */
844 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700845 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700846
847 /*
848 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400849 * different cgroups in hierarchies. As different cgroups may
850 * share the same effective css, this comparison is always
851 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700852 */
Tejun Heo69d02062013-06-12 21:04:50 -0700853 l1 = &cset->cgrp_links;
854 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700855 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700856 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700857 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700858
859 l1 = l1->next;
860 l2 = l2->next;
861 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700862 if (l1 == &cset->cgrp_links) {
863 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700864 break;
865 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700866 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700867 }
868 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700869 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
870 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
871 cgrp1 = link1->cgrp;
872 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700873 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700874 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700875
876 /*
877 * If this hierarchy is the hierarchy of the cgroup
878 * that's changing, then we need to check that this
879 * css_set points to the new cgroup; if it's any other
880 * hierarchy, then this css_set should point to the
881 * same cgroup as the old css_set.
882 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700883 if (cgrp1->root == new_cgrp->root) {
884 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700885 return false;
886 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700887 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700888 return false;
889 }
890 }
891 return true;
892}
893
Tejun Heob326f9d2013-06-24 15:21:48 -0700894/**
895 * find_existing_css_set - init css array and find the matching css_set
896 * @old_cset: the css_set that we're using before the cgroup transition
897 * @cgrp: the cgroup that we're moving into
898 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700899 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700900static struct css_set *find_existing_css_set(struct css_set *old_cset,
901 struct cgroup *cgrp,
902 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700903{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400904 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700905 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700906 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800907 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700908 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700909
Ben Blumaae8aab2010-03-10 15:22:07 -0800910 /*
911 * Build the set of subsystem state objects that we want to see in the
912 * new css_set. while subsystems can change globally, the entries here
913 * won't change, so no need for locking.
914 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700915 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400916 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400917 /*
918 * @ss is in this hierarchy, so we want the
919 * effective css from @cgrp.
920 */
921 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700922 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400923 /*
924 * @ss is not in this hierarchy, so we don't want
925 * to change the css.
926 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700927 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700928 }
929 }
930
Li Zefan0ac801f2013-01-10 11:49:27 +0800931 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700932 hash_for_each_possible(css_set_table, cset, hlist, key) {
933 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700934 continue;
935
936 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700937 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700938 }
Paul Menage817929e2007-10-18 23:39:36 -0700939
940 /* No existing cgroup group matched */
941 return NULL;
942}
943
Tejun Heo69d02062013-06-12 21:04:50 -0700944static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700945{
Tejun Heo69d02062013-06-12 21:04:50 -0700946 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700947
Tejun Heo69d02062013-06-12 21:04:50 -0700948 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
949 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700950 kfree(link);
951 }
952}
953
Tejun Heo69d02062013-06-12 21:04:50 -0700954/**
955 * allocate_cgrp_cset_links - allocate cgrp_cset_links
956 * @count: the number of links to allocate
957 * @tmp_links: list_head the allocated links are put on
958 *
959 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
960 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700961 */
Tejun Heo69d02062013-06-12 21:04:50 -0700962static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700963{
Tejun Heo69d02062013-06-12 21:04:50 -0700964 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700965 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700966
967 INIT_LIST_HEAD(tmp_links);
968
Li Zefan36553432008-07-29 22:33:19 -0700969 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700970 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700971 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700972 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700973 return -ENOMEM;
974 }
Tejun Heo69d02062013-06-12 21:04:50 -0700975 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700976 }
977 return 0;
978}
979
Li Zefanc12f65d2009-01-07 18:07:42 -0800980/**
981 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700982 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700983 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800984 * @cgrp: the destination cgroup
985 */
Tejun Heo69d02062013-06-12 21:04:50 -0700986static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
987 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800988{
Tejun Heo69d02062013-06-12 21:04:50 -0700989 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800990
Tejun Heo69d02062013-06-12 21:04:50 -0700991 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400992
993 if (cgroup_on_dfl(cgrp))
994 cset->dfl_cgrp = cgrp;
995
Tejun Heo69d02062013-06-12 21:04:50 -0700996 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
997 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700998 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400999
Paul Menage7717f7b2009-09-23 15:56:22 -07001000 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001001 * Always add links to the tail of the lists so that the lists are
1002 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001003 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001004 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001005 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001006
1007 if (cgroup_parent(cgrp))
1008 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001009}
1010
Tejun Heob326f9d2013-06-24 15:21:48 -07001011/**
1012 * find_css_set - return a new css_set with one cgroup updated
1013 * @old_cset: the baseline css_set
1014 * @cgrp: the cgroup to be updated
1015 *
1016 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1017 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001018 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001019static struct css_set *find_css_set(struct css_set *old_cset,
1020 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001021{
Tejun Heob326f9d2013-06-24 15:21:48 -07001022 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001023 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001024 struct list_head tmp_links;
1025 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001026 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001027 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001028 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001029
Tejun Heob326f9d2013-06-24 15:21:48 -07001030 lockdep_assert_held(&cgroup_mutex);
1031
Paul Menage817929e2007-10-18 23:39:36 -07001032 /* First see if we already have a cgroup group that matches
1033 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001034 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001035 cset = find_existing_css_set(old_cset, cgrp, template);
1036 if (cset)
1037 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001038 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001039
Tejun Heo5abb8852013-06-12 21:04:49 -07001040 if (cset)
1041 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001042
Tejun Heof4f4be22013-06-12 21:04:51 -07001043 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001044 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001045 return NULL;
1046
Tejun Heo69d02062013-06-12 21:04:50 -07001047 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001048 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001049 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001050 return NULL;
1051 }
1052
Tejun Heo5abb8852013-06-12 21:04:49 -07001053 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001054 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001055 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001056 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001057 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001058 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001059 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001060 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001061
1062 /* Copy the set of subsystem state objects generated in
1063 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001064 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001065
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001066 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001067 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001068 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001069 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001070
Paul Menage7717f7b2009-09-23 15:56:22 -07001071 if (c->root == cgrp->root)
1072 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001073 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001074 }
Paul Menage817929e2007-10-18 23:39:36 -07001075
Tejun Heo69d02062013-06-12 21:04:50 -07001076 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001077
Paul Menage817929e2007-10-18 23:39:36 -07001078 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001079
Tejun Heo2d8f2432014-04-23 11:13:15 -04001080 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001081 key = css_set_hash(cset->subsys);
1082 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001083
Tejun Heo53254f92015-11-23 14:55:41 -05001084 for_each_subsys(ss, ssid) {
1085 struct cgroup_subsys_state *css = cset->subsys[ssid];
1086
Tejun Heo2d8f2432014-04-23 11:13:15 -04001087 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001088 &css->cgroup->e_csets[ssid]);
1089 css_get(css);
1090 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001091
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001092 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001093
Tejun Heo5abb8852013-06-12 21:04:49 -07001094 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001095}
1096
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001097static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001098{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001099 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001100
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001101 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001102}
1103
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001104static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001105{
1106 int id;
1107
1108 lockdep_assert_held(&cgroup_mutex);
1109
Tejun Heo985ed672014-03-19 10:23:53 -04001110 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001111 if (id < 0)
1112 return id;
1113
1114 root->hierarchy_id = id;
1115 return 0;
1116}
1117
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001118static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001119{
1120 lockdep_assert_held(&cgroup_mutex);
1121
1122 if (root->hierarchy_id) {
1123 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1124 root->hierarchy_id = 0;
1125 }
1126}
1127
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001128static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001129{
1130 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001131 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001132 WARN_ON_ONCE(root->hierarchy_id);
1133
1134 idr_destroy(&root->cgroup_idr);
1135 kfree(root);
1136 }
1137}
1138
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001139static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001140{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001141 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001142 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001143
Tejun Heo2bd59d42014-02-11 11:52:49 -05001144 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001145
Tejun Heo776f02f2014-02-12 09:29:50 -05001146 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001147 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001148
Tejun Heof2e85d52014-02-11 11:52:49 -05001149 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001150 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001151
1152 /*
1153 * Release all the links from cset_links to this hierarchy's
1154 * root cgroup
1155 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001156 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001157
1158 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1159 list_del(&link->cset_link);
1160 list_del(&link->cgrp_link);
1161 kfree(link);
1162 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001163
1164 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001165
1166 if (!list_empty(&root->root_list)) {
1167 list_del(&root->root_list);
1168 cgroup_root_count--;
1169 }
1170
1171 cgroup_exit_root_id(root);
1172
1173 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001174
Tejun Heo2bd59d42014-02-11 11:52:49 -05001175 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001176 cgroup_free_root(root);
1177}
1178
Tejun Heoceb6a082014-02-25 10:04:02 -05001179/* look up cgroup associated with given css_set on the specified hierarchy */
1180static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001181 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001182{
Paul Menage7717f7b2009-09-23 15:56:22 -07001183 struct cgroup *res = NULL;
1184
Tejun Heo96d365e2014-02-13 06:58:40 -05001185 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001186 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001187
Tejun Heo5abb8852013-06-12 21:04:49 -07001188 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001189 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001190 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001191 struct cgrp_cset_link *link;
1192
1193 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001194 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001195
Paul Menage7717f7b2009-09-23 15:56:22 -07001196 if (c->root == root) {
1197 res = c;
1198 break;
1199 }
1200 }
1201 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001202
Paul Menage7717f7b2009-09-23 15:56:22 -07001203 BUG_ON(!res);
1204 return res;
1205}
1206
1207/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001208 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001209 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001210 */
1211static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001212 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001213{
1214 /*
1215 * No need to lock the task - since we hold cgroup_mutex the
1216 * task can't change groups, so the only thing that can happen
1217 * is that it exits and its css is set back to init_css_set.
1218 */
1219 return cset_cgroup_from_root(task_css_set(task), root);
1220}
1221
1222/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001223 * A task must hold cgroup_mutex to modify cgroups.
1224 *
1225 * Any task can increment and decrement the count field without lock.
1226 * So in general, code holding cgroup_mutex can't rely on the count
1227 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001228 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229 * means that no tasks are currently attached, therefore there is no
1230 * way a task attached to that cgroup can fork (the other way to
1231 * increment the count). So code holding cgroup_mutex can safely
1232 * assume that if the count is zero, it will stay zero. Similarly, if
1233 * a task holds cgroup_mutex on a cgroup with zero count, it
1234 * knows that the cgroup won't be removed, as cgroup_rmdir()
1235 * needs that mutex.
1236 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001237 * A cgroup can only be deleted if both its 'count' of using tasks
1238 * is zero, and its list of 'children' cgroups is empty. Since all
1239 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001240 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001242 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 *
1244 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001245 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001246 */
1247
Tejun Heo2bd59d42014-02-11 11:52:49 -05001248static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001249static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001250
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001251static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1252 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001254 struct cgroup_subsys *ss = cft->ss;
1255
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001256 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1257 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1258 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001259 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1260 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001261 else
1262 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1263 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264}
1265
Tejun Heof2e85d52014-02-11 11:52:49 -05001266/**
1267 * cgroup_file_mode - deduce file mode of a control file
1268 * @cft: the control file in question
1269 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001270 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001271 */
1272static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001273{
Tejun Heof2e85d52014-02-11 11:52:49 -05001274 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001275
Tejun Heof2e85d52014-02-11 11:52:49 -05001276 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1277 mode |= S_IRUGO;
1278
Tejun Heo7dbdb192015-09-18 17:54:23 -04001279 if (cft->write_u64 || cft->write_s64 || cft->write) {
1280 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1281 mode |= S_IWUGO;
1282 else
1283 mode |= S_IWUSR;
1284 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001285
1286 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001287}
1288
Tejun Heoa9746d82014-05-13 12:19:22 -04001289/**
Tejun Heo8699b772016-02-22 22:25:46 -05001290 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001291 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001292 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001293 *
1294 * On the default hierarchy, a subsystem may request other subsystems to be
1295 * enabled together through its ->depends_on mask. In such cases, more
1296 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1297 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001298 * This function calculates which subsystems need to be enabled if
1299 * @subtree_control is to be applied to @cgrp. The returned mask is always
1300 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001301 */
Tejun Heo6e5c8302016-02-22 22:25:47 -05001302static u16 cgroup_calc_subtree_ss_mask(struct cgroup *cgrp, u16 subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001303{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001304 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001305 struct cgroup_subsys *ss;
1306 int ssid;
1307
1308 lockdep_assert_held(&cgroup_mutex);
1309
Tejun Heo0f060de2014-11-18 02:49:50 -05001310 if (!cgroup_on_dfl(cgrp))
1311 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001312
1313 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001314 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001315
Tejun Heob4e0eea2016-02-22 22:25:46 -05001316 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001317 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001318 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001319
1320 /*
1321 * Mask out subsystems which aren't available. This can
1322 * happen only if some depended-upon subsystems were bound
1323 * to non-default hierarchies.
1324 */
Tejun Heo5531dc92016-03-03 09:57:58 -05001325 new_ss_mask &= cgroup_ss_mask(cgrp);
Tejun Heoaf0ba672014-07-08 18:02:57 -04001326
1327 if (new_ss_mask == cur_ss_mask)
1328 break;
1329 cur_ss_mask = new_ss_mask;
1330 }
1331
Tejun Heo0f060de2014-11-18 02:49:50 -05001332 return cur_ss_mask;
1333}
1334
1335/**
Tejun Heo8699b772016-02-22 22:25:46 -05001336 * cgroup_refresh_subtree_ss_mask - update subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001337 * @cgrp: the target cgroup
1338 *
Tejun Heo8699b772016-02-22 22:25:46 -05001339 * Update @cgrp->subtree_ss_mask according to the current
1340 * @cgrp->subtree_control using cgroup_calc_subtree_ss_mask().
Tejun Heo0f060de2014-11-18 02:49:50 -05001341 */
Tejun Heo8699b772016-02-22 22:25:46 -05001342static void cgroup_refresh_subtree_ss_mask(struct cgroup *cgrp)
Tejun Heo0f060de2014-11-18 02:49:50 -05001343{
Tejun Heo8699b772016-02-22 22:25:46 -05001344 cgrp->subtree_ss_mask =
1345 cgroup_calc_subtree_ss_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001346}
1347
Tejun Heoa9746d82014-05-13 12:19:22 -04001348/**
1349 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1350 * @kn: the kernfs_node being serviced
1351 *
1352 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1353 * the method finishes if locking succeeded. Note that once this function
1354 * returns the cgroup returned by cgroup_kn_lock_live() may become
1355 * inaccessible any time. If the caller intends to continue to access the
1356 * cgroup, it should pin it before invoking this function.
1357 */
1358static void cgroup_kn_unlock(struct kernfs_node *kn)
1359{
1360 struct cgroup *cgrp;
1361
1362 if (kernfs_type(kn) == KERNFS_DIR)
1363 cgrp = kn->priv;
1364 else
1365 cgrp = kn->parent->priv;
1366
1367 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001368
1369 kernfs_unbreak_active_protection(kn);
1370 cgroup_put(cgrp);
1371}
1372
1373/**
1374 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1375 * @kn: the kernfs_node being serviced
1376 *
1377 * This helper is to be used by a cgroup kernfs method currently servicing
1378 * @kn. It breaks the active protection, performs cgroup locking and
1379 * verifies that the associated cgroup is alive. Returns the cgroup if
1380 * alive; otherwise, %NULL. A successful return should be undone by a
1381 * matching cgroup_kn_unlock() invocation.
1382 *
1383 * Any cgroup kernfs method implementation which requires locking the
1384 * associated cgroup should use this helper. It avoids nesting cgroup
1385 * locking under kernfs active protection and allows all kernfs operations
1386 * including self-removal.
1387 */
1388static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1389{
1390 struct cgroup *cgrp;
1391
1392 if (kernfs_type(kn) == KERNFS_DIR)
1393 cgrp = kn->priv;
1394 else
1395 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396
Tejun Heo2bd59d42014-02-11 11:52:49 -05001397 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001398 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001399 * active_ref. cgroup liveliness check alone provides enough
1400 * protection against removal. Ensure @cgrp stays accessible and
1401 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001402 */
Li Zefanaa323622014-09-04 14:43:38 +08001403 if (!cgroup_tryget(cgrp))
1404 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001405 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406
Tejun Heoa9746d82014-05-13 12:19:22 -04001407 mutex_lock(&cgroup_mutex);
1408
1409 if (!cgroup_is_dead(cgrp))
1410 return cgrp;
1411
1412 cgroup_kn_unlock(kn);
1413 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414}
1415
Li Zefan2739d3c2013-01-21 18:18:33 +08001416static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001418 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419
Tejun Heo01f64742014-05-13 12:19:23 -04001420 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001421
1422 if (cft->file_offset) {
1423 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1424 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1425
1426 spin_lock_irq(&cgroup_file_kn_lock);
1427 cfile->kn = NULL;
1428 spin_unlock_irq(&cgroup_file_kn_lock);
1429 }
1430
Tejun Heo2bd59d42014-02-11 11:52:49 -05001431 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001432}
1433
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001434/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001435 * css_clear_dir - remove subsys files in a cgroup directory
1436 * @css: taget css
1437 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001438 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001439static void css_clear_dir(struct cgroup_subsys_state *css,
1440 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001441{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001442 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1443 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001444
Tejun Heo88cb04b2016-03-03 09:57:58 -05001445 if (!(css->flags & CSS_VISIBLE))
1446 return;
1447
1448 css->flags &= ~CSS_VISIBLE;
1449
Tejun Heo4df8dc92015-09-18 17:54:23 -04001450 list_for_each_entry(cfts, &css->ss->cfts, node)
1451 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452}
1453
Tejun Heoccdca212015-09-18 17:54:23 -04001454/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001455 * css_populate_dir - create subsys files in a cgroup directory
1456 * @css: target css
1457 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001458 *
1459 * On failure, no file is added.
1460 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001461static int css_populate_dir(struct cgroup_subsys_state *css,
1462 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001463{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001464 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1465 struct cftype *cfts, *failed_cfts;
1466 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001467
Tejun Heo88cb04b2016-03-03 09:57:58 -05001468 if (css->flags & CSS_VISIBLE)
1469 return 0;
1470
Tejun Heo4df8dc92015-09-18 17:54:23 -04001471 if (!css->ss) {
1472 if (cgroup_on_dfl(cgrp))
1473 cfts = cgroup_dfl_base_files;
1474 else
1475 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001476
Tejun Heo4df8dc92015-09-18 17:54:23 -04001477 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1478 }
Tejun Heoccdca212015-09-18 17:54:23 -04001479
Tejun Heo4df8dc92015-09-18 17:54:23 -04001480 list_for_each_entry(cfts, &css->ss->cfts, node) {
1481 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1482 if (ret < 0) {
1483 failed_cfts = cfts;
1484 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001485 }
1486 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001487
1488 css->flags |= CSS_VISIBLE;
1489
Tejun Heoccdca212015-09-18 17:54:23 -04001490 return 0;
1491err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001492 list_for_each_entry(cfts, &css->ss->cfts, node) {
1493 if (cfts == failed_cfts)
1494 break;
1495 cgroup_addrm_files(css, cgrp, cfts, false);
1496 }
Tejun Heoccdca212015-09-18 17:54:23 -04001497 return ret;
1498}
1499
Tejun Heo6e5c8302016-02-22 22:25:47 -05001500static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501{
Tejun Heo1ada4832015-09-18 17:54:23 -04001502 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001503 struct cgroup_subsys *ss;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001504 u16 tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001505 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506
Tejun Heoace2bee2014-02-11 11:52:47 -05001507 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001508
Tejun Heob4e0eea2016-02-22 22:25:46 -05001509 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001510 /* if @ss has non-root csses attached to it, can't move */
1511 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001512 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513
Tejun Heo5df36032014-03-19 10:23:54 -04001514 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001515 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001516 return -EBUSY;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001517 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518
Tejun Heo5533e012014-05-14 19:33:07 -04001519 /* skip creating root files on dfl_root for inhibited subsystems */
1520 tmp_ss_mask = ss_mask;
1521 if (dst_root == &cgrp_dfl_root)
Tejun Heoa7165262016-02-23 10:00:50 -05001522 tmp_ss_mask &= ~cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -04001523
Tejun Heob4e0eea2016-02-22 22:25:46 -05001524 do_each_subsys_mask(ss, ssid, tmp_ss_mask) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04001525 struct cgroup *scgrp = &ss->root->cgrp;
1526 int tssid;
1527
1528 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1529 if (!ret)
1530 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
Tejun Heoa2dd4242014-03-19 10:23:55 -04001532 /*
1533 * Rebinding back to the default root is not allowed to
1534 * fail. Using both default and non-default roots should
1535 * be rare. Moving subsystems back and forth even more so.
1536 * Just warn about it and continue.
1537 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001538 if (dst_root == &cgrp_dfl_root) {
Tejun Heoa7165262016-02-23 10:00:50 -05001539 if (cgrp_dfl_visible) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001540 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Tejun Heo4df8dc92015-09-18 17:54:23 -04001541 ret, ss_mask);
1542 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1543 }
1544 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001545 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001546
Tejun Heob4e0eea2016-02-22 22:25:46 -05001547 do_each_subsys_mask(ss, tssid, tmp_ss_mask) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04001548 if (tssid == ssid)
1549 break;
1550 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001551 } while_each_subsys_mask();
Tejun Heo4df8dc92015-09-18 17:54:23 -04001552 return ret;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001553 } while_each_subsys_mask();
Tejun Heo31261212013-06-28 17:07:30 -07001554
1555 /*
1556 * Nothing can fail from this point on. Remove files for the
1557 * removed subsystems and rebind each subsystem.
1558 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05001559 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001560 struct cgroup_root *src_root = ss->root;
1561 struct cgroup *scgrp = &src_root->cgrp;
1562 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001563 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001564
Tejun Heo1ada4832015-09-18 17:54:23 -04001565 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001566
Tejun Heo4df8dc92015-09-18 17:54:23 -04001567 css_clear_dir(css, NULL);
1568
Tejun Heo1ada4832015-09-18 17:54:23 -04001569 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1570 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001571 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001572 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001573
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001574 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001575 hash_for_each(css_set_table, i, cset, hlist)
1576 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001577 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001578 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001579
Tejun Heof392e512014-04-23 11:13:14 -04001580 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001581 scgrp->subtree_control &= ~(1 << ssid);
Tejun Heo8699b772016-02-22 22:25:46 -05001582 cgroup_refresh_subtree_ss_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001583
Tejun Heobd53d612014-04-23 11:13:16 -04001584 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001585 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001586 if (dst_root == &cgrp_dfl_root) {
1587 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1588 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001589 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo8699b772016-02-22 22:25:46 -05001590 cgroup_refresh_subtree_ss_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001591 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001592 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001593
Tejun Heo5df36032014-03-19 10:23:54 -04001594 if (ss->bind)
1595 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001596 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597
Tejun Heo1ada4832015-09-18 17:54:23 -04001598 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 return 0;
1600}
1601
Tejun Heo2bd59d42014-02-11 11:52:49 -05001602static int cgroup_show_options(struct seq_file *seq,
1603 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001605 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001607 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
Tejun Heod98817d2015-08-18 13:58:16 -07001609 if (root != &cgrp_dfl_root)
1610 for_each_subsys(ss, ssid)
1611 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001612 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001613 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001615 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001616 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001617
1618 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001619 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001620 seq_show_option(seq, "release_agent",
1621 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001622 spin_unlock(&release_agent_path_lock);
1623
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001624 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001625 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001626 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001627 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 return 0;
1629}
1630
1631struct cgroup_sb_opts {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001632 u16 subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001633 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001634 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001635 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001636 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001637 /* User explicitly requested empty subsystem */
1638 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639};
1640
Ben Blumcf5d5942010-03-10 15:22:09 -08001641static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001643 char *token, *o = data;
1644 bool all_ss = false, one_ss = false;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001645 u16 mask = U16_MAX;
Tejun Heo30159ec2013-06-25 11:53:37 -07001646 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001647 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001648 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001649
1650#ifdef CONFIG_CPUSETS
Tejun Heo6e5c8302016-02-22 22:25:47 -05001651 mask = ~((u16)1 << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001652#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653
Paul Menagec6d57f32009-09-23 15:56:19 -07001654 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655
1656 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001657 nr_opts++;
1658
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 if (!*token)
1660 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001661 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001662 /* Explicitly have no subsystems */
1663 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001664 continue;
1665 }
1666 if (!strcmp(token, "all")) {
1667 /* Mutually exclusive option 'all' + subsystem name */
1668 if (one_ss)
1669 return -EINVAL;
1670 all_ss = true;
1671 continue;
1672 }
1673 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001674 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001675 continue;
1676 }
1677 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001678 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001679 continue;
1680 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001681 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001682 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001683 continue;
1684 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001685 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001686 /* Specifying two release agents is forbidden */
1687 if (opts->release_agent)
1688 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001689 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001690 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001691 if (!opts->release_agent)
1692 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001693 continue;
1694 }
1695 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001696 const char *name = token + 5;
1697 /* Can't specify an empty name */
1698 if (!strlen(name))
1699 return -EINVAL;
1700 /* Must match [\w.-]+ */
1701 for (i = 0; i < strlen(name); i++) {
1702 char c = name[i];
1703 if (isalnum(c))
1704 continue;
1705 if ((c == '.') || (c == '-') || (c == '_'))
1706 continue;
1707 return -EINVAL;
1708 }
1709 /* Specifying two names is forbidden */
1710 if (opts->name)
1711 return -EINVAL;
1712 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001713 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001714 GFP_KERNEL);
1715 if (!opts->name)
1716 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001717
1718 continue;
1719 }
1720
Tejun Heo30159ec2013-06-25 11:53:37 -07001721 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001722 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001723 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001724 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001725 continue;
Johannes Weiner223ffb22016-02-11 13:34:49 -05001726 if (cgroup_ssid_no_v1(i))
1727 continue;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001728
1729 /* Mutually exclusive option 'all' + subsystem name */
1730 if (all_ss)
1731 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001732 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001733 one_ss = true;
1734
1735 break;
1736 }
1737 if (i == CGROUP_SUBSYS_COUNT)
1738 return -ENOENT;
1739 }
1740
Li Zefanf9ab5b52009-06-17 16:26:33 -07001741 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001742 * If the 'all' option was specified select all the subsystems,
1743 * otherwise if 'none', 'name=' and a subsystem name options were
1744 * not specified, let's default to 'all'
1745 */
1746 if (all_ss || (!one_ss && !opts->none && !opts->name))
1747 for_each_subsys(ss, i)
Johannes Weiner223ffb22016-02-11 13:34:49 -05001748 if (cgroup_ssid_enabled(i) && !cgroup_ssid_no_v1(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001749 opts->subsys_mask |= (1 << i);
1750
1751 /*
1752 * We either have to specify by name or by subsystems. (So all
1753 * empty hierarchies must have a name).
1754 */
1755 if (!opts->subsys_mask && !opts->name)
1756 return -EINVAL;
1757
1758 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001759 * Option noprefix was introduced just for backward compatibility
1760 * with the old cpuset, so we allow noprefix only if mounting just
1761 * the cpuset subsystem.
1762 */
Tejun Heo93438622013-04-14 20:15:25 -07001763 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001764 return -EINVAL;
1765
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001766 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001767 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001768 return -EINVAL;
1769
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770 return 0;
1771}
1772
Tejun Heo2bd59d42014-02-11 11:52:49 -05001773static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774{
1775 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001776 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 struct cgroup_sb_opts opts;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001778 u16 added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779
Tejun Heoaa6ec292014-07-09 10:08:08 -04001780 if (root == &cgrp_dfl_root) {
1781 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001782 return -EINVAL;
1783 }
1784
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 mutex_lock(&cgroup_mutex);
1786
1787 /* See what subsystems are wanted */
1788 ret = parse_cgroupfs_options(data, &opts);
1789 if (ret)
1790 goto out_unlock;
1791
Tejun Heof392e512014-04-23 11:13:14 -04001792 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001793 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001794 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001795
Tejun Heof392e512014-04-23 11:13:14 -04001796 added_mask = opts.subsys_mask & ~root->subsys_mask;
1797 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001798
Ben Blumcf5d5942010-03-10 15:22:09 -08001799 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001800 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001801 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001802 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001803 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001805 goto out_unlock;
1806 }
1807
Tejun Heof172e672013-06-28 17:07:30 -07001808 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001809 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001810 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001811 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001812 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813
Tejun Heo5df36032014-03-19 10:23:54 -04001814 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001815 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001818 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001819
Tejun Heo69e943b2014-02-08 10:36:58 -05001820 if (opts.release_agent) {
1821 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001822 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001823 spin_unlock(&release_agent_path_lock);
1824 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001825 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001826 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001827 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829 return ret;
1830}
1831
Tejun Heoafeb0f92014-02-13 06:58:39 -05001832/*
1833 * To reduce the fork() overhead for systems that are not actually using
1834 * their cgroups capability, we don't maintain the lists running through
1835 * each css_set to its tasks until we see the list actually used - in other
1836 * words after the first mount.
1837 */
1838static bool use_task_css_set_links __read_mostly;
1839
1840static void cgroup_enable_task_cg_lists(void)
1841{
1842 struct task_struct *p, *g;
1843
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001844 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001845
1846 if (use_task_css_set_links)
1847 goto out_unlock;
1848
1849 use_task_css_set_links = true;
1850
1851 /*
1852 * We need tasklist_lock because RCU is not safe against
1853 * while_each_thread(). Besides, a forking task that has passed
1854 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1855 * is not guaranteed to have its child immediately visible in the
1856 * tasklist if we walk through it with RCU.
1857 */
1858 read_lock(&tasklist_lock);
1859 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001860 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1861 task_css_set(p) != &init_css_set);
1862
1863 /*
1864 * We should check if the process is exiting, otherwise
1865 * it will race with cgroup_exit() in that the list
1866 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001867 * Do it while holding siglock so that we don't end up
1868 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001869 */
Tejun Heof153ad12014-02-25 09:56:49 -05001870 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001871 if (!(p->flags & PF_EXITING)) {
1872 struct css_set *cset = task_css_set(p);
1873
Tejun Heo0de09422015-10-15 16:41:49 -04001874 if (!css_set_populated(cset))
1875 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001876 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001877 get_css_set(cset);
1878 }
Tejun Heof153ad12014-02-25 09:56:49 -05001879 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001880 } while_each_thread(g, p);
1881 read_unlock(&tasklist_lock);
1882out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001883 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001884}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001885
Paul Menagecc31edc2008-10-18 20:28:04 -07001886static void init_cgroup_housekeeping(struct cgroup *cgrp)
1887{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001888 struct cgroup_subsys *ss;
1889 int ssid;
1890
Tejun Heod5c419b2014-05-16 13:22:48 -04001891 INIT_LIST_HEAD(&cgrp->self.sibling);
1892 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001893 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001894 INIT_LIST_HEAD(&cgrp->pidlists);
1895 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001896 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001897 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001898
1899 for_each_subsys(ss, ssid)
1900 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001901
1902 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001903 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001904}
Paul Menagec6d57f32009-09-23 15:56:19 -07001905
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001906static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001907 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001908{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001909 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001910
Paul Menageddbcc7e2007-10-18 23:39:30 -07001911 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001912 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001913 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001914 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001915 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001916
Paul Menagec6d57f32009-09-23 15:56:19 -07001917 root->flags = opts->flags;
1918 if (opts->release_agent)
1919 strcpy(root->release_agent_path, opts->release_agent);
1920 if (opts->name)
1921 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001922 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001923 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001924}
1925
Tejun Heo6e5c8302016-02-22 22:25:47 -05001926static int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001927{
Tejun Heod427dfe2014-02-11 11:52:48 -05001928 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001929 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001930 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001931 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001932
Tejun Heod427dfe2014-02-11 11:52:48 -05001933 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001934
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001935 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001936 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001937 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001938 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001939 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001940
Tejun Heo2aad2a82014-09-24 13:31:50 -04001941 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1942 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001943 if (ret)
1944 goto out;
1945
Tejun Heod427dfe2014-02-11 11:52:48 -05001946 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001947 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001948 * but that's OK - it can only be increased by someone holding
1949 * cgroup_lock, and that's us. The worst that can happen is that we
1950 * have some link structures left over
1951 */
1952 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001953 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001954 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001955
Tejun Heo985ed672014-03-19 10:23:53 -04001956 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001957 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001958 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001959
Tejun Heo2bd59d42014-02-11 11:52:49 -05001960 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1961 KERNFS_ROOT_CREATE_DEACTIVATED,
1962 root_cgrp);
1963 if (IS_ERR(root->kf_root)) {
1964 ret = PTR_ERR(root->kf_root);
1965 goto exit_root_id;
1966 }
1967 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001968
Tejun Heo4df8dc92015-09-18 17:54:23 -04001969 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001970 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001971 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001972
Tejun Heo5df36032014-03-19 10:23:54 -04001973 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001974 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001975 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001976
Tejun Heod427dfe2014-02-11 11:52:48 -05001977 /*
1978 * There must be no failure case after here, since rebinding takes
1979 * care of subsystems' refcounts, which are explicitly dropped in
1980 * the failure exit path.
1981 */
1982 list_add(&root->root_list, &cgroup_roots);
1983 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001984
Tejun Heod427dfe2014-02-11 11:52:48 -05001985 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001986 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001987 * objects.
1988 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001989 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04001990 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001991 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04001992 if (css_set_populated(cset))
1993 cgroup_update_populated(root_cgrp, true);
1994 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001995 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001996
Tejun Heod5c419b2014-05-16 13:22:48 -04001997 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001998 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001999
Tejun Heo2bd59d42014-02-11 11:52:49 -05002000 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05002001 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002002 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05002003
Tejun Heo2bd59d42014-02-11 11:52:49 -05002004destroy_root:
2005 kernfs_destroy_root(root->kf_root);
2006 root->kf_root = NULL;
2007exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05002008 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04002009cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04002010 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002011out:
Tejun Heod427dfe2014-02-11 11:52:48 -05002012 free_cgrp_cset_links(&tmp_links);
2013 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002014}
2015
Al Virof7e83572010-07-26 13:23:11 +04002016static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002017 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04002018 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002019{
Tejun Heo67e9c742015-11-16 11:13:34 -05002020 bool is_v2 = fs_type == &cgroup2_fs_type;
Li Zefan3a32bd72014-06-30 11:50:59 +08002021 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08002022 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002023 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002024 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002025 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002026 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08002027 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002028 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07002029
2030 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05002031 * The first time anyone tries to mount a cgroup, enable the list
2032 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07002033 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05002034 if (!use_task_css_set_links)
2035 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002036
Tejun Heo67e9c742015-11-16 11:13:34 -05002037 if (is_v2) {
2038 if (data) {
2039 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
2040 return ERR_PTR(-EINVAL);
2041 }
Tejun Heoa7165262016-02-23 10:00:50 -05002042 cgrp_dfl_visible = true;
Tejun Heo67e9c742015-11-16 11:13:34 -05002043 root = &cgrp_dfl_root;
2044 cgroup_get(&root->cgrp);
2045 goto out_mount;
2046 }
2047
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002048 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002049
Paul Menageddbcc7e2007-10-18 23:39:30 -07002050 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002051 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002052 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002053 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002054
Li Zefan970317a2014-06-30 11:49:58 +08002055 /*
2056 * Destruction of cgroup root is asynchronous, so subsystems may
2057 * still be dying after the previous unmount. Let's drain the
2058 * dying subsystems. We just need to ensure that the ones
2059 * unmounted previously finish dying and don't care about new ones
2060 * starting. Testing ref liveliness is good enough.
2061 */
2062 for_each_subsys(ss, i) {
2063 if (!(opts.subsys_mask & (1 << i)) ||
2064 ss->root == &cgrp_dfl_root)
2065 continue;
2066
2067 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2068 mutex_unlock(&cgroup_mutex);
2069 msleep(10);
2070 ret = restart_syscall();
2071 goto out_free;
2072 }
2073 cgroup_put(&ss->root->cgrp);
2074 }
2075
Tejun Heo985ed672014-03-19 10:23:53 -04002076 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002077 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002078
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002079 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002080 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002081
Paul Menage817929e2007-10-18 23:39:36 -07002082 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002083 * If we asked for a name then it must match. Also, if
2084 * name matches but sybsys_mask doesn't, we should fail.
2085 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002086 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002087 if (opts.name) {
2088 if (strcmp(opts.name, root->name))
2089 continue;
2090 name_match = true;
2091 }
Tejun Heo31261212013-06-28 17:07:30 -07002092
2093 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002094 * If we asked for subsystems (or explicitly for no
2095 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002096 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002097 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002098 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002099 if (!name_match)
2100 continue;
2101 ret = -EBUSY;
2102 goto out_unlock;
2103 }
Tejun Heo873fe092013-04-14 20:15:26 -07002104
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002105 if (root->flags ^ opts.flags)
2106 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002107
Tejun Heo776f02f2014-02-12 09:29:50 -05002108 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002109 * We want to reuse @root whose lifetime is governed by its
2110 * ->cgrp. Let's check whether @root is alive and keep it
2111 * that way. As cgroup_kill_sb() can happen anytime, we
2112 * want to block it by pinning the sb so that @root doesn't
2113 * get killed before mount is complete.
2114 *
2115 * With the sb pinned, tryget_live can reliably indicate
2116 * whether @root can be reused. If it's being killed,
2117 * drain it. We can use wait_queue for the wait but this
2118 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002119 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002120 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2121 if (IS_ERR(pinned_sb) ||
2122 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002123 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002124 if (!IS_ERR_OR_NULL(pinned_sb))
2125 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002126 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002127 ret = restart_syscall();
2128 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002129 }
2130
2131 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002132 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002133 }
2134
Tejun Heo172a2c062014-03-19 10:23:53 -04002135 /*
2136 * No such thing, create a new one. name= matching without subsys
2137 * specification is allowed for already existing hierarchies but we
2138 * can't create new one without subsys specification.
2139 */
2140 if (!opts.subsys_mask && !opts.none) {
2141 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002142 goto out_unlock;
2143 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002144
Tejun Heo172a2c062014-03-19 10:23:53 -04002145 root = kzalloc(sizeof(*root), GFP_KERNEL);
2146 if (!root) {
2147 ret = -ENOMEM;
2148 goto out_unlock;
2149 }
2150
2151 init_cgroup_root(root, &opts);
2152
Tejun Heo35585572014-02-13 06:58:38 -05002153 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002154 if (ret)
2155 cgroup_free_root(root);
2156
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002157out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002158 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002159out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002160 kfree(opts.release_agent);
2161 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002162
Tejun Heo2bd59d42014-02-11 11:52:49 -05002163 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002164 return ERR_PTR(ret);
Tejun Heo67e9c742015-11-16 11:13:34 -05002165out_mount:
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002166 dentry = kernfs_mount(fs_type, flags, root->kf_root,
Tejun Heo67e9c742015-11-16 11:13:34 -05002167 is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
2168 &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002169 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002170 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002171
2172 /*
2173 * If @pinned_sb, we're reusing an existing root and holding an
2174 * extra ref on its sb. Mount is complete. Put the extra ref.
2175 */
2176 if (pinned_sb) {
2177 WARN_ON(new_sb);
2178 deactivate_super(pinned_sb);
2179 }
2180
Tejun Heo2bd59d42014-02-11 11:52:49 -05002181 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002182}
2183
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002184static void cgroup_kill_sb(struct super_block *sb)
2185{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002186 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002187 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002188
Tejun Heo9d755d32014-05-14 09:15:02 -04002189 /*
2190 * If @root doesn't have any mounts or children, start killing it.
2191 * This prevents new mounts by disabling percpu_ref_tryget_live().
2192 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002193 *
2194 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002195 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002196 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002197 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002198 cgroup_put(&root->cgrp);
2199 else
2200 percpu_ref_kill(&root->cgrp.self.refcnt);
2201
Tejun Heo2bd59d42014-02-11 11:52:49 -05002202 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002203}
2204
2205static struct file_system_type cgroup_fs_type = {
2206 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002207 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002208 .kill_sb = cgroup_kill_sb,
2209};
2210
Tejun Heo67e9c742015-11-16 11:13:34 -05002211static struct file_system_type cgroup2_fs_type = {
2212 .name = "cgroup2",
2213 .mount = cgroup_mount,
2214 .kill_sb = cgroup_kill_sb,
2215};
2216
Li Zefana043e3b2008-02-23 15:24:09 -08002217/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002218 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002219 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002220 * @buf: the buffer to write the path into
2221 * @buflen: the length of the buffer
2222 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002223 * Determine @task's cgroup on the first (the one with the lowest non-zero
2224 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2225 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2226 * cgroup controller callbacks.
2227 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002228 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002229 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002230char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002231{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002232 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002233 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002234 int hierarchy_id = 1;
2235 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002236
2237 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002238 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002239
Tejun Heo913ffdb2013-07-11 16:34:48 -07002240 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2241
Tejun Heo857a2be2013-04-14 20:50:08 -07002242 if (root) {
2243 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002244 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002245 } else {
2246 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002247 if (strlcpy(buf, "/", buflen) < buflen)
2248 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002249 }
2250
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002251 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002252 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002253 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002254}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002255EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002256
Tejun Heob3dc0942014-02-25 10:04:01 -05002257/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002258struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002259 /* the src and dst cset list running through cset->mg_node */
2260 struct list_head src_csets;
2261 struct list_head dst_csets;
2262
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002263 /* the subsys currently being processed */
2264 int ssid;
2265
Tejun Heob3dc0942014-02-25 10:04:01 -05002266 /*
2267 * Fields for cgroup_taskset_*() iteration.
2268 *
2269 * Before migration is committed, the target migration tasks are on
2270 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2271 * the csets on ->dst_csets. ->csets point to either ->src_csets
2272 * or ->dst_csets depending on whether migration is committed.
2273 *
2274 * ->cur_csets and ->cur_task point to the current task position
2275 * during iteration.
2276 */
2277 struct list_head *csets;
2278 struct css_set *cur_cset;
2279 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002280};
2281
Tejun Heoadaae5d2015-09-11 15:00:21 -04002282#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2283 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2284 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2285 .csets = &tset.src_csets, \
2286}
2287
2288/**
2289 * cgroup_taskset_add - try to add a migration target task to a taskset
2290 * @task: target task
2291 * @tset: target taskset
2292 *
2293 * Add @task, which is a migration target, to @tset. This function becomes
2294 * noop if @task doesn't need to be migrated. @task's css_set should have
2295 * been added as a migration source and @task->cg_list will be moved from
2296 * the css_set's tasks list to mg_tasks one.
2297 */
2298static void cgroup_taskset_add(struct task_struct *task,
2299 struct cgroup_taskset *tset)
2300{
2301 struct css_set *cset;
2302
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002303 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002304
2305 /* @task either already exited or can't exit until the end */
2306 if (task->flags & PF_EXITING)
2307 return;
2308
2309 /* leave @task alone if post_fork() hasn't linked it yet */
2310 if (list_empty(&task->cg_list))
2311 return;
2312
2313 cset = task_css_set(task);
2314 if (!cset->mg_src_cgrp)
2315 return;
2316
2317 list_move_tail(&task->cg_list, &cset->mg_tasks);
2318 if (list_empty(&cset->mg_node))
2319 list_add_tail(&cset->mg_node, &tset->src_csets);
2320 if (list_empty(&cset->mg_dst_cset->mg_node))
2321 list_move_tail(&cset->mg_dst_cset->mg_node,
2322 &tset->dst_csets);
2323}
2324
Tejun Heo2f7ee562011-12-12 18:12:21 -08002325/**
2326 * cgroup_taskset_first - reset taskset and return the first task
2327 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002328 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002329 *
2330 * @tset iteration is initialized and the first task is returned.
2331 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002332struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2333 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002334{
Tejun Heob3dc0942014-02-25 10:04:01 -05002335 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2336 tset->cur_task = NULL;
2337
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002338 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002339}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002340
2341/**
2342 * cgroup_taskset_next - iterate to the next task in taskset
2343 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002344 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002345 *
2346 * Return the next task in @tset. Iteration must have been initialized
2347 * with cgroup_taskset_first().
2348 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002349struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2350 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002351{
Tejun Heob3dc0942014-02-25 10:04:01 -05002352 struct css_set *cset = tset->cur_cset;
2353 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002354
Tejun Heob3dc0942014-02-25 10:04:01 -05002355 while (&cset->mg_node != tset->csets) {
2356 if (!task)
2357 task = list_first_entry(&cset->mg_tasks,
2358 struct task_struct, cg_list);
2359 else
2360 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002361
Tejun Heob3dc0942014-02-25 10:04:01 -05002362 if (&task->cg_list != &cset->mg_tasks) {
2363 tset->cur_cset = cset;
2364 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002365
2366 /*
2367 * This function may be called both before and
2368 * after cgroup_taskset_migrate(). The two cases
2369 * can be distinguished by looking at whether @cset
2370 * has its ->mg_dst_cset set.
2371 */
2372 if (cset->mg_dst_cset)
2373 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2374 else
2375 *dst_cssp = cset->subsys[tset->ssid];
2376
Tejun Heob3dc0942014-02-25 10:04:01 -05002377 return task;
2378 }
2379
2380 cset = list_next_entry(cset, mg_node);
2381 task = NULL;
2382 }
2383
2384 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002385}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002386
2387/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002388 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2389 * @tset: taget taskset
2390 * @dst_cgrp: destination cgroup
2391 *
2392 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2393 * ->can_attach callbacks fails and guarantees that either all or none of
2394 * the tasks in @tset are migrated. @tset is consumed regardless of
2395 * success.
2396 */
2397static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2398 struct cgroup *dst_cgrp)
2399{
2400 struct cgroup_subsys_state *css, *failed_css = NULL;
2401 struct task_struct *task, *tmp_task;
2402 struct css_set *cset, *tmp_cset;
2403 int i, ret;
2404
2405 /* methods shouldn't be called if no task is actually migrating */
2406 if (list_empty(&tset->src_csets))
2407 return 0;
2408
2409 /* check that we can legitimately attach to the cgroup */
2410 for_each_e_css(css, i, dst_cgrp) {
2411 if (css->ss->can_attach) {
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002412 tset->ssid = i;
2413 ret = css->ss->can_attach(tset);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002414 if (ret) {
2415 failed_css = css;
2416 goto out_cancel_attach;
2417 }
2418 }
2419 }
2420
2421 /*
2422 * Now that we're guaranteed success, proceed to move all tasks to
2423 * the new cgroup. There are no failure cases after here, so this
2424 * is the commit point.
2425 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002426 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002427 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002428 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2429 struct css_set *from_cset = task_css_set(task);
2430 struct css_set *to_cset = cset->mg_dst_cset;
2431
2432 get_css_set(to_cset);
2433 css_set_move_task(task, from_cset, to_cset, true);
2434 put_css_set_locked(from_cset);
2435 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002436 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002437 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002438
2439 /*
2440 * Migration is committed, all target tasks are now on dst_csets.
2441 * Nothing is sensitive to fork() after this point. Notify
2442 * controllers that migration is complete.
2443 */
2444 tset->csets = &tset->dst_csets;
2445
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002446 for_each_e_css(css, i, dst_cgrp) {
2447 if (css->ss->attach) {
2448 tset->ssid = i;
2449 css->ss->attach(tset);
2450 }
2451 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002452
2453 ret = 0;
2454 goto out_release_tset;
2455
2456out_cancel_attach:
2457 for_each_e_css(css, i, dst_cgrp) {
2458 if (css == failed_css)
2459 break;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002460 if (css->ss->cancel_attach) {
2461 tset->ssid = i;
2462 css->ss->cancel_attach(tset);
2463 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002464 }
2465out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002466 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002467 list_splice_init(&tset->dst_csets, &tset->src_csets);
2468 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2469 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2470 list_del_init(&cset->mg_node);
2471 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002472 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002473 return ret;
2474}
2475
2476/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002477 * cgroup_migrate_finish - cleanup after attach
2478 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002479 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002480 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2481 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002482 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002483static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002484{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002485 struct css_set *cset, *tmp_cset;
2486
2487 lockdep_assert_held(&cgroup_mutex);
2488
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002489 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002490 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2491 cset->mg_src_cgrp = NULL;
2492 cset->mg_dst_cset = NULL;
2493 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002494 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002495 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002496 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002497}
2498
2499/**
2500 * cgroup_migrate_add_src - add a migration source css_set
2501 * @src_cset: the source css_set to add
2502 * @dst_cgrp: the destination cgroup
2503 * @preloaded_csets: list of preloaded css_sets
2504 *
2505 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2506 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2507 * up by cgroup_migrate_finish().
2508 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002509 * This function may be called without holding cgroup_threadgroup_rwsem
2510 * even if the target is a process. Threads may be created and destroyed
2511 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2512 * into play and the preloaded css_sets are guaranteed to cover all
2513 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002514 */
2515static void cgroup_migrate_add_src(struct css_set *src_cset,
2516 struct cgroup *dst_cgrp,
2517 struct list_head *preloaded_csets)
2518{
2519 struct cgroup *src_cgrp;
2520
2521 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002522 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002523
2524 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2525
Tejun Heo1958d2d2014-02-25 10:04:03 -05002526 if (!list_empty(&src_cset->mg_preload_node))
2527 return;
2528
2529 WARN_ON(src_cset->mg_src_cgrp);
2530 WARN_ON(!list_empty(&src_cset->mg_tasks));
2531 WARN_ON(!list_empty(&src_cset->mg_node));
2532
2533 src_cset->mg_src_cgrp = src_cgrp;
2534 get_css_set(src_cset);
2535 list_add(&src_cset->mg_preload_node, preloaded_csets);
2536}
2537
2538/**
2539 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002540 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002541 * @preloaded_csets: list of preloaded source css_sets
2542 *
2543 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2544 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002545 * pins all destination css_sets, links each to its source, and append them
2546 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2547 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002548 *
2549 * This function must be called after cgroup_migrate_add_src() has been
2550 * called on each migration source css_set. After migration is performed
2551 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2552 * @preloaded_csets.
2553 */
2554static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2555 struct list_head *preloaded_csets)
2556{
2557 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002558 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002559
2560 lockdep_assert_held(&cgroup_mutex);
2561
Tejun Heof8f22e52014-04-23 11:13:16 -04002562 /*
Tejun Heo62716ea2016-02-23 10:00:51 -05002563 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002564 * with tasks so that child cgroups don't compete against tasks.
2565 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002566 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heo62716ea2016-02-23 10:00:51 -05002567 dst_cgrp->subtree_control)
Tejun Heof8f22e52014-04-23 11:13:16 -04002568 return -EBUSY;
2569
Tejun Heo1958d2d2014-02-25 10:04:03 -05002570 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002571 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002572 struct css_set *dst_cset;
2573
Tejun Heof817de92014-04-23 11:13:16 -04002574 dst_cset = find_css_set(src_cset,
2575 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002576 if (!dst_cset)
2577 goto err;
2578
2579 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002580
2581 /*
2582 * If src cset equals dst, it's noop. Drop the src.
2583 * cgroup_migrate() will skip the cset too. Note that we
2584 * can't handle src == dst as some nodes are used by both.
2585 */
2586 if (src_cset == dst_cset) {
2587 src_cset->mg_src_cgrp = NULL;
2588 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002589 put_css_set(src_cset);
2590 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002591 continue;
2592 }
2593
Tejun Heo1958d2d2014-02-25 10:04:03 -05002594 src_cset->mg_dst_cset = dst_cset;
2595
2596 if (list_empty(&dst_cset->mg_preload_node))
2597 list_add(&dst_cset->mg_preload_node, &csets);
2598 else
Zefan Lia25eb522014-09-19 16:51:00 +08002599 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002600 }
2601
Tejun Heof817de92014-04-23 11:13:16 -04002602 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002603 return 0;
2604err:
2605 cgroup_migrate_finish(&csets);
2606 return -ENOMEM;
2607}
2608
2609/**
2610 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002611 * @leader: the leader of the process or the task to migrate
2612 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002613 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002614 *
2615 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002616 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002617 * caller is also responsible for invoking cgroup_migrate_add_src() and
2618 * cgroup_migrate_prepare_dst() on the targets before invoking this
2619 * function and following up with cgroup_migrate_finish().
2620 *
2621 * As long as a controller's ->can_attach() doesn't fail, this function is
2622 * guaranteed to succeed. This means that, excluding ->can_attach()
2623 * failure, when migrating multiple targets, the success or failure can be
2624 * decided for all targets by invoking group_migrate_prepare_dst() before
2625 * actually starting migrating.
2626 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002627static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2628 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002629{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002630 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2631 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002632
2633 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002634 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2635 * already PF_EXITING could be freed from underneath us unless we
2636 * take an rcu_read_lock.
2637 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002638 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002639 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002640 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002641 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002642 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002643 if (!threadgroup)
2644 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002645 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002646 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002647 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002648
Tejun Heoadaae5d2015-09-11 15:00:21 -04002649 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002650}
2651
Tejun Heo1958d2d2014-02-25 10:04:03 -05002652/**
2653 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2654 * @dst_cgrp: the cgroup to attach to
2655 * @leader: the task or the leader of the threadgroup to be attached
2656 * @threadgroup: attach the whole threadgroup?
2657 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002658 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002659 */
2660static int cgroup_attach_task(struct cgroup *dst_cgrp,
2661 struct task_struct *leader, bool threadgroup)
2662{
2663 LIST_HEAD(preloaded_csets);
2664 struct task_struct *task;
2665 int ret;
2666
2667 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002668 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002669 rcu_read_lock();
2670 task = leader;
2671 do {
2672 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2673 &preloaded_csets);
2674 if (!threadgroup)
2675 break;
2676 } while_each_thread(leader, task);
2677 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002678 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002679
2680 /* prepare dst csets and commit */
2681 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2682 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002683 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002684
2685 cgroup_migrate_finish(&preloaded_csets);
2686 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002687}
2688
Tejun Heo187fe842015-06-18 16:54:28 -04002689static int cgroup_procs_write_permission(struct task_struct *task,
2690 struct cgroup *dst_cgrp,
2691 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002692{
2693 const struct cred *cred = current_cred();
2694 const struct cred *tcred = get_task_cred(task);
2695 int ret = 0;
2696
2697 /*
2698 * even if we're attaching all tasks in the thread group, we only
2699 * need to check permissions on one of them.
2700 */
2701 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2702 !uid_eq(cred->euid, tcred->uid) &&
2703 !uid_eq(cred->euid, tcred->suid))
2704 ret = -EACCES;
2705
Tejun Heo187fe842015-06-18 16:54:28 -04002706 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2707 struct super_block *sb = of->file->f_path.dentry->d_sb;
2708 struct cgroup *cgrp;
2709 struct inode *inode;
2710
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002711 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002712 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002713 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002714
2715 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2716 cgrp = cgroup_parent(cgrp);
2717
2718 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002719 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002720 if (inode) {
2721 ret = inode_permission(inode, MAY_WRITE);
2722 iput(inode);
2723 }
2724 }
2725
Tejun Heodedf22e2015-06-18 16:54:28 -04002726 put_cred(tcred);
2727 return ret;
2728}
2729
Ben Blum74a11662011-05-26 16:25:20 -07002730/*
2731 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002732 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002733 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002734 */
Tejun Heoacbef752014-05-13 12:16:22 -04002735static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2736 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002737{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002738 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002739 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002740 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002741 int ret;
2742
Tejun Heoacbef752014-05-13 12:16:22 -04002743 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2744 return -EINVAL;
2745
Tejun Heoe76ecae2014-05-13 12:19:23 -04002746 cgrp = cgroup_kn_lock_live(of->kn);
2747 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002748 return -ENODEV;
2749
Tejun Heo3014dde2015-09-16 13:03:02 -04002750 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002751 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002752 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002753 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002754 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002755 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002756 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002757 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002758 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002759 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002760 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002761
2762 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002763 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002764
2765 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002766 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002767 * trapped in a cpuset, or RT worker may be born in a cgroup
2768 * with no rt_runtime allocated. Just say no.
2769 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002770 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002771 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002772 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002773 }
2774
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002775 get_task_struct(tsk);
2776 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002777
Tejun Heo187fe842015-06-18 16:54:28 -04002778 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002779 if (!ret)
2780 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002781
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002782 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002783 goto out_unlock_threadgroup;
2784
2785out_unlock_rcu:
2786 rcu_read_unlock();
2787out_unlock_threadgroup:
2788 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002789 cgroup_kn_unlock(of->kn);
Tejun Heoe93ad192016-01-19 12:18:41 -05002790 cpuset_post_attach_flush();
Tejun Heoacbef752014-05-13 12:16:22 -04002791 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002792}
2793
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002794/**
2795 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2796 * @from: attach to all cgroups of a given task
2797 * @tsk: the task to be attached
2798 */
2799int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2800{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002801 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002802 int retval = 0;
2803
Tejun Heo47cfcd02013-04-07 09:29:51 -07002804 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002805 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002806 struct cgroup *from_cgrp;
2807
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002808 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002809 continue;
2810
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002811 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002812 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002813 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002814
Li Zefan6f4b7e62013-07-31 16:18:36 +08002815 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002816 if (retval)
2817 break;
2818 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002819 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002820
2821 return retval;
2822}
2823EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2824
Tejun Heoacbef752014-05-13 12:16:22 -04002825static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2826 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002827{
Tejun Heoacbef752014-05-13 12:16:22 -04002828 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002829}
2830
Tejun Heoacbef752014-05-13 12:16:22 -04002831static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2832 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002833{
Tejun Heoacbef752014-05-13 12:16:22 -04002834 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002835}
2836
Tejun Heo451af502014-05-13 12:16:21 -04002837static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2838 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002839{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002840 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002841
Tejun Heoe76ecae2014-05-13 12:19:23 -04002842 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2843
2844 cgrp = cgroup_kn_lock_live(of->kn);
2845 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002846 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002847 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002848 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2849 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002850 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002851 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002852 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002853}
2854
Tejun Heo2da8ca82013-12-05 12:28:04 -05002855static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002856{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002857 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002858
Tejun Heo46cfeb02014-05-13 12:11:00 -04002859 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002860 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002861 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002862 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002863 return 0;
2864}
2865
Tejun Heo2da8ca82013-12-05 12:28:04 -05002866static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002867{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002868 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002869 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002870}
2871
Tejun Heo6e5c8302016-02-22 22:25:47 -05002872static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002873{
2874 struct cgroup_subsys *ss;
2875 bool printed = false;
2876 int ssid;
2877
Tejun Heob4e0eea2016-02-22 22:25:46 -05002878 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002879 if (printed)
2880 seq_putc(seq, ' ');
2881 seq_printf(seq, "%s", ss->name);
2882 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002883 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002884 if (printed)
2885 seq_putc(seq, '\n');
2886}
2887
Tejun Heof8f22e52014-04-23 11:13:16 -04002888/* show controllers which are enabled from the parent */
2889static int cgroup_controllers_show(struct seq_file *seq, void *v)
2890{
2891 struct cgroup *cgrp = seq_css(seq)->cgroup;
2892
Tejun Heo5531dc92016-03-03 09:57:58 -05002893 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04002894 return 0;
2895}
2896
2897/* show controllers which are enabled for a given cgroup's children */
2898static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2899{
2900 struct cgroup *cgrp = seq_css(seq)->cgroup;
2901
Tejun Heo667c2492014-07-08 18:02:56 -04002902 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002903 return 0;
2904}
2905
2906/**
2907 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2908 * @cgrp: root of the subtree to update csses for
2909 *
Tejun Heo8699b772016-02-22 22:25:46 -05002910 * @cgrp's subtree_ss_mask has changed and its subtree's (self excluded)
Tejun Heof8f22e52014-04-23 11:13:16 -04002911 * css associations need to be updated accordingly. This function looks up
2912 * all css_sets which are attached to the subtree, creates the matching
2913 * updated css_sets and migrates the tasks to the new ones.
2914 */
2915static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2916{
2917 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002918 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002919 struct cgroup_subsys_state *css;
2920 struct css_set *src_cset;
2921 int ret;
2922
Tejun Heof8f22e52014-04-23 11:13:16 -04002923 lockdep_assert_held(&cgroup_mutex);
2924
Tejun Heo3014dde2015-09-16 13:03:02 -04002925 percpu_down_write(&cgroup_threadgroup_rwsem);
2926
Tejun Heof8f22e52014-04-23 11:13:16 -04002927 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002928 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002929 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2930 struct cgrp_cset_link *link;
2931
Tejun Heo8699b772016-02-22 22:25:46 -05002932 /* self is not affected by subtree_ss_mask change */
Tejun Heof8f22e52014-04-23 11:13:16 -04002933 if (css->cgroup == cgrp)
2934 continue;
2935
2936 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2937 cgroup_migrate_add_src(link->cset, cgrp,
2938 &preloaded_csets);
2939 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002940 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002941
2942 /* NULL dst indicates self on default hierarchy */
2943 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2944 if (ret)
2945 goto out_finish;
2946
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002947 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002948 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002949 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002950
2951 /* src_csets precede dst_csets, break on the first dst_cset */
2952 if (!src_cset->mg_src_cgrp)
2953 break;
2954
Tejun Heo10265072015-09-11 15:00:22 -04002955 /* all tasks in src_csets need to be migrated */
2956 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2957 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002958 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002959 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002960
Tejun Heo10265072015-09-11 15:00:22 -04002961 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002962out_finish:
2963 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002964 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002965 return ret;
2966}
2967
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002968/**
2969 * cgroup_drain_offline - wait for previously offlined csses to go away
2970 * @cgrp: parent of the target cgroups
2971 *
2972 * Because css offlining is asynchronous, userland may try to re-enable a
2973 * controller while the previous css is still around. This function drains
2974 * the previous css instances of @cgrp's children.
2975 *
2976 * Must be called with cgroup_mutex held. Returns %false if there were no
2977 * dying css instances. Returns %true if there were one or more and this
2978 * function waited. On %true return, cgroup_mutex has been dropped and
2979 * re-acquired inbetween which anything could have happened. The caller
2980 * typically would have to start over.
2981 */
2982static bool cgroup_drain_offline(struct cgroup *cgrp)
2983{
2984 struct cgroup *dsct;
2985 struct cgroup_subsys *ss;
2986 int ssid;
2987
2988 lockdep_assert_held(&cgroup_mutex);
2989
2990 cgroup_for_each_live_child(dsct, cgrp) {
2991 for_each_subsys(ss, ssid) {
2992 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2993 DEFINE_WAIT(wait);
2994
2995 if (!css)
2996 continue;
2997
2998 cgroup_get(dsct);
2999 prepare_to_wait(&dsct->offline_waitq, &wait,
3000 TASK_UNINTERRUPTIBLE);
3001
3002 mutex_unlock(&cgroup_mutex);
3003 schedule();
3004 finish_wait(&dsct->offline_waitq, &wait);
3005 mutex_lock(&cgroup_mutex);
3006
3007 cgroup_put(dsct);
3008 return true;
3009 }
3010 }
3011
3012 return false;
3013}
3014
Tejun Heof8f22e52014-04-23 11:13:16 -04003015/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003016static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3017 char *buf, size_t nbytes,
3018 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003019{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003020 u16 enable = 0, disable = 0;
3021 u16 css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04003022 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003023 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003024 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003025 int ssid, ret;
3026
3027 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003028 * Parse input - space separated list of subsystem names prefixed
3029 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003030 */
Tejun Heo451af502014-05-13 12:16:21 -04003031 buf = strstrip(buf);
3032 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003033 if (tok[0] == '\0')
3034 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003035 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003036 if (!cgroup_ssid_enabled(ssid) ||
3037 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003038 continue;
3039
3040 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003041 enable |= 1 << ssid;
3042 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003043 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003044 disable |= 1 << ssid;
3045 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003046 } else {
3047 return -EINVAL;
3048 }
3049 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003050 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003051 if (ssid == CGROUP_SUBSYS_COUNT)
3052 return -EINVAL;
3053 }
3054
Tejun Heoa9746d82014-05-13 12:19:22 -04003055 cgrp = cgroup_kn_lock_live(of->kn);
3056 if (!cgrp)
3057 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003058
3059 for_each_subsys(ss, ssid) {
3060 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003061 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003062 enable &= ~(1 << ssid);
3063 continue;
3064 }
3065
Tejun Heo5531dc92016-03-03 09:57:58 -05003066 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003067 ret = -ENOENT;
3068 goto out_unlock;
3069 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003070 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003071 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003072 disable &= ~(1 << ssid);
3073 continue;
3074 }
3075
3076 /* a child has it enabled? */
3077 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003078 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003079 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003080 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003081 }
3082 }
3083 }
3084 }
3085
3086 if (!enable && !disable) {
3087 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003088 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003089 }
3090
3091 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003092 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003093 * with tasks so that child cgroups don't compete against tasks.
3094 */
Tejun Heod51f39b2014-05-16 13:22:48 -04003095 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003096 ret = -EBUSY;
3097 goto out_unlock;
3098 }
3099
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003100 if (cgroup_drain_offline(cgrp)) {
3101 cgroup_kn_unlock(of->kn);
3102 return restart_syscall();
3103 }
3104
Tejun Heof8f22e52014-04-23 11:13:16 -04003105 /*
Tejun Heof63070d2014-07-08 18:02:57 -04003106 * Update subsys masks and calculate what needs to be done. More
3107 * subsystems than specified may need to be enabled or disabled
3108 * depending on subsystem dependencies.
3109 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05003110 old_sc = cgrp->subtree_control;
Tejun Heo8699b772016-02-22 22:25:46 -05003111 old_ss = cgrp->subtree_ss_mask;
Tejun Heo755bf5e2014-11-18 02:49:50 -05003112 new_sc = (old_sc | enable) & ~disable;
Tejun Heo8699b772016-02-22 22:25:46 -05003113 new_ss = cgroup_calc_subtree_ss_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04003114
Tejun Heo755bf5e2014-11-18 02:49:50 -05003115 css_enable = ~old_ss & new_ss;
3116 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04003117 enable |= css_enable;
3118 disable |= css_disable;
3119
Tejun Heo755bf5e2014-11-18 02:49:50 -05003120 cgrp->subtree_control = new_sc;
Tejun Heo8699b772016-02-22 22:25:46 -05003121 cgrp->subtree_ss_mask = new_ss;
Tejun Heo755bf5e2014-11-18 02:49:50 -05003122
Tejun Heof63070d2014-07-08 18:02:57 -04003123 /*
3124 * Create new csses or make the existing ones visible. A css is
3125 * created invisible if it's being implicitly enabled through
3126 * dependency. An invisible css is made visible when the userland
3127 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04003128 */
Tejun Heo996cd1f2016-02-22 22:25:46 -05003129 do_each_subsys_mask(ss, ssid, enable) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003130 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05003131 if (css_enable & (1 << ssid)) {
3132 struct cgroup_subsys_state *css;
3133
3134 css = css_create(child, ss);
3135 if (IS_ERR(css)) {
3136 ret = PTR_ERR(css);
3137 goto err_undo_css;
3138 }
3139
3140 if (cgrp->subtree_control & (1 << ssid)) {
3141 ret = css_populate_dir(css, NULL);
3142 if (ret)
3143 goto err_undo_css;
3144 }
3145 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003146 ret = css_populate_dir(cgroup_css(child, ss),
3147 NULL);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05003148 if (ret)
3149 goto err_undo_css;
3150 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003151 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05003152 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003153
Tejun Heoc29adf22014-07-08 18:02:56 -04003154 /*
3155 * At this point, cgroup_e_css() results reflect the new csses
3156 * making the following cgroup_update_dfl_csses() properly update
3157 * css associations of all tasks in the subtree.
3158 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003159 ret = cgroup_update_dfl_csses(cgrp);
3160 if (ret)
3161 goto err_undo_css;
3162
Tejun Heof63070d2014-07-08 18:02:57 -04003163 /*
3164 * All tasks are migrated out of disabled csses. Kill or hide
3165 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003166 * disabled while other subsystems are still depending on it. The
3167 * css must not actively control resources and be in the vanilla
3168 * state if it's made visible again later. Controllers which may
3169 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04003170 */
Tejun Heo996cd1f2016-02-22 22:25:46 -05003171 do_each_subsys_mask(ss, ssid, disable) {
Tejun Heof63070d2014-07-08 18:02:57 -04003172 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003173 struct cgroup_subsys_state *css = cgroup_css(child, ss);
3174
3175 if (css_disable & (1 << ssid)) {
3176 kill_css(css);
3177 } else {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003178 css_clear_dir(css, NULL);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04003179 if (ss->css_reset)
3180 ss->css_reset(css);
3181 }
Tejun Heof63070d2014-07-08 18:02:57 -04003182 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05003183 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003184
3185 kernfs_activate(cgrp->kn);
3186 ret = 0;
3187out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003188 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003189 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003190
3191err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05003192 cgrp->subtree_control = old_sc;
Tejun Heo8699b772016-02-22 22:25:46 -05003193 cgrp->subtree_ss_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04003194
Tejun Heo996cd1f2016-02-22 22:25:46 -05003195 do_each_subsys_mask(ss, ssid, enable) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003196 cgroup_for_each_live_child(child, cgrp) {
3197 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04003198
3199 if (!css)
3200 continue;
3201
3202 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003203 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003204 else
Tejun Heo4df8dc92015-09-18 17:54:23 -04003205 css_clear_dir(css, NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04003206 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05003207 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003208 goto out_unlock;
3209}
3210
Tejun Heo4a07c222015-09-18 17:54:22 -04003211static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003212{
Tejun Heo4a07c222015-09-18 17:54:22 -04003213 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003214 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003215 return 0;
3216}
3217
Tejun Heo2bd59d42014-02-11 11:52:49 -05003218static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3219 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003220{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003221 struct cgroup *cgrp = of->kn->parent->priv;
3222 struct cftype *cft = of->kn->priv;
3223 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003224 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003225
Tejun Heob4168642014-05-13 12:16:21 -04003226 if (cft->write)
3227 return cft->write(of, buf, nbytes, off);
3228
Tejun Heo2bd59d42014-02-11 11:52:49 -05003229 /*
3230 * kernfs guarantees that a file isn't deleted with operations in
3231 * flight, which means that the matching css is and stays alive and
3232 * doesn't need to be pinned. The RCU locking is not necessary
3233 * either. It's just for the convenience of using cgroup_css().
3234 */
3235 rcu_read_lock();
3236 css = cgroup_css(cgrp, cft->ss);
3237 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003238
Tejun Heo451af502014-05-13 12:16:21 -04003239 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003240 unsigned long long v;
3241 ret = kstrtoull(buf, 0, &v);
3242 if (!ret)
3243 ret = cft->write_u64(css, cft, v);
3244 } else if (cft->write_s64) {
3245 long long v;
3246 ret = kstrtoll(buf, 0, &v);
3247 if (!ret)
3248 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003249 } else {
3250 ret = -EINVAL;
3251 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003252
Tejun Heoa742c592013-12-05 12:28:03 -05003253 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003254}
3255
Tejun Heo6612f052013-12-05 12:28:04 -05003256static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003257{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003258 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003259}
3260
3261static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3262{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003263 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003264}
3265
3266static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3267{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003268 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003269}
3270
3271static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3272{
Tejun Heo7da11272013-12-05 12:28:04 -05003273 struct cftype *cft = seq_cft(m);
3274 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003275
Tejun Heo2da8ca82013-12-05 12:28:04 -05003276 if (cft->seq_show)
3277 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003278
Tejun Heo896f5192013-12-05 12:28:04 -05003279 if (cft->read_u64)
3280 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3281 else if (cft->read_s64)
3282 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3283 else
3284 return -EINVAL;
3285 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003286}
3287
Tejun Heo2bd59d42014-02-11 11:52:49 -05003288static struct kernfs_ops cgroup_kf_single_ops = {
3289 .atomic_write_len = PAGE_SIZE,
3290 .write = cgroup_file_write,
3291 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003292};
3293
Tejun Heo2bd59d42014-02-11 11:52:49 -05003294static struct kernfs_ops cgroup_kf_ops = {
3295 .atomic_write_len = PAGE_SIZE,
3296 .write = cgroup_file_write,
3297 .seq_start = cgroup_seqfile_start,
3298 .seq_next = cgroup_seqfile_next,
3299 .seq_stop = cgroup_seqfile_stop,
3300 .seq_show = cgroup_seqfile_show,
3301};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003302
3303/*
3304 * cgroup_rename - Only allow simple rename of directories in place.
3305 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003306static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3307 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003308{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003309 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003310 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003311
Tejun Heo2bd59d42014-02-11 11:52:49 -05003312 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003313 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003314 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003315 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003316
Tejun Heo6db8e852013-06-14 11:18:22 -07003317 /*
3318 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003319 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003320 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003321 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003322 return -EPERM;
3323
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003324 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003325 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003326 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003327 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003328 */
3329 kernfs_break_active_protection(new_parent);
3330 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003331
Tejun Heo2bd59d42014-02-11 11:52:49 -05003332 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003333
Tejun Heo2bd59d42014-02-11 11:52:49 -05003334 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003335
Tejun Heo2bd59d42014-02-11 11:52:49 -05003336 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003337
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003338 kernfs_unbreak_active_protection(kn);
3339 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003340 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003341}
3342
Tejun Heo49957f82014-04-07 16:44:47 -04003343/* set uid and gid of cgroup dirs and files to that of the creator */
3344static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3345{
3346 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3347 .ia_uid = current_fsuid(),
3348 .ia_gid = current_fsgid(), };
3349
3350 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3351 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3352 return 0;
3353
3354 return kernfs_setattr(kn, &iattr);
3355}
3356
Tejun Heo4df8dc92015-09-18 17:54:23 -04003357static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3358 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003359{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003360 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003361 struct kernfs_node *kn;
3362 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003363 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003364
Tejun Heo2bd59d42014-02-11 11:52:49 -05003365#ifdef CONFIG_DEBUG_LOCK_ALLOC
3366 key = &cft->lockdep_key;
3367#endif
3368 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3369 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003370 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003371 if (IS_ERR(kn))
3372 return PTR_ERR(kn);
3373
3374 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003375 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003376 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003377 return ret;
3378 }
3379
Tejun Heo6f60ead2015-09-18 17:54:23 -04003380 if (cft->file_offset) {
3381 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3382
Tejun Heo34c06252015-11-05 00:12:24 -05003383 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003384 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003385 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003386 }
3387
Tejun Heof8f22e52014-04-23 11:13:16 -04003388 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003389}
3390
Tejun Heob1f28d32013-06-28 16:24:10 -07003391/**
3392 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003393 * @css: the target css
3394 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003395 * @cfts: array of cftypes to be added
3396 * @is_add: whether to add or remove
3397 *
3398 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003399 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003400 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003401static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3402 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003403 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003404{
Tejun Heo6732ed82015-09-18 17:54:23 -04003405 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003406 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003407
Tejun Heo01f64742014-05-13 12:19:23 -04003408 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003409
Tejun Heo6732ed82015-09-18 17:54:23 -04003410restart:
3411 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003412 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003413 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003414 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003415 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003416 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003417 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003418 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003419 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003420 continue;
3421
Li Zefan2739d3c2013-01-21 18:18:33 +08003422 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003423 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003424 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003425 pr_warn("%s: failed to add %s, err=%d\n",
3426 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003427 cft_end = cft;
3428 is_add = false;
3429 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003430 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003431 } else {
3432 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003433 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003434 }
Tejun Heob598dde2016-02-22 22:25:45 -05003435 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003436}
3437
Tejun Heo21a2d342014-02-12 09:29:49 -05003438static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003439{
3440 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003441 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003442 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003443 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003444 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003445
Tejun Heo01f64742014-05-13 12:19:23 -04003446 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003447
Li Zefane8c82d22013-06-18 18:48:37 +08003448 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003449 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003450 struct cgroup *cgrp = css->cgroup;
3451
Tejun Heo88cb04b2016-03-03 09:57:58 -05003452 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08003453 continue;
3454
Tejun Heo4df8dc92015-09-18 17:54:23 -04003455 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003456 if (ret)
3457 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003458 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003459
3460 if (is_add && !ret)
3461 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003462 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003463}
3464
Tejun Heo2da440a2014-02-11 11:52:48 -05003465static void cgroup_exit_cftypes(struct cftype *cfts)
3466{
3467 struct cftype *cft;
3468
Tejun Heo2bd59d42014-02-11 11:52:49 -05003469 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3470 /* free copy for custom atomic_write_len, see init_cftypes() */
3471 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3472 kfree(cft->kf_ops);
3473 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003474 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003475
3476 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003477 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003478 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003479}
3480
Tejun Heo2bd59d42014-02-11 11:52:49 -05003481static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003482{
3483 struct cftype *cft;
3484
Tejun Heo2bd59d42014-02-11 11:52:49 -05003485 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3486 struct kernfs_ops *kf_ops;
3487
Tejun Heo0adb0702014-02-12 09:29:48 -05003488 WARN_ON(cft->ss || cft->kf_ops);
3489
Tejun Heo2bd59d42014-02-11 11:52:49 -05003490 if (cft->seq_start)
3491 kf_ops = &cgroup_kf_ops;
3492 else
3493 kf_ops = &cgroup_kf_single_ops;
3494
3495 /*
3496 * Ugh... if @cft wants a custom max_write_len, we need to
3497 * make a copy of kf_ops to set its atomic_write_len.
3498 */
3499 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3500 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3501 if (!kf_ops) {
3502 cgroup_exit_cftypes(cfts);
3503 return -ENOMEM;
3504 }
3505 kf_ops->atomic_write_len = cft->max_write_len;
3506 }
3507
3508 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003509 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003510 }
3511
3512 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003513}
3514
Tejun Heo21a2d342014-02-12 09:29:49 -05003515static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3516{
Tejun Heo01f64742014-05-13 12:19:23 -04003517 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003518
3519 if (!cfts || !cfts[0].ss)
3520 return -ENOENT;
3521
3522 list_del(&cfts->node);
3523 cgroup_apply_cftypes(cfts, false);
3524 cgroup_exit_cftypes(cfts);
3525 return 0;
3526}
3527
Tejun Heo8e3f6542012-04-01 12:09:55 -07003528/**
Tejun Heo80b13582014-02-12 09:29:48 -05003529 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3530 * @cfts: zero-length name terminated array of cftypes
3531 *
3532 * Unregister @cfts. Files described by @cfts are removed from all
3533 * existing cgroups and all future cgroups won't have them either. This
3534 * function can be called anytime whether @cfts' subsys is attached or not.
3535 *
3536 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3537 * registered.
3538 */
3539int cgroup_rm_cftypes(struct cftype *cfts)
3540{
Tejun Heo21a2d342014-02-12 09:29:49 -05003541 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003542
Tejun Heo01f64742014-05-13 12:19:23 -04003543 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003544 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003545 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003546 return ret;
3547}
3548
3549/**
3550 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3551 * @ss: target cgroup subsystem
3552 * @cfts: zero-length name terminated array of cftypes
3553 *
3554 * Register @cfts to @ss. Files described by @cfts are created for all
3555 * existing cgroups to which @ss is attached and all future cgroups will
3556 * have them too. This function can be called anytime whether @ss is
3557 * attached or not.
3558 *
3559 * Returns 0 on successful registration, -errno on failure. Note that this
3560 * function currently returns 0 as long as @cfts registration is successful
3561 * even if some file creation attempts on existing cgroups fail.
3562 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003563static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003564{
Tejun Heo9ccece82013-06-28 16:24:11 -07003565 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003566
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003567 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003568 return 0;
3569
Li Zefandc5736e2014-02-17 10:41:50 +08003570 if (!cfts || cfts[0].name[0] == '\0')
3571 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003572
Tejun Heo2bd59d42014-02-11 11:52:49 -05003573 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003574 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003575 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003576
Tejun Heo01f64742014-05-13 12:19:23 -04003577 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003578
Tejun Heo0adb0702014-02-12 09:29:48 -05003579 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003580 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003581 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003582 cgroup_rm_cftypes_locked(cfts);
3583
Tejun Heo01f64742014-05-13 12:19:23 -04003584 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003585 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003586}
Tejun Heo79578622012-04-01 12:09:56 -07003587
3588/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003589 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3590 * @ss: target cgroup subsystem
3591 * @cfts: zero-length name terminated array of cftypes
3592 *
3593 * Similar to cgroup_add_cftypes() but the added files are only used for
3594 * the default hierarchy.
3595 */
3596int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3597{
3598 struct cftype *cft;
3599
3600 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003601 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003602 return cgroup_add_cftypes(ss, cfts);
3603}
3604
3605/**
3606 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3607 * @ss: target cgroup subsystem
3608 * @cfts: zero-length name terminated array of cftypes
3609 *
3610 * Similar to cgroup_add_cftypes() but the added files are only used for
3611 * the legacy hierarchies.
3612 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003613int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3614{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003615 struct cftype *cft;
3616
Tejun Heoe4b70372015-10-15 17:00:43 -04003617 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3618 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003619 return cgroup_add_cftypes(ss, cfts);
3620}
3621
Li Zefana043e3b2008-02-23 15:24:09 -08003622/**
Tejun Heo34c06252015-11-05 00:12:24 -05003623 * cgroup_file_notify - generate a file modified event for a cgroup_file
3624 * @cfile: target cgroup_file
3625 *
3626 * @cfile must have been obtained by setting cftype->file_offset.
3627 */
3628void cgroup_file_notify(struct cgroup_file *cfile)
3629{
3630 unsigned long flags;
3631
3632 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3633 if (cfile->kn)
3634 kernfs_notify(cfile->kn);
3635 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3636}
3637
3638/**
Li Zefana043e3b2008-02-23 15:24:09 -08003639 * cgroup_task_count - count the number of tasks in a cgroup.
3640 * @cgrp: the cgroup in question
3641 *
3642 * Return the number of tasks in the cgroup.
3643 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003644static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003645{
3646 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003647 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003648
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003649 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003650 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3651 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003652 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003653 return count;
3654}
3655
Tejun Heo574bd9f2012-11-09 09:12:29 -08003656/**
Tejun Heo492eb212013-08-08 20:11:25 -04003657 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003658 * @pos: the current position (%NULL to initiate traversal)
3659 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003660 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003661 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003662 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003663 * that @parent and @pos are accessible. The next sibling is guaranteed to
3664 * be returned regardless of their states.
3665 *
3666 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3667 * css which finished ->css_online() is guaranteed to be visible in the
3668 * future iterations and will stay visible until the last reference is put.
3669 * A css which hasn't finished ->css_online() or already finished
3670 * ->css_offline() may show up during traversal. It's each subsystem's
3671 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003672 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003673struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3674 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003675{
Tejun Heoc2931b72014-05-16 13:22:51 -04003676 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003677
Tejun Heo8353da12014-05-13 12:19:23 -04003678 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003679
3680 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003681 * @pos could already have been unlinked from the sibling list.
3682 * Once a cgroup is removed, its ->sibling.next is no longer
3683 * updated when its next sibling changes. CSS_RELEASED is set when
3684 * @pos is taken off list, at which time its next pointer is valid,
3685 * and, as releases are serialized, the one pointed to by the next
3686 * pointer is guaranteed to not have started release yet. This
3687 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3688 * critical section, the one pointed to by its next pointer is
3689 * guaranteed to not have finished its RCU grace period even if we
3690 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003691 *
Tejun Heode3f0342014-05-16 13:22:49 -04003692 * If @pos has CSS_RELEASED set, its next pointer can't be
3693 * dereferenced; however, as each css is given a monotonically
3694 * increasing unique serial number and always appended to the
3695 * sibling list, the next one can be found by walking the parent's
3696 * children until the first css with higher serial number than
3697 * @pos's. While this path can be slower, it happens iff iteration
3698 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003699 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003700 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003701 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3702 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3703 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003704 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003705 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003706 if (next->serial_nr > pos->serial_nr)
3707 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003708 }
3709
Tejun Heo3b281af2014-04-23 11:13:15 -04003710 /*
3711 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003712 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003713 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003714 if (&next->sibling != &parent->children)
3715 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003716 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003717}
Tejun Heo53fa5262013-05-24 10:55:38 +09003718
3719/**
Tejun Heo492eb212013-08-08 20:11:25 -04003720 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003721 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003722 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003723 *
Tejun Heo492eb212013-08-08 20:11:25 -04003724 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003725 * to visit for pre-order traversal of @root's descendants. @root is
3726 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003727 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003728 * While this function requires cgroup_mutex or RCU read locking, it
3729 * doesn't require the whole traversal to be contained in a single critical
3730 * section. This function will return the correct next descendant as long
3731 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003732 *
3733 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3734 * css which finished ->css_online() is guaranteed to be visible in the
3735 * future iterations and will stay visible until the last reference is put.
3736 * A css which hasn't finished ->css_online() or already finished
3737 * ->css_offline() may show up during traversal. It's each subsystem's
3738 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003739 */
Tejun Heo492eb212013-08-08 20:11:25 -04003740struct cgroup_subsys_state *
3741css_next_descendant_pre(struct cgroup_subsys_state *pos,
3742 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003743{
Tejun Heo492eb212013-08-08 20:11:25 -04003744 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003745
Tejun Heo8353da12014-05-13 12:19:23 -04003746 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003747
Tejun Heobd8815a2013-08-08 20:11:27 -04003748 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003749 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003750 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003751
3752 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003753 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003754 if (next)
3755 return next;
3756
3757 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003758 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003759 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003760 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003761 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003762 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003763 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003764
3765 return NULL;
3766}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003767
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003768/**
Tejun Heo492eb212013-08-08 20:11:25 -04003769 * css_rightmost_descendant - return the rightmost descendant of a css
3770 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003771 *
Tejun Heo492eb212013-08-08 20:11:25 -04003772 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3773 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003774 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003775 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003776 * While this function requires cgroup_mutex or RCU read locking, it
3777 * doesn't require the whole traversal to be contained in a single critical
3778 * section. This function will return the correct rightmost descendant as
3779 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003780 */
Tejun Heo492eb212013-08-08 20:11:25 -04003781struct cgroup_subsys_state *
3782css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003783{
Tejun Heo492eb212013-08-08 20:11:25 -04003784 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003785
Tejun Heo8353da12014-05-13 12:19:23 -04003786 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003787
3788 do {
3789 last = pos;
3790 /* ->prev isn't RCU safe, walk ->next till the end */
3791 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003792 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003793 pos = tmp;
3794 } while (pos);
3795
3796 return last;
3797}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003798
Tejun Heo492eb212013-08-08 20:11:25 -04003799static struct cgroup_subsys_state *
3800css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003801{
Tejun Heo492eb212013-08-08 20:11:25 -04003802 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003803
3804 do {
3805 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003806 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003807 } while (pos);
3808
3809 return last;
3810}
3811
3812/**
Tejun Heo492eb212013-08-08 20:11:25 -04003813 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003814 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003815 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003816 *
Tejun Heo492eb212013-08-08 20:11:25 -04003817 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003818 * to visit for post-order traversal of @root's descendants. @root is
3819 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003820 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003821 * While this function requires cgroup_mutex or RCU read locking, it
3822 * doesn't require the whole traversal to be contained in a single critical
3823 * section. This function will return the correct next descendant as long
3824 * as both @pos and @cgroup are accessible and @pos is a descendant of
3825 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003826 *
3827 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3828 * css which finished ->css_online() is guaranteed to be visible in the
3829 * future iterations and will stay visible until the last reference is put.
3830 * A css which hasn't finished ->css_online() or already finished
3831 * ->css_offline() may show up during traversal. It's each subsystem's
3832 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003833 */
Tejun Heo492eb212013-08-08 20:11:25 -04003834struct cgroup_subsys_state *
3835css_next_descendant_post(struct cgroup_subsys_state *pos,
3836 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003837{
Tejun Heo492eb212013-08-08 20:11:25 -04003838 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003839
Tejun Heo8353da12014-05-13 12:19:23 -04003840 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003841
Tejun Heo58b79a92013-09-06 15:31:08 -04003842 /* if first iteration, visit leftmost descendant which may be @root */
3843 if (!pos)
3844 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003845
Tejun Heobd8815a2013-08-08 20:11:27 -04003846 /* if we visited @root, we're done */
3847 if (pos == root)
3848 return NULL;
3849
Tejun Heo574bd9f2012-11-09 09:12:29 -08003850 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003851 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003852 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003853 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003854
3855 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003856 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003857}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003858
Tejun Heof3d46502014-05-16 13:22:52 -04003859/**
3860 * css_has_online_children - does a css have online children
3861 * @css: the target css
3862 *
3863 * Returns %true if @css has any online children; otherwise, %false. This
3864 * function can be called from any context but the caller is responsible
3865 * for synchronizing against on/offlining as necessary.
3866 */
3867bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003868{
Tejun Heof3d46502014-05-16 13:22:52 -04003869 struct cgroup_subsys_state *child;
3870 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003871
3872 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003873 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003874 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003875 ret = true;
3876 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003877 }
3878 }
3879 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003880 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003881}
3882
Tejun Heo0942eee2013-08-08 20:11:26 -04003883/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003884 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003885 * @it: the iterator to advance
3886 *
3887 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003888 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003889static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003890{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003891 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003892 struct cgrp_cset_link *link;
3893 struct css_set *cset;
3894
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003895 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003896
Tejun Heod5158762013-08-08 20:11:26 -04003897 /* Advance to the next non-empty css_set */
3898 do {
3899 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003900 if (l == it->cset_head) {
3901 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003902 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003903 return;
3904 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003905
3906 if (it->ss) {
3907 cset = container_of(l, struct css_set,
3908 e_cset_node[it->ss->id]);
3909 } else {
3910 link = list_entry(l, struct cgrp_cset_link, cset_link);
3911 cset = link->cset;
3912 }
Tejun Heo0de09422015-10-15 16:41:49 -04003913 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05003914
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003915 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003916
3917 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003918 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003919 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003920 it->task_pos = cset->mg_tasks.next;
3921
3922 it->tasks_head = &cset->tasks;
3923 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04003924
3925 /*
3926 * We don't keep css_sets locked across iteration steps and thus
3927 * need to take steps to ensure that iteration can be resumed after
3928 * the lock is re-acquired. Iteration is performed at two levels -
3929 * css_sets and tasks in them.
3930 *
3931 * Once created, a css_set never leaves its cgroup lists, so a
3932 * pinned css_set is guaranteed to stay put and we can resume
3933 * iteration afterwards.
3934 *
3935 * Tasks may leave @cset across iteration steps. This is resolved
3936 * by registering each iterator with the css_set currently being
3937 * walked and making css_set_move_task() advance iterators whose
3938 * next task is leaving.
3939 */
3940 if (it->cur_cset) {
3941 list_del(&it->iters_node);
3942 put_css_set_locked(it->cur_cset);
3943 }
3944 get_css_set(cset);
3945 it->cur_cset = cset;
3946 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04003947}
3948
Tejun Heoecb9d532015-10-15 16:41:52 -04003949static void css_task_iter_advance(struct css_task_iter *it)
3950{
3951 struct list_head *l = it->task_pos;
3952
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003953 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04003954 WARN_ON_ONCE(!l);
3955
3956 /*
3957 * Advance iterator to find next entry. cset->tasks is consumed
3958 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3959 * next cset.
3960 */
3961 l = l->next;
3962
3963 if (l == it->tasks_head)
3964 l = it->mg_tasks_head->next;
3965
3966 if (l == it->mg_tasks_head)
3967 css_task_iter_advance_css_set(it);
3968 else
3969 it->task_pos = l;
3970}
3971
Tejun Heo0942eee2013-08-08 20:11:26 -04003972/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003973 * css_task_iter_start - initiate task iteration
3974 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003975 * @it: the task iterator to use
3976 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003977 * Initiate iteration through the tasks of @css. The caller can call
3978 * css_task_iter_next() to walk through the tasks until the function
3979 * returns NULL. On completion of iteration, css_task_iter_end() must be
3980 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003981 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003982void css_task_iter_start(struct cgroup_subsys_state *css,
3983 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003984{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003985 /* no one should try to iterate before mounting cgroups */
3986 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003987
Tejun Heoed27b9f2015-10-15 16:41:52 -04003988 memset(it, 0, sizeof(*it));
3989
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003990 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003991
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003992 it->ss = css->ss;
3993
3994 if (it->ss)
3995 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3996 else
3997 it->cset_pos = &css->cgroup->cset_links;
3998
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003999 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004000
Tejun Heoecb9d532015-10-15 16:41:52 -04004001 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004002
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004003 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004004}
Paul Menage817929e2007-10-18 23:39:36 -07004005
Tejun Heo0942eee2013-08-08 20:11:26 -04004006/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004007 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004008 * @it: the task iterator being iterated
4009 *
4010 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004011 * initialized via css_task_iter_start(). Returns NULL when the iteration
4012 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004013 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004014struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004015{
Tejun Heod5745672015-10-29 11:43:05 +09004016 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004017 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004018 it->cur_task = NULL;
4019 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004020
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004021 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004022
Tejun Heod5745672015-10-29 11:43:05 +09004023 if (it->task_pos) {
4024 it->cur_task = list_entry(it->task_pos, struct task_struct,
4025 cg_list);
4026 get_task_struct(it->cur_task);
4027 css_task_iter_advance(it);
4028 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004029
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004030 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004031
4032 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004033}
4034
Tejun Heo0942eee2013-08-08 20:11:26 -04004035/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004036 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004037 * @it: the task iterator to finish
4038 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004039 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004040 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004041void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004042{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004043 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004044 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004045 list_del(&it->iters_node);
4046 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004047 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004048 }
4049
4050 if (it->cur_task)
4051 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004052}
4053
4054/**
4055 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4056 * @to: cgroup to which the tasks will be moved
4057 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004058 *
4059 * Locking rules between cgroup_post_fork() and the migration path
4060 * guarantee that, if a task is forking while being migrated, the new child
4061 * is guaranteed to be either visible in the source cgroup after the
4062 * parent's migration is complete or put into the target cgroup. No task
4063 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004064 */
4065int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4066{
Tejun Heo952aaa12014-02-25 10:04:03 -05004067 LIST_HEAD(preloaded_csets);
4068 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004069 struct css_task_iter it;
4070 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004071 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004072
Tejun Heo952aaa12014-02-25 10:04:03 -05004073 mutex_lock(&cgroup_mutex);
4074
4075 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004076 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004077 list_for_each_entry(link, &from->cset_links, cset_link)
4078 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004079 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004080
4081 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4082 if (ret)
4083 goto out_err;
4084
4085 /*
Rami Rosen2cfa2b12016-01-09 23:33:06 +02004086 * Migrate tasks one-by-one until @from is empty. This fails iff
Tejun Heo952aaa12014-02-25 10:04:03 -05004087 * ->can_attach() fails.
4088 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004089 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004090 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004091 task = css_task_iter_next(&it);
4092 if (task)
4093 get_task_struct(task);
4094 css_task_iter_end(&it);
4095
4096 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004097 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004098 put_task_struct(task);
4099 }
4100 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004101out_err:
4102 cgroup_migrate_finish(&preloaded_csets);
4103 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004104 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004105}
4106
Paul Menage817929e2007-10-18 23:39:36 -07004107/*
Ben Blum102a7752009-09-23 15:56:26 -07004108 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004109 *
4110 * Reading this file can return large amounts of data if a cgroup has
4111 * *lots* of attached tasks. So it may need several calls to read(),
4112 * but we cannot guarantee that the information we produce is correct
4113 * unless we produce it entirely atomically.
4114 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004115 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004116
Li Zefan24528252012-01-20 11:58:43 +08004117/* which pidlist file are we talking about? */
4118enum cgroup_filetype {
4119 CGROUP_FILE_PROCS,
4120 CGROUP_FILE_TASKS,
4121};
4122
4123/*
4124 * A pidlist is a list of pids that virtually represents the contents of one
4125 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4126 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4127 * to the cgroup.
4128 */
4129struct cgroup_pidlist {
4130 /*
4131 * used to find which pidlist is wanted. doesn't change as long as
4132 * this particular list stays in the list.
4133 */
4134 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4135 /* array of xids */
4136 pid_t *list;
4137 /* how many elements the above list has */
4138 int length;
Li Zefan24528252012-01-20 11:58:43 +08004139 /* each of these stored in a list by its cgroup */
4140 struct list_head links;
4141 /* pointer to the cgroup we belong to, for list removal purposes */
4142 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004143 /* for delayed destruction */
4144 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004145};
4146
Paul Menagebbcb81d2007-10-18 23:39:32 -07004147/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004148 * The following two functions "fix" the issue where there are more pids
4149 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4150 * TODO: replace with a kernel-wide solution to this problem
4151 */
4152#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4153static void *pidlist_allocate(int count)
4154{
4155 if (PIDLIST_TOO_LARGE(count))
4156 return vmalloc(count * sizeof(pid_t));
4157 else
4158 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4159}
Tejun Heob1a21362013-11-29 10:42:58 -05004160
Ben Blumd1d9fd32009-09-23 15:56:28 -07004161static void pidlist_free(void *p)
4162{
Bandan Das58794512015-03-02 17:51:10 -05004163 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004164}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004165
4166/*
Tejun Heob1a21362013-11-29 10:42:58 -05004167 * Used to destroy all pidlists lingering waiting for destroy timer. None
4168 * should be left afterwards.
4169 */
4170static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4171{
4172 struct cgroup_pidlist *l, *tmp_l;
4173
4174 mutex_lock(&cgrp->pidlist_mutex);
4175 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4176 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4177 mutex_unlock(&cgrp->pidlist_mutex);
4178
4179 flush_workqueue(cgroup_pidlist_destroy_wq);
4180 BUG_ON(!list_empty(&cgrp->pidlists));
4181}
4182
4183static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4184{
4185 struct delayed_work *dwork = to_delayed_work(work);
4186 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4187 destroy_dwork);
4188 struct cgroup_pidlist *tofree = NULL;
4189
4190 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004191
4192 /*
Tejun Heo04502362013-11-29 10:42:59 -05004193 * Destroy iff we didn't get queued again. The state won't change
4194 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004195 */
Tejun Heo04502362013-11-29 10:42:59 -05004196 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004197 list_del(&l->links);
4198 pidlist_free(l->list);
4199 put_pid_ns(l->key.ns);
4200 tofree = l;
4201 }
4202
Tejun Heob1a21362013-11-29 10:42:58 -05004203 mutex_unlock(&l->owner->pidlist_mutex);
4204 kfree(tofree);
4205}
4206
4207/*
Ben Blum102a7752009-09-23 15:56:26 -07004208 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004209 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004210 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004211static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004212{
Ben Blum102a7752009-09-23 15:56:26 -07004213 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004214
4215 /*
4216 * we presume the 0th element is unique, so i starts at 1. trivial
4217 * edge cases first; no work needs to be done for either
4218 */
4219 if (length == 0 || length == 1)
4220 return length;
4221 /* src and dest walk down the list; dest counts unique elements */
4222 for (src = 1; src < length; src++) {
4223 /* find next unique element */
4224 while (list[src] == list[src-1]) {
4225 src++;
4226 if (src == length)
4227 goto after;
4228 }
4229 /* dest always points to where the next unique element goes */
4230 list[dest] = list[src];
4231 dest++;
4232 }
4233after:
Ben Blum102a7752009-09-23 15:56:26 -07004234 return dest;
4235}
4236
Tejun Heoafb2bc12013-11-29 10:42:59 -05004237/*
4238 * The two pid files - task and cgroup.procs - guaranteed that the result
4239 * is sorted, which forced this whole pidlist fiasco. As pid order is
4240 * different per namespace, each namespace needs differently sorted list,
4241 * making it impossible to use, for example, single rbtree of member tasks
4242 * sorted by task pointer. As pidlists can be fairly large, allocating one
4243 * per open file is dangerous, so cgroup had to implement shared pool of
4244 * pidlists keyed by cgroup and namespace.
4245 *
4246 * All this extra complexity was caused by the original implementation
4247 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004248 * want to do away with it. Explicitly scramble sort order if on the
4249 * default hierarchy so that no such expectation exists in the new
4250 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004251 *
4252 * Scrambling is done by swapping every two consecutive bits, which is
4253 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4254 */
4255static pid_t pid_fry(pid_t pid)
4256{
4257 unsigned a = pid & 0x55555555;
4258 unsigned b = pid & 0xAAAAAAAA;
4259
4260 return (a << 1) | (b >> 1);
4261}
4262
4263static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4264{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004265 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004266 return pid_fry(pid);
4267 else
4268 return pid;
4269}
4270
Ben Blum102a7752009-09-23 15:56:26 -07004271static int cmppid(const void *a, const void *b)
4272{
4273 return *(pid_t *)a - *(pid_t *)b;
4274}
4275
Tejun Heoafb2bc12013-11-29 10:42:59 -05004276static int fried_cmppid(const void *a, const void *b)
4277{
4278 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4279}
4280
Ben Blum72a8cb32009-09-23 15:56:27 -07004281static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4282 enum cgroup_filetype type)
4283{
4284 struct cgroup_pidlist *l;
4285 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004286 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004287
Tejun Heoe6b81712013-11-29 10:42:59 -05004288 lockdep_assert_held(&cgrp->pidlist_mutex);
4289
4290 list_for_each_entry(l, &cgrp->pidlists, links)
4291 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004292 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004293 return NULL;
4294}
4295
Ben Blum72a8cb32009-09-23 15:56:27 -07004296/*
4297 * find the appropriate pidlist for our purpose (given procs vs tasks)
4298 * returns with the lock on that pidlist already held, and takes care
4299 * of the use count, or returns NULL with no locks held if we're out of
4300 * memory.
4301 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004302static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4303 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004304{
4305 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004306
Tejun Heoe6b81712013-11-29 10:42:59 -05004307 lockdep_assert_held(&cgrp->pidlist_mutex);
4308
4309 l = cgroup_pidlist_find(cgrp, type);
4310 if (l)
4311 return l;
4312
Ben Blum72a8cb32009-09-23 15:56:27 -07004313 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004314 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004315 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004316 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004317
Tejun Heob1a21362013-11-29 10:42:58 -05004318 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004319 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004320 /* don't need task_nsproxy() if we're looking at ourself */
4321 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004322 l->owner = cgrp;
4323 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004324 return l;
4325}
4326
4327/*
Ben Blum102a7752009-09-23 15:56:26 -07004328 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4329 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004330static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4331 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004332{
4333 pid_t *array;
4334 int length;
4335 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004336 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004337 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004338 struct cgroup_pidlist *l;
4339
Tejun Heo4bac00d2013-11-29 10:42:59 -05004340 lockdep_assert_held(&cgrp->pidlist_mutex);
4341
Ben Blum102a7752009-09-23 15:56:26 -07004342 /*
4343 * If cgroup gets more users after we read count, we won't have
4344 * enough space - tough. This race is indistinguishable to the
4345 * caller from the case that the additional cgroup users didn't
4346 * show up until sometime later on.
4347 */
4348 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004349 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004350 if (!array)
4351 return -ENOMEM;
4352 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004353 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004354 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004355 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004356 break;
Ben Blum102a7752009-09-23 15:56:26 -07004357 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004358 if (type == CGROUP_FILE_PROCS)
4359 pid = task_tgid_vnr(tsk);
4360 else
4361 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004362 if (pid > 0) /* make sure to only use valid results */
4363 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004364 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004365 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004366 length = n;
4367 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004368 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004369 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4370 else
4371 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004372 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004373 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004374
Tejun Heoe6b81712013-11-29 10:42:59 -05004375 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004376 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004377 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004378 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004379 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004380
4381 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004382 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004383 l->list = array;
4384 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004385 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004386 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004387}
4388
Balbir Singh846c7bb2007-10-18 23:39:44 -07004389/**
Li Zefana043e3b2008-02-23 15:24:09 -08004390 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004391 * @stats: cgroupstats to fill information into
4392 * @dentry: A dentry entry belonging to the cgroup for which stats have
4393 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004394 *
4395 * Build and fill cgroupstats so that taskstats can export it to user
4396 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004397 */
4398int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4399{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004400 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004401 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004402 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004403 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004404
Tejun Heo2bd59d42014-02-11 11:52:49 -05004405 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4406 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4407 kernfs_type(kn) != KERNFS_DIR)
4408 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004409
Li Zefanbad34662014-02-14 16:54:28 +08004410 mutex_lock(&cgroup_mutex);
4411
Tejun Heo2bd59d42014-02-11 11:52:49 -05004412 /*
4413 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004414 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004415 * @kn->priv is RCU safe. Let's do the RCU dancing.
4416 */
4417 rcu_read_lock();
4418 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004419 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004420 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004421 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004422 return -ENOENT;
4423 }
Li Zefanbad34662014-02-14 16:54:28 +08004424 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004425
Tejun Heo9d800df2014-05-14 09:15:00 -04004426 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004427 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004428 switch (tsk->state) {
4429 case TASK_RUNNING:
4430 stats->nr_running++;
4431 break;
4432 case TASK_INTERRUPTIBLE:
4433 stats->nr_sleeping++;
4434 break;
4435 case TASK_UNINTERRUPTIBLE:
4436 stats->nr_uninterruptible++;
4437 break;
4438 case TASK_STOPPED:
4439 stats->nr_stopped++;
4440 break;
4441 default:
4442 if (delayacct_is_task_waiting_on_io(tsk))
4443 stats->nr_io_wait++;
4444 break;
4445 }
4446 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004447 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004448
Li Zefanbad34662014-02-14 16:54:28 +08004449 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004450 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004451}
4452
Paul Menage8f3ff202009-09-23 15:56:25 -07004453
Paul Menagecc31edc2008-10-18 20:28:04 -07004454/*
Ben Blum102a7752009-09-23 15:56:26 -07004455 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004456 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004457 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004458 */
4459
Ben Blum102a7752009-09-23 15:56:26 -07004460static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004461{
4462 /*
4463 * Initially we receive a position value that corresponds to
4464 * one more than the last pid shown (or 0 on the first call or
4465 * after a seek to the start). Use a binary-search to find the
4466 * next pid to display, if any
4467 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004468 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004469 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004470 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004471 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004472 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004473 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004474
Tejun Heo4bac00d2013-11-29 10:42:59 -05004475 mutex_lock(&cgrp->pidlist_mutex);
4476
4477 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004478 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004479 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004480 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004481 * could already have been destroyed.
4482 */
Tejun Heo5d224442013-12-05 12:28:04 -05004483 if (of->priv)
4484 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004485
4486 /*
4487 * Either this is the first start() after open or the matching
4488 * pidlist has been destroyed inbetween. Create a new one.
4489 */
Tejun Heo5d224442013-12-05 12:28:04 -05004490 if (!of->priv) {
4491 ret = pidlist_array_load(cgrp, type,
4492 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004493 if (ret)
4494 return ERR_PTR(ret);
4495 }
Tejun Heo5d224442013-12-05 12:28:04 -05004496 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004497
Paul Menagecc31edc2008-10-18 20:28:04 -07004498 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004499 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004500
Paul Menagecc31edc2008-10-18 20:28:04 -07004501 while (index < end) {
4502 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004503 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004504 index = mid;
4505 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004506 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004507 index = mid + 1;
4508 else
4509 end = mid;
4510 }
4511 }
4512 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004513 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004514 return NULL;
4515 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004516 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004517 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004518 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004519}
4520
Ben Blum102a7752009-09-23 15:56:26 -07004521static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004522{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004523 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004524 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004525
Tejun Heo5d224442013-12-05 12:28:04 -05004526 if (l)
4527 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004528 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004529 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004530}
4531
Ben Blum102a7752009-09-23 15:56:26 -07004532static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004533{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004534 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004535 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004536 pid_t *p = v;
4537 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004538 /*
4539 * Advance to the next pid in the array. If this goes off the
4540 * end, we're done
4541 */
4542 p++;
4543 if (p >= end) {
4544 return NULL;
4545 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004546 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004547 return p;
4548 }
4549}
4550
Ben Blum102a7752009-09-23 15:56:26 -07004551static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004552{
Joe Perches94ff2122015-04-15 16:18:20 -07004553 seq_printf(s, "%d\n", *(int *)v);
4554
4555 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004556}
4557
Tejun Heo182446d2013-08-08 20:11:24 -04004558static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4559 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004560{
Tejun Heo182446d2013-08-08 20:11:24 -04004561 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004562}
4563
Tejun Heo182446d2013-08-08 20:11:24 -04004564static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4565 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004566{
Paul Menage6379c102008-07-25 01:47:01 -07004567 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004568 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004569 else
Tejun Heo182446d2013-08-08 20:11:24 -04004570 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004571 return 0;
4572}
4573
Tejun Heo182446d2013-08-08 20:11:24 -04004574static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4575 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004576{
Tejun Heo182446d2013-08-08 20:11:24 -04004577 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004578}
4579
Tejun Heo182446d2013-08-08 20:11:24 -04004580static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4581 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004582{
4583 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004584 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004585 else
Tejun Heo182446d2013-08-08 20:11:24 -04004586 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004587 return 0;
4588}
4589
Tejun Heoa14c6872014-07-15 11:05:09 -04004590/* cgroup core interface files for the default hierarchy */
4591static struct cftype cgroup_dfl_base_files[] = {
4592 {
4593 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004594 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004595 .seq_start = cgroup_pidlist_start,
4596 .seq_next = cgroup_pidlist_next,
4597 .seq_stop = cgroup_pidlist_stop,
4598 .seq_show = cgroup_pidlist_show,
4599 .private = CGROUP_FILE_PROCS,
4600 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004601 },
4602 {
4603 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04004604 .seq_show = cgroup_controllers_show,
4605 },
4606 {
4607 .name = "cgroup.subtree_control",
4608 .seq_show = cgroup_subtree_control_show,
4609 .write = cgroup_subtree_control_write,
4610 },
4611 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004612 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004613 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004614 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004615 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004616 },
4617 { } /* terminate */
4618};
4619
4620/* cgroup core interface files for the legacy hierarchies */
4621static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004622 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004623 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004624 .seq_start = cgroup_pidlist_start,
4625 .seq_next = cgroup_pidlist_next,
4626 .seq_stop = cgroup_pidlist_stop,
4627 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004628 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004629 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004630 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004631 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004632 .name = "cgroup.clone_children",
4633 .read_u64 = cgroup_clone_children_read,
4634 .write_u64 = cgroup_clone_children_write,
4635 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004636 {
Tejun Heo873fe092013-04-14 20:15:26 -07004637 .name = "cgroup.sane_behavior",
4638 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004639 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004640 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004641 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004642 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004643 .seq_start = cgroup_pidlist_start,
4644 .seq_next = cgroup_pidlist_next,
4645 .seq_stop = cgroup_pidlist_stop,
4646 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004647 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004648 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004649 },
4650 {
4651 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004652 .read_u64 = cgroup_read_notify_on_release,
4653 .write_u64 = cgroup_write_notify_on_release,
4654 },
Tejun Heo873fe092013-04-14 20:15:26 -07004655 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004656 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004657 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004658 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004659 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004660 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004661 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004662 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004663};
4664
Tejun Heo0c21ead2013-08-13 20:22:51 -04004665/*
4666 * css destruction is four-stage process.
4667 *
4668 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4669 * Implemented in kill_css().
4670 *
4671 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004672 * and thus css_tryget_online() is guaranteed to fail, the css can be
4673 * offlined by invoking offline_css(). After offlining, the base ref is
4674 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004675 *
4676 * 3. When the percpu_ref reaches zero, the only possible remaining
4677 * accessors are inside RCU read sections. css_release() schedules the
4678 * RCU callback.
4679 *
4680 * 4. After the grace period, the css can be freed. Implemented in
4681 * css_free_work_fn().
4682 *
4683 * It is actually hairier because both step 2 and 4 require process context
4684 * and thus involve punting to css->destroy_work adding two additional
4685 * steps to the already complex sequence.
4686 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004687static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004688{
4689 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004690 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004691 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004692 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004693
Tejun Heo9a1049d2014-06-28 08:10:14 -04004694 percpu_ref_exit(&css->refcnt);
4695
Vladimir Davydov01e58652015-02-12 14:59:26 -08004696 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004697 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004698 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004699 int id = css->id;
4700
Vladimir Davydov01e58652015-02-12 14:59:26 -08004701 ss->css_free(css);
4702 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004703 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004704
4705 if (parent)
4706 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004707 } else {
4708 /* cgroup free path */
4709 atomic_dec(&cgrp->root->nr_cgrps);
4710 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004711 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004712
Tejun Heod51f39b2014-05-16 13:22:48 -04004713 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004714 /*
4715 * We get a ref to the parent, and put the ref when
4716 * this cgroup is being freed, so it's guaranteed
4717 * that the parent won't be destroyed before its
4718 * children.
4719 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004720 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004721 kernfs_put(cgrp->kn);
4722 kfree(cgrp);
4723 } else {
4724 /*
4725 * This is root cgroup's refcnt reaching zero,
4726 * which indicates that the root should be
4727 * released.
4728 */
4729 cgroup_destroy_root(cgrp->root);
4730 }
4731 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004732}
4733
4734static void css_free_rcu_fn(struct rcu_head *rcu_head)
4735{
4736 struct cgroup_subsys_state *css =
4737 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4738
Tejun Heo0c21ead2013-08-13 20:22:51 -04004739 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004740 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004741}
4742
Tejun Heo25e15d82014-05-14 09:15:02 -04004743static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004744{
4745 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004746 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004747 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004748 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004749
Tejun Heo1fed1b22014-05-16 13:22:49 -04004750 mutex_lock(&cgroup_mutex);
4751
Tejun Heode3f0342014-05-16 13:22:49 -04004752 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004753 list_del_rcu(&css->sibling);
4754
Tejun Heo9d755d32014-05-14 09:15:02 -04004755 if (ss) {
4756 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004757 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004758 if (ss->css_released)
4759 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004760 } else {
4761 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004762 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4763 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004764
4765 /*
4766 * There are two control paths which try to determine
4767 * cgroup from dentry without going through kernfs -
4768 * cgroupstats_build() and css_tryget_online_from_dir().
4769 * Those are supported by RCU protecting clearing of
4770 * cgrp->kn->priv backpointer.
4771 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004772 if (cgrp->kn)
4773 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4774 NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004775 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004776
Tejun Heo1fed1b22014-05-16 13:22:49 -04004777 mutex_unlock(&cgroup_mutex);
4778
Tejun Heo0c21ead2013-08-13 20:22:51 -04004779 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004780}
4781
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004782static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004783{
4784 struct cgroup_subsys_state *css =
4785 container_of(ref, struct cgroup_subsys_state, refcnt);
4786
Tejun Heo25e15d82014-05-14 09:15:02 -04004787 INIT_WORK(&css->destroy_work, css_release_work_fn);
4788 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789}
4790
Tejun Heoddfcada2014-05-04 15:09:14 -04004791static void init_and_link_css(struct cgroup_subsys_state *css,
4792 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004793{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004794 lockdep_assert_held(&cgroup_mutex);
4795
Tejun Heoddfcada2014-05-04 15:09:14 -04004796 cgroup_get(cgrp);
4797
Tejun Heod5c419b2014-05-16 13:22:48 -04004798 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004799 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004800 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004801 INIT_LIST_HEAD(&css->sibling);
4802 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004803 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05004804 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004805
Tejun Heod51f39b2014-05-16 13:22:48 -04004806 if (cgroup_parent(cgrp)) {
4807 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004808 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004809 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004810
Tejun Heoca8bdca2013-08-26 18:40:56 -04004811 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004812}
4813
Li Zefan2a4ac632013-07-31 16:16:40 +08004814/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004815static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004816{
Tejun Heo623f9262013-08-13 11:01:55 -04004817 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004818 int ret = 0;
4819
Tejun Heoa31f2d32012-11-19 08:13:37 -08004820 lockdep_assert_held(&cgroup_mutex);
4821
Tejun Heo92fb9742012-11-19 08:13:38 -08004822 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004823 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004824 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004825 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004826 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05004827
4828 atomic_inc(&css->online_cnt);
4829 if (css->parent)
4830 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04004831 }
Tejun Heob1929db2012-11-19 08:13:38 -08004832 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004833}
4834
Li Zefan2a4ac632013-07-31 16:16:40 +08004835/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004836static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004837{
Tejun Heo623f9262013-08-13 11:01:55 -04004838 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004839
4840 lockdep_assert_held(&cgroup_mutex);
4841
4842 if (!(css->flags & CSS_ONLINE))
4843 return;
4844
Vladimir Davydovfa062352016-03-01 19:56:30 +03004845 if (ss->css_reset)
4846 ss->css_reset(css);
4847
Li Zefand7eeac12013-03-12 15:35:59 -07004848 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004849 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004850
Tejun Heoeb954192013-08-08 20:11:23 -04004851 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004852 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004853
4854 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004855}
4856
Tejun Heoc81c925a2013-12-06 15:11:56 -05004857/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004858 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05004859 * @cgrp: the cgroup new css will be associated with
4860 * @ss: the subsys of new css
4861 *
4862 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004863 * css is online and installed in @cgrp. This function doesn't create the
4864 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004865 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004866static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4867 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004868{
Tejun Heod51f39b2014-05-16 13:22:48 -04004869 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004870 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004871 struct cgroup_subsys_state *css;
4872 int err;
4873
Tejun Heoc81c925a2013-12-06 15:11:56 -05004874 lockdep_assert_held(&cgroup_mutex);
4875
Tejun Heo1fed1b22014-05-16 13:22:49 -04004876 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004877 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004878 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004879
Tejun Heoddfcada2014-05-04 15:09:14 -04004880 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004881
Tejun Heo2aad2a82014-09-24 13:31:50 -04004882 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004883 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004884 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004885
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004886 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004887 if (err < 0)
4888 goto err_free_percpu_ref;
4889 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004890
Tejun Heo15a4c832014-05-04 15:09:14 -04004891 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004892 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004893 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004894
4895 err = online_css(css);
4896 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004897 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004898
Tejun Heoc81c925a2013-12-06 15:11:56 -05004899 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004900 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004901 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 -04004902 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004903 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004904 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004905 ss->warned_broken_hierarchy = true;
4906 }
4907
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004908 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004909
Tejun Heo1fed1b22014-05-16 13:22:49 -04004910err_list_del:
4911 list_del_rcu(&css->sibling);
Tejun Heo15a4c832014-05-04 15:09:14 -04004912 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004913err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004914 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004915err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004916 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004917 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004918}
4919
Tejun Heoa5bca212016-03-03 09:57:58 -05004920static struct cgroup *cgroup_create(struct cgroup *parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921{
Tejun Heoa5bca212016-03-03 09:57:58 -05004922 struct cgroup_root *root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004923 struct cgroup_subsys *ss;
Tejun Heoa5bca212016-03-03 09:57:58 -05004924 struct cgroup *cgrp, *tcgrp;
4925 int level = parent->level + 1;
4926 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004927
Tejun Heo0a950f62012-11-19 09:02:12 -08004928 /* allocate the cgroup and its ID, 0 is reserved for the root */
Tejun Heob11cfb52015-11-20 15:55:52 -05004929 cgrp = kzalloc(sizeof(*cgrp) +
4930 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05004931 if (!cgrp)
4932 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004933
Tejun Heo2aad2a82014-09-24 13:31:50 -04004934 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004935 if (ret)
4936 goto out_free_cgrp;
4937
Li Zefan0ab02ca2014-02-11 16:05:46 +08004938 /*
4939 * Temporarily set the pointer to NULL, so idr_find() won't return
4940 * a half-baked cgroup.
4941 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004942 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004943 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004944 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004945 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004946 }
4947
Paul Menagecc31edc2008-10-18 20:28:04 -07004948 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004949
Tejun Heo9d800df2014-05-14 09:15:00 -04004950 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004951 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05004952 cgrp->level = level;
4953
4954 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
4955 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004956
Li Zefanb6abdb02008-03-04 14:28:19 -08004957 if (notify_on_release(parent))
4958 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4959
Tejun Heo2260e7f2012-11-19 08:13:38 -08004960 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4961 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004962
Tejun Heo0cb51d72014-05-16 13:22:49 -04004963 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004964
Tejun Heo4e139af2012-11-19 08:13:36 -08004965 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004966 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004967 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004968 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004969
Tejun Heo0d802552013-12-06 15:11:56 -05004970 /*
4971 * @cgrp is now fully operational. If something fails after this
4972 * point, it'll be released via the normal destruction path.
4973 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004974 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004975
Tejun Heo195e9b62016-03-03 09:57:58 -05004976 /* create the csses */
Tejun Heo5531dc92016-03-03 09:57:58 -05004977 do_each_subsys_mask(ss, ssid, cgroup_ss_mask(cgrp)) {
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004978 struct cgroup_subsys_state *css;
4979
4980 css = css_create(cgrp, ss);
4981 if (IS_ERR(css)) {
4982 ret = PTR_ERR(css);
Tejun Heo996cd1f2016-02-22 22:25:46 -05004983 goto out_destroy;
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004984 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05004985 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07004986
Tejun Heobd53d612014-04-23 11:13:16 -04004987 /*
4988 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004989 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004990 */
Tejun Heo667c2492014-07-08 18:02:56 -04004991 if (!cgroup_on_dfl(cgrp)) {
Tejun Heo5531dc92016-03-03 09:57:58 -05004992 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo8699b772016-02-22 22:25:46 -05004993 cgroup_refresh_subtree_ss_mask(cgrp);
Tejun Heo667c2492014-07-08 18:02:56 -04004994 }
Tejun Heof392e512014-04-23 11:13:14 -04004995
Tejun Heoa5bca212016-03-03 09:57:58 -05004996 return cgrp;
4997
4998out_cancel_ref:
4999 percpu_ref_exit(&cgrp->self.refcnt);
5000out_free_cgrp:
5001 kfree(cgrp);
5002 return ERR_PTR(ret);
5003out_destroy:
5004 cgroup_destroy_locked(cgrp);
5005 return ERR_PTR(ret);
5006}
5007
5008static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
5009 umode_t mode)
5010{
5011 struct cgroup *parent, *cgrp;
5012 struct cgroup_subsys *ss;
5013 struct kernfs_node *kn;
5014 int ssid, ret;
5015
5016 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5017 if (strchr(name, '\n'))
5018 return -EINVAL;
5019
5020 parent = cgroup_kn_lock_live(parent_kn);
5021 if (!parent)
5022 return -ENODEV;
5023
5024 cgrp = cgroup_create(parent);
5025 if (IS_ERR(cgrp)) {
5026 ret = PTR_ERR(cgrp);
5027 goto out_unlock;
5028 }
5029
Tejun Heo195e9b62016-03-03 09:57:58 -05005030 /* create the directory */
5031 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5032 if (IS_ERR(kn)) {
5033 ret = PTR_ERR(kn);
5034 goto out_destroy;
5035 }
5036 cgrp->kn = kn;
5037
5038 /*
5039 * This extra ref will be put in cgroup_free_fn() and guarantees
5040 * that @cgrp->kn is always accessible.
5041 */
5042 kernfs_get(kn);
5043
5044 ret = cgroup_kn_set_ugid(kn);
5045 if (ret)
5046 goto out_destroy;
5047
5048 ret = css_populate_dir(&cgrp->self, NULL);
5049 if (ret)
5050 goto out_destroy;
5051
Tejun Heo5531dc92016-03-03 09:57:58 -05005052 do_each_subsys_mask(ss, ssid, cgroup_control(cgrp)) {
Tejun Heo195e9b62016-03-03 09:57:58 -05005053 ret = css_populate_dir(cgroup_css(cgrp, ss), NULL);
5054 if (ret)
5055 goto out_destroy;
5056 } while_each_subsys_mask();
5057
5058 /* let's create and online css's */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005059 kernfs_activate(kn);
5060
Tejun Heoba0f4d72014-05-13 12:19:22 -04005061 ret = 0;
5062 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005063
Tejun Heoa5bca212016-03-03 09:57:58 -05005064out_destroy:
5065 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005066out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005067 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005068 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005069}
5070
Tejun Heo223dbc32013-08-13 20:22:50 -04005071/*
5072 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005073 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5074 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005075 */
5076static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005077{
Tejun Heo223dbc32013-08-13 20:22:50 -04005078 struct cgroup_subsys_state *css =
5079 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005080
Tejun Heof20104d2013-08-13 20:22:50 -04005081 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005082
Tejun Heoaa226ff2016-01-21 15:31:11 -05005083 do {
5084 offline_css(css);
5085 css_put(css);
5086 /* @css can't go away while we're holding cgroup_mutex */
5087 css = css->parent;
5088 } while (css && atomic_dec_and_test(&css->online_cnt));
5089
5090 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005091}
5092
Tejun Heo223dbc32013-08-13 20:22:50 -04005093/* css kill confirmation processing requires process context, bounce */
5094static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005095{
5096 struct cgroup_subsys_state *css =
5097 container_of(ref, struct cgroup_subsys_state, refcnt);
5098
Tejun Heoaa226ff2016-01-21 15:31:11 -05005099 if (atomic_dec_and_test(&css->online_cnt)) {
5100 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5101 queue_work(cgroup_destroy_wq, &css->destroy_work);
5102 }
Tejun Heod3daf282013-06-13 19:39:16 -07005103}
5104
Tejun Heof392e512014-04-23 11:13:14 -04005105/**
5106 * kill_css - destroy a css
5107 * @css: css to destroy
5108 *
5109 * This function initiates destruction of @css by removing cgroup interface
5110 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005111 * asynchronously once css_tryget_online() is guaranteed to fail and when
5112 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005113 */
5114static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005115{
Tejun Heo01f64742014-05-13 12:19:23 -04005116 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005117
Tejun Heo2bd59d42014-02-11 11:52:49 -05005118 /*
5119 * This must happen before css is disassociated with its cgroup.
5120 * See seq_css() for details.
5121 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005122 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005123
Tejun Heoedae0c32013-08-13 20:22:51 -04005124 /*
5125 * Killing would put the base ref, but we need to keep it alive
5126 * until after ->css_offline().
5127 */
5128 css_get(css);
5129
5130 /*
5131 * cgroup core guarantees that, by the time ->css_offline() is
5132 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005133 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005134 * proceed to offlining css's because percpu_ref_kill() doesn't
5135 * guarantee that the ref is seen as killed on all CPUs on return.
5136 *
5137 * Use percpu_ref_kill_and_confirm() to get notifications as each
5138 * css is confirmed to be seen as killed on all CPUs.
5139 */
5140 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005141}
5142
5143/**
5144 * cgroup_destroy_locked - the first stage of cgroup destruction
5145 * @cgrp: cgroup to be destroyed
5146 *
5147 * css's make use of percpu refcnts whose killing latency shouldn't be
5148 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005149 * guarantee that css_tryget_online() won't succeed by the time
5150 * ->css_offline() is invoked. To satisfy all the requirements,
5151 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005152 *
5153 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5154 * userland visible parts and start killing the percpu refcnts of
5155 * css's. Set up so that the next stage will be kicked off once all
5156 * the percpu refcnts are confirmed to be killed.
5157 *
5158 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5159 * rest of destruction. Once all cgroup references are gone, the
5160 * cgroup is RCU-freed.
5161 *
5162 * This function implements s1. After this step, @cgrp is gone as far as
5163 * the userland is concerned and a new cgroup with the same name may be
5164 * created. As cgroup doesn't care about the names internally, this
5165 * doesn't cause any problem.
5166 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005167static int cgroup_destroy_locked(struct cgroup *cgrp)
5168 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005169{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005170 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005171 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005172
Tejun Heo42809dd2012-11-19 08:13:37 -08005173 lockdep_assert_held(&cgroup_mutex);
5174
Tejun Heo91486f62015-10-15 16:41:51 -04005175 /*
5176 * Only migration can raise populated from zero and we're already
5177 * holding cgroup_mutex.
5178 */
5179 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005180 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005181
Tejun Heo1a90dd52012-11-05 09:16:59 -08005182 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005183 * Make sure there's no live children. We can't test emptiness of
5184 * ->self.children as dead children linger on it while being
5185 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005186 */
Tejun Heof3d46502014-05-16 13:22:52 -04005187 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005188 return -EBUSY;
5189
5190 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005191 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005192 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005193 */
Tejun Heo184faf32014-05-16 13:22:51 -04005194 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005195
Tejun Heo249f3462014-05-14 09:15:01 -04005196 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005197 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005198 kill_css(css);
5199
Tejun Heo455050d2013-06-13 19:27:41 -07005200 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005201 * Remove @cgrp directory along with the base files. @cgrp has an
5202 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005203 */
Tejun Heo01f64742014-05-13 12:19:23 -04005204 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005205
Tejun Heod51f39b2014-05-16 13:22:48 -04005206 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005207
Tejun Heo249f3462014-05-14 09:15:01 -04005208 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005209 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005210
Tejun Heoea15f8c2013-06-13 19:27:42 -07005211 return 0;
5212};
5213
Tejun Heo2bd59d42014-02-11 11:52:49 -05005214static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005215{
Tejun Heoa9746d82014-05-13 12:19:22 -04005216 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005217 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005218
Tejun Heoa9746d82014-05-13 12:19:22 -04005219 cgrp = cgroup_kn_lock_live(kn);
5220 if (!cgrp)
5221 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005222
Tejun Heoa9746d82014-05-13 12:19:22 -04005223 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005224
Tejun Heoa9746d82014-05-13 12:19:22 -04005225 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005226 return ret;
5227}
5228
Tejun Heo2bd59d42014-02-11 11:52:49 -05005229static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5230 .remount_fs = cgroup_remount,
5231 .show_options = cgroup_show_options,
5232 .mkdir = cgroup_mkdir,
5233 .rmdir = cgroup_rmdir,
5234 .rename = cgroup_rename,
5235};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005236
Tejun Heo15a4c832014-05-04 15:09:14 -04005237static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005238{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005239 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005240
Tejun Heoa5ae9892015-12-29 14:53:56 -05005241 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005242
Tejun Heo648bb562012-11-19 08:13:36 -08005243 mutex_lock(&cgroup_mutex);
5244
Tejun Heo15a4c832014-05-04 15:09:14 -04005245 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005246 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005247
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005248 /* Create the root cgroup state for this subsystem */
5249 ss->root = &cgrp_dfl_root;
5250 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005251 /* We don't handle early failures gracefully */
5252 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005253 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005254
5255 /*
5256 * Root csses are never destroyed and we can't initialize
5257 * percpu_ref during early init. Disable refcnting.
5258 */
5259 css->flags |= CSS_NO_REF;
5260
Tejun Heo15a4c832014-05-04 15:09:14 -04005261 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005262 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005263 css->id = 1;
5264 } else {
5265 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5266 BUG_ON(css->id < 0);
5267 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005268
Li Zefane8d55fd2008-04-29 01:00:13 -07005269 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005270 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005271 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005272 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005273 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005274
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005275 have_fork_callback |= (bool)ss->fork << ss->id;
5276 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005277 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005278 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005279
Li Zefane8d55fd2008-04-29 01:00:13 -07005280 /* At system boot, before all subsystems have been
5281 * registered, no tasks have been forked, so we don't
5282 * need to invoke fork callbacks here. */
5283 BUG_ON(!list_empty(&init_task.tasks));
5284
Tejun Heoae7f1642013-08-13 20:22:50 -04005285 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005286
Tejun Heo648bb562012-11-19 08:13:36 -08005287 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005288}
5289
5290/**
Li Zefana043e3b2008-02-23 15:24:09 -08005291 * cgroup_init_early - cgroup initialization at system boot
5292 *
5293 * Initialize cgroups at system boot, and initialize any
5294 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005295 */
5296int __init cgroup_init_early(void)
5297{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005298 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005299 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005300 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005301
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005302 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005303 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5304
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005305 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005306
Tejun Heo3ed80a62014-02-08 10:36:58 -05005307 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005308 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005309 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005310 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005311 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005312 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5313 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005314
Tejun Heoaec25022014-02-08 10:36:58 -05005315 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005316 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005317 if (!ss->legacy_name)
5318 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005319
5320 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005321 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005322 }
5323 return 0;
5324}
5325
Tejun Heo6e5c8302016-02-22 22:25:47 -05005326static u16 cgroup_disable_mask __initdata;
Tejun Heoa3e72732015-09-25 16:24:27 -04005327
Paul Menageddbcc7e2007-10-18 23:39:30 -07005328/**
Li Zefana043e3b2008-02-23 15:24:09 -08005329 * cgroup_init - cgroup initialization
5330 *
5331 * Register cgroup filesystem and /proc file, and initialize
5332 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005333 */
5334int __init cgroup_init(void)
5335{
Tejun Heo30159ec2013-06-25 11:53:37 -07005336 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005337 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005338
Tejun Heo6e5c8302016-02-22 22:25:47 -05005339 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005340 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005341 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5342 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005343
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005344 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005345
Tejun Heo2378d8b2016-03-03 09:57:57 -05005346 /*
5347 * Add init_css_set to the hash table so that dfl_root can link to
5348 * it during init.
5349 */
5350 hash_add(css_set_table, &init_css_set.hlist,
5351 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005352
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005353 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005354
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005355 mutex_unlock(&cgroup_mutex);
5356
Tejun Heo172a2c062014-03-19 10:23:53 -04005357 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005358 if (ss->early_init) {
5359 struct cgroup_subsys_state *css =
5360 init_css_set.subsys[ss->id];
5361
5362 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5363 GFP_KERNEL);
5364 BUG_ON(css->id < 0);
5365 } else {
5366 cgroup_init_subsys(ss, false);
5367 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005368
Tejun Heo2d8f2432014-04-23 11:13:15 -04005369 list_add_tail(&init_css_set.e_cset_node[ssid],
5370 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005371
5372 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005373 * Setting dfl_root subsys_mask needs to consider the
5374 * disabled flag and cftype registration needs kmalloc,
5375 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005376 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005377 if (cgroup_disable_mask & (1 << ssid)) {
5378 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5379 printk(KERN_INFO "Disabling %s control group subsystem\n",
5380 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005381 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005382 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005383
Johannes Weiner223ffb22016-02-11 13:34:49 -05005384 if (cgroup_ssid_no_v1(ssid))
5385 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5386 ss->name);
5387
Tejun Heoa8ddc822014-07-15 11:05:10 -04005388 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5389
Tejun Heo5de4fa12014-07-15 11:05:10 -04005390 if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005391 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005392
Tejun Heoa8ddc822014-07-15 11:05:10 -04005393 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5394 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5395 } else {
5396 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5397 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005398 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005399
5400 if (ss->bind)
5401 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005402 }
Greg KH676db4a2010-08-05 13:53:35 -07005403
Tejun Heo2378d8b2016-03-03 09:57:57 -05005404 /* init_css_set.subsys[] has been updated, re-hash */
5405 hash_del(&init_css_set.hlist);
5406 hash_add(css_set_table, &init_css_set.hlist,
5407 css_set_hash(init_css_set.subsys));
5408
Tejun Heo035f4f52015-10-15 17:00:43 -04005409 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5410 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005411 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005412 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005413
Tejun Heo2bd59d42014-02-11 11:52:49 -05005414 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005415}
Paul Menageb4f48b62007-10-18 23:39:33 -07005416
Tejun Heoe5fca242013-11-22 17:14:39 -05005417static int __init cgroup_wq_init(void)
5418{
5419 /*
5420 * There isn't much point in executing destruction path in
5421 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005422 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005423 *
5424 * We would prefer to do this in cgroup_init() above, but that
5425 * is called before init_workqueues(): so leave this until after.
5426 */
Tejun Heo1a115332014-02-12 19:06:19 -05005427 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005428 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005429
5430 /*
5431 * Used to destroy pidlists and separate to serve as flush domain.
5432 * Cap @max_active to 1 too.
5433 */
5434 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5435 0, 1);
5436 BUG_ON(!cgroup_pidlist_destroy_wq);
5437
Tejun Heoe5fca242013-11-22 17:14:39 -05005438 return 0;
5439}
5440core_initcall(cgroup_wq_init);
5441
Paul Menagea4243162007-10-18 23:39:35 -07005442/*
5443 * proc_cgroup_show()
5444 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5445 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005446 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005447int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5448 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005449{
Tejun Heoe61734c2014-02-12 09:29:50 -05005450 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005451 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005452 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005453
5454 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005455 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005456 if (!buf)
5457 goto out;
5458
Paul Menagea4243162007-10-18 23:39:35 -07005459 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005460 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005461
Tejun Heo985ed672014-03-19 10:23:53 -04005462 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005463 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005464 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005465 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005466
Tejun Heoa7165262016-02-23 10:00:50 -05005467 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005468 continue;
5469
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005470 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005471 if (root != &cgrp_dfl_root)
5472 for_each_subsys(ss, ssid)
5473 if (root->subsys_mask & (1 << ssid))
5474 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005475 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005476 if (strlen(root->name))
5477 seq_printf(m, "%sname=%s", count ? "," : "",
5478 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005479 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005480
Paul Menage7717f7b2009-09-23 15:56:22 -07005481 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005482
5483 /*
5484 * On traditional hierarchies, all zombie tasks show up as
5485 * belonging to the root cgroup. On the default hierarchy,
5486 * while a zombie doesn't show up in "cgroup.procs" and
5487 * thus can't be migrated, its /proc/PID/cgroup keeps
5488 * reporting the cgroup it belonged to before exiting. If
5489 * the cgroup is removed before the zombie is reaped,
5490 * " (deleted)" is appended to the cgroup path.
5491 */
5492 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5493 path = cgroup_path(cgrp, buf, PATH_MAX);
5494 if (!path) {
5495 retval = -ENAMETOOLONG;
5496 goto out_unlock;
5497 }
5498 } else {
5499 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005500 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005501
Tejun Heoe61734c2014-02-12 09:29:50 -05005502 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005503
5504 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5505 seq_puts(m, " (deleted)\n");
5506 else
5507 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005508 }
5509
Zefan Li006f4ac2014-09-18 16:03:15 +08005510 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005511out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005512 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005513 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005514 kfree(buf);
5515out:
5516 return retval;
5517}
5518
Paul Menagea4243162007-10-18 23:39:35 -07005519/* Display information about each subsystem and each hierarchy */
5520static int proc_cgroupstats_show(struct seq_file *m, void *v)
5521{
Tejun Heo30159ec2013-06-25 11:53:37 -07005522 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005523 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005524
Paul Menage8bab8dd2008-04-04 14:29:57 -07005525 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005526 /*
5527 * ideally we don't want subsystems moving around while we do this.
5528 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5529 * subsys/hierarchy state.
5530 */
Paul Menagea4243162007-10-18 23:39:35 -07005531 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005532
5533 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005534 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005535 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005536 atomic_read(&ss->root->nr_cgrps),
5537 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005538
Paul Menagea4243162007-10-18 23:39:35 -07005539 mutex_unlock(&cgroup_mutex);
5540 return 0;
5541}
5542
5543static int cgroupstats_open(struct inode *inode, struct file *file)
5544{
Al Viro9dce07f2008-03-29 03:07:28 +00005545 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005546}
5547
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005548static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005549 .open = cgroupstats_open,
5550 .read = seq_read,
5551 .llseek = seq_lseek,
5552 .release = single_release,
5553};
5554
Paul Menageb4f48b62007-10-18 23:39:33 -07005555/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005556 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005557 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005558 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005559 * A task is associated with the init_css_set until cgroup_post_fork()
5560 * attaches it to the parent's css_set. Empty cg_list indicates that
5561 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005562 */
5563void cgroup_fork(struct task_struct *child)
5564{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005565 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005566 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005567}
5568
5569/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005570 * cgroup_can_fork - called on a new task before the process is exposed
5571 * @child: the task in question.
5572 *
5573 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5574 * returns an error, the fork aborts with that error code. This allows for
5575 * a cgroup subsystem to conditionally allow or deny new forks.
5576 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005577int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005578{
5579 struct cgroup_subsys *ss;
5580 int i, j, ret;
5581
Tejun Heob4e0eea2016-02-22 22:25:46 -05005582 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005583 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005584 if (ret)
5585 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05005586 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10005587
5588 return 0;
5589
5590out_revert:
5591 for_each_subsys(ss, j) {
5592 if (j >= i)
5593 break;
5594 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005595 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005596 }
5597
5598 return ret;
5599}
5600
5601/**
5602 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5603 * @child: the task in question
5604 *
5605 * This calls the cancel_fork() callbacks if a fork failed *after*
5606 * cgroup_can_fork() succeded.
5607 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005608void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005609{
5610 struct cgroup_subsys *ss;
5611 int i;
5612
5613 for_each_subsys(ss, i)
5614 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005615 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005616}
5617
5618/**
Li Zefana043e3b2008-02-23 15:24:09 -08005619 * cgroup_post_fork - called on a new task after adding it to the task list
5620 * @child: the task in question
5621 *
Tejun Heo5edee612012-10-16 15:03:14 -07005622 * Adds the task to the list running through its css_set if necessary and
5623 * call the subsystem fork() callbacks. Has to be after the task is
5624 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005625 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005626 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005627 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005628void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005629{
Tejun Heo30159ec2013-06-25 11:53:37 -07005630 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005631 int i;
5632
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005633 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005634 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005635 * function sets use_task_css_set_links before grabbing
5636 * tasklist_lock and we just went through tasklist_lock to add
5637 * @child, it's guaranteed that either we see the set
5638 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5639 * @child during its iteration.
5640 *
5641 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005642 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005643 * association is stable, and, on completion of the parent's
5644 * migration, @child is visible in the source of migration or
5645 * already in the destination cgroup. This guarantee is necessary
5646 * when implementing operations which need to migrate all tasks of
5647 * a cgroup to another.
5648 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005649 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005650 * will remain in init_css_set. This is safe because all tasks are
5651 * in the init_css_set before cg_links is enabled and there's no
5652 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005653 */
Paul Menage817929e2007-10-18 23:39:36 -07005654 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005655 struct css_set *cset;
5656
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005657 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005658 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005659 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005660 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005661 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005662 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005663 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005664 }
Tejun Heo5edee612012-10-16 15:03:14 -07005665
5666 /*
5667 * Call ss->fork(). This must happen after @child is linked on
5668 * css_set; otherwise, @child might change state between ->fork()
5669 * and addition to css_set.
5670 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005671 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005672 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005673 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07005674}
Tejun Heo5edee612012-10-16 15:03:14 -07005675
Paul Menage817929e2007-10-18 23:39:36 -07005676/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005677 * cgroup_exit - detach cgroup from exiting task
5678 * @tsk: pointer to task_struct of exiting process
5679 *
5680 * Description: Detach cgroup from @tsk and release it.
5681 *
5682 * Note that cgroups marked notify_on_release force every task in
5683 * them to take the global cgroup_mutex mutex when exiting.
5684 * This could impact scaling on very large systems. Be reluctant to
5685 * use notify_on_release cgroups where very high task exit scaling
5686 * is required on large systems.
5687 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005688 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5689 * call cgroup_exit() while the task is still competent to handle
5690 * notify_on_release(), then leave the task attached to the root cgroup in
5691 * each hierarchy for the remainder of its exit. No need to bother with
5692 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005693 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005694 */
Li Zefan1ec41832014-03-28 15:22:19 +08005695void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005696{
Tejun Heo30159ec2013-06-25 11:53:37 -07005697 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005698 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005699 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005700
5701 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005702 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005703 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005704 */
Tejun Heo0de09422015-10-15 16:41:49 -04005705 cset = task_css_set(tsk);
5706
Paul Menage817929e2007-10-18 23:39:36 -07005707 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005708 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005709 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005710 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005711 } else {
5712 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005713 }
5714
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005715 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005716 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04005717 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005718 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04005719}
Tejun Heo30159ec2013-06-25 11:53:37 -07005720
Tejun Heo2e91fa72015-10-15 16:41:53 -04005721void cgroup_free(struct task_struct *task)
5722{
5723 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005724 struct cgroup_subsys *ss;
5725 int ssid;
5726
Tejun Heob4e0eea2016-02-22 22:25:46 -05005727 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04005728 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005729 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005730
Tejun Heo2e91fa72015-10-15 16:41:53 -04005731 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005732}
Paul Menage697f4162007-10-18 23:39:34 -07005733
Paul Menagebd89aab2007-10-18 23:40:44 -07005734static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005735{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005736 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005737 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5738 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005739}
5740
Paul Menage81a6a5c2007-10-18 23:39:38 -07005741/*
5742 * Notify userspace when a cgroup is released, by running the
5743 * configured release agent with the name of the cgroup (path
5744 * relative to the root of cgroup file system) as the argument.
5745 *
5746 * Most likely, this user command will try to rmdir this cgroup.
5747 *
5748 * This races with the possibility that some other task will be
5749 * attached to this cgroup before it is removed, or that some other
5750 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5751 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5752 * unused, and this cgroup will be reprieved from its death sentence,
5753 * to continue to serve a useful existence. Next time it's released,
5754 * we will get notified again, if it still has 'notify_on_release' set.
5755 *
5756 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5757 * means only wait until the task is successfully execve()'d. The
5758 * separate release agent task is forked by call_usermodehelper(),
5759 * then control in this thread returns here, without waiting for the
5760 * release agent task. We don't bother to wait because the caller of
5761 * this routine has no use for the exit status of the release agent
5762 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005763 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005764static void cgroup_release_agent(struct work_struct *work)
5765{
Zefan Li971ff492014-09-18 16:06:19 +08005766 struct cgroup *cgrp =
5767 container_of(work, struct cgroup, release_agent_work);
5768 char *pathbuf = NULL, *agentbuf = NULL, *path;
5769 char *argv[3], *envp[3];
5770
Paul Menage81a6a5c2007-10-18 23:39:38 -07005771 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005772
Zefan Li971ff492014-09-18 16:06:19 +08005773 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5774 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5775 if (!pathbuf || !agentbuf)
5776 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005777
Zefan Li971ff492014-09-18 16:06:19 +08005778 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5779 if (!path)
5780 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005781
Zefan Li971ff492014-09-18 16:06:19 +08005782 argv[0] = agentbuf;
5783 argv[1] = path;
5784 argv[2] = NULL;
5785
5786 /* minimal command environment */
5787 envp[0] = "HOME=/";
5788 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5789 envp[2] = NULL;
5790
Paul Menage81a6a5c2007-10-18 23:39:38 -07005791 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005792 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005793 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005794out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005795 mutex_unlock(&cgroup_mutex);
5796out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005797 kfree(agentbuf);
5798 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005799}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005800
5801static int __init cgroup_disable(char *str)
5802{
Tejun Heo30159ec2013-06-25 11:53:37 -07005803 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005804 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005805 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005806
5807 while ((token = strsep(&str, ",")) != NULL) {
5808 if (!*token)
5809 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005810
Tejun Heo3ed80a62014-02-08 10:36:58 -05005811 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005812 if (strcmp(token, ss->name) &&
5813 strcmp(token, ss->legacy_name))
5814 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005815 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005816 }
5817 }
5818 return 1;
5819}
5820__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005821
Johannes Weiner223ffb22016-02-11 13:34:49 -05005822static int __init cgroup_no_v1(char *str)
5823{
5824 struct cgroup_subsys *ss;
5825 char *token;
5826 int i;
5827
5828 while ((token = strsep(&str, ",")) != NULL) {
5829 if (!*token)
5830 continue;
5831
5832 if (!strcmp(token, "all")) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05005833 cgroup_no_v1_mask = U16_MAX;
Johannes Weiner223ffb22016-02-11 13:34:49 -05005834 break;
5835 }
5836
5837 for_each_subsys(ss, i) {
5838 if (strcmp(token, ss->name) &&
5839 strcmp(token, ss->legacy_name))
5840 continue;
5841
5842 cgroup_no_v1_mask |= 1 << i;
5843 }
5844 }
5845 return 1;
5846}
5847__setup("cgroup_no_v1=", cgroup_no_v1);
5848
Tejun Heob77d7b62013-08-13 11:01:54 -04005849/**
Tejun Heoec903c02014-05-13 12:11:01 -04005850 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005851 * @dentry: directory dentry of interest
5852 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005853 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005854 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5855 * to get the corresponding css and return it. If such css doesn't exist
5856 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005857 */
Tejun Heoec903c02014-05-13 12:11:01 -04005858struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5859 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005860{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005861 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05005862 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005863 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005864 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005865
Tejun Heo35cf0832013-08-26 18:40:56 -04005866 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05005867 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5868 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005869 return ERR_PTR(-EBADF);
5870
Tejun Heo5a17f542014-02-11 11:52:47 -05005871 rcu_read_lock();
5872
Tejun Heo2bd59d42014-02-11 11:52:49 -05005873 /*
5874 * This path doesn't originate from kernfs and @kn could already
5875 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005876 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005877 */
5878 cgrp = rcu_dereference(kn->priv);
5879 if (cgrp)
5880 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005881
Tejun Heoec903c02014-05-13 12:11:01 -04005882 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005883 css = ERR_PTR(-ENOENT);
5884
5885 rcu_read_unlock();
5886 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005887}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005888
Li Zefan1cb650b2013-08-19 10:05:24 +08005889/**
5890 * css_from_id - lookup css by id
5891 * @id: the cgroup id
5892 * @ss: cgroup subsys to be looked into
5893 *
5894 * Returns the css if there's valid one with @id, otherwise returns NULL.
5895 * Should be called under rcu_read_lock().
5896 */
5897struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5898{
Tejun Heo6fa49182014-05-04 15:09:13 -04005899 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005900 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005901}
5902
Tejun Heo16af4392015-11-20 15:55:52 -05005903/**
5904 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5905 * @path: path on the default hierarchy
5906 *
5907 * Find the cgroup at @path on the default hierarchy, increment its
5908 * reference count and return it. Returns pointer to the found cgroup on
5909 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5910 * if @path points to a non-directory.
5911 */
5912struct cgroup *cgroup_get_from_path(const char *path)
5913{
5914 struct kernfs_node *kn;
5915 struct cgroup *cgrp;
5916
5917 mutex_lock(&cgroup_mutex);
5918
5919 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5920 if (kn) {
5921 if (kernfs_type(kn) == KERNFS_DIR) {
5922 cgrp = kn->priv;
5923 cgroup_get(cgrp);
5924 } else {
5925 cgrp = ERR_PTR(-ENOTDIR);
5926 }
5927 kernfs_put(kn);
5928 } else {
5929 cgrp = ERR_PTR(-ENOENT);
5930 }
5931
5932 mutex_unlock(&cgroup_mutex);
5933 return cgrp;
5934}
5935EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5936
Tejun Heobd1060a2015-12-07 17:38:53 -05005937/*
5938 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5939 * definition in cgroup-defs.h.
5940 */
5941#ifdef CONFIG_SOCK_CGROUP_DATA
5942
5943#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5944
Tejun Heo3fa4cc92015-12-14 11:24:06 -05005945DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05005946static bool cgroup_sk_alloc_disabled __read_mostly;
5947
5948void cgroup_sk_alloc_disable(void)
5949{
5950 if (cgroup_sk_alloc_disabled)
5951 return;
5952 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5953 cgroup_sk_alloc_disabled = true;
5954}
5955
5956#else
5957
5958#define cgroup_sk_alloc_disabled false
5959
5960#endif
5961
5962void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5963{
5964 if (cgroup_sk_alloc_disabled)
5965 return;
5966
5967 rcu_read_lock();
5968
5969 while (true) {
5970 struct css_set *cset;
5971
5972 cset = task_css_set(current);
5973 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5974 skcd->val = (unsigned long)cset->dfl_cgrp;
5975 break;
5976 }
5977 cpu_relax();
5978 }
5979
5980 rcu_read_unlock();
5981}
5982
5983void cgroup_sk_free(struct sock_cgroup_data *skcd)
5984{
5985 cgroup_put(sock_cgroup_ptr(skcd));
5986}
5987
5988#endif /* CONFIG_SOCK_CGROUP_DATA */
5989
Paul Menagefe693432009-09-23 15:56:20 -07005990#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005991static struct cgroup_subsys_state *
5992debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005993{
5994 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5995
5996 if (!css)
5997 return ERR_PTR(-ENOMEM);
5998
5999 return css;
6000}
6001
Tejun Heoeb954192013-08-08 20:11:23 -04006002static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07006003{
Tejun Heoeb954192013-08-08 20:11:23 -04006004 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07006005}
6006
Tejun Heo182446d2013-08-08 20:11:24 -04006007static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
6008 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006009{
Tejun Heo182446d2013-08-08 20:11:24 -04006010 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07006011}
6012
Tejun Heo182446d2013-08-08 20:11:24 -04006013static u64 current_css_set_read(struct cgroup_subsys_state *css,
6014 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006015{
6016 return (u64)(unsigned long)current->cgroups;
6017}
6018
Tejun Heo182446d2013-08-08 20:11:24 -04006019static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08006020 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006021{
6022 u64 count;
6023
6024 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07006025 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07006026 rcu_read_unlock();
6027 return count;
6028}
6029
Tejun Heo2da8ca82013-12-05 12:28:04 -05006030static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006031{
Tejun Heo69d02062013-06-12 21:04:50 -07006032 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07006033 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05006034 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07006035
Tejun Heoe61734c2014-02-12 09:29:50 -05006036 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
6037 if (!name_buf)
6038 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07006039
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006040 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006041 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07006042 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07006043 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07006044 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07006045
Tejun Heoa2dd4242014-03-19 10:23:55 -04006046 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07006047 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04006048 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006049 }
6050 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006051 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05006052 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006053 return 0;
6054}
6055
6056#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05006057static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006058{
Tejun Heo2da8ca82013-12-05 12:28:04 -05006059 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07006060 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07006061
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006062 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04006063 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07006064 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07006065 struct task_struct *task;
6066 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05006067
Tejun Heo5abb8852013-06-12 21:04:49 -07006068 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05006069
Tejun Heo5abb8852013-06-12 21:04:49 -07006070 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05006071 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6072 goto overflow;
6073 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07006074 }
Tejun Heoc7561122014-02-25 10:04:01 -05006075
6076 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
6077 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6078 goto overflow;
6079 seq_printf(seq, " task %d\n", task_pid_vnr(task));
6080 }
6081 continue;
6082 overflow:
6083 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07006084 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006085 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006086 return 0;
6087}
6088
Tejun Heo182446d2013-08-08 20:11:24 -04006089static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006090{
Tejun Heo27bd4db2015-10-15 16:41:50 -04006091 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08006092 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07006093}
6094
6095static struct cftype debug_files[] = {
6096 {
Paul Menagefe693432009-09-23 15:56:20 -07006097 .name = "taskcount",
6098 .read_u64 = debug_taskcount_read,
6099 },
6100
6101 {
6102 .name = "current_css_set",
6103 .read_u64 = current_css_set_read,
6104 },
6105
6106 {
6107 .name = "current_css_set_refcount",
6108 .read_u64 = current_css_set_refcount_read,
6109 },
6110
6111 {
Paul Menage7717f7b2009-09-23 15:56:22 -07006112 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006113 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006114 },
6115
6116 {
6117 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006118 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006119 },
6120
6121 {
Paul Menagefe693432009-09-23 15:56:20 -07006122 .name = "releasable",
6123 .read_u64 = releasable_read,
6124 },
Paul Menagefe693432009-09-23 15:56:20 -07006125
Tejun Heo4baf6e32012-04-01 12:09:55 -07006126 { } /* terminate */
6127};
Paul Menagefe693432009-09-23 15:56:20 -07006128
Tejun Heo073219e2014-02-08 10:36:58 -05006129struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006130 .css_alloc = debug_css_alloc,
6131 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04006132 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07006133};
6134#endif /* CONFIG_CGROUP_DEBUG */