blob: a5f75ac4e793ac84eb5069473684c5c0edcd3699 [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.
1783 */
Tejun Heof3d46502014-05-16 13:22:52 -04001784 if (css_has_online_children(&root->cgrp.self))
Tejun Heo9d755d32014-05-14 09:15:02 -04001785 cgroup_put(&root->cgrp);
1786 else
1787 percpu_ref_kill(&root->cgrp.self.refcnt);
1788
Tejun Heo2bd59d42014-02-11 11:52:49 -05001789 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790}
1791
1792static struct file_system_type cgroup_fs_type = {
1793 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001794 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 .kill_sb = cgroup_kill_sb,
1796};
1797
Greg KH676db4a2010-08-05 13:53:35 -07001798static struct kobject *cgroup_kobj;
1799
Li Zefana043e3b2008-02-23 15:24:09 -08001800/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001801 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001802 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001803 * @buf: the buffer to write the path into
1804 * @buflen: the length of the buffer
1805 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001806 * Determine @task's cgroup on the first (the one with the lowest non-zero
1807 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1808 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1809 * cgroup controller callbacks.
1810 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001811 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001812 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001813char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001814{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001815 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001816 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001817 int hierarchy_id = 1;
1818 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001819
1820 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001821 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001822
Tejun Heo913ffdb2013-07-11 16:34:48 -07001823 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1824
Tejun Heo857a2be2013-04-14 20:50:08 -07001825 if (root) {
1826 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001827 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001828 } else {
1829 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001830 if (strlcpy(buf, "/", buflen) < buflen)
1831 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001832 }
1833
Tejun Heo96d365e2014-02-13 06:58:40 -05001834 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001835 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001836 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001837}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001838EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001839
Tejun Heob3dc0942014-02-25 10:04:01 -05001840/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001841struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001842 /* the src and dst cset list running through cset->mg_node */
1843 struct list_head src_csets;
1844 struct list_head dst_csets;
1845
1846 /*
1847 * Fields for cgroup_taskset_*() iteration.
1848 *
1849 * Before migration is committed, the target migration tasks are on
1850 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1851 * the csets on ->dst_csets. ->csets point to either ->src_csets
1852 * or ->dst_csets depending on whether migration is committed.
1853 *
1854 * ->cur_csets and ->cur_task point to the current task position
1855 * during iteration.
1856 */
1857 struct list_head *csets;
1858 struct css_set *cur_cset;
1859 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001860};
1861
1862/**
1863 * cgroup_taskset_first - reset taskset and return the first task
1864 * @tset: taskset of interest
1865 *
1866 * @tset iteration is initialized and the first task is returned.
1867 */
1868struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1869{
Tejun Heob3dc0942014-02-25 10:04:01 -05001870 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1871 tset->cur_task = NULL;
1872
1873 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001874}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875
1876/**
1877 * cgroup_taskset_next - iterate to the next task in taskset
1878 * @tset: taskset of interest
1879 *
1880 * Return the next task in @tset. Iteration must have been initialized
1881 * with cgroup_taskset_first().
1882 */
1883struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1884{
Tejun Heob3dc0942014-02-25 10:04:01 -05001885 struct css_set *cset = tset->cur_cset;
1886 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001887
Tejun Heob3dc0942014-02-25 10:04:01 -05001888 while (&cset->mg_node != tset->csets) {
1889 if (!task)
1890 task = list_first_entry(&cset->mg_tasks,
1891 struct task_struct, cg_list);
1892 else
1893 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001894
Tejun Heob3dc0942014-02-25 10:04:01 -05001895 if (&task->cg_list != &cset->mg_tasks) {
1896 tset->cur_cset = cset;
1897 tset->cur_task = task;
1898 return task;
1899 }
1900
1901 cset = list_next_entry(cset, mg_node);
1902 task = NULL;
1903 }
1904
1905 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001906}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001907
1908/**
Ben Blum74a11662011-05-26 16:25:20 -07001909 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001910 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001911 * @tsk: the task being migrated
1912 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001913 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001914 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001915 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001916static void cgroup_task_migrate(struct cgroup *old_cgrp,
1917 struct task_struct *tsk,
1918 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001919{
Tejun Heo5abb8852013-06-12 21:04:49 -07001920 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001921
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001922 lockdep_assert_held(&cgroup_mutex);
1923 lockdep_assert_held(&css_set_rwsem);
1924
Ben Blum74a11662011-05-26 16:25:20 -07001925 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001926 * We are synchronized through threadgroup_lock() against PF_EXITING
1927 * setting such that we can't race against cgroup_exit() changing the
1928 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001929 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001930 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001931 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001932
Tejun Heob3dc0942014-02-25 10:04:01 -05001933 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001934 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001935
Tejun Heo1b9aba42014-03-19 17:43:21 -04001936 /*
1937 * Use move_tail so that cgroup_taskset_first() still returns the
1938 * leader after migration. This works because cgroup_migrate()
1939 * ensures that the dst_cset of the leader is the first on the
1940 * tset's dst_csets list.
1941 */
1942 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001943
1944 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001945 * We just gained a reference on old_cset by taking it from the
1946 * task. As trading it for new_cset is protected by cgroup_mutex,
1947 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001948 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001949 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001950 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001951}
1952
Li Zefana043e3b2008-02-23 15:24:09 -08001953/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001954 * cgroup_migrate_finish - cleanup after attach
1955 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001956 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001957 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1958 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001959 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001960static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001961{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001962 struct css_set *cset, *tmp_cset;
1963
1964 lockdep_assert_held(&cgroup_mutex);
1965
1966 down_write(&css_set_rwsem);
1967 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1968 cset->mg_src_cgrp = NULL;
1969 cset->mg_dst_cset = NULL;
1970 list_del_init(&cset->mg_preload_node);
1971 put_css_set_locked(cset, false);
1972 }
1973 up_write(&css_set_rwsem);
1974}
1975
1976/**
1977 * cgroup_migrate_add_src - add a migration source css_set
1978 * @src_cset: the source css_set to add
1979 * @dst_cgrp: the destination cgroup
1980 * @preloaded_csets: list of preloaded css_sets
1981 *
1982 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1983 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1984 * up by cgroup_migrate_finish().
1985 *
1986 * This function may be called without holding threadgroup_lock even if the
1987 * target is a process. Threads may be created and destroyed but as long
1988 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1989 * the preloaded css_sets are guaranteed to cover all migrations.
1990 */
1991static void cgroup_migrate_add_src(struct css_set *src_cset,
1992 struct cgroup *dst_cgrp,
1993 struct list_head *preloaded_csets)
1994{
1995 struct cgroup *src_cgrp;
1996
1997 lockdep_assert_held(&cgroup_mutex);
1998 lockdep_assert_held(&css_set_rwsem);
1999
2000 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2001
Tejun Heo1958d2d2014-02-25 10:04:03 -05002002 if (!list_empty(&src_cset->mg_preload_node))
2003 return;
2004
2005 WARN_ON(src_cset->mg_src_cgrp);
2006 WARN_ON(!list_empty(&src_cset->mg_tasks));
2007 WARN_ON(!list_empty(&src_cset->mg_node));
2008
2009 src_cset->mg_src_cgrp = src_cgrp;
2010 get_css_set(src_cset);
2011 list_add(&src_cset->mg_preload_node, preloaded_csets);
2012}
2013
2014/**
2015 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002016 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002017 * @preloaded_csets: list of preloaded source css_sets
2018 *
2019 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2020 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002021 * pins all destination css_sets, links each to its source, and append them
2022 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2023 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002024 *
2025 * This function must be called after cgroup_migrate_add_src() has been
2026 * called on each migration source css_set. After migration is performed
2027 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2028 * @preloaded_csets.
2029 */
2030static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2031 struct list_head *preloaded_csets)
2032{
2033 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002034 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002035
2036 lockdep_assert_held(&cgroup_mutex);
2037
Tejun Heof8f22e52014-04-23 11:13:16 -04002038 /*
2039 * Except for the root, child_subsys_mask must be zero for a cgroup
2040 * with tasks so that child cgroups don't compete against tasks.
2041 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002042 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002043 dst_cgrp->child_subsys_mask)
2044 return -EBUSY;
2045
Tejun Heo1958d2d2014-02-25 10:04:03 -05002046 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002047 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002048 struct css_set *dst_cset;
2049
Tejun Heof817de92014-04-23 11:13:16 -04002050 dst_cset = find_css_set(src_cset,
2051 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002052 if (!dst_cset)
2053 goto err;
2054
2055 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002056
2057 /*
2058 * If src cset equals dst, it's noop. Drop the src.
2059 * cgroup_migrate() will skip the cset too. Note that we
2060 * can't handle src == dst as some nodes are used by both.
2061 */
2062 if (src_cset == dst_cset) {
2063 src_cset->mg_src_cgrp = NULL;
2064 list_del_init(&src_cset->mg_preload_node);
2065 put_css_set(src_cset, false);
2066 put_css_set(dst_cset, false);
2067 continue;
2068 }
2069
Tejun Heo1958d2d2014-02-25 10:04:03 -05002070 src_cset->mg_dst_cset = dst_cset;
2071
2072 if (list_empty(&dst_cset->mg_preload_node))
2073 list_add(&dst_cset->mg_preload_node, &csets);
2074 else
2075 put_css_set(dst_cset, false);
2076 }
2077
Tejun Heof817de92014-04-23 11:13:16 -04002078 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002079 return 0;
2080err:
2081 cgroup_migrate_finish(&csets);
2082 return -ENOMEM;
2083}
2084
2085/**
2086 * cgroup_migrate - migrate a process or task to a cgroup
2087 * @cgrp: the destination cgroup
2088 * @leader: the leader of the process or the task to migrate
2089 * @threadgroup: whether @leader points to the whole process or a single task
2090 *
2091 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2092 * process, the caller must be holding threadgroup_lock of @leader. The
2093 * caller is also responsible for invoking cgroup_migrate_add_src() and
2094 * cgroup_migrate_prepare_dst() on the targets before invoking this
2095 * function and following up with cgroup_migrate_finish().
2096 *
2097 * As long as a controller's ->can_attach() doesn't fail, this function is
2098 * guaranteed to succeed. This means that, excluding ->can_attach()
2099 * failure, when migrating multiple targets, the success or failure can be
2100 * decided for all targets by invoking group_migrate_prepare_dst() before
2101 * actually starting migrating.
2102 */
2103static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2104 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002105{
Tejun Heob3dc0942014-02-25 10:04:01 -05002106 struct cgroup_taskset tset = {
2107 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2108 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2109 .csets = &tset.src_csets,
2110 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002111 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002112 struct css_set *cset, *tmp_cset;
2113 struct task_struct *task, *tmp_task;
2114 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002115
2116 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002117 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2118 * already PF_EXITING could be freed from underneath us unless we
2119 * take an rcu_read_lock.
2120 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002121 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002122 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002123 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002124 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002125 /* @task either already exited or can't exit until the end */
2126 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002127 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002128
Tejun Heoeaf797a2014-02-25 10:04:03 -05002129 /* leave @task alone if post_fork() hasn't linked it yet */
2130 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002131 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002132
Tejun Heob3dc0942014-02-25 10:04:01 -05002133 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002134 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002135 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002136
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002137 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002138 * cgroup_taskset_first() must always return the leader.
2139 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002140 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002141 list_move_tail(&task->cg_list, &cset->mg_tasks);
2142 if (list_empty(&cset->mg_node))
2143 list_add_tail(&cset->mg_node, &tset.src_csets);
2144 if (list_empty(&cset->mg_dst_cset->mg_node))
2145 list_move_tail(&cset->mg_dst_cset->mg_node,
2146 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002147 next:
Li Zefan081aa452013-03-13 09:17:09 +08002148 if (!threadgroup)
2149 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002150 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002151 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002152 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002153
Tejun Heo134d3372011-12-12 18:12:21 -08002154 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002155 if (list_empty(&tset.src_csets))
2156 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002157
Tejun Heo1958d2d2014-02-25 10:04:03 -05002158 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002159 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002160 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002161 ret = css->ss->can_attach(css, &tset);
2162 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002163 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002164 goto out_cancel_attach;
2165 }
2166 }
Ben Blum74a11662011-05-26 16:25:20 -07002167 }
2168
2169 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002170 * Now that we're guaranteed success, proceed to move all tasks to
2171 * the new cgroup. There are no failure cases after here, so this
2172 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002173 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002174 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002175 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2176 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2177 cgroup_task_migrate(cset->mg_src_cgrp, task,
2178 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002179 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002180 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002181
2182 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002183 * Migration is committed, all target tasks are now on dst_csets.
2184 * Nothing is sensitive to fork() after this point. Notify
2185 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002186 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002187 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002188
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002189 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002190 if (css->ss->attach)
2191 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002192
Tejun Heo9db8de32014-02-13 06:58:43 -05002193 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002194 goto out_release_tset;
2195
Ben Blum74a11662011-05-26 16:25:20 -07002196out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002197 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002198 if (css == failed_css)
2199 break;
2200 if (css->ss->cancel_attach)
2201 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002202 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002203out_release_tset:
2204 down_write(&css_set_rwsem);
2205 list_splice_init(&tset.dst_csets, &tset.src_csets);
2206 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002207 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002208 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002209 }
2210 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002211 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002212}
2213
Tejun Heo1958d2d2014-02-25 10:04:03 -05002214/**
2215 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2216 * @dst_cgrp: the cgroup to attach to
2217 * @leader: the task or the leader of the threadgroup to be attached
2218 * @threadgroup: attach the whole threadgroup?
2219 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002220 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002221 */
2222static int cgroup_attach_task(struct cgroup *dst_cgrp,
2223 struct task_struct *leader, bool threadgroup)
2224{
2225 LIST_HEAD(preloaded_csets);
2226 struct task_struct *task;
2227 int ret;
2228
2229 /* look up all src csets */
2230 down_read(&css_set_rwsem);
2231 rcu_read_lock();
2232 task = leader;
2233 do {
2234 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2235 &preloaded_csets);
2236 if (!threadgroup)
2237 break;
2238 } while_each_thread(leader, task);
2239 rcu_read_unlock();
2240 up_read(&css_set_rwsem);
2241
2242 /* prepare dst csets and commit */
2243 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2244 if (!ret)
2245 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2246
2247 cgroup_migrate_finish(&preloaded_csets);
2248 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002249}
2250
2251/*
2252 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002253 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002254 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002255 */
Tejun Heoacbef752014-05-13 12:16:22 -04002256static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2257 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002258{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002259 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002260 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002261 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002262 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002263 int ret;
2264
Tejun Heoacbef752014-05-13 12:16:22 -04002265 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2266 return -EINVAL;
2267
Tejun Heoe76ecae2014-05-13 12:19:23 -04002268 cgrp = cgroup_kn_lock_live(of->kn);
2269 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002270 return -ENODEV;
2271
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002272retry_find_task:
2273 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002274 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002275 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002276 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002277 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002278 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002279 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002280 }
Ben Blum74a11662011-05-26 16:25:20 -07002281 /*
2282 * even if we're attaching all tasks in the thread group, we
2283 * only need to check permissions on one of them.
2284 */
David Howellsc69e8d92008-11-14 10:39:19 +11002285 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002286 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2287 !uid_eq(cred->euid, tcred->uid) &&
2288 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002289 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002290 ret = -EACCES;
2291 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002292 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002293 } else
2294 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002295
2296 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002297 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002298
2299 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002300 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002301 * trapped in a cpuset, or RT worker may be born in a cgroup
2302 * with no rt_runtime allocated. Just say no.
2303 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002304 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002305 ret = -EINVAL;
2306 rcu_read_unlock();
2307 goto out_unlock_cgroup;
2308 }
2309
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002310 get_task_struct(tsk);
2311 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002312
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002313 threadgroup_lock(tsk);
2314 if (threadgroup) {
2315 if (!thread_group_leader(tsk)) {
2316 /*
2317 * a race with de_thread from another thread's exec()
2318 * may strip us of our leadership, if this happens,
2319 * there is no choice but to throw this task away and
2320 * try again; this is
2321 * "double-double-toil-and-trouble-check locking".
2322 */
2323 threadgroup_unlock(tsk);
2324 put_task_struct(tsk);
2325 goto retry_find_task;
2326 }
Li Zefan081aa452013-03-13 09:17:09 +08002327 }
2328
2329 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2330
Tejun Heocd3d0952011-12-12 18:12:21 -08002331 threadgroup_unlock(tsk);
2332
Paul Menagebbcb81d2007-10-18 23:39:32 -07002333 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002334out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002335 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002336 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002337}
2338
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002339/**
2340 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2341 * @from: attach to all cgroups of a given task
2342 * @tsk: the task to be attached
2343 */
2344int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2345{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002346 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002347 int retval = 0;
2348
Tejun Heo47cfcd02013-04-07 09:29:51 -07002349 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002350 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002351 struct cgroup *from_cgrp;
2352
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002353 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002354 continue;
2355
Tejun Heo96d365e2014-02-13 06:58:40 -05002356 down_read(&css_set_rwsem);
2357 from_cgrp = task_cgroup_from_root(from, root);
2358 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002359
Li Zefan6f4b7e62013-07-31 16:18:36 +08002360 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002361 if (retval)
2362 break;
2363 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002364 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002365
2366 return retval;
2367}
2368EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2369
Tejun Heoacbef752014-05-13 12:16:22 -04002370static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2371 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002372{
Tejun Heoacbef752014-05-13 12:16:22 -04002373 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002374}
2375
Tejun Heoacbef752014-05-13 12:16:22 -04002376static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2377 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002378{
Tejun Heoacbef752014-05-13 12:16:22 -04002379 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002380}
2381
Tejun Heo451af502014-05-13 12:16:21 -04002382static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2383 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002384{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002385 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002386
Tejun Heoe76ecae2014-05-13 12:19:23 -04002387 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2388
2389 cgrp = cgroup_kn_lock_live(of->kn);
2390 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002391 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002392 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002393 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2394 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002395 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002396 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002397 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002398}
2399
Tejun Heo2da8ca82013-12-05 12:28:04 -05002400static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002401{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002402 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002403
Tejun Heo46cfeb02014-05-13 12:11:00 -04002404 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002405 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002406 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002407 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002408 return 0;
2409}
2410
Tejun Heo2da8ca82013-12-05 12:28:04 -05002411static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002412{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002413 struct cgroup *cgrp = seq_css(seq)->cgroup;
2414
2415 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002416 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417}
2418
Tejun Heof8f22e52014-04-23 11:13:16 -04002419static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2420{
2421 struct cgroup_subsys *ss;
2422 bool printed = false;
2423 int ssid;
2424
2425 for_each_subsys(ss, ssid) {
2426 if (ss_mask & (1 << ssid)) {
2427 if (printed)
2428 seq_putc(seq, ' ');
2429 seq_printf(seq, "%s", ss->name);
2430 printed = true;
2431 }
2432 }
2433 if (printed)
2434 seq_putc(seq, '\n');
2435}
2436
2437/* show controllers which are currently attached to the default hierarchy */
2438static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2439{
2440 struct cgroup *cgrp = seq_css(seq)->cgroup;
2441
Tejun Heo5533e012014-05-14 19:33:07 -04002442 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2443 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002444 return 0;
2445}
2446
2447/* show controllers which are enabled from the parent */
2448static int cgroup_controllers_show(struct seq_file *seq, void *v)
2449{
2450 struct cgroup *cgrp = seq_css(seq)->cgroup;
2451
Tejun Heod51f39b2014-05-16 13:22:48 -04002452 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->child_subsys_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002453 return 0;
2454}
2455
2456/* show controllers which are enabled for a given cgroup's children */
2457static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2458{
2459 struct cgroup *cgrp = seq_css(seq)->cgroup;
2460
2461 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2462 return 0;
2463}
2464
2465/**
2466 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2467 * @cgrp: root of the subtree to update csses for
2468 *
2469 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2470 * css associations need to be updated accordingly. This function looks up
2471 * all css_sets which are attached to the subtree, creates the matching
2472 * updated css_sets and migrates the tasks to the new ones.
2473 */
2474static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2475{
2476 LIST_HEAD(preloaded_csets);
2477 struct cgroup_subsys_state *css;
2478 struct css_set *src_cset;
2479 int ret;
2480
Tejun Heof8f22e52014-04-23 11:13:16 -04002481 lockdep_assert_held(&cgroup_mutex);
2482
2483 /* look up all csses currently attached to @cgrp's subtree */
2484 down_read(&css_set_rwsem);
2485 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2486 struct cgrp_cset_link *link;
2487
2488 /* self is not affected by child_subsys_mask change */
2489 if (css->cgroup == cgrp)
2490 continue;
2491
2492 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2493 cgroup_migrate_add_src(link->cset, cgrp,
2494 &preloaded_csets);
2495 }
2496 up_read(&css_set_rwsem);
2497
2498 /* NULL dst indicates self on default hierarchy */
2499 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2500 if (ret)
2501 goto out_finish;
2502
2503 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2504 struct task_struct *last_task = NULL, *task;
2505
2506 /* src_csets precede dst_csets, break on the first dst_cset */
2507 if (!src_cset->mg_src_cgrp)
2508 break;
2509
2510 /*
2511 * All tasks in src_cset need to be migrated to the
2512 * matching dst_cset. Empty it process by process. We
2513 * walk tasks but migrate processes. The leader might even
2514 * belong to a different cset but such src_cset would also
2515 * be among the target src_csets because the default
2516 * hierarchy enforces per-process membership.
2517 */
2518 while (true) {
2519 down_read(&css_set_rwsem);
2520 task = list_first_entry_or_null(&src_cset->tasks,
2521 struct task_struct, cg_list);
2522 if (task) {
2523 task = task->group_leader;
2524 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2525 get_task_struct(task);
2526 }
2527 up_read(&css_set_rwsem);
2528
2529 if (!task)
2530 break;
2531
2532 /* guard against possible infinite loop */
2533 if (WARN(last_task == task,
2534 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2535 goto out_finish;
2536 last_task = task;
2537
2538 threadgroup_lock(task);
2539 /* raced against de_thread() from another thread? */
2540 if (!thread_group_leader(task)) {
2541 threadgroup_unlock(task);
2542 put_task_struct(task);
2543 continue;
2544 }
2545
2546 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2547
2548 threadgroup_unlock(task);
2549 put_task_struct(task);
2550
2551 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2552 goto out_finish;
2553 }
2554 }
2555
2556out_finish:
2557 cgroup_migrate_finish(&preloaded_csets);
2558 return ret;
2559}
2560
2561/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002562static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2563 char *buf, size_t nbytes,
2564 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002565{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002566 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002567 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002568 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002569 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002570 int ssid, ret;
2571
2572 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002573 * Parse input - space separated list of subsystem names prefixed
2574 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002575 */
Tejun Heo451af502014-05-13 12:16:21 -04002576 buf = strstrip(buf);
2577 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002578 if (tok[0] == '\0')
2579 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002580 for_each_subsys(ss, ssid) {
Tejun Heo5533e012014-05-14 19:33:07 -04002581 if (ss->disabled || strcmp(tok + 1, ss->name) ||
2582 ((1 << ss->id) & cgrp_dfl_root_inhibit_ss_mask))
Tejun Heof8f22e52014-04-23 11:13:16 -04002583 continue;
2584
2585 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002586 enable |= 1 << ssid;
2587 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002588 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002589 disable |= 1 << ssid;
2590 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002591 } else {
2592 return -EINVAL;
2593 }
2594 break;
2595 }
2596 if (ssid == CGROUP_SUBSYS_COUNT)
2597 return -EINVAL;
2598 }
2599
Tejun Heoa9746d82014-05-13 12:19:22 -04002600 cgrp = cgroup_kn_lock_live(of->kn);
2601 if (!cgrp)
2602 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002603
2604 for_each_subsys(ss, ssid) {
2605 if (enable & (1 << ssid)) {
2606 if (cgrp->child_subsys_mask & (1 << ssid)) {
2607 enable &= ~(1 << ssid);
2608 continue;
2609 }
2610
2611 /*
2612 * Because css offlining is asynchronous, userland
2613 * might try to re-enable the same controller while
2614 * the previous instance is still around. In such
2615 * cases, wait till it's gone using offline_waitq.
2616 */
2617 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002618 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002619
2620 if (!cgroup_css(child, ss))
2621 continue;
2622
Tejun Heo0cee8b72014-05-13 12:10:59 -04002623 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002624 prepare_to_wait(&child->offline_waitq, &wait,
2625 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002626 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002627 schedule();
2628 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002629 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002630
Tejun Heoa9746d82014-05-13 12:19:22 -04002631 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002632 }
2633
2634 /* unavailable or not enabled on the parent? */
2635 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
Tejun Heod51f39b2014-05-16 13:22:48 -04002636 (cgroup_parent(cgrp) &&
2637 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ssid)))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002638 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002639 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002640 }
2641 } else if (disable & (1 << ssid)) {
2642 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2643 disable &= ~(1 << ssid);
2644 continue;
2645 }
2646
2647 /* a child has it enabled? */
2648 cgroup_for_each_live_child(child, cgrp) {
2649 if (child->child_subsys_mask & (1 << ssid)) {
2650 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002651 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002652 }
2653 }
2654 }
2655 }
2656
2657 if (!enable && !disable) {
2658 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002659 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002660 }
2661
2662 /*
2663 * Except for the root, child_subsys_mask must be zero for a cgroup
2664 * with tasks so that child cgroups don't compete against tasks.
2665 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002666 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002667 ret = -EBUSY;
2668 goto out_unlock;
2669 }
2670
2671 /*
2672 * Create csses for enables and update child_subsys_mask. This
2673 * changes cgroup_e_css() results which in turn makes the
2674 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2675 * subtree to the updated csses.
2676 */
2677 for_each_subsys(ss, ssid) {
2678 if (!(enable & (1 << ssid)))
2679 continue;
2680
2681 cgroup_for_each_live_child(child, cgrp) {
2682 ret = create_css(child, ss);
2683 if (ret)
2684 goto err_undo_css;
2685 }
2686 }
2687
2688 cgrp->child_subsys_mask |= enable;
2689 cgrp->child_subsys_mask &= ~disable;
2690
2691 ret = cgroup_update_dfl_csses(cgrp);
2692 if (ret)
2693 goto err_undo_css;
2694
2695 /* all tasks are now migrated away from the old csses, kill them */
2696 for_each_subsys(ss, ssid) {
2697 if (!(disable & (1 << ssid)))
2698 continue;
2699
2700 cgroup_for_each_live_child(child, cgrp)
2701 kill_css(cgroup_css(child, ss));
2702 }
2703
2704 kernfs_activate(cgrp->kn);
2705 ret = 0;
2706out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002707 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002708 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002709
2710err_undo_css:
2711 cgrp->child_subsys_mask &= ~enable;
2712 cgrp->child_subsys_mask |= disable;
2713
2714 for_each_subsys(ss, ssid) {
2715 if (!(enable & (1 << ssid)))
2716 continue;
2717
2718 cgroup_for_each_live_child(child, cgrp) {
2719 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2720 if (css)
2721 kill_css(css);
2722 }
2723 }
2724 goto out_unlock;
2725}
2726
Tejun Heo842b5972014-04-25 18:28:02 -04002727static int cgroup_populated_show(struct seq_file *seq, void *v)
2728{
2729 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2730 return 0;
2731}
2732
Tejun Heo2bd59d42014-02-11 11:52:49 -05002733static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2734 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002736 struct cgroup *cgrp = of->kn->parent->priv;
2737 struct cftype *cft = of->kn->priv;
2738 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002739 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740
Tejun Heob4168642014-05-13 12:16:21 -04002741 if (cft->write)
2742 return cft->write(of, buf, nbytes, off);
2743
Tejun Heo2bd59d42014-02-11 11:52:49 -05002744 /*
2745 * kernfs guarantees that a file isn't deleted with operations in
2746 * flight, which means that the matching css is and stays alive and
2747 * doesn't need to be pinned. The RCU locking is not necessary
2748 * either. It's just for the convenience of using cgroup_css().
2749 */
2750 rcu_read_lock();
2751 css = cgroup_css(cgrp, cft->ss);
2752 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753
Tejun Heo451af502014-05-13 12:16:21 -04002754 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002755 unsigned long long v;
2756 ret = kstrtoull(buf, 0, &v);
2757 if (!ret)
2758 ret = cft->write_u64(css, cft, v);
2759 } else if (cft->write_s64) {
2760 long long v;
2761 ret = kstrtoll(buf, 0, &v);
2762 if (!ret)
2763 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002764 } else {
2765 ret = -EINVAL;
2766 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002767
Tejun Heoa742c592013-12-05 12:28:03 -05002768 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769}
2770
Tejun Heo6612f052013-12-05 12:28:04 -05002771static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002772{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002773 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002774}
2775
2776static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2777{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002778 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002779}
2780
2781static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2782{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002783 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002784}
2785
2786static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2787{
Tejun Heo7da11272013-12-05 12:28:04 -05002788 struct cftype *cft = seq_cft(m);
2789 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002790
Tejun Heo2da8ca82013-12-05 12:28:04 -05002791 if (cft->seq_show)
2792 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002793
Tejun Heo896f5192013-12-05 12:28:04 -05002794 if (cft->read_u64)
2795 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2796 else if (cft->read_s64)
2797 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2798 else
2799 return -EINVAL;
2800 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002801}
2802
Tejun Heo2bd59d42014-02-11 11:52:49 -05002803static struct kernfs_ops cgroup_kf_single_ops = {
2804 .atomic_write_len = PAGE_SIZE,
2805 .write = cgroup_file_write,
2806 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002807};
2808
Tejun Heo2bd59d42014-02-11 11:52:49 -05002809static struct kernfs_ops cgroup_kf_ops = {
2810 .atomic_write_len = PAGE_SIZE,
2811 .write = cgroup_file_write,
2812 .seq_start = cgroup_seqfile_start,
2813 .seq_next = cgroup_seqfile_next,
2814 .seq_stop = cgroup_seqfile_stop,
2815 .seq_show = cgroup_seqfile_show,
2816};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002817
2818/*
2819 * cgroup_rename - Only allow simple rename of directories in place.
2820 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002821static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2822 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002823{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002824 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002825 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002826
Tejun Heo2bd59d42014-02-11 11:52:49 -05002827 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002828 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002829 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002830 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002831
Tejun Heo6db8e852013-06-14 11:18:22 -07002832 /*
2833 * This isn't a proper migration and its usefulness is very
2834 * limited. Disallow if sane_behavior.
2835 */
2836 if (cgroup_sane_behavior(cgrp))
2837 return -EPERM;
2838
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002839 /*
Tejun Heo8353da12014-05-13 12:19:23 -04002840 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002841 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04002842 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002843 */
2844 kernfs_break_active_protection(new_parent);
2845 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002846
Tejun Heo2bd59d42014-02-11 11:52:49 -05002847 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002848
Tejun Heo2bd59d42014-02-11 11:52:49 -05002849 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002850
Tejun Heo2bd59d42014-02-11 11:52:49 -05002851 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002852
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002853 kernfs_unbreak_active_protection(kn);
2854 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002855 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002856}
2857
Tejun Heo49957f82014-04-07 16:44:47 -04002858/* set uid and gid of cgroup dirs and files to that of the creator */
2859static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2860{
2861 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2862 .ia_uid = current_fsuid(),
2863 .ia_gid = current_fsgid(), };
2864
2865 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2866 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2867 return 0;
2868
2869 return kernfs_setattr(kn, &iattr);
2870}
2871
Tejun Heo2bb566c2013-08-08 20:11:23 -04002872static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002873{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002874 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002875 struct kernfs_node *kn;
2876 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002877 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002878
Tejun Heo2bd59d42014-02-11 11:52:49 -05002879#ifdef CONFIG_DEBUG_LOCK_ALLOC
2880 key = &cft->lockdep_key;
2881#endif
2882 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2883 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2884 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002885 if (IS_ERR(kn))
2886 return PTR_ERR(kn);
2887
2888 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002889 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002890 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002891 return ret;
2892 }
2893
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002894 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002895 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002896 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002897}
2898
Tejun Heob1f28d32013-06-28 16:24:10 -07002899/**
2900 * cgroup_addrm_files - add or remove files to a cgroup directory
2901 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002902 * @cfts: array of cftypes to be added
2903 * @is_add: whether to add or remove
2904 *
2905 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002906 * For removals, this function never fails. If addition fails, this
2907 * function doesn't remove files already added. The caller is responsible
2908 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002909 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002910static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2911 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002912{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002913 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002914 int ret;
2915
Tejun Heo01f64742014-05-13 12:19:23 -04002916 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002917
2918 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002919 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002920 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2921 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002922 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2923 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002924 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002925 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04002926 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08002927 continue;
2928
Li Zefan2739d3c2013-01-21 18:18:33 +08002929 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002930 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002931 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002932 pr_warn("%s: failed to add %s, err=%d\n",
2933 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002934 return ret;
2935 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002936 } else {
2937 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002938 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002939 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002940 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002941}
2942
Tejun Heo21a2d342014-02-12 09:29:49 -05002943static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002944{
2945 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002946 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002947 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002948 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002949 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002950
Tejun Heo01f64742014-05-13 12:19:23 -04002951 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002952
Li Zefane8c82d22013-06-18 18:48:37 +08002953 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002954 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002955 struct cgroup *cgrp = css->cgroup;
2956
Li Zefane8c82d22013-06-18 18:48:37 +08002957 if (cgroup_is_dead(cgrp))
2958 continue;
2959
Tejun Heo21a2d342014-02-12 09:29:49 -05002960 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002961 if (ret)
2962 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002963 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002964
2965 if (is_add && !ret)
2966 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002967 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002968}
2969
Tejun Heo2da440a2014-02-11 11:52:48 -05002970static void cgroup_exit_cftypes(struct cftype *cfts)
2971{
2972 struct cftype *cft;
2973
Tejun Heo2bd59d42014-02-11 11:52:49 -05002974 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2975 /* free copy for custom atomic_write_len, see init_cftypes() */
2976 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2977 kfree(cft->kf_ops);
2978 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002979 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002980 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002981}
2982
Tejun Heo2bd59d42014-02-11 11:52:49 -05002983static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002984{
2985 struct cftype *cft;
2986
Tejun Heo2bd59d42014-02-11 11:52:49 -05002987 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2988 struct kernfs_ops *kf_ops;
2989
Tejun Heo0adb0702014-02-12 09:29:48 -05002990 WARN_ON(cft->ss || cft->kf_ops);
2991
Tejun Heo2bd59d42014-02-11 11:52:49 -05002992 if (cft->seq_start)
2993 kf_ops = &cgroup_kf_ops;
2994 else
2995 kf_ops = &cgroup_kf_single_ops;
2996
2997 /*
2998 * Ugh... if @cft wants a custom max_write_len, we need to
2999 * make a copy of kf_ops to set its atomic_write_len.
3000 */
3001 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3002 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3003 if (!kf_ops) {
3004 cgroup_exit_cftypes(cfts);
3005 return -ENOMEM;
3006 }
3007 kf_ops->atomic_write_len = cft->max_write_len;
3008 }
3009
3010 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003011 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003012 }
3013
3014 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003015}
3016
Tejun Heo21a2d342014-02-12 09:29:49 -05003017static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3018{
Tejun Heo01f64742014-05-13 12:19:23 -04003019 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003020
3021 if (!cfts || !cfts[0].ss)
3022 return -ENOENT;
3023
3024 list_del(&cfts->node);
3025 cgroup_apply_cftypes(cfts, false);
3026 cgroup_exit_cftypes(cfts);
3027 return 0;
3028}
3029
Tejun Heo8e3f6542012-04-01 12:09:55 -07003030/**
Tejun Heo80b13582014-02-12 09:29:48 -05003031 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3032 * @cfts: zero-length name terminated array of cftypes
3033 *
3034 * Unregister @cfts. Files described by @cfts are removed from all
3035 * existing cgroups and all future cgroups won't have them either. This
3036 * function can be called anytime whether @cfts' subsys is attached or not.
3037 *
3038 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3039 * registered.
3040 */
3041int cgroup_rm_cftypes(struct cftype *cfts)
3042{
Tejun Heo21a2d342014-02-12 09:29:49 -05003043 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003044
Tejun Heo01f64742014-05-13 12:19:23 -04003045 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003046 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003047 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003048 return ret;
3049}
3050
3051/**
3052 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3053 * @ss: target cgroup subsystem
3054 * @cfts: zero-length name terminated array of cftypes
3055 *
3056 * Register @cfts to @ss. Files described by @cfts are created for all
3057 * existing cgroups to which @ss is attached and all future cgroups will
3058 * have them too. This function can be called anytime whether @ss is
3059 * attached or not.
3060 *
3061 * Returns 0 on successful registration, -errno on failure. Note that this
3062 * function currently returns 0 as long as @cfts registration is successful
3063 * even if some file creation attempts on existing cgroups fail.
3064 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003065int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003066{
Tejun Heo9ccece82013-06-28 16:24:11 -07003067 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003068
Li Zefandc5736e2014-02-17 10:41:50 +08003069 if (!cfts || cfts[0].name[0] == '\0')
3070 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003071
Tejun Heo2bd59d42014-02-11 11:52:49 -05003072 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003073 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003074 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003075
Tejun Heo01f64742014-05-13 12:19:23 -04003076 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003077
Tejun Heo0adb0702014-02-12 09:29:48 -05003078 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003079 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003080 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003081 cgroup_rm_cftypes_locked(cfts);
3082
Tejun Heo01f64742014-05-13 12:19:23 -04003083 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003084 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003085}
Tejun Heo79578622012-04-01 12:09:56 -07003086
3087/**
Li Zefana043e3b2008-02-23 15:24:09 -08003088 * cgroup_task_count - count the number of tasks in a cgroup.
3089 * @cgrp: the cgroup in question
3090 *
3091 * Return the number of tasks in the cgroup.
3092 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003093static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003094{
3095 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003096 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003097
Tejun Heo96d365e2014-02-13 06:58:40 -05003098 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003099 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3100 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003101 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003102 return count;
3103}
3104
Tejun Heo574bd9f2012-11-09 09:12:29 -08003105/**
Tejun Heo492eb212013-08-08 20:11:25 -04003106 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003107 * @pos: the current position (%NULL to initiate traversal)
3108 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003109 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003110 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003111 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003112 * that @parent and @pos are accessible. The next sibling is guaranteed to
3113 * be returned regardless of their states.
3114 *
3115 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3116 * css which finished ->css_online() is guaranteed to be visible in the
3117 * future iterations and will stay visible until the last reference is put.
3118 * A css which hasn't finished ->css_online() or already finished
3119 * ->css_offline() may show up during traversal. It's each subsystem's
3120 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003121 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003122struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3123 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003124{
Tejun Heoc2931b72014-05-16 13:22:51 -04003125 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003126
Tejun Heo8353da12014-05-13 12:19:23 -04003127 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003128
3129 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003130 * @pos could already have been unlinked from the sibling list.
3131 * Once a cgroup is removed, its ->sibling.next is no longer
3132 * updated when its next sibling changes. CSS_RELEASED is set when
3133 * @pos is taken off list, at which time its next pointer is valid,
3134 * and, as releases are serialized, the one pointed to by the next
3135 * pointer is guaranteed to not have started release yet. This
3136 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3137 * critical section, the one pointed to by its next pointer is
3138 * guaranteed to not have finished its RCU grace period even if we
3139 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003140 *
Tejun Heode3f0342014-05-16 13:22:49 -04003141 * If @pos has CSS_RELEASED set, its next pointer can't be
3142 * dereferenced; however, as each css is given a monotonically
3143 * increasing unique serial number and always appended to the
3144 * sibling list, the next one can be found by walking the parent's
3145 * children until the first css with higher serial number than
3146 * @pos's. While this path can be slower, it happens iff iteration
3147 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003148 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003149 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003150 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3151 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3152 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003153 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003154 list_for_each_entry_rcu(next, &parent->children, sibling)
3155 if (next->serial_nr > pos->serial_nr)
Tejun Heo3b287a52013-08-08 20:11:24 -04003156 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003157 }
3158
Tejun Heo3b281af2014-04-23 11:13:15 -04003159 /*
3160 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003161 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003162 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003163 if (&next->sibling != &parent->children)
3164 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003165 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003166}
Tejun Heo53fa5262013-05-24 10:55:38 +09003167
3168/**
Tejun Heo492eb212013-08-08 20:11:25 -04003169 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003170 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003171 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172 *
Tejun Heo492eb212013-08-08 20:11:25 -04003173 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003174 * to visit for pre-order traversal of @root's descendants. @root is
3175 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003176 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003177 * While this function requires cgroup_mutex or RCU read locking, it
3178 * doesn't require the whole traversal to be contained in a single critical
3179 * section. This function will return the correct next descendant as long
3180 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003181 *
3182 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3183 * css which finished ->css_online() is guaranteed to be visible in the
3184 * future iterations and will stay visible until the last reference is put.
3185 * A css which hasn't finished ->css_online() or already finished
3186 * ->css_offline() may show up during traversal. It's each subsystem's
3187 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003188 */
Tejun Heo492eb212013-08-08 20:11:25 -04003189struct cgroup_subsys_state *
3190css_next_descendant_pre(struct cgroup_subsys_state *pos,
3191 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003192{
Tejun Heo492eb212013-08-08 20:11:25 -04003193 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003194
Tejun Heo8353da12014-05-13 12:19:23 -04003195 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003196
Tejun Heobd8815a2013-08-08 20:11:27 -04003197 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003198 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003199 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003200
3201 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003202 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003203 if (next)
3204 return next;
3205
3206 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003207 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003208 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003209 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003210 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003211 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003212 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003213
3214 return NULL;
3215}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003216
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003217/**
Tejun Heo492eb212013-08-08 20:11:25 -04003218 * css_rightmost_descendant - return the rightmost descendant of a css
3219 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003220 *
Tejun Heo492eb212013-08-08 20:11:25 -04003221 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3222 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003223 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003224 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003225 * While this function requires cgroup_mutex or RCU read locking, it
3226 * doesn't require the whole traversal to be contained in a single critical
3227 * section. This function will return the correct rightmost descendant as
3228 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003229 */
Tejun Heo492eb212013-08-08 20:11:25 -04003230struct cgroup_subsys_state *
3231css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003232{
Tejun Heo492eb212013-08-08 20:11:25 -04003233 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003234
Tejun Heo8353da12014-05-13 12:19:23 -04003235 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003236
3237 do {
3238 last = pos;
3239 /* ->prev isn't RCU safe, walk ->next till the end */
3240 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003241 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003242 pos = tmp;
3243 } while (pos);
3244
3245 return last;
3246}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003247
Tejun Heo492eb212013-08-08 20:11:25 -04003248static struct cgroup_subsys_state *
3249css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003250{
Tejun Heo492eb212013-08-08 20:11:25 -04003251 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003252
3253 do {
3254 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003255 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003256 } while (pos);
3257
3258 return last;
3259}
3260
3261/**
Tejun Heo492eb212013-08-08 20:11:25 -04003262 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003263 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003264 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003265 *
Tejun Heo492eb212013-08-08 20:11:25 -04003266 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003267 * to visit for post-order traversal of @root's descendants. @root is
3268 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003269 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003270 * While this function requires cgroup_mutex or RCU read locking, it
3271 * doesn't require the whole traversal to be contained in a single critical
3272 * section. This function will return the correct next descendant as long
3273 * as both @pos and @cgroup are accessible and @pos is a descendant of
3274 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003275 *
3276 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3277 * css which finished ->css_online() is guaranteed to be visible in the
3278 * future iterations and will stay visible until the last reference is put.
3279 * A css which hasn't finished ->css_online() or already finished
3280 * ->css_offline() may show up during traversal. It's each subsystem's
3281 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003282 */
Tejun Heo492eb212013-08-08 20:11:25 -04003283struct cgroup_subsys_state *
3284css_next_descendant_post(struct cgroup_subsys_state *pos,
3285 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003286{
Tejun Heo492eb212013-08-08 20:11:25 -04003287 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003288
Tejun Heo8353da12014-05-13 12:19:23 -04003289 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003290
Tejun Heo58b79a92013-09-06 15:31:08 -04003291 /* if first iteration, visit leftmost descendant which may be @root */
3292 if (!pos)
3293 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003294
Tejun Heobd8815a2013-08-08 20:11:27 -04003295 /* if we visited @root, we're done */
3296 if (pos == root)
3297 return NULL;
3298
Tejun Heo574bd9f2012-11-09 09:12:29 -08003299 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003300 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003301 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003302 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003303
3304 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003305 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003306}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003307
Tejun Heof3d46502014-05-16 13:22:52 -04003308/**
3309 * css_has_online_children - does a css have online children
3310 * @css: the target css
3311 *
3312 * Returns %true if @css has any online children; otherwise, %false. This
3313 * function can be called from any context but the caller is responsible
3314 * for synchronizing against on/offlining as necessary.
3315 */
3316bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003317{
Tejun Heof3d46502014-05-16 13:22:52 -04003318 struct cgroup_subsys_state *child;
3319 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003320
3321 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003322 css_for_each_child(child, css) {
3323 if (css->flags & CSS_ONLINE) {
3324 ret = true;
3325 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003326 }
3327 }
3328 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003329 return ret;
Tejun Heocbc125e2014-05-14 09:15:01 -04003330}
3331
Tejun Heo0942eee2013-08-08 20:11:26 -04003332/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003333 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003334 * @it: the iterator to advance
3335 *
3336 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003337 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003338static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003339{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003340 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003341 struct cgrp_cset_link *link;
3342 struct css_set *cset;
3343
3344 /* Advance to the next non-empty css_set */
3345 do {
3346 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003347 if (l == it->cset_head) {
3348 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003349 return;
3350 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003351
3352 if (it->ss) {
3353 cset = container_of(l, struct css_set,
3354 e_cset_node[it->ss->id]);
3355 } else {
3356 link = list_entry(l, struct cgrp_cset_link, cset_link);
3357 cset = link->cset;
3358 }
Tejun Heoc7561122014-02-25 10:04:01 -05003359 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3360
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003361 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003362
3363 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003364 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003365 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003366 it->task_pos = cset->mg_tasks.next;
3367
3368 it->tasks_head = &cset->tasks;
3369 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003370}
3371
Tejun Heo0942eee2013-08-08 20:11:26 -04003372/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003373 * css_task_iter_start - initiate task iteration
3374 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003375 * @it: the task iterator to use
3376 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003377 * Initiate iteration through the tasks of @css. The caller can call
3378 * css_task_iter_next() to walk through the tasks until the function
3379 * returns NULL. On completion of iteration, css_task_iter_end() must be
3380 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003381 *
3382 * Note that this function acquires a lock which is released when the
3383 * iteration finishes. The caller can't sleep while iteration is in
3384 * progress.
3385 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003386void css_task_iter_start(struct cgroup_subsys_state *css,
3387 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003388 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003389{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003390 /* no one should try to iterate before mounting cgroups */
3391 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003392
Tejun Heo96d365e2014-02-13 06:58:40 -05003393 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003394
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003395 it->ss = css->ss;
3396
3397 if (it->ss)
3398 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3399 else
3400 it->cset_pos = &css->cgroup->cset_links;
3401
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003402 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003403
Tejun Heo72ec7022013-08-08 20:11:26 -04003404 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003405}
3406
Tejun Heo0942eee2013-08-08 20:11:26 -04003407/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003408 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003409 * @it: the task iterator being iterated
3410 *
3411 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003412 * initialized via css_task_iter_start(). Returns NULL when the iteration
3413 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003414 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003415struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003416{
3417 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003418 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003419
3420 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003421 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003422 return NULL;
3423 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003424
3425 /*
3426 * Advance iterator to find next entry. cset->tasks is consumed
3427 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3428 * next cset.
3429 */
Paul Menage817929e2007-10-18 23:39:36 -07003430 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003431
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003432 if (l == it->tasks_head)
3433 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003434
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003435 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003436 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003437 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003438 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003439
Paul Menage817929e2007-10-18 23:39:36 -07003440 return res;
3441}
3442
Tejun Heo0942eee2013-08-08 20:11:26 -04003443/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003444 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003445 * @it: the task iterator to finish
3446 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003447 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003448 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003449void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003450 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003451{
Tejun Heo96d365e2014-02-13 06:58:40 -05003452 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003453}
3454
3455/**
3456 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3457 * @to: cgroup to which the tasks will be moved
3458 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003459 *
3460 * Locking rules between cgroup_post_fork() and the migration path
3461 * guarantee that, if a task is forking while being migrated, the new child
3462 * is guaranteed to be either visible in the source cgroup after the
3463 * parent's migration is complete or put into the target cgroup. No task
3464 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003465 */
3466int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3467{
Tejun Heo952aaa12014-02-25 10:04:03 -05003468 LIST_HEAD(preloaded_csets);
3469 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003470 struct css_task_iter it;
3471 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003472 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003473
Tejun Heo952aaa12014-02-25 10:04:03 -05003474 mutex_lock(&cgroup_mutex);
3475
3476 /* all tasks in @from are being moved, all csets are source */
3477 down_read(&css_set_rwsem);
3478 list_for_each_entry(link, &from->cset_links, cset_link)
3479 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3480 up_read(&css_set_rwsem);
3481
3482 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3483 if (ret)
3484 goto out_err;
3485
3486 /*
3487 * Migrate tasks one-by-one until @form is empty. This fails iff
3488 * ->can_attach() fails.
3489 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003490 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003491 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003492 task = css_task_iter_next(&it);
3493 if (task)
3494 get_task_struct(task);
3495 css_task_iter_end(&it);
3496
3497 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003498 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003499 put_task_struct(task);
3500 }
3501 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003502out_err:
3503 cgroup_migrate_finish(&preloaded_csets);
3504 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003505 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003506}
3507
Paul Menage817929e2007-10-18 23:39:36 -07003508/*
Ben Blum102a7752009-09-23 15:56:26 -07003509 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003510 *
3511 * Reading this file can return large amounts of data if a cgroup has
3512 * *lots* of attached tasks. So it may need several calls to read(),
3513 * but we cannot guarantee that the information we produce is correct
3514 * unless we produce it entirely atomically.
3515 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003516 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003517
Li Zefan24528252012-01-20 11:58:43 +08003518/* which pidlist file are we talking about? */
3519enum cgroup_filetype {
3520 CGROUP_FILE_PROCS,
3521 CGROUP_FILE_TASKS,
3522};
3523
3524/*
3525 * A pidlist is a list of pids that virtually represents the contents of one
3526 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3527 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3528 * to the cgroup.
3529 */
3530struct cgroup_pidlist {
3531 /*
3532 * used to find which pidlist is wanted. doesn't change as long as
3533 * this particular list stays in the list.
3534 */
3535 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3536 /* array of xids */
3537 pid_t *list;
3538 /* how many elements the above list has */
3539 int length;
Li Zefan24528252012-01-20 11:58:43 +08003540 /* each of these stored in a list by its cgroup */
3541 struct list_head links;
3542 /* pointer to the cgroup we belong to, for list removal purposes */
3543 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003544 /* for delayed destruction */
3545 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003546};
3547
Paul Menagebbcb81d2007-10-18 23:39:32 -07003548/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003549 * The following two functions "fix" the issue where there are more pids
3550 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3551 * TODO: replace with a kernel-wide solution to this problem
3552 */
3553#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3554static void *pidlist_allocate(int count)
3555{
3556 if (PIDLIST_TOO_LARGE(count))
3557 return vmalloc(count * sizeof(pid_t));
3558 else
3559 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3560}
Tejun Heob1a21362013-11-29 10:42:58 -05003561
Ben Blumd1d9fd32009-09-23 15:56:28 -07003562static void pidlist_free(void *p)
3563{
3564 if (is_vmalloc_addr(p))
3565 vfree(p);
3566 else
3567 kfree(p);
3568}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003569
3570/*
Tejun Heob1a21362013-11-29 10:42:58 -05003571 * Used to destroy all pidlists lingering waiting for destroy timer. None
3572 * should be left afterwards.
3573 */
3574static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3575{
3576 struct cgroup_pidlist *l, *tmp_l;
3577
3578 mutex_lock(&cgrp->pidlist_mutex);
3579 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3580 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3581 mutex_unlock(&cgrp->pidlist_mutex);
3582
3583 flush_workqueue(cgroup_pidlist_destroy_wq);
3584 BUG_ON(!list_empty(&cgrp->pidlists));
3585}
3586
3587static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3588{
3589 struct delayed_work *dwork = to_delayed_work(work);
3590 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3591 destroy_dwork);
3592 struct cgroup_pidlist *tofree = NULL;
3593
3594 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003595
3596 /*
Tejun Heo04502362013-11-29 10:42:59 -05003597 * Destroy iff we didn't get queued again. The state won't change
3598 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003599 */
Tejun Heo04502362013-11-29 10:42:59 -05003600 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003601 list_del(&l->links);
3602 pidlist_free(l->list);
3603 put_pid_ns(l->key.ns);
3604 tofree = l;
3605 }
3606
Tejun Heob1a21362013-11-29 10:42:58 -05003607 mutex_unlock(&l->owner->pidlist_mutex);
3608 kfree(tofree);
3609}
3610
3611/*
Ben Blum102a7752009-09-23 15:56:26 -07003612 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003613 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003614 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003615static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003616{
Ben Blum102a7752009-09-23 15:56:26 -07003617 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003618
3619 /*
3620 * we presume the 0th element is unique, so i starts at 1. trivial
3621 * edge cases first; no work needs to be done for either
3622 */
3623 if (length == 0 || length == 1)
3624 return length;
3625 /* src and dest walk down the list; dest counts unique elements */
3626 for (src = 1; src < length; src++) {
3627 /* find next unique element */
3628 while (list[src] == list[src-1]) {
3629 src++;
3630 if (src == length)
3631 goto after;
3632 }
3633 /* dest always points to where the next unique element goes */
3634 list[dest] = list[src];
3635 dest++;
3636 }
3637after:
Ben Blum102a7752009-09-23 15:56:26 -07003638 return dest;
3639}
3640
Tejun Heoafb2bc12013-11-29 10:42:59 -05003641/*
3642 * The two pid files - task and cgroup.procs - guaranteed that the result
3643 * is sorted, which forced this whole pidlist fiasco. As pid order is
3644 * different per namespace, each namespace needs differently sorted list,
3645 * making it impossible to use, for example, single rbtree of member tasks
3646 * sorted by task pointer. As pidlists can be fairly large, allocating one
3647 * per open file is dangerous, so cgroup had to implement shared pool of
3648 * pidlists keyed by cgroup and namespace.
3649 *
3650 * All this extra complexity was caused by the original implementation
3651 * committing to an entirely unnecessary property. In the long term, we
3652 * want to do away with it. Explicitly scramble sort order if
3653 * sane_behavior so that no such expectation exists in the new interface.
3654 *
3655 * Scrambling is done by swapping every two consecutive bits, which is
3656 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3657 */
3658static pid_t pid_fry(pid_t pid)
3659{
3660 unsigned a = pid & 0x55555555;
3661 unsigned b = pid & 0xAAAAAAAA;
3662
3663 return (a << 1) | (b >> 1);
3664}
3665
3666static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3667{
3668 if (cgroup_sane_behavior(cgrp))
3669 return pid_fry(pid);
3670 else
3671 return pid;
3672}
3673
Ben Blum102a7752009-09-23 15:56:26 -07003674static int cmppid(const void *a, const void *b)
3675{
3676 return *(pid_t *)a - *(pid_t *)b;
3677}
3678
Tejun Heoafb2bc12013-11-29 10:42:59 -05003679static int fried_cmppid(const void *a, const void *b)
3680{
3681 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3682}
3683
Ben Blum72a8cb32009-09-23 15:56:27 -07003684static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3685 enum cgroup_filetype type)
3686{
3687 struct cgroup_pidlist *l;
3688 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003689 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003690
Tejun Heoe6b81712013-11-29 10:42:59 -05003691 lockdep_assert_held(&cgrp->pidlist_mutex);
3692
3693 list_for_each_entry(l, &cgrp->pidlists, links)
3694 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003696 return NULL;
3697}
3698
Ben Blum72a8cb32009-09-23 15:56:27 -07003699/*
3700 * find the appropriate pidlist for our purpose (given procs vs tasks)
3701 * returns with the lock on that pidlist already held, and takes care
3702 * of the use count, or returns NULL with no locks held if we're out of
3703 * memory.
3704 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003705static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3706 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003707{
3708 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003709
Tejun Heoe6b81712013-11-29 10:42:59 -05003710 lockdep_assert_held(&cgrp->pidlist_mutex);
3711
3712 l = cgroup_pidlist_find(cgrp, type);
3713 if (l)
3714 return l;
3715
Ben Blum72a8cb32009-09-23 15:56:27 -07003716 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003717 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003718 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003719 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003720
Tejun Heob1a21362013-11-29 10:42:58 -05003721 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003722 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003723 /* don't need task_nsproxy() if we're looking at ourself */
3724 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003725 l->owner = cgrp;
3726 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003727 return l;
3728}
3729
3730/*
Ben Blum102a7752009-09-23 15:56:26 -07003731 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3732 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003733static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3734 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003735{
3736 pid_t *array;
3737 int length;
3738 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003739 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003740 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003741 struct cgroup_pidlist *l;
3742
Tejun Heo4bac00d2013-11-29 10:42:59 -05003743 lockdep_assert_held(&cgrp->pidlist_mutex);
3744
Ben Blum102a7752009-09-23 15:56:26 -07003745 /*
3746 * If cgroup gets more users after we read count, we won't have
3747 * enough space - tough. This race is indistinguishable to the
3748 * caller from the case that the additional cgroup users didn't
3749 * show up until sometime later on.
3750 */
3751 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003752 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003753 if (!array)
3754 return -ENOMEM;
3755 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04003756 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003757 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003758 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003759 break;
Ben Blum102a7752009-09-23 15:56:26 -07003760 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003761 if (type == CGROUP_FILE_PROCS)
3762 pid = task_tgid_vnr(tsk);
3763 else
3764 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003765 if (pid > 0) /* make sure to only use valid results */
3766 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003767 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003768 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003769 length = n;
3770 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003771 if (cgroup_sane_behavior(cgrp))
3772 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3773 else
3774 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003775 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003776 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003777
Tejun Heoe6b81712013-11-29 10:42:59 -05003778 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003779 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003780 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003781 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003782 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003783 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003784
3785 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003786 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003787 l->list = array;
3788 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003789 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003790 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003791}
3792
Balbir Singh846c7bb2007-10-18 23:39:44 -07003793/**
Li Zefana043e3b2008-02-23 15:24:09 -08003794 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003795 * @stats: cgroupstats to fill information into
3796 * @dentry: A dentry entry belonging to the cgroup for which stats have
3797 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003798 *
3799 * Build and fill cgroupstats so that taskstats can export it to user
3800 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003801 */
3802int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3803{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003804 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003805 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003806 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003807 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003808
Tejun Heo2bd59d42014-02-11 11:52:49 -05003809 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3810 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3811 kernfs_type(kn) != KERNFS_DIR)
3812 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003813
Li Zefanbad34662014-02-14 16:54:28 +08003814 mutex_lock(&cgroup_mutex);
3815
Tejun Heo2bd59d42014-02-11 11:52:49 -05003816 /*
3817 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003818 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003819 * @kn->priv is RCU safe. Let's do the RCU dancing.
3820 */
3821 rcu_read_lock();
3822 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003823 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003824 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003825 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003826 return -ENOENT;
3827 }
Li Zefanbad34662014-02-14 16:54:28 +08003828 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003829
Tejun Heo9d800df2014-05-14 09:15:00 -04003830 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04003831 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003832 switch (tsk->state) {
3833 case TASK_RUNNING:
3834 stats->nr_running++;
3835 break;
3836 case TASK_INTERRUPTIBLE:
3837 stats->nr_sleeping++;
3838 break;
3839 case TASK_UNINTERRUPTIBLE:
3840 stats->nr_uninterruptible++;
3841 break;
3842 case TASK_STOPPED:
3843 stats->nr_stopped++;
3844 break;
3845 default:
3846 if (delayacct_is_task_waiting_on_io(tsk))
3847 stats->nr_io_wait++;
3848 break;
3849 }
3850 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003851 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003852
Li Zefanbad34662014-02-14 16:54:28 +08003853 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003854 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003855}
3856
Paul Menage8f3ff202009-09-23 15:56:25 -07003857
Paul Menagecc31edc2008-10-18 20:28:04 -07003858/*
Ben Blum102a7752009-09-23 15:56:26 -07003859 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003860 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003861 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003862 */
3863
Ben Blum102a7752009-09-23 15:56:26 -07003864static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003865{
3866 /*
3867 * Initially we receive a position value that corresponds to
3868 * one more than the last pid shown (or 0 on the first call or
3869 * after a seek to the start). Use a binary-search to find the
3870 * next pid to display, if any
3871 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003872 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003873 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003874 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003875 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003876 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003877 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003878
Tejun Heo4bac00d2013-11-29 10:42:59 -05003879 mutex_lock(&cgrp->pidlist_mutex);
3880
3881 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003882 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003883 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003884 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003885 * could already have been destroyed.
3886 */
Tejun Heo5d224442013-12-05 12:28:04 -05003887 if (of->priv)
3888 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003889
3890 /*
3891 * Either this is the first start() after open or the matching
3892 * pidlist has been destroyed inbetween. Create a new one.
3893 */
Tejun Heo5d224442013-12-05 12:28:04 -05003894 if (!of->priv) {
3895 ret = pidlist_array_load(cgrp, type,
3896 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003897 if (ret)
3898 return ERR_PTR(ret);
3899 }
Tejun Heo5d224442013-12-05 12:28:04 -05003900 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003901
Paul Menagecc31edc2008-10-18 20:28:04 -07003902 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003903 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003904
Paul Menagecc31edc2008-10-18 20:28:04 -07003905 while (index < end) {
3906 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003907 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003908 index = mid;
3909 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003910 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003911 index = mid + 1;
3912 else
3913 end = mid;
3914 }
3915 }
3916 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003917 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003918 return NULL;
3919 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003920 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003921 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003922 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003923}
3924
Ben Blum102a7752009-09-23 15:56:26 -07003925static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003926{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003927 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003928 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003929
Tejun Heo5d224442013-12-05 12:28:04 -05003930 if (l)
3931 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003932 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003933 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003934}
3935
Ben Blum102a7752009-09-23 15:56:26 -07003936static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003937{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003938 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003939 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003940 pid_t *p = v;
3941 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003942 /*
3943 * Advance to the next pid in the array. If this goes off the
3944 * end, we're done
3945 */
3946 p++;
3947 if (p >= end) {
3948 return NULL;
3949 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003950 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003951 return p;
3952 }
3953}
3954
Ben Blum102a7752009-09-23 15:56:26 -07003955static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003956{
3957 return seq_printf(s, "%d\n", *(int *)v);
3958}
3959
Tejun Heo182446d2013-08-08 20:11:24 -04003960static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3961 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003962{
Tejun Heo182446d2013-08-08 20:11:24 -04003963 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003964}
3965
Tejun Heo182446d2013-08-08 20:11:24 -04003966static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3967 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003968{
Tejun Heo182446d2013-08-08 20:11:24 -04003969 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003970 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003971 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003972 else
Tejun Heo182446d2013-08-08 20:11:24 -04003973 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003974 return 0;
3975}
3976
Tejun Heo182446d2013-08-08 20:11:24 -04003977static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3978 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003979{
Tejun Heo182446d2013-08-08 20:11:24 -04003980 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003981}
3982
Tejun Heo182446d2013-08-08 20:11:24 -04003983static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3984 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003985{
3986 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003987 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003988 else
Tejun Heo182446d2013-08-08 20:11:24 -04003989 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003990 return 0;
3991}
3992
Tejun Heod5c56ce2013-06-03 19:14:34 -07003993static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003994 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003995 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003996 .seq_start = cgroup_pidlist_start,
3997 .seq_next = cgroup_pidlist_next,
3998 .seq_stop = cgroup_pidlist_stop,
3999 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004000 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004001 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004002 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004003 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004004 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004005 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004006 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004007 .read_u64 = cgroup_clone_children_read,
4008 .write_u64 = cgroup_clone_children_write,
4009 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004010 {
Tejun Heo873fe092013-04-14 20:15:26 -07004011 .name = "cgroup.sane_behavior",
4012 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004013 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004014 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004015 {
4016 .name = "cgroup.controllers",
4017 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
4018 .seq_show = cgroup_root_controllers_show,
4019 },
4020 {
4021 .name = "cgroup.controllers",
4022 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4023 .seq_show = cgroup_controllers_show,
4024 },
4025 {
4026 .name = "cgroup.subtree_control",
4027 .flags = CFTYPE_ONLY_ON_DFL,
4028 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004029 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004030 },
Tejun Heo842b5972014-04-25 18:28:02 -04004031 {
4032 .name = "cgroup.populated",
4033 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4034 .seq_show = cgroup_populated_show,
4035 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004036
4037 /*
4038 * Historical crazy stuff. These don't have "cgroup." prefix and
4039 * don't exist if sane_behavior. If you're depending on these, be
4040 * prepared to be burned.
4041 */
4042 {
4043 .name = "tasks",
4044 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004045 .seq_start = cgroup_pidlist_start,
4046 .seq_next = cgroup_pidlist_next,
4047 .seq_stop = cgroup_pidlist_stop,
4048 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004049 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004050 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004051 .mode = S_IRUGO | S_IWUSR,
4052 },
4053 {
4054 .name = "notify_on_release",
4055 .flags = CFTYPE_INSANE,
4056 .read_u64 = cgroup_read_notify_on_release,
4057 .write_u64 = cgroup_write_notify_on_release,
4058 },
Tejun Heo873fe092013-04-14 20:15:26 -07004059 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004060 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004061 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004062 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004063 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004064 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004065 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004066 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004067};
4068
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004069/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004070 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004071 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004072 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004073 *
4074 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004075 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004076static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004077{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004079 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004080
Tejun Heo8e3f6542012-04-01 12:09:55 -07004081 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004082 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004083 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004084
Tejun Heo69dfa002014-05-04 15:09:13 -04004085 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004086 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004087
Tejun Heo0adb0702014-02-12 09:29:48 -05004088 list_for_each_entry(cfts, &ss->cfts, node) {
4089 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004090 if (ret < 0)
4091 goto err;
4092 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004095err:
4096 cgroup_clear_dir(cgrp, subsys_mask);
4097 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098}
4099
Tejun Heo0c21ead2013-08-13 20:22:51 -04004100/*
4101 * css destruction is four-stage process.
4102 *
4103 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4104 * Implemented in kill_css().
4105 *
4106 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004107 * and thus css_tryget_online() is guaranteed to fail, the css can be
4108 * offlined by invoking offline_css(). After offlining, the base ref is
4109 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004110 *
4111 * 3. When the percpu_ref reaches zero, the only possible remaining
4112 * accessors are inside RCU read sections. css_release() schedules the
4113 * RCU callback.
4114 *
4115 * 4. After the grace period, the css can be freed. Implemented in
4116 * css_free_work_fn().
4117 *
4118 * It is actually hairier because both step 2 and 4 require process context
4119 * and thus involve punting to css->destroy_work adding two additional
4120 * steps to the already complex sequence.
4121 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004122static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004123{
4124 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004125 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004126 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004127
Tejun Heo9d755d32014-05-14 09:15:02 -04004128 if (css->ss) {
4129 /* css free path */
4130 if (css->parent)
4131 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004132
Tejun Heo9d755d32014-05-14 09:15:02 -04004133 css->ss->css_free(css);
4134 cgroup_put(cgrp);
4135 } else {
4136 /* cgroup free path */
4137 atomic_dec(&cgrp->root->nr_cgrps);
4138 cgroup_pidlist_destroy_all(cgrp);
4139
Tejun Heod51f39b2014-05-16 13:22:48 -04004140 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004141 /*
4142 * We get a ref to the parent, and put the ref when
4143 * this cgroup is being freed, so it's guaranteed
4144 * that the parent won't be destroyed before its
4145 * children.
4146 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004147 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004148 kernfs_put(cgrp->kn);
4149 kfree(cgrp);
4150 } else {
4151 /*
4152 * This is root cgroup's refcnt reaching zero,
4153 * which indicates that the root should be
4154 * released.
4155 */
4156 cgroup_destroy_root(cgrp->root);
4157 }
4158 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004159}
4160
4161static void css_free_rcu_fn(struct rcu_head *rcu_head)
4162{
4163 struct cgroup_subsys_state *css =
4164 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4165
Tejun Heo0c21ead2013-08-13 20:22:51 -04004166 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004167 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004168}
4169
Tejun Heo25e15d82014-05-14 09:15:02 -04004170static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004171{
4172 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004173 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004174 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004175 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004176
Tejun Heo1fed1b22014-05-16 13:22:49 -04004177 mutex_lock(&cgroup_mutex);
4178
Tejun Heode3f0342014-05-16 13:22:49 -04004179 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004180 list_del_rcu(&css->sibling);
4181
Tejun Heo9d755d32014-05-14 09:15:02 -04004182 if (ss) {
4183 /* css release path */
4184 cgroup_idr_remove(&ss->css_idr, css->id);
4185 } else {
4186 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004187 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4188 cgrp->id = -1;
4189 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004190
Tejun Heo1fed1b22014-05-16 13:22:49 -04004191 mutex_unlock(&cgroup_mutex);
4192
Tejun Heo0c21ead2013-08-13 20:22:51 -04004193 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004194}
4195
Tejun Heo25e15d82014-05-14 09:15:02 -04004196static void css_release(struct percpu_ref *ref)
4197{
4198 struct cgroup_subsys_state *css =
4199 container_of(ref, struct cgroup_subsys_state, refcnt);
4200
4201 INIT_WORK(&css->destroy_work, css_release_work_fn);
4202 queue_work(cgroup_destroy_wq, &css->destroy_work);
4203}
4204
Tejun Heoddfcada2014-05-04 15:09:14 -04004205static void init_and_link_css(struct cgroup_subsys_state *css,
4206 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004208 lockdep_assert_held(&cgroup_mutex);
4209
Tejun Heoddfcada2014-05-04 15:09:14 -04004210 cgroup_get(cgrp);
4211
Tejun Heod5c419b2014-05-16 13:22:48 -04004212 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004213 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004214 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004215 INIT_LIST_HEAD(&css->sibling);
4216 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004217 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004218
Tejun Heod51f39b2014-05-16 13:22:48 -04004219 if (cgroup_parent(cgrp)) {
4220 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004221 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004222 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004223
Tejun Heoca8bdca2013-08-26 18:40:56 -04004224 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225}
4226
Li Zefan2a4ac632013-07-31 16:16:40 +08004227/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004228static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004229{
Tejun Heo623f9262013-08-13 11:01:55 -04004230 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004231 int ret = 0;
4232
Tejun Heoa31f2d32012-11-19 08:13:37 -08004233 lockdep_assert_held(&cgroup_mutex);
4234
Tejun Heo92fb9742012-11-19 08:13:38 -08004235 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004236 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004237 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004238 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004239 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004240 }
Tejun Heob1929db2012-11-19 08:13:38 -08004241 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004242}
4243
Li Zefan2a4ac632013-07-31 16:16:40 +08004244/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004245static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004246{
Tejun Heo623f9262013-08-13 11:01:55 -04004247 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004248
4249 lockdep_assert_held(&cgroup_mutex);
4250
4251 if (!(css->flags & CSS_ONLINE))
4252 return;
4253
Li Zefand7eeac12013-03-12 15:35:59 -07004254 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004255 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004256
Tejun Heoeb954192013-08-08 20:11:23 -04004257 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004258 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004259
4260 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004261}
4262
Tejun Heoc81c925a2013-12-06 15:11:56 -05004263/**
4264 * create_css - create a cgroup_subsys_state
4265 * @cgrp: the cgroup new css will be associated with
4266 * @ss: the subsys of new css
4267 *
4268 * Create a new css associated with @cgrp - @ss pair. On success, the new
4269 * css is online and installed in @cgrp with all interface files created.
4270 * Returns 0 on success, -errno on failure.
4271 */
4272static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4273{
Tejun Heod51f39b2014-05-16 13:22:48 -04004274 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004275 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004276 struct cgroup_subsys_state *css;
4277 int err;
4278
Tejun Heoc81c925a2013-12-06 15:11:56 -05004279 lockdep_assert_held(&cgroup_mutex);
4280
Tejun Heo1fed1b22014-05-16 13:22:49 -04004281 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004282 if (IS_ERR(css))
4283 return PTR_ERR(css);
4284
Tejun Heoddfcada2014-05-04 15:09:14 -04004285 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004286
Tejun Heoc81c925a2013-12-06 15:11:56 -05004287 err = percpu_ref_init(&css->refcnt, css_release);
4288 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004289 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004290
Tejun Heo15a4c832014-05-04 15:09:14 -04004291 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4292 if (err < 0)
4293 goto err_free_percpu_ref;
4294 css->id = err;
4295
Tejun Heoaec25022014-02-08 10:36:58 -05004296 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004297 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004298 goto err_free_id;
4299
4300 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004301 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004302 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004303
4304 err = online_css(css);
4305 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004306 goto err_list_del;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004307
Tejun Heoc81c925a2013-12-06 15:11:56 -05004308 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004309 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004310 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 -04004311 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004312 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004313 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004314 ss->warned_broken_hierarchy = true;
4315 }
4316
4317 return 0;
4318
Tejun Heo1fed1b22014-05-16 13:22:49 -04004319err_list_del:
4320 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004321 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004322err_free_id:
4323 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004324err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004325 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004326err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004327 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004328 return err;
4329}
4330
Tejun Heob3bfd982014-05-13 12:19:22 -04004331static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4332 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333{
Tejun Heoa9746d82014-05-13 12:19:22 -04004334 struct cgroup *parent, *cgrp;
4335 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004337 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004338 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004339
Tejun Heoa9746d82014-05-13 12:19:22 -04004340 parent = cgroup_kn_lock_live(parent_kn);
4341 if (!parent)
4342 return -ENODEV;
4343 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004344
4345 /* allocate the cgroup and its ID, 0 is reserved for the root */
4346 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4347 if (!cgrp) {
4348 ret = -ENOMEM;
4349 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004350 }
4351
Tejun Heo9d755d32014-05-14 09:15:02 -04004352 ret = percpu_ref_init(&cgrp->self.refcnt, css_release);
4353 if (ret)
4354 goto out_free_cgrp;
4355
Li Zefan0ab02ca2014-02-11 16:05:46 +08004356 /*
4357 * Temporarily set the pointer to NULL, so idr_find() won't return
4358 * a half-baked cgroup.
4359 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004360 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004361 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004362 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004363 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004364 }
4365
Paul Menagecc31edc2008-10-18 20:28:04 -07004366 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367
Tejun Heo9d800df2014-05-14 09:15:00 -04004368 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004369 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370
Li Zefanb6abdb02008-03-04 14:28:19 -08004371 if (notify_on_release(parent))
4372 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4373
Tejun Heo2260e7f2012-11-19 08:13:38 -08004374 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4375 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004376
Tejun Heo2bd59d42014-02-11 11:52:49 -05004377 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004378 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004379 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004380 ret = PTR_ERR(kn);
4381 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004382 }
4383 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384
Tejun Heo6f305582014-02-12 09:29:50 -05004385 /*
4386 * This extra ref will be put in cgroup_free_fn() and guarantees
4387 * that @cgrp->kn is always accessible.
4388 */
4389 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390
Tejun Heo0cb51d72014-05-16 13:22:49 -04004391 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004392
Tejun Heo4e139af2012-11-19 08:13:36 -08004393 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004394 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004395 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004396 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004397
Tejun Heo0d802552013-12-06 15:11:56 -05004398 /*
4399 * @cgrp is now fully operational. If something fails after this
4400 * point, it'll be released via the normal destruction path.
4401 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004402 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004403
Tejun Heoba0f4d72014-05-13 12:19:22 -04004404 ret = cgroup_kn_set_ugid(kn);
4405 if (ret)
4406 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004407
Tejun Heoba0f4d72014-05-13 12:19:22 -04004408 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4409 if (ret)
4410 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004411
Tejun Heo9d403e92013-12-06 15:11:56 -05004412 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004413 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004414 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004415 ret = create_css(cgrp, ss);
4416 if (ret)
4417 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004418 }
Tejun Heoa8638032012-11-09 09:12:29 -08004419 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004420
Tejun Heobd53d612014-04-23 11:13:16 -04004421 /*
4422 * On the default hierarchy, a child doesn't automatically inherit
4423 * child_subsys_mask from the parent. Each is configured manually.
4424 */
4425 if (!cgroup_on_dfl(cgrp))
4426 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004427
Tejun Heo2bd59d42014-02-11 11:52:49 -05004428 kernfs_activate(kn);
4429
Tejun Heoba0f4d72014-05-13 12:19:22 -04004430 ret = 0;
4431 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004432
Tejun Heoba0f4d72014-05-13 12:19:22 -04004433out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004434 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004435out_cancel_ref:
4436 percpu_ref_cancel_init(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004437out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004438 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004439out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004440 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004441 return ret;
4442
4443out_destroy:
4444 cgroup_destroy_locked(cgrp);
4445 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004446}
4447
Tejun Heo223dbc32013-08-13 20:22:50 -04004448/*
4449 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004450 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4451 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004452 */
4453static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004454{
Tejun Heo223dbc32013-08-13 20:22:50 -04004455 struct cgroup_subsys_state *css =
4456 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004457
Tejun Heof20104d2013-08-13 20:22:50 -04004458 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004459 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004460 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004461
Tejun Heo09a503ea2013-08-13 20:22:50 -04004462 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004463}
4464
Tejun Heo223dbc32013-08-13 20:22:50 -04004465/* css kill confirmation processing requires process context, bounce */
4466static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004467{
4468 struct cgroup_subsys_state *css =
4469 container_of(ref, struct cgroup_subsys_state, refcnt);
4470
Tejun Heo223dbc32013-08-13 20:22:50 -04004471 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004472 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004473}
4474
Tejun Heof392e512014-04-23 11:13:14 -04004475/**
4476 * kill_css - destroy a css
4477 * @css: css to destroy
4478 *
4479 * This function initiates destruction of @css by removing cgroup interface
4480 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004481 * asynchronously once css_tryget_online() is guaranteed to fail and when
4482 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004483 */
4484static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004485{
Tejun Heo01f64742014-05-13 12:19:23 -04004486 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004487
Tejun Heo2bd59d42014-02-11 11:52:49 -05004488 /*
4489 * This must happen before css is disassociated with its cgroup.
4490 * See seq_css() for details.
4491 */
Tejun Heoaec25022014-02-08 10:36:58 -05004492 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004493
Tejun Heoedae0c32013-08-13 20:22:51 -04004494 /*
4495 * Killing would put the base ref, but we need to keep it alive
4496 * until after ->css_offline().
4497 */
4498 css_get(css);
4499
4500 /*
4501 * cgroup core guarantees that, by the time ->css_offline() is
4502 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004503 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004504 * proceed to offlining css's because percpu_ref_kill() doesn't
4505 * guarantee that the ref is seen as killed on all CPUs on return.
4506 *
4507 * Use percpu_ref_kill_and_confirm() to get notifications as each
4508 * css is confirmed to be seen as killed on all CPUs.
4509 */
4510 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004511}
4512
4513/**
4514 * cgroup_destroy_locked - the first stage of cgroup destruction
4515 * @cgrp: cgroup to be destroyed
4516 *
4517 * css's make use of percpu refcnts whose killing latency shouldn't be
4518 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004519 * guarantee that css_tryget_online() won't succeed by the time
4520 * ->css_offline() is invoked. To satisfy all the requirements,
4521 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004522 *
4523 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4524 * userland visible parts and start killing the percpu refcnts of
4525 * css's. Set up so that the next stage will be kicked off once all
4526 * the percpu refcnts are confirmed to be killed.
4527 *
4528 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4529 * rest of destruction. Once all cgroup references are gone, the
4530 * cgroup is RCU-freed.
4531 *
4532 * This function implements s1. After this step, @cgrp is gone as far as
4533 * the userland is concerned and a new cgroup with the same name may be
4534 * created. As cgroup doesn't care about the names internally, this
4535 * doesn't cause any problem.
4536 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004537static int cgroup_destroy_locked(struct cgroup *cgrp)
4538 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004539{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004540 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004541 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004542 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004543
Tejun Heo42809dd2012-11-19 08:13:37 -08004544 lockdep_assert_held(&cgroup_mutex);
4545
Tejun Heoddd69142013-06-12 21:04:54 -07004546 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004547 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004548 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004549 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004550 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004551 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004552 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004553 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004554 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004555
Tejun Heo1a90dd52012-11-05 09:16:59 -08004556 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004557 * Make sure there's no live children. We can't test emptiness of
4558 * ->self.children as dead children linger on it while being
4559 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004560 */
Tejun Heof3d46502014-05-16 13:22:52 -04004561 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004562 return -EBUSY;
4563
4564 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004565 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004566 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004567 */
Tejun Heo184faf32014-05-16 13:22:51 -04004568 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004569
Tejun Heo249f3462014-05-14 09:15:01 -04004570 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004571 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004572 kill_css(css);
4573
Tejun Heo184faf32014-05-16 13:22:51 -04004574 /* CSS_ONLINE is clear, remove from ->release_list for the last time */
Tejun Heo455050d2013-06-13 19:27:41 -07004575 raw_spin_lock(&release_list_lock);
4576 if (!list_empty(&cgrp->release_list))
4577 list_del_init(&cgrp->release_list);
4578 raw_spin_unlock(&release_list_lock);
4579
4580 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004581 * Remove @cgrp directory along with the base files. @cgrp has an
4582 * extra ref on its kn.
4583 */
4584 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004585
Tejun Heod51f39b2014-05-16 13:22:48 -04004586 set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags);
4587 check_for_release(cgroup_parent(cgrp));
Tejun Heo9e4173e2014-05-14 09:15:01 -04004588
Tejun Heo249f3462014-05-14 09:15:01 -04004589 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004590 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo249f3462014-05-14 09:15:01 -04004591
Tejun Heoea15f8c2013-06-13 19:27:42 -07004592 return 0;
4593};
4594
Tejun Heo2bd59d42014-02-11 11:52:49 -05004595static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004596{
Tejun Heoa9746d82014-05-13 12:19:22 -04004597 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004598 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004599
Tejun Heoa9746d82014-05-13 12:19:22 -04004600 cgrp = cgroup_kn_lock_live(kn);
4601 if (!cgrp)
4602 return 0;
4603 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004604
Tejun Heoa9746d82014-05-13 12:19:22 -04004605 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004606
Tejun Heoa9746d82014-05-13 12:19:22 -04004607 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004608
4609 /*
4610 * There are two control paths which try to determine cgroup from
4611 * dentry without going through kernfs - cgroupstats_build() and
4612 * css_tryget_online_from_dir(). Those are supported by RCU
4613 * protecting clearing of cgrp->kn->priv backpointer, which should
4614 * happen after all files under it have been removed.
4615 */
4616 if (!ret)
4617 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4618
Tejun Heo2bd59d42014-02-11 11:52:49 -05004619 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004620 return ret;
4621}
4622
Tejun Heo2bd59d42014-02-11 11:52:49 -05004623static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4624 .remount_fs = cgroup_remount,
4625 .show_options = cgroup_show_options,
4626 .mkdir = cgroup_mkdir,
4627 .rmdir = cgroup_rmdir,
4628 .rename = cgroup_rename,
4629};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004630
Tejun Heo15a4c832014-05-04 15:09:14 -04004631static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004633 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004634
4635 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636
Tejun Heo648bb562012-11-19 08:13:36 -08004637 mutex_lock(&cgroup_mutex);
4638
Tejun Heo15a4c832014-05-04 15:09:14 -04004639 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004640 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004641
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004642 /* Create the root cgroup state for this subsystem */
4643 ss->root = &cgrp_dfl_root;
4644 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645 /* We don't handle early failures gracefully */
4646 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004647 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004648
4649 /*
4650 * Root csses are never destroyed and we can't initialize
4651 * percpu_ref during early init. Disable refcnting.
4652 */
4653 css->flags |= CSS_NO_REF;
4654
Tejun Heo15a4c832014-05-04 15:09:14 -04004655 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004656 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004657 css->id = 1;
4658 } else {
4659 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4660 BUG_ON(css->id < 0);
4661 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662
Li Zefane8d55fd2008-04-29 01:00:13 -07004663 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004664 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004665 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004666 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004667 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668
4669 need_forkexit_callback |= ss->fork || ss->exit;
4670
Li Zefane8d55fd2008-04-29 01:00:13 -07004671 /* At system boot, before all subsystems have been
4672 * registered, no tasks have been forked, so we don't
4673 * need to invoke fork callbacks here. */
4674 BUG_ON(!list_empty(&init_task.tasks));
4675
Tejun Heoae7f1642013-08-13 20:22:50 -04004676 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004677
Tejun Heof392e512014-04-23 11:13:14 -04004678 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004679
Ben Blume6a11052010-03-10 15:22:09 -08004680 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004681}
4682
4683/**
Li Zefana043e3b2008-02-23 15:24:09 -08004684 * cgroup_init_early - cgroup initialization at system boot
4685 *
4686 * Initialize cgroups at system boot, and initialize any
4687 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688 */
4689int __init cgroup_init_early(void)
4690{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004691 static struct cgroup_sb_opts __initdata opts =
4692 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004693 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004695
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004696 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004697 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4698
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004699 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004700
Tejun Heo3ed80a62014-02-08 10:36:58 -05004701 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004702 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004703 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4704 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004705 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004706 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4707 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004708
Tejun Heoaec25022014-02-08 10:36:58 -05004709 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004710 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004711
4712 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004713 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004714 }
4715 return 0;
4716}
4717
4718/**
Li Zefana043e3b2008-02-23 15:24:09 -08004719 * cgroup_init - cgroup initialization
4720 *
4721 * Register cgroup filesystem and /proc file, and initialize
4722 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004723 */
4724int __init cgroup_init(void)
4725{
Tejun Heo30159ec2013-06-25 11:53:37 -07004726 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004727 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004728 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004729
Tejun Heo2bd59d42014-02-11 11:52:49 -05004730 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004731
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004732 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004733
Tejun Heo82fe9b02013-06-25 11:53:37 -07004734 /* Add init_css_set to the hash table */
4735 key = css_set_hash(init_css_set.subsys);
4736 hash_add(css_set_table, &init_css_set.hlist, key);
4737
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004738 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004739
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004740 mutex_unlock(&cgroup_mutex);
4741
Tejun Heo172a2c062014-03-19 10:23:53 -04004742 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004743 if (ss->early_init) {
4744 struct cgroup_subsys_state *css =
4745 init_css_set.subsys[ss->id];
4746
4747 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4748 GFP_KERNEL);
4749 BUG_ON(css->id < 0);
4750 } else {
4751 cgroup_init_subsys(ss, false);
4752 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004753
Tejun Heo2d8f2432014-04-23 11:13:15 -04004754 list_add_tail(&init_css_set.e_cset_node[ssid],
4755 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4756
Tejun Heo172a2c062014-03-19 10:23:53 -04004757 /*
4758 * cftype registration needs kmalloc and can't be done
4759 * during early_init. Register base cftypes separately.
4760 */
4761 if (ss->base_cftypes)
4762 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4763 }
Greg KH676db4a2010-08-05 13:53:35 -07004764
Paul Menageddbcc7e2007-10-18 23:39:30 -07004765 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004766 if (!cgroup_kobj)
4767 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004768
4769 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004770 if (err < 0) {
4771 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004772 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004773 }
4774
4775 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004776 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004777}
Paul Menageb4f48b62007-10-18 23:39:33 -07004778
Tejun Heoe5fca242013-11-22 17:14:39 -05004779static int __init cgroup_wq_init(void)
4780{
4781 /*
4782 * There isn't much point in executing destruction path in
4783 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004784 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004785 *
4786 * We would prefer to do this in cgroup_init() above, but that
4787 * is called before init_workqueues(): so leave this until after.
4788 */
Tejun Heo1a115332014-02-12 19:06:19 -05004789 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004790 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004791
4792 /*
4793 * Used to destroy pidlists and separate to serve as flush domain.
4794 * Cap @max_active to 1 too.
4795 */
4796 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4797 0, 1);
4798 BUG_ON(!cgroup_pidlist_destroy_wq);
4799
Tejun Heoe5fca242013-11-22 17:14:39 -05004800 return 0;
4801}
4802core_initcall(cgroup_wq_init);
4803
Paul Menagea4243162007-10-18 23:39:35 -07004804/*
4805 * proc_cgroup_show()
4806 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4807 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004808 */
4809
4810/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004811int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004812{
4813 struct pid *pid;
4814 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004815 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004816 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004817 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004818
4819 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004820 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004821 if (!buf)
4822 goto out;
4823
4824 retval = -ESRCH;
4825 pid = m->private;
4826 tsk = get_pid_task(pid, PIDTYPE_PID);
4827 if (!tsk)
4828 goto out_free;
4829
4830 retval = 0;
4831
4832 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004833 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004834
Tejun Heo985ed672014-03-19 10:23:53 -04004835 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004836 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004837 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004838 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004839
Tejun Heoa2dd4242014-03-19 10:23:55 -04004840 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004841 continue;
4842
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004843 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004844 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004845 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004846 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004847 if (strlen(root->name))
4848 seq_printf(m, "%sname=%s", count ? "," : "",
4849 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004850 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004851 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004852 path = cgroup_path(cgrp, buf, PATH_MAX);
4853 if (!path) {
4854 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004855 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004856 }
4857 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004858 seq_putc(m, '\n');
4859 }
4860
4861out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004862 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004863 mutex_unlock(&cgroup_mutex);
4864 put_task_struct(tsk);
4865out_free:
4866 kfree(buf);
4867out:
4868 return retval;
4869}
4870
Paul Menagea4243162007-10-18 23:39:35 -07004871/* Display information about each subsystem and each hierarchy */
4872static int proc_cgroupstats_show(struct seq_file *m, void *v)
4873{
Tejun Heo30159ec2013-06-25 11:53:37 -07004874 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004875 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004876
Paul Menage8bab8dd2008-04-04 14:29:57 -07004877 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004878 /*
4879 * ideally we don't want subsystems moving around while we do this.
4880 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4881 * subsys/hierarchy state.
4882 */
Paul Menagea4243162007-10-18 23:39:35 -07004883 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004884
4885 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004886 seq_printf(m, "%s\t%d\t%d\t%d\n",
4887 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004888 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004889
Paul Menagea4243162007-10-18 23:39:35 -07004890 mutex_unlock(&cgroup_mutex);
4891 return 0;
4892}
4893
4894static int cgroupstats_open(struct inode *inode, struct file *file)
4895{
Al Viro9dce07f2008-03-29 03:07:28 +00004896 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004897}
4898
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004899static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004900 .open = cgroupstats_open,
4901 .read = seq_read,
4902 .llseek = seq_lseek,
4903 .release = single_release,
4904};
4905
Paul Menageb4f48b62007-10-18 23:39:33 -07004906/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004907 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004908 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004909 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004910 * A task is associated with the init_css_set until cgroup_post_fork()
4911 * attaches it to the parent's css_set. Empty cg_list indicates that
4912 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004913 */
4914void cgroup_fork(struct task_struct *child)
4915{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004916 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004917 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004918}
4919
4920/**
Li Zefana043e3b2008-02-23 15:24:09 -08004921 * cgroup_post_fork - called on a new task after adding it to the task list
4922 * @child: the task in question
4923 *
Tejun Heo5edee612012-10-16 15:03:14 -07004924 * Adds the task to the list running through its css_set if necessary and
4925 * call the subsystem fork() callbacks. Has to be after the task is
4926 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004927 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004928 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004929 */
Paul Menage817929e2007-10-18 23:39:36 -07004930void cgroup_post_fork(struct task_struct *child)
4931{
Tejun Heo30159ec2013-06-25 11:53:37 -07004932 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004933 int i;
4934
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004935 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004936 * This may race against cgroup_enable_task_cg_links(). As that
4937 * function sets use_task_css_set_links before grabbing
4938 * tasklist_lock and we just went through tasklist_lock to add
4939 * @child, it's guaranteed that either we see the set
4940 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4941 * @child during its iteration.
4942 *
4943 * If we won the race, @child is associated with %current's
4944 * css_set. Grabbing css_set_rwsem guarantees both that the
4945 * association is stable, and, on completion of the parent's
4946 * migration, @child is visible in the source of migration or
4947 * already in the destination cgroup. This guarantee is necessary
4948 * when implementing operations which need to migrate all tasks of
4949 * a cgroup to another.
4950 *
4951 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4952 * will remain in init_css_set. This is safe because all tasks are
4953 * in the init_css_set before cg_links is enabled and there's no
4954 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004955 */
Paul Menage817929e2007-10-18 23:39:36 -07004956 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004957 struct css_set *cset;
4958
Tejun Heo96d365e2014-02-13 06:58:40 -05004959 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004960 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004961 if (list_empty(&child->cg_list)) {
4962 rcu_assign_pointer(child->cgroups, cset);
4963 list_add(&child->cg_list, &cset->tasks);
4964 get_css_set(cset);
4965 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004966 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004967 }
Tejun Heo5edee612012-10-16 15:03:14 -07004968
4969 /*
4970 * Call ss->fork(). This must happen after @child is linked on
4971 * css_set; otherwise, @child might change state between ->fork()
4972 * and addition to css_set.
4973 */
4974 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004975 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004976 if (ss->fork)
4977 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004978 }
Paul Menage817929e2007-10-18 23:39:36 -07004979}
Tejun Heo5edee612012-10-16 15:03:14 -07004980
Paul Menage817929e2007-10-18 23:39:36 -07004981/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004982 * cgroup_exit - detach cgroup from exiting task
4983 * @tsk: pointer to task_struct of exiting process
4984 *
4985 * Description: Detach cgroup from @tsk and release it.
4986 *
4987 * Note that cgroups marked notify_on_release force every task in
4988 * them to take the global cgroup_mutex mutex when exiting.
4989 * This could impact scaling on very large systems. Be reluctant to
4990 * use notify_on_release cgroups where very high task exit scaling
4991 * is required on large systems.
4992 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004993 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4994 * call cgroup_exit() while the task is still competent to handle
4995 * notify_on_release(), then leave the task attached to the root cgroup in
4996 * each hierarchy for the remainder of its exit. No need to bother with
4997 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004998 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004999 */
Li Zefan1ec41832014-03-28 15:22:19 +08005000void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005001{
Tejun Heo30159ec2013-06-25 11:53:37 -07005002 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005003 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005004 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005005 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005006
5007 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005008 * Unlink from @tsk from its css_set. As migration path can't race
5009 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005010 */
5011 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005012 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005013 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005014 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005015 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005016 }
5017
Paul Menageb4f48b62007-10-18 23:39:33 -07005018 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005019 cset = task_css_set(tsk);
5020 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005021
Li Zefan1ec41832014-03-28 15:22:19 +08005022 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005023 /* see cgroup_post_fork() for details */
5024 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005025 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005026 struct cgroup_subsys_state *old_css = cset->subsys[i];
5027 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005028
Tejun Heoeb954192013-08-08 20:11:23 -04005029 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005030 }
5031 }
5032 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005033
Tejun Heoeaf797a2014-02-25 10:04:03 -05005034 if (put_cset)
5035 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005036}
Paul Menage697f4162007-10-18 23:39:34 -07005037
Paul Menagebd89aab2007-10-18 23:40:44 -07005038static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005039{
Tejun Heof3d46502014-05-16 13:22:52 -04005040 if (cgroup_is_releasable(cgrp) && list_empty(&cgrp->cset_links) &&
5041 !css_has_online_children(&cgrp->self)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005042 /*
5043 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005044 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005045 * it now
5046 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005048
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005049 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005050 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005051 list_empty(&cgrp->release_list)) {
5052 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005053 need_schedule_work = 1;
5054 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005055 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005056 if (need_schedule_work)
5057 schedule_work(&release_agent_work);
5058 }
5059}
5060
Paul Menage81a6a5c2007-10-18 23:39:38 -07005061/*
5062 * Notify userspace when a cgroup is released, by running the
5063 * configured release agent with the name of the cgroup (path
5064 * relative to the root of cgroup file system) as the argument.
5065 *
5066 * Most likely, this user command will try to rmdir this cgroup.
5067 *
5068 * This races with the possibility that some other task will be
5069 * attached to this cgroup before it is removed, or that some other
5070 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5071 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5072 * unused, and this cgroup will be reprieved from its death sentence,
5073 * to continue to serve a useful existence. Next time it's released,
5074 * we will get notified again, if it still has 'notify_on_release' set.
5075 *
5076 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5077 * means only wait until the task is successfully execve()'d. The
5078 * separate release agent task is forked by call_usermodehelper(),
5079 * then control in this thread returns here, without waiting for the
5080 * release agent task. We don't bother to wait because the caller of
5081 * this routine has no use for the exit status of the release agent
5082 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005083 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005084static void cgroup_release_agent(struct work_struct *work)
5085{
5086 BUG_ON(work != &release_agent_work);
5087 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005088 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005089 while (!list_empty(&release_list)) {
5090 char *argv[3], *envp[3];
5091 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005092 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005093 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005094 struct cgroup,
5095 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005096 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005097 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005098 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005099 if (!pathbuf)
5100 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005101 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5102 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005103 goto continue_free;
5104 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5105 if (!agentbuf)
5106 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005107
5108 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005109 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005110 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005111 argv[i] = NULL;
5112
5113 i = 0;
5114 /* minimal command environment */
5115 envp[i++] = "HOME=/";
5116 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5117 envp[i] = NULL;
5118
5119 /* Drop the lock while we invoke the usermode helper,
5120 * since the exec could involve hitting disk and hence
5121 * be a slow process */
5122 mutex_unlock(&cgroup_mutex);
5123 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005124 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005125 continue_free:
5126 kfree(pathbuf);
5127 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005128 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005129 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005130 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131 mutex_unlock(&cgroup_mutex);
5132}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005133
5134static int __init cgroup_disable(char *str)
5135{
Tejun Heo30159ec2013-06-25 11:53:37 -07005136 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005137 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005138 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005139
5140 while ((token = strsep(&str, ",")) != NULL) {
5141 if (!*token)
5142 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005143
Tejun Heo3ed80a62014-02-08 10:36:58 -05005144 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005145 if (!strcmp(token, ss->name)) {
5146 ss->disabled = 1;
5147 printk(KERN_INFO "Disabling %s control group"
5148 " subsystem\n", ss->name);
5149 break;
5150 }
5151 }
5152 }
5153 return 1;
5154}
5155__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005156
Tejun Heob77d7b62013-08-13 11:01:54 -04005157/**
Tejun Heoec903c02014-05-13 12:11:01 -04005158 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005159 * @dentry: directory dentry of interest
5160 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005161 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005162 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5163 * to get the corresponding css and return it. If such css doesn't exist
5164 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005165 */
Tejun Heoec903c02014-05-13 12:11:01 -04005166struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5167 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005168{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005169 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5170 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005171 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005172
Tejun Heo35cf0832013-08-26 18:40:56 -04005173 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005174 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5175 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005176 return ERR_PTR(-EBADF);
5177
Tejun Heo5a17f542014-02-11 11:52:47 -05005178 rcu_read_lock();
5179
Tejun Heo2bd59d42014-02-11 11:52:49 -05005180 /*
5181 * This path doesn't originate from kernfs and @kn could already
5182 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005183 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005184 */
5185 cgrp = rcu_dereference(kn->priv);
5186 if (cgrp)
5187 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005188
Tejun Heoec903c02014-05-13 12:11:01 -04005189 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005190 css = ERR_PTR(-ENOENT);
5191
5192 rcu_read_unlock();
5193 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005194}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005195
Li Zefan1cb650b2013-08-19 10:05:24 +08005196/**
5197 * css_from_id - lookup css by id
5198 * @id: the cgroup id
5199 * @ss: cgroup subsys to be looked into
5200 *
5201 * Returns the css if there's valid one with @id, otherwise returns NULL.
5202 * Should be called under rcu_read_lock().
5203 */
5204struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5205{
Tejun Heo6fa49182014-05-04 15:09:13 -04005206 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005207 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005208}
5209
Paul Menagefe693432009-09-23 15:56:20 -07005210#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005211static struct cgroup_subsys_state *
5212debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005213{
5214 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5215
5216 if (!css)
5217 return ERR_PTR(-ENOMEM);
5218
5219 return css;
5220}
5221
Tejun Heoeb954192013-08-08 20:11:23 -04005222static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005223{
Tejun Heoeb954192013-08-08 20:11:23 -04005224 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005225}
5226
Tejun Heo182446d2013-08-08 20:11:24 -04005227static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5228 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005229{
Tejun Heo182446d2013-08-08 20:11:24 -04005230 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005231}
5232
Tejun Heo182446d2013-08-08 20:11:24 -04005233static u64 current_css_set_read(struct cgroup_subsys_state *css,
5234 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005235{
5236 return (u64)(unsigned long)current->cgroups;
5237}
5238
Tejun Heo182446d2013-08-08 20:11:24 -04005239static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005240 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005241{
5242 u64 count;
5243
5244 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005245 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005246 rcu_read_unlock();
5247 return count;
5248}
5249
Tejun Heo2da8ca82013-12-05 12:28:04 -05005250static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005251{
Tejun Heo69d02062013-06-12 21:04:50 -07005252 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005253 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005254 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005255
Tejun Heoe61734c2014-02-12 09:29:50 -05005256 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5257 if (!name_buf)
5258 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005259
Tejun Heo96d365e2014-02-13 06:58:40 -05005260 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005261 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005262 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005263 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005264 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005265
Tejun Heoa2dd4242014-03-19 10:23:55 -04005266 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005267 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005268 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005269 }
5270 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005271 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005272 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005273 return 0;
5274}
5275
5276#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005277static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005278{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005279 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005280 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005281
Tejun Heo96d365e2014-02-13 06:58:40 -05005282 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005283 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005284 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005285 struct task_struct *task;
5286 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005287
Tejun Heo5abb8852013-06-12 21:04:49 -07005288 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005289
Tejun Heo5abb8852013-06-12 21:04:49 -07005290 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005291 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5292 goto overflow;
5293 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005294 }
Tejun Heoc7561122014-02-25 10:04:01 -05005295
5296 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5297 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5298 goto overflow;
5299 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5300 }
5301 continue;
5302 overflow:
5303 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005304 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005305 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005306 return 0;
5307}
5308
Tejun Heo182446d2013-08-08 20:11:24 -04005309static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005310{
Tejun Heo182446d2013-08-08 20:11:24 -04005311 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005312}
5313
5314static struct cftype debug_files[] = {
5315 {
Paul Menagefe693432009-09-23 15:56:20 -07005316 .name = "taskcount",
5317 .read_u64 = debug_taskcount_read,
5318 },
5319
5320 {
5321 .name = "current_css_set",
5322 .read_u64 = current_css_set_read,
5323 },
5324
5325 {
5326 .name = "current_css_set_refcount",
5327 .read_u64 = current_css_set_refcount_read,
5328 },
5329
5330 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005331 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005332 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005333 },
5334
5335 {
5336 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005337 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005338 },
5339
5340 {
Paul Menagefe693432009-09-23 15:56:20 -07005341 .name = "releasable",
5342 .read_u64 = releasable_read,
5343 },
Paul Menagefe693432009-09-23 15:56:20 -07005344
Tejun Heo4baf6e32012-04-01 12:09:55 -07005345 { } /* terminate */
5346};
Paul Menagefe693432009-09-23 15:56:20 -07005347
Tejun Heo073219e2014-02-08 10:36:58 -05005348struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005349 .css_alloc = debug_css_alloc,
5350 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005351 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005352};
5353#endif /* CONFIG_CGROUP_DEBUG */