blob: dcb06e181ce4f6178a02bd82e3bc6d898faf533b [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 Heo9d755d32014-05-14 09:15:02 -0400179static bool cgroup_has_live_children(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400180static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400181 unsigned int ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800182static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400183static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400184static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400185static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400186static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
187 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500188static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800189
Tejun Heo6fa49182014-05-04 15:09:13 -0400190/* IDR wrappers which synchronize using cgroup_idr_lock */
191static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
192 gfp_t gfp_mask)
193{
194 int ret;
195
196 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400197 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400198 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400199 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400200 idr_preload_end();
201 return ret;
202}
203
204static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
205{
206 void *ret;
207
Tejun Heo54504e92014-05-13 12:10:59 -0400208 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400209 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400210 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400211 return ret;
212}
213
214static void cgroup_idr_remove(struct idr *idr, int id)
215{
Tejun Heo54504e92014-05-13 12:10:59 -0400216 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400217 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400218 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400219}
220
Tejun Heod51f39b2014-05-16 13:22:48 -0400221static struct cgroup *cgroup_parent(struct cgroup *cgrp)
222{
223 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
224
225 if (parent_css)
226 return container_of(parent_css, struct cgroup, self);
227 return NULL;
228}
229
Tejun Heo95109b62013-08-08 20:11:27 -0400230/**
231 * cgroup_css - obtain a cgroup's css for the specified subsystem
232 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400233 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400234 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400235 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
236 * function must be called either under cgroup_mutex or rcu_read_lock() and
237 * the caller is responsible for pinning the returned css if it wants to
238 * keep accessing it outside the said locks. This function may return
239 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400240 */
241static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400242 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400243{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400244 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500245 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500246 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400247 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400248 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400249}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400251/**
252 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
253 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400254 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400255 *
256 * Similar to cgroup_css() but returns the effctive css, which is defined
257 * as the matching css of the nearest ancestor including self which has @ss
258 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
259 * function is guaranteed to return non-NULL css.
260 */
261static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
262 struct cgroup_subsys *ss)
263{
264 lockdep_assert_held(&cgroup_mutex);
265
266 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400267 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400268
269 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
270 return NULL;
271
Tejun Heod51f39b2014-05-16 13:22:48 -0400272 while (cgroup_parent(cgrp) &&
273 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
274 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400275
276 return cgroup_css(cgrp, ss);
277}
278
Paul Menageddbcc7e2007-10-18 23:39:30 -0700279/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700280static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281{
Tejun Heo54766d42013-06-12 21:04:53 -0700282 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283}
284
Tejun Heob4168642014-05-13 12:16:21 -0400285struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500286{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500287 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400288 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500289
290 /*
291 * This is open and unprotected implementation of cgroup_css().
292 * seq_css() is only called from a kernfs file operation which has
293 * an active reference on the file. Because all the subsystem
294 * files are drained before a css is disassociated with a cgroup,
295 * the matching css from the cgroup's subsys table is guaranteed to
296 * be and stay valid until the enclosing operation is complete.
297 */
298 if (cft->ss)
299 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
300 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400301 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500302}
Tejun Heob4168642014-05-13 12:16:21 -0400303EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500304
Li Zefan78574cf2013-04-08 19:00:38 -0700305/**
306 * cgroup_is_descendant - test ancestry
307 * @cgrp: the cgroup to be tested
308 * @ancestor: possible ancestor of @cgrp
309 *
310 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
311 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
312 * and @ancestor are accessible.
313 */
314bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
315{
316 while (cgrp) {
317 if (cgrp == ancestor)
318 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400319 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700320 }
321 return false;
322}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700323
Adrian Bunke9685a02008-02-07 00:13:46 -0800324static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700325{
326 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700327 (1 << CGRP_RELEASABLE) |
328 (1 << CGRP_NOTIFY_ON_RELEASE);
329 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700330}
331
Adrian Bunke9685a02008-02-07 00:13:46 -0800332static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333{
Paul Menagebd89aab2007-10-18 23:40:44 -0700334 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335}
336
Tejun Heo30159ec2013-06-25 11:53:37 -0700337/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500338 * for_each_css - iterate all css's of a cgroup
339 * @css: the iteration cursor
340 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
341 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700342 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400343 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700344 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500345#define for_each_css(css, ssid, cgrp) \
346 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
347 if (!((css) = rcu_dereference_check( \
348 (cgrp)->subsys[(ssid)], \
349 lockdep_is_held(&cgroup_mutex)))) { } \
350 else
351
352/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400353 * for_each_e_css - iterate all effective css's of a cgroup
354 * @css: the iteration cursor
355 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
356 * @cgrp: the target cgroup to iterate css's of
357 *
358 * Should be called under cgroup_[tree_]mutex.
359 */
360#define for_each_e_css(css, ssid, cgrp) \
361 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
362 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
363 ; \
364 else
365
366/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500367 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700368 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500369 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700370 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500371#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500372 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
373 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700374
Tejun Heo985ed672014-03-19 10:23:53 -0400375/* iterate across the hierarchies */
376#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700377 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700378
Tejun Heof8f22e52014-04-23 11:13:16 -0400379/* iterate over child cgrps, lock should be held throughout iteration */
380#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400381 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400382 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400383 cgroup_is_dead(child); })) \
384 ; \
385 else
386
Paul Menage81a6a5c2007-10-18 23:39:38 -0700387/* the list of cgroups eligible for automatic release. Protected by
388 * release_list_lock */
389static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200390static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700391static void cgroup_release_agent(struct work_struct *work);
392static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700393static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700394
Tejun Heo69d02062013-06-12 21:04:50 -0700395/*
396 * A cgroup can be associated with multiple css_sets as different tasks may
397 * belong to different cgroups on different hierarchies. In the other
398 * direction, a css_set is naturally associated with multiple cgroups.
399 * This M:N relationship is represented by the following link structure
400 * which exists for each association and allows traversing the associations
401 * from both sides.
402 */
403struct cgrp_cset_link {
404 /* the cgroup and css_set this link associates */
405 struct cgroup *cgrp;
406 struct css_set *cset;
407
408 /* list of cgrp_cset_links anchored at cgrp->cset_links */
409 struct list_head cset_link;
410
411 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
412 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700413};
414
Tejun Heo172a2c062014-03-19 10:23:53 -0400415/*
416 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700417 * hierarchies being mounted. It contains a pointer to the root state
418 * for each subsystem. Also used to anchor the list of css_sets. Not
419 * reference-counted, to improve performance when child cgroups
420 * haven't been created.
421 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400422struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400423 .refcount = ATOMIC_INIT(1),
424 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
425 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
426 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
427 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
428 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
429};
Paul Menage817929e2007-10-18 23:39:36 -0700430
Tejun Heo172a2c062014-03-19 10:23:53 -0400431static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700432
Tejun Heo842b5972014-04-25 18:28:02 -0400433/**
434 * cgroup_update_populated - updated populated count of a cgroup
435 * @cgrp: the target cgroup
436 * @populated: inc or dec populated count
437 *
438 * @cgrp is either getting the first task (css_set) or losing the last.
439 * Update @cgrp->populated_cnt accordingly. The count is propagated
440 * towards root so that a given cgroup's populated_cnt is zero iff the
441 * cgroup and all its descendants are empty.
442 *
443 * @cgrp's interface file "cgroup.populated" is zero if
444 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
445 * changes from or to zero, userland is notified that the content of the
446 * interface file has changed. This can be used to detect when @cgrp and
447 * its descendants become populated or empty.
448 */
449static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
450{
451 lockdep_assert_held(&css_set_rwsem);
452
453 do {
454 bool trigger;
455
456 if (populated)
457 trigger = !cgrp->populated_cnt++;
458 else
459 trigger = !--cgrp->populated_cnt;
460
461 if (!trigger)
462 break;
463
464 if (cgrp->populated_kn)
465 kernfs_notify(cgrp->populated_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400466 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400467 } while (cgrp);
468}
469
Paul Menage7717f7b2009-09-23 15:56:22 -0700470/*
471 * hash table for cgroup groups. This improves the performance to find
472 * an existing css_set. This hash doesn't (currently) take into
473 * account cgroups in empty hierarchies.
474 */
Li Zefan472b1052008-04-29 01:00:11 -0700475#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800476static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700477
Li Zefan0ac801f2013-01-10 11:49:27 +0800478static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700479{
Li Zefan0ac801f2013-01-10 11:49:27 +0800480 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700481 struct cgroup_subsys *ss;
482 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700483
Tejun Heo30159ec2013-06-25 11:53:37 -0700484 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800485 key += (unsigned long)css[i];
486 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700487
Li Zefan0ac801f2013-01-10 11:49:27 +0800488 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700489}
490
Tejun Heo89c55092014-02-13 06:58:40 -0500491static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700492{
Tejun Heo69d02062013-06-12 21:04:50 -0700493 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400494 struct cgroup_subsys *ss;
495 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700496
Tejun Heo89c55092014-02-13 06:58:40 -0500497 lockdep_assert_held(&css_set_rwsem);
498
499 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700500 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700501
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700502 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400503 for_each_subsys(ss, ssid)
504 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700505 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700506 css_set_count--;
507
Tejun Heo69d02062013-06-12 21:04:50 -0700508 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700509 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700510
Tejun Heo69d02062013-06-12 21:04:50 -0700511 list_del(&link->cset_link);
512 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800513
Tejun Heo96d365e2014-02-13 06:58:40 -0500514 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400515 if (list_empty(&cgrp->cset_links)) {
516 cgroup_update_populated(cgrp, false);
517 if (notify_on_release(cgrp)) {
518 if (taskexit)
519 set_bit(CGRP_RELEASABLE, &cgrp->flags);
520 check_for_release(cgrp);
521 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700522 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700523
524 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700525 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700526
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700528}
529
Tejun Heo89c55092014-02-13 06:58:40 -0500530static void put_css_set(struct css_set *cset, bool taskexit)
531{
532 /*
533 * Ensure that the refcount doesn't hit zero while any readers
534 * can see it. Similar to atomic_dec_and_lock(), but for an
535 * rwlock
536 */
537 if (atomic_add_unless(&cset->refcount, -1, 1))
538 return;
539
540 down_write(&css_set_rwsem);
541 put_css_set_locked(cset, taskexit);
542 up_write(&css_set_rwsem);
543}
544
Paul Menage817929e2007-10-18 23:39:36 -0700545/*
546 * refcounted get/put for css_set objects
547 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700548static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700549{
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700551}
552
Tejun Heob326f9d2013-06-24 15:21:48 -0700553/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700554 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700555 * @cset: candidate css_set being tested
556 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700557 * @new_cgrp: cgroup that's being entered by the task
558 * @template: desired set of css pointers in css_set (pre-calculated)
559 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800560 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700561 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
562 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700563static bool compare_css_sets(struct css_set *cset,
564 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700565 struct cgroup *new_cgrp,
566 struct cgroup_subsys_state *template[])
567{
568 struct list_head *l1, *l2;
569
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400570 /*
571 * On the default hierarchy, there can be csets which are
572 * associated with the same set of cgroups but different csses.
573 * Let's first ensure that csses match.
574 */
575 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700576 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700577
578 /*
579 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400580 * different cgroups in hierarchies. As different cgroups may
581 * share the same effective css, this comparison is always
582 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700583 */
Tejun Heo69d02062013-06-12 21:04:50 -0700584 l1 = &cset->cgrp_links;
585 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700586 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700587 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700588 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700589
590 l1 = l1->next;
591 l2 = l2->next;
592 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700593 if (l1 == &cset->cgrp_links) {
594 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700595 break;
596 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700597 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700598 }
599 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700600 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
601 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
602 cgrp1 = link1->cgrp;
603 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700604 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700605 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700606
607 /*
608 * If this hierarchy is the hierarchy of the cgroup
609 * that's changing, then we need to check that this
610 * css_set points to the new cgroup; if it's any other
611 * hierarchy, then this css_set should point to the
612 * same cgroup as the old css_set.
613 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700614 if (cgrp1->root == new_cgrp->root) {
615 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700616 return false;
617 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700618 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700619 return false;
620 }
621 }
622 return true;
623}
624
Tejun Heob326f9d2013-06-24 15:21:48 -0700625/**
626 * find_existing_css_set - init css array and find the matching css_set
627 * @old_cset: the css_set that we're using before the cgroup transition
628 * @cgrp: the cgroup that we're moving into
629 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700630 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700631static struct css_set *find_existing_css_set(struct css_set *old_cset,
632 struct cgroup *cgrp,
633 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700634{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400635 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700636 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700637 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800638 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700639 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700640
Ben Blumaae8aab2010-03-10 15:22:07 -0800641 /*
642 * Build the set of subsystem state objects that we want to see in the
643 * new css_set. while subsystems can change globally, the entries here
644 * won't change, so no need for locking.
645 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700646 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400647 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400648 /*
649 * @ss is in this hierarchy, so we want the
650 * effective css from @cgrp.
651 */
652 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700653 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400654 /*
655 * @ss is not in this hierarchy, so we don't want
656 * to change the css.
657 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700659 }
660 }
661
Li Zefan0ac801f2013-01-10 11:49:27 +0800662 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 hash_for_each_possible(css_set_table, cset, hlist, key) {
664 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700665 continue;
666
667 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700668 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700669 }
Paul Menage817929e2007-10-18 23:39:36 -0700670
671 /* No existing cgroup group matched */
672 return NULL;
673}
674
Tejun Heo69d02062013-06-12 21:04:50 -0700675static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700676{
Tejun Heo69d02062013-06-12 21:04:50 -0700677 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700678
Tejun Heo69d02062013-06-12 21:04:50 -0700679 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
680 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700681 kfree(link);
682 }
683}
684
Tejun Heo69d02062013-06-12 21:04:50 -0700685/**
686 * allocate_cgrp_cset_links - allocate cgrp_cset_links
687 * @count: the number of links to allocate
688 * @tmp_links: list_head the allocated links are put on
689 *
690 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
691 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700692 */
Tejun Heo69d02062013-06-12 21:04:50 -0700693static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700694{
Tejun Heo69d02062013-06-12 21:04:50 -0700695 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700696 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700697
698 INIT_LIST_HEAD(tmp_links);
699
Li Zefan36553432008-07-29 22:33:19 -0700700 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700701 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700702 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700703 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700704 return -ENOMEM;
705 }
Tejun Heo69d02062013-06-12 21:04:50 -0700706 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700707 }
708 return 0;
709}
710
Li Zefanc12f65d2009-01-07 18:07:42 -0800711/**
712 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700713 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700714 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800715 * @cgrp: the destination cgroup
716 */
Tejun Heo69d02062013-06-12 21:04:50 -0700717static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
718 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800719{
Tejun Heo69d02062013-06-12 21:04:50 -0700720 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800721
Tejun Heo69d02062013-06-12 21:04:50 -0700722 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400723
724 if (cgroup_on_dfl(cgrp))
725 cset->dfl_cgrp = cgrp;
726
Tejun Heo69d02062013-06-12 21:04:50 -0700727 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
728 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400730
731 if (list_empty(&cgrp->cset_links))
732 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700733 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400734
Paul Menage7717f7b2009-09-23 15:56:22 -0700735 /*
736 * Always add links to the tail of the list so that the list
737 * is sorted by order of hierarchy creation
738 */
Tejun Heo69d02062013-06-12 21:04:50 -0700739 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800740}
741
Tejun Heob326f9d2013-06-24 15:21:48 -0700742/**
743 * find_css_set - return a new css_set with one cgroup updated
744 * @old_cset: the baseline css_set
745 * @cgrp: the cgroup to be updated
746 *
747 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
748 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700749 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700750static struct css_set *find_css_set(struct css_set *old_cset,
751 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700752{
Tejun Heob326f9d2013-06-24 15:21:48 -0700753 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700754 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700755 struct list_head tmp_links;
756 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400757 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800758 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400759 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700760
Tejun Heob326f9d2013-06-24 15:21:48 -0700761 lockdep_assert_held(&cgroup_mutex);
762
Paul Menage817929e2007-10-18 23:39:36 -0700763 /* First see if we already have a cgroup group that matches
764 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500765 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700766 cset = find_existing_css_set(old_cset, cgrp, template);
767 if (cset)
768 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500769 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700770
Tejun Heo5abb8852013-06-12 21:04:49 -0700771 if (cset)
772 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700773
Tejun Heof4f4be22013-06-12 21:04:51 -0700774 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700775 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700776 return NULL;
777
Tejun Heo69d02062013-06-12 21:04:50 -0700778 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700779 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700780 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700781 return NULL;
782 }
783
Tejun Heo5abb8852013-06-12 21:04:49 -0700784 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700785 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700786 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500787 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500788 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500789 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700790 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700791
792 /* Copy the set of subsystem state objects generated in
793 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700794 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700795
Tejun Heo96d365e2014-02-13 06:58:40 -0500796 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700797 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700798 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700799 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700800
Paul Menage7717f7b2009-09-23 15:56:22 -0700801 if (c->root == cgrp->root)
802 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700803 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700804 }
Paul Menage817929e2007-10-18 23:39:36 -0700805
Tejun Heo69d02062013-06-12 21:04:50 -0700806 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700807
Paul Menage817929e2007-10-18 23:39:36 -0700808 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700809
Tejun Heo2d8f2432014-04-23 11:13:15 -0400810 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700811 key = css_set_hash(cset->subsys);
812 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700813
Tejun Heo2d8f2432014-04-23 11:13:15 -0400814 for_each_subsys(ss, ssid)
815 list_add_tail(&cset->e_cset_node[ssid],
816 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
817
Tejun Heo96d365e2014-02-13 06:58:40 -0500818 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700819
Tejun Heo5abb8852013-06-12 21:04:49 -0700820 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700821}
822
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400823static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700824{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400825 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500826
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400827 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500828}
829
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400830static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500831{
832 int id;
833
834 lockdep_assert_held(&cgroup_mutex);
835
Tejun Heo985ed672014-03-19 10:23:53 -0400836 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500837 if (id < 0)
838 return id;
839
840 root->hierarchy_id = id;
841 return 0;
842}
843
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400844static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500845{
846 lockdep_assert_held(&cgroup_mutex);
847
848 if (root->hierarchy_id) {
849 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
850 root->hierarchy_id = 0;
851 }
852}
853
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400854static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500855{
856 if (root) {
857 /* hierarhcy ID shoulid already have been released */
858 WARN_ON_ONCE(root->hierarchy_id);
859
860 idr_destroy(&root->cgroup_idr);
861 kfree(root);
862 }
863}
864
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400865static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500866{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400867 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500868 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500869
Tejun Heo2bd59d42014-02-11 11:52:49 -0500870 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500871
Tejun Heo776f02f2014-02-12 09:29:50 -0500872 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -0400873 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -0500874
Tejun Heof2e85d52014-02-11 11:52:49 -0500875 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400876 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500877
878 /*
879 * Release all the links from cset_links to this hierarchy's
880 * root cgroup
881 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500882 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500883
884 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
885 list_del(&link->cset_link);
886 list_del(&link->cgrp_link);
887 kfree(link);
888 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500889 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500890
891 if (!list_empty(&root->root_list)) {
892 list_del(&root->root_list);
893 cgroup_root_count--;
894 }
895
896 cgroup_exit_root_id(root);
897
898 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500899
Tejun Heo2bd59d42014-02-11 11:52:49 -0500900 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500901 cgroup_free_root(root);
902}
903
Tejun Heoceb6a082014-02-25 10:04:02 -0500904/* look up cgroup associated with given css_set on the specified hierarchy */
905static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400906 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700907{
Paul Menage7717f7b2009-09-23 15:56:22 -0700908 struct cgroup *res = NULL;
909
Tejun Heo96d365e2014-02-13 06:58:40 -0500910 lockdep_assert_held(&cgroup_mutex);
911 lockdep_assert_held(&css_set_rwsem);
912
Tejun Heo5abb8852013-06-12 21:04:49 -0700913 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400914 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700915 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700916 struct cgrp_cset_link *link;
917
918 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700919 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700920
Paul Menage7717f7b2009-09-23 15:56:22 -0700921 if (c->root == root) {
922 res = c;
923 break;
924 }
925 }
926 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500927
Paul Menage7717f7b2009-09-23 15:56:22 -0700928 BUG_ON(!res);
929 return res;
930}
931
932/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500933 * Return the cgroup for "task" from the given hierarchy. Must be
934 * called with cgroup_mutex and css_set_rwsem held.
935 */
936static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400937 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500938{
939 /*
940 * No need to lock the task - since we hold cgroup_mutex the
941 * task can't change groups, so the only thing that can happen
942 * is that it exits and its css is set back to init_css_set.
943 */
944 return cset_cgroup_from_root(task_css_set(task), root);
945}
946
947/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 * A task must hold cgroup_mutex to modify cgroups.
949 *
950 * Any task can increment and decrement the count field without lock.
951 * So in general, code holding cgroup_mutex can't rely on the count
952 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800953 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954 * means that no tasks are currently attached, therefore there is no
955 * way a task attached to that cgroup can fork (the other way to
956 * increment the count). So code holding cgroup_mutex can safely
957 * assume that if the count is zero, it will stay zero. Similarly, if
958 * a task holds cgroup_mutex on a cgroup with zero count, it
959 * knows that the cgroup won't be removed, as cgroup_rmdir()
960 * needs that mutex.
961 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
963 * (usually) take cgroup_mutex. These are the two most performance
964 * critical pieces of code here. The exception occurs on cgroup_exit(),
965 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
966 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800967 * to the release agent with the name of the cgroup (path relative to
968 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969 *
970 * A cgroup can only be deleted if both its 'count' of using tasks
971 * is zero, and its list of 'children' cgroups is empty. Since all
972 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400973 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400975 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 *
977 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800978 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979 */
980
Tejun Heo69dfa002014-05-04 15:09:13 -0400981static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500982static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700983static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700984
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500985static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
986 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500988 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
989 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
990 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
991 cft->ss->name, cft->name);
992 else
993 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
994 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995}
996
Tejun Heof2e85d52014-02-11 11:52:49 -0500997/**
998 * cgroup_file_mode - deduce file mode of a control file
999 * @cft: the control file in question
1000 *
1001 * returns cft->mode if ->mode is not 0
1002 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1003 * returns S_IRUGO if it has only a read handler
1004 * returns S_IWUSR if it has only a write hander
1005 */
1006static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001007{
Tejun Heof2e85d52014-02-11 11:52:49 -05001008 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001009
Tejun Heof2e85d52014-02-11 11:52:49 -05001010 if (cft->mode)
1011 return cft->mode;
1012
1013 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1014 mode |= S_IRUGO;
1015
Tejun Heo6770c642014-05-13 12:16:21 -04001016 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001017 mode |= S_IWUSR;
1018
1019 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001020}
1021
Tejun Heo59f52962014-02-11 11:52:49 -05001022static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001024 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001025 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026}
1027
Tejun Heo59f52962014-02-11 11:52:49 -05001028static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029{
Tejun Heo9d755d32014-05-14 09:15:02 -04001030 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031}
1032
Tejun Heoa9746d82014-05-13 12:19:22 -04001033/**
1034 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1035 * @kn: the kernfs_node being serviced
1036 *
1037 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1038 * the method finishes if locking succeeded. Note that once this function
1039 * returns the cgroup returned by cgroup_kn_lock_live() may become
1040 * inaccessible any time. If the caller intends to continue to access the
1041 * cgroup, it should pin it before invoking this function.
1042 */
1043static void cgroup_kn_unlock(struct kernfs_node *kn)
1044{
1045 struct cgroup *cgrp;
1046
1047 if (kernfs_type(kn) == KERNFS_DIR)
1048 cgrp = kn->priv;
1049 else
1050 cgrp = kn->parent->priv;
1051
1052 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001053
1054 kernfs_unbreak_active_protection(kn);
1055 cgroup_put(cgrp);
1056}
1057
1058/**
1059 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1060 * @kn: the kernfs_node being serviced
1061 *
1062 * This helper is to be used by a cgroup kernfs method currently servicing
1063 * @kn. It breaks the active protection, performs cgroup locking and
1064 * verifies that the associated cgroup is alive. Returns the cgroup if
1065 * alive; otherwise, %NULL. A successful return should be undone by a
1066 * matching cgroup_kn_unlock() invocation.
1067 *
1068 * Any cgroup kernfs method implementation which requires locking the
1069 * associated cgroup should use this helper. It avoids nesting cgroup
1070 * locking under kernfs active protection and allows all kernfs operations
1071 * including self-removal.
1072 */
1073static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1074{
1075 struct cgroup *cgrp;
1076
1077 if (kernfs_type(kn) == KERNFS_DIR)
1078 cgrp = kn->priv;
1079 else
1080 cgrp = kn->parent->priv;
1081
1082 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001083 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001084 * active_ref. cgroup liveliness check alone provides enough
1085 * protection against removal. Ensure @cgrp stays accessible and
1086 * break the active_ref protection.
1087 */
1088 cgroup_get(cgrp);
1089 kernfs_break_active_protection(kn);
1090
Tejun Heoa9746d82014-05-13 12:19:22 -04001091 mutex_lock(&cgroup_mutex);
1092
1093 if (!cgroup_is_dead(cgrp))
1094 return cgrp;
1095
1096 cgroup_kn_unlock(kn);
1097 return NULL;
1098}
1099
Li Zefan2739d3c2013-01-21 18:18:33 +08001100static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001102 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103
Tejun Heo01f64742014-05-13 12:19:23 -04001104 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001105 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001106}
1107
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001108/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001109 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001110 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001111 * @subsys_mask: mask of the subsystem ids whose files should be removed
1112 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001113static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001114{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001115 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001116 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001117
Tejun Heob420ba72013-07-12 12:34:02 -07001118 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001119 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001120
Tejun Heo69dfa002014-05-04 15:09:13 -04001121 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001122 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001123 list_for_each_entry(cfts, &ss->cfts, node)
1124 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001125 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126}
1127
Tejun Heo69dfa002014-05-04 15:09:13 -04001128static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129{
Tejun Heo30159ec2013-06-25 11:53:37 -07001130 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001131 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132
Tejun Heoace2bee2014-02-11 11:52:47 -05001133 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001134
Tejun Heo5df36032014-03-19 10:23:54 -04001135 for_each_subsys(ss, ssid) {
1136 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001138
Tejun Heo7fd8c562014-04-23 11:13:16 -04001139 /* if @ss has non-root csses attached to it, can't move */
1140 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001141 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142
Tejun Heo5df36032014-03-19 10:23:54 -04001143 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001144 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001145 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146 }
1147
Tejun Heoa2dd4242014-03-19 10:23:55 -04001148 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1149 if (ret) {
1150 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001151 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152
Tejun Heoa2dd4242014-03-19 10:23:55 -04001153 /*
1154 * Rebinding back to the default root is not allowed to
1155 * fail. Using both default and non-default roots should
1156 * be rare. Moving subsystems back and forth even more so.
1157 * Just warn about it and continue.
1158 */
1159 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001160 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001161 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001162 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001163 }
Tejun Heo5df36032014-03-19 10:23:54 -04001164 }
Tejun Heo31261212013-06-28 17:07:30 -07001165
1166 /*
1167 * Nothing can fail from this point on. Remove files for the
1168 * removed subsystems and rebind each subsystem.
1169 */
Tejun Heo5df36032014-03-19 10:23:54 -04001170 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001171 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001172 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001173
Tejun Heo5df36032014-03-19 10:23:54 -04001174 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001175 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001176 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001177 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001178
Tejun Heo5df36032014-03-19 10:23:54 -04001179 if (!(ss_mask & (1 << ssid)))
1180 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001181
Tejun Heo5df36032014-03-19 10:23:54 -04001182 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001183 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001184
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001185 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001186
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001187 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1188 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001189 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001190 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001191
Tejun Heo2d8f2432014-04-23 11:13:15 -04001192 down_write(&css_set_rwsem);
1193 hash_for_each(css_set_table, i, cset, hlist)
1194 list_move_tail(&cset->e_cset_node[ss->id],
1195 &dst_root->cgrp.e_csets[ss->id]);
1196 up_write(&css_set_rwsem);
1197
Tejun Heof392e512014-04-23 11:13:14 -04001198 src_root->subsys_mask &= ~(1 << ssid);
1199 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1200
Tejun Heobd53d612014-04-23 11:13:16 -04001201 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001202 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001203 if (dst_root != &cgrp_dfl_root)
1204 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001205
Tejun Heo5df36032014-03-19 10:23:54 -04001206 if (ss->bind)
1207 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001208 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001209
Tejun Heoa2dd4242014-03-19 10:23:55 -04001210 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001211 return 0;
1212}
1213
Tejun Heo2bd59d42014-02-11 11:52:49 -05001214static int cgroup_show_options(struct seq_file *seq,
1215 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001216{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001217 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001218 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001219 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001220
Tejun Heob85d2042013-12-06 15:11:57 -05001221 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001222 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001223 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001224 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1225 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001226 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001227 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001228 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001229 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001230
1231 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001232 if (strlen(root->release_agent_path))
1233 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001234 spin_unlock(&release_agent_path_lock);
1235
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001236 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001237 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001238 if (strlen(root->name))
1239 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001240 return 0;
1241}
1242
1243struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001244 unsigned int subsys_mask;
1245 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001246 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001247 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001248 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001249 /* User explicitly requested empty subsystem */
1250 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001251};
1252
Ben Blumcf5d5942010-03-10 15:22:09 -08001253static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001254{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001255 char *token, *o = data;
1256 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001257 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001258 struct cgroup_subsys *ss;
1259 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001260
1261#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001262 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001263#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264
Paul Menagec6d57f32009-09-23 15:56:19 -07001265 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001266
1267 while ((token = strsep(&o, ",")) != NULL) {
1268 if (!*token)
1269 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001270 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001271 /* Explicitly have no subsystems */
1272 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001273 continue;
1274 }
1275 if (!strcmp(token, "all")) {
1276 /* Mutually exclusive option 'all' + subsystem name */
1277 if (one_ss)
1278 return -EINVAL;
1279 all_ss = true;
1280 continue;
1281 }
Tejun Heo873fe092013-04-14 20:15:26 -07001282 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1283 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1284 continue;
1285 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001286 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001287 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001288 continue;
1289 }
1290 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001291 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001292 continue;
1293 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001294 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001295 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001296 continue;
1297 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001298 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001299 /* Specifying two release agents is forbidden */
1300 if (opts->release_agent)
1301 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001302 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001303 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001304 if (!opts->release_agent)
1305 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001306 continue;
1307 }
1308 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001309 const char *name = token + 5;
1310 /* Can't specify an empty name */
1311 if (!strlen(name))
1312 return -EINVAL;
1313 /* Must match [\w.-]+ */
1314 for (i = 0; i < strlen(name); i++) {
1315 char c = name[i];
1316 if (isalnum(c))
1317 continue;
1318 if ((c == '.') || (c == '-') || (c == '_'))
1319 continue;
1320 return -EINVAL;
1321 }
1322 /* Specifying two names is forbidden */
1323 if (opts->name)
1324 return -EINVAL;
1325 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001326 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001327 GFP_KERNEL);
1328 if (!opts->name)
1329 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001330
1331 continue;
1332 }
1333
Tejun Heo30159ec2013-06-25 11:53:37 -07001334 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001335 if (strcmp(token, ss->name))
1336 continue;
1337 if (ss->disabled)
1338 continue;
1339
1340 /* Mutually exclusive option 'all' + subsystem name */
1341 if (all_ss)
1342 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001343 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001344 one_ss = true;
1345
1346 break;
1347 }
1348 if (i == CGROUP_SUBSYS_COUNT)
1349 return -ENOENT;
1350 }
1351
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001352 /* Consistency checks */
1353
Tejun Heo873fe092013-04-14 20:15:26 -07001354 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001355 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 -07001356
Tejun Heod3ba07c2014-02-13 06:58:38 -05001357 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1358 opts->cpuset_clone_children || opts->release_agent ||
1359 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001360 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001361 return -EINVAL;
1362 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001363 } else {
1364 /*
1365 * If the 'all' option was specified select all the
1366 * subsystems, otherwise if 'none', 'name=' and a subsystem
1367 * name options were not specified, let's default to 'all'
1368 */
1369 if (all_ss || (!one_ss && !opts->none && !opts->name))
1370 for_each_subsys(ss, i)
1371 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001372 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001373
Tejun Heoa2dd4242014-03-19 10:23:55 -04001374 /*
1375 * We either have to specify by name or by subsystems. (So
1376 * all empty hierarchies must have a name).
1377 */
1378 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001379 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001380 }
1381
Li Zefanf9ab5b52009-06-17 16:26:33 -07001382 /*
1383 * Option noprefix was introduced just for backward compatibility
1384 * with the old cpuset, so we allow noprefix only if mounting just
1385 * the cpuset subsystem.
1386 */
Tejun Heo93438622013-04-14 20:15:25 -07001387 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001388 return -EINVAL;
1389
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001390
1391 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001392 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001393 return -EINVAL;
1394
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 return 0;
1396}
1397
Tejun Heo2bd59d42014-02-11 11:52:49 -05001398static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399{
1400 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001401 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001403 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404
Tejun Heo873fe092013-04-14 20:15:26 -07001405 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001406 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001407 return -EINVAL;
1408 }
1409
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410 mutex_lock(&cgroup_mutex);
1411
1412 /* See what subsystems are wanted */
1413 ret = parse_cgroupfs_options(data, &opts);
1414 if (ret)
1415 goto out_unlock;
1416
Tejun Heof392e512014-04-23 11:13:14 -04001417 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001418 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001419 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001420
Tejun Heof392e512014-04-23 11:13:14 -04001421 added_mask = opts.subsys_mask & ~root->subsys_mask;
1422 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001423
Ben Blumcf5d5942010-03-10 15:22:09 -08001424 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001425 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001426 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001427 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001428 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1429 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001431 goto out_unlock;
1432 }
1433
Tejun Heof172e672013-06-28 17:07:30 -07001434 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001435 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001436 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001437 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001438 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439
Tejun Heo5df36032014-03-19 10:23:54 -04001440 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001441 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001443
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001444 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001445
Tejun Heo69e943b2014-02-08 10:36:58 -05001446 if (opts.release_agent) {
1447 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001448 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001449 spin_unlock(&release_agent_path_lock);
1450 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001452 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001453 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 return ret;
1456}
1457
Tejun Heoafeb0f92014-02-13 06:58:39 -05001458/*
1459 * To reduce the fork() overhead for systems that are not actually using
1460 * their cgroups capability, we don't maintain the lists running through
1461 * each css_set to its tasks until we see the list actually used - in other
1462 * words after the first mount.
1463 */
1464static bool use_task_css_set_links __read_mostly;
1465
1466static void cgroup_enable_task_cg_lists(void)
1467{
1468 struct task_struct *p, *g;
1469
Tejun Heo96d365e2014-02-13 06:58:40 -05001470 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001471
1472 if (use_task_css_set_links)
1473 goto out_unlock;
1474
1475 use_task_css_set_links = true;
1476
1477 /*
1478 * We need tasklist_lock because RCU is not safe against
1479 * while_each_thread(). Besides, a forking task that has passed
1480 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1481 * is not guaranteed to have its child immediately visible in the
1482 * tasklist if we walk through it with RCU.
1483 */
1484 read_lock(&tasklist_lock);
1485 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001486 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1487 task_css_set(p) != &init_css_set);
1488
1489 /*
1490 * We should check if the process is exiting, otherwise
1491 * it will race with cgroup_exit() in that the list
1492 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001493 * Do it while holding siglock so that we don't end up
1494 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001495 */
Tejun Heof153ad12014-02-25 09:56:49 -05001496 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001497 if (!(p->flags & PF_EXITING)) {
1498 struct css_set *cset = task_css_set(p);
1499
1500 list_add(&p->cg_list, &cset->tasks);
1501 get_css_set(cset);
1502 }
Tejun Heof153ad12014-02-25 09:56:49 -05001503 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001504 } while_each_thread(g, p);
1505 read_unlock(&tasklist_lock);
1506out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001507 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001508}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509
Paul Menagecc31edc2008-10-18 20:28:04 -07001510static void init_cgroup_housekeeping(struct cgroup *cgrp)
1511{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001512 struct cgroup_subsys *ss;
1513 int ssid;
1514
Tejun Heod5c419b2014-05-16 13:22:48 -04001515 INIT_LIST_HEAD(&cgrp->self.sibling);
1516 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001517 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001518 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001519 INIT_LIST_HEAD(&cgrp->pidlists);
1520 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001521 cgrp->self.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001522
1523 for_each_subsys(ss, ssid)
1524 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001525
1526 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001527}
Paul Menagec6d57f32009-09-23 15:56:19 -07001528
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001529static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001530 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001532 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001533
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001535 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001536 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001537 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001538 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
Paul Menagec6d57f32009-09-23 15:56:19 -07001540 root->flags = opts->flags;
1541 if (opts->release_agent)
1542 strcpy(root->release_agent_path, opts->release_agent);
1543 if (opts->name)
1544 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001545 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001546 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001547}
1548
Tejun Heo69dfa002014-05-04 15:09:13 -04001549static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001550{
Tejun Heod427dfe2014-02-11 11:52:48 -05001551 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001552 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001553 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001554 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001555
Tejun Heod427dfe2014-02-11 11:52:48 -05001556 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001557
Tejun Heo6fa49182014-05-04 15:09:13 -04001558 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001559 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001560 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001561 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001562
Tejun Heo9d755d32014-05-14 09:15:02 -04001563 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release);
1564 if (ret)
1565 goto out;
1566
Tejun Heod427dfe2014-02-11 11:52:48 -05001567 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001568 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001569 * but that's OK - it can only be increased by someone holding
1570 * cgroup_lock, and that's us. The worst that can happen is that we
1571 * have some link structures left over
1572 */
1573 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001575 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576
Tejun Heo985ed672014-03-19 10:23:53 -04001577 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001578 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001579 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580
Tejun Heo2bd59d42014-02-11 11:52:49 -05001581 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1582 KERNFS_ROOT_CREATE_DEACTIVATED,
1583 root_cgrp);
1584 if (IS_ERR(root->kf_root)) {
1585 ret = PTR_ERR(root->kf_root);
1586 goto exit_root_id;
1587 }
1588 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589
Tejun Heod427dfe2014-02-11 11:52:48 -05001590 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1591 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001592 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Tejun Heo5df36032014-03-19 10:23:54 -04001594 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001595 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001596 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001597
Tejun Heod427dfe2014-02-11 11:52:48 -05001598 /*
1599 * There must be no failure case after here, since rebinding takes
1600 * care of subsystems' refcounts, which are explicitly dropped in
1601 * the failure exit path.
1602 */
1603 list_add(&root->root_list, &cgroup_roots);
1604 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
Tejun Heod427dfe2014-02-11 11:52:48 -05001606 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001607 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001608 * objects.
1609 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001610 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001611 hash_for_each(css_set_table, i, cset, hlist)
1612 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001613 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614
Tejun Heod5c419b2014-05-16 13:22:48 -04001615 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001616 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001617
Tejun Heo2bd59d42014-02-11 11:52:49 -05001618 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001619 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001620 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001621
Tejun Heo2bd59d42014-02-11 11:52:49 -05001622destroy_root:
1623 kernfs_destroy_root(root->kf_root);
1624 root->kf_root = NULL;
1625exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001626 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001627cancel_ref:
1628 percpu_ref_cancel_init(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001629out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001630 free_cgrp_cset_links(&tmp_links);
1631 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632}
1633
Al Virof7e83572010-07-26 13:23:11 +04001634static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001636 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001638 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001640 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001641 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001642 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001643
1644 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001645 * The first time anyone tries to mount a cgroup, enable the list
1646 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001647 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001648 if (!use_task_css_set_links)
1649 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001650
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001651 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001656 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001657
Tejun Heo2bd59d42014-02-11 11:52:49 -05001658 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001659 if (!opts.subsys_mask && !opts.none && !opts.name) {
1660 cgrp_dfl_root_visible = true;
1661 root = &cgrp_dfl_root;
1662 cgroup_get(&root->cgrp);
1663 ret = 0;
1664 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 }
1666
Tejun Heo985ed672014-03-19 10:23:53 -04001667 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001668 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001670 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001671 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001672
Paul Menage817929e2007-10-18 23:39:36 -07001673 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001674 * If we asked for a name then it must match. Also, if
1675 * name matches but sybsys_mask doesn't, we should fail.
1676 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001677 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001678 if (opts.name) {
1679 if (strcmp(opts.name, root->name))
1680 continue;
1681 name_match = true;
1682 }
Tejun Heo31261212013-06-28 17:07:30 -07001683
1684 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001685 * If we asked for subsystems (or explicitly for no
1686 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001687 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001688 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001689 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001690 if (!name_match)
1691 continue;
1692 ret = -EBUSY;
1693 goto out_unlock;
1694 }
Tejun Heo873fe092013-04-14 20:15:26 -07001695
Tejun Heoc7ba8282013-06-29 14:06:10 -07001696 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001697 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001698 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001699 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001700 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001701 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001702 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001703 }
Tejun Heo873fe092013-04-14 20:15:26 -07001704 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001705
Tejun Heo776f02f2014-02-12 09:29:50 -05001706 /*
Tejun Heo9d755d32014-05-14 09:15:02 -04001707 * A root's lifetime is governed by its root cgroup.
1708 * tryget_live failure indicate that the root is being
1709 * destroyed. Wait for destruction to complete so that the
1710 * subsystems are free. We can use wait_queue for the wait
1711 * but this path is super cold. Let's just sleep for a bit
1712 * and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001713 */
Tejun Heo9d755d32014-05-14 09:15:02 -04001714 if (!percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001715 mutex_unlock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001716 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001717 ret = restart_syscall();
1718 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001719 }
1720
1721 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001722 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 }
1724
Tejun Heo172a2c062014-03-19 10:23:53 -04001725 /*
1726 * No such thing, create a new one. name= matching without subsys
1727 * specification is allowed for already existing hierarchies but we
1728 * can't create new one without subsys specification.
1729 */
1730 if (!opts.subsys_mask && !opts.none) {
1731 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001732 goto out_unlock;
1733 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734
Tejun Heo172a2c062014-03-19 10:23:53 -04001735 root = kzalloc(sizeof(*root), GFP_KERNEL);
1736 if (!root) {
1737 ret = -ENOMEM;
1738 goto out_unlock;
1739 }
1740
1741 init_cgroup_root(root, &opts);
1742
Tejun Heo35585572014-02-13 06:58:38 -05001743 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001744 if (ret)
1745 cgroup_free_root(root);
1746
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001747out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001748 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001749out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001750 kfree(opts.release_agent);
1751 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001752
Tejun Heo2bd59d42014-02-11 11:52:49 -05001753 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001754 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001755
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001756 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1757 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001758 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001759 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760}
1761
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001762static void cgroup_kill_sb(struct super_block *sb)
1763{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001764 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001765 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766
Tejun Heo9d755d32014-05-14 09:15:02 -04001767 /*
1768 * If @root doesn't have any mounts or children, start killing it.
1769 * This prevents new mounts by disabling percpu_ref_tryget_live().
1770 * cgroup_mount() may wait for @root's release.
1771 */
1772 if (cgroup_has_live_children(&root->cgrp))
1773 cgroup_put(&root->cgrp);
1774 else
1775 percpu_ref_kill(&root->cgrp.self.refcnt);
1776
Tejun Heo2bd59d42014-02-11 11:52:49 -05001777 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778}
1779
1780static struct file_system_type cgroup_fs_type = {
1781 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001782 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783 .kill_sb = cgroup_kill_sb,
1784};
1785
Greg KH676db4a2010-08-05 13:53:35 -07001786static struct kobject *cgroup_kobj;
1787
Li Zefana043e3b2008-02-23 15:24:09 -08001788/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001789 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001790 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001791 * @buf: the buffer to write the path into
1792 * @buflen: the length of the buffer
1793 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001794 * Determine @task's cgroup on the first (the one with the lowest non-zero
1795 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1796 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1797 * cgroup controller callbacks.
1798 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001799 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001800 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001801char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001802{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001803 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001804 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001805 int hierarchy_id = 1;
1806 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001807
1808 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001809 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001810
Tejun Heo913ffdb2013-07-11 16:34:48 -07001811 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1812
Tejun Heo857a2be2013-04-14 20:50:08 -07001813 if (root) {
1814 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001815 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001816 } else {
1817 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001818 if (strlcpy(buf, "/", buflen) < buflen)
1819 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001820 }
1821
Tejun Heo96d365e2014-02-13 06:58:40 -05001822 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001823 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001824 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001825}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001826EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001827
Tejun Heob3dc0942014-02-25 10:04:01 -05001828/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001829struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001830 /* the src and dst cset list running through cset->mg_node */
1831 struct list_head src_csets;
1832 struct list_head dst_csets;
1833
1834 /*
1835 * Fields for cgroup_taskset_*() iteration.
1836 *
1837 * Before migration is committed, the target migration tasks are on
1838 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1839 * the csets on ->dst_csets. ->csets point to either ->src_csets
1840 * or ->dst_csets depending on whether migration is committed.
1841 *
1842 * ->cur_csets and ->cur_task point to the current task position
1843 * during iteration.
1844 */
1845 struct list_head *csets;
1846 struct css_set *cur_cset;
1847 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001848};
1849
1850/**
1851 * cgroup_taskset_first - reset taskset and return the first task
1852 * @tset: taskset of interest
1853 *
1854 * @tset iteration is initialized and the first task is returned.
1855 */
1856struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1857{
Tejun Heob3dc0942014-02-25 10:04:01 -05001858 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1859 tset->cur_task = NULL;
1860
1861 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001862}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001863
1864/**
1865 * cgroup_taskset_next - iterate to the next task in taskset
1866 * @tset: taskset of interest
1867 *
1868 * Return the next task in @tset. Iteration must have been initialized
1869 * with cgroup_taskset_first().
1870 */
1871struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1872{
Tejun Heob3dc0942014-02-25 10:04:01 -05001873 struct css_set *cset = tset->cur_cset;
1874 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875
Tejun Heob3dc0942014-02-25 10:04:01 -05001876 while (&cset->mg_node != tset->csets) {
1877 if (!task)
1878 task = list_first_entry(&cset->mg_tasks,
1879 struct task_struct, cg_list);
1880 else
1881 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001882
Tejun Heob3dc0942014-02-25 10:04:01 -05001883 if (&task->cg_list != &cset->mg_tasks) {
1884 tset->cur_cset = cset;
1885 tset->cur_task = task;
1886 return task;
1887 }
1888
1889 cset = list_next_entry(cset, mg_node);
1890 task = NULL;
1891 }
1892
1893 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001894}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001895
1896/**
Ben Blum74a11662011-05-26 16:25:20 -07001897 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001898 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001899 * @tsk: the task being migrated
1900 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001901 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001902 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001903 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001904static void cgroup_task_migrate(struct cgroup *old_cgrp,
1905 struct task_struct *tsk,
1906 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001907{
Tejun Heo5abb8852013-06-12 21:04:49 -07001908 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001909
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001910 lockdep_assert_held(&cgroup_mutex);
1911 lockdep_assert_held(&css_set_rwsem);
1912
Ben Blum74a11662011-05-26 16:25:20 -07001913 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001914 * We are synchronized through threadgroup_lock() against PF_EXITING
1915 * setting such that we can't race against cgroup_exit() changing the
1916 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001917 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001918 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001919 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001920
Tejun Heob3dc0942014-02-25 10:04:01 -05001921 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001922 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001923
Tejun Heo1b9aba42014-03-19 17:43:21 -04001924 /*
1925 * Use move_tail so that cgroup_taskset_first() still returns the
1926 * leader after migration. This works because cgroup_migrate()
1927 * ensures that the dst_cset of the leader is the first on the
1928 * tset's dst_csets list.
1929 */
1930 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001931
1932 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001933 * We just gained a reference on old_cset by taking it from the
1934 * task. As trading it for new_cset is protected by cgroup_mutex,
1935 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001936 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001937 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001938 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001939}
1940
Li Zefana043e3b2008-02-23 15:24:09 -08001941/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001942 * cgroup_migrate_finish - cleanup after attach
1943 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001944 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001945 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1946 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001947 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001948static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001949{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001950 struct css_set *cset, *tmp_cset;
1951
1952 lockdep_assert_held(&cgroup_mutex);
1953
1954 down_write(&css_set_rwsem);
1955 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1956 cset->mg_src_cgrp = NULL;
1957 cset->mg_dst_cset = NULL;
1958 list_del_init(&cset->mg_preload_node);
1959 put_css_set_locked(cset, false);
1960 }
1961 up_write(&css_set_rwsem);
1962}
1963
1964/**
1965 * cgroup_migrate_add_src - add a migration source css_set
1966 * @src_cset: the source css_set to add
1967 * @dst_cgrp: the destination cgroup
1968 * @preloaded_csets: list of preloaded css_sets
1969 *
1970 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1971 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1972 * up by cgroup_migrate_finish().
1973 *
1974 * This function may be called without holding threadgroup_lock even if the
1975 * target is a process. Threads may be created and destroyed but as long
1976 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1977 * the preloaded css_sets are guaranteed to cover all migrations.
1978 */
1979static void cgroup_migrate_add_src(struct css_set *src_cset,
1980 struct cgroup *dst_cgrp,
1981 struct list_head *preloaded_csets)
1982{
1983 struct cgroup *src_cgrp;
1984
1985 lockdep_assert_held(&cgroup_mutex);
1986 lockdep_assert_held(&css_set_rwsem);
1987
1988 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1989
Tejun Heo1958d2d2014-02-25 10:04:03 -05001990 if (!list_empty(&src_cset->mg_preload_node))
1991 return;
1992
1993 WARN_ON(src_cset->mg_src_cgrp);
1994 WARN_ON(!list_empty(&src_cset->mg_tasks));
1995 WARN_ON(!list_empty(&src_cset->mg_node));
1996
1997 src_cset->mg_src_cgrp = src_cgrp;
1998 get_css_set(src_cset);
1999 list_add(&src_cset->mg_preload_node, preloaded_csets);
2000}
2001
2002/**
2003 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002004 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002005 * @preloaded_csets: list of preloaded source css_sets
2006 *
2007 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2008 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002009 * pins all destination css_sets, links each to its source, and append them
2010 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2011 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002012 *
2013 * This function must be called after cgroup_migrate_add_src() has been
2014 * called on each migration source css_set. After migration is performed
2015 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2016 * @preloaded_csets.
2017 */
2018static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2019 struct list_head *preloaded_csets)
2020{
2021 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002022 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002023
2024 lockdep_assert_held(&cgroup_mutex);
2025
Tejun Heof8f22e52014-04-23 11:13:16 -04002026 /*
2027 * Except for the root, child_subsys_mask must be zero for a cgroup
2028 * with tasks so that child cgroups don't compete against tasks.
2029 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002030 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002031 dst_cgrp->child_subsys_mask)
2032 return -EBUSY;
2033
Tejun Heo1958d2d2014-02-25 10:04:03 -05002034 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002035 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002036 struct css_set *dst_cset;
2037
Tejun Heof817de92014-04-23 11:13:16 -04002038 dst_cset = find_css_set(src_cset,
2039 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002040 if (!dst_cset)
2041 goto err;
2042
2043 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002044
2045 /*
2046 * If src cset equals dst, it's noop. Drop the src.
2047 * cgroup_migrate() will skip the cset too. Note that we
2048 * can't handle src == dst as some nodes are used by both.
2049 */
2050 if (src_cset == dst_cset) {
2051 src_cset->mg_src_cgrp = NULL;
2052 list_del_init(&src_cset->mg_preload_node);
2053 put_css_set(src_cset, false);
2054 put_css_set(dst_cset, false);
2055 continue;
2056 }
2057
Tejun Heo1958d2d2014-02-25 10:04:03 -05002058 src_cset->mg_dst_cset = dst_cset;
2059
2060 if (list_empty(&dst_cset->mg_preload_node))
2061 list_add(&dst_cset->mg_preload_node, &csets);
2062 else
2063 put_css_set(dst_cset, false);
2064 }
2065
Tejun Heof817de92014-04-23 11:13:16 -04002066 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002067 return 0;
2068err:
2069 cgroup_migrate_finish(&csets);
2070 return -ENOMEM;
2071}
2072
2073/**
2074 * cgroup_migrate - migrate a process or task to a cgroup
2075 * @cgrp: the destination cgroup
2076 * @leader: the leader of the process or the task to migrate
2077 * @threadgroup: whether @leader points to the whole process or a single task
2078 *
2079 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2080 * process, the caller must be holding threadgroup_lock of @leader. The
2081 * caller is also responsible for invoking cgroup_migrate_add_src() and
2082 * cgroup_migrate_prepare_dst() on the targets before invoking this
2083 * function and following up with cgroup_migrate_finish().
2084 *
2085 * As long as a controller's ->can_attach() doesn't fail, this function is
2086 * guaranteed to succeed. This means that, excluding ->can_attach()
2087 * failure, when migrating multiple targets, the success or failure can be
2088 * decided for all targets by invoking group_migrate_prepare_dst() before
2089 * actually starting migrating.
2090 */
2091static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2092 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002093{
Tejun Heob3dc0942014-02-25 10:04:01 -05002094 struct cgroup_taskset tset = {
2095 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2096 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2097 .csets = &tset.src_csets,
2098 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002099 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002100 struct css_set *cset, *tmp_cset;
2101 struct task_struct *task, *tmp_task;
2102 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002103
2104 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002105 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2106 * already PF_EXITING could be freed from underneath us unless we
2107 * take an rcu_read_lock.
2108 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002109 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002110 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002111 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002112 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002113 /* @task either already exited or can't exit until the end */
2114 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002115 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002116
Tejun Heoeaf797a2014-02-25 10:04:03 -05002117 /* leave @task alone if post_fork() hasn't linked it yet */
2118 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002119 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002120
Tejun Heob3dc0942014-02-25 10:04:01 -05002121 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002122 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002123 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002124
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002125 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002126 * cgroup_taskset_first() must always return the leader.
2127 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002128 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002129 list_move_tail(&task->cg_list, &cset->mg_tasks);
2130 if (list_empty(&cset->mg_node))
2131 list_add_tail(&cset->mg_node, &tset.src_csets);
2132 if (list_empty(&cset->mg_dst_cset->mg_node))
2133 list_move_tail(&cset->mg_dst_cset->mg_node,
2134 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002135 next:
Li Zefan081aa452013-03-13 09:17:09 +08002136 if (!threadgroup)
2137 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002138 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002139 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002140 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002141
Tejun Heo134d3372011-12-12 18:12:21 -08002142 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002143 if (list_empty(&tset.src_csets))
2144 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002145
Tejun Heo1958d2d2014-02-25 10:04:03 -05002146 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002147 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002148 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002149 ret = css->ss->can_attach(css, &tset);
2150 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002151 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002152 goto out_cancel_attach;
2153 }
2154 }
Ben Blum74a11662011-05-26 16:25:20 -07002155 }
2156
2157 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002158 * Now that we're guaranteed success, proceed to move all tasks to
2159 * the new cgroup. There are no failure cases after here, so this
2160 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002161 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002162 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002163 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2164 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2165 cgroup_task_migrate(cset->mg_src_cgrp, task,
2166 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002167 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002168 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002169
2170 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002171 * Migration is committed, all target tasks are now on dst_csets.
2172 * Nothing is sensitive to fork() after this point. Notify
2173 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002174 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002175 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002176
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002177 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002178 if (css->ss->attach)
2179 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002180
Tejun Heo9db8de32014-02-13 06:58:43 -05002181 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002182 goto out_release_tset;
2183
Ben Blum74a11662011-05-26 16:25:20 -07002184out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002185 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002186 if (css == failed_css)
2187 break;
2188 if (css->ss->cancel_attach)
2189 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002190 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002191out_release_tset:
2192 down_write(&css_set_rwsem);
2193 list_splice_init(&tset.dst_csets, &tset.src_csets);
2194 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002195 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002196 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002197 }
2198 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002199 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002200}
2201
Tejun Heo1958d2d2014-02-25 10:04:03 -05002202/**
2203 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2204 * @dst_cgrp: the cgroup to attach to
2205 * @leader: the task or the leader of the threadgroup to be attached
2206 * @threadgroup: attach the whole threadgroup?
2207 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002208 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002209 */
2210static int cgroup_attach_task(struct cgroup *dst_cgrp,
2211 struct task_struct *leader, bool threadgroup)
2212{
2213 LIST_HEAD(preloaded_csets);
2214 struct task_struct *task;
2215 int ret;
2216
2217 /* look up all src csets */
2218 down_read(&css_set_rwsem);
2219 rcu_read_lock();
2220 task = leader;
2221 do {
2222 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2223 &preloaded_csets);
2224 if (!threadgroup)
2225 break;
2226 } while_each_thread(leader, task);
2227 rcu_read_unlock();
2228 up_read(&css_set_rwsem);
2229
2230 /* prepare dst csets and commit */
2231 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2232 if (!ret)
2233 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2234
2235 cgroup_migrate_finish(&preloaded_csets);
2236 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002237}
2238
2239/*
2240 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002241 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002242 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002243 */
Tejun Heoacbef752014-05-13 12:16:22 -04002244static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2245 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002246{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002247 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002248 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002249 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002250 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002251 int ret;
2252
Tejun Heoacbef752014-05-13 12:16:22 -04002253 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2254 return -EINVAL;
2255
Tejun Heoe76ecae2014-05-13 12:19:23 -04002256 cgrp = cgroup_kn_lock_live(of->kn);
2257 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002258 return -ENODEV;
2259
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002260retry_find_task:
2261 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002262 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002263 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002264 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002265 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002266 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002267 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002268 }
Ben Blum74a11662011-05-26 16:25:20 -07002269 /*
2270 * even if we're attaching all tasks in the thread group, we
2271 * only need to check permissions on one of them.
2272 */
David Howellsc69e8d92008-11-14 10:39:19 +11002273 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002274 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2275 !uid_eq(cred->euid, tcred->uid) &&
2276 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002277 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002278 ret = -EACCES;
2279 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002280 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002281 } else
2282 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002283
2284 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002285 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002286
2287 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002288 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002289 * trapped in a cpuset, or RT worker may be born in a cgroup
2290 * with no rt_runtime allocated. Just say no.
2291 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002292 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002293 ret = -EINVAL;
2294 rcu_read_unlock();
2295 goto out_unlock_cgroup;
2296 }
2297
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002298 get_task_struct(tsk);
2299 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002300
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002301 threadgroup_lock(tsk);
2302 if (threadgroup) {
2303 if (!thread_group_leader(tsk)) {
2304 /*
2305 * a race with de_thread from another thread's exec()
2306 * may strip us of our leadership, if this happens,
2307 * there is no choice but to throw this task away and
2308 * try again; this is
2309 * "double-double-toil-and-trouble-check locking".
2310 */
2311 threadgroup_unlock(tsk);
2312 put_task_struct(tsk);
2313 goto retry_find_task;
2314 }
Li Zefan081aa452013-03-13 09:17:09 +08002315 }
2316
2317 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2318
Tejun Heocd3d0952011-12-12 18:12:21 -08002319 threadgroup_unlock(tsk);
2320
Paul Menagebbcb81d2007-10-18 23:39:32 -07002321 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002322out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002323 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002324 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002325}
2326
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002327/**
2328 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2329 * @from: attach to all cgroups of a given task
2330 * @tsk: the task to be attached
2331 */
2332int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2333{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002334 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002335 int retval = 0;
2336
Tejun Heo47cfcd02013-04-07 09:29:51 -07002337 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002338 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002339 struct cgroup *from_cgrp;
2340
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002341 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002342 continue;
2343
Tejun Heo96d365e2014-02-13 06:58:40 -05002344 down_read(&css_set_rwsem);
2345 from_cgrp = task_cgroup_from_root(from, root);
2346 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002347
Li Zefan6f4b7e62013-07-31 16:18:36 +08002348 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002349 if (retval)
2350 break;
2351 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002352 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002353
2354 return retval;
2355}
2356EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2357
Tejun Heoacbef752014-05-13 12:16:22 -04002358static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2359 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002360{
Tejun Heoacbef752014-05-13 12:16:22 -04002361 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002362}
2363
Tejun Heoacbef752014-05-13 12:16:22 -04002364static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2365 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002366{
Tejun Heoacbef752014-05-13 12:16:22 -04002367 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002368}
2369
Tejun Heo451af502014-05-13 12:16:21 -04002370static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2371 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002372{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002373 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002374
Tejun Heoe76ecae2014-05-13 12:19:23 -04002375 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2376
2377 cgrp = cgroup_kn_lock_live(of->kn);
2378 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002379 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002380 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002381 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2382 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002383 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002384 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002385 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002386}
2387
Tejun Heo2da8ca82013-12-05 12:28:04 -05002388static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002389{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002390 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002391
Tejun Heo46cfeb02014-05-13 12:11:00 -04002392 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002393 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002394 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002395 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002396 return 0;
2397}
2398
Tejun Heo2da8ca82013-12-05 12:28:04 -05002399static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002400{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002401 struct cgroup *cgrp = seq_css(seq)->cgroup;
2402
2403 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002404 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405}
2406
Tejun Heof8f22e52014-04-23 11:13:16 -04002407static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2408{
2409 struct cgroup_subsys *ss;
2410 bool printed = false;
2411 int ssid;
2412
2413 for_each_subsys(ss, ssid) {
2414 if (ss_mask & (1 << ssid)) {
2415 if (printed)
2416 seq_putc(seq, ' ');
2417 seq_printf(seq, "%s", ss->name);
2418 printed = true;
2419 }
2420 }
2421 if (printed)
2422 seq_putc(seq, '\n');
2423}
2424
2425/* show controllers which are currently attached to the default hierarchy */
2426static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2427{
2428 struct cgroup *cgrp = seq_css(seq)->cgroup;
2429
2430 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2431 return 0;
2432}
2433
2434/* show controllers which are enabled from the parent */
2435static int cgroup_controllers_show(struct seq_file *seq, void *v)
2436{
2437 struct cgroup *cgrp = seq_css(seq)->cgroup;
2438
Tejun Heod51f39b2014-05-16 13:22:48 -04002439 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->child_subsys_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002440 return 0;
2441}
2442
2443/* show controllers which are enabled for a given cgroup's children */
2444static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2445{
2446 struct cgroup *cgrp = seq_css(seq)->cgroup;
2447
2448 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2449 return 0;
2450}
2451
2452/**
2453 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2454 * @cgrp: root of the subtree to update csses for
2455 *
2456 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2457 * css associations need to be updated accordingly. This function looks up
2458 * all css_sets which are attached to the subtree, creates the matching
2459 * updated css_sets and migrates the tasks to the new ones.
2460 */
2461static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2462{
2463 LIST_HEAD(preloaded_csets);
2464 struct cgroup_subsys_state *css;
2465 struct css_set *src_cset;
2466 int ret;
2467
Tejun Heof8f22e52014-04-23 11:13:16 -04002468 lockdep_assert_held(&cgroup_mutex);
2469
2470 /* look up all csses currently attached to @cgrp's subtree */
2471 down_read(&css_set_rwsem);
2472 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2473 struct cgrp_cset_link *link;
2474
2475 /* self is not affected by child_subsys_mask change */
2476 if (css->cgroup == cgrp)
2477 continue;
2478
2479 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2480 cgroup_migrate_add_src(link->cset, cgrp,
2481 &preloaded_csets);
2482 }
2483 up_read(&css_set_rwsem);
2484
2485 /* NULL dst indicates self on default hierarchy */
2486 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2487 if (ret)
2488 goto out_finish;
2489
2490 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2491 struct task_struct *last_task = NULL, *task;
2492
2493 /* src_csets precede dst_csets, break on the first dst_cset */
2494 if (!src_cset->mg_src_cgrp)
2495 break;
2496
2497 /*
2498 * All tasks in src_cset need to be migrated to the
2499 * matching dst_cset. Empty it process by process. We
2500 * walk tasks but migrate processes. The leader might even
2501 * belong to a different cset but such src_cset would also
2502 * be among the target src_csets because the default
2503 * hierarchy enforces per-process membership.
2504 */
2505 while (true) {
2506 down_read(&css_set_rwsem);
2507 task = list_first_entry_or_null(&src_cset->tasks,
2508 struct task_struct, cg_list);
2509 if (task) {
2510 task = task->group_leader;
2511 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2512 get_task_struct(task);
2513 }
2514 up_read(&css_set_rwsem);
2515
2516 if (!task)
2517 break;
2518
2519 /* guard against possible infinite loop */
2520 if (WARN(last_task == task,
2521 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2522 goto out_finish;
2523 last_task = task;
2524
2525 threadgroup_lock(task);
2526 /* raced against de_thread() from another thread? */
2527 if (!thread_group_leader(task)) {
2528 threadgroup_unlock(task);
2529 put_task_struct(task);
2530 continue;
2531 }
2532
2533 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2534
2535 threadgroup_unlock(task);
2536 put_task_struct(task);
2537
2538 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2539 goto out_finish;
2540 }
2541 }
2542
2543out_finish:
2544 cgroup_migrate_finish(&preloaded_csets);
2545 return ret;
2546}
2547
2548/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002549static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2550 char *buf, size_t nbytes,
2551 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002552{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002553 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002554 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002555 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002556 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002557 int ssid, ret;
2558
2559 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002560 * Parse input - space separated list of subsystem names prefixed
2561 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002562 */
Tejun Heo451af502014-05-13 12:16:21 -04002563 buf = strstrip(buf);
2564 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002565 if (tok[0] == '\0')
2566 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002567 for_each_subsys(ss, ssid) {
2568 if (ss->disabled || strcmp(tok + 1, ss->name))
2569 continue;
2570
2571 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002572 enable |= 1 << ssid;
2573 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002574 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002575 disable |= 1 << ssid;
2576 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002577 } else {
2578 return -EINVAL;
2579 }
2580 break;
2581 }
2582 if (ssid == CGROUP_SUBSYS_COUNT)
2583 return -EINVAL;
2584 }
2585
Tejun Heoa9746d82014-05-13 12:19:22 -04002586 cgrp = cgroup_kn_lock_live(of->kn);
2587 if (!cgrp)
2588 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002589
2590 for_each_subsys(ss, ssid) {
2591 if (enable & (1 << ssid)) {
2592 if (cgrp->child_subsys_mask & (1 << ssid)) {
2593 enable &= ~(1 << ssid);
2594 continue;
2595 }
2596
2597 /*
2598 * Because css offlining is asynchronous, userland
2599 * might try to re-enable the same controller while
2600 * the previous instance is still around. In such
2601 * cases, wait till it's gone using offline_waitq.
2602 */
2603 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002604 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002605
2606 if (!cgroup_css(child, ss))
2607 continue;
2608
Tejun Heo0cee8b72014-05-13 12:10:59 -04002609 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002610 prepare_to_wait(&child->offline_waitq, &wait,
2611 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002612 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002613 schedule();
2614 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002615 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002616
Tejun Heoa9746d82014-05-13 12:19:22 -04002617 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002618 }
2619
2620 /* unavailable or not enabled on the parent? */
2621 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
Tejun Heod51f39b2014-05-16 13:22:48 -04002622 (cgroup_parent(cgrp) &&
2623 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ssid)))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002624 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002625 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002626 }
2627 } else if (disable & (1 << ssid)) {
2628 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2629 disable &= ~(1 << ssid);
2630 continue;
2631 }
2632
2633 /* a child has it enabled? */
2634 cgroup_for_each_live_child(child, cgrp) {
2635 if (child->child_subsys_mask & (1 << ssid)) {
2636 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002637 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002638 }
2639 }
2640 }
2641 }
2642
2643 if (!enable && !disable) {
2644 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002645 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002646 }
2647
2648 /*
2649 * Except for the root, child_subsys_mask must be zero for a cgroup
2650 * with tasks so that child cgroups don't compete against tasks.
2651 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002652 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002653 ret = -EBUSY;
2654 goto out_unlock;
2655 }
2656
2657 /*
2658 * Create csses for enables and update child_subsys_mask. This
2659 * changes cgroup_e_css() results which in turn makes the
2660 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2661 * subtree to the updated csses.
2662 */
2663 for_each_subsys(ss, ssid) {
2664 if (!(enable & (1 << ssid)))
2665 continue;
2666
2667 cgroup_for_each_live_child(child, cgrp) {
2668 ret = create_css(child, ss);
2669 if (ret)
2670 goto err_undo_css;
2671 }
2672 }
2673
2674 cgrp->child_subsys_mask |= enable;
2675 cgrp->child_subsys_mask &= ~disable;
2676
2677 ret = cgroup_update_dfl_csses(cgrp);
2678 if (ret)
2679 goto err_undo_css;
2680
2681 /* all tasks are now migrated away from the old csses, kill them */
2682 for_each_subsys(ss, ssid) {
2683 if (!(disable & (1 << ssid)))
2684 continue;
2685
2686 cgroup_for_each_live_child(child, cgrp)
2687 kill_css(cgroup_css(child, ss));
2688 }
2689
2690 kernfs_activate(cgrp->kn);
2691 ret = 0;
2692out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002693 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002694 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002695
2696err_undo_css:
2697 cgrp->child_subsys_mask &= ~enable;
2698 cgrp->child_subsys_mask |= disable;
2699
2700 for_each_subsys(ss, ssid) {
2701 if (!(enable & (1 << ssid)))
2702 continue;
2703
2704 cgroup_for_each_live_child(child, cgrp) {
2705 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2706 if (css)
2707 kill_css(css);
2708 }
2709 }
2710 goto out_unlock;
2711}
2712
Tejun Heo842b5972014-04-25 18:28:02 -04002713static int cgroup_populated_show(struct seq_file *seq, void *v)
2714{
2715 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2716 return 0;
2717}
2718
Tejun Heo2bd59d42014-02-11 11:52:49 -05002719static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2720 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002722 struct cgroup *cgrp = of->kn->parent->priv;
2723 struct cftype *cft = of->kn->priv;
2724 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002725 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726
Tejun Heob4168642014-05-13 12:16:21 -04002727 if (cft->write)
2728 return cft->write(of, buf, nbytes, off);
2729
Tejun Heo2bd59d42014-02-11 11:52:49 -05002730 /*
2731 * kernfs guarantees that a file isn't deleted with operations in
2732 * flight, which means that the matching css is and stays alive and
2733 * doesn't need to be pinned. The RCU locking is not necessary
2734 * either. It's just for the convenience of using cgroup_css().
2735 */
2736 rcu_read_lock();
2737 css = cgroup_css(cgrp, cft->ss);
2738 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739
Tejun Heo451af502014-05-13 12:16:21 -04002740 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002741 unsigned long long v;
2742 ret = kstrtoull(buf, 0, &v);
2743 if (!ret)
2744 ret = cft->write_u64(css, cft, v);
2745 } else if (cft->write_s64) {
2746 long long v;
2747 ret = kstrtoll(buf, 0, &v);
2748 if (!ret)
2749 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002750 } else {
2751 ret = -EINVAL;
2752 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002753
Tejun Heoa742c592013-12-05 12:28:03 -05002754 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755}
2756
Tejun Heo6612f052013-12-05 12:28:04 -05002757static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002758{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002759 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002760}
2761
2762static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2763{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002764 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002765}
2766
2767static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2768{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002769 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002770}
2771
2772static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2773{
Tejun Heo7da11272013-12-05 12:28:04 -05002774 struct cftype *cft = seq_cft(m);
2775 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002776
Tejun Heo2da8ca82013-12-05 12:28:04 -05002777 if (cft->seq_show)
2778 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002779
Tejun Heo896f5192013-12-05 12:28:04 -05002780 if (cft->read_u64)
2781 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2782 else if (cft->read_s64)
2783 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2784 else
2785 return -EINVAL;
2786 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002787}
2788
Tejun Heo2bd59d42014-02-11 11:52:49 -05002789static struct kernfs_ops cgroup_kf_single_ops = {
2790 .atomic_write_len = PAGE_SIZE,
2791 .write = cgroup_file_write,
2792 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002793};
2794
Tejun Heo2bd59d42014-02-11 11:52:49 -05002795static struct kernfs_ops cgroup_kf_ops = {
2796 .atomic_write_len = PAGE_SIZE,
2797 .write = cgroup_file_write,
2798 .seq_start = cgroup_seqfile_start,
2799 .seq_next = cgroup_seqfile_next,
2800 .seq_stop = cgroup_seqfile_stop,
2801 .seq_show = cgroup_seqfile_show,
2802};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002803
2804/*
2805 * cgroup_rename - Only allow simple rename of directories in place.
2806 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002807static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2808 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002809{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002810 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002811 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002812
Tejun Heo2bd59d42014-02-11 11:52:49 -05002813 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002814 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002815 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002816 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002817
Tejun Heo6db8e852013-06-14 11:18:22 -07002818 /*
2819 * This isn't a proper migration and its usefulness is very
2820 * limited. Disallow if sane_behavior.
2821 */
2822 if (cgroup_sane_behavior(cgrp))
2823 return -EPERM;
2824
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002825 /*
Tejun Heo8353da12014-05-13 12:19:23 -04002826 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002827 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04002828 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002829 */
2830 kernfs_break_active_protection(new_parent);
2831 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002832
Tejun Heo2bd59d42014-02-11 11:52:49 -05002833 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002834
Tejun Heo2bd59d42014-02-11 11:52:49 -05002835 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002836
Tejun Heo2bd59d42014-02-11 11:52:49 -05002837 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002838
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002839 kernfs_unbreak_active_protection(kn);
2840 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002841 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002842}
2843
Tejun Heo49957f82014-04-07 16:44:47 -04002844/* set uid and gid of cgroup dirs and files to that of the creator */
2845static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2846{
2847 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2848 .ia_uid = current_fsuid(),
2849 .ia_gid = current_fsgid(), };
2850
2851 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2852 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2853 return 0;
2854
2855 return kernfs_setattr(kn, &iattr);
2856}
2857
Tejun Heo2bb566c2013-08-08 20:11:23 -04002858static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002859{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002860 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002861 struct kernfs_node *kn;
2862 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002863 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002864
Tejun Heo2bd59d42014-02-11 11:52:49 -05002865#ifdef CONFIG_DEBUG_LOCK_ALLOC
2866 key = &cft->lockdep_key;
2867#endif
2868 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2869 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2870 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002871 if (IS_ERR(kn))
2872 return PTR_ERR(kn);
2873
2874 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002875 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002876 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002877 return ret;
2878 }
2879
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002880 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002881 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002882 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002883}
2884
Tejun Heob1f28d32013-06-28 16:24:10 -07002885/**
2886 * cgroup_addrm_files - add or remove files to a cgroup directory
2887 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002888 * @cfts: array of cftypes to be added
2889 * @is_add: whether to add or remove
2890 *
2891 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002892 * For removals, this function never fails. If addition fails, this
2893 * function doesn't remove files already added. The caller is responsible
2894 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002895 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002896static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2897 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002898{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002899 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002900 int ret;
2901
Tejun Heo01f64742014-05-13 12:19:23 -04002902 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002903
2904 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002905 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002906 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2907 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002908 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2909 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002910 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002911 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002912 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002913 continue;
2914
Li Zefan2739d3c2013-01-21 18:18:33 +08002915 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002916 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002917 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002918 pr_warn("%s: failed to add %s, err=%d\n",
2919 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002920 return ret;
2921 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002922 } else {
2923 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002924 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002925 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002926 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002927}
2928
Tejun Heo21a2d342014-02-12 09:29:49 -05002929static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930{
2931 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002932 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002933 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002934 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002935 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002936
Tejun Heo01f64742014-05-13 12:19:23 -04002937 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002938
Li Zefane8c82d22013-06-18 18:48:37 +08002939 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002940 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002941 struct cgroup *cgrp = css->cgroup;
2942
Li Zefane8c82d22013-06-18 18:48:37 +08002943 if (cgroup_is_dead(cgrp))
2944 continue;
2945
Tejun Heo21a2d342014-02-12 09:29:49 -05002946 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002947 if (ret)
2948 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002949 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002950
2951 if (is_add && !ret)
2952 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002953 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002954}
2955
Tejun Heo2da440a2014-02-11 11:52:48 -05002956static void cgroup_exit_cftypes(struct cftype *cfts)
2957{
2958 struct cftype *cft;
2959
Tejun Heo2bd59d42014-02-11 11:52:49 -05002960 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2961 /* free copy for custom atomic_write_len, see init_cftypes() */
2962 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2963 kfree(cft->kf_ops);
2964 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002965 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002966 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002967}
2968
Tejun Heo2bd59d42014-02-11 11:52:49 -05002969static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002970{
2971 struct cftype *cft;
2972
Tejun Heo2bd59d42014-02-11 11:52:49 -05002973 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2974 struct kernfs_ops *kf_ops;
2975
Tejun Heo0adb0702014-02-12 09:29:48 -05002976 WARN_ON(cft->ss || cft->kf_ops);
2977
Tejun Heo2bd59d42014-02-11 11:52:49 -05002978 if (cft->seq_start)
2979 kf_ops = &cgroup_kf_ops;
2980 else
2981 kf_ops = &cgroup_kf_single_ops;
2982
2983 /*
2984 * Ugh... if @cft wants a custom max_write_len, we need to
2985 * make a copy of kf_ops to set its atomic_write_len.
2986 */
2987 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2988 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2989 if (!kf_ops) {
2990 cgroup_exit_cftypes(cfts);
2991 return -ENOMEM;
2992 }
2993 kf_ops->atomic_write_len = cft->max_write_len;
2994 }
2995
2996 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002997 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002998 }
2999
3000 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003001}
3002
Tejun Heo21a2d342014-02-12 09:29:49 -05003003static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3004{
Tejun Heo01f64742014-05-13 12:19:23 -04003005 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003006
3007 if (!cfts || !cfts[0].ss)
3008 return -ENOENT;
3009
3010 list_del(&cfts->node);
3011 cgroup_apply_cftypes(cfts, false);
3012 cgroup_exit_cftypes(cfts);
3013 return 0;
3014}
3015
Tejun Heo8e3f6542012-04-01 12:09:55 -07003016/**
Tejun Heo80b13582014-02-12 09:29:48 -05003017 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3018 * @cfts: zero-length name terminated array of cftypes
3019 *
3020 * Unregister @cfts. Files described by @cfts are removed from all
3021 * existing cgroups and all future cgroups won't have them either. This
3022 * function can be called anytime whether @cfts' subsys is attached or not.
3023 *
3024 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3025 * registered.
3026 */
3027int cgroup_rm_cftypes(struct cftype *cfts)
3028{
Tejun Heo21a2d342014-02-12 09:29:49 -05003029 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003030
Tejun Heo01f64742014-05-13 12:19:23 -04003031 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003032 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003033 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003034 return ret;
3035}
3036
3037/**
3038 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3039 * @ss: target cgroup subsystem
3040 * @cfts: zero-length name terminated array of cftypes
3041 *
3042 * Register @cfts to @ss. Files described by @cfts are created for all
3043 * existing cgroups to which @ss is attached and all future cgroups will
3044 * have them too. This function can be called anytime whether @ss is
3045 * attached or not.
3046 *
3047 * Returns 0 on successful registration, -errno on failure. Note that this
3048 * function currently returns 0 as long as @cfts registration is successful
3049 * even if some file creation attempts on existing cgroups fail.
3050 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003051int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003052{
Tejun Heo9ccece82013-06-28 16:24:11 -07003053 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003054
Li Zefandc5736e2014-02-17 10:41:50 +08003055 if (!cfts || cfts[0].name[0] == '\0')
3056 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003057
Tejun Heo2bd59d42014-02-11 11:52:49 -05003058 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003059 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003060 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003061
Tejun Heo01f64742014-05-13 12:19:23 -04003062 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003063
Tejun Heo0adb0702014-02-12 09:29:48 -05003064 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003065 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003066 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003067 cgroup_rm_cftypes_locked(cfts);
3068
Tejun Heo01f64742014-05-13 12:19:23 -04003069 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003070 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003071}
Tejun Heo79578622012-04-01 12:09:56 -07003072
3073/**
Li Zefana043e3b2008-02-23 15:24:09 -08003074 * cgroup_task_count - count the number of tasks in a cgroup.
3075 * @cgrp: the cgroup in question
3076 *
3077 * Return the number of tasks in the cgroup.
3078 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003079static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003080{
3081 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003082 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003083
Tejun Heo96d365e2014-02-13 06:58:40 -05003084 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003085 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3086 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003087 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003088 return count;
3089}
3090
Tejun Heo574bd9f2012-11-09 09:12:29 -08003091/**
Tejun Heo492eb212013-08-08 20:11:25 -04003092 * css_next_child - find the next child of a given css
3093 * @pos_css: the current position (%NULL to initiate traversal)
3094 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003095 *
Tejun Heo492eb212013-08-08 20:11:25 -04003096 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003097 * under either cgroup_mutex or RCU read lock. The only requirement is
3098 * that @parent_css and @pos_css are accessible. The next sibling is
3099 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003100 */
Tejun Heo492eb212013-08-08 20:11:25 -04003101struct cgroup_subsys_state *
3102css_next_child(struct cgroup_subsys_state *pos_css,
3103 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003104{
Tejun Heo492eb212013-08-08 20:11:25 -04003105 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3106 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003107 struct cgroup *next;
3108
Tejun Heo8353da12014-05-13 12:19:23 -04003109 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003110
3111 /*
3112 * @pos could already have been removed. Once a cgroup is removed,
3113 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003114 * changes. As CGRP_DEAD assertion is serialized and happens
3115 * before the cgroup is taken off the ->sibling list, if we see it
3116 * unasserted, it's guaranteed that the next sibling hasn't
3117 * finished its grace period even if it's already removed, and thus
3118 * safe to dereference from this RCU critical section. If
3119 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3120 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003121 *
3122 * If @pos is dead, its next pointer can't be dereferenced;
3123 * however, as each cgroup is given a monotonically increasing
3124 * unique serial number and always appended to the sibling list,
3125 * the next one can be found by walking the parent's children until
3126 * we see a cgroup with higher serial number than @pos's. While
3127 * this path can be slower, it's taken only when either the current
3128 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003129 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003130 if (!pos) {
Tejun Heod5c419b2014-05-16 13:22:48 -04003131 next = list_entry_rcu(cgrp->self.children.next, struct cgroup, self.sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003132 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heod5c419b2014-05-16 13:22:48 -04003133 next = list_entry_rcu(pos->self.sibling.next, struct cgroup, self.sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003134 } else {
Tejun Heod5c419b2014-05-16 13:22:48 -04003135 list_for_each_entry_rcu(next, &cgrp->self.children, self.sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003136 if (next->serial_nr > pos->serial_nr)
3137 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003138 }
3139
Tejun Heo3b281af2014-04-23 11:13:15 -04003140 /*
3141 * @next, if not pointing to the head, can be dereferenced and is
3142 * the next sibling; however, it might have @ss disabled. If so,
3143 * fast-forward to the next enabled one.
3144 */
Tejun Heod5c419b2014-05-16 13:22:48 -04003145 while (&next->self.sibling != &cgrp->self.children) {
Tejun Heo3b281af2014-04-23 11:13:15 -04003146 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003147
Tejun Heo3b281af2014-04-23 11:13:15 -04003148 if (next_css)
3149 return next_css;
Tejun Heod5c419b2014-05-16 13:22:48 -04003150 next = list_entry_rcu(next->self.sibling.next, struct cgroup, self.sibling);
Tejun Heo3b281af2014-04-23 11:13:15 -04003151 }
3152 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003153}
Tejun Heo53fa5262013-05-24 10:55:38 +09003154
3155/**
Tejun Heo492eb212013-08-08 20:11:25 -04003156 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003157 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003158 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003159 *
Tejun Heo492eb212013-08-08 20:11:25 -04003160 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003161 * to visit for pre-order traversal of @root's descendants. @root is
3162 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003163 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003164 * While this function requires cgroup_mutex or RCU read locking, it
3165 * doesn't require the whole traversal to be contained in a single critical
3166 * section. This function will return the correct next descendant as long
3167 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168 */
Tejun Heo492eb212013-08-08 20:11:25 -04003169struct cgroup_subsys_state *
3170css_next_descendant_pre(struct cgroup_subsys_state *pos,
3171 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172{
Tejun Heo492eb212013-08-08 20:11:25 -04003173 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003174
Tejun Heo8353da12014-05-13 12:19:23 -04003175 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003176
Tejun Heobd8815a2013-08-08 20:11:27 -04003177 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003178 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003179 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003180
3181 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003182 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003183 if (next)
3184 return next;
3185
3186 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003187 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003188 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003189 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003190 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003191 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003192 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193
3194 return NULL;
3195}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003196
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003197/**
Tejun Heo492eb212013-08-08 20:11:25 -04003198 * css_rightmost_descendant - return the rightmost descendant of a css
3199 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003200 *
Tejun Heo492eb212013-08-08 20:11:25 -04003201 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3202 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003203 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003204 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003205 * While this function requires cgroup_mutex or RCU read locking, it
3206 * doesn't require the whole traversal to be contained in a single critical
3207 * section. This function will return the correct rightmost descendant as
3208 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003209 */
Tejun Heo492eb212013-08-08 20:11:25 -04003210struct cgroup_subsys_state *
3211css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003212{
Tejun Heo492eb212013-08-08 20:11:25 -04003213 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003214
Tejun Heo8353da12014-05-13 12:19:23 -04003215 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003216
3217 do {
3218 last = pos;
3219 /* ->prev isn't RCU safe, walk ->next till the end */
3220 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003221 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003222 pos = tmp;
3223 } while (pos);
3224
3225 return last;
3226}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003227
Tejun Heo492eb212013-08-08 20:11:25 -04003228static struct cgroup_subsys_state *
3229css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003230{
Tejun Heo492eb212013-08-08 20:11:25 -04003231 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003232
3233 do {
3234 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003235 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003236 } while (pos);
3237
3238 return last;
3239}
3240
3241/**
Tejun Heo492eb212013-08-08 20:11:25 -04003242 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003243 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003244 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003245 *
Tejun Heo492eb212013-08-08 20:11:25 -04003246 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003247 * to visit for post-order traversal of @root's descendants. @root is
3248 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003249 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003250 * While this function requires cgroup_mutex or RCU read locking, it
3251 * doesn't require the whole traversal to be contained in a single critical
3252 * section. This function will return the correct next descendant as long
3253 * as both @pos and @cgroup are accessible and @pos is a descendant of
3254 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003255 */
Tejun Heo492eb212013-08-08 20:11:25 -04003256struct cgroup_subsys_state *
3257css_next_descendant_post(struct cgroup_subsys_state *pos,
3258 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003259{
Tejun Heo492eb212013-08-08 20:11:25 -04003260 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003261
Tejun Heo8353da12014-05-13 12:19:23 -04003262 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003263
Tejun Heo58b79a92013-09-06 15:31:08 -04003264 /* if first iteration, visit leftmost descendant which may be @root */
3265 if (!pos)
3266 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003267
Tejun Heobd8815a2013-08-08 20:11:27 -04003268 /* if we visited @root, we're done */
3269 if (pos == root)
3270 return NULL;
3271
Tejun Heo574bd9f2012-11-09 09:12:29 -08003272 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003273 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003274 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003275 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003276
3277 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003278 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003279}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003280
Tejun Heocbc125e2014-05-14 09:15:01 -04003281static bool cgroup_has_live_children(struct cgroup *cgrp)
3282{
3283 struct cgroup *child;
3284
3285 rcu_read_lock();
Tejun Heod5c419b2014-05-16 13:22:48 -04003286 list_for_each_entry_rcu(child, &cgrp->self.children, self.sibling) {
Tejun Heocbc125e2014-05-14 09:15:01 -04003287 if (!cgroup_is_dead(child)) {
3288 rcu_read_unlock();
3289 return true;
3290 }
3291 }
3292 rcu_read_unlock();
3293 return false;
3294}
3295
Tejun Heo0942eee2013-08-08 20:11:26 -04003296/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003297 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003298 * @it: the iterator to advance
3299 *
3300 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003301 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003302static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003303{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003304 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003305 struct cgrp_cset_link *link;
3306 struct css_set *cset;
3307
3308 /* Advance to the next non-empty css_set */
3309 do {
3310 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003311 if (l == it->cset_head) {
3312 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003313 return;
3314 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003315
3316 if (it->ss) {
3317 cset = container_of(l, struct css_set,
3318 e_cset_node[it->ss->id]);
3319 } else {
3320 link = list_entry(l, struct cgrp_cset_link, cset_link);
3321 cset = link->cset;
3322 }
Tejun Heoc7561122014-02-25 10:04:01 -05003323 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3324
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003325 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003326
3327 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003328 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003329 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003330 it->task_pos = cset->mg_tasks.next;
3331
3332 it->tasks_head = &cset->tasks;
3333 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003334}
3335
Tejun Heo0942eee2013-08-08 20:11:26 -04003336/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003337 * css_task_iter_start - initiate task iteration
3338 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003339 * @it: the task iterator to use
3340 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003341 * Initiate iteration through the tasks of @css. The caller can call
3342 * css_task_iter_next() to walk through the tasks until the function
3343 * returns NULL. On completion of iteration, css_task_iter_end() must be
3344 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003345 *
3346 * Note that this function acquires a lock which is released when the
3347 * iteration finishes. The caller can't sleep while iteration is in
3348 * progress.
3349 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003350void css_task_iter_start(struct cgroup_subsys_state *css,
3351 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003352 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003353{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003354 /* no one should try to iterate before mounting cgroups */
3355 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003356
Tejun Heo96d365e2014-02-13 06:58:40 -05003357 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003358
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003359 it->ss = css->ss;
3360
3361 if (it->ss)
3362 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3363 else
3364 it->cset_pos = &css->cgroup->cset_links;
3365
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003366 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003367
Tejun Heo72ec7022013-08-08 20:11:26 -04003368 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003369}
3370
Tejun Heo0942eee2013-08-08 20:11:26 -04003371/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003372 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003373 * @it: the task iterator being iterated
3374 *
3375 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003376 * initialized via css_task_iter_start(). Returns NULL when the iteration
3377 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003378 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003379struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003380{
3381 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003382 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003383
3384 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003385 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003386 return NULL;
3387 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003388
3389 /*
3390 * Advance iterator to find next entry. cset->tasks is consumed
3391 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3392 * next cset.
3393 */
Paul Menage817929e2007-10-18 23:39:36 -07003394 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003395
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003396 if (l == it->tasks_head)
3397 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003398
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003399 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003400 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003401 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003402 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003403
Paul Menage817929e2007-10-18 23:39:36 -07003404 return res;
3405}
3406
Tejun Heo0942eee2013-08-08 20:11:26 -04003407/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003408 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003409 * @it: the task iterator to finish
3410 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003411 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003412 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003413void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003414 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003415{
Tejun Heo96d365e2014-02-13 06:58:40 -05003416 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003417}
3418
3419/**
3420 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3421 * @to: cgroup to which the tasks will be moved
3422 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003423 *
3424 * Locking rules between cgroup_post_fork() and the migration path
3425 * guarantee that, if a task is forking while being migrated, the new child
3426 * is guaranteed to be either visible in the source cgroup after the
3427 * parent's migration is complete or put into the target cgroup. No task
3428 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003429 */
3430int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3431{
Tejun Heo952aaa12014-02-25 10:04:03 -05003432 LIST_HEAD(preloaded_csets);
3433 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003434 struct css_task_iter it;
3435 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003436 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003437
Tejun Heo952aaa12014-02-25 10:04:03 -05003438 mutex_lock(&cgroup_mutex);
3439
3440 /* all tasks in @from are being moved, all csets are source */
3441 down_read(&css_set_rwsem);
3442 list_for_each_entry(link, &from->cset_links, cset_link)
3443 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3444 up_read(&css_set_rwsem);
3445
3446 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3447 if (ret)
3448 goto out_err;
3449
3450 /*
3451 * Migrate tasks one-by-one until @form is empty. This fails iff
3452 * ->can_attach() fails.
3453 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003454 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003455 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003456 task = css_task_iter_next(&it);
3457 if (task)
3458 get_task_struct(task);
3459 css_task_iter_end(&it);
3460
3461 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003462 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003463 put_task_struct(task);
3464 }
3465 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003466out_err:
3467 cgroup_migrate_finish(&preloaded_csets);
3468 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003469 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003470}
3471
Paul Menage817929e2007-10-18 23:39:36 -07003472/*
Ben Blum102a7752009-09-23 15:56:26 -07003473 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003474 *
3475 * Reading this file can return large amounts of data if a cgroup has
3476 * *lots* of attached tasks. So it may need several calls to read(),
3477 * but we cannot guarantee that the information we produce is correct
3478 * unless we produce it entirely atomically.
3479 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003480 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003481
Li Zefan24528252012-01-20 11:58:43 +08003482/* which pidlist file are we talking about? */
3483enum cgroup_filetype {
3484 CGROUP_FILE_PROCS,
3485 CGROUP_FILE_TASKS,
3486};
3487
3488/*
3489 * A pidlist is a list of pids that virtually represents the contents of one
3490 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3491 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3492 * to the cgroup.
3493 */
3494struct cgroup_pidlist {
3495 /*
3496 * used to find which pidlist is wanted. doesn't change as long as
3497 * this particular list stays in the list.
3498 */
3499 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3500 /* array of xids */
3501 pid_t *list;
3502 /* how many elements the above list has */
3503 int length;
Li Zefan24528252012-01-20 11:58:43 +08003504 /* each of these stored in a list by its cgroup */
3505 struct list_head links;
3506 /* pointer to the cgroup we belong to, for list removal purposes */
3507 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003508 /* for delayed destruction */
3509 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003510};
3511
Paul Menagebbcb81d2007-10-18 23:39:32 -07003512/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003513 * The following two functions "fix" the issue where there are more pids
3514 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3515 * TODO: replace with a kernel-wide solution to this problem
3516 */
3517#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3518static void *pidlist_allocate(int count)
3519{
3520 if (PIDLIST_TOO_LARGE(count))
3521 return vmalloc(count * sizeof(pid_t));
3522 else
3523 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3524}
Tejun Heob1a21362013-11-29 10:42:58 -05003525
Ben Blumd1d9fd32009-09-23 15:56:28 -07003526static void pidlist_free(void *p)
3527{
3528 if (is_vmalloc_addr(p))
3529 vfree(p);
3530 else
3531 kfree(p);
3532}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003533
3534/*
Tejun Heob1a21362013-11-29 10:42:58 -05003535 * Used to destroy all pidlists lingering waiting for destroy timer. None
3536 * should be left afterwards.
3537 */
3538static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3539{
3540 struct cgroup_pidlist *l, *tmp_l;
3541
3542 mutex_lock(&cgrp->pidlist_mutex);
3543 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3544 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3545 mutex_unlock(&cgrp->pidlist_mutex);
3546
3547 flush_workqueue(cgroup_pidlist_destroy_wq);
3548 BUG_ON(!list_empty(&cgrp->pidlists));
3549}
3550
3551static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3552{
3553 struct delayed_work *dwork = to_delayed_work(work);
3554 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3555 destroy_dwork);
3556 struct cgroup_pidlist *tofree = NULL;
3557
3558 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003559
3560 /*
Tejun Heo04502362013-11-29 10:42:59 -05003561 * Destroy iff we didn't get queued again. The state won't change
3562 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003563 */
Tejun Heo04502362013-11-29 10:42:59 -05003564 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003565 list_del(&l->links);
3566 pidlist_free(l->list);
3567 put_pid_ns(l->key.ns);
3568 tofree = l;
3569 }
3570
Tejun Heob1a21362013-11-29 10:42:58 -05003571 mutex_unlock(&l->owner->pidlist_mutex);
3572 kfree(tofree);
3573}
3574
3575/*
Ben Blum102a7752009-09-23 15:56:26 -07003576 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003577 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003578 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003579static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003580{
Ben Blum102a7752009-09-23 15:56:26 -07003581 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003582
3583 /*
3584 * we presume the 0th element is unique, so i starts at 1. trivial
3585 * edge cases first; no work needs to be done for either
3586 */
3587 if (length == 0 || length == 1)
3588 return length;
3589 /* src and dest walk down the list; dest counts unique elements */
3590 for (src = 1; src < length; src++) {
3591 /* find next unique element */
3592 while (list[src] == list[src-1]) {
3593 src++;
3594 if (src == length)
3595 goto after;
3596 }
3597 /* dest always points to where the next unique element goes */
3598 list[dest] = list[src];
3599 dest++;
3600 }
3601after:
Ben Blum102a7752009-09-23 15:56:26 -07003602 return dest;
3603}
3604
Tejun Heoafb2bc12013-11-29 10:42:59 -05003605/*
3606 * The two pid files - task and cgroup.procs - guaranteed that the result
3607 * is sorted, which forced this whole pidlist fiasco. As pid order is
3608 * different per namespace, each namespace needs differently sorted list,
3609 * making it impossible to use, for example, single rbtree of member tasks
3610 * sorted by task pointer. As pidlists can be fairly large, allocating one
3611 * per open file is dangerous, so cgroup had to implement shared pool of
3612 * pidlists keyed by cgroup and namespace.
3613 *
3614 * All this extra complexity was caused by the original implementation
3615 * committing to an entirely unnecessary property. In the long term, we
3616 * want to do away with it. Explicitly scramble sort order if
3617 * sane_behavior so that no such expectation exists in the new interface.
3618 *
3619 * Scrambling is done by swapping every two consecutive bits, which is
3620 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3621 */
3622static pid_t pid_fry(pid_t pid)
3623{
3624 unsigned a = pid & 0x55555555;
3625 unsigned b = pid & 0xAAAAAAAA;
3626
3627 return (a << 1) | (b >> 1);
3628}
3629
3630static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3631{
3632 if (cgroup_sane_behavior(cgrp))
3633 return pid_fry(pid);
3634 else
3635 return pid;
3636}
3637
Ben Blum102a7752009-09-23 15:56:26 -07003638static int cmppid(const void *a, const void *b)
3639{
3640 return *(pid_t *)a - *(pid_t *)b;
3641}
3642
Tejun Heoafb2bc12013-11-29 10:42:59 -05003643static int fried_cmppid(const void *a, const void *b)
3644{
3645 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3646}
3647
Ben Blum72a8cb32009-09-23 15:56:27 -07003648static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3649 enum cgroup_filetype type)
3650{
3651 struct cgroup_pidlist *l;
3652 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003653 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003654
Tejun Heoe6b81712013-11-29 10:42:59 -05003655 lockdep_assert_held(&cgrp->pidlist_mutex);
3656
3657 list_for_each_entry(l, &cgrp->pidlists, links)
3658 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003659 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003660 return NULL;
3661}
3662
Ben Blum72a8cb32009-09-23 15:56:27 -07003663/*
3664 * find the appropriate pidlist for our purpose (given procs vs tasks)
3665 * returns with the lock on that pidlist already held, and takes care
3666 * of the use count, or returns NULL with no locks held if we're out of
3667 * memory.
3668 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003669static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3670 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003671{
3672 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003673
Tejun Heoe6b81712013-11-29 10:42:59 -05003674 lockdep_assert_held(&cgrp->pidlist_mutex);
3675
3676 l = cgroup_pidlist_find(cgrp, type);
3677 if (l)
3678 return l;
3679
Ben Blum72a8cb32009-09-23 15:56:27 -07003680 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003681 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003682 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003683 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003684
Tejun Heob1a21362013-11-29 10:42:58 -05003685 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003686 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003687 /* don't need task_nsproxy() if we're looking at ourself */
3688 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003689 l->owner = cgrp;
3690 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003691 return l;
3692}
3693
3694/*
Ben Blum102a7752009-09-23 15:56:26 -07003695 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3696 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003697static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3698 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003699{
3700 pid_t *array;
3701 int length;
3702 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003703 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003704 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003705 struct cgroup_pidlist *l;
3706
Tejun Heo4bac00d2013-11-29 10:42:59 -05003707 lockdep_assert_held(&cgrp->pidlist_mutex);
3708
Ben Blum102a7752009-09-23 15:56:26 -07003709 /*
3710 * If cgroup gets more users after we read count, we won't have
3711 * enough space - tough. This race is indistinguishable to the
3712 * caller from the case that the additional cgroup users didn't
3713 * show up until sometime later on.
3714 */
3715 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003716 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003717 if (!array)
3718 return -ENOMEM;
3719 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003720 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003721 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003722 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003723 break;
Ben Blum102a7752009-09-23 15:56:26 -07003724 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003725 if (type == CGROUP_FILE_PROCS)
3726 pid = task_tgid_vnr(tsk);
3727 else
3728 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003729 if (pid > 0) /* make sure to only use valid results */
3730 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003731 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003732 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003733 length = n;
3734 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003735 if (cgroup_sane_behavior(cgrp))
3736 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3737 else
3738 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003739 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003740 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003741
Tejun Heoe6b81712013-11-29 10:42:59 -05003742 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003743 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003744 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003745 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003746 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003747 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003748
3749 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003750 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003751 l->list = array;
3752 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003753 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003754 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003755}
3756
Balbir Singh846c7bb2007-10-18 23:39:44 -07003757/**
Li Zefana043e3b2008-02-23 15:24:09 -08003758 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003759 * @stats: cgroupstats to fill information into
3760 * @dentry: A dentry entry belonging to the cgroup for which stats have
3761 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003762 *
3763 * Build and fill cgroupstats so that taskstats can export it to user
3764 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003765 */
3766int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3767{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003768 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003769 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003770 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003771 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003772
Tejun Heo2bd59d42014-02-11 11:52:49 -05003773 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3774 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3775 kernfs_type(kn) != KERNFS_DIR)
3776 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003777
Li Zefanbad34662014-02-14 16:54:28 +08003778 mutex_lock(&cgroup_mutex);
3779
Tejun Heo2bd59d42014-02-11 11:52:49 -05003780 /*
3781 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003782 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003783 * @kn->priv is RCU safe. Let's do the RCU dancing.
3784 */
3785 rcu_read_lock();
3786 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003787 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003788 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003789 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003790 return -ENOENT;
3791 }
Li Zefanbad34662014-02-14 16:54:28 +08003792 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003793
Tejun Heo9d800df2014-05-14 09:15:00 -04003794 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003795 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003796 switch (tsk->state) {
3797 case TASK_RUNNING:
3798 stats->nr_running++;
3799 break;
3800 case TASK_INTERRUPTIBLE:
3801 stats->nr_sleeping++;
3802 break;
3803 case TASK_UNINTERRUPTIBLE:
3804 stats->nr_uninterruptible++;
3805 break;
3806 case TASK_STOPPED:
3807 stats->nr_stopped++;
3808 break;
3809 default:
3810 if (delayacct_is_task_waiting_on_io(tsk))
3811 stats->nr_io_wait++;
3812 break;
3813 }
3814 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003815 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003816
Li Zefanbad34662014-02-14 16:54:28 +08003817 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003818 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003819}
3820
Paul Menage8f3ff202009-09-23 15:56:25 -07003821
Paul Menagecc31edc2008-10-18 20:28:04 -07003822/*
Ben Blum102a7752009-09-23 15:56:26 -07003823 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003824 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003825 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003826 */
3827
Ben Blum102a7752009-09-23 15:56:26 -07003828static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003829{
3830 /*
3831 * Initially we receive a position value that corresponds to
3832 * one more than the last pid shown (or 0 on the first call or
3833 * after a seek to the start). Use a binary-search to find the
3834 * next pid to display, if any
3835 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003836 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003837 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003838 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003839 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003840 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003841 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003842
Tejun Heo4bac00d2013-11-29 10:42:59 -05003843 mutex_lock(&cgrp->pidlist_mutex);
3844
3845 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003846 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003847 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003848 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003849 * could already have been destroyed.
3850 */
Tejun Heo5d224442013-12-05 12:28:04 -05003851 if (of->priv)
3852 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003853
3854 /*
3855 * Either this is the first start() after open or the matching
3856 * pidlist has been destroyed inbetween. Create a new one.
3857 */
Tejun Heo5d224442013-12-05 12:28:04 -05003858 if (!of->priv) {
3859 ret = pidlist_array_load(cgrp, type,
3860 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003861 if (ret)
3862 return ERR_PTR(ret);
3863 }
Tejun Heo5d224442013-12-05 12:28:04 -05003864 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003865
Paul Menagecc31edc2008-10-18 20:28:04 -07003866 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003867 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003868
Paul Menagecc31edc2008-10-18 20:28:04 -07003869 while (index < end) {
3870 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003871 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003872 index = mid;
3873 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003874 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003875 index = mid + 1;
3876 else
3877 end = mid;
3878 }
3879 }
3880 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003881 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003882 return NULL;
3883 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003884 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003885 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003886 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003887}
3888
Ben Blum102a7752009-09-23 15:56:26 -07003889static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003890{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003891 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003892 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003893
Tejun Heo5d224442013-12-05 12:28:04 -05003894 if (l)
3895 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003896 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003897 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003898}
3899
Ben Blum102a7752009-09-23 15:56:26 -07003900static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003901{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003902 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003903 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003904 pid_t *p = v;
3905 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003906 /*
3907 * Advance to the next pid in the array. If this goes off the
3908 * end, we're done
3909 */
3910 p++;
3911 if (p >= end) {
3912 return NULL;
3913 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003914 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003915 return p;
3916 }
3917}
3918
Ben Blum102a7752009-09-23 15:56:26 -07003919static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003920{
3921 return seq_printf(s, "%d\n", *(int *)v);
3922}
3923
Tejun Heo182446d2013-08-08 20:11:24 -04003924static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3925 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003926{
Tejun Heo182446d2013-08-08 20:11:24 -04003927 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003928}
3929
Tejun Heo182446d2013-08-08 20:11:24 -04003930static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3931 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003932{
Tejun Heo182446d2013-08-08 20:11:24 -04003933 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003934 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003935 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003936 else
Tejun Heo182446d2013-08-08 20:11:24 -04003937 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003938 return 0;
3939}
3940
Tejun Heo182446d2013-08-08 20:11:24 -04003941static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3942 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003943{
Tejun Heo182446d2013-08-08 20:11:24 -04003944 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003945}
3946
Tejun Heo182446d2013-08-08 20:11:24 -04003947static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3948 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003949{
3950 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003951 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003952 else
Tejun Heo182446d2013-08-08 20:11:24 -04003953 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003954 return 0;
3955}
3956
Tejun Heod5c56ce2013-06-03 19:14:34 -07003957static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003958 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003959 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003960 .seq_start = cgroup_pidlist_start,
3961 .seq_next = cgroup_pidlist_next,
3962 .seq_stop = cgroup_pidlist_stop,
3963 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003964 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003965 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003966 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003967 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003968 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003969 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003970 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003971 .read_u64 = cgroup_clone_children_read,
3972 .write_u64 = cgroup_clone_children_write,
3973 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003974 {
Tejun Heo873fe092013-04-14 20:15:26 -07003975 .name = "cgroup.sane_behavior",
3976 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003977 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003978 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003979 {
3980 .name = "cgroup.controllers",
3981 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3982 .seq_show = cgroup_root_controllers_show,
3983 },
3984 {
3985 .name = "cgroup.controllers",
3986 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3987 .seq_show = cgroup_controllers_show,
3988 },
3989 {
3990 .name = "cgroup.subtree_control",
3991 .flags = CFTYPE_ONLY_ON_DFL,
3992 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04003993 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04003994 },
Tejun Heo842b5972014-04-25 18:28:02 -04003995 {
3996 .name = "cgroup.populated",
3997 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3998 .seq_show = cgroup_populated_show,
3999 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004000
4001 /*
4002 * Historical crazy stuff. These don't have "cgroup." prefix and
4003 * don't exist if sane_behavior. If you're depending on these, be
4004 * prepared to be burned.
4005 */
4006 {
4007 .name = "tasks",
4008 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004009 .seq_start = cgroup_pidlist_start,
4010 .seq_next = cgroup_pidlist_next,
4011 .seq_stop = cgroup_pidlist_stop,
4012 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004013 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004014 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004015 .mode = S_IRUGO | S_IWUSR,
4016 },
4017 {
4018 .name = "notify_on_release",
4019 .flags = CFTYPE_INSANE,
4020 .read_u64 = cgroup_read_notify_on_release,
4021 .write_u64 = cgroup_write_notify_on_release,
4022 },
Tejun Heo873fe092013-04-14 20:15:26 -07004023 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004024 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004025 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004026 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004027 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004028 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004029 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004030 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004031};
4032
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004033/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004034 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004035 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004036 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004037 *
4038 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004039 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004040static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004043 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004044
Tejun Heo8e3f6542012-04-01 12:09:55 -07004045 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004046 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004047 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004048
Tejun Heo69dfa002014-05-04 15:09:13 -04004049 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004050 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004051
Tejun Heo0adb0702014-02-12 09:29:48 -05004052 list_for_each_entry(cfts, &ss->cfts, node) {
4053 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004054 if (ret < 0)
4055 goto err;
4056 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004058 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004059err:
4060 cgroup_clear_dir(cgrp, subsys_mask);
4061 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062}
4063
Tejun Heo0c21ead2013-08-13 20:22:51 -04004064/*
4065 * css destruction is four-stage process.
4066 *
4067 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4068 * Implemented in kill_css().
4069 *
4070 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004071 * and thus css_tryget_online() is guaranteed to fail, the css can be
4072 * offlined by invoking offline_css(). After offlining, the base ref is
4073 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004074 *
4075 * 3. When the percpu_ref reaches zero, the only possible remaining
4076 * accessors are inside RCU read sections. css_release() schedules the
4077 * RCU callback.
4078 *
4079 * 4. After the grace period, the css can be freed. Implemented in
4080 * css_free_work_fn().
4081 *
4082 * It is actually hairier because both step 2 and 4 require process context
4083 * and thus involve punting to css->destroy_work adding two additional
4084 * steps to the already complex sequence.
4085 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004086static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004087{
4088 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004089 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004090 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004091
Tejun Heo9d755d32014-05-14 09:15:02 -04004092 if (css->ss) {
4093 /* css free path */
4094 if (css->parent)
4095 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004096
Tejun Heo9d755d32014-05-14 09:15:02 -04004097 css->ss->css_free(css);
4098 cgroup_put(cgrp);
4099 } else {
4100 /* cgroup free path */
4101 atomic_dec(&cgrp->root->nr_cgrps);
4102 cgroup_pidlist_destroy_all(cgrp);
4103
Tejun Heod51f39b2014-05-16 13:22:48 -04004104 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004105 /*
4106 * We get a ref to the parent, and put the ref when
4107 * this cgroup is being freed, so it's guaranteed
4108 * that the parent won't be destroyed before its
4109 * children.
4110 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004111 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004112 kernfs_put(cgrp->kn);
4113 kfree(cgrp);
4114 } else {
4115 /*
4116 * This is root cgroup's refcnt reaching zero,
4117 * which indicates that the root should be
4118 * released.
4119 */
4120 cgroup_destroy_root(cgrp->root);
4121 }
4122 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004123}
4124
4125static void css_free_rcu_fn(struct rcu_head *rcu_head)
4126{
4127 struct cgroup_subsys_state *css =
4128 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4129
Tejun Heo0c21ead2013-08-13 20:22:51 -04004130 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004131 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004132}
4133
Tejun Heo25e15d82014-05-14 09:15:02 -04004134static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004135{
4136 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004137 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004138 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004139 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004140
Tejun Heo1fed1b22014-05-16 13:22:49 -04004141 mutex_lock(&cgroup_mutex);
4142
4143 list_del_rcu(&css->sibling);
4144
Tejun Heo9d755d32014-05-14 09:15:02 -04004145 if (ss) {
4146 /* css release path */
4147 cgroup_idr_remove(&ss->css_idr, css->id);
4148 } else {
4149 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004150 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4151 cgrp->id = -1;
4152 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004153
Tejun Heo1fed1b22014-05-16 13:22:49 -04004154 mutex_unlock(&cgroup_mutex);
4155
Tejun Heo0c21ead2013-08-13 20:22:51 -04004156 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004157}
4158
Tejun Heo25e15d82014-05-14 09:15:02 -04004159static void css_release(struct percpu_ref *ref)
4160{
4161 struct cgroup_subsys_state *css =
4162 container_of(ref, struct cgroup_subsys_state, refcnt);
4163
4164 INIT_WORK(&css->destroy_work, css_release_work_fn);
4165 queue_work(cgroup_destroy_wq, &css->destroy_work);
4166}
4167
Tejun Heoddfcada2014-05-04 15:09:14 -04004168static void init_and_link_css(struct cgroup_subsys_state *css,
4169 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004170{
Tejun Heoddfcada2014-05-04 15:09:14 -04004171 cgroup_get(cgrp);
4172
Tejun Heod5c419b2014-05-16 13:22:48 -04004173 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004174 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004175 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004176 INIT_LIST_HEAD(&css->sibling);
4177 INIT_LIST_HEAD(&css->children);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004178
Tejun Heod51f39b2014-05-16 13:22:48 -04004179 if (cgroup_parent(cgrp)) {
4180 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004181 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004182 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004183
Tejun Heoca8bdca2013-08-26 18:40:56 -04004184 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004185}
4186
Li Zefan2a4ac632013-07-31 16:16:40 +08004187/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004188static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004189{
Tejun Heo623f9262013-08-13 11:01:55 -04004190 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004191 int ret = 0;
4192
Tejun Heoa31f2d32012-11-19 08:13:37 -08004193 lockdep_assert_held(&cgroup_mutex);
4194
Tejun Heo92fb9742012-11-19 08:13:38 -08004195 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004196 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004197 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004198 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004199 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004200 }
Tejun Heob1929db2012-11-19 08:13:38 -08004201 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004202}
4203
Li Zefan2a4ac632013-07-31 16:16:40 +08004204/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004205static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004206{
Tejun Heo623f9262013-08-13 11:01:55 -04004207 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004208
4209 lockdep_assert_held(&cgroup_mutex);
4210
4211 if (!(css->flags & CSS_ONLINE))
4212 return;
4213
Li Zefand7eeac12013-03-12 15:35:59 -07004214 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004215 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004216
Tejun Heoeb954192013-08-08 20:11:23 -04004217 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004218 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004219
4220 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004221}
4222
Tejun Heoc81c925a2013-12-06 15:11:56 -05004223/**
4224 * create_css - create a cgroup_subsys_state
4225 * @cgrp: the cgroup new css will be associated with
4226 * @ss: the subsys of new css
4227 *
4228 * Create a new css associated with @cgrp - @ss pair. On success, the new
4229 * css is online and installed in @cgrp with all interface files created.
4230 * Returns 0 on success, -errno on failure.
4231 */
4232static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4233{
Tejun Heod51f39b2014-05-16 13:22:48 -04004234 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004235 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004236 struct cgroup_subsys_state *css;
4237 int err;
4238
Tejun Heoc81c925a2013-12-06 15:11:56 -05004239 lockdep_assert_held(&cgroup_mutex);
4240
Tejun Heo1fed1b22014-05-16 13:22:49 -04004241 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004242 if (IS_ERR(css))
4243 return PTR_ERR(css);
4244
Tejun Heoddfcada2014-05-04 15:09:14 -04004245 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004246
Tejun Heoc81c925a2013-12-06 15:11:56 -05004247 err = percpu_ref_init(&css->refcnt, css_release);
4248 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004249 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004250
Tejun Heo15a4c832014-05-04 15:09:14 -04004251 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4252 if (err < 0)
4253 goto err_free_percpu_ref;
4254 css->id = err;
4255
Tejun Heoaec25022014-02-08 10:36:58 -05004256 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004257 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004258 goto err_free_id;
4259
4260 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004261 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004262 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004263
4264 err = online_css(css);
4265 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004266 goto err_list_del;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004267
Tejun Heoc81c925a2013-12-06 15:11:56 -05004268 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004269 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004270 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 -04004271 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004272 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004273 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004274 ss->warned_broken_hierarchy = true;
4275 }
4276
4277 return 0;
4278
Tejun Heo1fed1b22014-05-16 13:22:49 -04004279err_list_del:
4280 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004281 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004282err_free_id:
4283 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004284err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004285 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004286err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004287 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004288 return err;
4289}
4290
Tejun Heob3bfd982014-05-13 12:19:22 -04004291static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4292 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004293{
Tejun Heoa9746d82014-05-13 12:19:22 -04004294 struct cgroup *parent, *cgrp;
4295 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004296 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004297 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004298 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004299
Tejun Heoa9746d82014-05-13 12:19:22 -04004300 parent = cgroup_kn_lock_live(parent_kn);
4301 if (!parent)
4302 return -ENODEV;
4303 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004304
4305 /* allocate the cgroup and its ID, 0 is reserved for the root */
4306 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4307 if (!cgrp) {
4308 ret = -ENOMEM;
4309 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004310 }
4311
Tejun Heo9d755d32014-05-14 09:15:02 -04004312 ret = percpu_ref_init(&cgrp->self.refcnt, css_release);
4313 if (ret)
4314 goto out_free_cgrp;
4315
Li Zefan0ab02ca2014-02-11 16:05:46 +08004316 /*
4317 * Temporarily set the pointer to NULL, so idr_find() won't return
4318 * a half-baked cgroup.
4319 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004320 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004321 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004322 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004323 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004324 }
4325
Paul Menagecc31edc2008-10-18 20:28:04 -07004326 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327
Tejun Heo9d800df2014-05-14 09:15:00 -04004328 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004329 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330
Li Zefanb6abdb02008-03-04 14:28:19 -08004331 if (notify_on_release(parent))
4332 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4333
Tejun Heo2260e7f2012-11-19 08:13:38 -08004334 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4335 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004336
Tejun Heo2bd59d42014-02-11 11:52:49 -05004337 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004338 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004339 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004340 ret = PTR_ERR(kn);
4341 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004342 }
4343 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344
Tejun Heo6f305582014-02-12 09:29:50 -05004345 /*
4346 * This extra ref will be put in cgroup_free_fn() and guarantees
4347 * that @cgrp->kn is always accessible.
4348 */
4349 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004350
Tejun Heo00356bd2013-06-18 11:14:22 -07004351 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004352
Tejun Heo4e139af2012-11-19 08:13:36 -08004353 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004354 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004355 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004356 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004357
Tejun Heo0d802552013-12-06 15:11:56 -05004358 /*
4359 * @cgrp is now fully operational. If something fails after this
4360 * point, it'll be released via the normal destruction path.
4361 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004362 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004363
Tejun Heoba0f4d72014-05-13 12:19:22 -04004364 ret = cgroup_kn_set_ugid(kn);
4365 if (ret)
4366 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004367
Tejun Heoba0f4d72014-05-13 12:19:22 -04004368 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4369 if (ret)
4370 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004371
Tejun Heo9d403e92013-12-06 15:11:56 -05004372 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004373 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004374 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004375 ret = create_css(cgrp, ss);
4376 if (ret)
4377 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004378 }
Tejun Heoa8638032012-11-09 09:12:29 -08004379 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004380
Tejun Heobd53d612014-04-23 11:13:16 -04004381 /*
4382 * On the default hierarchy, a child doesn't automatically inherit
4383 * child_subsys_mask from the parent. Each is configured manually.
4384 */
4385 if (!cgroup_on_dfl(cgrp))
4386 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004387
Tejun Heo2bd59d42014-02-11 11:52:49 -05004388 kernfs_activate(kn);
4389
Tejun Heoba0f4d72014-05-13 12:19:22 -04004390 ret = 0;
4391 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392
Tejun Heoba0f4d72014-05-13 12:19:22 -04004393out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004394 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004395out_cancel_ref:
4396 percpu_ref_cancel_init(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004397out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004398 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004399out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004400 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004401 return ret;
4402
4403out_destroy:
4404 cgroup_destroy_locked(cgrp);
4405 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004406}
4407
Tejun Heo223dbc32013-08-13 20:22:50 -04004408/*
4409 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004410 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4411 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004412 */
4413static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004414{
Tejun Heo223dbc32013-08-13 20:22:50 -04004415 struct cgroup_subsys_state *css =
4416 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004417
Tejun Heof20104d2013-08-13 20:22:50 -04004418 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004419 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004420 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004421
Tejun Heo09a503ea2013-08-13 20:22:50 -04004422 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004423}
4424
Tejun Heo223dbc32013-08-13 20:22:50 -04004425/* css kill confirmation processing requires process context, bounce */
4426static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004427{
4428 struct cgroup_subsys_state *css =
4429 container_of(ref, struct cgroup_subsys_state, refcnt);
4430
Tejun Heo223dbc32013-08-13 20:22:50 -04004431 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004432 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004433}
4434
Tejun Heof392e512014-04-23 11:13:14 -04004435/**
4436 * kill_css - destroy a css
4437 * @css: css to destroy
4438 *
4439 * This function initiates destruction of @css by removing cgroup interface
4440 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004441 * asynchronously once css_tryget_online() is guaranteed to fail and when
4442 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004443 */
4444static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004445{
Tejun Heo01f64742014-05-13 12:19:23 -04004446 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004447
Tejun Heo2bd59d42014-02-11 11:52:49 -05004448 /*
4449 * This must happen before css is disassociated with its cgroup.
4450 * See seq_css() for details.
4451 */
Tejun Heoaec25022014-02-08 10:36:58 -05004452 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004453
Tejun Heoedae0c32013-08-13 20:22:51 -04004454 /*
4455 * Killing would put the base ref, but we need to keep it alive
4456 * until after ->css_offline().
4457 */
4458 css_get(css);
4459
4460 /*
4461 * cgroup core guarantees that, by the time ->css_offline() is
4462 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004463 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004464 * proceed to offlining css's because percpu_ref_kill() doesn't
4465 * guarantee that the ref is seen as killed on all CPUs on return.
4466 *
4467 * Use percpu_ref_kill_and_confirm() to get notifications as each
4468 * css is confirmed to be seen as killed on all CPUs.
4469 */
4470 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004471}
4472
4473/**
4474 * cgroup_destroy_locked - the first stage of cgroup destruction
4475 * @cgrp: cgroup to be destroyed
4476 *
4477 * css's make use of percpu refcnts whose killing latency shouldn't be
4478 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004479 * guarantee that css_tryget_online() won't succeed by the time
4480 * ->css_offline() is invoked. To satisfy all the requirements,
4481 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004482 *
4483 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4484 * userland visible parts and start killing the percpu refcnts of
4485 * css's. Set up so that the next stage will be kicked off once all
4486 * the percpu refcnts are confirmed to be killed.
4487 *
4488 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4489 * rest of destruction. Once all cgroup references are gone, the
4490 * cgroup is RCU-freed.
4491 *
4492 * This function implements s1. After this step, @cgrp is gone as far as
4493 * the userland is concerned and a new cgroup with the same name may be
4494 * created. As cgroup doesn't care about the names internally, this
4495 * doesn't cause any problem.
4496 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004497static int cgroup_destroy_locked(struct cgroup *cgrp)
4498 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004499{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004500 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004501 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004502 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004503
Tejun Heo42809dd2012-11-19 08:13:37 -08004504 lockdep_assert_held(&cgroup_mutex);
4505
Tejun Heoddd69142013-06-12 21:04:54 -07004506 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004507 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004508 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004509 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004510 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004511 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004512 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004513 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004514 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004515
Tejun Heo1a90dd52012-11-05 09:16:59 -08004516 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004517 * Make sure there's no live children. We can't test emptiness of
4518 * ->self.children as dead children linger on it while being
4519 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004520 */
Tejun Heocbc125e2014-05-14 09:15:01 -04004521 if (cgroup_has_live_children(cgrp))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004522 return -EBUSY;
4523
4524 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004525 * Mark @cgrp dead. This prevents further task migration and child
4526 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004527 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004528 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004529 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004530 */
Tejun Heo54766d42013-06-12 21:04:53 -07004531 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004532
Tejun Heo249f3462014-05-14 09:15:01 -04004533 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004534 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004535 kill_css(css);
4536
Tejun Heo455050d2013-06-13 19:27:41 -07004537 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4538 raw_spin_lock(&release_list_lock);
4539 if (!list_empty(&cgrp->release_list))
4540 list_del_init(&cgrp->release_list);
4541 raw_spin_unlock(&release_list_lock);
4542
4543 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004544 * Remove @cgrp directory along with the base files. @cgrp has an
4545 * extra ref on its kn.
4546 */
4547 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004548
Tejun Heod51f39b2014-05-16 13:22:48 -04004549 set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags);
4550 check_for_release(cgroup_parent(cgrp));
Tejun Heo9e4173e2014-05-14 09:15:01 -04004551
Tejun Heo249f3462014-05-14 09:15:01 -04004552 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004553 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo249f3462014-05-14 09:15:01 -04004554
Tejun Heoea15f8c2013-06-13 19:27:42 -07004555 return 0;
4556};
4557
Tejun Heo2bd59d42014-02-11 11:52:49 -05004558static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004559{
Tejun Heoa9746d82014-05-13 12:19:22 -04004560 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004561 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004562
Tejun Heoa9746d82014-05-13 12:19:22 -04004563 cgrp = cgroup_kn_lock_live(kn);
4564 if (!cgrp)
4565 return 0;
4566 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004567
Tejun Heoa9746d82014-05-13 12:19:22 -04004568 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004569
Tejun Heoa9746d82014-05-13 12:19:22 -04004570 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004571
4572 /*
4573 * There are two control paths which try to determine cgroup from
4574 * dentry without going through kernfs - cgroupstats_build() and
4575 * css_tryget_online_from_dir(). Those are supported by RCU
4576 * protecting clearing of cgrp->kn->priv backpointer, which should
4577 * happen after all files under it have been removed.
4578 */
4579 if (!ret)
4580 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4581
Tejun Heo2bd59d42014-02-11 11:52:49 -05004582 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004583 return ret;
4584}
4585
Tejun Heo2bd59d42014-02-11 11:52:49 -05004586static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4587 .remount_fs = cgroup_remount,
4588 .show_options = cgroup_show_options,
4589 .mkdir = cgroup_mkdir,
4590 .rmdir = cgroup_rmdir,
4591 .rename = cgroup_rename,
4592};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004593
Tejun Heo15a4c832014-05-04 15:09:14 -04004594static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004595{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004597
4598 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004599
Tejun Heo648bb562012-11-19 08:13:36 -08004600 mutex_lock(&cgroup_mutex);
4601
Tejun Heo15a4c832014-05-04 15:09:14 -04004602 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004603 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004604
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004605 /* Create the root cgroup state for this subsystem */
4606 ss->root = &cgrp_dfl_root;
4607 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608 /* We don't handle early failures gracefully */
4609 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004610 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004611
4612 /*
4613 * Root csses are never destroyed and we can't initialize
4614 * percpu_ref during early init. Disable refcnting.
4615 */
4616 css->flags |= CSS_NO_REF;
4617
Tejun Heo15a4c832014-05-04 15:09:14 -04004618 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004619 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004620 css->id = 1;
4621 } else {
4622 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4623 BUG_ON(css->id < 0);
4624 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625
Li Zefane8d55fd2008-04-29 01:00:13 -07004626 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004627 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004628 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004629 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004630 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631
4632 need_forkexit_callback |= ss->fork || ss->exit;
4633
Li Zefane8d55fd2008-04-29 01:00:13 -07004634 /* At system boot, before all subsystems have been
4635 * registered, no tasks have been forked, so we don't
4636 * need to invoke fork callbacks here. */
4637 BUG_ON(!list_empty(&init_task.tasks));
4638
Tejun Heoae7f1642013-08-13 20:22:50 -04004639 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004640
Tejun Heof392e512014-04-23 11:13:14 -04004641 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004642
Ben Blume6a11052010-03-10 15:22:09 -08004643 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644}
4645
4646/**
Li Zefana043e3b2008-02-23 15:24:09 -08004647 * cgroup_init_early - cgroup initialization at system boot
4648 *
4649 * Initialize cgroups at system boot, and initialize any
4650 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004651 */
4652int __init cgroup_init_early(void)
4653{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004654 static struct cgroup_sb_opts __initdata opts =
4655 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004656 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004658
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004659 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004660 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4661
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004662 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004663
Tejun Heo3ed80a62014-02-08 10:36:58 -05004664 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004665 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004666 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4667 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004668 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004669 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4670 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671
Tejun Heoaec25022014-02-08 10:36:58 -05004672 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004673 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674
4675 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004676 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004677 }
4678 return 0;
4679}
4680
4681/**
Li Zefana043e3b2008-02-23 15:24:09 -08004682 * cgroup_init - cgroup initialization
4683 *
4684 * Register cgroup filesystem and /proc file, and initialize
4685 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004686 */
4687int __init cgroup_init(void)
4688{
Tejun Heo30159ec2013-06-25 11:53:37 -07004689 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004690 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004691 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004692
Tejun Heo2bd59d42014-02-11 11:52:49 -05004693 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004695 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004696
Tejun Heo82fe9b02013-06-25 11:53:37 -07004697 /* Add init_css_set to the hash table */
4698 key = css_set_hash(init_css_set.subsys);
4699 hash_add(css_set_table, &init_css_set.hlist, key);
4700
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004701 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004702
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004703 mutex_unlock(&cgroup_mutex);
4704
Tejun Heo172a2c062014-03-19 10:23:53 -04004705 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004706 if (ss->early_init) {
4707 struct cgroup_subsys_state *css =
4708 init_css_set.subsys[ss->id];
4709
4710 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4711 GFP_KERNEL);
4712 BUG_ON(css->id < 0);
4713 } else {
4714 cgroup_init_subsys(ss, false);
4715 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004716
Tejun Heo2d8f2432014-04-23 11:13:15 -04004717 list_add_tail(&init_css_set.e_cset_node[ssid],
4718 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4719
Tejun Heo172a2c062014-03-19 10:23:53 -04004720 /*
4721 * cftype registration needs kmalloc and can't be done
4722 * during early_init. Register base cftypes separately.
4723 */
4724 if (ss->base_cftypes)
4725 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4726 }
Greg KH676db4a2010-08-05 13:53:35 -07004727
Paul Menageddbcc7e2007-10-18 23:39:30 -07004728 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004729 if (!cgroup_kobj)
4730 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004731
4732 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733 if (err < 0) {
4734 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004735 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004736 }
4737
4738 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004739 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004740}
Paul Menageb4f48b62007-10-18 23:39:33 -07004741
Tejun Heoe5fca242013-11-22 17:14:39 -05004742static int __init cgroup_wq_init(void)
4743{
4744 /*
4745 * There isn't much point in executing destruction path in
4746 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004747 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004748 *
4749 * We would prefer to do this in cgroup_init() above, but that
4750 * is called before init_workqueues(): so leave this until after.
4751 */
Tejun Heo1a115332014-02-12 19:06:19 -05004752 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004753 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004754
4755 /*
4756 * Used to destroy pidlists and separate to serve as flush domain.
4757 * Cap @max_active to 1 too.
4758 */
4759 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4760 0, 1);
4761 BUG_ON(!cgroup_pidlist_destroy_wq);
4762
Tejun Heoe5fca242013-11-22 17:14:39 -05004763 return 0;
4764}
4765core_initcall(cgroup_wq_init);
4766
Paul Menagea4243162007-10-18 23:39:35 -07004767/*
4768 * proc_cgroup_show()
4769 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4770 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004771 */
4772
4773/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004774int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004775{
4776 struct pid *pid;
4777 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004778 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004779 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004780 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004781
4782 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004783 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004784 if (!buf)
4785 goto out;
4786
4787 retval = -ESRCH;
4788 pid = m->private;
4789 tsk = get_pid_task(pid, PIDTYPE_PID);
4790 if (!tsk)
4791 goto out_free;
4792
4793 retval = 0;
4794
4795 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004796 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004797
Tejun Heo985ed672014-03-19 10:23:53 -04004798 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004799 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004800 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004801 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004802
Tejun Heoa2dd4242014-03-19 10:23:55 -04004803 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004804 continue;
4805
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004806 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004807 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004808 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004809 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004810 if (strlen(root->name))
4811 seq_printf(m, "%sname=%s", count ? "," : "",
4812 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004813 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004814 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004815 path = cgroup_path(cgrp, buf, PATH_MAX);
4816 if (!path) {
4817 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004818 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004819 }
4820 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004821 seq_putc(m, '\n');
4822 }
4823
4824out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004825 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004826 mutex_unlock(&cgroup_mutex);
4827 put_task_struct(tsk);
4828out_free:
4829 kfree(buf);
4830out:
4831 return retval;
4832}
4833
Paul Menagea4243162007-10-18 23:39:35 -07004834/* Display information about each subsystem and each hierarchy */
4835static int proc_cgroupstats_show(struct seq_file *m, void *v)
4836{
Tejun Heo30159ec2013-06-25 11:53:37 -07004837 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004838 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004839
Paul Menage8bab8dd2008-04-04 14:29:57 -07004840 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004841 /*
4842 * ideally we don't want subsystems moving around while we do this.
4843 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4844 * subsys/hierarchy state.
4845 */
Paul Menagea4243162007-10-18 23:39:35 -07004846 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004847
4848 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004849 seq_printf(m, "%s\t%d\t%d\t%d\n",
4850 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004851 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004852
Paul Menagea4243162007-10-18 23:39:35 -07004853 mutex_unlock(&cgroup_mutex);
4854 return 0;
4855}
4856
4857static int cgroupstats_open(struct inode *inode, struct file *file)
4858{
Al Viro9dce07f2008-03-29 03:07:28 +00004859 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004860}
4861
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004862static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004863 .open = cgroupstats_open,
4864 .read = seq_read,
4865 .llseek = seq_lseek,
4866 .release = single_release,
4867};
4868
Paul Menageb4f48b62007-10-18 23:39:33 -07004869/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004870 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004871 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004872 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004873 * A task is associated with the init_css_set until cgroup_post_fork()
4874 * attaches it to the parent's css_set. Empty cg_list indicates that
4875 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004876 */
4877void cgroup_fork(struct task_struct *child)
4878{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004879 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004880 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004881}
4882
4883/**
Li Zefana043e3b2008-02-23 15:24:09 -08004884 * cgroup_post_fork - called on a new task after adding it to the task list
4885 * @child: the task in question
4886 *
Tejun Heo5edee612012-10-16 15:03:14 -07004887 * Adds the task to the list running through its css_set if necessary and
4888 * call the subsystem fork() callbacks. Has to be after the task is
4889 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004890 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004891 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004892 */
Paul Menage817929e2007-10-18 23:39:36 -07004893void cgroup_post_fork(struct task_struct *child)
4894{
Tejun Heo30159ec2013-06-25 11:53:37 -07004895 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004896 int i;
4897
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004898 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004899 * This may race against cgroup_enable_task_cg_links(). As that
4900 * function sets use_task_css_set_links before grabbing
4901 * tasklist_lock and we just went through tasklist_lock to add
4902 * @child, it's guaranteed that either we see the set
4903 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4904 * @child during its iteration.
4905 *
4906 * If we won the race, @child is associated with %current's
4907 * css_set. Grabbing css_set_rwsem guarantees both that the
4908 * association is stable, and, on completion of the parent's
4909 * migration, @child is visible in the source of migration or
4910 * already in the destination cgroup. This guarantee is necessary
4911 * when implementing operations which need to migrate all tasks of
4912 * a cgroup to another.
4913 *
4914 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4915 * will remain in init_css_set. This is safe because all tasks are
4916 * in the init_css_set before cg_links is enabled and there's no
4917 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004918 */
Paul Menage817929e2007-10-18 23:39:36 -07004919 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004920 struct css_set *cset;
4921
Tejun Heo96d365e2014-02-13 06:58:40 -05004922 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004923 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004924 if (list_empty(&child->cg_list)) {
4925 rcu_assign_pointer(child->cgroups, cset);
4926 list_add(&child->cg_list, &cset->tasks);
4927 get_css_set(cset);
4928 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004929 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004930 }
Tejun Heo5edee612012-10-16 15:03:14 -07004931
4932 /*
4933 * Call ss->fork(). This must happen after @child is linked on
4934 * css_set; otherwise, @child might change state between ->fork()
4935 * and addition to css_set.
4936 */
4937 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004938 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004939 if (ss->fork)
4940 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004941 }
Paul Menage817929e2007-10-18 23:39:36 -07004942}
Tejun Heo5edee612012-10-16 15:03:14 -07004943
Paul Menage817929e2007-10-18 23:39:36 -07004944/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004945 * cgroup_exit - detach cgroup from exiting task
4946 * @tsk: pointer to task_struct of exiting process
4947 *
4948 * Description: Detach cgroup from @tsk and release it.
4949 *
4950 * Note that cgroups marked notify_on_release force every task in
4951 * them to take the global cgroup_mutex mutex when exiting.
4952 * This could impact scaling on very large systems. Be reluctant to
4953 * use notify_on_release cgroups where very high task exit scaling
4954 * is required on large systems.
4955 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004956 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4957 * call cgroup_exit() while the task is still competent to handle
4958 * notify_on_release(), then leave the task attached to the root cgroup in
4959 * each hierarchy for the remainder of its exit. No need to bother with
4960 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004961 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004962 */
Li Zefan1ec41832014-03-28 15:22:19 +08004963void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004964{
Tejun Heo30159ec2013-06-25 11:53:37 -07004965 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004966 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004967 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004968 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004969
4970 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004971 * Unlink from @tsk from its css_set. As migration path can't race
4972 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004973 */
4974 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004975 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004976 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004977 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004978 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004979 }
4980
Paul Menageb4f48b62007-10-18 23:39:33 -07004981 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004982 cset = task_css_set(tsk);
4983 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004984
Li Zefan1ec41832014-03-28 15:22:19 +08004985 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004986 /* see cgroup_post_fork() for details */
4987 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004988 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004989 struct cgroup_subsys_state *old_css = cset->subsys[i];
4990 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004991
Tejun Heoeb954192013-08-08 20:11:23 -04004992 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004993 }
4994 }
4995 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004996
Tejun Heoeaf797a2014-02-25 10:04:03 -05004997 if (put_cset)
4998 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004999}
Paul Menage697f4162007-10-18 23:39:34 -07005000
Paul Menagebd89aab2007-10-18 23:40:44 -07005001static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005002{
Li Zefanf50daa72013-03-01 15:06:07 +08005003 if (cgroup_is_releasable(cgrp) &&
Tejun Heo9e4173e2014-05-14 09:15:01 -04005004 list_empty(&cgrp->cset_links) && !cgroup_has_live_children(cgrp)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005005 /*
5006 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005007 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005008 * it now
5009 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005010 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005011
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005012 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005013 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005014 list_empty(&cgrp->release_list)) {
5015 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005016 need_schedule_work = 1;
5017 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005018 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005019 if (need_schedule_work)
5020 schedule_work(&release_agent_work);
5021 }
5022}
5023
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024/*
5025 * Notify userspace when a cgroup is released, by running the
5026 * configured release agent with the name of the cgroup (path
5027 * relative to the root of cgroup file system) as the argument.
5028 *
5029 * Most likely, this user command will try to rmdir this cgroup.
5030 *
5031 * This races with the possibility that some other task will be
5032 * attached to this cgroup before it is removed, or that some other
5033 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5034 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5035 * unused, and this cgroup will be reprieved from its death sentence,
5036 * to continue to serve a useful existence. Next time it's released,
5037 * we will get notified again, if it still has 'notify_on_release' set.
5038 *
5039 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5040 * means only wait until the task is successfully execve()'d. The
5041 * separate release agent task is forked by call_usermodehelper(),
5042 * then control in this thread returns here, without waiting for the
5043 * release agent task. We don't bother to wait because the caller of
5044 * this routine has no use for the exit status of the release agent
5045 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047static void cgroup_release_agent(struct work_struct *work)
5048{
5049 BUG_ON(work != &release_agent_work);
5050 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005051 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005052 while (!list_empty(&release_list)) {
5053 char *argv[3], *envp[3];
5054 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005055 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005056 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005057 struct cgroup,
5058 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005059 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005060 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005061 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005062 if (!pathbuf)
5063 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005064 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5065 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005066 goto continue_free;
5067 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5068 if (!agentbuf)
5069 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005070
5071 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005072 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005073 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005074 argv[i] = NULL;
5075
5076 i = 0;
5077 /* minimal command environment */
5078 envp[i++] = "HOME=/";
5079 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5080 envp[i] = NULL;
5081
5082 /* Drop the lock while we invoke the usermode helper,
5083 * since the exec could involve hitting disk and hence
5084 * be a slow process */
5085 mutex_unlock(&cgroup_mutex);
5086 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005087 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005088 continue_free:
5089 kfree(pathbuf);
5090 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005091 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005093 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005094 mutex_unlock(&cgroup_mutex);
5095}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005096
5097static int __init cgroup_disable(char *str)
5098{
Tejun Heo30159ec2013-06-25 11:53:37 -07005099 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005100 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005101 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005102
5103 while ((token = strsep(&str, ",")) != NULL) {
5104 if (!*token)
5105 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005106
Tejun Heo3ed80a62014-02-08 10:36:58 -05005107 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005108 if (!strcmp(token, ss->name)) {
5109 ss->disabled = 1;
5110 printk(KERN_INFO "Disabling %s control group"
5111 " subsystem\n", ss->name);
5112 break;
5113 }
5114 }
5115 }
5116 return 1;
5117}
5118__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005119
Tejun Heob77d7b62013-08-13 11:01:54 -04005120/**
Tejun Heoec903c02014-05-13 12:11:01 -04005121 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005122 * @dentry: directory dentry of interest
5123 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005124 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005125 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5126 * to get the corresponding css and return it. If such css doesn't exist
5127 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005128 */
Tejun Heoec903c02014-05-13 12:11:01 -04005129struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5130 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005131{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005132 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5133 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005134 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005135
Tejun Heo35cf0832013-08-26 18:40:56 -04005136 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005137 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5138 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005139 return ERR_PTR(-EBADF);
5140
Tejun Heo5a17f542014-02-11 11:52:47 -05005141 rcu_read_lock();
5142
Tejun Heo2bd59d42014-02-11 11:52:49 -05005143 /*
5144 * This path doesn't originate from kernfs and @kn could already
5145 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005146 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005147 */
5148 cgrp = rcu_dereference(kn->priv);
5149 if (cgrp)
5150 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005151
Tejun Heoec903c02014-05-13 12:11:01 -04005152 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005153 css = ERR_PTR(-ENOENT);
5154
5155 rcu_read_unlock();
5156 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005157}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005158
Li Zefan1cb650b2013-08-19 10:05:24 +08005159/**
5160 * css_from_id - lookup css by id
5161 * @id: the cgroup id
5162 * @ss: cgroup subsys to be looked into
5163 *
5164 * Returns the css if there's valid one with @id, otherwise returns NULL.
5165 * Should be called under rcu_read_lock().
5166 */
5167struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5168{
Tejun Heo6fa49182014-05-04 15:09:13 -04005169 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005170 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005171}
5172
Paul Menagefe693432009-09-23 15:56:20 -07005173#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005174static struct cgroup_subsys_state *
5175debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005176{
5177 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5178
5179 if (!css)
5180 return ERR_PTR(-ENOMEM);
5181
5182 return css;
5183}
5184
Tejun Heoeb954192013-08-08 20:11:23 -04005185static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005186{
Tejun Heoeb954192013-08-08 20:11:23 -04005187 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005188}
5189
Tejun Heo182446d2013-08-08 20:11:24 -04005190static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5191 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005192{
Tejun Heo182446d2013-08-08 20:11:24 -04005193 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005194}
5195
Tejun Heo182446d2013-08-08 20:11:24 -04005196static u64 current_css_set_read(struct cgroup_subsys_state *css,
5197 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005198{
5199 return (u64)(unsigned long)current->cgroups;
5200}
5201
Tejun Heo182446d2013-08-08 20:11:24 -04005202static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005203 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005204{
5205 u64 count;
5206
5207 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005208 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005209 rcu_read_unlock();
5210 return count;
5211}
5212
Tejun Heo2da8ca82013-12-05 12:28:04 -05005213static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005214{
Tejun Heo69d02062013-06-12 21:04:50 -07005215 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005216 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005217 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005218
Tejun Heoe61734c2014-02-12 09:29:50 -05005219 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5220 if (!name_buf)
5221 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005222
Tejun Heo96d365e2014-02-13 06:58:40 -05005223 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005224 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005225 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005226 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005227 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005228
Tejun Heoa2dd4242014-03-19 10:23:55 -04005229 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005230 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005231 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005232 }
5233 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005234 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005235 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005236 return 0;
5237}
5238
5239#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005240static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005241{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005242 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005243 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005244
Tejun Heo96d365e2014-02-13 06:58:40 -05005245 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005246 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005247 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005248 struct task_struct *task;
5249 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005250
Tejun Heo5abb8852013-06-12 21:04:49 -07005251 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005252
Tejun Heo5abb8852013-06-12 21:04:49 -07005253 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005254 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5255 goto overflow;
5256 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005257 }
Tejun Heoc7561122014-02-25 10:04:01 -05005258
5259 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5260 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5261 goto overflow;
5262 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5263 }
5264 continue;
5265 overflow:
5266 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005267 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005268 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005269 return 0;
5270}
5271
Tejun Heo182446d2013-08-08 20:11:24 -04005272static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005273{
Tejun Heo182446d2013-08-08 20:11:24 -04005274 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005275}
5276
5277static struct cftype debug_files[] = {
5278 {
Paul Menagefe693432009-09-23 15:56:20 -07005279 .name = "taskcount",
5280 .read_u64 = debug_taskcount_read,
5281 },
5282
5283 {
5284 .name = "current_css_set",
5285 .read_u64 = current_css_set_read,
5286 },
5287
5288 {
5289 .name = "current_css_set_refcount",
5290 .read_u64 = current_css_set_refcount_read,
5291 },
5292
5293 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005294 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005295 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005296 },
5297
5298 {
5299 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005300 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005301 },
5302
5303 {
Paul Menagefe693432009-09-23 15:56:20 -07005304 .name = "releasable",
5305 .read_u64 = releasable_read,
5306 },
Paul Menagefe693432009-09-23 15:56:20 -07005307
Tejun Heo4baf6e32012-04-01 12:09:55 -07005308 { } /* terminate */
5309};
Paul Menagefe693432009-09-23 15:56:20 -07005310
Tejun Heo073219e2014-02-08 10:36:58 -05005311struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005312 .css_alloc = debug_css_alloc,
5313 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005314 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005315};
5316#endif /* CONFIG_CGROUP_DEBUG */