blob: d5af128ec1ecb3d6cd4927fa91576b1206d8f4cc [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 Heo54766d42013-06-12 21:04:53 -0700281 return test_bit(CGRP_DEAD, &cgrp->flags);
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 Gleixnercdcc1362009-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 Lezcano32a8cf22010-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 Lezcano32a8cf22010-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 Lezcano32a8cf22010-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 Lezcano32a8cf22010-10-27 15:33:37 -07001285 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001286 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-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 Lezcano32a8cf22010-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 Lezcano32a8cf22010-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 Lezcano32a8cf22010-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 Lezcano32a8cf22010-10-27 15:33:37 -07001329
1330 continue;
1331 }
1332
Tejun Heo30159ec2013-06-25 11:53:37 -07001333 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-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 Lezcano32a8cf22010-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 Rozanskia1a71b452012-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 Heo2d8f2432014-04-23 11:13:15 -04001521
1522 for_each_subsys(ss, ssid)
1523 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001524
1525 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001526}
Paul Menagec6d57f32009-09-23 15:56:19 -07001527
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001528static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001529 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001531 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001532
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001534 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001535 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001536 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001537 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538
Paul Menagec6d57f32009-09-23 15:56:19 -07001539 root->flags = opts->flags;
1540 if (opts->release_agent)
1541 strcpy(root->release_agent_path, opts->release_agent);
1542 if (opts->name)
1543 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001544 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001545 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001546}
1547
Tejun Heo69dfa002014-05-04 15:09:13 -04001548static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001549{
Tejun Heod427dfe2014-02-11 11:52:48 -05001550 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001551 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001552 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001553 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001554
Tejun Heod427dfe2014-02-11 11:52:48 -05001555 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001556
Tejun Heo6fa49182014-05-04 15:09:13 -04001557 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001558 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001559 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001560 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001561
Tejun Heo9d755d32014-05-14 09:15:02 -04001562 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release);
1563 if (ret)
1564 goto out;
1565
Tejun Heod427dfe2014-02-11 11:52:48 -05001566 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001567 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001568 * but that's OK - it can only be increased by someone holding
1569 * cgroup_lock, and that's us. The worst that can happen is that we
1570 * have some link structures left over
1571 */
1572 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001574 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
Tejun Heo985ed672014-03-19 10:23:53 -04001576 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001577 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001578 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579
Tejun Heo2bd59d42014-02-11 11:52:49 -05001580 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1581 KERNFS_ROOT_CREATE_DEACTIVATED,
1582 root_cgrp);
1583 if (IS_ERR(root->kf_root)) {
1584 ret = PTR_ERR(root->kf_root);
1585 goto exit_root_id;
1586 }
1587 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588
Tejun Heod427dfe2014-02-11 11:52:48 -05001589 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1590 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001591 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
Tejun Heo5df36032014-03-19 10:23:54 -04001593 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001594 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001595 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001596
Tejun Heod427dfe2014-02-11 11:52:48 -05001597 /*
1598 * There must be no failure case after here, since rebinding takes
1599 * care of subsystems' refcounts, which are explicitly dropped in
1600 * the failure exit path.
1601 */
1602 list_add(&root->root_list, &cgroup_roots);
1603 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604
Tejun Heod427dfe2014-02-11 11:52:48 -05001605 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001606 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 * objects.
1608 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001609 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001610 hash_for_each(css_set_table, i, cset, hlist)
1611 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001612 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Tejun Heod5c419b2014-05-16 13:22:48 -04001614 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001615 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001616
Tejun Heo2bd59d42014-02-11 11:52:49 -05001617 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001618 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001619 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001620
Tejun Heo2bd59d42014-02-11 11:52:49 -05001621destroy_root:
1622 kernfs_destroy_root(root->kf_root);
1623 root->kf_root = NULL;
1624exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001625 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001626cancel_ref:
1627 percpu_ref_cancel_init(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001628out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001629 free_cgrp_cset_links(&tmp_links);
1630 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631}
1632
Al Virof7e83572010-07-26 13:23:11 +04001633static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001635 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001637 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001639 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001640 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001641 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001642
1643 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001644 * The first time anyone tries to mount a cgroup, enable the list
1645 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001646 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001647 if (!use_task_css_set_links)
1648 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001649
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001650 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001655 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001656
Tejun Heo2bd59d42014-02-11 11:52:49 -05001657 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001658 if (!opts.subsys_mask && !opts.none && !opts.name) {
1659 cgrp_dfl_root_visible = true;
1660 root = &cgrp_dfl_root;
1661 cgroup_get(&root->cgrp);
1662 ret = 0;
1663 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 }
1665
Tejun Heo985ed672014-03-19 10:23:53 -04001666 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001667 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001669 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001670 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001671
Paul Menage817929e2007-10-18 23:39:36 -07001672 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001673 * If we asked for a name then it must match. Also, if
1674 * name matches but sybsys_mask doesn't, we should fail.
1675 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001676 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001677 if (opts.name) {
1678 if (strcmp(opts.name, root->name))
1679 continue;
1680 name_match = true;
1681 }
Tejun Heo31261212013-06-28 17:07:30 -07001682
1683 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001684 * If we asked for subsystems (or explicitly for no
1685 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001686 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001687 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001688 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001689 if (!name_match)
1690 continue;
1691 ret = -EBUSY;
1692 goto out_unlock;
1693 }
Tejun Heo873fe092013-04-14 20:15:26 -07001694
Tejun Heoc7ba8282013-06-29 14:06:10 -07001695 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001696 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001697 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001698 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001699 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001700 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001701 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001702 }
Tejun Heo873fe092013-04-14 20:15:26 -07001703 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001704
Tejun Heo776f02f2014-02-12 09:29:50 -05001705 /*
Tejun Heo9d755d32014-05-14 09:15:02 -04001706 * A root's lifetime is governed by its root cgroup.
1707 * tryget_live failure indicate that the root is being
1708 * destroyed. Wait for destruction to complete so that the
1709 * subsystems are free. We can use wait_queue for the wait
1710 * but this path is super cold. Let's just sleep for a bit
1711 * and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001712 */
Tejun Heo9d755d32014-05-14 09:15:02 -04001713 if (!percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001714 mutex_unlock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001715 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001716 ret = restart_syscall();
1717 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001718 }
1719
1720 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001721 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722 }
1723
Tejun Heo172a2c062014-03-19 10:23:53 -04001724 /*
1725 * No such thing, create a new one. name= matching without subsys
1726 * specification is allowed for already existing hierarchies but we
1727 * can't create new one without subsys specification.
1728 */
1729 if (!opts.subsys_mask && !opts.none) {
1730 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001731 goto out_unlock;
1732 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733
Tejun Heo172a2c062014-03-19 10:23:53 -04001734 root = kzalloc(sizeof(*root), GFP_KERNEL);
1735 if (!root) {
1736 ret = -ENOMEM;
1737 goto out_unlock;
1738 }
1739
1740 init_cgroup_root(root, &opts);
1741
Tejun Heo35585572014-02-13 06:58:38 -05001742 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001743 if (ret)
1744 cgroup_free_root(root);
1745
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001746out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001747 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001748out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001749 kfree(opts.release_agent);
1750 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001751
Tejun Heo2bd59d42014-02-11 11:52:49 -05001752 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001753 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001754
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001755 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1756 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001757 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001758 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759}
1760
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001761static void cgroup_kill_sb(struct super_block *sb)
1762{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001763 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001764 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765
Tejun Heo9d755d32014-05-14 09:15:02 -04001766 /*
1767 * If @root doesn't have any mounts or children, start killing it.
1768 * This prevents new mounts by disabling percpu_ref_tryget_live().
1769 * cgroup_mount() may wait for @root's release.
1770 */
1771 if (cgroup_has_live_children(&root->cgrp))
1772 cgroup_put(&root->cgrp);
1773 else
1774 percpu_ref_kill(&root->cgrp.self.refcnt);
1775
Tejun Heo2bd59d42014-02-11 11:52:49 -05001776 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777}
1778
1779static struct file_system_type cgroup_fs_type = {
1780 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001781 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 .kill_sb = cgroup_kill_sb,
1783};
1784
Greg KH676db4a2010-08-05 13:53:35 -07001785static struct kobject *cgroup_kobj;
1786
Li Zefana043e3b2008-02-23 15:24:09 -08001787/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001788 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001789 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001790 * @buf: the buffer to write the path into
1791 * @buflen: the length of the buffer
1792 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001793 * Determine @task's cgroup on the first (the one with the lowest non-zero
1794 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1795 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1796 * cgroup controller callbacks.
1797 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001798 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001800char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001801{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001802 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001803 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001804 int hierarchy_id = 1;
1805 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001806
1807 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001808 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001809
Tejun Heo913ffdb2013-07-11 16:34:48 -07001810 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1811
Tejun Heo857a2be2013-04-14 20:50:08 -07001812 if (root) {
1813 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001814 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001815 } else {
1816 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001817 if (strlcpy(buf, "/", buflen) < buflen)
1818 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001819 }
1820
Tejun Heo96d365e2014-02-13 06:58:40 -05001821 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001822 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001823 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001824}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001825EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001826
Tejun Heob3dc0942014-02-25 10:04:01 -05001827/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001828struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001829 /* the src and dst cset list running through cset->mg_node */
1830 struct list_head src_csets;
1831 struct list_head dst_csets;
1832
1833 /*
1834 * Fields for cgroup_taskset_*() iteration.
1835 *
1836 * Before migration is committed, the target migration tasks are on
1837 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1838 * the csets on ->dst_csets. ->csets point to either ->src_csets
1839 * or ->dst_csets depending on whether migration is committed.
1840 *
1841 * ->cur_csets and ->cur_task point to the current task position
1842 * during iteration.
1843 */
1844 struct list_head *csets;
1845 struct css_set *cur_cset;
1846 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001847};
1848
1849/**
1850 * cgroup_taskset_first - reset taskset and return the first task
1851 * @tset: taskset of interest
1852 *
1853 * @tset iteration is initialized and the first task is returned.
1854 */
1855struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1856{
Tejun Heob3dc0942014-02-25 10:04:01 -05001857 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1858 tset->cur_task = NULL;
1859
1860 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001861}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001862
1863/**
1864 * cgroup_taskset_next - iterate to the next task in taskset
1865 * @tset: taskset of interest
1866 *
1867 * Return the next task in @tset. Iteration must have been initialized
1868 * with cgroup_taskset_first().
1869 */
1870struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1871{
Tejun Heob3dc0942014-02-25 10:04:01 -05001872 struct css_set *cset = tset->cur_cset;
1873 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001874
Tejun Heob3dc0942014-02-25 10:04:01 -05001875 while (&cset->mg_node != tset->csets) {
1876 if (!task)
1877 task = list_first_entry(&cset->mg_tasks,
1878 struct task_struct, cg_list);
1879 else
1880 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001881
Tejun Heob3dc0942014-02-25 10:04:01 -05001882 if (&task->cg_list != &cset->mg_tasks) {
1883 tset->cur_cset = cset;
1884 tset->cur_task = task;
1885 return task;
1886 }
1887
1888 cset = list_next_entry(cset, mg_node);
1889 task = NULL;
1890 }
1891
1892 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001893}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001894
1895/**
Ben Blum74a11662011-05-26 16:25:20 -07001896 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001897 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001898 * @tsk: the task being migrated
1899 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001900 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001901 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001902 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001903static void cgroup_task_migrate(struct cgroup *old_cgrp,
1904 struct task_struct *tsk,
1905 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001906{
Tejun Heo5abb8852013-06-12 21:04:49 -07001907 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001908
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001909 lockdep_assert_held(&cgroup_mutex);
1910 lockdep_assert_held(&css_set_rwsem);
1911
Ben Blum74a11662011-05-26 16:25:20 -07001912 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001913 * We are synchronized through threadgroup_lock() against PF_EXITING
1914 * setting such that we can't race against cgroup_exit() changing the
1915 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001916 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001917 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001918 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001919
Tejun Heob3dc0942014-02-25 10:04:01 -05001920 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001921 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001922
Tejun Heo1b9aba42014-03-19 17:43:21 -04001923 /*
1924 * Use move_tail so that cgroup_taskset_first() still returns the
1925 * leader after migration. This works because cgroup_migrate()
1926 * ensures that the dst_cset of the leader is the first on the
1927 * tset's dst_csets list.
1928 */
1929 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001930
1931 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001932 * We just gained a reference on old_cset by taking it from the
1933 * task. As trading it for new_cset is protected by cgroup_mutex,
1934 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001935 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001936 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001937 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001938}
1939
Li Zefana043e3b2008-02-23 15:24:09 -08001940/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001941 * cgroup_migrate_finish - cleanup after attach
1942 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001943 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001944 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1945 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001946 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001947static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001948{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001949 struct css_set *cset, *tmp_cset;
1950
1951 lockdep_assert_held(&cgroup_mutex);
1952
1953 down_write(&css_set_rwsem);
1954 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1955 cset->mg_src_cgrp = NULL;
1956 cset->mg_dst_cset = NULL;
1957 list_del_init(&cset->mg_preload_node);
1958 put_css_set_locked(cset, false);
1959 }
1960 up_write(&css_set_rwsem);
1961}
1962
1963/**
1964 * cgroup_migrate_add_src - add a migration source css_set
1965 * @src_cset: the source css_set to add
1966 * @dst_cgrp: the destination cgroup
1967 * @preloaded_csets: list of preloaded css_sets
1968 *
1969 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1970 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1971 * up by cgroup_migrate_finish().
1972 *
1973 * This function may be called without holding threadgroup_lock even if the
1974 * target is a process. Threads may be created and destroyed but as long
1975 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1976 * the preloaded css_sets are guaranteed to cover all migrations.
1977 */
1978static void cgroup_migrate_add_src(struct css_set *src_cset,
1979 struct cgroup *dst_cgrp,
1980 struct list_head *preloaded_csets)
1981{
1982 struct cgroup *src_cgrp;
1983
1984 lockdep_assert_held(&cgroup_mutex);
1985 lockdep_assert_held(&css_set_rwsem);
1986
1987 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1988
Tejun Heo1958d2d2014-02-25 10:04:03 -05001989 if (!list_empty(&src_cset->mg_preload_node))
1990 return;
1991
1992 WARN_ON(src_cset->mg_src_cgrp);
1993 WARN_ON(!list_empty(&src_cset->mg_tasks));
1994 WARN_ON(!list_empty(&src_cset->mg_node));
1995
1996 src_cset->mg_src_cgrp = src_cgrp;
1997 get_css_set(src_cset);
1998 list_add(&src_cset->mg_preload_node, preloaded_csets);
1999}
2000
2001/**
2002 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002003 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002004 * @preloaded_csets: list of preloaded source css_sets
2005 *
2006 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2007 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002008 * pins all destination css_sets, links each to its source, and append them
2009 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2010 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002011 *
2012 * This function must be called after cgroup_migrate_add_src() has been
2013 * called on each migration source css_set. After migration is performed
2014 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2015 * @preloaded_csets.
2016 */
2017static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2018 struct list_head *preloaded_csets)
2019{
2020 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002021 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002022
2023 lockdep_assert_held(&cgroup_mutex);
2024
Tejun Heof8f22e52014-04-23 11:13:16 -04002025 /*
2026 * Except for the root, child_subsys_mask must be zero for a cgroup
2027 * with tasks so that child cgroups don't compete against tasks.
2028 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002029 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002030 dst_cgrp->child_subsys_mask)
2031 return -EBUSY;
2032
Tejun Heo1958d2d2014-02-25 10:04:03 -05002033 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002034 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002035 struct css_set *dst_cset;
2036
Tejun Heof817de92014-04-23 11:13:16 -04002037 dst_cset = find_css_set(src_cset,
2038 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002039 if (!dst_cset)
2040 goto err;
2041
2042 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002043
2044 /*
2045 * If src cset equals dst, it's noop. Drop the src.
2046 * cgroup_migrate() will skip the cset too. Note that we
2047 * can't handle src == dst as some nodes are used by both.
2048 */
2049 if (src_cset == dst_cset) {
2050 src_cset->mg_src_cgrp = NULL;
2051 list_del_init(&src_cset->mg_preload_node);
2052 put_css_set(src_cset, false);
2053 put_css_set(dst_cset, false);
2054 continue;
2055 }
2056
Tejun Heo1958d2d2014-02-25 10:04:03 -05002057 src_cset->mg_dst_cset = dst_cset;
2058
2059 if (list_empty(&dst_cset->mg_preload_node))
2060 list_add(&dst_cset->mg_preload_node, &csets);
2061 else
2062 put_css_set(dst_cset, false);
2063 }
2064
Tejun Heof817de92014-04-23 11:13:16 -04002065 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002066 return 0;
2067err:
2068 cgroup_migrate_finish(&csets);
2069 return -ENOMEM;
2070}
2071
2072/**
2073 * cgroup_migrate - migrate a process or task to a cgroup
2074 * @cgrp: the destination cgroup
2075 * @leader: the leader of the process or the task to migrate
2076 * @threadgroup: whether @leader points to the whole process or a single task
2077 *
2078 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2079 * process, the caller must be holding threadgroup_lock of @leader. The
2080 * caller is also responsible for invoking cgroup_migrate_add_src() and
2081 * cgroup_migrate_prepare_dst() on the targets before invoking this
2082 * function and following up with cgroup_migrate_finish().
2083 *
2084 * As long as a controller's ->can_attach() doesn't fail, this function is
2085 * guaranteed to succeed. This means that, excluding ->can_attach()
2086 * failure, when migrating multiple targets, the success or failure can be
2087 * decided for all targets by invoking group_migrate_prepare_dst() before
2088 * actually starting migrating.
2089 */
2090static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2091 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002092{
Tejun Heob3dc0942014-02-25 10:04:01 -05002093 struct cgroup_taskset tset = {
2094 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2095 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2096 .csets = &tset.src_csets,
2097 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002098 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002099 struct css_set *cset, *tmp_cset;
2100 struct task_struct *task, *tmp_task;
2101 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002102
2103 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002104 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2105 * already PF_EXITING could be freed from underneath us unless we
2106 * take an rcu_read_lock.
2107 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002108 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002109 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002110 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002111 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002112 /* @task either already exited or can't exit until the end */
2113 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002114 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002115
Tejun Heoeaf797a2014-02-25 10:04:03 -05002116 /* leave @task alone if post_fork() hasn't linked it yet */
2117 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002118 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002119
Tejun Heob3dc0942014-02-25 10:04:01 -05002120 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002121 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002122 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002123
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002124 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002125 * cgroup_taskset_first() must always return the leader.
2126 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002127 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002128 list_move_tail(&task->cg_list, &cset->mg_tasks);
2129 if (list_empty(&cset->mg_node))
2130 list_add_tail(&cset->mg_node, &tset.src_csets);
2131 if (list_empty(&cset->mg_dst_cset->mg_node))
2132 list_move_tail(&cset->mg_dst_cset->mg_node,
2133 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002134 next:
Li Zefan081aa452013-03-13 09:17:09 +08002135 if (!threadgroup)
2136 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002137 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002138 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002139 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002140
Tejun Heo134d3372011-12-12 18:12:21 -08002141 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002142 if (list_empty(&tset.src_csets))
2143 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002144
Tejun Heo1958d2d2014-02-25 10:04:03 -05002145 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002146 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002147 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002148 ret = css->ss->can_attach(css, &tset);
2149 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002150 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002151 goto out_cancel_attach;
2152 }
2153 }
Ben Blum74a11662011-05-26 16:25:20 -07002154 }
2155
2156 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002157 * Now that we're guaranteed success, proceed to move all tasks to
2158 * the new cgroup. There are no failure cases after here, so this
2159 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002160 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002161 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002162 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2163 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2164 cgroup_task_migrate(cset->mg_src_cgrp, task,
2165 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002166 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002167 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002168
2169 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002170 * Migration is committed, all target tasks are now on dst_csets.
2171 * Nothing is sensitive to fork() after this point. Notify
2172 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002173 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002174 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002175
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002176 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002177 if (css->ss->attach)
2178 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002179
Tejun Heo9db8de32014-02-13 06:58:43 -05002180 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002181 goto out_release_tset;
2182
Ben Blum74a11662011-05-26 16:25:20 -07002183out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002184 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002185 if (css == failed_css)
2186 break;
2187 if (css->ss->cancel_attach)
2188 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002189 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002190out_release_tset:
2191 down_write(&css_set_rwsem);
2192 list_splice_init(&tset.dst_csets, &tset.src_csets);
2193 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002194 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002195 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002196 }
2197 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002198 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002199}
2200
Tejun Heo1958d2d2014-02-25 10:04:03 -05002201/**
2202 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2203 * @dst_cgrp: the cgroup to attach to
2204 * @leader: the task or the leader of the threadgroup to be attached
2205 * @threadgroup: attach the whole threadgroup?
2206 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002207 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002208 */
2209static int cgroup_attach_task(struct cgroup *dst_cgrp,
2210 struct task_struct *leader, bool threadgroup)
2211{
2212 LIST_HEAD(preloaded_csets);
2213 struct task_struct *task;
2214 int ret;
2215
2216 /* look up all src csets */
2217 down_read(&css_set_rwsem);
2218 rcu_read_lock();
2219 task = leader;
2220 do {
2221 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2222 &preloaded_csets);
2223 if (!threadgroup)
2224 break;
2225 } while_each_thread(leader, task);
2226 rcu_read_unlock();
2227 up_read(&css_set_rwsem);
2228
2229 /* prepare dst csets and commit */
2230 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2231 if (!ret)
2232 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2233
2234 cgroup_migrate_finish(&preloaded_csets);
2235 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002236}
2237
2238/*
2239 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002240 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002241 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002242 */
Tejun Heoacbef752014-05-13 12:16:22 -04002243static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2244 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002245{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002246 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002247 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002248 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002249 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002250 int ret;
2251
Tejun Heoacbef752014-05-13 12:16:22 -04002252 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2253 return -EINVAL;
2254
Tejun Heoe76ecae2014-05-13 12:19:23 -04002255 cgrp = cgroup_kn_lock_live(of->kn);
2256 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002257 return -ENODEV;
2258
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002259retry_find_task:
2260 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002261 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002262 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002263 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002264 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002265 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002266 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002267 }
Ben Blum74a11662011-05-26 16:25:20 -07002268 /*
2269 * even if we're attaching all tasks in the thread group, we
2270 * only need to check permissions on one of them.
2271 */
David Howellsc69e8d92008-11-14 10:39:19 +11002272 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002273 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2274 !uid_eq(cred->euid, tcred->uid) &&
2275 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002276 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002277 ret = -EACCES;
2278 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002279 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002280 } else
2281 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002282
2283 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002284 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002285
2286 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002287 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002288 * trapped in a cpuset, or RT worker may be born in a cgroup
2289 * with no rt_runtime allocated. Just say no.
2290 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002291 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002292 ret = -EINVAL;
2293 rcu_read_unlock();
2294 goto out_unlock_cgroup;
2295 }
2296
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002297 get_task_struct(tsk);
2298 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002299
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002300 threadgroup_lock(tsk);
2301 if (threadgroup) {
2302 if (!thread_group_leader(tsk)) {
2303 /*
2304 * a race with de_thread from another thread's exec()
2305 * may strip us of our leadership, if this happens,
2306 * there is no choice but to throw this task away and
2307 * try again; this is
2308 * "double-double-toil-and-trouble-check locking".
2309 */
2310 threadgroup_unlock(tsk);
2311 put_task_struct(tsk);
2312 goto retry_find_task;
2313 }
Li Zefan081aa452013-03-13 09:17:09 +08002314 }
2315
2316 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2317
Tejun Heocd3d0952011-12-12 18:12:21 -08002318 threadgroup_unlock(tsk);
2319
Paul Menagebbcb81d2007-10-18 23:39:32 -07002320 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002321out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002322 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002323 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002324}
2325
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002326/**
2327 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2328 * @from: attach to all cgroups of a given task
2329 * @tsk: the task to be attached
2330 */
2331int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2332{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002333 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002334 int retval = 0;
2335
Tejun Heo47cfcd02013-04-07 09:29:51 -07002336 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002337 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002338 struct cgroup *from_cgrp;
2339
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002340 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002341 continue;
2342
Tejun Heo96d365e2014-02-13 06:58:40 -05002343 down_read(&css_set_rwsem);
2344 from_cgrp = task_cgroup_from_root(from, root);
2345 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002346
Li Zefan6f4b7e62013-07-31 16:18:36 +08002347 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002348 if (retval)
2349 break;
2350 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002351 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002352
2353 return retval;
2354}
2355EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2356
Tejun Heoacbef752014-05-13 12:16:22 -04002357static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2358 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002359{
Tejun Heoacbef752014-05-13 12:16:22 -04002360 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002361}
2362
Tejun Heoacbef752014-05-13 12:16:22 -04002363static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2364 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002365{
Tejun Heoacbef752014-05-13 12:16:22 -04002366 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002367}
2368
Tejun Heo451af502014-05-13 12:16:21 -04002369static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2370 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002371{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002372 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002373
Tejun Heoe76ecae2014-05-13 12:19:23 -04002374 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2375
2376 cgrp = cgroup_kn_lock_live(of->kn);
2377 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002378 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002379 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002380 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2381 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002382 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002383 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002384 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002385}
2386
Tejun Heo2da8ca82013-12-05 12:28:04 -05002387static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002388{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002389 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002390
Tejun Heo46cfeb02014-05-13 12:11:00 -04002391 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002392 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002393 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002394 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002395 return 0;
2396}
2397
Tejun Heo2da8ca82013-12-05 12:28:04 -05002398static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002399{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002400 struct cgroup *cgrp = seq_css(seq)->cgroup;
2401
2402 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002403 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404}
2405
Tejun Heof8f22e52014-04-23 11:13:16 -04002406static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2407{
2408 struct cgroup_subsys *ss;
2409 bool printed = false;
2410 int ssid;
2411
2412 for_each_subsys(ss, ssid) {
2413 if (ss_mask & (1 << ssid)) {
2414 if (printed)
2415 seq_putc(seq, ' ');
2416 seq_printf(seq, "%s", ss->name);
2417 printed = true;
2418 }
2419 }
2420 if (printed)
2421 seq_putc(seq, '\n');
2422}
2423
2424/* show controllers which are currently attached to the default hierarchy */
2425static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2426{
2427 struct cgroup *cgrp = seq_css(seq)->cgroup;
2428
2429 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2430 return 0;
2431}
2432
2433/* show controllers which are enabled from the parent */
2434static int cgroup_controllers_show(struct seq_file *seq, void *v)
2435{
2436 struct cgroup *cgrp = seq_css(seq)->cgroup;
2437
Tejun Heod51f39b2014-05-16 13:22:48 -04002438 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->child_subsys_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002439 return 0;
2440}
2441
2442/* show controllers which are enabled for a given cgroup's children */
2443static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2444{
2445 struct cgroup *cgrp = seq_css(seq)->cgroup;
2446
2447 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2448 return 0;
2449}
2450
2451/**
2452 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2453 * @cgrp: root of the subtree to update csses for
2454 *
2455 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2456 * css associations need to be updated accordingly. This function looks up
2457 * all css_sets which are attached to the subtree, creates the matching
2458 * updated css_sets and migrates the tasks to the new ones.
2459 */
2460static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2461{
2462 LIST_HEAD(preloaded_csets);
2463 struct cgroup_subsys_state *css;
2464 struct css_set *src_cset;
2465 int ret;
2466
Tejun Heof8f22e52014-04-23 11:13:16 -04002467 lockdep_assert_held(&cgroup_mutex);
2468
2469 /* look up all csses currently attached to @cgrp's subtree */
2470 down_read(&css_set_rwsem);
2471 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2472 struct cgrp_cset_link *link;
2473
2474 /* self is not affected by child_subsys_mask change */
2475 if (css->cgroup == cgrp)
2476 continue;
2477
2478 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2479 cgroup_migrate_add_src(link->cset, cgrp,
2480 &preloaded_csets);
2481 }
2482 up_read(&css_set_rwsem);
2483
2484 /* NULL dst indicates self on default hierarchy */
2485 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2486 if (ret)
2487 goto out_finish;
2488
2489 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2490 struct task_struct *last_task = NULL, *task;
2491
2492 /* src_csets precede dst_csets, break on the first dst_cset */
2493 if (!src_cset->mg_src_cgrp)
2494 break;
2495
2496 /*
2497 * All tasks in src_cset need to be migrated to the
2498 * matching dst_cset. Empty it process by process. We
2499 * walk tasks but migrate processes. The leader might even
2500 * belong to a different cset but such src_cset would also
2501 * be among the target src_csets because the default
2502 * hierarchy enforces per-process membership.
2503 */
2504 while (true) {
2505 down_read(&css_set_rwsem);
2506 task = list_first_entry_or_null(&src_cset->tasks,
2507 struct task_struct, cg_list);
2508 if (task) {
2509 task = task->group_leader;
2510 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2511 get_task_struct(task);
2512 }
2513 up_read(&css_set_rwsem);
2514
2515 if (!task)
2516 break;
2517
2518 /* guard against possible infinite loop */
2519 if (WARN(last_task == task,
2520 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2521 goto out_finish;
2522 last_task = task;
2523
2524 threadgroup_lock(task);
2525 /* raced against de_thread() from another thread? */
2526 if (!thread_group_leader(task)) {
2527 threadgroup_unlock(task);
2528 put_task_struct(task);
2529 continue;
2530 }
2531
2532 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2533
2534 threadgroup_unlock(task);
2535 put_task_struct(task);
2536
2537 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2538 goto out_finish;
2539 }
2540 }
2541
2542out_finish:
2543 cgroup_migrate_finish(&preloaded_csets);
2544 return ret;
2545}
2546
2547/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002548static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2549 char *buf, size_t nbytes,
2550 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002551{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002552 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002553 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002554 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002555 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002556 int ssid, ret;
2557
2558 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002559 * Parse input - space separated list of subsystem names prefixed
2560 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002561 */
Tejun Heo451af502014-05-13 12:16:21 -04002562 buf = strstrip(buf);
2563 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002564 if (tok[0] == '\0')
2565 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002566 for_each_subsys(ss, ssid) {
2567 if (ss->disabled || strcmp(tok + 1, ss->name))
2568 continue;
2569
2570 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002571 enable |= 1 << ssid;
2572 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002573 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002574 disable |= 1 << ssid;
2575 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002576 } else {
2577 return -EINVAL;
2578 }
2579 break;
2580 }
2581 if (ssid == CGROUP_SUBSYS_COUNT)
2582 return -EINVAL;
2583 }
2584
Tejun Heoa9746d82014-05-13 12:19:22 -04002585 cgrp = cgroup_kn_lock_live(of->kn);
2586 if (!cgrp)
2587 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002588
2589 for_each_subsys(ss, ssid) {
2590 if (enable & (1 << ssid)) {
2591 if (cgrp->child_subsys_mask & (1 << ssid)) {
2592 enable &= ~(1 << ssid);
2593 continue;
2594 }
2595
2596 /*
2597 * Because css offlining is asynchronous, userland
2598 * might try to re-enable the same controller while
2599 * the previous instance is still around. In such
2600 * cases, wait till it's gone using offline_waitq.
2601 */
2602 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002603 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002604
2605 if (!cgroup_css(child, ss))
2606 continue;
2607
Tejun Heo0cee8b72014-05-13 12:10:59 -04002608 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002609 prepare_to_wait(&child->offline_waitq, &wait,
2610 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002611 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002612 schedule();
2613 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002614 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002615
Tejun Heoa9746d82014-05-13 12:19:22 -04002616 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002617 }
2618
2619 /* unavailable or not enabled on the parent? */
2620 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
Tejun Heod51f39b2014-05-16 13:22:48 -04002621 (cgroup_parent(cgrp) &&
2622 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ssid)))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002623 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002624 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002625 }
2626 } else if (disable & (1 << ssid)) {
2627 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2628 disable &= ~(1 << ssid);
2629 continue;
2630 }
2631
2632 /* a child has it enabled? */
2633 cgroup_for_each_live_child(child, cgrp) {
2634 if (child->child_subsys_mask & (1 << ssid)) {
2635 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002636 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002637 }
2638 }
2639 }
2640 }
2641
2642 if (!enable && !disable) {
2643 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002644 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002645 }
2646
2647 /*
2648 * Except for the root, child_subsys_mask must be zero for a cgroup
2649 * with tasks so that child cgroups don't compete against tasks.
2650 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002651 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002652 ret = -EBUSY;
2653 goto out_unlock;
2654 }
2655
2656 /*
2657 * Create csses for enables and update child_subsys_mask. This
2658 * changes cgroup_e_css() results which in turn makes the
2659 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2660 * subtree to the updated csses.
2661 */
2662 for_each_subsys(ss, ssid) {
2663 if (!(enable & (1 << ssid)))
2664 continue;
2665
2666 cgroup_for_each_live_child(child, cgrp) {
2667 ret = create_css(child, ss);
2668 if (ret)
2669 goto err_undo_css;
2670 }
2671 }
2672
2673 cgrp->child_subsys_mask |= enable;
2674 cgrp->child_subsys_mask &= ~disable;
2675
2676 ret = cgroup_update_dfl_csses(cgrp);
2677 if (ret)
2678 goto err_undo_css;
2679
2680 /* all tasks are now migrated away from the old csses, kill them */
2681 for_each_subsys(ss, ssid) {
2682 if (!(disable & (1 << ssid)))
2683 continue;
2684
2685 cgroup_for_each_live_child(child, cgrp)
2686 kill_css(cgroup_css(child, ss));
2687 }
2688
2689 kernfs_activate(cgrp->kn);
2690 ret = 0;
2691out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002692 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002693 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002694
2695err_undo_css:
2696 cgrp->child_subsys_mask &= ~enable;
2697 cgrp->child_subsys_mask |= disable;
2698
2699 for_each_subsys(ss, ssid) {
2700 if (!(enable & (1 << ssid)))
2701 continue;
2702
2703 cgroup_for_each_live_child(child, cgrp) {
2704 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2705 if (css)
2706 kill_css(css);
2707 }
2708 }
2709 goto out_unlock;
2710}
2711
Tejun Heo842b5972014-04-25 18:28:02 -04002712static int cgroup_populated_show(struct seq_file *seq, void *v)
2713{
2714 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2715 return 0;
2716}
2717
Tejun Heo2bd59d42014-02-11 11:52:49 -05002718static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2719 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002721 struct cgroup *cgrp = of->kn->parent->priv;
2722 struct cftype *cft = of->kn->priv;
2723 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002724 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725
Tejun Heob4168642014-05-13 12:16:21 -04002726 if (cft->write)
2727 return cft->write(of, buf, nbytes, off);
2728
Tejun Heo2bd59d42014-02-11 11:52:49 -05002729 /*
2730 * kernfs guarantees that a file isn't deleted with operations in
2731 * flight, which means that the matching css is and stays alive and
2732 * doesn't need to be pinned. The RCU locking is not necessary
2733 * either. It's just for the convenience of using cgroup_css().
2734 */
2735 rcu_read_lock();
2736 css = cgroup_css(cgrp, cft->ss);
2737 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738
Tejun Heo451af502014-05-13 12:16:21 -04002739 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002740 unsigned long long v;
2741 ret = kstrtoull(buf, 0, &v);
2742 if (!ret)
2743 ret = cft->write_u64(css, cft, v);
2744 } else if (cft->write_s64) {
2745 long long v;
2746 ret = kstrtoll(buf, 0, &v);
2747 if (!ret)
2748 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002749 } else {
2750 ret = -EINVAL;
2751 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002752
Tejun Heoa742c592013-12-05 12:28:03 -05002753 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754}
2755
Tejun Heo6612f052013-12-05 12:28:04 -05002756static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002757{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002758 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002759}
2760
2761static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2762{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002763 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002764}
2765
2766static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2767{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002768 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002769}
2770
2771static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2772{
Tejun Heo7da11272013-12-05 12:28:04 -05002773 struct cftype *cft = seq_cft(m);
2774 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002775
Tejun Heo2da8ca82013-12-05 12:28:04 -05002776 if (cft->seq_show)
2777 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002778
Tejun Heo896f5192013-12-05 12:28:04 -05002779 if (cft->read_u64)
2780 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2781 else if (cft->read_s64)
2782 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2783 else
2784 return -EINVAL;
2785 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002786}
2787
Tejun Heo2bd59d42014-02-11 11:52:49 -05002788static struct kernfs_ops cgroup_kf_single_ops = {
2789 .atomic_write_len = PAGE_SIZE,
2790 .write = cgroup_file_write,
2791 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002792};
2793
Tejun Heo2bd59d42014-02-11 11:52:49 -05002794static struct kernfs_ops cgroup_kf_ops = {
2795 .atomic_write_len = PAGE_SIZE,
2796 .write = cgroup_file_write,
2797 .seq_start = cgroup_seqfile_start,
2798 .seq_next = cgroup_seqfile_next,
2799 .seq_stop = cgroup_seqfile_stop,
2800 .seq_show = cgroup_seqfile_show,
2801};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002802
2803/*
2804 * cgroup_rename - Only allow simple rename of directories in place.
2805 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002806static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2807 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002808{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002809 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002810 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002811
Tejun Heo2bd59d42014-02-11 11:52:49 -05002812 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002813 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002814 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002815 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002816
Tejun Heo6db8e852013-06-14 11:18:22 -07002817 /*
2818 * This isn't a proper migration and its usefulness is very
2819 * limited. Disallow if sane_behavior.
2820 */
2821 if (cgroup_sane_behavior(cgrp))
2822 return -EPERM;
2823
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002824 /*
Tejun Heo8353da12014-05-13 12:19:23 -04002825 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002826 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04002827 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002828 */
2829 kernfs_break_active_protection(new_parent);
2830 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002831
Tejun Heo2bd59d42014-02-11 11:52:49 -05002832 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002833
Tejun Heo2bd59d42014-02-11 11:52:49 -05002834 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002835
Tejun Heo2bd59d42014-02-11 11:52:49 -05002836 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002837
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002838 kernfs_unbreak_active_protection(kn);
2839 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002840 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002841}
2842
Tejun Heo49957f82014-04-07 16:44:47 -04002843/* set uid and gid of cgroup dirs and files to that of the creator */
2844static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2845{
2846 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2847 .ia_uid = current_fsuid(),
2848 .ia_gid = current_fsgid(), };
2849
2850 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2851 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2852 return 0;
2853
2854 return kernfs_setattr(kn, &iattr);
2855}
2856
Tejun Heo2bb566c2013-08-08 20:11:23 -04002857static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002858{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002859 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002860 struct kernfs_node *kn;
2861 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002862 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002863
Tejun Heo2bd59d42014-02-11 11:52:49 -05002864#ifdef CONFIG_DEBUG_LOCK_ALLOC
2865 key = &cft->lockdep_key;
2866#endif
2867 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2868 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2869 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002870 if (IS_ERR(kn))
2871 return PTR_ERR(kn);
2872
2873 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002874 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002875 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002876 return ret;
2877 }
2878
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002879 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002880 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002881 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002882}
2883
Tejun Heob1f28d32013-06-28 16:24:10 -07002884/**
2885 * cgroup_addrm_files - add or remove files to a cgroup directory
2886 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002887 * @cfts: array of cftypes to be added
2888 * @is_add: whether to add or remove
2889 *
2890 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002891 * For removals, this function never fails. If addition fails, this
2892 * function doesn't remove files already added. The caller is responsible
2893 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002894 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002895static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2896 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002897{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002898 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002899 int ret;
2900
Tejun Heo01f64742014-05-13 12:19:23 -04002901 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002902
2903 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002904 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002905 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2906 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002907 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2908 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002909 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002910 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002911 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002912 continue;
2913
Li Zefan2739d3c2013-01-21 18:18:33 +08002914 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002915 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002916 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002917 pr_warn("%s: failed to add %s, err=%d\n",
2918 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002919 return ret;
2920 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002921 } else {
2922 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002923 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002924 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002925 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002926}
2927
Tejun Heo21a2d342014-02-12 09:29:49 -05002928static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002929{
2930 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002931 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002932 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002933 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002934 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002935
Tejun Heo01f64742014-05-13 12:19:23 -04002936 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002937
Li Zefane8c82d22013-06-18 18:48:37 +08002938 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002939 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002940 struct cgroup *cgrp = css->cgroup;
2941
Li Zefane8c82d22013-06-18 18:48:37 +08002942 if (cgroup_is_dead(cgrp))
2943 continue;
2944
Tejun Heo21a2d342014-02-12 09:29:49 -05002945 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002946 if (ret)
2947 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002948 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002949
2950 if (is_add && !ret)
2951 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002952 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002953}
2954
Tejun Heo2da440a2014-02-11 11:52:48 -05002955static void cgroup_exit_cftypes(struct cftype *cfts)
2956{
2957 struct cftype *cft;
2958
Tejun Heo2bd59d42014-02-11 11:52:49 -05002959 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2960 /* free copy for custom atomic_write_len, see init_cftypes() */
2961 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2962 kfree(cft->kf_ops);
2963 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002964 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002965 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002966}
2967
Tejun Heo2bd59d42014-02-11 11:52:49 -05002968static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002969{
2970 struct cftype *cft;
2971
Tejun Heo2bd59d42014-02-11 11:52:49 -05002972 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2973 struct kernfs_ops *kf_ops;
2974
Tejun Heo0adb0702014-02-12 09:29:48 -05002975 WARN_ON(cft->ss || cft->kf_ops);
2976
Tejun Heo2bd59d42014-02-11 11:52:49 -05002977 if (cft->seq_start)
2978 kf_ops = &cgroup_kf_ops;
2979 else
2980 kf_ops = &cgroup_kf_single_ops;
2981
2982 /*
2983 * Ugh... if @cft wants a custom max_write_len, we need to
2984 * make a copy of kf_ops to set its atomic_write_len.
2985 */
2986 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2987 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2988 if (!kf_ops) {
2989 cgroup_exit_cftypes(cfts);
2990 return -ENOMEM;
2991 }
2992 kf_ops->atomic_write_len = cft->max_write_len;
2993 }
2994
2995 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002996 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002997 }
2998
2999 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003000}
3001
Tejun Heo21a2d342014-02-12 09:29:49 -05003002static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3003{
Tejun Heo01f64742014-05-13 12:19:23 -04003004 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003005
3006 if (!cfts || !cfts[0].ss)
3007 return -ENOENT;
3008
3009 list_del(&cfts->node);
3010 cgroup_apply_cftypes(cfts, false);
3011 cgroup_exit_cftypes(cfts);
3012 return 0;
3013}
3014
Tejun Heo8e3f6542012-04-01 12:09:55 -07003015/**
Tejun Heo80b13582014-02-12 09:29:48 -05003016 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3017 * @cfts: zero-length name terminated array of cftypes
3018 *
3019 * Unregister @cfts. Files described by @cfts are removed from all
3020 * existing cgroups and all future cgroups won't have them either. This
3021 * function can be called anytime whether @cfts' subsys is attached or not.
3022 *
3023 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3024 * registered.
3025 */
3026int cgroup_rm_cftypes(struct cftype *cfts)
3027{
Tejun Heo21a2d342014-02-12 09:29:49 -05003028 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003029
Tejun Heo01f64742014-05-13 12:19:23 -04003030 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003031 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003032 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003033 return ret;
3034}
3035
3036/**
3037 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3038 * @ss: target cgroup subsystem
3039 * @cfts: zero-length name terminated array of cftypes
3040 *
3041 * Register @cfts to @ss. Files described by @cfts are created for all
3042 * existing cgroups to which @ss is attached and all future cgroups will
3043 * have them too. This function can be called anytime whether @ss is
3044 * attached or not.
3045 *
3046 * Returns 0 on successful registration, -errno on failure. Note that this
3047 * function currently returns 0 as long as @cfts registration is successful
3048 * even if some file creation attempts on existing cgroups fail.
3049 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003050int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003051{
Tejun Heo9ccece82013-06-28 16:24:11 -07003052 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003053
Li Zefandc5736e2014-02-17 10:41:50 +08003054 if (!cfts || cfts[0].name[0] == '\0')
3055 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003056
Tejun Heo2bd59d42014-02-11 11:52:49 -05003057 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003058 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003059 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003060
Tejun Heo01f64742014-05-13 12:19:23 -04003061 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003062
Tejun Heo0adb0702014-02-12 09:29:48 -05003063 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003064 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003065 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003066 cgroup_rm_cftypes_locked(cfts);
3067
Tejun Heo01f64742014-05-13 12:19:23 -04003068 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003069 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003070}
Tejun Heo79578622012-04-01 12:09:56 -07003071
3072/**
Li Zefana043e3b2008-02-23 15:24:09 -08003073 * cgroup_task_count - count the number of tasks in a cgroup.
3074 * @cgrp: the cgroup in question
3075 *
3076 * Return the number of tasks in the cgroup.
3077 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003078static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003079{
3080 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003081 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003082
Tejun Heo96d365e2014-02-13 06:58:40 -05003083 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003084 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3085 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003086 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003087 return count;
3088}
3089
Tejun Heo574bd9f2012-11-09 09:12:29 -08003090/**
Tejun Heo492eb212013-08-08 20:11:25 -04003091 * css_next_child - find the next child of a given css
3092 * @pos_css: the current position (%NULL to initiate traversal)
3093 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003094 *
Tejun Heo492eb212013-08-08 20:11:25 -04003095 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003096 * under either cgroup_mutex or RCU read lock. The only requirement is
3097 * that @parent_css and @pos_css are accessible. The next sibling is
3098 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003099 */
Tejun Heo492eb212013-08-08 20:11:25 -04003100struct cgroup_subsys_state *
3101css_next_child(struct cgroup_subsys_state *pos_css,
3102 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003103{
Tejun Heo492eb212013-08-08 20:11:25 -04003104 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3105 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003106 struct cgroup *next;
3107
Tejun Heo8353da12014-05-13 12:19:23 -04003108 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003109
3110 /*
3111 * @pos could already have been removed. Once a cgroup is removed,
3112 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003113 * changes. As CGRP_DEAD assertion is serialized and happens
3114 * before the cgroup is taken off the ->sibling list, if we see it
3115 * unasserted, it's guaranteed that the next sibling hasn't
3116 * finished its grace period even if it's already removed, and thus
3117 * safe to dereference from this RCU critical section. If
3118 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3119 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003120 *
3121 * If @pos is dead, its next pointer can't be dereferenced;
3122 * however, as each cgroup is given a monotonically increasing
3123 * unique serial number and always appended to the sibling list,
3124 * the next one can be found by walking the parent's children until
3125 * we see a cgroup with higher serial number than @pos's. While
3126 * this path can be slower, it's taken only when either the current
3127 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003128 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003129 if (!pos) {
Tejun Heod5c419b2014-05-16 13:22:48 -04003130 next = list_entry_rcu(cgrp->self.children.next, struct cgroup, self.sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003131 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heod5c419b2014-05-16 13:22:48 -04003132 next = list_entry_rcu(pos->self.sibling.next, struct cgroup, self.sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003133 } else {
Tejun Heod5c419b2014-05-16 13:22:48 -04003134 list_for_each_entry_rcu(next, &cgrp->self.children, self.sibling)
Tejun Heo0cb51d72014-05-16 13:22:49 -04003135 if (next->self.serial_nr > pos->self.serial_nr)
Tejun Heo3b287a52013-08-08 20:11:24 -04003136 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003137 }
3138
Tejun Heo3b281af2014-04-23 11:13:15 -04003139 /*
3140 * @next, if not pointing to the head, can be dereferenced and is
3141 * the next sibling; however, it might have @ss disabled. If so,
3142 * fast-forward to the next enabled one.
3143 */
Tejun Heod5c419b2014-05-16 13:22:48 -04003144 while (&next->self.sibling != &cgrp->self.children) {
Tejun Heo3b281af2014-04-23 11:13:15 -04003145 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003146
Tejun Heo3b281af2014-04-23 11:13:15 -04003147 if (next_css)
3148 return next_css;
Tejun Heod5c419b2014-05-16 13:22:48 -04003149 next = list_entry_rcu(next->self.sibling.next, struct cgroup, self.sibling);
Tejun Heo3b281af2014-04-23 11:13:15 -04003150 }
3151 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 Heo574bd9f2012-11-09 09:12:29 -08003167 */
Tejun Heo492eb212013-08-08 20:11:25 -04003168struct cgroup_subsys_state *
3169css_next_descendant_pre(struct cgroup_subsys_state *pos,
3170 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003171{
Tejun Heo492eb212013-08-08 20:11:25 -04003172 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003173
Tejun Heo8353da12014-05-13 12:19:23 -04003174 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003175
Tejun Heobd8815a2013-08-08 20:11:27 -04003176 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003177 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003178 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003179
3180 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003181 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182 if (next)
3183 return next;
3184
3185 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003186 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003187 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003188 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003190 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003191 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003192
3193 return NULL;
3194}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003195
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003196/**
Tejun Heo492eb212013-08-08 20:11:25 -04003197 * css_rightmost_descendant - return the rightmost descendant of a css
3198 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003199 *
Tejun Heo492eb212013-08-08 20:11:25 -04003200 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3201 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003202 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003203 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003204 * While this function requires cgroup_mutex or RCU read locking, it
3205 * doesn't require the whole traversal to be contained in a single critical
3206 * section. This function will return the correct rightmost descendant as
3207 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003208 */
Tejun Heo492eb212013-08-08 20:11:25 -04003209struct cgroup_subsys_state *
3210css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003211{
Tejun Heo492eb212013-08-08 20:11:25 -04003212 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003213
Tejun Heo8353da12014-05-13 12:19:23 -04003214 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003215
3216 do {
3217 last = pos;
3218 /* ->prev isn't RCU safe, walk ->next till the end */
3219 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003220 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003221 pos = tmp;
3222 } while (pos);
3223
3224 return last;
3225}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003226
Tejun Heo492eb212013-08-08 20:11:25 -04003227static struct cgroup_subsys_state *
3228css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003229{
Tejun Heo492eb212013-08-08 20:11:25 -04003230 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003231
3232 do {
3233 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003234 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003235 } while (pos);
3236
3237 return last;
3238}
3239
3240/**
Tejun Heo492eb212013-08-08 20:11:25 -04003241 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003242 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003243 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003244 *
Tejun Heo492eb212013-08-08 20:11:25 -04003245 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003246 * to visit for post-order traversal of @root's descendants. @root is
3247 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003248 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003249 * While this function requires cgroup_mutex or RCU read locking, it
3250 * doesn't require the whole traversal to be contained in a single critical
3251 * section. This function will return the correct next descendant as long
3252 * as both @pos and @cgroup are accessible and @pos is a descendant of
3253 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003254 */
Tejun Heo492eb212013-08-08 20:11:25 -04003255struct cgroup_subsys_state *
3256css_next_descendant_post(struct cgroup_subsys_state *pos,
3257 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003258{
Tejun Heo492eb212013-08-08 20:11:25 -04003259 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003260
Tejun Heo8353da12014-05-13 12:19:23 -04003261 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003262
Tejun Heo58b79a92013-09-06 15:31:08 -04003263 /* if first iteration, visit leftmost descendant which may be @root */
3264 if (!pos)
3265 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003266
Tejun Heobd8815a2013-08-08 20:11:27 -04003267 /* if we visited @root, we're done */
3268 if (pos == root)
3269 return NULL;
3270
Tejun Heo574bd9f2012-11-09 09:12:29 -08003271 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003272 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003273 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003274 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003275
3276 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003277 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003278}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003279
Tejun Heocbc125e2014-05-14 09:15:01 -04003280static bool cgroup_has_live_children(struct cgroup *cgrp)
3281{
3282 struct cgroup *child;
3283
3284 rcu_read_lock();
Tejun Heod5c419b2014-05-16 13:22:48 -04003285 list_for_each_entry_rcu(child, &cgrp->self.children, self.sibling) {
Tejun Heocbc125e2014-05-14 09:15:01 -04003286 if (!cgroup_is_dead(child)) {
3287 rcu_read_unlock();
3288 return true;
3289 }
3290 }
3291 rcu_read_unlock();
3292 return false;
3293}
3294
Tejun Heo0942eee2013-08-08 20:11:26 -04003295/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003296 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003297 * @it: the iterator to advance
3298 *
3299 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003300 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003301static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003302{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003303 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003304 struct cgrp_cset_link *link;
3305 struct css_set *cset;
3306
3307 /* Advance to the next non-empty css_set */
3308 do {
3309 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003310 if (l == it->cset_head) {
3311 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003312 return;
3313 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003314
3315 if (it->ss) {
3316 cset = container_of(l, struct css_set,
3317 e_cset_node[it->ss->id]);
3318 } else {
3319 link = list_entry(l, struct cgrp_cset_link, cset_link);
3320 cset = link->cset;
3321 }
Tejun Heoc7561122014-02-25 10:04:01 -05003322 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3323
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003324 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003325
3326 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003327 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003328 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003329 it->task_pos = cset->mg_tasks.next;
3330
3331 it->tasks_head = &cset->tasks;
3332 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003333}
3334
Tejun Heo0942eee2013-08-08 20:11:26 -04003335/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003336 * css_task_iter_start - initiate task iteration
3337 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003338 * @it: the task iterator to use
3339 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003340 * Initiate iteration through the tasks of @css. The caller can call
3341 * css_task_iter_next() to walk through the tasks until the function
3342 * returns NULL. On completion of iteration, css_task_iter_end() must be
3343 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003344 *
3345 * Note that this function acquires a lock which is released when the
3346 * iteration finishes. The caller can't sleep while iteration is in
3347 * progress.
3348 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003349void css_task_iter_start(struct cgroup_subsys_state *css,
3350 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003351 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003352{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003353 /* no one should try to iterate before mounting cgroups */
3354 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003355
Tejun Heo96d365e2014-02-13 06:58:40 -05003356 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003357
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003358 it->ss = css->ss;
3359
3360 if (it->ss)
3361 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3362 else
3363 it->cset_pos = &css->cgroup->cset_links;
3364
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003365 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003366
Tejun Heo72ec7022013-08-08 20:11:26 -04003367 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003368}
3369
Tejun Heo0942eee2013-08-08 20:11:26 -04003370/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003371 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003372 * @it: the task iterator being iterated
3373 *
3374 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003375 * initialized via css_task_iter_start(). Returns NULL when the iteration
3376 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003377 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003378struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003379{
3380 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003381 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003382
3383 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003384 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003385 return NULL;
3386 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003387
3388 /*
3389 * Advance iterator to find next entry. cset->tasks is consumed
3390 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3391 * next cset.
3392 */
Paul Menage817929e2007-10-18 23:39:36 -07003393 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003394
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003395 if (l == it->tasks_head)
3396 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003397
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003398 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003399 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003400 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003401 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003402
Paul Menage817929e2007-10-18 23:39:36 -07003403 return res;
3404}
3405
Tejun Heo0942eee2013-08-08 20:11:26 -04003406/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003407 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003408 * @it: the task iterator to finish
3409 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003410 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003411 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003412void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003413 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003414{
Tejun Heo96d365e2014-02-13 06:58:40 -05003415 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003416}
3417
3418/**
3419 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3420 * @to: cgroup to which the tasks will be moved
3421 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003422 *
3423 * Locking rules between cgroup_post_fork() and the migration path
3424 * guarantee that, if a task is forking while being migrated, the new child
3425 * is guaranteed to be either visible in the source cgroup after the
3426 * parent's migration is complete or put into the target cgroup. No task
3427 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003428 */
3429int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3430{
Tejun Heo952aaa12014-02-25 10:04:03 -05003431 LIST_HEAD(preloaded_csets);
3432 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003433 struct css_task_iter it;
3434 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003435 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003436
Tejun Heo952aaa12014-02-25 10:04:03 -05003437 mutex_lock(&cgroup_mutex);
3438
3439 /* all tasks in @from are being moved, all csets are source */
3440 down_read(&css_set_rwsem);
3441 list_for_each_entry(link, &from->cset_links, cset_link)
3442 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3443 up_read(&css_set_rwsem);
3444
3445 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3446 if (ret)
3447 goto out_err;
3448
3449 /*
3450 * Migrate tasks one-by-one until @form is empty. This fails iff
3451 * ->can_attach() fails.
3452 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003453 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003454 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003455 task = css_task_iter_next(&it);
3456 if (task)
3457 get_task_struct(task);
3458 css_task_iter_end(&it);
3459
3460 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003461 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003462 put_task_struct(task);
3463 }
3464 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003465out_err:
3466 cgroup_migrate_finish(&preloaded_csets);
3467 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003468 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003469}
3470
Paul Menage817929e2007-10-18 23:39:36 -07003471/*
Ben Blum102a7752009-09-23 15:56:26 -07003472 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003473 *
3474 * Reading this file can return large amounts of data if a cgroup has
3475 * *lots* of attached tasks. So it may need several calls to read(),
3476 * but we cannot guarantee that the information we produce is correct
3477 * unless we produce it entirely atomically.
3478 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003479 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003480
Li Zefan24528252012-01-20 11:58:43 +08003481/* which pidlist file are we talking about? */
3482enum cgroup_filetype {
3483 CGROUP_FILE_PROCS,
3484 CGROUP_FILE_TASKS,
3485};
3486
3487/*
3488 * A pidlist is a list of pids that virtually represents the contents of one
3489 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3490 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3491 * to the cgroup.
3492 */
3493struct cgroup_pidlist {
3494 /*
3495 * used to find which pidlist is wanted. doesn't change as long as
3496 * this particular list stays in the list.
3497 */
3498 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3499 /* array of xids */
3500 pid_t *list;
3501 /* how many elements the above list has */
3502 int length;
Li Zefan24528252012-01-20 11:58:43 +08003503 /* each of these stored in a list by its cgroup */
3504 struct list_head links;
3505 /* pointer to the cgroup we belong to, for list removal purposes */
3506 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003507 /* for delayed destruction */
3508 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003509};
3510
Paul Menagebbcb81d2007-10-18 23:39:32 -07003511/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003512 * The following two functions "fix" the issue where there are more pids
3513 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3514 * TODO: replace with a kernel-wide solution to this problem
3515 */
3516#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3517static void *pidlist_allocate(int count)
3518{
3519 if (PIDLIST_TOO_LARGE(count))
3520 return vmalloc(count * sizeof(pid_t));
3521 else
3522 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3523}
Tejun Heob1a21362013-11-29 10:42:58 -05003524
Ben Blumd1d9fd32009-09-23 15:56:28 -07003525static void pidlist_free(void *p)
3526{
3527 if (is_vmalloc_addr(p))
3528 vfree(p);
3529 else
3530 kfree(p);
3531}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003532
3533/*
Tejun Heob1a21362013-11-29 10:42:58 -05003534 * Used to destroy all pidlists lingering waiting for destroy timer. None
3535 * should be left afterwards.
3536 */
3537static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3538{
3539 struct cgroup_pidlist *l, *tmp_l;
3540
3541 mutex_lock(&cgrp->pidlist_mutex);
3542 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3543 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3544 mutex_unlock(&cgrp->pidlist_mutex);
3545
3546 flush_workqueue(cgroup_pidlist_destroy_wq);
3547 BUG_ON(!list_empty(&cgrp->pidlists));
3548}
3549
3550static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3551{
3552 struct delayed_work *dwork = to_delayed_work(work);
3553 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3554 destroy_dwork);
3555 struct cgroup_pidlist *tofree = NULL;
3556
3557 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003558
3559 /*
Tejun Heo04502362013-11-29 10:42:59 -05003560 * Destroy iff we didn't get queued again. The state won't change
3561 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003562 */
Tejun Heo04502362013-11-29 10:42:59 -05003563 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003564 list_del(&l->links);
3565 pidlist_free(l->list);
3566 put_pid_ns(l->key.ns);
3567 tofree = l;
3568 }
3569
Tejun Heob1a21362013-11-29 10:42:58 -05003570 mutex_unlock(&l->owner->pidlist_mutex);
3571 kfree(tofree);
3572}
3573
3574/*
Ben Blum102a7752009-09-23 15:56:26 -07003575 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003576 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003577 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003578static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003579{
Ben Blum102a7752009-09-23 15:56:26 -07003580 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003581
3582 /*
3583 * we presume the 0th element is unique, so i starts at 1. trivial
3584 * edge cases first; no work needs to be done for either
3585 */
3586 if (length == 0 || length == 1)
3587 return length;
3588 /* src and dest walk down the list; dest counts unique elements */
3589 for (src = 1; src < length; src++) {
3590 /* find next unique element */
3591 while (list[src] == list[src-1]) {
3592 src++;
3593 if (src == length)
3594 goto after;
3595 }
3596 /* dest always points to where the next unique element goes */
3597 list[dest] = list[src];
3598 dest++;
3599 }
3600after:
Ben Blum102a7752009-09-23 15:56:26 -07003601 return dest;
3602}
3603
Tejun Heoafb2bc12013-11-29 10:42:59 -05003604/*
3605 * The two pid files - task and cgroup.procs - guaranteed that the result
3606 * is sorted, which forced this whole pidlist fiasco. As pid order is
3607 * different per namespace, each namespace needs differently sorted list,
3608 * making it impossible to use, for example, single rbtree of member tasks
3609 * sorted by task pointer. As pidlists can be fairly large, allocating one
3610 * per open file is dangerous, so cgroup had to implement shared pool of
3611 * pidlists keyed by cgroup and namespace.
3612 *
3613 * All this extra complexity was caused by the original implementation
3614 * committing to an entirely unnecessary property. In the long term, we
3615 * want to do away with it. Explicitly scramble sort order if
3616 * sane_behavior so that no such expectation exists in the new interface.
3617 *
3618 * Scrambling is done by swapping every two consecutive bits, which is
3619 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3620 */
3621static pid_t pid_fry(pid_t pid)
3622{
3623 unsigned a = pid & 0x55555555;
3624 unsigned b = pid & 0xAAAAAAAA;
3625
3626 return (a << 1) | (b >> 1);
3627}
3628
3629static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3630{
3631 if (cgroup_sane_behavior(cgrp))
3632 return pid_fry(pid);
3633 else
3634 return pid;
3635}
3636
Ben Blum102a7752009-09-23 15:56:26 -07003637static int cmppid(const void *a, const void *b)
3638{
3639 return *(pid_t *)a - *(pid_t *)b;
3640}
3641
Tejun Heoafb2bc12013-11-29 10:42:59 -05003642static int fried_cmppid(const void *a, const void *b)
3643{
3644 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3645}
3646
Ben Blum72a8cb32009-09-23 15:56:27 -07003647static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3648 enum cgroup_filetype type)
3649{
3650 struct cgroup_pidlist *l;
3651 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003652 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003653
Tejun Heoe6b81712013-11-29 10:42:59 -05003654 lockdep_assert_held(&cgrp->pidlist_mutex);
3655
3656 list_for_each_entry(l, &cgrp->pidlists, links)
3657 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003658 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003659 return NULL;
3660}
3661
Ben Blum72a8cb32009-09-23 15:56:27 -07003662/*
3663 * find the appropriate pidlist for our purpose (given procs vs tasks)
3664 * returns with the lock on that pidlist already held, and takes care
3665 * of the use count, or returns NULL with no locks held if we're out of
3666 * memory.
3667 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003668static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3669 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003670{
3671 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003672
Tejun Heoe6b81712013-11-29 10:42:59 -05003673 lockdep_assert_held(&cgrp->pidlist_mutex);
3674
3675 l = cgroup_pidlist_find(cgrp, type);
3676 if (l)
3677 return l;
3678
Ben Blum72a8cb32009-09-23 15:56:27 -07003679 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003680 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003681 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003683
Tejun Heob1a21362013-11-29 10:42:58 -05003684 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003686 /* don't need task_nsproxy() if we're looking at ourself */
3687 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003688 l->owner = cgrp;
3689 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003690 return l;
3691}
3692
3693/*
Ben Blum102a7752009-09-23 15:56:26 -07003694 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3695 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003696static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3697 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003698{
3699 pid_t *array;
3700 int length;
3701 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003702 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003703 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003704 struct cgroup_pidlist *l;
3705
Tejun Heo4bac00d2013-11-29 10:42:59 -05003706 lockdep_assert_held(&cgrp->pidlist_mutex);
3707
Ben Blum102a7752009-09-23 15:56:26 -07003708 /*
3709 * If cgroup gets more users after we read count, we won't have
3710 * enough space - tough. This race is indistinguishable to the
3711 * caller from the case that the additional cgroup users didn't
3712 * show up until sometime later on.
3713 */
3714 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003715 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003716 if (!array)
3717 return -ENOMEM;
3718 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003719 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003720 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003721 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003722 break;
Ben Blum102a7752009-09-23 15:56:26 -07003723 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003724 if (type == CGROUP_FILE_PROCS)
3725 pid = task_tgid_vnr(tsk);
3726 else
3727 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003728 if (pid > 0) /* make sure to only use valid results */
3729 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003730 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003731 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003732 length = n;
3733 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003734 if (cgroup_sane_behavior(cgrp))
3735 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3736 else
3737 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003739 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003740
Tejun Heoe6b81712013-11-29 10:42:59 -05003741 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003742 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003743 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003744 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003745 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003746 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003747
3748 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003749 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003750 l->list = array;
3751 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003752 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003753 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003754}
3755
Balbir Singh846c7bb2007-10-18 23:39:44 -07003756/**
Li Zefana043e3b2008-02-23 15:24:09 -08003757 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003758 * @stats: cgroupstats to fill information into
3759 * @dentry: A dentry entry belonging to the cgroup for which stats have
3760 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003761 *
3762 * Build and fill cgroupstats so that taskstats can export it to user
3763 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003764 */
3765int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3766{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003767 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003768 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003769 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003770 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003771
Tejun Heo2bd59d42014-02-11 11:52:49 -05003772 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3773 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3774 kernfs_type(kn) != KERNFS_DIR)
3775 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003776
Li Zefanbad34662014-02-14 16:54:28 +08003777 mutex_lock(&cgroup_mutex);
3778
Tejun Heo2bd59d42014-02-11 11:52:49 -05003779 /*
3780 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003781 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003782 * @kn->priv is RCU safe. Let's do the RCU dancing.
3783 */
3784 rcu_read_lock();
3785 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003786 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003787 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003788 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003789 return -ENOENT;
3790 }
Li Zefanbad34662014-02-14 16:54:28 +08003791 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003792
Tejun Heo9d800df2014-05-14 09:15:00 -04003793 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003794 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003795 switch (tsk->state) {
3796 case TASK_RUNNING:
3797 stats->nr_running++;
3798 break;
3799 case TASK_INTERRUPTIBLE:
3800 stats->nr_sleeping++;
3801 break;
3802 case TASK_UNINTERRUPTIBLE:
3803 stats->nr_uninterruptible++;
3804 break;
3805 case TASK_STOPPED:
3806 stats->nr_stopped++;
3807 break;
3808 default:
3809 if (delayacct_is_task_waiting_on_io(tsk))
3810 stats->nr_io_wait++;
3811 break;
3812 }
3813 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003814 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003815
Li Zefanbad34662014-02-14 16:54:28 +08003816 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003817 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003818}
3819
Paul Menage8f3ff202009-09-23 15:56:25 -07003820
Paul Menagecc31edc2008-10-18 20:28:04 -07003821/*
Ben Blum102a7752009-09-23 15:56:26 -07003822 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003823 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003824 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003825 */
3826
Ben Blum102a7752009-09-23 15:56:26 -07003827static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003828{
3829 /*
3830 * Initially we receive a position value that corresponds to
3831 * one more than the last pid shown (or 0 on the first call or
3832 * after a seek to the start). Use a binary-search to find the
3833 * next pid to display, if any
3834 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003835 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003836 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003837 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003838 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003839 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003840 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003841
Tejun Heo4bac00d2013-11-29 10:42:59 -05003842 mutex_lock(&cgrp->pidlist_mutex);
3843
3844 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003845 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003846 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003847 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003848 * could already have been destroyed.
3849 */
Tejun Heo5d224442013-12-05 12:28:04 -05003850 if (of->priv)
3851 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003852
3853 /*
3854 * Either this is the first start() after open or the matching
3855 * pidlist has been destroyed inbetween. Create a new one.
3856 */
Tejun Heo5d224442013-12-05 12:28:04 -05003857 if (!of->priv) {
3858 ret = pidlist_array_load(cgrp, type,
3859 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003860 if (ret)
3861 return ERR_PTR(ret);
3862 }
Tejun Heo5d224442013-12-05 12:28:04 -05003863 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003864
Paul Menagecc31edc2008-10-18 20:28:04 -07003865 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003866 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003867
Paul Menagecc31edc2008-10-18 20:28:04 -07003868 while (index < end) {
3869 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003870 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003871 index = mid;
3872 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003873 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003874 index = mid + 1;
3875 else
3876 end = mid;
3877 }
3878 }
3879 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003880 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003881 return NULL;
3882 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003883 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003884 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003885 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003886}
3887
Ben Blum102a7752009-09-23 15:56:26 -07003888static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003889{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003890 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003891 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003892
Tejun Heo5d224442013-12-05 12:28:04 -05003893 if (l)
3894 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003895 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003896 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003897}
3898
Ben Blum102a7752009-09-23 15:56:26 -07003899static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003900{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003901 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003902 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003903 pid_t *p = v;
3904 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003905 /*
3906 * Advance to the next pid in the array. If this goes off the
3907 * end, we're done
3908 */
3909 p++;
3910 if (p >= end) {
3911 return NULL;
3912 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003913 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003914 return p;
3915 }
3916}
3917
Ben Blum102a7752009-09-23 15:56:26 -07003918static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003919{
3920 return seq_printf(s, "%d\n", *(int *)v);
3921}
3922
Tejun Heo182446d2013-08-08 20:11:24 -04003923static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3924 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003925{
Tejun Heo182446d2013-08-08 20:11:24 -04003926 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003927}
3928
Tejun Heo182446d2013-08-08 20:11:24 -04003929static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3930 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003931{
Tejun Heo182446d2013-08-08 20:11:24 -04003932 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003933 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003934 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003935 else
Tejun Heo182446d2013-08-08 20:11:24 -04003936 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003937 return 0;
3938}
3939
Tejun Heo182446d2013-08-08 20:11:24 -04003940static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3941 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003942{
Tejun Heo182446d2013-08-08 20:11:24 -04003943 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003944}
3945
Tejun Heo182446d2013-08-08 20:11:24 -04003946static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3947 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003948{
3949 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003950 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003951 else
Tejun Heo182446d2013-08-08 20:11:24 -04003952 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003953 return 0;
3954}
3955
Tejun Heod5c56ce2013-06-03 19:14:34 -07003956static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003957 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003958 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003959 .seq_start = cgroup_pidlist_start,
3960 .seq_next = cgroup_pidlist_next,
3961 .seq_stop = cgroup_pidlist_stop,
3962 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003963 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003964 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003965 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003966 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003967 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003968 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003969 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003970 .read_u64 = cgroup_clone_children_read,
3971 .write_u64 = cgroup_clone_children_write,
3972 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003973 {
Tejun Heo873fe092013-04-14 20:15:26 -07003974 .name = "cgroup.sane_behavior",
3975 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003976 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003977 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003978 {
3979 .name = "cgroup.controllers",
3980 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3981 .seq_show = cgroup_root_controllers_show,
3982 },
3983 {
3984 .name = "cgroup.controllers",
3985 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3986 .seq_show = cgroup_controllers_show,
3987 },
3988 {
3989 .name = "cgroup.subtree_control",
3990 .flags = CFTYPE_ONLY_ON_DFL,
3991 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04003992 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04003993 },
Tejun Heo842b5972014-04-25 18:28:02 -04003994 {
3995 .name = "cgroup.populated",
3996 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3997 .seq_show = cgroup_populated_show,
3998 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003999
4000 /*
4001 * Historical crazy stuff. These don't have "cgroup." prefix and
4002 * don't exist if sane_behavior. If you're depending on these, be
4003 * prepared to be burned.
4004 */
4005 {
4006 .name = "tasks",
4007 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004008 .seq_start = cgroup_pidlist_start,
4009 .seq_next = cgroup_pidlist_next,
4010 .seq_stop = cgroup_pidlist_stop,
4011 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004012 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004013 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004014 .mode = S_IRUGO | S_IWUSR,
4015 },
4016 {
4017 .name = "notify_on_release",
4018 .flags = CFTYPE_INSANE,
4019 .read_u64 = cgroup_read_notify_on_release,
4020 .write_u64 = cgroup_write_notify_on_release,
4021 },
Tejun Heo873fe092013-04-14 20:15:26 -07004022 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004023 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004024 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004025 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004026 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004027 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004028 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004029 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004030};
4031
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004032/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004033 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004034 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004035 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004036 *
4037 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004038 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004039static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004040{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004042 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004043
Tejun Heo8e3f6542012-04-01 12:09:55 -07004044 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004045 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004046 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004047
Tejun Heo69dfa002014-05-04 15:09:13 -04004048 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004049 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004050
Tejun Heo0adb0702014-02-12 09:29:48 -05004051 list_for_each_entry(cfts, &ss->cfts, node) {
4052 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004053 if (ret < 0)
4054 goto err;
4055 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004056 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004058err:
4059 cgroup_clear_dir(cgrp, subsys_mask);
4060 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004061}
4062
Tejun Heo0c21ead2013-08-13 20:22:51 -04004063/*
4064 * css destruction is four-stage process.
4065 *
4066 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4067 * Implemented in kill_css().
4068 *
4069 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004070 * and thus css_tryget_online() is guaranteed to fail, the css can be
4071 * offlined by invoking offline_css(). After offlining, the base ref is
4072 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004073 *
4074 * 3. When the percpu_ref reaches zero, the only possible remaining
4075 * accessors are inside RCU read sections. css_release() schedules the
4076 * RCU callback.
4077 *
4078 * 4. After the grace period, the css can be freed. Implemented in
4079 * css_free_work_fn().
4080 *
4081 * It is actually hairier because both step 2 and 4 require process context
4082 * and thus involve punting to css->destroy_work adding two additional
4083 * steps to the already complex sequence.
4084 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004085static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004086{
4087 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004088 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004089 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004090
Tejun Heo9d755d32014-05-14 09:15:02 -04004091 if (css->ss) {
4092 /* css free path */
4093 if (css->parent)
4094 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004095
Tejun Heo9d755d32014-05-14 09:15:02 -04004096 css->ss->css_free(css);
4097 cgroup_put(cgrp);
4098 } else {
4099 /* cgroup free path */
4100 atomic_dec(&cgrp->root->nr_cgrps);
4101 cgroup_pidlist_destroy_all(cgrp);
4102
Tejun Heod51f39b2014-05-16 13:22:48 -04004103 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004104 /*
4105 * We get a ref to the parent, and put the ref when
4106 * this cgroup is being freed, so it's guaranteed
4107 * that the parent won't be destroyed before its
4108 * children.
4109 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004110 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004111 kernfs_put(cgrp->kn);
4112 kfree(cgrp);
4113 } else {
4114 /*
4115 * This is root cgroup's refcnt reaching zero,
4116 * which indicates that the root should be
4117 * released.
4118 */
4119 cgroup_destroy_root(cgrp->root);
4120 }
4121 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004122}
4123
4124static void css_free_rcu_fn(struct rcu_head *rcu_head)
4125{
4126 struct cgroup_subsys_state *css =
4127 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4128
Tejun Heo0c21ead2013-08-13 20:22:51 -04004129 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004130 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004131}
4132
Tejun Heo25e15d82014-05-14 09:15:02 -04004133static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004134{
4135 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004136 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004137 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004138 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004139
Tejun Heo1fed1b22014-05-16 13:22:49 -04004140 mutex_lock(&cgroup_mutex);
4141
4142 list_del_rcu(&css->sibling);
4143
Tejun Heo9d755d32014-05-14 09:15:02 -04004144 if (ss) {
4145 /* css release path */
4146 cgroup_idr_remove(&ss->css_idr, css->id);
4147 } else {
4148 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004149 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4150 cgrp->id = -1;
4151 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004152
Tejun Heo1fed1b22014-05-16 13:22:49 -04004153 mutex_unlock(&cgroup_mutex);
4154
Tejun Heo0c21ead2013-08-13 20:22:51 -04004155 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004156}
4157
Tejun Heo25e15d82014-05-14 09:15:02 -04004158static void css_release(struct percpu_ref *ref)
4159{
4160 struct cgroup_subsys_state *css =
4161 container_of(ref, struct cgroup_subsys_state, refcnt);
4162
4163 INIT_WORK(&css->destroy_work, css_release_work_fn);
4164 queue_work(cgroup_destroy_wq, &css->destroy_work);
4165}
4166
Tejun Heoddfcada2014-05-04 15:09:14 -04004167static void init_and_link_css(struct cgroup_subsys_state *css,
4168 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004170 lockdep_assert_held(&cgroup_mutex);
4171
Tejun Heoddfcada2014-05-04 15:09:14 -04004172 cgroup_get(cgrp);
4173
Tejun Heod5c419b2014-05-16 13:22:48 -04004174 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004175 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004176 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004177 INIT_LIST_HEAD(&css->sibling);
4178 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004179 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004180
Tejun Heod51f39b2014-05-16 13:22:48 -04004181 if (cgroup_parent(cgrp)) {
4182 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004183 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004184 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004185
Tejun Heoca8bdca2013-08-26 18:40:56 -04004186 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187}
4188
Li Zefan2a4ac632013-07-31 16:16:40 +08004189/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004190static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004191{
Tejun Heo623f9262013-08-13 11:01:55 -04004192 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004193 int ret = 0;
4194
Tejun Heoa31f2d32012-11-19 08:13:37 -08004195 lockdep_assert_held(&cgroup_mutex);
4196
Tejun Heo92fb9742012-11-19 08:13:38 -08004197 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004198 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004199 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004200 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004201 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004202 }
Tejun Heob1929db2012-11-19 08:13:38 -08004203 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004204}
4205
Li Zefan2a4ac632013-07-31 16:16:40 +08004206/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004207static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004208{
Tejun Heo623f9262013-08-13 11:01:55 -04004209 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004210
4211 lockdep_assert_held(&cgroup_mutex);
4212
4213 if (!(css->flags & CSS_ONLINE))
4214 return;
4215
Li Zefand7eeac12013-03-12 15:35:59 -07004216 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004217 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004218
Tejun Heoeb954192013-08-08 20:11:23 -04004219 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004220 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004221
4222 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004223}
4224
Tejun Heoc81c925a2013-12-06 15:11:56 -05004225/**
4226 * create_css - create a cgroup_subsys_state
4227 * @cgrp: the cgroup new css will be associated with
4228 * @ss: the subsys of new css
4229 *
4230 * Create a new css associated with @cgrp - @ss pair. On success, the new
4231 * css is online and installed in @cgrp with all interface files created.
4232 * Returns 0 on success, -errno on failure.
4233 */
4234static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4235{
Tejun Heod51f39b2014-05-16 13:22:48 -04004236 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004237 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004238 struct cgroup_subsys_state *css;
4239 int err;
4240
Tejun Heoc81c925a2013-12-06 15:11:56 -05004241 lockdep_assert_held(&cgroup_mutex);
4242
Tejun Heo1fed1b22014-05-16 13:22:49 -04004243 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004244 if (IS_ERR(css))
4245 return PTR_ERR(css);
4246
Tejun Heoddfcada2014-05-04 15:09:14 -04004247 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004248
Tejun Heoc81c925a2013-12-06 15:11:56 -05004249 err = percpu_ref_init(&css->refcnt, css_release);
4250 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004251 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004252
Tejun Heo15a4c832014-05-04 15:09:14 -04004253 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4254 if (err < 0)
4255 goto err_free_percpu_ref;
4256 css->id = err;
4257
Tejun Heoaec25022014-02-08 10:36:58 -05004258 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004259 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004260 goto err_free_id;
4261
4262 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004263 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004264 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004265
4266 err = online_css(css);
4267 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004268 goto err_list_del;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004269
Tejun Heoc81c925a2013-12-06 15:11:56 -05004270 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004271 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004272 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 -04004273 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004274 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004275 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004276 ss->warned_broken_hierarchy = true;
4277 }
4278
4279 return 0;
4280
Tejun Heo1fed1b22014-05-16 13:22:49 -04004281err_list_del:
4282 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004283 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004284err_free_id:
4285 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004286err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004287 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004288err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004289 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004290 return err;
4291}
4292
Tejun Heob3bfd982014-05-13 12:19:22 -04004293static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4294 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004295{
Tejun Heoa9746d82014-05-13 12:19:22 -04004296 struct cgroup *parent, *cgrp;
4297 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004298 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004299 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004300 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004301
Tejun Heoa9746d82014-05-13 12:19:22 -04004302 parent = cgroup_kn_lock_live(parent_kn);
4303 if (!parent)
4304 return -ENODEV;
4305 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004306
4307 /* allocate the cgroup and its ID, 0 is reserved for the root */
4308 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4309 if (!cgrp) {
4310 ret = -ENOMEM;
4311 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004312 }
4313
Tejun Heo9d755d32014-05-14 09:15:02 -04004314 ret = percpu_ref_init(&cgrp->self.refcnt, css_release);
4315 if (ret)
4316 goto out_free_cgrp;
4317
Li Zefan0ab02ca2014-02-11 16:05:46 +08004318 /*
4319 * Temporarily set the pointer to NULL, so idr_find() won't return
4320 * a half-baked cgroup.
4321 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004322 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004323 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004324 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004325 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004326 }
4327
Paul Menagecc31edc2008-10-18 20:28:04 -07004328 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329
Tejun Heo9d800df2014-05-14 09:15:00 -04004330 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004331 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332
Li Zefanb6abdb02008-03-04 14:28:19 -08004333 if (notify_on_release(parent))
4334 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4335
Tejun Heo2260e7f2012-11-19 08:13:38 -08004336 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4337 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004338
Tejun Heo2bd59d42014-02-11 11:52:49 -05004339 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004340 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004341 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004342 ret = PTR_ERR(kn);
4343 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004344 }
4345 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004346
Tejun Heo6f305582014-02-12 09:29:50 -05004347 /*
4348 * This extra ref will be put in cgroup_free_fn() and guarantees
4349 * that @cgrp->kn is always accessible.
4350 */
4351 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352
Tejun Heo0cb51d72014-05-16 13:22:49 -04004353 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004354
Tejun Heo4e139af2012-11-19 08:13:36 -08004355 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004356 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004357 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004358 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004359
Tejun Heo0d802552013-12-06 15:11:56 -05004360 /*
4361 * @cgrp is now fully operational. If something fails after this
4362 * point, it'll be released via the normal destruction path.
4363 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004364 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004365
Tejun Heoba0f4d72014-05-13 12:19:22 -04004366 ret = cgroup_kn_set_ugid(kn);
4367 if (ret)
4368 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004369
Tejun Heoba0f4d72014-05-13 12:19:22 -04004370 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4371 if (ret)
4372 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004373
Tejun Heo9d403e92013-12-06 15:11:56 -05004374 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004375 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004376 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004377 ret = create_css(cgrp, ss);
4378 if (ret)
4379 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004380 }
Tejun Heoa8638032012-11-09 09:12:29 -08004381 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382
Tejun Heobd53d612014-04-23 11:13:16 -04004383 /*
4384 * On the default hierarchy, a child doesn't automatically inherit
4385 * child_subsys_mask from the parent. Each is configured manually.
4386 */
4387 if (!cgroup_on_dfl(cgrp))
4388 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004389
Tejun Heo2bd59d42014-02-11 11:52:49 -05004390 kernfs_activate(kn);
4391
Tejun Heoba0f4d72014-05-13 12:19:22 -04004392 ret = 0;
4393 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004394
Tejun Heoba0f4d72014-05-13 12:19:22 -04004395out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004396 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004397out_cancel_ref:
4398 percpu_ref_cancel_init(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004399out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004400 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004401out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004402 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004403 return ret;
4404
4405out_destroy:
4406 cgroup_destroy_locked(cgrp);
4407 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004408}
4409
Tejun Heo223dbc32013-08-13 20:22:50 -04004410/*
4411 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004412 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4413 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004414 */
4415static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004416{
Tejun Heo223dbc32013-08-13 20:22:50 -04004417 struct cgroup_subsys_state *css =
4418 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004419
Tejun Heof20104d2013-08-13 20:22:50 -04004420 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004421 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004422 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004423
Tejun Heo09a503ea2013-08-13 20:22:50 -04004424 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004425}
4426
Tejun Heo223dbc32013-08-13 20:22:50 -04004427/* css kill confirmation processing requires process context, bounce */
4428static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004429{
4430 struct cgroup_subsys_state *css =
4431 container_of(ref, struct cgroup_subsys_state, refcnt);
4432
Tejun Heo223dbc32013-08-13 20:22:50 -04004433 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004434 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004435}
4436
Tejun Heof392e512014-04-23 11:13:14 -04004437/**
4438 * kill_css - destroy a css
4439 * @css: css to destroy
4440 *
4441 * This function initiates destruction of @css by removing cgroup interface
4442 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004443 * asynchronously once css_tryget_online() is guaranteed to fail and when
4444 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004445 */
4446static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004447{
Tejun Heo01f64742014-05-13 12:19:23 -04004448 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004449
Tejun Heo2bd59d42014-02-11 11:52:49 -05004450 /*
4451 * This must happen before css is disassociated with its cgroup.
4452 * See seq_css() for details.
4453 */
Tejun Heoaec25022014-02-08 10:36:58 -05004454 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004455
Tejun Heoedae0c32013-08-13 20:22:51 -04004456 /*
4457 * Killing would put the base ref, but we need to keep it alive
4458 * until after ->css_offline().
4459 */
4460 css_get(css);
4461
4462 /*
4463 * cgroup core guarantees that, by the time ->css_offline() is
4464 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004465 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004466 * proceed to offlining css's because percpu_ref_kill() doesn't
4467 * guarantee that the ref is seen as killed on all CPUs on return.
4468 *
4469 * Use percpu_ref_kill_and_confirm() to get notifications as each
4470 * css is confirmed to be seen as killed on all CPUs.
4471 */
4472 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004473}
4474
4475/**
4476 * cgroup_destroy_locked - the first stage of cgroup destruction
4477 * @cgrp: cgroup to be destroyed
4478 *
4479 * css's make use of percpu refcnts whose killing latency shouldn't be
4480 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004481 * guarantee that css_tryget_online() won't succeed by the time
4482 * ->css_offline() is invoked. To satisfy all the requirements,
4483 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004484 *
4485 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4486 * userland visible parts and start killing the percpu refcnts of
4487 * css's. Set up so that the next stage will be kicked off once all
4488 * the percpu refcnts are confirmed to be killed.
4489 *
4490 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4491 * rest of destruction. Once all cgroup references are gone, the
4492 * cgroup is RCU-freed.
4493 *
4494 * This function implements s1. After this step, @cgrp is gone as far as
4495 * the userland is concerned and a new cgroup with the same name may be
4496 * created. As cgroup doesn't care about the names internally, this
4497 * doesn't cause any problem.
4498 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004499static int cgroup_destroy_locked(struct cgroup *cgrp)
4500 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004501{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004502 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004503 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004504 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004505
Tejun Heo42809dd2012-11-19 08:13:37 -08004506 lockdep_assert_held(&cgroup_mutex);
4507
Tejun Heoddd69142013-06-12 21:04:54 -07004508 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004509 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004510 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004511 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004512 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004513 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004514 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004515 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004516 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004517
Tejun Heo1a90dd52012-11-05 09:16:59 -08004518 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004519 * Make sure there's no live children. We can't test emptiness of
4520 * ->self.children as dead children linger on it while being
4521 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004522 */
Tejun Heocbc125e2014-05-14 09:15:01 -04004523 if (cgroup_has_live_children(cgrp))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004524 return -EBUSY;
4525
4526 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004527 * Mark @cgrp dead. This prevents further task migration and child
4528 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004529 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004530 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004531 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004532 */
Tejun Heo54766d42013-06-12 21:04:53 -07004533 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004534
Tejun Heo249f3462014-05-14 09:15:01 -04004535 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004536 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004537 kill_css(css);
4538
Tejun Heo455050d2013-06-13 19:27:41 -07004539 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4540 raw_spin_lock(&release_list_lock);
4541 if (!list_empty(&cgrp->release_list))
4542 list_del_init(&cgrp->release_list);
4543 raw_spin_unlock(&release_list_lock);
4544
4545 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004546 * Remove @cgrp directory along with the base files. @cgrp has an
4547 * extra ref on its kn.
4548 */
4549 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004550
Tejun Heod51f39b2014-05-16 13:22:48 -04004551 set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags);
4552 check_for_release(cgroup_parent(cgrp));
Tejun Heo9e4173e2014-05-14 09:15:01 -04004553
Tejun Heo249f3462014-05-14 09:15:01 -04004554 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004555 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo249f3462014-05-14 09:15:01 -04004556
Tejun Heoea15f8c2013-06-13 19:27:42 -07004557 return 0;
4558};
4559
Tejun Heo2bd59d42014-02-11 11:52:49 -05004560static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004561{
Tejun Heoa9746d82014-05-13 12:19:22 -04004562 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004563 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004564
Tejun Heoa9746d82014-05-13 12:19:22 -04004565 cgrp = cgroup_kn_lock_live(kn);
4566 if (!cgrp)
4567 return 0;
4568 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004569
Tejun Heoa9746d82014-05-13 12:19:22 -04004570 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004571
Tejun Heoa9746d82014-05-13 12:19:22 -04004572 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004573
4574 /*
4575 * There are two control paths which try to determine cgroup from
4576 * dentry without going through kernfs - cgroupstats_build() and
4577 * css_tryget_online_from_dir(). Those are supported by RCU
4578 * protecting clearing of cgrp->kn->priv backpointer, which should
4579 * happen after all files under it have been removed.
4580 */
4581 if (!ret)
4582 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4583
Tejun Heo2bd59d42014-02-11 11:52:49 -05004584 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004585 return ret;
4586}
4587
Tejun Heo2bd59d42014-02-11 11:52:49 -05004588static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4589 .remount_fs = cgroup_remount,
4590 .show_options = cgroup_show_options,
4591 .mkdir = cgroup_mkdir,
4592 .rmdir = cgroup_rmdir,
4593 .rename = cgroup_rename,
4594};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004595
Tejun Heo15a4c832014-05-04 15:09:14 -04004596static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004597{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004598 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004599
4600 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601
Tejun Heo648bb562012-11-19 08:13:36 -08004602 mutex_lock(&cgroup_mutex);
4603
Tejun Heo15a4c832014-05-04 15:09:14 -04004604 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004605 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004606
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004607 /* Create the root cgroup state for this subsystem */
4608 ss->root = &cgrp_dfl_root;
4609 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 /* We don't handle early failures gracefully */
4611 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004612 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004613
4614 /*
4615 * Root csses are never destroyed and we can't initialize
4616 * percpu_ref during early init. Disable refcnting.
4617 */
4618 css->flags |= CSS_NO_REF;
4619
Tejun Heo15a4c832014-05-04 15:09:14 -04004620 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004621 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004622 css->id = 1;
4623 } else {
4624 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4625 BUG_ON(css->id < 0);
4626 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627
Li Zefane8d55fd2008-04-29 01:00:13 -07004628 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004629 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004630 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004631 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004632 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004633
4634 need_forkexit_callback |= ss->fork || ss->exit;
4635
Li Zefane8d55fd2008-04-29 01:00:13 -07004636 /* At system boot, before all subsystems have been
4637 * registered, no tasks have been forked, so we don't
4638 * need to invoke fork callbacks here. */
4639 BUG_ON(!list_empty(&init_task.tasks));
4640
Tejun Heoae7f1642013-08-13 20:22:50 -04004641 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004642
Tejun Heof392e512014-04-23 11:13:14 -04004643 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004644
Ben Blume6a11052010-03-10 15:22:09 -08004645 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646}
4647
4648/**
Li Zefana043e3b2008-02-23 15:24:09 -08004649 * cgroup_init_early - cgroup initialization at system boot
4650 *
4651 * Initialize cgroups at system boot, and initialize any
4652 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653 */
4654int __init cgroup_init_early(void)
4655{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004656 static struct cgroup_sb_opts __initdata opts =
4657 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004658 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004660
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004661 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004662 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4663
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004664 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004665
Tejun Heo3ed80a62014-02-08 10:36:58 -05004666 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004667 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004668 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4669 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004670 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004671 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4672 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673
Tejun Heoaec25022014-02-08 10:36:58 -05004674 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004675 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676
4677 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004678 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679 }
4680 return 0;
4681}
4682
4683/**
Li Zefana043e3b2008-02-23 15:24:09 -08004684 * cgroup_init - cgroup initialization
4685 *
4686 * Register cgroup filesystem and /proc file, and initialize
4687 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688 */
4689int __init cgroup_init(void)
4690{
Tejun Heo30159ec2013-06-25 11:53:37 -07004691 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004692 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004693 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004694
Tejun Heo2bd59d42014-02-11 11:52:49 -05004695 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004696
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004697 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004698
Tejun Heo82fe9b02013-06-25 11:53:37 -07004699 /* Add init_css_set to the hash table */
4700 key = css_set_hash(init_css_set.subsys);
4701 hash_add(css_set_table, &init_css_set.hlist, key);
4702
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004703 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004704
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004705 mutex_unlock(&cgroup_mutex);
4706
Tejun Heo172a2c062014-03-19 10:23:53 -04004707 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004708 if (ss->early_init) {
4709 struct cgroup_subsys_state *css =
4710 init_css_set.subsys[ss->id];
4711
4712 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4713 GFP_KERNEL);
4714 BUG_ON(css->id < 0);
4715 } else {
4716 cgroup_init_subsys(ss, false);
4717 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004718
Tejun Heo2d8f2432014-04-23 11:13:15 -04004719 list_add_tail(&init_css_set.e_cset_node[ssid],
4720 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4721
Tejun Heo172a2c062014-03-19 10:23:53 -04004722 /*
4723 * cftype registration needs kmalloc and can't be done
4724 * during early_init. Register base cftypes separately.
4725 */
4726 if (ss->base_cftypes)
4727 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4728 }
Greg KH676db4a2010-08-05 13:53:35 -07004729
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004731 if (!cgroup_kobj)
4732 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004733
4734 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004735 if (err < 0) {
4736 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004737 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004738 }
4739
4740 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004741 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742}
Paul Menageb4f48b62007-10-18 23:39:33 -07004743
Tejun Heoe5fca242013-11-22 17:14:39 -05004744static int __init cgroup_wq_init(void)
4745{
4746 /*
4747 * There isn't much point in executing destruction path in
4748 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004749 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004750 *
4751 * We would prefer to do this in cgroup_init() above, but that
4752 * is called before init_workqueues(): so leave this until after.
4753 */
Tejun Heo1a115332014-02-12 19:06:19 -05004754 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004755 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004756
4757 /*
4758 * Used to destroy pidlists and separate to serve as flush domain.
4759 * Cap @max_active to 1 too.
4760 */
4761 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4762 0, 1);
4763 BUG_ON(!cgroup_pidlist_destroy_wq);
4764
Tejun Heoe5fca242013-11-22 17:14:39 -05004765 return 0;
4766}
4767core_initcall(cgroup_wq_init);
4768
Paul Menagea4243162007-10-18 23:39:35 -07004769/*
4770 * proc_cgroup_show()
4771 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4772 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004773 */
4774
4775/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004776int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004777{
4778 struct pid *pid;
4779 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004780 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004781 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004782 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004783
4784 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004785 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004786 if (!buf)
4787 goto out;
4788
4789 retval = -ESRCH;
4790 pid = m->private;
4791 tsk = get_pid_task(pid, PIDTYPE_PID);
4792 if (!tsk)
4793 goto out_free;
4794
4795 retval = 0;
4796
4797 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004798 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004799
Tejun Heo985ed672014-03-19 10:23:53 -04004800 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004801 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004802 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004803 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004804
Tejun Heoa2dd4242014-03-19 10:23:55 -04004805 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004806 continue;
4807
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004808 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004809 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004810 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004811 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004812 if (strlen(root->name))
4813 seq_printf(m, "%sname=%s", count ? "," : "",
4814 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004815 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004816 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004817 path = cgroup_path(cgrp, buf, PATH_MAX);
4818 if (!path) {
4819 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004820 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004821 }
4822 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004823 seq_putc(m, '\n');
4824 }
4825
4826out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004827 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004828 mutex_unlock(&cgroup_mutex);
4829 put_task_struct(tsk);
4830out_free:
4831 kfree(buf);
4832out:
4833 return retval;
4834}
4835
Paul Menagea4243162007-10-18 23:39:35 -07004836/* Display information about each subsystem and each hierarchy */
4837static int proc_cgroupstats_show(struct seq_file *m, void *v)
4838{
Tejun Heo30159ec2013-06-25 11:53:37 -07004839 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004840 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004841
Paul Menage8bab8dd2008-04-04 14:29:57 -07004842 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004843 /*
4844 * ideally we don't want subsystems moving around while we do this.
4845 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4846 * subsys/hierarchy state.
4847 */
Paul Menagea4243162007-10-18 23:39:35 -07004848 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004849
4850 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004851 seq_printf(m, "%s\t%d\t%d\t%d\n",
4852 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004853 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004854
Paul Menagea4243162007-10-18 23:39:35 -07004855 mutex_unlock(&cgroup_mutex);
4856 return 0;
4857}
4858
4859static int cgroupstats_open(struct inode *inode, struct file *file)
4860{
Al Viro9dce07f2008-03-29 03:07:28 +00004861 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004862}
4863
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004864static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004865 .open = cgroupstats_open,
4866 .read = seq_read,
4867 .llseek = seq_lseek,
4868 .release = single_release,
4869};
4870
Paul Menageb4f48b62007-10-18 23:39:33 -07004871/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004872 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004873 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004874 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004875 * A task is associated with the init_css_set until cgroup_post_fork()
4876 * attaches it to the parent's css_set. Empty cg_list indicates that
4877 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004878 */
4879void cgroup_fork(struct task_struct *child)
4880{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004881 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004882 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004883}
4884
4885/**
Li Zefana043e3b2008-02-23 15:24:09 -08004886 * cgroup_post_fork - called on a new task after adding it to the task list
4887 * @child: the task in question
4888 *
Tejun Heo5edee612012-10-16 15:03:14 -07004889 * Adds the task to the list running through its css_set if necessary and
4890 * call the subsystem fork() callbacks. Has to be after the task is
4891 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004892 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004893 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004894 */
Paul Menage817929e2007-10-18 23:39:36 -07004895void cgroup_post_fork(struct task_struct *child)
4896{
Tejun Heo30159ec2013-06-25 11:53:37 -07004897 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004898 int i;
4899
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004900 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004901 * This may race against cgroup_enable_task_cg_links(). As that
4902 * function sets use_task_css_set_links before grabbing
4903 * tasklist_lock and we just went through tasklist_lock to add
4904 * @child, it's guaranteed that either we see the set
4905 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4906 * @child during its iteration.
4907 *
4908 * If we won the race, @child is associated with %current's
4909 * css_set. Grabbing css_set_rwsem guarantees both that the
4910 * association is stable, and, on completion of the parent's
4911 * migration, @child is visible in the source of migration or
4912 * already in the destination cgroup. This guarantee is necessary
4913 * when implementing operations which need to migrate all tasks of
4914 * a cgroup to another.
4915 *
4916 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4917 * will remain in init_css_set. This is safe because all tasks are
4918 * in the init_css_set before cg_links is enabled and there's no
4919 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004920 */
Paul Menage817929e2007-10-18 23:39:36 -07004921 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004922 struct css_set *cset;
4923
Tejun Heo96d365e2014-02-13 06:58:40 -05004924 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004925 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004926 if (list_empty(&child->cg_list)) {
4927 rcu_assign_pointer(child->cgroups, cset);
4928 list_add(&child->cg_list, &cset->tasks);
4929 get_css_set(cset);
4930 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004931 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004932 }
Tejun Heo5edee612012-10-16 15:03:14 -07004933
4934 /*
4935 * Call ss->fork(). This must happen after @child is linked on
4936 * css_set; otherwise, @child might change state between ->fork()
4937 * and addition to css_set.
4938 */
4939 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004940 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004941 if (ss->fork)
4942 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004943 }
Paul Menage817929e2007-10-18 23:39:36 -07004944}
Tejun Heo5edee612012-10-16 15:03:14 -07004945
Paul Menage817929e2007-10-18 23:39:36 -07004946/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004947 * cgroup_exit - detach cgroup from exiting task
4948 * @tsk: pointer to task_struct of exiting process
4949 *
4950 * Description: Detach cgroup from @tsk and release it.
4951 *
4952 * Note that cgroups marked notify_on_release force every task in
4953 * them to take the global cgroup_mutex mutex when exiting.
4954 * This could impact scaling on very large systems. Be reluctant to
4955 * use notify_on_release cgroups where very high task exit scaling
4956 * is required on large systems.
4957 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004958 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4959 * call cgroup_exit() while the task is still competent to handle
4960 * notify_on_release(), then leave the task attached to the root cgroup in
4961 * each hierarchy for the remainder of its exit. No need to bother with
4962 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004963 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004964 */
Li Zefan1ec41832014-03-28 15:22:19 +08004965void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004966{
Tejun Heo30159ec2013-06-25 11:53:37 -07004967 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004968 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004969 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004970 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004971
4972 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004973 * Unlink from @tsk from its css_set. As migration path can't race
4974 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004975 */
4976 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004977 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004978 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004979 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004980 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004981 }
4982
Paul Menageb4f48b62007-10-18 23:39:33 -07004983 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004984 cset = task_css_set(tsk);
4985 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004986
Li Zefan1ec41832014-03-28 15:22:19 +08004987 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004988 /* see cgroup_post_fork() for details */
4989 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004990 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004991 struct cgroup_subsys_state *old_css = cset->subsys[i];
4992 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004993
Tejun Heoeb954192013-08-08 20:11:23 -04004994 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004995 }
4996 }
4997 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004998
Tejun Heoeaf797a2014-02-25 10:04:03 -05004999 if (put_cset)
5000 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005001}
Paul Menage697f4162007-10-18 23:39:34 -07005002
Paul Menagebd89aab2007-10-18 23:40:44 -07005003static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005004{
Li Zefanf50daa72013-03-01 15:06:07 +08005005 if (cgroup_is_releasable(cgrp) &&
Tejun Heo9e4173e2014-05-14 09:15:01 -04005006 list_empty(&cgrp->cset_links) && !cgroup_has_live_children(cgrp)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005007 /*
5008 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005009 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005010 * it now
5011 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005012 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005013
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005014 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005015 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005016 list_empty(&cgrp->release_list)) {
5017 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005018 need_schedule_work = 1;
5019 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005020 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 if (need_schedule_work)
5022 schedule_work(&release_agent_work);
5023 }
5024}
5025
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026/*
5027 * Notify userspace when a cgroup is released, by running the
5028 * configured release agent with the name of the cgroup (path
5029 * relative to the root of cgroup file system) as the argument.
5030 *
5031 * Most likely, this user command will try to rmdir this cgroup.
5032 *
5033 * This races with the possibility that some other task will be
5034 * attached to this cgroup before it is removed, or that some other
5035 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5036 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5037 * unused, and this cgroup will be reprieved from its death sentence,
5038 * to continue to serve a useful existence. Next time it's released,
5039 * we will get notified again, if it still has 'notify_on_release' set.
5040 *
5041 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5042 * means only wait until the task is successfully execve()'d. The
5043 * separate release agent task is forked by call_usermodehelper(),
5044 * then control in this thread returns here, without waiting for the
5045 * release agent task. We don't bother to wait because the caller of
5046 * this routine has no use for the exit status of the release agent
5047 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005048 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049static void cgroup_release_agent(struct work_struct *work)
5050{
5051 BUG_ON(work != &release_agent_work);
5052 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005053 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005054 while (!list_empty(&release_list)) {
5055 char *argv[3], *envp[3];
5056 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005057 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005058 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005059 struct cgroup,
5060 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005061 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005062 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005063 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005064 if (!pathbuf)
5065 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005066 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5067 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005068 goto continue_free;
5069 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5070 if (!agentbuf)
5071 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072
5073 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005074 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005075 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 argv[i] = NULL;
5077
5078 i = 0;
5079 /* minimal command environment */
5080 envp[i++] = "HOME=/";
5081 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5082 envp[i] = NULL;
5083
5084 /* Drop the lock while we invoke the usermode helper,
5085 * since the exec could involve hitting disk and hence
5086 * be a slow process */
5087 mutex_unlock(&cgroup_mutex);
5088 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005089 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005090 continue_free:
5091 kfree(pathbuf);
5092 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005093 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005094 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005095 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005096 mutex_unlock(&cgroup_mutex);
5097}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005098
5099static int __init cgroup_disable(char *str)
5100{
Tejun Heo30159ec2013-06-25 11:53:37 -07005101 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005102 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005103 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005104
5105 while ((token = strsep(&str, ",")) != NULL) {
5106 if (!*token)
5107 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005108
Tejun Heo3ed80a62014-02-08 10:36:58 -05005109 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005110 if (!strcmp(token, ss->name)) {
5111 ss->disabled = 1;
5112 printk(KERN_INFO "Disabling %s control group"
5113 " subsystem\n", ss->name);
5114 break;
5115 }
5116 }
5117 }
5118 return 1;
5119}
5120__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005121
Tejun Heob77d7b62013-08-13 11:01:54 -04005122/**
Tejun Heoec903c02014-05-13 12:11:01 -04005123 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005124 * @dentry: directory dentry of interest
5125 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005126 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005127 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5128 * to get the corresponding css and return it. If such css doesn't exist
5129 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005130 */
Tejun Heoec903c02014-05-13 12:11:01 -04005131struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5132 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005133{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005134 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5135 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005136 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005137
Tejun Heo35cf0832013-08-26 18:40:56 -04005138 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005139 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5140 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005141 return ERR_PTR(-EBADF);
5142
Tejun Heo5a17f542014-02-11 11:52:47 -05005143 rcu_read_lock();
5144
Tejun Heo2bd59d42014-02-11 11:52:49 -05005145 /*
5146 * This path doesn't originate from kernfs and @kn could already
5147 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005148 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005149 */
5150 cgrp = rcu_dereference(kn->priv);
5151 if (cgrp)
5152 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005153
Tejun Heoec903c02014-05-13 12:11:01 -04005154 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005155 css = ERR_PTR(-ENOENT);
5156
5157 rcu_read_unlock();
5158 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005159}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005160
Li Zefan1cb650b2013-08-19 10:05:24 +08005161/**
5162 * css_from_id - lookup css by id
5163 * @id: the cgroup id
5164 * @ss: cgroup subsys to be looked into
5165 *
5166 * Returns the css if there's valid one with @id, otherwise returns NULL.
5167 * Should be called under rcu_read_lock().
5168 */
5169struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5170{
Tejun Heo6fa49182014-05-04 15:09:13 -04005171 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005172 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005173}
5174
Paul Menagefe693432009-09-23 15:56:20 -07005175#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005176static struct cgroup_subsys_state *
5177debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005178{
5179 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5180
5181 if (!css)
5182 return ERR_PTR(-ENOMEM);
5183
5184 return css;
5185}
5186
Tejun Heoeb954192013-08-08 20:11:23 -04005187static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005188{
Tejun Heoeb954192013-08-08 20:11:23 -04005189 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005190}
5191
Tejun Heo182446d2013-08-08 20:11:24 -04005192static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5193 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005194{
Tejun Heo182446d2013-08-08 20:11:24 -04005195 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005196}
5197
Tejun Heo182446d2013-08-08 20:11:24 -04005198static u64 current_css_set_read(struct cgroup_subsys_state *css,
5199 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005200{
5201 return (u64)(unsigned long)current->cgroups;
5202}
5203
Tejun Heo182446d2013-08-08 20:11:24 -04005204static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005205 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005206{
5207 u64 count;
5208
5209 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005210 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005211 rcu_read_unlock();
5212 return count;
5213}
5214
Tejun Heo2da8ca82013-12-05 12:28:04 -05005215static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005216{
Tejun Heo69d02062013-06-12 21:04:50 -07005217 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005218 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005219 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005220
Tejun Heoe61734c2014-02-12 09:29:50 -05005221 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5222 if (!name_buf)
5223 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005224
Tejun Heo96d365e2014-02-13 06:58:40 -05005225 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005226 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005227 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005228 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005229 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005230
Tejun Heoa2dd4242014-03-19 10:23:55 -04005231 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005232 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005233 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005234 }
5235 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005236 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005237 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005238 return 0;
5239}
5240
5241#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005242static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005243{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005244 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005245 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005246
Tejun Heo96d365e2014-02-13 06:58:40 -05005247 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005248 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005249 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005250 struct task_struct *task;
5251 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005252
Tejun Heo5abb8852013-06-12 21:04:49 -07005253 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005254
Tejun Heo5abb8852013-06-12 21:04:49 -07005255 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005256 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5257 goto overflow;
5258 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005259 }
Tejun Heoc7561122014-02-25 10:04:01 -05005260
5261 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5262 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5263 goto overflow;
5264 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5265 }
5266 continue;
5267 overflow:
5268 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005269 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005270 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005271 return 0;
5272}
5273
Tejun Heo182446d2013-08-08 20:11:24 -04005274static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005275{
Tejun Heo182446d2013-08-08 20:11:24 -04005276 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005277}
5278
5279static struct cftype debug_files[] = {
5280 {
Paul Menagefe693432009-09-23 15:56:20 -07005281 .name = "taskcount",
5282 .read_u64 = debug_taskcount_read,
5283 },
5284
5285 {
5286 .name = "current_css_set",
5287 .read_u64 = current_css_set_read,
5288 },
5289
5290 {
5291 .name = "current_css_set_refcount",
5292 .read_u64 = current_css_set_refcount_read,
5293 },
5294
5295 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005296 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005297 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005298 },
5299
5300 {
5301 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005302 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005303 },
5304
5305 {
Paul Menagefe693432009-09-23 15:56:20 -07005306 .name = "releasable",
5307 .read_u64 = releasable_read,
5308 },
Paul Menagefe693432009-09-23 15:56:20 -07005309
Tejun Heo4baf6e32012-04-01 12:09:55 -07005310 { } /* terminate */
5311};
Paul Menagefe693432009-09-23 15:56:20 -07005312
Tejun Heo073219e2014-02-08 10:36:58 -05005313struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005314 .css_alloc = debug_css_alloc,
5315 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005316 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005317};
5318#endif /* CONFIG_CGROUP_DEBUG */