blob: 21739e48100618f9c9ec936822d19a8e6868185a [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 Heoace2bee2014-02-11 11:52:47 -050074 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
75 * creation/removal and hierarchy changing operations including cgroup
76 * creation, removal, css association and controller rebinding. This outer
77 * lock is needed mainly to resolve the circular dependency between kernfs
78 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
79 */
80static DEFINE_MUTEX(cgroup_tree_mutex);
81
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082/*
83 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
85 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050086 * css_set_rwsem protects task->cgroups pointer, the list of css_set
87 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080088 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050089 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
90 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080091 */
Tejun Heo22194492013-04-07 09:29:51 -070092#ifdef CONFIG_PROVE_RCU
93DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050094DECLARE_RWSEM(css_set_rwsem);
95EXPORT_SYMBOL_GPL(cgroup_mutex);
96EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070097#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070098static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050099static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -0700100#endif
101
Tejun Heo69e943b2014-02-08 10:36:58 -0500102/*
Tejun Heo15a4c832014-05-04 15:09:14 -0400103 * Protects cgroup_idr and css_idr so that IDs can be released without
104 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -0400105 */
106static DEFINE_SPINLOCK(cgroup_idr_lock);
107
108/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500109 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
110 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
111 */
112static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700113
Tejun Heoace2bee2014-02-11 11:52:47 -0500114#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500115 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500116 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500117 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500118 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500119
Ben Blumaae8aab2010-03-10 15:22:07 -0800120/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500121 * cgroup destruction makes heavy use of work items and there can be a lot
122 * of concurrent destructions. Use a separate workqueue so that cgroup
123 * destruction work items don't end up filling up max_active of system_wq
124 * which may lead to deadlock.
125 */
126static struct workqueue_struct *cgroup_destroy_wq;
127
128/*
Tejun Heob1a21362013-11-29 10:42:58 -0500129 * pidlist destructions need to be flushed on cgroup destruction. Use a
130 * separate workqueue as flush domain.
131 */
132static struct workqueue_struct *cgroup_pidlist_destroy_wq;
133
Tejun Heo3ed80a62014-02-08 10:36:58 -0500134/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500135#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500136static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137#include <linux/cgroup_subsys.h>
138};
Tejun Heo073219e2014-02-08 10:36:58 -0500139#undef SUBSYS
140
141/* array of cgroup subsystem names */
142#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
143static const char *cgroup_subsys_name[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Paul Menageddbcc7e2007-10-18 23:39:30 -0700148/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400149 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700150 * unattached - it never has more than a single cgroup, and all tasks are
151 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400153struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700154
Tejun Heoa2dd4242014-03-19 10:23:55 -0400155/*
156 * The default hierarchy always exists but is hidden until mounted for the
157 * first time. This is for backward compatibility.
158 */
159static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160
161/* The list of hierarchy roots */
162
Tejun Heo9871bf92013-06-24 15:21:47 -0700163static LIST_HEAD(cgroup_roots);
164static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165
Tejun Heo3417ae12014-02-08 10:37:01 -0500166/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700167static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700168
Li Zefan794611a2013-06-18 18:53:53 +0800169/*
170 * Assign a monotonically increasing serial number to cgroups. It
171 * guarantees cgroups with bigger numbers are newer than those with smaller
172 * numbers. Also, as cgroups are always appended to the parent's
173 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700174 * the ascending serial number order on the list. Protected by
175 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800176 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700177static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800178
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800180 * check for fork/exit handlers to call. This avoids us having to do
181 * extra work in the fork/exit path if none of the subsystems need to
182 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700184static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185
Tejun Heo628f7cd2013-06-28 16:24:11 -0700186static struct cftype cgroup_base_files[];
187
Tejun Heo59f52962014-02-11 11:52:49 -0500188static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400189static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400190 unsigned int ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400191static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800192static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400193static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
194static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400195static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
196 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500197static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800198
Tejun Heo6fa49182014-05-04 15:09:13 -0400199/* IDR wrappers which synchronize using cgroup_idr_lock */
200static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
201 gfp_t gfp_mask)
202{
203 int ret;
204
205 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400206 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400207 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400208 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400209 idr_preload_end();
210 return ret;
211}
212
213static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
214{
215 void *ret;
216
Tejun Heo54504e92014-05-13 12:10:59 -0400217 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400218 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400219 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400220 return ret;
221}
222
223static void cgroup_idr_remove(struct idr *idr, int id)
224{
Tejun Heo54504e92014-05-13 12:10:59 -0400225 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400226 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400227 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400228}
229
Tejun Heo95109b62013-08-08 20:11:27 -0400230/**
231 * cgroup_css - obtain a cgroup's css for the specified subsystem
232 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400233 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400234 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400235 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
236 * function must be called either under cgroup_mutex or rcu_read_lock() and
237 * the caller is responsible for pinning the returned css if it wants to
238 * keep accessing it outside the said locks. This function may return
239 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400240 */
241static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400242 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400243{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400244 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500245 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500246 lockdep_is_held(&cgroup_tree_mutex) ||
247 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400248 else
249 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400250}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700251
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400252/**
253 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
254 * @cgrp: the cgroup of interest
255 * @ss: the subsystem of interest (%NULL returns the dummy_css)
256 *
257 * Similar to cgroup_css() but returns the effctive css, which is defined
258 * as the matching css of the nearest ancestor including self which has @ss
259 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
260 * function is guaranteed to return non-NULL css.
261 */
262static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
263 struct cgroup_subsys *ss)
264{
265 lockdep_assert_held(&cgroup_mutex);
266
267 if (!ss)
268 return &cgrp->dummy_css;
269
270 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
271 return NULL;
272
273 while (cgrp->parent &&
274 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
275 cgrp = cgrp->parent;
276
277 return cgroup_css(cgrp, ss);
278}
279
Paul Menageddbcc7e2007-10-18 23:39:30 -0700280/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700281static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700282{
Tejun Heo54766d42013-06-12 21:04:53 -0700283 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284}
285
Tejun Heob4168642014-05-13 12:16:21 -0400286struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500287{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500288 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400289 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500290
291 /*
292 * This is open and unprotected implementation of cgroup_css().
293 * seq_css() is only called from a kernfs file operation which has
294 * an active reference on the file. Because all the subsystem
295 * files are drained before a css is disassociated with a cgroup,
296 * the matching css from the cgroup's subsys table is guaranteed to
297 * be and stay valid until the enclosing operation is complete.
298 */
299 if (cft->ss)
300 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
301 else
302 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500303}
Tejun Heob4168642014-05-13 12:16:21 -0400304EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500305
Li Zefan78574cf2013-04-08 19:00:38 -0700306/**
307 * cgroup_is_descendant - test ancestry
308 * @cgrp: the cgroup to be tested
309 * @ancestor: possible ancestor of @cgrp
310 *
311 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
312 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
313 * and @ancestor are accessible.
314 */
315bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
316{
317 while (cgrp) {
318 if (cgrp == ancestor)
319 return true;
320 cgrp = cgrp->parent;
321 }
322 return false;
323}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700324
Adrian Bunke9685a02008-02-07 00:13:46 -0800325static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700326{
327 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700328 (1 << CGRP_RELEASABLE) |
329 (1 << CGRP_NOTIFY_ON_RELEASE);
330 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331}
332
Adrian Bunke9685a02008-02-07 00:13:46 -0800333static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334{
Paul Menagebd89aab2007-10-18 23:40:44 -0700335 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336}
337
Tejun Heo30159ec2013-06-25 11:53:37 -0700338/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500339 * for_each_css - iterate all css's of a cgroup
340 * @css: the iteration cursor
341 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
342 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700343 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400344 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700345 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500346#define for_each_css(css, ssid, cgrp) \
347 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
348 if (!((css) = rcu_dereference_check( \
349 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500350 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500351 lockdep_is_held(&cgroup_mutex)))) { } \
352 else
353
354/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400355 * for_each_e_css - iterate all effective css's of a cgroup
356 * @css: the iteration cursor
357 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
358 * @cgrp: the target cgroup to iterate css's of
359 *
360 * Should be called under cgroup_[tree_]mutex.
361 */
362#define for_each_e_css(css, ssid, cgrp) \
363 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
364 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
365 ; \
366 else
367
368/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500369 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700370 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500371 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700372 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500373#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500374 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
375 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700376
Tejun Heo985ed672014-03-19 10:23:53 -0400377/* iterate across the hierarchies */
378#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700379 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700380
Tejun Heof8f22e52014-04-23 11:13:16 -0400381/* iterate over child cgrps, lock should be held throughout iteration */
382#define cgroup_for_each_live_child(child, cgrp) \
383 list_for_each_entry((child), &(cgrp)->children, sibling) \
384 if (({ lockdep_assert_held(&cgroup_tree_mutex); \
385 cgroup_is_dead(child); })) \
386 ; \
387 else
388
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700389/**
390 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
391 * @cgrp: the cgroup to be checked for liveness
392 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700393 * On success, returns true; the mutex should be later unlocked. On
394 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700395 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700396static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700397{
398 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700399 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700400 mutex_unlock(&cgroup_mutex);
401 return false;
402 }
403 return true;
404}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700405
Paul Menage81a6a5c2007-10-18 23:39:38 -0700406/* the list of cgroups eligible for automatic release. Protected by
407 * release_list_lock */
408static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200409static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700410static void cgroup_release_agent(struct work_struct *work);
411static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700412static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413
Tejun Heo69d02062013-06-12 21:04:50 -0700414/*
415 * A cgroup can be associated with multiple css_sets as different tasks may
416 * belong to different cgroups on different hierarchies. In the other
417 * direction, a css_set is naturally associated with multiple cgroups.
418 * This M:N relationship is represented by the following link structure
419 * which exists for each association and allows traversing the associations
420 * from both sides.
421 */
422struct cgrp_cset_link {
423 /* the cgroup and css_set this link associates */
424 struct cgroup *cgrp;
425 struct css_set *cset;
426
427 /* list of cgrp_cset_links anchored at cgrp->cset_links */
428 struct list_head cset_link;
429
430 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
431 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700432};
433
Tejun Heo172a2c062014-03-19 10:23:53 -0400434/*
435 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700436 * hierarchies being mounted. It contains a pointer to the root state
437 * for each subsystem. Also used to anchor the list of css_sets. Not
438 * reference-counted, to improve performance when child cgroups
439 * haven't been created.
440 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400441struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400442 .refcount = ATOMIC_INIT(1),
443 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
444 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
445 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
446 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
447 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
448};
Paul Menage817929e2007-10-18 23:39:36 -0700449
Tejun Heo172a2c062014-03-19 10:23:53 -0400450static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700451
Tejun Heo842b5972014-04-25 18:28:02 -0400452/**
453 * cgroup_update_populated - updated populated count of a cgroup
454 * @cgrp: the target cgroup
455 * @populated: inc or dec populated count
456 *
457 * @cgrp is either getting the first task (css_set) or losing the last.
458 * Update @cgrp->populated_cnt accordingly. The count is propagated
459 * towards root so that a given cgroup's populated_cnt is zero iff the
460 * cgroup and all its descendants are empty.
461 *
462 * @cgrp's interface file "cgroup.populated" is zero if
463 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
464 * changes from or to zero, userland is notified that the content of the
465 * interface file has changed. This can be used to detect when @cgrp and
466 * its descendants become populated or empty.
467 */
468static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
469{
470 lockdep_assert_held(&css_set_rwsem);
471
472 do {
473 bool trigger;
474
475 if (populated)
476 trigger = !cgrp->populated_cnt++;
477 else
478 trigger = !--cgrp->populated_cnt;
479
480 if (!trigger)
481 break;
482
483 if (cgrp->populated_kn)
484 kernfs_notify(cgrp->populated_kn);
485 cgrp = cgrp->parent;
486 } while (cgrp);
487}
488
Paul Menage7717f7b2009-09-23 15:56:22 -0700489/*
490 * hash table for cgroup groups. This improves the performance to find
491 * an existing css_set. This hash doesn't (currently) take into
492 * account cgroups in empty hierarchies.
493 */
Li Zefan472b1052008-04-29 01:00:11 -0700494#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800495static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700496
Li Zefan0ac801f2013-01-10 11:49:27 +0800497static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700498{
Li Zefan0ac801f2013-01-10 11:49:27 +0800499 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700500 struct cgroup_subsys *ss;
501 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700502
Tejun Heo30159ec2013-06-25 11:53:37 -0700503 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800504 key += (unsigned long)css[i];
505 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700506
Li Zefan0ac801f2013-01-10 11:49:27 +0800507 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700508}
509
Tejun Heo89c55092014-02-13 06:58:40 -0500510static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700511{
Tejun Heo69d02062013-06-12 21:04:50 -0700512 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400513 struct cgroup_subsys *ss;
514 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700515
Tejun Heo89c55092014-02-13 06:58:40 -0500516 lockdep_assert_held(&css_set_rwsem);
517
518 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700519 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700520
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700521 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400522 for_each_subsys(ss, ssid)
523 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700524 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700525 css_set_count--;
526
Tejun Heo69d02062013-06-12 21:04:50 -0700527 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700528 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700529
Tejun Heo69d02062013-06-12 21:04:50 -0700530 list_del(&link->cset_link);
531 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800532
Tejun Heo96d365e2014-02-13 06:58:40 -0500533 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400534 if (list_empty(&cgrp->cset_links)) {
535 cgroup_update_populated(cgrp, false);
536 if (notify_on_release(cgrp)) {
537 if (taskexit)
538 set_bit(CGRP_RELEASABLE, &cgrp->flags);
539 check_for_release(cgrp);
540 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700541 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700542
543 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700544 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700545
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700547}
548
Tejun Heo89c55092014-02-13 06:58:40 -0500549static void put_css_set(struct css_set *cset, bool taskexit)
550{
551 /*
552 * Ensure that the refcount doesn't hit zero while any readers
553 * can see it. Similar to atomic_dec_and_lock(), but for an
554 * rwlock
555 */
556 if (atomic_add_unless(&cset->refcount, -1, 1))
557 return;
558
559 down_write(&css_set_rwsem);
560 put_css_set_locked(cset, taskexit);
561 up_write(&css_set_rwsem);
562}
563
Paul Menage817929e2007-10-18 23:39:36 -0700564/*
565 * refcounted get/put for css_set objects
566 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700567static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700568{
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700570}
571
Tejun Heob326f9d2013-06-24 15:21:48 -0700572/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700573 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700574 * @cset: candidate css_set being tested
575 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700576 * @new_cgrp: cgroup that's being entered by the task
577 * @template: desired set of css pointers in css_set (pre-calculated)
578 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800579 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700580 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
581 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700582static bool compare_css_sets(struct css_set *cset,
583 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700584 struct cgroup *new_cgrp,
585 struct cgroup_subsys_state *template[])
586{
587 struct list_head *l1, *l2;
588
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400589 /*
590 * On the default hierarchy, there can be csets which are
591 * associated with the same set of cgroups but different csses.
592 * Let's first ensure that csses match.
593 */
594 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700595 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700596
597 /*
598 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400599 * different cgroups in hierarchies. As different cgroups may
600 * share the same effective css, this comparison is always
601 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700602 */
Tejun Heo69d02062013-06-12 21:04:50 -0700603 l1 = &cset->cgrp_links;
604 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700605 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700606 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700607 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700608
609 l1 = l1->next;
610 l2 = l2->next;
611 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700612 if (l1 == &cset->cgrp_links) {
613 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700614 break;
615 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700616 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700617 }
618 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700619 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
620 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
621 cgrp1 = link1->cgrp;
622 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700623 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700624 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700625
626 /*
627 * If this hierarchy is the hierarchy of the cgroup
628 * that's changing, then we need to check that this
629 * css_set points to the new cgroup; if it's any other
630 * hierarchy, then this css_set should point to the
631 * same cgroup as the old css_set.
632 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700633 if (cgrp1->root == new_cgrp->root) {
634 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700635 return false;
636 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700637 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700638 return false;
639 }
640 }
641 return true;
642}
643
Tejun Heob326f9d2013-06-24 15:21:48 -0700644/**
645 * find_existing_css_set - init css array and find the matching css_set
646 * @old_cset: the css_set that we're using before the cgroup transition
647 * @cgrp: the cgroup that we're moving into
648 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700649 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700650static struct css_set *find_existing_css_set(struct css_set *old_cset,
651 struct cgroup *cgrp,
652 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700653{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400654 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700655 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800657 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700658 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700659
Ben Blumaae8aab2010-03-10 15:22:07 -0800660 /*
661 * Build the set of subsystem state objects that we want to see in the
662 * new css_set. while subsystems can change globally, the entries here
663 * won't change, so no need for locking.
664 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700665 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400666 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400667 /*
668 * @ss is in this hierarchy, so we want the
669 * effective css from @cgrp.
670 */
671 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700672 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400673 /*
674 * @ss is not in this hierarchy, so we don't want
675 * to change the css.
676 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700678 }
679 }
680
Li Zefan0ac801f2013-01-10 11:49:27 +0800681 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 hash_for_each_possible(css_set_table, cset, hlist, key) {
683 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700684 continue;
685
686 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700688 }
Paul Menage817929e2007-10-18 23:39:36 -0700689
690 /* No existing cgroup group matched */
691 return NULL;
692}
693
Tejun Heo69d02062013-06-12 21:04:50 -0700694static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700695{
Tejun Heo69d02062013-06-12 21:04:50 -0700696 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700697
Tejun Heo69d02062013-06-12 21:04:50 -0700698 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
699 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700700 kfree(link);
701 }
702}
703
Tejun Heo69d02062013-06-12 21:04:50 -0700704/**
705 * allocate_cgrp_cset_links - allocate cgrp_cset_links
706 * @count: the number of links to allocate
707 * @tmp_links: list_head the allocated links are put on
708 *
709 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
710 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700711 */
Tejun Heo69d02062013-06-12 21:04:50 -0700712static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700713{
Tejun Heo69d02062013-06-12 21:04:50 -0700714 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700715 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700716
717 INIT_LIST_HEAD(tmp_links);
718
Li Zefan36553432008-07-29 22:33:19 -0700719 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700720 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700721 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700722 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700723 return -ENOMEM;
724 }
Tejun Heo69d02062013-06-12 21:04:50 -0700725 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700726 }
727 return 0;
728}
729
Li Zefanc12f65d2009-01-07 18:07:42 -0800730/**
731 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700732 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700733 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800734 * @cgrp: the destination cgroup
735 */
Tejun Heo69d02062013-06-12 21:04:50 -0700736static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
737 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800738{
Tejun Heo69d02062013-06-12 21:04:50 -0700739 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800740
Tejun Heo69d02062013-06-12 21:04:50 -0700741 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400742
743 if (cgroup_on_dfl(cgrp))
744 cset->dfl_cgrp = cgrp;
745
Tejun Heo69d02062013-06-12 21:04:50 -0700746 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
747 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700748 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400749
750 if (list_empty(&cgrp->cset_links))
751 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700752 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400753
Paul Menage7717f7b2009-09-23 15:56:22 -0700754 /*
755 * Always add links to the tail of the list so that the list
756 * is sorted by order of hierarchy creation
757 */
Tejun Heo69d02062013-06-12 21:04:50 -0700758 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800759}
760
Tejun Heob326f9d2013-06-24 15:21:48 -0700761/**
762 * find_css_set - return a new css_set with one cgroup updated
763 * @old_cset: the baseline css_set
764 * @cgrp: the cgroup to be updated
765 *
766 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
767 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700768 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700769static struct css_set *find_css_set(struct css_set *old_cset,
770 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700771{
Tejun Heob326f9d2013-06-24 15:21:48 -0700772 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700773 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700774 struct list_head tmp_links;
775 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400776 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800777 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400778 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700779
Tejun Heob326f9d2013-06-24 15:21:48 -0700780 lockdep_assert_held(&cgroup_mutex);
781
Paul Menage817929e2007-10-18 23:39:36 -0700782 /* First see if we already have a cgroup group that matches
783 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500784 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700785 cset = find_existing_css_set(old_cset, cgrp, template);
786 if (cset)
787 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500788 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700789
Tejun Heo5abb8852013-06-12 21:04:49 -0700790 if (cset)
791 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700792
Tejun Heof4f4be22013-06-12 21:04:51 -0700793 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700794 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700795 return NULL;
796
Tejun Heo69d02062013-06-12 21:04:50 -0700797 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700798 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700799 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700800 return NULL;
801 }
802
Tejun Heo5abb8852013-06-12 21:04:49 -0700803 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700804 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700805 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500806 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500807 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500808 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700809 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700810
811 /* Copy the set of subsystem state objects generated in
812 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700813 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700814
Tejun Heo96d365e2014-02-13 06:58:40 -0500815 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700816 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700817 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700818 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700819
Paul Menage7717f7b2009-09-23 15:56:22 -0700820 if (c->root == cgrp->root)
821 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700822 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700823 }
Paul Menage817929e2007-10-18 23:39:36 -0700824
Tejun Heo69d02062013-06-12 21:04:50 -0700825 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700826
Paul Menage817929e2007-10-18 23:39:36 -0700827 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700828
Tejun Heo2d8f2432014-04-23 11:13:15 -0400829 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700830 key = css_set_hash(cset->subsys);
831 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700832
Tejun Heo2d8f2432014-04-23 11:13:15 -0400833 for_each_subsys(ss, ssid)
834 list_add_tail(&cset->e_cset_node[ssid],
835 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
836
Tejun Heo96d365e2014-02-13 06:58:40 -0500837 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700838
Tejun Heo5abb8852013-06-12 21:04:49 -0700839 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700840}
841
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400842static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700843{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400844 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500845
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400846 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500847}
848
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400849static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500850{
851 int id;
852
853 lockdep_assert_held(&cgroup_mutex);
854
Tejun Heo985ed672014-03-19 10:23:53 -0400855 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500856 if (id < 0)
857 return id;
858
859 root->hierarchy_id = id;
860 return 0;
861}
862
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400863static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500864{
865 lockdep_assert_held(&cgroup_mutex);
866
867 if (root->hierarchy_id) {
868 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
869 root->hierarchy_id = 0;
870 }
871}
872
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400873static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500874{
875 if (root) {
876 /* hierarhcy ID shoulid already have been released */
877 WARN_ON_ONCE(root->hierarchy_id);
878
879 idr_destroy(&root->cgroup_idr);
880 kfree(root);
881 }
882}
883
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400884static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500885{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400886 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500887 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500888
Tejun Heo2bd59d42014-02-11 11:52:49 -0500889 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500890 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500891
Tejun Heo776f02f2014-02-12 09:29:50 -0500892 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500893 BUG_ON(!list_empty(&cgrp->children));
894
Tejun Heof2e85d52014-02-11 11:52:49 -0500895 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400896 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500897
898 /*
899 * Release all the links from cset_links to this hierarchy's
900 * root cgroup
901 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500902 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500903
904 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
905 list_del(&link->cset_link);
906 list_del(&link->cgrp_link);
907 kfree(link);
908 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500909 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500910
911 if (!list_empty(&root->root_list)) {
912 list_del(&root->root_list);
913 cgroup_root_count--;
914 }
915
916 cgroup_exit_root_id(root);
917
918 mutex_unlock(&cgroup_mutex);
919 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500920
Tejun Heo2bd59d42014-02-11 11:52:49 -0500921 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500922 cgroup_free_root(root);
923}
924
Tejun Heoceb6a082014-02-25 10:04:02 -0500925/* look up cgroup associated with given css_set on the specified hierarchy */
926static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400927 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700928{
Paul Menage7717f7b2009-09-23 15:56:22 -0700929 struct cgroup *res = NULL;
930
Tejun Heo96d365e2014-02-13 06:58:40 -0500931 lockdep_assert_held(&cgroup_mutex);
932 lockdep_assert_held(&css_set_rwsem);
933
Tejun Heo5abb8852013-06-12 21:04:49 -0700934 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400935 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700936 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700937 struct cgrp_cset_link *link;
938
939 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700940 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700941
Paul Menage7717f7b2009-09-23 15:56:22 -0700942 if (c->root == root) {
943 res = c;
944 break;
945 }
946 }
947 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500948
Paul Menage7717f7b2009-09-23 15:56:22 -0700949 BUG_ON(!res);
950 return res;
951}
952
953/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500954 * Return the cgroup for "task" from the given hierarchy. Must be
955 * called with cgroup_mutex and css_set_rwsem held.
956 */
957static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400958 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500959{
960 /*
961 * No need to lock the task - since we hold cgroup_mutex the
962 * task can't change groups, so the only thing that can happen
963 * is that it exits and its css is set back to init_css_set.
964 */
965 return cset_cgroup_from_root(task_css_set(task), root);
966}
967
968/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969 * A task must hold cgroup_mutex to modify cgroups.
970 *
971 * Any task can increment and decrement the count field without lock.
972 * So in general, code holding cgroup_mutex can't rely on the count
973 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800974 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975 * means that no tasks are currently attached, therefore there is no
976 * way a task attached to that cgroup can fork (the other way to
977 * increment the count). So code holding cgroup_mutex can safely
978 * assume that if the count is zero, it will stay zero. Similarly, if
979 * a task holds cgroup_mutex on a cgroup with zero count, it
980 * knows that the cgroup won't be removed, as cgroup_rmdir()
981 * needs that mutex.
982 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
984 * (usually) take cgroup_mutex. These are the two most performance
985 * critical pieces of code here. The exception occurs on cgroup_exit(),
986 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
987 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800988 * to the release agent with the name of the cgroup (path relative to
989 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 *
991 * A cgroup can only be deleted if both its 'count' of using tasks
992 * is zero, and its list of 'children' cgroups is empty. Since all
993 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400994 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400996 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 *
998 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800999 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 */
1001
Tejun Heo69dfa002014-05-04 15:09:13 -04001002static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001003static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001004static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001005
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001006static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1007 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001009 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1010 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1011 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1012 cft->ss->name, cft->name);
1013 else
1014 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1015 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016}
1017
Tejun Heof2e85d52014-02-11 11:52:49 -05001018/**
1019 * cgroup_file_mode - deduce file mode of a control file
1020 * @cft: the control file in question
1021 *
1022 * returns cft->mode if ->mode is not 0
1023 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1024 * returns S_IRUGO if it has only a read handler
1025 * returns S_IWUSR if it has only a write hander
1026 */
1027static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001028{
Tejun Heof2e85d52014-02-11 11:52:49 -05001029 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001030
Tejun Heof2e85d52014-02-11 11:52:49 -05001031 if (cft->mode)
1032 return cft->mode;
1033
1034 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1035 mode |= S_IRUGO;
1036
Tejun Heo6770c642014-05-13 12:16:21 -04001037 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001038 mode |= S_IWUSR;
1039
1040 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001041}
1042
Li Zefanbe445622013-01-24 14:31:42 +08001043static void cgroup_free_fn(struct work_struct *work)
1044{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001045 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001046
Tejun Heo3c9c8252014-02-12 09:29:50 -05001047 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001048 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001049
Tejun Heo776f02f2014-02-12 09:29:50 -05001050 if (cgrp->parent) {
1051 /*
1052 * We get a ref to the parent, and put the ref when this
1053 * cgroup is being freed, so it's guaranteed that the
1054 * parent won't be destroyed before its children.
1055 */
1056 cgroup_put(cgrp->parent);
1057 kernfs_put(cgrp->kn);
1058 kfree(cgrp);
1059 } else {
1060 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001061 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001062 * indicates that the root should be released.
1063 */
1064 cgroup_destroy_root(cgrp->root);
1065 }
Li Zefanbe445622013-01-24 14:31:42 +08001066}
1067
1068static void cgroup_free_rcu(struct rcu_head *head)
1069{
1070 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1071
Tejun Heoea15f8c2013-06-13 19:27:42 -07001072 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001073 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001074}
1075
Tejun Heo59f52962014-02-11 11:52:49 -05001076static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001078 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1079 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1080 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081}
1082
Tejun Heo59f52962014-02-11 11:52:49 -05001083static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001085 if (!atomic_dec_and_test(&cgrp->refcnt))
1086 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001087 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001088 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089
Tejun Heo6fa49182014-05-04 15:09:13 -04001090 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001091 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092
Tejun Heo2bd59d42014-02-11 11:52:49 -05001093 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094}
1095
Tejun Heoa9746d82014-05-13 12:19:22 -04001096/**
1097 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1098 * @kn: the kernfs_node being serviced
1099 *
1100 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1101 * the method finishes if locking succeeded. Note that once this function
1102 * returns the cgroup returned by cgroup_kn_lock_live() may become
1103 * inaccessible any time. If the caller intends to continue to access the
1104 * cgroup, it should pin it before invoking this function.
1105 */
1106static void cgroup_kn_unlock(struct kernfs_node *kn)
1107{
1108 struct cgroup *cgrp;
1109
1110 if (kernfs_type(kn) == KERNFS_DIR)
1111 cgrp = kn->priv;
1112 else
1113 cgrp = kn->parent->priv;
1114
1115 mutex_unlock(&cgroup_mutex);
1116 mutex_unlock(&cgroup_tree_mutex);
1117
1118 kernfs_unbreak_active_protection(kn);
1119 cgroup_put(cgrp);
1120}
1121
1122/**
1123 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1124 * @kn: the kernfs_node being serviced
1125 *
1126 * This helper is to be used by a cgroup kernfs method currently servicing
1127 * @kn. It breaks the active protection, performs cgroup locking and
1128 * verifies that the associated cgroup is alive. Returns the cgroup if
1129 * alive; otherwise, %NULL. A successful return should be undone by a
1130 * matching cgroup_kn_unlock() invocation.
1131 *
1132 * Any cgroup kernfs method implementation which requires locking the
1133 * associated cgroup should use this helper. It avoids nesting cgroup
1134 * locking under kernfs active protection and allows all kernfs operations
1135 * including self-removal.
1136 */
1137static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1138{
1139 struct cgroup *cgrp;
1140
1141 if (kernfs_type(kn) == KERNFS_DIR)
1142 cgrp = kn->priv;
1143 else
1144 cgrp = kn->parent->priv;
1145
1146 /*
1147 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
1148 * active_ref. cgroup liveliness check alone provides enough
1149 * protection against removal. Ensure @cgrp stays accessible and
1150 * break the active_ref protection.
1151 */
1152 cgroup_get(cgrp);
1153 kernfs_break_active_protection(kn);
1154
1155 mutex_lock(&cgroup_tree_mutex);
1156 mutex_lock(&cgroup_mutex);
1157
1158 if (!cgroup_is_dead(cgrp))
1159 return cgrp;
1160
1161 cgroup_kn_unlock(kn);
1162 return NULL;
1163}
1164
Li Zefan2739d3c2013-01-21 18:18:33 +08001165static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001166{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001167 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001168
Tejun Heoace2bee2014-02-11 11:52:47 -05001169 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001170 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001171}
1172
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001173/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001174 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001175 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001176 * @subsys_mask: mask of the subsystem ids whose files should be removed
1177 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001178static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001179{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001180 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001181 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001182
Tejun Heob420ba72013-07-12 12:34:02 -07001183 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001184 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001185
Tejun Heo69dfa002014-05-04 15:09:13 -04001186 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001187 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001188 list_for_each_entry(cfts, &ss->cfts, node)
1189 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001190 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001191}
1192
Tejun Heo69dfa002014-05-04 15:09:13 -04001193static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001194{
Tejun Heo30159ec2013-06-25 11:53:37 -07001195 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001196 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001197
Tejun Heoace2bee2014-02-11 11:52:47 -05001198 lockdep_assert_held(&cgroup_tree_mutex);
1199 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001200
Tejun Heo5df36032014-03-19 10:23:54 -04001201 for_each_subsys(ss, ssid) {
1202 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001203 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001204
Tejun Heo7fd8c562014-04-23 11:13:16 -04001205 /* if @ss has non-root csses attached to it, can't move */
1206 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001207 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001208
Tejun Heo5df36032014-03-19 10:23:54 -04001209 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001210 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001211 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 }
1213
Tejun Heoa2dd4242014-03-19 10:23:55 -04001214 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1215 if (ret) {
1216 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001217 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001218
Tejun Heoa2dd4242014-03-19 10:23:55 -04001219 /*
1220 * Rebinding back to the default root is not allowed to
1221 * fail. Using both default and non-default roots should
1222 * be rare. Moving subsystems back and forth even more so.
1223 * Just warn about it and continue.
1224 */
1225 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001226 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001227 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001228 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001229 }
Tejun Heo5df36032014-03-19 10:23:54 -04001230 }
Tejun Heo31261212013-06-28 17:07:30 -07001231
1232 /*
1233 * Nothing can fail from this point on. Remove files for the
1234 * removed subsystems and rebind each subsystem.
1235 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001236 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001237 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001238 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001239 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001240 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001241
Tejun Heo5df36032014-03-19 10:23:54 -04001242 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001243 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001244 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001245 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001246
Tejun Heo5df36032014-03-19 10:23:54 -04001247 if (!(ss_mask & (1 << ssid)))
1248 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001249
Tejun Heo5df36032014-03-19 10:23:54 -04001250 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001251 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001252
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001253 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001254
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001255 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1256 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001257 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001258 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001259
Tejun Heo2d8f2432014-04-23 11:13:15 -04001260 down_write(&css_set_rwsem);
1261 hash_for_each(css_set_table, i, cset, hlist)
1262 list_move_tail(&cset->e_cset_node[ss->id],
1263 &dst_root->cgrp.e_csets[ss->id]);
1264 up_write(&css_set_rwsem);
1265
Tejun Heof392e512014-04-23 11:13:14 -04001266 src_root->subsys_mask &= ~(1 << ssid);
1267 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1268
Tejun Heobd53d612014-04-23 11:13:16 -04001269 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001270 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001271 if (dst_root != &cgrp_dfl_root)
1272 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001273
Tejun Heo5df36032014-03-19 10:23:54 -04001274 if (ss->bind)
1275 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001276 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001277
Tejun Heoa2dd4242014-03-19 10:23:55 -04001278 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279 return 0;
1280}
1281
Tejun Heo2bd59d42014-02-11 11:52:49 -05001282static int cgroup_show_options(struct seq_file *seq,
1283 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001285 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001287 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001288
Tejun Heob85d2042013-12-06 15:11:57 -05001289 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001290 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001291 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001292 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1293 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001294 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001296 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001297 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001298
1299 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001300 if (strlen(root->release_agent_path))
1301 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001302 spin_unlock(&release_agent_path_lock);
1303
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001304 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001305 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001306 if (strlen(root->name))
1307 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308 return 0;
1309}
1310
1311struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001312 unsigned int subsys_mask;
1313 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001314 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001315 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001316 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001317 /* User explicitly requested empty subsystem */
1318 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319};
1320
Ben Blumcf5d5942010-03-10 15:22:09 -08001321static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001323 char *token, *o = data;
1324 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001325 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001326 struct cgroup_subsys *ss;
1327 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001328
1329#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001330 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001331#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332
Paul Menagec6d57f32009-09-23 15:56:19 -07001333 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334
1335 while ((token = strsep(&o, ",")) != NULL) {
1336 if (!*token)
1337 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001338 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001339 /* Explicitly have no subsystems */
1340 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001341 continue;
1342 }
1343 if (!strcmp(token, "all")) {
1344 /* Mutually exclusive option 'all' + subsystem name */
1345 if (one_ss)
1346 return -EINVAL;
1347 all_ss = true;
1348 continue;
1349 }
Tejun Heo873fe092013-04-14 20:15:26 -07001350 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1351 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1352 continue;
1353 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001354 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001355 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001356 continue;
1357 }
1358 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001359 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001360 continue;
1361 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001362 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001363 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001364 continue;
1365 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001366 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001367 /* Specifying two release agents is forbidden */
1368 if (opts->release_agent)
1369 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001370 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001371 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001372 if (!opts->release_agent)
1373 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001374 continue;
1375 }
1376 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001377 const char *name = token + 5;
1378 /* Can't specify an empty name */
1379 if (!strlen(name))
1380 return -EINVAL;
1381 /* Must match [\w.-]+ */
1382 for (i = 0; i < strlen(name); i++) {
1383 char c = name[i];
1384 if (isalnum(c))
1385 continue;
1386 if ((c == '.') || (c == '-') || (c == '_'))
1387 continue;
1388 return -EINVAL;
1389 }
1390 /* Specifying two names is forbidden */
1391 if (opts->name)
1392 return -EINVAL;
1393 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001394 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001395 GFP_KERNEL);
1396 if (!opts->name)
1397 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001398
1399 continue;
1400 }
1401
Tejun Heo30159ec2013-06-25 11:53:37 -07001402 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001403 if (strcmp(token, ss->name))
1404 continue;
1405 if (ss->disabled)
1406 continue;
1407
1408 /* Mutually exclusive option 'all' + subsystem name */
1409 if (all_ss)
1410 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001411 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001412 one_ss = true;
1413
1414 break;
1415 }
1416 if (i == CGROUP_SUBSYS_COUNT)
1417 return -ENOENT;
1418 }
1419
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001420 /* Consistency checks */
1421
Tejun Heo873fe092013-04-14 20:15:26 -07001422 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001423 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 -07001424
Tejun Heod3ba07c2014-02-13 06:58:38 -05001425 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1426 opts->cpuset_clone_children || opts->release_agent ||
1427 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001428 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001429 return -EINVAL;
1430 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001431 } else {
1432 /*
1433 * If the 'all' option was specified select all the
1434 * subsystems, otherwise if 'none', 'name=' and a subsystem
1435 * name options were not specified, let's default to 'all'
1436 */
1437 if (all_ss || (!one_ss && !opts->none && !opts->name))
1438 for_each_subsys(ss, i)
1439 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001440 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001441
Tejun Heoa2dd4242014-03-19 10:23:55 -04001442 /*
1443 * We either have to specify by name or by subsystems. (So
1444 * all empty hierarchies must have a name).
1445 */
1446 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001447 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001448 }
1449
Li Zefanf9ab5b52009-06-17 16:26:33 -07001450 /*
1451 * Option noprefix was introduced just for backward compatibility
1452 * with the old cpuset, so we allow noprefix only if mounting just
1453 * the cpuset subsystem.
1454 */
Tejun Heo93438622013-04-14 20:15:25 -07001455 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001456 return -EINVAL;
1457
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001458
1459 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001460 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001461 return -EINVAL;
1462
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463 return 0;
1464}
1465
Tejun Heo2bd59d42014-02-11 11:52:49 -05001466static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467{
1468 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001469 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001471 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472
Tejun Heo873fe092013-04-14 20:15:26 -07001473 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001474 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001475 return -EINVAL;
1476 }
1477
Tejun Heoace2bee2014-02-11 11:52:47 -05001478 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479 mutex_lock(&cgroup_mutex);
1480
1481 /* See what subsystems are wanted */
1482 ret = parse_cgroupfs_options(data, &opts);
1483 if (ret)
1484 goto out_unlock;
1485
Tejun Heof392e512014-04-23 11:13:14 -04001486 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001487 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001488 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001489
Tejun Heof392e512014-04-23 11:13:14 -04001490 added_mask = opts.subsys_mask & ~root->subsys_mask;
1491 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001492
Ben Blumcf5d5942010-03-10 15:22:09 -08001493 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001494 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001495 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001496 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001497 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1498 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001499 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 goto out_unlock;
1501 }
1502
Tejun Heof172e672013-06-28 17:07:30 -07001503 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001504 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001505 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001506 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001507 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
Tejun Heo5df36032014-03-19 10:23:54 -04001509 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001510 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001513 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001514
Tejun Heo69e943b2014-02-08 10:36:58 -05001515 if (opts.release_agent) {
1516 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001517 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001518 spin_unlock(&release_agent_path_lock);
1519 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001521 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001523 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001524 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525 return ret;
1526}
1527
Tejun Heoafeb0f92014-02-13 06:58:39 -05001528/*
1529 * To reduce the fork() overhead for systems that are not actually using
1530 * their cgroups capability, we don't maintain the lists running through
1531 * each css_set to its tasks until we see the list actually used - in other
1532 * words after the first mount.
1533 */
1534static bool use_task_css_set_links __read_mostly;
1535
1536static void cgroup_enable_task_cg_lists(void)
1537{
1538 struct task_struct *p, *g;
1539
Tejun Heo96d365e2014-02-13 06:58:40 -05001540 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001541
1542 if (use_task_css_set_links)
1543 goto out_unlock;
1544
1545 use_task_css_set_links = true;
1546
1547 /*
1548 * We need tasklist_lock because RCU is not safe against
1549 * while_each_thread(). Besides, a forking task that has passed
1550 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1551 * is not guaranteed to have its child immediately visible in the
1552 * tasklist if we walk through it with RCU.
1553 */
1554 read_lock(&tasklist_lock);
1555 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001556 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1557 task_css_set(p) != &init_css_set);
1558
1559 /*
1560 * We should check if the process is exiting, otherwise
1561 * it will race with cgroup_exit() in that the list
1562 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001563 * Do it while holding siglock so that we don't end up
1564 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001565 */
Tejun Heof153ad12014-02-25 09:56:49 -05001566 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001567 if (!(p->flags & PF_EXITING)) {
1568 struct css_set *cset = task_css_set(p);
1569
1570 list_add(&p->cg_list, &cset->tasks);
1571 get_css_set(cset);
1572 }
Tejun Heof153ad12014-02-25 09:56:49 -05001573 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001574 } while_each_thread(g, p);
1575 read_unlock(&tasklist_lock);
1576out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001577 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001578}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579
Paul Menagecc31edc2008-10-18 20:28:04 -07001580static void init_cgroup_housekeeping(struct cgroup *cgrp)
1581{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001582 struct cgroup_subsys *ss;
1583 int ssid;
1584
Tejun Heo2bd59d42014-02-11 11:52:49 -05001585 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001586 INIT_LIST_HEAD(&cgrp->sibling);
1587 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001588 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001589 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001590 INIT_LIST_HEAD(&cgrp->pidlists);
1591 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001592 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001593
1594 for_each_subsys(ss, ssid)
1595 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001596
1597 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001598}
Paul Menagec6d57f32009-09-23 15:56:19 -07001599
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001600static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001601 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001603 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001604
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001606 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001607 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001608 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001609 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610
Paul Menagec6d57f32009-09-23 15:56:19 -07001611 root->flags = opts->flags;
1612 if (opts->release_agent)
1613 strcpy(root->release_agent_path, opts->release_agent);
1614 if (opts->name)
1615 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001616 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001617 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001618}
1619
Tejun Heo69dfa002014-05-04 15:09:13 -04001620static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001621{
Tejun Heod427dfe2014-02-11 11:52:48 -05001622 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001623 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001624 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001625 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001626
Tejun Heod427dfe2014-02-11 11:52:48 -05001627 lockdep_assert_held(&cgroup_tree_mutex);
1628 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001629
Tejun Heo6fa49182014-05-04 15:09:13 -04001630 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001631 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001632 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001633 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001634
Tejun Heod427dfe2014-02-11 11:52:48 -05001635 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001636 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001637 * but that's OK - it can only be increased by someone holding
1638 * cgroup_lock, and that's us. The worst that can happen is that we
1639 * have some link structures left over
1640 */
1641 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001643 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644
Tejun Heo985ed672014-03-19 10:23:53 -04001645 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001646 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001647 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648
Tejun Heo2bd59d42014-02-11 11:52:49 -05001649 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1650 KERNFS_ROOT_CREATE_DEACTIVATED,
1651 root_cgrp);
1652 if (IS_ERR(root->kf_root)) {
1653 ret = PTR_ERR(root->kf_root);
1654 goto exit_root_id;
1655 }
1656 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657
Tejun Heod427dfe2014-02-11 11:52:48 -05001658 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1659 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001660 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661
Tejun Heo5df36032014-03-19 10:23:54 -04001662 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001663 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001664 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001665
Tejun Heod427dfe2014-02-11 11:52:48 -05001666 /*
1667 * There must be no failure case after here, since rebinding takes
1668 * care of subsystems' refcounts, which are explicitly dropped in
1669 * the failure exit path.
1670 */
1671 list_add(&root->root_list, &cgroup_roots);
1672 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673
Tejun Heod427dfe2014-02-11 11:52:48 -05001674 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001675 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001676 * objects.
1677 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001678 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001679 hash_for_each(css_set_table, i, cset, hlist)
1680 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001681 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682
Tejun Heod427dfe2014-02-11 11:52:48 -05001683 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001684 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001685
Tejun Heo2bd59d42014-02-11 11:52:49 -05001686 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001687 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001688 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001689
Tejun Heo2bd59d42014-02-11 11:52:49 -05001690destroy_root:
1691 kernfs_destroy_root(root->kf_root);
1692 root->kf_root = NULL;
1693exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001694 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001695out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001696 free_cgrp_cset_links(&tmp_links);
1697 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698}
1699
Al Virof7e83572010-07-26 13:23:11 +04001700static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001702 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001704 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001706 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001707 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001708 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001709
1710 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001711 * The first time anyone tries to mount a cgroup, enable the list
1712 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001713 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001714 if (!use_task_css_set_links)
1715 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001716
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001717 mutex_lock(&cgroup_tree_mutex);
1718 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001723 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001724retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001725 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001726 if (!opts.subsys_mask && !opts.none && !opts.name) {
1727 cgrp_dfl_root_visible = true;
1728 root = &cgrp_dfl_root;
1729 cgroup_get(&root->cgrp);
1730 ret = 0;
1731 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 }
1733
Tejun Heo985ed672014-03-19 10:23:53 -04001734 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001735 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001737 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001738 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001739
Paul Menage817929e2007-10-18 23:39:36 -07001740 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001741 * If we asked for a name then it must match. Also, if
1742 * name matches but sybsys_mask doesn't, we should fail.
1743 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001744 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001745 if (opts.name) {
1746 if (strcmp(opts.name, root->name))
1747 continue;
1748 name_match = true;
1749 }
Tejun Heo31261212013-06-28 17:07:30 -07001750
1751 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001752 * If we asked for subsystems (or explicitly for no
1753 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001754 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001755 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001756 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001757 if (!name_match)
1758 continue;
1759 ret = -EBUSY;
1760 goto out_unlock;
1761 }
Tejun Heo873fe092013-04-14 20:15:26 -07001762
Tejun Heoc7ba8282013-06-29 14:06:10 -07001763 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001764 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001765 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001766 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001767 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001768 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001769 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001770 }
Tejun Heo873fe092013-04-14 20:15:26 -07001771 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001772
Tejun Heo776f02f2014-02-12 09:29:50 -05001773 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001774 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001775 * ref indicate that the root is being destroyed. Wait for
1776 * destruction to complete so that the subsystems are free.
1777 * We can use wait_queue for the wait but this path is
1778 * super cold. Let's just sleep for a bit and retry.
1779 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001780 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001781 mutex_unlock(&cgroup_mutex);
1782 mutex_unlock(&cgroup_tree_mutex);
1783 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001784 mutex_lock(&cgroup_tree_mutex);
1785 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001786 goto retry;
1787 }
1788
1789 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001790 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 }
1792
Tejun Heo172a2c062014-03-19 10:23:53 -04001793 /*
1794 * No such thing, create a new one. name= matching without subsys
1795 * specification is allowed for already existing hierarchies but we
1796 * can't create new one without subsys specification.
1797 */
1798 if (!opts.subsys_mask && !opts.none) {
1799 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001800 goto out_unlock;
1801 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001802
Tejun Heo172a2c062014-03-19 10:23:53 -04001803 root = kzalloc(sizeof(*root), GFP_KERNEL);
1804 if (!root) {
1805 ret = -ENOMEM;
1806 goto out_unlock;
1807 }
1808
1809 init_cgroup_root(root, &opts);
1810
Tejun Heo35585572014-02-13 06:58:38 -05001811 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001812 if (ret)
1813 cgroup_free_root(root);
1814
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001815out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001816 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001817 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001818
Paul Menagec6d57f32009-09-23 15:56:19 -07001819 kfree(opts.release_agent);
1820 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001821
Tejun Heo2bd59d42014-02-11 11:52:49 -05001822 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001823 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001824
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001825 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1826 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001827 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001828 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829}
1830
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001831static void cgroup_kill_sb(struct super_block *sb)
1832{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001833 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001834 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001836 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001837 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001838}
1839
1840static struct file_system_type cgroup_fs_type = {
1841 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001842 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001843 .kill_sb = cgroup_kill_sb,
1844};
1845
Greg KH676db4a2010-08-05 13:53:35 -07001846static struct kobject *cgroup_kobj;
1847
Li Zefana043e3b2008-02-23 15:24:09 -08001848/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001849 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001850 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001851 * @buf: the buffer to write the path into
1852 * @buflen: the length of the buffer
1853 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001854 * Determine @task's cgroup on the first (the one with the lowest non-zero
1855 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1856 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1857 * cgroup controller callbacks.
1858 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001859 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001860 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001861char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001862{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001863 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001864 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001865 int hierarchy_id = 1;
1866 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001867
1868 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001869 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001870
Tejun Heo913ffdb2013-07-11 16:34:48 -07001871 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1872
Tejun Heo857a2be2013-04-14 20:50:08 -07001873 if (root) {
1874 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001875 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001876 } else {
1877 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001878 if (strlcpy(buf, "/", buflen) < buflen)
1879 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001880 }
1881
Tejun Heo96d365e2014-02-13 06:58:40 -05001882 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001883 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001884 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001885}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001886EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001887
Tejun Heob3dc0942014-02-25 10:04:01 -05001888/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001889struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001890 /* the src and dst cset list running through cset->mg_node */
1891 struct list_head src_csets;
1892 struct list_head dst_csets;
1893
1894 /*
1895 * Fields for cgroup_taskset_*() iteration.
1896 *
1897 * Before migration is committed, the target migration tasks are on
1898 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1899 * the csets on ->dst_csets. ->csets point to either ->src_csets
1900 * or ->dst_csets depending on whether migration is committed.
1901 *
1902 * ->cur_csets and ->cur_task point to the current task position
1903 * during iteration.
1904 */
1905 struct list_head *csets;
1906 struct css_set *cur_cset;
1907 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001908};
1909
1910/**
1911 * cgroup_taskset_first - reset taskset and return the first task
1912 * @tset: taskset of interest
1913 *
1914 * @tset iteration is initialized and the first task is returned.
1915 */
1916struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1917{
Tejun Heob3dc0942014-02-25 10:04:01 -05001918 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1919 tset->cur_task = NULL;
1920
1921 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001922}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001923
1924/**
1925 * cgroup_taskset_next - iterate to the next task in taskset
1926 * @tset: taskset of interest
1927 *
1928 * Return the next task in @tset. Iteration must have been initialized
1929 * with cgroup_taskset_first().
1930 */
1931struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1932{
Tejun Heob3dc0942014-02-25 10:04:01 -05001933 struct css_set *cset = tset->cur_cset;
1934 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001935
Tejun Heob3dc0942014-02-25 10:04:01 -05001936 while (&cset->mg_node != tset->csets) {
1937 if (!task)
1938 task = list_first_entry(&cset->mg_tasks,
1939 struct task_struct, cg_list);
1940 else
1941 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001942
Tejun Heob3dc0942014-02-25 10:04:01 -05001943 if (&task->cg_list != &cset->mg_tasks) {
1944 tset->cur_cset = cset;
1945 tset->cur_task = task;
1946 return task;
1947 }
1948
1949 cset = list_next_entry(cset, mg_node);
1950 task = NULL;
1951 }
1952
1953 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001954}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001955
1956/**
Ben Blum74a11662011-05-26 16:25:20 -07001957 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001958 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001959 * @tsk: the task being migrated
1960 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001961 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001962 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001963 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001964static void cgroup_task_migrate(struct cgroup *old_cgrp,
1965 struct task_struct *tsk,
1966 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001967{
Tejun Heo5abb8852013-06-12 21:04:49 -07001968 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001969
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001970 lockdep_assert_held(&cgroup_mutex);
1971 lockdep_assert_held(&css_set_rwsem);
1972
Ben Blum74a11662011-05-26 16:25:20 -07001973 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001974 * We are synchronized through threadgroup_lock() against PF_EXITING
1975 * setting such that we can't race against cgroup_exit() changing the
1976 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001977 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001978 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001979 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001980
Tejun Heob3dc0942014-02-25 10:04:01 -05001981 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001982 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001983
Tejun Heo1b9aba42014-03-19 17:43:21 -04001984 /*
1985 * Use move_tail so that cgroup_taskset_first() still returns the
1986 * leader after migration. This works because cgroup_migrate()
1987 * ensures that the dst_cset of the leader is the first on the
1988 * tset's dst_csets list.
1989 */
1990 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001991
1992 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001993 * We just gained a reference on old_cset by taking it from the
1994 * task. As trading it for new_cset is protected by cgroup_mutex,
1995 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001996 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001997 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001998 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001999}
2000
Li Zefana043e3b2008-02-23 15:24:09 -08002001/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002002 * cgroup_migrate_finish - cleanup after attach
2003 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002004 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002005 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2006 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002007 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002008static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002009{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002010 struct css_set *cset, *tmp_cset;
2011
2012 lockdep_assert_held(&cgroup_mutex);
2013
2014 down_write(&css_set_rwsem);
2015 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2016 cset->mg_src_cgrp = NULL;
2017 cset->mg_dst_cset = NULL;
2018 list_del_init(&cset->mg_preload_node);
2019 put_css_set_locked(cset, false);
2020 }
2021 up_write(&css_set_rwsem);
2022}
2023
2024/**
2025 * cgroup_migrate_add_src - add a migration source css_set
2026 * @src_cset: the source css_set to add
2027 * @dst_cgrp: the destination cgroup
2028 * @preloaded_csets: list of preloaded css_sets
2029 *
2030 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2031 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2032 * up by cgroup_migrate_finish().
2033 *
2034 * This function may be called without holding threadgroup_lock even if the
2035 * target is a process. Threads may be created and destroyed but as long
2036 * as cgroup_mutex is not dropped, no new css_set can be put into play and
2037 * the preloaded css_sets are guaranteed to cover all migrations.
2038 */
2039static void cgroup_migrate_add_src(struct css_set *src_cset,
2040 struct cgroup *dst_cgrp,
2041 struct list_head *preloaded_csets)
2042{
2043 struct cgroup *src_cgrp;
2044
2045 lockdep_assert_held(&cgroup_mutex);
2046 lockdep_assert_held(&css_set_rwsem);
2047
2048 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2049
Tejun Heo1958d2d2014-02-25 10:04:03 -05002050 if (!list_empty(&src_cset->mg_preload_node))
2051 return;
2052
2053 WARN_ON(src_cset->mg_src_cgrp);
2054 WARN_ON(!list_empty(&src_cset->mg_tasks));
2055 WARN_ON(!list_empty(&src_cset->mg_node));
2056
2057 src_cset->mg_src_cgrp = src_cgrp;
2058 get_css_set(src_cset);
2059 list_add(&src_cset->mg_preload_node, preloaded_csets);
2060}
2061
2062/**
2063 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002064 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002065 * @preloaded_csets: list of preloaded source css_sets
2066 *
2067 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2068 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002069 * pins all destination css_sets, links each to its source, and append them
2070 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2071 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002072 *
2073 * This function must be called after cgroup_migrate_add_src() has been
2074 * called on each migration source css_set. After migration is performed
2075 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2076 * @preloaded_csets.
2077 */
2078static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2079 struct list_head *preloaded_csets)
2080{
2081 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002082 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002083
2084 lockdep_assert_held(&cgroup_mutex);
2085
Tejun Heof8f22e52014-04-23 11:13:16 -04002086 /*
2087 * Except for the root, child_subsys_mask must be zero for a cgroup
2088 * with tasks so that child cgroups don't compete against tasks.
2089 */
2090 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2091 dst_cgrp->child_subsys_mask)
2092 return -EBUSY;
2093
Tejun Heo1958d2d2014-02-25 10:04:03 -05002094 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002095 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002096 struct css_set *dst_cset;
2097
Tejun Heof817de92014-04-23 11:13:16 -04002098 dst_cset = find_css_set(src_cset,
2099 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002100 if (!dst_cset)
2101 goto err;
2102
2103 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002104
2105 /*
2106 * If src cset equals dst, it's noop. Drop the src.
2107 * cgroup_migrate() will skip the cset too. Note that we
2108 * can't handle src == dst as some nodes are used by both.
2109 */
2110 if (src_cset == dst_cset) {
2111 src_cset->mg_src_cgrp = NULL;
2112 list_del_init(&src_cset->mg_preload_node);
2113 put_css_set(src_cset, false);
2114 put_css_set(dst_cset, false);
2115 continue;
2116 }
2117
Tejun Heo1958d2d2014-02-25 10:04:03 -05002118 src_cset->mg_dst_cset = dst_cset;
2119
2120 if (list_empty(&dst_cset->mg_preload_node))
2121 list_add(&dst_cset->mg_preload_node, &csets);
2122 else
2123 put_css_set(dst_cset, false);
2124 }
2125
Tejun Heof817de92014-04-23 11:13:16 -04002126 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002127 return 0;
2128err:
2129 cgroup_migrate_finish(&csets);
2130 return -ENOMEM;
2131}
2132
2133/**
2134 * cgroup_migrate - migrate a process or task to a cgroup
2135 * @cgrp: the destination cgroup
2136 * @leader: the leader of the process or the task to migrate
2137 * @threadgroup: whether @leader points to the whole process or a single task
2138 *
2139 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2140 * process, the caller must be holding threadgroup_lock of @leader. The
2141 * caller is also responsible for invoking cgroup_migrate_add_src() and
2142 * cgroup_migrate_prepare_dst() on the targets before invoking this
2143 * function and following up with cgroup_migrate_finish().
2144 *
2145 * As long as a controller's ->can_attach() doesn't fail, this function is
2146 * guaranteed to succeed. This means that, excluding ->can_attach()
2147 * failure, when migrating multiple targets, the success or failure can be
2148 * decided for all targets by invoking group_migrate_prepare_dst() before
2149 * actually starting migrating.
2150 */
2151static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2152 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002153{
Tejun Heob3dc0942014-02-25 10:04:01 -05002154 struct cgroup_taskset tset = {
2155 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2156 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2157 .csets = &tset.src_csets,
2158 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002159 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002160 struct css_set *cset, *tmp_cset;
2161 struct task_struct *task, *tmp_task;
2162 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002163
2164 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002165 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2166 * already PF_EXITING could be freed from underneath us unless we
2167 * take an rcu_read_lock.
2168 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002169 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002170 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002171 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002172 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002173 /* @task either already exited or can't exit until the end */
2174 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002175 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002176
Tejun Heoeaf797a2014-02-25 10:04:03 -05002177 /* leave @task alone if post_fork() hasn't linked it yet */
2178 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002179 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002180
Tejun Heob3dc0942014-02-25 10:04:01 -05002181 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002182 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002183 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002184
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002185 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002186 * cgroup_taskset_first() must always return the leader.
2187 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002188 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002189 list_move_tail(&task->cg_list, &cset->mg_tasks);
2190 if (list_empty(&cset->mg_node))
2191 list_add_tail(&cset->mg_node, &tset.src_csets);
2192 if (list_empty(&cset->mg_dst_cset->mg_node))
2193 list_move_tail(&cset->mg_dst_cset->mg_node,
2194 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002195 next:
Li Zefan081aa452013-03-13 09:17:09 +08002196 if (!threadgroup)
2197 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002198 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002199 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002200 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002201
Tejun Heo134d3372011-12-12 18:12:21 -08002202 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002203 if (list_empty(&tset.src_csets))
2204 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002205
Tejun Heo1958d2d2014-02-25 10:04:03 -05002206 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002207 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002208 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002209 ret = css->ss->can_attach(css, &tset);
2210 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002211 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002212 goto out_cancel_attach;
2213 }
2214 }
Ben Blum74a11662011-05-26 16:25:20 -07002215 }
2216
2217 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002218 * Now that we're guaranteed success, proceed to move all tasks to
2219 * the new cgroup. There are no failure cases after here, so this
2220 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002221 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002222 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002223 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2224 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2225 cgroup_task_migrate(cset->mg_src_cgrp, task,
2226 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002227 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002228 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002229
2230 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002231 * Migration is committed, all target tasks are now on dst_csets.
2232 * Nothing is sensitive to fork() after this point. Notify
2233 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002234 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002235 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002236
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002237 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002238 if (css->ss->attach)
2239 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002240
Tejun Heo9db8de32014-02-13 06:58:43 -05002241 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002242 goto out_release_tset;
2243
Ben Blum74a11662011-05-26 16:25:20 -07002244out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002245 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002246 if (css == failed_css)
2247 break;
2248 if (css->ss->cancel_attach)
2249 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002250 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002251out_release_tset:
2252 down_write(&css_set_rwsem);
2253 list_splice_init(&tset.dst_csets, &tset.src_csets);
2254 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002255 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002256 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002257 }
2258 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002259 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002260}
2261
Tejun Heo1958d2d2014-02-25 10:04:03 -05002262/**
2263 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2264 * @dst_cgrp: the cgroup to attach to
2265 * @leader: the task or the leader of the threadgroup to be attached
2266 * @threadgroup: attach the whole threadgroup?
2267 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002268 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002269 */
2270static int cgroup_attach_task(struct cgroup *dst_cgrp,
2271 struct task_struct *leader, bool threadgroup)
2272{
2273 LIST_HEAD(preloaded_csets);
2274 struct task_struct *task;
2275 int ret;
2276
2277 /* look up all src csets */
2278 down_read(&css_set_rwsem);
2279 rcu_read_lock();
2280 task = leader;
2281 do {
2282 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2283 &preloaded_csets);
2284 if (!threadgroup)
2285 break;
2286 } while_each_thread(leader, task);
2287 rcu_read_unlock();
2288 up_read(&css_set_rwsem);
2289
2290 /* prepare dst csets and commit */
2291 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2292 if (!ret)
2293 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2294
2295 cgroup_migrate_finish(&preloaded_csets);
2296 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002297}
2298
2299/*
2300 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002301 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002302 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002303 */
Tejun Heoacbef752014-05-13 12:16:22 -04002304static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2305 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002306{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002307 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002308 const struct cred *cred = current_cred(), *tcred;
Tejun Heoacbef752014-05-13 12:16:22 -04002309 struct cgroup *cgrp = of_css(of)->cgroup;
2310 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002311 int ret;
2312
Tejun Heoacbef752014-05-13 12:16:22 -04002313 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2314 return -EINVAL;
2315
Ben Blum74a11662011-05-26 16:25:20 -07002316 if (!cgroup_lock_live_group(cgrp))
2317 return -ENODEV;
2318
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002319retry_find_task:
2320 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002321 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002322 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002323 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002324 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002325 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002326 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002327 }
Ben Blum74a11662011-05-26 16:25:20 -07002328 /*
2329 * even if we're attaching all tasks in the thread group, we
2330 * only need to check permissions on one of them.
2331 */
David Howellsc69e8d92008-11-14 10:39:19 +11002332 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002333 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2334 !uid_eq(cred->euid, tcred->uid) &&
2335 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002336 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002337 ret = -EACCES;
2338 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002339 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002340 } else
2341 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002342
2343 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002344 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002345
2346 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002347 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002348 * trapped in a cpuset, or RT worker may be born in a cgroup
2349 * with no rt_runtime allocated. Just say no.
2350 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002351 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002352 ret = -EINVAL;
2353 rcu_read_unlock();
2354 goto out_unlock_cgroup;
2355 }
2356
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002357 get_task_struct(tsk);
2358 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002359
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002360 threadgroup_lock(tsk);
2361 if (threadgroup) {
2362 if (!thread_group_leader(tsk)) {
2363 /*
2364 * a race with de_thread from another thread's exec()
2365 * may strip us of our leadership, if this happens,
2366 * there is no choice but to throw this task away and
2367 * try again; this is
2368 * "double-double-toil-and-trouble-check locking".
2369 */
2370 threadgroup_unlock(tsk);
2371 put_task_struct(tsk);
2372 goto retry_find_task;
2373 }
Li Zefan081aa452013-03-13 09:17:09 +08002374 }
2375
2376 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2377
Tejun Heocd3d0952011-12-12 18:12:21 -08002378 threadgroup_unlock(tsk);
2379
Paul Menagebbcb81d2007-10-18 23:39:32 -07002380 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002381out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002382 mutex_unlock(&cgroup_mutex);
Tejun Heoacbef752014-05-13 12:16:22 -04002383 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002384}
2385
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002386/**
2387 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2388 * @from: attach to all cgroups of a given task
2389 * @tsk: the task to be attached
2390 */
2391int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2392{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002393 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002394 int retval = 0;
2395
Tejun Heo47cfcd02013-04-07 09:29:51 -07002396 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002397 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002398 struct cgroup *from_cgrp;
2399
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002400 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002401 continue;
2402
Tejun Heo96d365e2014-02-13 06:58:40 -05002403 down_read(&css_set_rwsem);
2404 from_cgrp = task_cgroup_from_root(from, root);
2405 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002406
Li Zefan6f4b7e62013-07-31 16:18:36 +08002407 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002408 if (retval)
2409 break;
2410 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002411 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002412
2413 return retval;
2414}
2415EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2416
Tejun Heoacbef752014-05-13 12:16:22 -04002417static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2418 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002419{
Tejun Heoacbef752014-05-13 12:16:22 -04002420 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002421}
2422
Tejun Heoacbef752014-05-13 12:16:22 -04002423static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2424 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002425{
Tejun Heoacbef752014-05-13 12:16:22 -04002426 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002427}
2428
Tejun Heo451af502014-05-13 12:16:21 -04002429static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2430 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002431{
Tejun Heo451af502014-05-13 12:16:21 -04002432 struct cgroup *cgrp = of_css(of)->cgroup;
2433 struct cgroup_root *root = cgrp->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002434
2435 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo451af502014-05-13 12:16:21 -04002436 if (!cgroup_lock_live_group(cgrp))
Paul Menagee788e062008-07-25 01:46:59 -07002437 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002438 spin_lock(&release_agent_path_lock);
Tejun Heo451af502014-05-13 12:16:21 -04002439 strlcpy(root->release_agent_path, strstrip(buf),
Tejun Heo5f469902014-02-11 11:52:48 -05002440 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002441 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002442 mutex_unlock(&cgroup_mutex);
Tejun Heo451af502014-05-13 12:16:21 -04002443 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002444}
2445
Tejun Heo2da8ca82013-12-05 12:28:04 -05002446static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002447{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002448 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002449
Tejun Heo46cfeb02014-05-13 12:11:00 -04002450 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002451 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002452 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002453 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002454 return 0;
2455}
2456
Tejun Heo2da8ca82013-12-05 12:28:04 -05002457static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002458{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002459 struct cgroup *cgrp = seq_css(seq)->cgroup;
2460
2461 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002462 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002463}
2464
Tejun Heof8f22e52014-04-23 11:13:16 -04002465static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2466{
2467 struct cgroup_subsys *ss;
2468 bool printed = false;
2469 int ssid;
2470
2471 for_each_subsys(ss, ssid) {
2472 if (ss_mask & (1 << ssid)) {
2473 if (printed)
2474 seq_putc(seq, ' ');
2475 seq_printf(seq, "%s", ss->name);
2476 printed = true;
2477 }
2478 }
2479 if (printed)
2480 seq_putc(seq, '\n');
2481}
2482
2483/* show controllers which are currently attached to the default hierarchy */
2484static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2485{
2486 struct cgroup *cgrp = seq_css(seq)->cgroup;
2487
2488 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2489 return 0;
2490}
2491
2492/* show controllers which are enabled from the parent */
2493static int cgroup_controllers_show(struct seq_file *seq, void *v)
2494{
2495 struct cgroup *cgrp = seq_css(seq)->cgroup;
2496
2497 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2498 return 0;
2499}
2500
2501/* show controllers which are enabled for a given cgroup's children */
2502static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2503{
2504 struct cgroup *cgrp = seq_css(seq)->cgroup;
2505
2506 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2507 return 0;
2508}
2509
2510/**
2511 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2512 * @cgrp: root of the subtree to update csses for
2513 *
2514 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2515 * css associations need to be updated accordingly. This function looks up
2516 * all css_sets which are attached to the subtree, creates the matching
2517 * updated css_sets and migrates the tasks to the new ones.
2518 */
2519static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2520{
2521 LIST_HEAD(preloaded_csets);
2522 struct cgroup_subsys_state *css;
2523 struct css_set *src_cset;
2524 int ret;
2525
2526 lockdep_assert_held(&cgroup_tree_mutex);
2527 lockdep_assert_held(&cgroup_mutex);
2528
2529 /* look up all csses currently attached to @cgrp's subtree */
2530 down_read(&css_set_rwsem);
2531 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2532 struct cgrp_cset_link *link;
2533
2534 /* self is not affected by child_subsys_mask change */
2535 if (css->cgroup == cgrp)
2536 continue;
2537
2538 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2539 cgroup_migrate_add_src(link->cset, cgrp,
2540 &preloaded_csets);
2541 }
2542 up_read(&css_set_rwsem);
2543
2544 /* NULL dst indicates self on default hierarchy */
2545 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2546 if (ret)
2547 goto out_finish;
2548
2549 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2550 struct task_struct *last_task = NULL, *task;
2551
2552 /* src_csets precede dst_csets, break on the first dst_cset */
2553 if (!src_cset->mg_src_cgrp)
2554 break;
2555
2556 /*
2557 * All tasks in src_cset need to be migrated to the
2558 * matching dst_cset. Empty it process by process. We
2559 * walk tasks but migrate processes. The leader might even
2560 * belong to a different cset but such src_cset would also
2561 * be among the target src_csets because the default
2562 * hierarchy enforces per-process membership.
2563 */
2564 while (true) {
2565 down_read(&css_set_rwsem);
2566 task = list_first_entry_or_null(&src_cset->tasks,
2567 struct task_struct, cg_list);
2568 if (task) {
2569 task = task->group_leader;
2570 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2571 get_task_struct(task);
2572 }
2573 up_read(&css_set_rwsem);
2574
2575 if (!task)
2576 break;
2577
2578 /* guard against possible infinite loop */
2579 if (WARN(last_task == task,
2580 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2581 goto out_finish;
2582 last_task = task;
2583
2584 threadgroup_lock(task);
2585 /* raced against de_thread() from another thread? */
2586 if (!thread_group_leader(task)) {
2587 threadgroup_unlock(task);
2588 put_task_struct(task);
2589 continue;
2590 }
2591
2592 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2593
2594 threadgroup_unlock(task);
2595 put_task_struct(task);
2596
2597 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2598 goto out_finish;
2599 }
2600 }
2601
2602out_finish:
2603 cgroup_migrate_finish(&preloaded_csets);
2604 return ret;
2605}
2606
2607/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002608static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2609 char *buf, size_t nbytes,
2610 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002611{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002612 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002613 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002614 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002615 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002616 int ssid, ret;
2617
2618 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002619 * Parse input - space separated list of subsystem names prefixed
2620 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002621 */
Tejun Heo451af502014-05-13 12:16:21 -04002622 buf = strstrip(buf);
2623 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002624 if (tok[0] == '\0')
2625 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002626 for_each_subsys(ss, ssid) {
2627 if (ss->disabled || strcmp(tok + 1, ss->name))
2628 continue;
2629
2630 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002631 enable |= 1 << ssid;
2632 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002633 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002634 disable |= 1 << ssid;
2635 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002636 } else {
2637 return -EINVAL;
2638 }
2639 break;
2640 }
2641 if (ssid == CGROUP_SUBSYS_COUNT)
2642 return -EINVAL;
2643 }
2644
Tejun Heoa9746d82014-05-13 12:19:22 -04002645 cgrp = cgroup_kn_lock_live(of->kn);
2646 if (!cgrp)
2647 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002648
2649 for_each_subsys(ss, ssid) {
2650 if (enable & (1 << ssid)) {
2651 if (cgrp->child_subsys_mask & (1 << ssid)) {
2652 enable &= ~(1 << ssid);
2653 continue;
2654 }
2655
2656 /*
2657 * Because css offlining is asynchronous, userland
2658 * might try to re-enable the same controller while
2659 * the previous instance is still around. In such
2660 * cases, wait till it's gone using offline_waitq.
2661 */
2662 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002663 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002664
2665 if (!cgroup_css(child, ss))
2666 continue;
2667
Tejun Heo0cee8b72014-05-13 12:10:59 -04002668 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002669 prepare_to_wait(&child->offline_waitq, &wait,
2670 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002671 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002672 schedule();
2673 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002674 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002675
Tejun Heoa9746d82014-05-13 12:19:22 -04002676 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002677 }
2678
2679 /* unavailable or not enabled on the parent? */
2680 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2681 (cgrp->parent &&
2682 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2683 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002684 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002685 }
2686 } else if (disable & (1 << ssid)) {
2687 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2688 disable &= ~(1 << ssid);
2689 continue;
2690 }
2691
2692 /* a child has it enabled? */
2693 cgroup_for_each_live_child(child, cgrp) {
2694 if (child->child_subsys_mask & (1 << ssid)) {
2695 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002696 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002697 }
2698 }
2699 }
2700 }
2701
2702 if (!enable && !disable) {
2703 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002704 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002705 }
2706
2707 /*
2708 * Except for the root, child_subsys_mask must be zero for a cgroup
2709 * with tasks so that child cgroups don't compete against tasks.
2710 */
2711 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2712 ret = -EBUSY;
2713 goto out_unlock;
2714 }
2715
2716 /*
2717 * Create csses for enables and update child_subsys_mask. This
2718 * changes cgroup_e_css() results which in turn makes the
2719 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2720 * subtree to the updated csses.
2721 */
2722 for_each_subsys(ss, ssid) {
2723 if (!(enable & (1 << ssid)))
2724 continue;
2725
2726 cgroup_for_each_live_child(child, cgrp) {
2727 ret = create_css(child, ss);
2728 if (ret)
2729 goto err_undo_css;
2730 }
2731 }
2732
2733 cgrp->child_subsys_mask |= enable;
2734 cgrp->child_subsys_mask &= ~disable;
2735
2736 ret = cgroup_update_dfl_csses(cgrp);
2737 if (ret)
2738 goto err_undo_css;
2739
2740 /* all tasks are now migrated away from the old csses, kill them */
2741 for_each_subsys(ss, ssid) {
2742 if (!(disable & (1 << ssid)))
2743 continue;
2744
2745 cgroup_for_each_live_child(child, cgrp)
2746 kill_css(cgroup_css(child, ss));
2747 }
2748
2749 kernfs_activate(cgrp->kn);
2750 ret = 0;
2751out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002752 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002753 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002754
2755err_undo_css:
2756 cgrp->child_subsys_mask &= ~enable;
2757 cgrp->child_subsys_mask |= disable;
2758
2759 for_each_subsys(ss, ssid) {
2760 if (!(enable & (1 << ssid)))
2761 continue;
2762
2763 cgroup_for_each_live_child(child, cgrp) {
2764 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2765 if (css)
2766 kill_css(css);
2767 }
2768 }
2769 goto out_unlock;
2770}
2771
Tejun Heo842b5972014-04-25 18:28:02 -04002772static int cgroup_populated_show(struct seq_file *seq, void *v)
2773{
2774 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2775 return 0;
2776}
2777
Tejun Heo2bd59d42014-02-11 11:52:49 -05002778static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2779 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002780{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002781 struct cgroup *cgrp = of->kn->parent->priv;
2782 struct cftype *cft = of->kn->priv;
2783 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002784 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002785
Tejun Heob4168642014-05-13 12:16:21 -04002786 if (cft->write)
2787 return cft->write(of, buf, nbytes, off);
2788
Tejun Heo2bd59d42014-02-11 11:52:49 -05002789 /*
2790 * kernfs guarantees that a file isn't deleted with operations in
2791 * flight, which means that the matching css is and stays alive and
2792 * doesn't need to be pinned. The RCU locking is not necessary
2793 * either. It's just for the convenience of using cgroup_css().
2794 */
2795 rcu_read_lock();
2796 css = cgroup_css(cgrp, cft->ss);
2797 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002798
Tejun Heo451af502014-05-13 12:16:21 -04002799 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002800 unsigned long long v;
2801 ret = kstrtoull(buf, 0, &v);
2802 if (!ret)
2803 ret = cft->write_u64(css, cft, v);
2804 } else if (cft->write_s64) {
2805 long long v;
2806 ret = kstrtoll(buf, 0, &v);
2807 if (!ret)
2808 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002809 } else {
2810 ret = -EINVAL;
2811 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002812
Tejun Heoa742c592013-12-05 12:28:03 -05002813 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002814}
2815
Tejun Heo6612f052013-12-05 12:28:04 -05002816static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002817{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002818 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002819}
2820
2821static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2822{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002823 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002824}
2825
2826static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2827{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002828 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002829}
2830
2831static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2832{
Tejun Heo7da11272013-12-05 12:28:04 -05002833 struct cftype *cft = seq_cft(m);
2834 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002835
Tejun Heo2da8ca82013-12-05 12:28:04 -05002836 if (cft->seq_show)
2837 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002838
Tejun Heo896f5192013-12-05 12:28:04 -05002839 if (cft->read_u64)
2840 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2841 else if (cft->read_s64)
2842 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2843 else
2844 return -EINVAL;
2845 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002846}
2847
Tejun Heo2bd59d42014-02-11 11:52:49 -05002848static struct kernfs_ops cgroup_kf_single_ops = {
2849 .atomic_write_len = PAGE_SIZE,
2850 .write = cgroup_file_write,
2851 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002852};
2853
Tejun Heo2bd59d42014-02-11 11:52:49 -05002854static struct kernfs_ops cgroup_kf_ops = {
2855 .atomic_write_len = PAGE_SIZE,
2856 .write = cgroup_file_write,
2857 .seq_start = cgroup_seqfile_start,
2858 .seq_next = cgroup_seqfile_next,
2859 .seq_stop = cgroup_seqfile_stop,
2860 .seq_show = cgroup_seqfile_show,
2861};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002862
2863/*
2864 * cgroup_rename - Only allow simple rename of directories in place.
2865 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002866static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2867 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002868{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002869 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002870 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002871
Tejun Heo2bd59d42014-02-11 11:52:49 -05002872 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002873 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002874 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002875 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002876
Tejun Heo6db8e852013-06-14 11:18:22 -07002877 /*
2878 * This isn't a proper migration and its usefulness is very
2879 * limited. Disallow if sane_behavior.
2880 */
2881 if (cgroup_sane_behavior(cgrp))
2882 return -EPERM;
2883
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002884 /*
2885 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2886 * active_ref. kernfs_rename() doesn't require active_ref
2887 * protection. Break them before grabbing cgroup_tree_mutex.
2888 */
2889 kernfs_break_active_protection(new_parent);
2890 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002891
Tejun Heo2bd59d42014-02-11 11:52:49 -05002892 mutex_lock(&cgroup_tree_mutex);
2893 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002894
Tejun Heo2bd59d42014-02-11 11:52:49 -05002895 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002896
Tejun Heo2bd59d42014-02-11 11:52:49 -05002897 mutex_unlock(&cgroup_mutex);
2898 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002899
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002900 kernfs_unbreak_active_protection(kn);
2901 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002902 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002903}
2904
Tejun Heo49957f82014-04-07 16:44:47 -04002905/* set uid and gid of cgroup dirs and files to that of the creator */
2906static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2907{
2908 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2909 .ia_uid = current_fsuid(),
2910 .ia_gid = current_fsgid(), };
2911
2912 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2913 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2914 return 0;
2915
2916 return kernfs_setattr(kn, &iattr);
2917}
2918
Tejun Heo2bb566c2013-08-08 20:11:23 -04002919static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002920{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002921 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002922 struct kernfs_node *kn;
2923 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002924 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002925
Tejun Heo2bd59d42014-02-11 11:52:49 -05002926#ifdef CONFIG_DEBUG_LOCK_ALLOC
2927 key = &cft->lockdep_key;
2928#endif
2929 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2930 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2931 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002932 if (IS_ERR(kn))
2933 return PTR_ERR(kn);
2934
2935 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002936 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002937 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002938 return ret;
2939 }
2940
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002941 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002942 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002943 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002944}
2945
Tejun Heob1f28d32013-06-28 16:24:10 -07002946/**
2947 * cgroup_addrm_files - add or remove files to a cgroup directory
2948 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002949 * @cfts: array of cftypes to be added
2950 * @is_add: whether to add or remove
2951 *
2952 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002953 * For removals, this function never fails. If addition fails, this
2954 * function doesn't remove files already added. The caller is responsible
2955 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002956 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002957static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2958 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002959{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002960 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002961 int ret;
2962
Tejun Heoace2bee2014-02-11 11:52:47 -05002963 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002964
2965 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002966 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002967 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2968 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002969 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2970 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002971 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2972 continue;
2973 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2974 continue;
2975
Li Zefan2739d3c2013-01-21 18:18:33 +08002976 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002977 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002978 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002979 pr_warn("%s: failed to add %s, err=%d\n",
2980 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002981 return ret;
2982 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002983 } else {
2984 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002985 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002986 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002987 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002988}
2989
Tejun Heo21a2d342014-02-12 09:29:49 -05002990static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002991{
2992 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002993 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002994 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002995 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002996 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002997
Tejun Heo21a2d342014-02-12 09:29:49 -05002998 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002999
Li Zefane8c82d22013-06-18 18:48:37 +08003000 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003001 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003002 struct cgroup *cgrp = css->cgroup;
3003
Li Zefane8c82d22013-06-18 18:48:37 +08003004 if (cgroup_is_dead(cgrp))
3005 continue;
3006
Tejun Heo21a2d342014-02-12 09:29:49 -05003007 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003008 if (ret)
3009 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003010 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003011
3012 if (is_add && !ret)
3013 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003014 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003015}
3016
Tejun Heo2da440a2014-02-11 11:52:48 -05003017static void cgroup_exit_cftypes(struct cftype *cfts)
3018{
3019 struct cftype *cft;
3020
Tejun Heo2bd59d42014-02-11 11:52:49 -05003021 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3022 /* free copy for custom atomic_write_len, see init_cftypes() */
3023 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3024 kfree(cft->kf_ops);
3025 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003026 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003027 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003028}
3029
Tejun Heo2bd59d42014-02-11 11:52:49 -05003030static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003031{
3032 struct cftype *cft;
3033
Tejun Heo2bd59d42014-02-11 11:52:49 -05003034 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3035 struct kernfs_ops *kf_ops;
3036
Tejun Heo0adb0702014-02-12 09:29:48 -05003037 WARN_ON(cft->ss || cft->kf_ops);
3038
Tejun Heo2bd59d42014-02-11 11:52:49 -05003039 if (cft->seq_start)
3040 kf_ops = &cgroup_kf_ops;
3041 else
3042 kf_ops = &cgroup_kf_single_ops;
3043
3044 /*
3045 * Ugh... if @cft wants a custom max_write_len, we need to
3046 * make a copy of kf_ops to set its atomic_write_len.
3047 */
3048 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3049 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3050 if (!kf_ops) {
3051 cgroup_exit_cftypes(cfts);
3052 return -ENOMEM;
3053 }
3054 kf_ops->atomic_write_len = cft->max_write_len;
3055 }
3056
3057 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003058 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003059 }
3060
3061 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003062}
3063
Tejun Heo21a2d342014-02-12 09:29:49 -05003064static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3065{
3066 lockdep_assert_held(&cgroup_tree_mutex);
3067
3068 if (!cfts || !cfts[0].ss)
3069 return -ENOENT;
3070
3071 list_del(&cfts->node);
3072 cgroup_apply_cftypes(cfts, false);
3073 cgroup_exit_cftypes(cfts);
3074 return 0;
3075}
3076
Tejun Heo8e3f6542012-04-01 12:09:55 -07003077/**
Tejun Heo80b13582014-02-12 09:29:48 -05003078 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3079 * @cfts: zero-length name terminated array of cftypes
3080 *
3081 * Unregister @cfts. Files described by @cfts are removed from all
3082 * existing cgroups and all future cgroups won't have them either. This
3083 * function can be called anytime whether @cfts' subsys is attached or not.
3084 *
3085 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3086 * registered.
3087 */
3088int cgroup_rm_cftypes(struct cftype *cfts)
3089{
Tejun Heo21a2d342014-02-12 09:29:49 -05003090 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003091
Tejun Heo21a2d342014-02-12 09:29:49 -05003092 mutex_lock(&cgroup_tree_mutex);
3093 ret = cgroup_rm_cftypes_locked(cfts);
3094 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003095 return ret;
3096}
3097
3098/**
3099 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3100 * @ss: target cgroup subsystem
3101 * @cfts: zero-length name terminated array of cftypes
3102 *
3103 * Register @cfts to @ss. Files described by @cfts are created for all
3104 * existing cgroups to which @ss is attached and all future cgroups will
3105 * have them too. This function can be called anytime whether @ss is
3106 * attached or not.
3107 *
3108 * Returns 0 on successful registration, -errno on failure. Note that this
3109 * function currently returns 0 as long as @cfts registration is successful
3110 * even if some file creation attempts on existing cgroups fail.
3111 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003112int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003113{
Tejun Heo9ccece82013-06-28 16:24:11 -07003114 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003115
Li Zefandc5736e2014-02-17 10:41:50 +08003116 if (!cfts || cfts[0].name[0] == '\0')
3117 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003118
Tejun Heo2bd59d42014-02-11 11:52:49 -05003119 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003120 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003121 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003122
Tejun Heo21a2d342014-02-12 09:29:49 -05003123 mutex_lock(&cgroup_tree_mutex);
3124
Tejun Heo0adb0702014-02-12 09:29:48 -05003125 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003126 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003127 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003128 cgroup_rm_cftypes_locked(cfts);
3129
3130 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003131 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003132}
Tejun Heo79578622012-04-01 12:09:56 -07003133
3134/**
Li Zefana043e3b2008-02-23 15:24:09 -08003135 * cgroup_task_count - count the number of tasks in a cgroup.
3136 * @cgrp: the cgroup in question
3137 *
3138 * Return the number of tasks in the cgroup.
3139 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003140static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003141{
3142 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003143 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003144
Tejun Heo96d365e2014-02-13 06:58:40 -05003145 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003146 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3147 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003148 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003149 return count;
3150}
3151
Tejun Heo574bd9f2012-11-09 09:12:29 -08003152/**
Tejun Heo492eb212013-08-08 20:11:25 -04003153 * css_next_child - find the next child of a given css
3154 * @pos_css: the current position (%NULL to initiate traversal)
3155 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003156 *
Tejun Heo492eb212013-08-08 20:11:25 -04003157 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003158 * under either cgroup_mutex or RCU read lock. The only requirement is
3159 * that @parent_css and @pos_css are accessible. The next sibling is
3160 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003161 */
Tejun Heo492eb212013-08-08 20:11:25 -04003162struct cgroup_subsys_state *
3163css_next_child(struct cgroup_subsys_state *pos_css,
3164 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003165{
Tejun Heo492eb212013-08-08 20:11:25 -04003166 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3167 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003168 struct cgroup *next;
3169
Tejun Heoace2bee2014-02-11 11:52:47 -05003170 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003171
3172 /*
3173 * @pos could already have been removed. Once a cgroup is removed,
3174 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003175 * changes. As CGRP_DEAD assertion is serialized and happens
3176 * before the cgroup is taken off the ->sibling list, if we see it
3177 * unasserted, it's guaranteed that the next sibling hasn't
3178 * finished its grace period even if it's already removed, and thus
3179 * safe to dereference from this RCU critical section. If
3180 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3181 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003182 *
3183 * If @pos is dead, its next pointer can't be dereferenced;
3184 * however, as each cgroup is given a monotonically increasing
3185 * unique serial number and always appended to the sibling list,
3186 * the next one can be found by walking the parent's children until
3187 * we see a cgroup with higher serial number than @pos's. While
3188 * this path can be slower, it's taken only when either the current
3189 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003190 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003191 if (!pos) {
3192 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3193 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003194 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003195 } else {
3196 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3197 if (next->serial_nr > pos->serial_nr)
3198 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003199 }
3200
Tejun Heo3b281af2014-04-23 11:13:15 -04003201 /*
3202 * @next, if not pointing to the head, can be dereferenced and is
3203 * the next sibling; however, it might have @ss disabled. If so,
3204 * fast-forward to the next enabled one.
3205 */
3206 while (&next->sibling != &cgrp->children) {
3207 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003208
Tejun Heo3b281af2014-04-23 11:13:15 -04003209 if (next_css)
3210 return next_css;
3211 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3212 }
3213 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003214}
Tejun Heo53fa5262013-05-24 10:55:38 +09003215
3216/**
Tejun Heo492eb212013-08-08 20:11:25 -04003217 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003218 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003219 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003220 *
Tejun Heo492eb212013-08-08 20:11:25 -04003221 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003222 * to visit for pre-order traversal of @root's descendants. @root is
3223 * included in the iteration and the first node to be visited.
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 next descendant as long
3228 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003229 */
Tejun Heo492eb212013-08-08 20:11:25 -04003230struct cgroup_subsys_state *
3231css_next_descendant_pre(struct cgroup_subsys_state *pos,
3232 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003233{
Tejun Heo492eb212013-08-08 20:11:25 -04003234 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003235
Tejun Heoace2bee2014-02-11 11:52:47 -05003236 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003237
Tejun Heobd8815a2013-08-08 20:11:27 -04003238 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003239 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003240 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003241
3242 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003243 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003244 if (next)
3245 return next;
3246
3247 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003248 while (pos != root) {
3249 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003250 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003251 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003252 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003253 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003254
3255 return NULL;
3256}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003257
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003258/**
Tejun Heo492eb212013-08-08 20:11:25 -04003259 * css_rightmost_descendant - return the rightmost descendant of a css
3260 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003261 *
Tejun Heo492eb212013-08-08 20:11:25 -04003262 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3263 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003264 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003265 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003266 * While this function requires cgroup_mutex or RCU read locking, it
3267 * doesn't require the whole traversal to be contained in a single critical
3268 * section. This function will return the correct rightmost descendant as
3269 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003270 */
Tejun Heo492eb212013-08-08 20:11:25 -04003271struct cgroup_subsys_state *
3272css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003273{
Tejun Heo492eb212013-08-08 20:11:25 -04003274 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003275
Tejun Heoace2bee2014-02-11 11:52:47 -05003276 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003277
3278 do {
3279 last = pos;
3280 /* ->prev isn't RCU safe, walk ->next till the end */
3281 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003282 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003283 pos = tmp;
3284 } while (pos);
3285
3286 return last;
3287}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003288
Tejun Heo492eb212013-08-08 20:11:25 -04003289static struct cgroup_subsys_state *
3290css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003291{
Tejun Heo492eb212013-08-08 20:11:25 -04003292 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003293
3294 do {
3295 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003296 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003297 } while (pos);
3298
3299 return last;
3300}
3301
3302/**
Tejun Heo492eb212013-08-08 20:11:25 -04003303 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003304 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003305 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003306 *
Tejun Heo492eb212013-08-08 20:11:25 -04003307 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003308 * to visit for post-order traversal of @root's descendants. @root is
3309 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003310 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003311 * While this function requires cgroup_mutex or RCU read locking, it
3312 * doesn't require the whole traversal to be contained in a single critical
3313 * section. This function will return the correct next descendant as long
3314 * as both @pos and @cgroup are accessible and @pos is a descendant of
3315 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003316 */
Tejun Heo492eb212013-08-08 20:11:25 -04003317struct cgroup_subsys_state *
3318css_next_descendant_post(struct cgroup_subsys_state *pos,
3319 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003320{
Tejun Heo492eb212013-08-08 20:11:25 -04003321 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003322
Tejun Heoace2bee2014-02-11 11:52:47 -05003323 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003324
Tejun Heo58b79a92013-09-06 15:31:08 -04003325 /* if first iteration, visit leftmost descendant which may be @root */
3326 if (!pos)
3327 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003328
Tejun Heobd8815a2013-08-08 20:11:27 -04003329 /* if we visited @root, we're done */
3330 if (pos == root)
3331 return NULL;
3332
Tejun Heo574bd9f2012-11-09 09:12:29 -08003333 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003334 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003335 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003336 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003337
3338 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003339 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003340}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003341
Tejun Heo0942eee2013-08-08 20:11:26 -04003342/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003343 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003344 * @it: the iterator to advance
3345 *
3346 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003347 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003348static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003349{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003350 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003351 struct cgrp_cset_link *link;
3352 struct css_set *cset;
3353
3354 /* Advance to the next non-empty css_set */
3355 do {
3356 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003357 if (l == it->cset_head) {
3358 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003359 return;
3360 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003361
3362 if (it->ss) {
3363 cset = container_of(l, struct css_set,
3364 e_cset_node[it->ss->id]);
3365 } else {
3366 link = list_entry(l, struct cgrp_cset_link, cset_link);
3367 cset = link->cset;
3368 }
Tejun Heoc7561122014-02-25 10:04:01 -05003369 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3370
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003371 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003372
3373 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003374 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003375 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003376 it->task_pos = cset->mg_tasks.next;
3377
3378 it->tasks_head = &cset->tasks;
3379 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003380}
3381
Tejun Heo0942eee2013-08-08 20:11:26 -04003382/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003383 * css_task_iter_start - initiate task iteration
3384 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003385 * @it: the task iterator to use
3386 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003387 * Initiate iteration through the tasks of @css. The caller can call
3388 * css_task_iter_next() to walk through the tasks until the function
3389 * returns NULL. On completion of iteration, css_task_iter_end() must be
3390 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003391 *
3392 * Note that this function acquires a lock which is released when the
3393 * iteration finishes. The caller can't sleep while iteration is in
3394 * progress.
3395 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003396void css_task_iter_start(struct cgroup_subsys_state *css,
3397 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003398 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003399{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003400 /* no one should try to iterate before mounting cgroups */
3401 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003402
Tejun Heo96d365e2014-02-13 06:58:40 -05003403 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003404
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003405 it->ss = css->ss;
3406
3407 if (it->ss)
3408 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3409 else
3410 it->cset_pos = &css->cgroup->cset_links;
3411
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003412 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003413
Tejun Heo72ec7022013-08-08 20:11:26 -04003414 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003415}
3416
Tejun Heo0942eee2013-08-08 20:11:26 -04003417/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003418 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003419 * @it: the task iterator being iterated
3420 *
3421 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003422 * initialized via css_task_iter_start(). Returns NULL when the iteration
3423 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003424 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003425struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003426{
3427 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003428 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003429
3430 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003431 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003432 return NULL;
3433 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003434
3435 /*
3436 * Advance iterator to find next entry. cset->tasks is consumed
3437 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3438 * next cset.
3439 */
Paul Menage817929e2007-10-18 23:39:36 -07003440 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003441
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003442 if (l == it->tasks_head)
3443 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003444
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003445 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003446 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003447 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003448 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003449
Paul Menage817929e2007-10-18 23:39:36 -07003450 return res;
3451}
3452
Tejun Heo0942eee2013-08-08 20:11:26 -04003453/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003454 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003455 * @it: the task iterator to finish
3456 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003457 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003458 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003459void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003460 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003461{
Tejun Heo96d365e2014-02-13 06:58:40 -05003462 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003463}
3464
3465/**
3466 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3467 * @to: cgroup to which the tasks will be moved
3468 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003469 *
3470 * Locking rules between cgroup_post_fork() and the migration path
3471 * guarantee that, if a task is forking while being migrated, the new child
3472 * is guaranteed to be either visible in the source cgroup after the
3473 * parent's migration is complete or put into the target cgroup. No task
3474 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003475 */
3476int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3477{
Tejun Heo952aaa12014-02-25 10:04:03 -05003478 LIST_HEAD(preloaded_csets);
3479 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003480 struct css_task_iter it;
3481 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003482 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003483
Tejun Heo952aaa12014-02-25 10:04:03 -05003484 mutex_lock(&cgroup_mutex);
3485
3486 /* all tasks in @from are being moved, all csets are source */
3487 down_read(&css_set_rwsem);
3488 list_for_each_entry(link, &from->cset_links, cset_link)
3489 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3490 up_read(&css_set_rwsem);
3491
3492 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3493 if (ret)
3494 goto out_err;
3495
3496 /*
3497 * Migrate tasks one-by-one until @form is empty. This fails iff
3498 * ->can_attach() fails.
3499 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003500 do {
3501 css_task_iter_start(&from->dummy_css, &it);
3502 task = css_task_iter_next(&it);
3503 if (task)
3504 get_task_struct(task);
3505 css_task_iter_end(&it);
3506
3507 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003508 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003509 put_task_struct(task);
3510 }
3511 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003512out_err:
3513 cgroup_migrate_finish(&preloaded_csets);
3514 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003515 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003516}
3517
Paul Menage817929e2007-10-18 23:39:36 -07003518/*
Ben Blum102a7752009-09-23 15:56:26 -07003519 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003520 *
3521 * Reading this file can return large amounts of data if a cgroup has
3522 * *lots* of attached tasks. So it may need several calls to read(),
3523 * but we cannot guarantee that the information we produce is correct
3524 * unless we produce it entirely atomically.
3525 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003526 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003527
Li Zefan24528252012-01-20 11:58:43 +08003528/* which pidlist file are we talking about? */
3529enum cgroup_filetype {
3530 CGROUP_FILE_PROCS,
3531 CGROUP_FILE_TASKS,
3532};
3533
3534/*
3535 * A pidlist is a list of pids that virtually represents the contents of one
3536 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3537 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3538 * to the cgroup.
3539 */
3540struct cgroup_pidlist {
3541 /*
3542 * used to find which pidlist is wanted. doesn't change as long as
3543 * this particular list stays in the list.
3544 */
3545 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3546 /* array of xids */
3547 pid_t *list;
3548 /* how many elements the above list has */
3549 int length;
Li Zefan24528252012-01-20 11:58:43 +08003550 /* each of these stored in a list by its cgroup */
3551 struct list_head links;
3552 /* pointer to the cgroup we belong to, for list removal purposes */
3553 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003554 /* for delayed destruction */
3555 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003556};
3557
Paul Menagebbcb81d2007-10-18 23:39:32 -07003558/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003559 * The following two functions "fix" the issue where there are more pids
3560 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3561 * TODO: replace with a kernel-wide solution to this problem
3562 */
3563#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3564static void *pidlist_allocate(int count)
3565{
3566 if (PIDLIST_TOO_LARGE(count))
3567 return vmalloc(count * sizeof(pid_t));
3568 else
3569 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3570}
Tejun Heob1a21362013-11-29 10:42:58 -05003571
Ben Blumd1d9fd32009-09-23 15:56:28 -07003572static void pidlist_free(void *p)
3573{
3574 if (is_vmalloc_addr(p))
3575 vfree(p);
3576 else
3577 kfree(p);
3578}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003579
3580/*
Tejun Heob1a21362013-11-29 10:42:58 -05003581 * Used to destroy all pidlists lingering waiting for destroy timer. None
3582 * should be left afterwards.
3583 */
3584static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3585{
3586 struct cgroup_pidlist *l, *tmp_l;
3587
3588 mutex_lock(&cgrp->pidlist_mutex);
3589 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3590 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3591 mutex_unlock(&cgrp->pidlist_mutex);
3592
3593 flush_workqueue(cgroup_pidlist_destroy_wq);
3594 BUG_ON(!list_empty(&cgrp->pidlists));
3595}
3596
3597static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3598{
3599 struct delayed_work *dwork = to_delayed_work(work);
3600 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3601 destroy_dwork);
3602 struct cgroup_pidlist *tofree = NULL;
3603
3604 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003605
3606 /*
Tejun Heo04502362013-11-29 10:42:59 -05003607 * Destroy iff we didn't get queued again. The state won't change
3608 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003609 */
Tejun Heo04502362013-11-29 10:42:59 -05003610 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003611 list_del(&l->links);
3612 pidlist_free(l->list);
3613 put_pid_ns(l->key.ns);
3614 tofree = l;
3615 }
3616
Tejun Heob1a21362013-11-29 10:42:58 -05003617 mutex_unlock(&l->owner->pidlist_mutex);
3618 kfree(tofree);
3619}
3620
3621/*
Ben Blum102a7752009-09-23 15:56:26 -07003622 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003623 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003624 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003625static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003626{
Ben Blum102a7752009-09-23 15:56:26 -07003627 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003628
3629 /*
3630 * we presume the 0th element is unique, so i starts at 1. trivial
3631 * edge cases first; no work needs to be done for either
3632 */
3633 if (length == 0 || length == 1)
3634 return length;
3635 /* src and dest walk down the list; dest counts unique elements */
3636 for (src = 1; src < length; src++) {
3637 /* find next unique element */
3638 while (list[src] == list[src-1]) {
3639 src++;
3640 if (src == length)
3641 goto after;
3642 }
3643 /* dest always points to where the next unique element goes */
3644 list[dest] = list[src];
3645 dest++;
3646 }
3647after:
Ben Blum102a7752009-09-23 15:56:26 -07003648 return dest;
3649}
3650
Tejun Heoafb2bc12013-11-29 10:42:59 -05003651/*
3652 * The two pid files - task and cgroup.procs - guaranteed that the result
3653 * is sorted, which forced this whole pidlist fiasco. As pid order is
3654 * different per namespace, each namespace needs differently sorted list,
3655 * making it impossible to use, for example, single rbtree of member tasks
3656 * sorted by task pointer. As pidlists can be fairly large, allocating one
3657 * per open file is dangerous, so cgroup had to implement shared pool of
3658 * pidlists keyed by cgroup and namespace.
3659 *
3660 * All this extra complexity was caused by the original implementation
3661 * committing to an entirely unnecessary property. In the long term, we
3662 * want to do away with it. Explicitly scramble sort order if
3663 * sane_behavior so that no such expectation exists in the new interface.
3664 *
3665 * Scrambling is done by swapping every two consecutive bits, which is
3666 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3667 */
3668static pid_t pid_fry(pid_t pid)
3669{
3670 unsigned a = pid & 0x55555555;
3671 unsigned b = pid & 0xAAAAAAAA;
3672
3673 return (a << 1) | (b >> 1);
3674}
3675
3676static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3677{
3678 if (cgroup_sane_behavior(cgrp))
3679 return pid_fry(pid);
3680 else
3681 return pid;
3682}
3683
Ben Blum102a7752009-09-23 15:56:26 -07003684static int cmppid(const void *a, const void *b)
3685{
3686 return *(pid_t *)a - *(pid_t *)b;
3687}
3688
Tejun Heoafb2bc12013-11-29 10:42:59 -05003689static int fried_cmppid(const void *a, const void *b)
3690{
3691 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3692}
3693
Ben Blum72a8cb32009-09-23 15:56:27 -07003694static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3695 enum cgroup_filetype type)
3696{
3697 struct cgroup_pidlist *l;
3698 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003699 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003700
Tejun Heoe6b81712013-11-29 10:42:59 -05003701 lockdep_assert_held(&cgrp->pidlist_mutex);
3702
3703 list_for_each_entry(l, &cgrp->pidlists, links)
3704 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003705 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003706 return NULL;
3707}
3708
Ben Blum72a8cb32009-09-23 15:56:27 -07003709/*
3710 * find the appropriate pidlist for our purpose (given procs vs tasks)
3711 * returns with the lock on that pidlist already held, and takes care
3712 * of the use count, or returns NULL with no locks held if we're out of
3713 * memory.
3714 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003715static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3716 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003717{
3718 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003719
Tejun Heoe6b81712013-11-29 10:42:59 -05003720 lockdep_assert_held(&cgrp->pidlist_mutex);
3721
3722 l = cgroup_pidlist_find(cgrp, type);
3723 if (l)
3724 return l;
3725
Ben Blum72a8cb32009-09-23 15:56:27 -07003726 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003727 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003728 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003729 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003730
Tejun Heob1a21362013-11-29 10:42:58 -05003731 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003732 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003733 /* don't need task_nsproxy() if we're looking at ourself */
3734 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003735 l->owner = cgrp;
3736 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003737 return l;
3738}
3739
3740/*
Ben Blum102a7752009-09-23 15:56:26 -07003741 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3742 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003743static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3744 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003745{
3746 pid_t *array;
3747 int length;
3748 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003749 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003750 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003751 struct cgroup_pidlist *l;
3752
Tejun Heo4bac00d2013-11-29 10:42:59 -05003753 lockdep_assert_held(&cgrp->pidlist_mutex);
3754
Ben Blum102a7752009-09-23 15:56:26 -07003755 /*
3756 * If cgroup gets more users after we read count, we won't have
3757 * enough space - tough. This race is indistinguishable to the
3758 * caller from the case that the additional cgroup users didn't
3759 * show up until sometime later on.
3760 */
3761 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003762 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003763 if (!array)
3764 return -ENOMEM;
3765 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003766 css_task_iter_start(&cgrp->dummy_css, &it);
3767 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003768 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003769 break;
Ben Blum102a7752009-09-23 15:56:26 -07003770 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003771 if (type == CGROUP_FILE_PROCS)
3772 pid = task_tgid_vnr(tsk);
3773 else
3774 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003775 if (pid > 0) /* make sure to only use valid results */
3776 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003777 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003778 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003779 length = n;
3780 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003781 if (cgroup_sane_behavior(cgrp))
3782 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3783 else
3784 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003785 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003786 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003787
Tejun Heoe6b81712013-11-29 10:42:59 -05003788 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003789 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003790 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003791 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003792 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003793 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003794
3795 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003796 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003797 l->list = array;
3798 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003799 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003800 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003801}
3802
Balbir Singh846c7bb2007-10-18 23:39:44 -07003803/**
Li Zefana043e3b2008-02-23 15:24:09 -08003804 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003805 * @stats: cgroupstats to fill information into
3806 * @dentry: A dentry entry belonging to the cgroup for which stats have
3807 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003808 *
3809 * Build and fill cgroupstats so that taskstats can export it to user
3810 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003811 */
3812int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3813{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003814 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003815 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003816 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003817 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003818
Tejun Heo2bd59d42014-02-11 11:52:49 -05003819 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3820 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3821 kernfs_type(kn) != KERNFS_DIR)
3822 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003823
Li Zefanbad34662014-02-14 16:54:28 +08003824 mutex_lock(&cgroup_mutex);
3825
Tejun Heo2bd59d42014-02-11 11:52:49 -05003826 /*
3827 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003828 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003829 * @kn->priv is RCU safe. Let's do the RCU dancing.
3830 */
3831 rcu_read_lock();
3832 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003833 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003834 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003835 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003836 return -ENOENT;
3837 }
Li Zefanbad34662014-02-14 16:54:28 +08003838 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003839
Tejun Heo72ec7022013-08-08 20:11:26 -04003840 css_task_iter_start(&cgrp->dummy_css, &it);
3841 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003842 switch (tsk->state) {
3843 case TASK_RUNNING:
3844 stats->nr_running++;
3845 break;
3846 case TASK_INTERRUPTIBLE:
3847 stats->nr_sleeping++;
3848 break;
3849 case TASK_UNINTERRUPTIBLE:
3850 stats->nr_uninterruptible++;
3851 break;
3852 case TASK_STOPPED:
3853 stats->nr_stopped++;
3854 break;
3855 default:
3856 if (delayacct_is_task_waiting_on_io(tsk))
3857 stats->nr_io_wait++;
3858 break;
3859 }
3860 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003861 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003862
Li Zefanbad34662014-02-14 16:54:28 +08003863 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003864 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003865}
3866
Paul Menage8f3ff202009-09-23 15:56:25 -07003867
Paul Menagecc31edc2008-10-18 20:28:04 -07003868/*
Ben Blum102a7752009-09-23 15:56:26 -07003869 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003871 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003872 */
3873
Ben Blum102a7752009-09-23 15:56:26 -07003874static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003875{
3876 /*
3877 * Initially we receive a position value that corresponds to
3878 * one more than the last pid shown (or 0 on the first call or
3879 * after a seek to the start). Use a binary-search to find the
3880 * next pid to display, if any
3881 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003882 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003883 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003884 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003885 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003886 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003887 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003888
Tejun Heo4bac00d2013-11-29 10:42:59 -05003889 mutex_lock(&cgrp->pidlist_mutex);
3890
3891 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003892 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003893 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003894 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003895 * could already have been destroyed.
3896 */
Tejun Heo5d224442013-12-05 12:28:04 -05003897 if (of->priv)
3898 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003899
3900 /*
3901 * Either this is the first start() after open or the matching
3902 * pidlist has been destroyed inbetween. Create a new one.
3903 */
Tejun Heo5d224442013-12-05 12:28:04 -05003904 if (!of->priv) {
3905 ret = pidlist_array_load(cgrp, type,
3906 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003907 if (ret)
3908 return ERR_PTR(ret);
3909 }
Tejun Heo5d224442013-12-05 12:28:04 -05003910 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003911
Paul Menagecc31edc2008-10-18 20:28:04 -07003912 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003913 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003914
Paul Menagecc31edc2008-10-18 20:28:04 -07003915 while (index < end) {
3916 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003917 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003918 index = mid;
3919 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003920 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003921 index = mid + 1;
3922 else
3923 end = mid;
3924 }
3925 }
3926 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003927 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003928 return NULL;
3929 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003930 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003931 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003932 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003933}
3934
Ben Blum102a7752009-09-23 15:56:26 -07003935static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003936{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003937 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003938 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003939
Tejun Heo5d224442013-12-05 12:28:04 -05003940 if (l)
3941 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003942 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003943 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003944}
3945
Ben Blum102a7752009-09-23 15:56:26 -07003946static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003947{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003948 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003949 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003950 pid_t *p = v;
3951 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003952 /*
3953 * Advance to the next pid in the array. If this goes off the
3954 * end, we're done
3955 */
3956 p++;
3957 if (p >= end) {
3958 return NULL;
3959 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003960 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003961 return p;
3962 }
3963}
3964
Ben Blum102a7752009-09-23 15:56:26 -07003965static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003966{
3967 return seq_printf(s, "%d\n", *(int *)v);
3968}
3969
Tejun Heo182446d2013-08-08 20:11:24 -04003970static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3971 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003972{
Tejun Heo182446d2013-08-08 20:11:24 -04003973 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003974}
3975
Tejun Heo182446d2013-08-08 20:11:24 -04003976static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3977 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003978{
Tejun Heo182446d2013-08-08 20:11:24 -04003979 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003980 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003981 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003982 else
Tejun Heo182446d2013-08-08 20:11:24 -04003983 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003984 return 0;
3985}
3986
Tejun Heo182446d2013-08-08 20:11:24 -04003987static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3988 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003989{
Tejun Heo182446d2013-08-08 20:11:24 -04003990 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003991}
3992
Tejun Heo182446d2013-08-08 20:11:24 -04003993static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3994 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003995{
3996 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003997 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003998 else
Tejun Heo182446d2013-08-08 20:11:24 -04003999 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004000 return 0;
4001}
4002
Tejun Heod5c56ce2013-06-03 19:14:34 -07004003static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004004 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004005 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004006 .seq_start = cgroup_pidlist_start,
4007 .seq_next = cgroup_pidlist_next,
4008 .seq_stop = cgroup_pidlist_stop,
4009 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004010 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004011 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004012 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004013 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004014 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004015 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004016 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004017 .read_u64 = cgroup_clone_children_read,
4018 .write_u64 = cgroup_clone_children_write,
4019 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004020 {
Tejun Heo873fe092013-04-14 20:15:26 -07004021 .name = "cgroup.sane_behavior",
4022 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004023 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004024 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004025 {
4026 .name = "cgroup.controllers",
4027 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
4028 .seq_show = cgroup_root_controllers_show,
4029 },
4030 {
4031 .name = "cgroup.controllers",
4032 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4033 .seq_show = cgroup_controllers_show,
4034 },
4035 {
4036 .name = "cgroup.subtree_control",
4037 .flags = CFTYPE_ONLY_ON_DFL,
4038 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004039 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004040 },
Tejun Heo842b5972014-04-25 18:28:02 -04004041 {
4042 .name = "cgroup.populated",
4043 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4044 .seq_show = cgroup_populated_show,
4045 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004046
4047 /*
4048 * Historical crazy stuff. These don't have "cgroup." prefix and
4049 * don't exist if sane_behavior. If you're depending on these, be
4050 * prepared to be burned.
4051 */
4052 {
4053 .name = "tasks",
4054 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004055 .seq_start = cgroup_pidlist_start,
4056 .seq_next = cgroup_pidlist_next,
4057 .seq_stop = cgroup_pidlist_stop,
4058 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004059 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004060 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004061 .mode = S_IRUGO | S_IWUSR,
4062 },
4063 {
4064 .name = "notify_on_release",
4065 .flags = CFTYPE_INSANE,
4066 .read_u64 = cgroup_read_notify_on_release,
4067 .write_u64 = cgroup_write_notify_on_release,
4068 },
Tejun Heo873fe092013-04-14 20:15:26 -07004069 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004070 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004071 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004072 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004073 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004074 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004075 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004076 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004077};
4078
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004079/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004080 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004081 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004082 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004083 *
4084 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004085 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004086static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004088 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004089 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004090
Tejun Heo8e3f6542012-04-01 12:09:55 -07004091 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004092 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004093 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004094
Tejun Heo69dfa002014-05-04 15:09:13 -04004095 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004096 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004097
Tejun Heo0adb0702014-02-12 09:29:48 -05004098 list_for_each_entry(cfts, &ss->cfts, node) {
4099 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004100 if (ret < 0)
4101 goto err;
4102 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004103 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004104 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004105err:
4106 cgroup_clear_dir(cgrp, subsys_mask);
4107 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108}
4109
Tejun Heo0c21ead2013-08-13 20:22:51 -04004110/*
4111 * css destruction is four-stage process.
4112 *
4113 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4114 * Implemented in kill_css().
4115 *
4116 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004117 * and thus css_tryget_online() is guaranteed to fail, the css can be
4118 * offlined by invoking offline_css(). After offlining, the base ref is
4119 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004120 *
4121 * 3. When the percpu_ref reaches zero, the only possible remaining
4122 * accessors are inside RCU read sections. css_release() schedules the
4123 * RCU callback.
4124 *
4125 * 4. After the grace period, the css can be freed. Implemented in
4126 * css_free_work_fn().
4127 *
4128 * It is actually hairier because both step 2 and 4 require process context
4129 * and thus involve punting to css->destroy_work adding two additional
4130 * steps to the already complex sequence.
4131 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004132static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004133{
4134 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004135 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004136 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004137
Tejun Heo0ae78e02013-08-13 11:01:54 -04004138 if (css->parent)
4139 css_put(css->parent);
4140
Tejun Heo0c21ead2013-08-13 20:22:51 -04004141 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004142 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004143}
4144
4145static void css_free_rcu_fn(struct rcu_head *rcu_head)
4146{
4147 struct cgroup_subsys_state *css =
4148 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4149
Tejun Heo0c21ead2013-08-13 20:22:51 -04004150 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004151 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004152}
4153
Tejun Heod3daf282013-06-13 19:39:16 -07004154static void css_release(struct percpu_ref *ref)
4155{
4156 struct cgroup_subsys_state *css =
4157 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo15a4c832014-05-04 15:09:14 -04004158 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004159
Tejun Heo15a4c832014-05-04 15:09:14 -04004160 cgroup_idr_remove(&ss->css_idr, css->id);
4161
Tejun Heo0c21ead2013-08-13 20:22:51 -04004162 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004163}
4164
Tejun Heoddfcada2014-05-04 15:09:14 -04004165static void init_and_link_css(struct cgroup_subsys_state *css,
4166 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167{
Tejun Heoddfcada2014-05-04 15:09:14 -04004168 cgroup_get(cgrp);
4169
Paul Menagebd89aab2007-10-18 23:40:44 -07004170 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004171 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004172 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004173
Tejun Heoddfcada2014-05-04 15:09:14 -04004174 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004175 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004176 css_get(css->parent);
4177 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004178 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004179 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004180
Tejun Heoca8bdca2013-08-26 18:40:56 -04004181 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004182}
4183
Li Zefan2a4ac632013-07-31 16:16:40 +08004184/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004185static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004186{
Tejun Heo623f9262013-08-13 11:01:55 -04004187 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004188 int ret = 0;
4189
Tejun Heoace2bee2014-02-11 11:52:47 -05004190 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004191 lockdep_assert_held(&cgroup_mutex);
4192
Tejun Heo92fb9742012-11-19 08:13:38 -08004193 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004194 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004195 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004196 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004197 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004198 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004199 }
Tejun Heob1929db2012-11-19 08:13:38 -08004200 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004201}
4202
Li Zefan2a4ac632013-07-31 16:16:40 +08004203/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004204static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004205{
Tejun Heo623f9262013-08-13 11:01:55 -04004206 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004207
Tejun Heoace2bee2014-02-11 11:52:47 -05004208 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004209 lockdep_assert_held(&cgroup_mutex);
4210
4211 if (!(css->flags & CSS_ONLINE))
4212 return;
4213
Li Zefand7eeac12013-03-12 15:35:59 -07004214 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004215 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004216
Tejun Heoeb954192013-08-08 20:11:23 -04004217 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004218 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004219 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004220
4221 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004222}
4223
Tejun Heoc81c925a2013-12-06 15:11:56 -05004224/**
4225 * create_css - create a cgroup_subsys_state
4226 * @cgrp: the cgroup new css will be associated with
4227 * @ss: the subsys of new css
4228 *
4229 * Create a new css associated with @cgrp - @ss pair. On success, the new
4230 * css is online and installed in @cgrp with all interface files created.
4231 * Returns 0 on success, -errno on failure.
4232 */
4233static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4234{
4235 struct cgroup *parent = cgrp->parent;
4236 struct cgroup_subsys_state *css;
4237 int err;
4238
Tejun Heoc81c925a2013-12-06 15:11:56 -05004239 lockdep_assert_held(&cgroup_mutex);
4240
4241 css = ss->css_alloc(cgroup_css(parent, ss));
4242 if (IS_ERR(css))
4243 return PTR_ERR(css);
4244
Tejun Heoddfcada2014-05-04 15:09:14 -04004245 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004246
Tejun Heoc81c925a2013-12-06 15:11:56 -05004247 err = percpu_ref_init(&css->refcnt, css_release);
4248 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004249 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004250
Tejun Heo15a4c832014-05-04 15:09:14 -04004251 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4252 if (err < 0)
4253 goto err_free_percpu_ref;
4254 css->id = err;
4255
Tejun Heoaec25022014-02-08 10:36:58 -05004256 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004257 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004258 goto err_free_id;
4259
4260 /* @css is ready to be brought online now, make it visible */
4261 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004262
4263 err = online_css(css);
4264 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004265 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004266
Tejun Heoc81c925a2013-12-06 15:11:56 -05004267 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4268 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004269 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 -04004270 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004271 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004272 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004273 ss->warned_broken_hierarchy = true;
4274 }
4275
4276 return 0;
4277
Li Zefan3eb59ec2014-03-18 17:02:36 +08004278err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004279 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004280err_free_id:
4281 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004282err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004283 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004284err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004285 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004286 return err;
4287}
4288
Tejun Heob3bfd982014-05-13 12:19:22 -04004289static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4290 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004291{
Tejun Heoa9746d82014-05-13 12:19:22 -04004292 struct cgroup *parent, *cgrp;
4293 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004295 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004296 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004297
Tejun Heoa9746d82014-05-13 12:19:22 -04004298 parent = cgroup_kn_lock_live(parent_kn);
4299 if (!parent)
4300 return -ENODEV;
4301 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004302
4303 /* allocate the cgroup and its ID, 0 is reserved for the root */
4304 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4305 if (!cgrp) {
4306 ret = -ENOMEM;
4307 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004308 }
4309
4310 /*
4311 * Temporarily set the pointer to NULL, so idr_find() won't return
4312 * a half-baked cgroup.
4313 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004314 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004315 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004316 ret = -ENOMEM;
4317 goto out_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004318 }
4319
Paul Menagecc31edc2008-10-18 20:28:04 -07004320 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321
Paul Menagebd89aab2007-10-18 23:40:44 -07004322 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004323 cgrp->dummy_css.parent = &parent->dummy_css;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004324 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325
Li Zefanb6abdb02008-03-04 14:28:19 -08004326 if (notify_on_release(parent))
4327 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4328
Tejun Heo2260e7f2012-11-19 08:13:38 -08004329 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4330 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004331
Tejun Heo2bd59d42014-02-11 11:52:49 -05004332 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004333 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004334 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004335 ret = PTR_ERR(kn);
4336 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004337 }
4338 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339
Tejun Heo6f305582014-02-12 09:29:50 -05004340 /*
4341 * This extra ref will be put in cgroup_free_fn() and guarantees
4342 * that @cgrp->kn is always accessible.
4343 */
4344 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345
Tejun Heo00356bd2013-06-18 11:14:22 -07004346 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004347
Tejun Heo4e139af2012-11-19 08:13:36 -08004348 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004349 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004350 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004351 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004352
Tejun Heo0d802552013-12-06 15:11:56 -05004353 /*
4354 * @cgrp is now fully operational. If something fails after this
4355 * point, it'll be released via the normal destruction path.
4356 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004357 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004358
Tejun Heoba0f4d72014-05-13 12:19:22 -04004359 ret = cgroup_kn_set_ugid(kn);
4360 if (ret)
4361 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004362
Tejun Heoba0f4d72014-05-13 12:19:22 -04004363 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4364 if (ret)
4365 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004366
Tejun Heo9d403e92013-12-06 15:11:56 -05004367 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004368 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004369 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004370 ret = create_css(cgrp, ss);
4371 if (ret)
4372 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004373 }
Tejun Heoa8638032012-11-09 09:12:29 -08004374 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004375
Tejun Heobd53d612014-04-23 11:13:16 -04004376 /*
4377 * On the default hierarchy, a child doesn't automatically inherit
4378 * child_subsys_mask from the parent. Each is configured manually.
4379 */
4380 if (!cgroup_on_dfl(cgrp))
4381 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004382
Tejun Heo2bd59d42014-02-11 11:52:49 -05004383 kernfs_activate(kn);
4384
Tejun Heoba0f4d72014-05-13 12:19:22 -04004385 ret = 0;
4386 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387
Tejun Heoba0f4d72014-05-13 12:19:22 -04004388out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004389 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004390out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004391 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004392out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004393 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004394 return ret;
4395
4396out_destroy:
4397 cgroup_destroy_locked(cgrp);
4398 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399}
4400
Tejun Heo223dbc32013-08-13 20:22:50 -04004401/*
4402 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heoec903c02014-05-13 12:11:01 -04004403 * css_tryget_online() is now guaranteed to fail.
Tejun Heo223dbc32013-08-13 20:22:50 -04004404 */
4405static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004406{
Tejun Heo223dbc32013-08-13 20:22:50 -04004407 struct cgroup_subsys_state *css =
4408 container_of(work, struct cgroup_subsys_state, destroy_work);
4409 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004410
Tejun Heoace2bee2014-02-11 11:52:47 -05004411 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004412 mutex_lock(&cgroup_mutex);
4413
4414 /*
Tejun Heoec903c02014-05-13 12:11:01 -04004415 * css_tryget_online() is guaranteed to fail now. Tell subsystems
4416 * to initate destruction.
Tejun Heo09a503ea2013-08-13 20:22:50 -04004417 */
4418 offline_css(css);
4419
4420 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004421 * If @cgrp is marked dead, it's waiting for refs of all css's to
4422 * be disabled before proceeding to the second phase of cgroup
4423 * destruction. If we are the last one, kick it off.
4424 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004425 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004426 cgroup_destroy_css_killed(cgrp);
4427
4428 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004429 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004430
4431 /*
4432 * Put the css refs from kill_css(). Each css holds an extra
4433 * reference to the cgroup's dentry and cgroup removal proceeds
4434 * regardless of css refs. On the last put of each css, whenever
4435 * that may be, the extra dentry ref is put so that dentry
4436 * destruction happens only after all css's are released.
4437 */
4438 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004439}
4440
Tejun Heo223dbc32013-08-13 20:22:50 -04004441/* css kill confirmation processing requires process context, bounce */
4442static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004443{
4444 struct cgroup_subsys_state *css =
4445 container_of(ref, struct cgroup_subsys_state, refcnt);
4446
Tejun Heo223dbc32013-08-13 20:22:50 -04004447 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004448 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004449}
4450
Tejun Heof392e512014-04-23 11:13:14 -04004451/**
4452 * kill_css - destroy a css
4453 * @css: css to destroy
4454 *
4455 * This function initiates destruction of @css by removing cgroup interface
4456 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004457 * asynchronously once css_tryget_online() is guaranteed to fail and when
4458 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004459 */
4460static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004461{
Tejun Heo94419622014-03-19 10:23:54 -04004462 lockdep_assert_held(&cgroup_tree_mutex);
4463
Tejun Heo2bd59d42014-02-11 11:52:49 -05004464 /*
4465 * This must happen before css is disassociated with its cgroup.
4466 * See seq_css() for details.
4467 */
Tejun Heoaec25022014-02-08 10:36:58 -05004468 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004469
Tejun Heoedae0c32013-08-13 20:22:51 -04004470 /*
4471 * Killing would put the base ref, but we need to keep it alive
4472 * until after ->css_offline().
4473 */
4474 css_get(css);
4475
4476 /*
4477 * cgroup core guarantees that, by the time ->css_offline() is
4478 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004479 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004480 * proceed to offlining css's because percpu_ref_kill() doesn't
4481 * guarantee that the ref is seen as killed on all CPUs on return.
4482 *
4483 * Use percpu_ref_kill_and_confirm() to get notifications as each
4484 * css is confirmed to be seen as killed on all CPUs.
4485 */
4486 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004487}
4488
4489/**
4490 * cgroup_destroy_locked - the first stage of cgroup destruction
4491 * @cgrp: cgroup to be destroyed
4492 *
4493 * css's make use of percpu refcnts whose killing latency shouldn't be
4494 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004495 * guarantee that css_tryget_online() won't succeed by the time
4496 * ->css_offline() is invoked. To satisfy all the requirements,
4497 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004498 *
4499 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4500 * userland visible parts and start killing the percpu refcnts of
4501 * css's. Set up so that the next stage will be kicked off once all
4502 * the percpu refcnts are confirmed to be killed.
4503 *
4504 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4505 * rest of destruction. Once all cgroup references are gone, the
4506 * cgroup is RCU-freed.
4507 *
4508 * This function implements s1. After this step, @cgrp is gone as far as
4509 * the userland is concerned and a new cgroup with the same name may be
4510 * created. As cgroup doesn't care about the names internally, this
4511 * doesn't cause any problem.
4512 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004513static int cgroup_destroy_locked(struct cgroup *cgrp)
4514 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004515{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004516 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004517 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004518 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004519 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004520
Tejun Heoace2bee2014-02-11 11:52:47 -05004521 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004522 lockdep_assert_held(&cgroup_mutex);
4523
Tejun Heoddd69142013-06-12 21:04:54 -07004524 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004525 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004526 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004527 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004528 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004529 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004530 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004531 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004532 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004533
Tejun Heo1a90dd52012-11-05 09:16:59 -08004534 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004535 * Make sure there's no live children. We can't test ->children
4536 * emptiness as dead children linger on it while being destroyed;
4537 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4538 */
4539 empty = true;
4540 rcu_read_lock();
4541 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4542 empty = cgroup_is_dead(child);
4543 if (!empty)
4544 break;
4545 }
4546 rcu_read_unlock();
4547 if (!empty)
4548 return -EBUSY;
4549
4550 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004551 * Mark @cgrp dead. This prevents further task migration and child
4552 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004553 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004554 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004555 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004556 */
Tejun Heo54766d42013-06-12 21:04:53 -07004557 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004558
Tejun Heo5d773812014-03-19 10:23:53 -04004559 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004560 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4561 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004562 * percpu refs of all css's are confirmed to be killed. This
4563 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004564 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004565 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004566 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004567 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004568 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004569
Tejun Heo455050d2013-06-13 19:27:41 -07004570 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4571 raw_spin_lock(&release_list_lock);
4572 if (!list_empty(&cgrp->release_list))
4573 list_del_init(&cgrp->release_list);
4574 raw_spin_unlock(&release_list_lock);
4575
4576 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004577 * If @cgrp has css's attached, the second stage of cgroup
4578 * destruction is kicked off from css_killed_work_fn() after the
4579 * refs of all attached css's are killed. If @cgrp doesn't have
4580 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004581 */
Tejun Heof20104d2013-08-13 20:22:50 -04004582 if (!cgrp->nr_css)
4583 cgroup_destroy_css_killed(cgrp);
4584
Tejun Heo2bd59d42014-02-11 11:52:49 -05004585 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004586 mutex_unlock(&cgroup_mutex);
Tejun Heo6f305582014-02-12 09:29:50 -05004587 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo4ac06012014-02-11 11:52:47 -05004588 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004589
Tejun Heoea15f8c2013-06-13 19:27:42 -07004590 return 0;
4591};
4592
Tejun Heod3daf282013-06-13 19:39:16 -07004593/**
Tejun Heof20104d2013-08-13 20:22:50 -04004594 * cgroup_destroy_css_killed - the second step of cgroup destruction
Fabian Frederick60106942014-05-05 20:08:13 +02004595 * @cgrp: the cgroup whose csses have just finished offlining
Tejun Heod3daf282013-06-13 19:39:16 -07004596 *
4597 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004598 * destroyed after all css's are offlined and performs the rest of
4599 * destruction. This is the second step of destruction described in the
4600 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004601 */
Tejun Heof20104d2013-08-13 20:22:50 -04004602static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004603{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004604 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004605
Tejun Heoace2bee2014-02-11 11:52:47 -05004606 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004607 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004608
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004609 /* delete this cgroup from parent->children */
4610 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004611
Tejun Heo59f52962014-02-11 11:52:49 -05004612 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004613
Paul Menageddbcc7e2007-10-18 23:39:30 -07004614 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004615 check_for_release(parent);
4616}
4617
Tejun Heo2bd59d42014-02-11 11:52:49 -05004618static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004619{
Tejun Heoa9746d82014-05-13 12:19:22 -04004620 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004621 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004622
Tejun Heoa9746d82014-05-13 12:19:22 -04004623 cgrp = cgroup_kn_lock_live(kn);
4624 if (!cgrp)
4625 return 0;
4626 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004627
Tejun Heoa9746d82014-05-13 12:19:22 -04004628 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004629
Tejun Heoa9746d82014-05-13 12:19:22 -04004630 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004631
4632 /*
4633 * There are two control paths which try to determine cgroup from
4634 * dentry without going through kernfs - cgroupstats_build() and
4635 * css_tryget_online_from_dir(). Those are supported by RCU
4636 * protecting clearing of cgrp->kn->priv backpointer, which should
4637 * happen after all files under it have been removed.
4638 */
4639 if (!ret)
4640 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4641
Tejun Heo2bd59d42014-02-11 11:52:49 -05004642 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004643 return ret;
4644}
4645
Tejun Heo2bd59d42014-02-11 11:52:49 -05004646static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4647 .remount_fs = cgroup_remount,
4648 .show_options = cgroup_show_options,
4649 .mkdir = cgroup_mkdir,
4650 .rmdir = cgroup_rmdir,
4651 .rename = cgroup_rename,
4652};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004653
Tejun Heo15a4c832014-05-04 15:09:14 -04004654static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004655{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004656 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004657
4658 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659
Tejun Heoace2bee2014-02-11 11:52:47 -05004660 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004661 mutex_lock(&cgroup_mutex);
4662
Tejun Heo15a4c832014-05-04 15:09:14 -04004663 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004664 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004665
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004666 /* Create the root cgroup state for this subsystem */
4667 ss->root = &cgrp_dfl_root;
4668 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669 /* We don't handle early failures gracefully */
4670 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004671 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004672 if (early) {
4673 /* idr_alloc() can't be called safely during early init */
4674 css->id = 1;
4675 } else {
4676 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4677 BUG_ON(css->id < 0);
4678 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679
Li Zefane8d55fd2008-04-29 01:00:13 -07004680 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004681 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004682 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004683 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004684 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685
4686 need_forkexit_callback |= ss->fork || ss->exit;
4687
Li Zefane8d55fd2008-04-29 01:00:13 -07004688 /* At system boot, before all subsystems have been
4689 * registered, no tasks have been forked, so we don't
4690 * need to invoke fork callbacks here. */
4691 BUG_ON(!list_empty(&init_task.tasks));
4692
Tejun Heoae7f1642013-08-13 20:22:50 -04004693 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004694
Tejun Heof392e512014-04-23 11:13:14 -04004695 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004696
Ben Blume6a11052010-03-10 15:22:09 -08004697 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004698 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004699}
4700
4701/**
Li Zefana043e3b2008-02-23 15:24:09 -08004702 * cgroup_init_early - cgroup initialization at system boot
4703 *
4704 * Initialize cgroups at system boot, and initialize any
4705 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004706 */
4707int __init cgroup_init_early(void)
4708{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004709 static struct cgroup_sb_opts __initdata opts =
4710 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004711 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004712 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004713
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004714 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004715 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004716
Tejun Heo3ed80a62014-02-08 10:36:58 -05004717 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004718 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004719 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4720 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004721 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004722 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4723 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004724
Tejun Heoaec25022014-02-08 10:36:58 -05004725 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004726 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727
4728 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004729 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730 }
4731 return 0;
4732}
4733
4734/**
Li Zefana043e3b2008-02-23 15:24:09 -08004735 * cgroup_init - cgroup initialization
4736 *
4737 * Register cgroup filesystem and /proc file, and initialize
4738 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004739 */
4740int __init cgroup_init(void)
4741{
Tejun Heo30159ec2013-06-25 11:53:37 -07004742 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004743 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004744 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004745
Tejun Heo2bd59d42014-02-11 11:52:49 -05004746 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004747
Tejun Heo985ed672014-03-19 10:23:53 -04004748 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004749 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004750
Tejun Heo82fe9b02013-06-25 11:53:37 -07004751 /* Add init_css_set to the hash table */
4752 key = css_set_hash(init_css_set.subsys);
4753 hash_add(css_set_table, &init_css_set.hlist, key);
4754
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004755 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004756
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004757 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004758 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004759
Tejun Heo172a2c062014-03-19 10:23:53 -04004760 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004761 if (ss->early_init) {
4762 struct cgroup_subsys_state *css =
4763 init_css_set.subsys[ss->id];
4764
4765 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4766 GFP_KERNEL);
4767 BUG_ON(css->id < 0);
4768 } else {
4769 cgroup_init_subsys(ss, false);
4770 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004771
Tejun Heo2d8f2432014-04-23 11:13:15 -04004772 list_add_tail(&init_css_set.e_cset_node[ssid],
4773 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4774
Tejun Heo172a2c062014-03-19 10:23:53 -04004775 /*
4776 * cftype registration needs kmalloc and can't be done
4777 * during early_init. Register base cftypes separately.
4778 */
4779 if (ss->base_cftypes)
4780 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4781 }
Greg KH676db4a2010-08-05 13:53:35 -07004782
Paul Menageddbcc7e2007-10-18 23:39:30 -07004783 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004784 if (!cgroup_kobj)
4785 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004786
4787 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004788 if (err < 0) {
4789 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004790 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004791 }
4792
4793 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004794 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004795}
Paul Menageb4f48b62007-10-18 23:39:33 -07004796
Tejun Heoe5fca242013-11-22 17:14:39 -05004797static int __init cgroup_wq_init(void)
4798{
4799 /*
4800 * There isn't much point in executing destruction path in
4801 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004802 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004803 *
4804 * We would prefer to do this in cgroup_init() above, but that
4805 * is called before init_workqueues(): so leave this until after.
4806 */
Tejun Heo1a115332014-02-12 19:06:19 -05004807 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004808 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004809
4810 /*
4811 * Used to destroy pidlists and separate to serve as flush domain.
4812 * Cap @max_active to 1 too.
4813 */
4814 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4815 0, 1);
4816 BUG_ON(!cgroup_pidlist_destroy_wq);
4817
Tejun Heoe5fca242013-11-22 17:14:39 -05004818 return 0;
4819}
4820core_initcall(cgroup_wq_init);
4821
Paul Menagea4243162007-10-18 23:39:35 -07004822/*
4823 * proc_cgroup_show()
4824 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4825 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004826 */
4827
4828/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004829int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004830{
4831 struct pid *pid;
4832 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004833 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004834 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004835 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004836
4837 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004838 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004839 if (!buf)
4840 goto out;
4841
4842 retval = -ESRCH;
4843 pid = m->private;
4844 tsk = get_pid_task(pid, PIDTYPE_PID);
4845 if (!tsk)
4846 goto out_free;
4847
4848 retval = 0;
4849
4850 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004851 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004852
Tejun Heo985ed672014-03-19 10:23:53 -04004853 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004854 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004855 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004856 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004857
Tejun Heoa2dd4242014-03-19 10:23:55 -04004858 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004859 continue;
4860
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004861 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004862 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004863 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004864 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004865 if (strlen(root->name))
4866 seq_printf(m, "%sname=%s", count ? "," : "",
4867 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004868 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004869 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004870 path = cgroup_path(cgrp, buf, PATH_MAX);
4871 if (!path) {
4872 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004873 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004874 }
4875 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004876 seq_putc(m, '\n');
4877 }
4878
4879out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004880 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004881 mutex_unlock(&cgroup_mutex);
4882 put_task_struct(tsk);
4883out_free:
4884 kfree(buf);
4885out:
4886 return retval;
4887}
4888
Paul Menagea4243162007-10-18 23:39:35 -07004889/* Display information about each subsystem and each hierarchy */
4890static int proc_cgroupstats_show(struct seq_file *m, void *v)
4891{
Tejun Heo30159ec2013-06-25 11:53:37 -07004892 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004893 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004894
Paul Menage8bab8dd2008-04-04 14:29:57 -07004895 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004896 /*
4897 * ideally we don't want subsystems moving around while we do this.
4898 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4899 * subsys/hierarchy state.
4900 */
Paul Menagea4243162007-10-18 23:39:35 -07004901 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004902
4903 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004904 seq_printf(m, "%s\t%d\t%d\t%d\n",
4905 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004906 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004907
Paul Menagea4243162007-10-18 23:39:35 -07004908 mutex_unlock(&cgroup_mutex);
4909 return 0;
4910}
4911
4912static int cgroupstats_open(struct inode *inode, struct file *file)
4913{
Al Viro9dce07f2008-03-29 03:07:28 +00004914 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004915}
4916
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004917static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004918 .open = cgroupstats_open,
4919 .read = seq_read,
4920 .llseek = seq_lseek,
4921 .release = single_release,
4922};
4923
Paul Menageb4f48b62007-10-18 23:39:33 -07004924/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004925 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004926 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004927 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004928 * A task is associated with the init_css_set until cgroup_post_fork()
4929 * attaches it to the parent's css_set. Empty cg_list indicates that
4930 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004931 */
4932void cgroup_fork(struct task_struct *child)
4933{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004934 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004935 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004936}
4937
4938/**
Li Zefana043e3b2008-02-23 15:24:09 -08004939 * cgroup_post_fork - called on a new task after adding it to the task list
4940 * @child: the task in question
4941 *
Tejun Heo5edee612012-10-16 15:03:14 -07004942 * Adds the task to the list running through its css_set if necessary and
4943 * call the subsystem fork() callbacks. Has to be after the task is
4944 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004945 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004946 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004947 */
Paul Menage817929e2007-10-18 23:39:36 -07004948void cgroup_post_fork(struct task_struct *child)
4949{
Tejun Heo30159ec2013-06-25 11:53:37 -07004950 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004951 int i;
4952
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004953 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004954 * This may race against cgroup_enable_task_cg_links(). As that
4955 * function sets use_task_css_set_links before grabbing
4956 * tasklist_lock and we just went through tasklist_lock to add
4957 * @child, it's guaranteed that either we see the set
4958 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4959 * @child during its iteration.
4960 *
4961 * If we won the race, @child is associated with %current's
4962 * css_set. Grabbing css_set_rwsem guarantees both that the
4963 * association is stable, and, on completion of the parent's
4964 * migration, @child is visible in the source of migration or
4965 * already in the destination cgroup. This guarantee is necessary
4966 * when implementing operations which need to migrate all tasks of
4967 * a cgroup to another.
4968 *
4969 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4970 * will remain in init_css_set. This is safe because all tasks are
4971 * in the init_css_set before cg_links is enabled and there's no
4972 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004973 */
Paul Menage817929e2007-10-18 23:39:36 -07004974 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004975 struct css_set *cset;
4976
Tejun Heo96d365e2014-02-13 06:58:40 -05004977 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004978 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004979 if (list_empty(&child->cg_list)) {
4980 rcu_assign_pointer(child->cgroups, cset);
4981 list_add(&child->cg_list, &cset->tasks);
4982 get_css_set(cset);
4983 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004984 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004985 }
Tejun Heo5edee612012-10-16 15:03:14 -07004986
4987 /*
4988 * Call ss->fork(). This must happen after @child is linked on
4989 * css_set; otherwise, @child might change state between ->fork()
4990 * and addition to css_set.
4991 */
4992 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004993 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004994 if (ss->fork)
4995 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004996 }
Paul Menage817929e2007-10-18 23:39:36 -07004997}
Tejun Heo5edee612012-10-16 15:03:14 -07004998
Paul Menage817929e2007-10-18 23:39:36 -07004999/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005000 * cgroup_exit - detach cgroup from exiting task
5001 * @tsk: pointer to task_struct of exiting process
5002 *
5003 * Description: Detach cgroup from @tsk and release it.
5004 *
5005 * Note that cgroups marked notify_on_release force every task in
5006 * them to take the global cgroup_mutex mutex when exiting.
5007 * This could impact scaling on very large systems. Be reluctant to
5008 * use notify_on_release cgroups where very high task exit scaling
5009 * is required on large systems.
5010 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005011 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5012 * call cgroup_exit() while the task is still competent to handle
5013 * notify_on_release(), then leave the task attached to the root cgroup in
5014 * each hierarchy for the remainder of its exit. No need to bother with
5015 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005016 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005017 */
Li Zefan1ec41832014-03-28 15:22:19 +08005018void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005019{
Tejun Heo30159ec2013-06-25 11:53:37 -07005020 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005021 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005022 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005023 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005024
5025 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005026 * Unlink from @tsk from its css_set. As migration path can't race
5027 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005028 */
5029 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005030 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005031 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005032 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005033 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005034 }
5035
Paul Menageb4f48b62007-10-18 23:39:33 -07005036 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005037 cset = task_css_set(tsk);
5038 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005039
Li Zefan1ec41832014-03-28 15:22:19 +08005040 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005041 /* see cgroup_post_fork() for details */
5042 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005043 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005044 struct cgroup_subsys_state *old_css = cset->subsys[i];
5045 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005046
Tejun Heoeb954192013-08-08 20:11:23 -04005047 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005048 }
5049 }
5050 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005051
Tejun Heoeaf797a2014-02-25 10:04:03 -05005052 if (put_cset)
5053 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005054}
Paul Menage697f4162007-10-18 23:39:34 -07005055
Paul Menagebd89aab2007-10-18 23:40:44 -07005056static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005057{
Li Zefanf50daa72013-03-01 15:06:07 +08005058 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005059 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005060 /*
5061 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005063 * it now
5064 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005065 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005066
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005067 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005068 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005069 list_empty(&cgrp->release_list)) {
5070 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005071 need_schedule_work = 1;
5072 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005073 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005074 if (need_schedule_work)
5075 schedule_work(&release_agent_work);
5076 }
5077}
5078
Paul Menage81a6a5c2007-10-18 23:39:38 -07005079/*
5080 * Notify userspace when a cgroup is released, by running the
5081 * configured release agent with the name of the cgroup (path
5082 * relative to the root of cgroup file system) as the argument.
5083 *
5084 * Most likely, this user command will try to rmdir this cgroup.
5085 *
5086 * This races with the possibility that some other task will be
5087 * attached to this cgroup before it is removed, or that some other
5088 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5089 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5090 * unused, and this cgroup will be reprieved from its death sentence,
5091 * to continue to serve a useful existence. Next time it's released,
5092 * we will get notified again, if it still has 'notify_on_release' set.
5093 *
5094 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5095 * means only wait until the task is successfully execve()'d. The
5096 * separate release agent task is forked by call_usermodehelper(),
5097 * then control in this thread returns here, without waiting for the
5098 * release agent task. We don't bother to wait because the caller of
5099 * this routine has no use for the exit status of the release agent
5100 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005101 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005102static void cgroup_release_agent(struct work_struct *work)
5103{
5104 BUG_ON(work != &release_agent_work);
5105 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005106 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005107 while (!list_empty(&release_list)) {
5108 char *argv[3], *envp[3];
5109 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005110 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005111 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005112 struct cgroup,
5113 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005114 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005115 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005116 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005117 if (!pathbuf)
5118 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005119 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5120 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005121 goto continue_free;
5122 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5123 if (!agentbuf)
5124 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005125
5126 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005127 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005128 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005129 argv[i] = NULL;
5130
5131 i = 0;
5132 /* minimal command environment */
5133 envp[i++] = "HOME=/";
5134 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5135 envp[i] = NULL;
5136
5137 /* Drop the lock while we invoke the usermode helper,
5138 * since the exec could involve hitting disk and hence
5139 * be a slow process */
5140 mutex_unlock(&cgroup_mutex);
5141 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005142 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005143 continue_free:
5144 kfree(pathbuf);
5145 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005146 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005147 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005148 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005149 mutex_unlock(&cgroup_mutex);
5150}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005151
5152static int __init cgroup_disable(char *str)
5153{
Tejun Heo30159ec2013-06-25 11:53:37 -07005154 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005155 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005156 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005157
5158 while ((token = strsep(&str, ",")) != NULL) {
5159 if (!*token)
5160 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005161
Tejun Heo3ed80a62014-02-08 10:36:58 -05005162 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005163 if (!strcmp(token, ss->name)) {
5164 ss->disabled = 1;
5165 printk(KERN_INFO "Disabling %s control group"
5166 " subsystem\n", ss->name);
5167 break;
5168 }
5169 }
5170 }
5171 return 1;
5172}
5173__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005174
Tejun Heob77d7b62013-08-13 11:01:54 -04005175/**
Tejun Heoec903c02014-05-13 12:11:01 -04005176 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005177 * @dentry: directory dentry of interest
5178 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005179 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005180 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5181 * to get the corresponding css and return it. If such css doesn't exist
5182 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005183 */
Tejun Heoec903c02014-05-13 12:11:01 -04005184struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5185 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005186{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005187 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5188 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005189 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005190
Tejun Heo35cf0832013-08-26 18:40:56 -04005191 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005192 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5193 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005194 return ERR_PTR(-EBADF);
5195
Tejun Heo5a17f542014-02-11 11:52:47 -05005196 rcu_read_lock();
5197
Tejun Heo2bd59d42014-02-11 11:52:49 -05005198 /*
5199 * This path doesn't originate from kernfs and @kn could already
5200 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005201 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005202 */
5203 cgrp = rcu_dereference(kn->priv);
5204 if (cgrp)
5205 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005206
Tejun Heoec903c02014-05-13 12:11:01 -04005207 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005208 css = ERR_PTR(-ENOENT);
5209
5210 rcu_read_unlock();
5211 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005212}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005213
Li Zefan1cb650b2013-08-19 10:05:24 +08005214/**
5215 * css_from_id - lookup css by id
5216 * @id: the cgroup id
5217 * @ss: cgroup subsys to be looked into
5218 *
5219 * Returns the css if there's valid one with @id, otherwise returns NULL.
5220 * Should be called under rcu_read_lock().
5221 */
5222struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5223{
Tejun Heo6fa49182014-05-04 15:09:13 -04005224 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005225 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005226}
5227
Paul Menagefe693432009-09-23 15:56:20 -07005228#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005229static struct cgroup_subsys_state *
5230debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005231{
5232 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5233
5234 if (!css)
5235 return ERR_PTR(-ENOMEM);
5236
5237 return css;
5238}
5239
Tejun Heoeb954192013-08-08 20:11:23 -04005240static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005241{
Tejun Heoeb954192013-08-08 20:11:23 -04005242 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005243}
5244
Tejun Heo182446d2013-08-08 20:11:24 -04005245static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5246 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005247{
Tejun Heo182446d2013-08-08 20:11:24 -04005248 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005249}
5250
Tejun Heo182446d2013-08-08 20:11:24 -04005251static u64 current_css_set_read(struct cgroup_subsys_state *css,
5252 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005253{
5254 return (u64)(unsigned long)current->cgroups;
5255}
5256
Tejun Heo182446d2013-08-08 20:11:24 -04005257static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005258 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005259{
5260 u64 count;
5261
5262 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005263 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005264 rcu_read_unlock();
5265 return count;
5266}
5267
Tejun Heo2da8ca82013-12-05 12:28:04 -05005268static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005269{
Tejun Heo69d02062013-06-12 21:04:50 -07005270 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005271 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005272 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005273
Tejun Heoe61734c2014-02-12 09:29:50 -05005274 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5275 if (!name_buf)
5276 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005277
Tejun Heo96d365e2014-02-13 06:58:40 -05005278 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005279 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005280 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005281 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005282 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005283
Tejun Heoa2dd4242014-03-19 10:23:55 -04005284 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005285 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005286 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005287 }
5288 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005289 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005290 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005291 return 0;
5292}
5293
5294#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005295static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005296{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005297 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005298 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005299
Tejun Heo96d365e2014-02-13 06:58:40 -05005300 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005301 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005302 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005303 struct task_struct *task;
5304 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005305
Tejun Heo5abb8852013-06-12 21:04:49 -07005306 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005307
Tejun Heo5abb8852013-06-12 21:04:49 -07005308 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005309 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5310 goto overflow;
5311 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005312 }
Tejun Heoc7561122014-02-25 10:04:01 -05005313
5314 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5315 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5316 goto overflow;
5317 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5318 }
5319 continue;
5320 overflow:
5321 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005322 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005323 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005324 return 0;
5325}
5326
Tejun Heo182446d2013-08-08 20:11:24 -04005327static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005328{
Tejun Heo182446d2013-08-08 20:11:24 -04005329 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005330}
5331
5332static struct cftype debug_files[] = {
5333 {
Paul Menagefe693432009-09-23 15:56:20 -07005334 .name = "taskcount",
5335 .read_u64 = debug_taskcount_read,
5336 },
5337
5338 {
5339 .name = "current_css_set",
5340 .read_u64 = current_css_set_read,
5341 },
5342
5343 {
5344 .name = "current_css_set_refcount",
5345 .read_u64 = current_css_set_refcount_read,
5346 },
5347
5348 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005349 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005350 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005351 },
5352
5353 {
5354 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005355 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005356 },
5357
5358 {
Paul Menagefe693432009-09-23 15:56:20 -07005359 .name = "releasable",
5360 .read_u64 = releasable_read,
5361 },
Paul Menagefe693432009-09-23 15:56:20 -07005362
Tejun Heo4baf6e32012-04-01 12:09:55 -07005363 { } /* terminate */
5364};
Paul Menagefe693432009-09-23 15:56:20 -07005365
Tejun Heo073219e2014-02-08 10:36:58 -05005366struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005367 .css_alloc = debug_css_alloc,
5368 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005369 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005370};
5371#endif /* CONFIG_CGROUP_DEBUG */