blob: e694f4153edb22d824c8f07183cc73313335e820 [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 Heo42809dd2012-11-19 08:13:37 -0800181static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400182static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
183static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400184static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
185 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500186static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800187
Tejun Heo6fa49182014-05-04 15:09:13 -0400188/* IDR wrappers which synchronize using cgroup_idr_lock */
189static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
190 gfp_t gfp_mask)
191{
192 int ret;
193
194 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400195 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400196 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400197 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400198 idr_preload_end();
199 return ret;
200}
201
202static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
203{
204 void *ret;
205
Tejun Heo54504e92014-05-13 12:10:59 -0400206 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400207 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400208 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400209 return ret;
210}
211
212static void cgroup_idr_remove(struct idr *idr, int id)
213{
Tejun Heo54504e92014-05-13 12:10:59 -0400214 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400215 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400216 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400217}
218
Tejun Heo95109b62013-08-08 20:11:27 -0400219/**
220 * cgroup_css - obtain a cgroup's css for the specified subsystem
221 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400222 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400223 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400224 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
225 * function must be called either under cgroup_mutex or rcu_read_lock() and
226 * the caller is responsible for pinning the returned css if it wants to
227 * keep accessing it outside the said locks. This function may return
228 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400229 */
230static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400231 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400232{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400233 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500234 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500235 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400236 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400237 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400238}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700239
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400240/**
241 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
242 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400243 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400244 *
245 * Similar to cgroup_css() but returns the effctive css, which is defined
246 * as the matching css of the nearest ancestor including self which has @ss
247 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
248 * function is guaranteed to return non-NULL css.
249 */
250static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
251 struct cgroup_subsys *ss)
252{
253 lockdep_assert_held(&cgroup_mutex);
254
255 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400256 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400257
258 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
259 return NULL;
260
261 while (cgrp->parent &&
262 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
263 cgrp = cgrp->parent;
264
265 return cgroup_css(cgrp, ss);
266}
267
Paul Menageddbcc7e2007-10-18 23:39:30 -0700268/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700269static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700270{
Tejun Heo54766d42013-06-12 21:04:53 -0700271 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700272}
273
Tejun Heob4168642014-05-13 12:16:21 -0400274struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500275{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500276 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400277 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500278
279 /*
280 * This is open and unprotected implementation of cgroup_css().
281 * seq_css() is only called from a kernfs file operation which has
282 * an active reference on the file. Because all the subsystem
283 * files are drained before a css is disassociated with a cgroup,
284 * the matching css from the cgroup's subsys table is guaranteed to
285 * be and stay valid until the enclosing operation is complete.
286 */
287 if (cft->ss)
288 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
289 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400290 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500291}
Tejun Heob4168642014-05-13 12:16:21 -0400292EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500293
Li Zefan78574cf2013-04-08 19:00:38 -0700294/**
295 * cgroup_is_descendant - test ancestry
296 * @cgrp: the cgroup to be tested
297 * @ancestor: possible ancestor of @cgrp
298 *
299 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
300 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
301 * and @ancestor are accessible.
302 */
303bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
304{
305 while (cgrp) {
306 if (cgrp == ancestor)
307 return true;
308 cgrp = cgrp->parent;
309 }
310 return false;
311}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700312
Adrian Bunke9685a02008-02-07 00:13:46 -0800313static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314{
315 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700316 (1 << CGRP_RELEASABLE) |
317 (1 << CGRP_NOTIFY_ON_RELEASE);
318 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700319}
320
Adrian Bunke9685a02008-02-07 00:13:46 -0800321static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700322{
Paul Menagebd89aab2007-10-18 23:40:44 -0700323 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700324}
325
Tejun Heo30159ec2013-06-25 11:53:37 -0700326/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500327 * for_each_css - iterate all css's of a cgroup
328 * @css: the iteration cursor
329 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
330 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700331 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400332 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700333 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500334#define for_each_css(css, ssid, cgrp) \
335 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
336 if (!((css) = rcu_dereference_check( \
337 (cgrp)->subsys[(ssid)], \
338 lockdep_is_held(&cgroup_mutex)))) { } \
339 else
340
341/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400342 * for_each_e_css - iterate all effective css's of a cgroup
343 * @css: the iteration cursor
344 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
345 * @cgrp: the target cgroup to iterate css's of
346 *
347 * Should be called under cgroup_[tree_]mutex.
348 */
349#define for_each_e_css(css, ssid, cgrp) \
350 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
351 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
352 ; \
353 else
354
355/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500356 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700357 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500358 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700359 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500360#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500361 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
362 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700363
Tejun Heo985ed672014-03-19 10:23:53 -0400364/* iterate across the hierarchies */
365#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700366 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700367
Tejun Heof8f22e52014-04-23 11:13:16 -0400368/* iterate over child cgrps, lock should be held throughout iteration */
369#define cgroup_for_each_live_child(child, cgrp) \
370 list_for_each_entry((child), &(cgrp)->children, sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400371 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400372 cgroup_is_dead(child); })) \
373 ; \
374 else
375
Paul Menage81a6a5c2007-10-18 23:39:38 -0700376/* the list of cgroups eligible for automatic release. Protected by
377 * release_list_lock */
378static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200379static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700380static void cgroup_release_agent(struct work_struct *work);
381static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700382static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700383
Tejun Heo69d02062013-06-12 21:04:50 -0700384/*
385 * A cgroup can be associated with multiple css_sets as different tasks may
386 * belong to different cgroups on different hierarchies. In the other
387 * direction, a css_set is naturally associated with multiple cgroups.
388 * This M:N relationship is represented by the following link structure
389 * which exists for each association and allows traversing the associations
390 * from both sides.
391 */
392struct cgrp_cset_link {
393 /* the cgroup and css_set this link associates */
394 struct cgroup *cgrp;
395 struct css_set *cset;
396
397 /* list of cgrp_cset_links anchored at cgrp->cset_links */
398 struct list_head cset_link;
399
400 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
401 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700402};
403
Tejun Heo172a2c062014-03-19 10:23:53 -0400404/*
405 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700406 * hierarchies being mounted. It contains a pointer to the root state
407 * for each subsystem. Also used to anchor the list of css_sets. Not
408 * reference-counted, to improve performance when child cgroups
409 * haven't been created.
410 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400411struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400412 .refcount = ATOMIC_INIT(1),
413 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
414 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
415 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
416 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
417 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
418};
Paul Menage817929e2007-10-18 23:39:36 -0700419
Tejun Heo172a2c062014-03-19 10:23:53 -0400420static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700421
Tejun Heo842b5972014-04-25 18:28:02 -0400422/**
423 * cgroup_update_populated - updated populated count of a cgroup
424 * @cgrp: the target cgroup
425 * @populated: inc or dec populated count
426 *
427 * @cgrp is either getting the first task (css_set) or losing the last.
428 * Update @cgrp->populated_cnt accordingly. The count is propagated
429 * towards root so that a given cgroup's populated_cnt is zero iff the
430 * cgroup and all its descendants are empty.
431 *
432 * @cgrp's interface file "cgroup.populated" is zero if
433 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
434 * changes from or to zero, userland is notified that the content of the
435 * interface file has changed. This can be used to detect when @cgrp and
436 * its descendants become populated or empty.
437 */
438static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
439{
440 lockdep_assert_held(&css_set_rwsem);
441
442 do {
443 bool trigger;
444
445 if (populated)
446 trigger = !cgrp->populated_cnt++;
447 else
448 trigger = !--cgrp->populated_cnt;
449
450 if (!trigger)
451 break;
452
453 if (cgrp->populated_kn)
454 kernfs_notify(cgrp->populated_kn);
455 cgrp = cgrp->parent;
456 } while (cgrp);
457}
458
Paul Menage7717f7b2009-09-23 15:56:22 -0700459/*
460 * hash table for cgroup groups. This improves the performance to find
461 * an existing css_set. This hash doesn't (currently) take into
462 * account cgroups in empty hierarchies.
463 */
Li Zefan472b1052008-04-29 01:00:11 -0700464#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800465static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700466
Li Zefan0ac801f2013-01-10 11:49:27 +0800467static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700468{
Li Zefan0ac801f2013-01-10 11:49:27 +0800469 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700470 struct cgroup_subsys *ss;
471 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700472
Tejun Heo30159ec2013-06-25 11:53:37 -0700473 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800474 key += (unsigned long)css[i];
475 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700476
Li Zefan0ac801f2013-01-10 11:49:27 +0800477 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700478}
479
Tejun Heo89c55092014-02-13 06:58:40 -0500480static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700481{
Tejun Heo69d02062013-06-12 21:04:50 -0700482 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400483 struct cgroup_subsys *ss;
484 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700485
Tejun Heo89c55092014-02-13 06:58:40 -0500486 lockdep_assert_held(&css_set_rwsem);
487
488 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700489 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700490
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700491 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400492 for_each_subsys(ss, ssid)
493 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700494 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700495 css_set_count--;
496
Tejun Heo69d02062013-06-12 21:04:50 -0700497 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700498 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700499
Tejun Heo69d02062013-06-12 21:04:50 -0700500 list_del(&link->cset_link);
501 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800502
Tejun Heo96d365e2014-02-13 06:58:40 -0500503 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400504 if (list_empty(&cgrp->cset_links)) {
505 cgroup_update_populated(cgrp, false);
506 if (notify_on_release(cgrp)) {
507 if (taskexit)
508 set_bit(CGRP_RELEASABLE, &cgrp->flags);
509 check_for_release(cgrp);
510 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700511 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700512
513 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700514 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700515
Tejun Heo5abb8852013-06-12 21:04:49 -0700516 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700517}
518
Tejun Heo89c55092014-02-13 06:58:40 -0500519static void put_css_set(struct css_set *cset, bool taskexit)
520{
521 /*
522 * Ensure that the refcount doesn't hit zero while any readers
523 * can see it. Similar to atomic_dec_and_lock(), but for an
524 * rwlock
525 */
526 if (atomic_add_unless(&cset->refcount, -1, 1))
527 return;
528
529 down_write(&css_set_rwsem);
530 put_css_set_locked(cset, taskexit);
531 up_write(&css_set_rwsem);
532}
533
Paul Menage817929e2007-10-18 23:39:36 -0700534/*
535 * refcounted get/put for css_set objects
536 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700537static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700538{
Tejun Heo5abb8852013-06-12 21:04:49 -0700539 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700540}
541
Tejun Heob326f9d2013-06-24 15:21:48 -0700542/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700543 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700544 * @cset: candidate css_set being tested
545 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700546 * @new_cgrp: cgroup that's being entered by the task
547 * @template: desired set of css pointers in css_set (pre-calculated)
548 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800549 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700550 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
551 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700552static bool compare_css_sets(struct css_set *cset,
553 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700554 struct cgroup *new_cgrp,
555 struct cgroup_subsys_state *template[])
556{
557 struct list_head *l1, *l2;
558
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400559 /*
560 * On the default hierarchy, there can be csets which are
561 * associated with the same set of cgroups but different csses.
562 * Let's first ensure that csses match.
563 */
564 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700565 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700566
567 /*
568 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400569 * different cgroups in hierarchies. As different cgroups may
570 * share the same effective css, this comparison is always
571 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700572 */
Tejun Heo69d02062013-06-12 21:04:50 -0700573 l1 = &cset->cgrp_links;
574 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700575 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700576 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700577 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700578
579 l1 = l1->next;
580 l2 = l2->next;
581 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700582 if (l1 == &cset->cgrp_links) {
583 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700584 break;
585 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700586 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700587 }
588 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700589 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
590 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
591 cgrp1 = link1->cgrp;
592 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700593 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700594 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700595
596 /*
597 * If this hierarchy is the hierarchy of the cgroup
598 * that's changing, then we need to check that this
599 * css_set points to the new cgroup; if it's any other
600 * hierarchy, then this css_set should point to the
601 * same cgroup as the old css_set.
602 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700603 if (cgrp1->root == new_cgrp->root) {
604 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700605 return false;
606 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700607 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700608 return false;
609 }
610 }
611 return true;
612}
613
Tejun Heob326f9d2013-06-24 15:21:48 -0700614/**
615 * find_existing_css_set - init css array and find the matching css_set
616 * @old_cset: the css_set that we're using before the cgroup transition
617 * @cgrp: the cgroup that we're moving into
618 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700619 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700620static struct css_set *find_existing_css_set(struct css_set *old_cset,
621 struct cgroup *cgrp,
622 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700623{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400624 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700625 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700626 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800627 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700628 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700629
Ben Blumaae8aab2010-03-10 15:22:07 -0800630 /*
631 * Build the set of subsystem state objects that we want to see in the
632 * new css_set. while subsystems can change globally, the entries here
633 * won't change, so no need for locking.
634 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700635 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400636 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400637 /*
638 * @ss is in this hierarchy, so we want the
639 * effective css from @cgrp.
640 */
641 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700642 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400643 /*
644 * @ss is not in this hierarchy, so we don't want
645 * to change the css.
646 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700647 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700648 }
649 }
650
Li Zefan0ac801f2013-01-10 11:49:27 +0800651 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 hash_for_each_possible(css_set_table, cset, hlist, key) {
653 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700654 continue;
655
656 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700658 }
Paul Menage817929e2007-10-18 23:39:36 -0700659
660 /* No existing cgroup group matched */
661 return NULL;
662}
663
Tejun Heo69d02062013-06-12 21:04:50 -0700664static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700665{
Tejun Heo69d02062013-06-12 21:04:50 -0700666 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700667
Tejun Heo69d02062013-06-12 21:04:50 -0700668 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
669 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700670 kfree(link);
671 }
672}
673
Tejun Heo69d02062013-06-12 21:04:50 -0700674/**
675 * allocate_cgrp_cset_links - allocate cgrp_cset_links
676 * @count: the number of links to allocate
677 * @tmp_links: list_head the allocated links are put on
678 *
679 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
680 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700681 */
Tejun Heo69d02062013-06-12 21:04:50 -0700682static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700683{
Tejun Heo69d02062013-06-12 21:04:50 -0700684 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700685 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700686
687 INIT_LIST_HEAD(tmp_links);
688
Li Zefan36553432008-07-29 22:33:19 -0700689 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700690 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700691 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700692 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700693 return -ENOMEM;
694 }
Tejun Heo69d02062013-06-12 21:04:50 -0700695 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700696 }
697 return 0;
698}
699
Li Zefanc12f65d2009-01-07 18:07:42 -0800700/**
701 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700702 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800704 * @cgrp: the destination cgroup
705 */
Tejun Heo69d02062013-06-12 21:04:50 -0700706static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
707 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800708{
Tejun Heo69d02062013-06-12 21:04:50 -0700709 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800710
Tejun Heo69d02062013-06-12 21:04:50 -0700711 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400712
713 if (cgroup_on_dfl(cgrp))
714 cset->dfl_cgrp = cgrp;
715
Tejun Heo69d02062013-06-12 21:04:50 -0700716 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
717 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400719
720 if (list_empty(&cgrp->cset_links))
721 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700722 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400723
Paul Menage7717f7b2009-09-23 15:56:22 -0700724 /*
725 * Always add links to the tail of the list so that the list
726 * is sorted by order of hierarchy creation
727 */
Tejun Heo69d02062013-06-12 21:04:50 -0700728 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800729}
730
Tejun Heob326f9d2013-06-24 15:21:48 -0700731/**
732 * find_css_set - return a new css_set with one cgroup updated
733 * @old_cset: the baseline css_set
734 * @cgrp: the cgroup to be updated
735 *
736 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
737 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700738 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700739static struct css_set *find_css_set(struct css_set *old_cset,
740 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700741{
Tejun Heob326f9d2013-06-24 15:21:48 -0700742 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700743 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700744 struct list_head tmp_links;
745 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400746 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800747 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400748 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700749
Tejun Heob326f9d2013-06-24 15:21:48 -0700750 lockdep_assert_held(&cgroup_mutex);
751
Paul Menage817929e2007-10-18 23:39:36 -0700752 /* First see if we already have a cgroup group that matches
753 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500754 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700755 cset = find_existing_css_set(old_cset, cgrp, template);
756 if (cset)
757 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500758 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700759
Tejun Heo5abb8852013-06-12 21:04:49 -0700760 if (cset)
761 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700762
Tejun Heof4f4be22013-06-12 21:04:51 -0700763 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700764 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700765 return NULL;
766
Tejun Heo69d02062013-06-12 21:04:50 -0700767 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700768 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700769 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700770 return NULL;
771 }
772
Tejun Heo5abb8852013-06-12 21:04:49 -0700773 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700774 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700775 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500776 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500777 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500778 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700779 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700780
781 /* Copy the set of subsystem state objects generated in
782 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700783 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700784
Tejun Heo96d365e2014-02-13 06:58:40 -0500785 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700786 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700787 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700788 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700789
Paul Menage7717f7b2009-09-23 15:56:22 -0700790 if (c->root == cgrp->root)
791 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700792 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700793 }
Paul Menage817929e2007-10-18 23:39:36 -0700794
Tejun Heo69d02062013-06-12 21:04:50 -0700795 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700796
Paul Menage817929e2007-10-18 23:39:36 -0700797 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700798
Tejun Heo2d8f2432014-04-23 11:13:15 -0400799 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700800 key = css_set_hash(cset->subsys);
801 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700802
Tejun Heo2d8f2432014-04-23 11:13:15 -0400803 for_each_subsys(ss, ssid)
804 list_add_tail(&cset->e_cset_node[ssid],
805 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
806
Tejun Heo96d365e2014-02-13 06:58:40 -0500807 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700808
Tejun Heo5abb8852013-06-12 21:04:49 -0700809 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700810}
811
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400812static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700813{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400814 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500815
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400816 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500817}
818
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400819static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500820{
821 int id;
822
823 lockdep_assert_held(&cgroup_mutex);
824
Tejun Heo985ed672014-03-19 10:23:53 -0400825 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500826 if (id < 0)
827 return id;
828
829 root->hierarchy_id = id;
830 return 0;
831}
832
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400833static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500834{
835 lockdep_assert_held(&cgroup_mutex);
836
837 if (root->hierarchy_id) {
838 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
839 root->hierarchy_id = 0;
840 }
841}
842
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400843static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500844{
845 if (root) {
846 /* hierarhcy ID shoulid already have been released */
847 WARN_ON_ONCE(root->hierarchy_id);
848
849 idr_destroy(&root->cgroup_idr);
850 kfree(root);
851 }
852}
853
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400854static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500855{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400856 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500857 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500858
Tejun Heo2bd59d42014-02-11 11:52:49 -0500859 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500860
Tejun Heo776f02f2014-02-12 09:29:50 -0500861 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500862 BUG_ON(!list_empty(&cgrp->children));
863
Tejun Heof2e85d52014-02-11 11:52:49 -0500864 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400865 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500866
867 /*
868 * Release all the links from cset_links to this hierarchy's
869 * root cgroup
870 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500871 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500872
873 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
874 list_del(&link->cset_link);
875 list_del(&link->cgrp_link);
876 kfree(link);
877 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500878 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500879
880 if (!list_empty(&root->root_list)) {
881 list_del(&root->root_list);
882 cgroup_root_count--;
883 }
884
885 cgroup_exit_root_id(root);
886
887 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500888
Tejun Heo2bd59d42014-02-11 11:52:49 -0500889 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500890 cgroup_free_root(root);
891}
892
Tejun Heoceb6a082014-02-25 10:04:02 -0500893/* look up cgroup associated with given css_set on the specified hierarchy */
894static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400895 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700896{
Paul Menage7717f7b2009-09-23 15:56:22 -0700897 struct cgroup *res = NULL;
898
Tejun Heo96d365e2014-02-13 06:58:40 -0500899 lockdep_assert_held(&cgroup_mutex);
900 lockdep_assert_held(&css_set_rwsem);
901
Tejun Heo5abb8852013-06-12 21:04:49 -0700902 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400903 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700904 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700905 struct cgrp_cset_link *link;
906
907 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700908 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700909
Paul Menage7717f7b2009-09-23 15:56:22 -0700910 if (c->root == root) {
911 res = c;
912 break;
913 }
914 }
915 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500916
Paul Menage7717f7b2009-09-23 15:56:22 -0700917 BUG_ON(!res);
918 return res;
919}
920
921/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500922 * Return the cgroup for "task" from the given hierarchy. Must be
923 * called with cgroup_mutex and css_set_rwsem held.
924 */
925static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400926 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500927{
928 /*
929 * No need to lock the task - since we hold cgroup_mutex the
930 * task can't change groups, so the only thing that can happen
931 * is that it exits and its css is set back to init_css_set.
932 */
933 return cset_cgroup_from_root(task_css_set(task), root);
934}
935
936/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937 * A task must hold cgroup_mutex to modify cgroups.
938 *
939 * Any task can increment and decrement the count field without lock.
940 * So in general, code holding cgroup_mutex can't rely on the count
941 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800942 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700943 * means that no tasks are currently attached, therefore there is no
944 * way a task attached to that cgroup can fork (the other way to
945 * increment the count). So code holding cgroup_mutex can safely
946 * assume that if the count is zero, it will stay zero. Similarly, if
947 * a task holds cgroup_mutex on a cgroup with zero count, it
948 * knows that the cgroup won't be removed, as cgroup_rmdir()
949 * needs that mutex.
950 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
952 * (usually) take cgroup_mutex. These are the two most performance
953 * critical pieces of code here. The exception occurs on cgroup_exit(),
954 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
955 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800956 * to the release agent with the name of the cgroup (path relative to
957 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958 *
959 * A cgroup can only be deleted if both its 'count' of using tasks
960 * is zero, and its list of 'children' cgroups is empty. Since all
961 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400962 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700963 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400964 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965 *
966 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800967 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968 */
969
Tejun Heo69dfa002014-05-04 15:09:13 -0400970static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500971static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700972static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700973
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500974static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
975 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500977 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
978 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
979 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
980 cft->ss->name, cft->name);
981 else
982 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
983 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984}
985
Tejun Heof2e85d52014-02-11 11:52:49 -0500986/**
987 * cgroup_file_mode - deduce file mode of a control file
988 * @cft: the control file in question
989 *
990 * returns cft->mode if ->mode is not 0
991 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
992 * returns S_IRUGO if it has only a read handler
993 * returns S_IWUSR if it has only a write hander
994 */
995static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800996{
Tejun Heof2e85d52014-02-11 11:52:49 -0500997 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800998
Tejun Heof2e85d52014-02-11 11:52:49 -0500999 if (cft->mode)
1000 return cft->mode;
1001
1002 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1003 mode |= S_IRUGO;
1004
Tejun Heo6770c642014-05-13 12:16:21 -04001005 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001006 mode |= S_IWUSR;
1007
1008 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001009}
1010
Li Zefanbe445622013-01-24 14:31:42 +08001011static void cgroup_free_fn(struct work_struct *work)
1012{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001013 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001014
Tejun Heo3c9c8252014-02-12 09:29:50 -05001015 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001016 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001017
Tejun Heo776f02f2014-02-12 09:29:50 -05001018 if (cgrp->parent) {
1019 /*
1020 * We get a ref to the parent, and put the ref when this
1021 * cgroup is being freed, so it's guaranteed that the
1022 * parent won't be destroyed before its children.
1023 */
1024 cgroup_put(cgrp->parent);
1025 kernfs_put(cgrp->kn);
1026 kfree(cgrp);
1027 } else {
1028 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001029 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001030 * indicates that the root should be released.
1031 */
1032 cgroup_destroy_root(cgrp->root);
1033 }
Li Zefanbe445622013-01-24 14:31:42 +08001034}
1035
1036static void cgroup_free_rcu(struct rcu_head *head)
1037{
1038 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1039
Tejun Heoea15f8c2013-06-13 19:27:42 -07001040 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001041 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001042}
1043
Tejun Heo59f52962014-02-11 11:52:49 -05001044static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001046 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1047 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1048 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049}
1050
Tejun Heo59f52962014-02-11 11:52:49 -05001051static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001053 if (!atomic_dec_and_test(&cgrp->refcnt))
1054 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001055 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001056 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057
Tejun Heo4e4e2842014-05-14 09:15:01 -04001058 /* delete this cgroup from parent->children */
1059 mutex_lock(&cgroup_mutex);
1060 list_del_rcu(&cgrp->sibling);
1061 mutex_unlock(&cgroup_mutex);
1062
Tejun Heo6fa49182014-05-04 15:09:13 -04001063 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001064 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065
Tejun Heo2bd59d42014-02-11 11:52:49 -05001066 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067}
1068
Tejun Heoa9746d82014-05-13 12:19:22 -04001069/**
1070 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1071 * @kn: the kernfs_node being serviced
1072 *
1073 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1074 * the method finishes if locking succeeded. Note that once this function
1075 * returns the cgroup returned by cgroup_kn_lock_live() may become
1076 * inaccessible any time. If the caller intends to continue to access the
1077 * cgroup, it should pin it before invoking this function.
1078 */
1079static void cgroup_kn_unlock(struct kernfs_node *kn)
1080{
1081 struct cgroup *cgrp;
1082
1083 if (kernfs_type(kn) == KERNFS_DIR)
1084 cgrp = kn->priv;
1085 else
1086 cgrp = kn->parent->priv;
1087
1088 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001089
1090 kernfs_unbreak_active_protection(kn);
1091 cgroup_put(cgrp);
1092}
1093
1094/**
1095 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1096 * @kn: the kernfs_node being serviced
1097 *
1098 * This helper is to be used by a cgroup kernfs method currently servicing
1099 * @kn. It breaks the active protection, performs cgroup locking and
1100 * verifies that the associated cgroup is alive. Returns the cgroup if
1101 * alive; otherwise, %NULL. A successful return should be undone by a
1102 * matching cgroup_kn_unlock() invocation.
1103 *
1104 * Any cgroup kernfs method implementation which requires locking the
1105 * associated cgroup should use this helper. It avoids nesting cgroup
1106 * locking under kernfs active protection and allows all kernfs operations
1107 * including self-removal.
1108 */
1109static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1110{
1111 struct cgroup *cgrp;
1112
1113 if (kernfs_type(kn) == KERNFS_DIR)
1114 cgrp = kn->priv;
1115 else
1116 cgrp = kn->parent->priv;
1117
1118 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001119 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001120 * active_ref. cgroup liveliness check alone provides enough
1121 * protection against removal. Ensure @cgrp stays accessible and
1122 * break the active_ref protection.
1123 */
1124 cgroup_get(cgrp);
1125 kernfs_break_active_protection(kn);
1126
Tejun Heoa9746d82014-05-13 12:19:22 -04001127 mutex_lock(&cgroup_mutex);
1128
1129 if (!cgroup_is_dead(cgrp))
1130 return cgrp;
1131
1132 cgroup_kn_unlock(kn);
1133 return NULL;
1134}
1135
Li Zefan2739d3c2013-01-21 18:18:33 +08001136static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001138 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
Tejun Heo01f64742014-05-13 12:19:23 -04001140 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001141 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001142}
1143
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001144/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001145 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001146 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001147 * @subsys_mask: mask of the subsystem ids whose files should be removed
1148 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001149static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001150{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001151 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001152 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001153
Tejun Heob420ba72013-07-12 12:34:02 -07001154 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001155 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001156
Tejun Heo69dfa002014-05-04 15:09:13 -04001157 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001158 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001159 list_for_each_entry(cfts, &ss->cfts, node)
1160 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001161 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162}
1163
Tejun Heo69dfa002014-05-04 15:09:13 -04001164static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001165{
Tejun Heo30159ec2013-06-25 11:53:37 -07001166 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001167 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001168
Tejun Heoace2bee2014-02-11 11:52:47 -05001169 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001170
Tejun Heo5df36032014-03-19 10:23:54 -04001171 for_each_subsys(ss, ssid) {
1172 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001173 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001174
Tejun Heo7fd8c562014-04-23 11:13:16 -04001175 /* if @ss has non-root csses attached to it, can't move */
1176 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001177 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001178
Tejun Heo5df36032014-03-19 10:23:54 -04001179 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001180 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001181 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182 }
1183
Tejun Heoa2dd4242014-03-19 10:23:55 -04001184 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1185 if (ret) {
1186 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001187 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001188
Tejun Heoa2dd4242014-03-19 10:23:55 -04001189 /*
1190 * Rebinding back to the default root is not allowed to
1191 * fail. Using both default and non-default roots should
1192 * be rare. Moving subsystems back and forth even more so.
1193 * Just warn about it and continue.
1194 */
1195 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001196 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001197 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001198 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001199 }
Tejun Heo5df36032014-03-19 10:23:54 -04001200 }
Tejun Heo31261212013-06-28 17:07:30 -07001201
1202 /*
1203 * Nothing can fail from this point on. Remove files for the
1204 * removed subsystems and rebind each subsystem.
1205 */
Tejun Heo5df36032014-03-19 10:23:54 -04001206 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001207 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001208 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001209
Tejun Heo5df36032014-03-19 10:23:54 -04001210 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001211 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001212 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001213 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001214
Tejun Heo5df36032014-03-19 10:23:54 -04001215 if (!(ss_mask & (1 << ssid)))
1216 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001217
Tejun Heo5df36032014-03-19 10:23:54 -04001218 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001219 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001220
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001221 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001222
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001223 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1224 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001225 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001226 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001227
Tejun Heo2d8f2432014-04-23 11:13:15 -04001228 down_write(&css_set_rwsem);
1229 hash_for_each(css_set_table, i, cset, hlist)
1230 list_move_tail(&cset->e_cset_node[ss->id],
1231 &dst_root->cgrp.e_csets[ss->id]);
1232 up_write(&css_set_rwsem);
1233
Tejun Heof392e512014-04-23 11:13:14 -04001234 src_root->subsys_mask &= ~(1 << ssid);
1235 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1236
Tejun Heobd53d612014-04-23 11:13:16 -04001237 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001238 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001239 if (dst_root != &cgrp_dfl_root)
1240 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001241
Tejun Heo5df36032014-03-19 10:23:54 -04001242 if (ss->bind)
1243 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001244 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245
Tejun Heoa2dd4242014-03-19 10:23:55 -04001246 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 return 0;
1248}
1249
Tejun Heo2bd59d42014-02-11 11:52:49 -05001250static int cgroup_show_options(struct seq_file *seq,
1251 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001253 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001254 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001255 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001256
Tejun Heob85d2042013-12-06 15:11:57 -05001257 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001258 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001259 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001260 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1261 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001262 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001264 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001265 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001266
1267 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001268 if (strlen(root->release_agent_path))
1269 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001270 spin_unlock(&release_agent_path_lock);
1271
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001272 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001273 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001274 if (strlen(root->name))
1275 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001276 return 0;
1277}
1278
1279struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001280 unsigned int subsys_mask;
1281 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001282 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001283 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001284 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001285 /* User explicitly requested empty subsystem */
1286 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287};
1288
Ben Blumcf5d5942010-03-10 15:22:09 -08001289static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001291 char *token, *o = data;
1292 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001293 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001294 struct cgroup_subsys *ss;
1295 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001296
1297#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001298 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001299#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001300
Paul Menagec6d57f32009-09-23 15:56:19 -07001301 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302
1303 while ((token = strsep(&o, ",")) != NULL) {
1304 if (!*token)
1305 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001306 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001307 /* Explicitly have no subsystems */
1308 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001309 continue;
1310 }
1311 if (!strcmp(token, "all")) {
1312 /* Mutually exclusive option 'all' + subsystem name */
1313 if (one_ss)
1314 return -EINVAL;
1315 all_ss = true;
1316 continue;
1317 }
Tejun Heo873fe092013-04-14 20:15:26 -07001318 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1319 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1320 continue;
1321 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001322 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001323 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001324 continue;
1325 }
1326 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001327 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001328 continue;
1329 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001330 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001331 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001332 continue;
1333 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001334 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001335 /* Specifying two release agents is forbidden */
1336 if (opts->release_agent)
1337 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001338 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001339 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001340 if (!opts->release_agent)
1341 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001342 continue;
1343 }
1344 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001345 const char *name = token + 5;
1346 /* Can't specify an empty name */
1347 if (!strlen(name))
1348 return -EINVAL;
1349 /* Must match [\w.-]+ */
1350 for (i = 0; i < strlen(name); i++) {
1351 char c = name[i];
1352 if (isalnum(c))
1353 continue;
1354 if ((c == '.') || (c == '-') || (c == '_'))
1355 continue;
1356 return -EINVAL;
1357 }
1358 /* Specifying two names is forbidden */
1359 if (opts->name)
1360 return -EINVAL;
1361 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001362 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001363 GFP_KERNEL);
1364 if (!opts->name)
1365 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001366
1367 continue;
1368 }
1369
Tejun Heo30159ec2013-06-25 11:53:37 -07001370 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001371 if (strcmp(token, ss->name))
1372 continue;
1373 if (ss->disabled)
1374 continue;
1375
1376 /* Mutually exclusive option 'all' + subsystem name */
1377 if (all_ss)
1378 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001379 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001380 one_ss = true;
1381
1382 break;
1383 }
1384 if (i == CGROUP_SUBSYS_COUNT)
1385 return -ENOENT;
1386 }
1387
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001388 /* Consistency checks */
1389
Tejun Heo873fe092013-04-14 20:15:26 -07001390 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001391 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 -07001392
Tejun Heod3ba07c2014-02-13 06:58:38 -05001393 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1394 opts->cpuset_clone_children || opts->release_agent ||
1395 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001396 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001397 return -EINVAL;
1398 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001399 } else {
1400 /*
1401 * If the 'all' option was specified select all the
1402 * subsystems, otherwise if 'none', 'name=' and a subsystem
1403 * name options were not specified, let's default to 'all'
1404 */
1405 if (all_ss || (!one_ss && !opts->none && !opts->name))
1406 for_each_subsys(ss, i)
1407 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001408 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001409
Tejun Heoa2dd4242014-03-19 10:23:55 -04001410 /*
1411 * We either have to specify by name or by subsystems. (So
1412 * all empty hierarchies must have a name).
1413 */
1414 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001415 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001416 }
1417
Li Zefanf9ab5b52009-06-17 16:26:33 -07001418 /*
1419 * Option noprefix was introduced just for backward compatibility
1420 * with the old cpuset, so we allow noprefix only if mounting just
1421 * the cpuset subsystem.
1422 */
Tejun Heo93438622013-04-14 20:15:25 -07001423 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001424 return -EINVAL;
1425
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001426
1427 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001428 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429 return -EINVAL;
1430
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431 return 0;
1432}
1433
Tejun Heo2bd59d42014-02-11 11:52:49 -05001434static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435{
1436 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001437 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001439 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440
Tejun Heo873fe092013-04-14 20:15:26 -07001441 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001442 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001443 return -EINVAL;
1444 }
1445
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446 mutex_lock(&cgroup_mutex);
1447
1448 /* See what subsystems are wanted */
1449 ret = parse_cgroupfs_options(data, &opts);
1450 if (ret)
1451 goto out_unlock;
1452
Tejun Heof392e512014-04-23 11:13:14 -04001453 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001454 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001455 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001456
Tejun Heof392e512014-04-23 11:13:14 -04001457 added_mask = opts.subsys_mask & ~root->subsys_mask;
1458 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001459
Ben Blumcf5d5942010-03-10 15:22:09 -08001460 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001461 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001462 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001463 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001464 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1465 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 goto out_unlock;
1468 }
1469
Tejun Heof172e672013-06-28 17:07:30 -07001470 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001471 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001472 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001473 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001474 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475
Tejun Heo5df36032014-03-19 10:23:54 -04001476 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001477 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001480 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001481
Tejun Heo69e943b2014-02-08 10:36:58 -05001482 if (opts.release_agent) {
1483 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001484 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001485 spin_unlock(&release_agent_path_lock);
1486 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001488 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491 return ret;
1492}
1493
Tejun Heoafeb0f92014-02-13 06:58:39 -05001494/*
1495 * To reduce the fork() overhead for systems that are not actually using
1496 * their cgroups capability, we don't maintain the lists running through
1497 * each css_set to its tasks until we see the list actually used - in other
1498 * words after the first mount.
1499 */
1500static bool use_task_css_set_links __read_mostly;
1501
1502static void cgroup_enable_task_cg_lists(void)
1503{
1504 struct task_struct *p, *g;
1505
Tejun Heo96d365e2014-02-13 06:58:40 -05001506 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001507
1508 if (use_task_css_set_links)
1509 goto out_unlock;
1510
1511 use_task_css_set_links = true;
1512
1513 /*
1514 * We need tasklist_lock because RCU is not safe against
1515 * while_each_thread(). Besides, a forking task that has passed
1516 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1517 * is not guaranteed to have its child immediately visible in the
1518 * tasklist if we walk through it with RCU.
1519 */
1520 read_lock(&tasklist_lock);
1521 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001522 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1523 task_css_set(p) != &init_css_set);
1524
1525 /*
1526 * We should check if the process is exiting, otherwise
1527 * it will race with cgroup_exit() in that the list
1528 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001529 * Do it while holding siglock so that we don't end up
1530 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001531 */
Tejun Heof153ad12014-02-25 09:56:49 -05001532 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001533 if (!(p->flags & PF_EXITING)) {
1534 struct css_set *cset = task_css_set(p);
1535
1536 list_add(&p->cg_list, &cset->tasks);
1537 get_css_set(cset);
1538 }
Tejun Heof153ad12014-02-25 09:56:49 -05001539 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001540 } while_each_thread(g, p);
1541 read_unlock(&tasklist_lock);
1542out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001543 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001544}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545
Paul Menagecc31edc2008-10-18 20:28:04 -07001546static void init_cgroup_housekeeping(struct cgroup *cgrp)
1547{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001548 struct cgroup_subsys *ss;
1549 int ssid;
1550
Tejun Heo2bd59d42014-02-11 11:52:49 -05001551 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001552 INIT_LIST_HEAD(&cgrp->sibling);
1553 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001554 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001555 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001556 INIT_LIST_HEAD(&cgrp->pidlists);
1557 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001558 cgrp->self.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001559
1560 for_each_subsys(ss, ssid)
1561 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001562
1563 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001564}
Paul Menagec6d57f32009-09-23 15:56:19 -07001565
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001566static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001567 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001569 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001570
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001572 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001573 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001574 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001575 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 root->flags = opts->flags;
1578 if (opts->release_agent)
1579 strcpy(root->release_agent_path, opts->release_agent);
1580 if (opts->name)
1581 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001582 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001583 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001584}
1585
Tejun Heo69dfa002014-05-04 15:09:13 -04001586static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001587{
Tejun Heod427dfe2014-02-11 11:52:48 -05001588 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001589 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001590 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001591 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001592
Tejun Heod427dfe2014-02-11 11:52:48 -05001593 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001594
Tejun Heo6fa49182014-05-04 15:09:13 -04001595 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001596 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001597 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001598 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001599
Tejun Heod427dfe2014-02-11 11:52:48 -05001600 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001601 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001602 * but that's OK - it can only be increased by someone holding
1603 * cgroup_lock, and that's us. The worst that can happen is that we
1604 * have some link structures left over
1605 */
1606 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001608 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609
Tejun Heo985ed672014-03-19 10:23:53 -04001610 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001611 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001612 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Tejun Heo2bd59d42014-02-11 11:52:49 -05001614 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1615 KERNFS_ROOT_CREATE_DEACTIVATED,
1616 root_cgrp);
1617 if (IS_ERR(root->kf_root)) {
1618 ret = PTR_ERR(root->kf_root);
1619 goto exit_root_id;
1620 }
1621 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622
Tejun Heod427dfe2014-02-11 11:52:48 -05001623 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1624 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001625 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Tejun Heo5df36032014-03-19 10:23:54 -04001627 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001628 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001629 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001630
Tejun Heod427dfe2014-02-11 11:52:48 -05001631 /*
1632 * There must be no failure case after here, since rebinding takes
1633 * care of subsystems' refcounts, which are explicitly dropped in
1634 * the failure exit path.
1635 */
1636 list_add(&root->root_list, &cgroup_roots);
1637 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638
Tejun Heod427dfe2014-02-11 11:52:48 -05001639 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001640 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001641 * objects.
1642 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001643 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001644 hash_for_each(css_set_table, i, cset, hlist)
1645 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001646 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647
Tejun Heod427dfe2014-02-11 11:52:48 -05001648 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001649 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001650
Tejun Heo2bd59d42014-02-11 11:52:49 -05001651 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001652 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001653 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001654
Tejun Heo2bd59d42014-02-11 11:52:49 -05001655destroy_root:
1656 kernfs_destroy_root(root->kf_root);
1657 root->kf_root = NULL;
1658exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001659 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001660out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001661 free_cgrp_cset_links(&tmp_links);
1662 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663}
1664
Al Virof7e83572010-07-26 13:23:11 +04001665static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001667 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001669 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001671 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001672 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001673 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001674
1675 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001676 * The first time anyone tries to mount a cgroup, enable the list
1677 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001678 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001679 if (!use_task_css_set_links)
1680 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001681
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001682 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001687 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001688
Tejun Heo2bd59d42014-02-11 11:52:49 -05001689 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001690 if (!opts.subsys_mask && !opts.none && !opts.name) {
1691 cgrp_dfl_root_visible = true;
1692 root = &cgrp_dfl_root;
1693 cgroup_get(&root->cgrp);
1694 ret = 0;
1695 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 }
1697
Tejun Heo985ed672014-03-19 10:23:53 -04001698 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001699 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001701 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001702 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001703
Paul Menage817929e2007-10-18 23:39:36 -07001704 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001705 * If we asked for a name then it must match. Also, if
1706 * name matches but sybsys_mask doesn't, we should fail.
1707 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001708 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001709 if (opts.name) {
1710 if (strcmp(opts.name, root->name))
1711 continue;
1712 name_match = true;
1713 }
Tejun Heo31261212013-06-28 17:07:30 -07001714
1715 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001716 * If we asked for subsystems (or explicitly for no
1717 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001718 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001719 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001720 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001721 if (!name_match)
1722 continue;
1723 ret = -EBUSY;
1724 goto out_unlock;
1725 }
Tejun Heo873fe092013-04-14 20:15:26 -07001726
Tejun Heoc7ba8282013-06-29 14:06:10 -07001727 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001728 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001729 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001730 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001731 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001732 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001733 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001734 }
Tejun Heo873fe092013-04-14 20:15:26 -07001735 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001736
Tejun Heo776f02f2014-02-12 09:29:50 -05001737 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001738 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001739 * ref indicate that the root is being destroyed. Wait for
1740 * destruction to complete so that the subsystems are free.
1741 * We can use wait_queue for the wait but this path is
1742 * super cold. Let's just sleep for a bit and retry.
1743 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001744 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001745 mutex_unlock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001746 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001747 ret = restart_syscall();
1748 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001749 }
1750
1751 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001752 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 }
1754
Tejun Heo172a2c062014-03-19 10:23:53 -04001755 /*
1756 * No such thing, create a new one. name= matching without subsys
1757 * specification is allowed for already existing hierarchies but we
1758 * can't create new one without subsys specification.
1759 */
1760 if (!opts.subsys_mask && !opts.none) {
1761 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001762 goto out_unlock;
1763 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764
Tejun Heo172a2c062014-03-19 10:23:53 -04001765 root = kzalloc(sizeof(*root), GFP_KERNEL);
1766 if (!root) {
1767 ret = -ENOMEM;
1768 goto out_unlock;
1769 }
1770
1771 init_cgroup_root(root, &opts);
1772
Tejun Heo35585572014-02-13 06:58:38 -05001773 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001774 if (ret)
1775 cgroup_free_root(root);
1776
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001777out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001778 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001779out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001780 kfree(opts.release_agent);
1781 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001782
Tejun Heo2bd59d42014-02-11 11:52:49 -05001783 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001784 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001785
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001786 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1787 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001788 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001789 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790}
1791
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001792static void cgroup_kill_sb(struct super_block *sb)
1793{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001794 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001795 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001797 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001798 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799}
1800
1801static struct file_system_type cgroup_fs_type = {
1802 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001803 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 .kill_sb = cgroup_kill_sb,
1805};
1806
Greg KH676db4a2010-08-05 13:53:35 -07001807static struct kobject *cgroup_kobj;
1808
Li Zefana043e3b2008-02-23 15:24:09 -08001809/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001810 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001811 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001812 * @buf: the buffer to write the path into
1813 * @buflen: the length of the buffer
1814 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001815 * Determine @task's cgroup on the first (the one with the lowest non-zero
1816 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1817 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1818 * cgroup controller callbacks.
1819 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001820 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001821 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001822char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001823{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001824 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001825 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001826 int hierarchy_id = 1;
1827 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001828
1829 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001830 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001831
Tejun Heo913ffdb2013-07-11 16:34:48 -07001832 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1833
Tejun Heo857a2be2013-04-14 20:50:08 -07001834 if (root) {
1835 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001836 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001837 } else {
1838 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001839 if (strlcpy(buf, "/", buflen) < buflen)
1840 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001841 }
1842
Tejun Heo96d365e2014-02-13 06:58:40 -05001843 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001844 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001845 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001846}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001847EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001848
Tejun Heob3dc0942014-02-25 10:04:01 -05001849/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001850struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001851 /* the src and dst cset list running through cset->mg_node */
1852 struct list_head src_csets;
1853 struct list_head dst_csets;
1854
1855 /*
1856 * Fields for cgroup_taskset_*() iteration.
1857 *
1858 * Before migration is committed, the target migration tasks are on
1859 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1860 * the csets on ->dst_csets. ->csets point to either ->src_csets
1861 * or ->dst_csets depending on whether migration is committed.
1862 *
1863 * ->cur_csets and ->cur_task point to the current task position
1864 * during iteration.
1865 */
1866 struct list_head *csets;
1867 struct css_set *cur_cset;
1868 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001869};
1870
1871/**
1872 * cgroup_taskset_first - reset taskset and return the first task
1873 * @tset: taskset of interest
1874 *
1875 * @tset iteration is initialized and the first task is returned.
1876 */
1877struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1878{
Tejun Heob3dc0942014-02-25 10:04:01 -05001879 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1880 tset->cur_task = NULL;
1881
1882 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001883}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001884
1885/**
1886 * cgroup_taskset_next - iterate to the next task in taskset
1887 * @tset: taskset of interest
1888 *
1889 * Return the next task in @tset. Iteration must have been initialized
1890 * with cgroup_taskset_first().
1891 */
1892struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1893{
Tejun Heob3dc0942014-02-25 10:04:01 -05001894 struct css_set *cset = tset->cur_cset;
1895 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001896
Tejun Heob3dc0942014-02-25 10:04:01 -05001897 while (&cset->mg_node != tset->csets) {
1898 if (!task)
1899 task = list_first_entry(&cset->mg_tasks,
1900 struct task_struct, cg_list);
1901 else
1902 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001903
Tejun Heob3dc0942014-02-25 10:04:01 -05001904 if (&task->cg_list != &cset->mg_tasks) {
1905 tset->cur_cset = cset;
1906 tset->cur_task = task;
1907 return task;
1908 }
1909
1910 cset = list_next_entry(cset, mg_node);
1911 task = NULL;
1912 }
1913
1914 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001915}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001916
1917/**
Ben Blum74a11662011-05-26 16:25:20 -07001918 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001919 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001920 * @tsk: the task being migrated
1921 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001922 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001923 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001924 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001925static void cgroup_task_migrate(struct cgroup *old_cgrp,
1926 struct task_struct *tsk,
1927 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001928{
Tejun Heo5abb8852013-06-12 21:04:49 -07001929 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001930
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001931 lockdep_assert_held(&cgroup_mutex);
1932 lockdep_assert_held(&css_set_rwsem);
1933
Ben Blum74a11662011-05-26 16:25:20 -07001934 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001935 * We are synchronized through threadgroup_lock() against PF_EXITING
1936 * setting such that we can't race against cgroup_exit() changing the
1937 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001938 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001939 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001940 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001941
Tejun Heob3dc0942014-02-25 10:04:01 -05001942 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001943 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001944
Tejun Heo1b9aba42014-03-19 17:43:21 -04001945 /*
1946 * Use move_tail so that cgroup_taskset_first() still returns the
1947 * leader after migration. This works because cgroup_migrate()
1948 * ensures that the dst_cset of the leader is the first on the
1949 * tset's dst_csets list.
1950 */
1951 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001952
1953 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001954 * We just gained a reference on old_cset by taking it from the
1955 * task. As trading it for new_cset is protected by cgroup_mutex,
1956 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001957 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001958 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001959 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001960}
1961
Li Zefana043e3b2008-02-23 15:24:09 -08001962/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001963 * cgroup_migrate_finish - cleanup after attach
1964 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001965 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001966 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1967 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001968 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001969static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001970{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001971 struct css_set *cset, *tmp_cset;
1972
1973 lockdep_assert_held(&cgroup_mutex);
1974
1975 down_write(&css_set_rwsem);
1976 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1977 cset->mg_src_cgrp = NULL;
1978 cset->mg_dst_cset = NULL;
1979 list_del_init(&cset->mg_preload_node);
1980 put_css_set_locked(cset, false);
1981 }
1982 up_write(&css_set_rwsem);
1983}
1984
1985/**
1986 * cgroup_migrate_add_src - add a migration source css_set
1987 * @src_cset: the source css_set to add
1988 * @dst_cgrp: the destination cgroup
1989 * @preloaded_csets: list of preloaded css_sets
1990 *
1991 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1992 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1993 * up by cgroup_migrate_finish().
1994 *
1995 * This function may be called without holding threadgroup_lock even if the
1996 * target is a process. Threads may be created and destroyed but as long
1997 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1998 * the preloaded css_sets are guaranteed to cover all migrations.
1999 */
2000static void cgroup_migrate_add_src(struct css_set *src_cset,
2001 struct cgroup *dst_cgrp,
2002 struct list_head *preloaded_csets)
2003{
2004 struct cgroup *src_cgrp;
2005
2006 lockdep_assert_held(&cgroup_mutex);
2007 lockdep_assert_held(&css_set_rwsem);
2008
2009 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2010
Tejun Heo1958d2d2014-02-25 10:04:03 -05002011 if (!list_empty(&src_cset->mg_preload_node))
2012 return;
2013
2014 WARN_ON(src_cset->mg_src_cgrp);
2015 WARN_ON(!list_empty(&src_cset->mg_tasks));
2016 WARN_ON(!list_empty(&src_cset->mg_node));
2017
2018 src_cset->mg_src_cgrp = src_cgrp;
2019 get_css_set(src_cset);
2020 list_add(&src_cset->mg_preload_node, preloaded_csets);
2021}
2022
2023/**
2024 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002025 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002026 * @preloaded_csets: list of preloaded source css_sets
2027 *
2028 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2029 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002030 * pins all destination css_sets, links each to its source, and append them
2031 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2032 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002033 *
2034 * This function must be called after cgroup_migrate_add_src() has been
2035 * called on each migration source css_set. After migration is performed
2036 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2037 * @preloaded_csets.
2038 */
2039static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2040 struct list_head *preloaded_csets)
2041{
2042 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002043 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002044
2045 lockdep_assert_held(&cgroup_mutex);
2046
Tejun Heof8f22e52014-04-23 11:13:16 -04002047 /*
2048 * Except for the root, child_subsys_mask must be zero for a cgroup
2049 * with tasks so that child cgroups don't compete against tasks.
2050 */
2051 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2052 dst_cgrp->child_subsys_mask)
2053 return -EBUSY;
2054
Tejun Heo1958d2d2014-02-25 10:04:03 -05002055 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002056 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002057 struct css_set *dst_cset;
2058
Tejun Heof817de92014-04-23 11:13:16 -04002059 dst_cset = find_css_set(src_cset,
2060 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002061 if (!dst_cset)
2062 goto err;
2063
2064 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002065
2066 /*
2067 * If src cset equals dst, it's noop. Drop the src.
2068 * cgroup_migrate() will skip the cset too. Note that we
2069 * can't handle src == dst as some nodes are used by both.
2070 */
2071 if (src_cset == dst_cset) {
2072 src_cset->mg_src_cgrp = NULL;
2073 list_del_init(&src_cset->mg_preload_node);
2074 put_css_set(src_cset, false);
2075 put_css_set(dst_cset, false);
2076 continue;
2077 }
2078
Tejun Heo1958d2d2014-02-25 10:04:03 -05002079 src_cset->mg_dst_cset = dst_cset;
2080
2081 if (list_empty(&dst_cset->mg_preload_node))
2082 list_add(&dst_cset->mg_preload_node, &csets);
2083 else
2084 put_css_set(dst_cset, false);
2085 }
2086
Tejun Heof817de92014-04-23 11:13:16 -04002087 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002088 return 0;
2089err:
2090 cgroup_migrate_finish(&csets);
2091 return -ENOMEM;
2092}
2093
2094/**
2095 * cgroup_migrate - migrate a process or task to a cgroup
2096 * @cgrp: the destination cgroup
2097 * @leader: the leader of the process or the task to migrate
2098 * @threadgroup: whether @leader points to the whole process or a single task
2099 *
2100 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2101 * process, the caller must be holding threadgroup_lock of @leader. The
2102 * caller is also responsible for invoking cgroup_migrate_add_src() and
2103 * cgroup_migrate_prepare_dst() on the targets before invoking this
2104 * function and following up with cgroup_migrate_finish().
2105 *
2106 * As long as a controller's ->can_attach() doesn't fail, this function is
2107 * guaranteed to succeed. This means that, excluding ->can_attach()
2108 * failure, when migrating multiple targets, the success or failure can be
2109 * decided for all targets by invoking group_migrate_prepare_dst() before
2110 * actually starting migrating.
2111 */
2112static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2113 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002114{
Tejun Heob3dc0942014-02-25 10:04:01 -05002115 struct cgroup_taskset tset = {
2116 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2117 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2118 .csets = &tset.src_csets,
2119 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002120 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002121 struct css_set *cset, *tmp_cset;
2122 struct task_struct *task, *tmp_task;
2123 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002124
2125 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002126 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2127 * already PF_EXITING could be freed from underneath us unless we
2128 * take an rcu_read_lock.
2129 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002130 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002131 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002132 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002133 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002134 /* @task either already exited or can't exit until the end */
2135 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002136 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002137
Tejun Heoeaf797a2014-02-25 10:04:03 -05002138 /* leave @task alone if post_fork() hasn't linked it yet */
2139 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002140 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002141
Tejun Heob3dc0942014-02-25 10:04:01 -05002142 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002143 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002144 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002145
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002146 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002147 * cgroup_taskset_first() must always return the leader.
2148 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002149 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002150 list_move_tail(&task->cg_list, &cset->mg_tasks);
2151 if (list_empty(&cset->mg_node))
2152 list_add_tail(&cset->mg_node, &tset.src_csets);
2153 if (list_empty(&cset->mg_dst_cset->mg_node))
2154 list_move_tail(&cset->mg_dst_cset->mg_node,
2155 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002156 next:
Li Zefan081aa452013-03-13 09:17:09 +08002157 if (!threadgroup)
2158 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002159 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002160 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002161 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002162
Tejun Heo134d3372011-12-12 18:12:21 -08002163 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002164 if (list_empty(&tset.src_csets))
2165 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002166
Tejun Heo1958d2d2014-02-25 10:04:03 -05002167 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002168 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002169 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002170 ret = css->ss->can_attach(css, &tset);
2171 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002172 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002173 goto out_cancel_attach;
2174 }
2175 }
Ben Blum74a11662011-05-26 16:25:20 -07002176 }
2177
2178 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002179 * Now that we're guaranteed success, proceed to move all tasks to
2180 * the new cgroup. There are no failure cases after here, so this
2181 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002182 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002183 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002184 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2185 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2186 cgroup_task_migrate(cset->mg_src_cgrp, task,
2187 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002188 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002189 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002190
2191 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002192 * Migration is committed, all target tasks are now on dst_csets.
2193 * Nothing is sensitive to fork() after this point. Notify
2194 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002195 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002196 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002197
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002198 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002199 if (css->ss->attach)
2200 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002201
Tejun Heo9db8de32014-02-13 06:58:43 -05002202 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002203 goto out_release_tset;
2204
Ben Blum74a11662011-05-26 16:25:20 -07002205out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002206 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002207 if (css == failed_css)
2208 break;
2209 if (css->ss->cancel_attach)
2210 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002211 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002212out_release_tset:
2213 down_write(&css_set_rwsem);
2214 list_splice_init(&tset.dst_csets, &tset.src_csets);
2215 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002216 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002217 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002218 }
2219 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002220 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002221}
2222
Tejun Heo1958d2d2014-02-25 10:04:03 -05002223/**
2224 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2225 * @dst_cgrp: the cgroup to attach to
2226 * @leader: the task or the leader of the threadgroup to be attached
2227 * @threadgroup: attach the whole threadgroup?
2228 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002229 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002230 */
2231static int cgroup_attach_task(struct cgroup *dst_cgrp,
2232 struct task_struct *leader, bool threadgroup)
2233{
2234 LIST_HEAD(preloaded_csets);
2235 struct task_struct *task;
2236 int ret;
2237
2238 /* look up all src csets */
2239 down_read(&css_set_rwsem);
2240 rcu_read_lock();
2241 task = leader;
2242 do {
2243 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2244 &preloaded_csets);
2245 if (!threadgroup)
2246 break;
2247 } while_each_thread(leader, task);
2248 rcu_read_unlock();
2249 up_read(&css_set_rwsem);
2250
2251 /* prepare dst csets and commit */
2252 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2253 if (!ret)
2254 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2255
2256 cgroup_migrate_finish(&preloaded_csets);
2257 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002258}
2259
2260/*
2261 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002262 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002263 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002264 */
Tejun Heoacbef752014-05-13 12:16:22 -04002265static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2266 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002267{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002268 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002269 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002270 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002271 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002272 int ret;
2273
Tejun Heoacbef752014-05-13 12:16:22 -04002274 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2275 return -EINVAL;
2276
Tejun Heoe76ecae2014-05-13 12:19:23 -04002277 cgrp = cgroup_kn_lock_live(of->kn);
2278 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002279 return -ENODEV;
2280
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002281retry_find_task:
2282 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002283 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002284 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002285 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002286 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002287 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002288 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002289 }
Ben Blum74a11662011-05-26 16:25:20 -07002290 /*
2291 * even if we're attaching all tasks in the thread group, we
2292 * only need to check permissions on one of them.
2293 */
David Howellsc69e8d92008-11-14 10:39:19 +11002294 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002295 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2296 !uid_eq(cred->euid, tcred->uid) &&
2297 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002298 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002299 ret = -EACCES;
2300 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002301 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002302 } else
2303 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002304
2305 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002306 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002307
2308 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002309 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002310 * trapped in a cpuset, or RT worker may be born in a cgroup
2311 * with no rt_runtime allocated. Just say no.
2312 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002313 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002314 ret = -EINVAL;
2315 rcu_read_unlock();
2316 goto out_unlock_cgroup;
2317 }
2318
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002319 get_task_struct(tsk);
2320 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002321
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002322 threadgroup_lock(tsk);
2323 if (threadgroup) {
2324 if (!thread_group_leader(tsk)) {
2325 /*
2326 * a race with de_thread from another thread's exec()
2327 * may strip us of our leadership, if this happens,
2328 * there is no choice but to throw this task away and
2329 * try again; this is
2330 * "double-double-toil-and-trouble-check locking".
2331 */
2332 threadgroup_unlock(tsk);
2333 put_task_struct(tsk);
2334 goto retry_find_task;
2335 }
Li Zefan081aa452013-03-13 09:17:09 +08002336 }
2337
2338 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2339
Tejun Heocd3d0952011-12-12 18:12:21 -08002340 threadgroup_unlock(tsk);
2341
Paul Menagebbcb81d2007-10-18 23:39:32 -07002342 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002343out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002344 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002345 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002346}
2347
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002348/**
2349 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2350 * @from: attach to all cgroups of a given task
2351 * @tsk: the task to be attached
2352 */
2353int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2354{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002355 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002356 int retval = 0;
2357
Tejun Heo47cfcd02013-04-07 09:29:51 -07002358 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002359 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002360 struct cgroup *from_cgrp;
2361
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002362 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002363 continue;
2364
Tejun Heo96d365e2014-02-13 06:58:40 -05002365 down_read(&css_set_rwsem);
2366 from_cgrp = task_cgroup_from_root(from, root);
2367 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002368
Li Zefan6f4b7e62013-07-31 16:18:36 +08002369 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002370 if (retval)
2371 break;
2372 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002373 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002374
2375 return retval;
2376}
2377EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2378
Tejun Heoacbef752014-05-13 12:16:22 -04002379static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2380 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002381{
Tejun Heoacbef752014-05-13 12:16:22 -04002382 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002383}
2384
Tejun Heoacbef752014-05-13 12:16:22 -04002385static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2386 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002387{
Tejun Heoacbef752014-05-13 12:16:22 -04002388 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002389}
2390
Tejun Heo451af502014-05-13 12:16:21 -04002391static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2392 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002393{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002394 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002395
Tejun Heoe76ecae2014-05-13 12:19:23 -04002396 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2397
2398 cgrp = cgroup_kn_lock_live(of->kn);
2399 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002400 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002401 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002402 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2403 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002404 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002405 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002406 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002407}
2408
Tejun Heo2da8ca82013-12-05 12:28:04 -05002409static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002410{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002411 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002412
Tejun Heo46cfeb02014-05-13 12:11:00 -04002413 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002414 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002415 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002416 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002417 return 0;
2418}
2419
Tejun Heo2da8ca82013-12-05 12:28:04 -05002420static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002421{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002422 struct cgroup *cgrp = seq_css(seq)->cgroup;
2423
2424 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002425 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426}
2427
Tejun Heof8f22e52014-04-23 11:13:16 -04002428static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2429{
2430 struct cgroup_subsys *ss;
2431 bool printed = false;
2432 int ssid;
2433
2434 for_each_subsys(ss, ssid) {
2435 if (ss_mask & (1 << ssid)) {
2436 if (printed)
2437 seq_putc(seq, ' ');
2438 seq_printf(seq, "%s", ss->name);
2439 printed = true;
2440 }
2441 }
2442 if (printed)
2443 seq_putc(seq, '\n');
2444}
2445
2446/* show controllers which are currently attached to the default hierarchy */
2447static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2448{
2449 struct cgroup *cgrp = seq_css(seq)->cgroup;
2450
2451 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2452 return 0;
2453}
2454
2455/* show controllers which are enabled from the parent */
2456static int cgroup_controllers_show(struct seq_file *seq, void *v)
2457{
2458 struct cgroup *cgrp = seq_css(seq)->cgroup;
2459
2460 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2461 return 0;
2462}
2463
2464/* show controllers which are enabled for a given cgroup's children */
2465static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2466{
2467 struct cgroup *cgrp = seq_css(seq)->cgroup;
2468
2469 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2470 return 0;
2471}
2472
2473/**
2474 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2475 * @cgrp: root of the subtree to update csses for
2476 *
2477 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2478 * css associations need to be updated accordingly. This function looks up
2479 * all css_sets which are attached to the subtree, creates the matching
2480 * updated css_sets and migrates the tasks to the new ones.
2481 */
2482static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2483{
2484 LIST_HEAD(preloaded_csets);
2485 struct cgroup_subsys_state *css;
2486 struct css_set *src_cset;
2487 int ret;
2488
Tejun Heof8f22e52014-04-23 11:13:16 -04002489 lockdep_assert_held(&cgroup_mutex);
2490
2491 /* look up all csses currently attached to @cgrp's subtree */
2492 down_read(&css_set_rwsem);
2493 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2494 struct cgrp_cset_link *link;
2495
2496 /* self is not affected by child_subsys_mask change */
2497 if (css->cgroup == cgrp)
2498 continue;
2499
2500 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2501 cgroup_migrate_add_src(link->cset, cgrp,
2502 &preloaded_csets);
2503 }
2504 up_read(&css_set_rwsem);
2505
2506 /* NULL dst indicates self on default hierarchy */
2507 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2508 if (ret)
2509 goto out_finish;
2510
2511 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2512 struct task_struct *last_task = NULL, *task;
2513
2514 /* src_csets precede dst_csets, break on the first dst_cset */
2515 if (!src_cset->mg_src_cgrp)
2516 break;
2517
2518 /*
2519 * All tasks in src_cset need to be migrated to the
2520 * matching dst_cset. Empty it process by process. We
2521 * walk tasks but migrate processes. The leader might even
2522 * belong to a different cset but such src_cset would also
2523 * be among the target src_csets because the default
2524 * hierarchy enforces per-process membership.
2525 */
2526 while (true) {
2527 down_read(&css_set_rwsem);
2528 task = list_first_entry_or_null(&src_cset->tasks,
2529 struct task_struct, cg_list);
2530 if (task) {
2531 task = task->group_leader;
2532 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2533 get_task_struct(task);
2534 }
2535 up_read(&css_set_rwsem);
2536
2537 if (!task)
2538 break;
2539
2540 /* guard against possible infinite loop */
2541 if (WARN(last_task == task,
2542 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2543 goto out_finish;
2544 last_task = task;
2545
2546 threadgroup_lock(task);
2547 /* raced against de_thread() from another thread? */
2548 if (!thread_group_leader(task)) {
2549 threadgroup_unlock(task);
2550 put_task_struct(task);
2551 continue;
2552 }
2553
2554 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2555
2556 threadgroup_unlock(task);
2557 put_task_struct(task);
2558
2559 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2560 goto out_finish;
2561 }
2562 }
2563
2564out_finish:
2565 cgroup_migrate_finish(&preloaded_csets);
2566 return ret;
2567}
2568
2569/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002570static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2571 char *buf, size_t nbytes,
2572 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002573{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002574 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002575 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002576 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002577 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002578 int ssid, ret;
2579
2580 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002581 * Parse input - space separated list of subsystem names prefixed
2582 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002583 */
Tejun Heo451af502014-05-13 12:16:21 -04002584 buf = strstrip(buf);
2585 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002586 if (tok[0] == '\0')
2587 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002588 for_each_subsys(ss, ssid) {
2589 if (ss->disabled || strcmp(tok + 1, ss->name))
2590 continue;
2591
2592 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002593 enable |= 1 << ssid;
2594 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002595 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002596 disable |= 1 << ssid;
2597 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002598 } else {
2599 return -EINVAL;
2600 }
2601 break;
2602 }
2603 if (ssid == CGROUP_SUBSYS_COUNT)
2604 return -EINVAL;
2605 }
2606
Tejun Heoa9746d82014-05-13 12:19:22 -04002607 cgrp = cgroup_kn_lock_live(of->kn);
2608 if (!cgrp)
2609 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002610
2611 for_each_subsys(ss, ssid) {
2612 if (enable & (1 << ssid)) {
2613 if (cgrp->child_subsys_mask & (1 << ssid)) {
2614 enable &= ~(1 << ssid);
2615 continue;
2616 }
2617
2618 /*
2619 * Because css offlining is asynchronous, userland
2620 * might try to re-enable the same controller while
2621 * the previous instance is still around. In such
2622 * cases, wait till it's gone using offline_waitq.
2623 */
2624 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002625 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002626
2627 if (!cgroup_css(child, ss))
2628 continue;
2629
Tejun Heo0cee8b72014-05-13 12:10:59 -04002630 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002631 prepare_to_wait(&child->offline_waitq, &wait,
2632 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002633 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002634 schedule();
2635 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002636 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002637
Tejun Heoa9746d82014-05-13 12:19:22 -04002638 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002639 }
2640
2641 /* unavailable or not enabled on the parent? */
2642 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2643 (cgrp->parent &&
2644 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2645 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002646 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002647 }
2648 } else if (disable & (1 << ssid)) {
2649 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2650 disable &= ~(1 << ssid);
2651 continue;
2652 }
2653
2654 /* a child has it enabled? */
2655 cgroup_for_each_live_child(child, cgrp) {
2656 if (child->child_subsys_mask & (1 << ssid)) {
2657 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002658 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002659 }
2660 }
2661 }
2662 }
2663
2664 if (!enable && !disable) {
2665 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002666 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002667 }
2668
2669 /*
2670 * Except for the root, child_subsys_mask must be zero for a cgroup
2671 * with tasks so that child cgroups don't compete against tasks.
2672 */
2673 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2674 ret = -EBUSY;
2675 goto out_unlock;
2676 }
2677
2678 /*
2679 * Create csses for enables and update child_subsys_mask. This
2680 * changes cgroup_e_css() results which in turn makes the
2681 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2682 * subtree to the updated csses.
2683 */
2684 for_each_subsys(ss, ssid) {
2685 if (!(enable & (1 << ssid)))
2686 continue;
2687
2688 cgroup_for_each_live_child(child, cgrp) {
2689 ret = create_css(child, ss);
2690 if (ret)
2691 goto err_undo_css;
2692 }
2693 }
2694
2695 cgrp->child_subsys_mask |= enable;
2696 cgrp->child_subsys_mask &= ~disable;
2697
2698 ret = cgroup_update_dfl_csses(cgrp);
2699 if (ret)
2700 goto err_undo_css;
2701
2702 /* all tasks are now migrated away from the old csses, kill them */
2703 for_each_subsys(ss, ssid) {
2704 if (!(disable & (1 << ssid)))
2705 continue;
2706
2707 cgroup_for_each_live_child(child, cgrp)
2708 kill_css(cgroup_css(child, ss));
2709 }
2710
2711 kernfs_activate(cgrp->kn);
2712 ret = 0;
2713out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002714 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002715 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002716
2717err_undo_css:
2718 cgrp->child_subsys_mask &= ~enable;
2719 cgrp->child_subsys_mask |= disable;
2720
2721 for_each_subsys(ss, ssid) {
2722 if (!(enable & (1 << ssid)))
2723 continue;
2724
2725 cgroup_for_each_live_child(child, cgrp) {
2726 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2727 if (css)
2728 kill_css(css);
2729 }
2730 }
2731 goto out_unlock;
2732}
2733
Tejun Heo842b5972014-04-25 18:28:02 -04002734static int cgroup_populated_show(struct seq_file *seq, void *v)
2735{
2736 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2737 return 0;
2738}
2739
Tejun Heo2bd59d42014-02-11 11:52:49 -05002740static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2741 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002743 struct cgroup *cgrp = of->kn->parent->priv;
2744 struct cftype *cft = of->kn->priv;
2745 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002746 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747
Tejun Heob4168642014-05-13 12:16:21 -04002748 if (cft->write)
2749 return cft->write(of, buf, nbytes, off);
2750
Tejun Heo2bd59d42014-02-11 11:52:49 -05002751 /*
2752 * kernfs guarantees that a file isn't deleted with operations in
2753 * flight, which means that the matching css is and stays alive and
2754 * doesn't need to be pinned. The RCU locking is not necessary
2755 * either. It's just for the convenience of using cgroup_css().
2756 */
2757 rcu_read_lock();
2758 css = cgroup_css(cgrp, cft->ss);
2759 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760
Tejun Heo451af502014-05-13 12:16:21 -04002761 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002762 unsigned long long v;
2763 ret = kstrtoull(buf, 0, &v);
2764 if (!ret)
2765 ret = cft->write_u64(css, cft, v);
2766 } else if (cft->write_s64) {
2767 long long v;
2768 ret = kstrtoll(buf, 0, &v);
2769 if (!ret)
2770 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002771 } else {
2772 ret = -EINVAL;
2773 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002774
Tejun Heoa742c592013-12-05 12:28:03 -05002775 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002776}
2777
Tejun Heo6612f052013-12-05 12:28:04 -05002778static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002779{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002780 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002781}
2782
2783static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2784{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002785 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002786}
2787
2788static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2789{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002790 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002791}
2792
2793static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2794{
Tejun Heo7da11272013-12-05 12:28:04 -05002795 struct cftype *cft = seq_cft(m);
2796 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002797
Tejun Heo2da8ca82013-12-05 12:28:04 -05002798 if (cft->seq_show)
2799 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002800
Tejun Heo896f5192013-12-05 12:28:04 -05002801 if (cft->read_u64)
2802 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2803 else if (cft->read_s64)
2804 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2805 else
2806 return -EINVAL;
2807 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002808}
2809
Tejun Heo2bd59d42014-02-11 11:52:49 -05002810static struct kernfs_ops cgroup_kf_single_ops = {
2811 .atomic_write_len = PAGE_SIZE,
2812 .write = cgroup_file_write,
2813 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002814};
2815
Tejun Heo2bd59d42014-02-11 11:52:49 -05002816static struct kernfs_ops cgroup_kf_ops = {
2817 .atomic_write_len = PAGE_SIZE,
2818 .write = cgroup_file_write,
2819 .seq_start = cgroup_seqfile_start,
2820 .seq_next = cgroup_seqfile_next,
2821 .seq_stop = cgroup_seqfile_stop,
2822 .seq_show = cgroup_seqfile_show,
2823};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002824
2825/*
2826 * cgroup_rename - Only allow simple rename of directories in place.
2827 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002828static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2829 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002830{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002831 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002832 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002833
Tejun Heo2bd59d42014-02-11 11:52:49 -05002834 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002835 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002836 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002837 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002838
Tejun Heo6db8e852013-06-14 11:18:22 -07002839 /*
2840 * This isn't a proper migration and its usefulness is very
2841 * limited. Disallow if sane_behavior.
2842 */
2843 if (cgroup_sane_behavior(cgrp))
2844 return -EPERM;
2845
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002846 /*
Tejun Heo8353da12014-05-13 12:19:23 -04002847 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002848 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04002849 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002850 */
2851 kernfs_break_active_protection(new_parent);
2852 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002853
Tejun Heo2bd59d42014-02-11 11:52:49 -05002854 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002855
Tejun Heo2bd59d42014-02-11 11:52:49 -05002856 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002857
Tejun Heo2bd59d42014-02-11 11:52:49 -05002858 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002859
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002860 kernfs_unbreak_active_protection(kn);
2861 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002862 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002863}
2864
Tejun Heo49957f82014-04-07 16:44:47 -04002865/* set uid and gid of cgroup dirs and files to that of the creator */
2866static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2867{
2868 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2869 .ia_uid = current_fsuid(),
2870 .ia_gid = current_fsgid(), };
2871
2872 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2873 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2874 return 0;
2875
2876 return kernfs_setattr(kn, &iattr);
2877}
2878
Tejun Heo2bb566c2013-08-08 20:11:23 -04002879static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002880{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002881 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002882 struct kernfs_node *kn;
2883 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002884 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885
Tejun Heo2bd59d42014-02-11 11:52:49 -05002886#ifdef CONFIG_DEBUG_LOCK_ALLOC
2887 key = &cft->lockdep_key;
2888#endif
2889 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2890 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2891 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002892 if (IS_ERR(kn))
2893 return PTR_ERR(kn);
2894
2895 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002896 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002897 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002898 return ret;
2899 }
2900
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002901 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002902 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002903 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002904}
2905
Tejun Heob1f28d32013-06-28 16:24:10 -07002906/**
2907 * cgroup_addrm_files - add or remove files to a cgroup directory
2908 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002909 * @cfts: array of cftypes to be added
2910 * @is_add: whether to add or remove
2911 *
2912 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002913 * For removals, this function never fails. If addition fails, this
2914 * function doesn't remove files already added. The caller is responsible
2915 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002916 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002917static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2918 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002919{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002920 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002921 int ret;
2922
Tejun Heo01f64742014-05-13 12:19:23 -04002923 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002924
2925 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002926 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002927 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2928 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002929 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2930 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002931 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2932 continue;
2933 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2934 continue;
2935
Li Zefan2739d3c2013-01-21 18:18:33 +08002936 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002937 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002938 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002939 pr_warn("%s: failed to add %s, err=%d\n",
2940 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002941 return ret;
2942 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002943 } else {
2944 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002945 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002946 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002947 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002948}
2949
Tejun Heo21a2d342014-02-12 09:29:49 -05002950static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002951{
2952 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002953 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002954 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002955 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002956 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002957
Tejun Heo01f64742014-05-13 12:19:23 -04002958 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002959
Li Zefane8c82d22013-06-18 18:48:37 +08002960 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002961 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002962 struct cgroup *cgrp = css->cgroup;
2963
Li Zefane8c82d22013-06-18 18:48:37 +08002964 if (cgroup_is_dead(cgrp))
2965 continue;
2966
Tejun Heo21a2d342014-02-12 09:29:49 -05002967 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002968 if (ret)
2969 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002970 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002971
2972 if (is_add && !ret)
2973 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002974 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002975}
2976
Tejun Heo2da440a2014-02-11 11:52:48 -05002977static void cgroup_exit_cftypes(struct cftype *cfts)
2978{
2979 struct cftype *cft;
2980
Tejun Heo2bd59d42014-02-11 11:52:49 -05002981 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2982 /* free copy for custom atomic_write_len, see init_cftypes() */
2983 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2984 kfree(cft->kf_ops);
2985 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002986 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002987 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002988}
2989
Tejun Heo2bd59d42014-02-11 11:52:49 -05002990static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002991{
2992 struct cftype *cft;
2993
Tejun Heo2bd59d42014-02-11 11:52:49 -05002994 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2995 struct kernfs_ops *kf_ops;
2996
Tejun Heo0adb0702014-02-12 09:29:48 -05002997 WARN_ON(cft->ss || cft->kf_ops);
2998
Tejun Heo2bd59d42014-02-11 11:52:49 -05002999 if (cft->seq_start)
3000 kf_ops = &cgroup_kf_ops;
3001 else
3002 kf_ops = &cgroup_kf_single_ops;
3003
3004 /*
3005 * Ugh... if @cft wants a custom max_write_len, we need to
3006 * make a copy of kf_ops to set its atomic_write_len.
3007 */
3008 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3009 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3010 if (!kf_ops) {
3011 cgroup_exit_cftypes(cfts);
3012 return -ENOMEM;
3013 }
3014 kf_ops->atomic_write_len = cft->max_write_len;
3015 }
3016
3017 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003018 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003019 }
3020
3021 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003022}
3023
Tejun Heo21a2d342014-02-12 09:29:49 -05003024static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3025{
Tejun Heo01f64742014-05-13 12:19:23 -04003026 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003027
3028 if (!cfts || !cfts[0].ss)
3029 return -ENOENT;
3030
3031 list_del(&cfts->node);
3032 cgroup_apply_cftypes(cfts, false);
3033 cgroup_exit_cftypes(cfts);
3034 return 0;
3035}
3036
Tejun Heo8e3f6542012-04-01 12:09:55 -07003037/**
Tejun Heo80b13582014-02-12 09:29:48 -05003038 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3039 * @cfts: zero-length name terminated array of cftypes
3040 *
3041 * Unregister @cfts. Files described by @cfts are removed from all
3042 * existing cgroups and all future cgroups won't have them either. This
3043 * function can be called anytime whether @cfts' subsys is attached or not.
3044 *
3045 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3046 * registered.
3047 */
3048int cgroup_rm_cftypes(struct cftype *cfts)
3049{
Tejun Heo21a2d342014-02-12 09:29:49 -05003050 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003051
Tejun Heo01f64742014-05-13 12:19:23 -04003052 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003053 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003054 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003055 return ret;
3056}
3057
3058/**
3059 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3060 * @ss: target cgroup subsystem
3061 * @cfts: zero-length name terminated array of cftypes
3062 *
3063 * Register @cfts to @ss. Files described by @cfts are created for all
3064 * existing cgroups to which @ss is attached and all future cgroups will
3065 * have them too. This function can be called anytime whether @ss is
3066 * attached or not.
3067 *
3068 * Returns 0 on successful registration, -errno on failure. Note that this
3069 * function currently returns 0 as long as @cfts registration is successful
3070 * even if some file creation attempts on existing cgroups fail.
3071 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003072int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003073{
Tejun Heo9ccece82013-06-28 16:24:11 -07003074 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003075
Li Zefandc5736e2014-02-17 10:41:50 +08003076 if (!cfts || cfts[0].name[0] == '\0')
3077 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003078
Tejun Heo2bd59d42014-02-11 11:52:49 -05003079 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003080 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003081 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003082
Tejun Heo01f64742014-05-13 12:19:23 -04003083 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003084
Tejun Heo0adb0702014-02-12 09:29:48 -05003085 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003086 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003087 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003088 cgroup_rm_cftypes_locked(cfts);
3089
Tejun Heo01f64742014-05-13 12:19:23 -04003090 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003091 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003092}
Tejun Heo79578622012-04-01 12:09:56 -07003093
3094/**
Li Zefana043e3b2008-02-23 15:24:09 -08003095 * cgroup_task_count - count the number of tasks in a cgroup.
3096 * @cgrp: the cgroup in question
3097 *
3098 * Return the number of tasks in the cgroup.
3099 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003100static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003101{
3102 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003103 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003104
Tejun Heo96d365e2014-02-13 06:58:40 -05003105 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003106 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3107 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003108 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003109 return count;
3110}
3111
Tejun Heo574bd9f2012-11-09 09:12:29 -08003112/**
Tejun Heo492eb212013-08-08 20:11:25 -04003113 * css_next_child - find the next child of a given css
3114 * @pos_css: the current position (%NULL to initiate traversal)
3115 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003116 *
Tejun Heo492eb212013-08-08 20:11:25 -04003117 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003118 * under either cgroup_mutex or RCU read lock. The only requirement is
3119 * that @parent_css and @pos_css are accessible. The next sibling is
3120 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003121 */
Tejun Heo492eb212013-08-08 20:11:25 -04003122struct cgroup_subsys_state *
3123css_next_child(struct cgroup_subsys_state *pos_css,
3124 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003125{
Tejun Heo492eb212013-08-08 20:11:25 -04003126 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3127 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003128 struct cgroup *next;
3129
Tejun Heo8353da12014-05-13 12:19:23 -04003130 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003131
3132 /*
3133 * @pos could already have been removed. Once a cgroup is removed,
3134 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003135 * changes. As CGRP_DEAD assertion is serialized and happens
3136 * before the cgroup is taken off the ->sibling list, if we see it
3137 * unasserted, it's guaranteed that the next sibling hasn't
3138 * finished its grace period even if it's already removed, and thus
3139 * safe to dereference from this RCU critical section. If
3140 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3141 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003142 *
3143 * If @pos is dead, its next pointer can't be dereferenced;
3144 * however, as each cgroup is given a monotonically increasing
3145 * unique serial number and always appended to the sibling list,
3146 * the next one can be found by walking the parent's children until
3147 * we see a cgroup with higher serial number than @pos's. While
3148 * this path can be slower, it's taken only when either the current
3149 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003150 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003151 if (!pos) {
3152 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3153 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003154 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003155 } else {
3156 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3157 if (next->serial_nr > pos->serial_nr)
3158 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003159 }
3160
Tejun Heo3b281af2014-04-23 11:13:15 -04003161 /*
3162 * @next, if not pointing to the head, can be dereferenced and is
3163 * the next sibling; however, it might have @ss disabled. If so,
3164 * fast-forward to the next enabled one.
3165 */
3166 while (&next->sibling != &cgrp->children) {
3167 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003168
Tejun Heo3b281af2014-04-23 11:13:15 -04003169 if (next_css)
3170 return next_css;
3171 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3172 }
3173 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003174}
Tejun Heo53fa5262013-05-24 10:55:38 +09003175
3176/**
Tejun Heo492eb212013-08-08 20:11:25 -04003177 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003179 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003180 *
Tejun Heo492eb212013-08-08 20:11:25 -04003181 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003182 * to visit for pre-order traversal of @root's descendants. @root is
3183 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003184 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003185 * While this function requires cgroup_mutex or RCU read locking, it
3186 * doesn't require the whole traversal to be contained in a single critical
3187 * section. This function will return the correct next descendant as long
3188 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189 */
Tejun Heo492eb212013-08-08 20:11:25 -04003190struct cgroup_subsys_state *
3191css_next_descendant_pre(struct cgroup_subsys_state *pos,
3192 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193{
Tejun Heo492eb212013-08-08 20:11:25 -04003194 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003195
Tejun Heo8353da12014-05-13 12:19:23 -04003196 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003197
Tejun Heobd8815a2013-08-08 20:11:27 -04003198 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003199 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003200 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003201
3202 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003203 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003204 if (next)
3205 return next;
3206
3207 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003208 while (pos != root) {
3209 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003210 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003211 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003212 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003213 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003214
3215 return NULL;
3216}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003217
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003218/**
Tejun Heo492eb212013-08-08 20:11:25 -04003219 * css_rightmost_descendant - return the rightmost descendant of a css
3220 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003221 *
Tejun Heo492eb212013-08-08 20:11:25 -04003222 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3223 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003224 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003225 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003226 * While this function requires cgroup_mutex or RCU read locking, it
3227 * doesn't require the whole traversal to be contained in a single critical
3228 * section. This function will return the correct rightmost descendant as
3229 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003230 */
Tejun Heo492eb212013-08-08 20:11:25 -04003231struct cgroup_subsys_state *
3232css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003233{
Tejun Heo492eb212013-08-08 20:11:25 -04003234 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003235
Tejun Heo8353da12014-05-13 12:19:23 -04003236 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003237
3238 do {
3239 last = pos;
3240 /* ->prev isn't RCU safe, walk ->next till the end */
3241 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003242 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003243 pos = tmp;
3244 } while (pos);
3245
3246 return last;
3247}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003248
Tejun Heo492eb212013-08-08 20:11:25 -04003249static struct cgroup_subsys_state *
3250css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003251{
Tejun Heo492eb212013-08-08 20:11:25 -04003252 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003253
3254 do {
3255 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003256 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003257 } while (pos);
3258
3259 return last;
3260}
3261
3262/**
Tejun Heo492eb212013-08-08 20:11:25 -04003263 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003264 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003265 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003266 *
Tejun Heo492eb212013-08-08 20:11:25 -04003267 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003268 * to visit for post-order traversal of @root's descendants. @root is
3269 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003270 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003271 * While this function requires cgroup_mutex or RCU read locking, it
3272 * doesn't require the whole traversal to be contained in a single critical
3273 * section. This function will return the correct next descendant as long
3274 * as both @pos and @cgroup are accessible and @pos is a descendant of
3275 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003276 */
Tejun Heo492eb212013-08-08 20:11:25 -04003277struct cgroup_subsys_state *
3278css_next_descendant_post(struct cgroup_subsys_state *pos,
3279 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003280{
Tejun Heo492eb212013-08-08 20:11:25 -04003281 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003282
Tejun Heo8353da12014-05-13 12:19:23 -04003283 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003284
Tejun Heo58b79a92013-09-06 15:31:08 -04003285 /* if first iteration, visit leftmost descendant which may be @root */
3286 if (!pos)
3287 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003288
Tejun Heobd8815a2013-08-08 20:11:27 -04003289 /* if we visited @root, we're done */
3290 if (pos == root)
3291 return NULL;
3292
Tejun Heo574bd9f2012-11-09 09:12:29 -08003293 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003294 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003295 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003296 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003297
3298 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003299 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003300}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003301
Tejun Heocbc125e2014-05-14 09:15:01 -04003302static bool cgroup_has_live_children(struct cgroup *cgrp)
3303{
3304 struct cgroup *child;
3305
3306 rcu_read_lock();
3307 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3308 if (!cgroup_is_dead(child)) {
3309 rcu_read_unlock();
3310 return true;
3311 }
3312 }
3313 rcu_read_unlock();
3314 return false;
3315}
3316
Tejun Heo0942eee2013-08-08 20:11:26 -04003317/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003318 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003319 * @it: the iterator to advance
3320 *
3321 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003322 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003323static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003324{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003325 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003326 struct cgrp_cset_link *link;
3327 struct css_set *cset;
3328
3329 /* Advance to the next non-empty css_set */
3330 do {
3331 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003332 if (l == it->cset_head) {
3333 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003334 return;
3335 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003336
3337 if (it->ss) {
3338 cset = container_of(l, struct css_set,
3339 e_cset_node[it->ss->id]);
3340 } else {
3341 link = list_entry(l, struct cgrp_cset_link, cset_link);
3342 cset = link->cset;
3343 }
Tejun Heoc7561122014-02-25 10:04:01 -05003344 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3345
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003346 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003347
3348 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003349 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003350 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003351 it->task_pos = cset->mg_tasks.next;
3352
3353 it->tasks_head = &cset->tasks;
3354 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003355}
3356
Tejun Heo0942eee2013-08-08 20:11:26 -04003357/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003358 * css_task_iter_start - initiate task iteration
3359 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003360 * @it: the task iterator to use
3361 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003362 * Initiate iteration through the tasks of @css. The caller can call
3363 * css_task_iter_next() to walk through the tasks until the function
3364 * returns NULL. On completion of iteration, css_task_iter_end() must be
3365 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003366 *
3367 * Note that this function acquires a lock which is released when the
3368 * iteration finishes. The caller can't sleep while iteration is in
3369 * progress.
3370 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003371void css_task_iter_start(struct cgroup_subsys_state *css,
3372 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003373 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003374{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003375 /* no one should try to iterate before mounting cgroups */
3376 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003377
Tejun Heo96d365e2014-02-13 06:58:40 -05003378 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003379
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003380 it->ss = css->ss;
3381
3382 if (it->ss)
3383 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3384 else
3385 it->cset_pos = &css->cgroup->cset_links;
3386
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003387 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003388
Tejun Heo72ec7022013-08-08 20:11:26 -04003389 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003390}
3391
Tejun Heo0942eee2013-08-08 20:11:26 -04003392/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003393 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003394 * @it: the task iterator being iterated
3395 *
3396 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003397 * initialized via css_task_iter_start(). Returns NULL when the iteration
3398 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003399 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003400struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003401{
3402 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003403 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003404
3405 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003406 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003407 return NULL;
3408 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003409
3410 /*
3411 * Advance iterator to find next entry. cset->tasks is consumed
3412 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3413 * next cset.
3414 */
Paul Menage817929e2007-10-18 23:39:36 -07003415 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003416
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003417 if (l == it->tasks_head)
3418 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003419
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003420 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003421 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003422 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003423 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003424
Paul Menage817929e2007-10-18 23:39:36 -07003425 return res;
3426}
3427
Tejun Heo0942eee2013-08-08 20:11:26 -04003428/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003429 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003430 * @it: the task iterator to finish
3431 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003432 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003433 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003434void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003435 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003436{
Tejun Heo96d365e2014-02-13 06:58:40 -05003437 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003438}
3439
3440/**
3441 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3442 * @to: cgroup to which the tasks will be moved
3443 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003444 *
3445 * Locking rules between cgroup_post_fork() and the migration path
3446 * guarantee that, if a task is forking while being migrated, the new child
3447 * is guaranteed to be either visible in the source cgroup after the
3448 * parent's migration is complete or put into the target cgroup. No task
3449 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003450 */
3451int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3452{
Tejun Heo952aaa12014-02-25 10:04:03 -05003453 LIST_HEAD(preloaded_csets);
3454 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003455 struct css_task_iter it;
3456 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003457 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003458
Tejun Heo952aaa12014-02-25 10:04:03 -05003459 mutex_lock(&cgroup_mutex);
3460
3461 /* all tasks in @from are being moved, all csets are source */
3462 down_read(&css_set_rwsem);
3463 list_for_each_entry(link, &from->cset_links, cset_link)
3464 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3465 up_read(&css_set_rwsem);
3466
3467 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3468 if (ret)
3469 goto out_err;
3470
3471 /*
3472 * Migrate tasks one-by-one until @form is empty. This fails iff
3473 * ->can_attach() fails.
3474 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003475 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003476 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003477 task = css_task_iter_next(&it);
3478 if (task)
3479 get_task_struct(task);
3480 css_task_iter_end(&it);
3481
3482 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003483 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003484 put_task_struct(task);
3485 }
3486 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003487out_err:
3488 cgroup_migrate_finish(&preloaded_csets);
3489 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003490 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003491}
3492
Paul Menage817929e2007-10-18 23:39:36 -07003493/*
Ben Blum102a7752009-09-23 15:56:26 -07003494 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003495 *
3496 * Reading this file can return large amounts of data if a cgroup has
3497 * *lots* of attached tasks. So it may need several calls to read(),
3498 * but we cannot guarantee that the information we produce is correct
3499 * unless we produce it entirely atomically.
3500 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003501 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003502
Li Zefan24528252012-01-20 11:58:43 +08003503/* which pidlist file are we talking about? */
3504enum cgroup_filetype {
3505 CGROUP_FILE_PROCS,
3506 CGROUP_FILE_TASKS,
3507};
3508
3509/*
3510 * A pidlist is a list of pids that virtually represents the contents of one
3511 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3512 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3513 * to the cgroup.
3514 */
3515struct cgroup_pidlist {
3516 /*
3517 * used to find which pidlist is wanted. doesn't change as long as
3518 * this particular list stays in the list.
3519 */
3520 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3521 /* array of xids */
3522 pid_t *list;
3523 /* how many elements the above list has */
3524 int length;
Li Zefan24528252012-01-20 11:58:43 +08003525 /* each of these stored in a list by its cgroup */
3526 struct list_head links;
3527 /* pointer to the cgroup we belong to, for list removal purposes */
3528 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003529 /* for delayed destruction */
3530 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003531};
3532
Paul Menagebbcb81d2007-10-18 23:39:32 -07003533/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003534 * The following two functions "fix" the issue where there are more pids
3535 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3536 * TODO: replace with a kernel-wide solution to this problem
3537 */
3538#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3539static void *pidlist_allocate(int count)
3540{
3541 if (PIDLIST_TOO_LARGE(count))
3542 return vmalloc(count * sizeof(pid_t));
3543 else
3544 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3545}
Tejun Heob1a21362013-11-29 10:42:58 -05003546
Ben Blumd1d9fd32009-09-23 15:56:28 -07003547static void pidlist_free(void *p)
3548{
3549 if (is_vmalloc_addr(p))
3550 vfree(p);
3551 else
3552 kfree(p);
3553}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003554
3555/*
Tejun Heob1a21362013-11-29 10:42:58 -05003556 * Used to destroy all pidlists lingering waiting for destroy timer. None
3557 * should be left afterwards.
3558 */
3559static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3560{
3561 struct cgroup_pidlist *l, *tmp_l;
3562
3563 mutex_lock(&cgrp->pidlist_mutex);
3564 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3565 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3566 mutex_unlock(&cgrp->pidlist_mutex);
3567
3568 flush_workqueue(cgroup_pidlist_destroy_wq);
3569 BUG_ON(!list_empty(&cgrp->pidlists));
3570}
3571
3572static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3573{
3574 struct delayed_work *dwork = to_delayed_work(work);
3575 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3576 destroy_dwork);
3577 struct cgroup_pidlist *tofree = NULL;
3578
3579 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003580
3581 /*
Tejun Heo04502362013-11-29 10:42:59 -05003582 * Destroy iff we didn't get queued again. The state won't change
3583 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003584 */
Tejun Heo04502362013-11-29 10:42:59 -05003585 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003586 list_del(&l->links);
3587 pidlist_free(l->list);
3588 put_pid_ns(l->key.ns);
3589 tofree = l;
3590 }
3591
Tejun Heob1a21362013-11-29 10:42:58 -05003592 mutex_unlock(&l->owner->pidlist_mutex);
3593 kfree(tofree);
3594}
3595
3596/*
Ben Blum102a7752009-09-23 15:56:26 -07003597 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003598 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003599 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003600static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003601{
Ben Blum102a7752009-09-23 15:56:26 -07003602 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003603
3604 /*
3605 * we presume the 0th element is unique, so i starts at 1. trivial
3606 * edge cases first; no work needs to be done for either
3607 */
3608 if (length == 0 || length == 1)
3609 return length;
3610 /* src and dest walk down the list; dest counts unique elements */
3611 for (src = 1; src < length; src++) {
3612 /* find next unique element */
3613 while (list[src] == list[src-1]) {
3614 src++;
3615 if (src == length)
3616 goto after;
3617 }
3618 /* dest always points to where the next unique element goes */
3619 list[dest] = list[src];
3620 dest++;
3621 }
3622after:
Ben Blum102a7752009-09-23 15:56:26 -07003623 return dest;
3624}
3625
Tejun Heoafb2bc12013-11-29 10:42:59 -05003626/*
3627 * The two pid files - task and cgroup.procs - guaranteed that the result
3628 * is sorted, which forced this whole pidlist fiasco. As pid order is
3629 * different per namespace, each namespace needs differently sorted list,
3630 * making it impossible to use, for example, single rbtree of member tasks
3631 * sorted by task pointer. As pidlists can be fairly large, allocating one
3632 * per open file is dangerous, so cgroup had to implement shared pool of
3633 * pidlists keyed by cgroup and namespace.
3634 *
3635 * All this extra complexity was caused by the original implementation
3636 * committing to an entirely unnecessary property. In the long term, we
3637 * want to do away with it. Explicitly scramble sort order if
3638 * sane_behavior so that no such expectation exists in the new interface.
3639 *
3640 * Scrambling is done by swapping every two consecutive bits, which is
3641 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3642 */
3643static pid_t pid_fry(pid_t pid)
3644{
3645 unsigned a = pid & 0x55555555;
3646 unsigned b = pid & 0xAAAAAAAA;
3647
3648 return (a << 1) | (b >> 1);
3649}
3650
3651static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3652{
3653 if (cgroup_sane_behavior(cgrp))
3654 return pid_fry(pid);
3655 else
3656 return pid;
3657}
3658
Ben Blum102a7752009-09-23 15:56:26 -07003659static int cmppid(const void *a, const void *b)
3660{
3661 return *(pid_t *)a - *(pid_t *)b;
3662}
3663
Tejun Heoafb2bc12013-11-29 10:42:59 -05003664static int fried_cmppid(const void *a, const void *b)
3665{
3666 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3667}
3668
Ben Blum72a8cb32009-09-23 15:56:27 -07003669static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3670 enum cgroup_filetype type)
3671{
3672 struct cgroup_pidlist *l;
3673 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003674 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003675
Tejun Heoe6b81712013-11-29 10:42:59 -05003676 lockdep_assert_held(&cgrp->pidlist_mutex);
3677
3678 list_for_each_entry(l, &cgrp->pidlists, links)
3679 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003680 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003681 return NULL;
3682}
3683
Ben Blum72a8cb32009-09-23 15:56:27 -07003684/*
3685 * find the appropriate pidlist for our purpose (given procs vs tasks)
3686 * returns with the lock on that pidlist already held, and takes care
3687 * of the use count, or returns NULL with no locks held if we're out of
3688 * memory.
3689 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003690static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3691 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003692{
3693 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003694
Tejun Heoe6b81712013-11-29 10:42:59 -05003695 lockdep_assert_held(&cgrp->pidlist_mutex);
3696
3697 l = cgroup_pidlist_find(cgrp, type);
3698 if (l)
3699 return l;
3700
Ben Blum72a8cb32009-09-23 15:56:27 -07003701 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003702 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003703 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003704 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003705
Tejun Heob1a21362013-11-29 10:42:58 -05003706 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003707 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003708 /* don't need task_nsproxy() if we're looking at ourself */
3709 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003710 l->owner = cgrp;
3711 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003712 return l;
3713}
3714
3715/*
Ben Blum102a7752009-09-23 15:56:26 -07003716 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3717 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003718static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3719 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003720{
3721 pid_t *array;
3722 int length;
3723 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003724 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003725 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003726 struct cgroup_pidlist *l;
3727
Tejun Heo4bac00d2013-11-29 10:42:59 -05003728 lockdep_assert_held(&cgrp->pidlist_mutex);
3729
Ben Blum102a7752009-09-23 15:56:26 -07003730 /*
3731 * If cgroup gets more users after we read count, we won't have
3732 * enough space - tough. This race is indistinguishable to the
3733 * caller from the case that the additional cgroup users didn't
3734 * show up until sometime later on.
3735 */
3736 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003737 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003738 if (!array)
3739 return -ENOMEM;
3740 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003741 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003742 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003743 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003744 break;
Ben Blum102a7752009-09-23 15:56:26 -07003745 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003746 if (type == CGROUP_FILE_PROCS)
3747 pid = task_tgid_vnr(tsk);
3748 else
3749 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003750 if (pid > 0) /* make sure to only use valid results */
3751 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003752 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003753 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003754 length = n;
3755 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003756 if (cgroup_sane_behavior(cgrp))
3757 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3758 else
3759 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003760 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003761 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003762
Tejun Heoe6b81712013-11-29 10:42:59 -05003763 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003764 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003765 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003766 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003767 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003768 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003769
3770 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003771 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003772 l->list = array;
3773 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003774 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003775 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003776}
3777
Balbir Singh846c7bb2007-10-18 23:39:44 -07003778/**
Li Zefana043e3b2008-02-23 15:24:09 -08003779 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003780 * @stats: cgroupstats to fill information into
3781 * @dentry: A dentry entry belonging to the cgroup for which stats have
3782 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003783 *
3784 * Build and fill cgroupstats so that taskstats can export it to user
3785 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003786 */
3787int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3788{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003789 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003790 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003791 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003792 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003793
Tejun Heo2bd59d42014-02-11 11:52:49 -05003794 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3795 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3796 kernfs_type(kn) != KERNFS_DIR)
3797 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003798
Li Zefanbad34662014-02-14 16:54:28 +08003799 mutex_lock(&cgroup_mutex);
3800
Tejun Heo2bd59d42014-02-11 11:52:49 -05003801 /*
3802 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003803 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003804 * @kn->priv is RCU safe. Let's do the RCU dancing.
3805 */
3806 rcu_read_lock();
3807 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003808 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003809 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003810 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003811 return -ENOENT;
3812 }
Li Zefanbad34662014-02-14 16:54:28 +08003813 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003814
Tejun Heo9d800df2014-05-14 09:15:00 -04003815 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003816 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003817 switch (tsk->state) {
3818 case TASK_RUNNING:
3819 stats->nr_running++;
3820 break;
3821 case TASK_INTERRUPTIBLE:
3822 stats->nr_sleeping++;
3823 break;
3824 case TASK_UNINTERRUPTIBLE:
3825 stats->nr_uninterruptible++;
3826 break;
3827 case TASK_STOPPED:
3828 stats->nr_stopped++;
3829 break;
3830 default:
3831 if (delayacct_is_task_waiting_on_io(tsk))
3832 stats->nr_io_wait++;
3833 break;
3834 }
3835 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003836 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003837
Li Zefanbad34662014-02-14 16:54:28 +08003838 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003839 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003840}
3841
Paul Menage8f3ff202009-09-23 15:56:25 -07003842
Paul Menagecc31edc2008-10-18 20:28:04 -07003843/*
Ben Blum102a7752009-09-23 15:56:26 -07003844 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003845 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003846 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003847 */
3848
Ben Blum102a7752009-09-23 15:56:26 -07003849static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003850{
3851 /*
3852 * Initially we receive a position value that corresponds to
3853 * one more than the last pid shown (or 0 on the first call or
3854 * after a seek to the start). Use a binary-search to find the
3855 * next pid to display, if any
3856 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003857 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003858 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003859 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003860 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003861 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003862 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003863
Tejun Heo4bac00d2013-11-29 10:42:59 -05003864 mutex_lock(&cgrp->pidlist_mutex);
3865
3866 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003867 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003868 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003869 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003870 * could already have been destroyed.
3871 */
Tejun Heo5d224442013-12-05 12:28:04 -05003872 if (of->priv)
3873 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003874
3875 /*
3876 * Either this is the first start() after open or the matching
3877 * pidlist has been destroyed inbetween. Create a new one.
3878 */
Tejun Heo5d224442013-12-05 12:28:04 -05003879 if (!of->priv) {
3880 ret = pidlist_array_load(cgrp, type,
3881 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003882 if (ret)
3883 return ERR_PTR(ret);
3884 }
Tejun Heo5d224442013-12-05 12:28:04 -05003885 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003886
Paul Menagecc31edc2008-10-18 20:28:04 -07003887 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003888 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003889
Paul Menagecc31edc2008-10-18 20:28:04 -07003890 while (index < end) {
3891 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003892 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003893 index = mid;
3894 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003895 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003896 index = mid + 1;
3897 else
3898 end = mid;
3899 }
3900 }
3901 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003902 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003903 return NULL;
3904 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003905 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003906 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003907 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003908}
3909
Ben Blum102a7752009-09-23 15:56:26 -07003910static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003911{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003912 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003913 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003914
Tejun Heo5d224442013-12-05 12:28:04 -05003915 if (l)
3916 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003917 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003918 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003919}
3920
Ben Blum102a7752009-09-23 15:56:26 -07003921static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003922{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003923 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003924 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003925 pid_t *p = v;
3926 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003927 /*
3928 * Advance to the next pid in the array. If this goes off the
3929 * end, we're done
3930 */
3931 p++;
3932 if (p >= end) {
3933 return NULL;
3934 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003935 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003936 return p;
3937 }
3938}
3939
Ben Blum102a7752009-09-23 15:56:26 -07003940static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003941{
3942 return seq_printf(s, "%d\n", *(int *)v);
3943}
3944
Tejun Heo182446d2013-08-08 20:11:24 -04003945static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3946 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003947{
Tejun Heo182446d2013-08-08 20:11:24 -04003948 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003949}
3950
Tejun Heo182446d2013-08-08 20:11:24 -04003951static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3952 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003953{
Tejun Heo182446d2013-08-08 20:11:24 -04003954 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003955 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003956 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003957 else
Tejun Heo182446d2013-08-08 20:11:24 -04003958 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003959 return 0;
3960}
3961
Tejun Heo182446d2013-08-08 20:11:24 -04003962static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3963 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003964{
Tejun Heo182446d2013-08-08 20:11:24 -04003965 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003966}
3967
Tejun Heo182446d2013-08-08 20:11:24 -04003968static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3969 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003970{
3971 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003972 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003973 else
Tejun Heo182446d2013-08-08 20:11:24 -04003974 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003975 return 0;
3976}
3977
Tejun Heod5c56ce2013-06-03 19:14:34 -07003978static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003979 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003980 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003981 .seq_start = cgroup_pidlist_start,
3982 .seq_next = cgroup_pidlist_next,
3983 .seq_stop = cgroup_pidlist_stop,
3984 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003985 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003986 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003987 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003988 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003989 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003990 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003991 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003992 .read_u64 = cgroup_clone_children_read,
3993 .write_u64 = cgroup_clone_children_write,
3994 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003995 {
Tejun Heo873fe092013-04-14 20:15:26 -07003996 .name = "cgroup.sane_behavior",
3997 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003998 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003999 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004000 {
4001 .name = "cgroup.controllers",
4002 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
4003 .seq_show = cgroup_root_controllers_show,
4004 },
4005 {
4006 .name = "cgroup.controllers",
4007 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4008 .seq_show = cgroup_controllers_show,
4009 },
4010 {
4011 .name = "cgroup.subtree_control",
4012 .flags = CFTYPE_ONLY_ON_DFL,
4013 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004014 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004015 },
Tejun Heo842b5972014-04-25 18:28:02 -04004016 {
4017 .name = "cgroup.populated",
4018 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4019 .seq_show = cgroup_populated_show,
4020 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004021
4022 /*
4023 * Historical crazy stuff. These don't have "cgroup." prefix and
4024 * don't exist if sane_behavior. If you're depending on these, be
4025 * prepared to be burned.
4026 */
4027 {
4028 .name = "tasks",
4029 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004030 .seq_start = cgroup_pidlist_start,
4031 .seq_next = cgroup_pidlist_next,
4032 .seq_stop = cgroup_pidlist_stop,
4033 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004034 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004035 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004036 .mode = S_IRUGO | S_IWUSR,
4037 },
4038 {
4039 .name = "notify_on_release",
4040 .flags = CFTYPE_INSANE,
4041 .read_u64 = cgroup_read_notify_on_release,
4042 .write_u64 = cgroup_write_notify_on_release,
4043 },
Tejun Heo873fe092013-04-14 20:15:26 -07004044 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004045 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004046 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004047 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004048 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004049 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004050 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004051 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004052};
4053
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004054/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004055 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004056 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004057 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004058 *
4059 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004060 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004061static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004063 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004064 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004065
Tejun Heo8e3f6542012-04-01 12:09:55 -07004066 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004067 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004068 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004069
Tejun Heo69dfa002014-05-04 15:09:13 -04004070 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004071 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004072
Tejun Heo0adb0702014-02-12 09:29:48 -05004073 list_for_each_entry(cfts, &ss->cfts, node) {
4074 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004075 if (ret < 0)
4076 goto err;
4077 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004080err:
4081 cgroup_clear_dir(cgrp, subsys_mask);
4082 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083}
4084
Tejun Heo0c21ead2013-08-13 20:22:51 -04004085/*
4086 * css destruction is four-stage process.
4087 *
4088 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4089 * Implemented in kill_css().
4090 *
4091 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004092 * and thus css_tryget_online() is guaranteed to fail, the css can be
4093 * offlined by invoking offline_css(). After offlining, the base ref is
4094 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004095 *
4096 * 3. When the percpu_ref reaches zero, the only possible remaining
4097 * accessors are inside RCU read sections. css_release() schedules the
4098 * RCU callback.
4099 *
4100 * 4. After the grace period, the css can be freed. Implemented in
4101 * css_free_work_fn().
4102 *
4103 * It is actually hairier because both step 2 and 4 require process context
4104 * and thus involve punting to css->destroy_work adding two additional
4105 * steps to the already complex sequence.
4106 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004107static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004108{
4109 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004110 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004111 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004112
Tejun Heo0ae78e02013-08-13 11:01:54 -04004113 if (css->parent)
4114 css_put(css->parent);
4115
Tejun Heo0c21ead2013-08-13 20:22:51 -04004116 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004117 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004118}
4119
4120static void css_free_rcu_fn(struct rcu_head *rcu_head)
4121{
4122 struct cgroup_subsys_state *css =
4123 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4124
Tejun Heo0c21ead2013-08-13 20:22:51 -04004125 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004126 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004127}
4128
Tejun Heo25e15d82014-05-14 09:15:02 -04004129static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004130{
4131 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004132 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004133 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004134
Tejun Heo15a4c832014-05-04 15:09:14 -04004135 cgroup_idr_remove(&ss->css_idr, css->id);
4136
Tejun Heo0c21ead2013-08-13 20:22:51 -04004137 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004138}
4139
Tejun Heo25e15d82014-05-14 09:15:02 -04004140static void css_release(struct percpu_ref *ref)
4141{
4142 struct cgroup_subsys_state *css =
4143 container_of(ref, struct cgroup_subsys_state, refcnt);
4144
4145 INIT_WORK(&css->destroy_work, css_release_work_fn);
4146 queue_work(cgroup_destroy_wq, &css->destroy_work);
4147}
4148
Tejun Heoddfcada2014-05-04 15:09:14 -04004149static void init_and_link_css(struct cgroup_subsys_state *css,
4150 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151{
Tejun Heoddfcada2014-05-04 15:09:14 -04004152 cgroup_get(cgrp);
4153
Paul Menagebd89aab2007-10-18 23:40:44 -07004154 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004155 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004156 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004157
Tejun Heoddfcada2014-05-04 15:09:14 -04004158 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004159 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004160 css_get(css->parent);
4161 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004162 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004163 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004164
Tejun Heoca8bdca2013-08-26 18:40:56 -04004165 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166}
4167
Li Zefan2a4ac632013-07-31 16:16:40 +08004168/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004169static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004170{
Tejun Heo623f9262013-08-13 11:01:55 -04004171 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004172 int ret = 0;
4173
Tejun Heoa31f2d32012-11-19 08:13:37 -08004174 lockdep_assert_held(&cgroup_mutex);
4175
Tejun Heo92fb9742012-11-19 08:13:38 -08004176 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004177 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004178 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004179 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004180 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004181 }
Tejun Heob1929db2012-11-19 08:13:38 -08004182 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004183}
4184
Li Zefan2a4ac632013-07-31 16:16:40 +08004185/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004186static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004187{
Tejun Heo623f9262013-08-13 11:01:55 -04004188 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004189
4190 lockdep_assert_held(&cgroup_mutex);
4191
4192 if (!(css->flags & CSS_ONLINE))
4193 return;
4194
Li Zefand7eeac12013-03-12 15:35:59 -07004195 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004196 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004197
Tejun Heoeb954192013-08-08 20:11:23 -04004198 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004199 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004200
4201 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004202}
4203
Tejun Heoc81c925a2013-12-06 15:11:56 -05004204/**
4205 * create_css - create a cgroup_subsys_state
4206 * @cgrp: the cgroup new css will be associated with
4207 * @ss: the subsys of new css
4208 *
4209 * Create a new css associated with @cgrp - @ss pair. On success, the new
4210 * css is online and installed in @cgrp with all interface files created.
4211 * Returns 0 on success, -errno on failure.
4212 */
4213static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4214{
4215 struct cgroup *parent = cgrp->parent;
4216 struct cgroup_subsys_state *css;
4217 int err;
4218
Tejun Heoc81c925a2013-12-06 15:11:56 -05004219 lockdep_assert_held(&cgroup_mutex);
4220
4221 css = ss->css_alloc(cgroup_css(parent, ss));
4222 if (IS_ERR(css))
4223 return PTR_ERR(css);
4224
Tejun Heoddfcada2014-05-04 15:09:14 -04004225 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004226
Tejun Heoc81c925a2013-12-06 15:11:56 -05004227 err = percpu_ref_init(&css->refcnt, css_release);
4228 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004229 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004230
Tejun Heo15a4c832014-05-04 15:09:14 -04004231 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4232 if (err < 0)
4233 goto err_free_percpu_ref;
4234 css->id = err;
4235
Tejun Heoaec25022014-02-08 10:36:58 -05004236 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004237 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004238 goto err_free_id;
4239
4240 /* @css is ready to be brought online now, make it visible */
4241 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004242
4243 err = online_css(css);
4244 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004245 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004246
Tejun Heoc81c925a2013-12-06 15:11:56 -05004247 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4248 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004249 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 -04004250 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004251 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004252 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004253 ss->warned_broken_hierarchy = true;
4254 }
4255
4256 return 0;
4257
Li Zefan3eb59ec2014-03-18 17:02:36 +08004258err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004259 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004260err_free_id:
4261 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004262err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004263 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004264err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004265 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004266 return err;
4267}
4268
Tejun Heob3bfd982014-05-13 12:19:22 -04004269static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4270 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271{
Tejun Heoa9746d82014-05-13 12:19:22 -04004272 struct cgroup *parent, *cgrp;
4273 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004275 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004276 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004277
Tejun Heoa9746d82014-05-13 12:19:22 -04004278 parent = cgroup_kn_lock_live(parent_kn);
4279 if (!parent)
4280 return -ENODEV;
4281 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004282
4283 /* allocate the cgroup and its ID, 0 is reserved for the root */
4284 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4285 if (!cgrp) {
4286 ret = -ENOMEM;
4287 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004288 }
4289
4290 /*
4291 * Temporarily set the pointer to NULL, so idr_find() won't return
4292 * a half-baked cgroup.
4293 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004294 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004295 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004296 ret = -ENOMEM;
4297 goto out_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004298 }
4299
Paul Menagecc31edc2008-10-18 20:28:04 -07004300 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004301
Paul Menagebd89aab2007-10-18 23:40:44 -07004302 cgrp->parent = parent;
Tejun Heo9d800df2014-05-14 09:15:00 -04004303 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004304 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305
Li Zefanb6abdb02008-03-04 14:28:19 -08004306 if (notify_on_release(parent))
4307 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4308
Tejun Heo2260e7f2012-11-19 08:13:38 -08004309 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4310 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004311
Tejun Heo2bd59d42014-02-11 11:52:49 -05004312 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004313 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004314 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004315 ret = PTR_ERR(kn);
4316 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004317 }
4318 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004319
Tejun Heo6f305582014-02-12 09:29:50 -05004320 /*
4321 * This extra ref will be put in cgroup_free_fn() and guarantees
4322 * that @cgrp->kn is always accessible.
4323 */
4324 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325
Tejun Heo00356bd2013-06-18 11:14:22 -07004326 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004327
Tejun Heo4e139af2012-11-19 08:13:36 -08004328 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004329 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004330 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004331 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004332
Tejun Heo0d802552013-12-06 15:11:56 -05004333 /*
4334 * @cgrp is now fully operational. If something fails after this
4335 * point, it'll be released via the normal destruction path.
4336 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004337 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004338
Tejun Heoba0f4d72014-05-13 12:19:22 -04004339 ret = cgroup_kn_set_ugid(kn);
4340 if (ret)
4341 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004342
Tejun Heoba0f4d72014-05-13 12:19:22 -04004343 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4344 if (ret)
4345 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004346
Tejun Heo9d403e92013-12-06 15:11:56 -05004347 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004348 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004349 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004350 ret = create_css(cgrp, ss);
4351 if (ret)
4352 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004353 }
Tejun Heoa8638032012-11-09 09:12:29 -08004354 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004355
Tejun Heobd53d612014-04-23 11:13:16 -04004356 /*
4357 * On the default hierarchy, a child doesn't automatically inherit
4358 * child_subsys_mask from the parent. Each is configured manually.
4359 */
4360 if (!cgroup_on_dfl(cgrp))
4361 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004362
Tejun Heo2bd59d42014-02-11 11:52:49 -05004363 kernfs_activate(kn);
4364
Tejun Heoba0f4d72014-05-13 12:19:22 -04004365 ret = 0;
4366 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367
Tejun Heoba0f4d72014-05-13 12:19:22 -04004368out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004369 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004370out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004371 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004372out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004373 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004374 return ret;
4375
4376out_destroy:
4377 cgroup_destroy_locked(cgrp);
4378 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379}
4380
Tejun Heo223dbc32013-08-13 20:22:50 -04004381/*
4382 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004383 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4384 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004385 */
4386static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004387{
Tejun Heo223dbc32013-08-13 20:22:50 -04004388 struct cgroup_subsys_state *css =
4389 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004390
Tejun Heof20104d2013-08-13 20:22:50 -04004391 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004392 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004393 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004394
Tejun Heo09a503ea2013-08-13 20:22:50 -04004395 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004396}
4397
Tejun Heo223dbc32013-08-13 20:22:50 -04004398/* css kill confirmation processing requires process context, bounce */
4399static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004400{
4401 struct cgroup_subsys_state *css =
4402 container_of(ref, struct cgroup_subsys_state, refcnt);
4403
Tejun Heo223dbc32013-08-13 20:22:50 -04004404 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004405 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004406}
4407
Tejun Heof392e512014-04-23 11:13:14 -04004408/**
4409 * kill_css - destroy a css
4410 * @css: css to destroy
4411 *
4412 * This function initiates destruction of @css by removing cgroup interface
4413 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004414 * asynchronously once css_tryget_online() is guaranteed to fail and when
4415 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004416 */
4417static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004418{
Tejun Heo01f64742014-05-13 12:19:23 -04004419 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004420
Tejun Heo2bd59d42014-02-11 11:52:49 -05004421 /*
4422 * This must happen before css is disassociated with its cgroup.
4423 * See seq_css() for details.
4424 */
Tejun Heoaec25022014-02-08 10:36:58 -05004425 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004426
Tejun Heoedae0c32013-08-13 20:22:51 -04004427 /*
4428 * Killing would put the base ref, but we need to keep it alive
4429 * until after ->css_offline().
4430 */
4431 css_get(css);
4432
4433 /*
4434 * cgroup core guarantees that, by the time ->css_offline() is
4435 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004436 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004437 * proceed to offlining css's because percpu_ref_kill() doesn't
4438 * guarantee that the ref is seen as killed on all CPUs on return.
4439 *
4440 * Use percpu_ref_kill_and_confirm() to get notifications as each
4441 * css is confirmed to be seen as killed on all CPUs.
4442 */
4443 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004444}
4445
4446/**
4447 * cgroup_destroy_locked - the first stage of cgroup destruction
4448 * @cgrp: cgroup to be destroyed
4449 *
4450 * css's make use of percpu refcnts whose killing latency shouldn't be
4451 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004452 * guarantee that css_tryget_online() won't succeed by the time
4453 * ->css_offline() is invoked. To satisfy all the requirements,
4454 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004455 *
4456 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4457 * userland visible parts and start killing the percpu refcnts of
4458 * css's. Set up so that the next stage will be kicked off once all
4459 * the percpu refcnts are confirmed to be killed.
4460 *
4461 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4462 * rest of destruction. Once all cgroup references are gone, the
4463 * cgroup is RCU-freed.
4464 *
4465 * This function implements s1. After this step, @cgrp is gone as far as
4466 * the userland is concerned and a new cgroup with the same name may be
4467 * created. As cgroup doesn't care about the names internally, this
4468 * doesn't cause any problem.
4469 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004470static int cgroup_destroy_locked(struct cgroup *cgrp)
4471 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004473 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004474 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004475 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004476
Tejun Heo42809dd2012-11-19 08:13:37 -08004477 lockdep_assert_held(&cgroup_mutex);
4478
Tejun Heoddd69142013-06-12 21:04:54 -07004479 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004480 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004481 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004482 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004483 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004484 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004485 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004486 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004487 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004488
Tejun Heo1a90dd52012-11-05 09:16:59 -08004489 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004490 * Make sure there's no live children. We can't test ->children
4491 * emptiness as dead children linger on it while being destroyed;
4492 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4493 */
Tejun Heocbc125e2014-05-14 09:15:01 -04004494 if (cgroup_has_live_children(cgrp))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004495 return -EBUSY;
4496
4497 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004498 * Mark @cgrp dead. This prevents further task migration and child
4499 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004500 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004501 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004502 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004503 */
Tejun Heo54766d42013-06-12 21:04:53 -07004504 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004505
Tejun Heo249f3462014-05-14 09:15:01 -04004506 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004507 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004508 kill_css(css);
4509
Tejun Heo455050d2013-06-13 19:27:41 -07004510 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4511 raw_spin_lock(&release_list_lock);
4512 if (!list_empty(&cgrp->release_list))
4513 list_del_init(&cgrp->release_list);
4514 raw_spin_unlock(&release_list_lock);
4515
4516 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004517 * Remove @cgrp directory along with the base files. @cgrp has an
4518 * extra ref on its kn.
4519 */
4520 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004521
Tejun Heo9e4173e2014-05-14 09:15:01 -04004522 set_bit(CGRP_RELEASABLE, &cgrp->parent->flags);
4523 check_for_release(cgrp->parent);
4524
Tejun Heo249f3462014-05-14 09:15:01 -04004525 /* put the base reference */
4526 cgroup_put(cgrp);
4527
Tejun Heoea15f8c2013-06-13 19:27:42 -07004528 return 0;
4529};
4530
Tejun Heo2bd59d42014-02-11 11:52:49 -05004531static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004532{
Tejun Heoa9746d82014-05-13 12:19:22 -04004533 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004534 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004535
Tejun Heoa9746d82014-05-13 12:19:22 -04004536 cgrp = cgroup_kn_lock_live(kn);
4537 if (!cgrp)
4538 return 0;
4539 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004540
Tejun Heoa9746d82014-05-13 12:19:22 -04004541 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004542
Tejun Heoa9746d82014-05-13 12:19:22 -04004543 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004544
4545 /*
4546 * There are two control paths which try to determine cgroup from
4547 * dentry without going through kernfs - cgroupstats_build() and
4548 * css_tryget_online_from_dir(). Those are supported by RCU
4549 * protecting clearing of cgrp->kn->priv backpointer, which should
4550 * happen after all files under it have been removed.
4551 */
4552 if (!ret)
4553 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4554
Tejun Heo2bd59d42014-02-11 11:52:49 -05004555 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004556 return ret;
4557}
4558
Tejun Heo2bd59d42014-02-11 11:52:49 -05004559static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4560 .remount_fs = cgroup_remount,
4561 .show_options = cgroup_show_options,
4562 .mkdir = cgroup_mkdir,
4563 .rmdir = cgroup_rmdir,
4564 .rename = cgroup_rename,
4565};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004566
Tejun Heo15a4c832014-05-04 15:09:14 -04004567static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004568{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004569 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004570
4571 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004572
Tejun Heo648bb562012-11-19 08:13:36 -08004573 mutex_lock(&cgroup_mutex);
4574
Tejun Heo15a4c832014-05-04 15:09:14 -04004575 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004576 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004577
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004578 /* Create the root cgroup state for this subsystem */
4579 ss->root = &cgrp_dfl_root;
4580 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581 /* We don't handle early failures gracefully */
4582 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004583 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004584 if (early) {
4585 /* idr_alloc() can't be called safely during early init */
4586 css->id = 1;
4587 } else {
4588 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4589 BUG_ON(css->id < 0);
4590 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004591
Li Zefane8d55fd2008-04-29 01:00:13 -07004592 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004593 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004594 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004595 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004596 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004597
4598 need_forkexit_callback |= ss->fork || ss->exit;
4599
Li Zefane8d55fd2008-04-29 01:00:13 -07004600 /* At system boot, before all subsystems have been
4601 * registered, no tasks have been forked, so we don't
4602 * need to invoke fork callbacks here. */
4603 BUG_ON(!list_empty(&init_task.tasks));
4604
Tejun Heoae7f1642013-08-13 20:22:50 -04004605 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004606
Tejun Heof392e512014-04-23 11:13:14 -04004607 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004608
Ben Blume6a11052010-03-10 15:22:09 -08004609 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610}
4611
4612/**
Li Zefana043e3b2008-02-23 15:24:09 -08004613 * cgroup_init_early - cgroup initialization at system boot
4614 *
4615 * Initialize cgroups at system boot, and initialize any
4616 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 */
4618int __init cgroup_init_early(void)
4619{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004620 static struct cgroup_sb_opts __initdata opts =
4621 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004622 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004623 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004624
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004625 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004626 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004627
Tejun Heo3ed80a62014-02-08 10:36:58 -05004628 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004629 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004630 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4631 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004632 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004633 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4634 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635
Tejun Heoaec25022014-02-08 10:36:58 -05004636 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004637 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638
4639 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004640 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004641 }
4642 return 0;
4643}
4644
4645/**
Li Zefana043e3b2008-02-23 15:24:09 -08004646 * cgroup_init - cgroup initialization
4647 *
4648 * Register cgroup filesystem and /proc file, and initialize
4649 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650 */
4651int __init cgroup_init(void)
4652{
Tejun Heo30159ec2013-06-25 11:53:37 -07004653 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004654 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004655 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004656
Tejun Heo2bd59d42014-02-11 11:52:49 -05004657 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004659 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004660
Tejun Heo82fe9b02013-06-25 11:53:37 -07004661 /* Add init_css_set to the hash table */
4662 key = css_set_hash(init_css_set.subsys);
4663 hash_add(css_set_table, &init_css_set.hlist, key);
4664
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004665 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004666
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004667 mutex_unlock(&cgroup_mutex);
4668
Tejun Heo172a2c062014-03-19 10:23:53 -04004669 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004670 if (ss->early_init) {
4671 struct cgroup_subsys_state *css =
4672 init_css_set.subsys[ss->id];
4673
4674 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4675 GFP_KERNEL);
4676 BUG_ON(css->id < 0);
4677 } else {
4678 cgroup_init_subsys(ss, false);
4679 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004680
Tejun Heo2d8f2432014-04-23 11:13:15 -04004681 list_add_tail(&init_css_set.e_cset_node[ssid],
4682 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4683
Tejun Heo172a2c062014-03-19 10:23:53 -04004684 /*
4685 * cftype registration needs kmalloc and can't be done
4686 * during early_init. Register base cftypes separately.
4687 */
4688 if (ss->base_cftypes)
4689 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4690 }
Greg KH676db4a2010-08-05 13:53:35 -07004691
Paul Menageddbcc7e2007-10-18 23:39:30 -07004692 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004693 if (!cgroup_kobj)
4694 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004695
4696 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004697 if (err < 0) {
4698 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004699 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004700 }
4701
4702 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004703 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004704}
Paul Menageb4f48b62007-10-18 23:39:33 -07004705
Tejun Heoe5fca242013-11-22 17:14:39 -05004706static int __init cgroup_wq_init(void)
4707{
4708 /*
4709 * There isn't much point in executing destruction path in
4710 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004711 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004712 *
4713 * We would prefer to do this in cgroup_init() above, but that
4714 * is called before init_workqueues(): so leave this until after.
4715 */
Tejun Heo1a115332014-02-12 19:06:19 -05004716 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004717 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004718
4719 /*
4720 * Used to destroy pidlists and separate to serve as flush domain.
4721 * Cap @max_active to 1 too.
4722 */
4723 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4724 0, 1);
4725 BUG_ON(!cgroup_pidlist_destroy_wq);
4726
Tejun Heoe5fca242013-11-22 17:14:39 -05004727 return 0;
4728}
4729core_initcall(cgroup_wq_init);
4730
Paul Menagea4243162007-10-18 23:39:35 -07004731/*
4732 * proc_cgroup_show()
4733 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4734 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004735 */
4736
4737/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004738int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004739{
4740 struct pid *pid;
4741 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004742 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004743 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004744 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004745
4746 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004747 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004748 if (!buf)
4749 goto out;
4750
4751 retval = -ESRCH;
4752 pid = m->private;
4753 tsk = get_pid_task(pid, PIDTYPE_PID);
4754 if (!tsk)
4755 goto out_free;
4756
4757 retval = 0;
4758
4759 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004760 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004761
Tejun Heo985ed672014-03-19 10:23:53 -04004762 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004763 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004764 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004765 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004766
Tejun Heoa2dd4242014-03-19 10:23:55 -04004767 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004768 continue;
4769
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004770 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004771 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004772 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004773 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004774 if (strlen(root->name))
4775 seq_printf(m, "%sname=%s", count ? "," : "",
4776 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004777 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004778 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004779 path = cgroup_path(cgrp, buf, PATH_MAX);
4780 if (!path) {
4781 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004782 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004783 }
4784 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004785 seq_putc(m, '\n');
4786 }
4787
4788out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004789 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004790 mutex_unlock(&cgroup_mutex);
4791 put_task_struct(tsk);
4792out_free:
4793 kfree(buf);
4794out:
4795 return retval;
4796}
4797
Paul Menagea4243162007-10-18 23:39:35 -07004798/* Display information about each subsystem and each hierarchy */
4799static int proc_cgroupstats_show(struct seq_file *m, void *v)
4800{
Tejun Heo30159ec2013-06-25 11:53:37 -07004801 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004802 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004803
Paul Menage8bab8dd2008-04-04 14:29:57 -07004804 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004805 /*
4806 * ideally we don't want subsystems moving around while we do this.
4807 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4808 * subsys/hierarchy state.
4809 */
Paul Menagea4243162007-10-18 23:39:35 -07004810 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004811
4812 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004813 seq_printf(m, "%s\t%d\t%d\t%d\n",
4814 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004815 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004816
Paul Menagea4243162007-10-18 23:39:35 -07004817 mutex_unlock(&cgroup_mutex);
4818 return 0;
4819}
4820
4821static int cgroupstats_open(struct inode *inode, struct file *file)
4822{
Al Viro9dce07f2008-03-29 03:07:28 +00004823 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004824}
4825
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004826static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004827 .open = cgroupstats_open,
4828 .read = seq_read,
4829 .llseek = seq_lseek,
4830 .release = single_release,
4831};
4832
Paul Menageb4f48b62007-10-18 23:39:33 -07004833/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004834 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004835 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004836 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004837 * A task is associated with the init_css_set until cgroup_post_fork()
4838 * attaches it to the parent's css_set. Empty cg_list indicates that
4839 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004840 */
4841void cgroup_fork(struct task_struct *child)
4842{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004843 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004844 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004845}
4846
4847/**
Li Zefana043e3b2008-02-23 15:24:09 -08004848 * cgroup_post_fork - called on a new task after adding it to the task list
4849 * @child: the task in question
4850 *
Tejun Heo5edee612012-10-16 15:03:14 -07004851 * Adds the task to the list running through its css_set if necessary and
4852 * call the subsystem fork() callbacks. Has to be after the task is
4853 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004854 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004855 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004856 */
Paul Menage817929e2007-10-18 23:39:36 -07004857void cgroup_post_fork(struct task_struct *child)
4858{
Tejun Heo30159ec2013-06-25 11:53:37 -07004859 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004860 int i;
4861
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004862 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004863 * This may race against cgroup_enable_task_cg_links(). As that
4864 * function sets use_task_css_set_links before grabbing
4865 * tasklist_lock and we just went through tasklist_lock to add
4866 * @child, it's guaranteed that either we see the set
4867 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4868 * @child during its iteration.
4869 *
4870 * If we won the race, @child is associated with %current's
4871 * css_set. Grabbing css_set_rwsem guarantees both that the
4872 * association is stable, and, on completion of the parent's
4873 * migration, @child is visible in the source of migration or
4874 * already in the destination cgroup. This guarantee is necessary
4875 * when implementing operations which need to migrate all tasks of
4876 * a cgroup to another.
4877 *
4878 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4879 * will remain in init_css_set. This is safe because all tasks are
4880 * in the init_css_set before cg_links is enabled and there's no
4881 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004882 */
Paul Menage817929e2007-10-18 23:39:36 -07004883 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004884 struct css_set *cset;
4885
Tejun Heo96d365e2014-02-13 06:58:40 -05004886 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004887 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004888 if (list_empty(&child->cg_list)) {
4889 rcu_assign_pointer(child->cgroups, cset);
4890 list_add(&child->cg_list, &cset->tasks);
4891 get_css_set(cset);
4892 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004893 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004894 }
Tejun Heo5edee612012-10-16 15:03:14 -07004895
4896 /*
4897 * Call ss->fork(). This must happen after @child is linked on
4898 * css_set; otherwise, @child might change state between ->fork()
4899 * and addition to css_set.
4900 */
4901 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004902 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004903 if (ss->fork)
4904 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004905 }
Paul Menage817929e2007-10-18 23:39:36 -07004906}
Tejun Heo5edee612012-10-16 15:03:14 -07004907
Paul Menage817929e2007-10-18 23:39:36 -07004908/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004909 * cgroup_exit - detach cgroup from exiting task
4910 * @tsk: pointer to task_struct of exiting process
4911 *
4912 * Description: Detach cgroup from @tsk and release it.
4913 *
4914 * Note that cgroups marked notify_on_release force every task in
4915 * them to take the global cgroup_mutex mutex when exiting.
4916 * This could impact scaling on very large systems. Be reluctant to
4917 * use notify_on_release cgroups where very high task exit scaling
4918 * is required on large systems.
4919 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004920 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4921 * call cgroup_exit() while the task is still competent to handle
4922 * notify_on_release(), then leave the task attached to the root cgroup in
4923 * each hierarchy for the remainder of its exit. No need to bother with
4924 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004925 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004926 */
Li Zefan1ec41832014-03-28 15:22:19 +08004927void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004928{
Tejun Heo30159ec2013-06-25 11:53:37 -07004929 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004930 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004931 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004932 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004933
4934 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004935 * Unlink from @tsk from its css_set. As migration path can't race
4936 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004937 */
4938 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004939 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004940 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004941 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004942 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004943 }
4944
Paul Menageb4f48b62007-10-18 23:39:33 -07004945 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004946 cset = task_css_set(tsk);
4947 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004948
Li Zefan1ec41832014-03-28 15:22:19 +08004949 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004950 /* see cgroup_post_fork() for details */
4951 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004952 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004953 struct cgroup_subsys_state *old_css = cset->subsys[i];
4954 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004955
Tejun Heoeb954192013-08-08 20:11:23 -04004956 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004957 }
4958 }
4959 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004960
Tejun Heoeaf797a2014-02-25 10:04:03 -05004961 if (put_cset)
4962 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004963}
Paul Menage697f4162007-10-18 23:39:34 -07004964
Paul Menagebd89aab2007-10-18 23:40:44 -07004965static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004966{
Li Zefanf50daa72013-03-01 15:06:07 +08004967 if (cgroup_is_releasable(cgrp) &&
Tejun Heo9e4173e2014-05-14 09:15:01 -04004968 list_empty(&cgrp->cset_links) && !cgroup_has_live_children(cgrp)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004969 /*
4970 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004971 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004972 * it now
4973 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004974 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004975
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004976 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004977 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004978 list_empty(&cgrp->release_list)) {
4979 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004980 need_schedule_work = 1;
4981 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004982 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004983 if (need_schedule_work)
4984 schedule_work(&release_agent_work);
4985 }
4986}
4987
Paul Menage81a6a5c2007-10-18 23:39:38 -07004988/*
4989 * Notify userspace when a cgroup is released, by running the
4990 * configured release agent with the name of the cgroup (path
4991 * relative to the root of cgroup file system) as the argument.
4992 *
4993 * Most likely, this user command will try to rmdir this cgroup.
4994 *
4995 * This races with the possibility that some other task will be
4996 * attached to this cgroup before it is removed, or that some other
4997 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4998 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4999 * unused, and this cgroup will be reprieved from its death sentence,
5000 * to continue to serve a useful existence. Next time it's released,
5001 * we will get notified again, if it still has 'notify_on_release' set.
5002 *
5003 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5004 * means only wait until the task is successfully execve()'d. The
5005 * separate release agent task is forked by call_usermodehelper(),
5006 * then control in this thread returns here, without waiting for the
5007 * release agent task. We don't bother to wait because the caller of
5008 * this routine has no use for the exit status of the release agent
5009 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005010 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005011static void cgroup_release_agent(struct work_struct *work)
5012{
5013 BUG_ON(work != &release_agent_work);
5014 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005015 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005016 while (!list_empty(&release_list)) {
5017 char *argv[3], *envp[3];
5018 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005019 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005020 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 struct cgroup,
5022 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005023 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005024 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005025 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005026 if (!pathbuf)
5027 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005028 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5029 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005030 goto continue_free;
5031 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5032 if (!agentbuf)
5033 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005034
5035 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005036 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005037 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005038 argv[i] = NULL;
5039
5040 i = 0;
5041 /* minimal command environment */
5042 envp[i++] = "HOME=/";
5043 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5044 envp[i] = NULL;
5045
5046 /* Drop the lock while we invoke the usermode helper,
5047 * since the exec could involve hitting disk and hence
5048 * be a slow process */
5049 mutex_unlock(&cgroup_mutex);
5050 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005052 continue_free:
5053 kfree(pathbuf);
5054 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005055 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005056 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005057 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005058 mutex_unlock(&cgroup_mutex);
5059}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005060
5061static int __init cgroup_disable(char *str)
5062{
Tejun Heo30159ec2013-06-25 11:53:37 -07005063 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005064 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005065 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005066
5067 while ((token = strsep(&str, ",")) != NULL) {
5068 if (!*token)
5069 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005070
Tejun Heo3ed80a62014-02-08 10:36:58 -05005071 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005072 if (!strcmp(token, ss->name)) {
5073 ss->disabled = 1;
5074 printk(KERN_INFO "Disabling %s control group"
5075 " subsystem\n", ss->name);
5076 break;
5077 }
5078 }
5079 }
5080 return 1;
5081}
5082__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005083
Tejun Heob77d7b62013-08-13 11:01:54 -04005084/**
Tejun Heoec903c02014-05-13 12:11:01 -04005085 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005086 * @dentry: directory dentry of interest
5087 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005088 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005089 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5090 * to get the corresponding css and return it. If such css doesn't exist
5091 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005092 */
Tejun Heoec903c02014-05-13 12:11:01 -04005093struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5094 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005095{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005096 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5097 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005098 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005099
Tejun Heo35cf0832013-08-26 18:40:56 -04005100 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005101 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5102 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005103 return ERR_PTR(-EBADF);
5104
Tejun Heo5a17f542014-02-11 11:52:47 -05005105 rcu_read_lock();
5106
Tejun Heo2bd59d42014-02-11 11:52:49 -05005107 /*
5108 * This path doesn't originate from kernfs and @kn could already
5109 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005110 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005111 */
5112 cgrp = rcu_dereference(kn->priv);
5113 if (cgrp)
5114 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005115
Tejun Heoec903c02014-05-13 12:11:01 -04005116 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005117 css = ERR_PTR(-ENOENT);
5118
5119 rcu_read_unlock();
5120 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005121}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005122
Li Zefan1cb650b2013-08-19 10:05:24 +08005123/**
5124 * css_from_id - lookup css by id
5125 * @id: the cgroup id
5126 * @ss: cgroup subsys to be looked into
5127 *
5128 * Returns the css if there's valid one with @id, otherwise returns NULL.
5129 * Should be called under rcu_read_lock().
5130 */
5131struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5132{
Tejun Heo6fa49182014-05-04 15:09:13 -04005133 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005134 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005135}
5136
Paul Menagefe693432009-09-23 15:56:20 -07005137#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005138static struct cgroup_subsys_state *
5139debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005140{
5141 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5142
5143 if (!css)
5144 return ERR_PTR(-ENOMEM);
5145
5146 return css;
5147}
5148
Tejun Heoeb954192013-08-08 20:11:23 -04005149static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005150{
Tejun Heoeb954192013-08-08 20:11:23 -04005151 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005152}
5153
Tejun Heo182446d2013-08-08 20:11:24 -04005154static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5155 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005156{
Tejun Heo182446d2013-08-08 20:11:24 -04005157 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005158}
5159
Tejun Heo182446d2013-08-08 20:11:24 -04005160static u64 current_css_set_read(struct cgroup_subsys_state *css,
5161 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005162{
5163 return (u64)(unsigned long)current->cgroups;
5164}
5165
Tejun Heo182446d2013-08-08 20:11:24 -04005166static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005167 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005168{
5169 u64 count;
5170
5171 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005172 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005173 rcu_read_unlock();
5174 return count;
5175}
5176
Tejun Heo2da8ca82013-12-05 12:28:04 -05005177static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005178{
Tejun Heo69d02062013-06-12 21:04:50 -07005179 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005180 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005181 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005182
Tejun Heoe61734c2014-02-12 09:29:50 -05005183 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5184 if (!name_buf)
5185 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005186
Tejun Heo96d365e2014-02-13 06:58:40 -05005187 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005188 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005189 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005190 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005191 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005192
Tejun Heoa2dd4242014-03-19 10:23:55 -04005193 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005194 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005195 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005196 }
5197 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005198 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005199 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005200 return 0;
5201}
5202
5203#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005204static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005205{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005206 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005207 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005208
Tejun Heo96d365e2014-02-13 06:58:40 -05005209 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005210 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005211 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005212 struct task_struct *task;
5213 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005214
Tejun Heo5abb8852013-06-12 21:04:49 -07005215 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005216
Tejun Heo5abb8852013-06-12 21:04:49 -07005217 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005218 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5219 goto overflow;
5220 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005221 }
Tejun Heoc7561122014-02-25 10:04:01 -05005222
5223 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5224 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5225 goto overflow;
5226 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5227 }
5228 continue;
5229 overflow:
5230 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005231 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005232 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005233 return 0;
5234}
5235
Tejun Heo182446d2013-08-08 20:11:24 -04005236static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005237{
Tejun Heo182446d2013-08-08 20:11:24 -04005238 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005239}
5240
5241static struct cftype debug_files[] = {
5242 {
Paul Menagefe693432009-09-23 15:56:20 -07005243 .name = "taskcount",
5244 .read_u64 = debug_taskcount_read,
5245 },
5246
5247 {
5248 .name = "current_css_set",
5249 .read_u64 = current_css_set_read,
5250 },
5251
5252 {
5253 .name = "current_css_set_refcount",
5254 .read_u64 = current_css_set_refcount_read,
5255 },
5256
5257 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005258 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005259 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005260 },
5261
5262 {
5263 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005264 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005265 },
5266
5267 {
Paul Menagefe693432009-09-23 15:56:20 -07005268 .name = "releasable",
5269 .read_u64 = releasable_read,
5270 },
Paul Menagefe693432009-09-23 15:56:20 -07005271
Tejun Heo4baf6e32012-04-01 12:09:55 -07005272 { } /* terminate */
5273};
Paul Menagefe693432009-09-23 15:56:20 -07005274
Tejun Heo073219e2014-02-08 10:36:58 -05005275struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005276 .css_alloc = debug_css_alloc,
5277 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005278 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005279};
5280#endif /* CONFIG_CGROUP_DEBUG */