blob: 3edcc8ae83b56e37f4df9c75a6486bc5b6be75bb [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
Tejun Heo5533e012014-05-14 19:33:07 -0400151/* some controllers are not supported in the default hierarchy */
152static const unsigned int cgrp_dfl_root_inhibit_ss_mask = 0
153#ifdef CONFIG_CGROUP_DEBUG
154 | (1 << debug_cgrp_id)
155#endif
156 ;
157
Paul Menageddbcc7e2007-10-18 23:39:30 -0700158/* The list of hierarchy roots */
159
Tejun Heo9871bf92013-06-24 15:21:47 -0700160static LIST_HEAD(cgroup_roots);
161static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162
Tejun Heo3417ae12014-02-08 10:37:01 -0500163/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700164static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700165
Li Zefan794611a2013-06-18 18:53:53 +0800166/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400167 * Assign a monotonically increasing serial number to csses. It guarantees
168 * cgroups with bigger numbers are newer than those with smaller numbers.
169 * Also, as csses are always appended to the parent's ->children list, it
170 * guarantees that sibling csses are always sorted in the ascending serial
171 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800172 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400173static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800174
Paul Menageddbcc7e2007-10-18 23:39:30 -0700175/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800176 * check for fork/exit handlers to call. This avoids us having to do
177 * extra work in the fork/exit path if none of the subsystems need to
178 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700180static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700181
Tejun Heo628f7cd2013-06-28 16:24:11 -0700182static struct cftype cgroup_base_files[];
183
Tejun Heo59f52962014-02-11 11:52:49 -0500184static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400185static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400186 unsigned int ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800187static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400188static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400189static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400190static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400191static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
192 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500193static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800194
Tejun Heo6fa49182014-05-04 15:09:13 -0400195/* IDR wrappers which synchronize using cgroup_idr_lock */
196static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
197 gfp_t gfp_mask)
198{
199 int ret;
200
201 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400202 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400203 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400204 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400205 idr_preload_end();
206 return ret;
207}
208
209static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
210{
211 void *ret;
212
Tejun Heo54504e92014-05-13 12:10:59 -0400213 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400214 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400215 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400216 return ret;
217}
218
219static void cgroup_idr_remove(struct idr *idr, int id)
220{
Tejun Heo54504e92014-05-13 12:10:59 -0400221 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400222 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400223 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400224}
225
Tejun Heod51f39b2014-05-16 13:22:48 -0400226static struct cgroup *cgroup_parent(struct cgroup *cgrp)
227{
228 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
229
230 if (parent_css)
231 return container_of(parent_css, struct cgroup, self);
232 return NULL;
233}
234
Tejun Heo95109b62013-08-08 20:11:27 -0400235/**
236 * cgroup_css - obtain a cgroup's css for the specified subsystem
237 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400238 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400239 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400240 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
241 * function must be called either under cgroup_mutex or rcu_read_lock() and
242 * the caller is responsible for pinning the returned css if it wants to
243 * keep accessing it outside the said locks. This function may return
244 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400245 */
246static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400247 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400248{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400249 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500250 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500251 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400252 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400253 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400254}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700255
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400256/**
257 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
258 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400259 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400260 *
261 * Similar to cgroup_css() but returns the effctive css, which is defined
262 * as the matching css of the nearest ancestor including self which has @ss
263 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
264 * function is guaranteed to return non-NULL css.
265 */
266static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
267 struct cgroup_subsys *ss)
268{
269 lockdep_assert_held(&cgroup_mutex);
270
271 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400272 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400273
274 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
275 return NULL;
276
Tejun Heod51f39b2014-05-16 13:22:48 -0400277 while (cgroup_parent(cgrp) &&
278 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
279 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400280
281 return cgroup_css(cgrp, ss);
282}
283
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700285static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700286{
Tejun Heo184faf32014-05-16 13:22:51 -0400287 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700288}
289
Tejun Heob4168642014-05-13 12:16:21 -0400290struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500291{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500292 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400293 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500294
295 /*
296 * This is open and unprotected implementation of cgroup_css().
297 * seq_css() is only called from a kernfs file operation which has
298 * an active reference on the file. Because all the subsystem
299 * files are drained before a css is disassociated with a cgroup,
300 * the matching css from the cgroup's subsys table is guaranteed to
301 * be and stay valid until the enclosing operation is complete.
302 */
303 if (cft->ss)
304 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
305 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400306 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500307}
Tejun Heob4168642014-05-13 12:16:21 -0400308EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500309
Li Zefan78574cf2013-04-08 19:00:38 -0700310/**
311 * cgroup_is_descendant - test ancestry
312 * @cgrp: the cgroup to be tested
313 * @ancestor: possible ancestor of @cgrp
314 *
315 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
316 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
317 * and @ancestor are accessible.
318 */
319bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
320{
321 while (cgrp) {
322 if (cgrp == ancestor)
323 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400324 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700325 }
326 return false;
327}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700328
Adrian Bunke9685a02008-02-07 00:13:46 -0800329static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700330{
331 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700332 (1 << CGRP_RELEASABLE) |
333 (1 << CGRP_NOTIFY_ON_RELEASE);
334 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335}
336
Adrian Bunke9685a02008-02-07 00:13:46 -0800337static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700338{
Paul Menagebd89aab2007-10-18 23:40:44 -0700339 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700340}
341
Tejun Heo30159ec2013-06-25 11:53:37 -0700342/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500343 * for_each_css - iterate all css's of a cgroup
344 * @css: the iteration cursor
345 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
346 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700347 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400348 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700349 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500350#define for_each_css(css, ssid, cgrp) \
351 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
352 if (!((css) = rcu_dereference_check( \
353 (cgrp)->subsys[(ssid)], \
354 lockdep_is_held(&cgroup_mutex)))) { } \
355 else
356
357/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400358 * for_each_e_css - iterate all effective css's of a cgroup
359 * @css: the iteration cursor
360 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
361 * @cgrp: the target cgroup to iterate css's of
362 *
363 * Should be called under cgroup_[tree_]mutex.
364 */
365#define for_each_e_css(css, ssid, cgrp) \
366 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
367 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
368 ; \
369 else
370
371/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500372 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700373 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500374 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700375 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500376#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500377 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
378 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700379
Tejun Heo985ed672014-03-19 10:23:53 -0400380/* iterate across the hierarchies */
381#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700382 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700383
Tejun Heof8f22e52014-04-23 11:13:16 -0400384/* iterate over child cgrps, lock should be held throughout iteration */
385#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400386 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400387 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400388 cgroup_is_dead(child); })) \
389 ; \
390 else
391
Paul Menage81a6a5c2007-10-18 23:39:38 -0700392/* the list of cgroups eligible for automatic release. Protected by
393 * release_list_lock */
394static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200395static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700396static void cgroup_release_agent(struct work_struct *work);
397static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700398static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700399
Tejun Heo69d02062013-06-12 21:04:50 -0700400/*
401 * A cgroup can be associated with multiple css_sets as different tasks may
402 * belong to different cgroups on different hierarchies. In the other
403 * direction, a css_set is naturally associated with multiple cgroups.
404 * This M:N relationship is represented by the following link structure
405 * which exists for each association and allows traversing the associations
406 * from both sides.
407 */
408struct cgrp_cset_link {
409 /* the cgroup and css_set this link associates */
410 struct cgroup *cgrp;
411 struct css_set *cset;
412
413 /* list of cgrp_cset_links anchored at cgrp->cset_links */
414 struct list_head cset_link;
415
416 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
417 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700418};
419
Tejun Heo172a2c062014-03-19 10:23:53 -0400420/*
421 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700422 * hierarchies being mounted. It contains a pointer to the root state
423 * for each subsystem. Also used to anchor the list of css_sets. Not
424 * reference-counted, to improve performance when child cgroups
425 * haven't been created.
426 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400427struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400428 .refcount = ATOMIC_INIT(1),
429 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
430 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
431 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
432 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
433 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
434};
Paul Menage817929e2007-10-18 23:39:36 -0700435
Tejun Heo172a2c062014-03-19 10:23:53 -0400436static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700437
Tejun Heo842b5972014-04-25 18:28:02 -0400438/**
439 * cgroup_update_populated - updated populated count of a cgroup
440 * @cgrp: the target cgroup
441 * @populated: inc or dec populated count
442 *
443 * @cgrp is either getting the first task (css_set) or losing the last.
444 * Update @cgrp->populated_cnt accordingly. The count is propagated
445 * towards root so that a given cgroup's populated_cnt is zero iff the
446 * cgroup and all its descendants are empty.
447 *
448 * @cgrp's interface file "cgroup.populated" is zero if
449 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
450 * changes from or to zero, userland is notified that the content of the
451 * interface file has changed. This can be used to detect when @cgrp and
452 * its descendants become populated or empty.
453 */
454static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
455{
456 lockdep_assert_held(&css_set_rwsem);
457
458 do {
459 bool trigger;
460
461 if (populated)
462 trigger = !cgrp->populated_cnt++;
463 else
464 trigger = !--cgrp->populated_cnt;
465
466 if (!trigger)
467 break;
468
469 if (cgrp->populated_kn)
470 kernfs_notify(cgrp->populated_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400471 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400472 } while (cgrp);
473}
474
Paul Menage7717f7b2009-09-23 15:56:22 -0700475/*
476 * hash table for cgroup groups. This improves the performance to find
477 * an existing css_set. This hash doesn't (currently) take into
478 * account cgroups in empty hierarchies.
479 */
Li Zefan472b1052008-04-29 01:00:11 -0700480#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800481static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700482
Li Zefan0ac801f2013-01-10 11:49:27 +0800483static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700484{
Li Zefan0ac801f2013-01-10 11:49:27 +0800485 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700486 struct cgroup_subsys *ss;
487 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700488
Tejun Heo30159ec2013-06-25 11:53:37 -0700489 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800490 key += (unsigned long)css[i];
491 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700492
Li Zefan0ac801f2013-01-10 11:49:27 +0800493 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700494}
495
Tejun Heo89c55092014-02-13 06:58:40 -0500496static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700497{
Tejun Heo69d02062013-06-12 21:04:50 -0700498 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400499 struct cgroup_subsys *ss;
500 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700501
Tejun Heo89c55092014-02-13 06:58:40 -0500502 lockdep_assert_held(&css_set_rwsem);
503
504 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700505 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700506
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700507 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400508 for_each_subsys(ss, ssid)
509 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700510 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700511 css_set_count--;
512
Tejun Heo69d02062013-06-12 21:04:50 -0700513 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700514 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700515
Tejun Heo69d02062013-06-12 21:04:50 -0700516 list_del(&link->cset_link);
517 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800518
Tejun Heo96d365e2014-02-13 06:58:40 -0500519 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400520 if (list_empty(&cgrp->cset_links)) {
521 cgroup_update_populated(cgrp, false);
522 if (notify_on_release(cgrp)) {
523 if (taskexit)
524 set_bit(CGRP_RELEASABLE, &cgrp->flags);
525 check_for_release(cgrp);
526 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700527 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700528
529 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700530 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700531
Tejun Heo5abb8852013-06-12 21:04:49 -0700532 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700533}
534
Tejun Heo89c55092014-02-13 06:58:40 -0500535static void put_css_set(struct css_set *cset, bool taskexit)
536{
537 /*
538 * Ensure that the refcount doesn't hit zero while any readers
539 * can see it. Similar to atomic_dec_and_lock(), but for an
540 * rwlock
541 */
542 if (atomic_add_unless(&cset->refcount, -1, 1))
543 return;
544
545 down_write(&css_set_rwsem);
546 put_css_set_locked(cset, taskexit);
547 up_write(&css_set_rwsem);
548}
549
Paul Menage817929e2007-10-18 23:39:36 -0700550/*
551 * refcounted get/put for css_set objects
552 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700553static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700554{
Tejun Heo5abb8852013-06-12 21:04:49 -0700555 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700556}
557
Tejun Heob326f9d2013-06-24 15:21:48 -0700558/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700559 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700560 * @cset: candidate css_set being tested
561 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700562 * @new_cgrp: cgroup that's being entered by the task
563 * @template: desired set of css pointers in css_set (pre-calculated)
564 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800565 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700566 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
567 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700568static bool compare_css_sets(struct css_set *cset,
569 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700570 struct cgroup *new_cgrp,
571 struct cgroup_subsys_state *template[])
572{
573 struct list_head *l1, *l2;
574
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400575 /*
576 * On the default hierarchy, there can be csets which are
577 * associated with the same set of cgroups but different csses.
578 * Let's first ensure that csses match.
579 */
580 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700581 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700582
583 /*
584 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400585 * different cgroups in hierarchies. As different cgroups may
586 * share the same effective css, this comparison is always
587 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700588 */
Tejun Heo69d02062013-06-12 21:04:50 -0700589 l1 = &cset->cgrp_links;
590 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700591 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700592 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700593 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700594
595 l1 = l1->next;
596 l2 = l2->next;
597 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700598 if (l1 == &cset->cgrp_links) {
599 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700600 break;
601 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700602 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700603 }
604 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700605 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
606 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
607 cgrp1 = link1->cgrp;
608 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700609 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700610 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700611
612 /*
613 * If this hierarchy is the hierarchy of the cgroup
614 * that's changing, then we need to check that this
615 * css_set points to the new cgroup; if it's any other
616 * hierarchy, then this css_set should point to the
617 * same cgroup as the old css_set.
618 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700619 if (cgrp1->root == new_cgrp->root) {
620 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700621 return false;
622 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700623 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700624 return false;
625 }
626 }
627 return true;
628}
629
Tejun Heob326f9d2013-06-24 15:21:48 -0700630/**
631 * find_existing_css_set - init css array and find the matching css_set
632 * @old_cset: the css_set that we're using before the cgroup transition
633 * @cgrp: the cgroup that we're moving into
634 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700635 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700636static struct css_set *find_existing_css_set(struct css_set *old_cset,
637 struct cgroup *cgrp,
638 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700639{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400640 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700641 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700642 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800643 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700644 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700645
Ben Blumaae8aab2010-03-10 15:22:07 -0800646 /*
647 * Build the set of subsystem state objects that we want to see in the
648 * new css_set. while subsystems can change globally, the entries here
649 * won't change, so no need for locking.
650 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700651 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400652 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400653 /*
654 * @ss is in this hierarchy, so we want the
655 * effective css from @cgrp.
656 */
657 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700658 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400659 /*
660 * @ss is not in this hierarchy, so we don't want
661 * to change the css.
662 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700664 }
665 }
666
Li Zefan0ac801f2013-01-10 11:49:27 +0800667 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700668 hash_for_each_possible(css_set_table, cset, hlist, key) {
669 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 continue;
671
672 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700673 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700674 }
Paul Menage817929e2007-10-18 23:39:36 -0700675
676 /* No existing cgroup group matched */
677 return NULL;
678}
679
Tejun Heo69d02062013-06-12 21:04:50 -0700680static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700681{
Tejun Heo69d02062013-06-12 21:04:50 -0700682 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700683
Tejun Heo69d02062013-06-12 21:04:50 -0700684 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
685 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700686 kfree(link);
687 }
688}
689
Tejun Heo69d02062013-06-12 21:04:50 -0700690/**
691 * allocate_cgrp_cset_links - allocate cgrp_cset_links
692 * @count: the number of links to allocate
693 * @tmp_links: list_head the allocated links are put on
694 *
695 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
696 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700697 */
Tejun Heo69d02062013-06-12 21:04:50 -0700698static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700699{
Tejun Heo69d02062013-06-12 21:04:50 -0700700 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700701 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700702
703 INIT_LIST_HEAD(tmp_links);
704
Li Zefan36553432008-07-29 22:33:19 -0700705 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700706 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700707 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700708 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700709 return -ENOMEM;
710 }
Tejun Heo69d02062013-06-12 21:04:50 -0700711 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700712 }
713 return 0;
714}
715
Li Zefanc12f65d2009-01-07 18:07:42 -0800716/**
717 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700718 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800720 * @cgrp: the destination cgroup
721 */
Tejun Heo69d02062013-06-12 21:04:50 -0700722static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
723 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800724{
Tejun Heo69d02062013-06-12 21:04:50 -0700725 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800726
Tejun Heo69d02062013-06-12 21:04:50 -0700727 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400728
729 if (cgroup_on_dfl(cgrp))
730 cset->dfl_cgrp = cgrp;
731
Tejun Heo69d02062013-06-12 21:04:50 -0700732 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
733 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700734 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400735
736 if (list_empty(&cgrp->cset_links))
737 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700738 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400739
Paul Menage7717f7b2009-09-23 15:56:22 -0700740 /*
741 * Always add links to the tail of the list so that the list
742 * is sorted by order of hierarchy creation
743 */
Tejun Heo69d02062013-06-12 21:04:50 -0700744 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800745}
746
Tejun Heob326f9d2013-06-24 15:21:48 -0700747/**
748 * find_css_set - return a new css_set with one cgroup updated
749 * @old_cset: the baseline css_set
750 * @cgrp: the cgroup to be updated
751 *
752 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
753 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700754 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700755static struct css_set *find_css_set(struct css_set *old_cset,
756 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700757{
Tejun Heob326f9d2013-06-24 15:21:48 -0700758 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700759 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700760 struct list_head tmp_links;
761 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400762 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800763 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400764 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700765
Tejun Heob326f9d2013-06-24 15:21:48 -0700766 lockdep_assert_held(&cgroup_mutex);
767
Paul Menage817929e2007-10-18 23:39:36 -0700768 /* First see if we already have a cgroup group that matches
769 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500770 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700771 cset = find_existing_css_set(old_cset, cgrp, template);
772 if (cset)
773 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500774 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700775
Tejun Heo5abb8852013-06-12 21:04:49 -0700776 if (cset)
777 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700778
Tejun Heof4f4be22013-06-12 21:04:51 -0700779 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700780 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700781 return NULL;
782
Tejun Heo69d02062013-06-12 21:04:50 -0700783 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700784 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700785 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700786 return NULL;
787 }
788
Tejun Heo5abb8852013-06-12 21:04:49 -0700789 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700790 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700791 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500792 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500793 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500794 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700795 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700796
797 /* Copy the set of subsystem state objects generated in
798 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700799 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700800
Tejun Heo96d365e2014-02-13 06:58:40 -0500801 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700802 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700803 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700804 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700805
Paul Menage7717f7b2009-09-23 15:56:22 -0700806 if (c->root == cgrp->root)
807 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700808 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700809 }
Paul Menage817929e2007-10-18 23:39:36 -0700810
Tejun Heo69d02062013-06-12 21:04:50 -0700811 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700812
Paul Menage817929e2007-10-18 23:39:36 -0700813 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700814
Tejun Heo2d8f2432014-04-23 11:13:15 -0400815 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700816 key = css_set_hash(cset->subsys);
817 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700818
Tejun Heo2d8f2432014-04-23 11:13:15 -0400819 for_each_subsys(ss, ssid)
820 list_add_tail(&cset->e_cset_node[ssid],
821 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
822
Tejun Heo96d365e2014-02-13 06:58:40 -0500823 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700824
Tejun Heo5abb8852013-06-12 21:04:49 -0700825 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700826}
827
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400828static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700829{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400830 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500831
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400832 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500833}
834
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400835static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500836{
837 int id;
838
839 lockdep_assert_held(&cgroup_mutex);
840
Tejun Heo985ed672014-03-19 10:23:53 -0400841 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500842 if (id < 0)
843 return id;
844
845 root->hierarchy_id = id;
846 return 0;
847}
848
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400849static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500850{
851 lockdep_assert_held(&cgroup_mutex);
852
853 if (root->hierarchy_id) {
854 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
855 root->hierarchy_id = 0;
856 }
857}
858
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400859static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500860{
861 if (root) {
862 /* hierarhcy ID shoulid already have been released */
863 WARN_ON_ONCE(root->hierarchy_id);
864
865 idr_destroy(&root->cgroup_idr);
866 kfree(root);
867 }
868}
869
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400870static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500871{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400872 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500873 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500874
Tejun Heo2bd59d42014-02-11 11:52:49 -0500875 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500876
Tejun Heo776f02f2014-02-12 09:29:50 -0500877 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -0400878 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -0500879
Tejun Heof2e85d52014-02-11 11:52:49 -0500880 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400881 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500882
883 /*
884 * Release all the links from cset_links to this hierarchy's
885 * root cgroup
886 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500887 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500888
889 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
890 list_del(&link->cset_link);
891 list_del(&link->cgrp_link);
892 kfree(link);
893 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500894 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500895
896 if (!list_empty(&root->root_list)) {
897 list_del(&root->root_list);
898 cgroup_root_count--;
899 }
900
901 cgroup_exit_root_id(root);
902
903 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500904
Tejun Heo2bd59d42014-02-11 11:52:49 -0500905 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500906 cgroup_free_root(root);
907}
908
Tejun Heoceb6a082014-02-25 10:04:02 -0500909/* look up cgroup associated with given css_set on the specified hierarchy */
910static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400911 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700912{
Paul Menage7717f7b2009-09-23 15:56:22 -0700913 struct cgroup *res = NULL;
914
Tejun Heo96d365e2014-02-13 06:58:40 -0500915 lockdep_assert_held(&cgroup_mutex);
916 lockdep_assert_held(&css_set_rwsem);
917
Tejun Heo5abb8852013-06-12 21:04:49 -0700918 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400919 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700920 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700921 struct cgrp_cset_link *link;
922
923 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700924 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700925
Paul Menage7717f7b2009-09-23 15:56:22 -0700926 if (c->root == root) {
927 res = c;
928 break;
929 }
930 }
931 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500932
Paul Menage7717f7b2009-09-23 15:56:22 -0700933 BUG_ON(!res);
934 return res;
935}
936
937/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500938 * Return the cgroup for "task" from the given hierarchy. Must be
939 * called with cgroup_mutex and css_set_rwsem held.
940 */
941static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400942 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500943{
944 /*
945 * No need to lock the task - since we hold cgroup_mutex the
946 * task can't change groups, so the only thing that can happen
947 * is that it exits and its css is set back to init_css_set.
948 */
949 return cset_cgroup_from_root(task_css_set(task), root);
950}
951
952/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953 * A task must hold cgroup_mutex to modify cgroups.
954 *
955 * Any task can increment and decrement the count field without lock.
956 * So in general, code holding cgroup_mutex can't rely on the count
957 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800958 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 * means that no tasks are currently attached, therefore there is no
960 * way a task attached to that cgroup can fork (the other way to
961 * increment the count). So code holding cgroup_mutex can safely
962 * assume that if the count is zero, it will stay zero. Similarly, if
963 * a task holds cgroup_mutex on a cgroup with zero count, it
964 * knows that the cgroup won't be removed, as cgroup_rmdir()
965 * needs that mutex.
966 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
968 * (usually) take cgroup_mutex. These are the two most performance
969 * critical pieces of code here. The exception occurs on cgroup_exit(),
970 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
971 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800972 * to the release agent with the name of the cgroup (path relative to
973 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974 *
975 * A cgroup can only be deleted if both its 'count' of using tasks
976 * is zero, and its list of 'children' cgroups is empty. Since all
977 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400978 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400980 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 *
982 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800983 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 */
985
Tejun Heo69dfa002014-05-04 15:09:13 -0400986static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500987static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700988static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700989
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500990static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
991 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500993 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
994 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
995 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
996 cft->ss->name, cft->name);
997 else
998 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
999 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000}
1001
Tejun Heof2e85d52014-02-11 11:52:49 -05001002/**
1003 * cgroup_file_mode - deduce file mode of a control file
1004 * @cft: the control file in question
1005 *
1006 * returns cft->mode if ->mode is not 0
1007 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1008 * returns S_IRUGO if it has only a read handler
1009 * returns S_IWUSR if it has only a write hander
1010 */
1011static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001012{
Tejun Heof2e85d52014-02-11 11:52:49 -05001013 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001014
Tejun Heof2e85d52014-02-11 11:52:49 -05001015 if (cft->mode)
1016 return cft->mode;
1017
1018 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1019 mode |= S_IRUGO;
1020
Tejun Heo6770c642014-05-13 12:16:21 -04001021 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001022 mode |= S_IWUSR;
1023
1024 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001025}
1026
Tejun Heo59f52962014-02-11 11:52:49 -05001027static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001029 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001030 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031}
1032
Tejun Heo59f52962014-02-11 11:52:49 -05001033static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034{
Tejun Heo9d755d32014-05-14 09:15:02 -04001035 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036}
1037
Tejun Heoa9746d82014-05-13 12:19:22 -04001038/**
1039 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1040 * @kn: the kernfs_node being serviced
1041 *
1042 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1043 * the method finishes if locking succeeded. Note that once this function
1044 * returns the cgroup returned by cgroup_kn_lock_live() may become
1045 * inaccessible any time. If the caller intends to continue to access the
1046 * cgroup, it should pin it before invoking this function.
1047 */
1048static void cgroup_kn_unlock(struct kernfs_node *kn)
1049{
1050 struct cgroup *cgrp;
1051
1052 if (kernfs_type(kn) == KERNFS_DIR)
1053 cgrp = kn->priv;
1054 else
1055 cgrp = kn->parent->priv;
1056
1057 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001058
1059 kernfs_unbreak_active_protection(kn);
1060 cgroup_put(cgrp);
1061}
1062
1063/**
1064 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1065 * @kn: the kernfs_node being serviced
1066 *
1067 * This helper is to be used by a cgroup kernfs method currently servicing
1068 * @kn. It breaks the active protection, performs cgroup locking and
1069 * verifies that the associated cgroup is alive. Returns the cgroup if
1070 * alive; otherwise, %NULL. A successful return should be undone by a
1071 * matching cgroup_kn_unlock() invocation.
1072 *
1073 * Any cgroup kernfs method implementation which requires locking the
1074 * associated cgroup should use this helper. It avoids nesting cgroup
1075 * locking under kernfs active protection and allows all kernfs operations
1076 * including self-removal.
1077 */
1078static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1079{
1080 struct cgroup *cgrp;
1081
1082 if (kernfs_type(kn) == KERNFS_DIR)
1083 cgrp = kn->priv;
1084 else
1085 cgrp = kn->parent->priv;
1086
1087 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001088 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001089 * active_ref. cgroup liveliness check alone provides enough
1090 * protection against removal. Ensure @cgrp stays accessible and
1091 * break the active_ref protection.
1092 */
1093 cgroup_get(cgrp);
1094 kernfs_break_active_protection(kn);
1095
Tejun Heoa9746d82014-05-13 12:19:22 -04001096 mutex_lock(&cgroup_mutex);
1097
1098 if (!cgroup_is_dead(cgrp))
1099 return cgrp;
1100
1101 cgroup_kn_unlock(kn);
1102 return NULL;
1103}
1104
Li Zefan2739d3c2013-01-21 18:18:33 +08001105static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001107 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108
Tejun Heo01f64742014-05-13 12:19:23 -04001109 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001110 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001111}
1112
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001113/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001114 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001115 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001116 * @subsys_mask: mask of the subsystem ids whose files should be removed
1117 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001118static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001119{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001120 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001121 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001122
Tejun Heob420ba72013-07-12 12:34:02 -07001123 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001124 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001125
Tejun Heo69dfa002014-05-04 15:09:13 -04001126 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001127 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001128 list_for_each_entry(cfts, &ss->cfts, node)
1129 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001130 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131}
1132
Tejun Heo69dfa002014-05-04 15:09:13 -04001133static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134{
Tejun Heo30159ec2013-06-25 11:53:37 -07001135 struct cgroup_subsys *ss;
Tejun Heo5533e012014-05-14 19:33:07 -04001136 unsigned int tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001137 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138
Tejun Heoace2bee2014-02-11 11:52:47 -05001139 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001140
Tejun Heo5df36032014-03-19 10:23:54 -04001141 for_each_subsys(ss, ssid) {
1142 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001144
Tejun Heo7fd8c562014-04-23 11:13:16 -04001145 /* if @ss has non-root csses attached to it, can't move */
1146 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001147 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001148
Tejun Heo5df36032014-03-19 10:23:54 -04001149 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001150 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001151 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152 }
1153
Tejun Heo5533e012014-05-14 19:33:07 -04001154 /* skip creating root files on dfl_root for inhibited subsystems */
1155 tmp_ss_mask = ss_mask;
1156 if (dst_root == &cgrp_dfl_root)
1157 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1158
1159 ret = cgroup_populate_dir(&dst_root->cgrp, tmp_ss_mask);
Tejun Heoa2dd4242014-03-19 10:23:55 -04001160 if (ret) {
1161 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001162 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001163
Tejun Heoa2dd4242014-03-19 10:23:55 -04001164 /*
1165 * Rebinding back to the default root is not allowed to
1166 * fail. Using both default and non-default roots should
1167 * be rare. Moving subsystems back and forth even more so.
1168 * Just warn about it and continue.
1169 */
1170 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001171 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001172 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001173 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001174 }
Tejun Heo5df36032014-03-19 10:23:54 -04001175 }
Tejun Heo31261212013-06-28 17:07:30 -07001176
1177 /*
1178 * Nothing can fail from this point on. Remove files for the
1179 * removed subsystems and rebind each subsystem.
1180 */
Tejun Heo5df36032014-03-19 10:23:54 -04001181 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001182 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001183 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001184
Tejun Heo5df36032014-03-19 10:23:54 -04001185 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001186 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001187 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001188 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001189
Tejun Heo5df36032014-03-19 10:23:54 -04001190 if (!(ss_mask & (1 << ssid)))
1191 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001192
Tejun Heo5df36032014-03-19 10:23:54 -04001193 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001194 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001195
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001196 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001197
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001198 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1199 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001200 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001201 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001202
Tejun Heo2d8f2432014-04-23 11:13:15 -04001203 down_write(&css_set_rwsem);
1204 hash_for_each(css_set_table, i, cset, hlist)
1205 list_move_tail(&cset->e_cset_node[ss->id],
1206 &dst_root->cgrp.e_csets[ss->id]);
1207 up_write(&css_set_rwsem);
1208
Tejun Heof392e512014-04-23 11:13:14 -04001209 src_root->subsys_mask &= ~(1 << ssid);
1210 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1211
Tejun Heobd53d612014-04-23 11:13:16 -04001212 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001213 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001214 if (dst_root != &cgrp_dfl_root)
1215 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001216
Tejun Heo5df36032014-03-19 10:23:54 -04001217 if (ss->bind)
1218 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001219 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001220
Tejun Heoa2dd4242014-03-19 10:23:55 -04001221 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001222 return 0;
1223}
1224
Tejun Heo2bd59d42014-02-11 11:52:49 -05001225static int cgroup_show_options(struct seq_file *seq,
1226 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001227{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001228 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001230 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001231
Tejun Heob85d2042013-12-06 15:11:57 -05001232 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001233 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001234 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001235 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1236 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001237 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001238 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001239 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001240 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001241
1242 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001243 if (strlen(root->release_agent_path))
1244 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001245 spin_unlock(&release_agent_path_lock);
1246
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001247 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001248 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001249 if (strlen(root->name))
1250 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001251 return 0;
1252}
1253
1254struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001255 unsigned int subsys_mask;
1256 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001257 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001258 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001259 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001260 /* User explicitly requested empty subsystem */
1261 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001262};
1263
Ben Blumcf5d5942010-03-10 15:22:09 -08001264static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001265{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001266 char *token, *o = data;
1267 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001268 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001269 struct cgroup_subsys *ss;
1270 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001271
1272#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001273 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001274#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001275
Paul Menagec6d57f32009-09-23 15:56:19 -07001276 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001277
1278 while ((token = strsep(&o, ",")) != NULL) {
1279 if (!*token)
1280 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001281 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001282 /* Explicitly have no subsystems */
1283 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001284 continue;
1285 }
1286 if (!strcmp(token, "all")) {
1287 /* Mutually exclusive option 'all' + subsystem name */
1288 if (one_ss)
1289 return -EINVAL;
1290 all_ss = true;
1291 continue;
1292 }
Tejun Heo873fe092013-04-14 20:15:26 -07001293 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1294 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1295 continue;
1296 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001297 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001298 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001299 continue;
1300 }
1301 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001302 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001303 continue;
1304 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001305 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001306 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001307 continue;
1308 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001309 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001310 /* Specifying two release agents is forbidden */
1311 if (opts->release_agent)
1312 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001313 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001314 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001315 if (!opts->release_agent)
1316 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001317 continue;
1318 }
1319 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001320 const char *name = token + 5;
1321 /* Can't specify an empty name */
1322 if (!strlen(name))
1323 return -EINVAL;
1324 /* Must match [\w.-]+ */
1325 for (i = 0; i < strlen(name); i++) {
1326 char c = name[i];
1327 if (isalnum(c))
1328 continue;
1329 if ((c == '.') || (c == '-') || (c == '_'))
1330 continue;
1331 return -EINVAL;
1332 }
1333 /* Specifying two names is forbidden */
1334 if (opts->name)
1335 return -EINVAL;
1336 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001337 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001338 GFP_KERNEL);
1339 if (!opts->name)
1340 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001341
1342 continue;
1343 }
1344
Tejun Heo30159ec2013-06-25 11:53:37 -07001345 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001346 if (strcmp(token, ss->name))
1347 continue;
1348 if (ss->disabled)
1349 continue;
1350
1351 /* Mutually exclusive option 'all' + subsystem name */
1352 if (all_ss)
1353 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001354 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001355 one_ss = true;
1356
1357 break;
1358 }
1359 if (i == CGROUP_SUBSYS_COUNT)
1360 return -ENOENT;
1361 }
1362
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001363 /* Consistency checks */
1364
Tejun Heo873fe092013-04-14 20:15:26 -07001365 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001366 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 -07001367
Tejun Heod3ba07c2014-02-13 06:58:38 -05001368 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1369 opts->cpuset_clone_children || opts->release_agent ||
1370 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001371 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001372 return -EINVAL;
1373 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001374 } else {
1375 /*
1376 * If the 'all' option was specified select all the
1377 * subsystems, otherwise if 'none', 'name=' and a subsystem
1378 * name options were not specified, let's default to 'all'
1379 */
1380 if (all_ss || (!one_ss && !opts->none && !opts->name))
1381 for_each_subsys(ss, i)
1382 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001383 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001384
Tejun Heoa2dd4242014-03-19 10:23:55 -04001385 /*
1386 * We either have to specify by name or by subsystems. (So
1387 * all empty hierarchies must have a name).
1388 */
1389 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001390 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001391 }
1392
Li Zefanf9ab5b52009-06-17 16:26:33 -07001393 /*
1394 * Option noprefix was introduced just for backward compatibility
1395 * with the old cpuset, so we allow noprefix only if mounting just
1396 * the cpuset subsystem.
1397 */
Tejun Heo93438622013-04-14 20:15:25 -07001398 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001399 return -EINVAL;
1400
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001401
1402 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001403 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001404 return -EINVAL;
1405
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406 return 0;
1407}
1408
Tejun Heo2bd59d42014-02-11 11:52:49 -05001409static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410{
1411 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001412 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001414 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415
Tejun Heo873fe092013-04-14 20:15:26 -07001416 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001417 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001418 return -EINVAL;
1419 }
1420
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421 mutex_lock(&cgroup_mutex);
1422
1423 /* See what subsystems are wanted */
1424 ret = parse_cgroupfs_options(data, &opts);
1425 if (ret)
1426 goto out_unlock;
1427
Tejun Heof392e512014-04-23 11:13:14 -04001428 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001429 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001430 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001431
Tejun Heof392e512014-04-23 11:13:14 -04001432 added_mask = opts.subsys_mask & ~root->subsys_mask;
1433 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001434
Ben Blumcf5d5942010-03-10 15:22:09 -08001435 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001436 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001437 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001438 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001439 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1440 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001442 goto out_unlock;
1443 }
1444
Tejun Heof172e672013-06-28 17:07:30 -07001445 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001446 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001447 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001448 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001449 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450
Tejun Heo5df36032014-03-19 10:23:54 -04001451 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001452 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001455 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001456
Tejun Heo69e943b2014-02-08 10:36:58 -05001457 if (opts.release_agent) {
1458 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001459 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001460 spin_unlock(&release_agent_path_lock);
1461 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001463 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466 return ret;
1467}
1468
Tejun Heoafeb0f92014-02-13 06:58:39 -05001469/*
1470 * To reduce the fork() overhead for systems that are not actually using
1471 * their cgroups capability, we don't maintain the lists running through
1472 * each css_set to its tasks until we see the list actually used - in other
1473 * words after the first mount.
1474 */
1475static bool use_task_css_set_links __read_mostly;
1476
1477static void cgroup_enable_task_cg_lists(void)
1478{
1479 struct task_struct *p, *g;
1480
Tejun Heo96d365e2014-02-13 06:58:40 -05001481 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001482
1483 if (use_task_css_set_links)
1484 goto out_unlock;
1485
1486 use_task_css_set_links = true;
1487
1488 /*
1489 * We need tasklist_lock because RCU is not safe against
1490 * while_each_thread(). Besides, a forking task that has passed
1491 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1492 * is not guaranteed to have its child immediately visible in the
1493 * tasklist if we walk through it with RCU.
1494 */
1495 read_lock(&tasklist_lock);
1496 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001497 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1498 task_css_set(p) != &init_css_set);
1499
1500 /*
1501 * We should check if the process is exiting, otherwise
1502 * it will race with cgroup_exit() in that the list
1503 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001504 * Do it while holding siglock so that we don't end up
1505 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001506 */
Tejun Heof153ad12014-02-25 09:56:49 -05001507 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001508 if (!(p->flags & PF_EXITING)) {
1509 struct css_set *cset = task_css_set(p);
1510
1511 list_add(&p->cg_list, &cset->tasks);
1512 get_css_set(cset);
1513 }
Tejun Heof153ad12014-02-25 09:56:49 -05001514 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001515 } while_each_thread(g, p);
1516 read_unlock(&tasklist_lock);
1517out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001518 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001519}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
Paul Menagecc31edc2008-10-18 20:28:04 -07001521static void init_cgroup_housekeeping(struct cgroup *cgrp)
1522{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001523 struct cgroup_subsys *ss;
1524 int ssid;
1525
Tejun Heod5c419b2014-05-16 13:22:48 -04001526 INIT_LIST_HEAD(&cgrp->self.sibling);
1527 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001528 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001529 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001530 INIT_LIST_HEAD(&cgrp->pidlists);
1531 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001532 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001533 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001534
1535 for_each_subsys(ss, ssid)
1536 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001537
1538 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001539}
Paul Menagec6d57f32009-09-23 15:56:19 -07001540
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001541static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001542 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001544 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001545
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001547 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001548 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001549 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001550 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551
Paul Menagec6d57f32009-09-23 15:56:19 -07001552 root->flags = opts->flags;
1553 if (opts->release_agent)
1554 strcpy(root->release_agent_path, opts->release_agent);
1555 if (opts->name)
1556 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001557 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001558 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001559}
1560
Tejun Heo69dfa002014-05-04 15:09:13 -04001561static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001562{
Tejun Heod427dfe2014-02-11 11:52:48 -05001563 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001564 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001565 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001566 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001567
Tejun Heod427dfe2014-02-11 11:52:48 -05001568 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001569
Tejun Heo6fa49182014-05-04 15:09:13 -04001570 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001571 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001572 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001573 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001574
Tejun Heo9d755d32014-05-14 09:15:02 -04001575 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release);
1576 if (ret)
1577 goto out;
1578
Tejun Heod427dfe2014-02-11 11:52:48 -05001579 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001580 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001581 * but that's OK - it can only be increased by someone holding
1582 * cgroup_lock, and that's us. The worst that can happen is that we
1583 * have some link structures left over
1584 */
1585 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001587 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588
Tejun Heo985ed672014-03-19 10:23:53 -04001589 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001590 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001591 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
Tejun Heo2bd59d42014-02-11 11:52:49 -05001593 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1594 KERNFS_ROOT_CREATE_DEACTIVATED,
1595 root_cgrp);
1596 if (IS_ERR(root->kf_root)) {
1597 ret = PTR_ERR(root->kf_root);
1598 goto exit_root_id;
1599 }
1600 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601
Tejun Heod427dfe2014-02-11 11:52:48 -05001602 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1603 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001604 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
Tejun Heo5df36032014-03-19 10:23:54 -04001606 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001608 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001609
Tejun Heod427dfe2014-02-11 11:52:48 -05001610 /*
1611 * There must be no failure case after here, since rebinding takes
1612 * care of subsystems' refcounts, which are explicitly dropped in
1613 * the failure exit path.
1614 */
1615 list_add(&root->root_list, &cgroup_roots);
1616 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617
Tejun Heod427dfe2014-02-11 11:52:48 -05001618 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001619 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001620 * objects.
1621 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001622 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001623 hash_for_each(css_set_table, i, cset, hlist)
1624 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001625 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Tejun Heod5c419b2014-05-16 13:22:48 -04001627 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001628 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001629
Tejun Heo2bd59d42014-02-11 11:52:49 -05001630 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001631 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001632 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001633
Tejun Heo2bd59d42014-02-11 11:52:49 -05001634destroy_root:
1635 kernfs_destroy_root(root->kf_root);
1636 root->kf_root = NULL;
1637exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001638 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001639cancel_ref:
1640 percpu_ref_cancel_init(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001641out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001642 free_cgrp_cset_links(&tmp_links);
1643 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644}
1645
Al Virof7e83572010-07-26 13:23:11 +04001646static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001648 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001650 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001652 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001653 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001654 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001655
1656 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001657 * The first time anyone tries to mount a cgroup, enable the list
1658 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001659 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001660 if (!use_task_css_set_links)
1661 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001662
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001663 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001668 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001669
Tejun Heo2bd59d42014-02-11 11:52:49 -05001670 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001671 if (!opts.subsys_mask && !opts.none && !opts.name) {
1672 cgrp_dfl_root_visible = true;
1673 root = &cgrp_dfl_root;
1674 cgroup_get(&root->cgrp);
1675 ret = 0;
1676 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 }
1678
Tejun Heo985ed672014-03-19 10:23:53 -04001679 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001680 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001682 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001683 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001684
Paul Menage817929e2007-10-18 23:39:36 -07001685 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001686 * If we asked for a name then it must match. Also, if
1687 * name matches but sybsys_mask doesn't, we should fail.
1688 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001689 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001690 if (opts.name) {
1691 if (strcmp(opts.name, root->name))
1692 continue;
1693 name_match = true;
1694 }
Tejun Heo31261212013-06-28 17:07:30 -07001695
1696 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001697 * If we asked for subsystems (or explicitly for no
1698 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001699 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001700 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001701 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001702 if (!name_match)
1703 continue;
1704 ret = -EBUSY;
1705 goto out_unlock;
1706 }
Tejun Heo873fe092013-04-14 20:15:26 -07001707
Tejun Heoc7ba8282013-06-29 14:06:10 -07001708 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001709 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001710 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001711 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001712 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001713 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001714 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001715 }
Tejun Heo873fe092013-04-14 20:15:26 -07001716 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001717
Tejun Heo776f02f2014-02-12 09:29:50 -05001718 /*
Tejun Heo9d755d32014-05-14 09:15:02 -04001719 * A root's lifetime is governed by its root cgroup.
1720 * tryget_live failure indicate that the root is being
1721 * destroyed. Wait for destruction to complete so that the
1722 * subsystems are free. We can use wait_queue for the wait
1723 * but this path is super cold. Let's just sleep for a bit
1724 * and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001725 */
Tejun Heo9d755d32014-05-14 09:15:02 -04001726 if (!percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001727 mutex_unlock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001728 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001729 ret = restart_syscall();
1730 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001731 }
1732
1733 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001734 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735 }
1736
Tejun Heo172a2c062014-03-19 10:23:53 -04001737 /*
1738 * No such thing, create a new one. name= matching without subsys
1739 * specification is allowed for already existing hierarchies but we
1740 * can't create new one without subsys specification.
1741 */
1742 if (!opts.subsys_mask && !opts.none) {
1743 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001744 goto out_unlock;
1745 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746
Tejun Heo172a2c062014-03-19 10:23:53 -04001747 root = kzalloc(sizeof(*root), GFP_KERNEL);
1748 if (!root) {
1749 ret = -ENOMEM;
1750 goto out_unlock;
1751 }
1752
1753 init_cgroup_root(root, &opts);
1754
Tejun Heo35585572014-02-13 06:58:38 -05001755 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001756 if (ret)
1757 cgroup_free_root(root);
1758
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001759out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001760 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001761out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001762 kfree(opts.release_agent);
1763 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001764
Tejun Heo2bd59d42014-02-11 11:52:49 -05001765 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001766 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001767
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001768 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1769 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001770 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001771 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772}
1773
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001774static void cgroup_kill_sb(struct super_block *sb)
1775{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001776 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001777 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778
Tejun Heo9d755d32014-05-14 09:15:02 -04001779 /*
1780 * If @root doesn't have any mounts or children, start killing it.
1781 * This prevents new mounts by disabling percpu_ref_tryget_live().
1782 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08001783 *
1784 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04001785 */
Li Zefan1f779fb2014-06-04 16:48:15 +08001786 if (css_has_online_children(&root->cgrp.self) ||
1787 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04001788 cgroup_put(&root->cgrp);
1789 else
1790 percpu_ref_kill(&root->cgrp.self.refcnt);
1791
Tejun Heo2bd59d42014-02-11 11:52:49 -05001792 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793}
1794
1795static struct file_system_type cgroup_fs_type = {
1796 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001797 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 .kill_sb = cgroup_kill_sb,
1799};
1800
Greg KH676db4a2010-08-05 13:53:35 -07001801static struct kobject *cgroup_kobj;
1802
Li Zefana043e3b2008-02-23 15:24:09 -08001803/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001804 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001805 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001806 * @buf: the buffer to write the path into
1807 * @buflen: the length of the buffer
1808 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001809 * Determine @task's cgroup on the first (the one with the lowest non-zero
1810 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1811 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1812 * cgroup controller callbacks.
1813 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001814 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001815 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001816char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001817{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001818 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001819 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001820 int hierarchy_id = 1;
1821 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001822
1823 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001824 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001825
Tejun Heo913ffdb2013-07-11 16:34:48 -07001826 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1827
Tejun Heo857a2be2013-04-14 20:50:08 -07001828 if (root) {
1829 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001830 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001831 } else {
1832 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001833 if (strlcpy(buf, "/", buflen) < buflen)
1834 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001835 }
1836
Tejun Heo96d365e2014-02-13 06:58:40 -05001837 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001838 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001839 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001840}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001841EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001842
Tejun Heob3dc0942014-02-25 10:04:01 -05001843/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001844struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001845 /* the src and dst cset list running through cset->mg_node */
1846 struct list_head src_csets;
1847 struct list_head dst_csets;
1848
1849 /*
1850 * Fields for cgroup_taskset_*() iteration.
1851 *
1852 * Before migration is committed, the target migration tasks are on
1853 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1854 * the csets on ->dst_csets. ->csets point to either ->src_csets
1855 * or ->dst_csets depending on whether migration is committed.
1856 *
1857 * ->cur_csets and ->cur_task point to the current task position
1858 * during iteration.
1859 */
1860 struct list_head *csets;
1861 struct css_set *cur_cset;
1862 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001863};
1864
1865/**
1866 * cgroup_taskset_first - reset taskset and return the first task
1867 * @tset: taskset of interest
1868 *
1869 * @tset iteration is initialized and the first task is returned.
1870 */
1871struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1872{
Tejun Heob3dc0942014-02-25 10:04:01 -05001873 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1874 tset->cur_task = NULL;
1875
1876 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001877}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001878
1879/**
1880 * cgroup_taskset_next - iterate to the next task in taskset
1881 * @tset: taskset of interest
1882 *
1883 * Return the next task in @tset. Iteration must have been initialized
1884 * with cgroup_taskset_first().
1885 */
1886struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1887{
Tejun Heob3dc0942014-02-25 10:04:01 -05001888 struct css_set *cset = tset->cur_cset;
1889 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001890
Tejun Heob3dc0942014-02-25 10:04:01 -05001891 while (&cset->mg_node != tset->csets) {
1892 if (!task)
1893 task = list_first_entry(&cset->mg_tasks,
1894 struct task_struct, cg_list);
1895 else
1896 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001897
Tejun Heob3dc0942014-02-25 10:04:01 -05001898 if (&task->cg_list != &cset->mg_tasks) {
1899 tset->cur_cset = cset;
1900 tset->cur_task = task;
1901 return task;
1902 }
1903
1904 cset = list_next_entry(cset, mg_node);
1905 task = NULL;
1906 }
1907
1908 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001909}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001910
1911/**
Ben Blum74a11662011-05-26 16:25:20 -07001912 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001913 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001914 * @tsk: the task being migrated
1915 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001916 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001917 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001918 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001919static void cgroup_task_migrate(struct cgroup *old_cgrp,
1920 struct task_struct *tsk,
1921 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001922{
Tejun Heo5abb8852013-06-12 21:04:49 -07001923 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001924
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001925 lockdep_assert_held(&cgroup_mutex);
1926 lockdep_assert_held(&css_set_rwsem);
1927
Ben Blum74a11662011-05-26 16:25:20 -07001928 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001929 * We are synchronized through threadgroup_lock() against PF_EXITING
1930 * setting such that we can't race against cgroup_exit() changing the
1931 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001932 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001933 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001934 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001935
Tejun Heob3dc0942014-02-25 10:04:01 -05001936 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001937 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001938
Tejun Heo1b9aba42014-03-19 17:43:21 -04001939 /*
1940 * Use move_tail so that cgroup_taskset_first() still returns the
1941 * leader after migration. This works because cgroup_migrate()
1942 * ensures that the dst_cset of the leader is the first on the
1943 * tset's dst_csets list.
1944 */
1945 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001946
1947 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001948 * We just gained a reference on old_cset by taking it from the
1949 * task. As trading it for new_cset is protected by cgroup_mutex,
1950 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001951 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001952 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001953 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001954}
1955
Li Zefana043e3b2008-02-23 15:24:09 -08001956/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001957 * cgroup_migrate_finish - cleanup after attach
1958 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001959 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001960 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1961 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001962 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001963static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001964{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001965 struct css_set *cset, *tmp_cset;
1966
1967 lockdep_assert_held(&cgroup_mutex);
1968
1969 down_write(&css_set_rwsem);
1970 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1971 cset->mg_src_cgrp = NULL;
1972 cset->mg_dst_cset = NULL;
1973 list_del_init(&cset->mg_preload_node);
1974 put_css_set_locked(cset, false);
1975 }
1976 up_write(&css_set_rwsem);
1977}
1978
1979/**
1980 * cgroup_migrate_add_src - add a migration source css_set
1981 * @src_cset: the source css_set to add
1982 * @dst_cgrp: the destination cgroup
1983 * @preloaded_csets: list of preloaded css_sets
1984 *
1985 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1986 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1987 * up by cgroup_migrate_finish().
1988 *
1989 * This function may be called without holding threadgroup_lock even if the
1990 * target is a process. Threads may be created and destroyed but as long
1991 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1992 * the preloaded css_sets are guaranteed to cover all migrations.
1993 */
1994static void cgroup_migrate_add_src(struct css_set *src_cset,
1995 struct cgroup *dst_cgrp,
1996 struct list_head *preloaded_csets)
1997{
1998 struct cgroup *src_cgrp;
1999
2000 lockdep_assert_held(&cgroup_mutex);
2001 lockdep_assert_held(&css_set_rwsem);
2002
2003 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2004
Tejun Heo1958d2d2014-02-25 10:04:03 -05002005 if (!list_empty(&src_cset->mg_preload_node))
2006 return;
2007
2008 WARN_ON(src_cset->mg_src_cgrp);
2009 WARN_ON(!list_empty(&src_cset->mg_tasks));
2010 WARN_ON(!list_empty(&src_cset->mg_node));
2011
2012 src_cset->mg_src_cgrp = src_cgrp;
2013 get_css_set(src_cset);
2014 list_add(&src_cset->mg_preload_node, preloaded_csets);
2015}
2016
2017/**
2018 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002019 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002020 * @preloaded_csets: list of preloaded source css_sets
2021 *
2022 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2023 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002024 * pins all destination css_sets, links each to its source, and append them
2025 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2026 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002027 *
2028 * This function must be called after cgroup_migrate_add_src() has been
2029 * called on each migration source css_set. After migration is performed
2030 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2031 * @preloaded_csets.
2032 */
2033static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2034 struct list_head *preloaded_csets)
2035{
2036 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002037 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002038
2039 lockdep_assert_held(&cgroup_mutex);
2040
Tejun Heof8f22e52014-04-23 11:13:16 -04002041 /*
2042 * Except for the root, child_subsys_mask must be zero for a cgroup
2043 * with tasks so that child cgroups don't compete against tasks.
2044 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002045 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002046 dst_cgrp->child_subsys_mask)
2047 return -EBUSY;
2048
Tejun Heo1958d2d2014-02-25 10:04:03 -05002049 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002050 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002051 struct css_set *dst_cset;
2052
Tejun Heof817de92014-04-23 11:13:16 -04002053 dst_cset = find_css_set(src_cset,
2054 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002055 if (!dst_cset)
2056 goto err;
2057
2058 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002059
2060 /*
2061 * If src cset equals dst, it's noop. Drop the src.
2062 * cgroup_migrate() will skip the cset too. Note that we
2063 * can't handle src == dst as some nodes are used by both.
2064 */
2065 if (src_cset == dst_cset) {
2066 src_cset->mg_src_cgrp = NULL;
2067 list_del_init(&src_cset->mg_preload_node);
2068 put_css_set(src_cset, false);
2069 put_css_set(dst_cset, false);
2070 continue;
2071 }
2072
Tejun Heo1958d2d2014-02-25 10:04:03 -05002073 src_cset->mg_dst_cset = dst_cset;
2074
2075 if (list_empty(&dst_cset->mg_preload_node))
2076 list_add(&dst_cset->mg_preload_node, &csets);
2077 else
2078 put_css_set(dst_cset, false);
2079 }
2080
Tejun Heof817de92014-04-23 11:13:16 -04002081 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002082 return 0;
2083err:
2084 cgroup_migrate_finish(&csets);
2085 return -ENOMEM;
2086}
2087
2088/**
2089 * cgroup_migrate - migrate a process or task to a cgroup
2090 * @cgrp: the destination cgroup
2091 * @leader: the leader of the process or the task to migrate
2092 * @threadgroup: whether @leader points to the whole process or a single task
2093 *
2094 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2095 * process, the caller must be holding threadgroup_lock of @leader. The
2096 * caller is also responsible for invoking cgroup_migrate_add_src() and
2097 * cgroup_migrate_prepare_dst() on the targets before invoking this
2098 * function and following up with cgroup_migrate_finish().
2099 *
2100 * As long as a controller's ->can_attach() doesn't fail, this function is
2101 * guaranteed to succeed. This means that, excluding ->can_attach()
2102 * failure, when migrating multiple targets, the success or failure can be
2103 * decided for all targets by invoking group_migrate_prepare_dst() before
2104 * actually starting migrating.
2105 */
2106static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2107 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002108{
Tejun Heob3dc0942014-02-25 10:04:01 -05002109 struct cgroup_taskset tset = {
2110 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2111 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2112 .csets = &tset.src_csets,
2113 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002114 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002115 struct css_set *cset, *tmp_cset;
2116 struct task_struct *task, *tmp_task;
2117 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002118
2119 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002120 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2121 * already PF_EXITING could be freed from underneath us unless we
2122 * take an rcu_read_lock.
2123 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002124 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002125 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002126 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002127 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002128 /* @task either already exited or can't exit until the end */
2129 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002130 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002131
Tejun Heoeaf797a2014-02-25 10:04:03 -05002132 /* leave @task alone if post_fork() hasn't linked it yet */
2133 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002134 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002135
Tejun Heob3dc0942014-02-25 10:04:01 -05002136 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002137 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002138 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002139
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002140 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002141 * cgroup_taskset_first() must always return the leader.
2142 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002143 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002144 list_move_tail(&task->cg_list, &cset->mg_tasks);
2145 if (list_empty(&cset->mg_node))
2146 list_add_tail(&cset->mg_node, &tset.src_csets);
2147 if (list_empty(&cset->mg_dst_cset->mg_node))
2148 list_move_tail(&cset->mg_dst_cset->mg_node,
2149 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002150 next:
Li Zefan081aa452013-03-13 09:17:09 +08002151 if (!threadgroup)
2152 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002153 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002154 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002155 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002156
Tejun Heo134d3372011-12-12 18:12:21 -08002157 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002158 if (list_empty(&tset.src_csets))
2159 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002160
Tejun Heo1958d2d2014-02-25 10:04:03 -05002161 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002162 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002163 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002164 ret = css->ss->can_attach(css, &tset);
2165 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002166 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002167 goto out_cancel_attach;
2168 }
2169 }
Ben Blum74a11662011-05-26 16:25:20 -07002170 }
2171
2172 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002173 * Now that we're guaranteed success, proceed to move all tasks to
2174 * the new cgroup. There are no failure cases after here, so this
2175 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002176 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002177 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002178 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2179 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2180 cgroup_task_migrate(cset->mg_src_cgrp, task,
2181 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002182 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002183 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002184
2185 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002186 * Migration is committed, all target tasks are now on dst_csets.
2187 * Nothing is sensitive to fork() after this point. Notify
2188 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002189 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002190 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002191
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002192 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002193 if (css->ss->attach)
2194 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002195
Tejun Heo9db8de32014-02-13 06:58:43 -05002196 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002197 goto out_release_tset;
2198
Ben Blum74a11662011-05-26 16:25:20 -07002199out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002200 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002201 if (css == failed_css)
2202 break;
2203 if (css->ss->cancel_attach)
2204 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002205 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002206out_release_tset:
2207 down_write(&css_set_rwsem);
2208 list_splice_init(&tset.dst_csets, &tset.src_csets);
2209 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002210 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002211 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002212 }
2213 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002214 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002215}
2216
Tejun Heo1958d2d2014-02-25 10:04:03 -05002217/**
2218 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2219 * @dst_cgrp: the cgroup to attach to
2220 * @leader: the task or the leader of the threadgroup to be attached
2221 * @threadgroup: attach the whole threadgroup?
2222 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002223 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002224 */
2225static int cgroup_attach_task(struct cgroup *dst_cgrp,
2226 struct task_struct *leader, bool threadgroup)
2227{
2228 LIST_HEAD(preloaded_csets);
2229 struct task_struct *task;
2230 int ret;
2231
2232 /* look up all src csets */
2233 down_read(&css_set_rwsem);
2234 rcu_read_lock();
2235 task = leader;
2236 do {
2237 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2238 &preloaded_csets);
2239 if (!threadgroup)
2240 break;
2241 } while_each_thread(leader, task);
2242 rcu_read_unlock();
2243 up_read(&css_set_rwsem);
2244
2245 /* prepare dst csets and commit */
2246 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2247 if (!ret)
2248 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2249
2250 cgroup_migrate_finish(&preloaded_csets);
2251 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002252}
2253
2254/*
2255 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002256 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002257 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002258 */
Tejun Heoacbef752014-05-13 12:16:22 -04002259static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2260 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002261{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002262 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002263 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002264 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002265 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002266 int ret;
2267
Tejun Heoacbef752014-05-13 12:16:22 -04002268 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2269 return -EINVAL;
2270
Tejun Heoe76ecae2014-05-13 12:19:23 -04002271 cgrp = cgroup_kn_lock_live(of->kn);
2272 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002273 return -ENODEV;
2274
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002275retry_find_task:
2276 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002277 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002278 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002279 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002280 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002281 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002282 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002283 }
Ben Blum74a11662011-05-26 16:25:20 -07002284 /*
2285 * even if we're attaching all tasks in the thread group, we
2286 * only need to check permissions on one of them.
2287 */
David Howellsc69e8d92008-11-14 10:39:19 +11002288 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002289 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2290 !uid_eq(cred->euid, tcred->uid) &&
2291 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002292 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002293 ret = -EACCES;
2294 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002295 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002296 } else
2297 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002298
2299 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002300 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002301
2302 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002303 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002304 * trapped in a cpuset, or RT worker may be born in a cgroup
2305 * with no rt_runtime allocated. Just say no.
2306 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002307 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002308 ret = -EINVAL;
2309 rcu_read_unlock();
2310 goto out_unlock_cgroup;
2311 }
2312
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002313 get_task_struct(tsk);
2314 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002315
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002316 threadgroup_lock(tsk);
2317 if (threadgroup) {
2318 if (!thread_group_leader(tsk)) {
2319 /*
2320 * a race with de_thread from another thread's exec()
2321 * may strip us of our leadership, if this happens,
2322 * there is no choice but to throw this task away and
2323 * try again; this is
2324 * "double-double-toil-and-trouble-check locking".
2325 */
2326 threadgroup_unlock(tsk);
2327 put_task_struct(tsk);
2328 goto retry_find_task;
2329 }
Li Zefan081aa452013-03-13 09:17:09 +08002330 }
2331
2332 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2333
Tejun Heocd3d0952011-12-12 18:12:21 -08002334 threadgroup_unlock(tsk);
2335
Paul Menagebbcb81d2007-10-18 23:39:32 -07002336 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002337out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002338 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002339 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002340}
2341
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002342/**
2343 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2344 * @from: attach to all cgroups of a given task
2345 * @tsk: the task to be attached
2346 */
2347int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2348{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002349 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002350 int retval = 0;
2351
Tejun Heo47cfcd02013-04-07 09:29:51 -07002352 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002353 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002354 struct cgroup *from_cgrp;
2355
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002356 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002357 continue;
2358
Tejun Heo96d365e2014-02-13 06:58:40 -05002359 down_read(&css_set_rwsem);
2360 from_cgrp = task_cgroup_from_root(from, root);
2361 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002362
Li Zefan6f4b7e62013-07-31 16:18:36 +08002363 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002364 if (retval)
2365 break;
2366 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002367 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002368
2369 return retval;
2370}
2371EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2372
Tejun Heoacbef752014-05-13 12:16:22 -04002373static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2374 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002375{
Tejun Heoacbef752014-05-13 12:16:22 -04002376 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002377}
2378
Tejun Heoacbef752014-05-13 12:16:22 -04002379static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2380 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002381{
Tejun Heoacbef752014-05-13 12:16:22 -04002382 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002383}
2384
Tejun Heo451af502014-05-13 12:16:21 -04002385static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2386 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002387{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002388 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002389
Tejun Heoe76ecae2014-05-13 12:19:23 -04002390 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2391
2392 cgrp = cgroup_kn_lock_live(of->kn);
2393 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002394 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002395 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002396 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2397 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002398 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002399 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002400 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002401}
2402
Tejun Heo2da8ca82013-12-05 12:28:04 -05002403static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002404{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002405 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002406
Tejun Heo46cfeb02014-05-13 12:11:00 -04002407 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002408 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002409 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002410 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002411 return 0;
2412}
2413
Tejun Heo2da8ca82013-12-05 12:28:04 -05002414static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002415{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002416 struct cgroup *cgrp = seq_css(seq)->cgroup;
2417
2418 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002419 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420}
2421
Tejun Heof8f22e52014-04-23 11:13:16 -04002422static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2423{
2424 struct cgroup_subsys *ss;
2425 bool printed = false;
2426 int ssid;
2427
2428 for_each_subsys(ss, ssid) {
2429 if (ss_mask & (1 << ssid)) {
2430 if (printed)
2431 seq_putc(seq, ' ');
2432 seq_printf(seq, "%s", ss->name);
2433 printed = true;
2434 }
2435 }
2436 if (printed)
2437 seq_putc(seq, '\n');
2438}
2439
2440/* show controllers which are currently attached to the default hierarchy */
2441static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2442{
2443 struct cgroup *cgrp = seq_css(seq)->cgroup;
2444
Tejun Heo5533e012014-05-14 19:33:07 -04002445 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2446 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002447 return 0;
2448}
2449
2450/* show controllers which are enabled from the parent */
2451static int cgroup_controllers_show(struct seq_file *seq, void *v)
2452{
2453 struct cgroup *cgrp = seq_css(seq)->cgroup;
2454
Tejun Heod51f39b2014-05-16 13:22:48 -04002455 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->child_subsys_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002456 return 0;
2457}
2458
2459/* show controllers which are enabled for a given cgroup's children */
2460static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2461{
2462 struct cgroup *cgrp = seq_css(seq)->cgroup;
2463
2464 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2465 return 0;
2466}
2467
2468/**
2469 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2470 * @cgrp: root of the subtree to update csses for
2471 *
2472 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2473 * css associations need to be updated accordingly. This function looks up
2474 * all css_sets which are attached to the subtree, creates the matching
2475 * updated css_sets and migrates the tasks to the new ones.
2476 */
2477static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2478{
2479 LIST_HEAD(preloaded_csets);
2480 struct cgroup_subsys_state *css;
2481 struct css_set *src_cset;
2482 int ret;
2483
Tejun Heof8f22e52014-04-23 11:13:16 -04002484 lockdep_assert_held(&cgroup_mutex);
2485
2486 /* look up all csses currently attached to @cgrp's subtree */
2487 down_read(&css_set_rwsem);
2488 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2489 struct cgrp_cset_link *link;
2490
2491 /* self is not affected by child_subsys_mask change */
2492 if (css->cgroup == cgrp)
2493 continue;
2494
2495 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2496 cgroup_migrate_add_src(link->cset, cgrp,
2497 &preloaded_csets);
2498 }
2499 up_read(&css_set_rwsem);
2500
2501 /* NULL dst indicates self on default hierarchy */
2502 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2503 if (ret)
2504 goto out_finish;
2505
2506 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2507 struct task_struct *last_task = NULL, *task;
2508
2509 /* src_csets precede dst_csets, break on the first dst_cset */
2510 if (!src_cset->mg_src_cgrp)
2511 break;
2512
2513 /*
2514 * All tasks in src_cset need to be migrated to the
2515 * matching dst_cset. Empty it process by process. We
2516 * walk tasks but migrate processes. The leader might even
2517 * belong to a different cset but such src_cset would also
2518 * be among the target src_csets because the default
2519 * hierarchy enforces per-process membership.
2520 */
2521 while (true) {
2522 down_read(&css_set_rwsem);
2523 task = list_first_entry_or_null(&src_cset->tasks,
2524 struct task_struct, cg_list);
2525 if (task) {
2526 task = task->group_leader;
2527 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2528 get_task_struct(task);
2529 }
2530 up_read(&css_set_rwsem);
2531
2532 if (!task)
2533 break;
2534
2535 /* guard against possible infinite loop */
2536 if (WARN(last_task == task,
2537 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2538 goto out_finish;
2539 last_task = task;
2540
2541 threadgroup_lock(task);
2542 /* raced against de_thread() from another thread? */
2543 if (!thread_group_leader(task)) {
2544 threadgroup_unlock(task);
2545 put_task_struct(task);
2546 continue;
2547 }
2548
2549 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2550
2551 threadgroup_unlock(task);
2552 put_task_struct(task);
2553
2554 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2555 goto out_finish;
2556 }
2557 }
2558
2559out_finish:
2560 cgroup_migrate_finish(&preloaded_csets);
2561 return ret;
2562}
2563
2564/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002565static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2566 char *buf, size_t nbytes,
2567 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002568{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002569 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002570 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002571 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002572 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002573 int ssid, ret;
2574
2575 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002576 * Parse input - space separated list of subsystem names prefixed
2577 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002578 */
Tejun Heo451af502014-05-13 12:16:21 -04002579 buf = strstrip(buf);
2580 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002581 if (tok[0] == '\0')
2582 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002583 for_each_subsys(ss, ssid) {
Tejun Heo5533e012014-05-14 19:33:07 -04002584 if (ss->disabled || strcmp(tok + 1, ss->name) ||
2585 ((1 << ss->id) & cgrp_dfl_root_inhibit_ss_mask))
Tejun Heof8f22e52014-04-23 11:13:16 -04002586 continue;
2587
2588 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002589 enable |= 1 << ssid;
2590 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002591 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002592 disable |= 1 << ssid;
2593 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002594 } else {
2595 return -EINVAL;
2596 }
2597 break;
2598 }
2599 if (ssid == CGROUP_SUBSYS_COUNT)
2600 return -EINVAL;
2601 }
2602
Tejun Heoa9746d82014-05-13 12:19:22 -04002603 cgrp = cgroup_kn_lock_live(of->kn);
2604 if (!cgrp)
2605 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002606
2607 for_each_subsys(ss, ssid) {
2608 if (enable & (1 << ssid)) {
2609 if (cgrp->child_subsys_mask & (1 << ssid)) {
2610 enable &= ~(1 << ssid);
2611 continue;
2612 }
2613
2614 /*
2615 * Because css offlining is asynchronous, userland
2616 * might try to re-enable the same controller while
2617 * the previous instance is still around. In such
2618 * cases, wait till it's gone using offline_waitq.
2619 */
2620 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002621 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002622
2623 if (!cgroup_css(child, ss))
2624 continue;
2625
Tejun Heo0cee8b72014-05-13 12:10:59 -04002626 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002627 prepare_to_wait(&child->offline_waitq, &wait,
2628 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002629 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002630 schedule();
2631 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002632 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002633
Tejun Heoa9746d82014-05-13 12:19:22 -04002634 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002635 }
2636
2637 /* unavailable or not enabled on the parent? */
2638 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
Tejun Heod51f39b2014-05-16 13:22:48 -04002639 (cgroup_parent(cgrp) &&
2640 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ssid)))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002641 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002642 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002643 }
2644 } else if (disable & (1 << ssid)) {
2645 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2646 disable &= ~(1 << ssid);
2647 continue;
2648 }
2649
2650 /* a child has it enabled? */
2651 cgroup_for_each_live_child(child, cgrp) {
2652 if (child->child_subsys_mask & (1 << ssid)) {
2653 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002654 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002655 }
2656 }
2657 }
2658 }
2659
2660 if (!enable && !disable) {
2661 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002662 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002663 }
2664
2665 /*
2666 * Except for the root, child_subsys_mask must be zero for a cgroup
2667 * with tasks so that child cgroups don't compete against tasks.
2668 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002669 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002670 ret = -EBUSY;
2671 goto out_unlock;
2672 }
2673
2674 /*
2675 * Create csses for enables and update child_subsys_mask. This
2676 * changes cgroup_e_css() results which in turn makes the
2677 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2678 * subtree to the updated csses.
2679 */
2680 for_each_subsys(ss, ssid) {
2681 if (!(enable & (1 << ssid)))
2682 continue;
2683
2684 cgroup_for_each_live_child(child, cgrp) {
2685 ret = create_css(child, ss);
2686 if (ret)
2687 goto err_undo_css;
2688 }
2689 }
2690
2691 cgrp->child_subsys_mask |= enable;
2692 cgrp->child_subsys_mask &= ~disable;
2693
2694 ret = cgroup_update_dfl_csses(cgrp);
2695 if (ret)
2696 goto err_undo_css;
2697
2698 /* all tasks are now migrated away from the old csses, kill them */
2699 for_each_subsys(ss, ssid) {
2700 if (!(disable & (1 << ssid)))
2701 continue;
2702
2703 cgroup_for_each_live_child(child, cgrp)
2704 kill_css(cgroup_css(child, ss));
2705 }
2706
2707 kernfs_activate(cgrp->kn);
2708 ret = 0;
2709out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002710 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002711 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002712
2713err_undo_css:
2714 cgrp->child_subsys_mask &= ~enable;
2715 cgrp->child_subsys_mask |= disable;
2716
2717 for_each_subsys(ss, ssid) {
2718 if (!(enable & (1 << ssid)))
2719 continue;
2720
2721 cgroup_for_each_live_child(child, cgrp) {
2722 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2723 if (css)
2724 kill_css(css);
2725 }
2726 }
2727 goto out_unlock;
2728}
2729
Tejun Heo842b5972014-04-25 18:28:02 -04002730static int cgroup_populated_show(struct seq_file *seq, void *v)
2731{
2732 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2733 return 0;
2734}
2735
Tejun Heo2bd59d42014-02-11 11:52:49 -05002736static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2737 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002739 struct cgroup *cgrp = of->kn->parent->priv;
2740 struct cftype *cft = of->kn->priv;
2741 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002742 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002743
Tejun Heob4168642014-05-13 12:16:21 -04002744 if (cft->write)
2745 return cft->write(of, buf, nbytes, off);
2746
Tejun Heo2bd59d42014-02-11 11:52:49 -05002747 /*
2748 * kernfs guarantees that a file isn't deleted with operations in
2749 * flight, which means that the matching css is and stays alive and
2750 * doesn't need to be pinned. The RCU locking is not necessary
2751 * either. It's just for the convenience of using cgroup_css().
2752 */
2753 rcu_read_lock();
2754 css = cgroup_css(cgrp, cft->ss);
2755 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756
Tejun Heo451af502014-05-13 12:16:21 -04002757 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002758 unsigned long long v;
2759 ret = kstrtoull(buf, 0, &v);
2760 if (!ret)
2761 ret = cft->write_u64(css, cft, v);
2762 } else if (cft->write_s64) {
2763 long long v;
2764 ret = kstrtoll(buf, 0, &v);
2765 if (!ret)
2766 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002767 } else {
2768 ret = -EINVAL;
2769 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002770
Tejun Heoa742c592013-12-05 12:28:03 -05002771 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002772}
2773
Tejun Heo6612f052013-12-05 12:28:04 -05002774static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002775{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002776 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002777}
2778
2779static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2780{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002781 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002782}
2783
2784static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2785{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002786 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002787}
2788
2789static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2790{
Tejun Heo7da11272013-12-05 12:28:04 -05002791 struct cftype *cft = seq_cft(m);
2792 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002793
Tejun Heo2da8ca82013-12-05 12:28:04 -05002794 if (cft->seq_show)
2795 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002796
Tejun Heo896f5192013-12-05 12:28:04 -05002797 if (cft->read_u64)
2798 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2799 else if (cft->read_s64)
2800 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2801 else
2802 return -EINVAL;
2803 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002804}
2805
Tejun Heo2bd59d42014-02-11 11:52:49 -05002806static struct kernfs_ops cgroup_kf_single_ops = {
2807 .atomic_write_len = PAGE_SIZE,
2808 .write = cgroup_file_write,
2809 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002810};
2811
Tejun Heo2bd59d42014-02-11 11:52:49 -05002812static struct kernfs_ops cgroup_kf_ops = {
2813 .atomic_write_len = PAGE_SIZE,
2814 .write = cgroup_file_write,
2815 .seq_start = cgroup_seqfile_start,
2816 .seq_next = cgroup_seqfile_next,
2817 .seq_stop = cgroup_seqfile_stop,
2818 .seq_show = cgroup_seqfile_show,
2819};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002820
2821/*
2822 * cgroup_rename - Only allow simple rename of directories in place.
2823 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002824static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2825 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002826{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002827 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002828 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002829
Tejun Heo2bd59d42014-02-11 11:52:49 -05002830 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002831 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002832 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002833 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002834
Tejun Heo6db8e852013-06-14 11:18:22 -07002835 /*
2836 * This isn't a proper migration and its usefulness is very
2837 * limited. Disallow if sane_behavior.
2838 */
2839 if (cgroup_sane_behavior(cgrp))
2840 return -EPERM;
2841
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002842 /*
Tejun Heo8353da12014-05-13 12:19:23 -04002843 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002844 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04002845 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002846 */
2847 kernfs_break_active_protection(new_parent);
2848 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002849
Tejun Heo2bd59d42014-02-11 11:52:49 -05002850 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002851
Tejun Heo2bd59d42014-02-11 11:52:49 -05002852 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002853
Tejun Heo2bd59d42014-02-11 11:52:49 -05002854 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002855
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002856 kernfs_unbreak_active_protection(kn);
2857 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002858 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002859}
2860
Tejun Heo49957f82014-04-07 16:44:47 -04002861/* set uid and gid of cgroup dirs and files to that of the creator */
2862static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2863{
2864 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2865 .ia_uid = current_fsuid(),
2866 .ia_gid = current_fsgid(), };
2867
2868 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2869 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2870 return 0;
2871
2872 return kernfs_setattr(kn, &iattr);
2873}
2874
Tejun Heo2bb566c2013-08-08 20:11:23 -04002875static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002876{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002877 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002878 struct kernfs_node *kn;
2879 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002880 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002881
Tejun Heo2bd59d42014-02-11 11:52:49 -05002882#ifdef CONFIG_DEBUG_LOCK_ALLOC
2883 key = &cft->lockdep_key;
2884#endif
2885 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2886 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2887 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002888 if (IS_ERR(kn))
2889 return PTR_ERR(kn);
2890
2891 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002892 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002893 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002894 return ret;
2895 }
2896
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002897 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002898 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002899 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002900}
2901
Tejun Heob1f28d32013-06-28 16:24:10 -07002902/**
2903 * cgroup_addrm_files - add or remove files to a cgroup directory
2904 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002905 * @cfts: array of cftypes to be added
2906 * @is_add: whether to add or remove
2907 *
2908 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002909 * For removals, this function never fails. If addition fails, this
2910 * function doesn't remove files already added. The caller is responsible
2911 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002912 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002913static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2914 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002915{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002916 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002917 int ret;
2918
Tejun Heo01f64742014-05-13 12:19:23 -04002919 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002920
2921 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002922 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002923 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2924 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002925 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2926 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002927 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002928 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002929 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002930 continue;
2931
Li Zefan2739d3c2013-01-21 18:18:33 +08002932 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002933 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002934 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002935 pr_warn("%s: failed to add %s, err=%d\n",
2936 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002937 return ret;
2938 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002939 } else {
2940 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002941 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002942 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002943 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002944}
2945
Tejun Heo21a2d342014-02-12 09:29:49 -05002946static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002947{
2948 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002949 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002950 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002951 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002952 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002953
Tejun Heo01f64742014-05-13 12:19:23 -04002954 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002955
Li Zefane8c82d22013-06-18 18:48:37 +08002956 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002957 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002958 struct cgroup *cgrp = css->cgroup;
2959
Li Zefane8c82d22013-06-18 18:48:37 +08002960 if (cgroup_is_dead(cgrp))
2961 continue;
2962
Tejun Heo21a2d342014-02-12 09:29:49 -05002963 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002964 if (ret)
2965 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002966 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002967
2968 if (is_add && !ret)
2969 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002970 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002971}
2972
Tejun Heo2da440a2014-02-11 11:52:48 -05002973static void cgroup_exit_cftypes(struct cftype *cfts)
2974{
2975 struct cftype *cft;
2976
Tejun Heo2bd59d42014-02-11 11:52:49 -05002977 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2978 /* free copy for custom atomic_write_len, see init_cftypes() */
2979 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2980 kfree(cft->kf_ops);
2981 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002982 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002983 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002984}
2985
Tejun Heo2bd59d42014-02-11 11:52:49 -05002986static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002987{
2988 struct cftype *cft;
2989
Tejun Heo2bd59d42014-02-11 11:52:49 -05002990 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2991 struct kernfs_ops *kf_ops;
2992
Tejun Heo0adb0702014-02-12 09:29:48 -05002993 WARN_ON(cft->ss || cft->kf_ops);
2994
Tejun Heo2bd59d42014-02-11 11:52:49 -05002995 if (cft->seq_start)
2996 kf_ops = &cgroup_kf_ops;
2997 else
2998 kf_ops = &cgroup_kf_single_ops;
2999
3000 /*
3001 * Ugh... if @cft wants a custom max_write_len, we need to
3002 * make a copy of kf_ops to set its atomic_write_len.
3003 */
3004 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3005 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3006 if (!kf_ops) {
3007 cgroup_exit_cftypes(cfts);
3008 return -ENOMEM;
3009 }
3010 kf_ops->atomic_write_len = cft->max_write_len;
3011 }
3012
3013 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003014 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003015 }
3016
3017 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003018}
3019
Tejun Heo21a2d342014-02-12 09:29:49 -05003020static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3021{
Tejun Heo01f64742014-05-13 12:19:23 -04003022 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003023
3024 if (!cfts || !cfts[0].ss)
3025 return -ENOENT;
3026
3027 list_del(&cfts->node);
3028 cgroup_apply_cftypes(cfts, false);
3029 cgroup_exit_cftypes(cfts);
3030 return 0;
3031}
3032
Tejun Heo8e3f6542012-04-01 12:09:55 -07003033/**
Tejun Heo80b13582014-02-12 09:29:48 -05003034 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3035 * @cfts: zero-length name terminated array of cftypes
3036 *
3037 * Unregister @cfts. Files described by @cfts are removed from all
3038 * existing cgroups and all future cgroups won't have them either. This
3039 * function can be called anytime whether @cfts' subsys is attached or not.
3040 *
3041 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3042 * registered.
3043 */
3044int cgroup_rm_cftypes(struct cftype *cfts)
3045{
Tejun Heo21a2d342014-02-12 09:29:49 -05003046 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003047
Tejun Heo01f64742014-05-13 12:19:23 -04003048 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003049 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003050 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003051 return ret;
3052}
3053
3054/**
3055 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3056 * @ss: target cgroup subsystem
3057 * @cfts: zero-length name terminated array of cftypes
3058 *
3059 * Register @cfts to @ss. Files described by @cfts are created for all
3060 * existing cgroups to which @ss is attached and all future cgroups will
3061 * have them too. This function can be called anytime whether @ss is
3062 * attached or not.
3063 *
3064 * Returns 0 on successful registration, -errno on failure. Note that this
3065 * function currently returns 0 as long as @cfts registration is successful
3066 * even if some file creation attempts on existing cgroups fail.
3067 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003068int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003069{
Tejun Heo9ccece82013-06-28 16:24:11 -07003070 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003071
Li Zefanc731ae12014-06-05 17:16:30 +08003072 if (ss->disabled)
3073 return 0;
3074
Li Zefandc5736e2014-02-17 10:41:50 +08003075 if (!cfts || cfts[0].name[0] == '\0')
3076 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003077
Tejun Heo2bd59d42014-02-11 11:52:49 -05003078 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003079 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003080 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003081
Tejun Heo01f64742014-05-13 12:19:23 -04003082 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003083
Tejun Heo0adb0702014-02-12 09:29:48 -05003084 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003085 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003086 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003087 cgroup_rm_cftypes_locked(cfts);
3088
Tejun Heo01f64742014-05-13 12:19:23 -04003089 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003090 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003091}
Tejun Heo79578622012-04-01 12:09:56 -07003092
3093/**
Li Zefana043e3b2008-02-23 15:24:09 -08003094 * cgroup_task_count - count the number of tasks in a cgroup.
3095 * @cgrp: the cgroup in question
3096 *
3097 * Return the number of tasks in the cgroup.
3098 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003099static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003100{
3101 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003102 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003103
Tejun Heo96d365e2014-02-13 06:58:40 -05003104 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003105 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3106 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003107 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003108 return count;
3109}
3110
Tejun Heo574bd9f2012-11-09 09:12:29 -08003111/**
Tejun Heo492eb212013-08-08 20:11:25 -04003112 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003113 * @pos: the current position (%NULL to initiate traversal)
3114 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003115 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003116 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003117 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003118 * that @parent and @pos are accessible. The next sibling is guaranteed to
3119 * be returned regardless of their states.
3120 *
3121 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3122 * css which finished ->css_online() is guaranteed to be visible in the
3123 * future iterations and will stay visible until the last reference is put.
3124 * A css which hasn't finished ->css_online() or already finished
3125 * ->css_offline() may show up during traversal. It's each subsystem's
3126 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003127 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003128struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3129 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003130{
Tejun Heoc2931b72014-05-16 13:22:51 -04003131 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003132
Tejun Heo8353da12014-05-13 12:19:23 -04003133 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003134
3135 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003136 * @pos could already have been unlinked from the sibling list.
3137 * Once a cgroup is removed, its ->sibling.next is no longer
3138 * updated when its next sibling changes. CSS_RELEASED is set when
3139 * @pos is taken off list, at which time its next pointer is valid,
3140 * and, as releases are serialized, the one pointed to by the next
3141 * pointer is guaranteed to not have started release yet. This
3142 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3143 * critical section, the one pointed to by its next pointer is
3144 * guaranteed to not have finished its RCU grace period even if we
3145 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003146 *
Tejun Heode3f0342014-05-16 13:22:49 -04003147 * If @pos has CSS_RELEASED set, its next pointer can't be
3148 * dereferenced; however, as each css is given a monotonically
3149 * increasing unique serial number and always appended to the
3150 * sibling list, the next one can be found by walking the parent's
3151 * children until the first css with higher serial number than
3152 * @pos's. While this path can be slower, it happens iff iteration
3153 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003154 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003155 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003156 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3157 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3158 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003159 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003160 list_for_each_entry_rcu(next, &parent->children, sibling)
3161 if (next->serial_nr > pos->serial_nr)
Tejun Heo3b287a52013-08-08 20:11:24 -04003162 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003163 }
3164
Tejun Heo3b281af2014-04-23 11:13:15 -04003165 /*
3166 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003167 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003168 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003169 if (&next->sibling != &parent->children)
3170 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003171 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003172}
Tejun Heo53fa5262013-05-24 10:55:38 +09003173
3174/**
Tejun Heo492eb212013-08-08 20:11:25 -04003175 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003176 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003177 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178 *
Tejun Heo492eb212013-08-08 20:11:25 -04003179 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003180 * to visit for pre-order traversal of @root's descendants. @root is
3181 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003182 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003183 * While this function requires cgroup_mutex or RCU read locking, it
3184 * doesn't require the whole traversal to be contained in a single critical
3185 * section. This function will return the correct next descendant as long
3186 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003187 *
3188 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3189 * css which finished ->css_online() is guaranteed to be visible in the
3190 * future iterations and will stay visible until the last reference is put.
3191 * A css which hasn't finished ->css_online() or already finished
3192 * ->css_offline() may show up during traversal. It's each subsystem's
3193 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003194 */
Tejun Heo492eb212013-08-08 20:11:25 -04003195struct cgroup_subsys_state *
3196css_next_descendant_pre(struct cgroup_subsys_state *pos,
3197 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003198{
Tejun Heo492eb212013-08-08 20:11:25 -04003199 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003200
Tejun Heo8353da12014-05-13 12:19:23 -04003201 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003202
Tejun Heobd8815a2013-08-08 20:11:27 -04003203 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003204 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003205 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003206
3207 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003208 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003209 if (next)
3210 return next;
3211
3212 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003213 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003214 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003215 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003216 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003217 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003218 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003219
3220 return NULL;
3221}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003222
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003223/**
Tejun Heo492eb212013-08-08 20:11:25 -04003224 * css_rightmost_descendant - return the rightmost descendant of a css
3225 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003226 *
Tejun Heo492eb212013-08-08 20:11:25 -04003227 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3228 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003229 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003230 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003231 * While this function requires cgroup_mutex or RCU read locking, it
3232 * doesn't require the whole traversal to be contained in a single critical
3233 * section. This function will return the correct rightmost descendant as
3234 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003235 */
Tejun Heo492eb212013-08-08 20:11:25 -04003236struct cgroup_subsys_state *
3237css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003238{
Tejun Heo492eb212013-08-08 20:11:25 -04003239 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003240
Tejun Heo8353da12014-05-13 12:19:23 -04003241 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003242
3243 do {
3244 last = pos;
3245 /* ->prev isn't RCU safe, walk ->next till the end */
3246 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003247 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003248 pos = tmp;
3249 } while (pos);
3250
3251 return last;
3252}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003253
Tejun Heo492eb212013-08-08 20:11:25 -04003254static struct cgroup_subsys_state *
3255css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003256{
Tejun Heo492eb212013-08-08 20:11:25 -04003257 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003258
3259 do {
3260 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003261 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003262 } while (pos);
3263
3264 return last;
3265}
3266
3267/**
Tejun Heo492eb212013-08-08 20:11:25 -04003268 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003269 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003270 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003271 *
Tejun Heo492eb212013-08-08 20:11:25 -04003272 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003273 * to visit for post-order traversal of @root's descendants. @root is
3274 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003275 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003276 * While this function requires cgroup_mutex or RCU read locking, it
3277 * doesn't require the whole traversal to be contained in a single critical
3278 * section. This function will return the correct next descendant as long
3279 * as both @pos and @cgroup are accessible and @pos is a descendant of
3280 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003281 *
3282 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3283 * css which finished ->css_online() is guaranteed to be visible in the
3284 * future iterations and will stay visible until the last reference is put.
3285 * A css which hasn't finished ->css_online() or already finished
3286 * ->css_offline() may show up during traversal. It's each subsystem's
3287 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003288 */
Tejun Heo492eb212013-08-08 20:11:25 -04003289struct cgroup_subsys_state *
3290css_next_descendant_post(struct cgroup_subsys_state *pos,
3291 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003292{
Tejun Heo492eb212013-08-08 20:11:25 -04003293 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003294
Tejun Heo8353da12014-05-13 12:19:23 -04003295 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003296
Tejun Heo58b79a92013-09-06 15:31:08 -04003297 /* if first iteration, visit leftmost descendant which may be @root */
3298 if (!pos)
3299 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003300
Tejun Heobd8815a2013-08-08 20:11:27 -04003301 /* if we visited @root, we're done */
3302 if (pos == root)
3303 return NULL;
3304
Tejun Heo574bd9f2012-11-09 09:12:29 -08003305 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003306 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003307 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003308 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003309
3310 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003311 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003312}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003313
Tejun Heof3d46502014-05-16 13:22:52 -04003314/**
3315 * css_has_online_children - does a css have online children
3316 * @css: the target css
3317 *
3318 * Returns %true if @css has any online children; otherwise, %false. This
3319 * function can be called from any context but the caller is responsible
3320 * for synchronizing against on/offlining as necessary.
3321 */
3322bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003323{
Tejun Heof3d46502014-05-16 13:22:52 -04003324 struct cgroup_subsys_state *child;
3325 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003326
3327 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003328 css_for_each_child(child, css) {
3329 if (css->flags & CSS_ONLINE) {
3330 ret = true;
3331 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003332 }
3333 }
3334 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003335 return ret;
Tejun Heocbc125e2014-05-14 09:15:01 -04003336}
3337
Tejun Heo0942eee2013-08-08 20:11:26 -04003338/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003339 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003340 * @it: the iterator to advance
3341 *
3342 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003343 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003344static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003345{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003346 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003347 struct cgrp_cset_link *link;
3348 struct css_set *cset;
3349
3350 /* Advance to the next non-empty css_set */
3351 do {
3352 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003353 if (l == it->cset_head) {
3354 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003355 return;
3356 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003357
3358 if (it->ss) {
3359 cset = container_of(l, struct css_set,
3360 e_cset_node[it->ss->id]);
3361 } else {
3362 link = list_entry(l, struct cgrp_cset_link, cset_link);
3363 cset = link->cset;
3364 }
Tejun Heoc7561122014-02-25 10:04:01 -05003365 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3366
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003367 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003368
3369 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003370 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003371 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003372 it->task_pos = cset->mg_tasks.next;
3373
3374 it->tasks_head = &cset->tasks;
3375 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003376}
3377
Tejun Heo0942eee2013-08-08 20:11:26 -04003378/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003379 * css_task_iter_start - initiate task iteration
3380 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003381 * @it: the task iterator to use
3382 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003383 * Initiate iteration through the tasks of @css. The caller can call
3384 * css_task_iter_next() to walk through the tasks until the function
3385 * returns NULL. On completion of iteration, css_task_iter_end() must be
3386 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003387 *
3388 * Note that this function acquires a lock which is released when the
3389 * iteration finishes. The caller can't sleep while iteration is in
3390 * progress.
3391 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003392void css_task_iter_start(struct cgroup_subsys_state *css,
3393 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003394 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003395{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003396 /* no one should try to iterate before mounting cgroups */
3397 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003398
Tejun Heo96d365e2014-02-13 06:58:40 -05003399 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003400
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003401 it->ss = css->ss;
3402
3403 if (it->ss)
3404 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3405 else
3406 it->cset_pos = &css->cgroup->cset_links;
3407
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003408 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003409
Tejun Heo72ec7022013-08-08 20:11:26 -04003410 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003411}
3412
Tejun Heo0942eee2013-08-08 20:11:26 -04003413/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003414 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003415 * @it: the task iterator being iterated
3416 *
3417 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003418 * initialized via css_task_iter_start(). Returns NULL when the iteration
3419 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003420 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003421struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003422{
3423 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003424 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003425
3426 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003427 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003428 return NULL;
3429 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003430
3431 /*
3432 * Advance iterator to find next entry. cset->tasks is consumed
3433 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3434 * next cset.
3435 */
Paul Menage817929e2007-10-18 23:39:36 -07003436 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003437
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003438 if (l == it->tasks_head)
3439 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003440
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003441 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003442 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003443 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003444 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003445
Paul Menage817929e2007-10-18 23:39:36 -07003446 return res;
3447}
3448
Tejun Heo0942eee2013-08-08 20:11:26 -04003449/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003450 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003451 * @it: the task iterator to finish
3452 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003453 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003454 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003455void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003456 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003457{
Tejun Heo96d365e2014-02-13 06:58:40 -05003458 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003459}
3460
3461/**
3462 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3463 * @to: cgroup to which the tasks will be moved
3464 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003465 *
3466 * Locking rules between cgroup_post_fork() and the migration path
3467 * guarantee that, if a task is forking while being migrated, the new child
3468 * is guaranteed to be either visible in the source cgroup after the
3469 * parent's migration is complete or put into the target cgroup. No task
3470 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003471 */
3472int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3473{
Tejun Heo952aaa12014-02-25 10:04:03 -05003474 LIST_HEAD(preloaded_csets);
3475 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003476 struct css_task_iter it;
3477 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003478 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003479
Tejun Heo952aaa12014-02-25 10:04:03 -05003480 mutex_lock(&cgroup_mutex);
3481
3482 /* all tasks in @from are being moved, all csets are source */
3483 down_read(&css_set_rwsem);
3484 list_for_each_entry(link, &from->cset_links, cset_link)
3485 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3486 up_read(&css_set_rwsem);
3487
3488 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3489 if (ret)
3490 goto out_err;
3491
3492 /*
3493 * Migrate tasks one-by-one until @form is empty. This fails iff
3494 * ->can_attach() fails.
3495 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003496 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003497 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003498 task = css_task_iter_next(&it);
3499 if (task)
3500 get_task_struct(task);
3501 css_task_iter_end(&it);
3502
3503 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003504 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003505 put_task_struct(task);
3506 }
3507 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003508out_err:
3509 cgroup_migrate_finish(&preloaded_csets);
3510 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003511 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003512}
3513
Paul Menage817929e2007-10-18 23:39:36 -07003514/*
Ben Blum102a7752009-09-23 15:56:26 -07003515 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003516 *
3517 * Reading this file can return large amounts of data if a cgroup has
3518 * *lots* of attached tasks. So it may need several calls to read(),
3519 * but we cannot guarantee that the information we produce is correct
3520 * unless we produce it entirely atomically.
3521 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003522 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003523
Li Zefan24528252012-01-20 11:58:43 +08003524/* which pidlist file are we talking about? */
3525enum cgroup_filetype {
3526 CGROUP_FILE_PROCS,
3527 CGROUP_FILE_TASKS,
3528};
3529
3530/*
3531 * A pidlist is a list of pids that virtually represents the contents of one
3532 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3533 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3534 * to the cgroup.
3535 */
3536struct cgroup_pidlist {
3537 /*
3538 * used to find which pidlist is wanted. doesn't change as long as
3539 * this particular list stays in the list.
3540 */
3541 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3542 /* array of xids */
3543 pid_t *list;
3544 /* how many elements the above list has */
3545 int length;
Li Zefan24528252012-01-20 11:58:43 +08003546 /* each of these stored in a list by its cgroup */
3547 struct list_head links;
3548 /* pointer to the cgroup we belong to, for list removal purposes */
3549 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003550 /* for delayed destruction */
3551 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003552};
3553
Paul Menagebbcb81d2007-10-18 23:39:32 -07003554/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003555 * The following two functions "fix" the issue where there are more pids
3556 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3557 * TODO: replace with a kernel-wide solution to this problem
3558 */
3559#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3560static void *pidlist_allocate(int count)
3561{
3562 if (PIDLIST_TOO_LARGE(count))
3563 return vmalloc(count * sizeof(pid_t));
3564 else
3565 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3566}
Tejun Heob1a21362013-11-29 10:42:58 -05003567
Ben Blumd1d9fd32009-09-23 15:56:28 -07003568static void pidlist_free(void *p)
3569{
3570 if (is_vmalloc_addr(p))
3571 vfree(p);
3572 else
3573 kfree(p);
3574}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003575
3576/*
Tejun Heob1a21362013-11-29 10:42:58 -05003577 * Used to destroy all pidlists lingering waiting for destroy timer. None
3578 * should be left afterwards.
3579 */
3580static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3581{
3582 struct cgroup_pidlist *l, *tmp_l;
3583
3584 mutex_lock(&cgrp->pidlist_mutex);
3585 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3586 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3587 mutex_unlock(&cgrp->pidlist_mutex);
3588
3589 flush_workqueue(cgroup_pidlist_destroy_wq);
3590 BUG_ON(!list_empty(&cgrp->pidlists));
3591}
3592
3593static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3594{
3595 struct delayed_work *dwork = to_delayed_work(work);
3596 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3597 destroy_dwork);
3598 struct cgroup_pidlist *tofree = NULL;
3599
3600 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003601
3602 /*
Tejun Heo04502362013-11-29 10:42:59 -05003603 * Destroy iff we didn't get queued again. The state won't change
3604 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003605 */
Tejun Heo04502362013-11-29 10:42:59 -05003606 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003607 list_del(&l->links);
3608 pidlist_free(l->list);
3609 put_pid_ns(l->key.ns);
3610 tofree = l;
3611 }
3612
Tejun Heob1a21362013-11-29 10:42:58 -05003613 mutex_unlock(&l->owner->pidlist_mutex);
3614 kfree(tofree);
3615}
3616
3617/*
Ben Blum102a7752009-09-23 15:56:26 -07003618 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003619 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003620 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003621static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003622{
Ben Blum102a7752009-09-23 15:56:26 -07003623 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003624
3625 /*
3626 * we presume the 0th element is unique, so i starts at 1. trivial
3627 * edge cases first; no work needs to be done for either
3628 */
3629 if (length == 0 || length == 1)
3630 return length;
3631 /* src and dest walk down the list; dest counts unique elements */
3632 for (src = 1; src < length; src++) {
3633 /* find next unique element */
3634 while (list[src] == list[src-1]) {
3635 src++;
3636 if (src == length)
3637 goto after;
3638 }
3639 /* dest always points to where the next unique element goes */
3640 list[dest] = list[src];
3641 dest++;
3642 }
3643after:
Ben Blum102a7752009-09-23 15:56:26 -07003644 return dest;
3645}
3646
Tejun Heoafb2bc12013-11-29 10:42:59 -05003647/*
3648 * The two pid files - task and cgroup.procs - guaranteed that the result
3649 * is sorted, which forced this whole pidlist fiasco. As pid order is
3650 * different per namespace, each namespace needs differently sorted list,
3651 * making it impossible to use, for example, single rbtree of member tasks
3652 * sorted by task pointer. As pidlists can be fairly large, allocating one
3653 * per open file is dangerous, so cgroup had to implement shared pool of
3654 * pidlists keyed by cgroup and namespace.
3655 *
3656 * All this extra complexity was caused by the original implementation
3657 * committing to an entirely unnecessary property. In the long term, we
3658 * want to do away with it. Explicitly scramble sort order if
3659 * sane_behavior so that no such expectation exists in the new interface.
3660 *
3661 * Scrambling is done by swapping every two consecutive bits, which is
3662 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3663 */
3664static pid_t pid_fry(pid_t pid)
3665{
3666 unsigned a = pid & 0x55555555;
3667 unsigned b = pid & 0xAAAAAAAA;
3668
3669 return (a << 1) | (b >> 1);
3670}
3671
3672static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3673{
3674 if (cgroup_sane_behavior(cgrp))
3675 return pid_fry(pid);
3676 else
3677 return pid;
3678}
3679
Ben Blum102a7752009-09-23 15:56:26 -07003680static int cmppid(const void *a, const void *b)
3681{
3682 return *(pid_t *)a - *(pid_t *)b;
3683}
3684
Tejun Heoafb2bc12013-11-29 10:42:59 -05003685static int fried_cmppid(const void *a, const void *b)
3686{
3687 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3688}
3689
Ben Blum72a8cb32009-09-23 15:56:27 -07003690static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3691 enum cgroup_filetype type)
3692{
3693 struct cgroup_pidlist *l;
3694 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003695 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003696
Tejun Heoe6b81712013-11-29 10:42:59 -05003697 lockdep_assert_held(&cgrp->pidlist_mutex);
3698
3699 list_for_each_entry(l, &cgrp->pidlists, links)
3700 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003701 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003702 return NULL;
3703}
3704
Ben Blum72a8cb32009-09-23 15:56:27 -07003705/*
3706 * find the appropriate pidlist for our purpose (given procs vs tasks)
3707 * returns with the lock on that pidlist already held, and takes care
3708 * of the use count, or returns NULL with no locks held if we're out of
3709 * memory.
3710 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003711static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3712 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003713{
3714 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003715
Tejun Heoe6b81712013-11-29 10:42:59 -05003716 lockdep_assert_held(&cgrp->pidlist_mutex);
3717
3718 l = cgroup_pidlist_find(cgrp, type);
3719 if (l)
3720 return l;
3721
Ben Blum72a8cb32009-09-23 15:56:27 -07003722 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003723 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003724 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003725 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003726
Tejun Heob1a21362013-11-29 10:42:58 -05003727 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003728 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003729 /* don't need task_nsproxy() if we're looking at ourself */
3730 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003731 l->owner = cgrp;
3732 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003733 return l;
3734}
3735
3736/*
Ben Blum102a7752009-09-23 15:56:26 -07003737 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3738 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003739static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3740 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003741{
3742 pid_t *array;
3743 int length;
3744 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003745 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003746 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003747 struct cgroup_pidlist *l;
3748
Tejun Heo4bac00d2013-11-29 10:42:59 -05003749 lockdep_assert_held(&cgrp->pidlist_mutex);
3750
Ben Blum102a7752009-09-23 15:56:26 -07003751 /*
3752 * If cgroup gets more users after we read count, we won't have
3753 * enough space - tough. This race is indistinguishable to the
3754 * caller from the case that the additional cgroup users didn't
3755 * show up until sometime later on.
3756 */
3757 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003758 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003759 if (!array)
3760 return -ENOMEM;
3761 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003762 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003763 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003764 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003765 break;
Ben Blum102a7752009-09-23 15:56:26 -07003766 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003767 if (type == CGROUP_FILE_PROCS)
3768 pid = task_tgid_vnr(tsk);
3769 else
3770 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003771 if (pid > 0) /* make sure to only use valid results */
3772 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003773 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003774 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003775 length = n;
3776 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003777 if (cgroup_sane_behavior(cgrp))
3778 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3779 else
3780 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003781 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003782 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003783
Tejun Heoe6b81712013-11-29 10:42:59 -05003784 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003785 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003786 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003787 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003788 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003789 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003790
3791 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003792 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003793 l->list = array;
3794 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003795 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003796 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003797}
3798
Balbir Singh846c7bb2007-10-18 23:39:44 -07003799/**
Li Zefana043e3b2008-02-23 15:24:09 -08003800 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003801 * @stats: cgroupstats to fill information into
3802 * @dentry: A dentry entry belonging to the cgroup for which stats have
3803 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003804 *
3805 * Build and fill cgroupstats so that taskstats can export it to user
3806 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003807 */
3808int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3809{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003810 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003811 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003812 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003813 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003814
Tejun Heo2bd59d42014-02-11 11:52:49 -05003815 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3816 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3817 kernfs_type(kn) != KERNFS_DIR)
3818 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003819
Li Zefanbad34662014-02-14 16:54:28 +08003820 mutex_lock(&cgroup_mutex);
3821
Tejun Heo2bd59d42014-02-11 11:52:49 -05003822 /*
3823 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003824 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003825 * @kn->priv is RCU safe. Let's do the RCU dancing.
3826 */
3827 rcu_read_lock();
3828 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003829 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003830 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003831 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003832 return -ENOENT;
3833 }
Li Zefanbad34662014-02-14 16:54:28 +08003834 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003835
Tejun Heo9d800df2014-05-14 09:15:00 -04003836 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003837 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003838 switch (tsk->state) {
3839 case TASK_RUNNING:
3840 stats->nr_running++;
3841 break;
3842 case TASK_INTERRUPTIBLE:
3843 stats->nr_sleeping++;
3844 break;
3845 case TASK_UNINTERRUPTIBLE:
3846 stats->nr_uninterruptible++;
3847 break;
3848 case TASK_STOPPED:
3849 stats->nr_stopped++;
3850 break;
3851 default:
3852 if (delayacct_is_task_waiting_on_io(tsk))
3853 stats->nr_io_wait++;
3854 break;
3855 }
3856 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003857 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003858
Li Zefanbad34662014-02-14 16:54:28 +08003859 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003860 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003861}
3862
Paul Menage8f3ff202009-09-23 15:56:25 -07003863
Paul Menagecc31edc2008-10-18 20:28:04 -07003864/*
Ben Blum102a7752009-09-23 15:56:26 -07003865 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003866 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003867 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003868 */
3869
Ben Blum102a7752009-09-23 15:56:26 -07003870static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003871{
3872 /*
3873 * Initially we receive a position value that corresponds to
3874 * one more than the last pid shown (or 0 on the first call or
3875 * after a seek to the start). Use a binary-search to find the
3876 * next pid to display, if any
3877 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003878 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003879 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003880 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003881 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003882 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003883 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003884
Tejun Heo4bac00d2013-11-29 10:42:59 -05003885 mutex_lock(&cgrp->pidlist_mutex);
3886
3887 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003888 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003889 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003890 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003891 * could already have been destroyed.
3892 */
Tejun Heo5d224442013-12-05 12:28:04 -05003893 if (of->priv)
3894 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003895
3896 /*
3897 * Either this is the first start() after open or the matching
3898 * pidlist has been destroyed inbetween. Create a new one.
3899 */
Tejun Heo5d224442013-12-05 12:28:04 -05003900 if (!of->priv) {
3901 ret = pidlist_array_load(cgrp, type,
3902 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003903 if (ret)
3904 return ERR_PTR(ret);
3905 }
Tejun Heo5d224442013-12-05 12:28:04 -05003906 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003907
Paul Menagecc31edc2008-10-18 20:28:04 -07003908 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003909 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003910
Paul Menagecc31edc2008-10-18 20:28:04 -07003911 while (index < end) {
3912 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003913 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003914 index = mid;
3915 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003916 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003917 index = mid + 1;
3918 else
3919 end = mid;
3920 }
3921 }
3922 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003923 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003924 return NULL;
3925 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003926 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003927 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003928 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003929}
3930
Ben Blum102a7752009-09-23 15:56:26 -07003931static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003932{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003933 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003934 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003935
Tejun Heo5d224442013-12-05 12:28:04 -05003936 if (l)
3937 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003938 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003939 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003940}
3941
Ben Blum102a7752009-09-23 15:56:26 -07003942static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003943{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003944 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003945 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003946 pid_t *p = v;
3947 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003948 /*
3949 * Advance to the next pid in the array. If this goes off the
3950 * end, we're done
3951 */
3952 p++;
3953 if (p >= end) {
3954 return NULL;
3955 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003956 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003957 return p;
3958 }
3959}
3960
Ben Blum102a7752009-09-23 15:56:26 -07003961static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003962{
3963 return seq_printf(s, "%d\n", *(int *)v);
3964}
3965
Tejun Heo182446d2013-08-08 20:11:24 -04003966static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3967 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003968{
Tejun Heo182446d2013-08-08 20:11:24 -04003969 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003970}
3971
Tejun Heo182446d2013-08-08 20:11:24 -04003972static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3973 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003974{
Tejun Heo182446d2013-08-08 20:11:24 -04003975 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003976 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003977 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003978 else
Tejun Heo182446d2013-08-08 20:11:24 -04003979 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003980 return 0;
3981}
3982
Tejun Heo182446d2013-08-08 20:11:24 -04003983static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3984 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003985{
Tejun Heo182446d2013-08-08 20:11:24 -04003986 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003987}
3988
Tejun Heo182446d2013-08-08 20:11:24 -04003989static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3990 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003991{
3992 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003993 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003994 else
Tejun Heo182446d2013-08-08 20:11:24 -04003995 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003996 return 0;
3997}
3998
Tejun Heod5c56ce2013-06-03 19:14:34 -07003999static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004000 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004001 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004002 .seq_start = cgroup_pidlist_start,
4003 .seq_next = cgroup_pidlist_next,
4004 .seq_stop = cgroup_pidlist_stop,
4005 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004006 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004007 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004008 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004009 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004010 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004011 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004012 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004013 .read_u64 = cgroup_clone_children_read,
4014 .write_u64 = cgroup_clone_children_write,
4015 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004016 {
Tejun Heo873fe092013-04-14 20:15:26 -07004017 .name = "cgroup.sane_behavior",
4018 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004019 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004020 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004021 {
4022 .name = "cgroup.controllers",
4023 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
4024 .seq_show = cgroup_root_controllers_show,
4025 },
4026 {
4027 .name = "cgroup.controllers",
4028 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4029 .seq_show = cgroup_controllers_show,
4030 },
4031 {
4032 .name = "cgroup.subtree_control",
4033 .flags = CFTYPE_ONLY_ON_DFL,
4034 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004035 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004036 },
Tejun Heo842b5972014-04-25 18:28:02 -04004037 {
4038 .name = "cgroup.populated",
4039 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4040 .seq_show = cgroup_populated_show,
4041 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004042
4043 /*
4044 * Historical crazy stuff. These don't have "cgroup." prefix and
4045 * don't exist if sane_behavior. If you're depending on these, be
4046 * prepared to be burned.
4047 */
4048 {
4049 .name = "tasks",
4050 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004051 .seq_start = cgroup_pidlist_start,
4052 .seq_next = cgroup_pidlist_next,
4053 .seq_stop = cgroup_pidlist_stop,
4054 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004055 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004056 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004057 .mode = S_IRUGO | S_IWUSR,
4058 },
4059 {
4060 .name = "notify_on_release",
4061 .flags = CFTYPE_INSANE,
4062 .read_u64 = cgroup_read_notify_on_release,
4063 .write_u64 = cgroup_write_notify_on_release,
4064 },
Tejun Heo873fe092013-04-14 20:15:26 -07004065 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004066 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004067 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004068 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004069 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004070 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004071 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004072 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004073};
4074
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004075/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004076 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004077 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004078 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004079 *
4080 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004081 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004082static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004084 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004085 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004086
Tejun Heo8e3f6542012-04-01 12:09:55 -07004087 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004088 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004089 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004090
Tejun Heo69dfa002014-05-04 15:09:13 -04004091 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004092 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004093
Tejun Heo0adb0702014-02-12 09:29:48 -05004094 list_for_each_entry(cfts, &ss->cfts, node) {
4095 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004096 if (ret < 0)
4097 goto err;
4098 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004101err:
4102 cgroup_clear_dir(cgrp, subsys_mask);
4103 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004104}
4105
Tejun Heo0c21ead2013-08-13 20:22:51 -04004106/*
4107 * css destruction is four-stage process.
4108 *
4109 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4110 * Implemented in kill_css().
4111 *
4112 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004113 * and thus css_tryget_online() is guaranteed to fail, the css can be
4114 * offlined by invoking offline_css(). After offlining, the base ref is
4115 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004116 *
4117 * 3. When the percpu_ref reaches zero, the only possible remaining
4118 * accessors are inside RCU read sections. css_release() schedules the
4119 * RCU callback.
4120 *
4121 * 4. After the grace period, the css can be freed. Implemented in
4122 * css_free_work_fn().
4123 *
4124 * It is actually hairier because both step 2 and 4 require process context
4125 * and thus involve punting to css->destroy_work adding two additional
4126 * steps to the already complex sequence.
4127 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004128static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004129{
4130 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004131 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004132 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004133
Tejun Heo9d755d32014-05-14 09:15:02 -04004134 if (css->ss) {
4135 /* css free path */
4136 if (css->parent)
4137 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004138
Tejun Heo9d755d32014-05-14 09:15:02 -04004139 css->ss->css_free(css);
4140 cgroup_put(cgrp);
4141 } else {
4142 /* cgroup free path */
4143 atomic_dec(&cgrp->root->nr_cgrps);
4144 cgroup_pidlist_destroy_all(cgrp);
4145
Tejun Heod51f39b2014-05-16 13:22:48 -04004146 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004147 /*
4148 * We get a ref to the parent, and put the ref when
4149 * this cgroup is being freed, so it's guaranteed
4150 * that the parent won't be destroyed before its
4151 * children.
4152 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004153 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004154 kernfs_put(cgrp->kn);
4155 kfree(cgrp);
4156 } else {
4157 /*
4158 * This is root cgroup's refcnt reaching zero,
4159 * which indicates that the root should be
4160 * released.
4161 */
4162 cgroup_destroy_root(cgrp->root);
4163 }
4164 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004165}
4166
4167static void css_free_rcu_fn(struct rcu_head *rcu_head)
4168{
4169 struct cgroup_subsys_state *css =
4170 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4171
Tejun Heo0c21ead2013-08-13 20:22:51 -04004172 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004173 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004174}
4175
Tejun Heo25e15d82014-05-14 09:15:02 -04004176static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004177{
4178 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004179 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004180 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004181 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004182
Tejun Heo1fed1b22014-05-16 13:22:49 -04004183 mutex_lock(&cgroup_mutex);
4184
Tejun Heode3f0342014-05-16 13:22:49 -04004185 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004186 list_del_rcu(&css->sibling);
4187
Tejun Heo9d755d32014-05-14 09:15:02 -04004188 if (ss) {
4189 /* css release path */
4190 cgroup_idr_remove(&ss->css_idr, css->id);
4191 } else {
4192 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004193 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4194 cgrp->id = -1;
4195 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004196
Tejun Heo1fed1b22014-05-16 13:22:49 -04004197 mutex_unlock(&cgroup_mutex);
4198
Tejun Heo0c21ead2013-08-13 20:22:51 -04004199 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004200}
4201
Tejun Heo25e15d82014-05-14 09:15:02 -04004202static void css_release(struct percpu_ref *ref)
4203{
4204 struct cgroup_subsys_state *css =
4205 container_of(ref, struct cgroup_subsys_state, refcnt);
4206
4207 INIT_WORK(&css->destroy_work, css_release_work_fn);
4208 queue_work(cgroup_destroy_wq, &css->destroy_work);
4209}
4210
Tejun Heoddfcada2014-05-04 15:09:14 -04004211static void init_and_link_css(struct cgroup_subsys_state *css,
4212 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004214 lockdep_assert_held(&cgroup_mutex);
4215
Tejun Heoddfcada2014-05-04 15:09:14 -04004216 cgroup_get(cgrp);
4217
Tejun Heod5c419b2014-05-16 13:22:48 -04004218 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004219 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004220 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004221 INIT_LIST_HEAD(&css->sibling);
4222 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004223 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004224
Tejun Heod51f39b2014-05-16 13:22:48 -04004225 if (cgroup_parent(cgrp)) {
4226 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004227 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004228 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004229
Tejun Heoca8bdca2013-08-26 18:40:56 -04004230 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231}
4232
Li Zefan2a4ac632013-07-31 16:16:40 +08004233/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004234static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004235{
Tejun Heo623f9262013-08-13 11:01:55 -04004236 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004237 int ret = 0;
4238
Tejun Heoa31f2d32012-11-19 08:13:37 -08004239 lockdep_assert_held(&cgroup_mutex);
4240
Tejun Heo92fb9742012-11-19 08:13:38 -08004241 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004242 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004243 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004244 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004245 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004246 }
Tejun Heob1929db2012-11-19 08:13:38 -08004247 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004248}
4249
Li Zefan2a4ac632013-07-31 16:16:40 +08004250/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004251static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004252{
Tejun Heo623f9262013-08-13 11:01:55 -04004253 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004254
4255 lockdep_assert_held(&cgroup_mutex);
4256
4257 if (!(css->flags & CSS_ONLINE))
4258 return;
4259
Li Zefand7eeac12013-03-12 15:35:59 -07004260 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004261 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004262
Tejun Heoeb954192013-08-08 20:11:23 -04004263 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004264 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004265
4266 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004267}
4268
Tejun Heoc81c925a2013-12-06 15:11:56 -05004269/**
4270 * create_css - create a cgroup_subsys_state
4271 * @cgrp: the cgroup new css will be associated with
4272 * @ss: the subsys of new css
4273 *
4274 * Create a new css associated with @cgrp - @ss pair. On success, the new
4275 * css is online and installed in @cgrp with all interface files created.
4276 * Returns 0 on success, -errno on failure.
4277 */
4278static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4279{
Tejun Heod51f39b2014-05-16 13:22:48 -04004280 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004281 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004282 struct cgroup_subsys_state *css;
4283 int err;
4284
Tejun Heoc81c925a2013-12-06 15:11:56 -05004285 lockdep_assert_held(&cgroup_mutex);
4286
Tejun Heo1fed1b22014-05-16 13:22:49 -04004287 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004288 if (IS_ERR(css))
4289 return PTR_ERR(css);
4290
Tejun Heoddfcada2014-05-04 15:09:14 -04004291 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004292
Tejun Heoc81c925a2013-12-06 15:11:56 -05004293 err = percpu_ref_init(&css->refcnt, css_release);
4294 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004295 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004296
Tejun Heo15a4c832014-05-04 15:09:14 -04004297 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4298 if (err < 0)
4299 goto err_free_percpu_ref;
4300 css->id = err;
4301
Tejun Heoaec25022014-02-08 10:36:58 -05004302 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004303 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004304 goto err_free_id;
4305
4306 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004307 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004308 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004309
4310 err = online_css(css);
4311 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004312 goto err_list_del;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004313
Tejun Heoc81c925a2013-12-06 15:11:56 -05004314 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004315 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004316 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 -04004317 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004318 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004319 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004320 ss->warned_broken_hierarchy = true;
4321 }
4322
4323 return 0;
4324
Tejun Heo1fed1b22014-05-16 13:22:49 -04004325err_list_del:
4326 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004327 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004328err_free_id:
4329 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004330err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004331 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004332err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004333 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004334 return err;
4335}
4336
Tejun Heob3bfd982014-05-13 12:19:22 -04004337static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4338 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339{
Tejun Heoa9746d82014-05-13 12:19:22 -04004340 struct cgroup *parent, *cgrp;
4341 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004342 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004343 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004344 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004345
Tejun Heoa9746d82014-05-13 12:19:22 -04004346 parent = cgroup_kn_lock_live(parent_kn);
4347 if (!parent)
4348 return -ENODEV;
4349 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004350
4351 /* allocate the cgroup and its ID, 0 is reserved for the root */
4352 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4353 if (!cgrp) {
4354 ret = -ENOMEM;
4355 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004356 }
4357
Tejun Heo9d755d32014-05-14 09:15:02 -04004358 ret = percpu_ref_init(&cgrp->self.refcnt, css_release);
4359 if (ret)
4360 goto out_free_cgrp;
4361
Li Zefan0ab02ca2014-02-11 16:05:46 +08004362 /*
4363 * Temporarily set the pointer to NULL, so idr_find() won't return
4364 * a half-baked cgroup.
4365 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004366 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004367 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004368 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004369 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004370 }
4371
Paul Menagecc31edc2008-10-18 20:28:04 -07004372 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373
Tejun Heo9d800df2014-05-14 09:15:00 -04004374 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004375 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376
Li Zefanb6abdb02008-03-04 14:28:19 -08004377 if (notify_on_release(parent))
4378 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4379
Tejun Heo2260e7f2012-11-19 08:13:38 -08004380 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4381 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004382
Tejun Heo2bd59d42014-02-11 11:52:49 -05004383 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004384 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004385 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004386 ret = PTR_ERR(kn);
4387 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004388 }
4389 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390
Tejun Heo6f305582014-02-12 09:29:50 -05004391 /*
4392 * This extra ref will be put in cgroup_free_fn() and guarantees
4393 * that @cgrp->kn is always accessible.
4394 */
4395 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396
Tejun Heo0cb51d72014-05-16 13:22:49 -04004397 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004398
Tejun Heo4e139af2012-11-19 08:13:36 -08004399 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004400 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004401 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004402 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004403
Tejun Heo0d802552013-12-06 15:11:56 -05004404 /*
4405 * @cgrp is now fully operational. If something fails after this
4406 * point, it'll be released via the normal destruction path.
4407 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004408 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004409
Tejun Heoba0f4d72014-05-13 12:19:22 -04004410 ret = cgroup_kn_set_ugid(kn);
4411 if (ret)
4412 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004413
Tejun Heoba0f4d72014-05-13 12:19:22 -04004414 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4415 if (ret)
4416 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004417
Tejun Heo9d403e92013-12-06 15:11:56 -05004418 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004419 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004420 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004421 ret = create_css(cgrp, ss);
4422 if (ret)
4423 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004424 }
Tejun Heoa8638032012-11-09 09:12:29 -08004425 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004426
Tejun Heobd53d612014-04-23 11:13:16 -04004427 /*
4428 * On the default hierarchy, a child doesn't automatically inherit
4429 * child_subsys_mask from the parent. Each is configured manually.
4430 */
4431 if (!cgroup_on_dfl(cgrp))
4432 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004433
Tejun Heo2bd59d42014-02-11 11:52:49 -05004434 kernfs_activate(kn);
4435
Tejun Heoba0f4d72014-05-13 12:19:22 -04004436 ret = 0;
4437 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004438
Tejun Heoba0f4d72014-05-13 12:19:22 -04004439out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004440 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004441out_cancel_ref:
4442 percpu_ref_cancel_init(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004443out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004444 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004445out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004446 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004447 return ret;
4448
4449out_destroy:
4450 cgroup_destroy_locked(cgrp);
4451 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452}
4453
Tejun Heo223dbc32013-08-13 20:22:50 -04004454/*
4455 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004456 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4457 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004458 */
4459static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004460{
Tejun Heo223dbc32013-08-13 20:22:50 -04004461 struct cgroup_subsys_state *css =
4462 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004463
Tejun Heof20104d2013-08-13 20:22:50 -04004464 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004465 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004466 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004467
Tejun Heo09a503ea2013-08-13 20:22:50 -04004468 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004469}
4470
Tejun Heo223dbc32013-08-13 20:22:50 -04004471/* css kill confirmation processing requires process context, bounce */
4472static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004473{
4474 struct cgroup_subsys_state *css =
4475 container_of(ref, struct cgroup_subsys_state, refcnt);
4476
Tejun Heo223dbc32013-08-13 20:22:50 -04004477 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004478 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004479}
4480
Tejun Heof392e512014-04-23 11:13:14 -04004481/**
4482 * kill_css - destroy a css
4483 * @css: css to destroy
4484 *
4485 * This function initiates destruction of @css by removing cgroup interface
4486 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004487 * asynchronously once css_tryget_online() is guaranteed to fail and when
4488 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004489 */
4490static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004491{
Tejun Heo01f64742014-05-13 12:19:23 -04004492 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004493
Tejun Heo2bd59d42014-02-11 11:52:49 -05004494 /*
4495 * This must happen before css is disassociated with its cgroup.
4496 * See seq_css() for details.
4497 */
Tejun Heoaec25022014-02-08 10:36:58 -05004498 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004499
Tejun Heoedae0c32013-08-13 20:22:51 -04004500 /*
4501 * Killing would put the base ref, but we need to keep it alive
4502 * until after ->css_offline().
4503 */
4504 css_get(css);
4505
4506 /*
4507 * cgroup core guarantees that, by the time ->css_offline() is
4508 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004509 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004510 * proceed to offlining css's because percpu_ref_kill() doesn't
4511 * guarantee that the ref is seen as killed on all CPUs on return.
4512 *
4513 * Use percpu_ref_kill_and_confirm() to get notifications as each
4514 * css is confirmed to be seen as killed on all CPUs.
4515 */
4516 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004517}
4518
4519/**
4520 * cgroup_destroy_locked - the first stage of cgroup destruction
4521 * @cgrp: cgroup to be destroyed
4522 *
4523 * css's make use of percpu refcnts whose killing latency shouldn't be
4524 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004525 * guarantee that css_tryget_online() won't succeed by the time
4526 * ->css_offline() is invoked. To satisfy all the requirements,
4527 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004528 *
4529 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4530 * userland visible parts and start killing the percpu refcnts of
4531 * css's. Set up so that the next stage will be kicked off once all
4532 * the percpu refcnts are confirmed to be killed.
4533 *
4534 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4535 * rest of destruction. Once all cgroup references are gone, the
4536 * cgroup is RCU-freed.
4537 *
4538 * This function implements s1. After this step, @cgrp is gone as far as
4539 * the userland is concerned and a new cgroup with the same name may be
4540 * created. As cgroup doesn't care about the names internally, this
4541 * doesn't cause any problem.
4542 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004543static int cgroup_destroy_locked(struct cgroup *cgrp)
4544 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004545{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004546 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004547 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004548 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004549
Tejun Heo42809dd2012-11-19 08:13:37 -08004550 lockdep_assert_held(&cgroup_mutex);
4551
Tejun Heoddd69142013-06-12 21:04:54 -07004552 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004553 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004554 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004555 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004556 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004557 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004558 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004559 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004560 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004561
Tejun Heo1a90dd52012-11-05 09:16:59 -08004562 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004563 * Make sure there's no live children. We can't test emptiness of
4564 * ->self.children as dead children linger on it while being
4565 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004566 */
Tejun Heof3d46502014-05-16 13:22:52 -04004567 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004568 return -EBUSY;
4569
4570 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004571 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004572 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004573 */
Tejun Heo184faf32014-05-16 13:22:51 -04004574 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004575
Tejun Heo249f3462014-05-14 09:15:01 -04004576 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004577 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004578 kill_css(css);
4579
Tejun Heo184faf32014-05-16 13:22:51 -04004580 /* CSS_ONLINE is clear, remove from ->release_list for the last time */
Tejun Heo455050d2013-06-13 19:27:41 -07004581 raw_spin_lock(&release_list_lock);
4582 if (!list_empty(&cgrp->release_list))
4583 list_del_init(&cgrp->release_list);
4584 raw_spin_unlock(&release_list_lock);
4585
4586 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004587 * Remove @cgrp directory along with the base files. @cgrp has an
4588 * extra ref on its kn.
4589 */
4590 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004591
Tejun Heod51f39b2014-05-16 13:22:48 -04004592 set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags);
4593 check_for_release(cgroup_parent(cgrp));
Tejun Heo9e4173e2014-05-14 09:15:01 -04004594
Tejun Heo249f3462014-05-14 09:15:01 -04004595 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004596 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo249f3462014-05-14 09:15:01 -04004597
Tejun Heoea15f8c2013-06-13 19:27:42 -07004598 return 0;
4599};
4600
Tejun Heo2bd59d42014-02-11 11:52:49 -05004601static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004602{
Tejun Heoa9746d82014-05-13 12:19:22 -04004603 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004604 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004605
Tejun Heoa9746d82014-05-13 12:19:22 -04004606 cgrp = cgroup_kn_lock_live(kn);
4607 if (!cgrp)
4608 return 0;
4609 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004610
Tejun Heoa9746d82014-05-13 12:19:22 -04004611 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004612
Tejun Heoa9746d82014-05-13 12:19:22 -04004613 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004614
4615 /*
4616 * There are two control paths which try to determine cgroup from
4617 * dentry without going through kernfs - cgroupstats_build() and
4618 * css_tryget_online_from_dir(). Those are supported by RCU
4619 * protecting clearing of cgrp->kn->priv backpointer, which should
4620 * happen after all files under it have been removed.
4621 */
4622 if (!ret)
4623 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4624
Tejun Heo2bd59d42014-02-11 11:52:49 -05004625 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004626 return ret;
4627}
4628
Tejun Heo2bd59d42014-02-11 11:52:49 -05004629static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4630 .remount_fs = cgroup_remount,
4631 .show_options = cgroup_show_options,
4632 .mkdir = cgroup_mkdir,
4633 .rmdir = cgroup_rmdir,
4634 .rename = cgroup_rename,
4635};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004636
Tejun Heo15a4c832014-05-04 15:09:14 -04004637static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004640
4641 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004642
Tejun Heo648bb562012-11-19 08:13:36 -08004643 mutex_lock(&cgroup_mutex);
4644
Tejun Heo15a4c832014-05-04 15:09:14 -04004645 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004646 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004647
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004648 /* Create the root cgroup state for this subsystem */
4649 ss->root = &cgrp_dfl_root;
4650 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004651 /* We don't handle early failures gracefully */
4652 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004653 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004654
4655 /*
4656 * Root csses are never destroyed and we can't initialize
4657 * percpu_ref during early init. Disable refcnting.
4658 */
4659 css->flags |= CSS_NO_REF;
4660
Tejun Heo15a4c832014-05-04 15:09:14 -04004661 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004662 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004663 css->id = 1;
4664 } else {
4665 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4666 BUG_ON(css->id < 0);
4667 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668
Li Zefane8d55fd2008-04-29 01:00:13 -07004669 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004670 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004671 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004672 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004673 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674
4675 need_forkexit_callback |= ss->fork || ss->exit;
4676
Li Zefane8d55fd2008-04-29 01:00:13 -07004677 /* At system boot, before all subsystems have been
4678 * registered, no tasks have been forked, so we don't
4679 * need to invoke fork callbacks here. */
4680 BUG_ON(!list_empty(&init_task.tasks));
4681
Tejun Heoae7f1642013-08-13 20:22:50 -04004682 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004683
Tejun Heo648bb562012-11-19 08:13:36 -08004684 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685}
4686
4687/**
Li Zefana043e3b2008-02-23 15:24:09 -08004688 * cgroup_init_early - cgroup initialization at system boot
4689 *
4690 * Initialize cgroups at system boot, and initialize any
4691 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004692 */
4693int __init cgroup_init_early(void)
4694{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004695 static struct cgroup_sb_opts __initdata opts =
4696 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004697 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004698 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004699
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004700 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004701 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4702
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004703 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004704
Tejun Heo3ed80a62014-02-08 10:36:58 -05004705 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004706 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004707 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4708 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004709 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004710 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4711 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004712
Tejun Heoaec25022014-02-08 10:36:58 -05004713 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004714 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004715
4716 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004717 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004718 }
4719 return 0;
4720}
4721
4722/**
Li Zefana043e3b2008-02-23 15:24:09 -08004723 * cgroup_init - cgroup initialization
4724 *
4725 * Register cgroup filesystem and /proc file, and initialize
4726 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727 */
4728int __init cgroup_init(void)
4729{
Tejun Heo30159ec2013-06-25 11:53:37 -07004730 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004731 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004732 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004733
Tejun Heo2bd59d42014-02-11 11:52:49 -05004734 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004735
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004736 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004737
Tejun Heo82fe9b02013-06-25 11:53:37 -07004738 /* Add init_css_set to the hash table */
4739 key = css_set_hash(init_css_set.subsys);
4740 hash_add(css_set_table, &init_css_set.hlist, key);
4741
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004742 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004743
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004744 mutex_unlock(&cgroup_mutex);
4745
Tejun Heo172a2c062014-03-19 10:23:53 -04004746 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004747 if (ss->early_init) {
4748 struct cgroup_subsys_state *css =
4749 init_css_set.subsys[ss->id];
4750
4751 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4752 GFP_KERNEL);
4753 BUG_ON(css->id < 0);
4754 } else {
4755 cgroup_init_subsys(ss, false);
4756 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004757
Tejun Heo2d8f2432014-04-23 11:13:15 -04004758 list_add_tail(&init_css_set.e_cset_node[ssid],
4759 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4760
Tejun Heo172a2c062014-03-19 10:23:53 -04004761 /*
Li Zefanc731ae12014-06-05 17:16:30 +08004762 * Setting dfl_root subsys_mask needs to consider the
4763 * disabled flag and cftype registration needs kmalloc,
4764 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04004765 */
Li Zefanc731ae12014-06-05 17:16:30 +08004766 if (!ss->disabled) {
4767 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo172a2c062014-03-19 10:23:53 -04004768 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08004769 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004770 }
Greg KH676db4a2010-08-05 13:53:35 -07004771
Paul Menageddbcc7e2007-10-18 23:39:30 -07004772 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004773 if (!cgroup_kobj)
4774 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004775
4776 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004777 if (err < 0) {
4778 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004779 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004780 }
4781
4782 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004783 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004784}
Paul Menageb4f48b62007-10-18 23:39:33 -07004785
Tejun Heoe5fca242013-11-22 17:14:39 -05004786static int __init cgroup_wq_init(void)
4787{
4788 /*
4789 * There isn't much point in executing destruction path in
4790 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004791 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004792 *
4793 * We would prefer to do this in cgroup_init() above, but that
4794 * is called before init_workqueues(): so leave this until after.
4795 */
Tejun Heo1a115332014-02-12 19:06:19 -05004796 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004797 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004798
4799 /*
4800 * Used to destroy pidlists and separate to serve as flush domain.
4801 * Cap @max_active to 1 too.
4802 */
4803 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4804 0, 1);
4805 BUG_ON(!cgroup_pidlist_destroy_wq);
4806
Tejun Heoe5fca242013-11-22 17:14:39 -05004807 return 0;
4808}
4809core_initcall(cgroup_wq_init);
4810
Paul Menagea4243162007-10-18 23:39:35 -07004811/*
4812 * proc_cgroup_show()
4813 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4814 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004815 */
4816
4817/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004818int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004819{
4820 struct pid *pid;
4821 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004822 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004823 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004824 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004825
4826 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004827 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004828 if (!buf)
4829 goto out;
4830
4831 retval = -ESRCH;
4832 pid = m->private;
4833 tsk = get_pid_task(pid, PIDTYPE_PID);
4834 if (!tsk)
4835 goto out_free;
4836
4837 retval = 0;
4838
4839 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004840 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004841
Tejun Heo985ed672014-03-19 10:23:53 -04004842 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004843 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004844 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004845 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004846
Tejun Heoa2dd4242014-03-19 10:23:55 -04004847 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004848 continue;
4849
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004850 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004851 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004852 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004853 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004854 if (strlen(root->name))
4855 seq_printf(m, "%sname=%s", count ? "," : "",
4856 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004857 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004858 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004859 path = cgroup_path(cgrp, buf, PATH_MAX);
4860 if (!path) {
4861 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004862 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004863 }
4864 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004865 seq_putc(m, '\n');
4866 }
4867
4868out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004869 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004870 mutex_unlock(&cgroup_mutex);
4871 put_task_struct(tsk);
4872out_free:
4873 kfree(buf);
4874out:
4875 return retval;
4876}
4877
Paul Menagea4243162007-10-18 23:39:35 -07004878/* Display information about each subsystem and each hierarchy */
4879static int proc_cgroupstats_show(struct seq_file *m, void *v)
4880{
Tejun Heo30159ec2013-06-25 11:53:37 -07004881 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004882 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004883
Paul Menage8bab8dd2008-04-04 14:29:57 -07004884 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004885 /*
4886 * ideally we don't want subsystems moving around while we do this.
4887 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4888 * subsys/hierarchy state.
4889 */
Paul Menagea4243162007-10-18 23:39:35 -07004890 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004891
4892 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004893 seq_printf(m, "%s\t%d\t%d\t%d\n",
4894 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004895 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004896
Paul Menagea4243162007-10-18 23:39:35 -07004897 mutex_unlock(&cgroup_mutex);
4898 return 0;
4899}
4900
4901static int cgroupstats_open(struct inode *inode, struct file *file)
4902{
Al Viro9dce07f2008-03-29 03:07:28 +00004903 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004904}
4905
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004906static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004907 .open = cgroupstats_open,
4908 .read = seq_read,
4909 .llseek = seq_lseek,
4910 .release = single_release,
4911};
4912
Paul Menageb4f48b62007-10-18 23:39:33 -07004913/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004914 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004915 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004916 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004917 * A task is associated with the init_css_set until cgroup_post_fork()
4918 * attaches it to the parent's css_set. Empty cg_list indicates that
4919 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004920 */
4921void cgroup_fork(struct task_struct *child)
4922{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004923 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004924 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004925}
4926
4927/**
Li Zefana043e3b2008-02-23 15:24:09 -08004928 * cgroup_post_fork - called on a new task after adding it to the task list
4929 * @child: the task in question
4930 *
Tejun Heo5edee612012-10-16 15:03:14 -07004931 * Adds the task to the list running through its css_set if necessary and
4932 * call the subsystem fork() callbacks. Has to be after the task is
4933 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004934 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004935 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004936 */
Paul Menage817929e2007-10-18 23:39:36 -07004937void cgroup_post_fork(struct task_struct *child)
4938{
Tejun Heo30159ec2013-06-25 11:53:37 -07004939 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004940 int i;
4941
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004942 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004943 * This may race against cgroup_enable_task_cg_links(). As that
4944 * function sets use_task_css_set_links before grabbing
4945 * tasklist_lock and we just went through tasklist_lock to add
4946 * @child, it's guaranteed that either we see the set
4947 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4948 * @child during its iteration.
4949 *
4950 * If we won the race, @child is associated with %current's
4951 * css_set. Grabbing css_set_rwsem guarantees both that the
4952 * association is stable, and, on completion of the parent's
4953 * migration, @child is visible in the source of migration or
4954 * already in the destination cgroup. This guarantee is necessary
4955 * when implementing operations which need to migrate all tasks of
4956 * a cgroup to another.
4957 *
4958 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4959 * will remain in init_css_set. This is safe because all tasks are
4960 * in the init_css_set before cg_links is enabled and there's no
4961 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004962 */
Paul Menage817929e2007-10-18 23:39:36 -07004963 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004964 struct css_set *cset;
4965
Tejun Heo96d365e2014-02-13 06:58:40 -05004966 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004967 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004968 if (list_empty(&child->cg_list)) {
4969 rcu_assign_pointer(child->cgroups, cset);
4970 list_add(&child->cg_list, &cset->tasks);
4971 get_css_set(cset);
4972 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004973 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004974 }
Tejun Heo5edee612012-10-16 15:03:14 -07004975
4976 /*
4977 * Call ss->fork(). This must happen after @child is linked on
4978 * css_set; otherwise, @child might change state between ->fork()
4979 * and addition to css_set.
4980 */
4981 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004982 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004983 if (ss->fork)
4984 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004985 }
Paul Menage817929e2007-10-18 23:39:36 -07004986}
Tejun Heo5edee612012-10-16 15:03:14 -07004987
Paul Menage817929e2007-10-18 23:39:36 -07004988/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004989 * cgroup_exit - detach cgroup from exiting task
4990 * @tsk: pointer to task_struct of exiting process
4991 *
4992 * Description: Detach cgroup from @tsk and release it.
4993 *
4994 * Note that cgroups marked notify_on_release force every task in
4995 * them to take the global cgroup_mutex mutex when exiting.
4996 * This could impact scaling on very large systems. Be reluctant to
4997 * use notify_on_release cgroups where very high task exit scaling
4998 * is required on large systems.
4999 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005000 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5001 * call cgroup_exit() while the task is still competent to handle
5002 * notify_on_release(), then leave the task attached to the root cgroup in
5003 * each hierarchy for the remainder of its exit. No need to bother with
5004 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005005 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005006 */
Li Zefan1ec41832014-03-28 15:22:19 +08005007void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005008{
Tejun Heo30159ec2013-06-25 11:53:37 -07005009 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005010 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005011 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005012 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005013
5014 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005015 * Unlink from @tsk from its css_set. As migration path can't race
5016 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005017 */
5018 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005019 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005020 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005021 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005022 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005023 }
5024
Paul Menageb4f48b62007-10-18 23:39:33 -07005025 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005026 cset = task_css_set(tsk);
5027 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005028
Li Zefan1ec41832014-03-28 15:22:19 +08005029 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005030 /* see cgroup_post_fork() for details */
5031 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005032 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005033 struct cgroup_subsys_state *old_css = cset->subsys[i];
5034 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005035
Tejun Heoeb954192013-08-08 20:11:23 -04005036 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005037 }
5038 }
5039 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005040
Tejun Heoeaf797a2014-02-25 10:04:03 -05005041 if (put_cset)
5042 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005043}
Paul Menage697f4162007-10-18 23:39:34 -07005044
Paul Menagebd89aab2007-10-18 23:40:44 -07005045static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046{
Tejun Heof3d46502014-05-16 13:22:52 -04005047 if (cgroup_is_releasable(cgrp) && list_empty(&cgrp->cset_links) &&
5048 !css_has_online_children(&cgrp->self)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005049 /*
5050 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005052 * it now
5053 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005054 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005055
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005056 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005057 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005058 list_empty(&cgrp->release_list)) {
5059 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 need_schedule_work = 1;
5061 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005062 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005063 if (need_schedule_work)
5064 schedule_work(&release_agent_work);
5065 }
5066}
5067
Paul Menage81a6a5c2007-10-18 23:39:38 -07005068/*
5069 * Notify userspace when a cgroup is released, by running the
5070 * configured release agent with the name of the cgroup (path
5071 * relative to the root of cgroup file system) as the argument.
5072 *
5073 * Most likely, this user command will try to rmdir this cgroup.
5074 *
5075 * This races with the possibility that some other task will be
5076 * attached to this cgroup before it is removed, or that some other
5077 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5078 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5079 * unused, and this cgroup will be reprieved from its death sentence,
5080 * to continue to serve a useful existence. Next time it's released,
5081 * we will get notified again, if it still has 'notify_on_release' set.
5082 *
5083 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5084 * means only wait until the task is successfully execve()'d. The
5085 * separate release agent task is forked by call_usermodehelper(),
5086 * then control in this thread returns here, without waiting for the
5087 * release agent task. We don't bother to wait because the caller of
5088 * this routine has no use for the exit status of the release agent
5089 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005090 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005091static void cgroup_release_agent(struct work_struct *work)
5092{
5093 BUG_ON(work != &release_agent_work);
5094 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005095 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005096 while (!list_empty(&release_list)) {
5097 char *argv[3], *envp[3];
5098 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005099 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005100 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005101 struct cgroup,
5102 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005103 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005104 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005105 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005106 if (!pathbuf)
5107 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005108 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5109 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005110 goto continue_free;
5111 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5112 if (!agentbuf)
5113 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005114
5115 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005116 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005117 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005118 argv[i] = NULL;
5119
5120 i = 0;
5121 /* minimal command environment */
5122 envp[i++] = "HOME=/";
5123 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5124 envp[i] = NULL;
5125
5126 /* Drop the lock while we invoke the usermode helper,
5127 * since the exec could involve hitting disk and hence
5128 * be a slow process */
5129 mutex_unlock(&cgroup_mutex);
5130 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005132 continue_free:
5133 kfree(pathbuf);
5134 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005135 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005136 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005137 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005138 mutex_unlock(&cgroup_mutex);
5139}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005140
5141static int __init cgroup_disable(char *str)
5142{
Tejun Heo30159ec2013-06-25 11:53:37 -07005143 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005144 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005145 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005146
5147 while ((token = strsep(&str, ",")) != NULL) {
5148 if (!*token)
5149 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005150
Tejun Heo3ed80a62014-02-08 10:36:58 -05005151 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005152 if (!strcmp(token, ss->name)) {
5153 ss->disabled = 1;
5154 printk(KERN_INFO "Disabling %s control group"
5155 " subsystem\n", ss->name);
5156 break;
5157 }
5158 }
5159 }
5160 return 1;
5161}
5162__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005163
Tejun Heob77d7b62013-08-13 11:01:54 -04005164/**
Tejun Heoec903c02014-05-13 12:11:01 -04005165 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005166 * @dentry: directory dentry of interest
5167 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005168 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005169 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5170 * to get the corresponding css and return it. If such css doesn't exist
5171 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005172 */
Tejun Heoec903c02014-05-13 12:11:01 -04005173struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5174 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005175{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005176 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5177 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005178 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005179
Tejun Heo35cf0832013-08-26 18:40:56 -04005180 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005181 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5182 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005183 return ERR_PTR(-EBADF);
5184
Tejun Heo5a17f542014-02-11 11:52:47 -05005185 rcu_read_lock();
5186
Tejun Heo2bd59d42014-02-11 11:52:49 -05005187 /*
5188 * This path doesn't originate from kernfs and @kn could already
5189 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005190 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005191 */
5192 cgrp = rcu_dereference(kn->priv);
5193 if (cgrp)
5194 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005195
Tejun Heoec903c02014-05-13 12:11:01 -04005196 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005197 css = ERR_PTR(-ENOENT);
5198
5199 rcu_read_unlock();
5200 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005201}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005202
Li Zefan1cb650b2013-08-19 10:05:24 +08005203/**
5204 * css_from_id - lookup css by id
5205 * @id: the cgroup id
5206 * @ss: cgroup subsys to be looked into
5207 *
5208 * Returns the css if there's valid one with @id, otherwise returns NULL.
5209 * Should be called under rcu_read_lock().
5210 */
5211struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5212{
Tejun Heo6fa49182014-05-04 15:09:13 -04005213 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005214 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005215}
5216
Paul Menagefe693432009-09-23 15:56:20 -07005217#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005218static struct cgroup_subsys_state *
5219debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005220{
5221 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5222
5223 if (!css)
5224 return ERR_PTR(-ENOMEM);
5225
5226 return css;
5227}
5228
Tejun Heoeb954192013-08-08 20:11:23 -04005229static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005230{
Tejun Heoeb954192013-08-08 20:11:23 -04005231 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005232}
5233
Tejun Heo182446d2013-08-08 20:11:24 -04005234static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5235 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005236{
Tejun Heo182446d2013-08-08 20:11:24 -04005237 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005238}
5239
Tejun Heo182446d2013-08-08 20:11:24 -04005240static u64 current_css_set_read(struct cgroup_subsys_state *css,
5241 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005242{
5243 return (u64)(unsigned long)current->cgroups;
5244}
5245
Tejun Heo182446d2013-08-08 20:11:24 -04005246static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005247 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005248{
5249 u64 count;
5250
5251 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005252 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005253 rcu_read_unlock();
5254 return count;
5255}
5256
Tejun Heo2da8ca82013-12-05 12:28:04 -05005257static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005258{
Tejun Heo69d02062013-06-12 21:04:50 -07005259 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005260 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005261 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005262
Tejun Heoe61734c2014-02-12 09:29:50 -05005263 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5264 if (!name_buf)
5265 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005266
Tejun Heo96d365e2014-02-13 06:58:40 -05005267 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005268 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005269 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005270 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005271 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005272
Tejun Heoa2dd4242014-03-19 10:23:55 -04005273 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005274 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005275 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005276 }
5277 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005278 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005279 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005280 return 0;
5281}
5282
5283#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005284static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005285{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005286 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005287 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005288
Tejun Heo96d365e2014-02-13 06:58:40 -05005289 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005290 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005291 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005292 struct task_struct *task;
5293 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005294
Tejun Heo5abb8852013-06-12 21:04:49 -07005295 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005296
Tejun Heo5abb8852013-06-12 21:04:49 -07005297 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005298 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5299 goto overflow;
5300 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005301 }
Tejun Heoc7561122014-02-25 10:04:01 -05005302
5303 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5304 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5305 goto overflow;
5306 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5307 }
5308 continue;
5309 overflow:
5310 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005311 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005312 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005313 return 0;
5314}
5315
Tejun Heo182446d2013-08-08 20:11:24 -04005316static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005317{
Tejun Heo182446d2013-08-08 20:11:24 -04005318 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005319}
5320
5321static struct cftype debug_files[] = {
5322 {
Paul Menagefe693432009-09-23 15:56:20 -07005323 .name = "taskcount",
5324 .read_u64 = debug_taskcount_read,
5325 },
5326
5327 {
5328 .name = "current_css_set",
5329 .read_u64 = current_css_set_read,
5330 },
5331
5332 {
5333 .name = "current_css_set_refcount",
5334 .read_u64 = current_css_set_refcount_read,
5335 },
5336
5337 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005338 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005339 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005340 },
5341
5342 {
5343 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005344 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005345 },
5346
5347 {
Paul Menagefe693432009-09-23 15:56:20 -07005348 .name = "releasable",
5349 .read_u64 = releasable_read,
5350 },
Paul Menagefe693432009-09-23 15:56:20 -07005351
Tejun Heo4baf6e32012-04-01 12:09:55 -07005352 { } /* terminate */
5353};
Paul Menagefe693432009-09-23 15:56:20 -07005354
Tejun Heo073219e2014-02-08 10:36:58 -05005355struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005356 .css_alloc = debug_css_alloc,
5357 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005358 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005359};
5360#endif /* CONFIG_CGROUP_DEBUG */