blob: 452a90e455fa9340ef8300a9d488eee2b2be261c [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
Tejun Heoce3f1d92016-03-03 09:57:59 -0500576/* walk live descendants in preorder */
577#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
578 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
579 if (({ lockdep_assert_held(&cgroup_mutex); \
580 (dsct) = (d_css)->cgroup; \
581 cgroup_is_dead(dsct); })) \
582 ; \
583 else
584
585/* walk live descendants in postorder */
586#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
587 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
588 if (({ lockdep_assert_held(&cgroup_mutex); \
589 (dsct) = (d_css)->cgroup; \
590 cgroup_is_dead(dsct); })) \
591 ; \
592 else
593
Paul Menage81a6a5c2007-10-18 23:39:38 -0700594static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700595static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700596
Tejun Heo69d02062013-06-12 21:04:50 -0700597/*
598 * A cgroup can be associated with multiple css_sets as different tasks may
599 * belong to different cgroups on different hierarchies. In the other
600 * direction, a css_set is naturally associated with multiple cgroups.
601 * This M:N relationship is represented by the following link structure
602 * which exists for each association and allows traversing the associations
603 * from both sides.
604 */
605struct cgrp_cset_link {
606 /* the cgroup and css_set this link associates */
607 struct cgroup *cgrp;
608 struct css_set *cset;
609
610 /* list of cgrp_cset_links anchored at cgrp->cset_links */
611 struct list_head cset_link;
612
613 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
614 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700615};
616
Tejun Heo172a2c062014-03-19 10:23:53 -0400617/*
618 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700619 * hierarchies being mounted. It contains a pointer to the root state
620 * for each subsystem. Also used to anchor the list of css_sets. Not
621 * reference-counted, to improve performance when child cgroups
622 * haven't been created.
623 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400624struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400625 .refcount = ATOMIC_INIT(1),
626 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
627 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
628 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
629 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
630 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heoed27b9f2015-10-15 16:41:52 -0400631 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo172a2c062014-03-19 10:23:53 -0400632};
Paul Menage817929e2007-10-18 23:39:36 -0700633
Tejun Heo172a2c062014-03-19 10:23:53 -0400634static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700635
Tejun Heo842b5972014-04-25 18:28:02 -0400636/**
Tejun Heo0de09422015-10-15 16:41:49 -0400637 * css_set_populated - does a css_set contain any tasks?
638 * @cset: target css_set
639 */
640static bool css_set_populated(struct css_set *cset)
641{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400642 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400643
644 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
645}
646
647/**
Tejun Heo842b5972014-04-25 18:28:02 -0400648 * cgroup_update_populated - updated populated count of a cgroup
649 * @cgrp: the target cgroup
650 * @populated: inc or dec populated count
651 *
Tejun Heo0de09422015-10-15 16:41:49 -0400652 * One of the css_sets associated with @cgrp is either getting its first
653 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
654 * count is propagated towards root so that a given cgroup's populated_cnt
655 * is zero iff the cgroup and all its descendants don't contain any tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400656 *
657 * @cgrp's interface file "cgroup.populated" is zero if
658 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
659 * changes from or to zero, userland is notified that the content of the
660 * interface file has changed. This can be used to detect when @cgrp and
661 * its descendants become populated or empty.
662 */
663static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
664{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400665 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400666
667 do {
668 bool trigger;
669
670 if (populated)
671 trigger = !cgrp->populated_cnt++;
672 else
673 trigger = !--cgrp->populated_cnt;
674
675 if (!trigger)
676 break;
677
Tejun Heoad2ed2b2015-10-15 16:41:50 -0400678 check_for_release(cgrp);
Tejun Heo6f60ead2015-09-18 17:54:23 -0400679 cgroup_file_notify(&cgrp->events_file);
680
Tejun Heod51f39b2014-05-16 13:22:48 -0400681 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400682 } while (cgrp);
683}
684
Tejun Heo0de09422015-10-15 16:41:49 -0400685/**
686 * css_set_update_populated - update populated state of a css_set
687 * @cset: target css_set
688 * @populated: whether @cset is populated or depopulated
689 *
690 * @cset is either getting the first task or losing the last. Update the
691 * ->populated_cnt of all associated cgroups accordingly.
692 */
693static void css_set_update_populated(struct css_set *cset, bool populated)
694{
695 struct cgrp_cset_link *link;
696
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400697 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400698
699 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
700 cgroup_update_populated(link->cgrp, populated);
701}
702
Tejun Heof6d7d042015-10-15 16:41:52 -0400703/**
704 * css_set_move_task - move a task from one css_set to another
705 * @task: task being moved
706 * @from_cset: css_set @task currently belongs to (may be NULL)
707 * @to_cset: new css_set @task is being moved to (may be NULL)
708 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
709 *
710 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
711 * css_set, @from_cset can be NULL. If @task is being disassociated
712 * instead of moved, @to_cset can be NULL.
713 *
Tejun Heoed27b9f2015-10-15 16:41:52 -0400714 * This function automatically handles populated_cnt updates and
715 * css_task_iter adjustments but the caller is responsible for managing
716 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400717 */
718static void css_set_move_task(struct task_struct *task,
719 struct css_set *from_cset, struct css_set *to_cset,
720 bool use_mg_tasks)
721{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400722 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400723
Tejun Heo20b454a2016-03-03 09:57:57 -0500724 if (to_cset && !css_set_populated(to_cset))
725 css_set_update_populated(to_cset, true);
726
Tejun Heof6d7d042015-10-15 16:41:52 -0400727 if (from_cset) {
Tejun Heoed27b9f2015-10-15 16:41:52 -0400728 struct css_task_iter *it, *pos;
729
Tejun Heof6d7d042015-10-15 16:41:52 -0400730 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400731
732 /*
733 * @task is leaving, advance task iterators which are
734 * pointing to it so that they can resume at the next
735 * position. Advancing an iterator might remove it from
736 * the list, use safe walk. See css_task_iter_advance*()
737 * for details.
738 */
739 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
740 iters_node)
741 if (it->task_pos == &task->cg_list)
742 css_task_iter_advance(it);
743
Tejun Heof6d7d042015-10-15 16:41:52 -0400744 list_del_init(&task->cg_list);
745 if (!css_set_populated(from_cset))
746 css_set_update_populated(from_cset, false);
747 } else {
748 WARN_ON_ONCE(!list_empty(&task->cg_list));
749 }
750
751 if (to_cset) {
752 /*
753 * We are synchronized through cgroup_threadgroup_rwsem
754 * against PF_EXITING setting such that we can't race
755 * against cgroup_exit() changing the css_set to
756 * init_css_set and dropping the old one.
757 */
758 WARN_ON_ONCE(task->flags & PF_EXITING);
759
Tejun Heof6d7d042015-10-15 16:41:52 -0400760 rcu_assign_pointer(task->cgroups, to_cset);
761 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
762 &to_cset->tasks);
763 }
764}
765
Paul Menage7717f7b2009-09-23 15:56:22 -0700766/*
767 * hash table for cgroup groups. This improves the performance to find
768 * an existing css_set. This hash doesn't (currently) take into
769 * account cgroups in empty hierarchies.
770 */
Li Zefan472b1052008-04-29 01:00:11 -0700771#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800772static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700773
Li Zefan0ac801f2013-01-10 11:49:27 +0800774static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700775{
Li Zefan0ac801f2013-01-10 11:49:27 +0800776 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700777 struct cgroup_subsys *ss;
778 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700779
Tejun Heo30159ec2013-06-25 11:53:37 -0700780 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800781 key += (unsigned long)css[i];
782 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700783
Li Zefan0ac801f2013-01-10 11:49:27 +0800784 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700785}
786
Zefan Lia25eb522014-09-19 16:51:00 +0800787static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700788{
Tejun Heo69d02062013-06-12 21:04:50 -0700789 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400790 struct cgroup_subsys *ss;
791 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700792
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400793 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500794
795 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700796 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700797
Tejun Heo53254f92015-11-23 14:55:41 -0500798 /* This css_set is dead. unlink it and release cgroup and css refs */
799 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400800 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500801 css_put(cset->subsys[ssid]);
802 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700803 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700804 css_set_count--;
805
Tejun Heo69d02062013-06-12 21:04:50 -0700806 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700807 list_del(&link->cset_link);
808 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400809 if (cgroup_parent(link->cgrp))
810 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700811 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700812 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700813
Tejun Heo5abb8852013-06-12 21:04:49 -0700814 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700815}
816
Zefan Lia25eb522014-09-19 16:51:00 +0800817static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500818{
819 /*
820 * Ensure that the refcount doesn't hit zero while any readers
821 * can see it. Similar to atomic_dec_and_lock(), but for an
822 * rwlock
823 */
824 if (atomic_add_unless(&cset->refcount, -1, 1))
825 return;
826
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400827 spin_lock_bh(&css_set_lock);
Zefan Lia25eb522014-09-19 16:51:00 +0800828 put_css_set_locked(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400829 spin_unlock_bh(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500830}
831
Paul Menage817929e2007-10-18 23:39:36 -0700832/*
833 * refcounted get/put for css_set objects
834 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700835static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700836{
Tejun Heo5abb8852013-06-12 21:04:49 -0700837 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700838}
839
Tejun Heob326f9d2013-06-24 15:21:48 -0700840/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700841 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700842 * @cset: candidate css_set being tested
843 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700844 * @new_cgrp: cgroup that's being entered by the task
845 * @template: desired set of css pointers in css_set (pre-calculated)
846 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800847 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700848 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
849 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700850static bool compare_css_sets(struct css_set *cset,
851 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700852 struct cgroup *new_cgrp,
853 struct cgroup_subsys_state *template[])
854{
855 struct list_head *l1, *l2;
856
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400857 /*
858 * On the default hierarchy, there can be csets which are
859 * associated with the same set of cgroups but different csses.
860 * Let's first ensure that csses match.
861 */
862 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700863 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700864
865 /*
866 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400867 * different cgroups in hierarchies. As different cgroups may
868 * share the same effective css, this comparison is always
869 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700870 */
Tejun Heo69d02062013-06-12 21:04:50 -0700871 l1 = &cset->cgrp_links;
872 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700873 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700874 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700875 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700876
877 l1 = l1->next;
878 l2 = l2->next;
879 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700880 if (l1 == &cset->cgrp_links) {
881 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700882 break;
883 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700884 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700885 }
886 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700887 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
888 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
889 cgrp1 = link1->cgrp;
890 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700891 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700892 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700893
894 /*
895 * If this hierarchy is the hierarchy of the cgroup
896 * that's changing, then we need to check that this
897 * css_set points to the new cgroup; if it's any other
898 * hierarchy, then this css_set should point to the
899 * same cgroup as the old css_set.
900 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700901 if (cgrp1->root == new_cgrp->root) {
902 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700903 return false;
904 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700905 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700906 return false;
907 }
908 }
909 return true;
910}
911
Tejun Heob326f9d2013-06-24 15:21:48 -0700912/**
913 * find_existing_css_set - init css array and find the matching css_set
914 * @old_cset: the css_set that we're using before the cgroup transition
915 * @cgrp: the cgroup that we're moving into
916 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700917 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700918static struct css_set *find_existing_css_set(struct css_set *old_cset,
919 struct cgroup *cgrp,
920 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700921{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400922 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700923 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700924 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800925 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700926 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700927
Ben Blumaae8aab2010-03-10 15:22:07 -0800928 /*
929 * Build the set of subsystem state objects that we want to see in the
930 * new css_set. while subsystems can change globally, the entries here
931 * won't change, so no need for locking.
932 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700933 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400934 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400935 /*
936 * @ss is in this hierarchy, so we want the
937 * effective css from @cgrp.
938 */
939 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700940 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400941 /*
942 * @ss is not in this hierarchy, so we don't want
943 * to change the css.
944 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700945 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700946 }
947 }
948
Li Zefan0ac801f2013-01-10 11:49:27 +0800949 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700950 hash_for_each_possible(css_set_table, cset, hlist, key) {
951 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700952 continue;
953
954 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700955 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700956 }
Paul Menage817929e2007-10-18 23:39:36 -0700957
958 /* No existing cgroup group matched */
959 return NULL;
960}
961
Tejun Heo69d02062013-06-12 21:04:50 -0700962static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700963{
Tejun Heo69d02062013-06-12 21:04:50 -0700964 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700965
Tejun Heo69d02062013-06-12 21:04:50 -0700966 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
967 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700968 kfree(link);
969 }
970}
971
Tejun Heo69d02062013-06-12 21:04:50 -0700972/**
973 * allocate_cgrp_cset_links - allocate cgrp_cset_links
974 * @count: the number of links to allocate
975 * @tmp_links: list_head the allocated links are put on
976 *
977 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
978 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700979 */
Tejun Heo69d02062013-06-12 21:04:50 -0700980static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700981{
Tejun Heo69d02062013-06-12 21:04:50 -0700982 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700983 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700984
985 INIT_LIST_HEAD(tmp_links);
986
Li Zefan36553432008-07-29 22:33:19 -0700987 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700988 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700989 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700990 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700991 return -ENOMEM;
992 }
Tejun Heo69d02062013-06-12 21:04:50 -0700993 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700994 }
995 return 0;
996}
997
Li Zefanc12f65d2009-01-07 18:07:42 -0800998/**
999 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -07001000 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -07001001 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -08001002 * @cgrp: the destination cgroup
1003 */
Tejun Heo69d02062013-06-12 21:04:50 -07001004static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1005 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -08001006{
Tejun Heo69d02062013-06-12 21:04:50 -07001007 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -08001008
Tejun Heo69d02062013-06-12 21:04:50 -07001009 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -04001010
1011 if (cgroup_on_dfl(cgrp))
1012 cset->dfl_cgrp = cgrp;
1013
Tejun Heo69d02062013-06-12 21:04:50 -07001014 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1015 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07001016 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -04001017
Paul Menage7717f7b2009-09-23 15:56:22 -07001018 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001019 * Always add links to the tail of the lists so that the lists are
1020 * in choronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001021 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001022 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001023 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001024
1025 if (cgroup_parent(cgrp))
1026 cgroup_get(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001027}
1028
Tejun Heob326f9d2013-06-24 15:21:48 -07001029/**
1030 * find_css_set - return a new css_set with one cgroup updated
1031 * @old_cset: the baseline css_set
1032 * @cgrp: the cgroup to be updated
1033 *
1034 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1035 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001036 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001037static struct css_set *find_css_set(struct css_set *old_cset,
1038 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001039{
Tejun Heob326f9d2013-06-24 15:21:48 -07001040 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001041 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001042 struct list_head tmp_links;
1043 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001044 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001045 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001046 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001047
Tejun Heob326f9d2013-06-24 15:21:48 -07001048 lockdep_assert_held(&cgroup_mutex);
1049
Paul Menage817929e2007-10-18 23:39:36 -07001050 /* First see if we already have a cgroup group that matches
1051 * the desired set */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001052 spin_lock_bh(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001053 cset = find_existing_css_set(old_cset, cgrp, template);
1054 if (cset)
1055 get_css_set(cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001056 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001057
Tejun Heo5abb8852013-06-12 21:04:49 -07001058 if (cset)
1059 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001060
Tejun Heof4f4be22013-06-12 21:04:51 -07001061 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001062 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001063 return NULL;
1064
Tejun Heo69d02062013-06-12 21:04:50 -07001065 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001066 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001067 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001068 return NULL;
1069 }
1070
Tejun Heo5abb8852013-06-12 21:04:49 -07001071 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07001072 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -07001073 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001074 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001075 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001076 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001077 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo5abb8852013-06-12 21:04:49 -07001078 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -07001079
1080 /* Copy the set of subsystem state objects generated in
1081 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001082 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001083
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001084 spin_lock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001085 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001086 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001087 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001088
Paul Menage7717f7b2009-09-23 15:56:22 -07001089 if (c->root == cgrp->root)
1090 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001091 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001092 }
Paul Menage817929e2007-10-18 23:39:36 -07001093
Tejun Heo69d02062013-06-12 21:04:50 -07001094 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001095
Paul Menage817929e2007-10-18 23:39:36 -07001096 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001097
Tejun Heo2d8f2432014-04-23 11:13:15 -04001098 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001099 key = css_set_hash(cset->subsys);
1100 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001101
Tejun Heo53254f92015-11-23 14:55:41 -05001102 for_each_subsys(ss, ssid) {
1103 struct cgroup_subsys_state *css = cset->subsys[ssid];
1104
Tejun Heo2d8f2432014-04-23 11:13:15 -04001105 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001106 &css->cgroup->e_csets[ssid]);
1107 css_get(css);
1108 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001109
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001110 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001111
Tejun Heo5abb8852013-06-12 21:04:49 -07001112 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001113}
1114
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001115static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001116{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001117 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001118
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001119 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001120}
1121
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001122static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001123{
1124 int id;
1125
1126 lockdep_assert_held(&cgroup_mutex);
1127
Tejun Heo985ed672014-03-19 10:23:53 -04001128 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001129 if (id < 0)
1130 return id;
1131
1132 root->hierarchy_id = id;
1133 return 0;
1134}
1135
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001136static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001137{
1138 lockdep_assert_held(&cgroup_mutex);
1139
1140 if (root->hierarchy_id) {
1141 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1142 root->hierarchy_id = 0;
1143 }
1144}
1145
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001146static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001147{
1148 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -04001149 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -05001150 WARN_ON_ONCE(root->hierarchy_id);
1151
1152 idr_destroy(&root->cgroup_idr);
1153 kfree(root);
1154 }
1155}
1156
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001157static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001158{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001159 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001160 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001161
Tejun Heo2bd59d42014-02-11 11:52:49 -05001162 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001163
Tejun Heo776f02f2014-02-12 09:29:50 -05001164 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001165 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001166
Tejun Heof2e85d52014-02-11 11:52:49 -05001167 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -04001168 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -05001169
1170 /*
1171 * Release all the links from cset_links to this hierarchy's
1172 * root cgroup
1173 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001174 spin_lock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001175
1176 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1177 list_del(&link->cset_link);
1178 list_del(&link->cgrp_link);
1179 kfree(link);
1180 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001181
1182 spin_unlock_bh(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001183
1184 if (!list_empty(&root->root_list)) {
1185 list_del(&root->root_list);
1186 cgroup_root_count--;
1187 }
1188
1189 cgroup_exit_root_id(root);
1190
1191 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001192
Tejun Heo2bd59d42014-02-11 11:52:49 -05001193 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001194 cgroup_free_root(root);
1195}
1196
Tejun Heoceb6a082014-02-25 10:04:02 -05001197/* look up cgroup associated with given css_set on the specified hierarchy */
1198static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001199 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001200{
Paul Menage7717f7b2009-09-23 15:56:22 -07001201 struct cgroup *res = NULL;
1202
Tejun Heo96d365e2014-02-13 06:58:40 -05001203 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001204 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001205
Tejun Heo5abb8852013-06-12 21:04:49 -07001206 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001207 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001208 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001209 struct cgrp_cset_link *link;
1210
1211 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001212 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001213
Paul Menage7717f7b2009-09-23 15:56:22 -07001214 if (c->root == root) {
1215 res = c;
1216 break;
1217 }
1218 }
1219 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001220
Paul Menage7717f7b2009-09-23 15:56:22 -07001221 BUG_ON(!res);
1222 return res;
1223}
1224
1225/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001226 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001227 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001228 */
1229static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001230 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001231{
1232 /*
1233 * No need to lock the task - since we hold cgroup_mutex the
1234 * task can't change groups, so the only thing that can happen
1235 * is that it exits and its css is set back to init_css_set.
1236 */
1237 return cset_cgroup_from_root(task_css_set(task), root);
1238}
1239
1240/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241 * A task must hold cgroup_mutex to modify cgroups.
1242 *
1243 * Any task can increment and decrement the count field without lock.
1244 * So in general, code holding cgroup_mutex can't rely on the count
1245 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001246 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 * means that no tasks are currently attached, therefore there is no
1248 * way a task attached to that cgroup can fork (the other way to
1249 * increment the count). So code holding cgroup_mutex can safely
1250 * assume that if the count is zero, it will stay zero. Similarly, if
1251 * a task holds cgroup_mutex on a cgroup with zero count, it
1252 * knows that the cgroup won't be removed, as cgroup_rmdir()
1253 * needs that mutex.
1254 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255 * A cgroup can only be deleted if both its 'count' of using tasks
1256 * is zero, and its list of 'children' cgroups is empty. Since all
1257 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001258 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001260 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261 *
1262 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001263 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264 */
1265
Tejun Heo2bd59d42014-02-11 11:52:49 -05001266static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001267static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001268
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001269static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1270 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001272 struct cgroup_subsys *ss = cft->ss;
1273
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001274 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1275 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1276 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001277 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1278 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001279 else
1280 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1281 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001282}
1283
Tejun Heof2e85d52014-02-11 11:52:49 -05001284/**
1285 * cgroup_file_mode - deduce file mode of a control file
1286 * @cft: the control file in question
1287 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001288 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001289 */
1290static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001291{
Tejun Heof2e85d52014-02-11 11:52:49 -05001292 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001293
Tejun Heof2e85d52014-02-11 11:52:49 -05001294 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1295 mode |= S_IRUGO;
1296
Tejun Heo7dbdb192015-09-18 17:54:23 -04001297 if (cft->write_u64 || cft->write_s64 || cft->write) {
1298 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1299 mode |= S_IWUGO;
1300 else
1301 mode |= S_IWUSR;
1302 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001303
1304 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001305}
1306
Tejun Heoa9746d82014-05-13 12:19:22 -04001307/**
Tejun Heo8699b772016-02-22 22:25:46 -05001308 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001309 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001310 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001311 *
1312 * On the default hierarchy, a subsystem may request other subsystems to be
1313 * enabled together through its ->depends_on mask. In such cases, more
1314 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1315 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001316 * This function calculates which subsystems need to be enabled if
1317 * @subtree_control is to be applied to @cgrp. The returned mask is always
1318 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001319 */
Tejun Heo6e5c8302016-02-22 22:25:47 -05001320static u16 cgroup_calc_subtree_ss_mask(struct cgroup *cgrp, u16 subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001321{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001322 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001323 struct cgroup_subsys *ss;
1324 int ssid;
1325
1326 lockdep_assert_held(&cgroup_mutex);
1327
Tejun Heo0f060de2014-11-18 02:49:50 -05001328 if (!cgroup_on_dfl(cgrp))
1329 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001330
1331 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001332 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001333
Tejun Heob4e0eea2016-02-22 22:25:46 -05001334 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001335 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001336 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001337
1338 /*
1339 * Mask out subsystems which aren't available. This can
1340 * happen only if some depended-upon subsystems were bound
1341 * to non-default hierarchies.
1342 */
Tejun Heo5531dc92016-03-03 09:57:58 -05001343 new_ss_mask &= cgroup_ss_mask(cgrp);
Tejun Heoaf0ba672014-07-08 18:02:57 -04001344
1345 if (new_ss_mask == cur_ss_mask)
1346 break;
1347 cur_ss_mask = new_ss_mask;
1348 }
1349
Tejun Heo0f060de2014-11-18 02:49:50 -05001350 return cur_ss_mask;
1351}
1352
1353/**
Tejun Heo8699b772016-02-22 22:25:46 -05001354 * cgroup_refresh_subtree_ss_mask - update subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001355 * @cgrp: the target cgroup
1356 *
Tejun Heo8699b772016-02-22 22:25:46 -05001357 * Update @cgrp->subtree_ss_mask according to the current
1358 * @cgrp->subtree_control using cgroup_calc_subtree_ss_mask().
Tejun Heo0f060de2014-11-18 02:49:50 -05001359 */
Tejun Heo8699b772016-02-22 22:25:46 -05001360static void cgroup_refresh_subtree_ss_mask(struct cgroup *cgrp)
Tejun Heo0f060de2014-11-18 02:49:50 -05001361{
Tejun Heo8699b772016-02-22 22:25:46 -05001362 cgrp->subtree_ss_mask =
1363 cgroup_calc_subtree_ss_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001364}
1365
Tejun Heoa9746d82014-05-13 12:19:22 -04001366/**
1367 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1368 * @kn: the kernfs_node being serviced
1369 *
1370 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1371 * the method finishes if locking succeeded. Note that once this function
1372 * returns the cgroup returned by cgroup_kn_lock_live() may become
1373 * inaccessible any time. If the caller intends to continue to access the
1374 * cgroup, it should pin it before invoking this function.
1375 */
1376static void cgroup_kn_unlock(struct kernfs_node *kn)
1377{
1378 struct cgroup *cgrp;
1379
1380 if (kernfs_type(kn) == KERNFS_DIR)
1381 cgrp = kn->priv;
1382 else
1383 cgrp = kn->parent->priv;
1384
1385 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001386
1387 kernfs_unbreak_active_protection(kn);
1388 cgroup_put(cgrp);
1389}
1390
1391/**
1392 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1393 * @kn: the kernfs_node being serviced
1394 *
1395 * This helper is to be used by a cgroup kernfs method currently servicing
1396 * @kn. It breaks the active protection, performs cgroup locking and
1397 * verifies that the associated cgroup is alive. Returns the cgroup if
1398 * alive; otherwise, %NULL. A successful return should be undone by a
1399 * matching cgroup_kn_unlock() invocation.
1400 *
1401 * Any cgroup kernfs method implementation which requires locking the
1402 * associated cgroup should use this helper. It avoids nesting cgroup
1403 * locking under kernfs active protection and allows all kernfs operations
1404 * including self-removal.
1405 */
1406static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1407{
1408 struct cgroup *cgrp;
1409
1410 if (kernfs_type(kn) == KERNFS_DIR)
1411 cgrp = kn->priv;
1412 else
1413 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414
Tejun Heo2bd59d42014-02-11 11:52:49 -05001415 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001416 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001417 * active_ref. cgroup liveliness check alone provides enough
1418 * protection against removal. Ensure @cgrp stays accessible and
1419 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001420 */
Li Zefanaa323622014-09-04 14:43:38 +08001421 if (!cgroup_tryget(cgrp))
1422 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001423 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424
Tejun Heoa9746d82014-05-13 12:19:22 -04001425 mutex_lock(&cgroup_mutex);
1426
1427 if (!cgroup_is_dead(cgrp))
1428 return cgrp;
1429
1430 cgroup_kn_unlock(kn);
1431 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432}
1433
Li Zefan2739d3c2013-01-21 18:18:33 +08001434static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001436 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001437
Tejun Heo01f64742014-05-13 12:19:23 -04001438 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001439
1440 if (cft->file_offset) {
1441 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1442 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1443
1444 spin_lock_irq(&cgroup_file_kn_lock);
1445 cfile->kn = NULL;
1446 spin_unlock_irq(&cgroup_file_kn_lock);
1447 }
1448
Tejun Heo2bd59d42014-02-11 11:52:49 -05001449 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001450}
1451
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001452/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001453 * css_clear_dir - remove subsys files in a cgroup directory
1454 * @css: taget css
1455 * @cgrp_override: specify if target cgroup is different from css->cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001456 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001457static void css_clear_dir(struct cgroup_subsys_state *css,
1458 struct cgroup *cgrp_override)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001459{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001460 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1461 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001462
Tejun Heo88cb04b2016-03-03 09:57:58 -05001463 if (!(css->flags & CSS_VISIBLE))
1464 return;
1465
1466 css->flags &= ~CSS_VISIBLE;
1467
Tejun Heo4df8dc92015-09-18 17:54:23 -04001468 list_for_each_entry(cfts, &css->ss->cfts, node)
1469 cgroup_addrm_files(css, cgrp, cfts, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470}
1471
Tejun Heoccdca212015-09-18 17:54:23 -04001472/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001473 * css_populate_dir - create subsys files in a cgroup directory
1474 * @css: target css
1475 * @cgrp_overried: specify if target cgroup is different from css->cgroup
Tejun Heoccdca212015-09-18 17:54:23 -04001476 *
1477 * On failure, no file is added.
1478 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001479static int css_populate_dir(struct cgroup_subsys_state *css,
1480 struct cgroup *cgrp_override)
Tejun Heoccdca212015-09-18 17:54:23 -04001481{
Tejun Heo4df8dc92015-09-18 17:54:23 -04001482 struct cgroup *cgrp = cgrp_override ?: css->cgroup;
1483 struct cftype *cfts, *failed_cfts;
1484 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001485
Tejun Heo88cb04b2016-03-03 09:57:58 -05001486 if (css->flags & CSS_VISIBLE)
1487 return 0;
1488
Tejun Heo4df8dc92015-09-18 17:54:23 -04001489 if (!css->ss) {
1490 if (cgroup_on_dfl(cgrp))
1491 cfts = cgroup_dfl_base_files;
1492 else
1493 cfts = cgroup_legacy_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001494
Tejun Heo4df8dc92015-09-18 17:54:23 -04001495 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1496 }
Tejun Heoccdca212015-09-18 17:54:23 -04001497
Tejun Heo4df8dc92015-09-18 17:54:23 -04001498 list_for_each_entry(cfts, &css->ss->cfts, node) {
1499 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1500 if (ret < 0) {
1501 failed_cfts = cfts;
1502 goto err;
Tejun Heoccdca212015-09-18 17:54:23 -04001503 }
1504 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001505
1506 css->flags |= CSS_VISIBLE;
1507
Tejun Heoccdca212015-09-18 17:54:23 -04001508 return 0;
1509err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001510 list_for_each_entry(cfts, &css->ss->cfts, node) {
1511 if (cfts == failed_cfts)
1512 break;
1513 cgroup_addrm_files(css, cgrp, cfts, false);
1514 }
Tejun Heoccdca212015-09-18 17:54:23 -04001515 return ret;
1516}
1517
Tejun Heo6e5c8302016-02-22 22:25:47 -05001518static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519{
Tejun Heo1ada4832015-09-18 17:54:23 -04001520 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001521 struct cgroup_subsys *ss;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001522 u16 tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001523 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
Tejun Heoace2bee2014-02-11 11:52:47 -05001525 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001526
Tejun Heob4e0eea2016-02-22 22:25:46 -05001527 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001528 /* if @ss has non-root csses attached to it, can't move */
1529 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001530 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
Tejun Heo5df36032014-03-19 10:23:54 -04001532 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001533 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001534 return -EBUSY;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001535 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536
Tejun Heo5533e012014-05-14 19:33:07 -04001537 /* skip creating root files on dfl_root for inhibited subsystems */
1538 tmp_ss_mask = ss_mask;
1539 if (dst_root == &cgrp_dfl_root)
Tejun Heoa7165262016-02-23 10:00:50 -05001540 tmp_ss_mask &= ~cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -04001541
Tejun Heob4e0eea2016-02-22 22:25:46 -05001542 do_each_subsys_mask(ss, ssid, tmp_ss_mask) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04001543 struct cgroup *scgrp = &ss->root->cgrp;
1544 int tssid;
1545
1546 ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
1547 if (!ret)
1548 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
Tejun Heoa2dd4242014-03-19 10:23:55 -04001550 /*
1551 * Rebinding back to the default root is not allowed to
1552 * fail. Using both default and non-default roots should
1553 * be rare. Moving subsystems back and forth even more so.
1554 * Just warn about it and continue.
1555 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04001556 if (dst_root == &cgrp_dfl_root) {
Tejun Heoa7165262016-02-23 10:00:50 -05001557 if (cgrp_dfl_visible) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001558 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Tejun Heo4df8dc92015-09-18 17:54:23 -04001559 ret, ss_mask);
1560 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1561 }
1562 continue;
Tejun Heoa2dd4242014-03-19 10:23:55 -04001563 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001564
Tejun Heob4e0eea2016-02-22 22:25:46 -05001565 do_each_subsys_mask(ss, tssid, tmp_ss_mask) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04001566 if (tssid == ssid)
1567 break;
1568 css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001569 } while_each_subsys_mask();
Tejun Heo4df8dc92015-09-18 17:54:23 -04001570 return ret;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001571 } while_each_subsys_mask();
Tejun Heo31261212013-06-28 17:07:30 -07001572
1573 /*
1574 * Nothing can fail from this point on. Remove files for the
1575 * removed subsystems and rebind each subsystem.
1576 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05001577 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001578 struct cgroup_root *src_root = ss->root;
1579 struct cgroup *scgrp = &src_root->cgrp;
1580 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001581 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001582
Tejun Heo1ada4832015-09-18 17:54:23 -04001583 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001584
Tejun Heo4df8dc92015-09-18 17:54:23 -04001585 css_clear_dir(css, NULL);
1586
Tejun Heo1ada4832015-09-18 17:54:23 -04001587 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1588 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001589 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001590 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001591
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001592 spin_lock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001593 hash_for_each(css_set_table, i, cset, hlist)
1594 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001595 &dcgrp->e_csets[ss->id]);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001596 spin_unlock_bh(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001597
Tejun Heof392e512014-04-23 11:13:14 -04001598 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo1ada4832015-09-18 17:54:23 -04001599 scgrp->subtree_control &= ~(1 << ssid);
Tejun Heo8699b772016-02-22 22:25:46 -05001600 cgroup_refresh_subtree_ss_mask(scgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001601
Tejun Heobd53d612014-04-23 11:13:16 -04001602 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001603 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001604 if (dst_root == &cgrp_dfl_root) {
1605 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1606 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001607 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo8699b772016-02-22 22:25:46 -05001608 cgroup_refresh_subtree_ss_mask(dcgrp);
Tejun Heo49d1dc42015-09-18 11:56:28 -04001609 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001610 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001611
Tejun Heo5df36032014-03-19 10:23:54 -04001612 if (ss->bind)
1613 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001614 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001615
Tejun Heo1ada4832015-09-18 17:54:23 -04001616 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617 return 0;
1618}
1619
Tejun Heo2bd59d42014-02-11 11:52:49 -05001620static int cgroup_show_options(struct seq_file *seq,
1621 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001623 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001625 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Tejun Heod98817d2015-08-18 13:58:16 -07001627 if (root != &cgrp_dfl_root)
1628 for_each_subsys(ss, ssid)
1629 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001630 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001631 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001633 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001634 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001635
1636 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001637 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001638 seq_show_option(seq, "release_agent",
1639 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001640 spin_unlock(&release_agent_path_lock);
1641
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001642 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001643 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001644 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001645 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646 return 0;
1647}
1648
1649struct cgroup_sb_opts {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001650 u16 subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001651 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001652 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001653 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001654 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001655 /* User explicitly requested empty subsystem */
1656 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657};
1658
Ben Blumcf5d5942010-03-10 15:22:09 -08001659static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001661 char *token, *o = data;
1662 bool all_ss = false, one_ss = false;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001663 u16 mask = U16_MAX;
Tejun Heo30159ec2013-06-25 11:53:37 -07001664 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001665 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001666 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001667
1668#ifdef CONFIG_CPUSETS
Tejun Heo6e5c8302016-02-22 22:25:47 -05001669 mask = ~((u16)1 << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001670#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671
Paul Menagec6d57f32009-09-23 15:56:19 -07001672 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673
1674 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001675 nr_opts++;
1676
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 if (!*token)
1678 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001679 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001680 /* Explicitly have no subsystems */
1681 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001682 continue;
1683 }
1684 if (!strcmp(token, "all")) {
1685 /* Mutually exclusive option 'all' + subsystem name */
1686 if (one_ss)
1687 return -EINVAL;
1688 all_ss = true;
1689 continue;
1690 }
1691 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001692 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001693 continue;
1694 }
1695 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001696 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001697 continue;
1698 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001699 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001700 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001701 continue;
1702 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001703 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001704 /* Specifying two release agents is forbidden */
1705 if (opts->release_agent)
1706 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001707 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001708 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001709 if (!opts->release_agent)
1710 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001711 continue;
1712 }
1713 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001714 const char *name = token + 5;
1715 /* Can't specify an empty name */
1716 if (!strlen(name))
1717 return -EINVAL;
1718 /* Must match [\w.-]+ */
1719 for (i = 0; i < strlen(name); i++) {
1720 char c = name[i];
1721 if (isalnum(c))
1722 continue;
1723 if ((c == '.') || (c == '-') || (c == '_'))
1724 continue;
1725 return -EINVAL;
1726 }
1727 /* Specifying two names is forbidden */
1728 if (opts->name)
1729 return -EINVAL;
1730 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001731 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001732 GFP_KERNEL);
1733 if (!opts->name)
1734 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001735
1736 continue;
1737 }
1738
Tejun Heo30159ec2013-06-25 11:53:37 -07001739 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001740 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001741 continue;
Tejun Heofc5ed1e2015-09-18 11:56:28 -04001742 if (!cgroup_ssid_enabled(i))
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001743 continue;
Johannes Weiner223ffb22016-02-11 13:34:49 -05001744 if (cgroup_ssid_no_v1(i))
1745 continue;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001746
1747 /* Mutually exclusive option 'all' + subsystem name */
1748 if (all_ss)
1749 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001750 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001751 one_ss = true;
1752
1753 break;
1754 }
1755 if (i == CGROUP_SUBSYS_COUNT)
1756 return -ENOENT;
1757 }
1758
Li Zefanf9ab5b52009-06-17 16:26:33 -07001759 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001760 * If the 'all' option was specified select all the subsystems,
1761 * otherwise if 'none', 'name=' and a subsystem name options were
1762 * not specified, let's default to 'all'
1763 */
1764 if (all_ss || (!one_ss && !opts->none && !opts->name))
1765 for_each_subsys(ss, i)
Johannes Weiner223ffb22016-02-11 13:34:49 -05001766 if (cgroup_ssid_enabled(i) && !cgroup_ssid_no_v1(i))
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001767 opts->subsys_mask |= (1 << i);
1768
1769 /*
1770 * We either have to specify by name or by subsystems. (So all
1771 * empty hierarchies must have a name).
1772 */
1773 if (!opts->subsys_mask && !opts->name)
1774 return -EINVAL;
1775
1776 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001777 * Option noprefix was introduced just for backward compatibility
1778 * with the old cpuset, so we allow noprefix only if mounting just
1779 * the cpuset subsystem.
1780 */
Tejun Heo93438622013-04-14 20:15:25 -07001781 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001782 return -EINVAL;
1783
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001784 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001785 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001786 return -EINVAL;
1787
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 return 0;
1789}
1790
Tejun Heo2bd59d42014-02-11 11:52:49 -05001791static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792{
1793 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001794 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 struct cgroup_sb_opts opts;
Tejun Heo6e5c8302016-02-22 22:25:47 -05001796 u16 added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797
Tejun Heoaa6ec292014-07-09 10:08:08 -04001798 if (root == &cgrp_dfl_root) {
1799 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001800 return -EINVAL;
1801 }
1802
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 mutex_lock(&cgroup_mutex);
1804
1805 /* See what subsystems are wanted */
1806 ret = parse_cgroupfs_options(data, &opts);
1807 if (ret)
1808 goto out_unlock;
1809
Tejun Heof392e512014-04-23 11:13:14 -04001810 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001811 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001812 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001813
Tejun Heof392e512014-04-23 11:13:14 -04001814 added_mask = opts.subsys_mask & ~root->subsys_mask;
1815 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001816
Ben Blumcf5d5942010-03-10 15:22:09 -08001817 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001818 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001819 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001820 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001821 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001823 goto out_unlock;
1824 }
1825
Tejun Heof172e672013-06-28 17:07:30 -07001826 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001827 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001828 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001829 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001830 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001831
Tejun Heo5df36032014-03-19 10:23:54 -04001832 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001833 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001834 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001836 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001837
Tejun Heo69e943b2014-02-08 10:36:58 -05001838 if (opts.release_agent) {
1839 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001840 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001841 spin_unlock(&release_agent_path_lock);
1842 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001843 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001844 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001845 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001846 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001847 return ret;
1848}
1849
Tejun Heoafeb0f92014-02-13 06:58:39 -05001850/*
1851 * To reduce the fork() overhead for systems that are not actually using
1852 * their cgroups capability, we don't maintain the lists running through
1853 * each css_set to its tasks until we see the list actually used - in other
1854 * words after the first mount.
1855 */
1856static bool use_task_css_set_links __read_mostly;
1857
1858static void cgroup_enable_task_cg_lists(void)
1859{
1860 struct task_struct *p, *g;
1861
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001862 spin_lock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001863
1864 if (use_task_css_set_links)
1865 goto out_unlock;
1866
1867 use_task_css_set_links = true;
1868
1869 /*
1870 * We need tasklist_lock because RCU is not safe against
1871 * while_each_thread(). Besides, a forking task that has passed
1872 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1873 * is not guaranteed to have its child immediately visible in the
1874 * tasklist if we walk through it with RCU.
1875 */
1876 read_lock(&tasklist_lock);
1877 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001878 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1879 task_css_set(p) != &init_css_set);
1880
1881 /*
1882 * We should check if the process is exiting, otherwise
1883 * it will race with cgroup_exit() in that the list
1884 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001885 * Do it while holding siglock so that we don't end up
1886 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001887 */
Tejun Heof153ad12014-02-25 09:56:49 -05001888 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001889 if (!(p->flags & PF_EXITING)) {
1890 struct css_set *cset = task_css_set(p);
1891
Tejun Heo0de09422015-10-15 16:41:49 -04001892 if (!css_set_populated(cset))
1893 css_set_update_populated(cset, true);
Tejun Heo389b9c12015-10-15 16:41:51 -04001894 list_add_tail(&p->cg_list, &cset->tasks);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001895 get_css_set(cset);
1896 }
Tejun Heof153ad12014-02-25 09:56:49 -05001897 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001898 } while_each_thread(g, p);
1899 read_unlock(&tasklist_lock);
1900out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001901 spin_unlock_bh(&css_set_lock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001902}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001903
Paul Menagecc31edc2008-10-18 20:28:04 -07001904static void init_cgroup_housekeeping(struct cgroup *cgrp)
1905{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001906 struct cgroup_subsys *ss;
1907 int ssid;
1908
Tejun Heod5c419b2014-05-16 13:22:48 -04001909 INIT_LIST_HEAD(&cgrp->self.sibling);
1910 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001911 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001912 INIT_LIST_HEAD(&cgrp->pidlists);
1913 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001914 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001915 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001916
1917 for_each_subsys(ss, ssid)
1918 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001919
1920 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001921 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001922}
Paul Menagec6d57f32009-09-23 15:56:19 -07001923
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001924static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001925 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001926{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001927 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001928
Paul Menageddbcc7e2007-10-18 23:39:30 -07001929 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001930 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001931 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001932 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001933 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001934
Paul Menagec6d57f32009-09-23 15:56:19 -07001935 root->flags = opts->flags;
1936 if (opts->release_agent)
1937 strcpy(root->release_agent_path, opts->release_agent);
1938 if (opts->name)
1939 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001940 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001941 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001942}
1943
Tejun Heo6e5c8302016-02-22 22:25:47 -05001944static int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001945{
Tejun Heod427dfe2014-02-11 11:52:48 -05001946 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001947 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001948 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001949 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001950
Tejun Heod427dfe2014-02-11 11:52:48 -05001951 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001952
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001953 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001954 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001955 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001956 root_cgrp->id = ret;
Tejun Heob11cfb52015-11-20 15:55:52 -05001957 root_cgrp->ancestor_ids[0] = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001958
Tejun Heo2aad2a82014-09-24 13:31:50 -04001959 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1960 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001961 if (ret)
1962 goto out;
1963
Tejun Heod427dfe2014-02-11 11:52:48 -05001964 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001965 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001966 * but that's OK - it can only be increased by someone holding
1967 * cgroup_lock, and that's us. The worst that can happen is that we
1968 * have some link structures left over
1969 */
1970 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001971 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001972 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001973
Tejun Heo985ed672014-03-19 10:23:53 -04001974 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001975 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001976 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001977
Tejun Heo2bd59d42014-02-11 11:52:49 -05001978 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1979 KERNFS_ROOT_CREATE_DEACTIVATED,
1980 root_cgrp);
1981 if (IS_ERR(root->kf_root)) {
1982 ret = PTR_ERR(root->kf_root);
1983 goto exit_root_id;
1984 }
1985 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001986
Tejun Heo4df8dc92015-09-18 17:54:23 -04001987 ret = css_populate_dir(&root_cgrp->self, NULL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001988 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001989 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001990
Tejun Heo5df36032014-03-19 10:23:54 -04001991 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001992 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001993 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001994
Tejun Heod427dfe2014-02-11 11:52:48 -05001995 /*
1996 * There must be no failure case after here, since rebinding takes
1997 * care of subsystems' refcounts, which are explicitly dropped in
1998 * the failure exit path.
1999 */
2000 list_add(&root->root_list, &cgroup_roots);
2001 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002002
Tejun Heod427dfe2014-02-11 11:52:48 -05002003 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002004 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05002005 * objects.
2006 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002007 spin_lock_bh(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04002008 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05002009 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04002010 if (css_set_populated(cset))
2011 cgroup_update_populated(root_cgrp, true);
2012 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002013 spin_unlock_bh(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002014
Tejun Heod5c419b2014-05-16 13:22:48 -04002015 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05002016 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05002017
Tejun Heo2bd59d42014-02-11 11:52:49 -05002018 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05002019 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002020 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05002021
Tejun Heo2bd59d42014-02-11 11:52:49 -05002022destroy_root:
2023 kernfs_destroy_root(root->kf_root);
2024 root->kf_root = NULL;
2025exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05002026 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04002027cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04002028 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002029out:
Tejun Heod427dfe2014-02-11 11:52:48 -05002030 free_cgrp_cset_links(&tmp_links);
2031 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002032}
2033
Al Virof7e83572010-07-26 13:23:11 +04002034static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002035 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04002036 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002037{
Tejun Heo67e9c742015-11-16 11:13:34 -05002038 bool is_v2 = fs_type == &cgroup2_fs_type;
Li Zefan3a32bd72014-06-30 11:50:59 +08002039 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08002040 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002041 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002042 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002043 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002044 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08002045 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002046 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07002047
2048 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05002049 * The first time anyone tries to mount a cgroup, enable the list
2050 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07002051 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05002052 if (!use_task_css_set_links)
2053 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08002054
Tejun Heo67e9c742015-11-16 11:13:34 -05002055 if (is_v2) {
2056 if (data) {
2057 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
2058 return ERR_PTR(-EINVAL);
2059 }
Tejun Heoa7165262016-02-23 10:00:50 -05002060 cgrp_dfl_visible = true;
Tejun Heo67e9c742015-11-16 11:13:34 -05002061 root = &cgrp_dfl_root;
2062 cgroup_get(&root->cgrp);
2063 goto out_mount;
2064 }
2065
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002066 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002067
Paul Menageddbcc7e2007-10-18 23:39:30 -07002068 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002069 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002070 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002071 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04002072
Li Zefan970317a2014-06-30 11:49:58 +08002073 /*
2074 * Destruction of cgroup root is asynchronous, so subsystems may
2075 * still be dying after the previous unmount. Let's drain the
2076 * dying subsystems. We just need to ensure that the ones
2077 * unmounted previously finish dying and don't care about new ones
2078 * starting. Testing ref liveliness is good enough.
2079 */
2080 for_each_subsys(ss, i) {
2081 if (!(opts.subsys_mask & (1 << i)) ||
2082 ss->root == &cgrp_dfl_root)
2083 continue;
2084
2085 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
2086 mutex_unlock(&cgroup_mutex);
2087 msleep(10);
2088 ret = restart_syscall();
2089 goto out_free;
2090 }
2091 cgroup_put(&ss->root->cgrp);
2092 }
2093
Tejun Heo985ed672014-03-19 10:23:53 -04002094 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002095 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002096
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002097 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002098 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07002099
Paul Menage817929e2007-10-18 23:39:36 -07002100 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002101 * If we asked for a name then it must match. Also, if
2102 * name matches but sybsys_mask doesn't, we should fail.
2103 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07002104 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002105 if (opts.name) {
2106 if (strcmp(opts.name, root->name))
2107 continue;
2108 name_match = true;
2109 }
Tejun Heo31261212013-06-28 17:07:30 -07002110
2111 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05002112 * If we asked for subsystems (or explicitly for no
2113 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07002114 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002115 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04002116 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05002117 if (!name_match)
2118 continue;
2119 ret = -EBUSY;
2120 goto out_unlock;
2121 }
Tejun Heo873fe092013-04-14 20:15:26 -07002122
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04002123 if (root->flags ^ opts.flags)
2124 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05002125
Tejun Heo776f02f2014-02-12 09:29:50 -05002126 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08002127 * We want to reuse @root whose lifetime is governed by its
2128 * ->cgrp. Let's check whether @root is alive and keep it
2129 * that way. As cgroup_kill_sb() can happen anytime, we
2130 * want to block it by pinning the sb so that @root doesn't
2131 * get killed before mount is complete.
2132 *
2133 * With the sb pinned, tryget_live can reliably indicate
2134 * whether @root can be reused. If it's being killed,
2135 * drain it. We can use wait_queue for the wait but this
2136 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05002137 */
Li Zefan3a32bd72014-06-30 11:50:59 +08002138 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
2139 if (IS_ERR(pinned_sb) ||
2140 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05002141 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08002142 if (!IS_ERR_OR_NULL(pinned_sb))
2143 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05002144 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04002145 ret = restart_syscall();
2146 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05002147 }
2148
2149 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002150 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002151 }
2152
Tejun Heo172a2c062014-03-19 10:23:53 -04002153 /*
2154 * No such thing, create a new one. name= matching without subsys
2155 * specification is allowed for already existing hierarchies but we
2156 * can't create new one without subsys specification.
2157 */
2158 if (!opts.subsys_mask && !opts.none) {
2159 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002160 goto out_unlock;
2161 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002162
Tejun Heo172a2c062014-03-19 10:23:53 -04002163 root = kzalloc(sizeof(*root), GFP_KERNEL);
2164 if (!root) {
2165 ret = -ENOMEM;
2166 goto out_unlock;
2167 }
2168
2169 init_cgroup_root(root, &opts);
2170
Tejun Heo35585572014-02-13 06:58:38 -05002171 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002172 if (ret)
2173 cgroup_free_root(root);
2174
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002175out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002176 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04002177out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07002178 kfree(opts.release_agent);
2179 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002180
Tejun Heo2bd59d42014-02-11 11:52:49 -05002181 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05002182 return ERR_PTR(ret);
Tejun Heo67e9c742015-11-16 11:13:34 -05002183out_mount:
Jianyu Zhanc9482a52014-04-26 15:40:28 +08002184 dentry = kernfs_mount(fs_type, flags, root->kf_root,
Tejun Heo67e9c742015-11-16 11:13:34 -05002185 is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
2186 &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08002187 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002188 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002189
2190 /*
2191 * If @pinned_sb, we're reusing an existing root and holding an
2192 * extra ref on its sb. Mount is complete. Put the extra ref.
2193 */
2194 if (pinned_sb) {
2195 WARN_ON(new_sb);
2196 deactivate_super(pinned_sb);
2197 }
2198
Tejun Heo2bd59d42014-02-11 11:52:49 -05002199 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002200}
2201
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002202static void cgroup_kill_sb(struct super_block *sb)
2203{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002204 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002205 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002206
Tejun Heo9d755d32014-05-14 09:15:02 -04002207 /*
2208 * If @root doesn't have any mounts or children, start killing it.
2209 * This prevents new mounts by disabling percpu_ref_tryget_live().
2210 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08002211 *
2212 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002213 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05002214 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08002215 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04002216 cgroup_put(&root->cgrp);
2217 else
2218 percpu_ref_kill(&root->cgrp.self.refcnt);
2219
Tejun Heo2bd59d42014-02-11 11:52:49 -05002220 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002221}
2222
2223static struct file_system_type cgroup_fs_type = {
2224 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04002225 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002226 .kill_sb = cgroup_kill_sb,
2227};
2228
Tejun Heo67e9c742015-11-16 11:13:34 -05002229static struct file_system_type cgroup2_fs_type = {
2230 .name = "cgroup2",
2231 .mount = cgroup_mount,
2232 .kill_sb = cgroup_kill_sb,
2233};
2234
Li Zefana043e3b2008-02-23 15:24:09 -08002235/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002236 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002237 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002238 * @buf: the buffer to write the path into
2239 * @buflen: the length of the buffer
2240 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002241 * Determine @task's cgroup on the first (the one with the lowest non-zero
2242 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2243 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2244 * cgroup controller callbacks.
2245 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002246 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002247 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002248char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002249{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002250 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002251 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002252 int hierarchy_id = 1;
2253 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002254
2255 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002256 spin_lock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002257
Tejun Heo913ffdb2013-07-11 16:34:48 -07002258 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2259
Tejun Heo857a2be2013-04-14 20:50:08 -07002260 if (root) {
2261 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002262 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002263 } else {
2264 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002265 if (strlcpy(buf, "/", buflen) < buflen)
2266 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002267 }
2268
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002269 spin_unlock_bh(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002270 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002271 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002272}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002273EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002274
Tejun Heob3dc0942014-02-25 10:04:01 -05002275/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002276struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002277 /* the src and dst cset list running through cset->mg_node */
2278 struct list_head src_csets;
2279 struct list_head dst_csets;
2280
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002281 /* the subsys currently being processed */
2282 int ssid;
2283
Tejun Heob3dc0942014-02-25 10:04:01 -05002284 /*
2285 * Fields for cgroup_taskset_*() iteration.
2286 *
2287 * Before migration is committed, the target migration tasks are on
2288 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2289 * the csets on ->dst_csets. ->csets point to either ->src_csets
2290 * or ->dst_csets depending on whether migration is committed.
2291 *
2292 * ->cur_csets and ->cur_task point to the current task position
2293 * during iteration.
2294 */
2295 struct list_head *csets;
2296 struct css_set *cur_cset;
2297 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002298};
2299
Tejun Heoadaae5d2015-09-11 15:00:21 -04002300#define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2301 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2302 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2303 .csets = &tset.src_csets, \
2304}
2305
2306/**
2307 * cgroup_taskset_add - try to add a migration target task to a taskset
2308 * @task: target task
2309 * @tset: target taskset
2310 *
2311 * Add @task, which is a migration target, to @tset. This function becomes
2312 * noop if @task doesn't need to be migrated. @task's css_set should have
2313 * been added as a migration source and @task->cg_list will be moved from
2314 * the css_set's tasks list to mg_tasks one.
2315 */
2316static void cgroup_taskset_add(struct task_struct *task,
2317 struct cgroup_taskset *tset)
2318{
2319 struct css_set *cset;
2320
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002321 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002322
2323 /* @task either already exited or can't exit until the end */
2324 if (task->flags & PF_EXITING)
2325 return;
2326
2327 /* leave @task alone if post_fork() hasn't linked it yet */
2328 if (list_empty(&task->cg_list))
2329 return;
2330
2331 cset = task_css_set(task);
2332 if (!cset->mg_src_cgrp)
2333 return;
2334
2335 list_move_tail(&task->cg_list, &cset->mg_tasks);
2336 if (list_empty(&cset->mg_node))
2337 list_add_tail(&cset->mg_node, &tset->src_csets);
2338 if (list_empty(&cset->mg_dst_cset->mg_node))
2339 list_move_tail(&cset->mg_dst_cset->mg_node,
2340 &tset->dst_csets);
2341}
2342
Tejun Heo2f7ee562011-12-12 18:12:21 -08002343/**
2344 * cgroup_taskset_first - reset taskset and return the first task
2345 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002346 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002347 *
2348 * @tset iteration is initialized and the first task is returned.
2349 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002350struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2351 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002352{
Tejun Heob3dc0942014-02-25 10:04:01 -05002353 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2354 tset->cur_task = NULL;
2355
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002356 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002357}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002358
2359/**
2360 * cgroup_taskset_next - iterate to the next task in taskset
2361 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002362 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002363 *
2364 * Return the next task in @tset. Iteration must have been initialized
2365 * with cgroup_taskset_first().
2366 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002367struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2368 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002369{
Tejun Heob3dc0942014-02-25 10:04:01 -05002370 struct css_set *cset = tset->cur_cset;
2371 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002372
Tejun Heob3dc0942014-02-25 10:04:01 -05002373 while (&cset->mg_node != tset->csets) {
2374 if (!task)
2375 task = list_first_entry(&cset->mg_tasks,
2376 struct task_struct, cg_list);
2377 else
2378 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002379
Tejun Heob3dc0942014-02-25 10:04:01 -05002380 if (&task->cg_list != &cset->mg_tasks) {
2381 tset->cur_cset = cset;
2382 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002383
2384 /*
2385 * This function may be called both before and
2386 * after cgroup_taskset_migrate(). The two cases
2387 * can be distinguished by looking at whether @cset
2388 * has its ->mg_dst_cset set.
2389 */
2390 if (cset->mg_dst_cset)
2391 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2392 else
2393 *dst_cssp = cset->subsys[tset->ssid];
2394
Tejun Heob3dc0942014-02-25 10:04:01 -05002395 return task;
2396 }
2397
2398 cset = list_next_entry(cset, mg_node);
2399 task = NULL;
2400 }
2401
2402 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002403}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002404
2405/**
Tejun Heoadaae5d2015-09-11 15:00:21 -04002406 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2407 * @tset: taget taskset
2408 * @dst_cgrp: destination cgroup
2409 *
2410 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2411 * ->can_attach callbacks fails and guarantees that either all or none of
2412 * the tasks in @tset are migrated. @tset is consumed regardless of
2413 * success.
2414 */
2415static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
2416 struct cgroup *dst_cgrp)
2417{
2418 struct cgroup_subsys_state *css, *failed_css = NULL;
2419 struct task_struct *task, *tmp_task;
2420 struct css_set *cset, *tmp_cset;
2421 int i, ret;
2422
2423 /* methods shouldn't be called if no task is actually migrating */
2424 if (list_empty(&tset->src_csets))
2425 return 0;
2426
2427 /* check that we can legitimately attach to the cgroup */
2428 for_each_e_css(css, i, dst_cgrp) {
2429 if (css->ss->can_attach) {
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002430 tset->ssid = i;
2431 ret = css->ss->can_attach(tset);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002432 if (ret) {
2433 failed_css = css;
2434 goto out_cancel_attach;
2435 }
2436 }
2437 }
2438
2439 /*
2440 * Now that we're guaranteed success, proceed to move all tasks to
2441 * the new cgroup. There are no failure cases after here, so this
2442 * is the commit point.
2443 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002444 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002445 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002446 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2447 struct css_set *from_cset = task_css_set(task);
2448 struct css_set *to_cset = cset->mg_dst_cset;
2449
2450 get_css_set(to_cset);
2451 css_set_move_task(task, from_cset, to_cset, true);
2452 put_css_set_locked(from_cset);
2453 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002454 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002455 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002456
2457 /*
2458 * Migration is committed, all target tasks are now on dst_csets.
2459 * Nothing is sensitive to fork() after this point. Notify
2460 * controllers that migration is complete.
2461 */
2462 tset->csets = &tset->dst_csets;
2463
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002464 for_each_e_css(css, i, dst_cgrp) {
2465 if (css->ss->attach) {
2466 tset->ssid = i;
2467 css->ss->attach(tset);
2468 }
2469 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002470
2471 ret = 0;
2472 goto out_release_tset;
2473
2474out_cancel_attach:
2475 for_each_e_css(css, i, dst_cgrp) {
2476 if (css == failed_css)
2477 break;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002478 if (css->ss->cancel_attach) {
2479 tset->ssid = i;
2480 css->ss->cancel_attach(tset);
2481 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002482 }
2483out_release_tset:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002484 spin_lock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002485 list_splice_init(&tset->dst_csets, &tset->src_csets);
2486 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2487 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2488 list_del_init(&cset->mg_node);
2489 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002490 spin_unlock_bh(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002491 return ret;
2492}
2493
2494/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002495 * cgroup_migrate_finish - cleanup after attach
2496 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002497 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002498 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2499 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002500 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002501static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002502{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002503 struct css_set *cset, *tmp_cset;
2504
2505 lockdep_assert_held(&cgroup_mutex);
2506
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002507 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002508 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2509 cset->mg_src_cgrp = NULL;
2510 cset->mg_dst_cset = NULL;
2511 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002512 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002513 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002514 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002515}
2516
2517/**
2518 * cgroup_migrate_add_src - add a migration source css_set
2519 * @src_cset: the source css_set to add
2520 * @dst_cgrp: the destination cgroup
2521 * @preloaded_csets: list of preloaded css_sets
2522 *
2523 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2524 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2525 * up by cgroup_migrate_finish().
2526 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002527 * This function may be called without holding cgroup_threadgroup_rwsem
2528 * even if the target is a process. Threads may be created and destroyed
2529 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2530 * into play and the preloaded css_sets are guaranteed to cover all
2531 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002532 */
2533static void cgroup_migrate_add_src(struct css_set *src_cset,
2534 struct cgroup *dst_cgrp,
2535 struct list_head *preloaded_csets)
2536{
2537 struct cgroup *src_cgrp;
2538
2539 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002540 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002541
2542 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2543
Tejun Heo1958d2d2014-02-25 10:04:03 -05002544 if (!list_empty(&src_cset->mg_preload_node))
2545 return;
2546
2547 WARN_ON(src_cset->mg_src_cgrp);
2548 WARN_ON(!list_empty(&src_cset->mg_tasks));
2549 WARN_ON(!list_empty(&src_cset->mg_node));
2550
2551 src_cset->mg_src_cgrp = src_cgrp;
2552 get_css_set(src_cset);
2553 list_add(&src_cset->mg_preload_node, preloaded_csets);
2554}
2555
2556/**
2557 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002558 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002559 * @preloaded_csets: list of preloaded source css_sets
2560 *
2561 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2562 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002563 * pins all destination css_sets, links each to its source, and append them
2564 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2565 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002566 *
2567 * This function must be called after cgroup_migrate_add_src() has been
2568 * called on each migration source css_set. After migration is performed
2569 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2570 * @preloaded_csets.
2571 */
2572static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2573 struct list_head *preloaded_csets)
2574{
2575 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002576 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002577
2578 lockdep_assert_held(&cgroup_mutex);
2579
Tejun Heof8f22e52014-04-23 11:13:16 -04002580 /*
Tejun Heo62716ea2016-02-23 10:00:51 -05002581 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002582 * with tasks so that child cgroups don't compete against tasks.
2583 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002584 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heo62716ea2016-02-23 10:00:51 -05002585 dst_cgrp->subtree_control)
Tejun Heof8f22e52014-04-23 11:13:16 -04002586 return -EBUSY;
2587
Tejun Heo1958d2d2014-02-25 10:04:03 -05002588 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002589 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002590 struct css_set *dst_cset;
2591
Tejun Heof817de92014-04-23 11:13:16 -04002592 dst_cset = find_css_set(src_cset,
2593 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002594 if (!dst_cset)
2595 goto err;
2596
2597 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002598
2599 /*
2600 * If src cset equals dst, it's noop. Drop the src.
2601 * cgroup_migrate() will skip the cset too. Note that we
2602 * can't handle src == dst as some nodes are used by both.
2603 */
2604 if (src_cset == dst_cset) {
2605 src_cset->mg_src_cgrp = NULL;
2606 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002607 put_css_set(src_cset);
2608 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002609 continue;
2610 }
2611
Tejun Heo1958d2d2014-02-25 10:04:03 -05002612 src_cset->mg_dst_cset = dst_cset;
2613
2614 if (list_empty(&dst_cset->mg_preload_node))
2615 list_add(&dst_cset->mg_preload_node, &csets);
2616 else
Zefan Lia25eb522014-09-19 16:51:00 +08002617 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002618 }
2619
Tejun Heof817de92014-04-23 11:13:16 -04002620 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002621 return 0;
2622err:
2623 cgroup_migrate_finish(&csets);
2624 return -ENOMEM;
2625}
2626
2627/**
2628 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002629 * @leader: the leader of the process or the task to migrate
2630 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heo9af2ec42015-09-11 15:00:20 -04002631 * @cgrp: the destination cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002632 *
2633 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo1ed13282015-09-16 12:53:17 -04002634 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002635 * caller is also responsible for invoking cgroup_migrate_add_src() and
2636 * cgroup_migrate_prepare_dst() on the targets before invoking this
2637 * function and following up with cgroup_migrate_finish().
2638 *
2639 * As long as a controller's ->can_attach() doesn't fail, this function is
2640 * guaranteed to succeed. This means that, excluding ->can_attach()
2641 * failure, when migrating multiple targets, the success or failure can be
2642 * decided for all targets by invoking group_migrate_prepare_dst() before
2643 * actually starting migrating.
2644 */
Tejun Heo9af2ec42015-09-11 15:00:20 -04002645static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2646 struct cgroup *cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002647{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002648 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
2649 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002650
2651 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002652 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2653 * already PF_EXITING could be freed from underneath us unless we
2654 * take an rcu_read_lock.
2655 */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002656 spin_lock_bh(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002657 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002658 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002659 do {
Tejun Heoadaae5d2015-09-11 15:00:21 -04002660 cgroup_taskset_add(task, &tset);
Li Zefan081aa452013-03-13 09:17:09 +08002661 if (!threadgroup)
2662 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002663 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002664 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002665 spin_unlock_bh(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002666
Tejun Heoadaae5d2015-09-11 15:00:21 -04002667 return cgroup_taskset_migrate(&tset, cgrp);
Ben Blum74a11662011-05-26 16:25:20 -07002668}
2669
Tejun Heo1958d2d2014-02-25 10:04:03 -05002670/**
2671 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2672 * @dst_cgrp: the cgroup to attach to
2673 * @leader: the task or the leader of the threadgroup to be attached
2674 * @threadgroup: attach the whole threadgroup?
2675 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002676 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002677 */
2678static int cgroup_attach_task(struct cgroup *dst_cgrp,
2679 struct task_struct *leader, bool threadgroup)
2680{
2681 LIST_HEAD(preloaded_csets);
2682 struct task_struct *task;
2683 int ret;
2684
2685 /* look up all src csets */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002686 spin_lock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002687 rcu_read_lock();
2688 task = leader;
2689 do {
2690 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2691 &preloaded_csets);
2692 if (!threadgroup)
2693 break;
2694 } while_each_thread(leader, task);
2695 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002696 spin_unlock_bh(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002697
2698 /* prepare dst csets and commit */
2699 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2700 if (!ret)
Tejun Heo9af2ec42015-09-11 15:00:20 -04002701 ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002702
2703 cgroup_migrate_finish(&preloaded_csets);
2704 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002705}
2706
Tejun Heo187fe842015-06-18 16:54:28 -04002707static int cgroup_procs_write_permission(struct task_struct *task,
2708 struct cgroup *dst_cgrp,
2709 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002710{
2711 const struct cred *cred = current_cred();
2712 const struct cred *tcred = get_task_cred(task);
2713 int ret = 0;
2714
2715 /*
2716 * even if we're attaching all tasks in the thread group, we only
2717 * need to check permissions on one of them.
2718 */
2719 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2720 !uid_eq(cred->euid, tcred->uid) &&
2721 !uid_eq(cred->euid, tcred->suid))
2722 ret = -EACCES;
2723
Tejun Heo187fe842015-06-18 16:54:28 -04002724 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2725 struct super_block *sb = of->file->f_path.dentry->d_sb;
2726 struct cgroup *cgrp;
2727 struct inode *inode;
2728
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002729 spin_lock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002730 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002731 spin_unlock_bh(&css_set_lock);
Tejun Heo187fe842015-06-18 16:54:28 -04002732
2733 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2734 cgrp = cgroup_parent(cgrp);
2735
2736 ret = -ENOMEM;
Tejun Heo6f60ead2015-09-18 17:54:23 -04002737 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
Tejun Heo187fe842015-06-18 16:54:28 -04002738 if (inode) {
2739 ret = inode_permission(inode, MAY_WRITE);
2740 iput(inode);
2741 }
2742 }
2743
Tejun Heodedf22e2015-06-18 16:54:28 -04002744 put_cred(tcred);
2745 return ret;
2746}
2747
Ben Blum74a11662011-05-26 16:25:20 -07002748/*
2749 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002750 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002751 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002752 */
Tejun Heoacbef752014-05-13 12:16:22 -04002753static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2754 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002755{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002756 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002757 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002758 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002759 int ret;
2760
Tejun Heoacbef752014-05-13 12:16:22 -04002761 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2762 return -EINVAL;
2763
Tejun Heoe76ecae2014-05-13 12:19:23 -04002764 cgrp = cgroup_kn_lock_live(of->kn);
2765 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002766 return -ENODEV;
2767
Tejun Heo3014dde2015-09-16 13:03:02 -04002768 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002769 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002770 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002771 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002772 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002773 ret = -ESRCH;
Tejun Heo3014dde2015-09-16 13:03:02 -04002774 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002775 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002776 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002777 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002778 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002779
2780 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002781 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002782
2783 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002784 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002785 * trapped in a cpuset, or RT worker may be born in a cgroup
2786 * with no rt_runtime allocated. Just say no.
2787 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002788 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002789 ret = -EINVAL;
Tejun Heo3014dde2015-09-16 13:03:02 -04002790 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002791 }
2792
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002793 get_task_struct(tsk);
2794 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002795
Tejun Heo187fe842015-06-18 16:54:28 -04002796 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002797 if (!ret)
2798 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002799
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002800 put_task_struct(tsk);
Tejun Heo3014dde2015-09-16 13:03:02 -04002801 goto out_unlock_threadgroup;
2802
2803out_unlock_rcu:
2804 rcu_read_unlock();
2805out_unlock_threadgroup:
2806 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002807 cgroup_kn_unlock(of->kn);
Tejun Heoe93ad192016-01-19 12:18:41 -05002808 cpuset_post_attach_flush();
Tejun Heoacbef752014-05-13 12:16:22 -04002809 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002810}
2811
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002812/**
2813 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2814 * @from: attach to all cgroups of a given task
2815 * @tsk: the task to be attached
2816 */
2817int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2818{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002819 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002820 int retval = 0;
2821
Tejun Heo47cfcd02013-04-07 09:29:51 -07002822 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002823 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002824 struct cgroup *from_cgrp;
2825
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002826 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002827 continue;
2828
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002829 spin_lock_bh(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05002830 from_cgrp = task_cgroup_from_root(from, root);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002831 spin_unlock_bh(&css_set_lock);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002832
Li Zefan6f4b7e62013-07-31 16:18:36 +08002833 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002834 if (retval)
2835 break;
2836 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002837 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002838
2839 return retval;
2840}
2841EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2842
Tejun Heoacbef752014-05-13 12:16:22 -04002843static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2844 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002845{
Tejun Heoacbef752014-05-13 12:16:22 -04002846 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002847}
2848
Tejun Heoacbef752014-05-13 12:16:22 -04002849static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2850 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002851{
Tejun Heoacbef752014-05-13 12:16:22 -04002852 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002853}
2854
Tejun Heo451af502014-05-13 12:16:21 -04002855static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2856 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002857{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002858 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002859
Tejun Heoe76ecae2014-05-13 12:19:23 -04002860 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2861
2862 cgrp = cgroup_kn_lock_live(of->kn);
2863 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002864 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002865 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002866 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2867 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002868 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002869 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002870 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002871}
2872
Tejun Heo2da8ca82013-12-05 12:28:04 -05002873static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002874{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002875 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002876
Tejun Heo46cfeb02014-05-13 12:11:00 -04002877 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002878 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002879 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002880 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002881 return 0;
2882}
2883
Tejun Heo2da8ca82013-12-05 12:28:04 -05002884static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002885{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002886 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002887 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002888}
2889
Tejun Heo6e5c8302016-02-22 22:25:47 -05002890static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002891{
2892 struct cgroup_subsys *ss;
2893 bool printed = false;
2894 int ssid;
2895
Tejun Heob4e0eea2016-02-22 22:25:46 -05002896 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002897 if (printed)
2898 seq_putc(seq, ' ');
2899 seq_printf(seq, "%s", ss->name);
2900 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002901 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002902 if (printed)
2903 seq_putc(seq, '\n');
2904}
2905
Tejun Heof8f22e52014-04-23 11:13:16 -04002906/* show controllers which are enabled from the parent */
2907static int cgroup_controllers_show(struct seq_file *seq, void *v)
2908{
2909 struct cgroup *cgrp = seq_css(seq)->cgroup;
2910
Tejun Heo5531dc92016-03-03 09:57:58 -05002911 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04002912 return 0;
2913}
2914
2915/* show controllers which are enabled for a given cgroup's children */
2916static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2917{
2918 struct cgroup *cgrp = seq_css(seq)->cgroup;
2919
Tejun Heo667c2492014-07-08 18:02:56 -04002920 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002921 return 0;
2922}
2923
2924/**
2925 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2926 * @cgrp: root of the subtree to update csses for
2927 *
Tejun Heo8699b772016-02-22 22:25:46 -05002928 * @cgrp's subtree_ss_mask has changed and its subtree's (self excluded)
Tejun Heof8f22e52014-04-23 11:13:16 -04002929 * css associations need to be updated accordingly. This function looks up
2930 * all css_sets which are attached to the subtree, creates the matching
2931 * updated css_sets and migrates the tasks to the new ones.
2932 */
2933static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2934{
2935 LIST_HEAD(preloaded_csets);
Tejun Heo10265072015-09-11 15:00:22 -04002936 struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002937 struct cgroup_subsys_state *css;
2938 struct css_set *src_cset;
2939 int ret;
2940
Tejun Heof8f22e52014-04-23 11:13:16 -04002941 lockdep_assert_held(&cgroup_mutex);
2942
Tejun Heo3014dde2015-09-16 13:03:02 -04002943 percpu_down_write(&cgroup_threadgroup_rwsem);
2944
Tejun Heof8f22e52014-04-23 11:13:16 -04002945 /* look up all csses currently attached to @cgrp's subtree */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002946 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002947 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2948 struct cgrp_cset_link *link;
2949
Tejun Heo8699b772016-02-22 22:25:46 -05002950 /* self is not affected by subtree_ss_mask change */
Tejun Heof8f22e52014-04-23 11:13:16 -04002951 if (css->cgroup == cgrp)
2952 continue;
2953
2954 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2955 cgroup_migrate_add_src(link->cset, cgrp,
2956 &preloaded_csets);
2957 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002958 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002959
2960 /* NULL dst indicates self on default hierarchy */
2961 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2962 if (ret)
2963 goto out_finish;
2964
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002965 spin_lock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002966 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002967 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002968
2969 /* src_csets precede dst_csets, break on the first dst_cset */
2970 if (!src_cset->mg_src_cgrp)
2971 break;
2972
Tejun Heo10265072015-09-11 15:00:22 -04002973 /* all tasks in src_csets need to be migrated */
2974 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2975 cgroup_taskset_add(task, &tset);
Tejun Heof8f22e52014-04-23 11:13:16 -04002976 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002977 spin_unlock_bh(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002978
Tejun Heo10265072015-09-11 15:00:22 -04002979 ret = cgroup_taskset_migrate(&tset, cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04002980out_finish:
2981 cgroup_migrate_finish(&preloaded_csets);
Tejun Heo3014dde2015-09-16 13:03:02 -04002982 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002983 return ret;
2984}
2985
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002986/**
2987 * cgroup_drain_offline - wait for previously offlined csses to go away
Tejun Heoce3f1d92016-03-03 09:57:59 -05002988 * @cgrp: root of the target subtree
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002989 *
2990 * Because css offlining is asynchronous, userland may try to re-enable a
2991 * controller while the previous css is still around. This function drains
Tejun Heoce3f1d92016-03-03 09:57:59 -05002992 * the previous css instances of @cgrp's subtree.
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002993 *
2994 * Must be called with cgroup_mutex held. Returns %false if there were no
2995 * dying css instances. Returns %true if there were one or more and this
2996 * function waited. On %true return, cgroup_mutex has been dropped and
2997 * re-acquired inbetween which anything could have happened. The caller
2998 * typically would have to start over.
2999 */
3000static bool cgroup_drain_offline(struct cgroup *cgrp)
3001{
3002 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003003 struct cgroup_subsys_state *d_css;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003004 struct cgroup_subsys *ss;
3005 int ssid;
3006
3007 lockdep_assert_held(&cgroup_mutex);
3008
Tejun Heoce3f1d92016-03-03 09:57:59 -05003009 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003010 for_each_subsys(ss, ssid) {
3011 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3012 DEFINE_WAIT(wait);
3013
Tejun Heoce3f1d92016-03-03 09:57:59 -05003014 if (!css || !percpu_ref_is_dying(&css->refcnt))
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003015 continue;
3016
3017 cgroup_get(dsct);
3018 prepare_to_wait(&dsct->offline_waitq, &wait,
3019 TASK_UNINTERRUPTIBLE);
3020
3021 mutex_unlock(&cgroup_mutex);
3022 schedule();
3023 finish_wait(&dsct->offline_waitq, &wait);
3024 mutex_lock(&cgroup_mutex);
3025
3026 cgroup_put(dsct);
3027 return true;
3028 }
3029 }
3030
3031 return false;
3032}
3033
Tejun Heo12b3bb62016-03-03 09:57:59 -05003034/**
Tejun Heo15a27c32016-03-03 09:57:59 -05003035 * cgroup_save_control - save control masks of a subtree
3036 * @cgrp: root of the target subtree
3037 *
3038 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
3039 * prefixed fields for @cgrp's subtree including @cgrp itself.
3040 */
3041static void cgroup_save_control(struct cgroup *cgrp)
3042{
3043 struct cgroup *dsct;
3044 struct cgroup_subsys_state *d_css;
3045
3046 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3047 dsct->old_subtree_control = dsct->subtree_control;
3048 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
3049 }
3050}
3051
3052/**
3053 * cgroup_propagate_control - refresh control masks of a subtree
3054 * @cgrp: root of the target subtree
3055 *
3056 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
3057 * ->subtree_control and propagate controller availability through the
3058 * subtree so that descendants don't have unavailable controllers enabled.
3059 */
3060static void cgroup_propagate_control(struct cgroup *cgrp)
3061{
3062 struct cgroup *dsct;
3063 struct cgroup_subsys_state *d_css;
3064
3065 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3066 dsct->subtree_control &= cgroup_control(dsct);
3067 dsct->subtree_ss_mask = cgroup_calc_subtree_ss_mask(dsct,
3068 dsct->subtree_control);
3069 }
3070}
3071
3072/**
3073 * cgroup_restore_control - restore control masks of a subtree
3074 * @cgrp: root of the target subtree
3075 *
3076 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
3077 * prefixed fields for @cgrp's subtree including @cgrp itself.
3078 */
3079static void cgroup_restore_control(struct cgroup *cgrp)
3080{
3081 struct cgroup *dsct;
3082 struct cgroup_subsys_state *d_css;
3083
3084 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
3085 dsct->subtree_control = dsct->old_subtree_control;
3086 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
3087 }
3088}
3089
3090/**
Tejun Heobdb53bd2016-03-03 09:57:59 -05003091 * cgroup_apply_control_enable - enable or show csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003092 * @cgrp: root of the target subtree
Tejun Heobdb53bd2016-03-03 09:57:59 -05003093 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003094 * Walk @cgrp's subtree and create new csses or make the existing ones
Tejun Heobdb53bd2016-03-03 09:57:59 -05003095 * visible. A css is created invisible if it's being implicitly enabled
3096 * through dependency. An invisible css is made visible when the userland
3097 * explicitly enables it.
3098 *
3099 * Returns 0 on success, -errno on failure. On failure, csses which have
3100 * been processed already aren't cleaned up. The caller is responsible for
3101 * cleaning up with cgroup_apply_control_disble().
3102 */
3103static int cgroup_apply_control_enable(struct cgroup *cgrp)
3104{
3105 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003106 struct cgroup_subsys_state *d_css;
Tejun Heobdb53bd2016-03-03 09:57:59 -05003107 struct cgroup_subsys *ss;
3108 int ssid, ret;
3109
Tejun Heoce3f1d92016-03-03 09:57:59 -05003110 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heobdb53bd2016-03-03 09:57:59 -05003111 for_each_subsys(ss, ssid) {
3112 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3113
3114 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
3115 continue;
3116
3117 if (!css) {
3118 css = css_create(dsct, ss);
3119 if (IS_ERR(css))
3120 return PTR_ERR(css);
3121 }
3122
3123 if (cgroup_control(dsct) & (1 << ss->id)) {
3124 ret = css_populate_dir(css, NULL);
3125 if (ret)
3126 return ret;
3127 }
3128 }
3129 }
3130
3131 return 0;
3132}
3133
3134/**
Tejun Heo12b3bb62016-03-03 09:57:59 -05003135 * cgroup_apply_control_disable - kill or hide csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003136 * @cgrp: root of the target subtree
Tejun Heo12b3bb62016-03-03 09:57:59 -05003137 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003138 * Walk @cgrp's subtree and kill and hide csses so that they match
Tejun Heo12b3bb62016-03-03 09:57:59 -05003139 * cgroup_ss_mask() and cgroup_visible_mask().
3140 *
3141 * A css is hidden when the userland requests it to be disabled while other
3142 * subsystems are still depending on it. The css must not actively control
3143 * resources and be in the vanilla state if it's made visible again later.
3144 * Controllers which may be depended upon should provide ->css_reset() for
3145 * this purpose.
3146 */
3147static void cgroup_apply_control_disable(struct cgroup *cgrp)
3148{
3149 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003150 struct cgroup_subsys_state *d_css;
Tejun Heo12b3bb62016-03-03 09:57:59 -05003151 struct cgroup_subsys *ss;
3152 int ssid;
3153
Tejun Heoce3f1d92016-03-03 09:57:59 -05003154 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003155 for_each_subsys(ss, ssid) {
3156 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3157
3158 if (!css)
3159 continue;
3160
3161 if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) {
3162 kill_css(css);
3163 } else if (!(cgroup_control(dsct) & (1 << ss->id))) {
3164 css_clear_dir(css, NULL);
3165 if (ss->css_reset)
3166 ss->css_reset(css);
3167 }
3168 }
3169 }
3170}
3171
Tejun Heof8f22e52014-04-23 11:13:16 -04003172/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003173static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3174 char *buf, size_t nbytes,
3175 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003176{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003177 u16 enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04003178 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003179 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003180 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003181 int ssid, ret;
3182
3183 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003184 * Parse input - space separated list of subsystem names prefixed
3185 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003186 */
Tejun Heo451af502014-05-13 12:16:21 -04003187 buf = strstrip(buf);
3188 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003189 if (tok[0] == '\0')
3190 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003191 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003192 if (!cgroup_ssid_enabled(ssid) ||
3193 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003194 continue;
3195
3196 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003197 enable |= 1 << ssid;
3198 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003199 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003200 disable |= 1 << ssid;
3201 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003202 } else {
3203 return -EINVAL;
3204 }
3205 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003206 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003207 if (ssid == CGROUP_SUBSYS_COUNT)
3208 return -EINVAL;
3209 }
3210
Tejun Heoa9746d82014-05-13 12:19:22 -04003211 cgrp = cgroup_kn_lock_live(of->kn);
3212 if (!cgrp)
3213 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003214
3215 for_each_subsys(ss, ssid) {
3216 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003217 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003218 enable &= ~(1 << ssid);
3219 continue;
3220 }
3221
Tejun Heo5531dc92016-03-03 09:57:58 -05003222 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003223 ret = -ENOENT;
3224 goto out_unlock;
3225 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003226 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003227 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003228 disable &= ~(1 << ssid);
3229 continue;
3230 }
3231
3232 /* a child has it enabled? */
3233 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003234 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003235 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003236 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003237 }
3238 }
3239 }
3240 }
3241
3242 if (!enable && !disable) {
3243 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003244 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003245 }
3246
3247 /*
Tejun Heo667c2492014-07-08 18:02:56 -04003248 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04003249 * with tasks so that child cgroups don't compete against tasks.
3250 */
Tejun Heod51f39b2014-05-16 13:22:48 -04003251 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003252 ret = -EBUSY;
3253 goto out_unlock;
3254 }
3255
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003256 if (cgroup_drain_offline(cgrp)) {
3257 cgroup_kn_unlock(of->kn);
3258 return restart_syscall();
3259 }
3260
Tejun Heo15a27c32016-03-03 09:57:59 -05003261 /* save and update control masks and prepare csses */
3262 cgroup_save_control(cgrp);
Tejun Heoc29adf22014-07-08 18:02:56 -04003263
Tejun Heo15a27c32016-03-03 09:57:59 -05003264 cgrp->subtree_control |= enable;
3265 cgrp->subtree_control &= ~disable;
Tejun Heof63070d2014-07-08 18:02:57 -04003266
Tejun Heo15a27c32016-03-03 09:57:59 -05003267 cgroup_propagate_control(cgrp);
Tejun Heo755bf5e2014-11-18 02:49:50 -05003268
Tejun Heobdb53bd2016-03-03 09:57:59 -05003269 ret = cgroup_apply_control_enable(cgrp);
3270 if (ret)
3271 goto err_undo_css;
Tejun Heof8f22e52014-04-23 11:13:16 -04003272
Tejun Heoc29adf22014-07-08 18:02:56 -04003273 /*
3274 * At this point, cgroup_e_css() results reflect the new csses
3275 * making the following cgroup_update_dfl_csses() properly update
3276 * css associations of all tasks in the subtree.
3277 */
Tejun Heof8f22e52014-04-23 11:13:16 -04003278 ret = cgroup_update_dfl_csses(cgrp);
3279 if (ret)
3280 goto err_undo_css;
3281
Tejun Heo12b3bb62016-03-03 09:57:59 -05003282 /* all tasks are migrated out of disabled csses, commit disable */
3283 cgroup_apply_control_disable(cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -04003284
3285 kernfs_activate(cgrp->kn);
3286 ret = 0;
3287out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003288 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003289 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003290
3291err_undo_css:
Tejun Heo12b3bb62016-03-03 09:57:59 -05003292 /* restore masks and shoot down new csses */
Tejun Heo15a27c32016-03-03 09:57:59 -05003293 cgroup_restore_control(cgrp);
Tejun Heo12b3bb62016-03-03 09:57:59 -05003294 cgroup_apply_control_disable(cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -04003295
Tejun Heof8f22e52014-04-23 11:13:16 -04003296 goto out_unlock;
3297}
3298
Tejun Heo4a07c222015-09-18 17:54:22 -04003299static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003300{
Tejun Heo4a07c222015-09-18 17:54:22 -04003301 seq_printf(seq, "populated %d\n",
Tejun Heo27bd4db2015-10-15 16:41:50 -04003302 cgroup_is_populated(seq_css(seq)->cgroup));
Tejun Heo842b5972014-04-25 18:28:02 -04003303 return 0;
3304}
3305
Tejun Heo2bd59d42014-02-11 11:52:49 -05003306static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3307 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003308{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003309 struct cgroup *cgrp = of->kn->parent->priv;
3310 struct cftype *cft = of->kn->priv;
3311 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003312 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003313
Tejun Heob4168642014-05-13 12:16:21 -04003314 if (cft->write)
3315 return cft->write(of, buf, nbytes, off);
3316
Tejun Heo2bd59d42014-02-11 11:52:49 -05003317 /*
3318 * kernfs guarantees that a file isn't deleted with operations in
3319 * flight, which means that the matching css is and stays alive and
3320 * doesn't need to be pinned. The RCU locking is not necessary
3321 * either. It's just for the convenience of using cgroup_css().
3322 */
3323 rcu_read_lock();
3324 css = cgroup_css(cgrp, cft->ss);
3325 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003326
Tejun Heo451af502014-05-13 12:16:21 -04003327 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003328 unsigned long long v;
3329 ret = kstrtoull(buf, 0, &v);
3330 if (!ret)
3331 ret = cft->write_u64(css, cft, v);
3332 } else if (cft->write_s64) {
3333 long long v;
3334 ret = kstrtoll(buf, 0, &v);
3335 if (!ret)
3336 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003337 } else {
3338 ret = -EINVAL;
3339 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003340
Tejun Heoa742c592013-12-05 12:28:03 -05003341 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003342}
3343
Tejun Heo6612f052013-12-05 12:28:04 -05003344static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003345{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003346 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003347}
3348
3349static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3350{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003351 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003352}
3353
3354static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3355{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003356 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003357}
3358
3359static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3360{
Tejun Heo7da11272013-12-05 12:28:04 -05003361 struct cftype *cft = seq_cft(m);
3362 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003363
Tejun Heo2da8ca82013-12-05 12:28:04 -05003364 if (cft->seq_show)
3365 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003366
Tejun Heo896f5192013-12-05 12:28:04 -05003367 if (cft->read_u64)
3368 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3369 else if (cft->read_s64)
3370 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3371 else
3372 return -EINVAL;
3373 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003374}
3375
Tejun Heo2bd59d42014-02-11 11:52:49 -05003376static struct kernfs_ops cgroup_kf_single_ops = {
3377 .atomic_write_len = PAGE_SIZE,
3378 .write = cgroup_file_write,
3379 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003380};
3381
Tejun Heo2bd59d42014-02-11 11:52:49 -05003382static struct kernfs_ops cgroup_kf_ops = {
3383 .atomic_write_len = PAGE_SIZE,
3384 .write = cgroup_file_write,
3385 .seq_start = cgroup_seqfile_start,
3386 .seq_next = cgroup_seqfile_next,
3387 .seq_stop = cgroup_seqfile_stop,
3388 .seq_show = cgroup_seqfile_show,
3389};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003390
3391/*
3392 * cgroup_rename - Only allow simple rename of directories in place.
3393 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003394static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3395 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003396{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003397 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003398 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003399
Tejun Heo2bd59d42014-02-11 11:52:49 -05003400 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003401 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003402 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003403 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003404
Tejun Heo6db8e852013-06-14 11:18:22 -07003405 /*
3406 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003407 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003408 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003409 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003410 return -EPERM;
3411
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003412 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003413 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003414 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003415 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003416 */
3417 kernfs_break_active_protection(new_parent);
3418 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003419
Tejun Heo2bd59d42014-02-11 11:52:49 -05003420 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003421
Tejun Heo2bd59d42014-02-11 11:52:49 -05003422 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003423
Tejun Heo2bd59d42014-02-11 11:52:49 -05003424 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003425
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003426 kernfs_unbreak_active_protection(kn);
3427 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003428 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003429}
3430
Tejun Heo49957f82014-04-07 16:44:47 -04003431/* set uid and gid of cgroup dirs and files to that of the creator */
3432static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3433{
3434 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3435 .ia_uid = current_fsuid(),
3436 .ia_gid = current_fsgid(), };
3437
3438 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3439 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3440 return 0;
3441
3442 return kernfs_setattr(kn, &iattr);
3443}
3444
Tejun Heo4df8dc92015-09-18 17:54:23 -04003445static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3446 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003447{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003448 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003449 struct kernfs_node *kn;
3450 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003451 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003452
Tejun Heo2bd59d42014-02-11 11:52:49 -05003453#ifdef CONFIG_DEBUG_LOCK_ALLOC
3454 key = &cft->lockdep_key;
3455#endif
3456 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3457 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003458 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003459 if (IS_ERR(kn))
3460 return PTR_ERR(kn);
3461
3462 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003463 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003464 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003465 return ret;
3466 }
3467
Tejun Heo6f60ead2015-09-18 17:54:23 -04003468 if (cft->file_offset) {
3469 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3470
Tejun Heo34c06252015-11-05 00:12:24 -05003471 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003472 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05003473 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04003474 }
3475
Tejun Heof8f22e52014-04-23 11:13:16 -04003476 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003477}
3478
Tejun Heob1f28d32013-06-28 16:24:10 -07003479/**
3480 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04003481 * @css: the target css
3482 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07003483 * @cfts: array of cftypes to be added
3484 * @is_add: whether to add or remove
3485 *
3486 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04003487 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07003488 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04003489static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3490 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04003491 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003492{
Tejun Heo6732ed82015-09-18 17:54:23 -04003493 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05003494 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07003495
Tejun Heo01f64742014-05-13 12:19:23 -04003496 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003497
Tejun Heo6732ed82015-09-18 17:54:23 -04003498restart:
3499 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003500 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003501 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003502 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003503 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003504 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003505 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003506 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003507 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003508 continue;
3509
Li Zefan2739d3c2013-01-21 18:18:33 +08003510 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04003511 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003512 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003513 pr_warn("%s: failed to add %s, err=%d\n",
3514 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04003515 cft_end = cft;
3516 is_add = false;
3517 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07003518 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003519 } else {
3520 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003521 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003522 }
Tejun Heob598dde2016-02-22 22:25:45 -05003523 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003524}
3525
Tejun Heo21a2d342014-02-12 09:29:49 -05003526static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003527{
3528 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003529 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003530 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003531 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003532 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003533
Tejun Heo01f64742014-05-13 12:19:23 -04003534 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003535
Li Zefane8c82d22013-06-18 18:48:37 +08003536 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003537 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003538 struct cgroup *cgrp = css->cgroup;
3539
Tejun Heo88cb04b2016-03-03 09:57:58 -05003540 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08003541 continue;
3542
Tejun Heo4df8dc92015-09-18 17:54:23 -04003543 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003544 if (ret)
3545 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003546 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003547
3548 if (is_add && !ret)
3549 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003550 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003551}
3552
Tejun Heo2da440a2014-02-11 11:52:48 -05003553static void cgroup_exit_cftypes(struct cftype *cfts)
3554{
3555 struct cftype *cft;
3556
Tejun Heo2bd59d42014-02-11 11:52:49 -05003557 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3558 /* free copy for custom atomic_write_len, see init_cftypes() */
3559 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3560 kfree(cft->kf_ops);
3561 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003562 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003563
3564 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003565 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003566 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003567}
3568
Tejun Heo2bd59d42014-02-11 11:52:49 -05003569static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003570{
3571 struct cftype *cft;
3572
Tejun Heo2bd59d42014-02-11 11:52:49 -05003573 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3574 struct kernfs_ops *kf_ops;
3575
Tejun Heo0adb0702014-02-12 09:29:48 -05003576 WARN_ON(cft->ss || cft->kf_ops);
3577
Tejun Heo2bd59d42014-02-11 11:52:49 -05003578 if (cft->seq_start)
3579 kf_ops = &cgroup_kf_ops;
3580 else
3581 kf_ops = &cgroup_kf_single_ops;
3582
3583 /*
3584 * Ugh... if @cft wants a custom max_write_len, we need to
3585 * make a copy of kf_ops to set its atomic_write_len.
3586 */
3587 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3588 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3589 if (!kf_ops) {
3590 cgroup_exit_cftypes(cfts);
3591 return -ENOMEM;
3592 }
3593 kf_ops->atomic_write_len = cft->max_write_len;
3594 }
3595
3596 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003597 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003598 }
3599
3600 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003601}
3602
Tejun Heo21a2d342014-02-12 09:29:49 -05003603static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3604{
Tejun Heo01f64742014-05-13 12:19:23 -04003605 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003606
3607 if (!cfts || !cfts[0].ss)
3608 return -ENOENT;
3609
3610 list_del(&cfts->node);
3611 cgroup_apply_cftypes(cfts, false);
3612 cgroup_exit_cftypes(cfts);
3613 return 0;
3614}
3615
Tejun Heo8e3f6542012-04-01 12:09:55 -07003616/**
Tejun Heo80b13582014-02-12 09:29:48 -05003617 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3618 * @cfts: zero-length name terminated array of cftypes
3619 *
3620 * Unregister @cfts. Files described by @cfts are removed from all
3621 * existing cgroups and all future cgroups won't have them either. This
3622 * function can be called anytime whether @cfts' subsys is attached or not.
3623 *
3624 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3625 * registered.
3626 */
3627int cgroup_rm_cftypes(struct cftype *cfts)
3628{
Tejun Heo21a2d342014-02-12 09:29:49 -05003629 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003630
Tejun Heo01f64742014-05-13 12:19:23 -04003631 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003632 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003633 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003634 return ret;
3635}
3636
3637/**
3638 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3639 * @ss: target cgroup subsystem
3640 * @cfts: zero-length name terminated array of cftypes
3641 *
3642 * Register @cfts to @ss. Files described by @cfts are created for all
3643 * existing cgroups to which @ss is attached and all future cgroups will
3644 * have them too. This function can be called anytime whether @ss is
3645 * attached or not.
3646 *
3647 * Returns 0 on successful registration, -errno on failure. Note that this
3648 * function currently returns 0 as long as @cfts registration is successful
3649 * even if some file creation attempts on existing cgroups fail.
3650 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003651static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003652{
Tejun Heo9ccece82013-06-28 16:24:11 -07003653 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003654
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003655 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08003656 return 0;
3657
Li Zefandc5736e2014-02-17 10:41:50 +08003658 if (!cfts || cfts[0].name[0] == '\0')
3659 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003660
Tejun Heo2bd59d42014-02-11 11:52:49 -05003661 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003662 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003663 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003664
Tejun Heo01f64742014-05-13 12:19:23 -04003665 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003666
Tejun Heo0adb0702014-02-12 09:29:48 -05003667 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003668 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003669 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003670 cgroup_rm_cftypes_locked(cfts);
3671
Tejun Heo01f64742014-05-13 12:19:23 -04003672 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003673 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003674}
Tejun Heo79578622012-04-01 12:09:56 -07003675
3676/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003677 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3678 * @ss: target cgroup subsystem
3679 * @cfts: zero-length name terminated array of cftypes
3680 *
3681 * Similar to cgroup_add_cftypes() but the added files are only used for
3682 * the default hierarchy.
3683 */
3684int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3685{
3686 struct cftype *cft;
3687
3688 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003689 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003690 return cgroup_add_cftypes(ss, cfts);
3691}
3692
3693/**
3694 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3695 * @ss: target cgroup subsystem
3696 * @cfts: zero-length name terminated array of cftypes
3697 *
3698 * Similar to cgroup_add_cftypes() but the added files are only used for
3699 * the legacy hierarchies.
3700 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003701int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3702{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003703 struct cftype *cft;
3704
Tejun Heoe4b70372015-10-15 17:00:43 -04003705 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3706 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04003707 return cgroup_add_cftypes(ss, cfts);
3708}
3709
Li Zefana043e3b2008-02-23 15:24:09 -08003710/**
Tejun Heo34c06252015-11-05 00:12:24 -05003711 * cgroup_file_notify - generate a file modified event for a cgroup_file
3712 * @cfile: target cgroup_file
3713 *
3714 * @cfile must have been obtained by setting cftype->file_offset.
3715 */
3716void cgroup_file_notify(struct cgroup_file *cfile)
3717{
3718 unsigned long flags;
3719
3720 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3721 if (cfile->kn)
3722 kernfs_notify(cfile->kn);
3723 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3724}
3725
3726/**
Li Zefana043e3b2008-02-23 15:24:09 -08003727 * cgroup_task_count - count the number of tasks in a cgroup.
3728 * @cgrp: the cgroup in question
3729 *
3730 * Return the number of tasks in the cgroup.
3731 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003732static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003733{
3734 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003735 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003736
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003737 spin_lock_bh(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003738 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3739 count += atomic_read(&link->cset->refcount);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003740 spin_unlock_bh(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003741 return count;
3742}
3743
Tejun Heo574bd9f2012-11-09 09:12:29 -08003744/**
Tejun Heo492eb212013-08-08 20:11:25 -04003745 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003746 * @pos: the current position (%NULL to initiate traversal)
3747 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003748 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003749 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003750 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003751 * that @parent and @pos are accessible. The next sibling is guaranteed to
3752 * be returned regardless of their states.
3753 *
3754 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3755 * css which finished ->css_online() is guaranteed to be visible in the
3756 * future iterations and will stay visible until the last reference is put.
3757 * A css which hasn't finished ->css_online() or already finished
3758 * ->css_offline() may show up during traversal. It's each subsystem's
3759 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003760 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003761struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3762 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003763{
Tejun Heoc2931b72014-05-16 13:22:51 -04003764 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003765
Tejun Heo8353da12014-05-13 12:19:23 -04003766 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003767
3768 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003769 * @pos could already have been unlinked from the sibling list.
3770 * Once a cgroup is removed, its ->sibling.next is no longer
3771 * updated when its next sibling changes. CSS_RELEASED is set when
3772 * @pos is taken off list, at which time its next pointer is valid,
3773 * and, as releases are serialized, the one pointed to by the next
3774 * pointer is guaranteed to not have started release yet. This
3775 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3776 * critical section, the one pointed to by its next pointer is
3777 * guaranteed to not have finished its RCU grace period even if we
3778 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003779 *
Tejun Heode3f0342014-05-16 13:22:49 -04003780 * If @pos has CSS_RELEASED set, its next pointer can't be
3781 * dereferenced; however, as each css is given a monotonically
3782 * increasing unique serial number and always appended to the
3783 * sibling list, the next one can be found by walking the parent's
3784 * children until the first css with higher serial number than
3785 * @pos's. While this path can be slower, it happens iff iteration
3786 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003787 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003788 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003789 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3790 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3791 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003792 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003793 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003794 if (next->serial_nr > pos->serial_nr)
3795 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003796 }
3797
Tejun Heo3b281af2014-04-23 11:13:15 -04003798 /*
3799 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003800 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003801 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003802 if (&next->sibling != &parent->children)
3803 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003804 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003805}
Tejun Heo53fa5262013-05-24 10:55:38 +09003806
3807/**
Tejun Heo492eb212013-08-08 20:11:25 -04003808 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003809 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003810 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003811 *
Tejun Heo492eb212013-08-08 20:11:25 -04003812 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003813 * to visit for pre-order traversal of @root's descendants. @root is
3814 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003815 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003816 * While this function requires cgroup_mutex or RCU read locking, it
3817 * doesn't require the whole traversal to be contained in a single critical
3818 * section. This function will return the correct next descendant as long
3819 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003820 *
3821 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3822 * css which finished ->css_online() is guaranteed to be visible in the
3823 * future iterations and will stay visible until the last reference is put.
3824 * A css which hasn't finished ->css_online() or already finished
3825 * ->css_offline() may show up during traversal. It's each subsystem's
3826 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003827 */
Tejun Heo492eb212013-08-08 20:11:25 -04003828struct cgroup_subsys_state *
3829css_next_descendant_pre(struct cgroup_subsys_state *pos,
3830 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003831{
Tejun Heo492eb212013-08-08 20:11:25 -04003832 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003833
Tejun Heo8353da12014-05-13 12:19:23 -04003834 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003835
Tejun Heobd8815a2013-08-08 20:11:27 -04003836 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003837 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003838 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003839
3840 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003841 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003842 if (next)
3843 return next;
3844
3845 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003846 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003847 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003848 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003849 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003850 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003851 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003852
3853 return NULL;
3854}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003855
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003856/**
Tejun Heo492eb212013-08-08 20:11:25 -04003857 * css_rightmost_descendant - return the rightmost descendant of a css
3858 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003859 *
Tejun Heo492eb212013-08-08 20:11:25 -04003860 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3861 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003862 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003863 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003864 * While this function requires cgroup_mutex or RCU read locking, it
3865 * doesn't require the whole traversal to be contained in a single critical
3866 * section. This function will return the correct rightmost descendant as
3867 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003868 */
Tejun Heo492eb212013-08-08 20:11:25 -04003869struct cgroup_subsys_state *
3870css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003871{
Tejun Heo492eb212013-08-08 20:11:25 -04003872 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003873
Tejun Heo8353da12014-05-13 12:19:23 -04003874 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003875
3876 do {
3877 last = pos;
3878 /* ->prev isn't RCU safe, walk ->next till the end */
3879 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003880 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003881 pos = tmp;
3882 } while (pos);
3883
3884 return last;
3885}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003886
Tejun Heo492eb212013-08-08 20:11:25 -04003887static struct cgroup_subsys_state *
3888css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003889{
Tejun Heo492eb212013-08-08 20:11:25 -04003890 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003891
3892 do {
3893 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003894 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003895 } while (pos);
3896
3897 return last;
3898}
3899
3900/**
Tejun Heo492eb212013-08-08 20:11:25 -04003901 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003902 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003903 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003904 *
Tejun Heo492eb212013-08-08 20:11:25 -04003905 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003906 * to visit for post-order traversal of @root's descendants. @root is
3907 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003908 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003909 * While this function requires cgroup_mutex or RCU read locking, it
3910 * doesn't require the whole traversal to be contained in a single critical
3911 * section. This function will return the correct next descendant as long
3912 * as both @pos and @cgroup are accessible and @pos is a descendant of
3913 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003914 *
3915 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3916 * css which finished ->css_online() is guaranteed to be visible in the
3917 * future iterations and will stay visible until the last reference is put.
3918 * A css which hasn't finished ->css_online() or already finished
3919 * ->css_offline() may show up during traversal. It's each subsystem's
3920 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003921 */
Tejun Heo492eb212013-08-08 20:11:25 -04003922struct cgroup_subsys_state *
3923css_next_descendant_post(struct cgroup_subsys_state *pos,
3924 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003925{
Tejun Heo492eb212013-08-08 20:11:25 -04003926 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003927
Tejun Heo8353da12014-05-13 12:19:23 -04003928 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003929
Tejun Heo58b79a92013-09-06 15:31:08 -04003930 /* if first iteration, visit leftmost descendant which may be @root */
3931 if (!pos)
3932 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003933
Tejun Heobd8815a2013-08-08 20:11:27 -04003934 /* if we visited @root, we're done */
3935 if (pos == root)
3936 return NULL;
3937
Tejun Heo574bd9f2012-11-09 09:12:29 -08003938 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003939 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003940 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003941 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003942
3943 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003944 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003945}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003946
Tejun Heof3d46502014-05-16 13:22:52 -04003947/**
3948 * css_has_online_children - does a css have online children
3949 * @css: the target css
3950 *
3951 * Returns %true if @css has any online children; otherwise, %false. This
3952 * function can be called from any context but the caller is responsible
3953 * for synchronizing against on/offlining as necessary.
3954 */
3955bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003956{
Tejun Heof3d46502014-05-16 13:22:52 -04003957 struct cgroup_subsys_state *child;
3958 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003959
3960 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003961 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003962 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003963 ret = true;
3964 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003965 }
3966 }
3967 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003968 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003969}
3970
Tejun Heo0942eee2013-08-08 20:11:26 -04003971/**
Tejun Heoecb9d532015-10-15 16:41:52 -04003972 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003973 * @it: the iterator to advance
3974 *
3975 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003976 */
Tejun Heoecb9d532015-10-15 16:41:52 -04003977static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003978{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003979 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003980 struct cgrp_cset_link *link;
3981 struct css_set *cset;
3982
Tejun Heof0d9a5f2015-10-15 16:41:53 -04003983 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04003984
Tejun Heod5158762013-08-08 20:11:26 -04003985 /* Advance to the next non-empty css_set */
3986 do {
3987 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003988 if (l == it->cset_head) {
3989 it->cset_pos = NULL;
Tejun Heoecb9d532015-10-15 16:41:52 -04003990 it->task_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003991 return;
3992 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003993
3994 if (it->ss) {
3995 cset = container_of(l, struct css_set,
3996 e_cset_node[it->ss->id]);
3997 } else {
3998 link = list_entry(l, struct cgrp_cset_link, cset_link);
3999 cset = link->cset;
4000 }
Tejun Heo0de09422015-10-15 16:41:49 -04004001 } while (!css_set_populated(cset));
Tejun Heoc7561122014-02-25 10:04:01 -05004002
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004003 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05004004
4005 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004006 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05004007 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004008 it->task_pos = cset->mg_tasks.next;
4009
4010 it->tasks_head = &cset->tasks;
4011 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heoed27b9f2015-10-15 16:41:52 -04004012
4013 /*
4014 * We don't keep css_sets locked across iteration steps and thus
4015 * need to take steps to ensure that iteration can be resumed after
4016 * the lock is re-acquired. Iteration is performed at two levels -
4017 * css_sets and tasks in them.
4018 *
4019 * Once created, a css_set never leaves its cgroup lists, so a
4020 * pinned css_set is guaranteed to stay put and we can resume
4021 * iteration afterwards.
4022 *
4023 * Tasks may leave @cset across iteration steps. This is resolved
4024 * by registering each iterator with the css_set currently being
4025 * walked and making css_set_move_task() advance iterators whose
4026 * next task is leaving.
4027 */
4028 if (it->cur_cset) {
4029 list_del(&it->iters_node);
4030 put_css_set_locked(it->cur_cset);
4031 }
4032 get_css_set(cset);
4033 it->cur_cset = cset;
4034 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04004035}
4036
Tejun Heoecb9d532015-10-15 16:41:52 -04004037static void css_task_iter_advance(struct css_task_iter *it)
4038{
4039 struct list_head *l = it->task_pos;
4040
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004041 lockdep_assert_held(&css_set_lock);
Tejun Heoecb9d532015-10-15 16:41:52 -04004042 WARN_ON_ONCE(!l);
4043
4044 /*
4045 * Advance iterator to find next entry. cset->tasks is consumed
4046 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
4047 * next cset.
4048 */
4049 l = l->next;
4050
4051 if (l == it->tasks_head)
4052 l = it->mg_tasks_head->next;
4053
4054 if (l == it->mg_tasks_head)
4055 css_task_iter_advance_css_set(it);
4056 else
4057 it->task_pos = l;
4058}
4059
Tejun Heo0942eee2013-08-08 20:11:26 -04004060/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004061 * css_task_iter_start - initiate task iteration
4062 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04004063 * @it: the task iterator to use
4064 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004065 * Initiate iteration through the tasks of @css. The caller can call
4066 * css_task_iter_next() to walk through the tasks until the function
4067 * returns NULL. On completion of iteration, css_task_iter_end() must be
4068 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04004069 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004070void css_task_iter_start(struct cgroup_subsys_state *css,
4071 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004072{
Tejun Heo56fde9e2014-02-13 06:58:38 -05004073 /* no one should try to iterate before mounting cgroups */
4074 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07004075
Tejun Heoed27b9f2015-10-15 16:41:52 -04004076 memset(it, 0, sizeof(*it));
4077
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004078 spin_lock_bh(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004079
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004080 it->ss = css->ss;
4081
4082 if (it->ss)
4083 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4084 else
4085 it->cset_pos = &css->cgroup->cset_links;
4086
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004087 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004088
Tejun Heoecb9d532015-10-15 16:41:52 -04004089 css_task_iter_advance_css_set(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004090
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004091 spin_unlock_bh(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004092}
Paul Menage817929e2007-10-18 23:39:36 -07004093
Tejun Heo0942eee2013-08-08 20:11:26 -04004094/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004095 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004096 * @it: the task iterator being iterated
4097 *
4098 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004099 * initialized via css_task_iter_start(). Returns NULL when the iteration
4100 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004101 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004102struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004103{
Tejun Heod5745672015-10-29 11:43:05 +09004104 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004105 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004106 it->cur_task = NULL;
4107 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004108
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004109 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004110
Tejun Heod5745672015-10-29 11:43:05 +09004111 if (it->task_pos) {
4112 it->cur_task = list_entry(it->task_pos, struct task_struct,
4113 cg_list);
4114 get_task_struct(it->cur_task);
4115 css_task_iter_advance(it);
4116 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004117
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004118 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004119
4120 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004121}
4122
Tejun Heo0942eee2013-08-08 20:11:26 -04004123/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004124 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004125 * @it: the task iterator to finish
4126 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004127 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004128 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004129void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004130{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004131 if (it->cur_cset) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004132 spin_lock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004133 list_del(&it->iters_node);
4134 put_css_set_locked(it->cur_cset);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004135 spin_unlock_bh(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004136 }
4137
4138 if (it->cur_task)
4139 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004140}
4141
4142/**
4143 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4144 * @to: cgroup to which the tasks will be moved
4145 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05004146 *
4147 * Locking rules between cgroup_post_fork() and the migration path
4148 * guarantee that, if a task is forking while being migrated, the new child
4149 * is guaranteed to be either visible in the source cgroup after the
4150 * parent's migration is complete or put into the target cgroup. No task
4151 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07004152 */
4153int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
4154{
Tejun Heo952aaa12014-02-25 10:04:03 -05004155 LIST_HEAD(preloaded_csets);
4156 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004157 struct css_task_iter it;
4158 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05004159 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004160
Tejun Heo952aaa12014-02-25 10:04:03 -05004161 mutex_lock(&cgroup_mutex);
4162
4163 /* all tasks in @from are being moved, all csets are source */
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004164 spin_lock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004165 list_for_each_entry(link, &from->cset_links, cset_link)
4166 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004167 spin_unlock_bh(&css_set_lock);
Tejun Heo952aaa12014-02-25 10:04:03 -05004168
4169 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4170 if (ret)
4171 goto out_err;
4172
4173 /*
Rami Rosen2cfa2b12016-01-09 23:33:06 +02004174 * Migrate tasks one-by-one until @from is empty. This fails iff
Tejun Heo952aaa12014-02-25 10:04:03 -05004175 * ->can_attach() fails.
4176 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05004177 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04004178 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004179 task = css_task_iter_next(&it);
4180 if (task)
4181 get_task_struct(task);
4182 css_task_iter_end(&it);
4183
4184 if (task) {
Tejun Heo9af2ec42015-09-11 15:00:20 -04004185 ret = cgroup_migrate(task, false, to);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004186 put_task_struct(task);
4187 }
4188 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05004189out_err:
4190 cgroup_migrate_finish(&preloaded_csets);
4191 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05004192 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07004193}
4194
Paul Menage817929e2007-10-18 23:39:36 -07004195/*
Ben Blum102a7752009-09-23 15:56:26 -07004196 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004197 *
4198 * Reading this file can return large amounts of data if a cgroup has
4199 * *lots* of attached tasks. So it may need several calls to read(),
4200 * but we cannot guarantee that the information we produce is correct
4201 * unless we produce it entirely atomically.
4202 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07004203 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004204
Li Zefan24528252012-01-20 11:58:43 +08004205/* which pidlist file are we talking about? */
4206enum cgroup_filetype {
4207 CGROUP_FILE_PROCS,
4208 CGROUP_FILE_TASKS,
4209};
4210
4211/*
4212 * A pidlist is a list of pids that virtually represents the contents of one
4213 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4214 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4215 * to the cgroup.
4216 */
4217struct cgroup_pidlist {
4218 /*
4219 * used to find which pidlist is wanted. doesn't change as long as
4220 * this particular list stays in the list.
4221 */
4222 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
4223 /* array of xids */
4224 pid_t *list;
4225 /* how many elements the above list has */
4226 int length;
Li Zefan24528252012-01-20 11:58:43 +08004227 /* each of these stored in a list by its cgroup */
4228 struct list_head links;
4229 /* pointer to the cgroup we belong to, for list removal purposes */
4230 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05004231 /* for delayed destruction */
4232 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08004233};
4234
Paul Menagebbcb81d2007-10-18 23:39:32 -07004235/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07004236 * The following two functions "fix" the issue where there are more pids
4237 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4238 * TODO: replace with a kernel-wide solution to this problem
4239 */
4240#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4241static void *pidlist_allocate(int count)
4242{
4243 if (PIDLIST_TOO_LARGE(count))
4244 return vmalloc(count * sizeof(pid_t));
4245 else
4246 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
4247}
Tejun Heob1a21362013-11-29 10:42:58 -05004248
Ben Blumd1d9fd32009-09-23 15:56:28 -07004249static void pidlist_free(void *p)
4250{
Bandan Das58794512015-03-02 17:51:10 -05004251 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004252}
Ben Blumd1d9fd32009-09-23 15:56:28 -07004253
4254/*
Tejun Heob1a21362013-11-29 10:42:58 -05004255 * Used to destroy all pidlists lingering waiting for destroy timer. None
4256 * should be left afterwards.
4257 */
4258static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
4259{
4260 struct cgroup_pidlist *l, *tmp_l;
4261
4262 mutex_lock(&cgrp->pidlist_mutex);
4263 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
4264 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
4265 mutex_unlock(&cgrp->pidlist_mutex);
4266
4267 flush_workqueue(cgroup_pidlist_destroy_wq);
4268 BUG_ON(!list_empty(&cgrp->pidlists));
4269}
4270
4271static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
4272{
4273 struct delayed_work *dwork = to_delayed_work(work);
4274 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
4275 destroy_dwork);
4276 struct cgroup_pidlist *tofree = NULL;
4277
4278 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05004279
4280 /*
Tejun Heo04502362013-11-29 10:42:59 -05004281 * Destroy iff we didn't get queued again. The state won't change
4282 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05004283 */
Tejun Heo04502362013-11-29 10:42:59 -05004284 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05004285 list_del(&l->links);
4286 pidlist_free(l->list);
4287 put_pid_ns(l->key.ns);
4288 tofree = l;
4289 }
4290
Tejun Heob1a21362013-11-29 10:42:58 -05004291 mutex_unlock(&l->owner->pidlist_mutex);
4292 kfree(tofree);
4293}
4294
4295/*
Ben Blum102a7752009-09-23 15:56:26 -07004296 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07004297 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07004298 */
Li Zefan6ee211a2013-03-12 15:36:00 -07004299static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07004300{
Ben Blum102a7752009-09-23 15:56:26 -07004301 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07004302
4303 /*
4304 * we presume the 0th element is unique, so i starts at 1. trivial
4305 * edge cases first; no work needs to be done for either
4306 */
4307 if (length == 0 || length == 1)
4308 return length;
4309 /* src and dest walk down the list; dest counts unique elements */
4310 for (src = 1; src < length; src++) {
4311 /* find next unique element */
4312 while (list[src] == list[src-1]) {
4313 src++;
4314 if (src == length)
4315 goto after;
4316 }
4317 /* dest always points to where the next unique element goes */
4318 list[dest] = list[src];
4319 dest++;
4320 }
4321after:
Ben Blum102a7752009-09-23 15:56:26 -07004322 return dest;
4323}
4324
Tejun Heoafb2bc12013-11-29 10:42:59 -05004325/*
4326 * The two pid files - task and cgroup.procs - guaranteed that the result
4327 * is sorted, which forced this whole pidlist fiasco. As pid order is
4328 * different per namespace, each namespace needs differently sorted list,
4329 * making it impossible to use, for example, single rbtree of member tasks
4330 * sorted by task pointer. As pidlists can be fairly large, allocating one
4331 * per open file is dangerous, so cgroup had to implement shared pool of
4332 * pidlists keyed by cgroup and namespace.
4333 *
4334 * All this extra complexity was caused by the original implementation
4335 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04004336 * want to do away with it. Explicitly scramble sort order if on the
4337 * default hierarchy so that no such expectation exists in the new
4338 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05004339 *
4340 * Scrambling is done by swapping every two consecutive bits, which is
4341 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4342 */
4343static pid_t pid_fry(pid_t pid)
4344{
4345 unsigned a = pid & 0x55555555;
4346 unsigned b = pid & 0xAAAAAAAA;
4347
4348 return (a << 1) | (b >> 1);
4349}
4350
4351static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4352{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004353 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004354 return pid_fry(pid);
4355 else
4356 return pid;
4357}
4358
Ben Blum102a7752009-09-23 15:56:26 -07004359static int cmppid(const void *a, const void *b)
4360{
4361 return *(pid_t *)a - *(pid_t *)b;
4362}
4363
Tejun Heoafb2bc12013-11-29 10:42:59 -05004364static int fried_cmppid(const void *a, const void *b)
4365{
4366 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4367}
4368
Ben Blum72a8cb32009-09-23 15:56:27 -07004369static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4370 enum cgroup_filetype type)
4371{
4372 struct cgroup_pidlist *l;
4373 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004374 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004375
Tejun Heoe6b81712013-11-29 10:42:59 -05004376 lockdep_assert_held(&cgrp->pidlist_mutex);
4377
4378 list_for_each_entry(l, &cgrp->pidlists, links)
4379 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004380 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004381 return NULL;
4382}
4383
Ben Blum72a8cb32009-09-23 15:56:27 -07004384/*
4385 * find the appropriate pidlist for our purpose (given procs vs tasks)
4386 * returns with the lock on that pidlist already held, and takes care
4387 * of the use count, or returns NULL with no locks held if we're out of
4388 * memory.
4389 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004390static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4391 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004392{
4393 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004394
Tejun Heoe6b81712013-11-29 10:42:59 -05004395 lockdep_assert_held(&cgrp->pidlist_mutex);
4396
4397 l = cgroup_pidlist_find(cgrp, type);
4398 if (l)
4399 return l;
4400
Ben Blum72a8cb32009-09-23 15:56:27 -07004401 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004402 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004403 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004404 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004405
Tejun Heob1a21362013-11-29 10:42:58 -05004406 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004407 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004408 /* don't need task_nsproxy() if we're looking at ourself */
4409 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004410 l->owner = cgrp;
4411 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004412 return l;
4413}
4414
4415/*
Ben Blum102a7752009-09-23 15:56:26 -07004416 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4417 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004418static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4419 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004420{
4421 pid_t *array;
4422 int length;
4423 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004424 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004425 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004426 struct cgroup_pidlist *l;
4427
Tejun Heo4bac00d2013-11-29 10:42:59 -05004428 lockdep_assert_held(&cgrp->pidlist_mutex);
4429
Ben Blum102a7752009-09-23 15:56:26 -07004430 /*
4431 * If cgroup gets more users after we read count, we won't have
4432 * enough space - tough. This race is indistinguishable to the
4433 * caller from the case that the additional cgroup users didn't
4434 * show up until sometime later on.
4435 */
4436 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004437 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004438 if (!array)
4439 return -ENOMEM;
4440 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004441 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004442 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004443 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004444 break;
Ben Blum102a7752009-09-23 15:56:26 -07004445 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004446 if (type == CGROUP_FILE_PROCS)
4447 pid = task_tgid_vnr(tsk);
4448 else
4449 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004450 if (pid > 0) /* make sure to only use valid results */
4451 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004452 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004453 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004454 length = n;
4455 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004456 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004457 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4458 else
4459 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004460 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004461 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004462
Tejun Heoe6b81712013-11-29 10:42:59 -05004463 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004464 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004465 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004466 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004467 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004468
4469 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004470 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004471 l->list = array;
4472 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004473 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004474 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004475}
4476
Balbir Singh846c7bb2007-10-18 23:39:44 -07004477/**
Li Zefana043e3b2008-02-23 15:24:09 -08004478 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004479 * @stats: cgroupstats to fill information into
4480 * @dentry: A dentry entry belonging to the cgroup for which stats have
4481 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004482 *
4483 * Build and fill cgroupstats so that taskstats can export it to user
4484 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004485 */
4486int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4487{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004488 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004489 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004490 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004491 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004492
Tejun Heo2bd59d42014-02-11 11:52:49 -05004493 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4494 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4495 kernfs_type(kn) != KERNFS_DIR)
4496 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004497
Li Zefanbad34662014-02-14 16:54:28 +08004498 mutex_lock(&cgroup_mutex);
4499
Tejun Heo2bd59d42014-02-11 11:52:49 -05004500 /*
4501 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004502 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004503 * @kn->priv is RCU safe. Let's do the RCU dancing.
4504 */
4505 rcu_read_lock();
4506 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004507 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004508 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004509 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004510 return -ENOENT;
4511 }
Li Zefanbad34662014-02-14 16:54:28 +08004512 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004513
Tejun Heo9d800df2014-05-14 09:15:00 -04004514 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004515 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004516 switch (tsk->state) {
4517 case TASK_RUNNING:
4518 stats->nr_running++;
4519 break;
4520 case TASK_INTERRUPTIBLE:
4521 stats->nr_sleeping++;
4522 break;
4523 case TASK_UNINTERRUPTIBLE:
4524 stats->nr_uninterruptible++;
4525 break;
4526 case TASK_STOPPED:
4527 stats->nr_stopped++;
4528 break;
4529 default:
4530 if (delayacct_is_task_waiting_on_io(tsk))
4531 stats->nr_io_wait++;
4532 break;
4533 }
4534 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004535 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004536
Li Zefanbad34662014-02-14 16:54:28 +08004537 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004538 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004539}
4540
Paul Menage8f3ff202009-09-23 15:56:25 -07004541
Paul Menagecc31edc2008-10-18 20:28:04 -07004542/*
Ben Blum102a7752009-09-23 15:56:26 -07004543 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004544 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004545 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004546 */
4547
Ben Blum102a7752009-09-23 15:56:26 -07004548static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004549{
4550 /*
4551 * Initially we receive a position value that corresponds to
4552 * one more than the last pid shown (or 0 on the first call or
4553 * after a seek to the start). Use a binary-search to find the
4554 * next pid to display, if any
4555 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004556 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004557 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004558 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004559 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004560 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004561 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004562
Tejun Heo4bac00d2013-11-29 10:42:59 -05004563 mutex_lock(&cgrp->pidlist_mutex);
4564
4565 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004566 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004567 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004568 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004569 * could already have been destroyed.
4570 */
Tejun Heo5d224442013-12-05 12:28:04 -05004571 if (of->priv)
4572 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004573
4574 /*
4575 * Either this is the first start() after open or the matching
4576 * pidlist has been destroyed inbetween. Create a new one.
4577 */
Tejun Heo5d224442013-12-05 12:28:04 -05004578 if (!of->priv) {
4579 ret = pidlist_array_load(cgrp, type,
4580 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004581 if (ret)
4582 return ERR_PTR(ret);
4583 }
Tejun Heo5d224442013-12-05 12:28:04 -05004584 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004585
Paul Menagecc31edc2008-10-18 20:28:04 -07004586 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004587 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004588
Paul Menagecc31edc2008-10-18 20:28:04 -07004589 while (index < end) {
4590 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004591 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004592 index = mid;
4593 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004594 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004595 index = mid + 1;
4596 else
4597 end = mid;
4598 }
4599 }
4600 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004601 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004602 return NULL;
4603 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004604 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004605 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004606 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004607}
4608
Ben Blum102a7752009-09-23 15:56:26 -07004609static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004610{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004611 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004612 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004613
Tejun Heo5d224442013-12-05 12:28:04 -05004614 if (l)
4615 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004616 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004617 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004618}
4619
Ben Blum102a7752009-09-23 15:56:26 -07004620static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004621{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004622 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004623 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004624 pid_t *p = v;
4625 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004626 /*
4627 * Advance to the next pid in the array. If this goes off the
4628 * end, we're done
4629 */
4630 p++;
4631 if (p >= end) {
4632 return NULL;
4633 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004634 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004635 return p;
4636 }
4637}
4638
Ben Blum102a7752009-09-23 15:56:26 -07004639static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004640{
Joe Perches94ff2122015-04-15 16:18:20 -07004641 seq_printf(s, "%d\n", *(int *)v);
4642
4643 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004644}
4645
Tejun Heo182446d2013-08-08 20:11:24 -04004646static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4647 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004648{
Tejun Heo182446d2013-08-08 20:11:24 -04004649 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004650}
4651
Tejun Heo182446d2013-08-08 20:11:24 -04004652static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4653 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004654{
Paul Menage6379c102008-07-25 01:47:01 -07004655 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004656 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004657 else
Tejun Heo182446d2013-08-08 20:11:24 -04004658 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004659 return 0;
4660}
4661
Tejun Heo182446d2013-08-08 20:11:24 -04004662static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4663 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004664{
Tejun Heo182446d2013-08-08 20:11:24 -04004665 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004666}
4667
Tejun Heo182446d2013-08-08 20:11:24 -04004668static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4669 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004670{
4671 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004672 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004673 else
Tejun Heo182446d2013-08-08 20:11:24 -04004674 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004675 return 0;
4676}
4677
Tejun Heoa14c6872014-07-15 11:05:09 -04004678/* cgroup core interface files for the default hierarchy */
4679static struct cftype cgroup_dfl_base_files[] = {
4680 {
4681 .name = "cgroup.procs",
Tejun Heo6f60ead2015-09-18 17:54:23 -04004682 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heoa14c6872014-07-15 11:05:09 -04004683 .seq_start = cgroup_pidlist_start,
4684 .seq_next = cgroup_pidlist_next,
4685 .seq_stop = cgroup_pidlist_stop,
4686 .seq_show = cgroup_pidlist_show,
4687 .private = CGROUP_FILE_PROCS,
4688 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004689 },
4690 {
4691 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04004692 .seq_show = cgroup_controllers_show,
4693 },
4694 {
4695 .name = "cgroup.subtree_control",
4696 .seq_show = cgroup_subtree_control_show,
4697 .write = cgroup_subtree_control_write,
4698 },
4699 {
Tejun Heo4a07c222015-09-18 17:54:22 -04004700 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04004701 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004702 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04004703 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004704 },
4705 { } /* terminate */
4706};
4707
4708/* cgroup core interface files for the legacy hierarchies */
4709static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004710 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004711 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004712 .seq_start = cgroup_pidlist_start,
4713 .seq_next = cgroup_pidlist_next,
4714 .seq_stop = cgroup_pidlist_stop,
4715 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004716 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004717 .write = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004718 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004719 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004720 .name = "cgroup.clone_children",
4721 .read_u64 = cgroup_clone_children_read,
4722 .write_u64 = cgroup_clone_children_write,
4723 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004724 {
Tejun Heo873fe092013-04-14 20:15:26 -07004725 .name = "cgroup.sane_behavior",
4726 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004727 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004728 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004729 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004730 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004731 .seq_start = cgroup_pidlist_start,
4732 .seq_next = cgroup_pidlist_next,
4733 .seq_stop = cgroup_pidlist_stop,
4734 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004735 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004736 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004737 },
4738 {
4739 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004740 .read_u64 = cgroup_read_notify_on_release,
4741 .write_u64 = cgroup_write_notify_on_release,
4742 },
Tejun Heo873fe092013-04-14 20:15:26 -07004743 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004744 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004745 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004746 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004747 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004748 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004749 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004750 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004751};
4752
Tejun Heo0c21ead2013-08-13 20:22:51 -04004753/*
4754 * css destruction is four-stage process.
4755 *
4756 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4757 * Implemented in kill_css().
4758 *
4759 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004760 * and thus css_tryget_online() is guaranteed to fail, the css can be
4761 * offlined by invoking offline_css(). After offlining, the base ref is
4762 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004763 *
4764 * 3. When the percpu_ref reaches zero, the only possible remaining
4765 * accessors are inside RCU read sections. css_release() schedules the
4766 * RCU callback.
4767 *
4768 * 4. After the grace period, the css can be freed. Implemented in
4769 * css_free_work_fn().
4770 *
4771 * It is actually hairier because both step 2 and 4 require process context
4772 * and thus involve punting to css->destroy_work adding two additional
4773 * steps to the already complex sequence.
4774 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004775static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004776{
4777 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004778 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004779 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004780 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004781
Tejun Heo9a1049d2014-06-28 08:10:14 -04004782 percpu_ref_exit(&css->refcnt);
4783
Vladimir Davydov01e58652015-02-12 14:59:26 -08004784 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004785 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004786 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08004787 int id = css->id;
4788
Vladimir Davydov01e58652015-02-12 14:59:26 -08004789 ss->css_free(css);
4790 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004791 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05004792
4793 if (parent)
4794 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04004795 } else {
4796 /* cgroup free path */
4797 atomic_dec(&cgrp->root->nr_cgrps);
4798 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004799 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004800
Tejun Heod51f39b2014-05-16 13:22:48 -04004801 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004802 /*
4803 * We get a ref to the parent, and put the ref when
4804 * this cgroup is being freed, so it's guaranteed
4805 * that the parent won't be destroyed before its
4806 * children.
4807 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004808 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004809 kernfs_put(cgrp->kn);
4810 kfree(cgrp);
4811 } else {
4812 /*
4813 * This is root cgroup's refcnt reaching zero,
4814 * which indicates that the root should be
4815 * released.
4816 */
4817 cgroup_destroy_root(cgrp->root);
4818 }
4819 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004820}
4821
4822static void css_free_rcu_fn(struct rcu_head *rcu_head)
4823{
4824 struct cgroup_subsys_state *css =
4825 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4826
Tejun Heo0c21ead2013-08-13 20:22:51 -04004827 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004828 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004829}
4830
Tejun Heo25e15d82014-05-14 09:15:02 -04004831static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004832{
4833 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004834 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004835 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004836 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004837
Tejun Heo1fed1b22014-05-16 13:22:49 -04004838 mutex_lock(&cgroup_mutex);
4839
Tejun Heode3f0342014-05-16 13:22:49 -04004840 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004841 list_del_rcu(&css->sibling);
4842
Tejun Heo9d755d32014-05-14 09:15:02 -04004843 if (ss) {
4844 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004845 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004846 if (ss->css_released)
4847 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004848 } else {
4849 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004850 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4851 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004852
4853 /*
4854 * There are two control paths which try to determine
4855 * cgroup from dentry without going through kernfs -
4856 * cgroupstats_build() and css_tryget_online_from_dir().
4857 * Those are supported by RCU protecting clearing of
4858 * cgrp->kn->priv backpointer.
4859 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004860 if (cgrp->kn)
4861 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4862 NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004863 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004864
Tejun Heo1fed1b22014-05-16 13:22:49 -04004865 mutex_unlock(&cgroup_mutex);
4866
Tejun Heo0c21ead2013-08-13 20:22:51 -04004867 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004868}
4869
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004870static void css_release(struct percpu_ref *ref)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004871{
4872 struct cgroup_subsys_state *css =
4873 container_of(ref, struct cgroup_subsys_state, refcnt);
4874
Tejun Heo25e15d82014-05-14 09:15:02 -04004875 INIT_WORK(&css->destroy_work, css_release_work_fn);
4876 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004877}
4878
Tejun Heoddfcada2014-05-04 15:09:14 -04004879static void init_and_link_css(struct cgroup_subsys_state *css,
4880 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004881{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004882 lockdep_assert_held(&cgroup_mutex);
4883
Tejun Heoddfcada2014-05-04 15:09:14 -04004884 cgroup_get(cgrp);
4885
Tejun Heod5c419b2014-05-16 13:22:48 -04004886 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004887 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004888 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004889 INIT_LIST_HEAD(&css->sibling);
4890 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004891 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05004892 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004893
Tejun Heod51f39b2014-05-16 13:22:48 -04004894 if (cgroup_parent(cgrp)) {
4895 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004896 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004897 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004898
Tejun Heoca8bdca2013-08-26 18:40:56 -04004899 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004900}
4901
Li Zefan2a4ac632013-07-31 16:16:40 +08004902/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004903static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004904{
Tejun Heo623f9262013-08-13 11:01:55 -04004905 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004906 int ret = 0;
4907
Tejun Heoa31f2d32012-11-19 08:13:37 -08004908 lockdep_assert_held(&cgroup_mutex);
4909
Tejun Heo92fb9742012-11-19 08:13:38 -08004910 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004911 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004912 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004913 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004914 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05004915
4916 atomic_inc(&css->online_cnt);
4917 if (css->parent)
4918 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04004919 }
Tejun Heob1929db2012-11-19 08:13:38 -08004920 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004921}
4922
Li Zefan2a4ac632013-07-31 16:16:40 +08004923/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004924static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004925{
Tejun Heo623f9262013-08-13 11:01:55 -04004926 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004927
4928 lockdep_assert_held(&cgroup_mutex);
4929
4930 if (!(css->flags & CSS_ONLINE))
4931 return;
4932
Vladimir Davydovfa062352016-03-01 19:56:30 +03004933 if (ss->css_reset)
4934 ss->css_reset(css);
4935
Li Zefand7eeac12013-03-12 15:35:59 -07004936 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004937 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004938
Tejun Heoeb954192013-08-08 20:11:23 -04004939 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004940 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004941
4942 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004943}
4944
Tejun Heoc81c925a2013-12-06 15:11:56 -05004945/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004946 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05004947 * @cgrp: the cgroup new css will be associated with
4948 * @ss: the subsys of new css
4949 *
4950 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004951 * css is online and installed in @cgrp. This function doesn't create the
4952 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004953 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004954static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4955 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004956{
Tejun Heod51f39b2014-05-16 13:22:48 -04004957 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004958 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004959 struct cgroup_subsys_state *css;
4960 int err;
4961
Tejun Heoc81c925a2013-12-06 15:11:56 -05004962 lockdep_assert_held(&cgroup_mutex);
4963
Tejun Heo1fed1b22014-05-16 13:22:49 -04004964 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004965 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004966 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004967
Tejun Heoddfcada2014-05-04 15:09:14 -04004968 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004969
Tejun Heo2aad2a82014-09-24 13:31:50 -04004970 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004971 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004972 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004973
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004974 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004975 if (err < 0)
4976 goto err_free_percpu_ref;
4977 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004978
Tejun Heo15a4c832014-05-04 15:09:14 -04004979 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004980 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004981 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004982
4983 err = online_css(css);
4984 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004985 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004986
Tejun Heoc81c925a2013-12-06 15:11:56 -05004987 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004988 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004989 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 -04004990 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004991 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004992 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004993 ss->warned_broken_hierarchy = true;
4994 }
4995
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05004996 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004997
Tejun Heo1fed1b22014-05-16 13:22:49 -04004998err_list_del:
4999 list_del_rcu(&css->sibling);
Tejun Heo15a4c832014-05-04 15:09:14 -04005000 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08005001err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04005002 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08005003err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04005004 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005005 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005006}
5007
Tejun Heoa5bca212016-03-03 09:57:58 -05005008static struct cgroup *cgroup_create(struct cgroup *parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005009{
Tejun Heoa5bca212016-03-03 09:57:58 -05005010 struct cgroup_root *root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005011 struct cgroup_subsys *ss;
Tejun Heoa5bca212016-03-03 09:57:58 -05005012 struct cgroup *cgrp, *tcgrp;
5013 int level = parent->level + 1;
5014 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005015
Tejun Heo0a950f62012-11-19 09:02:12 -08005016 /* allocate the cgroup and its ID, 0 is reserved for the root */
Tejun Heob11cfb52015-11-20 15:55:52 -05005017 cgrp = kzalloc(sizeof(*cgrp) +
5018 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05005019 if (!cgrp)
5020 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08005021
Tejun Heo2aad2a82014-09-24 13:31:50 -04005022 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04005023 if (ret)
5024 goto out_free_cgrp;
5025
Li Zefan0ab02ca2014-02-11 16:05:46 +08005026 /*
5027 * Temporarily set the pointer to NULL, so idr_find() won't return
5028 * a half-baked cgroup.
5029 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03005030 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08005031 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04005032 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04005033 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08005034 }
5035
Paul Menagecc31edc2008-10-18 20:28:04 -07005036 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005037
Tejun Heo9d800df2014-05-14 09:15:00 -04005038 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04005039 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05005040 cgrp->level = level;
5041
5042 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
5043 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005044
Li Zefanb6abdb02008-03-04 14:28:19 -08005045 if (notify_on_release(parent))
5046 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
5047
Tejun Heo2260e7f2012-11-19 08:13:38 -08005048 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
5049 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005050
Tejun Heo0cb51d72014-05-16 13:22:49 -04005051 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09005052
Tejun Heo4e139af2012-11-19 08:13:36 -08005053 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04005054 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05005055 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05005056 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08005057
Tejun Heo0d802552013-12-06 15:11:56 -05005058 /*
5059 * @cgrp is now fully operational. If something fails after this
5060 * point, it'll be released via the normal destruction path.
5061 */
Tejun Heo6fa49182014-05-04 15:09:13 -04005062 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005063
Tejun Heo195e9b62016-03-03 09:57:58 -05005064 /* create the csses */
Tejun Heo5531dc92016-03-03 09:57:58 -05005065 do_each_subsys_mask(ss, ssid, cgroup_ss_mask(cgrp)) {
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005066 struct cgroup_subsys_state *css;
5067
5068 css = css_create(cgrp, ss);
5069 if (IS_ERR(css)) {
5070 ret = PTR_ERR(css);
Tejun Heo996cd1f2016-02-22 22:25:46 -05005071 goto out_destroy;
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005072 }
Tejun Heo996cd1f2016-02-22 22:25:46 -05005073 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07005074
Tejun Heobd53d612014-04-23 11:13:16 -04005075 /*
5076 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04005077 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04005078 */
Tejun Heo667c2492014-07-08 18:02:56 -04005079 if (!cgroup_on_dfl(cgrp)) {
Tejun Heo5531dc92016-03-03 09:57:58 -05005080 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo8699b772016-02-22 22:25:46 -05005081 cgroup_refresh_subtree_ss_mask(cgrp);
Tejun Heo667c2492014-07-08 18:02:56 -04005082 }
Tejun Heof392e512014-04-23 11:13:14 -04005083
Tejun Heoa5bca212016-03-03 09:57:58 -05005084 return cgrp;
5085
5086out_cancel_ref:
5087 percpu_ref_exit(&cgrp->self.refcnt);
5088out_free_cgrp:
5089 kfree(cgrp);
5090 return ERR_PTR(ret);
5091out_destroy:
5092 cgroup_destroy_locked(cgrp);
5093 return ERR_PTR(ret);
5094}
5095
5096static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
5097 umode_t mode)
5098{
5099 struct cgroup *parent, *cgrp;
5100 struct cgroup_subsys *ss;
5101 struct kernfs_node *kn;
5102 int ssid, ret;
5103
5104 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5105 if (strchr(name, '\n'))
5106 return -EINVAL;
5107
5108 parent = cgroup_kn_lock_live(parent_kn);
5109 if (!parent)
5110 return -ENODEV;
5111
5112 cgrp = cgroup_create(parent);
5113 if (IS_ERR(cgrp)) {
5114 ret = PTR_ERR(cgrp);
5115 goto out_unlock;
5116 }
5117
Tejun Heo195e9b62016-03-03 09:57:58 -05005118 /* create the directory */
5119 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5120 if (IS_ERR(kn)) {
5121 ret = PTR_ERR(kn);
5122 goto out_destroy;
5123 }
5124 cgrp->kn = kn;
5125
5126 /*
5127 * This extra ref will be put in cgroup_free_fn() and guarantees
5128 * that @cgrp->kn is always accessible.
5129 */
5130 kernfs_get(kn);
5131
5132 ret = cgroup_kn_set_ugid(kn);
5133 if (ret)
5134 goto out_destroy;
5135
5136 ret = css_populate_dir(&cgrp->self, NULL);
5137 if (ret)
5138 goto out_destroy;
5139
Tejun Heo5531dc92016-03-03 09:57:58 -05005140 do_each_subsys_mask(ss, ssid, cgroup_control(cgrp)) {
Tejun Heo195e9b62016-03-03 09:57:58 -05005141 ret = css_populate_dir(cgroup_css(cgrp, ss), NULL);
5142 if (ret)
5143 goto out_destroy;
5144 } while_each_subsys_mask();
5145
5146 /* let's create and online css's */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005147 kernfs_activate(kn);
5148
Tejun Heoba0f4d72014-05-13 12:19:22 -04005149 ret = 0;
5150 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005151
Tejun Heoa5bca212016-03-03 09:57:58 -05005152out_destroy:
5153 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005154out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005155 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005156 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005157}
5158
Tejun Heo223dbc32013-08-13 20:22:50 -04005159/*
5160 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005161 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5162 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005163 */
5164static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005165{
Tejun Heo223dbc32013-08-13 20:22:50 -04005166 struct cgroup_subsys_state *css =
5167 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005168
Tejun Heof20104d2013-08-13 20:22:50 -04005169 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005170
Tejun Heoaa226ff2016-01-21 15:31:11 -05005171 do {
5172 offline_css(css);
5173 css_put(css);
5174 /* @css can't go away while we're holding cgroup_mutex */
5175 css = css->parent;
5176 } while (css && atomic_dec_and_test(&css->online_cnt));
5177
5178 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005179}
5180
Tejun Heo223dbc32013-08-13 20:22:50 -04005181/* css kill confirmation processing requires process context, bounce */
5182static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005183{
5184 struct cgroup_subsys_state *css =
5185 container_of(ref, struct cgroup_subsys_state, refcnt);
5186
Tejun Heoaa226ff2016-01-21 15:31:11 -05005187 if (atomic_dec_and_test(&css->online_cnt)) {
5188 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5189 queue_work(cgroup_destroy_wq, &css->destroy_work);
5190 }
Tejun Heod3daf282013-06-13 19:39:16 -07005191}
5192
Tejun Heof392e512014-04-23 11:13:14 -04005193/**
5194 * kill_css - destroy a css
5195 * @css: css to destroy
5196 *
5197 * This function initiates destruction of @css by removing cgroup interface
5198 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005199 * asynchronously once css_tryget_online() is guaranteed to fail and when
5200 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005201 */
5202static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005203{
Tejun Heo01f64742014-05-13 12:19:23 -04005204 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005205
Tejun Heo2bd59d42014-02-11 11:52:49 -05005206 /*
5207 * This must happen before css is disassociated with its cgroup.
5208 * See seq_css() for details.
5209 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04005210 css_clear_dir(css, NULL);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005211
Tejun Heoedae0c32013-08-13 20:22:51 -04005212 /*
5213 * Killing would put the base ref, but we need to keep it alive
5214 * until after ->css_offline().
5215 */
5216 css_get(css);
5217
5218 /*
5219 * cgroup core guarantees that, by the time ->css_offline() is
5220 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005221 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005222 * proceed to offlining css's because percpu_ref_kill() doesn't
5223 * guarantee that the ref is seen as killed on all CPUs on return.
5224 *
5225 * Use percpu_ref_kill_and_confirm() to get notifications as each
5226 * css is confirmed to be seen as killed on all CPUs.
5227 */
5228 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005229}
5230
5231/**
5232 * cgroup_destroy_locked - the first stage of cgroup destruction
5233 * @cgrp: cgroup to be destroyed
5234 *
5235 * css's make use of percpu refcnts whose killing latency shouldn't be
5236 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005237 * guarantee that css_tryget_online() won't succeed by the time
5238 * ->css_offline() is invoked. To satisfy all the requirements,
5239 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005240 *
5241 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5242 * userland visible parts and start killing the percpu refcnts of
5243 * css's. Set up so that the next stage will be kicked off once all
5244 * the percpu refcnts are confirmed to be killed.
5245 *
5246 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5247 * rest of destruction. Once all cgroup references are gone, the
5248 * cgroup is RCU-freed.
5249 *
5250 * This function implements s1. After this step, @cgrp is gone as far as
5251 * the userland is concerned and a new cgroup with the same name may be
5252 * created. As cgroup doesn't care about the names internally, this
5253 * doesn't cause any problem.
5254 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005255static int cgroup_destroy_locked(struct cgroup *cgrp)
5256 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005257{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005258 struct cgroup_subsys_state *css;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005259 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005260
Tejun Heo42809dd2012-11-19 08:13:37 -08005261 lockdep_assert_held(&cgroup_mutex);
5262
Tejun Heo91486f62015-10-15 16:41:51 -04005263 /*
5264 * Only migration can raise populated from zero and we're already
5265 * holding cgroup_mutex.
5266 */
5267 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005268 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005269
Tejun Heo1a90dd52012-11-05 09:16:59 -08005270 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005271 * Make sure there's no live children. We can't test emptiness of
5272 * ->self.children as dead children linger on it while being
5273 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005274 */
Tejun Heof3d46502014-05-16 13:22:52 -04005275 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005276 return -EBUSY;
5277
5278 /*
Tejun Heo455050d2013-06-13 19:27:41 -07005279 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04005280 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07005281 */
Tejun Heo184faf32014-05-16 13:22:51 -04005282 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005283
Tejun Heo249f3462014-05-14 09:15:01 -04005284 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005285 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005286 kill_css(css);
5287
Tejun Heo455050d2013-06-13 19:27:41 -07005288 /*
Tejun Heo01f64742014-05-13 12:19:23 -04005289 * Remove @cgrp directory along with the base files. @cgrp has an
5290 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07005291 */
Tejun Heo01f64742014-05-13 12:19:23 -04005292 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005293
Tejun Heod51f39b2014-05-16 13:22:48 -04005294 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05005295
Tejun Heo249f3462014-05-14 09:15:01 -04005296 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005297 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005298
Tejun Heoea15f8c2013-06-13 19:27:42 -07005299 return 0;
5300};
5301
Tejun Heo2bd59d42014-02-11 11:52:49 -05005302static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005303{
Tejun Heoa9746d82014-05-13 12:19:22 -04005304 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005305 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005306
Tejun Heoa9746d82014-05-13 12:19:22 -04005307 cgrp = cgroup_kn_lock_live(kn);
5308 if (!cgrp)
5309 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005310
Tejun Heoa9746d82014-05-13 12:19:22 -04005311 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08005312
Tejun Heoa9746d82014-05-13 12:19:22 -04005313 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005314 return ret;
5315}
5316
Tejun Heo2bd59d42014-02-11 11:52:49 -05005317static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5318 .remount_fs = cgroup_remount,
5319 .show_options = cgroup_show_options,
5320 .mkdir = cgroup_mkdir,
5321 .rmdir = cgroup_rmdir,
5322 .rename = cgroup_rename,
5323};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005324
Tejun Heo15a4c832014-05-04 15:09:14 -04005325static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005326{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005327 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005328
Tejun Heoa5ae9892015-12-29 14:53:56 -05005329 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005330
Tejun Heo648bb562012-11-19 08:13:36 -08005331 mutex_lock(&cgroup_mutex);
5332
Tejun Heo15a4c832014-05-04 15:09:14 -04005333 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005334 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005335
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005336 /* Create the root cgroup state for this subsystem */
5337 ss->root = &cgrp_dfl_root;
5338 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005339 /* We don't handle early failures gracefully */
5340 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005341 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005342
5343 /*
5344 * Root csses are never destroyed and we can't initialize
5345 * percpu_ref during early init. Disable refcnting.
5346 */
5347 css->flags |= CSS_NO_REF;
5348
Tejun Heo15a4c832014-05-04 15:09:14 -04005349 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005350 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005351 css->id = 1;
5352 } else {
5353 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5354 BUG_ON(css->id < 0);
5355 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005356
Li Zefane8d55fd2008-04-29 01:00:13 -07005357 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005358 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005359 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005360 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005361 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005362
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005363 have_fork_callback |= (bool)ss->fork << ss->id;
5364 have_exit_callback |= (bool)ss->exit << ss->id;
Tejun Heoafcf6c82015-10-15 16:41:53 -04005365 have_free_callback |= (bool)ss->free << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005366 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005367
Li Zefane8d55fd2008-04-29 01:00:13 -07005368 /* At system boot, before all subsystems have been
5369 * registered, no tasks have been forked, so we don't
5370 * need to invoke fork callbacks here. */
5371 BUG_ON(!list_empty(&init_task.tasks));
5372
Tejun Heoae7f1642013-08-13 20:22:50 -04005373 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005374
Tejun Heo648bb562012-11-19 08:13:36 -08005375 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005376}
5377
5378/**
Li Zefana043e3b2008-02-23 15:24:09 -08005379 * cgroup_init_early - cgroup initialization at system boot
5380 *
5381 * Initialize cgroups at system boot, and initialize any
5382 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005383 */
5384int __init cgroup_init_early(void)
5385{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005386 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005387 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005388 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005389
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005390 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005391 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5392
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005393 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005394
Tejun Heo3ed80a62014-02-08 10:36:58 -05005395 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005396 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005397 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005398 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005399 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005400 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5401 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005402
Tejun Heoaec25022014-02-08 10:36:58 -05005403 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005404 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005405 if (!ss->legacy_name)
5406 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005407
5408 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005409 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005410 }
5411 return 0;
5412}
5413
Tejun Heo6e5c8302016-02-22 22:25:47 -05005414static u16 cgroup_disable_mask __initdata;
Tejun Heoa3e72732015-09-25 16:24:27 -04005415
Paul Menageddbcc7e2007-10-18 23:39:30 -07005416/**
Li Zefana043e3b2008-02-23 15:24:09 -08005417 * cgroup_init - cgroup initialization
5418 *
5419 * Register cgroup filesystem and /proc file, and initialize
5420 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005421 */
5422int __init cgroup_init(void)
5423{
Tejun Heo30159ec2013-06-25 11:53:37 -07005424 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005425 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005426
Tejun Heo6e5c8302016-02-22 22:25:47 -05005427 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heo1ed13282015-09-16 12:53:17 -04005428 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005429 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5430 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005431
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005432 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005433
Tejun Heo2378d8b2016-03-03 09:57:57 -05005434 /*
5435 * Add init_css_set to the hash table so that dfl_root can link to
5436 * it during init.
5437 */
5438 hash_add(css_set_table, &init_css_set.hlist,
5439 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005440
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005441 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005442
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005443 mutex_unlock(&cgroup_mutex);
5444
Tejun Heo172a2c062014-03-19 10:23:53 -04005445 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005446 if (ss->early_init) {
5447 struct cgroup_subsys_state *css =
5448 init_css_set.subsys[ss->id];
5449
5450 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5451 GFP_KERNEL);
5452 BUG_ON(css->id < 0);
5453 } else {
5454 cgroup_init_subsys(ss, false);
5455 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005456
Tejun Heo2d8f2432014-04-23 11:13:15 -04005457 list_add_tail(&init_css_set.e_cset_node[ssid],
5458 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005459
5460 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005461 * Setting dfl_root subsys_mask needs to consider the
5462 * disabled flag and cftype registration needs kmalloc,
5463 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005464 */
Tejun Heoa3e72732015-09-25 16:24:27 -04005465 if (cgroup_disable_mask & (1 << ssid)) {
5466 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5467 printk(KERN_INFO "Disabling %s control group subsystem\n",
5468 ss->name);
Tejun Heoa8ddc822014-07-15 11:05:10 -04005469 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005470 }
Tejun Heoa8ddc822014-07-15 11:05:10 -04005471
Johannes Weiner223ffb22016-02-11 13:34:49 -05005472 if (cgroup_ssid_no_v1(ssid))
5473 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5474 ss->name);
5475
Tejun Heoa8ddc822014-07-15 11:05:10 -04005476 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5477
Tejun Heo5de4fa12014-07-15 11:05:10 -04005478 if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005479 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005480
Tejun Heoa8ddc822014-07-15 11:05:10 -04005481 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5482 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5483 } else {
5484 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5485 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005486 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005487
5488 if (ss->bind)
5489 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005490 }
Greg KH676db4a2010-08-05 13:53:35 -07005491
Tejun Heo2378d8b2016-03-03 09:57:57 -05005492 /* init_css_set.subsys[] has been updated, re-hash */
5493 hash_del(&init_css_set.hlist);
5494 hash_add(css_set_table, &init_css_set.hlist,
5495 css_set_hash(init_css_set.subsys));
5496
Tejun Heo035f4f52015-10-15 17:00:43 -04005497 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5498 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005499 WARN_ON(register_filesystem(&cgroup2_fs_type));
Tejun Heo035f4f52015-10-15 17:00:43 -04005500 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
Paul Menagea4243162007-10-18 23:39:35 -07005501
Tejun Heo2bd59d42014-02-11 11:52:49 -05005502 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005503}
Paul Menageb4f48b62007-10-18 23:39:33 -07005504
Tejun Heoe5fca242013-11-22 17:14:39 -05005505static int __init cgroup_wq_init(void)
5506{
5507 /*
5508 * There isn't much point in executing destruction path in
5509 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005510 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005511 *
5512 * We would prefer to do this in cgroup_init() above, but that
5513 * is called before init_workqueues(): so leave this until after.
5514 */
Tejun Heo1a115332014-02-12 19:06:19 -05005515 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005516 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005517
5518 /*
5519 * Used to destroy pidlists and separate to serve as flush domain.
5520 * Cap @max_active to 1 too.
5521 */
5522 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5523 0, 1);
5524 BUG_ON(!cgroup_pidlist_destroy_wq);
5525
Tejun Heoe5fca242013-11-22 17:14:39 -05005526 return 0;
5527}
5528core_initcall(cgroup_wq_init);
5529
Paul Menagea4243162007-10-18 23:39:35 -07005530/*
5531 * proc_cgroup_show()
5532 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5533 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005534 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005535int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5536 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005537{
Tejun Heoe61734c2014-02-12 09:29:50 -05005538 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005539 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005540 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005541
5542 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005543 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005544 if (!buf)
5545 goto out;
5546
Paul Menagea4243162007-10-18 23:39:35 -07005547 mutex_lock(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005548 spin_lock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005549
Tejun Heo985ed672014-03-19 10:23:53 -04005550 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005551 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005552 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005553 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005554
Tejun Heoa7165262016-02-23 10:00:50 -05005555 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005556 continue;
5557
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005558 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005559 if (root != &cgrp_dfl_root)
5560 for_each_subsys(ss, ssid)
5561 if (root->subsys_mask & (1 << ssid))
5562 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005563 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005564 if (strlen(root->name))
5565 seq_printf(m, "%sname=%s", count ? "," : "",
5566 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005567 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04005568
Paul Menage7717f7b2009-09-23 15:56:22 -07005569 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005570
5571 /*
5572 * On traditional hierarchies, all zombie tasks show up as
5573 * belonging to the root cgroup. On the default hierarchy,
5574 * while a zombie doesn't show up in "cgroup.procs" and
5575 * thus can't be migrated, its /proc/PID/cgroup keeps
5576 * reporting the cgroup it belonged to before exiting. If
5577 * the cgroup is removed before the zombie is reaped,
5578 * " (deleted)" is appended to the cgroup path.
5579 */
5580 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5581 path = cgroup_path(cgrp, buf, PATH_MAX);
5582 if (!path) {
5583 retval = -ENAMETOOLONG;
5584 goto out_unlock;
5585 }
5586 } else {
5587 path = "/";
Tejun Heoe61734c2014-02-12 09:29:50 -05005588 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04005589
Tejun Heoe61734c2014-02-12 09:29:50 -05005590 seq_puts(m, path);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005591
5592 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5593 seq_puts(m, " (deleted)\n");
5594 else
5595 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07005596 }
5597
Zefan Li006f4ac2014-09-18 16:03:15 +08005598 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005599out_unlock:
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005600 spin_unlock_bh(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07005601 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005602 kfree(buf);
5603out:
5604 return retval;
5605}
5606
Paul Menagea4243162007-10-18 23:39:35 -07005607/* Display information about each subsystem and each hierarchy */
5608static int proc_cgroupstats_show(struct seq_file *m, void *v)
5609{
Tejun Heo30159ec2013-06-25 11:53:37 -07005610 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005611 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005612
Paul Menage8bab8dd2008-04-04 14:29:57 -07005613 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005614 /*
5615 * ideally we don't want subsystems moving around while we do this.
5616 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5617 * subsys/hierarchy state.
5618 */
Paul Menagea4243162007-10-18 23:39:35 -07005619 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005620
5621 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005622 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005623 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heofc5ed1e2015-09-18 11:56:28 -04005624 atomic_read(&ss->root->nr_cgrps),
5625 cgroup_ssid_enabled(i));
Tejun Heo30159ec2013-06-25 11:53:37 -07005626
Paul Menagea4243162007-10-18 23:39:35 -07005627 mutex_unlock(&cgroup_mutex);
5628 return 0;
5629}
5630
5631static int cgroupstats_open(struct inode *inode, struct file *file)
5632{
Al Viro9dce07f2008-03-29 03:07:28 +00005633 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005634}
5635
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005636static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005637 .open = cgroupstats_open,
5638 .read = seq_read,
5639 .llseek = seq_lseek,
5640 .release = single_release,
5641};
5642
Paul Menageb4f48b62007-10-18 23:39:33 -07005643/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005644 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005645 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005646 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005647 * A task is associated with the init_css_set until cgroup_post_fork()
5648 * attaches it to the parent's css_set. Empty cg_list indicates that
5649 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005650 */
5651void cgroup_fork(struct task_struct *child)
5652{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005653 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005654 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005655}
5656
5657/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005658 * cgroup_can_fork - called on a new task before the process is exposed
5659 * @child: the task in question.
5660 *
5661 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5662 * returns an error, the fork aborts with that error code. This allows for
5663 * a cgroup subsystem to conditionally allow or deny new forks.
5664 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005665int cgroup_can_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005666{
5667 struct cgroup_subsys *ss;
5668 int i, j, ret;
5669
Tejun Heob4e0eea2016-02-22 22:25:46 -05005670 do_each_subsys_mask(ss, i, have_canfork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005671 ret = ss->can_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005672 if (ret)
5673 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05005674 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10005675
5676 return 0;
5677
5678out_revert:
5679 for_each_subsys(ss, j) {
5680 if (j >= i)
5681 break;
5682 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005683 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005684 }
5685
5686 return ret;
5687}
5688
5689/**
5690 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5691 * @child: the task in question
5692 *
5693 * This calls the cancel_fork() callbacks if a fork failed *after*
5694 * cgroup_can_fork() succeded.
5695 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005696void cgroup_cancel_fork(struct task_struct *child)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005697{
5698 struct cgroup_subsys *ss;
5699 int i;
5700
5701 for_each_subsys(ss, i)
5702 if (ss->cancel_fork)
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005703 ss->cancel_fork(child);
Aleksa Sarai7e476822015-06-09 21:32:09 +10005704}
5705
5706/**
Li Zefana043e3b2008-02-23 15:24:09 -08005707 * cgroup_post_fork - called on a new task after adding it to the task list
5708 * @child: the task in question
5709 *
Tejun Heo5edee612012-10-16 15:03:14 -07005710 * Adds the task to the list running through its css_set if necessary and
5711 * call the subsystem fork() callbacks. Has to be after the task is
5712 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005713 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005714 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005715 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005716void cgroup_post_fork(struct task_struct *child)
Paul Menage817929e2007-10-18 23:39:36 -07005717{
Tejun Heo30159ec2013-06-25 11:53:37 -07005718 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005719 int i;
5720
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005721 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005722 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005723 * function sets use_task_css_set_links before grabbing
5724 * tasklist_lock and we just went through tasklist_lock to add
5725 * @child, it's guaranteed that either we see the set
5726 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5727 * @child during its iteration.
5728 *
5729 * If we won the race, @child is associated with %current's
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005730 * css_set. Grabbing css_set_lock guarantees both that the
Tejun Heoeaf797a2014-02-25 10:04:03 -05005731 * association is stable, and, on completion of the parent's
5732 * migration, @child is visible in the source of migration or
5733 * already in the destination cgroup. This guarantee is necessary
5734 * when implementing operations which need to migrate all tasks of
5735 * a cgroup to another.
5736 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005737 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005738 * will remain in init_css_set. This is safe because all tasks are
5739 * in the init_css_set before cg_links is enabled and there's no
5740 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005741 */
Paul Menage817929e2007-10-18 23:39:36 -07005742 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005743 struct css_set *cset;
5744
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005745 spin_lock_bh(&css_set_lock);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005746 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005747 if (list_empty(&child->cg_list)) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005748 get_css_set(cset);
Tejun Heof6d7d042015-10-15 16:41:52 -04005749 css_set_move_task(child, NULL, cset, false);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005750 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005751 spin_unlock_bh(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07005752 }
Tejun Heo5edee612012-10-16 15:03:14 -07005753
5754 /*
5755 * Call ss->fork(). This must happen after @child is linked on
5756 * css_set; otherwise, @child might change state between ->fork()
5757 * and addition to css_set.
5758 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005759 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05005760 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005761 } while_each_subsys_mask();
Paul Menage817929e2007-10-18 23:39:36 -07005762}
Tejun Heo5edee612012-10-16 15:03:14 -07005763
Paul Menage817929e2007-10-18 23:39:36 -07005764/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005765 * cgroup_exit - detach cgroup from exiting task
5766 * @tsk: pointer to task_struct of exiting process
5767 *
5768 * Description: Detach cgroup from @tsk and release it.
5769 *
5770 * Note that cgroups marked notify_on_release force every task in
5771 * them to take the global cgroup_mutex mutex when exiting.
5772 * This could impact scaling on very large systems. Be reluctant to
5773 * use notify_on_release cgroups where very high task exit scaling
5774 * is required on large systems.
5775 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005776 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5777 * call cgroup_exit() while the task is still competent to handle
5778 * notify_on_release(), then leave the task attached to the root cgroup in
5779 * each hierarchy for the remainder of its exit. No need to bother with
5780 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005781 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005782 */
Li Zefan1ec41832014-03-28 15:22:19 +08005783void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005784{
Tejun Heo30159ec2013-06-25 11:53:37 -07005785 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005786 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005787 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005788
5789 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005790 * Unlink from @tsk from its css_set. As migration path can't race
Tejun Heo0de09422015-10-15 16:41:49 -04005791 * with us, we can check css_set and cg_list without synchronization.
Paul Menage817929e2007-10-18 23:39:36 -07005792 */
Tejun Heo0de09422015-10-15 16:41:49 -04005793 cset = task_css_set(tsk);
5794
Paul Menage817929e2007-10-18 23:39:36 -07005795 if (!list_empty(&tsk->cg_list)) {
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005796 spin_lock_bh(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -04005797 css_set_move_task(tsk, cset, NULL, false);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04005798 spin_unlock_bh(&css_set_lock);
Tejun Heo2e91fa72015-10-15 16:41:53 -04005799 } else {
5800 get_css_set(cset);
Paul Menage817929e2007-10-18 23:39:36 -07005801 }
5802
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005803 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05005804 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04005805 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005806 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04005807}
Tejun Heo30159ec2013-06-25 11:53:37 -07005808
Tejun Heo2e91fa72015-10-15 16:41:53 -04005809void cgroup_free(struct task_struct *task)
5810{
5811 struct css_set *cset = task_css_set(task);
Tejun Heoafcf6c82015-10-15 16:41:53 -04005812 struct cgroup_subsys *ss;
5813 int ssid;
5814
Tejun Heob4e0eea2016-02-22 22:25:46 -05005815 do_each_subsys_mask(ss, ssid, have_free_callback) {
Tejun Heoafcf6c82015-10-15 16:41:53 -04005816 ss->free(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05005817 } while_each_subsys_mask();
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005818
Tejun Heo2e91fa72015-10-15 16:41:53 -04005819 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005820}
Paul Menage697f4162007-10-18 23:39:34 -07005821
Paul Menagebd89aab2007-10-18 23:40:44 -07005822static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005823{
Tejun Heo27bd4db2015-10-15 16:41:50 -04005824 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005825 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5826 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005827}
5828
Paul Menage81a6a5c2007-10-18 23:39:38 -07005829/*
5830 * Notify userspace when a cgroup is released, by running the
5831 * configured release agent with the name of the cgroup (path
5832 * relative to the root of cgroup file system) as the argument.
5833 *
5834 * Most likely, this user command will try to rmdir this cgroup.
5835 *
5836 * This races with the possibility that some other task will be
5837 * attached to this cgroup before it is removed, or that some other
5838 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5839 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5840 * unused, and this cgroup will be reprieved from its death sentence,
5841 * to continue to serve a useful existence. Next time it's released,
5842 * we will get notified again, if it still has 'notify_on_release' set.
5843 *
5844 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5845 * means only wait until the task is successfully execve()'d. The
5846 * separate release agent task is forked by call_usermodehelper(),
5847 * then control in this thread returns here, without waiting for the
5848 * release agent task. We don't bother to wait because the caller of
5849 * this routine has no use for the exit status of the release agent
5850 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005851 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005852static void cgroup_release_agent(struct work_struct *work)
5853{
Zefan Li971ff492014-09-18 16:06:19 +08005854 struct cgroup *cgrp =
5855 container_of(work, struct cgroup, release_agent_work);
5856 char *pathbuf = NULL, *agentbuf = NULL, *path;
5857 char *argv[3], *envp[3];
5858
Paul Menage81a6a5c2007-10-18 23:39:38 -07005859 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005860
Zefan Li971ff492014-09-18 16:06:19 +08005861 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5862 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5863 if (!pathbuf || !agentbuf)
5864 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005865
Zefan Li971ff492014-09-18 16:06:19 +08005866 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5867 if (!path)
5868 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005869
Zefan Li971ff492014-09-18 16:06:19 +08005870 argv[0] = agentbuf;
5871 argv[1] = path;
5872 argv[2] = NULL;
5873
5874 /* minimal command environment */
5875 envp[0] = "HOME=/";
5876 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5877 envp[2] = NULL;
5878
Paul Menage81a6a5c2007-10-18 23:39:38 -07005879 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005880 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005881 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005882out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005883 mutex_unlock(&cgroup_mutex);
5884out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005885 kfree(agentbuf);
5886 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005887}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005888
5889static int __init cgroup_disable(char *str)
5890{
Tejun Heo30159ec2013-06-25 11:53:37 -07005891 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005892 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005893 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005894
5895 while ((token = strsep(&str, ",")) != NULL) {
5896 if (!*token)
5897 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005898
Tejun Heo3ed80a62014-02-08 10:36:58 -05005899 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005900 if (strcmp(token, ss->name) &&
5901 strcmp(token, ss->legacy_name))
5902 continue;
Tejun Heoa3e72732015-09-25 16:24:27 -04005903 cgroup_disable_mask |= 1 << i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005904 }
5905 }
5906 return 1;
5907}
5908__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005909
Johannes Weiner223ffb22016-02-11 13:34:49 -05005910static int __init cgroup_no_v1(char *str)
5911{
5912 struct cgroup_subsys *ss;
5913 char *token;
5914 int i;
5915
5916 while ((token = strsep(&str, ",")) != NULL) {
5917 if (!*token)
5918 continue;
5919
5920 if (!strcmp(token, "all")) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05005921 cgroup_no_v1_mask = U16_MAX;
Johannes Weiner223ffb22016-02-11 13:34:49 -05005922 break;
5923 }
5924
5925 for_each_subsys(ss, i) {
5926 if (strcmp(token, ss->name) &&
5927 strcmp(token, ss->legacy_name))
5928 continue;
5929
5930 cgroup_no_v1_mask |= 1 << i;
5931 }
5932 }
5933 return 1;
5934}
5935__setup("cgroup_no_v1=", cgroup_no_v1);
5936
Tejun Heob77d7b62013-08-13 11:01:54 -04005937/**
Tejun Heoec903c02014-05-13 12:11:01 -04005938 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005939 * @dentry: directory dentry of interest
5940 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005941 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005942 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5943 * to get the corresponding css and return it. If such css doesn't exist
5944 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005945 */
Tejun Heoec903c02014-05-13 12:11:01 -04005946struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5947 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005948{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005949 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05005950 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005951 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005952 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005953
Tejun Heo35cf0832013-08-26 18:40:56 -04005954 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05005955 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5956 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005957 return ERR_PTR(-EBADF);
5958
Tejun Heo5a17f542014-02-11 11:52:47 -05005959 rcu_read_lock();
5960
Tejun Heo2bd59d42014-02-11 11:52:49 -05005961 /*
5962 * This path doesn't originate from kernfs and @kn could already
5963 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005964 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005965 */
5966 cgrp = rcu_dereference(kn->priv);
5967 if (cgrp)
5968 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005969
Tejun Heoec903c02014-05-13 12:11:01 -04005970 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005971 css = ERR_PTR(-ENOENT);
5972
5973 rcu_read_unlock();
5974 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005975}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005976
Li Zefan1cb650b2013-08-19 10:05:24 +08005977/**
5978 * css_from_id - lookup css by id
5979 * @id: the cgroup id
5980 * @ss: cgroup subsys to be looked into
5981 *
5982 * Returns the css if there's valid one with @id, otherwise returns NULL.
5983 * Should be called under rcu_read_lock().
5984 */
5985struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5986{
Tejun Heo6fa49182014-05-04 15:09:13 -04005987 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005988 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005989}
5990
Tejun Heo16af4392015-11-20 15:55:52 -05005991/**
5992 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5993 * @path: path on the default hierarchy
5994 *
5995 * Find the cgroup at @path on the default hierarchy, increment its
5996 * reference count and return it. Returns pointer to the found cgroup on
5997 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5998 * if @path points to a non-directory.
5999 */
6000struct cgroup *cgroup_get_from_path(const char *path)
6001{
6002 struct kernfs_node *kn;
6003 struct cgroup *cgrp;
6004
6005 mutex_lock(&cgroup_mutex);
6006
6007 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
6008 if (kn) {
6009 if (kernfs_type(kn) == KERNFS_DIR) {
6010 cgrp = kn->priv;
6011 cgroup_get(cgrp);
6012 } else {
6013 cgrp = ERR_PTR(-ENOTDIR);
6014 }
6015 kernfs_put(kn);
6016 } else {
6017 cgrp = ERR_PTR(-ENOENT);
6018 }
6019
6020 mutex_unlock(&cgroup_mutex);
6021 return cgrp;
6022}
6023EXPORT_SYMBOL_GPL(cgroup_get_from_path);
6024
Tejun Heobd1060a2015-12-07 17:38:53 -05006025/*
6026 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
6027 * definition in cgroup-defs.h.
6028 */
6029#ifdef CONFIG_SOCK_CGROUP_DATA
6030
6031#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
6032
Tejun Heo3fa4cc92015-12-14 11:24:06 -05006033DEFINE_SPINLOCK(cgroup_sk_update_lock);
Tejun Heobd1060a2015-12-07 17:38:53 -05006034static bool cgroup_sk_alloc_disabled __read_mostly;
6035
6036void cgroup_sk_alloc_disable(void)
6037{
6038 if (cgroup_sk_alloc_disabled)
6039 return;
6040 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
6041 cgroup_sk_alloc_disabled = true;
6042}
6043
6044#else
6045
6046#define cgroup_sk_alloc_disabled false
6047
6048#endif
6049
6050void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
6051{
6052 if (cgroup_sk_alloc_disabled)
6053 return;
6054
6055 rcu_read_lock();
6056
6057 while (true) {
6058 struct css_set *cset;
6059
6060 cset = task_css_set(current);
6061 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
6062 skcd->val = (unsigned long)cset->dfl_cgrp;
6063 break;
6064 }
6065 cpu_relax();
6066 }
6067
6068 rcu_read_unlock();
6069}
6070
6071void cgroup_sk_free(struct sock_cgroup_data *skcd)
6072{
6073 cgroup_put(sock_cgroup_ptr(skcd));
6074}
6075
6076#endif /* CONFIG_SOCK_CGROUP_DATA */
6077
Paul Menagefe693432009-09-23 15:56:20 -07006078#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04006079static struct cgroup_subsys_state *
6080debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07006081{
6082 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
6083
6084 if (!css)
6085 return ERR_PTR(-ENOMEM);
6086
6087 return css;
6088}
6089
Tejun Heoeb954192013-08-08 20:11:23 -04006090static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07006091{
Tejun Heoeb954192013-08-08 20:11:23 -04006092 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07006093}
6094
Tejun Heo182446d2013-08-08 20:11:24 -04006095static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
6096 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006097{
Tejun Heo182446d2013-08-08 20:11:24 -04006098 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07006099}
6100
Tejun Heo182446d2013-08-08 20:11:24 -04006101static u64 current_css_set_read(struct cgroup_subsys_state *css,
6102 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006103{
6104 return (u64)(unsigned long)current->cgroups;
6105}
6106
Tejun Heo182446d2013-08-08 20:11:24 -04006107static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08006108 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006109{
6110 u64 count;
6111
6112 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07006113 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07006114 rcu_read_unlock();
6115 return count;
6116}
6117
Tejun Heo2da8ca82013-12-05 12:28:04 -05006118static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006119{
Tejun Heo69d02062013-06-12 21:04:50 -07006120 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07006121 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05006122 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07006123
Tejun Heoe61734c2014-02-12 09:29:50 -05006124 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
6125 if (!name_buf)
6126 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07006127
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006128 spin_lock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006129 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07006130 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07006131 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07006132 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07006133
Tejun Heoa2dd4242014-03-19 10:23:55 -04006134 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07006135 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04006136 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006137 }
6138 rcu_read_unlock();
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006139 spin_unlock_bh(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05006140 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07006141 return 0;
6142}
6143
6144#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05006145static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07006146{
Tejun Heo2da8ca82013-12-05 12:28:04 -05006147 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07006148 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07006149
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006150 spin_lock_bh(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04006151 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07006152 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07006153 struct task_struct *task;
6154 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05006155
Tejun Heo5abb8852013-06-12 21:04:49 -07006156 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05006157
Tejun Heo5abb8852013-06-12 21:04:49 -07006158 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05006159 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6160 goto overflow;
6161 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07006162 }
Tejun Heoc7561122014-02-25 10:04:01 -05006163
6164 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
6165 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
6166 goto overflow;
6167 seq_printf(seq, " task %d\n", task_pid_vnr(task));
6168 }
6169 continue;
6170 overflow:
6171 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07006172 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04006173 spin_unlock_bh(&css_set_lock);
Paul Menage7717f7b2009-09-23 15:56:22 -07006174 return 0;
6175}
6176
Tejun Heo182446d2013-08-08 20:11:24 -04006177static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07006178{
Tejun Heo27bd4db2015-10-15 16:41:50 -04006179 return (!cgroup_is_populated(css->cgroup) &&
Zefan Lia25eb522014-09-19 16:51:00 +08006180 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07006181}
6182
6183static struct cftype debug_files[] = {
6184 {
Paul Menagefe693432009-09-23 15:56:20 -07006185 .name = "taskcount",
6186 .read_u64 = debug_taskcount_read,
6187 },
6188
6189 {
6190 .name = "current_css_set",
6191 .read_u64 = current_css_set_read,
6192 },
6193
6194 {
6195 .name = "current_css_set_refcount",
6196 .read_u64 = current_css_set_refcount_read,
6197 },
6198
6199 {
Paul Menage7717f7b2009-09-23 15:56:22 -07006200 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006201 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006202 },
6203
6204 {
6205 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05006206 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07006207 },
6208
6209 {
Paul Menagefe693432009-09-23 15:56:20 -07006210 .name = "releasable",
6211 .read_u64 = releasable_read,
6212 },
Paul Menagefe693432009-09-23 15:56:20 -07006213
Tejun Heo4baf6e32012-04-01 12:09:55 -07006214 { } /* terminate */
6215};
Paul Menagefe693432009-09-23 15:56:20 -07006216
Tejun Heo073219e2014-02-08 10:36:58 -05006217struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006218 .css_alloc = debug_css_alloc,
6219 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04006220 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07006221};
6222#endif /* CONFIG_CGROUP_DEBUG */