blob: 8afddb1a1c6c58b9a0bcbe14fd89dd4aa5df8280 [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
Li Zefan2739d3c2013-01-21 18:18:33 +08001096static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001098 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099
Tejun Heoace2bee2014-02-11 11:52:47 -05001100 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001101 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001102}
1103
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001104/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001105 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001106 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001107 * @subsys_mask: mask of the subsystem ids whose files should be removed
1108 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001109static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001110{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001111 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001112 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001113
Tejun Heob420ba72013-07-12 12:34:02 -07001114 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001115 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001116
Tejun Heo69dfa002014-05-04 15:09:13 -04001117 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001118 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001119 list_for_each_entry(cfts, &ss->cfts, node)
1120 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001121 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122}
1123
Tejun Heo69dfa002014-05-04 15:09:13 -04001124static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125{
Tejun Heo30159ec2013-06-25 11:53:37 -07001126 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001127 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128
Tejun Heoace2bee2014-02-11 11:52:47 -05001129 lockdep_assert_held(&cgroup_tree_mutex);
1130 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001131
Tejun Heo5df36032014-03-19 10:23:54 -04001132 for_each_subsys(ss, ssid) {
1133 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001135
Tejun Heo7fd8c562014-04-23 11:13:16 -04001136 /* if @ss has non-root csses attached to it, can't move */
1137 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001138 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
Tejun Heo5df36032014-03-19 10:23:54 -04001140 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001141 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001142 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143 }
1144
Tejun Heoa2dd4242014-03-19 10:23:55 -04001145 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1146 if (ret) {
1147 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001148 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149
Tejun Heoa2dd4242014-03-19 10:23:55 -04001150 /*
1151 * Rebinding back to the default root is not allowed to
1152 * fail. Using both default and non-default roots should
1153 * be rare. Moving subsystems back and forth even more so.
1154 * Just warn about it and continue.
1155 */
1156 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001157 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001158 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001159 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001160 }
Tejun Heo5df36032014-03-19 10:23:54 -04001161 }
Tejun Heo31261212013-06-28 17:07:30 -07001162
1163 /*
1164 * Nothing can fail from this point on. Remove files for the
1165 * removed subsystems and rebind each subsystem.
1166 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001167 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001168 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001169 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001170 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001171 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001172
Tejun Heo5df36032014-03-19 10:23:54 -04001173 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001174 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001175 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001176 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001177
Tejun Heo5df36032014-03-19 10:23:54 -04001178 if (!(ss_mask & (1 << ssid)))
1179 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001180
Tejun Heo5df36032014-03-19 10:23:54 -04001181 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001182 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001183
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001184 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001185
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001186 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1187 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001188 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001189 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001190
Tejun Heo2d8f2432014-04-23 11:13:15 -04001191 down_write(&css_set_rwsem);
1192 hash_for_each(css_set_table, i, cset, hlist)
1193 list_move_tail(&cset->e_cset_node[ss->id],
1194 &dst_root->cgrp.e_csets[ss->id]);
1195 up_write(&css_set_rwsem);
1196
Tejun Heof392e512014-04-23 11:13:14 -04001197 src_root->subsys_mask &= ~(1 << ssid);
1198 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1199
Tejun Heobd53d612014-04-23 11:13:16 -04001200 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001201 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001202 if (dst_root != &cgrp_dfl_root)
1203 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001204
Tejun Heo5df36032014-03-19 10:23:54 -04001205 if (ss->bind)
1206 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001208
Tejun Heoa2dd4242014-03-19 10:23:55 -04001209 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001210 return 0;
1211}
1212
Tejun Heo2bd59d42014-02-11 11:52:49 -05001213static int cgroup_show_options(struct seq_file *seq,
1214 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001216 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001218 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001219
Tejun Heob85d2042013-12-06 15:11:57 -05001220 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001221 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001222 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001223 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1224 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001225 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001226 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001227 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001228 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001229
1230 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001231 if (strlen(root->release_agent_path))
1232 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001233 spin_unlock(&release_agent_path_lock);
1234
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001235 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001236 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001237 if (strlen(root->name))
1238 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 return 0;
1240}
1241
1242struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001243 unsigned int subsys_mask;
1244 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001245 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001246 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001247 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248 /* User explicitly requested empty subsystem */
1249 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001250};
1251
Ben Blumcf5d5942010-03-10 15:22:09 -08001252static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001254 char *token, *o = data;
1255 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001256 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001257 struct cgroup_subsys *ss;
1258 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001259
1260#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001261 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263
Paul Menagec6d57f32009-09-23 15:56:19 -07001264 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001265
1266 while ((token = strsep(&o, ",")) != NULL) {
1267 if (!*token)
1268 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001269 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270 /* Explicitly have no subsystems */
1271 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001272 continue;
1273 }
1274 if (!strcmp(token, "all")) {
1275 /* Mutually exclusive option 'all' + subsystem name */
1276 if (one_ss)
1277 return -EINVAL;
1278 all_ss = true;
1279 continue;
1280 }
Tejun Heo873fe092013-04-14 20:15:26 -07001281 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1282 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1283 continue;
1284 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001285 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001286 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001287 continue;
1288 }
1289 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001290 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001291 continue;
1292 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001293 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001294 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001295 continue;
1296 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001297 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001298 /* Specifying two release agents is forbidden */
1299 if (opts->release_agent)
1300 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001301 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001302 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001303 if (!opts->release_agent)
1304 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001305 continue;
1306 }
1307 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001308 const char *name = token + 5;
1309 /* Can't specify an empty name */
1310 if (!strlen(name))
1311 return -EINVAL;
1312 /* Must match [\w.-]+ */
1313 for (i = 0; i < strlen(name); i++) {
1314 char c = name[i];
1315 if (isalnum(c))
1316 continue;
1317 if ((c == '.') || (c == '-') || (c == '_'))
1318 continue;
1319 return -EINVAL;
1320 }
1321 /* Specifying two names is forbidden */
1322 if (opts->name)
1323 return -EINVAL;
1324 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001325 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001326 GFP_KERNEL);
1327 if (!opts->name)
1328 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001329
1330 continue;
1331 }
1332
Tejun Heo30159ec2013-06-25 11:53:37 -07001333 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001334 if (strcmp(token, ss->name))
1335 continue;
1336 if (ss->disabled)
1337 continue;
1338
1339 /* Mutually exclusive option 'all' + subsystem name */
1340 if (all_ss)
1341 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001342 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001343 one_ss = true;
1344
1345 break;
1346 }
1347 if (i == CGROUP_SUBSYS_COUNT)
1348 return -ENOENT;
1349 }
1350
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001351 /* Consistency checks */
1352
Tejun Heo873fe092013-04-14 20:15:26 -07001353 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001354 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001355
Tejun Heod3ba07c2014-02-13 06:58:38 -05001356 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1357 opts->cpuset_clone_children || opts->release_agent ||
1358 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001359 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001360 return -EINVAL;
1361 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001362 } else {
1363 /*
1364 * If the 'all' option was specified select all the
1365 * subsystems, otherwise if 'none', 'name=' and a subsystem
1366 * name options were not specified, let's default to 'all'
1367 */
1368 if (all_ss || (!one_ss && !opts->none && !opts->name))
1369 for_each_subsys(ss, i)
1370 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001371 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001372
Tejun Heoa2dd4242014-03-19 10:23:55 -04001373 /*
1374 * We either have to specify by name or by subsystems. (So
1375 * all empty hierarchies must have a name).
1376 */
1377 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001378 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001379 }
1380
Li Zefanf9ab5b52009-06-17 16:26:33 -07001381 /*
1382 * Option noprefix was introduced just for backward compatibility
1383 * with the old cpuset, so we allow noprefix only if mounting just
1384 * the cpuset subsystem.
1385 */
Tejun Heo93438622013-04-14 20:15:25 -07001386 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001387 return -EINVAL;
1388
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001389
1390 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001391 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001392 return -EINVAL;
1393
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 return 0;
1395}
1396
Tejun Heo2bd59d42014-02-11 11:52:49 -05001397static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398{
1399 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001400 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001402 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403
Tejun Heo873fe092013-04-14 20:15:26 -07001404 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001405 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001406 return -EINVAL;
1407 }
1408
Tejun Heoace2bee2014-02-11 11:52:47 -05001409 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410 mutex_lock(&cgroup_mutex);
1411
1412 /* See what subsystems are wanted */
1413 ret = parse_cgroupfs_options(data, &opts);
1414 if (ret)
1415 goto out_unlock;
1416
Tejun Heof392e512014-04-23 11:13:14 -04001417 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001418 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001419 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001420
Tejun Heof392e512014-04-23 11:13:14 -04001421 added_mask = opts.subsys_mask & ~root->subsys_mask;
1422 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001423
Ben Blumcf5d5942010-03-10 15:22:09 -08001424 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001425 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001426 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001427 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001428 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1429 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001431 goto out_unlock;
1432 }
1433
Tejun Heof172e672013-06-28 17:07:30 -07001434 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001435 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001436 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001437 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001438 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439
Tejun Heo5df36032014-03-19 10:23:54 -04001440 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001441 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001443
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001444 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001445
Tejun Heo69e943b2014-02-08 10:36:58 -05001446 if (opts.release_agent) {
1447 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001448 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001449 spin_unlock(&release_agent_path_lock);
1450 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001452 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001453 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001455 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456 return ret;
1457}
1458
Tejun Heoafeb0f92014-02-13 06:58:39 -05001459/*
1460 * To reduce the fork() overhead for systems that are not actually using
1461 * their cgroups capability, we don't maintain the lists running through
1462 * each css_set to its tasks until we see the list actually used - in other
1463 * words after the first mount.
1464 */
1465static bool use_task_css_set_links __read_mostly;
1466
1467static void cgroup_enable_task_cg_lists(void)
1468{
1469 struct task_struct *p, *g;
1470
Tejun Heo96d365e2014-02-13 06:58:40 -05001471 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001472
1473 if (use_task_css_set_links)
1474 goto out_unlock;
1475
1476 use_task_css_set_links = true;
1477
1478 /*
1479 * We need tasklist_lock because RCU is not safe against
1480 * while_each_thread(). Besides, a forking task that has passed
1481 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1482 * is not guaranteed to have its child immediately visible in the
1483 * tasklist if we walk through it with RCU.
1484 */
1485 read_lock(&tasklist_lock);
1486 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001487 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1488 task_css_set(p) != &init_css_set);
1489
1490 /*
1491 * We should check if the process is exiting, otherwise
1492 * it will race with cgroup_exit() in that the list
1493 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001494 * Do it while holding siglock so that we don't end up
1495 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001496 */
Tejun Heof153ad12014-02-25 09:56:49 -05001497 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001498 if (!(p->flags & PF_EXITING)) {
1499 struct css_set *cset = task_css_set(p);
1500
1501 list_add(&p->cg_list, &cset->tasks);
1502 get_css_set(cset);
1503 }
Tejun Heof153ad12014-02-25 09:56:49 -05001504 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001505 } while_each_thread(g, p);
1506 read_unlock(&tasklist_lock);
1507out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001508 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001509}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510
Paul Menagecc31edc2008-10-18 20:28:04 -07001511static void init_cgroup_housekeeping(struct cgroup *cgrp)
1512{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001513 struct cgroup_subsys *ss;
1514 int ssid;
1515
Tejun Heo2bd59d42014-02-11 11:52:49 -05001516 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001517 INIT_LIST_HEAD(&cgrp->sibling);
1518 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001519 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001520 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001521 INIT_LIST_HEAD(&cgrp->pidlists);
1522 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001523 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001524
1525 for_each_subsys(ss, ssid)
1526 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001527
1528 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001529}
Paul Menagec6d57f32009-09-23 15:56:19 -07001530
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001531static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001532 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001534 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001535
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001537 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001538 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001539 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001540 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541
Paul Menagec6d57f32009-09-23 15:56:19 -07001542 root->flags = opts->flags;
1543 if (opts->release_agent)
1544 strcpy(root->release_agent_path, opts->release_agent);
1545 if (opts->name)
1546 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001547 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001548 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001549}
1550
Tejun Heo69dfa002014-05-04 15:09:13 -04001551static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001552{
Tejun Heod427dfe2014-02-11 11:52:48 -05001553 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001554 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001555 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001556 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001557
Tejun Heod427dfe2014-02-11 11:52:48 -05001558 lockdep_assert_held(&cgroup_tree_mutex);
1559 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001560
Tejun Heo6fa49182014-05-04 15:09:13 -04001561 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001562 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001563 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001564 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001565
Tejun Heod427dfe2014-02-11 11:52:48 -05001566 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001567 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001568 * but that's OK - it can only be increased by someone holding
1569 * cgroup_lock, and that's us. The worst that can happen is that we
1570 * have some link structures left over
1571 */
1572 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001574 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
Tejun Heo985ed672014-03-19 10:23:53 -04001576 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001577 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001578 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579
Tejun Heo2bd59d42014-02-11 11:52:49 -05001580 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1581 KERNFS_ROOT_CREATE_DEACTIVATED,
1582 root_cgrp);
1583 if (IS_ERR(root->kf_root)) {
1584 ret = PTR_ERR(root->kf_root);
1585 goto exit_root_id;
1586 }
1587 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588
Tejun Heod427dfe2014-02-11 11:52:48 -05001589 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1590 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001591 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
Tejun Heo5df36032014-03-19 10:23:54 -04001593 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001594 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001595 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001596
Tejun Heod427dfe2014-02-11 11:52:48 -05001597 /*
1598 * There must be no failure case after here, since rebinding takes
1599 * care of subsystems' refcounts, which are explicitly dropped in
1600 * the failure exit path.
1601 */
1602 list_add(&root->root_list, &cgroup_roots);
1603 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604
Tejun Heod427dfe2014-02-11 11:52:48 -05001605 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001606 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 * objects.
1608 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001609 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001610 hash_for_each(css_set_table, i, cset, hlist)
1611 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001612 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Tejun Heod427dfe2014-02-11 11:52:48 -05001614 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001615 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001616
Tejun Heo2bd59d42014-02-11 11:52:49 -05001617 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001618 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001619 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001620
Tejun Heo2bd59d42014-02-11 11:52:49 -05001621destroy_root:
1622 kernfs_destroy_root(root->kf_root);
1623 root->kf_root = NULL;
1624exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001625 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001626out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001627 free_cgrp_cset_links(&tmp_links);
1628 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001629}
1630
Al Virof7e83572010-07-26 13:23:11 +04001631static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001633 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001635 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001637 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001638 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001639 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001640
1641 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001642 * The first time anyone tries to mount a cgroup, enable the list
1643 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001644 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001645 if (!use_task_css_set_links)
1646 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001647
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001648 mutex_lock(&cgroup_tree_mutex);
1649 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001654 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001655retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001656 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001657 if (!opts.subsys_mask && !opts.none && !opts.name) {
1658 cgrp_dfl_root_visible = true;
1659 root = &cgrp_dfl_root;
1660 cgroup_get(&root->cgrp);
1661 ret = 0;
1662 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 }
1664
Tejun Heo985ed672014-03-19 10:23:53 -04001665 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001666 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001668 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001669 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001670
Paul Menage817929e2007-10-18 23:39:36 -07001671 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001672 * If we asked for a name then it must match. Also, if
1673 * name matches but sybsys_mask doesn't, we should fail.
1674 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001675 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001676 if (opts.name) {
1677 if (strcmp(opts.name, root->name))
1678 continue;
1679 name_match = true;
1680 }
Tejun Heo31261212013-06-28 17:07:30 -07001681
1682 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001683 * If we asked for subsystems (or explicitly for no
1684 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001685 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001686 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001687 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001688 if (!name_match)
1689 continue;
1690 ret = -EBUSY;
1691 goto out_unlock;
1692 }
Tejun Heo873fe092013-04-14 20:15:26 -07001693
Tejun Heoc7ba8282013-06-29 14:06:10 -07001694 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001695 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001696 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001697 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001698 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001699 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001700 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001701 }
Tejun Heo873fe092013-04-14 20:15:26 -07001702 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001703
Tejun Heo776f02f2014-02-12 09:29:50 -05001704 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001705 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001706 * ref indicate that the root is being destroyed. Wait for
1707 * destruction to complete so that the subsystems are free.
1708 * We can use wait_queue for the wait but this path is
1709 * super cold. Let's just sleep for a bit and retry.
1710 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001711 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001712 mutex_unlock(&cgroup_mutex);
1713 mutex_unlock(&cgroup_tree_mutex);
1714 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001715 mutex_lock(&cgroup_tree_mutex);
1716 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001717 goto retry;
1718 }
1719
1720 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001721 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722 }
1723
Tejun Heo172a2c062014-03-19 10:23:53 -04001724 /*
1725 * No such thing, create a new one. name= matching without subsys
1726 * specification is allowed for already existing hierarchies but we
1727 * can't create new one without subsys specification.
1728 */
1729 if (!opts.subsys_mask && !opts.none) {
1730 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001731 goto out_unlock;
1732 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733
Tejun Heo172a2c062014-03-19 10:23:53 -04001734 root = kzalloc(sizeof(*root), GFP_KERNEL);
1735 if (!root) {
1736 ret = -ENOMEM;
1737 goto out_unlock;
1738 }
1739
1740 init_cgroup_root(root, &opts);
1741
Tejun Heo35585572014-02-13 06:58:38 -05001742 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001743 if (ret)
1744 cgroup_free_root(root);
1745
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001746out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001747 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001748 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001749
Paul Menagec6d57f32009-09-23 15:56:19 -07001750 kfree(opts.release_agent);
1751 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001752
Tejun Heo2bd59d42014-02-11 11:52:49 -05001753 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001754 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001755
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001756 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1757 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001758 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001759 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760}
1761
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001762static void cgroup_kill_sb(struct super_block *sb)
1763{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001764 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001765 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001767 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001768 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769}
1770
1771static struct file_system_type cgroup_fs_type = {
1772 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001773 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 .kill_sb = cgroup_kill_sb,
1775};
1776
Greg KH676db4a2010-08-05 13:53:35 -07001777static struct kobject *cgroup_kobj;
1778
Li Zefana043e3b2008-02-23 15:24:09 -08001779/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001780 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001781 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001782 * @buf: the buffer to write the path into
1783 * @buflen: the length of the buffer
1784 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001785 * Determine @task's cgroup on the first (the one with the lowest non-zero
1786 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1787 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1788 * cgroup controller callbacks.
1789 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001790 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001791 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001792char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001793{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001794 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001795 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001796 int hierarchy_id = 1;
1797 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001798
1799 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001800 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001801
Tejun Heo913ffdb2013-07-11 16:34:48 -07001802 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1803
Tejun Heo857a2be2013-04-14 20:50:08 -07001804 if (root) {
1805 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001806 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001807 } else {
1808 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001809 if (strlcpy(buf, "/", buflen) < buflen)
1810 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001811 }
1812
Tejun Heo96d365e2014-02-13 06:58:40 -05001813 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001814 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001815 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001816}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001817EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001818
Tejun Heob3dc0942014-02-25 10:04:01 -05001819/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001820struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001821 /* the src and dst cset list running through cset->mg_node */
1822 struct list_head src_csets;
1823 struct list_head dst_csets;
1824
1825 /*
1826 * Fields for cgroup_taskset_*() iteration.
1827 *
1828 * Before migration is committed, the target migration tasks are on
1829 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1830 * the csets on ->dst_csets. ->csets point to either ->src_csets
1831 * or ->dst_csets depending on whether migration is committed.
1832 *
1833 * ->cur_csets and ->cur_task point to the current task position
1834 * during iteration.
1835 */
1836 struct list_head *csets;
1837 struct css_set *cur_cset;
1838 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001839};
1840
1841/**
1842 * cgroup_taskset_first - reset taskset and return the first task
1843 * @tset: taskset of interest
1844 *
1845 * @tset iteration is initialized and the first task is returned.
1846 */
1847struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1848{
Tejun Heob3dc0942014-02-25 10:04:01 -05001849 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1850 tset->cur_task = NULL;
1851
1852 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001853}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001854
1855/**
1856 * cgroup_taskset_next - iterate to the next task in taskset
1857 * @tset: taskset of interest
1858 *
1859 * Return the next task in @tset. Iteration must have been initialized
1860 * with cgroup_taskset_first().
1861 */
1862struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1863{
Tejun Heob3dc0942014-02-25 10:04:01 -05001864 struct css_set *cset = tset->cur_cset;
1865 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001866
Tejun Heob3dc0942014-02-25 10:04:01 -05001867 while (&cset->mg_node != tset->csets) {
1868 if (!task)
1869 task = list_first_entry(&cset->mg_tasks,
1870 struct task_struct, cg_list);
1871 else
1872 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001873
Tejun Heob3dc0942014-02-25 10:04:01 -05001874 if (&task->cg_list != &cset->mg_tasks) {
1875 tset->cur_cset = cset;
1876 tset->cur_task = task;
1877 return task;
1878 }
1879
1880 cset = list_next_entry(cset, mg_node);
1881 task = NULL;
1882 }
1883
1884 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001885}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886
1887/**
Ben Blum74a11662011-05-26 16:25:20 -07001888 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001889 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001890 * @tsk: the task being migrated
1891 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001892 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001893 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001894 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001895static void cgroup_task_migrate(struct cgroup *old_cgrp,
1896 struct task_struct *tsk,
1897 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001898{
Tejun Heo5abb8852013-06-12 21:04:49 -07001899 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001900
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001901 lockdep_assert_held(&cgroup_mutex);
1902 lockdep_assert_held(&css_set_rwsem);
1903
Ben Blum74a11662011-05-26 16:25:20 -07001904 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001905 * We are synchronized through threadgroup_lock() against PF_EXITING
1906 * setting such that we can't race against cgroup_exit() changing the
1907 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001908 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001909 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001910 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001911
Tejun Heob3dc0942014-02-25 10:04:01 -05001912 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001913 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001914
Tejun Heo1b9aba42014-03-19 17:43:21 -04001915 /*
1916 * Use move_tail so that cgroup_taskset_first() still returns the
1917 * leader after migration. This works because cgroup_migrate()
1918 * ensures that the dst_cset of the leader is the first on the
1919 * tset's dst_csets list.
1920 */
1921 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001922
1923 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001924 * We just gained a reference on old_cset by taking it from the
1925 * task. As trading it for new_cset is protected by cgroup_mutex,
1926 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001927 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001928 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001929 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001930}
1931
Li Zefana043e3b2008-02-23 15:24:09 -08001932/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001933 * cgroup_migrate_finish - cleanup after attach
1934 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001935 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001936 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1937 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001938 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001939static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001940{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001941 struct css_set *cset, *tmp_cset;
1942
1943 lockdep_assert_held(&cgroup_mutex);
1944
1945 down_write(&css_set_rwsem);
1946 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1947 cset->mg_src_cgrp = NULL;
1948 cset->mg_dst_cset = NULL;
1949 list_del_init(&cset->mg_preload_node);
1950 put_css_set_locked(cset, false);
1951 }
1952 up_write(&css_set_rwsem);
1953}
1954
1955/**
1956 * cgroup_migrate_add_src - add a migration source css_set
1957 * @src_cset: the source css_set to add
1958 * @dst_cgrp: the destination cgroup
1959 * @preloaded_csets: list of preloaded css_sets
1960 *
1961 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1962 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1963 * up by cgroup_migrate_finish().
1964 *
1965 * This function may be called without holding threadgroup_lock even if the
1966 * target is a process. Threads may be created and destroyed but as long
1967 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1968 * the preloaded css_sets are guaranteed to cover all migrations.
1969 */
1970static void cgroup_migrate_add_src(struct css_set *src_cset,
1971 struct cgroup *dst_cgrp,
1972 struct list_head *preloaded_csets)
1973{
1974 struct cgroup *src_cgrp;
1975
1976 lockdep_assert_held(&cgroup_mutex);
1977 lockdep_assert_held(&css_set_rwsem);
1978
1979 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1980
Tejun Heo1958d2d2014-02-25 10:04:03 -05001981 if (!list_empty(&src_cset->mg_preload_node))
1982 return;
1983
1984 WARN_ON(src_cset->mg_src_cgrp);
1985 WARN_ON(!list_empty(&src_cset->mg_tasks));
1986 WARN_ON(!list_empty(&src_cset->mg_node));
1987
1988 src_cset->mg_src_cgrp = src_cgrp;
1989 get_css_set(src_cset);
1990 list_add(&src_cset->mg_preload_node, preloaded_csets);
1991}
1992
1993/**
1994 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04001995 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05001996 * @preloaded_csets: list of preloaded source css_sets
1997 *
1998 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1999 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002000 * pins all destination css_sets, links each to its source, and append them
2001 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2002 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002003 *
2004 * This function must be called after cgroup_migrate_add_src() has been
2005 * called on each migration source css_set. After migration is performed
2006 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2007 * @preloaded_csets.
2008 */
2009static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2010 struct list_head *preloaded_csets)
2011{
2012 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002013 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002014
2015 lockdep_assert_held(&cgroup_mutex);
2016
Tejun Heof8f22e52014-04-23 11:13:16 -04002017 /*
2018 * Except for the root, child_subsys_mask must be zero for a cgroup
2019 * with tasks so that child cgroups don't compete against tasks.
2020 */
2021 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2022 dst_cgrp->child_subsys_mask)
2023 return -EBUSY;
2024
Tejun Heo1958d2d2014-02-25 10:04:03 -05002025 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002026 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002027 struct css_set *dst_cset;
2028
Tejun Heof817de92014-04-23 11:13:16 -04002029 dst_cset = find_css_set(src_cset,
2030 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002031 if (!dst_cset)
2032 goto err;
2033
2034 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002035
2036 /*
2037 * If src cset equals dst, it's noop. Drop the src.
2038 * cgroup_migrate() will skip the cset too. Note that we
2039 * can't handle src == dst as some nodes are used by both.
2040 */
2041 if (src_cset == dst_cset) {
2042 src_cset->mg_src_cgrp = NULL;
2043 list_del_init(&src_cset->mg_preload_node);
2044 put_css_set(src_cset, false);
2045 put_css_set(dst_cset, false);
2046 continue;
2047 }
2048
Tejun Heo1958d2d2014-02-25 10:04:03 -05002049 src_cset->mg_dst_cset = dst_cset;
2050
2051 if (list_empty(&dst_cset->mg_preload_node))
2052 list_add(&dst_cset->mg_preload_node, &csets);
2053 else
2054 put_css_set(dst_cset, false);
2055 }
2056
Tejun Heof817de92014-04-23 11:13:16 -04002057 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002058 return 0;
2059err:
2060 cgroup_migrate_finish(&csets);
2061 return -ENOMEM;
2062}
2063
2064/**
2065 * cgroup_migrate - migrate a process or task to a cgroup
2066 * @cgrp: the destination cgroup
2067 * @leader: the leader of the process or the task to migrate
2068 * @threadgroup: whether @leader points to the whole process or a single task
2069 *
2070 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2071 * process, the caller must be holding threadgroup_lock of @leader. The
2072 * caller is also responsible for invoking cgroup_migrate_add_src() and
2073 * cgroup_migrate_prepare_dst() on the targets before invoking this
2074 * function and following up with cgroup_migrate_finish().
2075 *
2076 * As long as a controller's ->can_attach() doesn't fail, this function is
2077 * guaranteed to succeed. This means that, excluding ->can_attach()
2078 * failure, when migrating multiple targets, the success or failure can be
2079 * decided for all targets by invoking group_migrate_prepare_dst() before
2080 * actually starting migrating.
2081 */
2082static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2083 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002084{
Tejun Heob3dc0942014-02-25 10:04:01 -05002085 struct cgroup_taskset tset = {
2086 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2087 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2088 .csets = &tset.src_csets,
2089 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002090 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002091 struct css_set *cset, *tmp_cset;
2092 struct task_struct *task, *tmp_task;
2093 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002094
2095 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002096 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2097 * already PF_EXITING could be freed from underneath us unless we
2098 * take an rcu_read_lock.
2099 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002100 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002101 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002102 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002103 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002104 /* @task either already exited or can't exit until the end */
2105 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002106 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002107
Tejun Heoeaf797a2014-02-25 10:04:03 -05002108 /* leave @task alone if post_fork() hasn't linked it yet */
2109 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002110 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002111
Tejun Heob3dc0942014-02-25 10:04:01 -05002112 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002113 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002114 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002115
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002116 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002117 * cgroup_taskset_first() must always return the leader.
2118 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002119 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002120 list_move_tail(&task->cg_list, &cset->mg_tasks);
2121 if (list_empty(&cset->mg_node))
2122 list_add_tail(&cset->mg_node, &tset.src_csets);
2123 if (list_empty(&cset->mg_dst_cset->mg_node))
2124 list_move_tail(&cset->mg_dst_cset->mg_node,
2125 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002126 next:
Li Zefan081aa452013-03-13 09:17:09 +08002127 if (!threadgroup)
2128 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002129 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002130 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002131 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002132
Tejun Heo134d3372011-12-12 18:12:21 -08002133 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002134 if (list_empty(&tset.src_csets))
2135 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002136
Tejun Heo1958d2d2014-02-25 10:04:03 -05002137 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002138 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002139 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002140 ret = css->ss->can_attach(css, &tset);
2141 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002142 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002143 goto out_cancel_attach;
2144 }
2145 }
Ben Blum74a11662011-05-26 16:25:20 -07002146 }
2147
2148 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002149 * Now that we're guaranteed success, proceed to move all tasks to
2150 * the new cgroup. There are no failure cases after here, so this
2151 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002152 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002153 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002154 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2155 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2156 cgroup_task_migrate(cset->mg_src_cgrp, task,
2157 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002158 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002159 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002160
2161 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002162 * Migration is committed, all target tasks are now on dst_csets.
2163 * Nothing is sensitive to fork() after this point. Notify
2164 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002165 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002166 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002167
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002168 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002169 if (css->ss->attach)
2170 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002171
Tejun Heo9db8de32014-02-13 06:58:43 -05002172 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002173 goto out_release_tset;
2174
Ben Blum74a11662011-05-26 16:25:20 -07002175out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002176 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002177 if (css == failed_css)
2178 break;
2179 if (css->ss->cancel_attach)
2180 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002181 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002182out_release_tset:
2183 down_write(&css_set_rwsem);
2184 list_splice_init(&tset.dst_csets, &tset.src_csets);
2185 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002186 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002187 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002188 }
2189 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002190 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002191}
2192
Tejun Heo1958d2d2014-02-25 10:04:03 -05002193/**
2194 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2195 * @dst_cgrp: the cgroup to attach to
2196 * @leader: the task or the leader of the threadgroup to be attached
2197 * @threadgroup: attach the whole threadgroup?
2198 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002199 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002200 */
2201static int cgroup_attach_task(struct cgroup *dst_cgrp,
2202 struct task_struct *leader, bool threadgroup)
2203{
2204 LIST_HEAD(preloaded_csets);
2205 struct task_struct *task;
2206 int ret;
2207
2208 /* look up all src csets */
2209 down_read(&css_set_rwsem);
2210 rcu_read_lock();
2211 task = leader;
2212 do {
2213 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2214 &preloaded_csets);
2215 if (!threadgroup)
2216 break;
2217 } while_each_thread(leader, task);
2218 rcu_read_unlock();
2219 up_read(&css_set_rwsem);
2220
2221 /* prepare dst csets and commit */
2222 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2223 if (!ret)
2224 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2225
2226 cgroup_migrate_finish(&preloaded_csets);
2227 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002228}
2229
2230/*
2231 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002232 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002233 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002234 */
Tejun Heoacbef752014-05-13 12:16:22 -04002235static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2236 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002237{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002238 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002239 const struct cred *cred = current_cred(), *tcred;
Tejun Heoacbef752014-05-13 12:16:22 -04002240 struct cgroup *cgrp = of_css(of)->cgroup;
2241 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002242 int ret;
2243
Tejun Heoacbef752014-05-13 12:16:22 -04002244 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2245 return -EINVAL;
2246
Ben Blum74a11662011-05-26 16:25:20 -07002247 if (!cgroup_lock_live_group(cgrp))
2248 return -ENODEV;
2249
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002250retry_find_task:
2251 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002252 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002253 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002254 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002255 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002256 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002257 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002258 }
Ben Blum74a11662011-05-26 16:25:20 -07002259 /*
2260 * even if we're attaching all tasks in the thread group, we
2261 * only need to check permissions on one of them.
2262 */
David Howellsc69e8d92008-11-14 10:39:19 +11002263 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002264 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2265 !uid_eq(cred->euid, tcred->uid) &&
2266 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002267 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002268 ret = -EACCES;
2269 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002270 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002271 } else
2272 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002273
2274 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002275 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002276
2277 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002278 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002279 * trapped in a cpuset, or RT worker may be born in a cgroup
2280 * with no rt_runtime allocated. Just say no.
2281 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002282 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002283 ret = -EINVAL;
2284 rcu_read_unlock();
2285 goto out_unlock_cgroup;
2286 }
2287
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002288 get_task_struct(tsk);
2289 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002290
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002291 threadgroup_lock(tsk);
2292 if (threadgroup) {
2293 if (!thread_group_leader(tsk)) {
2294 /*
2295 * a race with de_thread from another thread's exec()
2296 * may strip us of our leadership, if this happens,
2297 * there is no choice but to throw this task away and
2298 * try again; this is
2299 * "double-double-toil-and-trouble-check locking".
2300 */
2301 threadgroup_unlock(tsk);
2302 put_task_struct(tsk);
2303 goto retry_find_task;
2304 }
Li Zefan081aa452013-03-13 09:17:09 +08002305 }
2306
2307 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2308
Tejun Heocd3d0952011-12-12 18:12:21 -08002309 threadgroup_unlock(tsk);
2310
Paul Menagebbcb81d2007-10-18 23:39:32 -07002311 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002312out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002313 mutex_unlock(&cgroup_mutex);
Tejun Heoacbef752014-05-13 12:16:22 -04002314 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002315}
2316
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002317/**
2318 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2319 * @from: attach to all cgroups of a given task
2320 * @tsk: the task to be attached
2321 */
2322int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2323{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002324 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002325 int retval = 0;
2326
Tejun Heo47cfcd02013-04-07 09:29:51 -07002327 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002328 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002329 struct cgroup *from_cgrp;
2330
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002331 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002332 continue;
2333
Tejun Heo96d365e2014-02-13 06:58:40 -05002334 down_read(&css_set_rwsem);
2335 from_cgrp = task_cgroup_from_root(from, root);
2336 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002337
Li Zefan6f4b7e62013-07-31 16:18:36 +08002338 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002339 if (retval)
2340 break;
2341 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002342 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002343
2344 return retval;
2345}
2346EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2347
Tejun Heoacbef752014-05-13 12:16:22 -04002348static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2349 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002350{
Tejun Heoacbef752014-05-13 12:16:22 -04002351 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002352}
2353
Tejun Heoacbef752014-05-13 12:16:22 -04002354static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2355 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002356{
Tejun Heoacbef752014-05-13 12:16:22 -04002357 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002358}
2359
Tejun Heo451af502014-05-13 12:16:21 -04002360static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2361 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002362{
Tejun Heo451af502014-05-13 12:16:21 -04002363 struct cgroup *cgrp = of_css(of)->cgroup;
2364 struct cgroup_root *root = cgrp->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002365
2366 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo451af502014-05-13 12:16:21 -04002367 if (!cgroup_lock_live_group(cgrp))
Paul Menagee788e062008-07-25 01:46:59 -07002368 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002369 spin_lock(&release_agent_path_lock);
Tejun Heo451af502014-05-13 12:16:21 -04002370 strlcpy(root->release_agent_path, strstrip(buf),
Tejun Heo5f469902014-02-11 11:52:48 -05002371 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002372 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002373 mutex_unlock(&cgroup_mutex);
Tejun Heo451af502014-05-13 12:16:21 -04002374 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002375}
2376
Tejun Heo2da8ca82013-12-05 12:28:04 -05002377static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002378{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002379 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002380
Tejun Heo46cfeb02014-05-13 12:11:00 -04002381 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002382 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002383 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002384 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002385 return 0;
2386}
2387
Tejun Heo2da8ca82013-12-05 12:28:04 -05002388static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002389{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002390 struct cgroup *cgrp = seq_css(seq)->cgroup;
2391
2392 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002393 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394}
2395
Tejun Heof8f22e52014-04-23 11:13:16 -04002396static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2397{
2398 struct cgroup_subsys *ss;
2399 bool printed = false;
2400 int ssid;
2401
2402 for_each_subsys(ss, ssid) {
2403 if (ss_mask & (1 << ssid)) {
2404 if (printed)
2405 seq_putc(seq, ' ');
2406 seq_printf(seq, "%s", ss->name);
2407 printed = true;
2408 }
2409 }
2410 if (printed)
2411 seq_putc(seq, '\n');
2412}
2413
2414/* show controllers which are currently attached to the default hierarchy */
2415static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2416{
2417 struct cgroup *cgrp = seq_css(seq)->cgroup;
2418
2419 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2420 return 0;
2421}
2422
2423/* show controllers which are enabled from the parent */
2424static int cgroup_controllers_show(struct seq_file *seq, void *v)
2425{
2426 struct cgroup *cgrp = seq_css(seq)->cgroup;
2427
2428 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2429 return 0;
2430}
2431
2432/* show controllers which are enabled for a given cgroup's children */
2433static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2434{
2435 struct cgroup *cgrp = seq_css(seq)->cgroup;
2436
2437 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2438 return 0;
2439}
2440
2441/**
2442 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2443 * @cgrp: root of the subtree to update csses for
2444 *
2445 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2446 * css associations need to be updated accordingly. This function looks up
2447 * all css_sets which are attached to the subtree, creates the matching
2448 * updated css_sets and migrates the tasks to the new ones.
2449 */
2450static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2451{
2452 LIST_HEAD(preloaded_csets);
2453 struct cgroup_subsys_state *css;
2454 struct css_set *src_cset;
2455 int ret;
2456
2457 lockdep_assert_held(&cgroup_tree_mutex);
2458 lockdep_assert_held(&cgroup_mutex);
2459
2460 /* look up all csses currently attached to @cgrp's subtree */
2461 down_read(&css_set_rwsem);
2462 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2463 struct cgrp_cset_link *link;
2464
2465 /* self is not affected by child_subsys_mask change */
2466 if (css->cgroup == cgrp)
2467 continue;
2468
2469 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2470 cgroup_migrate_add_src(link->cset, cgrp,
2471 &preloaded_csets);
2472 }
2473 up_read(&css_set_rwsem);
2474
2475 /* NULL dst indicates self on default hierarchy */
2476 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2477 if (ret)
2478 goto out_finish;
2479
2480 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2481 struct task_struct *last_task = NULL, *task;
2482
2483 /* src_csets precede dst_csets, break on the first dst_cset */
2484 if (!src_cset->mg_src_cgrp)
2485 break;
2486
2487 /*
2488 * All tasks in src_cset need to be migrated to the
2489 * matching dst_cset. Empty it process by process. We
2490 * walk tasks but migrate processes. The leader might even
2491 * belong to a different cset but such src_cset would also
2492 * be among the target src_csets because the default
2493 * hierarchy enforces per-process membership.
2494 */
2495 while (true) {
2496 down_read(&css_set_rwsem);
2497 task = list_first_entry_or_null(&src_cset->tasks,
2498 struct task_struct, cg_list);
2499 if (task) {
2500 task = task->group_leader;
2501 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2502 get_task_struct(task);
2503 }
2504 up_read(&css_set_rwsem);
2505
2506 if (!task)
2507 break;
2508
2509 /* guard against possible infinite loop */
2510 if (WARN(last_task == task,
2511 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2512 goto out_finish;
2513 last_task = task;
2514
2515 threadgroup_lock(task);
2516 /* raced against de_thread() from another thread? */
2517 if (!thread_group_leader(task)) {
2518 threadgroup_unlock(task);
2519 put_task_struct(task);
2520 continue;
2521 }
2522
2523 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2524
2525 threadgroup_unlock(task);
2526 put_task_struct(task);
2527
2528 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2529 goto out_finish;
2530 }
2531 }
2532
2533out_finish:
2534 cgroup_migrate_finish(&preloaded_csets);
2535 return ret;
2536}
2537
2538/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002539static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2540 char *buf, size_t nbytes,
2541 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002542{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002543 unsigned int enable = 0, disable = 0;
Tejun Heo451af502014-05-13 12:16:21 -04002544 struct cgroup *cgrp = of_css(of)->cgroup, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002545 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002546 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002547 int ssid, ret;
2548
2549 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002550 * Parse input - space separated list of subsystem names prefixed
2551 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002552 */
Tejun Heo451af502014-05-13 12:16:21 -04002553 buf = strstrip(buf);
2554 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002555 if (tok[0] == '\0')
2556 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002557 for_each_subsys(ss, ssid) {
2558 if (ss->disabled || strcmp(tok + 1, ss->name))
2559 continue;
2560
2561 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002562 enable |= 1 << ssid;
2563 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002564 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002565 disable |= 1 << ssid;
2566 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002567 } else {
2568 return -EINVAL;
2569 }
2570 break;
2571 }
2572 if (ssid == CGROUP_SUBSYS_COUNT)
2573 return -EINVAL;
2574 }
2575
2576 /*
2577 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2578 * active_ref. cgroup_lock_live_group() already provides enough
2579 * protection. Ensure @cgrp stays accessible and break the
2580 * active_ref protection.
2581 */
2582 cgroup_get(cgrp);
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002583 kernfs_break_active_protection(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002584
2585 mutex_lock(&cgroup_tree_mutex);
Tejun Heoddab2b62014-05-13 12:19:22 -04002586 if (!cgroup_lock_live_group(cgrp)) {
2587 ret = -ENODEV;
2588 goto out_unlock_tree;
2589 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002590
2591 for_each_subsys(ss, ssid) {
2592 if (enable & (1 << ssid)) {
2593 if (cgrp->child_subsys_mask & (1 << ssid)) {
2594 enable &= ~(1 << ssid);
2595 continue;
2596 }
2597
2598 /*
2599 * Because css offlining is asynchronous, userland
2600 * might try to re-enable the same controller while
2601 * the previous instance is still around. In such
2602 * cases, wait till it's gone using offline_waitq.
2603 */
2604 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002605 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002606
2607 if (!cgroup_css(child, ss))
2608 continue;
2609
Tejun Heo0cee8b72014-05-13 12:10:59 -04002610 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002611 prepare_to_wait(&child->offline_waitq, &wait,
2612 TASK_UNINTERRUPTIBLE);
Tejun Heoddab2b62014-05-13 12:19:22 -04002613 mutex_unlock(&cgroup_mutex);
Tejun Heof8f22e52014-04-23 11:13:16 -04002614 mutex_unlock(&cgroup_tree_mutex);
2615 schedule();
2616 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002617 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002618
2619 ret = restart_syscall();
2620 goto out_unbreak;
Tejun Heof8f22e52014-04-23 11:13:16 -04002621 }
2622
2623 /* unavailable or not enabled on the parent? */
2624 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2625 (cgrp->parent &&
2626 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2627 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002628 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002629 }
2630 } else if (disable & (1 << ssid)) {
2631 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2632 disable &= ~(1 << ssid);
2633 continue;
2634 }
2635
2636 /* a child has it enabled? */
2637 cgroup_for_each_live_child(child, cgrp) {
2638 if (child->child_subsys_mask & (1 << ssid)) {
2639 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002640 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002641 }
2642 }
2643 }
2644 }
2645
2646 if (!enable && !disable) {
2647 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002648 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002649 }
2650
2651 /*
2652 * Except for the root, child_subsys_mask must be zero for a cgroup
2653 * with tasks so that child cgroups don't compete against tasks.
2654 */
2655 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2656 ret = -EBUSY;
2657 goto out_unlock;
2658 }
2659
2660 /*
2661 * Create csses for enables and update child_subsys_mask. This
2662 * changes cgroup_e_css() results which in turn makes the
2663 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2664 * subtree to the updated csses.
2665 */
2666 for_each_subsys(ss, ssid) {
2667 if (!(enable & (1 << ssid)))
2668 continue;
2669
2670 cgroup_for_each_live_child(child, cgrp) {
2671 ret = create_css(child, ss);
2672 if (ret)
2673 goto err_undo_css;
2674 }
2675 }
2676
2677 cgrp->child_subsys_mask |= enable;
2678 cgrp->child_subsys_mask &= ~disable;
2679
2680 ret = cgroup_update_dfl_csses(cgrp);
2681 if (ret)
2682 goto err_undo_css;
2683
2684 /* all tasks are now migrated away from the old csses, kill them */
2685 for_each_subsys(ss, ssid) {
2686 if (!(disable & (1 << ssid)))
2687 continue;
2688
2689 cgroup_for_each_live_child(child, cgrp)
2690 kill_css(cgroup_css(child, ss));
2691 }
2692
2693 kernfs_activate(cgrp->kn);
2694 ret = 0;
2695out_unlock:
2696 mutex_unlock(&cgroup_mutex);
2697out_unlock_tree:
2698 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002699out_unbreak:
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002700 kernfs_unbreak_active_protection(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002701 cgroup_put(cgrp);
Tejun Heo451af502014-05-13 12:16:21 -04002702 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002703
2704err_undo_css:
2705 cgrp->child_subsys_mask &= ~enable;
2706 cgrp->child_subsys_mask |= disable;
2707
2708 for_each_subsys(ss, ssid) {
2709 if (!(enable & (1 << ssid)))
2710 continue;
2711
2712 cgroup_for_each_live_child(child, cgrp) {
2713 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2714 if (css)
2715 kill_css(css);
2716 }
2717 }
2718 goto out_unlock;
2719}
2720
Tejun Heo842b5972014-04-25 18:28:02 -04002721static int cgroup_populated_show(struct seq_file *seq, void *v)
2722{
2723 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2724 return 0;
2725}
2726
Tejun Heo2bd59d42014-02-11 11:52:49 -05002727static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2728 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002730 struct cgroup *cgrp = of->kn->parent->priv;
2731 struct cftype *cft = of->kn->priv;
2732 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002733 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734
Tejun Heob4168642014-05-13 12:16:21 -04002735 if (cft->write)
2736 return cft->write(of, buf, nbytes, off);
2737
Tejun Heo2bd59d42014-02-11 11:52:49 -05002738 /*
2739 * kernfs guarantees that a file isn't deleted with operations in
2740 * flight, which means that the matching css is and stays alive and
2741 * doesn't need to be pinned. The RCU locking is not necessary
2742 * either. It's just for the convenience of using cgroup_css().
2743 */
2744 rcu_read_lock();
2745 css = cgroup_css(cgrp, cft->ss);
2746 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747
Tejun Heo451af502014-05-13 12:16:21 -04002748 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002749 unsigned long long v;
2750 ret = kstrtoull(buf, 0, &v);
2751 if (!ret)
2752 ret = cft->write_u64(css, cft, v);
2753 } else if (cft->write_s64) {
2754 long long v;
2755 ret = kstrtoll(buf, 0, &v);
2756 if (!ret)
2757 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002758 } else {
2759 ret = -EINVAL;
2760 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002761
Tejun Heoa742c592013-12-05 12:28:03 -05002762 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763}
2764
Tejun Heo6612f052013-12-05 12:28:04 -05002765static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002766{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002767 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002768}
2769
2770static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2771{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002772 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002773}
2774
2775static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2776{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002777 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002778}
2779
2780static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2781{
Tejun Heo7da11272013-12-05 12:28:04 -05002782 struct cftype *cft = seq_cft(m);
2783 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002784
Tejun Heo2da8ca82013-12-05 12:28:04 -05002785 if (cft->seq_show)
2786 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002787
Tejun Heo896f5192013-12-05 12:28:04 -05002788 if (cft->read_u64)
2789 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2790 else if (cft->read_s64)
2791 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2792 else
2793 return -EINVAL;
2794 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002795}
2796
Tejun Heo2bd59d42014-02-11 11:52:49 -05002797static struct kernfs_ops cgroup_kf_single_ops = {
2798 .atomic_write_len = PAGE_SIZE,
2799 .write = cgroup_file_write,
2800 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002801};
2802
Tejun Heo2bd59d42014-02-11 11:52:49 -05002803static struct kernfs_ops cgroup_kf_ops = {
2804 .atomic_write_len = PAGE_SIZE,
2805 .write = cgroup_file_write,
2806 .seq_start = cgroup_seqfile_start,
2807 .seq_next = cgroup_seqfile_next,
2808 .seq_stop = cgroup_seqfile_stop,
2809 .seq_show = cgroup_seqfile_show,
2810};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002811
2812/*
2813 * cgroup_rename - Only allow simple rename of directories in place.
2814 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002815static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2816 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002817{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002818 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002819 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002820
Tejun Heo2bd59d42014-02-11 11:52:49 -05002821 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002822 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002823 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002824 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002825
Tejun Heo6db8e852013-06-14 11:18:22 -07002826 /*
2827 * This isn't a proper migration and its usefulness is very
2828 * limited. Disallow if sane_behavior.
2829 */
2830 if (cgroup_sane_behavior(cgrp))
2831 return -EPERM;
2832
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002833 /*
2834 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2835 * active_ref. kernfs_rename() doesn't require active_ref
2836 * protection. Break them before grabbing cgroup_tree_mutex.
2837 */
2838 kernfs_break_active_protection(new_parent);
2839 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002840
Tejun Heo2bd59d42014-02-11 11:52:49 -05002841 mutex_lock(&cgroup_tree_mutex);
2842 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002843
Tejun Heo2bd59d42014-02-11 11:52:49 -05002844 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002845
Tejun Heo2bd59d42014-02-11 11:52:49 -05002846 mutex_unlock(&cgroup_mutex);
2847 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002848
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002849 kernfs_unbreak_active_protection(kn);
2850 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002851 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002852}
2853
Tejun Heo49957f82014-04-07 16:44:47 -04002854/* set uid and gid of cgroup dirs and files to that of the creator */
2855static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2856{
2857 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2858 .ia_uid = current_fsuid(),
2859 .ia_gid = current_fsgid(), };
2860
2861 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2862 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2863 return 0;
2864
2865 return kernfs_setattr(kn, &iattr);
2866}
2867
Tejun Heo2bb566c2013-08-08 20:11:23 -04002868static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002869{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002870 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002871 struct kernfs_node *kn;
2872 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002873 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002874
Tejun Heo2bd59d42014-02-11 11:52:49 -05002875#ifdef CONFIG_DEBUG_LOCK_ALLOC
2876 key = &cft->lockdep_key;
2877#endif
2878 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2879 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2880 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002881 if (IS_ERR(kn))
2882 return PTR_ERR(kn);
2883
2884 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002885 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002886 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002887 return ret;
2888 }
2889
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002890 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002891 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002892 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002893}
2894
Tejun Heob1f28d32013-06-28 16:24:10 -07002895/**
2896 * cgroup_addrm_files - add or remove files to a cgroup directory
2897 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002898 * @cfts: array of cftypes to be added
2899 * @is_add: whether to add or remove
2900 *
2901 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002902 * For removals, this function never fails. If addition fails, this
2903 * function doesn't remove files already added. The caller is responsible
2904 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002905 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002906static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2907 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002908{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002909 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002910 int ret;
2911
Tejun Heoace2bee2014-02-11 11:52:47 -05002912 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002913
2914 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002915 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002916 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2917 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002918 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2919 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002920 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2921 continue;
2922 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2923 continue;
2924
Li Zefan2739d3c2013-01-21 18:18:33 +08002925 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002926 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002927 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002928 pr_warn("%s: failed to add %s, err=%d\n",
2929 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002930 return ret;
2931 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002932 } else {
2933 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002934 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002935 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002936 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002937}
2938
Tejun Heo21a2d342014-02-12 09:29:49 -05002939static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002940{
2941 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002942 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002943 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002944 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002945 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002946
Tejun Heo21a2d342014-02-12 09:29:49 -05002947 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002948
Li Zefane8c82d22013-06-18 18:48:37 +08002949 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002950 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002951 struct cgroup *cgrp = css->cgroup;
2952
Li Zefane8c82d22013-06-18 18:48:37 +08002953 if (cgroup_is_dead(cgrp))
2954 continue;
2955
Tejun Heo21a2d342014-02-12 09:29:49 -05002956 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002957 if (ret)
2958 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002959 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002960
2961 if (is_add && !ret)
2962 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002963 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002964}
2965
Tejun Heo2da440a2014-02-11 11:52:48 -05002966static void cgroup_exit_cftypes(struct cftype *cfts)
2967{
2968 struct cftype *cft;
2969
Tejun Heo2bd59d42014-02-11 11:52:49 -05002970 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2971 /* free copy for custom atomic_write_len, see init_cftypes() */
2972 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2973 kfree(cft->kf_ops);
2974 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002975 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002976 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002977}
2978
Tejun Heo2bd59d42014-02-11 11:52:49 -05002979static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002980{
2981 struct cftype *cft;
2982
Tejun Heo2bd59d42014-02-11 11:52:49 -05002983 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2984 struct kernfs_ops *kf_ops;
2985
Tejun Heo0adb0702014-02-12 09:29:48 -05002986 WARN_ON(cft->ss || cft->kf_ops);
2987
Tejun Heo2bd59d42014-02-11 11:52:49 -05002988 if (cft->seq_start)
2989 kf_ops = &cgroup_kf_ops;
2990 else
2991 kf_ops = &cgroup_kf_single_ops;
2992
2993 /*
2994 * Ugh... if @cft wants a custom max_write_len, we need to
2995 * make a copy of kf_ops to set its atomic_write_len.
2996 */
2997 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2998 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2999 if (!kf_ops) {
3000 cgroup_exit_cftypes(cfts);
3001 return -ENOMEM;
3002 }
3003 kf_ops->atomic_write_len = cft->max_write_len;
3004 }
3005
3006 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003007 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003008 }
3009
3010 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003011}
3012
Tejun Heo21a2d342014-02-12 09:29:49 -05003013static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3014{
3015 lockdep_assert_held(&cgroup_tree_mutex);
3016
3017 if (!cfts || !cfts[0].ss)
3018 return -ENOENT;
3019
3020 list_del(&cfts->node);
3021 cgroup_apply_cftypes(cfts, false);
3022 cgroup_exit_cftypes(cfts);
3023 return 0;
3024}
3025
Tejun Heo8e3f6542012-04-01 12:09:55 -07003026/**
Tejun Heo80b13582014-02-12 09:29:48 -05003027 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3028 * @cfts: zero-length name terminated array of cftypes
3029 *
3030 * Unregister @cfts. Files described by @cfts are removed from all
3031 * existing cgroups and all future cgroups won't have them either. This
3032 * function can be called anytime whether @cfts' subsys is attached or not.
3033 *
3034 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3035 * registered.
3036 */
3037int cgroup_rm_cftypes(struct cftype *cfts)
3038{
Tejun Heo21a2d342014-02-12 09:29:49 -05003039 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003040
Tejun Heo21a2d342014-02-12 09:29:49 -05003041 mutex_lock(&cgroup_tree_mutex);
3042 ret = cgroup_rm_cftypes_locked(cfts);
3043 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003044 return ret;
3045}
3046
3047/**
3048 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3049 * @ss: target cgroup subsystem
3050 * @cfts: zero-length name terminated array of cftypes
3051 *
3052 * Register @cfts to @ss. Files described by @cfts are created for all
3053 * existing cgroups to which @ss is attached and all future cgroups will
3054 * have them too. This function can be called anytime whether @ss is
3055 * attached or not.
3056 *
3057 * Returns 0 on successful registration, -errno on failure. Note that this
3058 * function currently returns 0 as long as @cfts registration is successful
3059 * even if some file creation attempts on existing cgroups fail.
3060 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003061int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003062{
Tejun Heo9ccece82013-06-28 16:24:11 -07003063 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003064
Li Zefandc5736e2014-02-17 10:41:50 +08003065 if (!cfts || cfts[0].name[0] == '\0')
3066 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003067
Tejun Heo2bd59d42014-02-11 11:52:49 -05003068 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003069 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003070 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003071
Tejun Heo21a2d342014-02-12 09:29:49 -05003072 mutex_lock(&cgroup_tree_mutex);
3073
Tejun Heo0adb0702014-02-12 09:29:48 -05003074 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003075 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003076 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003077 cgroup_rm_cftypes_locked(cfts);
3078
3079 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003080 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003081}
Tejun Heo79578622012-04-01 12:09:56 -07003082
3083/**
Li Zefana043e3b2008-02-23 15:24:09 -08003084 * cgroup_task_count - count the number of tasks in a cgroup.
3085 * @cgrp: the cgroup in question
3086 *
3087 * Return the number of tasks in the cgroup.
3088 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003089static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003090{
3091 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003092 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003093
Tejun Heo96d365e2014-02-13 06:58:40 -05003094 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003095 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3096 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003097 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003098 return count;
3099}
3100
Tejun Heo574bd9f2012-11-09 09:12:29 -08003101/**
Tejun Heo492eb212013-08-08 20:11:25 -04003102 * css_next_child - find the next child of a given css
3103 * @pos_css: the current position (%NULL to initiate traversal)
3104 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003105 *
Tejun Heo492eb212013-08-08 20:11:25 -04003106 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003107 * under either cgroup_mutex or RCU read lock. The only requirement is
3108 * that @parent_css and @pos_css are accessible. The next sibling is
3109 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003110 */
Tejun Heo492eb212013-08-08 20:11:25 -04003111struct cgroup_subsys_state *
3112css_next_child(struct cgroup_subsys_state *pos_css,
3113 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003114{
Tejun Heo492eb212013-08-08 20:11:25 -04003115 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3116 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003117 struct cgroup *next;
3118
Tejun Heoace2bee2014-02-11 11:52:47 -05003119 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003120
3121 /*
3122 * @pos could already have been removed. Once a cgroup is removed,
3123 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003124 * changes. As CGRP_DEAD assertion is serialized and happens
3125 * before the cgroup is taken off the ->sibling list, if we see it
3126 * unasserted, it's guaranteed that the next sibling hasn't
3127 * finished its grace period even if it's already removed, and thus
3128 * safe to dereference from this RCU critical section. If
3129 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3130 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003131 *
3132 * If @pos is dead, its next pointer can't be dereferenced;
3133 * however, as each cgroup is given a monotonically increasing
3134 * unique serial number and always appended to the sibling list,
3135 * the next one can be found by walking the parent's children until
3136 * we see a cgroup with higher serial number than @pos's. While
3137 * this path can be slower, it's taken only when either the current
3138 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003139 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003140 if (!pos) {
3141 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3142 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003143 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003144 } else {
3145 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3146 if (next->serial_nr > pos->serial_nr)
3147 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003148 }
3149
Tejun Heo3b281af2014-04-23 11:13:15 -04003150 /*
3151 * @next, if not pointing to the head, can be dereferenced and is
3152 * the next sibling; however, it might have @ss disabled. If so,
3153 * fast-forward to the next enabled one.
3154 */
3155 while (&next->sibling != &cgrp->children) {
3156 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003157
Tejun Heo3b281af2014-04-23 11:13:15 -04003158 if (next_css)
3159 return next_css;
3160 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3161 }
3162 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003163}
Tejun Heo53fa5262013-05-24 10:55:38 +09003164
3165/**
Tejun Heo492eb212013-08-08 20:11:25 -04003166 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003167 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003168 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003169 *
Tejun Heo492eb212013-08-08 20:11:25 -04003170 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003171 * to visit for pre-order traversal of @root's descendants. @root is
3172 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003173 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003174 * While this function requires cgroup_mutex or RCU read locking, it
3175 * doesn't require the whole traversal to be contained in a single critical
3176 * section. This function will return the correct next descendant as long
3177 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178 */
Tejun Heo492eb212013-08-08 20:11:25 -04003179struct cgroup_subsys_state *
3180css_next_descendant_pre(struct cgroup_subsys_state *pos,
3181 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182{
Tejun Heo492eb212013-08-08 20:11:25 -04003183 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003184
Tejun Heoace2bee2014-02-11 11:52:47 -05003185 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003186
Tejun Heobd8815a2013-08-08 20:11:27 -04003187 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003188 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003189 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003190
3191 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003192 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193 if (next)
3194 return next;
3195
3196 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003197 while (pos != root) {
3198 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003199 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003200 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003201 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003202 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003203
3204 return NULL;
3205}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003206
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003207/**
Tejun Heo492eb212013-08-08 20:11:25 -04003208 * css_rightmost_descendant - return the rightmost descendant of a css
3209 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003210 *
Tejun Heo492eb212013-08-08 20:11:25 -04003211 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3212 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003213 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003214 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003215 * While this function requires cgroup_mutex or RCU read locking, it
3216 * doesn't require the whole traversal to be contained in a single critical
3217 * section. This function will return the correct rightmost descendant as
3218 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003219 */
Tejun Heo492eb212013-08-08 20:11:25 -04003220struct cgroup_subsys_state *
3221css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003222{
Tejun Heo492eb212013-08-08 20:11:25 -04003223 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003224
Tejun Heoace2bee2014-02-11 11:52:47 -05003225 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003226
3227 do {
3228 last = pos;
3229 /* ->prev isn't RCU safe, walk ->next till the end */
3230 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003231 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003232 pos = tmp;
3233 } while (pos);
3234
3235 return last;
3236}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003237
Tejun Heo492eb212013-08-08 20:11:25 -04003238static struct cgroup_subsys_state *
3239css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003240{
Tejun Heo492eb212013-08-08 20:11:25 -04003241 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003242
3243 do {
3244 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003245 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003246 } while (pos);
3247
3248 return last;
3249}
3250
3251/**
Tejun Heo492eb212013-08-08 20:11:25 -04003252 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003253 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003254 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003255 *
Tejun Heo492eb212013-08-08 20:11:25 -04003256 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003257 * to visit for post-order traversal of @root's descendants. @root is
3258 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003259 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003260 * While this function requires cgroup_mutex or RCU read locking, it
3261 * doesn't require the whole traversal to be contained in a single critical
3262 * section. This function will return the correct next descendant as long
3263 * as both @pos and @cgroup are accessible and @pos is a descendant of
3264 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003265 */
Tejun Heo492eb212013-08-08 20:11:25 -04003266struct cgroup_subsys_state *
3267css_next_descendant_post(struct cgroup_subsys_state *pos,
3268 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003269{
Tejun Heo492eb212013-08-08 20:11:25 -04003270 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003271
Tejun Heoace2bee2014-02-11 11:52:47 -05003272 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003273
Tejun Heo58b79a92013-09-06 15:31:08 -04003274 /* if first iteration, visit leftmost descendant which may be @root */
3275 if (!pos)
3276 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003277
Tejun Heobd8815a2013-08-08 20:11:27 -04003278 /* if we visited @root, we're done */
3279 if (pos == root)
3280 return NULL;
3281
Tejun Heo574bd9f2012-11-09 09:12:29 -08003282 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003283 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003284 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003285 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003286
3287 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003288 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003289}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003290
Tejun Heo0942eee2013-08-08 20:11:26 -04003291/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003292 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003293 * @it: the iterator to advance
3294 *
3295 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003296 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003297static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003298{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003299 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003300 struct cgrp_cset_link *link;
3301 struct css_set *cset;
3302
3303 /* Advance to the next non-empty css_set */
3304 do {
3305 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003306 if (l == it->cset_head) {
3307 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003308 return;
3309 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003310
3311 if (it->ss) {
3312 cset = container_of(l, struct css_set,
3313 e_cset_node[it->ss->id]);
3314 } else {
3315 link = list_entry(l, struct cgrp_cset_link, cset_link);
3316 cset = link->cset;
3317 }
Tejun Heoc7561122014-02-25 10:04:01 -05003318 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3319
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003320 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003321
3322 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003323 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003324 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003325 it->task_pos = cset->mg_tasks.next;
3326
3327 it->tasks_head = &cset->tasks;
3328 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003329}
3330
Tejun Heo0942eee2013-08-08 20:11:26 -04003331/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003332 * css_task_iter_start - initiate task iteration
3333 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003334 * @it: the task iterator to use
3335 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003336 * Initiate iteration through the tasks of @css. The caller can call
3337 * css_task_iter_next() to walk through the tasks until the function
3338 * returns NULL. On completion of iteration, css_task_iter_end() must be
3339 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003340 *
3341 * Note that this function acquires a lock which is released when the
3342 * iteration finishes. The caller can't sleep while iteration is in
3343 * progress.
3344 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003345void css_task_iter_start(struct cgroup_subsys_state *css,
3346 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003347 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003348{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003349 /* no one should try to iterate before mounting cgroups */
3350 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003351
Tejun Heo96d365e2014-02-13 06:58:40 -05003352 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003353
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003354 it->ss = css->ss;
3355
3356 if (it->ss)
3357 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3358 else
3359 it->cset_pos = &css->cgroup->cset_links;
3360
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003361 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003362
Tejun Heo72ec7022013-08-08 20:11:26 -04003363 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003364}
3365
Tejun Heo0942eee2013-08-08 20:11:26 -04003366/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003367 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003368 * @it: the task iterator being iterated
3369 *
3370 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003371 * initialized via css_task_iter_start(). Returns NULL when the iteration
3372 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003373 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003374struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003375{
3376 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003377 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003378
3379 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003380 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003381 return NULL;
3382 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003383
3384 /*
3385 * Advance iterator to find next entry. cset->tasks is consumed
3386 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3387 * next cset.
3388 */
Paul Menage817929e2007-10-18 23:39:36 -07003389 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003390
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003391 if (l == it->tasks_head)
3392 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003393
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003394 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003395 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003396 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003397 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003398
Paul Menage817929e2007-10-18 23:39:36 -07003399 return res;
3400}
3401
Tejun Heo0942eee2013-08-08 20:11:26 -04003402/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003403 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003404 * @it: the task iterator to finish
3405 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003406 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003407 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003408void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003409 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003410{
Tejun Heo96d365e2014-02-13 06:58:40 -05003411 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003412}
3413
3414/**
3415 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3416 * @to: cgroup to which the tasks will be moved
3417 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003418 *
3419 * Locking rules between cgroup_post_fork() and the migration path
3420 * guarantee that, if a task is forking while being migrated, the new child
3421 * is guaranteed to be either visible in the source cgroup after the
3422 * parent's migration is complete or put into the target cgroup. No task
3423 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003424 */
3425int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3426{
Tejun Heo952aaa12014-02-25 10:04:03 -05003427 LIST_HEAD(preloaded_csets);
3428 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003429 struct css_task_iter it;
3430 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003431 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003432
Tejun Heo952aaa12014-02-25 10:04:03 -05003433 mutex_lock(&cgroup_mutex);
3434
3435 /* all tasks in @from are being moved, all csets are source */
3436 down_read(&css_set_rwsem);
3437 list_for_each_entry(link, &from->cset_links, cset_link)
3438 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3439 up_read(&css_set_rwsem);
3440
3441 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3442 if (ret)
3443 goto out_err;
3444
3445 /*
3446 * Migrate tasks one-by-one until @form is empty. This fails iff
3447 * ->can_attach() fails.
3448 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003449 do {
3450 css_task_iter_start(&from->dummy_css, &it);
3451 task = css_task_iter_next(&it);
3452 if (task)
3453 get_task_struct(task);
3454 css_task_iter_end(&it);
3455
3456 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003457 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003458 put_task_struct(task);
3459 }
3460 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003461out_err:
3462 cgroup_migrate_finish(&preloaded_csets);
3463 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003464 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003465}
3466
Paul Menage817929e2007-10-18 23:39:36 -07003467/*
Ben Blum102a7752009-09-23 15:56:26 -07003468 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003469 *
3470 * Reading this file can return large amounts of data if a cgroup has
3471 * *lots* of attached tasks. So it may need several calls to read(),
3472 * but we cannot guarantee that the information we produce is correct
3473 * unless we produce it entirely atomically.
3474 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003475 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003476
Li Zefan24528252012-01-20 11:58:43 +08003477/* which pidlist file are we talking about? */
3478enum cgroup_filetype {
3479 CGROUP_FILE_PROCS,
3480 CGROUP_FILE_TASKS,
3481};
3482
3483/*
3484 * A pidlist is a list of pids that virtually represents the contents of one
3485 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3486 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3487 * to the cgroup.
3488 */
3489struct cgroup_pidlist {
3490 /*
3491 * used to find which pidlist is wanted. doesn't change as long as
3492 * this particular list stays in the list.
3493 */
3494 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3495 /* array of xids */
3496 pid_t *list;
3497 /* how many elements the above list has */
3498 int length;
Li Zefan24528252012-01-20 11:58:43 +08003499 /* each of these stored in a list by its cgroup */
3500 struct list_head links;
3501 /* pointer to the cgroup we belong to, for list removal purposes */
3502 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003503 /* for delayed destruction */
3504 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003505};
3506
Paul Menagebbcb81d2007-10-18 23:39:32 -07003507/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003508 * The following two functions "fix" the issue where there are more pids
3509 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3510 * TODO: replace with a kernel-wide solution to this problem
3511 */
3512#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3513static void *pidlist_allocate(int count)
3514{
3515 if (PIDLIST_TOO_LARGE(count))
3516 return vmalloc(count * sizeof(pid_t));
3517 else
3518 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3519}
Tejun Heob1a21362013-11-29 10:42:58 -05003520
Ben Blumd1d9fd32009-09-23 15:56:28 -07003521static void pidlist_free(void *p)
3522{
3523 if (is_vmalloc_addr(p))
3524 vfree(p);
3525 else
3526 kfree(p);
3527}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003528
3529/*
Tejun Heob1a21362013-11-29 10:42:58 -05003530 * Used to destroy all pidlists lingering waiting for destroy timer. None
3531 * should be left afterwards.
3532 */
3533static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3534{
3535 struct cgroup_pidlist *l, *tmp_l;
3536
3537 mutex_lock(&cgrp->pidlist_mutex);
3538 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3539 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3540 mutex_unlock(&cgrp->pidlist_mutex);
3541
3542 flush_workqueue(cgroup_pidlist_destroy_wq);
3543 BUG_ON(!list_empty(&cgrp->pidlists));
3544}
3545
3546static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3547{
3548 struct delayed_work *dwork = to_delayed_work(work);
3549 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3550 destroy_dwork);
3551 struct cgroup_pidlist *tofree = NULL;
3552
3553 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003554
3555 /*
Tejun Heo04502362013-11-29 10:42:59 -05003556 * Destroy iff we didn't get queued again. The state won't change
3557 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003558 */
Tejun Heo04502362013-11-29 10:42:59 -05003559 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003560 list_del(&l->links);
3561 pidlist_free(l->list);
3562 put_pid_ns(l->key.ns);
3563 tofree = l;
3564 }
3565
Tejun Heob1a21362013-11-29 10:42:58 -05003566 mutex_unlock(&l->owner->pidlist_mutex);
3567 kfree(tofree);
3568}
3569
3570/*
Ben Blum102a7752009-09-23 15:56:26 -07003571 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003572 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003573 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003574static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003575{
Ben Blum102a7752009-09-23 15:56:26 -07003576 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003577
3578 /*
3579 * we presume the 0th element is unique, so i starts at 1. trivial
3580 * edge cases first; no work needs to be done for either
3581 */
3582 if (length == 0 || length == 1)
3583 return length;
3584 /* src and dest walk down the list; dest counts unique elements */
3585 for (src = 1; src < length; src++) {
3586 /* find next unique element */
3587 while (list[src] == list[src-1]) {
3588 src++;
3589 if (src == length)
3590 goto after;
3591 }
3592 /* dest always points to where the next unique element goes */
3593 list[dest] = list[src];
3594 dest++;
3595 }
3596after:
Ben Blum102a7752009-09-23 15:56:26 -07003597 return dest;
3598}
3599
Tejun Heoafb2bc12013-11-29 10:42:59 -05003600/*
3601 * The two pid files - task and cgroup.procs - guaranteed that the result
3602 * is sorted, which forced this whole pidlist fiasco. As pid order is
3603 * different per namespace, each namespace needs differently sorted list,
3604 * making it impossible to use, for example, single rbtree of member tasks
3605 * sorted by task pointer. As pidlists can be fairly large, allocating one
3606 * per open file is dangerous, so cgroup had to implement shared pool of
3607 * pidlists keyed by cgroup and namespace.
3608 *
3609 * All this extra complexity was caused by the original implementation
3610 * committing to an entirely unnecessary property. In the long term, we
3611 * want to do away with it. Explicitly scramble sort order if
3612 * sane_behavior so that no such expectation exists in the new interface.
3613 *
3614 * Scrambling is done by swapping every two consecutive bits, which is
3615 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3616 */
3617static pid_t pid_fry(pid_t pid)
3618{
3619 unsigned a = pid & 0x55555555;
3620 unsigned b = pid & 0xAAAAAAAA;
3621
3622 return (a << 1) | (b >> 1);
3623}
3624
3625static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3626{
3627 if (cgroup_sane_behavior(cgrp))
3628 return pid_fry(pid);
3629 else
3630 return pid;
3631}
3632
Ben Blum102a7752009-09-23 15:56:26 -07003633static int cmppid(const void *a, const void *b)
3634{
3635 return *(pid_t *)a - *(pid_t *)b;
3636}
3637
Tejun Heoafb2bc12013-11-29 10:42:59 -05003638static int fried_cmppid(const void *a, const void *b)
3639{
3640 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3641}
3642
Ben Blum72a8cb32009-09-23 15:56:27 -07003643static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3644 enum cgroup_filetype type)
3645{
3646 struct cgroup_pidlist *l;
3647 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003648 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003649
Tejun Heoe6b81712013-11-29 10:42:59 -05003650 lockdep_assert_held(&cgrp->pidlist_mutex);
3651
3652 list_for_each_entry(l, &cgrp->pidlists, links)
3653 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003655 return NULL;
3656}
3657
Ben Blum72a8cb32009-09-23 15:56:27 -07003658/*
3659 * find the appropriate pidlist for our purpose (given procs vs tasks)
3660 * returns with the lock on that pidlist already held, and takes care
3661 * of the use count, or returns NULL with no locks held if we're out of
3662 * memory.
3663 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003664static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3665 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003666{
3667 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003668
Tejun Heoe6b81712013-11-29 10:42:59 -05003669 lockdep_assert_held(&cgrp->pidlist_mutex);
3670
3671 l = cgroup_pidlist_find(cgrp, type);
3672 if (l)
3673 return l;
3674
Ben Blum72a8cb32009-09-23 15:56:27 -07003675 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003676 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003677 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003678 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003679
Tejun Heob1a21362013-11-29 10:42:58 -05003680 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003681 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003682 /* don't need task_nsproxy() if we're looking at ourself */
3683 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003684 l->owner = cgrp;
3685 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003686 return l;
3687}
3688
3689/*
Ben Blum102a7752009-09-23 15:56:26 -07003690 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3691 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003692static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3693 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003694{
3695 pid_t *array;
3696 int length;
3697 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003698 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003699 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003700 struct cgroup_pidlist *l;
3701
Tejun Heo4bac00d2013-11-29 10:42:59 -05003702 lockdep_assert_held(&cgrp->pidlist_mutex);
3703
Ben Blum102a7752009-09-23 15:56:26 -07003704 /*
3705 * If cgroup gets more users after we read count, we won't have
3706 * enough space - tough. This race is indistinguishable to the
3707 * caller from the case that the additional cgroup users didn't
3708 * show up until sometime later on.
3709 */
3710 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003711 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003712 if (!array)
3713 return -ENOMEM;
3714 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003715 css_task_iter_start(&cgrp->dummy_css, &it);
3716 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003717 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003718 break;
Ben Blum102a7752009-09-23 15:56:26 -07003719 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003720 if (type == CGROUP_FILE_PROCS)
3721 pid = task_tgid_vnr(tsk);
3722 else
3723 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003724 if (pid > 0) /* make sure to only use valid results */
3725 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003726 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003727 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003728 length = n;
3729 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003730 if (cgroup_sane_behavior(cgrp))
3731 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3732 else
3733 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003734 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003735 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003736
Tejun Heoe6b81712013-11-29 10:42:59 -05003737 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003739 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003740 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003741 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003742 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003743
3744 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003745 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003746 l->list = array;
3747 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003748 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003749 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003750}
3751
Balbir Singh846c7bb2007-10-18 23:39:44 -07003752/**
Li Zefana043e3b2008-02-23 15:24:09 -08003753 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003754 * @stats: cgroupstats to fill information into
3755 * @dentry: A dentry entry belonging to the cgroup for which stats have
3756 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003757 *
3758 * Build and fill cgroupstats so that taskstats can export it to user
3759 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003760 */
3761int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3762{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003763 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003764 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003765 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003766 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003767
Tejun Heo2bd59d42014-02-11 11:52:49 -05003768 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3769 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3770 kernfs_type(kn) != KERNFS_DIR)
3771 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003772
Li Zefanbad34662014-02-14 16:54:28 +08003773 mutex_lock(&cgroup_mutex);
3774
Tejun Heo2bd59d42014-02-11 11:52:49 -05003775 /*
3776 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003777 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003778 * @kn->priv is RCU safe. Let's do the RCU dancing.
3779 */
3780 rcu_read_lock();
3781 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003782 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003783 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003784 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003785 return -ENOENT;
3786 }
Li Zefanbad34662014-02-14 16:54:28 +08003787 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003788
Tejun Heo72ec7022013-08-08 20:11:26 -04003789 css_task_iter_start(&cgrp->dummy_css, &it);
3790 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003791 switch (tsk->state) {
3792 case TASK_RUNNING:
3793 stats->nr_running++;
3794 break;
3795 case TASK_INTERRUPTIBLE:
3796 stats->nr_sleeping++;
3797 break;
3798 case TASK_UNINTERRUPTIBLE:
3799 stats->nr_uninterruptible++;
3800 break;
3801 case TASK_STOPPED:
3802 stats->nr_stopped++;
3803 break;
3804 default:
3805 if (delayacct_is_task_waiting_on_io(tsk))
3806 stats->nr_io_wait++;
3807 break;
3808 }
3809 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003810 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003811
Li Zefanbad34662014-02-14 16:54:28 +08003812 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003813 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003814}
3815
Paul Menage8f3ff202009-09-23 15:56:25 -07003816
Paul Menagecc31edc2008-10-18 20:28:04 -07003817/*
Ben Blum102a7752009-09-23 15:56:26 -07003818 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003819 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003820 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003821 */
3822
Ben Blum102a7752009-09-23 15:56:26 -07003823static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003824{
3825 /*
3826 * Initially we receive a position value that corresponds to
3827 * one more than the last pid shown (or 0 on the first call or
3828 * after a seek to the start). Use a binary-search to find the
3829 * next pid to display, if any
3830 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003831 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003832 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003833 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003834 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003835 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003836 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003837
Tejun Heo4bac00d2013-11-29 10:42:59 -05003838 mutex_lock(&cgrp->pidlist_mutex);
3839
3840 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003841 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003842 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003843 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003844 * could already have been destroyed.
3845 */
Tejun Heo5d224442013-12-05 12:28:04 -05003846 if (of->priv)
3847 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003848
3849 /*
3850 * Either this is the first start() after open or the matching
3851 * pidlist has been destroyed inbetween. Create a new one.
3852 */
Tejun Heo5d224442013-12-05 12:28:04 -05003853 if (!of->priv) {
3854 ret = pidlist_array_load(cgrp, type,
3855 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003856 if (ret)
3857 return ERR_PTR(ret);
3858 }
Tejun Heo5d224442013-12-05 12:28:04 -05003859 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003860
Paul Menagecc31edc2008-10-18 20:28:04 -07003861 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003862 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003863
Paul Menagecc31edc2008-10-18 20:28:04 -07003864 while (index < end) {
3865 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003866 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003867 index = mid;
3868 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003869 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 index = mid + 1;
3871 else
3872 end = mid;
3873 }
3874 }
3875 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003876 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003877 return NULL;
3878 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003879 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003880 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003881 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003882}
3883
Ben Blum102a7752009-09-23 15:56:26 -07003884static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003885{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003886 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003887 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003888
Tejun Heo5d224442013-12-05 12:28:04 -05003889 if (l)
3890 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003891 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003892 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003893}
3894
Ben Blum102a7752009-09-23 15:56:26 -07003895static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003896{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003897 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003898 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003899 pid_t *p = v;
3900 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003901 /*
3902 * Advance to the next pid in the array. If this goes off the
3903 * end, we're done
3904 */
3905 p++;
3906 if (p >= end) {
3907 return NULL;
3908 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003909 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003910 return p;
3911 }
3912}
3913
Ben Blum102a7752009-09-23 15:56:26 -07003914static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003915{
3916 return seq_printf(s, "%d\n", *(int *)v);
3917}
3918
Tejun Heo182446d2013-08-08 20:11:24 -04003919static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3920 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003921{
Tejun Heo182446d2013-08-08 20:11:24 -04003922 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003923}
3924
Tejun Heo182446d2013-08-08 20:11:24 -04003925static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3926 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003927{
Tejun Heo182446d2013-08-08 20:11:24 -04003928 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003929 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003930 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003931 else
Tejun Heo182446d2013-08-08 20:11:24 -04003932 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003933 return 0;
3934}
3935
Tejun Heo182446d2013-08-08 20:11:24 -04003936static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3937 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003938{
Tejun Heo182446d2013-08-08 20:11:24 -04003939 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003940}
3941
Tejun Heo182446d2013-08-08 20:11:24 -04003942static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3943 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003944{
3945 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003946 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 else
Tejun Heo182446d2013-08-08 20:11:24 -04003948 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003949 return 0;
3950}
3951
Tejun Heod5c56ce2013-06-03 19:14:34 -07003952static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003953 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003954 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003955 .seq_start = cgroup_pidlist_start,
3956 .seq_next = cgroup_pidlist_next,
3957 .seq_stop = cgroup_pidlist_stop,
3958 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003959 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04003960 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003961 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003962 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003963 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003964 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003965 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003966 .read_u64 = cgroup_clone_children_read,
3967 .write_u64 = cgroup_clone_children_write,
3968 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003969 {
Tejun Heo873fe092013-04-14 20:15:26 -07003970 .name = "cgroup.sane_behavior",
3971 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003972 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003973 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003974 {
3975 .name = "cgroup.controllers",
3976 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3977 .seq_show = cgroup_root_controllers_show,
3978 },
3979 {
3980 .name = "cgroup.controllers",
3981 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3982 .seq_show = cgroup_controllers_show,
3983 },
3984 {
3985 .name = "cgroup.subtree_control",
3986 .flags = CFTYPE_ONLY_ON_DFL,
3987 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04003988 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04003989 },
Tejun Heo842b5972014-04-25 18:28:02 -04003990 {
3991 .name = "cgroup.populated",
3992 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3993 .seq_show = cgroup_populated_show,
3994 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003995
3996 /*
3997 * Historical crazy stuff. These don't have "cgroup." prefix and
3998 * don't exist if sane_behavior. If you're depending on these, be
3999 * prepared to be burned.
4000 */
4001 {
4002 .name = "tasks",
4003 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004004 .seq_start = cgroup_pidlist_start,
4005 .seq_next = cgroup_pidlist_next,
4006 .seq_stop = cgroup_pidlist_stop,
4007 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004008 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004009 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004010 .mode = S_IRUGO | S_IWUSR,
4011 },
4012 {
4013 .name = "notify_on_release",
4014 .flags = CFTYPE_INSANE,
4015 .read_u64 = cgroup_read_notify_on_release,
4016 .write_u64 = cgroup_write_notify_on_release,
4017 },
Tejun Heo873fe092013-04-14 20:15:26 -07004018 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004019 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004020 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004021 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004022 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004023 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004024 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004025 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004026};
4027
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004028/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004029 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004030 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004031 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004032 *
4033 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004034 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004035static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004036{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004037 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004038 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004039
Tejun Heo8e3f6542012-04-01 12:09:55 -07004040 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004041 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004042 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004043
Tejun Heo69dfa002014-05-04 15:09:13 -04004044 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004045 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004046
Tejun Heo0adb0702014-02-12 09:29:48 -05004047 list_for_each_entry(cfts, &ss->cfts, node) {
4048 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004049 if (ret < 0)
4050 goto err;
4051 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004052 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004054err:
4055 cgroup_clear_dir(cgrp, subsys_mask);
4056 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057}
4058
Tejun Heo0c21ead2013-08-13 20:22:51 -04004059/*
4060 * css destruction is four-stage process.
4061 *
4062 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4063 * Implemented in kill_css().
4064 *
4065 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004066 * and thus css_tryget_online() is guaranteed to fail, the css can be
4067 * offlined by invoking offline_css(). After offlining, the base ref is
4068 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004069 *
4070 * 3. When the percpu_ref reaches zero, the only possible remaining
4071 * accessors are inside RCU read sections. css_release() schedules the
4072 * RCU callback.
4073 *
4074 * 4. After the grace period, the css can be freed. Implemented in
4075 * css_free_work_fn().
4076 *
4077 * It is actually hairier because both step 2 and 4 require process context
4078 * and thus involve punting to css->destroy_work adding two additional
4079 * steps to the already complex sequence.
4080 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004081static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004082{
4083 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004084 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004085 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004086
Tejun Heo0ae78e02013-08-13 11:01:54 -04004087 if (css->parent)
4088 css_put(css->parent);
4089
Tejun Heo0c21ead2013-08-13 20:22:51 -04004090 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004091 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004092}
4093
4094static void css_free_rcu_fn(struct rcu_head *rcu_head)
4095{
4096 struct cgroup_subsys_state *css =
4097 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4098
Tejun Heo0c21ead2013-08-13 20:22:51 -04004099 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004100 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004101}
4102
Tejun Heod3daf282013-06-13 19:39:16 -07004103static void css_release(struct percpu_ref *ref)
4104{
4105 struct cgroup_subsys_state *css =
4106 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo15a4c832014-05-04 15:09:14 -04004107 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004108
Tejun Heo15a4c832014-05-04 15:09:14 -04004109 cgroup_idr_remove(&ss->css_idr, css->id);
4110
Tejun Heo0c21ead2013-08-13 20:22:51 -04004111 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004112}
4113
Tejun Heoddfcada2014-05-04 15:09:14 -04004114static void init_and_link_css(struct cgroup_subsys_state *css,
4115 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116{
Tejun Heoddfcada2014-05-04 15:09:14 -04004117 cgroup_get(cgrp);
4118
Paul Menagebd89aab2007-10-18 23:40:44 -07004119 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004120 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004122
Tejun Heoddfcada2014-05-04 15:09:14 -04004123 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004124 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004125 css_get(css->parent);
4126 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004127 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004128 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004129
Tejun Heoca8bdca2013-08-26 18:40:56 -04004130 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004131}
4132
Li Zefan2a4ac632013-07-31 16:16:40 +08004133/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004134static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004135{
Tejun Heo623f9262013-08-13 11:01:55 -04004136 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004137 int ret = 0;
4138
Tejun Heoace2bee2014-02-11 11:52:47 -05004139 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004140 lockdep_assert_held(&cgroup_mutex);
4141
Tejun Heo92fb9742012-11-19 08:13:38 -08004142 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004143 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004144 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004145 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004146 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004147 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004148 }
Tejun Heob1929db2012-11-19 08:13:38 -08004149 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004150}
4151
Li Zefan2a4ac632013-07-31 16:16:40 +08004152/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004153static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004154{
Tejun Heo623f9262013-08-13 11:01:55 -04004155 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004156
Tejun Heoace2bee2014-02-11 11:52:47 -05004157 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004158 lockdep_assert_held(&cgroup_mutex);
4159
4160 if (!(css->flags & CSS_ONLINE))
4161 return;
4162
Li Zefand7eeac12013-03-12 15:35:59 -07004163 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004164 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004165
Tejun Heoeb954192013-08-08 20:11:23 -04004166 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004167 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004168 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004169
4170 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004171}
4172
Tejun Heoc81c925a2013-12-06 15:11:56 -05004173/**
4174 * create_css - create a cgroup_subsys_state
4175 * @cgrp: the cgroup new css will be associated with
4176 * @ss: the subsys of new css
4177 *
4178 * Create a new css associated with @cgrp - @ss pair. On success, the new
4179 * css is online and installed in @cgrp with all interface files created.
4180 * Returns 0 on success, -errno on failure.
4181 */
4182static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4183{
4184 struct cgroup *parent = cgrp->parent;
4185 struct cgroup_subsys_state *css;
4186 int err;
4187
Tejun Heoc81c925a2013-12-06 15:11:56 -05004188 lockdep_assert_held(&cgroup_mutex);
4189
4190 css = ss->css_alloc(cgroup_css(parent, ss));
4191 if (IS_ERR(css))
4192 return PTR_ERR(css);
4193
Tejun Heoddfcada2014-05-04 15:09:14 -04004194 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004195
Tejun Heoc81c925a2013-12-06 15:11:56 -05004196 err = percpu_ref_init(&css->refcnt, css_release);
4197 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004198 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004199
Tejun Heo15a4c832014-05-04 15:09:14 -04004200 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4201 if (err < 0)
4202 goto err_free_percpu_ref;
4203 css->id = err;
4204
Tejun Heoaec25022014-02-08 10:36:58 -05004205 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004206 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004207 goto err_free_id;
4208
4209 /* @css is ready to be brought online now, make it visible */
4210 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004211
4212 err = online_css(css);
4213 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004214 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004215
Tejun Heoc81c925a2013-12-06 15:11:56 -05004216 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4217 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004218 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 -04004219 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004220 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004221 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004222 ss->warned_broken_hierarchy = true;
4223 }
4224
4225 return 0;
4226
Li Zefan3eb59ec2014-03-18 17:02:36 +08004227err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004228 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004229err_free_id:
4230 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004231err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004232 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004233err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004234 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004235 return err;
4236}
4237
Tejun Heob3bfd982014-05-13 12:19:22 -04004238static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4239 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240{
Tejun Heob3bfd982014-05-13 12:19:22 -04004241 struct cgroup *parent = parent_kn->priv, *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004242 struct cgroup_root *root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004244 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004245 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004246
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004247 /*
Tejun Heob3bfd982014-05-13 12:19:22 -04004248 * cgroup_mkdir() grabs cgroup_tree_mutex which nests outside
4249 * kernfs active_ref and cgroup_create() already synchronizes
4250 * properly against removal through cgroup_lock_live_group().
4251 * Break it before calling cgroup_create().
Tejun Heo976c06b2012-11-05 09:16:59 -08004252 */
Tejun Heob3bfd982014-05-13 12:19:22 -04004253 cgroup_get(parent);
4254 kernfs_break_active_protection(parent_kn);
4255 mutex_lock(&cgroup_tree_mutex);
Tejun Heo976c06b2012-11-05 09:16:59 -08004256 if (!cgroup_lock_live_group(parent)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004257 ret = -ENODEV;
4258 goto out_unlock_tree;
4259 }
4260
4261 /* allocate the cgroup and its ID, 0 is reserved for the root */
4262 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4263 if (!cgrp) {
4264 ret = -ENOMEM;
4265 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004266 }
4267
4268 /*
4269 * Temporarily set the pointer to NULL, so idr_find() won't return
4270 * a half-baked cgroup.
4271 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004272 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004273 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004274 ret = -ENOMEM;
4275 goto out_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004276 }
4277
Paul Menagecc31edc2008-10-18 20:28:04 -07004278 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279
Paul Menagebd89aab2007-10-18 23:40:44 -07004280 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004281 cgrp->dummy_css.parent = &parent->dummy_css;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004282 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283
Li Zefanb6abdb02008-03-04 14:28:19 -08004284 if (notify_on_release(parent))
4285 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4286
Tejun Heo2260e7f2012-11-19 08:13:38 -08004287 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4288 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004289
Tejun Heo2bd59d42014-02-11 11:52:49 -05004290 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004291 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004292 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004293 ret = PTR_ERR(kn);
4294 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004295 }
4296 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297
Tejun Heo6f305582014-02-12 09:29:50 -05004298 /*
4299 * This extra ref will be put in cgroup_free_fn() and guarantees
4300 * that @cgrp->kn is always accessible.
4301 */
4302 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004303
Tejun Heo00356bd2013-06-18 11:14:22 -07004304 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004305
Tejun Heo4e139af2012-11-19 08:13:36 -08004306 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004307 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004308 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004309 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004310
Tejun Heo0d802552013-12-06 15:11:56 -05004311 /*
4312 * @cgrp is now fully operational. If something fails after this
4313 * point, it'll be released via the normal destruction path.
4314 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004315 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004316
Tejun Heoba0f4d72014-05-13 12:19:22 -04004317 ret = cgroup_kn_set_ugid(kn);
4318 if (ret)
4319 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004320
Tejun Heoba0f4d72014-05-13 12:19:22 -04004321 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4322 if (ret)
4323 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004324
Tejun Heo9d403e92013-12-06 15:11:56 -05004325 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004326 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004327 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004328 ret = create_css(cgrp, ss);
4329 if (ret)
4330 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004331 }
Tejun Heoa8638032012-11-09 09:12:29 -08004332 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333
Tejun Heobd53d612014-04-23 11:13:16 -04004334 /*
4335 * On the default hierarchy, a child doesn't automatically inherit
4336 * child_subsys_mask from the parent. Each is configured manually.
4337 */
4338 if (!cgroup_on_dfl(cgrp))
4339 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004340
Tejun Heo2bd59d42014-02-11 11:52:49 -05004341 kernfs_activate(kn);
4342
Tejun Heoba0f4d72014-05-13 12:19:22 -04004343 ret = 0;
4344 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345
Tejun Heoba0f4d72014-05-13 12:19:22 -04004346out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004347 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004348out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004349 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004350out_unlock:
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004351 mutex_unlock(&cgroup_mutex);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004352out_unlock_tree:
Tejun Heoace2bee2014-02-11 11:52:47 -05004353 mutex_unlock(&cgroup_tree_mutex);
Tejun Heob3bfd982014-05-13 12:19:22 -04004354 kernfs_unbreak_active_protection(parent_kn);
4355 cgroup_put(parent);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004356 return ret;
4357
4358out_destroy:
4359 cgroup_destroy_locked(cgrp);
4360 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361}
4362
Tejun Heo223dbc32013-08-13 20:22:50 -04004363/*
4364 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heoec903c02014-05-13 12:11:01 -04004365 * css_tryget_online() is now guaranteed to fail.
Tejun Heo223dbc32013-08-13 20:22:50 -04004366 */
4367static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004368{
Tejun Heo223dbc32013-08-13 20:22:50 -04004369 struct cgroup_subsys_state *css =
4370 container_of(work, struct cgroup_subsys_state, destroy_work);
4371 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004372
Tejun Heoace2bee2014-02-11 11:52:47 -05004373 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004374 mutex_lock(&cgroup_mutex);
4375
4376 /*
Tejun Heoec903c02014-05-13 12:11:01 -04004377 * css_tryget_online() is guaranteed to fail now. Tell subsystems
4378 * to initate destruction.
Tejun Heo09a503ea2013-08-13 20:22:50 -04004379 */
4380 offline_css(css);
4381
4382 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004383 * If @cgrp is marked dead, it's waiting for refs of all css's to
4384 * be disabled before proceeding to the second phase of cgroup
4385 * destruction. If we are the last one, kick it off.
4386 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004387 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004388 cgroup_destroy_css_killed(cgrp);
4389
4390 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004391 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004392
4393 /*
4394 * Put the css refs from kill_css(). Each css holds an extra
4395 * reference to the cgroup's dentry and cgroup removal proceeds
4396 * regardless of css refs. On the last put of each css, whenever
4397 * that may be, the extra dentry ref is put so that dentry
4398 * destruction happens only after all css's are released.
4399 */
4400 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004401}
4402
Tejun Heo223dbc32013-08-13 20:22:50 -04004403/* css kill confirmation processing requires process context, bounce */
4404static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004405{
4406 struct cgroup_subsys_state *css =
4407 container_of(ref, struct cgroup_subsys_state, refcnt);
4408
Tejun Heo223dbc32013-08-13 20:22:50 -04004409 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004410 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004411}
4412
Tejun Heof392e512014-04-23 11:13:14 -04004413/**
4414 * kill_css - destroy a css
4415 * @css: css to destroy
4416 *
4417 * This function initiates destruction of @css by removing cgroup interface
4418 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004419 * asynchronously once css_tryget_online() is guaranteed to fail and when
4420 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004421 */
4422static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004423{
Tejun Heo94419622014-03-19 10:23:54 -04004424 lockdep_assert_held(&cgroup_tree_mutex);
4425
Tejun Heo2bd59d42014-02-11 11:52:49 -05004426 /*
4427 * This must happen before css is disassociated with its cgroup.
4428 * See seq_css() for details.
4429 */
Tejun Heoaec25022014-02-08 10:36:58 -05004430 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004431
Tejun Heoedae0c32013-08-13 20:22:51 -04004432 /*
4433 * Killing would put the base ref, but we need to keep it alive
4434 * until after ->css_offline().
4435 */
4436 css_get(css);
4437
4438 /*
4439 * cgroup core guarantees that, by the time ->css_offline() is
4440 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004441 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004442 * proceed to offlining css's because percpu_ref_kill() doesn't
4443 * guarantee that the ref is seen as killed on all CPUs on return.
4444 *
4445 * Use percpu_ref_kill_and_confirm() to get notifications as each
4446 * css is confirmed to be seen as killed on all CPUs.
4447 */
4448 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004449}
4450
4451/**
4452 * cgroup_destroy_locked - the first stage of cgroup destruction
4453 * @cgrp: cgroup to be destroyed
4454 *
4455 * css's make use of percpu refcnts whose killing latency shouldn't be
4456 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004457 * guarantee that css_tryget_online() won't succeed by the time
4458 * ->css_offline() is invoked. To satisfy all the requirements,
4459 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004460 *
4461 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4462 * userland visible parts and start killing the percpu refcnts of
4463 * css's. Set up so that the next stage will be kicked off once all
4464 * the percpu refcnts are confirmed to be killed.
4465 *
4466 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4467 * rest of destruction. Once all cgroup references are gone, the
4468 * cgroup is RCU-freed.
4469 *
4470 * This function implements s1. After this step, @cgrp is gone as far as
4471 * the userland is concerned and a new cgroup with the same name may be
4472 * created. As cgroup doesn't care about the names internally, this
4473 * doesn't cause any problem.
4474 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004475static int cgroup_destroy_locked(struct cgroup *cgrp)
4476 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004478 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004479 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004480 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004481 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482
Tejun Heoace2bee2014-02-11 11:52:47 -05004483 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004484 lockdep_assert_held(&cgroup_mutex);
4485
Tejun Heoddd69142013-06-12 21:04:54 -07004486 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004487 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004488 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004489 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004490 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004491 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004492 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004493 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004495
Tejun Heo1a90dd52012-11-05 09:16:59 -08004496 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004497 * Make sure there's no live children. We can't test ->children
4498 * emptiness as dead children linger on it while being destroyed;
4499 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4500 */
4501 empty = true;
4502 rcu_read_lock();
4503 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4504 empty = cgroup_is_dead(child);
4505 if (!empty)
4506 break;
4507 }
4508 rcu_read_unlock();
4509 if (!empty)
4510 return -EBUSY;
4511
4512 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004513 * Mark @cgrp dead. This prevents further task migration and child
4514 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004515 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004516 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004517 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004518 */
Tejun Heo54766d42013-06-12 21:04:53 -07004519 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004520
Tejun Heo5d773812014-03-19 10:23:53 -04004521 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004522 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4523 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004524 * percpu refs of all css's are confirmed to be killed. This
4525 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004526 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004527 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004528 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004529 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004530 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004531
Tejun Heo455050d2013-06-13 19:27:41 -07004532 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4533 raw_spin_lock(&release_list_lock);
4534 if (!list_empty(&cgrp->release_list))
4535 list_del_init(&cgrp->release_list);
4536 raw_spin_unlock(&release_list_lock);
4537
4538 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004539 * If @cgrp has css's attached, the second stage of cgroup
4540 * destruction is kicked off from css_killed_work_fn() after the
4541 * refs of all attached css's are killed. If @cgrp doesn't have
4542 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004543 */
Tejun Heof20104d2013-08-13 20:22:50 -04004544 if (!cgrp->nr_css)
4545 cgroup_destroy_css_killed(cgrp);
4546
Tejun Heo2bd59d42014-02-11 11:52:49 -05004547 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004548 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004549
Tejun Heof20104d2013-08-13 20:22:50 -04004550 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004551 * There are two control paths which try to determine cgroup from
4552 * dentry without going through kernfs - cgroupstats_build() and
Tejun Heoec903c02014-05-13 12:11:01 -04004553 * css_tryget_online_from_dir(). Those are supported by RCU
4554 * protecting clearing of cgrp->kn->priv backpointer, which should
4555 * happen after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004556 */
Tejun Heo6f305582014-02-12 09:29:50 -05004557 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004558 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004559
Tejun Heo4ac06012014-02-11 11:52:47 -05004560 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004561
Tejun Heoea15f8c2013-06-13 19:27:42 -07004562 return 0;
4563};
4564
Tejun Heod3daf282013-06-13 19:39:16 -07004565/**
Tejun Heof20104d2013-08-13 20:22:50 -04004566 * cgroup_destroy_css_killed - the second step of cgroup destruction
Fabian Frederick60106942014-05-05 20:08:13 +02004567 * @cgrp: the cgroup whose csses have just finished offlining
Tejun Heod3daf282013-06-13 19:39:16 -07004568 *
4569 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004570 * destroyed after all css's are offlined and performs the rest of
4571 * destruction. This is the second step of destruction described in the
4572 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004573 */
Tejun Heof20104d2013-08-13 20:22:50 -04004574static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004575{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004576 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004577
Tejun Heoace2bee2014-02-11 11:52:47 -05004578 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004579 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004580
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004581 /* delete this cgroup from parent->children */
4582 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004583
Tejun Heo59f52962014-02-11 11:52:49 -05004584 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004585
Paul Menageddbcc7e2007-10-18 23:39:30 -07004586 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004587 check_for_release(parent);
4588}
4589
Tejun Heo2bd59d42014-02-11 11:52:49 -05004590static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004591{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004592 struct cgroup *cgrp = kn->priv;
4593 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004594
Tejun Heo2bd59d42014-02-11 11:52:49 -05004595 /*
4596 * This is self-destruction but @kn can't be removed while this
4597 * callback is in progress. Let's break active protection. Once
4598 * the protection is broken, @cgrp can be destroyed at any point.
4599 * Pin it so that it stays accessible.
4600 */
4601 cgroup_get(cgrp);
4602 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004603
Tejun Heoace2bee2014-02-11 11:52:47 -05004604 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004605 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004606
Tejun Heo2bd59d42014-02-11 11:52:49 -05004607 /*
4608 * @cgrp might already have been destroyed while we're trying to
4609 * grab the mutexes.
4610 */
4611 if (!cgroup_is_dead(cgrp))
4612 ret = cgroup_destroy_locked(cgrp);
4613
Tejun Heo42809dd2012-11-19 08:13:37 -08004614 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004615 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004616
Tejun Heo2bd59d42014-02-11 11:52:49 -05004617 kernfs_unbreak_active_protection(kn);
4618 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004619 return ret;
4620}
4621
Tejun Heo2bd59d42014-02-11 11:52:49 -05004622static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4623 .remount_fs = cgroup_remount,
4624 .show_options = cgroup_show_options,
4625 .mkdir = cgroup_mkdir,
4626 .rmdir = cgroup_rmdir,
4627 .rename = cgroup_rename,
4628};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004629
Tejun Heo15a4c832014-05-04 15:09:14 -04004630static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004633
4634 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635
Tejun Heoace2bee2014-02-11 11:52:47 -05004636 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004637 mutex_lock(&cgroup_mutex);
4638
Tejun Heo15a4c832014-05-04 15:09:14 -04004639 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004640 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004641
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004642 /* Create the root cgroup state for this subsystem */
4643 ss->root = &cgrp_dfl_root;
4644 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645 /* We don't handle early failures gracefully */
4646 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004647 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004648 if (early) {
4649 /* idr_alloc() can't be called safely during early init */
4650 css->id = 1;
4651 } else {
4652 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4653 BUG_ON(css->id < 0);
4654 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004655
Li Zefane8d55fd2008-04-29 01:00:13 -07004656 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004657 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004658 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004659 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004660 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004661
4662 need_forkexit_callback |= ss->fork || ss->exit;
4663
Li Zefane8d55fd2008-04-29 01:00:13 -07004664 /* At system boot, before all subsystems have been
4665 * registered, no tasks have been forked, so we don't
4666 * need to invoke fork callbacks here. */
4667 BUG_ON(!list_empty(&init_task.tasks));
4668
Tejun Heoae7f1642013-08-13 20:22:50 -04004669 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004670
Tejun Heof392e512014-04-23 11:13:14 -04004671 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004672
Ben Blume6a11052010-03-10 15:22:09 -08004673 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004674 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675}
4676
4677/**
Li Zefana043e3b2008-02-23 15:24:09 -08004678 * cgroup_init_early - cgroup initialization at system boot
4679 *
4680 * Initialize cgroups at system boot, and initialize any
4681 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004682 */
4683int __init cgroup_init_early(void)
4684{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004685 static struct cgroup_sb_opts __initdata opts =
4686 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004687 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004689
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004690 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004691 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004692
Tejun Heo3ed80a62014-02-08 10:36:58 -05004693 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004694 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004695 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4696 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004697 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004698 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4699 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004700
Tejun Heoaec25022014-02-08 10:36:58 -05004701 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004702 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004703
4704 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004705 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004706 }
4707 return 0;
4708}
4709
4710/**
Li Zefana043e3b2008-02-23 15:24:09 -08004711 * cgroup_init - cgroup initialization
4712 *
4713 * Register cgroup filesystem and /proc file, and initialize
4714 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004715 */
4716int __init cgroup_init(void)
4717{
Tejun Heo30159ec2013-06-25 11:53:37 -07004718 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004719 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004720 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004721
Tejun Heo2bd59d42014-02-11 11:52:49 -05004722 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004723
Tejun Heo985ed672014-03-19 10:23:53 -04004724 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004725 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004726
Tejun Heo82fe9b02013-06-25 11:53:37 -07004727 /* Add init_css_set to the hash table */
4728 key = css_set_hash(init_css_set.subsys);
4729 hash_add(css_set_table, &init_css_set.hlist, key);
4730
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004731 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004732
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004733 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004734 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004735
Tejun Heo172a2c062014-03-19 10:23:53 -04004736 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004737 if (ss->early_init) {
4738 struct cgroup_subsys_state *css =
4739 init_css_set.subsys[ss->id];
4740
4741 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4742 GFP_KERNEL);
4743 BUG_ON(css->id < 0);
4744 } else {
4745 cgroup_init_subsys(ss, false);
4746 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004747
Tejun Heo2d8f2432014-04-23 11:13:15 -04004748 list_add_tail(&init_css_set.e_cset_node[ssid],
4749 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4750
Tejun Heo172a2c062014-03-19 10:23:53 -04004751 /*
4752 * cftype registration needs kmalloc and can't be done
4753 * during early_init. Register base cftypes separately.
4754 */
4755 if (ss->base_cftypes)
4756 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4757 }
Greg KH676db4a2010-08-05 13:53:35 -07004758
Paul Menageddbcc7e2007-10-18 23:39:30 -07004759 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004760 if (!cgroup_kobj)
4761 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004762
4763 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004764 if (err < 0) {
4765 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004766 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004767 }
4768
4769 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004770 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004771}
Paul Menageb4f48b62007-10-18 23:39:33 -07004772
Tejun Heoe5fca242013-11-22 17:14:39 -05004773static int __init cgroup_wq_init(void)
4774{
4775 /*
4776 * There isn't much point in executing destruction path in
4777 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004778 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004779 *
4780 * We would prefer to do this in cgroup_init() above, but that
4781 * is called before init_workqueues(): so leave this until after.
4782 */
Tejun Heo1a115332014-02-12 19:06:19 -05004783 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004784 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004785
4786 /*
4787 * Used to destroy pidlists and separate to serve as flush domain.
4788 * Cap @max_active to 1 too.
4789 */
4790 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4791 0, 1);
4792 BUG_ON(!cgroup_pidlist_destroy_wq);
4793
Tejun Heoe5fca242013-11-22 17:14:39 -05004794 return 0;
4795}
4796core_initcall(cgroup_wq_init);
4797
Paul Menagea4243162007-10-18 23:39:35 -07004798/*
4799 * proc_cgroup_show()
4800 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4801 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004802 */
4803
4804/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004805int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004806{
4807 struct pid *pid;
4808 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004809 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004810 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004811 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004812
4813 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004814 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004815 if (!buf)
4816 goto out;
4817
4818 retval = -ESRCH;
4819 pid = m->private;
4820 tsk = get_pid_task(pid, PIDTYPE_PID);
4821 if (!tsk)
4822 goto out_free;
4823
4824 retval = 0;
4825
4826 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004827 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004828
Tejun Heo985ed672014-03-19 10:23:53 -04004829 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004830 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004831 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004832 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004833
Tejun Heoa2dd4242014-03-19 10:23:55 -04004834 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004835 continue;
4836
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004837 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004838 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004839 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004840 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004841 if (strlen(root->name))
4842 seq_printf(m, "%sname=%s", count ? "," : "",
4843 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004844 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004845 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004846 path = cgroup_path(cgrp, buf, PATH_MAX);
4847 if (!path) {
4848 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004849 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004850 }
4851 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004852 seq_putc(m, '\n');
4853 }
4854
4855out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004856 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004857 mutex_unlock(&cgroup_mutex);
4858 put_task_struct(tsk);
4859out_free:
4860 kfree(buf);
4861out:
4862 return retval;
4863}
4864
Paul Menagea4243162007-10-18 23:39:35 -07004865/* Display information about each subsystem and each hierarchy */
4866static int proc_cgroupstats_show(struct seq_file *m, void *v)
4867{
Tejun Heo30159ec2013-06-25 11:53:37 -07004868 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004869 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004870
Paul Menage8bab8dd2008-04-04 14:29:57 -07004871 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004872 /*
4873 * ideally we don't want subsystems moving around while we do this.
4874 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4875 * subsys/hierarchy state.
4876 */
Paul Menagea4243162007-10-18 23:39:35 -07004877 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004878
4879 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004880 seq_printf(m, "%s\t%d\t%d\t%d\n",
4881 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004882 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004883
Paul Menagea4243162007-10-18 23:39:35 -07004884 mutex_unlock(&cgroup_mutex);
4885 return 0;
4886}
4887
4888static int cgroupstats_open(struct inode *inode, struct file *file)
4889{
Al Viro9dce07f2008-03-29 03:07:28 +00004890 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004891}
4892
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004893static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004894 .open = cgroupstats_open,
4895 .read = seq_read,
4896 .llseek = seq_lseek,
4897 .release = single_release,
4898};
4899
Paul Menageb4f48b62007-10-18 23:39:33 -07004900/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004901 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004902 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004903 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004904 * A task is associated with the init_css_set until cgroup_post_fork()
4905 * attaches it to the parent's css_set. Empty cg_list indicates that
4906 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004907 */
4908void cgroup_fork(struct task_struct *child)
4909{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004910 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004911 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004912}
4913
4914/**
Li Zefana043e3b2008-02-23 15:24:09 -08004915 * cgroup_post_fork - called on a new task after adding it to the task list
4916 * @child: the task in question
4917 *
Tejun Heo5edee612012-10-16 15:03:14 -07004918 * Adds the task to the list running through its css_set if necessary and
4919 * call the subsystem fork() callbacks. Has to be after the task is
4920 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004921 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004922 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004923 */
Paul Menage817929e2007-10-18 23:39:36 -07004924void cgroup_post_fork(struct task_struct *child)
4925{
Tejun Heo30159ec2013-06-25 11:53:37 -07004926 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004927 int i;
4928
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004929 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004930 * This may race against cgroup_enable_task_cg_links(). As that
4931 * function sets use_task_css_set_links before grabbing
4932 * tasklist_lock and we just went through tasklist_lock to add
4933 * @child, it's guaranteed that either we see the set
4934 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4935 * @child during its iteration.
4936 *
4937 * If we won the race, @child is associated with %current's
4938 * css_set. Grabbing css_set_rwsem guarantees both that the
4939 * association is stable, and, on completion of the parent's
4940 * migration, @child is visible in the source of migration or
4941 * already in the destination cgroup. This guarantee is necessary
4942 * when implementing operations which need to migrate all tasks of
4943 * a cgroup to another.
4944 *
4945 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4946 * will remain in init_css_set. This is safe because all tasks are
4947 * in the init_css_set before cg_links is enabled and there's no
4948 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004949 */
Paul Menage817929e2007-10-18 23:39:36 -07004950 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004951 struct css_set *cset;
4952
Tejun Heo96d365e2014-02-13 06:58:40 -05004953 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004954 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004955 if (list_empty(&child->cg_list)) {
4956 rcu_assign_pointer(child->cgroups, cset);
4957 list_add(&child->cg_list, &cset->tasks);
4958 get_css_set(cset);
4959 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004960 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004961 }
Tejun Heo5edee612012-10-16 15:03:14 -07004962
4963 /*
4964 * Call ss->fork(). This must happen after @child is linked on
4965 * css_set; otherwise, @child might change state between ->fork()
4966 * and addition to css_set.
4967 */
4968 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004969 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004970 if (ss->fork)
4971 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004972 }
Paul Menage817929e2007-10-18 23:39:36 -07004973}
Tejun Heo5edee612012-10-16 15:03:14 -07004974
Paul Menage817929e2007-10-18 23:39:36 -07004975/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004976 * cgroup_exit - detach cgroup from exiting task
4977 * @tsk: pointer to task_struct of exiting process
4978 *
4979 * Description: Detach cgroup from @tsk and release it.
4980 *
4981 * Note that cgroups marked notify_on_release force every task in
4982 * them to take the global cgroup_mutex mutex when exiting.
4983 * This could impact scaling on very large systems. Be reluctant to
4984 * use notify_on_release cgroups where very high task exit scaling
4985 * is required on large systems.
4986 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004987 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4988 * call cgroup_exit() while the task is still competent to handle
4989 * notify_on_release(), then leave the task attached to the root cgroup in
4990 * each hierarchy for the remainder of its exit. No need to bother with
4991 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004992 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004993 */
Li Zefan1ec41832014-03-28 15:22:19 +08004994void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004995{
Tejun Heo30159ec2013-06-25 11:53:37 -07004996 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004997 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004998 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004999 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005000
5001 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005002 * Unlink from @tsk from its css_set. As migration path can't race
5003 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005004 */
5005 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005006 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005007 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005008 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005009 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005010 }
5011
Paul Menageb4f48b62007-10-18 23:39:33 -07005012 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005013 cset = task_css_set(tsk);
5014 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005015
Li Zefan1ec41832014-03-28 15:22:19 +08005016 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005017 /* see cgroup_post_fork() for details */
5018 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005019 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005020 struct cgroup_subsys_state *old_css = cset->subsys[i];
5021 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005022
Tejun Heoeb954192013-08-08 20:11:23 -04005023 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005024 }
5025 }
5026 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005027
Tejun Heoeaf797a2014-02-25 10:04:03 -05005028 if (put_cset)
5029 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005030}
Paul Menage697f4162007-10-18 23:39:34 -07005031
Paul Menagebd89aab2007-10-18 23:40:44 -07005032static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005033{
Li Zefanf50daa72013-03-01 15:06:07 +08005034 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005035 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005036 /*
5037 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005038 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005039 * it now
5040 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005041 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005042
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005043 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005044 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005045 list_empty(&cgrp->release_list)) {
5046 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047 need_schedule_work = 1;
5048 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005049 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005050 if (need_schedule_work)
5051 schedule_work(&release_agent_work);
5052 }
5053}
5054
Paul Menage81a6a5c2007-10-18 23:39:38 -07005055/*
5056 * Notify userspace when a cgroup is released, by running the
5057 * configured release agent with the name of the cgroup (path
5058 * relative to the root of cgroup file system) as the argument.
5059 *
5060 * Most likely, this user command will try to rmdir this cgroup.
5061 *
5062 * This races with the possibility that some other task will be
5063 * attached to this cgroup before it is removed, or that some other
5064 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5065 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5066 * unused, and this cgroup will be reprieved from its death sentence,
5067 * to continue to serve a useful existence. Next time it's released,
5068 * we will get notified again, if it still has 'notify_on_release' set.
5069 *
5070 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5071 * means only wait until the task is successfully execve()'d. The
5072 * separate release agent task is forked by call_usermodehelper(),
5073 * then control in this thread returns here, without waiting for the
5074 * release agent task. We don't bother to wait because the caller of
5075 * this routine has no use for the exit status of the release agent
5076 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005077 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005078static void cgroup_release_agent(struct work_struct *work)
5079{
5080 BUG_ON(work != &release_agent_work);
5081 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005082 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005083 while (!list_empty(&release_list)) {
5084 char *argv[3], *envp[3];
5085 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005086 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005087 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088 struct cgroup,
5089 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005090 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005091 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005092 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005093 if (!pathbuf)
5094 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005095 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5096 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005097 goto continue_free;
5098 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5099 if (!agentbuf)
5100 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005101
5102 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005103 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005104 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005105 argv[i] = NULL;
5106
5107 i = 0;
5108 /* minimal command environment */
5109 envp[i++] = "HOME=/";
5110 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5111 envp[i] = NULL;
5112
5113 /* Drop the lock while we invoke the usermode helper,
5114 * since the exec could involve hitting disk and hence
5115 * be a slow process */
5116 mutex_unlock(&cgroup_mutex);
5117 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005118 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005119 continue_free:
5120 kfree(pathbuf);
5121 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005122 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005123 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005124 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005125 mutex_unlock(&cgroup_mutex);
5126}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005127
5128static int __init cgroup_disable(char *str)
5129{
Tejun Heo30159ec2013-06-25 11:53:37 -07005130 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005131 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005132 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005133
5134 while ((token = strsep(&str, ",")) != NULL) {
5135 if (!*token)
5136 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005137
Tejun Heo3ed80a62014-02-08 10:36:58 -05005138 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005139 if (!strcmp(token, ss->name)) {
5140 ss->disabled = 1;
5141 printk(KERN_INFO "Disabling %s control group"
5142 " subsystem\n", ss->name);
5143 break;
5144 }
5145 }
5146 }
5147 return 1;
5148}
5149__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005150
Tejun Heob77d7b62013-08-13 11:01:54 -04005151/**
Tejun Heoec903c02014-05-13 12:11:01 -04005152 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005153 * @dentry: directory dentry of interest
5154 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005155 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005156 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5157 * to get the corresponding css and return it. If such css doesn't exist
5158 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005159 */
Tejun Heoec903c02014-05-13 12:11:01 -04005160struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5161 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005162{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005163 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5164 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005165 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005166
Tejun Heo35cf0832013-08-26 18:40:56 -04005167 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005168 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5169 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005170 return ERR_PTR(-EBADF);
5171
Tejun Heo5a17f542014-02-11 11:52:47 -05005172 rcu_read_lock();
5173
Tejun Heo2bd59d42014-02-11 11:52:49 -05005174 /*
5175 * This path doesn't originate from kernfs and @kn could already
5176 * have been or be removed at any point. @kn->priv is RCU
5177 * protected for this access. See destroy_locked() for details.
5178 */
5179 cgrp = rcu_dereference(kn->priv);
5180 if (cgrp)
5181 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005182
Tejun Heoec903c02014-05-13 12:11:01 -04005183 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005184 css = ERR_PTR(-ENOENT);
5185
5186 rcu_read_unlock();
5187 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005188}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005189
Li Zefan1cb650b2013-08-19 10:05:24 +08005190/**
5191 * css_from_id - lookup css by id
5192 * @id: the cgroup id
5193 * @ss: cgroup subsys to be looked into
5194 *
5195 * Returns the css if there's valid one with @id, otherwise returns NULL.
5196 * Should be called under rcu_read_lock().
5197 */
5198struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5199{
Tejun Heo6fa49182014-05-04 15:09:13 -04005200 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005201 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005202}
5203
Paul Menagefe693432009-09-23 15:56:20 -07005204#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005205static struct cgroup_subsys_state *
5206debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005207{
5208 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5209
5210 if (!css)
5211 return ERR_PTR(-ENOMEM);
5212
5213 return css;
5214}
5215
Tejun Heoeb954192013-08-08 20:11:23 -04005216static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005217{
Tejun Heoeb954192013-08-08 20:11:23 -04005218 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005219}
5220
Tejun Heo182446d2013-08-08 20:11:24 -04005221static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5222 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005223{
Tejun Heo182446d2013-08-08 20:11:24 -04005224 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005225}
5226
Tejun Heo182446d2013-08-08 20:11:24 -04005227static u64 current_css_set_read(struct cgroup_subsys_state *css,
5228 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005229{
5230 return (u64)(unsigned long)current->cgroups;
5231}
5232
Tejun Heo182446d2013-08-08 20:11:24 -04005233static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005234 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005235{
5236 u64 count;
5237
5238 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005239 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005240 rcu_read_unlock();
5241 return count;
5242}
5243
Tejun Heo2da8ca82013-12-05 12:28:04 -05005244static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005245{
Tejun Heo69d02062013-06-12 21:04:50 -07005246 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005247 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005248 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005249
Tejun Heoe61734c2014-02-12 09:29:50 -05005250 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5251 if (!name_buf)
5252 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005253
Tejun Heo96d365e2014-02-13 06:58:40 -05005254 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005255 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005256 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005257 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005258 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005259
Tejun Heoa2dd4242014-03-19 10:23:55 -04005260 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005261 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005262 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005263 }
5264 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005265 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005266 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005267 return 0;
5268}
5269
5270#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005271static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005272{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005273 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005274 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005275
Tejun Heo96d365e2014-02-13 06:58:40 -05005276 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005277 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005278 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005279 struct task_struct *task;
5280 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005281
Tejun Heo5abb8852013-06-12 21:04:49 -07005282 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005283
Tejun Heo5abb8852013-06-12 21:04:49 -07005284 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005285 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5286 goto overflow;
5287 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005288 }
Tejun Heoc7561122014-02-25 10:04:01 -05005289
5290 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5291 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5292 goto overflow;
5293 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5294 }
5295 continue;
5296 overflow:
5297 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005298 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005299 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005300 return 0;
5301}
5302
Tejun Heo182446d2013-08-08 20:11:24 -04005303static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005304{
Tejun Heo182446d2013-08-08 20:11:24 -04005305 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005306}
5307
5308static struct cftype debug_files[] = {
5309 {
Paul Menagefe693432009-09-23 15:56:20 -07005310 .name = "taskcount",
5311 .read_u64 = debug_taskcount_read,
5312 },
5313
5314 {
5315 .name = "current_css_set",
5316 .read_u64 = current_css_set_read,
5317 },
5318
5319 {
5320 .name = "current_css_set_refcount",
5321 .read_u64 = current_css_set_refcount_read,
5322 },
5323
5324 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005325 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005326 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005327 },
5328
5329 {
5330 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005331 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005332 },
5333
5334 {
Paul Menagefe693432009-09-23 15:56:20 -07005335 .name = "releasable",
5336 .read_u64 = releasable_read,
5337 },
Paul Menagefe693432009-09-23 15:56:20 -07005338
Tejun Heo4baf6e32012-04-01 12:09:55 -07005339 { } /* terminate */
5340};
Paul Menagefe693432009-09-23 15:56:20 -07005341
Tejun Heo073219e2014-02-08 10:36:58 -05005342struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005343 .css_alloc = debug_css_alloc,
5344 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005345 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005346};
5347#endif /* CONFIG_CGROUP_DEBUG */