blob: b57a949ae4bcbe1ddea8b84ebd2bd75f0d1269c0 [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 Heo0942eee2013-08-08 20:11:26 -04003298/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003299 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003300 * @it: the iterator to advance
3301 *
3302 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003303 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003304static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003305{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003306 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003307 struct cgrp_cset_link *link;
3308 struct css_set *cset;
3309
3310 /* Advance to the next non-empty css_set */
3311 do {
3312 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003313 if (l == it->cset_head) {
3314 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003315 return;
3316 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003317
3318 if (it->ss) {
3319 cset = container_of(l, struct css_set,
3320 e_cset_node[it->ss->id]);
3321 } else {
3322 link = list_entry(l, struct cgrp_cset_link, cset_link);
3323 cset = link->cset;
3324 }
Tejun Heoc7561122014-02-25 10:04:01 -05003325 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3326
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003327 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003328
3329 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003330 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003331 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003332 it->task_pos = cset->mg_tasks.next;
3333
3334 it->tasks_head = &cset->tasks;
3335 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003336}
3337
Tejun Heo0942eee2013-08-08 20:11:26 -04003338/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003339 * css_task_iter_start - initiate task iteration
3340 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003341 * @it: the task iterator to use
3342 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003343 * Initiate iteration through the tasks of @css. The caller can call
3344 * css_task_iter_next() to walk through the tasks until the function
3345 * returns NULL. On completion of iteration, css_task_iter_end() must be
3346 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003347 *
3348 * Note that this function acquires a lock which is released when the
3349 * iteration finishes. The caller can't sleep while iteration is in
3350 * progress.
3351 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003352void css_task_iter_start(struct cgroup_subsys_state *css,
3353 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003354 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003355{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003356 /* no one should try to iterate before mounting cgroups */
3357 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003358
Tejun Heo96d365e2014-02-13 06:58:40 -05003359 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003360
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003361 it->ss = css->ss;
3362
3363 if (it->ss)
3364 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3365 else
3366 it->cset_pos = &css->cgroup->cset_links;
3367
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003368 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003369
Tejun Heo72ec7022013-08-08 20:11:26 -04003370 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003371}
3372
Tejun Heo0942eee2013-08-08 20:11:26 -04003373/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003374 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003375 * @it: the task iterator being iterated
3376 *
3377 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003378 * initialized via css_task_iter_start(). Returns NULL when the iteration
3379 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003380 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003381struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003382{
3383 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003384 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003385
3386 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003387 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003388 return NULL;
3389 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003390
3391 /*
3392 * Advance iterator to find next entry. cset->tasks is consumed
3393 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3394 * next cset.
3395 */
Paul Menage817929e2007-10-18 23:39:36 -07003396 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003397
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003398 if (l == it->tasks_head)
3399 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003400
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003401 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003402 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003403 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003404 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003405
Paul Menage817929e2007-10-18 23:39:36 -07003406 return res;
3407}
3408
Tejun Heo0942eee2013-08-08 20:11:26 -04003409/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003410 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003411 * @it: the task iterator to finish
3412 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003413 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003414 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003415void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003416 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003417{
Tejun Heo96d365e2014-02-13 06:58:40 -05003418 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003419}
3420
3421/**
3422 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3423 * @to: cgroup to which the tasks will be moved
3424 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003425 *
3426 * Locking rules between cgroup_post_fork() and the migration path
3427 * guarantee that, if a task is forking while being migrated, the new child
3428 * is guaranteed to be either visible in the source cgroup after the
3429 * parent's migration is complete or put into the target cgroup. No task
3430 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003431 */
3432int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3433{
Tejun Heo952aaa12014-02-25 10:04:03 -05003434 LIST_HEAD(preloaded_csets);
3435 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003436 struct css_task_iter it;
3437 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003438 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003439
Tejun Heo952aaa12014-02-25 10:04:03 -05003440 mutex_lock(&cgroup_mutex);
3441
3442 /* all tasks in @from are being moved, all csets are source */
3443 down_read(&css_set_rwsem);
3444 list_for_each_entry(link, &from->cset_links, cset_link)
3445 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3446 up_read(&css_set_rwsem);
3447
3448 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3449 if (ret)
3450 goto out_err;
3451
3452 /*
3453 * Migrate tasks one-by-one until @form is empty. This fails iff
3454 * ->can_attach() fails.
3455 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003456 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003457 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003458 task = css_task_iter_next(&it);
3459 if (task)
3460 get_task_struct(task);
3461 css_task_iter_end(&it);
3462
3463 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003464 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003465 put_task_struct(task);
3466 }
3467 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003468out_err:
3469 cgroup_migrate_finish(&preloaded_csets);
3470 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003471 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003472}
3473
Paul Menage817929e2007-10-18 23:39:36 -07003474/*
Ben Blum102a7752009-09-23 15:56:26 -07003475 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003476 *
3477 * Reading this file can return large amounts of data if a cgroup has
3478 * *lots* of attached tasks. So it may need several calls to read(),
3479 * but we cannot guarantee that the information we produce is correct
3480 * unless we produce it entirely atomically.
3481 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003482 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003483
Li Zefan24528252012-01-20 11:58:43 +08003484/* which pidlist file are we talking about? */
3485enum cgroup_filetype {
3486 CGROUP_FILE_PROCS,
3487 CGROUP_FILE_TASKS,
3488};
3489
3490/*
3491 * A pidlist is a list of pids that virtually represents the contents of one
3492 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3493 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3494 * to the cgroup.
3495 */
3496struct cgroup_pidlist {
3497 /*
3498 * used to find which pidlist is wanted. doesn't change as long as
3499 * this particular list stays in the list.
3500 */
3501 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3502 /* array of xids */
3503 pid_t *list;
3504 /* how many elements the above list has */
3505 int length;
Li Zefan24528252012-01-20 11:58:43 +08003506 /* each of these stored in a list by its cgroup */
3507 struct list_head links;
3508 /* pointer to the cgroup we belong to, for list removal purposes */
3509 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003510 /* for delayed destruction */
3511 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003512};
3513
Paul Menagebbcb81d2007-10-18 23:39:32 -07003514/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003515 * The following two functions "fix" the issue where there are more pids
3516 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3517 * TODO: replace with a kernel-wide solution to this problem
3518 */
3519#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3520static void *pidlist_allocate(int count)
3521{
3522 if (PIDLIST_TOO_LARGE(count))
3523 return vmalloc(count * sizeof(pid_t));
3524 else
3525 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3526}
Tejun Heob1a21362013-11-29 10:42:58 -05003527
Ben Blumd1d9fd32009-09-23 15:56:28 -07003528static void pidlist_free(void *p)
3529{
3530 if (is_vmalloc_addr(p))
3531 vfree(p);
3532 else
3533 kfree(p);
3534}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003535
3536/*
Tejun Heob1a21362013-11-29 10:42:58 -05003537 * Used to destroy all pidlists lingering waiting for destroy timer. None
3538 * should be left afterwards.
3539 */
3540static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3541{
3542 struct cgroup_pidlist *l, *tmp_l;
3543
3544 mutex_lock(&cgrp->pidlist_mutex);
3545 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3546 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3547 mutex_unlock(&cgrp->pidlist_mutex);
3548
3549 flush_workqueue(cgroup_pidlist_destroy_wq);
3550 BUG_ON(!list_empty(&cgrp->pidlists));
3551}
3552
3553static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3554{
3555 struct delayed_work *dwork = to_delayed_work(work);
3556 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3557 destroy_dwork);
3558 struct cgroup_pidlist *tofree = NULL;
3559
3560 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003561
3562 /*
Tejun Heo04502362013-11-29 10:42:59 -05003563 * Destroy iff we didn't get queued again. The state won't change
3564 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003565 */
Tejun Heo04502362013-11-29 10:42:59 -05003566 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003567 list_del(&l->links);
3568 pidlist_free(l->list);
3569 put_pid_ns(l->key.ns);
3570 tofree = l;
3571 }
3572
Tejun Heob1a21362013-11-29 10:42:58 -05003573 mutex_unlock(&l->owner->pidlist_mutex);
3574 kfree(tofree);
3575}
3576
3577/*
Ben Blum102a7752009-09-23 15:56:26 -07003578 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003579 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003580 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003581static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003582{
Ben Blum102a7752009-09-23 15:56:26 -07003583 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003584
3585 /*
3586 * we presume the 0th element is unique, so i starts at 1. trivial
3587 * edge cases first; no work needs to be done for either
3588 */
3589 if (length == 0 || length == 1)
3590 return length;
3591 /* src and dest walk down the list; dest counts unique elements */
3592 for (src = 1; src < length; src++) {
3593 /* find next unique element */
3594 while (list[src] == list[src-1]) {
3595 src++;
3596 if (src == length)
3597 goto after;
3598 }
3599 /* dest always points to where the next unique element goes */
3600 list[dest] = list[src];
3601 dest++;
3602 }
3603after:
Ben Blum102a7752009-09-23 15:56:26 -07003604 return dest;
3605}
3606
Tejun Heoafb2bc12013-11-29 10:42:59 -05003607/*
3608 * The two pid files - task and cgroup.procs - guaranteed that the result
3609 * is sorted, which forced this whole pidlist fiasco. As pid order is
3610 * different per namespace, each namespace needs differently sorted list,
3611 * making it impossible to use, for example, single rbtree of member tasks
3612 * sorted by task pointer. As pidlists can be fairly large, allocating one
3613 * per open file is dangerous, so cgroup had to implement shared pool of
3614 * pidlists keyed by cgroup and namespace.
3615 *
3616 * All this extra complexity was caused by the original implementation
3617 * committing to an entirely unnecessary property. In the long term, we
3618 * want to do away with it. Explicitly scramble sort order if
3619 * sane_behavior so that no such expectation exists in the new interface.
3620 *
3621 * Scrambling is done by swapping every two consecutive bits, which is
3622 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3623 */
3624static pid_t pid_fry(pid_t pid)
3625{
3626 unsigned a = pid & 0x55555555;
3627 unsigned b = pid & 0xAAAAAAAA;
3628
3629 return (a << 1) | (b >> 1);
3630}
3631
3632static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3633{
3634 if (cgroup_sane_behavior(cgrp))
3635 return pid_fry(pid);
3636 else
3637 return pid;
3638}
3639
Ben Blum102a7752009-09-23 15:56:26 -07003640static int cmppid(const void *a, const void *b)
3641{
3642 return *(pid_t *)a - *(pid_t *)b;
3643}
3644
Tejun Heoafb2bc12013-11-29 10:42:59 -05003645static int fried_cmppid(const void *a, const void *b)
3646{
3647 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3648}
3649
Ben Blum72a8cb32009-09-23 15:56:27 -07003650static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3651 enum cgroup_filetype type)
3652{
3653 struct cgroup_pidlist *l;
3654 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003655 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003656
Tejun Heoe6b81712013-11-29 10:42:59 -05003657 lockdep_assert_held(&cgrp->pidlist_mutex);
3658
3659 list_for_each_entry(l, &cgrp->pidlists, links)
3660 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003661 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003662 return NULL;
3663}
3664
Ben Blum72a8cb32009-09-23 15:56:27 -07003665/*
3666 * find the appropriate pidlist for our purpose (given procs vs tasks)
3667 * returns with the lock on that pidlist already held, and takes care
3668 * of the use count, or returns NULL with no locks held if we're out of
3669 * memory.
3670 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003671static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3672 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003673{
3674 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003675
Tejun Heoe6b81712013-11-29 10:42:59 -05003676 lockdep_assert_held(&cgrp->pidlist_mutex);
3677
3678 l = cgroup_pidlist_find(cgrp, type);
3679 if (l)
3680 return l;
3681
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003683 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003684 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003686
Tejun Heob1a21362013-11-29 10:42:58 -05003687 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003688 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003689 /* don't need task_nsproxy() if we're looking at ourself */
3690 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003691 l->owner = cgrp;
3692 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 return l;
3694}
3695
3696/*
Ben Blum102a7752009-09-23 15:56:26 -07003697 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3698 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003699static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3700 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003701{
3702 pid_t *array;
3703 int length;
3704 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003705 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003706 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003707 struct cgroup_pidlist *l;
3708
Tejun Heo4bac00d2013-11-29 10:42:59 -05003709 lockdep_assert_held(&cgrp->pidlist_mutex);
3710
Ben Blum102a7752009-09-23 15:56:26 -07003711 /*
3712 * If cgroup gets more users after we read count, we won't have
3713 * enough space - tough. This race is indistinguishable to the
3714 * caller from the case that the additional cgroup users didn't
3715 * show up until sometime later on.
3716 */
3717 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003718 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003719 if (!array)
3720 return -ENOMEM;
3721 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003722 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003723 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003724 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003725 break;
Ben Blum102a7752009-09-23 15:56:26 -07003726 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003727 if (type == CGROUP_FILE_PROCS)
3728 pid = task_tgid_vnr(tsk);
3729 else
3730 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003731 if (pid > 0) /* make sure to only use valid results */
3732 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003733 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003734 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003735 length = n;
3736 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003737 if (cgroup_sane_behavior(cgrp))
3738 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3739 else
3740 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003741 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003742 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003743
Tejun Heoe6b81712013-11-29 10:42:59 -05003744 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003745 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003746 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003747 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003748 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003749 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003750
3751 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003752 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003753 l->list = array;
3754 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003755 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003756 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003757}
3758
Balbir Singh846c7bb2007-10-18 23:39:44 -07003759/**
Li Zefana043e3b2008-02-23 15:24:09 -08003760 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003761 * @stats: cgroupstats to fill information into
3762 * @dentry: A dentry entry belonging to the cgroup for which stats have
3763 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003764 *
3765 * Build and fill cgroupstats so that taskstats can export it to user
3766 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003767 */
3768int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3769{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003770 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003771 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003772 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003773 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003774
Tejun Heo2bd59d42014-02-11 11:52:49 -05003775 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3776 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3777 kernfs_type(kn) != KERNFS_DIR)
3778 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003779
Li Zefanbad34662014-02-14 16:54:28 +08003780 mutex_lock(&cgroup_mutex);
3781
Tejun Heo2bd59d42014-02-11 11:52:49 -05003782 /*
3783 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003784 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003785 * @kn->priv is RCU safe. Let's do the RCU dancing.
3786 */
3787 rcu_read_lock();
3788 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003789 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003790 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003791 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003792 return -ENOENT;
3793 }
Li Zefanbad34662014-02-14 16:54:28 +08003794 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003795
Tejun Heo9d800df2014-05-14 09:15:00 -04003796 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003797 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003798 switch (tsk->state) {
3799 case TASK_RUNNING:
3800 stats->nr_running++;
3801 break;
3802 case TASK_INTERRUPTIBLE:
3803 stats->nr_sleeping++;
3804 break;
3805 case TASK_UNINTERRUPTIBLE:
3806 stats->nr_uninterruptible++;
3807 break;
3808 case TASK_STOPPED:
3809 stats->nr_stopped++;
3810 break;
3811 default:
3812 if (delayacct_is_task_waiting_on_io(tsk))
3813 stats->nr_io_wait++;
3814 break;
3815 }
3816 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003817 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003818
Li Zefanbad34662014-02-14 16:54:28 +08003819 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003820 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003821}
3822
Paul Menage8f3ff202009-09-23 15:56:25 -07003823
Paul Menagecc31edc2008-10-18 20:28:04 -07003824/*
Ben Blum102a7752009-09-23 15:56:26 -07003825 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003826 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003827 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003828 */
3829
Ben Blum102a7752009-09-23 15:56:26 -07003830static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003831{
3832 /*
3833 * Initially we receive a position value that corresponds to
3834 * one more than the last pid shown (or 0 on the first call or
3835 * after a seek to the start). Use a binary-search to find the
3836 * next pid to display, if any
3837 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003838 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003839 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003840 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003841 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003842 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003843 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003844
Tejun Heo4bac00d2013-11-29 10:42:59 -05003845 mutex_lock(&cgrp->pidlist_mutex);
3846
3847 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003848 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003849 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003850 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003851 * could already have been destroyed.
3852 */
Tejun Heo5d224442013-12-05 12:28:04 -05003853 if (of->priv)
3854 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003855
3856 /*
3857 * Either this is the first start() after open or the matching
3858 * pidlist has been destroyed inbetween. Create a new one.
3859 */
Tejun Heo5d224442013-12-05 12:28:04 -05003860 if (!of->priv) {
3861 ret = pidlist_array_load(cgrp, type,
3862 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003863 if (ret)
3864 return ERR_PTR(ret);
3865 }
Tejun Heo5d224442013-12-05 12:28:04 -05003866 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003867
Paul Menagecc31edc2008-10-18 20:28:04 -07003868 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003869 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003870
Paul Menagecc31edc2008-10-18 20:28:04 -07003871 while (index < end) {
3872 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003873 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003874 index = mid;
3875 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003876 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003877 index = mid + 1;
3878 else
3879 end = mid;
3880 }
3881 }
3882 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003883 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003884 return NULL;
3885 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003886 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003887 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003888 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003889}
3890
Ben Blum102a7752009-09-23 15:56:26 -07003891static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003892{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003893 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003894 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003895
Tejun Heo5d224442013-12-05 12:28:04 -05003896 if (l)
3897 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003898 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003899 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003900}
3901
Ben Blum102a7752009-09-23 15:56:26 -07003902static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003903{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003904 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003905 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003906 pid_t *p = v;
3907 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003908 /*
3909 * Advance to the next pid in the array. If this goes off the
3910 * end, we're done
3911 */
3912 p++;
3913 if (p >= end) {
3914 return NULL;
3915 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003916 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003917 return p;
3918 }
3919}
3920
Ben Blum102a7752009-09-23 15:56:26 -07003921static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003922{
3923 return seq_printf(s, "%d\n", *(int *)v);
3924}
3925
Tejun Heo182446d2013-08-08 20:11:24 -04003926static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3927 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003928{
Tejun Heo182446d2013-08-08 20:11:24 -04003929 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003930}
3931
Tejun Heo182446d2013-08-08 20:11:24 -04003932static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3933 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003934{
Tejun Heo182446d2013-08-08 20:11:24 -04003935 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003936 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003937 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003938 else
Tejun Heo182446d2013-08-08 20:11:24 -04003939 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003940 return 0;
3941}
3942
Tejun Heo182446d2013-08-08 20:11:24 -04003943static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3944 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003945{
Tejun Heo182446d2013-08-08 20:11:24 -04003946 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947}
3948
Tejun Heo182446d2013-08-08 20:11:24 -04003949static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3950 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003951{
3952 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003953 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003954 else
Tejun Heo182446d2013-08-08 20:11:24 -04003955 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003956 return 0;
3957}
3958
Tejun Heod5c56ce2013-06-03 19:14:34 -07003959static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003960 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003961 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003962 .seq_start = cgroup_pidlist_start,
3963 .seq_next = cgroup_pidlist_next,
3964 .seq_stop = cgroup_pidlist_stop,
3965 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003966 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003967 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003968 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003969 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003970 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003971 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003972 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003973 .read_u64 = cgroup_clone_children_read,
3974 .write_u64 = cgroup_clone_children_write,
3975 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003976 {
Tejun Heo873fe092013-04-14 20:15:26 -07003977 .name = "cgroup.sane_behavior",
3978 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003979 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003980 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003981 {
3982 .name = "cgroup.controllers",
3983 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3984 .seq_show = cgroup_root_controllers_show,
3985 },
3986 {
3987 .name = "cgroup.controllers",
3988 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3989 .seq_show = cgroup_controllers_show,
3990 },
3991 {
3992 .name = "cgroup.subtree_control",
3993 .flags = CFTYPE_ONLY_ON_DFL,
3994 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04003995 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04003996 },
Tejun Heo842b5972014-04-25 18:28:02 -04003997 {
3998 .name = "cgroup.populated",
3999 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4000 .seq_show = cgroup_populated_show,
4001 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004002
4003 /*
4004 * Historical crazy stuff. These don't have "cgroup." prefix and
4005 * don't exist if sane_behavior. If you're depending on these, be
4006 * prepared to be burned.
4007 */
4008 {
4009 .name = "tasks",
4010 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004011 .seq_start = cgroup_pidlist_start,
4012 .seq_next = cgroup_pidlist_next,
4013 .seq_stop = cgroup_pidlist_stop,
4014 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004015 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004016 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004017 .mode = S_IRUGO | S_IWUSR,
4018 },
4019 {
4020 .name = "notify_on_release",
4021 .flags = CFTYPE_INSANE,
4022 .read_u64 = cgroup_read_notify_on_release,
4023 .write_u64 = cgroup_write_notify_on_release,
4024 },
Tejun Heo873fe092013-04-14 20:15:26 -07004025 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004026 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004027 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004028 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004029 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004030 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004031 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004032 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004033};
4034
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004035/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004036 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004037 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004038 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004039 *
4040 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004041 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004042static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004043{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004044 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004045 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004046
Tejun Heo8e3f6542012-04-01 12:09:55 -07004047 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004048 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004049 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004050
Tejun Heo69dfa002014-05-04 15:09:13 -04004051 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004052 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004053
Tejun Heo0adb0702014-02-12 09:29:48 -05004054 list_for_each_entry(cfts, &ss->cfts, node) {
4055 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004056 if (ret < 0)
4057 goto err;
4058 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004059 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004060 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004061err:
4062 cgroup_clear_dir(cgrp, subsys_mask);
4063 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064}
4065
Tejun Heo0c21ead2013-08-13 20:22:51 -04004066/*
4067 * css destruction is four-stage process.
4068 *
4069 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4070 * Implemented in kill_css().
4071 *
4072 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004073 * and thus css_tryget_online() is guaranteed to fail, the css can be
4074 * offlined by invoking offline_css(). After offlining, the base ref is
4075 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004076 *
4077 * 3. When the percpu_ref reaches zero, the only possible remaining
4078 * accessors are inside RCU read sections. css_release() schedules the
4079 * RCU callback.
4080 *
4081 * 4. After the grace period, the css can be freed. Implemented in
4082 * css_free_work_fn().
4083 *
4084 * It is actually hairier because both step 2 and 4 require process context
4085 * and thus involve punting to css->destroy_work adding two additional
4086 * steps to the already complex sequence.
4087 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004088static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004089{
4090 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004091 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004092 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004093
Tejun Heo0ae78e02013-08-13 11:01:54 -04004094 if (css->parent)
4095 css_put(css->parent);
4096
Tejun Heo0c21ead2013-08-13 20:22:51 -04004097 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004098 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004099}
4100
4101static void css_free_rcu_fn(struct rcu_head *rcu_head)
4102{
4103 struct cgroup_subsys_state *css =
4104 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4105
Tejun Heo0c21ead2013-08-13 20:22:51 -04004106 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004107 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004108}
4109
Tejun Heod3daf282013-06-13 19:39:16 -07004110static void css_release(struct percpu_ref *ref)
4111{
4112 struct cgroup_subsys_state *css =
4113 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo15a4c832014-05-04 15:09:14 -04004114 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004115
Tejun Heo15a4c832014-05-04 15:09:14 -04004116 cgroup_idr_remove(&ss->css_idr, css->id);
4117
Tejun Heo0c21ead2013-08-13 20:22:51 -04004118 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004119}
4120
Tejun Heoddfcada2014-05-04 15:09:14 -04004121static void init_and_link_css(struct cgroup_subsys_state *css,
4122 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123{
Tejun Heoddfcada2014-05-04 15:09:14 -04004124 cgroup_get(cgrp);
4125
Paul Menagebd89aab2007-10-18 23:40:44 -07004126 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004127 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004129
Tejun Heoddfcada2014-05-04 15:09:14 -04004130 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004131 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004132 css_get(css->parent);
4133 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004134 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004135 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004136
Tejun Heoca8bdca2013-08-26 18:40:56 -04004137 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138}
4139
Li Zefan2a4ac632013-07-31 16:16:40 +08004140/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004141static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004142{
Tejun Heo623f9262013-08-13 11:01:55 -04004143 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004144 int ret = 0;
4145
Tejun Heoa31f2d32012-11-19 08:13:37 -08004146 lockdep_assert_held(&cgroup_mutex);
4147
Tejun Heo92fb9742012-11-19 08:13:38 -08004148 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004149 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004150 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004151 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004152 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004153 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004154 }
Tejun Heob1929db2012-11-19 08:13:38 -08004155 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004156}
4157
Li Zefan2a4ac632013-07-31 16:16:40 +08004158/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004159static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004160{
Tejun Heo623f9262013-08-13 11:01:55 -04004161 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004162
4163 lockdep_assert_held(&cgroup_mutex);
4164
4165 if (!(css->flags & CSS_ONLINE))
4166 return;
4167
Li Zefand7eeac12013-03-12 15:35:59 -07004168 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004169 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004170
Tejun Heoeb954192013-08-08 20:11:23 -04004171 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004172 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004173 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004174
4175 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004176}
4177
Tejun Heoc81c925a2013-12-06 15:11:56 -05004178/**
4179 * create_css - create a cgroup_subsys_state
4180 * @cgrp: the cgroup new css will be associated with
4181 * @ss: the subsys of new css
4182 *
4183 * Create a new css associated with @cgrp - @ss pair. On success, the new
4184 * css is online and installed in @cgrp with all interface files created.
4185 * Returns 0 on success, -errno on failure.
4186 */
4187static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4188{
4189 struct cgroup *parent = cgrp->parent;
4190 struct cgroup_subsys_state *css;
4191 int err;
4192
Tejun Heoc81c925a2013-12-06 15:11:56 -05004193 lockdep_assert_held(&cgroup_mutex);
4194
4195 css = ss->css_alloc(cgroup_css(parent, ss));
4196 if (IS_ERR(css))
4197 return PTR_ERR(css);
4198
Tejun Heoddfcada2014-05-04 15:09:14 -04004199 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004200
Tejun Heoc81c925a2013-12-06 15:11:56 -05004201 err = percpu_ref_init(&css->refcnt, css_release);
4202 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004203 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004204
Tejun Heo15a4c832014-05-04 15:09:14 -04004205 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4206 if (err < 0)
4207 goto err_free_percpu_ref;
4208 css->id = err;
4209
Tejun Heoaec25022014-02-08 10:36:58 -05004210 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004211 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004212 goto err_free_id;
4213
4214 /* @css is ready to be brought online now, make it visible */
4215 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004216
4217 err = online_css(css);
4218 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004219 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004220
Tejun Heoc81c925a2013-12-06 15:11:56 -05004221 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4222 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004223 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 -04004224 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004225 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004226 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004227 ss->warned_broken_hierarchy = true;
4228 }
4229
4230 return 0;
4231
Li Zefan3eb59ec2014-03-18 17:02:36 +08004232err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004233 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004234err_free_id:
4235 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004236err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004237 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004238err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004239 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004240 return err;
4241}
4242
Tejun Heob3bfd982014-05-13 12:19:22 -04004243static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4244 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245{
Tejun Heoa9746d82014-05-13 12:19:22 -04004246 struct cgroup *parent, *cgrp;
4247 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004249 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004250 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004251
Tejun Heoa9746d82014-05-13 12:19:22 -04004252 parent = cgroup_kn_lock_live(parent_kn);
4253 if (!parent)
4254 return -ENODEV;
4255 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004256
4257 /* allocate the cgroup and its ID, 0 is reserved for the root */
4258 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4259 if (!cgrp) {
4260 ret = -ENOMEM;
4261 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004262 }
4263
4264 /*
4265 * Temporarily set the pointer to NULL, so idr_find() won't return
4266 * a half-baked cgroup.
4267 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004268 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004269 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004270 ret = -ENOMEM;
4271 goto out_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004272 }
4273
Paul Menagecc31edc2008-10-18 20:28:04 -07004274 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275
Paul Menagebd89aab2007-10-18 23:40:44 -07004276 cgrp->parent = parent;
Tejun Heo9d800df2014-05-14 09:15:00 -04004277 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004278 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279
Li Zefanb6abdb02008-03-04 14:28:19 -08004280 if (notify_on_release(parent))
4281 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4282
Tejun Heo2260e7f2012-11-19 08:13:38 -08004283 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4284 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004285
Tejun Heo2bd59d42014-02-11 11:52:49 -05004286 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004287 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004288 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004289 ret = PTR_ERR(kn);
4290 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004291 }
4292 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004293
Tejun Heo6f305582014-02-12 09:29:50 -05004294 /*
4295 * This extra ref will be put in cgroup_free_fn() and guarantees
4296 * that @cgrp->kn is always accessible.
4297 */
4298 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299
Tejun Heo00356bd2013-06-18 11:14:22 -07004300 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004301
Tejun Heo4e139af2012-11-19 08:13:36 -08004302 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004303 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004304 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004305 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004306
Tejun Heo0d802552013-12-06 15:11:56 -05004307 /*
4308 * @cgrp is now fully operational. If something fails after this
4309 * point, it'll be released via the normal destruction path.
4310 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004311 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004312
Tejun Heoba0f4d72014-05-13 12:19:22 -04004313 ret = cgroup_kn_set_ugid(kn);
4314 if (ret)
4315 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004316
Tejun Heoba0f4d72014-05-13 12:19:22 -04004317 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4318 if (ret)
4319 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004320
Tejun Heo9d403e92013-12-06 15:11:56 -05004321 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004322 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004323 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004324 ret = create_css(cgrp, ss);
4325 if (ret)
4326 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004327 }
Tejun Heoa8638032012-11-09 09:12:29 -08004328 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329
Tejun Heobd53d612014-04-23 11:13:16 -04004330 /*
4331 * On the default hierarchy, a child doesn't automatically inherit
4332 * child_subsys_mask from the parent. Each is configured manually.
4333 */
4334 if (!cgroup_on_dfl(cgrp))
4335 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004336
Tejun Heo2bd59d42014-02-11 11:52:49 -05004337 kernfs_activate(kn);
4338
Tejun Heoba0f4d72014-05-13 12:19:22 -04004339 ret = 0;
4340 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004341
Tejun Heoba0f4d72014-05-13 12:19:22 -04004342out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004343 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004344out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004345 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004346out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004347 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004348 return ret;
4349
4350out_destroy:
4351 cgroup_destroy_locked(cgrp);
4352 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004353}
4354
Tejun Heo223dbc32013-08-13 20:22:50 -04004355/*
4356 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heoec903c02014-05-13 12:11:01 -04004357 * css_tryget_online() is now guaranteed to fail.
Tejun Heo223dbc32013-08-13 20:22:50 -04004358 */
4359static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004360{
Tejun Heo223dbc32013-08-13 20:22:50 -04004361 struct cgroup_subsys_state *css =
4362 container_of(work, struct cgroup_subsys_state, destroy_work);
4363 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004364
Tejun Heof20104d2013-08-13 20:22:50 -04004365 mutex_lock(&cgroup_mutex);
4366
4367 /*
Tejun Heoec903c02014-05-13 12:11:01 -04004368 * css_tryget_online() is guaranteed to fail now. Tell subsystems
4369 * to initate destruction.
Tejun Heo09a503ea2013-08-13 20:22:50 -04004370 */
4371 offline_css(css);
4372
4373 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004374 * If @cgrp is marked dead, it's waiting for refs of all css's to
4375 * be disabled before proceeding to the second phase of cgroup
4376 * destruction. If we are the last one, kick it off.
4377 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004378 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004379 cgroup_destroy_css_killed(cgrp);
4380
4381 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004382
4383 /*
4384 * Put the css refs from kill_css(). Each css holds an extra
4385 * reference to the cgroup's dentry and cgroup removal proceeds
4386 * regardless of css refs. On the last put of each css, whenever
4387 * that may be, the extra dentry ref is put so that dentry
4388 * destruction happens only after all css's are released.
4389 */
4390 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004391}
4392
Tejun Heo223dbc32013-08-13 20:22:50 -04004393/* css kill confirmation processing requires process context, bounce */
4394static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004395{
4396 struct cgroup_subsys_state *css =
4397 container_of(ref, struct cgroup_subsys_state, refcnt);
4398
Tejun Heo223dbc32013-08-13 20:22:50 -04004399 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004400 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004401}
4402
Tejun Heof392e512014-04-23 11:13:14 -04004403/**
4404 * kill_css - destroy a css
4405 * @css: css to destroy
4406 *
4407 * This function initiates destruction of @css by removing cgroup interface
4408 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004409 * asynchronously once css_tryget_online() is guaranteed to fail and when
4410 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004411 */
4412static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004413{
Tejun Heo01f64742014-05-13 12:19:23 -04004414 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004415
Tejun Heo2bd59d42014-02-11 11:52:49 -05004416 /*
4417 * This must happen before css is disassociated with its cgroup.
4418 * See seq_css() for details.
4419 */
Tejun Heoaec25022014-02-08 10:36:58 -05004420 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004421
Tejun Heoedae0c32013-08-13 20:22:51 -04004422 /*
4423 * Killing would put the base ref, but we need to keep it alive
4424 * until after ->css_offline().
4425 */
4426 css_get(css);
4427
4428 /*
4429 * cgroup core guarantees that, by the time ->css_offline() is
4430 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004431 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004432 * proceed to offlining css's because percpu_ref_kill() doesn't
4433 * guarantee that the ref is seen as killed on all CPUs on return.
4434 *
4435 * Use percpu_ref_kill_and_confirm() to get notifications as each
4436 * css is confirmed to be seen as killed on all CPUs.
4437 */
4438 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004439}
4440
4441/**
4442 * cgroup_destroy_locked - the first stage of cgroup destruction
4443 * @cgrp: cgroup to be destroyed
4444 *
4445 * css's make use of percpu refcnts whose killing latency shouldn't be
4446 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004447 * guarantee that css_tryget_online() won't succeed by the time
4448 * ->css_offline() is invoked. To satisfy all the requirements,
4449 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004450 *
4451 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4452 * userland visible parts and start killing the percpu refcnts of
4453 * css's. Set up so that the next stage will be kicked off once all
4454 * the percpu refcnts are confirmed to be killed.
4455 *
4456 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4457 * rest of destruction. Once all cgroup references are gone, the
4458 * cgroup is RCU-freed.
4459 *
4460 * This function implements s1. After this step, @cgrp is gone as far as
4461 * the userland is concerned and a new cgroup with the same name may be
4462 * created. As cgroup doesn't care about the names internally, this
4463 * doesn't cause any problem.
4464 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004465static int cgroup_destroy_locked(struct cgroup *cgrp)
4466 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004467{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004468 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004469 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004470 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004471 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472
Tejun Heo42809dd2012-11-19 08:13:37 -08004473 lockdep_assert_held(&cgroup_mutex);
4474
Tejun Heoddd69142013-06-12 21:04:54 -07004475 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004476 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004477 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004478 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004479 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004480 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004481 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004482 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004483 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004484
Tejun Heo1a90dd52012-11-05 09:16:59 -08004485 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004486 * Make sure there's no live children. We can't test ->children
4487 * emptiness as dead children linger on it while being destroyed;
4488 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4489 */
4490 empty = true;
4491 rcu_read_lock();
4492 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4493 empty = cgroup_is_dead(child);
4494 if (!empty)
4495 break;
4496 }
4497 rcu_read_unlock();
4498 if (!empty)
4499 return -EBUSY;
4500
4501 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004502 * Mark @cgrp dead. This prevents further task migration and child
4503 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004504 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004505 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004506 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004507 */
Tejun Heo54766d42013-06-12 21:04:53 -07004508 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004509
Tejun Heo5d773812014-03-19 10:23:53 -04004510 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004511 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4512 * will be invoked to perform the rest of destruction once the
Tejun Heo01f64742014-05-13 12:19:23 -04004513 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004514 */
4515 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004516 kill_css(css);
4517
Tejun Heo455050d2013-06-13 19:27:41 -07004518 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4519 raw_spin_lock(&release_list_lock);
4520 if (!list_empty(&cgrp->release_list))
4521 list_del_init(&cgrp->release_list);
4522 raw_spin_unlock(&release_list_lock);
4523
4524 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004525 * If @cgrp has css's attached, the second stage of cgroup
4526 * destruction is kicked off from css_killed_work_fn() after the
4527 * refs of all attached css's are killed. If @cgrp doesn't have
4528 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004529 */
Tejun Heof20104d2013-08-13 20:22:50 -04004530 if (!cgrp->nr_css)
4531 cgroup_destroy_css_killed(cgrp);
4532
Tejun Heo01f64742014-05-13 12:19:23 -04004533 /*
4534 * Remove @cgrp directory along with the base files. @cgrp has an
4535 * extra ref on its kn.
4536 */
4537 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004538
Tejun Heoea15f8c2013-06-13 19:27:42 -07004539 return 0;
4540};
4541
Tejun Heod3daf282013-06-13 19:39:16 -07004542/**
Tejun Heof20104d2013-08-13 20:22:50 -04004543 * cgroup_destroy_css_killed - the second step of cgroup destruction
Fabian Frederick60106942014-05-05 20:08:13 +02004544 * @cgrp: the cgroup whose csses have just finished offlining
Tejun Heod3daf282013-06-13 19:39:16 -07004545 *
4546 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004547 * destroyed after all css's are offlined and performs the rest of
4548 * destruction. This is the second step of destruction described in the
4549 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004550 */
Tejun Heof20104d2013-08-13 20:22:50 -04004551static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004552{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004553 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004554
Tejun Heof20104d2013-08-13 20:22:50 -04004555 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556
Paul Menage999cd8a2009-01-07 18:08:36 -08004557 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004558 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004559
Tejun Heo59f52962014-02-11 11:52:49 -05004560 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004561
Paul Menagebd89aab2007-10-18 23:40:44 -07004562 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004563 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004564}
4565
Tejun Heo2bd59d42014-02-11 11:52:49 -05004566static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004567{
Tejun Heoa9746d82014-05-13 12:19:22 -04004568 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004569 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004570
Tejun Heoa9746d82014-05-13 12:19:22 -04004571 cgrp = cgroup_kn_lock_live(kn);
4572 if (!cgrp)
4573 return 0;
4574 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004575
Tejun Heoa9746d82014-05-13 12:19:22 -04004576 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004577
Tejun Heoa9746d82014-05-13 12:19:22 -04004578 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004579
4580 /*
4581 * There are two control paths which try to determine cgroup from
4582 * dentry without going through kernfs - cgroupstats_build() and
4583 * css_tryget_online_from_dir(). Those are supported by RCU
4584 * protecting clearing of cgrp->kn->priv backpointer, which should
4585 * happen after all files under it have been removed.
4586 */
4587 if (!ret)
4588 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4589
Tejun Heo2bd59d42014-02-11 11:52:49 -05004590 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004591 return ret;
4592}
4593
Tejun Heo2bd59d42014-02-11 11:52:49 -05004594static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4595 .remount_fs = cgroup_remount,
4596 .show_options = cgroup_show_options,
4597 .mkdir = cgroup_mkdir,
4598 .rmdir = cgroup_rmdir,
4599 .rename = cgroup_rename,
4600};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004601
Tejun Heo15a4c832014-05-04 15:09:14 -04004602static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004604 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004605
4606 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607
Tejun Heo648bb562012-11-19 08:13:36 -08004608 mutex_lock(&cgroup_mutex);
4609
Tejun Heo15a4c832014-05-04 15:09:14 -04004610 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004611 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004612
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004613 /* Create the root cgroup state for this subsystem */
4614 ss->root = &cgrp_dfl_root;
4615 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616 /* We don't handle early failures gracefully */
4617 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004618 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004619 if (early) {
4620 /* idr_alloc() can't be called safely during early init */
4621 css->id = 1;
4622 } else {
4623 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4624 BUG_ON(css->id < 0);
4625 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626
Li Zefane8d55fd2008-04-29 01:00:13 -07004627 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004628 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004629 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004630 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004631 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
4633 need_forkexit_callback |= ss->fork || ss->exit;
4634
Li Zefane8d55fd2008-04-29 01:00:13 -07004635 /* At system boot, before all subsystems have been
4636 * registered, no tasks have been forked, so we don't
4637 * need to invoke fork callbacks here. */
4638 BUG_ON(!list_empty(&init_task.tasks));
4639
Tejun Heoae7f1642013-08-13 20:22:50 -04004640 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004641
Tejun Heof392e512014-04-23 11:13:14 -04004642 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004643
Ben Blume6a11052010-03-10 15:22:09 -08004644 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645}
4646
4647/**
Li Zefana043e3b2008-02-23 15:24:09 -08004648 * cgroup_init_early - cgroup initialization at system boot
4649 *
4650 * Initialize cgroups at system boot, and initialize any
4651 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652 */
4653int __init cgroup_init_early(void)
4654{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004655 static struct cgroup_sb_opts __initdata opts =
4656 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004657 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004659
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004660 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004661 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004662
Tejun Heo3ed80a62014-02-08 10:36:58 -05004663 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004664 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004665 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4666 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004667 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004668 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4669 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004670
Tejun Heoaec25022014-02-08 10:36:58 -05004671 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004672 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673
4674 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004675 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676 }
4677 return 0;
4678}
4679
4680/**
Li Zefana043e3b2008-02-23 15:24:09 -08004681 * cgroup_init - cgroup initialization
4682 *
4683 * Register cgroup filesystem and /proc file, and initialize
4684 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685 */
4686int __init cgroup_init(void)
4687{
Tejun Heo30159ec2013-06-25 11:53:37 -07004688 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004689 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004690 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004691
Tejun Heo2bd59d42014-02-11 11:52:49 -05004692 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004693
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004694 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004695
Tejun Heo82fe9b02013-06-25 11:53:37 -07004696 /* Add init_css_set to the hash table */
4697 key = css_set_hash(init_css_set.subsys);
4698 hash_add(css_set_table, &init_css_set.hlist, key);
4699
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004700 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004701
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004702 mutex_unlock(&cgroup_mutex);
4703
Tejun Heo172a2c062014-03-19 10:23:53 -04004704 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004705 if (ss->early_init) {
4706 struct cgroup_subsys_state *css =
4707 init_css_set.subsys[ss->id];
4708
4709 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4710 GFP_KERNEL);
4711 BUG_ON(css->id < 0);
4712 } else {
4713 cgroup_init_subsys(ss, false);
4714 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004715
Tejun Heo2d8f2432014-04-23 11:13:15 -04004716 list_add_tail(&init_css_set.e_cset_node[ssid],
4717 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4718
Tejun Heo172a2c062014-03-19 10:23:53 -04004719 /*
4720 * cftype registration needs kmalloc and can't be done
4721 * during early_init. Register base cftypes separately.
4722 */
4723 if (ss->base_cftypes)
4724 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4725 }
Greg KH676db4a2010-08-05 13:53:35 -07004726
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004728 if (!cgroup_kobj)
4729 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004730
4731 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004732 if (err < 0) {
4733 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004734 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004735 }
4736
4737 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004738 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004739}
Paul Menageb4f48b62007-10-18 23:39:33 -07004740
Tejun Heoe5fca242013-11-22 17:14:39 -05004741static int __init cgroup_wq_init(void)
4742{
4743 /*
4744 * There isn't much point in executing destruction path in
4745 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004746 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004747 *
4748 * We would prefer to do this in cgroup_init() above, but that
4749 * is called before init_workqueues(): so leave this until after.
4750 */
Tejun Heo1a115332014-02-12 19:06:19 -05004751 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004752 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004753
4754 /*
4755 * Used to destroy pidlists and separate to serve as flush domain.
4756 * Cap @max_active to 1 too.
4757 */
4758 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4759 0, 1);
4760 BUG_ON(!cgroup_pidlist_destroy_wq);
4761
Tejun Heoe5fca242013-11-22 17:14:39 -05004762 return 0;
4763}
4764core_initcall(cgroup_wq_init);
4765
Paul Menagea4243162007-10-18 23:39:35 -07004766/*
4767 * proc_cgroup_show()
4768 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4769 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004770 */
4771
4772/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004773int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004774{
4775 struct pid *pid;
4776 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004777 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004778 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004779 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004780
4781 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004782 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004783 if (!buf)
4784 goto out;
4785
4786 retval = -ESRCH;
4787 pid = m->private;
4788 tsk = get_pid_task(pid, PIDTYPE_PID);
4789 if (!tsk)
4790 goto out_free;
4791
4792 retval = 0;
4793
4794 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004795 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004796
Tejun Heo985ed672014-03-19 10:23:53 -04004797 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004798 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004799 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004800 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004801
Tejun Heoa2dd4242014-03-19 10:23:55 -04004802 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004803 continue;
4804
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004805 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004806 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004807 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004808 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004809 if (strlen(root->name))
4810 seq_printf(m, "%sname=%s", count ? "," : "",
4811 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004812 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004813 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004814 path = cgroup_path(cgrp, buf, PATH_MAX);
4815 if (!path) {
4816 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004817 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004818 }
4819 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004820 seq_putc(m, '\n');
4821 }
4822
4823out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004824 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004825 mutex_unlock(&cgroup_mutex);
4826 put_task_struct(tsk);
4827out_free:
4828 kfree(buf);
4829out:
4830 return retval;
4831}
4832
Paul Menagea4243162007-10-18 23:39:35 -07004833/* Display information about each subsystem and each hierarchy */
4834static int proc_cgroupstats_show(struct seq_file *m, void *v)
4835{
Tejun Heo30159ec2013-06-25 11:53:37 -07004836 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004837 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004838
Paul Menage8bab8dd2008-04-04 14:29:57 -07004839 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004840 /*
4841 * ideally we don't want subsystems moving around while we do this.
4842 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4843 * subsys/hierarchy state.
4844 */
Paul Menagea4243162007-10-18 23:39:35 -07004845 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004846
4847 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004848 seq_printf(m, "%s\t%d\t%d\t%d\n",
4849 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004850 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004851
Paul Menagea4243162007-10-18 23:39:35 -07004852 mutex_unlock(&cgroup_mutex);
4853 return 0;
4854}
4855
4856static int cgroupstats_open(struct inode *inode, struct file *file)
4857{
Al Viro9dce07f2008-03-29 03:07:28 +00004858 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004859}
4860
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004861static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004862 .open = cgroupstats_open,
4863 .read = seq_read,
4864 .llseek = seq_lseek,
4865 .release = single_release,
4866};
4867
Paul Menageb4f48b62007-10-18 23:39:33 -07004868/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004869 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004870 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004871 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004872 * A task is associated with the init_css_set until cgroup_post_fork()
4873 * attaches it to the parent's css_set. Empty cg_list indicates that
4874 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004875 */
4876void cgroup_fork(struct task_struct *child)
4877{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004878 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004879 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004880}
4881
4882/**
Li Zefana043e3b2008-02-23 15:24:09 -08004883 * cgroup_post_fork - called on a new task after adding it to the task list
4884 * @child: the task in question
4885 *
Tejun Heo5edee612012-10-16 15:03:14 -07004886 * Adds the task to the list running through its css_set if necessary and
4887 * call the subsystem fork() callbacks. Has to be after the task is
4888 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004889 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004890 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004891 */
Paul Menage817929e2007-10-18 23:39:36 -07004892void cgroup_post_fork(struct task_struct *child)
4893{
Tejun Heo30159ec2013-06-25 11:53:37 -07004894 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004895 int i;
4896
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004897 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004898 * This may race against cgroup_enable_task_cg_links(). As that
4899 * function sets use_task_css_set_links before grabbing
4900 * tasklist_lock and we just went through tasklist_lock to add
4901 * @child, it's guaranteed that either we see the set
4902 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4903 * @child during its iteration.
4904 *
4905 * If we won the race, @child is associated with %current's
4906 * css_set. Grabbing css_set_rwsem guarantees both that the
4907 * association is stable, and, on completion of the parent's
4908 * migration, @child is visible in the source of migration or
4909 * already in the destination cgroup. This guarantee is necessary
4910 * when implementing operations which need to migrate all tasks of
4911 * a cgroup to another.
4912 *
4913 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4914 * will remain in init_css_set. This is safe because all tasks are
4915 * in the init_css_set before cg_links is enabled and there's no
4916 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004917 */
Paul Menage817929e2007-10-18 23:39:36 -07004918 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004919 struct css_set *cset;
4920
Tejun Heo96d365e2014-02-13 06:58:40 -05004921 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004922 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004923 if (list_empty(&child->cg_list)) {
4924 rcu_assign_pointer(child->cgroups, cset);
4925 list_add(&child->cg_list, &cset->tasks);
4926 get_css_set(cset);
4927 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004928 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004929 }
Tejun Heo5edee612012-10-16 15:03:14 -07004930
4931 /*
4932 * Call ss->fork(). This must happen after @child is linked on
4933 * css_set; otherwise, @child might change state between ->fork()
4934 * and addition to css_set.
4935 */
4936 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004937 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004938 if (ss->fork)
4939 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004940 }
Paul Menage817929e2007-10-18 23:39:36 -07004941}
Tejun Heo5edee612012-10-16 15:03:14 -07004942
Paul Menage817929e2007-10-18 23:39:36 -07004943/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004944 * cgroup_exit - detach cgroup from exiting task
4945 * @tsk: pointer to task_struct of exiting process
4946 *
4947 * Description: Detach cgroup from @tsk and release it.
4948 *
4949 * Note that cgroups marked notify_on_release force every task in
4950 * them to take the global cgroup_mutex mutex when exiting.
4951 * This could impact scaling on very large systems. Be reluctant to
4952 * use notify_on_release cgroups where very high task exit scaling
4953 * is required on large systems.
4954 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004955 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4956 * call cgroup_exit() while the task is still competent to handle
4957 * notify_on_release(), then leave the task attached to the root cgroup in
4958 * each hierarchy for the remainder of its exit. No need to bother with
4959 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004960 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004961 */
Li Zefan1ec41832014-03-28 15:22:19 +08004962void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004963{
Tejun Heo30159ec2013-06-25 11:53:37 -07004964 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004965 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004966 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004967 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004968
4969 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004970 * Unlink from @tsk from its css_set. As migration path can't race
4971 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004972 */
4973 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004974 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004975 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004976 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004977 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004978 }
4979
Paul Menageb4f48b62007-10-18 23:39:33 -07004980 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004981 cset = task_css_set(tsk);
4982 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004983
Li Zefan1ec41832014-03-28 15:22:19 +08004984 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004985 /* see cgroup_post_fork() for details */
4986 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004987 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004988 struct cgroup_subsys_state *old_css = cset->subsys[i];
4989 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004990
Tejun Heoeb954192013-08-08 20:11:23 -04004991 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004992 }
4993 }
4994 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004995
Tejun Heoeaf797a2014-02-25 10:04:03 -05004996 if (put_cset)
4997 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004998}
Paul Menage697f4162007-10-18 23:39:34 -07004999
Paul Menagebd89aab2007-10-18 23:40:44 -07005000static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005001{
Li Zefanf50daa72013-03-01 15:06:07 +08005002 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005003 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005004 /*
5005 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005006 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005007 * it now
5008 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005009 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005010
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005011 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005012 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005013 list_empty(&cgrp->release_list)) {
5014 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005015 need_schedule_work = 1;
5016 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005017 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005018 if (need_schedule_work)
5019 schedule_work(&release_agent_work);
5020 }
5021}
5022
Paul Menage81a6a5c2007-10-18 23:39:38 -07005023/*
5024 * Notify userspace when a cgroup is released, by running the
5025 * configured release agent with the name of the cgroup (path
5026 * relative to the root of cgroup file system) as the argument.
5027 *
5028 * Most likely, this user command will try to rmdir this cgroup.
5029 *
5030 * This races with the possibility that some other task will be
5031 * attached to this cgroup before it is removed, or that some other
5032 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5033 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5034 * unused, and this cgroup will be reprieved from its death sentence,
5035 * to continue to serve a useful existence. Next time it's released,
5036 * we will get notified again, if it still has 'notify_on_release' set.
5037 *
5038 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5039 * means only wait until the task is successfully execve()'d. The
5040 * separate release agent task is forked by call_usermodehelper(),
5041 * then control in this thread returns here, without waiting for the
5042 * release agent task. We don't bother to wait because the caller of
5043 * this routine has no use for the exit status of the release agent
5044 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005045 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046static void cgroup_release_agent(struct work_struct *work)
5047{
5048 BUG_ON(work != &release_agent_work);
5049 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005050 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 while (!list_empty(&release_list)) {
5052 char *argv[3], *envp[3];
5053 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005054 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005055 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005056 struct cgroup,
5057 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005058 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005059 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005060 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005061 if (!pathbuf)
5062 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005063 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5064 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005065 goto continue_free;
5066 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5067 if (!agentbuf)
5068 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069
5070 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005071 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005072 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005073 argv[i] = NULL;
5074
5075 i = 0;
5076 /* minimal command environment */
5077 envp[i++] = "HOME=/";
5078 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5079 envp[i] = NULL;
5080
5081 /* Drop the lock while we invoke the usermode helper,
5082 * since the exec could involve hitting disk and hence
5083 * be a slow process */
5084 mutex_unlock(&cgroup_mutex);
5085 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005086 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005087 continue_free:
5088 kfree(pathbuf);
5089 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005090 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005091 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005092 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005093 mutex_unlock(&cgroup_mutex);
5094}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005095
5096static int __init cgroup_disable(char *str)
5097{
Tejun Heo30159ec2013-06-25 11:53:37 -07005098 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005099 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005100 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005101
5102 while ((token = strsep(&str, ",")) != NULL) {
5103 if (!*token)
5104 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005105
Tejun Heo3ed80a62014-02-08 10:36:58 -05005106 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005107 if (!strcmp(token, ss->name)) {
5108 ss->disabled = 1;
5109 printk(KERN_INFO "Disabling %s control group"
5110 " subsystem\n", ss->name);
5111 break;
5112 }
5113 }
5114 }
5115 return 1;
5116}
5117__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005118
Tejun Heob77d7b62013-08-13 11:01:54 -04005119/**
Tejun Heoec903c02014-05-13 12:11:01 -04005120 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005121 * @dentry: directory dentry of interest
5122 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005123 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005124 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5125 * to get the corresponding css and return it. If such css doesn't exist
5126 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005127 */
Tejun Heoec903c02014-05-13 12:11:01 -04005128struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5129 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005130{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005131 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5132 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005133 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005134
Tejun Heo35cf0832013-08-26 18:40:56 -04005135 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005136 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5137 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005138 return ERR_PTR(-EBADF);
5139
Tejun Heo5a17f542014-02-11 11:52:47 -05005140 rcu_read_lock();
5141
Tejun Heo2bd59d42014-02-11 11:52:49 -05005142 /*
5143 * This path doesn't originate from kernfs and @kn could already
5144 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005145 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005146 */
5147 cgrp = rcu_dereference(kn->priv);
5148 if (cgrp)
5149 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005150
Tejun Heoec903c02014-05-13 12:11:01 -04005151 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005152 css = ERR_PTR(-ENOENT);
5153
5154 rcu_read_unlock();
5155 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005156}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005157
Li Zefan1cb650b2013-08-19 10:05:24 +08005158/**
5159 * css_from_id - lookup css by id
5160 * @id: the cgroup id
5161 * @ss: cgroup subsys to be looked into
5162 *
5163 * Returns the css if there's valid one with @id, otherwise returns NULL.
5164 * Should be called under rcu_read_lock().
5165 */
5166struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5167{
Tejun Heo6fa49182014-05-04 15:09:13 -04005168 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005169 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005170}
5171
Paul Menagefe693432009-09-23 15:56:20 -07005172#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005173static struct cgroup_subsys_state *
5174debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005175{
5176 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5177
5178 if (!css)
5179 return ERR_PTR(-ENOMEM);
5180
5181 return css;
5182}
5183
Tejun Heoeb954192013-08-08 20:11:23 -04005184static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005185{
Tejun Heoeb954192013-08-08 20:11:23 -04005186 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005187}
5188
Tejun Heo182446d2013-08-08 20:11:24 -04005189static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5190 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005191{
Tejun Heo182446d2013-08-08 20:11:24 -04005192 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005193}
5194
Tejun Heo182446d2013-08-08 20:11:24 -04005195static u64 current_css_set_read(struct cgroup_subsys_state *css,
5196 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005197{
5198 return (u64)(unsigned long)current->cgroups;
5199}
5200
Tejun Heo182446d2013-08-08 20:11:24 -04005201static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005202 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005203{
5204 u64 count;
5205
5206 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005207 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005208 rcu_read_unlock();
5209 return count;
5210}
5211
Tejun Heo2da8ca82013-12-05 12:28:04 -05005212static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005213{
Tejun Heo69d02062013-06-12 21:04:50 -07005214 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005215 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005216 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005217
Tejun Heoe61734c2014-02-12 09:29:50 -05005218 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5219 if (!name_buf)
5220 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005221
Tejun Heo96d365e2014-02-13 06:58:40 -05005222 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005223 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005224 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005225 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005226 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005227
Tejun Heoa2dd4242014-03-19 10:23:55 -04005228 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005229 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005230 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005231 }
5232 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005233 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005234 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005235 return 0;
5236}
5237
5238#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005239static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005240{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005241 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005242 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005243
Tejun Heo96d365e2014-02-13 06:58:40 -05005244 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005245 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005246 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005247 struct task_struct *task;
5248 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005249
Tejun Heo5abb8852013-06-12 21:04:49 -07005250 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005251
Tejun Heo5abb8852013-06-12 21:04:49 -07005252 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005253 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5254 goto overflow;
5255 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005256 }
Tejun Heoc7561122014-02-25 10:04:01 -05005257
5258 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5259 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5260 goto overflow;
5261 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5262 }
5263 continue;
5264 overflow:
5265 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005266 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005267 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005268 return 0;
5269}
5270
Tejun Heo182446d2013-08-08 20:11:24 -04005271static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005272{
Tejun Heo182446d2013-08-08 20:11:24 -04005273 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005274}
5275
5276static struct cftype debug_files[] = {
5277 {
Paul Menagefe693432009-09-23 15:56:20 -07005278 .name = "taskcount",
5279 .read_u64 = debug_taskcount_read,
5280 },
5281
5282 {
5283 .name = "current_css_set",
5284 .read_u64 = current_css_set_read,
5285 },
5286
5287 {
5288 .name = "current_css_set_refcount",
5289 .read_u64 = current_css_set_refcount_read,
5290 },
5291
5292 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005293 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005294 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005295 },
5296
5297 {
5298 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005299 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005300 },
5301
5302 {
Paul Menagefe693432009-09-23 15:56:20 -07005303 .name = "releasable",
5304 .read_u64 = releasable_read,
5305 },
Paul Menagefe693432009-09-23 15:56:20 -07005306
Tejun Heo4baf6e32012-04-01 12:09:55 -07005307 { } /* terminate */
5308};
Paul Menagefe693432009-09-23 15:56:20 -07005309
Tejun Heo073219e2014-02-08 10:36:58 -05005310struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005311 .css_alloc = debug_css_alloc,
5312 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005313 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005314};
5315#endif /* CONFIG_CGROUP_DEBUG */