blob: 004004fd0dedf06a59ae5c36b7e352c959072678 [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/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400160 * Assign a monotonically increasing serial number to csses. It guarantees
161 * cgroups with bigger numbers are newer than those with smaller numbers.
162 * Also, as csses are always appended to the parent's ->children list, it
163 * guarantees that sibling csses are always sorted in the ascending serial
164 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800165 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400166static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800167
Paul Menageddbcc7e2007-10-18 23:39:30 -0700168/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800169 * check for fork/exit handlers to call. This avoids us having to do
170 * extra work in the fork/exit path if none of the subsystems need to
171 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700172 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700173static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700174
Tejun Heo628f7cd2013-06-28 16:24:11 -0700175static struct cftype cgroup_base_files[];
176
Tejun Heo59f52962014-02-11 11:52:49 -0500177static void cgroup_put(struct cgroup *cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -0400178static bool cgroup_has_live_children(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400179static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400180 unsigned int ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800181static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400182static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400183static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400184static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400185static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
186 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500187static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800188
Tejun Heo6fa49182014-05-04 15:09:13 -0400189/* IDR wrappers which synchronize using cgroup_idr_lock */
190static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
191 gfp_t gfp_mask)
192{
193 int ret;
194
195 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400196 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400197 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400198 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400199 idr_preload_end();
200 return ret;
201}
202
203static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
204{
205 void *ret;
206
Tejun Heo54504e92014-05-13 12:10:59 -0400207 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400208 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400209 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400210 return ret;
211}
212
213static void cgroup_idr_remove(struct idr *idr, int id)
214{
Tejun Heo54504e92014-05-13 12:10:59 -0400215 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400216 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400217 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400218}
219
Tejun Heod51f39b2014-05-16 13:22:48 -0400220static struct cgroup *cgroup_parent(struct cgroup *cgrp)
221{
222 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
223
224 if (parent_css)
225 return container_of(parent_css, struct cgroup, self);
226 return NULL;
227}
228
Tejun Heo95109b62013-08-08 20:11:27 -0400229/**
230 * cgroup_css - obtain a cgroup's css for the specified subsystem
231 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400232 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400233 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400234 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
235 * function must be called either under cgroup_mutex or rcu_read_lock() and
236 * the caller is responsible for pinning the returned css if it wants to
237 * keep accessing it outside the said locks. This function may return
238 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400239 */
240static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400241 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400242{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400243 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500244 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500245 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400246 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400247 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400248}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700249
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400250/**
251 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
252 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400253 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400254 *
255 * Similar to cgroup_css() but returns the effctive css, which is defined
256 * as the matching css of the nearest ancestor including self which has @ss
257 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
258 * function is guaranteed to return non-NULL css.
259 */
260static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
261 struct cgroup_subsys *ss)
262{
263 lockdep_assert_held(&cgroup_mutex);
264
265 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400266 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400267
268 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
269 return NULL;
270
Tejun Heod51f39b2014-05-16 13:22:48 -0400271 while (cgroup_parent(cgrp) &&
272 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
273 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400274
275 return cgroup_css(cgrp, ss);
276}
277
Paul Menageddbcc7e2007-10-18 23:39:30 -0700278/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700279static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700280{
Tejun Heo184faf32014-05-16 13:22:51 -0400281 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700282}
283
Tejun Heob4168642014-05-13 12:16:21 -0400284struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500285{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500286 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400287 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500288
289 /*
290 * This is open and unprotected implementation of cgroup_css().
291 * seq_css() is only called from a kernfs file operation which has
292 * an active reference on the file. Because all the subsystem
293 * files are drained before a css is disassociated with a cgroup,
294 * the matching css from the cgroup's subsys table is guaranteed to
295 * be and stay valid until the enclosing operation is complete.
296 */
297 if (cft->ss)
298 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
299 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400300 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500301}
Tejun Heob4168642014-05-13 12:16:21 -0400302EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500303
Li Zefan78574cf2013-04-08 19:00:38 -0700304/**
305 * cgroup_is_descendant - test ancestry
306 * @cgrp: the cgroup to be tested
307 * @ancestor: possible ancestor of @cgrp
308 *
309 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
310 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
311 * and @ancestor are accessible.
312 */
313bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
314{
315 while (cgrp) {
316 if (cgrp == ancestor)
317 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400318 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700319 }
320 return false;
321}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700322
Adrian Bunke9685a02008-02-07 00:13:46 -0800323static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700324{
325 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700326 (1 << CGRP_RELEASABLE) |
327 (1 << CGRP_NOTIFY_ON_RELEASE);
328 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329}
330
Adrian Bunke9685a02008-02-07 00:13:46 -0800331static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332{
Paul Menagebd89aab2007-10-18 23:40:44 -0700333 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334}
335
Tejun Heo30159ec2013-06-25 11:53:37 -0700336/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500337 * for_each_css - iterate all css's of a cgroup
338 * @css: the iteration cursor
339 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
340 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700341 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400342 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700343 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500344#define for_each_css(css, ssid, cgrp) \
345 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
346 if (!((css) = rcu_dereference_check( \
347 (cgrp)->subsys[(ssid)], \
348 lockdep_is_held(&cgroup_mutex)))) { } \
349 else
350
351/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400352 * for_each_e_css - iterate all effective css's of a cgroup
353 * @css: the iteration cursor
354 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
355 * @cgrp: the target cgroup to iterate css's of
356 *
357 * Should be called under cgroup_[tree_]mutex.
358 */
359#define for_each_e_css(css, ssid, cgrp) \
360 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
361 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
362 ; \
363 else
364
365/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500366 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700367 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500368 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700369 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500370#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500371 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
372 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700373
Tejun Heo985ed672014-03-19 10:23:53 -0400374/* iterate across the hierarchies */
375#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700376 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700377
Tejun Heof8f22e52014-04-23 11:13:16 -0400378/* iterate over child cgrps, lock should be held throughout iteration */
379#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400380 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400381 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400382 cgroup_is_dead(child); })) \
383 ; \
384 else
385
Paul Menage81a6a5c2007-10-18 23:39:38 -0700386/* the list of cgroups eligible for automatic release. Protected by
387 * release_list_lock */
388static LIST_HEAD(release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +0200389static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700390static void cgroup_release_agent(struct work_struct *work);
391static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700392static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393
Tejun Heo69d02062013-06-12 21:04:50 -0700394/*
395 * A cgroup can be associated with multiple css_sets as different tasks may
396 * belong to different cgroups on different hierarchies. In the other
397 * direction, a css_set is naturally associated with multiple cgroups.
398 * This M:N relationship is represented by the following link structure
399 * which exists for each association and allows traversing the associations
400 * from both sides.
401 */
402struct cgrp_cset_link {
403 /* the cgroup and css_set this link associates */
404 struct cgroup *cgrp;
405 struct css_set *cset;
406
407 /* list of cgrp_cset_links anchored at cgrp->cset_links */
408 struct list_head cset_link;
409
410 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
411 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700412};
413
Tejun Heo172a2c062014-03-19 10:23:53 -0400414/*
415 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700416 * hierarchies being mounted. It contains a pointer to the root state
417 * for each subsystem. Also used to anchor the list of css_sets. Not
418 * reference-counted, to improve performance when child cgroups
419 * haven't been created.
420 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400421struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400422 .refcount = ATOMIC_INIT(1),
423 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
424 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
425 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
426 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
427 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
428};
Paul Menage817929e2007-10-18 23:39:36 -0700429
Tejun Heo172a2c062014-03-19 10:23:53 -0400430static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700431
Tejun Heo842b5972014-04-25 18:28:02 -0400432/**
433 * cgroup_update_populated - updated populated count of a cgroup
434 * @cgrp: the target cgroup
435 * @populated: inc or dec populated count
436 *
437 * @cgrp is either getting the first task (css_set) or losing the last.
438 * Update @cgrp->populated_cnt accordingly. The count is propagated
439 * towards root so that a given cgroup's populated_cnt is zero iff the
440 * cgroup and all its descendants are empty.
441 *
442 * @cgrp's interface file "cgroup.populated" is zero if
443 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
444 * changes from or to zero, userland is notified that the content of the
445 * interface file has changed. This can be used to detect when @cgrp and
446 * its descendants become populated or empty.
447 */
448static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
449{
450 lockdep_assert_held(&css_set_rwsem);
451
452 do {
453 bool trigger;
454
455 if (populated)
456 trigger = !cgrp->populated_cnt++;
457 else
458 trigger = !--cgrp->populated_cnt;
459
460 if (!trigger)
461 break;
462
463 if (cgrp->populated_kn)
464 kernfs_notify(cgrp->populated_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400465 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400466 } while (cgrp);
467}
468
Paul Menage7717f7b2009-09-23 15:56:22 -0700469/*
470 * hash table for cgroup groups. This improves the performance to find
471 * an existing css_set. This hash doesn't (currently) take into
472 * account cgroups in empty hierarchies.
473 */
Li Zefan472b1052008-04-29 01:00:11 -0700474#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800475static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700476
Li Zefan0ac801f2013-01-10 11:49:27 +0800477static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700478{
Li Zefan0ac801f2013-01-10 11:49:27 +0800479 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700480 struct cgroup_subsys *ss;
481 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700482
Tejun Heo30159ec2013-06-25 11:53:37 -0700483 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800484 key += (unsigned long)css[i];
485 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700486
Li Zefan0ac801f2013-01-10 11:49:27 +0800487 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700488}
489
Tejun Heo89c55092014-02-13 06:58:40 -0500490static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700491{
Tejun Heo69d02062013-06-12 21:04:50 -0700492 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400493 struct cgroup_subsys *ss;
494 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700495
Tejun Heo89c55092014-02-13 06:58:40 -0500496 lockdep_assert_held(&css_set_rwsem);
497
498 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700499 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700500
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700501 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400502 for_each_subsys(ss, ssid)
503 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700504 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700505 css_set_count--;
506
Tejun Heo69d02062013-06-12 21:04:50 -0700507 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700508 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700509
Tejun Heo69d02062013-06-12 21:04:50 -0700510 list_del(&link->cset_link);
511 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800512
Tejun Heo96d365e2014-02-13 06:58:40 -0500513 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400514 if (list_empty(&cgrp->cset_links)) {
515 cgroup_update_populated(cgrp, false);
516 if (notify_on_release(cgrp)) {
517 if (taskexit)
518 set_bit(CGRP_RELEASABLE, &cgrp->flags);
519 check_for_release(cgrp);
520 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700521 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700522
523 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700524 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700525
Tejun Heo5abb8852013-06-12 21:04:49 -0700526 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700527}
528
Tejun Heo89c55092014-02-13 06:58:40 -0500529static void put_css_set(struct css_set *cset, bool taskexit)
530{
531 /*
532 * Ensure that the refcount doesn't hit zero while any readers
533 * can see it. Similar to atomic_dec_and_lock(), but for an
534 * rwlock
535 */
536 if (atomic_add_unless(&cset->refcount, -1, 1))
537 return;
538
539 down_write(&css_set_rwsem);
540 put_css_set_locked(cset, taskexit);
541 up_write(&css_set_rwsem);
542}
543
Paul Menage817929e2007-10-18 23:39:36 -0700544/*
545 * refcounted get/put for css_set objects
546 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700547static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700548{
Tejun Heo5abb8852013-06-12 21:04:49 -0700549 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700550}
551
Tejun Heob326f9d2013-06-24 15:21:48 -0700552/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700553 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700554 * @cset: candidate css_set being tested
555 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700556 * @new_cgrp: cgroup that's being entered by the task
557 * @template: desired set of css pointers in css_set (pre-calculated)
558 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800559 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700560 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
561 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700562static bool compare_css_sets(struct css_set *cset,
563 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700564 struct cgroup *new_cgrp,
565 struct cgroup_subsys_state *template[])
566{
567 struct list_head *l1, *l2;
568
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400569 /*
570 * On the default hierarchy, there can be csets which are
571 * associated with the same set of cgroups but different csses.
572 * Let's first ensure that csses match.
573 */
574 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700575 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700576
577 /*
578 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400579 * different cgroups in hierarchies. As different cgroups may
580 * share the same effective css, this comparison is always
581 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700582 */
Tejun Heo69d02062013-06-12 21:04:50 -0700583 l1 = &cset->cgrp_links;
584 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700586 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700587 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700588
589 l1 = l1->next;
590 l2 = l2->next;
591 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700592 if (l1 == &cset->cgrp_links) {
593 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700594 break;
595 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700596 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700597 }
598 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700599 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
600 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
601 cgrp1 = link1->cgrp;
602 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700603 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700604 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700605
606 /*
607 * If this hierarchy is the hierarchy of the cgroup
608 * that's changing, then we need to check that this
609 * css_set points to the new cgroup; if it's any other
610 * hierarchy, then this css_set should point to the
611 * same cgroup as the old css_set.
612 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700613 if (cgrp1->root == new_cgrp->root) {
614 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700615 return false;
616 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700617 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 return false;
619 }
620 }
621 return true;
622}
623
Tejun Heob326f9d2013-06-24 15:21:48 -0700624/**
625 * find_existing_css_set - init css array and find the matching css_set
626 * @old_cset: the css_set that we're using before the cgroup transition
627 * @cgrp: the cgroup that we're moving into
628 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700629 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700630static struct css_set *find_existing_css_set(struct css_set *old_cset,
631 struct cgroup *cgrp,
632 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700633{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400634 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700635 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700636 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800637 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700638 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700639
Ben Blumaae8aab2010-03-10 15:22:07 -0800640 /*
641 * Build the set of subsystem state objects that we want to see in the
642 * new css_set. while subsystems can change globally, the entries here
643 * won't change, so no need for locking.
644 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700645 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400646 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400647 /*
648 * @ss is in this hierarchy, so we want the
649 * effective css from @cgrp.
650 */
651 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700652 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400653 /*
654 * @ss is not in this hierarchy, so we don't want
655 * to change the css.
656 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700658 }
659 }
660
Li Zefan0ac801f2013-01-10 11:49:27 +0800661 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 hash_for_each_possible(css_set_table, cset, hlist, key) {
663 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700664 continue;
665
666 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700668 }
Paul Menage817929e2007-10-18 23:39:36 -0700669
670 /* No existing cgroup group matched */
671 return NULL;
672}
673
Tejun Heo69d02062013-06-12 21:04:50 -0700674static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700675{
Tejun Heo69d02062013-06-12 21:04:50 -0700676 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700677
Tejun Heo69d02062013-06-12 21:04:50 -0700678 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
679 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700680 kfree(link);
681 }
682}
683
Tejun Heo69d02062013-06-12 21:04:50 -0700684/**
685 * allocate_cgrp_cset_links - allocate cgrp_cset_links
686 * @count: the number of links to allocate
687 * @tmp_links: list_head the allocated links are put on
688 *
689 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
690 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700691 */
Tejun Heo69d02062013-06-12 21:04:50 -0700692static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700693{
Tejun Heo69d02062013-06-12 21:04:50 -0700694 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700695 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700696
697 INIT_LIST_HEAD(tmp_links);
698
Li Zefan36553432008-07-29 22:33:19 -0700699 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700700 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700701 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700702 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700703 return -ENOMEM;
704 }
Tejun Heo69d02062013-06-12 21:04:50 -0700705 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700706 }
707 return 0;
708}
709
Li Zefanc12f65d2009-01-07 18:07:42 -0800710/**
711 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700712 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800714 * @cgrp: the destination cgroup
715 */
Tejun Heo69d02062013-06-12 21:04:50 -0700716static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
717 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800718{
Tejun Heo69d02062013-06-12 21:04:50 -0700719 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800720
Tejun Heo69d02062013-06-12 21:04:50 -0700721 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400722
723 if (cgroup_on_dfl(cgrp))
724 cset->dfl_cgrp = cgrp;
725
Tejun Heo69d02062013-06-12 21:04:50 -0700726 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
727 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700728 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400729
730 if (list_empty(&cgrp->cset_links))
731 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700732 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400733
Paul Menage7717f7b2009-09-23 15:56:22 -0700734 /*
735 * Always add links to the tail of the list so that the list
736 * is sorted by order of hierarchy creation
737 */
Tejun Heo69d02062013-06-12 21:04:50 -0700738 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800739}
740
Tejun Heob326f9d2013-06-24 15:21:48 -0700741/**
742 * find_css_set - return a new css_set with one cgroup updated
743 * @old_cset: the baseline css_set
744 * @cgrp: the cgroup to be updated
745 *
746 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
747 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700748 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700749static struct css_set *find_css_set(struct css_set *old_cset,
750 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700751{
Tejun Heob326f9d2013-06-24 15:21:48 -0700752 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700753 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700754 struct list_head tmp_links;
755 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400756 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800757 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400758 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700759
Tejun Heob326f9d2013-06-24 15:21:48 -0700760 lockdep_assert_held(&cgroup_mutex);
761
Paul Menage817929e2007-10-18 23:39:36 -0700762 /* First see if we already have a cgroup group that matches
763 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500764 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700765 cset = find_existing_css_set(old_cset, cgrp, template);
766 if (cset)
767 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500768 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700769
Tejun Heo5abb8852013-06-12 21:04:49 -0700770 if (cset)
771 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700772
Tejun Heof4f4be22013-06-12 21:04:51 -0700773 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700774 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700775 return NULL;
776
Tejun Heo69d02062013-06-12 21:04:50 -0700777 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700778 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700779 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700780 return NULL;
781 }
782
Tejun Heo5abb8852013-06-12 21:04:49 -0700783 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700784 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700785 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500786 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500787 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500788 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700789 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700790
791 /* Copy the set of subsystem state objects generated in
792 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700793 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700794
Tejun Heo96d365e2014-02-13 06:58:40 -0500795 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700796 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700797 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700798 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700799
Paul Menage7717f7b2009-09-23 15:56:22 -0700800 if (c->root == cgrp->root)
801 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700802 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700803 }
Paul Menage817929e2007-10-18 23:39:36 -0700804
Tejun Heo69d02062013-06-12 21:04:50 -0700805 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700806
Paul Menage817929e2007-10-18 23:39:36 -0700807 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700808
Tejun Heo2d8f2432014-04-23 11:13:15 -0400809 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700810 key = css_set_hash(cset->subsys);
811 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700812
Tejun Heo2d8f2432014-04-23 11:13:15 -0400813 for_each_subsys(ss, ssid)
814 list_add_tail(&cset->e_cset_node[ssid],
815 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
816
Tejun Heo96d365e2014-02-13 06:58:40 -0500817 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700818
Tejun Heo5abb8852013-06-12 21:04:49 -0700819 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700820}
821
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400822static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700823{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400824 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500825
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400826 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500827}
828
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400829static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500830{
831 int id;
832
833 lockdep_assert_held(&cgroup_mutex);
834
Tejun Heo985ed672014-03-19 10:23:53 -0400835 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500836 if (id < 0)
837 return id;
838
839 root->hierarchy_id = id;
840 return 0;
841}
842
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400843static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500844{
845 lockdep_assert_held(&cgroup_mutex);
846
847 if (root->hierarchy_id) {
848 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
849 root->hierarchy_id = 0;
850 }
851}
852
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400853static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500854{
855 if (root) {
856 /* hierarhcy ID shoulid already have been released */
857 WARN_ON_ONCE(root->hierarchy_id);
858
859 idr_destroy(&root->cgroup_idr);
860 kfree(root);
861 }
862}
863
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400864static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500865{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400866 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500867 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500868
Tejun Heo2bd59d42014-02-11 11:52:49 -0500869 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500870
Tejun Heo776f02f2014-02-12 09:29:50 -0500871 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -0400872 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -0500873
Tejun Heof2e85d52014-02-11 11:52:49 -0500874 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400875 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500876
877 /*
878 * Release all the links from cset_links to this hierarchy's
879 * root cgroup
880 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500881 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500882
883 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
884 list_del(&link->cset_link);
885 list_del(&link->cgrp_link);
886 kfree(link);
887 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500888 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500889
890 if (!list_empty(&root->root_list)) {
891 list_del(&root->root_list);
892 cgroup_root_count--;
893 }
894
895 cgroup_exit_root_id(root);
896
897 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500898
Tejun Heo2bd59d42014-02-11 11:52:49 -0500899 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500900 cgroup_free_root(root);
901}
902
Tejun Heoceb6a082014-02-25 10:04:02 -0500903/* look up cgroup associated with given css_set on the specified hierarchy */
904static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400905 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700906{
Paul Menage7717f7b2009-09-23 15:56:22 -0700907 struct cgroup *res = NULL;
908
Tejun Heo96d365e2014-02-13 06:58:40 -0500909 lockdep_assert_held(&cgroup_mutex);
910 lockdep_assert_held(&css_set_rwsem);
911
Tejun Heo5abb8852013-06-12 21:04:49 -0700912 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400913 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700914 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700915 struct cgrp_cset_link *link;
916
917 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700918 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700919
Paul Menage7717f7b2009-09-23 15:56:22 -0700920 if (c->root == root) {
921 res = c;
922 break;
923 }
924 }
925 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500926
Paul Menage7717f7b2009-09-23 15:56:22 -0700927 BUG_ON(!res);
928 return res;
929}
930
931/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500932 * Return the cgroup for "task" from the given hierarchy. Must be
933 * called with cgroup_mutex and css_set_rwsem held.
934 */
935static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400936 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500937{
938 /*
939 * No need to lock the task - since we hold cgroup_mutex the
940 * task can't change groups, so the only thing that can happen
941 * is that it exits and its css is set back to init_css_set.
942 */
943 return cset_cgroup_from_root(task_css_set(task), root);
944}
945
946/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947 * A task must hold cgroup_mutex to modify cgroups.
948 *
949 * Any task can increment and decrement the count field without lock.
950 * So in general, code holding cgroup_mutex can't rely on the count
951 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800952 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953 * means that no tasks are currently attached, therefore there is no
954 * way a task attached to that cgroup can fork (the other way to
955 * increment the count). So code holding cgroup_mutex can safely
956 * assume that if the count is zero, it will stay zero. Similarly, if
957 * a task holds cgroup_mutex on a cgroup with zero count, it
958 * knows that the cgroup won't be removed, as cgroup_rmdir()
959 * needs that mutex.
960 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
962 * (usually) take cgroup_mutex. These are the two most performance
963 * critical pieces of code here. The exception occurs on cgroup_exit(),
964 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
965 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800966 * to the release agent with the name of the cgroup (path relative to
967 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968 *
969 * A cgroup can only be deleted if both its 'count' of using tasks
970 * is zero, and its list of 'children' cgroups is empty. Since all
971 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400972 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400974 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975 *
976 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800977 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978 */
979
Tejun Heo69dfa002014-05-04 15:09:13 -0400980static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500981static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700982static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700983
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500984static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
985 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500987 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
988 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
989 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
990 cft->ss->name, cft->name);
991 else
992 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
993 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994}
995
Tejun Heof2e85d52014-02-11 11:52:49 -0500996/**
997 * cgroup_file_mode - deduce file mode of a control file
998 * @cft: the control file in question
999 *
1000 * returns cft->mode if ->mode is not 0
1001 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1002 * returns S_IRUGO if it has only a read handler
1003 * returns S_IWUSR if it has only a write hander
1004 */
1005static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001006{
Tejun Heof2e85d52014-02-11 11:52:49 -05001007 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001008
Tejun Heof2e85d52014-02-11 11:52:49 -05001009 if (cft->mode)
1010 return cft->mode;
1011
1012 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1013 mode |= S_IRUGO;
1014
Tejun Heo6770c642014-05-13 12:16:21 -04001015 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001016 mode |= S_IWUSR;
1017
1018 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001019}
1020
Tejun Heo59f52962014-02-11 11:52:49 -05001021static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001023 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001024 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025}
1026
Tejun Heo59f52962014-02-11 11:52:49 -05001027static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028{
Tejun Heo9d755d32014-05-14 09:15:02 -04001029 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030}
1031
Tejun Heoa9746d82014-05-13 12:19:22 -04001032/**
1033 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1034 * @kn: the kernfs_node being serviced
1035 *
1036 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1037 * the method finishes if locking succeeded. Note that once this function
1038 * returns the cgroup returned by cgroup_kn_lock_live() may become
1039 * inaccessible any time. If the caller intends to continue to access the
1040 * cgroup, it should pin it before invoking this function.
1041 */
1042static void cgroup_kn_unlock(struct kernfs_node *kn)
1043{
1044 struct cgroup *cgrp;
1045
1046 if (kernfs_type(kn) == KERNFS_DIR)
1047 cgrp = kn->priv;
1048 else
1049 cgrp = kn->parent->priv;
1050
1051 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001052
1053 kernfs_unbreak_active_protection(kn);
1054 cgroup_put(cgrp);
1055}
1056
1057/**
1058 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1059 * @kn: the kernfs_node being serviced
1060 *
1061 * This helper is to be used by a cgroup kernfs method currently servicing
1062 * @kn. It breaks the active protection, performs cgroup locking and
1063 * verifies that the associated cgroup is alive. Returns the cgroup if
1064 * alive; otherwise, %NULL. A successful return should be undone by a
1065 * matching cgroup_kn_unlock() invocation.
1066 *
1067 * Any cgroup kernfs method implementation which requires locking the
1068 * associated cgroup should use this helper. It avoids nesting cgroup
1069 * locking under kernfs active protection and allows all kernfs operations
1070 * including self-removal.
1071 */
1072static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1073{
1074 struct cgroup *cgrp;
1075
1076 if (kernfs_type(kn) == KERNFS_DIR)
1077 cgrp = kn->priv;
1078 else
1079 cgrp = kn->parent->priv;
1080
1081 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001082 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001083 * active_ref. cgroup liveliness check alone provides enough
1084 * protection against removal. Ensure @cgrp stays accessible and
1085 * break the active_ref protection.
1086 */
1087 cgroup_get(cgrp);
1088 kernfs_break_active_protection(kn);
1089
Tejun Heoa9746d82014-05-13 12:19:22 -04001090 mutex_lock(&cgroup_mutex);
1091
1092 if (!cgroup_is_dead(cgrp))
1093 return cgrp;
1094
1095 cgroup_kn_unlock(kn);
1096 return NULL;
1097}
1098
Li Zefan2739d3c2013-01-21 18:18:33 +08001099static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001101 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102
Tejun Heo01f64742014-05-13 12:19:23 -04001103 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001104 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001105}
1106
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001107/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001108 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001109 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001110 * @subsys_mask: mask of the subsystem ids whose files should be removed
1111 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001112static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001113{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001114 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001115 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001116
Tejun Heob420ba72013-07-12 12:34:02 -07001117 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001118 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001119
Tejun Heo69dfa002014-05-04 15:09:13 -04001120 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001121 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001122 list_for_each_entry(cfts, &ss->cfts, node)
1123 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001124 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125}
1126
Tejun Heo69dfa002014-05-04 15:09:13 -04001127static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128{
Tejun Heo30159ec2013-06-25 11:53:37 -07001129 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001130 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131
Tejun Heoace2bee2014-02-11 11:52:47 -05001132 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001133
Tejun Heo5df36032014-03-19 10:23:54 -04001134 for_each_subsys(ss, ssid) {
1135 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001137
Tejun Heo7fd8c562014-04-23 11:13:16 -04001138 /* if @ss has non-root csses attached to it, can't move */
1139 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001140 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141
Tejun Heo5df36032014-03-19 10:23:54 -04001142 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001143 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001144 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145 }
1146
Tejun Heoa2dd4242014-03-19 10:23:55 -04001147 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1148 if (ret) {
1149 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001150 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151
Tejun Heoa2dd4242014-03-19 10:23:55 -04001152 /*
1153 * Rebinding back to the default root is not allowed to
1154 * fail. Using both default and non-default roots should
1155 * be rare. Moving subsystems back and forth even more so.
1156 * Just warn about it and continue.
1157 */
1158 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001159 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001160 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001161 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001162 }
Tejun Heo5df36032014-03-19 10:23:54 -04001163 }
Tejun Heo31261212013-06-28 17:07:30 -07001164
1165 /*
1166 * Nothing can fail from this point on. Remove files for the
1167 * removed subsystems and rebind each subsystem.
1168 */
Tejun Heo5df36032014-03-19 10:23:54 -04001169 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001170 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001171 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001172
Tejun Heo5df36032014-03-19 10:23:54 -04001173 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001174 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001175 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001176 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001177
Tejun Heo5df36032014-03-19 10:23:54 -04001178 if (!(ss_mask & (1 << ssid)))
1179 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001180
Tejun Heo5df36032014-03-19 10:23:54 -04001181 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001182 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001183
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001184 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001185
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001186 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1187 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001188 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001189 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001190
Tejun Heo2d8f2432014-04-23 11:13:15 -04001191 down_write(&css_set_rwsem);
1192 hash_for_each(css_set_table, i, cset, hlist)
1193 list_move_tail(&cset->e_cset_node[ss->id],
1194 &dst_root->cgrp.e_csets[ss->id]);
1195 up_write(&css_set_rwsem);
1196
Tejun Heof392e512014-04-23 11:13:14 -04001197 src_root->subsys_mask &= ~(1 << ssid);
1198 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1199
Tejun Heobd53d612014-04-23 11:13:16 -04001200 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001201 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001202 if (dst_root != &cgrp_dfl_root)
1203 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001204
Tejun Heo5df36032014-03-19 10:23:54 -04001205 if (ss->bind)
1206 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001208
Tejun Heoa2dd4242014-03-19 10:23:55 -04001209 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001210 return 0;
1211}
1212
Tejun Heo2bd59d42014-02-11 11:52:49 -05001213static int cgroup_show_options(struct seq_file *seq,
1214 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001216 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001218 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001219
Tejun Heob85d2042013-12-06 15:11:57 -05001220 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001221 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001222 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001223 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1224 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001225 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001226 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001227 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001228 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001229
1230 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001231 if (strlen(root->release_agent_path))
1232 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001233 spin_unlock(&release_agent_path_lock);
1234
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001235 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001236 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001237 if (strlen(root->name))
1238 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 return 0;
1240}
1241
1242struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001243 unsigned int subsys_mask;
1244 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001245 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001246 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001247 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248 /* User explicitly requested empty subsystem */
1249 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001250};
1251
Ben Blumcf5d5942010-03-10 15:22:09 -08001252static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001254 char *token, *o = data;
1255 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001256 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001257 struct cgroup_subsys *ss;
1258 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001259
1260#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001261 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263
Paul Menagec6d57f32009-09-23 15:56:19 -07001264 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001265
1266 while ((token = strsep(&o, ",")) != NULL) {
1267 if (!*token)
1268 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001269 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270 /* Explicitly have no subsystems */
1271 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001272 continue;
1273 }
1274 if (!strcmp(token, "all")) {
1275 /* Mutually exclusive option 'all' + subsystem name */
1276 if (one_ss)
1277 return -EINVAL;
1278 all_ss = true;
1279 continue;
1280 }
Tejun Heo873fe092013-04-14 20:15:26 -07001281 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1282 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1283 continue;
1284 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001285 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001286 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001287 continue;
1288 }
1289 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001290 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001291 continue;
1292 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001293 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001294 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001295 continue;
1296 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001297 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001298 /* Specifying two release agents is forbidden */
1299 if (opts->release_agent)
1300 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001301 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001302 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001303 if (!opts->release_agent)
1304 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001305 continue;
1306 }
1307 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001308 const char *name = token + 5;
1309 /* Can't specify an empty name */
1310 if (!strlen(name))
1311 return -EINVAL;
1312 /* Must match [\w.-]+ */
1313 for (i = 0; i < strlen(name); i++) {
1314 char c = name[i];
1315 if (isalnum(c))
1316 continue;
1317 if ((c == '.') || (c == '-') || (c == '_'))
1318 continue;
1319 return -EINVAL;
1320 }
1321 /* Specifying two names is forbidden */
1322 if (opts->name)
1323 return -EINVAL;
1324 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001325 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001326 GFP_KERNEL);
1327 if (!opts->name)
1328 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001329
1330 continue;
1331 }
1332
Tejun Heo30159ec2013-06-25 11:53:37 -07001333 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001334 if (strcmp(token, ss->name))
1335 continue;
1336 if (ss->disabled)
1337 continue;
1338
1339 /* Mutually exclusive option 'all' + subsystem name */
1340 if (all_ss)
1341 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001342 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001343 one_ss = true;
1344
1345 break;
1346 }
1347 if (i == CGROUP_SUBSYS_COUNT)
1348 return -ENOENT;
1349 }
1350
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001351 /* Consistency checks */
1352
Tejun Heo873fe092013-04-14 20:15:26 -07001353 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001354 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 -07001355
Tejun Heod3ba07c2014-02-13 06:58:38 -05001356 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1357 opts->cpuset_clone_children || opts->release_agent ||
1358 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001359 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001360 return -EINVAL;
1361 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001362 } else {
1363 /*
1364 * If the 'all' option was specified select all the
1365 * subsystems, otherwise if 'none', 'name=' and a subsystem
1366 * name options were not specified, let's default to 'all'
1367 */
1368 if (all_ss || (!one_ss && !opts->none && !opts->name))
1369 for_each_subsys(ss, i)
1370 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001371 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001372
Tejun Heoa2dd4242014-03-19 10:23:55 -04001373 /*
1374 * We either have to specify by name or by subsystems. (So
1375 * all empty hierarchies must have a name).
1376 */
1377 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001378 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001379 }
1380
Li Zefanf9ab5b52009-06-17 16:26:33 -07001381 /*
1382 * Option noprefix was introduced just for backward compatibility
1383 * with the old cpuset, so we allow noprefix only if mounting just
1384 * the cpuset subsystem.
1385 */
Tejun Heo93438622013-04-14 20:15:25 -07001386 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001387 return -EINVAL;
1388
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001389
1390 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001391 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001392 return -EINVAL;
1393
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 return 0;
1395}
1396
Tejun Heo2bd59d42014-02-11 11:52:49 -05001397static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398{
1399 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001400 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001402 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403
Tejun Heo873fe092013-04-14 20:15:26 -07001404 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001405 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001406 return -EINVAL;
1407 }
1408
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409 mutex_lock(&cgroup_mutex);
1410
1411 /* See what subsystems are wanted */
1412 ret = parse_cgroupfs_options(data, &opts);
1413 if (ret)
1414 goto out_unlock;
1415
Tejun Heof392e512014-04-23 11:13:14 -04001416 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001417 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001418 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001419
Tejun Heof392e512014-04-23 11:13:14 -04001420 added_mask = opts.subsys_mask & ~root->subsys_mask;
1421 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001422
Ben Blumcf5d5942010-03-10 15:22:09 -08001423 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001424 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001425 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001426 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001427 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1428 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001430 goto out_unlock;
1431 }
1432
Tejun Heof172e672013-06-28 17:07:30 -07001433 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001434 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001435 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001436 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001437 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438
Tejun Heo5df36032014-03-19 10:23:54 -04001439 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001440 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001443 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001444
Tejun Heo69e943b2014-02-08 10:36:58 -05001445 if (opts.release_agent) {
1446 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001447 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001448 spin_unlock(&release_agent_path_lock);
1449 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001451 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001452 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454 return ret;
1455}
1456
Tejun Heoafeb0f92014-02-13 06:58:39 -05001457/*
1458 * To reduce the fork() overhead for systems that are not actually using
1459 * their cgroups capability, we don't maintain the lists running through
1460 * each css_set to its tasks until we see the list actually used - in other
1461 * words after the first mount.
1462 */
1463static bool use_task_css_set_links __read_mostly;
1464
1465static void cgroup_enable_task_cg_lists(void)
1466{
1467 struct task_struct *p, *g;
1468
Tejun Heo96d365e2014-02-13 06:58:40 -05001469 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001470
1471 if (use_task_css_set_links)
1472 goto out_unlock;
1473
1474 use_task_css_set_links = true;
1475
1476 /*
1477 * We need tasklist_lock because RCU is not safe against
1478 * while_each_thread(). Besides, a forking task that has passed
1479 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1480 * is not guaranteed to have its child immediately visible in the
1481 * tasklist if we walk through it with RCU.
1482 */
1483 read_lock(&tasklist_lock);
1484 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001485 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1486 task_css_set(p) != &init_css_set);
1487
1488 /*
1489 * We should check if the process is exiting, otherwise
1490 * it will race with cgroup_exit() in that the list
1491 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001492 * Do it while holding siglock so that we don't end up
1493 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001494 */
Tejun Heof153ad12014-02-25 09:56:49 -05001495 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001496 if (!(p->flags & PF_EXITING)) {
1497 struct css_set *cset = task_css_set(p);
1498
1499 list_add(&p->cg_list, &cset->tasks);
1500 get_css_set(cset);
1501 }
Tejun Heof153ad12014-02-25 09:56:49 -05001502 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001503 } while_each_thread(g, p);
1504 read_unlock(&tasklist_lock);
1505out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001506 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001507}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
Paul Menagecc31edc2008-10-18 20:28:04 -07001509static void init_cgroup_housekeeping(struct cgroup *cgrp)
1510{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001511 struct cgroup_subsys *ss;
1512 int ssid;
1513
Tejun Heod5c419b2014-05-16 13:22:48 -04001514 INIT_LIST_HEAD(&cgrp->self.sibling);
1515 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001516 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001517 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001518 INIT_LIST_HEAD(&cgrp->pidlists);
1519 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001520 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001521 cgrp->self.flags |= CSS_ONLINE;
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 Menagee788e0662008-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 Menagee788e0662008-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 Menagee788e0662008-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 Menagee788e0662008-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 Menagee788e0662008-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 Menagee788e0662008-07-25 01:46:59 -07002395 seq_putc(seq, '\n');
Paul Menagee788e0662008-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
Tejun Heoc2931b72014-05-16 13:22:51 -04003093 * @pos: the current position (%NULL to initiate traversal)
3094 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003095 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003096 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003097 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003098 * that @parent and @pos are accessible. The next sibling is guaranteed to
3099 * be returned regardless of their states.
3100 *
3101 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3102 * css which finished ->css_online() is guaranteed to be visible in the
3103 * future iterations and will stay visible until the last reference is put.
3104 * A css which hasn't finished ->css_online() or already finished
3105 * ->css_offline() may show up during traversal. It's each subsystem's
3106 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003107 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003108struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3109 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003110{
Tejun Heoc2931b72014-05-16 13:22:51 -04003111 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003112
Tejun Heo8353da12014-05-13 12:19:23 -04003113 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003114
3115 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003116 * @pos could already have been unlinked from the sibling list.
3117 * Once a cgroup is removed, its ->sibling.next is no longer
3118 * updated when its next sibling changes. CSS_RELEASED is set when
3119 * @pos is taken off list, at which time its next pointer is valid,
3120 * and, as releases are serialized, the one pointed to by the next
3121 * pointer is guaranteed to not have started release yet. This
3122 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3123 * critical section, the one pointed to by its next pointer is
3124 * guaranteed to not have finished its RCU grace period even if we
3125 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003126 *
Tejun Heode3f0342014-05-16 13:22:49 -04003127 * If @pos has CSS_RELEASED set, its next pointer can't be
3128 * dereferenced; however, as each css is given a monotonically
3129 * increasing unique serial number and always appended to the
3130 * sibling list, the next one can be found by walking the parent's
3131 * children until the first css with higher serial number than
3132 * @pos's. While this path can be slower, it happens iff iteration
3133 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003134 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003135 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003136 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3137 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3138 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003139 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003140 list_for_each_entry_rcu(next, &parent->children, sibling)
3141 if (next->serial_nr > pos->serial_nr)
Tejun Heo3b287a52013-08-08 20:11:24 -04003142 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003143 }
3144
Tejun Heo3b281af2014-04-23 11:13:15 -04003145 /*
3146 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003147 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003148 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003149 if (&next->sibling != &parent->children)
3150 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003151 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003152}
Tejun Heo53fa5262013-05-24 10:55:38 +09003153
3154/**
Tejun Heo492eb212013-08-08 20:11:25 -04003155 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003156 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003157 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158 *
Tejun Heo492eb212013-08-08 20:11:25 -04003159 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003160 * to visit for pre-order traversal of @root's descendants. @root is
3161 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003162 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003163 * While this function requires cgroup_mutex or RCU read locking, it
3164 * doesn't require the whole traversal to be contained in a single critical
3165 * section. This function will return the correct next descendant as long
3166 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003167 *
3168 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3169 * css which finished ->css_online() is guaranteed to be visible in the
3170 * future iterations and will stay visible until the last reference is put.
3171 * A css which hasn't finished ->css_online() or already finished
3172 * ->css_offline() may show up during traversal. It's each subsystem's
3173 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003174 */
Tejun Heo492eb212013-08-08 20:11:25 -04003175struct cgroup_subsys_state *
3176css_next_descendant_pre(struct cgroup_subsys_state *pos,
3177 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178{
Tejun Heo492eb212013-08-08 20:11:25 -04003179 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003180
Tejun Heo8353da12014-05-13 12:19:23 -04003181 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182
Tejun Heobd8815a2013-08-08 20:11:27 -04003183 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003184 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003185 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003186
3187 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003188 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189 if (next)
3190 return next;
3191
3192 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003193 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003194 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003195 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003196 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003197 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003198 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003199
3200 return NULL;
3201}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003202
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003203/**
Tejun Heo492eb212013-08-08 20:11:25 -04003204 * css_rightmost_descendant - return the rightmost descendant of a css
3205 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003206 *
Tejun Heo492eb212013-08-08 20:11:25 -04003207 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3208 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003209 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003210 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003211 * While this function requires cgroup_mutex or RCU read locking, it
3212 * doesn't require the whole traversal to be contained in a single critical
3213 * section. This function will return the correct rightmost descendant as
3214 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003215 */
Tejun Heo492eb212013-08-08 20:11:25 -04003216struct cgroup_subsys_state *
3217css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003218{
Tejun Heo492eb212013-08-08 20:11:25 -04003219 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003220
Tejun Heo8353da12014-05-13 12:19:23 -04003221 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003222
3223 do {
3224 last = pos;
3225 /* ->prev isn't RCU safe, walk ->next till the end */
3226 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003227 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003228 pos = tmp;
3229 } while (pos);
3230
3231 return last;
3232}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003233
Tejun Heo492eb212013-08-08 20:11:25 -04003234static struct cgroup_subsys_state *
3235css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003236{
Tejun Heo492eb212013-08-08 20:11:25 -04003237 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003238
3239 do {
3240 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003241 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003242 } while (pos);
3243
3244 return last;
3245}
3246
3247/**
Tejun Heo492eb212013-08-08 20:11:25 -04003248 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003249 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003250 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003251 *
Tejun Heo492eb212013-08-08 20:11:25 -04003252 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003253 * to visit for post-order traversal of @root's descendants. @root is
3254 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003255 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003256 * While this function requires cgroup_mutex or RCU read locking, it
3257 * doesn't require the whole traversal to be contained in a single critical
3258 * section. This function will return the correct next descendant as long
3259 * as both @pos and @cgroup are accessible and @pos is a descendant of
3260 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003261 *
3262 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3263 * css which finished ->css_online() is guaranteed to be visible in the
3264 * future iterations and will stay visible until the last reference is put.
3265 * A css which hasn't finished ->css_online() or already finished
3266 * ->css_offline() may show up during traversal. It's each subsystem's
3267 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003268 */
Tejun Heo492eb212013-08-08 20:11:25 -04003269struct cgroup_subsys_state *
3270css_next_descendant_post(struct cgroup_subsys_state *pos,
3271 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003272{
Tejun Heo492eb212013-08-08 20:11:25 -04003273 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003274
Tejun Heo8353da12014-05-13 12:19:23 -04003275 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003276
Tejun Heo58b79a92013-09-06 15:31:08 -04003277 /* if first iteration, visit leftmost descendant which may be @root */
3278 if (!pos)
3279 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003280
Tejun Heobd8815a2013-08-08 20:11:27 -04003281 /* if we visited @root, we're done */
3282 if (pos == root)
3283 return NULL;
3284
Tejun Heo574bd9f2012-11-09 09:12:29 -08003285 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003286 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003287 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003288 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003289
3290 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003291 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003292}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003293
Tejun Heocbc125e2014-05-14 09:15:01 -04003294static bool cgroup_has_live_children(struct cgroup *cgrp)
3295{
3296 struct cgroup *child;
3297
3298 rcu_read_lock();
Tejun Heod5c419b2014-05-16 13:22:48 -04003299 list_for_each_entry_rcu(child, &cgrp->self.children, self.sibling) {
Tejun Heocbc125e2014-05-14 09:15:01 -04003300 if (!cgroup_is_dead(child)) {
3301 rcu_read_unlock();
3302 return true;
3303 }
3304 }
3305 rcu_read_unlock();
3306 return false;
3307}
3308
Tejun Heo0942eee2013-08-08 20:11:26 -04003309/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003310 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003311 * @it: the iterator to advance
3312 *
3313 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003314 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003315static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003316{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003317 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003318 struct cgrp_cset_link *link;
3319 struct css_set *cset;
3320
3321 /* Advance to the next non-empty css_set */
3322 do {
3323 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003324 if (l == it->cset_head) {
3325 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003326 return;
3327 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003328
3329 if (it->ss) {
3330 cset = container_of(l, struct css_set,
3331 e_cset_node[it->ss->id]);
3332 } else {
3333 link = list_entry(l, struct cgrp_cset_link, cset_link);
3334 cset = link->cset;
3335 }
Tejun Heoc7561122014-02-25 10:04:01 -05003336 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3337
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003338 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003339
3340 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003341 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003342 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003343 it->task_pos = cset->mg_tasks.next;
3344
3345 it->tasks_head = &cset->tasks;
3346 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003347}
3348
Tejun Heo0942eee2013-08-08 20:11:26 -04003349/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003350 * css_task_iter_start - initiate task iteration
3351 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003352 * @it: the task iterator to use
3353 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003354 * Initiate iteration through the tasks of @css. The caller can call
3355 * css_task_iter_next() to walk through the tasks until the function
3356 * returns NULL. On completion of iteration, css_task_iter_end() must be
3357 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003358 *
3359 * Note that this function acquires a lock which is released when the
3360 * iteration finishes. The caller can't sleep while iteration is in
3361 * progress.
3362 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003363void css_task_iter_start(struct cgroup_subsys_state *css,
3364 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003365 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003366{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003367 /* no one should try to iterate before mounting cgroups */
3368 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003369
Tejun Heo96d365e2014-02-13 06:58:40 -05003370 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003371
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003372 it->ss = css->ss;
3373
3374 if (it->ss)
3375 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3376 else
3377 it->cset_pos = &css->cgroup->cset_links;
3378
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003379 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003380
Tejun Heo72ec7022013-08-08 20:11:26 -04003381 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003382}
3383
Tejun Heo0942eee2013-08-08 20:11:26 -04003384/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003385 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003386 * @it: the task iterator being iterated
3387 *
3388 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003389 * initialized via css_task_iter_start(). Returns NULL when the iteration
3390 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003391 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003392struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003393{
3394 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003395 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003396
3397 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003398 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003399 return NULL;
3400 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003401
3402 /*
3403 * Advance iterator to find next entry. cset->tasks is consumed
3404 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3405 * next cset.
3406 */
Paul Menage817929e2007-10-18 23:39:36 -07003407 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003408
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003409 if (l == it->tasks_head)
3410 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003411
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003412 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003413 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003414 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003415 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003416
Paul Menage817929e2007-10-18 23:39:36 -07003417 return res;
3418}
3419
Tejun Heo0942eee2013-08-08 20:11:26 -04003420/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003421 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003422 * @it: the task iterator to finish
3423 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003424 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003425 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003426void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003427 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003428{
Tejun Heo96d365e2014-02-13 06:58:40 -05003429 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003430}
3431
3432/**
3433 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3434 * @to: cgroup to which the tasks will be moved
3435 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003436 *
3437 * Locking rules between cgroup_post_fork() and the migration path
3438 * guarantee that, if a task is forking while being migrated, the new child
3439 * is guaranteed to be either visible in the source cgroup after the
3440 * parent's migration is complete or put into the target cgroup. No task
3441 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003442 */
3443int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3444{
Tejun Heo952aaa12014-02-25 10:04:03 -05003445 LIST_HEAD(preloaded_csets);
3446 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003447 struct css_task_iter it;
3448 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003449 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003450
Tejun Heo952aaa12014-02-25 10:04:03 -05003451 mutex_lock(&cgroup_mutex);
3452
3453 /* all tasks in @from are being moved, all csets are source */
3454 down_read(&css_set_rwsem);
3455 list_for_each_entry(link, &from->cset_links, cset_link)
3456 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3457 up_read(&css_set_rwsem);
3458
3459 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3460 if (ret)
3461 goto out_err;
3462
3463 /*
3464 * Migrate tasks one-by-one until @form is empty. This fails iff
3465 * ->can_attach() fails.
3466 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003467 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003468 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003469 task = css_task_iter_next(&it);
3470 if (task)
3471 get_task_struct(task);
3472 css_task_iter_end(&it);
3473
3474 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003475 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003476 put_task_struct(task);
3477 }
3478 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003479out_err:
3480 cgroup_migrate_finish(&preloaded_csets);
3481 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003482 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003483}
3484
Paul Menage817929e2007-10-18 23:39:36 -07003485/*
Ben Blum102a7752009-09-23 15:56:26 -07003486 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003487 *
3488 * Reading this file can return large amounts of data if a cgroup has
3489 * *lots* of attached tasks. So it may need several calls to read(),
3490 * but we cannot guarantee that the information we produce is correct
3491 * unless we produce it entirely atomically.
3492 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003493 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003494
Li Zefan24528252012-01-20 11:58:43 +08003495/* which pidlist file are we talking about? */
3496enum cgroup_filetype {
3497 CGROUP_FILE_PROCS,
3498 CGROUP_FILE_TASKS,
3499};
3500
3501/*
3502 * A pidlist is a list of pids that virtually represents the contents of one
3503 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3504 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3505 * to the cgroup.
3506 */
3507struct cgroup_pidlist {
3508 /*
3509 * used to find which pidlist is wanted. doesn't change as long as
3510 * this particular list stays in the list.
3511 */
3512 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3513 /* array of xids */
3514 pid_t *list;
3515 /* how many elements the above list has */
3516 int length;
Li Zefan24528252012-01-20 11:58:43 +08003517 /* each of these stored in a list by its cgroup */
3518 struct list_head links;
3519 /* pointer to the cgroup we belong to, for list removal purposes */
3520 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003521 /* for delayed destruction */
3522 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003523};
3524
Paul Menagebbcb81d2007-10-18 23:39:32 -07003525/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003526 * The following two functions "fix" the issue where there are more pids
3527 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3528 * TODO: replace with a kernel-wide solution to this problem
3529 */
3530#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3531static void *pidlist_allocate(int count)
3532{
3533 if (PIDLIST_TOO_LARGE(count))
3534 return vmalloc(count * sizeof(pid_t));
3535 else
3536 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3537}
Tejun Heob1a21362013-11-29 10:42:58 -05003538
Ben Blumd1d9fd32009-09-23 15:56:28 -07003539static void pidlist_free(void *p)
3540{
3541 if (is_vmalloc_addr(p))
3542 vfree(p);
3543 else
3544 kfree(p);
3545}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003546
3547/*
Tejun Heob1a21362013-11-29 10:42:58 -05003548 * Used to destroy all pidlists lingering waiting for destroy timer. None
3549 * should be left afterwards.
3550 */
3551static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3552{
3553 struct cgroup_pidlist *l, *tmp_l;
3554
3555 mutex_lock(&cgrp->pidlist_mutex);
3556 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3557 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3558 mutex_unlock(&cgrp->pidlist_mutex);
3559
3560 flush_workqueue(cgroup_pidlist_destroy_wq);
3561 BUG_ON(!list_empty(&cgrp->pidlists));
3562}
3563
3564static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3565{
3566 struct delayed_work *dwork = to_delayed_work(work);
3567 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3568 destroy_dwork);
3569 struct cgroup_pidlist *tofree = NULL;
3570
3571 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003572
3573 /*
Tejun Heo04502362013-11-29 10:42:59 -05003574 * Destroy iff we didn't get queued again. The state won't change
3575 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003576 */
Tejun Heo04502362013-11-29 10:42:59 -05003577 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003578 list_del(&l->links);
3579 pidlist_free(l->list);
3580 put_pid_ns(l->key.ns);
3581 tofree = l;
3582 }
3583
Tejun Heob1a21362013-11-29 10:42:58 -05003584 mutex_unlock(&l->owner->pidlist_mutex);
3585 kfree(tofree);
3586}
3587
3588/*
Ben Blum102a7752009-09-23 15:56:26 -07003589 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003590 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003591 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003592static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003593{
Ben Blum102a7752009-09-23 15:56:26 -07003594 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003595
3596 /*
3597 * we presume the 0th element is unique, so i starts at 1. trivial
3598 * edge cases first; no work needs to be done for either
3599 */
3600 if (length == 0 || length == 1)
3601 return length;
3602 /* src and dest walk down the list; dest counts unique elements */
3603 for (src = 1; src < length; src++) {
3604 /* find next unique element */
3605 while (list[src] == list[src-1]) {
3606 src++;
3607 if (src == length)
3608 goto after;
3609 }
3610 /* dest always points to where the next unique element goes */
3611 list[dest] = list[src];
3612 dest++;
3613 }
3614after:
Ben Blum102a7752009-09-23 15:56:26 -07003615 return dest;
3616}
3617
Tejun Heoafb2bc12013-11-29 10:42:59 -05003618/*
3619 * The two pid files - task and cgroup.procs - guaranteed that the result
3620 * is sorted, which forced this whole pidlist fiasco. As pid order is
3621 * different per namespace, each namespace needs differently sorted list,
3622 * making it impossible to use, for example, single rbtree of member tasks
3623 * sorted by task pointer. As pidlists can be fairly large, allocating one
3624 * per open file is dangerous, so cgroup had to implement shared pool of
3625 * pidlists keyed by cgroup and namespace.
3626 *
3627 * All this extra complexity was caused by the original implementation
3628 * committing to an entirely unnecessary property. In the long term, we
3629 * want to do away with it. Explicitly scramble sort order if
3630 * sane_behavior so that no such expectation exists in the new interface.
3631 *
3632 * Scrambling is done by swapping every two consecutive bits, which is
3633 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3634 */
3635static pid_t pid_fry(pid_t pid)
3636{
3637 unsigned a = pid & 0x55555555;
3638 unsigned b = pid & 0xAAAAAAAA;
3639
3640 return (a << 1) | (b >> 1);
3641}
3642
3643static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3644{
3645 if (cgroup_sane_behavior(cgrp))
3646 return pid_fry(pid);
3647 else
3648 return pid;
3649}
3650
Ben Blum102a7752009-09-23 15:56:26 -07003651static int cmppid(const void *a, const void *b)
3652{
3653 return *(pid_t *)a - *(pid_t *)b;
3654}
3655
Tejun Heoafb2bc12013-11-29 10:42:59 -05003656static int fried_cmppid(const void *a, const void *b)
3657{
3658 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3659}
3660
Ben Blum72a8cb32009-09-23 15:56:27 -07003661static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3662 enum cgroup_filetype type)
3663{
3664 struct cgroup_pidlist *l;
3665 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003666 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003667
Tejun Heoe6b81712013-11-29 10:42:59 -05003668 lockdep_assert_held(&cgrp->pidlist_mutex);
3669
3670 list_for_each_entry(l, &cgrp->pidlists, links)
3671 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003672 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003673 return NULL;
3674}
3675
Ben Blum72a8cb32009-09-23 15:56:27 -07003676/*
3677 * find the appropriate pidlist for our purpose (given procs vs tasks)
3678 * returns with the lock on that pidlist already held, and takes care
3679 * of the use count, or returns NULL with no locks held if we're out of
3680 * memory.
3681 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003682static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3683 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003684{
3685 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003686
Tejun Heoe6b81712013-11-29 10:42:59 -05003687 lockdep_assert_held(&cgrp->pidlist_mutex);
3688
3689 l = cgroup_pidlist_find(cgrp, type);
3690 if (l)
3691 return l;
3692
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003694 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003695 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003697
Tejun Heob1a21362013-11-29 10:42:58 -05003698 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003699 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003700 /* don't need task_nsproxy() if we're looking at ourself */
3701 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003702 l->owner = cgrp;
3703 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003704 return l;
3705}
3706
3707/*
Ben Blum102a7752009-09-23 15:56:26 -07003708 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3709 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003710static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3711 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003712{
3713 pid_t *array;
3714 int length;
3715 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003716 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003717 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003718 struct cgroup_pidlist *l;
3719
Tejun Heo4bac00d2013-11-29 10:42:59 -05003720 lockdep_assert_held(&cgrp->pidlist_mutex);
3721
Ben Blum102a7752009-09-23 15:56:26 -07003722 /*
3723 * If cgroup gets more users after we read count, we won't have
3724 * enough space - tough. This race is indistinguishable to the
3725 * caller from the case that the additional cgroup users didn't
3726 * show up until sometime later on.
3727 */
3728 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003729 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003730 if (!array)
3731 return -ENOMEM;
3732 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003733 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003734 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003735 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003736 break;
Ben Blum102a7752009-09-23 15:56:26 -07003737 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 if (type == CGROUP_FILE_PROCS)
3739 pid = task_tgid_vnr(tsk);
3740 else
3741 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003742 if (pid > 0) /* make sure to only use valid results */
3743 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003744 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003745 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003746 length = n;
3747 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003748 if (cgroup_sane_behavior(cgrp))
3749 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3750 else
3751 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003752 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003753 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003754
Tejun Heoe6b81712013-11-29 10:42:59 -05003755 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003756 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003757 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003758 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003759 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003760 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003761
3762 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003763 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003764 l->list = array;
3765 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003766 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003767 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003768}
3769
Balbir Singh846c7bb2007-10-18 23:39:44 -07003770/**
Li Zefana043e3b2008-02-23 15:24:09 -08003771 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003772 * @stats: cgroupstats to fill information into
3773 * @dentry: A dentry entry belonging to the cgroup for which stats have
3774 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003775 *
3776 * Build and fill cgroupstats so that taskstats can export it to user
3777 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003778 */
3779int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3780{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003781 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003782 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003783 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003784 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003785
Tejun Heo2bd59d42014-02-11 11:52:49 -05003786 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3787 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3788 kernfs_type(kn) != KERNFS_DIR)
3789 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003790
Li Zefanbad34662014-02-14 16:54:28 +08003791 mutex_lock(&cgroup_mutex);
3792
Tejun Heo2bd59d42014-02-11 11:52:49 -05003793 /*
3794 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003795 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003796 * @kn->priv is RCU safe. Let's do the RCU dancing.
3797 */
3798 rcu_read_lock();
3799 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003800 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003801 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003802 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003803 return -ENOENT;
3804 }
Li Zefanbad34662014-02-14 16:54:28 +08003805 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003806
Tejun Heo9d800df2014-05-14 09:15:00 -04003807 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003808 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003809 switch (tsk->state) {
3810 case TASK_RUNNING:
3811 stats->nr_running++;
3812 break;
3813 case TASK_INTERRUPTIBLE:
3814 stats->nr_sleeping++;
3815 break;
3816 case TASK_UNINTERRUPTIBLE:
3817 stats->nr_uninterruptible++;
3818 break;
3819 case TASK_STOPPED:
3820 stats->nr_stopped++;
3821 break;
3822 default:
3823 if (delayacct_is_task_waiting_on_io(tsk))
3824 stats->nr_io_wait++;
3825 break;
3826 }
3827 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003828 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003829
Li Zefanbad34662014-02-14 16:54:28 +08003830 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003831 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003832}
3833
Paul Menage8f3ff202009-09-23 15:56:25 -07003834
Paul Menagecc31edc2008-10-18 20:28:04 -07003835/*
Ben Blum102a7752009-09-23 15:56:26 -07003836 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003837 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003838 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003839 */
3840
Ben Blum102a7752009-09-23 15:56:26 -07003841static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003842{
3843 /*
3844 * Initially we receive a position value that corresponds to
3845 * one more than the last pid shown (or 0 on the first call or
3846 * after a seek to the start). Use a binary-search to find the
3847 * next pid to display, if any
3848 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003849 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003850 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003851 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003852 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003853 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003854 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003855
Tejun Heo4bac00d2013-11-29 10:42:59 -05003856 mutex_lock(&cgrp->pidlist_mutex);
3857
3858 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003859 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003860 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003861 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003862 * could already have been destroyed.
3863 */
Tejun Heo5d224442013-12-05 12:28:04 -05003864 if (of->priv)
3865 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003866
3867 /*
3868 * Either this is the first start() after open or the matching
3869 * pidlist has been destroyed inbetween. Create a new one.
3870 */
Tejun Heo5d224442013-12-05 12:28:04 -05003871 if (!of->priv) {
3872 ret = pidlist_array_load(cgrp, type,
3873 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003874 if (ret)
3875 return ERR_PTR(ret);
3876 }
Tejun Heo5d224442013-12-05 12:28:04 -05003877 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003878
Paul Menagecc31edc2008-10-18 20:28:04 -07003879 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003880 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003881
Paul Menagecc31edc2008-10-18 20:28:04 -07003882 while (index < end) {
3883 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003884 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003885 index = mid;
3886 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003887 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003888 index = mid + 1;
3889 else
3890 end = mid;
3891 }
3892 }
3893 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003894 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003895 return NULL;
3896 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003897 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003898 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003899 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003900}
3901
Ben Blum102a7752009-09-23 15:56:26 -07003902static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003903{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003904 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003905 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003906
Tejun Heo5d224442013-12-05 12:28:04 -05003907 if (l)
3908 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003909 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003910 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003911}
3912
Ben Blum102a7752009-09-23 15:56:26 -07003913static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003914{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003915 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003916 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003917 pid_t *p = v;
3918 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003919 /*
3920 * Advance to the next pid in the array. If this goes off the
3921 * end, we're done
3922 */
3923 p++;
3924 if (p >= end) {
3925 return NULL;
3926 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003927 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003928 return p;
3929 }
3930}
3931
Ben Blum102a7752009-09-23 15:56:26 -07003932static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003933{
3934 return seq_printf(s, "%d\n", *(int *)v);
3935}
3936
Tejun Heo182446d2013-08-08 20:11:24 -04003937static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3938 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003939{
Tejun Heo182446d2013-08-08 20:11:24 -04003940 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003941}
3942
Tejun Heo182446d2013-08-08 20:11:24 -04003943static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3944 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003945{
Tejun Heo182446d2013-08-08 20:11:24 -04003946 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003947 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003948 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003949 else
Tejun Heo182446d2013-08-08 20:11:24 -04003950 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003951 return 0;
3952}
3953
Tejun Heo182446d2013-08-08 20:11:24 -04003954static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3955 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003956{
Tejun Heo182446d2013-08-08 20:11:24 -04003957 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003958}
3959
Tejun Heo182446d2013-08-08 20:11:24 -04003960static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3961 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003962{
3963 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003964 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003965 else
Tejun Heo182446d2013-08-08 20:11:24 -04003966 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003967 return 0;
3968}
3969
Tejun Heod5c56ce2013-06-03 19:14:34 -07003970static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003971 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003972 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003973 .seq_start = cgroup_pidlist_start,
3974 .seq_next = cgroup_pidlist_next,
3975 .seq_stop = cgroup_pidlist_stop,
3976 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003977 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003978 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003979 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003980 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003981 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003982 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003983 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003984 .read_u64 = cgroup_clone_children_read,
3985 .write_u64 = cgroup_clone_children_write,
3986 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003987 {
Tejun Heo873fe092013-04-14 20:15:26 -07003988 .name = "cgroup.sane_behavior",
3989 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003990 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003991 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003992 {
3993 .name = "cgroup.controllers",
3994 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3995 .seq_show = cgroup_root_controllers_show,
3996 },
3997 {
3998 .name = "cgroup.controllers",
3999 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4000 .seq_show = cgroup_controllers_show,
4001 },
4002 {
4003 .name = "cgroup.subtree_control",
4004 .flags = CFTYPE_ONLY_ON_DFL,
4005 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004006 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004007 },
Tejun Heo842b5972014-04-25 18:28:02 -04004008 {
4009 .name = "cgroup.populated",
4010 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4011 .seq_show = cgroup_populated_show,
4012 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004013
4014 /*
4015 * Historical crazy stuff. These don't have "cgroup." prefix and
4016 * don't exist if sane_behavior. If you're depending on these, be
4017 * prepared to be burned.
4018 */
4019 {
4020 .name = "tasks",
4021 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004022 .seq_start = cgroup_pidlist_start,
4023 .seq_next = cgroup_pidlist_next,
4024 .seq_stop = cgroup_pidlist_stop,
4025 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004026 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004027 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004028 .mode = S_IRUGO | S_IWUSR,
4029 },
4030 {
4031 .name = "notify_on_release",
4032 .flags = CFTYPE_INSANE,
4033 .read_u64 = cgroup_read_notify_on_release,
4034 .write_u64 = cgroup_write_notify_on_release,
4035 },
Tejun Heo873fe092013-04-14 20:15:26 -07004036 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004037 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004038 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004039 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004040 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004041 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004042 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004043 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004044};
4045
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004046/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004047 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004048 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004049 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004050 *
4051 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004052 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004053static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004054{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004055 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004056 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004057
Tejun Heo8e3f6542012-04-01 12:09:55 -07004058 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004059 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004060 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004061
Tejun Heo69dfa002014-05-04 15:09:13 -04004062 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004063 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004064
Tejun Heo0adb0702014-02-12 09:29:48 -05004065 list_for_each_entry(cfts, &ss->cfts, node) {
4066 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004067 if (ret < 0)
4068 goto err;
4069 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004070 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004071 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004072err:
4073 cgroup_clear_dir(cgrp, subsys_mask);
4074 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004075}
4076
Tejun Heo0c21ead2013-08-13 20:22:51 -04004077/*
4078 * css destruction is four-stage process.
4079 *
4080 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4081 * Implemented in kill_css().
4082 *
4083 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004084 * and thus css_tryget_online() is guaranteed to fail, the css can be
4085 * offlined by invoking offline_css(). After offlining, the base ref is
4086 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004087 *
4088 * 3. When the percpu_ref reaches zero, the only possible remaining
4089 * accessors are inside RCU read sections. css_release() schedules the
4090 * RCU callback.
4091 *
4092 * 4. After the grace period, the css can be freed. Implemented in
4093 * css_free_work_fn().
4094 *
4095 * It is actually hairier because both step 2 and 4 require process context
4096 * and thus involve punting to css->destroy_work adding two additional
4097 * steps to the already complex sequence.
4098 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004099static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004100{
4101 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004102 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004103 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004104
Tejun Heo9d755d32014-05-14 09:15:02 -04004105 if (css->ss) {
4106 /* css free path */
4107 if (css->parent)
4108 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004109
Tejun Heo9d755d32014-05-14 09:15:02 -04004110 css->ss->css_free(css);
4111 cgroup_put(cgrp);
4112 } else {
4113 /* cgroup free path */
4114 atomic_dec(&cgrp->root->nr_cgrps);
4115 cgroup_pidlist_destroy_all(cgrp);
4116
Tejun Heod51f39b2014-05-16 13:22:48 -04004117 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004118 /*
4119 * We get a ref to the parent, and put the ref when
4120 * this cgroup is being freed, so it's guaranteed
4121 * that the parent won't be destroyed before its
4122 * children.
4123 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004124 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004125 kernfs_put(cgrp->kn);
4126 kfree(cgrp);
4127 } else {
4128 /*
4129 * This is root cgroup's refcnt reaching zero,
4130 * which indicates that the root should be
4131 * released.
4132 */
4133 cgroup_destroy_root(cgrp->root);
4134 }
4135 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004136}
4137
4138static void css_free_rcu_fn(struct rcu_head *rcu_head)
4139{
4140 struct cgroup_subsys_state *css =
4141 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4142
Tejun Heo0c21ead2013-08-13 20:22:51 -04004143 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004144 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004145}
4146
Tejun Heo25e15d82014-05-14 09:15:02 -04004147static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004148{
4149 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004150 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004151 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004152 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004153
Tejun Heo1fed1b22014-05-16 13:22:49 -04004154 mutex_lock(&cgroup_mutex);
4155
Tejun Heode3f0342014-05-16 13:22:49 -04004156 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004157 list_del_rcu(&css->sibling);
4158
Tejun Heo9d755d32014-05-14 09:15:02 -04004159 if (ss) {
4160 /* css release path */
4161 cgroup_idr_remove(&ss->css_idr, css->id);
4162 } else {
4163 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004164 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4165 cgrp->id = -1;
4166 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004167
Tejun Heo1fed1b22014-05-16 13:22:49 -04004168 mutex_unlock(&cgroup_mutex);
4169
Tejun Heo0c21ead2013-08-13 20:22:51 -04004170 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004171}
4172
Tejun Heo25e15d82014-05-14 09:15:02 -04004173static void css_release(struct percpu_ref *ref)
4174{
4175 struct cgroup_subsys_state *css =
4176 container_of(ref, struct cgroup_subsys_state, refcnt);
4177
4178 INIT_WORK(&css->destroy_work, css_release_work_fn);
4179 queue_work(cgroup_destroy_wq, &css->destroy_work);
4180}
4181
Tejun Heoddfcada2014-05-04 15:09:14 -04004182static void init_and_link_css(struct cgroup_subsys_state *css,
4183 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004185 lockdep_assert_held(&cgroup_mutex);
4186
Tejun Heoddfcada2014-05-04 15:09:14 -04004187 cgroup_get(cgrp);
4188
Tejun Heod5c419b2014-05-16 13:22:48 -04004189 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004190 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004191 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004192 INIT_LIST_HEAD(&css->sibling);
4193 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004194 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004195
Tejun Heod51f39b2014-05-16 13:22:48 -04004196 if (cgroup_parent(cgrp)) {
4197 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004198 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004199 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004200
Tejun Heoca8bdca2013-08-26 18:40:56 -04004201 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202}
4203
Li Zefan2a4ac632013-07-31 16:16:40 +08004204/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004205static int online_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 Heob1929db2012-11-19 08:13:38 -08004208 int ret = 0;
4209
Tejun Heoa31f2d32012-11-19 08:13:37 -08004210 lockdep_assert_held(&cgroup_mutex);
4211
Tejun Heo92fb9742012-11-19 08:13:38 -08004212 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004213 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004214 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004215 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004216 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004217 }
Tejun Heob1929db2012-11-19 08:13:38 -08004218 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004219}
4220
Li Zefan2a4ac632013-07-31 16:16:40 +08004221/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004222static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004223{
Tejun Heo623f9262013-08-13 11:01:55 -04004224 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004225
4226 lockdep_assert_held(&cgroup_mutex);
4227
4228 if (!(css->flags & CSS_ONLINE))
4229 return;
4230
Li Zefand7eeac12013-03-12 15:35:59 -07004231 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004232 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004233
Tejun Heoeb954192013-08-08 20:11:23 -04004234 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004235 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004236
4237 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004238}
4239
Tejun Heoc81c925a2013-12-06 15:11:56 -05004240/**
4241 * create_css - create a cgroup_subsys_state
4242 * @cgrp: the cgroup new css will be associated with
4243 * @ss: the subsys of new css
4244 *
4245 * Create a new css associated with @cgrp - @ss pair. On success, the new
4246 * css is online and installed in @cgrp with all interface files created.
4247 * Returns 0 on success, -errno on failure.
4248 */
4249static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4250{
Tejun Heod51f39b2014-05-16 13:22:48 -04004251 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004252 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004253 struct cgroup_subsys_state *css;
4254 int err;
4255
Tejun Heoc81c925a2013-12-06 15:11:56 -05004256 lockdep_assert_held(&cgroup_mutex);
4257
Tejun Heo1fed1b22014-05-16 13:22:49 -04004258 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004259 if (IS_ERR(css))
4260 return PTR_ERR(css);
4261
Tejun Heoddfcada2014-05-04 15:09:14 -04004262 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004263
Tejun Heoc81c925a2013-12-06 15:11:56 -05004264 err = percpu_ref_init(&css->refcnt, css_release);
4265 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004266 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004267
Tejun Heo15a4c832014-05-04 15:09:14 -04004268 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4269 if (err < 0)
4270 goto err_free_percpu_ref;
4271 css->id = err;
4272
Tejun Heoaec25022014-02-08 10:36:58 -05004273 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004274 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004275 goto err_free_id;
4276
4277 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004278 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004279 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004280
4281 err = online_css(css);
4282 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004283 goto err_list_del;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004284
Tejun Heoc81c925a2013-12-06 15:11:56 -05004285 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004286 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004287 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 -04004288 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004289 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004290 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004291 ss->warned_broken_hierarchy = true;
4292 }
4293
4294 return 0;
4295
Tejun Heo1fed1b22014-05-16 13:22:49 -04004296err_list_del:
4297 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004298 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004299err_free_id:
4300 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004301err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004302 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004303err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004304 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004305 return err;
4306}
4307
Tejun Heob3bfd982014-05-13 12:19:22 -04004308static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4309 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004310{
Tejun Heoa9746d82014-05-13 12:19:22 -04004311 struct cgroup *parent, *cgrp;
4312 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004313 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004314 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004315 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004316
Tejun Heoa9746d82014-05-13 12:19:22 -04004317 parent = cgroup_kn_lock_live(parent_kn);
4318 if (!parent)
4319 return -ENODEV;
4320 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004321
4322 /* allocate the cgroup and its ID, 0 is reserved for the root */
4323 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4324 if (!cgrp) {
4325 ret = -ENOMEM;
4326 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004327 }
4328
Tejun Heo9d755d32014-05-14 09:15:02 -04004329 ret = percpu_ref_init(&cgrp->self.refcnt, css_release);
4330 if (ret)
4331 goto out_free_cgrp;
4332
Li Zefan0ab02ca2014-02-11 16:05:46 +08004333 /*
4334 * Temporarily set the pointer to NULL, so idr_find() won't return
4335 * a half-baked cgroup.
4336 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004337 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004338 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004339 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004340 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004341 }
4342
Paul Menagecc31edc2008-10-18 20:28:04 -07004343 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344
Tejun Heo9d800df2014-05-14 09:15:00 -04004345 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004346 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347
Li Zefanb6abdb02008-03-04 14:28:19 -08004348 if (notify_on_release(parent))
4349 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4350
Tejun Heo2260e7f2012-11-19 08:13:38 -08004351 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4352 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004353
Tejun Heo2bd59d42014-02-11 11:52:49 -05004354 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004355 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004356 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004357 ret = PTR_ERR(kn);
4358 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004359 }
4360 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361
Tejun Heo6f305582014-02-12 09:29:50 -05004362 /*
4363 * This extra ref will be put in cgroup_free_fn() and guarantees
4364 * that @cgrp->kn is always accessible.
4365 */
4366 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367
Tejun Heo0cb51d72014-05-16 13:22:49 -04004368 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004369
Tejun Heo4e139af2012-11-19 08:13:36 -08004370 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004371 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004372 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004373 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004374
Tejun Heo0d802552013-12-06 15:11:56 -05004375 /*
4376 * @cgrp is now fully operational. If something fails after this
4377 * point, it'll be released via the normal destruction path.
4378 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004379 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004380
Tejun Heoba0f4d72014-05-13 12:19:22 -04004381 ret = cgroup_kn_set_ugid(kn);
4382 if (ret)
4383 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004384
Tejun Heoba0f4d72014-05-13 12:19:22 -04004385 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4386 if (ret)
4387 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004388
Tejun Heo9d403e92013-12-06 15:11:56 -05004389 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004390 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004391 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004392 ret = create_css(cgrp, ss);
4393 if (ret)
4394 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004395 }
Tejun Heoa8638032012-11-09 09:12:29 -08004396 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004397
Tejun Heobd53d612014-04-23 11:13:16 -04004398 /*
4399 * On the default hierarchy, a child doesn't automatically inherit
4400 * child_subsys_mask from the parent. Each is configured manually.
4401 */
4402 if (!cgroup_on_dfl(cgrp))
4403 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004404
Tejun Heo2bd59d42014-02-11 11:52:49 -05004405 kernfs_activate(kn);
4406
Tejun Heoba0f4d72014-05-13 12:19:22 -04004407 ret = 0;
4408 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004409
Tejun Heoba0f4d72014-05-13 12:19:22 -04004410out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004411 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004412out_cancel_ref:
4413 percpu_ref_cancel_init(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004414out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004415 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004416out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004417 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004418 return ret;
4419
4420out_destroy:
4421 cgroup_destroy_locked(cgrp);
4422 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423}
4424
Tejun Heo223dbc32013-08-13 20:22:50 -04004425/*
4426 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004427 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4428 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004429 */
4430static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004431{
Tejun Heo223dbc32013-08-13 20:22:50 -04004432 struct cgroup_subsys_state *css =
4433 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004434
Tejun Heof20104d2013-08-13 20:22:50 -04004435 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004436 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004437 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004438
Tejun Heo09a503ea2013-08-13 20:22:50 -04004439 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004440}
4441
Tejun Heo223dbc32013-08-13 20:22:50 -04004442/* css kill confirmation processing requires process context, bounce */
4443static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004444{
4445 struct cgroup_subsys_state *css =
4446 container_of(ref, struct cgroup_subsys_state, refcnt);
4447
Tejun Heo223dbc32013-08-13 20:22:50 -04004448 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004449 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004450}
4451
Tejun Heof392e512014-04-23 11:13:14 -04004452/**
4453 * kill_css - destroy a css
4454 * @css: css to destroy
4455 *
4456 * This function initiates destruction of @css by removing cgroup interface
4457 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004458 * asynchronously once css_tryget_online() is guaranteed to fail and when
4459 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004460 */
4461static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004462{
Tejun Heo01f64742014-05-13 12:19:23 -04004463 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004464
Tejun Heo2bd59d42014-02-11 11:52:49 -05004465 /*
4466 * This must happen before css is disassociated with its cgroup.
4467 * See seq_css() for details.
4468 */
Tejun Heoaec25022014-02-08 10:36:58 -05004469 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004470
Tejun Heoedae0c32013-08-13 20:22:51 -04004471 /*
4472 * Killing would put the base ref, but we need to keep it alive
4473 * until after ->css_offline().
4474 */
4475 css_get(css);
4476
4477 /*
4478 * cgroup core guarantees that, by the time ->css_offline() is
4479 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004480 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004481 * proceed to offlining css's because percpu_ref_kill() doesn't
4482 * guarantee that the ref is seen as killed on all CPUs on return.
4483 *
4484 * Use percpu_ref_kill_and_confirm() to get notifications as each
4485 * css is confirmed to be seen as killed on all CPUs.
4486 */
4487 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004488}
4489
4490/**
4491 * cgroup_destroy_locked - the first stage of cgroup destruction
4492 * @cgrp: cgroup to be destroyed
4493 *
4494 * css's make use of percpu refcnts whose killing latency shouldn't be
4495 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004496 * guarantee that css_tryget_online() won't succeed by the time
4497 * ->css_offline() is invoked. To satisfy all the requirements,
4498 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004499 *
4500 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4501 * userland visible parts and start killing the percpu refcnts of
4502 * css's. Set up so that the next stage will be kicked off once all
4503 * the percpu refcnts are confirmed to be killed.
4504 *
4505 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4506 * rest of destruction. Once all cgroup references are gone, the
4507 * cgroup is RCU-freed.
4508 *
4509 * This function implements s1. After this step, @cgrp is gone as far as
4510 * the userland is concerned and a new cgroup with the same name may be
4511 * created. As cgroup doesn't care about the names internally, this
4512 * doesn't cause any problem.
4513 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004514static int cgroup_destroy_locked(struct cgroup *cgrp)
4515 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004516{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004517 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004518 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004519 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004520
Tejun Heo42809dd2012-11-19 08:13:37 -08004521 lockdep_assert_held(&cgroup_mutex);
4522
Tejun Heoddd69142013-06-12 21:04:54 -07004523 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004524 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004525 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004526 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004527 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004528 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004529 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004530 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004531 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004532
Tejun Heo1a90dd52012-11-05 09:16:59 -08004533 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004534 * Make sure there's no live children. We can't test emptiness of
4535 * ->self.children as dead children linger on it while being
4536 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004537 */
Tejun Heocbc125e2014-05-14 09:15:01 -04004538 if (cgroup_has_live_children(cgrp))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004539 return -EBUSY;
4540
4541 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004542 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004543 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004544 */
Tejun Heo184faf32014-05-16 13:22:51 -04004545 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004546
Tejun Heo249f3462014-05-14 09:15:01 -04004547 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004548 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004549 kill_css(css);
4550
Tejun Heo184faf32014-05-16 13:22:51 -04004551 /* CSS_ONLINE is clear, remove from ->release_list for the last time */
Tejun Heo455050d2013-06-13 19:27:41 -07004552 raw_spin_lock(&release_list_lock);
4553 if (!list_empty(&cgrp->release_list))
4554 list_del_init(&cgrp->release_list);
4555 raw_spin_unlock(&release_list_lock);
4556
4557 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004558 * Remove @cgrp directory along with the base files. @cgrp has an
4559 * extra ref on its kn.
4560 */
4561 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004562
Tejun Heod51f39b2014-05-16 13:22:48 -04004563 set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags);
4564 check_for_release(cgroup_parent(cgrp));
Tejun Heo9e4173e2014-05-14 09:15:01 -04004565
Tejun Heo249f3462014-05-14 09:15:01 -04004566 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004567 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo249f3462014-05-14 09:15:01 -04004568
Tejun Heoea15f8c2013-06-13 19:27:42 -07004569 return 0;
4570};
4571
Tejun Heo2bd59d42014-02-11 11:52:49 -05004572static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004573{
Tejun Heoa9746d82014-05-13 12:19:22 -04004574 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004575 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004576
Tejun Heoa9746d82014-05-13 12:19:22 -04004577 cgrp = cgroup_kn_lock_live(kn);
4578 if (!cgrp)
4579 return 0;
4580 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004581
Tejun Heoa9746d82014-05-13 12:19:22 -04004582 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004583
Tejun Heoa9746d82014-05-13 12:19:22 -04004584 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004585
4586 /*
4587 * There are two control paths which try to determine cgroup from
4588 * dentry without going through kernfs - cgroupstats_build() and
4589 * css_tryget_online_from_dir(). Those are supported by RCU
4590 * protecting clearing of cgrp->kn->priv backpointer, which should
4591 * happen after all files under it have been removed.
4592 */
4593 if (!ret)
4594 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4595
Tejun Heo2bd59d42014-02-11 11:52:49 -05004596 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004597 return ret;
4598}
4599
Tejun Heo2bd59d42014-02-11 11:52:49 -05004600static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4601 .remount_fs = cgroup_remount,
4602 .show_options = cgroup_show_options,
4603 .mkdir = cgroup_mkdir,
4604 .rmdir = cgroup_rmdir,
4605 .rename = cgroup_rename,
4606};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004607
Tejun Heo15a4c832014-05-04 15:09:14 -04004608static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004611
4612 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613
Tejun Heo648bb562012-11-19 08:13:36 -08004614 mutex_lock(&cgroup_mutex);
4615
Tejun Heo15a4c832014-05-04 15:09:14 -04004616 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004617 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004618
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004619 /* Create the root cgroup state for this subsystem */
4620 ss->root = &cgrp_dfl_root;
4621 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004622 /* We don't handle early failures gracefully */
4623 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004624 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004625
4626 /*
4627 * Root csses are never destroyed and we can't initialize
4628 * percpu_ref during early init. Disable refcnting.
4629 */
4630 css->flags |= CSS_NO_REF;
4631
Tejun Heo15a4c832014-05-04 15:09:14 -04004632 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004633 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004634 css->id = 1;
4635 } else {
4636 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4637 BUG_ON(css->id < 0);
4638 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639
Li Zefane8d55fd2008-04-29 01:00:13 -07004640 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004641 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004642 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004643 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004644 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645
4646 need_forkexit_callback |= ss->fork || ss->exit;
4647
Li Zefane8d55fd2008-04-29 01:00:13 -07004648 /* At system boot, before all subsystems have been
4649 * registered, no tasks have been forked, so we don't
4650 * need to invoke fork callbacks here. */
4651 BUG_ON(!list_empty(&init_task.tasks));
4652
Tejun Heoae7f1642013-08-13 20:22:50 -04004653 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004654
Tejun Heof392e512014-04-23 11:13:14 -04004655 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004656
Ben Blume6a11052010-03-10 15:22:09 -08004657 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658}
4659
4660/**
Li Zefana043e3b2008-02-23 15:24:09 -08004661 * cgroup_init_early - cgroup initialization at system boot
4662 *
4663 * Initialize cgroups at system boot, and initialize any
4664 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004665 */
4666int __init cgroup_init_early(void)
4667{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004668 static struct cgroup_sb_opts __initdata opts =
4669 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004670 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004672
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004673 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004674 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4675
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004676 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004677
Tejun Heo3ed80a62014-02-08 10:36:58 -05004678 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004679 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004680 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4681 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004682 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004683 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4684 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685
Tejun Heoaec25022014-02-08 10:36:58 -05004686 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004687 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688
4689 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004690 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691 }
4692 return 0;
4693}
4694
4695/**
Li Zefana043e3b2008-02-23 15:24:09 -08004696 * cgroup_init - cgroup initialization
4697 *
4698 * Register cgroup filesystem and /proc file, and initialize
4699 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004700 */
4701int __init cgroup_init(void)
4702{
Tejun Heo30159ec2013-06-25 11:53:37 -07004703 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004704 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004705 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004706
Tejun Heo2bd59d42014-02-11 11:52:49 -05004707 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004708
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004709 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004710
Tejun Heo82fe9b02013-06-25 11:53:37 -07004711 /* Add init_css_set to the hash table */
4712 key = css_set_hash(init_css_set.subsys);
4713 hash_add(css_set_table, &init_css_set.hlist, key);
4714
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004715 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004716
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004717 mutex_unlock(&cgroup_mutex);
4718
Tejun Heo172a2c062014-03-19 10:23:53 -04004719 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004720 if (ss->early_init) {
4721 struct cgroup_subsys_state *css =
4722 init_css_set.subsys[ss->id];
4723
4724 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4725 GFP_KERNEL);
4726 BUG_ON(css->id < 0);
4727 } else {
4728 cgroup_init_subsys(ss, false);
4729 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004730
Tejun Heo2d8f2432014-04-23 11:13:15 -04004731 list_add_tail(&init_css_set.e_cset_node[ssid],
4732 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4733
Tejun Heo172a2c062014-03-19 10:23:53 -04004734 /*
4735 * cftype registration needs kmalloc and can't be done
4736 * during early_init. Register base cftypes separately.
4737 */
4738 if (ss->base_cftypes)
4739 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4740 }
Greg KH676db4a2010-08-05 13:53:35 -07004741
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004743 if (!cgroup_kobj)
4744 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004745
4746 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004747 if (err < 0) {
4748 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004749 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004750 }
4751
4752 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004753 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004754}
Paul Menageb4f48b62007-10-18 23:39:33 -07004755
Tejun Heoe5fca242013-11-22 17:14:39 -05004756static int __init cgroup_wq_init(void)
4757{
4758 /*
4759 * There isn't much point in executing destruction path in
4760 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004761 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004762 *
4763 * We would prefer to do this in cgroup_init() above, but that
4764 * is called before init_workqueues(): so leave this until after.
4765 */
Tejun Heo1a115332014-02-12 19:06:19 -05004766 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004767 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004768
4769 /*
4770 * Used to destroy pidlists and separate to serve as flush domain.
4771 * Cap @max_active to 1 too.
4772 */
4773 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4774 0, 1);
4775 BUG_ON(!cgroup_pidlist_destroy_wq);
4776
Tejun Heoe5fca242013-11-22 17:14:39 -05004777 return 0;
4778}
4779core_initcall(cgroup_wq_init);
4780
Paul Menagea4243162007-10-18 23:39:35 -07004781/*
4782 * proc_cgroup_show()
4783 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4784 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004785 */
4786
4787/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004788int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004789{
4790 struct pid *pid;
4791 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004792 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004793 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004794 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004795
4796 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004797 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004798 if (!buf)
4799 goto out;
4800
4801 retval = -ESRCH;
4802 pid = m->private;
4803 tsk = get_pid_task(pid, PIDTYPE_PID);
4804 if (!tsk)
4805 goto out_free;
4806
4807 retval = 0;
4808
4809 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004810 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004811
Tejun Heo985ed672014-03-19 10:23:53 -04004812 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004813 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004814 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004815 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004816
Tejun Heoa2dd4242014-03-19 10:23:55 -04004817 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004818 continue;
4819
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004820 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004821 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004822 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004823 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004824 if (strlen(root->name))
4825 seq_printf(m, "%sname=%s", count ? "," : "",
4826 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004827 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004828 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004829 path = cgroup_path(cgrp, buf, PATH_MAX);
4830 if (!path) {
4831 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004832 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004833 }
4834 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004835 seq_putc(m, '\n');
4836 }
4837
4838out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004839 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004840 mutex_unlock(&cgroup_mutex);
4841 put_task_struct(tsk);
4842out_free:
4843 kfree(buf);
4844out:
4845 return retval;
4846}
4847
Paul Menagea4243162007-10-18 23:39:35 -07004848/* Display information about each subsystem and each hierarchy */
4849static int proc_cgroupstats_show(struct seq_file *m, void *v)
4850{
Tejun Heo30159ec2013-06-25 11:53:37 -07004851 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004852 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004853
Paul Menage8bab8dd2008-04-04 14:29:57 -07004854 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004855 /*
4856 * ideally we don't want subsystems moving around while we do this.
4857 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4858 * subsys/hierarchy state.
4859 */
Paul Menagea4243162007-10-18 23:39:35 -07004860 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004861
4862 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004863 seq_printf(m, "%s\t%d\t%d\t%d\n",
4864 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004865 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004866
Paul Menagea4243162007-10-18 23:39:35 -07004867 mutex_unlock(&cgroup_mutex);
4868 return 0;
4869}
4870
4871static int cgroupstats_open(struct inode *inode, struct file *file)
4872{
Al Viro9dce07f2008-03-29 03:07:28 +00004873 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004874}
4875
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004876static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004877 .open = cgroupstats_open,
4878 .read = seq_read,
4879 .llseek = seq_lseek,
4880 .release = single_release,
4881};
4882
Paul Menageb4f48b62007-10-18 23:39:33 -07004883/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004884 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004885 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004886 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004887 * A task is associated with the init_css_set until cgroup_post_fork()
4888 * attaches it to the parent's css_set. Empty cg_list indicates that
4889 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004890 */
4891void cgroup_fork(struct task_struct *child)
4892{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004893 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004894 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004895}
4896
4897/**
Li Zefana043e3b2008-02-23 15:24:09 -08004898 * cgroup_post_fork - called on a new task after adding it to the task list
4899 * @child: the task in question
4900 *
Tejun Heo5edee612012-10-16 15:03:14 -07004901 * Adds the task to the list running through its css_set if necessary and
4902 * call the subsystem fork() callbacks. Has to be after the task is
4903 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004904 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004905 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004906 */
Paul Menage817929e2007-10-18 23:39:36 -07004907void cgroup_post_fork(struct task_struct *child)
4908{
Tejun Heo30159ec2013-06-25 11:53:37 -07004909 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004910 int i;
4911
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004912 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004913 * This may race against cgroup_enable_task_cg_links(). As that
4914 * function sets use_task_css_set_links before grabbing
4915 * tasklist_lock and we just went through tasklist_lock to add
4916 * @child, it's guaranteed that either we see the set
4917 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4918 * @child during its iteration.
4919 *
4920 * If we won the race, @child is associated with %current's
4921 * css_set. Grabbing css_set_rwsem guarantees both that the
4922 * association is stable, and, on completion of the parent's
4923 * migration, @child is visible in the source of migration or
4924 * already in the destination cgroup. This guarantee is necessary
4925 * when implementing operations which need to migrate all tasks of
4926 * a cgroup to another.
4927 *
4928 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4929 * will remain in init_css_set. This is safe because all tasks are
4930 * in the init_css_set before cg_links is enabled and there's no
4931 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004932 */
Paul Menage817929e2007-10-18 23:39:36 -07004933 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004934 struct css_set *cset;
4935
Tejun Heo96d365e2014-02-13 06:58:40 -05004936 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004937 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004938 if (list_empty(&child->cg_list)) {
4939 rcu_assign_pointer(child->cgroups, cset);
4940 list_add(&child->cg_list, &cset->tasks);
4941 get_css_set(cset);
4942 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004943 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004944 }
Tejun Heo5edee612012-10-16 15:03:14 -07004945
4946 /*
4947 * Call ss->fork(). This must happen after @child is linked on
4948 * css_set; otherwise, @child might change state between ->fork()
4949 * and addition to css_set.
4950 */
4951 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004952 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004953 if (ss->fork)
4954 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004955 }
Paul Menage817929e2007-10-18 23:39:36 -07004956}
Tejun Heo5edee612012-10-16 15:03:14 -07004957
Paul Menage817929e2007-10-18 23:39:36 -07004958/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004959 * cgroup_exit - detach cgroup from exiting task
4960 * @tsk: pointer to task_struct of exiting process
4961 *
4962 * Description: Detach cgroup from @tsk and release it.
4963 *
4964 * Note that cgroups marked notify_on_release force every task in
4965 * them to take the global cgroup_mutex mutex when exiting.
4966 * This could impact scaling on very large systems. Be reluctant to
4967 * use notify_on_release cgroups where very high task exit scaling
4968 * is required on large systems.
4969 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004970 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4971 * call cgroup_exit() while the task is still competent to handle
4972 * notify_on_release(), then leave the task attached to the root cgroup in
4973 * each hierarchy for the remainder of its exit. No need to bother with
4974 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004975 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004976 */
Li Zefan1ec41832014-03-28 15:22:19 +08004977void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004978{
Tejun Heo30159ec2013-06-25 11:53:37 -07004979 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004980 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004981 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004982 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004983
4984 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004985 * Unlink from @tsk from its css_set. As migration path can't race
4986 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004987 */
4988 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004989 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004990 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004991 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004992 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004993 }
4994
Paul Menageb4f48b62007-10-18 23:39:33 -07004995 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004996 cset = task_css_set(tsk);
4997 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004998
Li Zefan1ec41832014-03-28 15:22:19 +08004999 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005000 /* see cgroup_post_fork() for details */
5001 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005002 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005003 struct cgroup_subsys_state *old_css = cset->subsys[i];
5004 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005005
Tejun Heoeb954192013-08-08 20:11:23 -04005006 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005007 }
5008 }
5009 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005010
Tejun Heoeaf797a2014-02-25 10:04:03 -05005011 if (put_cset)
5012 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005013}
Paul Menage697f4162007-10-18 23:39:34 -07005014
Paul Menagebd89aab2007-10-18 23:40:44 -07005015static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005016{
Li Zefanf50daa72013-03-01 15:06:07 +08005017 if (cgroup_is_releasable(cgrp) &&
Tejun Heo9e4173e2014-05-14 09:15:01 -04005018 list_empty(&cgrp->cset_links) && !cgroup_has_live_children(cgrp)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005019 /*
5020 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005022 * it now
5023 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005025
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005026 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005027 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005028 list_empty(&cgrp->release_list)) {
5029 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005030 need_schedule_work = 1;
5031 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005032 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005033 if (need_schedule_work)
5034 schedule_work(&release_agent_work);
5035 }
5036}
5037
Paul Menage81a6a5c2007-10-18 23:39:38 -07005038/*
5039 * Notify userspace when a cgroup is released, by running the
5040 * configured release agent with the name of the cgroup (path
5041 * relative to the root of cgroup file system) as the argument.
5042 *
5043 * Most likely, this user command will try to rmdir this cgroup.
5044 *
5045 * This races with the possibility that some other task will be
5046 * attached to this cgroup before it is removed, or that some other
5047 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5048 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5049 * unused, and this cgroup will be reprieved from its death sentence,
5050 * to continue to serve a useful existence. Next time it's released,
5051 * we will get notified again, if it still has 'notify_on_release' set.
5052 *
5053 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5054 * means only wait until the task is successfully execve()'d. The
5055 * separate release agent task is forked by call_usermodehelper(),
5056 * then control in this thread returns here, without waiting for the
5057 * release agent task. We don't bother to wait because the caller of
5058 * this routine has no use for the exit status of the release agent
5059 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005061static void cgroup_release_agent(struct work_struct *work)
5062{
5063 BUG_ON(work != &release_agent_work);
5064 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005065 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005066 while (!list_empty(&release_list)) {
5067 char *argv[3], *envp[3];
5068 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005069 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005070 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005071 struct cgroup,
5072 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005073 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005074 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005075 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07005076 if (!pathbuf)
5077 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005078 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5079 if (!path)
Paul Menagee788e0662008-07-25 01:46:59 -07005080 goto continue_free;
5081 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5082 if (!agentbuf)
5083 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005084
5085 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07005086 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005087 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088 argv[i] = NULL;
5089
5090 i = 0;
5091 /* minimal command environment */
5092 envp[i++] = "HOME=/";
5093 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5094 envp[i] = NULL;
5095
5096 /* Drop the lock while we invoke the usermode helper,
5097 * since the exec could involve hitting disk and hence
5098 * be a slow process */
5099 mutex_unlock(&cgroup_mutex);
5100 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005101 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07005102 continue_free:
5103 kfree(pathbuf);
5104 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005105 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005106 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005107 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005108 mutex_unlock(&cgroup_mutex);
5109}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005110
5111static int __init cgroup_disable(char *str)
5112{
Tejun Heo30159ec2013-06-25 11:53:37 -07005113 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005114 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005115 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005116
5117 while ((token = strsep(&str, ",")) != NULL) {
5118 if (!*token)
5119 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005120
Tejun Heo3ed80a62014-02-08 10:36:58 -05005121 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005122 if (!strcmp(token, ss->name)) {
5123 ss->disabled = 1;
5124 printk(KERN_INFO "Disabling %s control group"
5125 " subsystem\n", ss->name);
5126 break;
5127 }
5128 }
5129 }
5130 return 1;
5131}
5132__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005133
Tejun Heob77d7b62013-08-13 11:01:54 -04005134/**
Tejun Heoec903c02014-05-13 12:11:01 -04005135 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005136 * @dentry: directory dentry of interest
5137 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005138 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005139 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5140 * to get the corresponding css and return it. If such css doesn't exist
5141 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005142 */
Tejun Heoec903c02014-05-13 12:11:01 -04005143struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5144 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005145{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005146 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5147 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005148 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005149
Tejun Heo35cf0832013-08-26 18:40:56 -04005150 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005151 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5152 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005153 return ERR_PTR(-EBADF);
5154
Tejun Heo5a17f542014-02-11 11:52:47 -05005155 rcu_read_lock();
5156
Tejun Heo2bd59d42014-02-11 11:52:49 -05005157 /*
5158 * This path doesn't originate from kernfs and @kn could already
5159 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005160 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005161 */
5162 cgrp = rcu_dereference(kn->priv);
5163 if (cgrp)
5164 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005165
Tejun Heoec903c02014-05-13 12:11:01 -04005166 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005167 css = ERR_PTR(-ENOENT);
5168
5169 rcu_read_unlock();
5170 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005171}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005172
Li Zefan1cb650b2013-08-19 10:05:24 +08005173/**
5174 * css_from_id - lookup css by id
5175 * @id: the cgroup id
5176 * @ss: cgroup subsys to be looked into
5177 *
5178 * Returns the css if there's valid one with @id, otherwise returns NULL.
5179 * Should be called under rcu_read_lock().
5180 */
5181struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5182{
Tejun Heo6fa49182014-05-04 15:09:13 -04005183 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005184 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005185}
5186
Paul Menagefe693432009-09-23 15:56:20 -07005187#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005188static struct cgroup_subsys_state *
5189debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005190{
5191 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5192
5193 if (!css)
5194 return ERR_PTR(-ENOMEM);
5195
5196 return css;
5197}
5198
Tejun Heoeb954192013-08-08 20:11:23 -04005199static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005200{
Tejun Heoeb954192013-08-08 20:11:23 -04005201 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005202}
5203
Tejun Heo182446d2013-08-08 20:11:24 -04005204static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5205 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005206{
Tejun Heo182446d2013-08-08 20:11:24 -04005207 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005208}
5209
Tejun Heo182446d2013-08-08 20:11:24 -04005210static u64 current_css_set_read(struct cgroup_subsys_state *css,
5211 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005212{
5213 return (u64)(unsigned long)current->cgroups;
5214}
5215
Tejun Heo182446d2013-08-08 20:11:24 -04005216static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005217 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005218{
5219 u64 count;
5220
5221 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005222 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005223 rcu_read_unlock();
5224 return count;
5225}
5226
Tejun Heo2da8ca82013-12-05 12:28:04 -05005227static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005228{
Tejun Heo69d02062013-06-12 21:04:50 -07005229 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005230 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005231 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005232
Tejun Heoe61734c2014-02-12 09:29:50 -05005233 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5234 if (!name_buf)
5235 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005236
Tejun Heo96d365e2014-02-13 06:58:40 -05005237 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005238 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005239 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005240 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005241 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005242
Tejun Heoa2dd4242014-03-19 10:23:55 -04005243 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005244 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005245 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005246 }
5247 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005248 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005249 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005250 return 0;
5251}
5252
5253#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005254static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005255{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005256 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005257 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005258
Tejun Heo96d365e2014-02-13 06:58:40 -05005259 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005260 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005261 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005262 struct task_struct *task;
5263 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005264
Tejun Heo5abb8852013-06-12 21:04:49 -07005265 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005266
Tejun Heo5abb8852013-06-12 21:04:49 -07005267 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005268 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5269 goto overflow;
5270 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005271 }
Tejun Heoc7561122014-02-25 10:04:01 -05005272
5273 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5274 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5275 goto overflow;
5276 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5277 }
5278 continue;
5279 overflow:
5280 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005281 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005282 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005283 return 0;
5284}
5285
Tejun Heo182446d2013-08-08 20:11:24 -04005286static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005287{
Tejun Heo182446d2013-08-08 20:11:24 -04005288 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005289}
5290
5291static struct cftype debug_files[] = {
5292 {
Paul Menagefe693432009-09-23 15:56:20 -07005293 .name = "taskcount",
5294 .read_u64 = debug_taskcount_read,
5295 },
5296
5297 {
5298 .name = "current_css_set",
5299 .read_u64 = current_css_set_read,
5300 },
5301
5302 {
5303 .name = "current_css_set_refcount",
5304 .read_u64 = current_css_set_refcount_read,
5305 },
5306
5307 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005308 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005309 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005310 },
5311
5312 {
5313 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005314 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005315 },
5316
5317 {
Paul Menagefe693432009-09-23 15:56:20 -07005318 .name = "releasable",
5319 .read_u64 = releasable_read,
5320 },
Paul Menagefe693432009-09-23 15:56:20 -07005321
Tejun Heo4baf6e32012-04-01 12:09:55 -07005322 { } /* terminate */
5323};
Paul Menagefe693432009-09-23 15:56:20 -07005324
Tejun Heo073219e2014-02-08 10:36:58 -05005325struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005326 .css_alloc = debug_css_alloc,
5327 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005328 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005329};
5330#endif /* CONFIG_CGROUP_DEBUG */