blob: 3f5f48130b6b8d7af8d9aaa65fd86900f80d0bb8 [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>
38#include <linux/mm.h>
39#include <linux/mutex.h>
40#include <linux/mount.h>
41#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070042#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/rcupdate.h>
44#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050047#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070048#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051#include <linux/delayacct.h>
52#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080053#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070054#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070055#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070056#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020057#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050058#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070059
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoe25e2cb2011-12-12 18:12:21 -080062/*
Tejun Heob1a21362013-11-29 10:42:58 -050063 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
67 */
68#define CGROUP_PIDLIST_DESTROY_DELAY HZ
69
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050070#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
71 MAX_CFTYPE_NAME + 2)
72
Tejun Heob1a21362013-11-29 10:42:58 -050073/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080074 * cgroup_mutex is the master lock. Any modification to cgroup or its
75 * hierarchy must be performed while holding it.
76 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050077 * css_set_rwsem protects task->cgroups pointer, the list of css_set
78 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080079 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050080 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
81 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082 */
Tejun Heo22194492013-04-07 09:29:51 -070083#ifdef CONFIG_PROVE_RCU
84DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050085DECLARE_RWSEM(css_set_rwsem);
86EXPORT_SYMBOL_GPL(cgroup_mutex);
87EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070088#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050090static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070091#endif
92
Tejun Heo69e943b2014-02-08 10:36:58 -050093/*
Tejun Heo15a4c832014-05-04 15:09:14 -040094 * Protects cgroup_idr and css_idr so that IDs can be released without
95 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040096 */
97static DEFINE_SPINLOCK(cgroup_idr_lock);
98
99/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500100 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
101 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
102 */
103static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700104
Tejun Heo8353da12014-05-13 12:19:23 -0400105#define cgroup_assert_mutex_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500106 rcu_lockdep_assert(rcu_read_lock_held() || \
107 lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400108 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500109
Ben Blumaae8aab2010-03-10 15:22:07 -0800110/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500111 * cgroup destruction makes heavy use of work items and there can be a lot
112 * of concurrent destructions. Use a separate workqueue so that cgroup
113 * destruction work items don't end up filling up max_active of system_wq
114 * which may lead to deadlock.
115 */
116static struct workqueue_struct *cgroup_destroy_wq;
117
118/*
Tejun Heob1a21362013-11-29 10:42:58 -0500119 * pidlist destructions need to be flushed on cgroup destruction. Use a
120 * separate workqueue as flush domain.
121 */
122static struct workqueue_struct *cgroup_pidlist_destroy_wq;
123
Tejun Heo3ed80a62014-02-08 10:36:58 -0500124/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500125#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500126static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700127#include <linux/cgroup_subsys.h>
128};
Tejun Heo073219e2014-02-08 10:36:58 -0500129#undef SUBSYS
130
131/* array of cgroup subsystem names */
132#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
133static const char *cgroup_subsys_name[] = {
134#include <linux/cgroup_subsys.h>
135};
136#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400139 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700140 * unattached - it never has more than a single cgroup, and all tasks are
141 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400143struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700144
Tejun Heoa2dd4242014-03-19 10:23:55 -0400145/*
146 * The default hierarchy always exists but is hidden until mounted for the
147 * first time. This is for backward compatibility.
148 */
149static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700150
151/* The list of hierarchy roots */
152
Tejun Heo9871bf92013-06-24 15:21:47 -0700153static LIST_HEAD(cgroup_roots);
154static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155
Tejun Heo3417ae12014-02-08 10:37:01 -0500156/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700157static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700158
Li Zefan794611a2013-06-18 18:53:53 +0800159/*
160 * Assign a monotonically increasing serial number to cgroups. It
161 * guarantees cgroups with bigger numbers are newer than those with smaller
162 * numbers. Also, as cgroups are always appended to the parent's
163 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700164 * the ascending serial number order on the list. Protected by
165 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800166 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700167static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800168
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800170 * check for fork/exit handlers to call. This avoids us having to do
171 * extra work in the fork/exit path if none of the subsystems need to
172 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700174static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700175
Tejun Heo628f7cd2013-06-28 16:24:11 -0700176static struct cftype cgroup_base_files[];
177
Tejun Heo59f52962014-02-11 11:52:49 -0500178static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400179static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400180 unsigned int ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400181static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800182static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400183static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
184static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400185static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
186 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500187static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800188
Tejun Heo6fa49182014-05-04 15:09:13 -0400189/* IDR wrappers which synchronize using cgroup_idr_lock */
190static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
191 gfp_t gfp_mask)
192{
193 int ret;
194
195 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400196 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400197 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400198 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400199 idr_preload_end();
200 return ret;
201}
202
203static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
204{
205 void *ret;
206
Tejun Heo54504e92014-05-13 12:10:59 -0400207 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400208 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400209 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400210 return ret;
211}
212
213static void cgroup_idr_remove(struct idr *idr, int id)
214{
Tejun Heo54504e92014-05-13 12:10:59 -0400215 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400216 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400217 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400218}
219
Tejun Heo95109b62013-08-08 20:11:27 -0400220/**
221 * cgroup_css - obtain a cgroup's css for the specified subsystem
222 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400223 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400224 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400225 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
226 * function must be called either under cgroup_mutex or rcu_read_lock() and
227 * the caller is responsible for pinning the returned css if it wants to
228 * keep accessing it outside the said locks. This function may return
229 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400230 */
231static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400232 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400233{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400234 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500235 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500236 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400237 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400238 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400239}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700240
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400241/**
242 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
243 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400244 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400245 *
246 * Similar to cgroup_css() but returns the effctive css, which is defined
247 * as the matching css of the nearest ancestor including self which has @ss
248 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
249 * function is guaranteed to return non-NULL css.
250 */
251static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
252 struct cgroup_subsys *ss)
253{
254 lockdep_assert_held(&cgroup_mutex);
255
256 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400257 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400258
259 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
260 return NULL;
261
262 while (cgrp->parent &&
263 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
264 cgrp = cgrp->parent;
265
266 return cgroup_css(cgrp, ss);
267}
268
Paul Menageddbcc7e2007-10-18 23:39:30 -0700269/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700270static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700271{
Tejun Heo54766d42013-06-12 21:04:53 -0700272 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273}
274
Tejun Heob4168642014-05-13 12:16:21 -0400275struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500276{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500277 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400278 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500279
280 /*
281 * This is open and unprotected implementation of cgroup_css().
282 * seq_css() is only called from a kernfs file operation which has
283 * an active reference on the file. Because all the subsystem
284 * files are drained before a css is disassociated with a cgroup,
285 * the matching css from the cgroup's subsys table is guaranteed to
286 * be and stay valid until the enclosing operation is complete.
287 */
288 if (cft->ss)
289 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
290 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400291 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500292}
Tejun Heob4168642014-05-13 12:16:21 -0400293EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500294
Li Zefan78574cf2013-04-08 19:00:38 -0700295/**
296 * cgroup_is_descendant - test ancestry
297 * @cgrp: the cgroup to be tested
298 * @ancestor: possible ancestor of @cgrp
299 *
300 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
301 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
302 * and @ancestor are accessible.
303 */
304bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
305{
306 while (cgrp) {
307 if (cgrp == ancestor)
308 return true;
309 cgrp = cgrp->parent;
310 }
311 return false;
312}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700313
Adrian Bunke9685a02008-02-07 00:13:46 -0800314static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700315{
316 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700317 (1 << CGRP_RELEASABLE) |
318 (1 << CGRP_NOTIFY_ON_RELEASE);
319 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700320}
321
Adrian Bunke9685a02008-02-07 00:13:46 -0800322static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700323{
Paul Menagebd89aab2007-10-18 23:40:44 -0700324 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700325}
326
Tejun Heo30159ec2013-06-25 11:53:37 -0700327/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500328 * for_each_css - iterate all css's of a cgroup
329 * @css: the iteration cursor
330 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
331 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700332 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400333 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700334 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500335#define for_each_css(css, ssid, cgrp) \
336 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
337 if (!((css) = rcu_dereference_check( \
338 (cgrp)->subsys[(ssid)], \
339 lockdep_is_held(&cgroup_mutex)))) { } \
340 else
341
342/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400343 * for_each_e_css - iterate all effective css's of a cgroup
344 * @css: the iteration cursor
345 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
346 * @cgrp: the target cgroup to iterate css's of
347 *
348 * Should be called under cgroup_[tree_]mutex.
349 */
350#define for_each_e_css(css, ssid, cgrp) \
351 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
352 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
353 ; \
354 else
355
356/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500357 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700358 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500359 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700360 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500361#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500362 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
363 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700364
Tejun Heo985ed672014-03-19 10:23:53 -0400365/* iterate across the hierarchies */
366#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700367 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700368
Tejun Heof8f22e52014-04-23 11:13:16 -0400369/* iterate over child cgrps, lock should be held throughout iteration */
370#define cgroup_for_each_live_child(child, cgrp) \
371 list_for_each_entry((child), &(cgrp)->children, sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400372 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400373 cgroup_is_dead(child); })) \
374 ; \
375 else
376
Paul Menage81a6a5c2007-10-18 23:39:38 -0700377/* the list of cgroups eligible for automatic release. Protected by
378 * release_list_lock */
379static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200380static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700381static void cgroup_release_agent(struct work_struct *work);
382static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700383static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700384
Tejun Heo69d02062013-06-12 21:04:50 -0700385/*
386 * A cgroup can be associated with multiple css_sets as different tasks may
387 * belong to different cgroups on different hierarchies. In the other
388 * direction, a css_set is naturally associated with multiple cgroups.
389 * This M:N relationship is represented by the following link structure
390 * which exists for each association and allows traversing the associations
391 * from both sides.
392 */
393struct cgrp_cset_link {
394 /* the cgroup and css_set this link associates */
395 struct cgroup *cgrp;
396 struct css_set *cset;
397
398 /* list of cgrp_cset_links anchored at cgrp->cset_links */
399 struct list_head cset_link;
400
401 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
402 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700403};
404
Tejun Heo172a2c062014-03-19 10:23:53 -0400405/*
406 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700407 * hierarchies being mounted. It contains a pointer to the root state
408 * for each subsystem. Also used to anchor the list of css_sets. Not
409 * reference-counted, to improve performance when child cgroups
410 * haven't been created.
411 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400412struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400413 .refcount = ATOMIC_INIT(1),
414 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
415 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
416 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
417 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
418 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
419};
Paul Menage817929e2007-10-18 23:39:36 -0700420
Tejun Heo172a2c062014-03-19 10:23:53 -0400421static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700422
Tejun Heo842b5972014-04-25 18:28:02 -0400423/**
424 * cgroup_update_populated - updated populated count of a cgroup
425 * @cgrp: the target cgroup
426 * @populated: inc or dec populated count
427 *
428 * @cgrp is either getting the first task (css_set) or losing the last.
429 * Update @cgrp->populated_cnt accordingly. The count is propagated
430 * towards root so that a given cgroup's populated_cnt is zero iff the
431 * cgroup and all its descendants are empty.
432 *
433 * @cgrp's interface file "cgroup.populated" is zero if
434 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
435 * changes from or to zero, userland is notified that the content of the
436 * interface file has changed. This can be used to detect when @cgrp and
437 * its descendants become populated or empty.
438 */
439static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
440{
441 lockdep_assert_held(&css_set_rwsem);
442
443 do {
444 bool trigger;
445
446 if (populated)
447 trigger = !cgrp->populated_cnt++;
448 else
449 trigger = !--cgrp->populated_cnt;
450
451 if (!trigger)
452 break;
453
454 if (cgrp->populated_kn)
455 kernfs_notify(cgrp->populated_kn);
456 cgrp = cgrp->parent;
457 } while (cgrp);
458}
459
Paul Menage7717f7b2009-09-23 15:56:22 -0700460/*
461 * hash table for cgroup groups. This improves the performance to find
462 * an existing css_set. This hash doesn't (currently) take into
463 * account cgroups in empty hierarchies.
464 */
Li Zefan472b1052008-04-29 01:00:11 -0700465#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800466static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700467
Li Zefan0ac801f2013-01-10 11:49:27 +0800468static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700469{
Li Zefan0ac801f2013-01-10 11:49:27 +0800470 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700471 struct cgroup_subsys *ss;
472 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700473
Tejun Heo30159ec2013-06-25 11:53:37 -0700474 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800475 key += (unsigned long)css[i];
476 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700477
Li Zefan0ac801f2013-01-10 11:49:27 +0800478 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700479}
480
Tejun Heo89c55092014-02-13 06:58:40 -0500481static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700482{
Tejun Heo69d02062013-06-12 21:04:50 -0700483 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400484 struct cgroup_subsys *ss;
485 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700486
Tejun Heo89c55092014-02-13 06:58:40 -0500487 lockdep_assert_held(&css_set_rwsem);
488
489 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700490 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700491
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700492 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400493 for_each_subsys(ss, ssid)
494 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700496 css_set_count--;
497
Tejun Heo69d02062013-06-12 21:04:50 -0700498 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700499 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700500
Tejun Heo69d02062013-06-12 21:04:50 -0700501 list_del(&link->cset_link);
502 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800503
Tejun Heo96d365e2014-02-13 06:58:40 -0500504 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400505 if (list_empty(&cgrp->cset_links)) {
506 cgroup_update_populated(cgrp, false);
507 if (notify_on_release(cgrp)) {
508 if (taskexit)
509 set_bit(CGRP_RELEASABLE, &cgrp->flags);
510 check_for_release(cgrp);
511 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700512 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700513
514 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700515 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700516
Tejun Heo5abb8852013-06-12 21:04:49 -0700517 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700518}
519
Tejun Heo89c55092014-02-13 06:58:40 -0500520static void put_css_set(struct css_set *cset, bool taskexit)
521{
522 /*
523 * Ensure that the refcount doesn't hit zero while any readers
524 * can see it. Similar to atomic_dec_and_lock(), but for an
525 * rwlock
526 */
527 if (atomic_add_unless(&cset->refcount, -1, 1))
528 return;
529
530 down_write(&css_set_rwsem);
531 put_css_set_locked(cset, taskexit);
532 up_write(&css_set_rwsem);
533}
534
Paul Menage817929e2007-10-18 23:39:36 -0700535/*
536 * refcounted get/put for css_set objects
537 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700538static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700539{
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700541}
542
Tejun Heob326f9d2013-06-24 15:21:48 -0700543/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700544 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 * @cset: candidate css_set being tested
546 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700547 * @new_cgrp: cgroup that's being entered by the task
548 * @template: desired set of css pointers in css_set (pre-calculated)
549 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800550 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700551 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
552 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700553static bool compare_css_sets(struct css_set *cset,
554 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700555 struct cgroup *new_cgrp,
556 struct cgroup_subsys_state *template[])
557{
558 struct list_head *l1, *l2;
559
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400560 /*
561 * On the default hierarchy, there can be csets which are
562 * associated with the same set of cgroups but different csses.
563 * Let's first ensure that csses match.
564 */
565 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700566 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700567
568 /*
569 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400570 * different cgroups in hierarchies. As different cgroups may
571 * share the same effective css, this comparison is always
572 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700573 */
Tejun Heo69d02062013-06-12 21:04:50 -0700574 l1 = &cset->cgrp_links;
575 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700576 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700577 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700578 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700579
580 l1 = l1->next;
581 l2 = l2->next;
582 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700583 if (l1 == &cset->cgrp_links) {
584 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 break;
586 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700587 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700588 }
589 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700590 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
591 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
592 cgrp1 = link1->cgrp;
593 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700594 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700595 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700596
597 /*
598 * If this hierarchy is the hierarchy of the cgroup
599 * that's changing, then we need to check that this
600 * css_set points to the new cgroup; if it's any other
601 * hierarchy, then this css_set should point to the
602 * same cgroup as the old css_set.
603 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700604 if (cgrp1->root == new_cgrp->root) {
605 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700606 return false;
607 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700608 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700609 return false;
610 }
611 }
612 return true;
613}
614
Tejun Heob326f9d2013-06-24 15:21:48 -0700615/**
616 * find_existing_css_set - init css array and find the matching css_set
617 * @old_cset: the css_set that we're using before the cgroup transition
618 * @cgrp: the cgroup that we're moving into
619 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700620 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700621static struct css_set *find_existing_css_set(struct css_set *old_cset,
622 struct cgroup *cgrp,
623 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700624{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400625 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700626 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700627 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800628 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700629 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700630
Ben Blumaae8aab2010-03-10 15:22:07 -0800631 /*
632 * Build the set of subsystem state objects that we want to see in the
633 * new css_set. while subsystems can change globally, the entries here
634 * won't change, so no need for locking.
635 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700636 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400637 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400638 /*
639 * @ss is in this hierarchy, so we want the
640 * effective css from @cgrp.
641 */
642 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700643 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400644 /*
645 * @ss is not in this hierarchy, so we don't want
646 * to change the css.
647 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700648 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700649 }
650 }
651
Li Zefan0ac801f2013-01-10 11:49:27 +0800652 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 hash_for_each_possible(css_set_table, cset, hlist, key) {
654 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700655 continue;
656
657 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700659 }
Paul Menage817929e2007-10-18 23:39:36 -0700660
661 /* No existing cgroup group matched */
662 return NULL;
663}
664
Tejun Heo69d02062013-06-12 21:04:50 -0700665static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700666{
Tejun Heo69d02062013-06-12 21:04:50 -0700667 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700668
Tejun Heo69d02062013-06-12 21:04:50 -0700669 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
670 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700671 kfree(link);
672 }
673}
674
Tejun Heo69d02062013-06-12 21:04:50 -0700675/**
676 * allocate_cgrp_cset_links - allocate cgrp_cset_links
677 * @count: the number of links to allocate
678 * @tmp_links: list_head the allocated links are put on
679 *
680 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
681 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700682 */
Tejun Heo69d02062013-06-12 21:04:50 -0700683static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700684{
Tejun Heo69d02062013-06-12 21:04:50 -0700685 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700686 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700687
688 INIT_LIST_HEAD(tmp_links);
689
Li Zefan36553432008-07-29 22:33:19 -0700690 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700691 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700692 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700693 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700694 return -ENOMEM;
695 }
Tejun Heo69d02062013-06-12 21:04:50 -0700696 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700697 }
698 return 0;
699}
700
Li Zefanc12f65d2009-01-07 18:07:42 -0800701/**
702 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700703 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800705 * @cgrp: the destination cgroup
706 */
Tejun Heo69d02062013-06-12 21:04:50 -0700707static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
708 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800709{
Tejun Heo69d02062013-06-12 21:04:50 -0700710 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800711
Tejun Heo69d02062013-06-12 21:04:50 -0700712 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400713
714 if (cgroup_on_dfl(cgrp))
715 cset->dfl_cgrp = cgrp;
716
Tejun Heo69d02062013-06-12 21:04:50 -0700717 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
718 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700719 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400720
721 if (list_empty(&cgrp->cset_links))
722 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700723 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400724
Paul Menage7717f7b2009-09-23 15:56:22 -0700725 /*
726 * Always add links to the tail of the list so that the list
727 * is sorted by order of hierarchy creation
728 */
Tejun Heo69d02062013-06-12 21:04:50 -0700729 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800730}
731
Tejun Heob326f9d2013-06-24 15:21:48 -0700732/**
733 * find_css_set - return a new css_set with one cgroup updated
734 * @old_cset: the baseline css_set
735 * @cgrp: the cgroup to be updated
736 *
737 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
738 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700739 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700740static struct css_set *find_css_set(struct css_set *old_cset,
741 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700742{
Tejun Heob326f9d2013-06-24 15:21:48 -0700743 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700744 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700745 struct list_head tmp_links;
746 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400747 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800748 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400749 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700750
Tejun Heob326f9d2013-06-24 15:21:48 -0700751 lockdep_assert_held(&cgroup_mutex);
752
Paul Menage817929e2007-10-18 23:39:36 -0700753 /* First see if we already have a cgroup group that matches
754 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500755 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700756 cset = find_existing_css_set(old_cset, cgrp, template);
757 if (cset)
758 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500759 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700760
Tejun Heo5abb8852013-06-12 21:04:49 -0700761 if (cset)
762 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700763
Tejun Heof4f4be22013-06-12 21:04:51 -0700764 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700765 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700766 return NULL;
767
Tejun Heo69d02062013-06-12 21:04:50 -0700768 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700769 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700770 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700771 return NULL;
772 }
773
Tejun Heo5abb8852013-06-12 21:04:49 -0700774 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700775 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700776 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500777 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500778 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500779 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700780 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700781
782 /* Copy the set of subsystem state objects generated in
783 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700784 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700785
Tejun Heo96d365e2014-02-13 06:58:40 -0500786 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700787 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700788 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700789 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700790
Paul Menage7717f7b2009-09-23 15:56:22 -0700791 if (c->root == cgrp->root)
792 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700793 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700794 }
Paul Menage817929e2007-10-18 23:39:36 -0700795
Tejun Heo69d02062013-06-12 21:04:50 -0700796 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700797
Paul Menage817929e2007-10-18 23:39:36 -0700798 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700799
Tejun Heo2d8f2432014-04-23 11:13:15 -0400800 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700801 key = css_set_hash(cset->subsys);
802 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700803
Tejun Heo2d8f2432014-04-23 11:13:15 -0400804 for_each_subsys(ss, ssid)
805 list_add_tail(&cset->e_cset_node[ssid],
806 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
807
Tejun Heo96d365e2014-02-13 06:58:40 -0500808 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700809
Tejun Heo5abb8852013-06-12 21:04:49 -0700810 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700811}
812
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400813static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700814{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400815 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500816
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400817 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500818}
819
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400820static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500821{
822 int id;
823
824 lockdep_assert_held(&cgroup_mutex);
825
Tejun Heo985ed672014-03-19 10:23:53 -0400826 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500827 if (id < 0)
828 return id;
829
830 root->hierarchy_id = id;
831 return 0;
832}
833
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400834static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500835{
836 lockdep_assert_held(&cgroup_mutex);
837
838 if (root->hierarchy_id) {
839 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
840 root->hierarchy_id = 0;
841 }
842}
843
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400844static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500845{
846 if (root) {
847 /* hierarhcy ID shoulid already have been released */
848 WARN_ON_ONCE(root->hierarchy_id);
849
850 idr_destroy(&root->cgroup_idr);
851 kfree(root);
852 }
853}
854
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400855static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500856{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400857 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500858 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500859
Tejun Heo2bd59d42014-02-11 11:52:49 -0500860 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500861
Tejun Heo776f02f2014-02-12 09:29:50 -0500862 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500863 BUG_ON(!list_empty(&cgrp->children));
864
Tejun Heof2e85d52014-02-11 11:52:49 -0500865 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400866 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500867
868 /*
869 * Release all the links from cset_links to this hierarchy's
870 * root cgroup
871 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500872 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500873
874 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
875 list_del(&link->cset_link);
876 list_del(&link->cgrp_link);
877 kfree(link);
878 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500879 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500880
881 if (!list_empty(&root->root_list)) {
882 list_del(&root->root_list);
883 cgroup_root_count--;
884 }
885
886 cgroup_exit_root_id(root);
887
888 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500889
Tejun Heo2bd59d42014-02-11 11:52:49 -0500890 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500891 cgroup_free_root(root);
892}
893
Tejun Heoceb6a082014-02-25 10:04:02 -0500894/* look up cgroup associated with given css_set on the specified hierarchy */
895static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400896 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700897{
Paul Menage7717f7b2009-09-23 15:56:22 -0700898 struct cgroup *res = NULL;
899
Tejun Heo96d365e2014-02-13 06:58:40 -0500900 lockdep_assert_held(&cgroup_mutex);
901 lockdep_assert_held(&css_set_rwsem);
902
Tejun Heo5abb8852013-06-12 21:04:49 -0700903 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400904 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700905 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700906 struct cgrp_cset_link *link;
907
908 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700909 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700910
Paul Menage7717f7b2009-09-23 15:56:22 -0700911 if (c->root == root) {
912 res = c;
913 break;
914 }
915 }
916 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500917
Paul Menage7717f7b2009-09-23 15:56:22 -0700918 BUG_ON(!res);
919 return res;
920}
921
922/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500923 * Return the cgroup for "task" from the given hierarchy. Must be
924 * called with cgroup_mutex and css_set_rwsem held.
925 */
926static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400927 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500928{
929 /*
930 * No need to lock the task - since we hold cgroup_mutex the
931 * task can't change groups, so the only thing that can happen
932 * is that it exits and its css is set back to init_css_set.
933 */
934 return cset_cgroup_from_root(task_css_set(task), root);
935}
936
937/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938 * A task must hold cgroup_mutex to modify cgroups.
939 *
940 * Any task can increment and decrement the count field without lock.
941 * So in general, code holding cgroup_mutex can't rely on the count
942 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800943 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700944 * means that no tasks are currently attached, therefore there is no
945 * way a task attached to that cgroup can fork (the other way to
946 * increment the count). So code holding cgroup_mutex can safely
947 * assume that if the count is zero, it will stay zero. Similarly, if
948 * a task holds cgroup_mutex on a cgroup with zero count, it
949 * knows that the cgroup won't be removed, as cgroup_rmdir()
950 * needs that mutex.
951 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
953 * (usually) take cgroup_mutex. These are the two most performance
954 * critical pieces of code here. The exception occurs on cgroup_exit(),
955 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
956 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800957 * to the release agent with the name of the cgroup (path relative to
958 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 *
960 * A cgroup can only be deleted if both its 'count' of using tasks
961 * is zero, and its list of 'children' cgroups is empty. Since all
962 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400963 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400965 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966 *
967 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800968 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969 */
970
Tejun Heo69dfa002014-05-04 15:09:13 -0400971static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500972static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700973static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700974
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500975static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
976 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500978 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
979 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
980 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
981 cft->ss->name, cft->name);
982 else
983 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
984 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985}
986
Tejun Heof2e85d52014-02-11 11:52:49 -0500987/**
988 * cgroup_file_mode - deduce file mode of a control file
989 * @cft: the control file in question
990 *
991 * returns cft->mode if ->mode is not 0
992 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
993 * returns S_IRUGO if it has only a read handler
994 * returns S_IWUSR if it has only a write hander
995 */
996static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800997{
Tejun Heof2e85d52014-02-11 11:52:49 -0500998 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800999
Tejun Heof2e85d52014-02-11 11:52:49 -05001000 if (cft->mode)
1001 return cft->mode;
1002
1003 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1004 mode |= S_IRUGO;
1005
Tejun Heo6770c642014-05-13 12:16:21 -04001006 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001007 mode |= S_IWUSR;
1008
1009 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001010}
1011
Li Zefanbe445622013-01-24 14:31:42 +08001012static void cgroup_free_fn(struct work_struct *work)
1013{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001014 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001015
Tejun Heo3c9c8252014-02-12 09:29:50 -05001016 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001017 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001018
Tejun Heo776f02f2014-02-12 09:29:50 -05001019 if (cgrp->parent) {
1020 /*
1021 * We get a ref to the parent, and put the ref when this
1022 * cgroup is being freed, so it's guaranteed that the
1023 * parent won't be destroyed before its children.
1024 */
1025 cgroup_put(cgrp->parent);
1026 kernfs_put(cgrp->kn);
1027 kfree(cgrp);
1028 } else {
1029 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001030 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001031 * indicates that the root should be released.
1032 */
1033 cgroup_destroy_root(cgrp->root);
1034 }
Li Zefanbe445622013-01-24 14:31:42 +08001035}
1036
1037static void cgroup_free_rcu(struct rcu_head *head)
1038{
1039 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1040
Tejun Heoea15f8c2013-06-13 19:27:42 -07001041 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001042 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001043}
1044
Tejun Heo59f52962014-02-11 11:52:49 -05001045static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001047 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1048 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1049 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050}
1051
Tejun Heo59f52962014-02-11 11:52:49 -05001052static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001054 if (!atomic_dec_and_test(&cgrp->refcnt))
1055 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001056 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001057 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058
Tejun Heo6fa49182014-05-04 15:09:13 -04001059 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001060 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061
Tejun Heo2bd59d42014-02-11 11:52:49 -05001062 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063}
1064
Tejun Heoa9746d82014-05-13 12:19:22 -04001065/**
1066 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1067 * @kn: the kernfs_node being serviced
1068 *
1069 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1070 * the method finishes if locking succeeded. Note that once this function
1071 * returns the cgroup returned by cgroup_kn_lock_live() may become
1072 * inaccessible any time. If the caller intends to continue to access the
1073 * cgroup, it should pin it before invoking this function.
1074 */
1075static void cgroup_kn_unlock(struct kernfs_node *kn)
1076{
1077 struct cgroup *cgrp;
1078
1079 if (kernfs_type(kn) == KERNFS_DIR)
1080 cgrp = kn->priv;
1081 else
1082 cgrp = kn->parent->priv;
1083
1084 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001085
1086 kernfs_unbreak_active_protection(kn);
1087 cgroup_put(cgrp);
1088}
1089
1090/**
1091 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1092 * @kn: the kernfs_node being serviced
1093 *
1094 * This helper is to be used by a cgroup kernfs method currently servicing
1095 * @kn. It breaks the active protection, performs cgroup locking and
1096 * verifies that the associated cgroup is alive. Returns the cgroup if
1097 * alive; otherwise, %NULL. A successful return should be undone by a
1098 * matching cgroup_kn_unlock() invocation.
1099 *
1100 * Any cgroup kernfs method implementation which requires locking the
1101 * associated cgroup should use this helper. It avoids nesting cgroup
1102 * locking under kernfs active protection and allows all kernfs operations
1103 * including self-removal.
1104 */
1105static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1106{
1107 struct cgroup *cgrp;
1108
1109 if (kernfs_type(kn) == KERNFS_DIR)
1110 cgrp = kn->priv;
1111 else
1112 cgrp = kn->parent->priv;
1113
1114 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001115 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001116 * active_ref. cgroup liveliness check alone provides enough
1117 * protection against removal. Ensure @cgrp stays accessible and
1118 * break the active_ref protection.
1119 */
1120 cgroup_get(cgrp);
1121 kernfs_break_active_protection(kn);
1122
Tejun Heoa9746d82014-05-13 12:19:22 -04001123 mutex_lock(&cgroup_mutex);
1124
1125 if (!cgroup_is_dead(cgrp))
1126 return cgrp;
1127
1128 cgroup_kn_unlock(kn);
1129 return NULL;
1130}
1131
Li Zefan2739d3c2013-01-21 18:18:33 +08001132static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001134 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135
Tejun Heo01f64742014-05-13 12:19:23 -04001136 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001137 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001138}
1139
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001140/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001141 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001142 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001143 * @subsys_mask: mask of the subsystem ids whose files should be removed
1144 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001145static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001146{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001147 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001148 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001149
Tejun Heob420ba72013-07-12 12:34:02 -07001150 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001151 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001152
Tejun Heo69dfa002014-05-04 15:09:13 -04001153 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001154 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001155 list_for_each_entry(cfts, &ss->cfts, node)
1156 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001157 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158}
1159
Tejun Heo69dfa002014-05-04 15:09:13 -04001160static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161{
Tejun Heo30159ec2013-06-25 11:53:37 -07001162 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001163 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164
Tejun Heoace2bee2014-02-11 11:52:47 -05001165 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001166
Tejun Heo5df36032014-03-19 10:23:54 -04001167 for_each_subsys(ss, ssid) {
1168 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001169 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001170
Tejun Heo7fd8c562014-04-23 11:13:16 -04001171 /* if @ss has non-root csses attached to it, can't move */
1172 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001173 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001174
Tejun Heo5df36032014-03-19 10:23:54 -04001175 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001176 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001177 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001178 }
1179
Tejun Heoa2dd4242014-03-19 10:23:55 -04001180 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1181 if (ret) {
1182 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001183 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184
Tejun Heoa2dd4242014-03-19 10:23:55 -04001185 /*
1186 * Rebinding back to the default root is not allowed to
1187 * fail. Using both default and non-default roots should
1188 * be rare. Moving subsystems back and forth even more so.
1189 * Just warn about it and continue.
1190 */
1191 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001192 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001193 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001194 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001195 }
Tejun Heo5df36032014-03-19 10:23:54 -04001196 }
Tejun Heo31261212013-06-28 17:07:30 -07001197
1198 /*
1199 * Nothing can fail from this point on. Remove files for the
1200 * removed subsystems and rebind each subsystem.
1201 */
Tejun Heo5df36032014-03-19 10:23:54 -04001202 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001203 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001204 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001205
Tejun Heo5df36032014-03-19 10:23:54 -04001206 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001207 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001208 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001209 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001210
Tejun Heo5df36032014-03-19 10:23:54 -04001211 if (!(ss_mask & (1 << ssid)))
1212 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001213
Tejun Heo5df36032014-03-19 10:23:54 -04001214 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001215 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001216
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001217 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001218
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001219 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1220 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001221 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001222 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001223
Tejun Heo2d8f2432014-04-23 11:13:15 -04001224 down_write(&css_set_rwsem);
1225 hash_for_each(css_set_table, i, cset, hlist)
1226 list_move_tail(&cset->e_cset_node[ss->id],
1227 &dst_root->cgrp.e_csets[ss->id]);
1228 up_write(&css_set_rwsem);
1229
Tejun Heof392e512014-04-23 11:13:14 -04001230 src_root->subsys_mask &= ~(1 << ssid);
1231 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1232
Tejun Heobd53d612014-04-23 11:13:16 -04001233 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001234 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001235 if (dst_root != &cgrp_dfl_root)
1236 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001237
Tejun Heo5df36032014-03-19 10:23:54 -04001238 if (ss->bind)
1239 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001240 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241
Tejun Heoa2dd4242014-03-19 10:23:55 -04001242 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 return 0;
1244}
1245
Tejun Heo2bd59d42014-02-11 11:52:49 -05001246static int cgroup_show_options(struct seq_file *seq,
1247 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001248{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001249 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001250 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001251 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252
Tejun Heob85d2042013-12-06 15:11:57 -05001253 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001254 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001255 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001256 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1257 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001258 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001260 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001261 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001262
1263 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001264 if (strlen(root->release_agent_path))
1265 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001266 spin_unlock(&release_agent_path_lock);
1267
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001268 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001269 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001270 if (strlen(root->name))
1271 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272 return 0;
1273}
1274
1275struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001276 unsigned int subsys_mask;
1277 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001278 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001279 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001280 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001281 /* User explicitly requested empty subsystem */
1282 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283};
1284
Ben Blumcf5d5942010-03-10 15:22:09 -08001285static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001287 char *token, *o = data;
1288 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001289 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001290 struct cgroup_subsys *ss;
1291 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001292
1293#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001294 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001295#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296
Paul Menagec6d57f32009-09-23 15:56:19 -07001297 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298
1299 while ((token = strsep(&o, ",")) != NULL) {
1300 if (!*token)
1301 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001302 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001303 /* Explicitly have no subsystems */
1304 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001305 continue;
1306 }
1307 if (!strcmp(token, "all")) {
1308 /* Mutually exclusive option 'all' + subsystem name */
1309 if (one_ss)
1310 return -EINVAL;
1311 all_ss = true;
1312 continue;
1313 }
Tejun Heo873fe092013-04-14 20:15:26 -07001314 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1315 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1316 continue;
1317 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001318 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001319 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001320 continue;
1321 }
1322 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001323 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001324 continue;
1325 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001326 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001327 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001328 continue;
1329 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001330 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001331 /* Specifying two release agents is forbidden */
1332 if (opts->release_agent)
1333 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001334 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001335 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001336 if (!opts->release_agent)
1337 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001338 continue;
1339 }
1340 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001341 const char *name = token + 5;
1342 /* Can't specify an empty name */
1343 if (!strlen(name))
1344 return -EINVAL;
1345 /* Must match [\w.-]+ */
1346 for (i = 0; i < strlen(name); i++) {
1347 char c = name[i];
1348 if (isalnum(c))
1349 continue;
1350 if ((c == '.') || (c == '-') || (c == '_'))
1351 continue;
1352 return -EINVAL;
1353 }
1354 /* Specifying two names is forbidden */
1355 if (opts->name)
1356 return -EINVAL;
1357 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001358 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001359 GFP_KERNEL);
1360 if (!opts->name)
1361 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001362
1363 continue;
1364 }
1365
Tejun Heo30159ec2013-06-25 11:53:37 -07001366 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001367 if (strcmp(token, ss->name))
1368 continue;
1369 if (ss->disabled)
1370 continue;
1371
1372 /* Mutually exclusive option 'all' + subsystem name */
1373 if (all_ss)
1374 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001375 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001376 one_ss = true;
1377
1378 break;
1379 }
1380 if (i == CGROUP_SUBSYS_COUNT)
1381 return -ENOENT;
1382 }
1383
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001384 /* Consistency checks */
1385
Tejun Heo873fe092013-04-14 20:15:26 -07001386 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001387 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001388
Tejun Heod3ba07c2014-02-13 06:58:38 -05001389 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1390 opts->cpuset_clone_children || opts->release_agent ||
1391 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001392 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001393 return -EINVAL;
1394 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001395 } else {
1396 /*
1397 * If the 'all' option was specified select all the
1398 * subsystems, otherwise if 'none', 'name=' and a subsystem
1399 * name options were not specified, let's default to 'all'
1400 */
1401 if (all_ss || (!one_ss && !opts->none && !opts->name))
1402 for_each_subsys(ss, i)
1403 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001404 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001405
Tejun Heoa2dd4242014-03-19 10:23:55 -04001406 /*
1407 * We either have to specify by name or by subsystems. (So
1408 * all empty hierarchies must have a name).
1409 */
1410 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001411 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001412 }
1413
Li Zefanf9ab5b52009-06-17 16:26:33 -07001414 /*
1415 * Option noprefix was introduced just for backward compatibility
1416 * with the old cpuset, so we allow noprefix only if mounting just
1417 * the cpuset subsystem.
1418 */
Tejun Heo93438622013-04-14 20:15:25 -07001419 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001420 return -EINVAL;
1421
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001422
1423 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001424 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001425 return -EINVAL;
1426
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427 return 0;
1428}
1429
Tejun Heo2bd59d42014-02-11 11:52:49 -05001430static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431{
1432 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001433 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001435 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436
Tejun Heo873fe092013-04-14 20:15:26 -07001437 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001438 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001439 return -EINVAL;
1440 }
1441
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 mutex_lock(&cgroup_mutex);
1443
1444 /* See what subsystems are wanted */
1445 ret = parse_cgroupfs_options(data, &opts);
1446 if (ret)
1447 goto out_unlock;
1448
Tejun Heof392e512014-04-23 11:13:14 -04001449 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001450 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001451 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001452
Tejun Heof392e512014-04-23 11:13:14 -04001453 added_mask = opts.subsys_mask & ~root->subsys_mask;
1454 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001455
Ben Blumcf5d5942010-03-10 15:22:09 -08001456 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001457 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001458 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001459 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001460 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1461 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 goto out_unlock;
1464 }
1465
Tejun Heof172e672013-06-28 17:07:30 -07001466 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001467 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001468 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001469 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001470 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471
Tejun Heo5df36032014-03-19 10:23:54 -04001472 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001473 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001476 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001477
Tejun Heo69e943b2014-02-08 10:36:58 -05001478 if (opts.release_agent) {
1479 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001480 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001481 spin_unlock(&release_agent_path_lock);
1482 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001484 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001485 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 return ret;
1488}
1489
Tejun Heoafeb0f92014-02-13 06:58:39 -05001490/*
1491 * To reduce the fork() overhead for systems that are not actually using
1492 * their cgroups capability, we don't maintain the lists running through
1493 * each css_set to its tasks until we see the list actually used - in other
1494 * words after the first mount.
1495 */
1496static bool use_task_css_set_links __read_mostly;
1497
1498static void cgroup_enable_task_cg_lists(void)
1499{
1500 struct task_struct *p, *g;
1501
Tejun Heo96d365e2014-02-13 06:58:40 -05001502 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001503
1504 if (use_task_css_set_links)
1505 goto out_unlock;
1506
1507 use_task_css_set_links = true;
1508
1509 /*
1510 * We need tasklist_lock because RCU is not safe against
1511 * while_each_thread(). Besides, a forking task that has passed
1512 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1513 * is not guaranteed to have its child immediately visible in the
1514 * tasklist if we walk through it with RCU.
1515 */
1516 read_lock(&tasklist_lock);
1517 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001518 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1519 task_css_set(p) != &init_css_set);
1520
1521 /*
1522 * We should check if the process is exiting, otherwise
1523 * it will race with cgroup_exit() in that the list
1524 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001525 * Do it while holding siglock so that we don't end up
1526 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001527 */
Tejun Heof153ad12014-02-25 09:56:49 -05001528 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001529 if (!(p->flags & PF_EXITING)) {
1530 struct css_set *cset = task_css_set(p);
1531
1532 list_add(&p->cg_list, &cset->tasks);
1533 get_css_set(cset);
1534 }
Tejun Heof153ad12014-02-25 09:56:49 -05001535 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001536 } while_each_thread(g, p);
1537 read_unlock(&tasklist_lock);
1538out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001539 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001540}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541
Paul Menagecc31edc2008-10-18 20:28:04 -07001542static void init_cgroup_housekeeping(struct cgroup *cgrp)
1543{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001544 struct cgroup_subsys *ss;
1545 int ssid;
1546
Tejun Heo2bd59d42014-02-11 11:52:49 -05001547 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001548 INIT_LIST_HEAD(&cgrp->sibling);
1549 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001550 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001551 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001552 INIT_LIST_HEAD(&cgrp->pidlists);
1553 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001554 cgrp->self.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001555
1556 for_each_subsys(ss, ssid)
1557 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001558
1559 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001560}
Paul Menagec6d57f32009-09-23 15:56:19 -07001561
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001562static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001563 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001565 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001566
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001568 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001569 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001570 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001571 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 root->flags = opts->flags;
1574 if (opts->release_agent)
1575 strcpy(root->release_agent_path, opts->release_agent);
1576 if (opts->name)
1577 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001578 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001579 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001580}
1581
Tejun Heo69dfa002014-05-04 15:09:13 -04001582static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001583{
Tejun Heod427dfe2014-02-11 11:52:48 -05001584 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001585 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001586 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001587 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001588
Tejun Heod427dfe2014-02-11 11:52:48 -05001589 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001590
Tejun Heo6fa49182014-05-04 15:09:13 -04001591 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001592 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001593 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001594 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001595
Tejun Heod427dfe2014-02-11 11:52:48 -05001596 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001597 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001598 * but that's OK - it can only be increased by someone holding
1599 * cgroup_lock, and that's us. The worst that can happen is that we
1600 * have some link structures left over
1601 */
1602 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001604 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
Tejun Heo985ed672014-03-19 10:23:53 -04001606 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001608 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609
Tejun Heo2bd59d42014-02-11 11:52:49 -05001610 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1611 KERNFS_ROOT_CREATE_DEACTIVATED,
1612 root_cgrp);
1613 if (IS_ERR(root->kf_root)) {
1614 ret = PTR_ERR(root->kf_root);
1615 goto exit_root_id;
1616 }
1617 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001618
Tejun Heod427dfe2014-02-11 11:52:48 -05001619 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1620 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001621 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622
Tejun Heo5df36032014-03-19 10:23:54 -04001623 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001624 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001625 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001626
Tejun Heod427dfe2014-02-11 11:52:48 -05001627 /*
1628 * There must be no failure case after here, since rebinding takes
1629 * care of subsystems' refcounts, which are explicitly dropped in
1630 * the failure exit path.
1631 */
1632 list_add(&root->root_list, &cgroup_roots);
1633 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634
Tejun Heod427dfe2014-02-11 11:52:48 -05001635 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001636 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001637 * objects.
1638 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001639 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001640 hash_for_each(css_set_table, i, cset, hlist)
1641 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001642 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643
Tejun Heod427dfe2014-02-11 11:52:48 -05001644 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001645 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001646
Tejun Heo2bd59d42014-02-11 11:52:49 -05001647 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001648 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001649 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001650
Tejun Heo2bd59d42014-02-11 11:52:49 -05001651destroy_root:
1652 kernfs_destroy_root(root->kf_root);
1653 root->kf_root = NULL;
1654exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001655 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001656out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001657 free_cgrp_cset_links(&tmp_links);
1658 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659}
1660
Al Virof7e83572010-07-26 13:23:11 +04001661static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001663 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001665 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001667 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001668 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001669 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001670
1671 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001672 * The first time anyone tries to mount a cgroup, enable the list
1673 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001674 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001675 if (!use_task_css_set_links)
1676 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001677
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001678 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001679
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001683 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001684
Tejun Heo2bd59d42014-02-11 11:52:49 -05001685 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001686 if (!opts.subsys_mask && !opts.none && !opts.name) {
1687 cgrp_dfl_root_visible = true;
1688 root = &cgrp_dfl_root;
1689 cgroup_get(&root->cgrp);
1690 ret = 0;
1691 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 }
1693
Tejun Heo985ed672014-03-19 10:23:53 -04001694 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001695 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001697 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001698 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001699
Paul Menage817929e2007-10-18 23:39:36 -07001700 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001701 * If we asked for a name then it must match. Also, if
1702 * name matches but sybsys_mask doesn't, we should fail.
1703 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001704 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001705 if (opts.name) {
1706 if (strcmp(opts.name, root->name))
1707 continue;
1708 name_match = true;
1709 }
Tejun Heo31261212013-06-28 17:07:30 -07001710
1711 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001712 * If we asked for subsystems (or explicitly for no
1713 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001714 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001715 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001716 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001717 if (!name_match)
1718 continue;
1719 ret = -EBUSY;
1720 goto out_unlock;
1721 }
Tejun Heo873fe092013-04-14 20:15:26 -07001722
Tejun Heoc7ba8282013-06-29 14:06:10 -07001723 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001724 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001725 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001726 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001727 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001728 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001729 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001730 }
Tejun Heo873fe092013-04-14 20:15:26 -07001731 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001732
Tejun Heo776f02f2014-02-12 09:29:50 -05001733 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001734 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001735 * ref indicate that the root is being destroyed. Wait for
1736 * destruction to complete so that the subsystems are free.
1737 * We can use wait_queue for the wait but this path is
1738 * super cold. Let's just sleep for a bit and retry.
1739 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001740 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001741 mutex_unlock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001742 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001743 ret = restart_syscall();
1744 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001745 }
1746
1747 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001748 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749 }
1750
Tejun Heo172a2c062014-03-19 10:23:53 -04001751 /*
1752 * No such thing, create a new one. name= matching without subsys
1753 * specification is allowed for already existing hierarchies but we
1754 * can't create new one without subsys specification.
1755 */
1756 if (!opts.subsys_mask && !opts.none) {
1757 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001758 goto out_unlock;
1759 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760
Tejun Heo172a2c062014-03-19 10:23:53 -04001761 root = kzalloc(sizeof(*root), GFP_KERNEL);
1762 if (!root) {
1763 ret = -ENOMEM;
1764 goto out_unlock;
1765 }
1766
1767 init_cgroup_root(root, &opts);
1768
Tejun Heo35585572014-02-13 06:58:38 -05001769 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001770 if (ret)
1771 cgroup_free_root(root);
1772
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001773out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001774 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001775out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001776 kfree(opts.release_agent);
1777 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001778
Tejun Heo2bd59d42014-02-11 11:52:49 -05001779 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001780 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001781
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001782 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1783 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001784 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001785 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786}
1787
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001788static void cgroup_kill_sb(struct super_block *sb)
1789{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001790 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001791 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001793 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001794 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795}
1796
1797static struct file_system_type cgroup_fs_type = {
1798 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001799 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 .kill_sb = cgroup_kill_sb,
1801};
1802
Greg KH676db4a2010-08-05 13:53:35 -07001803static struct kobject *cgroup_kobj;
1804
Li Zefana043e3b2008-02-23 15:24:09 -08001805/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001806 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001807 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001808 * @buf: the buffer to write the path into
1809 * @buflen: the length of the buffer
1810 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001811 * Determine @task's cgroup on the first (the one with the lowest non-zero
1812 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1813 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1814 * cgroup controller callbacks.
1815 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001816 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001817 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001818char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001819{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001820 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001821 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001822 int hierarchy_id = 1;
1823 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001824
1825 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001826 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001827
Tejun Heo913ffdb2013-07-11 16:34:48 -07001828 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1829
Tejun Heo857a2be2013-04-14 20:50:08 -07001830 if (root) {
1831 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001832 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001833 } else {
1834 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001835 if (strlcpy(buf, "/", buflen) < buflen)
1836 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001837 }
1838
Tejun Heo96d365e2014-02-13 06:58:40 -05001839 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001840 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001841 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001842}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001843EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001844
Tejun Heob3dc0942014-02-25 10:04:01 -05001845/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001846struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001847 /* the src and dst cset list running through cset->mg_node */
1848 struct list_head src_csets;
1849 struct list_head dst_csets;
1850
1851 /*
1852 * Fields for cgroup_taskset_*() iteration.
1853 *
1854 * Before migration is committed, the target migration tasks are on
1855 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1856 * the csets on ->dst_csets. ->csets point to either ->src_csets
1857 * or ->dst_csets depending on whether migration is committed.
1858 *
1859 * ->cur_csets and ->cur_task point to the current task position
1860 * during iteration.
1861 */
1862 struct list_head *csets;
1863 struct css_set *cur_cset;
1864 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001865};
1866
1867/**
1868 * cgroup_taskset_first - reset taskset and return the first task
1869 * @tset: taskset of interest
1870 *
1871 * @tset iteration is initialized and the first task is returned.
1872 */
1873struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1874{
Tejun Heob3dc0942014-02-25 10:04:01 -05001875 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1876 tset->cur_task = NULL;
1877
1878 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001879}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001880
1881/**
1882 * cgroup_taskset_next - iterate to the next task in taskset
1883 * @tset: taskset of interest
1884 *
1885 * Return the next task in @tset. Iteration must have been initialized
1886 * with cgroup_taskset_first().
1887 */
1888struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1889{
Tejun Heob3dc0942014-02-25 10:04:01 -05001890 struct css_set *cset = tset->cur_cset;
1891 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001892
Tejun Heob3dc0942014-02-25 10:04:01 -05001893 while (&cset->mg_node != tset->csets) {
1894 if (!task)
1895 task = list_first_entry(&cset->mg_tasks,
1896 struct task_struct, cg_list);
1897 else
1898 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001899
Tejun Heob3dc0942014-02-25 10:04:01 -05001900 if (&task->cg_list != &cset->mg_tasks) {
1901 tset->cur_cset = cset;
1902 tset->cur_task = task;
1903 return task;
1904 }
1905
1906 cset = list_next_entry(cset, mg_node);
1907 task = NULL;
1908 }
1909
1910 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001911}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001912
1913/**
Ben Blum74a11662011-05-26 16:25:20 -07001914 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001915 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001916 * @tsk: the task being migrated
1917 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001918 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001919 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001920 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001921static void cgroup_task_migrate(struct cgroup *old_cgrp,
1922 struct task_struct *tsk,
1923 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001924{
Tejun Heo5abb8852013-06-12 21:04:49 -07001925 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001926
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001927 lockdep_assert_held(&cgroup_mutex);
1928 lockdep_assert_held(&css_set_rwsem);
1929
Ben Blum74a11662011-05-26 16:25:20 -07001930 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001931 * We are synchronized through threadgroup_lock() against PF_EXITING
1932 * setting such that we can't race against cgroup_exit() changing the
1933 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001934 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001935 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001936 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001937
Tejun Heob3dc0942014-02-25 10:04:01 -05001938 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001939 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001940
Tejun Heo1b9aba42014-03-19 17:43:21 -04001941 /*
1942 * Use move_tail so that cgroup_taskset_first() still returns the
1943 * leader after migration. This works because cgroup_migrate()
1944 * ensures that the dst_cset of the leader is the first on the
1945 * tset's dst_csets list.
1946 */
1947 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001948
1949 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001950 * We just gained a reference on old_cset by taking it from the
1951 * task. As trading it for new_cset is protected by cgroup_mutex,
1952 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001953 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001954 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001955 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001956}
1957
Li Zefana043e3b2008-02-23 15:24:09 -08001958/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001959 * cgroup_migrate_finish - cleanup after attach
1960 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001961 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001962 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1963 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001964 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001965static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001966{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001967 struct css_set *cset, *tmp_cset;
1968
1969 lockdep_assert_held(&cgroup_mutex);
1970
1971 down_write(&css_set_rwsem);
1972 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1973 cset->mg_src_cgrp = NULL;
1974 cset->mg_dst_cset = NULL;
1975 list_del_init(&cset->mg_preload_node);
1976 put_css_set_locked(cset, false);
1977 }
1978 up_write(&css_set_rwsem);
1979}
1980
1981/**
1982 * cgroup_migrate_add_src - add a migration source css_set
1983 * @src_cset: the source css_set to add
1984 * @dst_cgrp: the destination cgroup
1985 * @preloaded_csets: list of preloaded css_sets
1986 *
1987 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1988 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1989 * up by cgroup_migrate_finish().
1990 *
1991 * This function may be called without holding threadgroup_lock even if the
1992 * target is a process. Threads may be created and destroyed but as long
1993 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1994 * the preloaded css_sets are guaranteed to cover all migrations.
1995 */
1996static void cgroup_migrate_add_src(struct css_set *src_cset,
1997 struct cgroup *dst_cgrp,
1998 struct list_head *preloaded_csets)
1999{
2000 struct cgroup *src_cgrp;
2001
2002 lockdep_assert_held(&cgroup_mutex);
2003 lockdep_assert_held(&css_set_rwsem);
2004
2005 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2006
Tejun Heo1958d2d2014-02-25 10:04:03 -05002007 if (!list_empty(&src_cset->mg_preload_node))
2008 return;
2009
2010 WARN_ON(src_cset->mg_src_cgrp);
2011 WARN_ON(!list_empty(&src_cset->mg_tasks));
2012 WARN_ON(!list_empty(&src_cset->mg_node));
2013
2014 src_cset->mg_src_cgrp = src_cgrp;
2015 get_css_set(src_cset);
2016 list_add(&src_cset->mg_preload_node, preloaded_csets);
2017}
2018
2019/**
2020 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002021 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002022 * @preloaded_csets: list of preloaded source css_sets
2023 *
2024 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2025 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002026 * pins all destination css_sets, links each to its source, and append them
2027 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2028 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002029 *
2030 * This function must be called after cgroup_migrate_add_src() has been
2031 * called on each migration source css_set. After migration is performed
2032 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2033 * @preloaded_csets.
2034 */
2035static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2036 struct list_head *preloaded_csets)
2037{
2038 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002039 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002040
2041 lockdep_assert_held(&cgroup_mutex);
2042
Tejun Heof8f22e52014-04-23 11:13:16 -04002043 /*
2044 * Except for the root, child_subsys_mask must be zero for a cgroup
2045 * with tasks so that child cgroups don't compete against tasks.
2046 */
2047 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2048 dst_cgrp->child_subsys_mask)
2049 return -EBUSY;
2050
Tejun Heo1958d2d2014-02-25 10:04:03 -05002051 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002052 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002053 struct css_set *dst_cset;
2054
Tejun Heof817de92014-04-23 11:13:16 -04002055 dst_cset = find_css_set(src_cset,
2056 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002057 if (!dst_cset)
2058 goto err;
2059
2060 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002061
2062 /*
2063 * If src cset equals dst, it's noop. Drop the src.
2064 * cgroup_migrate() will skip the cset too. Note that we
2065 * can't handle src == dst as some nodes are used by both.
2066 */
2067 if (src_cset == dst_cset) {
2068 src_cset->mg_src_cgrp = NULL;
2069 list_del_init(&src_cset->mg_preload_node);
2070 put_css_set(src_cset, false);
2071 put_css_set(dst_cset, false);
2072 continue;
2073 }
2074
Tejun Heo1958d2d2014-02-25 10:04:03 -05002075 src_cset->mg_dst_cset = dst_cset;
2076
2077 if (list_empty(&dst_cset->mg_preload_node))
2078 list_add(&dst_cset->mg_preload_node, &csets);
2079 else
2080 put_css_set(dst_cset, false);
2081 }
2082
Tejun Heof817de92014-04-23 11:13:16 -04002083 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002084 return 0;
2085err:
2086 cgroup_migrate_finish(&csets);
2087 return -ENOMEM;
2088}
2089
2090/**
2091 * cgroup_migrate - migrate a process or task to a cgroup
2092 * @cgrp: the destination cgroup
2093 * @leader: the leader of the process or the task to migrate
2094 * @threadgroup: whether @leader points to the whole process or a single task
2095 *
2096 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2097 * process, the caller must be holding threadgroup_lock of @leader. The
2098 * caller is also responsible for invoking cgroup_migrate_add_src() and
2099 * cgroup_migrate_prepare_dst() on the targets before invoking this
2100 * function and following up with cgroup_migrate_finish().
2101 *
2102 * As long as a controller's ->can_attach() doesn't fail, this function is
2103 * guaranteed to succeed. This means that, excluding ->can_attach()
2104 * failure, when migrating multiple targets, the success or failure can be
2105 * decided for all targets by invoking group_migrate_prepare_dst() before
2106 * actually starting migrating.
2107 */
2108static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2109 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002110{
Tejun Heob3dc0942014-02-25 10:04:01 -05002111 struct cgroup_taskset tset = {
2112 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2113 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2114 .csets = &tset.src_csets,
2115 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002116 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002117 struct css_set *cset, *tmp_cset;
2118 struct task_struct *task, *tmp_task;
2119 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002120
2121 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002122 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2123 * already PF_EXITING could be freed from underneath us unless we
2124 * take an rcu_read_lock.
2125 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002126 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002127 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002128 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002129 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002130 /* @task either already exited or can't exit until the end */
2131 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002132 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002133
Tejun Heoeaf797a2014-02-25 10:04:03 -05002134 /* leave @task alone if post_fork() hasn't linked it yet */
2135 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002136 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002137
Tejun Heob3dc0942014-02-25 10:04:01 -05002138 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002139 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002140 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002141
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002142 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002143 * cgroup_taskset_first() must always return the leader.
2144 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002145 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002146 list_move_tail(&task->cg_list, &cset->mg_tasks);
2147 if (list_empty(&cset->mg_node))
2148 list_add_tail(&cset->mg_node, &tset.src_csets);
2149 if (list_empty(&cset->mg_dst_cset->mg_node))
2150 list_move_tail(&cset->mg_dst_cset->mg_node,
2151 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002152 next:
Li Zefan081aa452013-03-13 09:17:09 +08002153 if (!threadgroup)
2154 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002155 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002156 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002157 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002158
Tejun Heo134d3372011-12-12 18:12:21 -08002159 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002160 if (list_empty(&tset.src_csets))
2161 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002162
Tejun Heo1958d2d2014-02-25 10:04:03 -05002163 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002164 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002165 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002166 ret = css->ss->can_attach(css, &tset);
2167 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002168 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002169 goto out_cancel_attach;
2170 }
2171 }
Ben Blum74a11662011-05-26 16:25:20 -07002172 }
2173
2174 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002175 * Now that we're guaranteed success, proceed to move all tasks to
2176 * the new cgroup. There are no failure cases after here, so this
2177 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002178 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002179 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002180 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2181 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2182 cgroup_task_migrate(cset->mg_src_cgrp, task,
2183 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002184 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002185 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002186
2187 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002188 * Migration is committed, all target tasks are now on dst_csets.
2189 * Nothing is sensitive to fork() after this point. Notify
2190 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002191 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002192 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002193
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002194 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002195 if (css->ss->attach)
2196 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002197
Tejun Heo9db8de32014-02-13 06:58:43 -05002198 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002199 goto out_release_tset;
2200
Ben Blum74a11662011-05-26 16:25:20 -07002201out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002202 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002203 if (css == failed_css)
2204 break;
2205 if (css->ss->cancel_attach)
2206 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002207 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002208out_release_tset:
2209 down_write(&css_set_rwsem);
2210 list_splice_init(&tset.dst_csets, &tset.src_csets);
2211 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002212 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002213 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002214 }
2215 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002216 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002217}
2218
Tejun Heo1958d2d2014-02-25 10:04:03 -05002219/**
2220 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2221 * @dst_cgrp: the cgroup to attach to
2222 * @leader: the task or the leader of the threadgroup to be attached
2223 * @threadgroup: attach the whole threadgroup?
2224 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002225 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002226 */
2227static int cgroup_attach_task(struct cgroup *dst_cgrp,
2228 struct task_struct *leader, bool threadgroup)
2229{
2230 LIST_HEAD(preloaded_csets);
2231 struct task_struct *task;
2232 int ret;
2233
2234 /* look up all src csets */
2235 down_read(&css_set_rwsem);
2236 rcu_read_lock();
2237 task = leader;
2238 do {
2239 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2240 &preloaded_csets);
2241 if (!threadgroup)
2242 break;
2243 } while_each_thread(leader, task);
2244 rcu_read_unlock();
2245 up_read(&css_set_rwsem);
2246
2247 /* prepare dst csets and commit */
2248 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2249 if (!ret)
2250 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2251
2252 cgroup_migrate_finish(&preloaded_csets);
2253 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002254}
2255
2256/*
2257 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002258 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002259 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002260 */
Tejun Heoacbef752014-05-13 12:16:22 -04002261static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2262 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002263{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002264 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002265 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002266 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002267 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002268 int ret;
2269
Tejun Heoacbef752014-05-13 12:16:22 -04002270 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2271 return -EINVAL;
2272
Tejun Heoe76ecae2014-05-13 12:19:23 -04002273 cgrp = cgroup_kn_lock_live(of->kn);
2274 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002275 return -ENODEV;
2276
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002277retry_find_task:
2278 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002279 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002280 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002281 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002282 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002283 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002284 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002285 }
Ben Blum74a11662011-05-26 16:25:20 -07002286 /*
2287 * even if we're attaching all tasks in the thread group, we
2288 * only need to check permissions on one of them.
2289 */
David Howellsc69e8d92008-11-14 10:39:19 +11002290 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002291 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2292 !uid_eq(cred->euid, tcred->uid) &&
2293 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002294 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002295 ret = -EACCES;
2296 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002297 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002298 } else
2299 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002300
2301 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002302 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002303
2304 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002305 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002306 * trapped in a cpuset, or RT worker may be born in a cgroup
2307 * with no rt_runtime allocated. Just say no.
2308 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002309 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002310 ret = -EINVAL;
2311 rcu_read_unlock();
2312 goto out_unlock_cgroup;
2313 }
2314
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002315 get_task_struct(tsk);
2316 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002317
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002318 threadgroup_lock(tsk);
2319 if (threadgroup) {
2320 if (!thread_group_leader(tsk)) {
2321 /*
2322 * a race with de_thread from another thread's exec()
2323 * may strip us of our leadership, if this happens,
2324 * there is no choice but to throw this task away and
2325 * try again; this is
2326 * "double-double-toil-and-trouble-check locking".
2327 */
2328 threadgroup_unlock(tsk);
2329 put_task_struct(tsk);
2330 goto retry_find_task;
2331 }
Li Zefan081aa452013-03-13 09:17:09 +08002332 }
2333
2334 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2335
Tejun Heocd3d0952011-12-12 18:12:21 -08002336 threadgroup_unlock(tsk);
2337
Paul Menagebbcb81d2007-10-18 23:39:32 -07002338 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002339out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002340 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002341 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002342}
2343
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002344/**
2345 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2346 * @from: attach to all cgroups of a given task
2347 * @tsk: the task to be attached
2348 */
2349int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2350{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002351 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002352 int retval = 0;
2353
Tejun Heo47cfcd02013-04-07 09:29:51 -07002354 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002355 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002356 struct cgroup *from_cgrp;
2357
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002358 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002359 continue;
2360
Tejun Heo96d365e2014-02-13 06:58:40 -05002361 down_read(&css_set_rwsem);
2362 from_cgrp = task_cgroup_from_root(from, root);
2363 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002364
Li Zefan6f4b7e62013-07-31 16:18:36 +08002365 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002366 if (retval)
2367 break;
2368 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002369 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002370
2371 return retval;
2372}
2373EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2374
Tejun Heoacbef752014-05-13 12:16:22 -04002375static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2376 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002377{
Tejun Heoacbef752014-05-13 12:16:22 -04002378 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002379}
2380
Tejun Heoacbef752014-05-13 12:16:22 -04002381static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2382 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002383{
Tejun Heoacbef752014-05-13 12:16:22 -04002384 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002385}
2386
Tejun Heo451af502014-05-13 12:16:21 -04002387static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2388 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002389{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002390 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002391
Tejun Heoe76ecae2014-05-13 12:19:23 -04002392 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2393
2394 cgrp = cgroup_kn_lock_live(of->kn);
2395 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002396 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002397 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002398 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2399 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002400 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002401 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002402 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002403}
2404
Tejun Heo2da8ca82013-12-05 12:28:04 -05002405static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002406{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002407 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002408
Tejun Heo46cfeb02014-05-13 12:11:00 -04002409 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002410 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002411 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002412 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002413 return 0;
2414}
2415
Tejun Heo2da8ca82013-12-05 12:28:04 -05002416static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002417{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002418 struct cgroup *cgrp = seq_css(seq)->cgroup;
2419
2420 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002421 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422}
2423
Tejun Heof8f22e52014-04-23 11:13:16 -04002424static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2425{
2426 struct cgroup_subsys *ss;
2427 bool printed = false;
2428 int ssid;
2429
2430 for_each_subsys(ss, ssid) {
2431 if (ss_mask & (1 << ssid)) {
2432 if (printed)
2433 seq_putc(seq, ' ');
2434 seq_printf(seq, "%s", ss->name);
2435 printed = true;
2436 }
2437 }
2438 if (printed)
2439 seq_putc(seq, '\n');
2440}
2441
2442/* show controllers which are currently attached to the default hierarchy */
2443static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2444{
2445 struct cgroup *cgrp = seq_css(seq)->cgroup;
2446
2447 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2448 return 0;
2449}
2450
2451/* show controllers which are enabled from the parent */
2452static int cgroup_controllers_show(struct seq_file *seq, void *v)
2453{
2454 struct cgroup *cgrp = seq_css(seq)->cgroup;
2455
2456 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2457 return 0;
2458}
2459
2460/* show controllers which are enabled for a given cgroup's children */
2461static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2462{
2463 struct cgroup *cgrp = seq_css(seq)->cgroup;
2464
2465 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2466 return 0;
2467}
2468
2469/**
2470 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2471 * @cgrp: root of the subtree to update csses for
2472 *
2473 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2474 * css associations need to be updated accordingly. This function looks up
2475 * all css_sets which are attached to the subtree, creates the matching
2476 * updated css_sets and migrates the tasks to the new ones.
2477 */
2478static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2479{
2480 LIST_HEAD(preloaded_csets);
2481 struct cgroup_subsys_state *css;
2482 struct css_set *src_cset;
2483 int ret;
2484
Tejun Heof8f22e52014-04-23 11:13:16 -04002485 lockdep_assert_held(&cgroup_mutex);
2486
2487 /* look up all csses currently attached to @cgrp's subtree */
2488 down_read(&css_set_rwsem);
2489 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2490 struct cgrp_cset_link *link;
2491
2492 /* self is not affected by child_subsys_mask change */
2493 if (css->cgroup == cgrp)
2494 continue;
2495
2496 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2497 cgroup_migrate_add_src(link->cset, cgrp,
2498 &preloaded_csets);
2499 }
2500 up_read(&css_set_rwsem);
2501
2502 /* NULL dst indicates self on default hierarchy */
2503 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2504 if (ret)
2505 goto out_finish;
2506
2507 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2508 struct task_struct *last_task = NULL, *task;
2509
2510 /* src_csets precede dst_csets, break on the first dst_cset */
2511 if (!src_cset->mg_src_cgrp)
2512 break;
2513
2514 /*
2515 * All tasks in src_cset need to be migrated to the
2516 * matching dst_cset. Empty it process by process. We
2517 * walk tasks but migrate processes. The leader might even
2518 * belong to a different cset but such src_cset would also
2519 * be among the target src_csets because the default
2520 * hierarchy enforces per-process membership.
2521 */
2522 while (true) {
2523 down_read(&css_set_rwsem);
2524 task = list_first_entry_or_null(&src_cset->tasks,
2525 struct task_struct, cg_list);
2526 if (task) {
2527 task = task->group_leader;
2528 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2529 get_task_struct(task);
2530 }
2531 up_read(&css_set_rwsem);
2532
2533 if (!task)
2534 break;
2535
2536 /* guard against possible infinite loop */
2537 if (WARN(last_task == task,
2538 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2539 goto out_finish;
2540 last_task = task;
2541
2542 threadgroup_lock(task);
2543 /* raced against de_thread() from another thread? */
2544 if (!thread_group_leader(task)) {
2545 threadgroup_unlock(task);
2546 put_task_struct(task);
2547 continue;
2548 }
2549
2550 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2551
2552 threadgroup_unlock(task);
2553 put_task_struct(task);
2554
2555 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2556 goto out_finish;
2557 }
2558 }
2559
2560out_finish:
2561 cgroup_migrate_finish(&preloaded_csets);
2562 return ret;
2563}
2564
2565/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002566static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2567 char *buf, size_t nbytes,
2568 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002569{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002570 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002571 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002572 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002573 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002574 int ssid, ret;
2575
2576 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002577 * Parse input - space separated list of subsystem names prefixed
2578 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002579 */
Tejun Heo451af502014-05-13 12:16:21 -04002580 buf = strstrip(buf);
2581 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002582 if (tok[0] == '\0')
2583 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002584 for_each_subsys(ss, ssid) {
2585 if (ss->disabled || strcmp(tok + 1, ss->name))
2586 continue;
2587
2588 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002589 enable |= 1 << ssid;
2590 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002591 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002592 disable |= 1 << ssid;
2593 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002594 } else {
2595 return -EINVAL;
2596 }
2597 break;
2598 }
2599 if (ssid == CGROUP_SUBSYS_COUNT)
2600 return -EINVAL;
2601 }
2602
Tejun Heoa9746d82014-05-13 12:19:22 -04002603 cgrp = cgroup_kn_lock_live(of->kn);
2604 if (!cgrp)
2605 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002606
2607 for_each_subsys(ss, ssid) {
2608 if (enable & (1 << ssid)) {
2609 if (cgrp->child_subsys_mask & (1 << ssid)) {
2610 enable &= ~(1 << ssid);
2611 continue;
2612 }
2613
2614 /*
2615 * Because css offlining is asynchronous, userland
2616 * might try to re-enable the same controller while
2617 * the previous instance is still around. In such
2618 * cases, wait till it's gone using offline_waitq.
2619 */
2620 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002621 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002622
2623 if (!cgroup_css(child, ss))
2624 continue;
2625
Tejun Heo0cee8b72014-05-13 12:10:59 -04002626 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002627 prepare_to_wait(&child->offline_waitq, &wait,
2628 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002629 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002630 schedule();
2631 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002632 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002633
Tejun Heoa9746d82014-05-13 12:19:22 -04002634 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002635 }
2636
2637 /* unavailable or not enabled on the parent? */
2638 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2639 (cgrp->parent &&
2640 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2641 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002642 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002643 }
2644 } else if (disable & (1 << ssid)) {
2645 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2646 disable &= ~(1 << ssid);
2647 continue;
2648 }
2649
2650 /* a child has it enabled? */
2651 cgroup_for_each_live_child(child, cgrp) {
2652 if (child->child_subsys_mask & (1 << ssid)) {
2653 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002654 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002655 }
2656 }
2657 }
2658 }
2659
2660 if (!enable && !disable) {
2661 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002662 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002663 }
2664
2665 /*
2666 * Except for the root, child_subsys_mask must be zero for a cgroup
2667 * with tasks so that child cgroups don't compete against tasks.
2668 */
2669 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2670 ret = -EBUSY;
2671 goto out_unlock;
2672 }
2673
2674 /*
2675 * Create csses for enables and update child_subsys_mask. This
2676 * changes cgroup_e_css() results which in turn makes the
2677 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2678 * subtree to the updated csses.
2679 */
2680 for_each_subsys(ss, ssid) {
2681 if (!(enable & (1 << ssid)))
2682 continue;
2683
2684 cgroup_for_each_live_child(child, cgrp) {
2685 ret = create_css(child, ss);
2686 if (ret)
2687 goto err_undo_css;
2688 }
2689 }
2690
2691 cgrp->child_subsys_mask |= enable;
2692 cgrp->child_subsys_mask &= ~disable;
2693
2694 ret = cgroup_update_dfl_csses(cgrp);
2695 if (ret)
2696 goto err_undo_css;
2697
2698 /* all tasks are now migrated away from the old csses, kill them */
2699 for_each_subsys(ss, ssid) {
2700 if (!(disable & (1 << ssid)))
2701 continue;
2702
2703 cgroup_for_each_live_child(child, cgrp)
2704 kill_css(cgroup_css(child, ss));
2705 }
2706
2707 kernfs_activate(cgrp->kn);
2708 ret = 0;
2709out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002710 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002711 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002712
2713err_undo_css:
2714 cgrp->child_subsys_mask &= ~enable;
2715 cgrp->child_subsys_mask |= disable;
2716
2717 for_each_subsys(ss, ssid) {
2718 if (!(enable & (1 << ssid)))
2719 continue;
2720
2721 cgroup_for_each_live_child(child, cgrp) {
2722 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2723 if (css)
2724 kill_css(css);
2725 }
2726 }
2727 goto out_unlock;
2728}
2729
Tejun Heo842b5972014-04-25 18:28:02 -04002730static int cgroup_populated_show(struct seq_file *seq, void *v)
2731{
2732 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2733 return 0;
2734}
2735
Tejun Heo2bd59d42014-02-11 11:52:49 -05002736static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2737 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002739 struct cgroup *cgrp = of->kn->parent->priv;
2740 struct cftype *cft = of->kn->priv;
2741 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002742 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002743
Tejun Heob4168642014-05-13 12:16:21 -04002744 if (cft->write)
2745 return cft->write(of, buf, nbytes, off);
2746
Tejun Heo2bd59d42014-02-11 11:52:49 -05002747 /*
2748 * kernfs guarantees that a file isn't deleted with operations in
2749 * flight, which means that the matching css is and stays alive and
2750 * doesn't need to be pinned. The RCU locking is not necessary
2751 * either. It's just for the convenience of using cgroup_css().
2752 */
2753 rcu_read_lock();
2754 css = cgroup_css(cgrp, cft->ss);
2755 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756
Tejun Heo451af502014-05-13 12:16:21 -04002757 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002758 unsigned long long v;
2759 ret = kstrtoull(buf, 0, &v);
2760 if (!ret)
2761 ret = cft->write_u64(css, cft, v);
2762 } else if (cft->write_s64) {
2763 long long v;
2764 ret = kstrtoll(buf, 0, &v);
2765 if (!ret)
2766 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002767 } else {
2768 ret = -EINVAL;
2769 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002770
Tejun Heoa742c592013-12-05 12:28:03 -05002771 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002772}
2773
Tejun Heo6612f052013-12-05 12:28:04 -05002774static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002775{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002776 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002777}
2778
2779static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2780{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002781 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002782}
2783
2784static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2785{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002786 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002787}
2788
2789static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2790{
Tejun Heo7da11272013-12-05 12:28:04 -05002791 struct cftype *cft = seq_cft(m);
2792 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002793
Tejun Heo2da8ca82013-12-05 12:28:04 -05002794 if (cft->seq_show)
2795 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002796
Tejun Heo896f5192013-12-05 12:28:04 -05002797 if (cft->read_u64)
2798 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2799 else if (cft->read_s64)
2800 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2801 else
2802 return -EINVAL;
2803 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002804}
2805
Tejun Heo2bd59d42014-02-11 11:52:49 -05002806static struct kernfs_ops cgroup_kf_single_ops = {
2807 .atomic_write_len = PAGE_SIZE,
2808 .write = cgroup_file_write,
2809 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002810};
2811
Tejun Heo2bd59d42014-02-11 11:52:49 -05002812static struct kernfs_ops cgroup_kf_ops = {
2813 .atomic_write_len = PAGE_SIZE,
2814 .write = cgroup_file_write,
2815 .seq_start = cgroup_seqfile_start,
2816 .seq_next = cgroup_seqfile_next,
2817 .seq_stop = cgroup_seqfile_stop,
2818 .seq_show = cgroup_seqfile_show,
2819};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002820
2821/*
2822 * cgroup_rename - Only allow simple rename of directories in place.
2823 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002824static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2825 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002826{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002827 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002828 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002829
Tejun Heo2bd59d42014-02-11 11:52:49 -05002830 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002831 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002832 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002833 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002834
Tejun Heo6db8e852013-06-14 11:18:22 -07002835 /*
2836 * This isn't a proper migration and its usefulness is very
2837 * limited. Disallow if sane_behavior.
2838 */
2839 if (cgroup_sane_behavior(cgrp))
2840 return -EPERM;
2841
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002842 /*
Tejun Heo8353da12014-05-13 12:19:23 -04002843 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002844 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04002845 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002846 */
2847 kernfs_break_active_protection(new_parent);
2848 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002849
Tejun Heo2bd59d42014-02-11 11:52:49 -05002850 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002851
Tejun Heo2bd59d42014-02-11 11:52:49 -05002852 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002853
Tejun Heo2bd59d42014-02-11 11:52:49 -05002854 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002855
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002856 kernfs_unbreak_active_protection(kn);
2857 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002858 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002859}
2860
Tejun Heo49957f82014-04-07 16:44:47 -04002861/* set uid and gid of cgroup dirs and files to that of the creator */
2862static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2863{
2864 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2865 .ia_uid = current_fsuid(),
2866 .ia_gid = current_fsgid(), };
2867
2868 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2869 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2870 return 0;
2871
2872 return kernfs_setattr(kn, &iattr);
2873}
2874
Tejun Heo2bb566c2013-08-08 20:11:23 -04002875static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002876{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002877 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002878 struct kernfs_node *kn;
2879 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002880 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002881
Tejun Heo2bd59d42014-02-11 11:52:49 -05002882#ifdef CONFIG_DEBUG_LOCK_ALLOC
2883 key = &cft->lockdep_key;
2884#endif
2885 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2886 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2887 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002888 if (IS_ERR(kn))
2889 return PTR_ERR(kn);
2890
2891 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002892 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002893 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002894 return ret;
2895 }
2896
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002897 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002898 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002899 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002900}
2901
Tejun Heob1f28d32013-06-28 16:24:10 -07002902/**
2903 * cgroup_addrm_files - add or remove files to a cgroup directory
2904 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002905 * @cfts: array of cftypes to be added
2906 * @is_add: whether to add or remove
2907 *
2908 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002909 * For removals, this function never fails. If addition fails, this
2910 * function doesn't remove files already added. The caller is responsible
2911 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002912 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002913static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2914 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002915{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002916 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002917 int ret;
2918
Tejun Heo01f64742014-05-13 12:19:23 -04002919 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002920
2921 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002922 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002923 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2924 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002925 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2926 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002927 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2928 continue;
2929 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2930 continue;
2931
Li Zefan2739d3c2013-01-21 18:18:33 +08002932 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002933 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002934 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002935 pr_warn("%s: failed to add %s, err=%d\n",
2936 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002937 return ret;
2938 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002939 } else {
2940 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002941 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002942 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002943 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002944}
2945
Tejun Heo21a2d342014-02-12 09:29:49 -05002946static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002947{
2948 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002949 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002950 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002951 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002952 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002953
Tejun Heo01f64742014-05-13 12:19:23 -04002954 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002955
Li Zefane8c82d22013-06-18 18:48:37 +08002956 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002957 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002958 struct cgroup *cgrp = css->cgroup;
2959
Li Zefane8c82d22013-06-18 18:48:37 +08002960 if (cgroup_is_dead(cgrp))
2961 continue;
2962
Tejun Heo21a2d342014-02-12 09:29:49 -05002963 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002964 if (ret)
2965 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002966 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002967
2968 if (is_add && !ret)
2969 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002970 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002971}
2972
Tejun Heo2da440a2014-02-11 11:52:48 -05002973static void cgroup_exit_cftypes(struct cftype *cfts)
2974{
2975 struct cftype *cft;
2976
Tejun Heo2bd59d42014-02-11 11:52:49 -05002977 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2978 /* free copy for custom atomic_write_len, see init_cftypes() */
2979 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2980 kfree(cft->kf_ops);
2981 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002982 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002983 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002984}
2985
Tejun Heo2bd59d42014-02-11 11:52:49 -05002986static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002987{
2988 struct cftype *cft;
2989
Tejun Heo2bd59d42014-02-11 11:52:49 -05002990 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2991 struct kernfs_ops *kf_ops;
2992
Tejun Heo0adb0702014-02-12 09:29:48 -05002993 WARN_ON(cft->ss || cft->kf_ops);
2994
Tejun Heo2bd59d42014-02-11 11:52:49 -05002995 if (cft->seq_start)
2996 kf_ops = &cgroup_kf_ops;
2997 else
2998 kf_ops = &cgroup_kf_single_ops;
2999
3000 /*
3001 * Ugh... if @cft wants a custom max_write_len, we need to
3002 * make a copy of kf_ops to set its atomic_write_len.
3003 */
3004 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3005 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3006 if (!kf_ops) {
3007 cgroup_exit_cftypes(cfts);
3008 return -ENOMEM;
3009 }
3010 kf_ops->atomic_write_len = cft->max_write_len;
3011 }
3012
3013 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003014 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003015 }
3016
3017 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003018}
3019
Tejun Heo21a2d342014-02-12 09:29:49 -05003020static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3021{
Tejun Heo01f64742014-05-13 12:19:23 -04003022 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003023
3024 if (!cfts || !cfts[0].ss)
3025 return -ENOENT;
3026
3027 list_del(&cfts->node);
3028 cgroup_apply_cftypes(cfts, false);
3029 cgroup_exit_cftypes(cfts);
3030 return 0;
3031}
3032
Tejun Heo8e3f6542012-04-01 12:09:55 -07003033/**
Tejun Heo80b13582014-02-12 09:29:48 -05003034 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3035 * @cfts: zero-length name terminated array of cftypes
3036 *
3037 * Unregister @cfts. Files described by @cfts are removed from all
3038 * existing cgroups and all future cgroups won't have them either. This
3039 * function can be called anytime whether @cfts' subsys is attached or not.
3040 *
3041 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3042 * registered.
3043 */
3044int cgroup_rm_cftypes(struct cftype *cfts)
3045{
Tejun Heo21a2d342014-02-12 09:29:49 -05003046 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003047
Tejun Heo01f64742014-05-13 12:19:23 -04003048 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003049 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003050 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003051 return ret;
3052}
3053
3054/**
3055 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3056 * @ss: target cgroup subsystem
3057 * @cfts: zero-length name terminated array of cftypes
3058 *
3059 * Register @cfts to @ss. Files described by @cfts are created for all
3060 * existing cgroups to which @ss is attached and all future cgroups will
3061 * have them too. This function can be called anytime whether @ss is
3062 * attached or not.
3063 *
3064 * Returns 0 on successful registration, -errno on failure. Note that this
3065 * function currently returns 0 as long as @cfts registration is successful
3066 * even if some file creation attempts on existing cgroups fail.
3067 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003068int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003069{
Tejun Heo9ccece82013-06-28 16:24:11 -07003070 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003071
Li Zefandc5736e2014-02-17 10:41:50 +08003072 if (!cfts || cfts[0].name[0] == '\0')
3073 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003074
Tejun Heo2bd59d42014-02-11 11:52:49 -05003075 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003076 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003077 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003078
Tejun Heo01f64742014-05-13 12:19:23 -04003079 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003080
Tejun Heo0adb0702014-02-12 09:29:48 -05003081 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003082 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003083 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003084 cgroup_rm_cftypes_locked(cfts);
3085
Tejun Heo01f64742014-05-13 12:19:23 -04003086 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003087 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003088}
Tejun Heo79578622012-04-01 12:09:56 -07003089
3090/**
Li Zefana043e3b2008-02-23 15:24:09 -08003091 * cgroup_task_count - count the number of tasks in a cgroup.
3092 * @cgrp: the cgroup in question
3093 *
3094 * Return the number of tasks in the cgroup.
3095 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003096static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003097{
3098 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003099 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003100
Tejun Heo96d365e2014-02-13 06:58:40 -05003101 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003102 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3103 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003104 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003105 return count;
3106}
3107
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108/**
Tejun Heo492eb212013-08-08 20:11:25 -04003109 * css_next_child - find the next child of a given css
3110 * @pos_css: the current position (%NULL to initiate traversal)
3111 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003112 *
Tejun Heo492eb212013-08-08 20:11:25 -04003113 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003114 * under either cgroup_mutex or RCU read lock. The only requirement is
3115 * that @parent_css and @pos_css are accessible. The next sibling is
3116 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003117 */
Tejun Heo492eb212013-08-08 20:11:25 -04003118struct cgroup_subsys_state *
3119css_next_child(struct cgroup_subsys_state *pos_css,
3120 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003121{
Tejun Heo492eb212013-08-08 20:11:25 -04003122 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3123 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003124 struct cgroup *next;
3125
Tejun Heo8353da12014-05-13 12:19:23 -04003126 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003127
3128 /*
3129 * @pos could already have been removed. Once a cgroup is removed,
3130 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003131 * changes. As CGRP_DEAD assertion is serialized and happens
3132 * before the cgroup is taken off the ->sibling list, if we see it
3133 * unasserted, it's guaranteed that the next sibling hasn't
3134 * finished its grace period even if it's already removed, and thus
3135 * safe to dereference from this RCU critical section. If
3136 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3137 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003138 *
3139 * If @pos is dead, its next pointer can't be dereferenced;
3140 * however, as each cgroup is given a monotonically increasing
3141 * unique serial number and always appended to the sibling list,
3142 * the next one can be found by walking the parent's children until
3143 * we see a cgroup with higher serial number than @pos's. While
3144 * this path can be slower, it's taken only when either the current
3145 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003146 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003147 if (!pos) {
3148 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3149 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003150 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003151 } else {
3152 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3153 if (next->serial_nr > pos->serial_nr)
3154 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003155 }
3156
Tejun Heo3b281af2014-04-23 11:13:15 -04003157 /*
3158 * @next, if not pointing to the head, can be dereferenced and is
3159 * the next sibling; however, it might have @ss disabled. If so,
3160 * fast-forward to the next enabled one.
3161 */
3162 while (&next->sibling != &cgrp->children) {
3163 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003164
Tejun Heo3b281af2014-04-23 11:13:15 -04003165 if (next_css)
3166 return next_css;
3167 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3168 }
3169 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003170}
Tejun Heo53fa5262013-05-24 10:55:38 +09003171
3172/**
Tejun Heo492eb212013-08-08 20:11:25 -04003173 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003174 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003175 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003176 *
Tejun Heo492eb212013-08-08 20:11:25 -04003177 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003178 * to visit for pre-order traversal of @root's descendants. @root is
3179 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003180 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003181 * While this function requires cgroup_mutex or RCU read locking, it
3182 * doesn't require the whole traversal to be contained in a single critical
3183 * section. This function will return the correct next descendant as long
3184 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003185 */
Tejun Heo492eb212013-08-08 20:11:25 -04003186struct cgroup_subsys_state *
3187css_next_descendant_pre(struct cgroup_subsys_state *pos,
3188 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189{
Tejun Heo492eb212013-08-08 20:11:25 -04003190 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003191
Tejun Heo8353da12014-05-13 12:19:23 -04003192 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193
Tejun Heobd8815a2013-08-08 20:11:27 -04003194 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003195 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003196 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003197
3198 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003199 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003200 if (next)
3201 return next;
3202
3203 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003204 while (pos != root) {
3205 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003206 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003207 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003208 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003209 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003210
3211 return NULL;
3212}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003213
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003214/**
Tejun Heo492eb212013-08-08 20:11:25 -04003215 * css_rightmost_descendant - return the rightmost descendant of a css
3216 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003217 *
Tejun Heo492eb212013-08-08 20:11:25 -04003218 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3219 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003220 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003221 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003222 * While this function requires cgroup_mutex or RCU read locking, it
3223 * doesn't require the whole traversal to be contained in a single critical
3224 * section. This function will return the correct rightmost descendant as
3225 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003226 */
Tejun Heo492eb212013-08-08 20:11:25 -04003227struct cgroup_subsys_state *
3228css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003229{
Tejun Heo492eb212013-08-08 20:11:25 -04003230 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003231
Tejun Heo8353da12014-05-13 12:19:23 -04003232 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003233
3234 do {
3235 last = pos;
3236 /* ->prev isn't RCU safe, walk ->next till the end */
3237 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003238 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003239 pos = tmp;
3240 } while (pos);
3241
3242 return last;
3243}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003244
Tejun Heo492eb212013-08-08 20:11:25 -04003245static struct cgroup_subsys_state *
3246css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003247{
Tejun Heo492eb212013-08-08 20:11:25 -04003248 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003249
3250 do {
3251 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003252 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003253 } while (pos);
3254
3255 return last;
3256}
3257
3258/**
Tejun Heo492eb212013-08-08 20:11:25 -04003259 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003260 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003261 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003262 *
Tejun Heo492eb212013-08-08 20:11:25 -04003263 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003264 * to visit for post-order traversal of @root's descendants. @root is
3265 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003266 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003267 * While this function requires cgroup_mutex or RCU read locking, it
3268 * doesn't require the whole traversal to be contained in a single critical
3269 * section. This function will return the correct next descendant as long
3270 * as both @pos and @cgroup are accessible and @pos is a descendant of
3271 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003272 */
Tejun Heo492eb212013-08-08 20:11:25 -04003273struct cgroup_subsys_state *
3274css_next_descendant_post(struct cgroup_subsys_state *pos,
3275 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003276{
Tejun Heo492eb212013-08-08 20:11:25 -04003277 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003278
Tejun Heo8353da12014-05-13 12:19:23 -04003279 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003280
Tejun Heo58b79a92013-09-06 15:31:08 -04003281 /* if first iteration, visit leftmost descendant which may be @root */
3282 if (!pos)
3283 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003284
Tejun Heobd8815a2013-08-08 20:11:27 -04003285 /* if we visited @root, we're done */
3286 if (pos == root)
3287 return NULL;
3288
Tejun Heo574bd9f2012-11-09 09:12:29 -08003289 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003290 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003291 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003292 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003293
3294 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003295 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003296}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003297
Tejun Heocbc125e2014-05-14 09:15:01 -04003298static bool cgroup_has_live_children(struct cgroup *cgrp)
3299{
3300 struct cgroup *child;
3301
3302 rcu_read_lock();
3303 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3304 if (!cgroup_is_dead(child)) {
3305 rcu_read_unlock();
3306 return true;
3307 }
3308 }
3309 rcu_read_unlock();
3310 return false;
3311}
3312
Tejun Heo0942eee2013-08-08 20:11:26 -04003313/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003314 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003315 * @it: the iterator to advance
3316 *
3317 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003318 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003319static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003320{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003321 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003322 struct cgrp_cset_link *link;
3323 struct css_set *cset;
3324
3325 /* Advance to the next non-empty css_set */
3326 do {
3327 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003328 if (l == it->cset_head) {
3329 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003330 return;
3331 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003332
3333 if (it->ss) {
3334 cset = container_of(l, struct css_set,
3335 e_cset_node[it->ss->id]);
3336 } else {
3337 link = list_entry(l, struct cgrp_cset_link, cset_link);
3338 cset = link->cset;
3339 }
Tejun Heoc7561122014-02-25 10:04:01 -05003340 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3341
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003342 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003343
3344 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003345 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003346 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003347 it->task_pos = cset->mg_tasks.next;
3348
3349 it->tasks_head = &cset->tasks;
3350 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003351}
3352
Tejun Heo0942eee2013-08-08 20:11:26 -04003353/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003354 * css_task_iter_start - initiate task iteration
3355 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003356 * @it: the task iterator to use
3357 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003358 * Initiate iteration through the tasks of @css. The caller can call
3359 * css_task_iter_next() to walk through the tasks until the function
3360 * returns NULL. On completion of iteration, css_task_iter_end() must be
3361 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003362 *
3363 * Note that this function acquires a lock which is released when the
3364 * iteration finishes. The caller can't sleep while iteration is in
3365 * progress.
3366 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003367void css_task_iter_start(struct cgroup_subsys_state *css,
3368 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003369 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003370{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003371 /* no one should try to iterate before mounting cgroups */
3372 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003373
Tejun Heo96d365e2014-02-13 06:58:40 -05003374 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003375
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003376 it->ss = css->ss;
3377
3378 if (it->ss)
3379 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3380 else
3381 it->cset_pos = &css->cgroup->cset_links;
3382
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003383 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003384
Tejun Heo72ec7022013-08-08 20:11:26 -04003385 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003386}
3387
Tejun Heo0942eee2013-08-08 20:11:26 -04003388/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003389 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003390 * @it: the task iterator being iterated
3391 *
3392 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003393 * initialized via css_task_iter_start(). Returns NULL when the iteration
3394 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003395 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003396struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003397{
3398 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003399 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003400
3401 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003402 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003403 return NULL;
3404 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003405
3406 /*
3407 * Advance iterator to find next entry. cset->tasks is consumed
3408 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3409 * next cset.
3410 */
Paul Menage817929e2007-10-18 23:39:36 -07003411 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003412
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003413 if (l == it->tasks_head)
3414 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003415
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003416 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003417 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003418 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003419 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003420
Paul Menage817929e2007-10-18 23:39:36 -07003421 return res;
3422}
3423
Tejun Heo0942eee2013-08-08 20:11:26 -04003424/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003425 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003426 * @it: the task iterator to finish
3427 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003428 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003429 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003430void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003431 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003432{
Tejun Heo96d365e2014-02-13 06:58:40 -05003433 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003434}
3435
3436/**
3437 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3438 * @to: cgroup to which the tasks will be moved
3439 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003440 *
3441 * Locking rules between cgroup_post_fork() and the migration path
3442 * guarantee that, if a task is forking while being migrated, the new child
3443 * is guaranteed to be either visible in the source cgroup after the
3444 * parent's migration is complete or put into the target cgroup. No task
3445 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003446 */
3447int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3448{
Tejun Heo952aaa12014-02-25 10:04:03 -05003449 LIST_HEAD(preloaded_csets);
3450 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003451 struct css_task_iter it;
3452 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003453 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003454
Tejun Heo952aaa12014-02-25 10:04:03 -05003455 mutex_lock(&cgroup_mutex);
3456
3457 /* all tasks in @from are being moved, all csets are source */
3458 down_read(&css_set_rwsem);
3459 list_for_each_entry(link, &from->cset_links, cset_link)
3460 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3461 up_read(&css_set_rwsem);
3462
3463 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3464 if (ret)
3465 goto out_err;
3466
3467 /*
3468 * Migrate tasks one-by-one until @form is empty. This fails iff
3469 * ->can_attach() fails.
3470 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003471 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003472 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003473 task = css_task_iter_next(&it);
3474 if (task)
3475 get_task_struct(task);
3476 css_task_iter_end(&it);
3477
3478 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003479 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003480 put_task_struct(task);
3481 }
3482 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003483out_err:
3484 cgroup_migrate_finish(&preloaded_csets);
3485 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003486 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003487}
3488
Paul Menage817929e2007-10-18 23:39:36 -07003489/*
Ben Blum102a7752009-09-23 15:56:26 -07003490 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003491 *
3492 * Reading this file can return large amounts of data if a cgroup has
3493 * *lots* of attached tasks. So it may need several calls to read(),
3494 * but we cannot guarantee that the information we produce is correct
3495 * unless we produce it entirely atomically.
3496 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003497 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003498
Li Zefan24528252012-01-20 11:58:43 +08003499/* which pidlist file are we talking about? */
3500enum cgroup_filetype {
3501 CGROUP_FILE_PROCS,
3502 CGROUP_FILE_TASKS,
3503};
3504
3505/*
3506 * A pidlist is a list of pids that virtually represents the contents of one
3507 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3508 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3509 * to the cgroup.
3510 */
3511struct cgroup_pidlist {
3512 /*
3513 * used to find which pidlist is wanted. doesn't change as long as
3514 * this particular list stays in the list.
3515 */
3516 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3517 /* array of xids */
3518 pid_t *list;
3519 /* how many elements the above list has */
3520 int length;
Li Zefan24528252012-01-20 11:58:43 +08003521 /* each of these stored in a list by its cgroup */
3522 struct list_head links;
3523 /* pointer to the cgroup we belong to, for list removal purposes */
3524 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003525 /* for delayed destruction */
3526 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003527};
3528
Paul Menagebbcb81d2007-10-18 23:39:32 -07003529/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003530 * The following two functions "fix" the issue where there are more pids
3531 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3532 * TODO: replace with a kernel-wide solution to this problem
3533 */
3534#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3535static void *pidlist_allocate(int count)
3536{
3537 if (PIDLIST_TOO_LARGE(count))
3538 return vmalloc(count * sizeof(pid_t));
3539 else
3540 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3541}
Tejun Heob1a21362013-11-29 10:42:58 -05003542
Ben Blumd1d9fd32009-09-23 15:56:28 -07003543static void pidlist_free(void *p)
3544{
3545 if (is_vmalloc_addr(p))
3546 vfree(p);
3547 else
3548 kfree(p);
3549}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003550
3551/*
Tejun Heob1a21362013-11-29 10:42:58 -05003552 * Used to destroy all pidlists lingering waiting for destroy timer. None
3553 * should be left afterwards.
3554 */
3555static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3556{
3557 struct cgroup_pidlist *l, *tmp_l;
3558
3559 mutex_lock(&cgrp->pidlist_mutex);
3560 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3561 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3562 mutex_unlock(&cgrp->pidlist_mutex);
3563
3564 flush_workqueue(cgroup_pidlist_destroy_wq);
3565 BUG_ON(!list_empty(&cgrp->pidlists));
3566}
3567
3568static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3569{
3570 struct delayed_work *dwork = to_delayed_work(work);
3571 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3572 destroy_dwork);
3573 struct cgroup_pidlist *tofree = NULL;
3574
3575 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003576
3577 /*
Tejun Heo04502362013-11-29 10:42:59 -05003578 * Destroy iff we didn't get queued again. The state won't change
3579 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003580 */
Tejun Heo04502362013-11-29 10:42:59 -05003581 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003582 list_del(&l->links);
3583 pidlist_free(l->list);
3584 put_pid_ns(l->key.ns);
3585 tofree = l;
3586 }
3587
Tejun Heob1a21362013-11-29 10:42:58 -05003588 mutex_unlock(&l->owner->pidlist_mutex);
3589 kfree(tofree);
3590}
3591
3592/*
Ben Blum102a7752009-09-23 15:56:26 -07003593 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003594 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003595 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003596static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003597{
Ben Blum102a7752009-09-23 15:56:26 -07003598 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003599
3600 /*
3601 * we presume the 0th element is unique, so i starts at 1. trivial
3602 * edge cases first; no work needs to be done for either
3603 */
3604 if (length == 0 || length == 1)
3605 return length;
3606 /* src and dest walk down the list; dest counts unique elements */
3607 for (src = 1; src < length; src++) {
3608 /* find next unique element */
3609 while (list[src] == list[src-1]) {
3610 src++;
3611 if (src == length)
3612 goto after;
3613 }
3614 /* dest always points to where the next unique element goes */
3615 list[dest] = list[src];
3616 dest++;
3617 }
3618after:
Ben Blum102a7752009-09-23 15:56:26 -07003619 return dest;
3620}
3621
Tejun Heoafb2bc12013-11-29 10:42:59 -05003622/*
3623 * The two pid files - task and cgroup.procs - guaranteed that the result
3624 * is sorted, which forced this whole pidlist fiasco. As pid order is
3625 * different per namespace, each namespace needs differently sorted list,
3626 * making it impossible to use, for example, single rbtree of member tasks
3627 * sorted by task pointer. As pidlists can be fairly large, allocating one
3628 * per open file is dangerous, so cgroup had to implement shared pool of
3629 * pidlists keyed by cgroup and namespace.
3630 *
3631 * All this extra complexity was caused by the original implementation
3632 * committing to an entirely unnecessary property. In the long term, we
3633 * want to do away with it. Explicitly scramble sort order if
3634 * sane_behavior so that no such expectation exists in the new interface.
3635 *
3636 * Scrambling is done by swapping every two consecutive bits, which is
3637 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3638 */
3639static pid_t pid_fry(pid_t pid)
3640{
3641 unsigned a = pid & 0x55555555;
3642 unsigned b = pid & 0xAAAAAAAA;
3643
3644 return (a << 1) | (b >> 1);
3645}
3646
3647static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3648{
3649 if (cgroup_sane_behavior(cgrp))
3650 return pid_fry(pid);
3651 else
3652 return pid;
3653}
3654
Ben Blum102a7752009-09-23 15:56:26 -07003655static int cmppid(const void *a, const void *b)
3656{
3657 return *(pid_t *)a - *(pid_t *)b;
3658}
3659
Tejun Heoafb2bc12013-11-29 10:42:59 -05003660static int fried_cmppid(const void *a, const void *b)
3661{
3662 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3663}
3664
Ben Blum72a8cb32009-09-23 15:56:27 -07003665static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3666 enum cgroup_filetype type)
3667{
3668 struct cgroup_pidlist *l;
3669 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003670 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003671
Tejun Heoe6b81712013-11-29 10:42:59 -05003672 lockdep_assert_held(&cgrp->pidlist_mutex);
3673
3674 list_for_each_entry(l, &cgrp->pidlists, links)
3675 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003676 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003677 return NULL;
3678}
3679
Ben Blum72a8cb32009-09-23 15:56:27 -07003680/*
3681 * find the appropriate pidlist for our purpose (given procs vs tasks)
3682 * returns with the lock on that pidlist already held, and takes care
3683 * of the use count, or returns NULL with no locks held if we're out of
3684 * memory.
3685 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003686static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3687 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003688{
3689 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003690
Tejun Heoe6b81712013-11-29 10:42:59 -05003691 lockdep_assert_held(&cgrp->pidlist_mutex);
3692
3693 l = cgroup_pidlist_find(cgrp, type);
3694 if (l)
3695 return l;
3696
Ben Blum72a8cb32009-09-23 15:56:27 -07003697 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003698 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003699 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003700 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003701
Tejun Heob1a21362013-11-29 10:42:58 -05003702 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003703 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003704 /* don't need task_nsproxy() if we're looking at ourself */
3705 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003706 l->owner = cgrp;
3707 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003708 return l;
3709}
3710
3711/*
Ben Blum102a7752009-09-23 15:56:26 -07003712 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3713 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003714static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3715 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003716{
3717 pid_t *array;
3718 int length;
3719 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003720 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003721 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003722 struct cgroup_pidlist *l;
3723
Tejun Heo4bac00d2013-11-29 10:42:59 -05003724 lockdep_assert_held(&cgrp->pidlist_mutex);
3725
Ben Blum102a7752009-09-23 15:56:26 -07003726 /*
3727 * If cgroup gets more users after we read count, we won't have
3728 * enough space - tough. This race is indistinguishable to the
3729 * caller from the case that the additional cgroup users didn't
3730 * show up until sometime later on.
3731 */
3732 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003733 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003734 if (!array)
3735 return -ENOMEM;
3736 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003737 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003738 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003739 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003740 break;
Ben Blum102a7752009-09-23 15:56:26 -07003741 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003742 if (type == CGROUP_FILE_PROCS)
3743 pid = task_tgid_vnr(tsk);
3744 else
3745 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003746 if (pid > 0) /* make sure to only use valid results */
3747 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003748 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003749 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003750 length = n;
3751 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003752 if (cgroup_sane_behavior(cgrp))
3753 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3754 else
3755 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003756 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003757 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003758
Tejun Heoe6b81712013-11-29 10:42:59 -05003759 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003760 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003761 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003762 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003763 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003764 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003765
3766 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003767 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003768 l->list = array;
3769 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003770 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003771 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003772}
3773
Balbir Singh846c7bb2007-10-18 23:39:44 -07003774/**
Li Zefana043e3b2008-02-23 15:24:09 -08003775 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003776 * @stats: cgroupstats to fill information into
3777 * @dentry: A dentry entry belonging to the cgroup for which stats have
3778 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003779 *
3780 * Build and fill cgroupstats so that taskstats can export it to user
3781 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003782 */
3783int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3784{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003785 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003786 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003787 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003788 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003789
Tejun Heo2bd59d42014-02-11 11:52:49 -05003790 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3791 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3792 kernfs_type(kn) != KERNFS_DIR)
3793 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003794
Li Zefanbad34662014-02-14 16:54:28 +08003795 mutex_lock(&cgroup_mutex);
3796
Tejun Heo2bd59d42014-02-11 11:52:49 -05003797 /*
3798 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003799 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003800 * @kn->priv is RCU safe. Let's do the RCU dancing.
3801 */
3802 rcu_read_lock();
3803 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003804 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003805 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003806 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003807 return -ENOENT;
3808 }
Li Zefanbad34662014-02-14 16:54:28 +08003809 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003810
Tejun Heo9d800df2014-05-14 09:15:00 -04003811 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003812 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003813 switch (tsk->state) {
3814 case TASK_RUNNING:
3815 stats->nr_running++;
3816 break;
3817 case TASK_INTERRUPTIBLE:
3818 stats->nr_sleeping++;
3819 break;
3820 case TASK_UNINTERRUPTIBLE:
3821 stats->nr_uninterruptible++;
3822 break;
3823 case TASK_STOPPED:
3824 stats->nr_stopped++;
3825 break;
3826 default:
3827 if (delayacct_is_task_waiting_on_io(tsk))
3828 stats->nr_io_wait++;
3829 break;
3830 }
3831 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003832 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003833
Li Zefanbad34662014-02-14 16:54:28 +08003834 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003835 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003836}
3837
Paul Menage8f3ff202009-09-23 15:56:25 -07003838
Paul Menagecc31edc2008-10-18 20:28:04 -07003839/*
Ben Blum102a7752009-09-23 15:56:26 -07003840 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003841 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003842 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003843 */
3844
Ben Blum102a7752009-09-23 15:56:26 -07003845static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003846{
3847 /*
3848 * Initially we receive a position value that corresponds to
3849 * one more than the last pid shown (or 0 on the first call or
3850 * after a seek to the start). Use a binary-search to find the
3851 * next pid to display, if any
3852 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003853 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003854 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003855 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003856 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003857 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003858 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003859
Tejun Heo4bac00d2013-11-29 10:42:59 -05003860 mutex_lock(&cgrp->pidlist_mutex);
3861
3862 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003863 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003864 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003865 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003866 * could already have been destroyed.
3867 */
Tejun Heo5d224442013-12-05 12:28:04 -05003868 if (of->priv)
3869 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003870
3871 /*
3872 * Either this is the first start() after open or the matching
3873 * pidlist has been destroyed inbetween. Create a new one.
3874 */
Tejun Heo5d224442013-12-05 12:28:04 -05003875 if (!of->priv) {
3876 ret = pidlist_array_load(cgrp, type,
3877 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003878 if (ret)
3879 return ERR_PTR(ret);
3880 }
Tejun Heo5d224442013-12-05 12:28:04 -05003881 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003882
Paul Menagecc31edc2008-10-18 20:28:04 -07003883 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003884 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003885
Paul Menagecc31edc2008-10-18 20:28:04 -07003886 while (index < end) {
3887 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003888 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003889 index = mid;
3890 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003891 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003892 index = mid + 1;
3893 else
3894 end = mid;
3895 }
3896 }
3897 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003898 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003899 return NULL;
3900 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003901 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003902 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003903 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003904}
3905
Ben Blum102a7752009-09-23 15:56:26 -07003906static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003907{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003908 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003909 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003910
Tejun Heo5d224442013-12-05 12:28:04 -05003911 if (l)
3912 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003913 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003914 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003915}
3916
Ben Blum102a7752009-09-23 15:56:26 -07003917static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003918{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003919 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003920 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003921 pid_t *p = v;
3922 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003923 /*
3924 * Advance to the next pid in the array. If this goes off the
3925 * end, we're done
3926 */
3927 p++;
3928 if (p >= end) {
3929 return NULL;
3930 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003931 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003932 return p;
3933 }
3934}
3935
Ben Blum102a7752009-09-23 15:56:26 -07003936static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003937{
3938 return seq_printf(s, "%d\n", *(int *)v);
3939}
3940
Tejun Heo182446d2013-08-08 20:11:24 -04003941static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3942 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003943{
Tejun Heo182446d2013-08-08 20:11:24 -04003944 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003945}
3946
Tejun Heo182446d2013-08-08 20:11:24 -04003947static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3948 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003949{
Tejun Heo182446d2013-08-08 20:11:24 -04003950 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003951 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003952 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003953 else
Tejun Heo182446d2013-08-08 20:11:24 -04003954 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003955 return 0;
3956}
3957
Tejun Heo182446d2013-08-08 20:11:24 -04003958static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3959 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003960{
Tejun Heo182446d2013-08-08 20:11:24 -04003961 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003962}
3963
Tejun Heo182446d2013-08-08 20:11:24 -04003964static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3965 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003966{
3967 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003968 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003969 else
Tejun Heo182446d2013-08-08 20:11:24 -04003970 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003971 return 0;
3972}
3973
Tejun Heod5c56ce2013-06-03 19:14:34 -07003974static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003975 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003976 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003977 .seq_start = cgroup_pidlist_start,
3978 .seq_next = cgroup_pidlist_next,
3979 .seq_stop = cgroup_pidlist_stop,
3980 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003981 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003982 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003983 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003984 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003985 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003986 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003987 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003988 .read_u64 = cgroup_clone_children_read,
3989 .write_u64 = cgroup_clone_children_write,
3990 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003991 {
Tejun Heo873fe092013-04-14 20:15:26 -07003992 .name = "cgroup.sane_behavior",
3993 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003994 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003995 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003996 {
3997 .name = "cgroup.controllers",
3998 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3999 .seq_show = cgroup_root_controllers_show,
4000 },
4001 {
4002 .name = "cgroup.controllers",
4003 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4004 .seq_show = cgroup_controllers_show,
4005 },
4006 {
4007 .name = "cgroup.subtree_control",
4008 .flags = CFTYPE_ONLY_ON_DFL,
4009 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004010 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004011 },
Tejun Heo842b5972014-04-25 18:28:02 -04004012 {
4013 .name = "cgroup.populated",
4014 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4015 .seq_show = cgroup_populated_show,
4016 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004017
4018 /*
4019 * Historical crazy stuff. These don't have "cgroup." prefix and
4020 * don't exist if sane_behavior. If you're depending on these, be
4021 * prepared to be burned.
4022 */
4023 {
4024 .name = "tasks",
4025 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004026 .seq_start = cgroup_pidlist_start,
4027 .seq_next = cgroup_pidlist_next,
4028 .seq_stop = cgroup_pidlist_stop,
4029 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004030 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004031 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004032 .mode = S_IRUGO | S_IWUSR,
4033 },
4034 {
4035 .name = "notify_on_release",
4036 .flags = CFTYPE_INSANE,
4037 .read_u64 = cgroup_read_notify_on_release,
4038 .write_u64 = cgroup_write_notify_on_release,
4039 },
Tejun Heo873fe092013-04-14 20:15:26 -07004040 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004041 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004042 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004043 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004044 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004045 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004046 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004047 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004048};
4049
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004050/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004051 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004052 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004053 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004054 *
4055 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004056 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004057static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004058{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004059 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004060 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004061
Tejun Heo8e3f6542012-04-01 12:09:55 -07004062 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004063 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004064 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004065
Tejun Heo69dfa002014-05-04 15:09:13 -04004066 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004067 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004068
Tejun Heo0adb0702014-02-12 09:29:48 -05004069 list_for_each_entry(cfts, &ss->cfts, node) {
4070 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004071 if (ret < 0)
4072 goto err;
4073 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004074 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004075 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004076err:
4077 cgroup_clear_dir(cgrp, subsys_mask);
4078 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079}
4080
Tejun Heo0c21ead2013-08-13 20:22:51 -04004081/*
4082 * css destruction is four-stage process.
4083 *
4084 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4085 * Implemented in kill_css().
4086 *
4087 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004088 * and thus css_tryget_online() is guaranteed to fail, the css can be
4089 * offlined by invoking offline_css(). After offlining, the base ref is
4090 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004091 *
4092 * 3. When the percpu_ref reaches zero, the only possible remaining
4093 * accessors are inside RCU read sections. css_release() schedules the
4094 * RCU callback.
4095 *
4096 * 4. After the grace period, the css can be freed. Implemented in
4097 * css_free_work_fn().
4098 *
4099 * It is actually hairier because both step 2 and 4 require process context
4100 * and thus involve punting to css->destroy_work adding two additional
4101 * steps to the already complex sequence.
4102 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004103static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004104{
4105 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004106 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004107 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004108
Tejun Heo0ae78e02013-08-13 11:01:54 -04004109 if (css->parent)
4110 css_put(css->parent);
4111
Tejun Heo0c21ead2013-08-13 20:22:51 -04004112 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004113 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004114}
4115
4116static void css_free_rcu_fn(struct rcu_head *rcu_head)
4117{
4118 struct cgroup_subsys_state *css =
4119 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4120
Tejun Heo0c21ead2013-08-13 20:22:51 -04004121 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004122 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004123}
4124
Tejun Heod3daf282013-06-13 19:39:16 -07004125static void css_release(struct percpu_ref *ref)
4126{
4127 struct cgroup_subsys_state *css =
4128 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo15a4c832014-05-04 15:09:14 -04004129 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004130
Tejun Heo15a4c832014-05-04 15:09:14 -04004131 cgroup_idr_remove(&ss->css_idr, css->id);
4132
Tejun Heo0c21ead2013-08-13 20:22:51 -04004133 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004134}
4135
Tejun Heoddfcada2014-05-04 15:09:14 -04004136static void init_and_link_css(struct cgroup_subsys_state *css,
4137 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138{
Tejun Heoddfcada2014-05-04 15:09:14 -04004139 cgroup_get(cgrp);
4140
Paul Menagebd89aab2007-10-18 23:40:44 -07004141 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004142 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004144
Tejun Heoddfcada2014-05-04 15:09:14 -04004145 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004146 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004147 css_get(css->parent);
4148 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004150 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004151
Tejun Heoca8bdca2013-08-26 18:40:56 -04004152 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004153}
4154
Li Zefan2a4ac632013-07-31 16:16:40 +08004155/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004156static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004157{
Tejun Heo623f9262013-08-13 11:01:55 -04004158 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004159 int ret = 0;
4160
Tejun Heoa31f2d32012-11-19 08:13:37 -08004161 lockdep_assert_held(&cgroup_mutex);
4162
Tejun Heo92fb9742012-11-19 08:13:38 -08004163 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004164 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004165 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004166 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004167 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004168 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004169 }
Tejun Heob1929db2012-11-19 08:13:38 -08004170 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004171}
4172
Li Zefan2a4ac632013-07-31 16:16:40 +08004173/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004174static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004175{
Tejun Heo623f9262013-08-13 11:01:55 -04004176 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004177
4178 lockdep_assert_held(&cgroup_mutex);
4179
4180 if (!(css->flags & CSS_ONLINE))
4181 return;
4182
Li Zefand7eeac12013-03-12 15:35:59 -07004183 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004184 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004185
Tejun Heoeb954192013-08-08 20:11:23 -04004186 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004187 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004188 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004189
4190 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004191}
4192
Tejun Heoc81c925a2013-12-06 15:11:56 -05004193/**
4194 * create_css - create a cgroup_subsys_state
4195 * @cgrp: the cgroup new css will be associated with
4196 * @ss: the subsys of new css
4197 *
4198 * Create a new css associated with @cgrp - @ss pair. On success, the new
4199 * css is online and installed in @cgrp with all interface files created.
4200 * Returns 0 on success, -errno on failure.
4201 */
4202static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4203{
4204 struct cgroup *parent = cgrp->parent;
4205 struct cgroup_subsys_state *css;
4206 int err;
4207
Tejun Heoc81c925a2013-12-06 15:11:56 -05004208 lockdep_assert_held(&cgroup_mutex);
4209
4210 css = ss->css_alloc(cgroup_css(parent, ss));
4211 if (IS_ERR(css))
4212 return PTR_ERR(css);
4213
Tejun Heoddfcada2014-05-04 15:09:14 -04004214 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004215
Tejun Heoc81c925a2013-12-06 15:11:56 -05004216 err = percpu_ref_init(&css->refcnt, css_release);
4217 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004218 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004219
Tejun Heo15a4c832014-05-04 15:09:14 -04004220 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4221 if (err < 0)
4222 goto err_free_percpu_ref;
4223 css->id = err;
4224
Tejun Heoaec25022014-02-08 10:36:58 -05004225 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004226 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004227 goto err_free_id;
4228
4229 /* @css is ready to be brought online now, make it visible */
4230 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004231
4232 err = online_css(css);
4233 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004234 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004235
Tejun Heoc81c925a2013-12-06 15:11:56 -05004236 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4237 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004238 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 -04004239 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004240 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004241 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004242 ss->warned_broken_hierarchy = true;
4243 }
4244
4245 return 0;
4246
Li Zefan3eb59ec2014-03-18 17:02:36 +08004247err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004248 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004249err_free_id:
4250 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004251err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004252 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004253err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004254 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004255 return err;
4256}
4257
Tejun Heob3bfd982014-05-13 12:19:22 -04004258static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4259 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260{
Tejun Heoa9746d82014-05-13 12:19:22 -04004261 struct cgroup *parent, *cgrp;
4262 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004264 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004265 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004266
Tejun Heoa9746d82014-05-13 12:19:22 -04004267 parent = cgroup_kn_lock_live(parent_kn);
4268 if (!parent)
4269 return -ENODEV;
4270 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004271
4272 /* allocate the cgroup and its ID, 0 is reserved for the root */
4273 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4274 if (!cgrp) {
4275 ret = -ENOMEM;
4276 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004277 }
4278
4279 /*
4280 * Temporarily set the pointer to NULL, so idr_find() won't return
4281 * a half-baked cgroup.
4282 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004283 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004284 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004285 ret = -ENOMEM;
4286 goto out_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004287 }
4288
Paul Menagecc31edc2008-10-18 20:28:04 -07004289 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004290
Paul Menagebd89aab2007-10-18 23:40:44 -07004291 cgrp->parent = parent;
Tejun Heo9d800df2014-05-14 09:15:00 -04004292 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004293 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294
Li Zefanb6abdb02008-03-04 14:28:19 -08004295 if (notify_on_release(parent))
4296 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4297
Tejun Heo2260e7f2012-11-19 08:13:38 -08004298 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4299 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004300
Tejun Heo2bd59d42014-02-11 11:52:49 -05004301 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004302 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004303 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004304 ret = PTR_ERR(kn);
4305 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004306 }
4307 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004308
Tejun Heo6f305582014-02-12 09:29:50 -05004309 /*
4310 * This extra ref will be put in cgroup_free_fn() and guarantees
4311 * that @cgrp->kn is always accessible.
4312 */
4313 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314
Tejun Heo00356bd2013-06-18 11:14:22 -07004315 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004316
Tejun Heo4e139af2012-11-19 08:13:36 -08004317 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004318 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004319 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004320 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004321
Tejun Heo0d802552013-12-06 15:11:56 -05004322 /*
4323 * @cgrp is now fully operational. If something fails after this
4324 * point, it'll be released via the normal destruction path.
4325 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004326 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004327
Tejun Heoba0f4d72014-05-13 12:19:22 -04004328 ret = cgroup_kn_set_ugid(kn);
4329 if (ret)
4330 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004331
Tejun Heoba0f4d72014-05-13 12:19:22 -04004332 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4333 if (ret)
4334 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004335
Tejun Heo9d403e92013-12-06 15:11:56 -05004336 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004337 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004338 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004339 ret = create_css(cgrp, ss);
4340 if (ret)
4341 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004342 }
Tejun Heoa8638032012-11-09 09:12:29 -08004343 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344
Tejun Heobd53d612014-04-23 11:13:16 -04004345 /*
4346 * On the default hierarchy, a child doesn't automatically inherit
4347 * child_subsys_mask from the parent. Each is configured manually.
4348 */
4349 if (!cgroup_on_dfl(cgrp))
4350 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004351
Tejun Heo2bd59d42014-02-11 11:52:49 -05004352 kernfs_activate(kn);
4353
Tejun Heoba0f4d72014-05-13 12:19:22 -04004354 ret = 0;
4355 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356
Tejun Heoba0f4d72014-05-13 12:19:22 -04004357out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004358 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004359out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004360 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004361out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004362 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004363 return ret;
4364
4365out_destroy:
4366 cgroup_destroy_locked(cgrp);
4367 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368}
4369
Tejun Heo223dbc32013-08-13 20:22:50 -04004370/*
4371 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heoec903c02014-05-13 12:11:01 -04004372 * css_tryget_online() is now guaranteed to fail.
Tejun Heo223dbc32013-08-13 20:22:50 -04004373 */
4374static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004375{
Tejun Heo223dbc32013-08-13 20:22:50 -04004376 struct cgroup_subsys_state *css =
4377 container_of(work, struct cgroup_subsys_state, destroy_work);
4378 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004379
Tejun Heof20104d2013-08-13 20:22:50 -04004380 mutex_lock(&cgroup_mutex);
4381
4382 /*
Tejun Heoec903c02014-05-13 12:11:01 -04004383 * css_tryget_online() is guaranteed to fail now. Tell subsystems
4384 * to initate destruction.
Tejun Heo09a503ea2013-08-13 20:22:50 -04004385 */
4386 offline_css(css);
4387
4388 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004389 * If @cgrp is marked dead, it's waiting for refs of all css's to
4390 * be disabled before proceeding to the second phase of cgroup
4391 * destruction. If we are the last one, kick it off.
4392 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004393 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004394 cgroup_destroy_css_killed(cgrp);
4395
4396 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004397
4398 /*
4399 * Put the css refs from kill_css(). Each css holds an extra
4400 * reference to the cgroup's dentry and cgroup removal proceeds
4401 * regardless of css refs. On the last put of each css, whenever
4402 * that may be, the extra dentry ref is put so that dentry
4403 * destruction happens only after all css's are released.
4404 */
4405 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004406}
4407
Tejun Heo223dbc32013-08-13 20:22:50 -04004408/* css kill confirmation processing requires process context, bounce */
4409static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004410{
4411 struct cgroup_subsys_state *css =
4412 container_of(ref, struct cgroup_subsys_state, refcnt);
4413
Tejun Heo223dbc32013-08-13 20:22:50 -04004414 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004415 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004416}
4417
Tejun Heof392e512014-04-23 11:13:14 -04004418/**
4419 * kill_css - destroy a css
4420 * @css: css to destroy
4421 *
4422 * This function initiates destruction of @css by removing cgroup interface
4423 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004424 * asynchronously once css_tryget_online() is guaranteed to fail and when
4425 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004426 */
4427static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004428{
Tejun Heo01f64742014-05-13 12:19:23 -04004429 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004430
Tejun Heo2bd59d42014-02-11 11:52:49 -05004431 /*
4432 * This must happen before css is disassociated with its cgroup.
4433 * See seq_css() for details.
4434 */
Tejun Heoaec25022014-02-08 10:36:58 -05004435 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004436
Tejun Heoedae0c32013-08-13 20:22:51 -04004437 /*
4438 * Killing would put the base ref, but we need to keep it alive
4439 * until after ->css_offline().
4440 */
4441 css_get(css);
4442
4443 /*
4444 * cgroup core guarantees that, by the time ->css_offline() is
4445 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004446 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004447 * proceed to offlining css's because percpu_ref_kill() doesn't
4448 * guarantee that the ref is seen as killed on all CPUs on return.
4449 *
4450 * Use percpu_ref_kill_and_confirm() to get notifications as each
4451 * css is confirmed to be seen as killed on all CPUs.
4452 */
4453 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004454}
4455
4456/**
4457 * cgroup_destroy_locked - the first stage of cgroup destruction
4458 * @cgrp: cgroup to be destroyed
4459 *
4460 * css's make use of percpu refcnts whose killing latency shouldn't be
4461 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004462 * guarantee that css_tryget_online() won't succeed by the time
4463 * ->css_offline() is invoked. To satisfy all the requirements,
4464 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004465 *
4466 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4467 * userland visible parts and start killing the percpu refcnts of
4468 * css's. Set up so that the next stage will be kicked off once all
4469 * the percpu refcnts are confirmed to be killed.
4470 *
4471 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4472 * rest of destruction. Once all cgroup references are gone, the
4473 * cgroup is RCU-freed.
4474 *
4475 * This function implements s1. After this step, @cgrp is gone as far as
4476 * the userland is concerned and a new cgroup with the same name may be
4477 * created. As cgroup doesn't care about the names internally, this
4478 * doesn't cause any problem.
4479 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004480static int cgroup_destroy_locked(struct cgroup *cgrp)
4481 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004483 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004484 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004485 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004486
Tejun Heo42809dd2012-11-19 08:13:37 -08004487 lockdep_assert_held(&cgroup_mutex);
4488
Tejun Heoddd69142013-06-12 21:04:54 -07004489 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004490 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004491 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004492 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004493 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004494 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004495 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004496 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004498
Tejun Heo1a90dd52012-11-05 09:16:59 -08004499 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004500 * Make sure there's no live children. We can't test ->children
4501 * emptiness as dead children linger on it while being destroyed;
4502 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4503 */
Tejun Heocbc125e2014-05-14 09:15:01 -04004504 if (cgroup_has_live_children(cgrp))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004505 return -EBUSY;
4506
4507 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004508 * Mark @cgrp dead. This prevents further task migration and child
4509 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004510 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004511 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004512 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004513 */
Tejun Heo54766d42013-06-12 21:04:53 -07004514 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004515
Tejun Heo5d773812014-03-19 10:23:53 -04004516 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004517 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4518 * will be invoked to perform the rest of destruction once the
Tejun Heo01f64742014-05-13 12:19:23 -04004519 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004520 */
4521 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004522 kill_css(css);
4523
Tejun Heo455050d2013-06-13 19:27:41 -07004524 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4525 raw_spin_lock(&release_list_lock);
4526 if (!list_empty(&cgrp->release_list))
4527 list_del_init(&cgrp->release_list);
4528 raw_spin_unlock(&release_list_lock);
4529
4530 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004531 * If @cgrp has css's attached, the second stage of cgroup
4532 * destruction is kicked off from css_killed_work_fn() after the
4533 * refs of all attached css's are killed. If @cgrp doesn't have
4534 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004535 */
Tejun Heof20104d2013-08-13 20:22:50 -04004536 if (!cgrp->nr_css)
4537 cgroup_destroy_css_killed(cgrp);
4538
Tejun Heo01f64742014-05-13 12:19:23 -04004539 /*
4540 * Remove @cgrp directory along with the base files. @cgrp has an
4541 * extra ref on its kn.
4542 */
4543 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004544
Tejun Heoea15f8c2013-06-13 19:27:42 -07004545 return 0;
4546};
4547
Tejun Heod3daf282013-06-13 19:39:16 -07004548/**
Tejun Heof20104d2013-08-13 20:22:50 -04004549 * cgroup_destroy_css_killed - the second step of cgroup destruction
Fabian Frederick60106942014-05-05 20:08:13 +02004550 * @cgrp: the cgroup whose csses have just finished offlining
Tejun Heod3daf282013-06-13 19:39:16 -07004551 *
4552 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004553 * destroyed after all css's are offlined and performs the rest of
4554 * destruction. This is the second step of destruction described in the
4555 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004556 */
Tejun Heof20104d2013-08-13 20:22:50 -04004557static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004558{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004559 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004560
Tejun Heof20104d2013-08-13 20:22:50 -04004561 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004562
Paul Menage999cd8a2009-01-07 18:08:36 -08004563 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004564 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004565
Tejun Heo59f52962014-02-11 11:52:49 -05004566 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004567
Paul Menagebd89aab2007-10-18 23:40:44 -07004568 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004569 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004570}
4571
Tejun Heo2bd59d42014-02-11 11:52:49 -05004572static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004573{
Tejun Heoa9746d82014-05-13 12:19:22 -04004574 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004575 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004576
Tejun Heoa9746d82014-05-13 12:19:22 -04004577 cgrp = cgroup_kn_lock_live(kn);
4578 if (!cgrp)
4579 return 0;
4580 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004581
Tejun Heoa9746d82014-05-13 12:19:22 -04004582 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004583
Tejun Heoa9746d82014-05-13 12:19:22 -04004584 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004585
4586 /*
4587 * There are two control paths which try to determine cgroup from
4588 * dentry without going through kernfs - cgroupstats_build() and
4589 * css_tryget_online_from_dir(). Those are supported by RCU
4590 * protecting clearing of cgrp->kn->priv backpointer, which should
4591 * happen after all files under it have been removed.
4592 */
4593 if (!ret)
4594 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4595
Tejun Heo2bd59d42014-02-11 11:52:49 -05004596 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004597 return ret;
4598}
4599
Tejun Heo2bd59d42014-02-11 11:52:49 -05004600static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4601 .remount_fs = cgroup_remount,
4602 .show_options = cgroup_show_options,
4603 .mkdir = cgroup_mkdir,
4604 .rmdir = cgroup_rmdir,
4605 .rename = cgroup_rename,
4606};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004607
Tejun Heo15a4c832014-05-04 15:09:14 -04004608static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004611
4612 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613
Tejun Heo648bb562012-11-19 08:13:36 -08004614 mutex_lock(&cgroup_mutex);
4615
Tejun Heo15a4c832014-05-04 15:09:14 -04004616 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004617 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004618
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004619 /* Create the root cgroup state for this subsystem */
4620 ss->root = &cgrp_dfl_root;
4621 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004622 /* We don't handle early failures gracefully */
4623 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004624 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004625 if (early) {
4626 /* idr_alloc() can't be called safely during early init */
4627 css->id = 1;
4628 } else {
4629 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4630 BUG_ON(css->id < 0);
4631 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
Li Zefane8d55fd2008-04-29 01:00:13 -07004633 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004634 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004635 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004636 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004637 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638
4639 need_forkexit_callback |= ss->fork || ss->exit;
4640
Li Zefane8d55fd2008-04-29 01:00:13 -07004641 /* At system boot, before all subsystems have been
4642 * registered, no tasks have been forked, so we don't
4643 * need to invoke fork callbacks here. */
4644 BUG_ON(!list_empty(&init_task.tasks));
4645
Tejun Heoae7f1642013-08-13 20:22:50 -04004646 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004647
Tejun Heof392e512014-04-23 11:13:14 -04004648 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004649
Ben Blume6a11052010-03-10 15:22:09 -08004650 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004651}
4652
4653/**
Li Zefana043e3b2008-02-23 15:24:09 -08004654 * cgroup_init_early - cgroup initialization at system boot
4655 *
4656 * Initialize cgroups at system boot, and initialize any
4657 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658 */
4659int __init cgroup_init_early(void)
4660{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004661 static struct cgroup_sb_opts __initdata opts =
4662 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004663 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004664 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004665
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004666 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004667 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004668
Tejun Heo3ed80a62014-02-08 10:36:58 -05004669 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004670 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004671 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4672 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004673 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004674 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4675 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676
Tejun Heoaec25022014-02-08 10:36:58 -05004677 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004678 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679
4680 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004681 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004682 }
4683 return 0;
4684}
4685
4686/**
Li Zefana043e3b2008-02-23 15:24:09 -08004687 * cgroup_init - cgroup initialization
4688 *
4689 * Register cgroup filesystem and /proc file, and initialize
4690 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691 */
4692int __init cgroup_init(void)
4693{
Tejun Heo30159ec2013-06-25 11:53:37 -07004694 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004695 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004696 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004697
Tejun Heo2bd59d42014-02-11 11:52:49 -05004698 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004699
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004700 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004701
Tejun Heo82fe9b02013-06-25 11:53:37 -07004702 /* Add init_css_set to the hash table */
4703 key = css_set_hash(init_css_set.subsys);
4704 hash_add(css_set_table, &init_css_set.hlist, key);
4705
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004706 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004707
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004708 mutex_unlock(&cgroup_mutex);
4709
Tejun Heo172a2c062014-03-19 10:23:53 -04004710 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004711 if (ss->early_init) {
4712 struct cgroup_subsys_state *css =
4713 init_css_set.subsys[ss->id];
4714
4715 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4716 GFP_KERNEL);
4717 BUG_ON(css->id < 0);
4718 } else {
4719 cgroup_init_subsys(ss, false);
4720 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004721
Tejun Heo2d8f2432014-04-23 11:13:15 -04004722 list_add_tail(&init_css_set.e_cset_node[ssid],
4723 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4724
Tejun Heo172a2c062014-03-19 10:23:53 -04004725 /*
4726 * cftype registration needs kmalloc and can't be done
4727 * during early_init. Register base cftypes separately.
4728 */
4729 if (ss->base_cftypes)
4730 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4731 }
Greg KH676db4a2010-08-05 13:53:35 -07004732
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004734 if (!cgroup_kobj)
4735 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004736
4737 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004738 if (err < 0) {
4739 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004740 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004741 }
4742
4743 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004744 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004745}
Paul Menageb4f48b62007-10-18 23:39:33 -07004746
Tejun Heoe5fca242013-11-22 17:14:39 -05004747static int __init cgroup_wq_init(void)
4748{
4749 /*
4750 * There isn't much point in executing destruction path in
4751 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004752 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004753 *
4754 * We would prefer to do this in cgroup_init() above, but that
4755 * is called before init_workqueues(): so leave this until after.
4756 */
Tejun Heo1a115332014-02-12 19:06:19 -05004757 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004758 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004759
4760 /*
4761 * Used to destroy pidlists and separate to serve as flush domain.
4762 * Cap @max_active to 1 too.
4763 */
4764 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4765 0, 1);
4766 BUG_ON(!cgroup_pidlist_destroy_wq);
4767
Tejun Heoe5fca242013-11-22 17:14:39 -05004768 return 0;
4769}
4770core_initcall(cgroup_wq_init);
4771
Paul Menagea4243162007-10-18 23:39:35 -07004772/*
4773 * proc_cgroup_show()
4774 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4775 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004776 */
4777
4778/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004779int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004780{
4781 struct pid *pid;
4782 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004783 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004784 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004785 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004786
4787 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004788 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004789 if (!buf)
4790 goto out;
4791
4792 retval = -ESRCH;
4793 pid = m->private;
4794 tsk = get_pid_task(pid, PIDTYPE_PID);
4795 if (!tsk)
4796 goto out_free;
4797
4798 retval = 0;
4799
4800 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004801 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004802
Tejun Heo985ed672014-03-19 10:23:53 -04004803 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004804 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004805 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004806 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004807
Tejun Heoa2dd4242014-03-19 10:23:55 -04004808 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004809 continue;
4810
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004811 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004812 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004813 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004814 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004815 if (strlen(root->name))
4816 seq_printf(m, "%sname=%s", count ? "," : "",
4817 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004818 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004819 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004820 path = cgroup_path(cgrp, buf, PATH_MAX);
4821 if (!path) {
4822 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004823 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004824 }
4825 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004826 seq_putc(m, '\n');
4827 }
4828
4829out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004830 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004831 mutex_unlock(&cgroup_mutex);
4832 put_task_struct(tsk);
4833out_free:
4834 kfree(buf);
4835out:
4836 return retval;
4837}
4838
Paul Menagea4243162007-10-18 23:39:35 -07004839/* Display information about each subsystem and each hierarchy */
4840static int proc_cgroupstats_show(struct seq_file *m, void *v)
4841{
Tejun Heo30159ec2013-06-25 11:53:37 -07004842 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004843 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004844
Paul Menage8bab8dd2008-04-04 14:29:57 -07004845 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004846 /*
4847 * ideally we don't want subsystems moving around while we do this.
4848 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4849 * subsys/hierarchy state.
4850 */
Paul Menagea4243162007-10-18 23:39:35 -07004851 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004852
4853 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004854 seq_printf(m, "%s\t%d\t%d\t%d\n",
4855 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004856 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004857
Paul Menagea4243162007-10-18 23:39:35 -07004858 mutex_unlock(&cgroup_mutex);
4859 return 0;
4860}
4861
4862static int cgroupstats_open(struct inode *inode, struct file *file)
4863{
Al Viro9dce07f2008-03-29 03:07:28 +00004864 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004865}
4866
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004867static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004868 .open = cgroupstats_open,
4869 .read = seq_read,
4870 .llseek = seq_lseek,
4871 .release = single_release,
4872};
4873
Paul Menageb4f48b62007-10-18 23:39:33 -07004874/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004875 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004876 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004877 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004878 * A task is associated with the init_css_set until cgroup_post_fork()
4879 * attaches it to the parent's css_set. Empty cg_list indicates that
4880 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004881 */
4882void cgroup_fork(struct task_struct *child)
4883{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004884 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004885 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004886}
4887
4888/**
Li Zefana043e3b2008-02-23 15:24:09 -08004889 * cgroup_post_fork - called on a new task after adding it to the task list
4890 * @child: the task in question
4891 *
Tejun Heo5edee612012-10-16 15:03:14 -07004892 * Adds the task to the list running through its css_set if necessary and
4893 * call the subsystem fork() callbacks. Has to be after the task is
4894 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004895 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004896 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004897 */
Paul Menage817929e2007-10-18 23:39:36 -07004898void cgroup_post_fork(struct task_struct *child)
4899{
Tejun Heo30159ec2013-06-25 11:53:37 -07004900 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004901 int i;
4902
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004903 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004904 * This may race against cgroup_enable_task_cg_links(). As that
4905 * function sets use_task_css_set_links before grabbing
4906 * tasklist_lock and we just went through tasklist_lock to add
4907 * @child, it's guaranteed that either we see the set
4908 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4909 * @child during its iteration.
4910 *
4911 * If we won the race, @child is associated with %current's
4912 * css_set. Grabbing css_set_rwsem guarantees both that the
4913 * association is stable, and, on completion of the parent's
4914 * migration, @child is visible in the source of migration or
4915 * already in the destination cgroup. This guarantee is necessary
4916 * when implementing operations which need to migrate all tasks of
4917 * a cgroup to another.
4918 *
4919 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4920 * will remain in init_css_set. This is safe because all tasks are
4921 * in the init_css_set before cg_links is enabled and there's no
4922 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004923 */
Paul Menage817929e2007-10-18 23:39:36 -07004924 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004925 struct css_set *cset;
4926
Tejun Heo96d365e2014-02-13 06:58:40 -05004927 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004928 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004929 if (list_empty(&child->cg_list)) {
4930 rcu_assign_pointer(child->cgroups, cset);
4931 list_add(&child->cg_list, &cset->tasks);
4932 get_css_set(cset);
4933 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004934 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004935 }
Tejun Heo5edee612012-10-16 15:03:14 -07004936
4937 /*
4938 * Call ss->fork(). This must happen after @child is linked on
4939 * css_set; otherwise, @child might change state between ->fork()
4940 * and addition to css_set.
4941 */
4942 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004943 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004944 if (ss->fork)
4945 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004946 }
Paul Menage817929e2007-10-18 23:39:36 -07004947}
Tejun Heo5edee612012-10-16 15:03:14 -07004948
Paul Menage817929e2007-10-18 23:39:36 -07004949/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004950 * cgroup_exit - detach cgroup from exiting task
4951 * @tsk: pointer to task_struct of exiting process
4952 *
4953 * Description: Detach cgroup from @tsk and release it.
4954 *
4955 * Note that cgroups marked notify_on_release force every task in
4956 * them to take the global cgroup_mutex mutex when exiting.
4957 * This could impact scaling on very large systems. Be reluctant to
4958 * use notify_on_release cgroups where very high task exit scaling
4959 * is required on large systems.
4960 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004961 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4962 * call cgroup_exit() while the task is still competent to handle
4963 * notify_on_release(), then leave the task attached to the root cgroup in
4964 * each hierarchy for the remainder of its exit. No need to bother with
4965 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004966 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004967 */
Li Zefan1ec41832014-03-28 15:22:19 +08004968void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004969{
Tejun Heo30159ec2013-06-25 11:53:37 -07004970 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004971 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004972 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004973 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004974
4975 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004976 * Unlink from @tsk from its css_set. As migration path can't race
4977 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004978 */
4979 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004980 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004981 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004982 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004983 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004984 }
4985
Paul Menageb4f48b62007-10-18 23:39:33 -07004986 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004987 cset = task_css_set(tsk);
4988 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004989
Li Zefan1ec41832014-03-28 15:22:19 +08004990 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004991 /* see cgroup_post_fork() for details */
4992 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004993 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004994 struct cgroup_subsys_state *old_css = cset->subsys[i];
4995 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004996
Tejun Heoeb954192013-08-08 20:11:23 -04004997 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004998 }
4999 }
5000 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005001
Tejun Heoeaf797a2014-02-25 10:04:03 -05005002 if (put_cset)
5003 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005004}
Paul Menage697f4162007-10-18 23:39:34 -07005005
Paul Menagebd89aab2007-10-18 23:40:44 -07005006static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005007{
Li Zefanf50daa72013-03-01 15:06:07 +08005008 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005009 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005010 /*
5011 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005012 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005013 * it now
5014 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005015 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005016
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005017 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005018 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005019 list_empty(&cgrp->release_list)) {
5020 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 need_schedule_work = 1;
5022 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005023 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024 if (need_schedule_work)
5025 schedule_work(&release_agent_work);
5026 }
5027}
5028
Paul Menage81a6a5c2007-10-18 23:39:38 -07005029/*
5030 * Notify userspace when a cgroup is released, by running the
5031 * configured release agent with the name of the cgroup (path
5032 * relative to the root of cgroup file system) as the argument.
5033 *
5034 * Most likely, this user command will try to rmdir this cgroup.
5035 *
5036 * This races with the possibility that some other task will be
5037 * attached to this cgroup before it is removed, or that some other
5038 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5039 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5040 * unused, and this cgroup will be reprieved from its death sentence,
5041 * to continue to serve a useful existence. Next time it's released,
5042 * we will get notified again, if it still has 'notify_on_release' set.
5043 *
5044 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5045 * means only wait until the task is successfully execve()'d. The
5046 * separate release agent task is forked by call_usermodehelper(),
5047 * then control in this thread returns here, without waiting for the
5048 * release agent task. We don't bother to wait because the caller of
5049 * this routine has no use for the exit status of the release agent
5050 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005052static void cgroup_release_agent(struct work_struct *work)
5053{
5054 BUG_ON(work != &release_agent_work);
5055 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005056 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005057 while (!list_empty(&release_list)) {
5058 char *argv[3], *envp[3];
5059 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005060 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005061 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062 struct cgroup,
5063 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005064 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005065 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005066 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005067 if (!pathbuf)
5068 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005069 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5070 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005071 goto continue_free;
5072 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5073 if (!agentbuf)
5074 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005075
5076 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005077 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005078 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005079 argv[i] = NULL;
5080
5081 i = 0;
5082 /* minimal command environment */
5083 envp[i++] = "HOME=/";
5084 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5085 envp[i] = NULL;
5086
5087 /* Drop the lock while we invoke the usermode helper,
5088 * since the exec could involve hitting disk and hence
5089 * be a slow process */
5090 mutex_unlock(&cgroup_mutex);
5091 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005093 continue_free:
5094 kfree(pathbuf);
5095 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005096 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005097 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005098 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005099 mutex_unlock(&cgroup_mutex);
5100}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005101
5102static int __init cgroup_disable(char *str)
5103{
Tejun Heo30159ec2013-06-25 11:53:37 -07005104 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005105 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005106 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005107
5108 while ((token = strsep(&str, ",")) != NULL) {
5109 if (!*token)
5110 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005111
Tejun Heo3ed80a62014-02-08 10:36:58 -05005112 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005113 if (!strcmp(token, ss->name)) {
5114 ss->disabled = 1;
5115 printk(KERN_INFO "Disabling %s control group"
5116 " subsystem\n", ss->name);
5117 break;
5118 }
5119 }
5120 }
5121 return 1;
5122}
5123__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005124
Tejun Heob77d7b62013-08-13 11:01:54 -04005125/**
Tejun Heoec903c02014-05-13 12:11:01 -04005126 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005127 * @dentry: directory dentry of interest
5128 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005129 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005130 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5131 * to get the corresponding css and return it. If such css doesn't exist
5132 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005133 */
Tejun Heoec903c02014-05-13 12:11:01 -04005134struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5135 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005136{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005137 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5138 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005139 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005140
Tejun Heo35cf0832013-08-26 18:40:56 -04005141 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005142 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5143 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005144 return ERR_PTR(-EBADF);
5145
Tejun Heo5a17f542014-02-11 11:52:47 -05005146 rcu_read_lock();
5147
Tejun Heo2bd59d42014-02-11 11:52:49 -05005148 /*
5149 * This path doesn't originate from kernfs and @kn could already
5150 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005151 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005152 */
5153 cgrp = rcu_dereference(kn->priv);
5154 if (cgrp)
5155 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005156
Tejun Heoec903c02014-05-13 12:11:01 -04005157 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005158 css = ERR_PTR(-ENOENT);
5159
5160 rcu_read_unlock();
5161 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005162}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005163
Li Zefan1cb650b2013-08-19 10:05:24 +08005164/**
5165 * css_from_id - lookup css by id
5166 * @id: the cgroup id
5167 * @ss: cgroup subsys to be looked into
5168 *
5169 * Returns the css if there's valid one with @id, otherwise returns NULL.
5170 * Should be called under rcu_read_lock().
5171 */
5172struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5173{
Tejun Heo6fa49182014-05-04 15:09:13 -04005174 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005175 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005176}
5177
Paul Menagefe693432009-09-23 15:56:20 -07005178#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005179static struct cgroup_subsys_state *
5180debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005181{
5182 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5183
5184 if (!css)
5185 return ERR_PTR(-ENOMEM);
5186
5187 return css;
5188}
5189
Tejun Heoeb954192013-08-08 20:11:23 -04005190static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005191{
Tejun Heoeb954192013-08-08 20:11:23 -04005192 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005193}
5194
Tejun Heo182446d2013-08-08 20:11:24 -04005195static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5196 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005197{
Tejun Heo182446d2013-08-08 20:11:24 -04005198 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005199}
5200
Tejun Heo182446d2013-08-08 20:11:24 -04005201static u64 current_css_set_read(struct cgroup_subsys_state *css,
5202 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005203{
5204 return (u64)(unsigned long)current->cgroups;
5205}
5206
Tejun Heo182446d2013-08-08 20:11:24 -04005207static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005208 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005209{
5210 u64 count;
5211
5212 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005213 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005214 rcu_read_unlock();
5215 return count;
5216}
5217
Tejun Heo2da8ca82013-12-05 12:28:04 -05005218static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005219{
Tejun Heo69d02062013-06-12 21:04:50 -07005220 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005221 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005222 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005223
Tejun Heoe61734c2014-02-12 09:29:50 -05005224 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5225 if (!name_buf)
5226 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005227
Tejun Heo96d365e2014-02-13 06:58:40 -05005228 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005229 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005230 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005231 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005232 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005233
Tejun Heoa2dd4242014-03-19 10:23:55 -04005234 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005235 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005236 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005237 }
5238 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005239 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005240 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005241 return 0;
5242}
5243
5244#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005245static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005246{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005247 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005248 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005249
Tejun Heo96d365e2014-02-13 06:58:40 -05005250 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005251 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005252 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005253 struct task_struct *task;
5254 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005255
Tejun Heo5abb8852013-06-12 21:04:49 -07005256 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005257
Tejun Heo5abb8852013-06-12 21:04:49 -07005258 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005259 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5260 goto overflow;
5261 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005262 }
Tejun Heoc7561122014-02-25 10:04:01 -05005263
5264 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5265 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5266 goto overflow;
5267 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5268 }
5269 continue;
5270 overflow:
5271 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005272 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005273 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005274 return 0;
5275}
5276
Tejun Heo182446d2013-08-08 20:11:24 -04005277static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005278{
Tejun Heo182446d2013-08-08 20:11:24 -04005279 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005280}
5281
5282static struct cftype debug_files[] = {
5283 {
Paul Menagefe693432009-09-23 15:56:20 -07005284 .name = "taskcount",
5285 .read_u64 = debug_taskcount_read,
5286 },
5287
5288 {
5289 .name = "current_css_set",
5290 .read_u64 = current_css_set_read,
5291 },
5292
5293 {
5294 .name = "current_css_set_refcount",
5295 .read_u64 = current_css_set_refcount_read,
5296 },
5297
5298 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005299 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005300 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005301 },
5302
5303 {
5304 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005305 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005306 },
5307
5308 {
Paul Menagefe693432009-09-23 15:56:20 -07005309 .name = "releasable",
5310 .read_u64 = releasable_read,
5311 },
Paul Menagefe693432009-09-23 15:56:20 -07005312
Tejun Heo4baf6e32012-04-01 12:09:55 -07005313 { } /* terminate */
5314};
Paul Menagefe693432009-09-23 15:56:20 -07005315
Tejun Heo073219e2014-02-08 10:36:58 -05005316struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005317 .css_alloc = debug_css_alloc,
5318 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005319 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005320};
5321#endif /* CONFIG_CGROUP_DEBUG */