blob: bf1d7ce250ac19fc1fc8ea1d74b667fa82d8c96e [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
Paul Menage81a6a5c2007-10-18 23:39:38 -0700389/* the list of cgroups eligible for automatic release. Protected by
390 * release_list_lock */
391static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200392static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393static void cgroup_release_agent(struct work_struct *work);
394static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700395static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700396
Tejun Heo69d02062013-06-12 21:04:50 -0700397/*
398 * A cgroup can be associated with multiple css_sets as different tasks may
399 * belong to different cgroups on different hierarchies. In the other
400 * direction, a css_set is naturally associated with multiple cgroups.
401 * This M:N relationship is represented by the following link structure
402 * which exists for each association and allows traversing the associations
403 * from both sides.
404 */
405struct cgrp_cset_link {
406 /* the cgroup and css_set this link associates */
407 struct cgroup *cgrp;
408 struct css_set *cset;
409
410 /* list of cgrp_cset_links anchored at cgrp->cset_links */
411 struct list_head cset_link;
412
413 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
414 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700415};
416
Tejun Heo172a2c062014-03-19 10:23:53 -0400417/*
418 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700419 * hierarchies being mounted. It contains a pointer to the root state
420 * for each subsystem. Also used to anchor the list of css_sets. Not
421 * reference-counted, to improve performance when child cgroups
422 * haven't been created.
423 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400424struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400425 .refcount = ATOMIC_INIT(1),
426 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
427 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
428 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
429 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
430 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
431};
Paul Menage817929e2007-10-18 23:39:36 -0700432
Tejun Heo172a2c062014-03-19 10:23:53 -0400433static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700434
Tejun Heo842b5972014-04-25 18:28:02 -0400435/**
436 * cgroup_update_populated - updated populated count of a cgroup
437 * @cgrp: the target cgroup
438 * @populated: inc or dec populated count
439 *
440 * @cgrp is either getting the first task (css_set) or losing the last.
441 * Update @cgrp->populated_cnt accordingly. The count is propagated
442 * towards root so that a given cgroup's populated_cnt is zero iff the
443 * cgroup and all its descendants are empty.
444 *
445 * @cgrp's interface file "cgroup.populated" is zero if
446 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
447 * changes from or to zero, userland is notified that the content of the
448 * interface file has changed. This can be used to detect when @cgrp and
449 * its descendants become populated or empty.
450 */
451static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
452{
453 lockdep_assert_held(&css_set_rwsem);
454
455 do {
456 bool trigger;
457
458 if (populated)
459 trigger = !cgrp->populated_cnt++;
460 else
461 trigger = !--cgrp->populated_cnt;
462
463 if (!trigger)
464 break;
465
466 if (cgrp->populated_kn)
467 kernfs_notify(cgrp->populated_kn);
468 cgrp = cgrp->parent;
469 } while (cgrp);
470}
471
Paul Menage7717f7b2009-09-23 15:56:22 -0700472/*
473 * hash table for cgroup groups. This improves the performance to find
474 * an existing css_set. This hash doesn't (currently) take into
475 * account cgroups in empty hierarchies.
476 */
Li Zefan472b1052008-04-29 01:00:11 -0700477#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800478static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700479
Li Zefan0ac801f2013-01-10 11:49:27 +0800480static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700481{
Li Zefan0ac801f2013-01-10 11:49:27 +0800482 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700483 struct cgroup_subsys *ss;
484 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700485
Tejun Heo30159ec2013-06-25 11:53:37 -0700486 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800487 key += (unsigned long)css[i];
488 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700489
Li Zefan0ac801f2013-01-10 11:49:27 +0800490 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700491}
492
Tejun Heo89c55092014-02-13 06:58:40 -0500493static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700494{
Tejun Heo69d02062013-06-12 21:04:50 -0700495 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400496 struct cgroup_subsys *ss;
497 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700498
Tejun Heo89c55092014-02-13 06:58:40 -0500499 lockdep_assert_held(&css_set_rwsem);
500
501 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700502 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700503
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700504 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400505 for_each_subsys(ss, ssid)
506 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700507 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700508 css_set_count--;
509
Tejun Heo69d02062013-06-12 21:04:50 -0700510 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700511 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700512
Tejun Heo69d02062013-06-12 21:04:50 -0700513 list_del(&link->cset_link);
514 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800515
Tejun Heo96d365e2014-02-13 06:58:40 -0500516 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400517 if (list_empty(&cgrp->cset_links)) {
518 cgroup_update_populated(cgrp, false);
519 if (notify_on_release(cgrp)) {
520 if (taskexit)
521 set_bit(CGRP_RELEASABLE, &cgrp->flags);
522 check_for_release(cgrp);
523 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700524 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700525
526 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700527 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700528
Tejun Heo5abb8852013-06-12 21:04:49 -0700529 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700530}
531
Tejun Heo89c55092014-02-13 06:58:40 -0500532static void put_css_set(struct css_set *cset, bool taskexit)
533{
534 /*
535 * Ensure that the refcount doesn't hit zero while any readers
536 * can see it. Similar to atomic_dec_and_lock(), but for an
537 * rwlock
538 */
539 if (atomic_add_unless(&cset->refcount, -1, 1))
540 return;
541
542 down_write(&css_set_rwsem);
543 put_css_set_locked(cset, taskexit);
544 up_write(&css_set_rwsem);
545}
546
Paul Menage817929e2007-10-18 23:39:36 -0700547/*
548 * refcounted get/put for css_set objects
549 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700550static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700551{
Tejun Heo5abb8852013-06-12 21:04:49 -0700552 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700553}
554
Tejun Heob326f9d2013-06-24 15:21:48 -0700555/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700556 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700557 * @cset: candidate css_set being tested
558 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700559 * @new_cgrp: cgroup that's being entered by the task
560 * @template: desired set of css pointers in css_set (pre-calculated)
561 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800562 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700563 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
564 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700565static bool compare_css_sets(struct css_set *cset,
566 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700567 struct cgroup *new_cgrp,
568 struct cgroup_subsys_state *template[])
569{
570 struct list_head *l1, *l2;
571
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400572 /*
573 * On the default hierarchy, there can be csets which are
574 * associated with the same set of cgroups but different csses.
575 * Let's first ensure that csses match.
576 */
577 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700578 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700579
580 /*
581 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400582 * different cgroups in hierarchies. As different cgroups may
583 * share the same effective css, this comparison is always
584 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 */
Tejun Heo69d02062013-06-12 21:04:50 -0700586 l1 = &cset->cgrp_links;
587 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700588 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700589 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700590 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700591
592 l1 = l1->next;
593 l2 = l2->next;
594 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700595 if (l1 == &cset->cgrp_links) {
596 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700597 break;
598 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700599 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700600 }
601 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700602 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
603 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
604 cgrp1 = link1->cgrp;
605 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700606 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700607 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700608
609 /*
610 * If this hierarchy is the hierarchy of the cgroup
611 * that's changing, then we need to check that this
612 * css_set points to the new cgroup; if it's any other
613 * hierarchy, then this css_set should point to the
614 * same cgroup as the old css_set.
615 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700616 if (cgrp1->root == new_cgrp->root) {
617 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 return false;
619 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700621 return false;
622 }
623 }
624 return true;
625}
626
Tejun Heob326f9d2013-06-24 15:21:48 -0700627/**
628 * find_existing_css_set - init css array and find the matching css_set
629 * @old_cset: the css_set that we're using before the cgroup transition
630 * @cgrp: the cgroup that we're moving into
631 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700632 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700633static struct css_set *find_existing_css_set(struct css_set *old_cset,
634 struct cgroup *cgrp,
635 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700636{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400637 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700638 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800640 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700641 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700642
Ben Blumaae8aab2010-03-10 15:22:07 -0800643 /*
644 * Build the set of subsystem state objects that we want to see in the
645 * new css_set. while subsystems can change globally, the entries here
646 * won't change, so no need for locking.
647 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700648 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400649 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400650 /*
651 * @ss is in this hierarchy, so we want the
652 * effective css from @cgrp.
653 */
654 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700655 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400656 /*
657 * @ss is not in this hierarchy, so we don't want
658 * to change the css.
659 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700661 }
662 }
663
Li Zefan0ac801f2013-01-10 11:49:27 +0800664 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 hash_for_each_possible(css_set_table, cset, hlist, key) {
666 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700667 continue;
668
669 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700670 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700671 }
Paul Menage817929e2007-10-18 23:39:36 -0700672
673 /* No existing cgroup group matched */
674 return NULL;
675}
676
Tejun Heo69d02062013-06-12 21:04:50 -0700677static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700678{
Tejun Heo69d02062013-06-12 21:04:50 -0700679 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700680
Tejun Heo69d02062013-06-12 21:04:50 -0700681 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
682 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700683 kfree(link);
684 }
685}
686
Tejun Heo69d02062013-06-12 21:04:50 -0700687/**
688 * allocate_cgrp_cset_links - allocate cgrp_cset_links
689 * @count: the number of links to allocate
690 * @tmp_links: list_head the allocated links are put on
691 *
692 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
693 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700694 */
Tejun Heo69d02062013-06-12 21:04:50 -0700695static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700696{
Tejun Heo69d02062013-06-12 21:04:50 -0700697 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700698 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700699
700 INIT_LIST_HEAD(tmp_links);
701
Li Zefan36553432008-07-29 22:33:19 -0700702 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700703 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700704 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700705 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700706 return -ENOMEM;
707 }
Tejun Heo69d02062013-06-12 21:04:50 -0700708 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700709 }
710 return 0;
711}
712
Li Zefanc12f65d2009-01-07 18:07:42 -0800713/**
714 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700715 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700716 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800717 * @cgrp: the destination cgroup
718 */
Tejun Heo69d02062013-06-12 21:04:50 -0700719static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
720 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800721{
Tejun Heo69d02062013-06-12 21:04:50 -0700722 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800723
Tejun Heo69d02062013-06-12 21:04:50 -0700724 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400725
726 if (cgroup_on_dfl(cgrp))
727 cset->dfl_cgrp = cgrp;
728
Tejun Heo69d02062013-06-12 21:04:50 -0700729 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
730 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700731 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400732
733 if (list_empty(&cgrp->cset_links))
734 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700735 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400736
Paul Menage7717f7b2009-09-23 15:56:22 -0700737 /*
738 * Always add links to the tail of the list so that the list
739 * is sorted by order of hierarchy creation
740 */
Tejun Heo69d02062013-06-12 21:04:50 -0700741 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800742}
743
Tejun Heob326f9d2013-06-24 15:21:48 -0700744/**
745 * find_css_set - return a new css_set with one cgroup updated
746 * @old_cset: the baseline css_set
747 * @cgrp: the cgroup to be updated
748 *
749 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
750 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700751 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700752static struct css_set *find_css_set(struct css_set *old_cset,
753 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700754{
Tejun Heob326f9d2013-06-24 15:21:48 -0700755 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700756 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700757 struct list_head tmp_links;
758 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400759 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800760 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400761 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700762
Tejun Heob326f9d2013-06-24 15:21:48 -0700763 lockdep_assert_held(&cgroup_mutex);
764
Paul Menage817929e2007-10-18 23:39:36 -0700765 /* First see if we already have a cgroup group that matches
766 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500767 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700768 cset = find_existing_css_set(old_cset, cgrp, template);
769 if (cset)
770 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500771 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700772
Tejun Heo5abb8852013-06-12 21:04:49 -0700773 if (cset)
774 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700775
Tejun Heof4f4be22013-06-12 21:04:51 -0700776 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700777 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700778 return NULL;
779
Tejun Heo69d02062013-06-12 21:04:50 -0700780 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700781 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700782 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700783 return NULL;
784 }
785
Tejun Heo5abb8852013-06-12 21:04:49 -0700786 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700787 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700788 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500789 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500790 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500791 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700792 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700793
794 /* Copy the set of subsystem state objects generated in
795 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700796 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700797
Tejun Heo96d365e2014-02-13 06:58:40 -0500798 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700799 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700800 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700801 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700802
Paul Menage7717f7b2009-09-23 15:56:22 -0700803 if (c->root == cgrp->root)
804 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700805 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700806 }
Paul Menage817929e2007-10-18 23:39:36 -0700807
Tejun Heo69d02062013-06-12 21:04:50 -0700808 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700809
Paul Menage817929e2007-10-18 23:39:36 -0700810 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700811
Tejun Heo2d8f2432014-04-23 11:13:15 -0400812 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700813 key = css_set_hash(cset->subsys);
814 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700815
Tejun Heo2d8f2432014-04-23 11:13:15 -0400816 for_each_subsys(ss, ssid)
817 list_add_tail(&cset->e_cset_node[ssid],
818 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
819
Tejun Heo96d365e2014-02-13 06:58:40 -0500820 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700821
Tejun Heo5abb8852013-06-12 21:04:49 -0700822 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700823}
824
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400825static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700826{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400827 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500828
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400829 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500830}
831
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400832static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500833{
834 int id;
835
836 lockdep_assert_held(&cgroup_mutex);
837
Tejun Heo985ed672014-03-19 10:23:53 -0400838 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500839 if (id < 0)
840 return id;
841
842 root->hierarchy_id = id;
843 return 0;
844}
845
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400846static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500847{
848 lockdep_assert_held(&cgroup_mutex);
849
850 if (root->hierarchy_id) {
851 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
852 root->hierarchy_id = 0;
853 }
854}
855
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400856static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500857{
858 if (root) {
859 /* hierarhcy ID shoulid already have been released */
860 WARN_ON_ONCE(root->hierarchy_id);
861
862 idr_destroy(&root->cgroup_idr);
863 kfree(root);
864 }
865}
866
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400867static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500868{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400869 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500870 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500871
Tejun Heo2bd59d42014-02-11 11:52:49 -0500872 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500873 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500874
Tejun Heo776f02f2014-02-12 09:29:50 -0500875 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500876 BUG_ON(!list_empty(&cgrp->children));
877
Tejun Heof2e85d52014-02-11 11:52:49 -0500878 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400879 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500880
881 /*
882 * Release all the links from cset_links to this hierarchy's
883 * root cgroup
884 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500885 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500886
887 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
888 list_del(&link->cset_link);
889 list_del(&link->cgrp_link);
890 kfree(link);
891 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500892 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500893
894 if (!list_empty(&root->root_list)) {
895 list_del(&root->root_list);
896 cgroup_root_count--;
897 }
898
899 cgroup_exit_root_id(root);
900
901 mutex_unlock(&cgroup_mutex);
902 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500903
Tejun Heo2bd59d42014-02-11 11:52:49 -0500904 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500905 cgroup_free_root(root);
906}
907
Tejun Heoceb6a082014-02-25 10:04:02 -0500908/* look up cgroup associated with given css_set on the specified hierarchy */
909static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400910 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700911{
Paul Menage7717f7b2009-09-23 15:56:22 -0700912 struct cgroup *res = NULL;
913
Tejun Heo96d365e2014-02-13 06:58:40 -0500914 lockdep_assert_held(&cgroup_mutex);
915 lockdep_assert_held(&css_set_rwsem);
916
Tejun Heo5abb8852013-06-12 21:04:49 -0700917 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400918 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700919 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700920 struct cgrp_cset_link *link;
921
922 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700923 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700924
Paul Menage7717f7b2009-09-23 15:56:22 -0700925 if (c->root == root) {
926 res = c;
927 break;
928 }
929 }
930 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500931
Paul Menage7717f7b2009-09-23 15:56:22 -0700932 BUG_ON(!res);
933 return res;
934}
935
936/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500937 * Return the cgroup for "task" from the given hierarchy. Must be
938 * called with cgroup_mutex and css_set_rwsem held.
939 */
940static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400941 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500942{
943 /*
944 * No need to lock the task - since we hold cgroup_mutex the
945 * task can't change groups, so the only thing that can happen
946 * is that it exits and its css is set back to init_css_set.
947 */
948 return cset_cgroup_from_root(task_css_set(task), root);
949}
950
951/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952 * A task must hold cgroup_mutex to modify cgroups.
953 *
954 * Any task can increment and decrement the count field without lock.
955 * So in general, code holding cgroup_mutex can't rely on the count
956 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800957 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958 * means that no tasks are currently attached, therefore there is no
959 * way a task attached to that cgroup can fork (the other way to
960 * increment the count). So code holding cgroup_mutex can safely
961 * assume that if the count is zero, it will stay zero. Similarly, if
962 * a task holds cgroup_mutex on a cgroup with zero count, it
963 * knows that the cgroup won't be removed, as cgroup_rmdir()
964 * needs that mutex.
965 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
967 * (usually) take cgroup_mutex. These are the two most performance
968 * critical pieces of code here. The exception occurs on cgroup_exit(),
969 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
970 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800971 * to the release agent with the name of the cgroup (path relative to
972 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 *
974 * A cgroup can only be deleted if both its 'count' of using tasks
975 * is zero, and its list of 'children' cgroups is empty. Since all
976 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400977 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400979 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980 *
981 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800982 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983 */
984
Tejun Heo69dfa002014-05-04 15:09:13 -0400985static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500986static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700987static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700988
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500989static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
990 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500992 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
993 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
994 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
995 cft->ss->name, cft->name);
996 else
997 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
998 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999}
1000
Tejun Heof2e85d52014-02-11 11:52:49 -05001001/**
1002 * cgroup_file_mode - deduce file mode of a control file
1003 * @cft: the control file in question
1004 *
1005 * returns cft->mode if ->mode is not 0
1006 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1007 * returns S_IRUGO if it has only a read handler
1008 * returns S_IWUSR if it has only a write hander
1009 */
1010static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001011{
Tejun Heof2e85d52014-02-11 11:52:49 -05001012 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001013
Tejun Heof2e85d52014-02-11 11:52:49 -05001014 if (cft->mode)
1015 return cft->mode;
1016
1017 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1018 mode |= S_IRUGO;
1019
Tejun Heo6770c642014-05-13 12:16:21 -04001020 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001021 mode |= S_IWUSR;
1022
1023 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001024}
1025
Li Zefanbe445622013-01-24 14:31:42 +08001026static void cgroup_free_fn(struct work_struct *work)
1027{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001028 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001029
Tejun Heo3c9c8252014-02-12 09:29:50 -05001030 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001031 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001032
Tejun Heo776f02f2014-02-12 09:29:50 -05001033 if (cgrp->parent) {
1034 /*
1035 * We get a ref to the parent, and put the ref when this
1036 * cgroup is being freed, so it's guaranteed that the
1037 * parent won't be destroyed before its children.
1038 */
1039 cgroup_put(cgrp->parent);
1040 kernfs_put(cgrp->kn);
1041 kfree(cgrp);
1042 } else {
1043 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001044 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001045 * indicates that the root should be released.
1046 */
1047 cgroup_destroy_root(cgrp->root);
1048 }
Li Zefanbe445622013-01-24 14:31:42 +08001049}
1050
1051static void cgroup_free_rcu(struct rcu_head *head)
1052{
1053 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1054
Tejun Heoea15f8c2013-06-13 19:27:42 -07001055 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001056 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001057}
1058
Tejun Heo59f52962014-02-11 11:52:49 -05001059static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001061 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1062 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1063 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064}
1065
Tejun Heo59f52962014-02-11 11:52:49 -05001066static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001068 if (!atomic_dec_and_test(&cgrp->refcnt))
1069 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001070 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001071 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072
Tejun Heo6fa49182014-05-04 15:09:13 -04001073 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001074 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075
Tejun Heo2bd59d42014-02-11 11:52:49 -05001076 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077}
1078
Tejun Heoa9746d82014-05-13 12:19:22 -04001079/**
1080 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1081 * @kn: the kernfs_node being serviced
1082 *
1083 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1084 * the method finishes if locking succeeded. Note that once this function
1085 * returns the cgroup returned by cgroup_kn_lock_live() may become
1086 * inaccessible any time. If the caller intends to continue to access the
1087 * cgroup, it should pin it before invoking this function.
1088 */
1089static void cgroup_kn_unlock(struct kernfs_node *kn)
1090{
1091 struct cgroup *cgrp;
1092
1093 if (kernfs_type(kn) == KERNFS_DIR)
1094 cgrp = kn->priv;
1095 else
1096 cgrp = kn->parent->priv;
1097
1098 mutex_unlock(&cgroup_mutex);
1099 mutex_unlock(&cgroup_tree_mutex);
1100
1101 kernfs_unbreak_active_protection(kn);
1102 cgroup_put(cgrp);
1103}
1104
1105/**
1106 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1107 * @kn: the kernfs_node being serviced
1108 *
1109 * This helper is to be used by a cgroup kernfs method currently servicing
1110 * @kn. It breaks the active protection, performs cgroup locking and
1111 * verifies that the associated cgroup is alive. Returns the cgroup if
1112 * alive; otherwise, %NULL. A successful return should be undone by a
1113 * matching cgroup_kn_unlock() invocation.
1114 *
1115 * Any cgroup kernfs method implementation which requires locking the
1116 * associated cgroup should use this helper. It avoids nesting cgroup
1117 * locking under kernfs active protection and allows all kernfs operations
1118 * including self-removal.
1119 */
1120static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1121{
1122 struct cgroup *cgrp;
1123
1124 if (kernfs_type(kn) == KERNFS_DIR)
1125 cgrp = kn->priv;
1126 else
1127 cgrp = kn->parent->priv;
1128
1129 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001130 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001131 * active_ref. cgroup liveliness check alone provides enough
1132 * protection against removal. Ensure @cgrp stays accessible and
1133 * break the active_ref protection.
1134 */
1135 cgroup_get(cgrp);
1136 kernfs_break_active_protection(kn);
1137
1138 mutex_lock(&cgroup_tree_mutex);
1139 mutex_lock(&cgroup_mutex);
1140
1141 if (!cgroup_is_dead(cgrp))
1142 return cgrp;
1143
1144 cgroup_kn_unlock(kn);
1145 return NULL;
1146}
1147
Li Zefan2739d3c2013-01-21 18:18:33 +08001148static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001150 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151
Tejun Heoace2bee2014-02-11 11:52:47 -05001152 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04001153 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001154 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001155}
1156
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001157/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001158 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001159 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001160 * @subsys_mask: mask of the subsystem ids whose files should be removed
1161 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001162static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001163{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001164 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001165 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001166
Tejun Heob420ba72013-07-12 12:34:02 -07001167 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001168 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001169
Tejun Heo69dfa002014-05-04 15:09:13 -04001170 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001171 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001172 list_for_each_entry(cfts, &ss->cfts, node)
1173 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001174 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001175}
1176
Tejun Heo69dfa002014-05-04 15:09:13 -04001177static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001178{
Tejun Heo30159ec2013-06-25 11:53:37 -07001179 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001180 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001181
Tejun Heoace2bee2014-02-11 11:52:47 -05001182 lockdep_assert_held(&cgroup_tree_mutex);
1183 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001184
Tejun Heo5df36032014-03-19 10:23:54 -04001185 for_each_subsys(ss, ssid) {
1186 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001187 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001188
Tejun Heo7fd8c562014-04-23 11:13:16 -04001189 /* if @ss has non-root csses attached to it, can't move */
1190 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001191 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192
Tejun Heo5df36032014-03-19 10:23:54 -04001193 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001194 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001195 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001196 }
1197
Tejun Heoa2dd4242014-03-19 10:23:55 -04001198 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1199 if (ret) {
1200 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001201 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001202
Tejun Heoa2dd4242014-03-19 10:23:55 -04001203 /*
1204 * Rebinding back to the default root is not allowed to
1205 * fail. Using both default and non-default roots should
1206 * be rare. Moving subsystems back and forth even more so.
1207 * Just warn about it and continue.
1208 */
1209 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001210 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001211 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001212 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001213 }
Tejun Heo5df36032014-03-19 10:23:54 -04001214 }
Tejun Heo31261212013-06-28 17:07:30 -07001215
1216 /*
1217 * Nothing can fail from this point on. Remove files for the
1218 * removed subsystems and rebind each subsystem.
1219 */
Tejun Heo5df36032014-03-19 10:23:54 -04001220 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001221 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001222 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001223
Tejun Heo5df36032014-03-19 10:23:54 -04001224 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001225 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001226 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001227 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001228
Tejun Heo5df36032014-03-19 10:23:54 -04001229 if (!(ss_mask & (1 << ssid)))
1230 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001231
Tejun Heo5df36032014-03-19 10:23:54 -04001232 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001233 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001234
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001235 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001236
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001237 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1238 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001239 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001240 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001241
Tejun Heo2d8f2432014-04-23 11:13:15 -04001242 down_write(&css_set_rwsem);
1243 hash_for_each(css_set_table, i, cset, hlist)
1244 list_move_tail(&cset->e_cset_node[ss->id],
1245 &dst_root->cgrp.e_csets[ss->id]);
1246 up_write(&css_set_rwsem);
1247
Tejun Heof392e512014-04-23 11:13:14 -04001248 src_root->subsys_mask &= ~(1 << ssid);
1249 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1250
Tejun Heobd53d612014-04-23 11:13:16 -04001251 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001252 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001253 if (dst_root != &cgrp_dfl_root)
1254 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001255
Tejun Heo5df36032014-03-19 10:23:54 -04001256 if (ss->bind)
1257 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001258 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259
Tejun Heoa2dd4242014-03-19 10:23:55 -04001260 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261 return 0;
1262}
1263
Tejun Heo2bd59d42014-02-11 11:52:49 -05001264static int cgroup_show_options(struct seq_file *seq,
1265 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001266{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001267 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001268 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001269 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001270
Tejun Heob85d2042013-12-06 15:11:57 -05001271 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001272 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001273 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001274 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1275 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001276 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001277 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001278 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001279 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001280
1281 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001282 if (strlen(root->release_agent_path))
1283 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001284 spin_unlock(&release_agent_path_lock);
1285
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001286 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001287 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001288 if (strlen(root->name))
1289 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290 return 0;
1291}
1292
1293struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001294 unsigned int subsys_mask;
1295 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001296 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001297 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001298 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001299 /* User explicitly requested empty subsystem */
1300 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301};
1302
Ben Blumcf5d5942010-03-10 15:22:09 -08001303static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001305 char *token, *o = data;
1306 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001307 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001308 struct cgroup_subsys *ss;
1309 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001310
1311#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001312 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001313#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314
Paul Menagec6d57f32009-09-23 15:56:19 -07001315 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316
1317 while ((token = strsep(&o, ",")) != NULL) {
1318 if (!*token)
1319 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001320 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001321 /* Explicitly have no subsystems */
1322 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001323 continue;
1324 }
1325 if (!strcmp(token, "all")) {
1326 /* Mutually exclusive option 'all' + subsystem name */
1327 if (one_ss)
1328 return -EINVAL;
1329 all_ss = true;
1330 continue;
1331 }
Tejun Heo873fe092013-04-14 20:15:26 -07001332 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1333 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1334 continue;
1335 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001336 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001337 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001338 continue;
1339 }
1340 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001341 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001342 continue;
1343 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001344 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001345 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001346 continue;
1347 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001348 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001349 /* Specifying two release agents is forbidden */
1350 if (opts->release_agent)
1351 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001352 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001353 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001354 if (!opts->release_agent)
1355 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001356 continue;
1357 }
1358 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001359 const char *name = token + 5;
1360 /* Can't specify an empty name */
1361 if (!strlen(name))
1362 return -EINVAL;
1363 /* Must match [\w.-]+ */
1364 for (i = 0; i < strlen(name); i++) {
1365 char c = name[i];
1366 if (isalnum(c))
1367 continue;
1368 if ((c == '.') || (c == '-') || (c == '_'))
1369 continue;
1370 return -EINVAL;
1371 }
1372 /* Specifying two names is forbidden */
1373 if (opts->name)
1374 return -EINVAL;
1375 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001376 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001377 GFP_KERNEL);
1378 if (!opts->name)
1379 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001380
1381 continue;
1382 }
1383
Tejun Heo30159ec2013-06-25 11:53:37 -07001384 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001385 if (strcmp(token, ss->name))
1386 continue;
1387 if (ss->disabled)
1388 continue;
1389
1390 /* Mutually exclusive option 'all' + subsystem name */
1391 if (all_ss)
1392 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001393 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001394 one_ss = true;
1395
1396 break;
1397 }
1398 if (i == CGROUP_SUBSYS_COUNT)
1399 return -ENOENT;
1400 }
1401
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001402 /* Consistency checks */
1403
Tejun Heo873fe092013-04-14 20:15:26 -07001404 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001405 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 -07001406
Tejun Heod3ba07c2014-02-13 06:58:38 -05001407 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1408 opts->cpuset_clone_children || opts->release_agent ||
1409 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001410 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001411 return -EINVAL;
1412 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001413 } else {
1414 /*
1415 * If the 'all' option was specified select all the
1416 * subsystems, otherwise if 'none', 'name=' and a subsystem
1417 * name options were not specified, let's default to 'all'
1418 */
1419 if (all_ss || (!one_ss && !opts->none && !opts->name))
1420 for_each_subsys(ss, i)
1421 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001422 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001423
Tejun Heoa2dd4242014-03-19 10:23:55 -04001424 /*
1425 * We either have to specify by name or by subsystems. (So
1426 * all empty hierarchies must have a name).
1427 */
1428 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001429 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001430 }
1431
Li Zefanf9ab5b52009-06-17 16:26:33 -07001432 /*
1433 * Option noprefix was introduced just for backward compatibility
1434 * with the old cpuset, so we allow noprefix only if mounting just
1435 * the cpuset subsystem.
1436 */
Tejun Heo93438622013-04-14 20:15:25 -07001437 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001438 return -EINVAL;
1439
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440
1441 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001442 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001443 return -EINVAL;
1444
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445 return 0;
1446}
1447
Tejun Heo2bd59d42014-02-11 11:52:49 -05001448static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449{
1450 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001451 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001453 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454
Tejun Heo873fe092013-04-14 20:15:26 -07001455 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001456 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001457 return -EINVAL;
1458 }
1459
Tejun Heoace2bee2014-02-11 11:52:47 -05001460 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 mutex_lock(&cgroup_mutex);
1462
1463 /* See what subsystems are wanted */
1464 ret = parse_cgroupfs_options(data, &opts);
1465 if (ret)
1466 goto out_unlock;
1467
Tejun Heof392e512014-04-23 11:13:14 -04001468 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001469 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001470 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001471
Tejun Heof392e512014-04-23 11:13:14 -04001472 added_mask = opts.subsys_mask & ~root->subsys_mask;
1473 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001474
Ben Blumcf5d5942010-03-10 15:22:09 -08001475 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001476 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001477 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001478 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001479 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1480 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 goto out_unlock;
1483 }
1484
Tejun Heof172e672013-06-28 17:07:30 -07001485 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001486 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001487 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001488 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001489 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490
Tejun Heo5df36032014-03-19 10:23:54 -04001491 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001492 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001495 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001496
Tejun Heo69e943b2014-02-08 10:36:58 -05001497 if (opts.release_agent) {
1498 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001499 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001500 spin_unlock(&release_agent_path_lock);
1501 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001503 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001506 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 return ret;
1508}
1509
Tejun Heoafeb0f92014-02-13 06:58:39 -05001510/*
1511 * To reduce the fork() overhead for systems that are not actually using
1512 * their cgroups capability, we don't maintain the lists running through
1513 * each css_set to its tasks until we see the list actually used - in other
1514 * words after the first mount.
1515 */
1516static bool use_task_css_set_links __read_mostly;
1517
1518static void cgroup_enable_task_cg_lists(void)
1519{
1520 struct task_struct *p, *g;
1521
Tejun Heo96d365e2014-02-13 06:58:40 -05001522 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001523
1524 if (use_task_css_set_links)
1525 goto out_unlock;
1526
1527 use_task_css_set_links = true;
1528
1529 /*
1530 * We need tasklist_lock because RCU is not safe against
1531 * while_each_thread(). Besides, a forking task that has passed
1532 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1533 * is not guaranteed to have its child immediately visible in the
1534 * tasklist if we walk through it with RCU.
1535 */
1536 read_lock(&tasklist_lock);
1537 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001538 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1539 task_css_set(p) != &init_css_set);
1540
1541 /*
1542 * We should check if the process is exiting, otherwise
1543 * it will race with cgroup_exit() in that the list
1544 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001545 * Do it while holding siglock so that we don't end up
1546 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001547 */
Tejun Heof153ad12014-02-25 09:56:49 -05001548 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001549 if (!(p->flags & PF_EXITING)) {
1550 struct css_set *cset = task_css_set(p);
1551
1552 list_add(&p->cg_list, &cset->tasks);
1553 get_css_set(cset);
1554 }
Tejun Heof153ad12014-02-25 09:56:49 -05001555 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001556 } while_each_thread(g, p);
1557 read_unlock(&tasklist_lock);
1558out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001559 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001560}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561
Paul Menagecc31edc2008-10-18 20:28:04 -07001562static void init_cgroup_housekeeping(struct cgroup *cgrp)
1563{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001564 struct cgroup_subsys *ss;
1565 int ssid;
1566
Tejun Heo2bd59d42014-02-11 11:52:49 -05001567 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001568 INIT_LIST_HEAD(&cgrp->sibling);
1569 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001570 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001571 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001572 INIT_LIST_HEAD(&cgrp->pidlists);
1573 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001574 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001575
1576 for_each_subsys(ss, ssid)
1577 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001578
1579 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001580}
Paul Menagec6d57f32009-09-23 15:56:19 -07001581
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001582static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001583 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001585 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001586
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001588 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001589 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001590 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001591 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
Paul Menagec6d57f32009-09-23 15:56:19 -07001593 root->flags = opts->flags;
1594 if (opts->release_agent)
1595 strcpy(root->release_agent_path, opts->release_agent);
1596 if (opts->name)
1597 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001598 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001599 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001600}
1601
Tejun Heo69dfa002014-05-04 15:09:13 -04001602static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001603{
Tejun Heod427dfe2014-02-11 11:52:48 -05001604 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001605 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001606 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001608
Tejun Heod427dfe2014-02-11 11:52:48 -05001609 lockdep_assert_held(&cgroup_tree_mutex);
1610 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001611
Tejun Heo6fa49182014-05-04 15:09:13 -04001612 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001613 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001614 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001615 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001616
Tejun Heod427dfe2014-02-11 11:52:48 -05001617 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001618 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001619 * but that's OK - it can only be increased by someone holding
1620 * cgroup_lock, and that's us. The worst that can happen is that we
1621 * have some link structures left over
1622 */
1623 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001625 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Tejun Heo985ed672014-03-19 10:23:53 -04001627 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001628 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001629 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Tejun Heo2bd59d42014-02-11 11:52:49 -05001631 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1632 KERNFS_ROOT_CREATE_DEACTIVATED,
1633 root_cgrp);
1634 if (IS_ERR(root->kf_root)) {
1635 ret = PTR_ERR(root->kf_root);
1636 goto exit_root_id;
1637 }
1638 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639
Tejun Heod427dfe2014-02-11 11:52:48 -05001640 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1641 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001642 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643
Tejun Heo5df36032014-03-19 10:23:54 -04001644 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001645 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001646 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001647
Tejun Heod427dfe2014-02-11 11:52:48 -05001648 /*
1649 * There must be no failure case after here, since rebinding takes
1650 * care of subsystems' refcounts, which are explicitly dropped in
1651 * the failure exit path.
1652 */
1653 list_add(&root->root_list, &cgroup_roots);
1654 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655
Tejun Heod427dfe2014-02-11 11:52:48 -05001656 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001657 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001658 * objects.
1659 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001660 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001661 hash_for_each(css_set_table, i, cset, hlist)
1662 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001663 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664
Tejun Heod427dfe2014-02-11 11:52:48 -05001665 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001666 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001667
Tejun Heo2bd59d42014-02-11 11:52:49 -05001668 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001669 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001670 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001671
Tejun Heo2bd59d42014-02-11 11:52:49 -05001672destroy_root:
1673 kernfs_destroy_root(root->kf_root);
1674 root->kf_root = NULL;
1675exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001676 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001677out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001678 free_cgrp_cset_links(&tmp_links);
1679 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680}
1681
Al Virof7e83572010-07-26 13:23:11 +04001682static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001684 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001686 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001688 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001689 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001690 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001691
1692 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001693 * The first time anyone tries to mount a cgroup, enable the list
1694 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001695 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001696 if (!use_task_css_set_links)
1697 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001698
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001699 mutex_lock(&cgroup_tree_mutex);
1700 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001705 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001706retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001707 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001708 if (!opts.subsys_mask && !opts.none && !opts.name) {
1709 cgrp_dfl_root_visible = true;
1710 root = &cgrp_dfl_root;
1711 cgroup_get(&root->cgrp);
1712 ret = 0;
1713 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714 }
1715
Tejun Heo985ed672014-03-19 10:23:53 -04001716 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001717 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001719 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001720 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001721
Paul Menage817929e2007-10-18 23:39:36 -07001722 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001723 * If we asked for a name then it must match. Also, if
1724 * name matches but sybsys_mask doesn't, we should fail.
1725 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001726 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001727 if (opts.name) {
1728 if (strcmp(opts.name, root->name))
1729 continue;
1730 name_match = true;
1731 }
Tejun Heo31261212013-06-28 17:07:30 -07001732
1733 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001734 * If we asked for subsystems (or explicitly for no
1735 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001736 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001737 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001738 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001739 if (!name_match)
1740 continue;
1741 ret = -EBUSY;
1742 goto out_unlock;
1743 }
Tejun Heo873fe092013-04-14 20:15:26 -07001744
Tejun Heoc7ba8282013-06-29 14:06:10 -07001745 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001746 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001747 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001748 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001749 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001750 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001751 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001752 }
Tejun Heo873fe092013-04-14 20:15:26 -07001753 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001754
Tejun Heo776f02f2014-02-12 09:29:50 -05001755 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001756 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001757 * ref indicate that the root is being destroyed. Wait for
1758 * destruction to complete so that the subsystems are free.
1759 * We can use wait_queue for the wait but this path is
1760 * super cold. Let's just sleep for a bit and retry.
1761 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001762 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001763 mutex_unlock(&cgroup_mutex);
1764 mutex_unlock(&cgroup_tree_mutex);
1765 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001766 mutex_lock(&cgroup_tree_mutex);
1767 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001768 goto retry;
1769 }
1770
1771 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001772 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 }
1774
Tejun Heo172a2c062014-03-19 10:23:53 -04001775 /*
1776 * No such thing, create a new one. name= matching without subsys
1777 * specification is allowed for already existing hierarchies but we
1778 * can't create new one without subsys specification.
1779 */
1780 if (!opts.subsys_mask && !opts.none) {
1781 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001782 goto out_unlock;
1783 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784
Tejun Heo172a2c062014-03-19 10:23:53 -04001785 root = kzalloc(sizeof(*root), GFP_KERNEL);
1786 if (!root) {
1787 ret = -ENOMEM;
1788 goto out_unlock;
1789 }
1790
1791 init_cgroup_root(root, &opts);
1792
Tejun Heo35585572014-02-13 06:58:38 -05001793 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001794 if (ret)
1795 cgroup_free_root(root);
1796
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001797out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001798 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001799 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001800
Paul Menagec6d57f32009-09-23 15:56:19 -07001801 kfree(opts.release_agent);
1802 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001803
Tejun Heo2bd59d42014-02-11 11:52:49 -05001804 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001805 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001806
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001807 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1808 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001809 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001810 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811}
1812
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001813static void cgroup_kill_sb(struct super_block *sb)
1814{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001815 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001816 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001818 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001819 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820}
1821
1822static struct file_system_type cgroup_fs_type = {
1823 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001824 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001825 .kill_sb = cgroup_kill_sb,
1826};
1827
Greg KH676db4a2010-08-05 13:53:35 -07001828static struct kobject *cgroup_kobj;
1829
Li Zefana043e3b2008-02-23 15:24:09 -08001830/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001831 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001832 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001833 * @buf: the buffer to write the path into
1834 * @buflen: the length of the buffer
1835 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001836 * Determine @task's cgroup on the first (the one with the lowest non-zero
1837 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1838 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1839 * cgroup controller callbacks.
1840 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001841 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001842 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001843char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001844{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001845 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001846 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001847 int hierarchy_id = 1;
1848 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001849
1850 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001851 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001852
Tejun Heo913ffdb2013-07-11 16:34:48 -07001853 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1854
Tejun Heo857a2be2013-04-14 20:50:08 -07001855 if (root) {
1856 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001857 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001858 } else {
1859 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001860 if (strlcpy(buf, "/", buflen) < buflen)
1861 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001862 }
1863
Tejun Heo96d365e2014-02-13 06:58:40 -05001864 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001865 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001866 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001867}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001868EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001869
Tejun Heob3dc0942014-02-25 10:04:01 -05001870/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001871struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001872 /* the src and dst cset list running through cset->mg_node */
1873 struct list_head src_csets;
1874 struct list_head dst_csets;
1875
1876 /*
1877 * Fields for cgroup_taskset_*() iteration.
1878 *
1879 * Before migration is committed, the target migration tasks are on
1880 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1881 * the csets on ->dst_csets. ->csets point to either ->src_csets
1882 * or ->dst_csets depending on whether migration is committed.
1883 *
1884 * ->cur_csets and ->cur_task point to the current task position
1885 * during iteration.
1886 */
1887 struct list_head *csets;
1888 struct css_set *cur_cset;
1889 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001890};
1891
1892/**
1893 * cgroup_taskset_first - reset taskset and return the first task
1894 * @tset: taskset of interest
1895 *
1896 * @tset iteration is initialized and the first task is returned.
1897 */
1898struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1899{
Tejun Heob3dc0942014-02-25 10:04:01 -05001900 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1901 tset->cur_task = NULL;
1902
1903 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001904}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001905
1906/**
1907 * cgroup_taskset_next - iterate to the next task in taskset
1908 * @tset: taskset of interest
1909 *
1910 * Return the next task in @tset. Iteration must have been initialized
1911 * with cgroup_taskset_first().
1912 */
1913struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1914{
Tejun Heob3dc0942014-02-25 10:04:01 -05001915 struct css_set *cset = tset->cur_cset;
1916 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001917
Tejun Heob3dc0942014-02-25 10:04:01 -05001918 while (&cset->mg_node != tset->csets) {
1919 if (!task)
1920 task = list_first_entry(&cset->mg_tasks,
1921 struct task_struct, cg_list);
1922 else
1923 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001924
Tejun Heob3dc0942014-02-25 10:04:01 -05001925 if (&task->cg_list != &cset->mg_tasks) {
1926 tset->cur_cset = cset;
1927 tset->cur_task = task;
1928 return task;
1929 }
1930
1931 cset = list_next_entry(cset, mg_node);
1932 task = NULL;
1933 }
1934
1935 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001936}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001937
1938/**
Ben Blum74a11662011-05-26 16:25:20 -07001939 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001940 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001941 * @tsk: the task being migrated
1942 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001943 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001944 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001945 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001946static void cgroup_task_migrate(struct cgroup *old_cgrp,
1947 struct task_struct *tsk,
1948 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001949{
Tejun Heo5abb8852013-06-12 21:04:49 -07001950 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001951
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001952 lockdep_assert_held(&cgroup_mutex);
1953 lockdep_assert_held(&css_set_rwsem);
1954
Ben Blum74a11662011-05-26 16:25:20 -07001955 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001956 * We are synchronized through threadgroup_lock() against PF_EXITING
1957 * setting such that we can't race against cgroup_exit() changing the
1958 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001959 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001960 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001961 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001962
Tejun Heob3dc0942014-02-25 10:04:01 -05001963 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001964 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001965
Tejun Heo1b9aba42014-03-19 17:43:21 -04001966 /*
1967 * Use move_tail so that cgroup_taskset_first() still returns the
1968 * leader after migration. This works because cgroup_migrate()
1969 * ensures that the dst_cset of the leader is the first on the
1970 * tset's dst_csets list.
1971 */
1972 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001973
1974 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001975 * We just gained a reference on old_cset by taking it from the
1976 * task. As trading it for new_cset is protected by cgroup_mutex,
1977 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001978 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001979 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001980 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001981}
1982
Li Zefana043e3b2008-02-23 15:24:09 -08001983/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001984 * cgroup_migrate_finish - cleanup after attach
1985 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001986 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001987 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1988 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001989 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001990static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001991{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001992 struct css_set *cset, *tmp_cset;
1993
1994 lockdep_assert_held(&cgroup_mutex);
1995
1996 down_write(&css_set_rwsem);
1997 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1998 cset->mg_src_cgrp = NULL;
1999 cset->mg_dst_cset = NULL;
2000 list_del_init(&cset->mg_preload_node);
2001 put_css_set_locked(cset, false);
2002 }
2003 up_write(&css_set_rwsem);
2004}
2005
2006/**
2007 * cgroup_migrate_add_src - add a migration source css_set
2008 * @src_cset: the source css_set to add
2009 * @dst_cgrp: the destination cgroup
2010 * @preloaded_csets: list of preloaded css_sets
2011 *
2012 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2013 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2014 * up by cgroup_migrate_finish().
2015 *
2016 * This function may be called without holding threadgroup_lock even if the
2017 * target is a process. Threads may be created and destroyed but as long
2018 * as cgroup_mutex is not dropped, no new css_set can be put into play and
2019 * the preloaded css_sets are guaranteed to cover all migrations.
2020 */
2021static void cgroup_migrate_add_src(struct css_set *src_cset,
2022 struct cgroup *dst_cgrp,
2023 struct list_head *preloaded_csets)
2024{
2025 struct cgroup *src_cgrp;
2026
2027 lockdep_assert_held(&cgroup_mutex);
2028 lockdep_assert_held(&css_set_rwsem);
2029
2030 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2031
Tejun Heo1958d2d2014-02-25 10:04:03 -05002032 if (!list_empty(&src_cset->mg_preload_node))
2033 return;
2034
2035 WARN_ON(src_cset->mg_src_cgrp);
2036 WARN_ON(!list_empty(&src_cset->mg_tasks));
2037 WARN_ON(!list_empty(&src_cset->mg_node));
2038
2039 src_cset->mg_src_cgrp = src_cgrp;
2040 get_css_set(src_cset);
2041 list_add(&src_cset->mg_preload_node, preloaded_csets);
2042}
2043
2044/**
2045 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002046 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002047 * @preloaded_csets: list of preloaded source css_sets
2048 *
2049 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2050 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002051 * pins all destination css_sets, links each to its source, and append them
2052 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2053 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002054 *
2055 * This function must be called after cgroup_migrate_add_src() has been
2056 * called on each migration source css_set. After migration is performed
2057 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2058 * @preloaded_csets.
2059 */
2060static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2061 struct list_head *preloaded_csets)
2062{
2063 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002064 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002065
2066 lockdep_assert_held(&cgroup_mutex);
2067
Tejun Heof8f22e52014-04-23 11:13:16 -04002068 /*
2069 * Except for the root, child_subsys_mask must be zero for a cgroup
2070 * with tasks so that child cgroups don't compete against tasks.
2071 */
2072 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2073 dst_cgrp->child_subsys_mask)
2074 return -EBUSY;
2075
Tejun Heo1958d2d2014-02-25 10:04:03 -05002076 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002077 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002078 struct css_set *dst_cset;
2079
Tejun Heof817de92014-04-23 11:13:16 -04002080 dst_cset = find_css_set(src_cset,
2081 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002082 if (!dst_cset)
2083 goto err;
2084
2085 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002086
2087 /*
2088 * If src cset equals dst, it's noop. Drop the src.
2089 * cgroup_migrate() will skip the cset too. Note that we
2090 * can't handle src == dst as some nodes are used by both.
2091 */
2092 if (src_cset == dst_cset) {
2093 src_cset->mg_src_cgrp = NULL;
2094 list_del_init(&src_cset->mg_preload_node);
2095 put_css_set(src_cset, false);
2096 put_css_set(dst_cset, false);
2097 continue;
2098 }
2099
Tejun Heo1958d2d2014-02-25 10:04:03 -05002100 src_cset->mg_dst_cset = dst_cset;
2101
2102 if (list_empty(&dst_cset->mg_preload_node))
2103 list_add(&dst_cset->mg_preload_node, &csets);
2104 else
2105 put_css_set(dst_cset, false);
2106 }
2107
Tejun Heof817de92014-04-23 11:13:16 -04002108 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002109 return 0;
2110err:
2111 cgroup_migrate_finish(&csets);
2112 return -ENOMEM;
2113}
2114
2115/**
2116 * cgroup_migrate - migrate a process or task to a cgroup
2117 * @cgrp: the destination cgroup
2118 * @leader: the leader of the process or the task to migrate
2119 * @threadgroup: whether @leader points to the whole process or a single task
2120 *
2121 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2122 * process, the caller must be holding threadgroup_lock of @leader. The
2123 * caller is also responsible for invoking cgroup_migrate_add_src() and
2124 * cgroup_migrate_prepare_dst() on the targets before invoking this
2125 * function and following up with cgroup_migrate_finish().
2126 *
2127 * As long as a controller's ->can_attach() doesn't fail, this function is
2128 * guaranteed to succeed. This means that, excluding ->can_attach()
2129 * failure, when migrating multiple targets, the success or failure can be
2130 * decided for all targets by invoking group_migrate_prepare_dst() before
2131 * actually starting migrating.
2132 */
2133static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2134 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002135{
Tejun Heob3dc0942014-02-25 10:04:01 -05002136 struct cgroup_taskset tset = {
2137 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2138 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2139 .csets = &tset.src_csets,
2140 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002141 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002142 struct css_set *cset, *tmp_cset;
2143 struct task_struct *task, *tmp_task;
2144 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002145
2146 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002147 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2148 * already PF_EXITING could be freed from underneath us unless we
2149 * take an rcu_read_lock.
2150 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002151 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002152 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002153 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002154 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002155 /* @task either already exited or can't exit until the end */
2156 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002157 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002158
Tejun Heoeaf797a2014-02-25 10:04:03 -05002159 /* leave @task alone if post_fork() hasn't linked it yet */
2160 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002161 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002162
Tejun Heob3dc0942014-02-25 10:04:01 -05002163 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002164 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002165 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002166
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002167 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002168 * cgroup_taskset_first() must always return the leader.
2169 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002170 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002171 list_move_tail(&task->cg_list, &cset->mg_tasks);
2172 if (list_empty(&cset->mg_node))
2173 list_add_tail(&cset->mg_node, &tset.src_csets);
2174 if (list_empty(&cset->mg_dst_cset->mg_node))
2175 list_move_tail(&cset->mg_dst_cset->mg_node,
2176 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002177 next:
Li Zefan081aa452013-03-13 09:17:09 +08002178 if (!threadgroup)
2179 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002180 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002181 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002182 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002183
Tejun Heo134d3372011-12-12 18:12:21 -08002184 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002185 if (list_empty(&tset.src_csets))
2186 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002187
Tejun Heo1958d2d2014-02-25 10:04:03 -05002188 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002189 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002190 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002191 ret = css->ss->can_attach(css, &tset);
2192 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002193 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002194 goto out_cancel_attach;
2195 }
2196 }
Ben Blum74a11662011-05-26 16:25:20 -07002197 }
2198
2199 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002200 * Now that we're guaranteed success, proceed to move all tasks to
2201 * the new cgroup. There are no failure cases after here, so this
2202 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002203 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002204 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002205 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2206 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2207 cgroup_task_migrate(cset->mg_src_cgrp, task,
2208 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002209 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002210 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002211
2212 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002213 * Migration is committed, all target tasks are now on dst_csets.
2214 * Nothing is sensitive to fork() after this point. Notify
2215 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002216 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002217 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002218
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002219 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002220 if (css->ss->attach)
2221 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002222
Tejun Heo9db8de32014-02-13 06:58:43 -05002223 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002224 goto out_release_tset;
2225
Ben Blum74a11662011-05-26 16:25:20 -07002226out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002227 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002228 if (css == failed_css)
2229 break;
2230 if (css->ss->cancel_attach)
2231 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002232 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002233out_release_tset:
2234 down_write(&css_set_rwsem);
2235 list_splice_init(&tset.dst_csets, &tset.src_csets);
2236 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002237 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002238 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002239 }
2240 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002241 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002242}
2243
Tejun Heo1958d2d2014-02-25 10:04:03 -05002244/**
2245 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2246 * @dst_cgrp: the cgroup to attach to
2247 * @leader: the task or the leader of the threadgroup to be attached
2248 * @threadgroup: attach the whole threadgroup?
2249 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002250 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002251 */
2252static int cgroup_attach_task(struct cgroup *dst_cgrp,
2253 struct task_struct *leader, bool threadgroup)
2254{
2255 LIST_HEAD(preloaded_csets);
2256 struct task_struct *task;
2257 int ret;
2258
2259 /* look up all src csets */
2260 down_read(&css_set_rwsem);
2261 rcu_read_lock();
2262 task = leader;
2263 do {
2264 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2265 &preloaded_csets);
2266 if (!threadgroup)
2267 break;
2268 } while_each_thread(leader, task);
2269 rcu_read_unlock();
2270 up_read(&css_set_rwsem);
2271
2272 /* prepare dst csets and commit */
2273 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2274 if (!ret)
2275 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2276
2277 cgroup_migrate_finish(&preloaded_csets);
2278 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002279}
2280
2281/*
2282 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002283 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002284 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002285 */
Tejun Heoacbef752014-05-13 12:16:22 -04002286static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2287 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002288{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002289 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002290 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002291 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002292 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002293 int ret;
2294
Tejun Heoacbef752014-05-13 12:16:22 -04002295 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2296 return -EINVAL;
2297
Tejun Heoe76ecae2014-05-13 12:19:23 -04002298 cgrp = cgroup_kn_lock_live(of->kn);
2299 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002300 return -ENODEV;
2301
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002302retry_find_task:
2303 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002304 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002305 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002306 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002307 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002308 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002309 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002310 }
Ben Blum74a11662011-05-26 16:25:20 -07002311 /*
2312 * even if we're attaching all tasks in the thread group, we
2313 * only need to check permissions on one of them.
2314 */
David Howellsc69e8d92008-11-14 10:39:19 +11002315 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002316 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2317 !uid_eq(cred->euid, tcred->uid) &&
2318 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002319 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002320 ret = -EACCES;
2321 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002322 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002323 } else
2324 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002325
2326 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002327 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002328
2329 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002330 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002331 * trapped in a cpuset, or RT worker may be born in a cgroup
2332 * with no rt_runtime allocated. Just say no.
2333 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002334 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002335 ret = -EINVAL;
2336 rcu_read_unlock();
2337 goto out_unlock_cgroup;
2338 }
2339
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002340 get_task_struct(tsk);
2341 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002342
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002343 threadgroup_lock(tsk);
2344 if (threadgroup) {
2345 if (!thread_group_leader(tsk)) {
2346 /*
2347 * a race with de_thread from another thread's exec()
2348 * may strip us of our leadership, if this happens,
2349 * there is no choice but to throw this task away and
2350 * try again; this is
2351 * "double-double-toil-and-trouble-check locking".
2352 */
2353 threadgroup_unlock(tsk);
2354 put_task_struct(tsk);
2355 goto retry_find_task;
2356 }
Li Zefan081aa452013-03-13 09:17:09 +08002357 }
2358
2359 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2360
Tejun Heocd3d0952011-12-12 18:12:21 -08002361 threadgroup_unlock(tsk);
2362
Paul Menagebbcb81d2007-10-18 23:39:32 -07002363 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002364out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002365 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002366 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002367}
2368
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002369/**
2370 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2371 * @from: attach to all cgroups of a given task
2372 * @tsk: the task to be attached
2373 */
2374int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2375{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002376 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002377 int retval = 0;
2378
Tejun Heo47cfcd02013-04-07 09:29:51 -07002379 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002380 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002381 struct cgroup *from_cgrp;
2382
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002383 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002384 continue;
2385
Tejun Heo96d365e2014-02-13 06:58:40 -05002386 down_read(&css_set_rwsem);
2387 from_cgrp = task_cgroup_from_root(from, root);
2388 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002389
Li Zefan6f4b7e62013-07-31 16:18:36 +08002390 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002391 if (retval)
2392 break;
2393 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002394 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002395
2396 return retval;
2397}
2398EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2399
Tejun Heoacbef752014-05-13 12:16:22 -04002400static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2401 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002402{
Tejun Heoacbef752014-05-13 12:16:22 -04002403 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002404}
2405
Tejun Heoacbef752014-05-13 12:16:22 -04002406static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2407 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002408{
Tejun Heoacbef752014-05-13 12:16:22 -04002409 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002410}
2411
Tejun Heo451af502014-05-13 12:16:21 -04002412static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2413 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002414{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002415 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002416
Tejun Heoe76ecae2014-05-13 12:19:23 -04002417 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2418
2419 cgrp = cgroup_kn_lock_live(of->kn);
2420 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002421 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002422 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002423 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2424 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002425 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002426 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002427 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002428}
2429
Tejun Heo2da8ca82013-12-05 12:28:04 -05002430static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002431{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002432 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002433
Tejun Heo46cfeb02014-05-13 12:11:00 -04002434 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002435 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002436 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002437 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002438 return 0;
2439}
2440
Tejun Heo2da8ca82013-12-05 12:28:04 -05002441static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002442{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002443 struct cgroup *cgrp = seq_css(seq)->cgroup;
2444
2445 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002446 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002447}
2448
Tejun Heof8f22e52014-04-23 11:13:16 -04002449static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2450{
2451 struct cgroup_subsys *ss;
2452 bool printed = false;
2453 int ssid;
2454
2455 for_each_subsys(ss, ssid) {
2456 if (ss_mask & (1 << ssid)) {
2457 if (printed)
2458 seq_putc(seq, ' ');
2459 seq_printf(seq, "%s", ss->name);
2460 printed = true;
2461 }
2462 }
2463 if (printed)
2464 seq_putc(seq, '\n');
2465}
2466
2467/* show controllers which are currently attached to the default hierarchy */
2468static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2469{
2470 struct cgroup *cgrp = seq_css(seq)->cgroup;
2471
2472 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2473 return 0;
2474}
2475
2476/* show controllers which are enabled from the parent */
2477static int cgroup_controllers_show(struct seq_file *seq, void *v)
2478{
2479 struct cgroup *cgrp = seq_css(seq)->cgroup;
2480
2481 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2482 return 0;
2483}
2484
2485/* show controllers which are enabled for a given cgroup's children */
2486static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2487{
2488 struct cgroup *cgrp = seq_css(seq)->cgroup;
2489
2490 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2491 return 0;
2492}
2493
2494/**
2495 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2496 * @cgrp: root of the subtree to update csses for
2497 *
2498 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2499 * css associations need to be updated accordingly. This function looks up
2500 * all css_sets which are attached to the subtree, creates the matching
2501 * updated css_sets and migrates the tasks to the new ones.
2502 */
2503static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2504{
2505 LIST_HEAD(preloaded_csets);
2506 struct cgroup_subsys_state *css;
2507 struct css_set *src_cset;
2508 int ret;
2509
2510 lockdep_assert_held(&cgroup_tree_mutex);
2511 lockdep_assert_held(&cgroup_mutex);
2512
2513 /* look up all csses currently attached to @cgrp's subtree */
2514 down_read(&css_set_rwsem);
2515 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2516 struct cgrp_cset_link *link;
2517
2518 /* self is not affected by child_subsys_mask change */
2519 if (css->cgroup == cgrp)
2520 continue;
2521
2522 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2523 cgroup_migrate_add_src(link->cset, cgrp,
2524 &preloaded_csets);
2525 }
2526 up_read(&css_set_rwsem);
2527
2528 /* NULL dst indicates self on default hierarchy */
2529 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2530 if (ret)
2531 goto out_finish;
2532
2533 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2534 struct task_struct *last_task = NULL, *task;
2535
2536 /* src_csets precede dst_csets, break on the first dst_cset */
2537 if (!src_cset->mg_src_cgrp)
2538 break;
2539
2540 /*
2541 * All tasks in src_cset need to be migrated to the
2542 * matching dst_cset. Empty it process by process. We
2543 * walk tasks but migrate processes. The leader might even
2544 * belong to a different cset but such src_cset would also
2545 * be among the target src_csets because the default
2546 * hierarchy enforces per-process membership.
2547 */
2548 while (true) {
2549 down_read(&css_set_rwsem);
2550 task = list_first_entry_or_null(&src_cset->tasks,
2551 struct task_struct, cg_list);
2552 if (task) {
2553 task = task->group_leader;
2554 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2555 get_task_struct(task);
2556 }
2557 up_read(&css_set_rwsem);
2558
2559 if (!task)
2560 break;
2561
2562 /* guard against possible infinite loop */
2563 if (WARN(last_task == task,
2564 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2565 goto out_finish;
2566 last_task = task;
2567
2568 threadgroup_lock(task);
2569 /* raced against de_thread() from another thread? */
2570 if (!thread_group_leader(task)) {
2571 threadgroup_unlock(task);
2572 put_task_struct(task);
2573 continue;
2574 }
2575
2576 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2577
2578 threadgroup_unlock(task);
2579 put_task_struct(task);
2580
2581 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2582 goto out_finish;
2583 }
2584 }
2585
2586out_finish:
2587 cgroup_migrate_finish(&preloaded_csets);
2588 return ret;
2589}
2590
2591/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002592static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2593 char *buf, size_t nbytes,
2594 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002595{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002596 unsigned int enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04002597 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002598 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002599 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002600 int ssid, ret;
2601
2602 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002603 * Parse input - space separated list of subsystem names prefixed
2604 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002605 */
Tejun Heo451af502014-05-13 12:16:21 -04002606 buf = strstrip(buf);
2607 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002608 if (tok[0] == '\0')
2609 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002610 for_each_subsys(ss, ssid) {
2611 if (ss->disabled || strcmp(tok + 1, ss->name))
2612 continue;
2613
2614 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002615 enable |= 1 << ssid;
2616 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002617 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002618 disable |= 1 << ssid;
2619 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002620 } else {
2621 return -EINVAL;
2622 }
2623 break;
2624 }
2625 if (ssid == CGROUP_SUBSYS_COUNT)
2626 return -EINVAL;
2627 }
2628
Tejun Heoa9746d82014-05-13 12:19:22 -04002629 cgrp = cgroup_kn_lock_live(of->kn);
2630 if (!cgrp)
2631 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002632
2633 for_each_subsys(ss, ssid) {
2634 if (enable & (1 << ssid)) {
2635 if (cgrp->child_subsys_mask & (1 << ssid)) {
2636 enable &= ~(1 << ssid);
2637 continue;
2638 }
2639
2640 /*
2641 * Because css offlining is asynchronous, userland
2642 * might try to re-enable the same controller while
2643 * the previous instance is still around. In such
2644 * cases, wait till it's gone using offline_waitq.
2645 */
2646 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002647 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002648
2649 if (!cgroup_css(child, ss))
2650 continue;
2651
Tejun Heo0cee8b72014-05-13 12:10:59 -04002652 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002653 prepare_to_wait(&child->offline_waitq, &wait,
2654 TASK_UNINTERRUPTIBLE);
Tejun Heoa9746d82014-05-13 12:19:22 -04002655 cgroup_kn_unlock(of->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002656 schedule();
2657 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002658 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002659
Tejun Heoa9746d82014-05-13 12:19:22 -04002660 return restart_syscall();
Tejun Heof8f22e52014-04-23 11:13:16 -04002661 }
2662
2663 /* unavailable or not enabled on the parent? */
2664 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2665 (cgrp->parent &&
2666 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2667 ret = -ENOENT;
Tejun Heoddab2b62014-05-13 12:19:22 -04002668 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002669 }
2670 } else if (disable & (1 << ssid)) {
2671 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2672 disable &= ~(1 << ssid);
2673 continue;
2674 }
2675
2676 /* a child has it enabled? */
2677 cgroup_for_each_live_child(child, cgrp) {
2678 if (child->child_subsys_mask & (1 << ssid)) {
2679 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002680 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002681 }
2682 }
2683 }
2684 }
2685
2686 if (!enable && !disable) {
2687 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002688 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002689 }
2690
2691 /*
2692 * Except for the root, child_subsys_mask must be zero for a cgroup
2693 * with tasks so that child cgroups don't compete against tasks.
2694 */
2695 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2696 ret = -EBUSY;
2697 goto out_unlock;
2698 }
2699
2700 /*
2701 * Create csses for enables and update child_subsys_mask. This
2702 * changes cgroup_e_css() results which in turn makes the
2703 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2704 * subtree to the updated csses.
2705 */
2706 for_each_subsys(ss, ssid) {
2707 if (!(enable & (1 << ssid)))
2708 continue;
2709
2710 cgroup_for_each_live_child(child, cgrp) {
2711 ret = create_css(child, ss);
2712 if (ret)
2713 goto err_undo_css;
2714 }
2715 }
2716
2717 cgrp->child_subsys_mask |= enable;
2718 cgrp->child_subsys_mask &= ~disable;
2719
2720 ret = cgroup_update_dfl_csses(cgrp);
2721 if (ret)
2722 goto err_undo_css;
2723
2724 /* all tasks are now migrated away from the old csses, kill them */
2725 for_each_subsys(ss, ssid) {
2726 if (!(disable & (1 << ssid)))
2727 continue;
2728
2729 cgroup_for_each_live_child(child, cgrp)
2730 kill_css(cgroup_css(child, ss));
2731 }
2732
2733 kernfs_activate(cgrp->kn);
2734 ret = 0;
2735out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002736 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002737 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002738
2739err_undo_css:
2740 cgrp->child_subsys_mask &= ~enable;
2741 cgrp->child_subsys_mask |= disable;
2742
2743 for_each_subsys(ss, ssid) {
2744 if (!(enable & (1 << ssid)))
2745 continue;
2746
2747 cgroup_for_each_live_child(child, cgrp) {
2748 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2749 if (css)
2750 kill_css(css);
2751 }
2752 }
2753 goto out_unlock;
2754}
2755
Tejun Heo842b5972014-04-25 18:28:02 -04002756static int cgroup_populated_show(struct seq_file *seq, void *v)
2757{
2758 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2759 return 0;
2760}
2761
Tejun Heo2bd59d42014-02-11 11:52:49 -05002762static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2763 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002765 struct cgroup *cgrp = of->kn->parent->priv;
2766 struct cftype *cft = of->kn->priv;
2767 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002768 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769
Tejun Heob4168642014-05-13 12:16:21 -04002770 if (cft->write)
2771 return cft->write(of, buf, nbytes, off);
2772
Tejun Heo2bd59d42014-02-11 11:52:49 -05002773 /*
2774 * kernfs guarantees that a file isn't deleted with operations in
2775 * flight, which means that the matching css is and stays alive and
2776 * doesn't need to be pinned. The RCU locking is not necessary
2777 * either. It's just for the convenience of using cgroup_css().
2778 */
2779 rcu_read_lock();
2780 css = cgroup_css(cgrp, cft->ss);
2781 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002782
Tejun Heo451af502014-05-13 12:16:21 -04002783 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002784 unsigned long long v;
2785 ret = kstrtoull(buf, 0, &v);
2786 if (!ret)
2787 ret = cft->write_u64(css, cft, v);
2788 } else if (cft->write_s64) {
2789 long long v;
2790 ret = kstrtoll(buf, 0, &v);
2791 if (!ret)
2792 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002793 } else {
2794 ret = -EINVAL;
2795 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002796
Tejun Heoa742c592013-12-05 12:28:03 -05002797 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002798}
2799
Tejun Heo6612f052013-12-05 12:28:04 -05002800static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002801{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002802 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002803}
2804
2805static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2806{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002807 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002808}
2809
2810static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2811{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002812 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002813}
2814
2815static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2816{
Tejun Heo7da11272013-12-05 12:28:04 -05002817 struct cftype *cft = seq_cft(m);
2818 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002819
Tejun Heo2da8ca82013-12-05 12:28:04 -05002820 if (cft->seq_show)
2821 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002822
Tejun Heo896f5192013-12-05 12:28:04 -05002823 if (cft->read_u64)
2824 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2825 else if (cft->read_s64)
2826 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2827 else
2828 return -EINVAL;
2829 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002830}
2831
Tejun Heo2bd59d42014-02-11 11:52:49 -05002832static struct kernfs_ops cgroup_kf_single_ops = {
2833 .atomic_write_len = PAGE_SIZE,
2834 .write = cgroup_file_write,
2835 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002836};
2837
Tejun Heo2bd59d42014-02-11 11:52:49 -05002838static struct kernfs_ops cgroup_kf_ops = {
2839 .atomic_write_len = PAGE_SIZE,
2840 .write = cgroup_file_write,
2841 .seq_start = cgroup_seqfile_start,
2842 .seq_next = cgroup_seqfile_next,
2843 .seq_stop = cgroup_seqfile_stop,
2844 .seq_show = cgroup_seqfile_show,
2845};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002846
2847/*
2848 * cgroup_rename - Only allow simple rename of directories in place.
2849 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002850static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2851 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002852{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002853 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002854 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002855
Tejun Heo2bd59d42014-02-11 11:52:49 -05002856 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002857 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002858 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002859 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002860
Tejun Heo6db8e852013-06-14 11:18:22 -07002861 /*
2862 * This isn't a proper migration and its usefulness is very
2863 * limited. Disallow if sane_behavior.
2864 */
2865 if (cgroup_sane_behavior(cgrp))
2866 return -EPERM;
2867
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002868 /*
2869 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2870 * active_ref. kernfs_rename() doesn't require active_ref
2871 * protection. Break them before grabbing cgroup_tree_mutex.
2872 */
2873 kernfs_break_active_protection(new_parent);
2874 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002875
Tejun Heo2bd59d42014-02-11 11:52:49 -05002876 mutex_lock(&cgroup_tree_mutex);
2877 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002878
Tejun Heo2bd59d42014-02-11 11:52:49 -05002879 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002880
Tejun Heo2bd59d42014-02-11 11:52:49 -05002881 mutex_unlock(&cgroup_mutex);
2882 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002883
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002884 kernfs_unbreak_active_protection(kn);
2885 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002886 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002887}
2888
Tejun Heo49957f82014-04-07 16:44:47 -04002889/* set uid and gid of cgroup dirs and files to that of the creator */
2890static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2891{
2892 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2893 .ia_uid = current_fsuid(),
2894 .ia_gid = current_fsgid(), };
2895
2896 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2897 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2898 return 0;
2899
2900 return kernfs_setattr(kn, &iattr);
2901}
2902
Tejun Heo2bb566c2013-08-08 20:11:23 -04002903static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002904{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002905 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002906 struct kernfs_node *kn;
2907 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002908 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002909
Tejun Heo2bd59d42014-02-11 11:52:49 -05002910#ifdef CONFIG_DEBUG_LOCK_ALLOC
2911 key = &cft->lockdep_key;
2912#endif
2913 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2914 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2915 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002916 if (IS_ERR(kn))
2917 return PTR_ERR(kn);
2918
2919 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002920 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002921 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002922 return ret;
2923 }
2924
Tejun Heob7fc5ad2014-05-13 12:16:22 -04002925 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04002926 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002927 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002928}
2929
Tejun Heob1f28d32013-06-28 16:24:10 -07002930/**
2931 * cgroup_addrm_files - add or remove files to a cgroup directory
2932 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002933 * @cfts: array of cftypes to be added
2934 * @is_add: whether to add or remove
2935 *
2936 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002937 * For removals, this function never fails. If addition fails, this
2938 * function doesn't remove files already added. The caller is responsible
2939 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002940 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002941static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2942 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002943{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002944 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002945 int ret;
2946
Tejun Heoace2bee2014-02-11 11:52:47 -05002947 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04002948 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002949
2950 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002951 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002952 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2953 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002954 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2955 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002956 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2957 continue;
2958 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2959 continue;
2960
Li Zefan2739d3c2013-01-21 18:18:33 +08002961 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002962 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002963 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002964 pr_warn("%s: failed to add %s, err=%d\n",
2965 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002966 return ret;
2967 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002968 } else {
2969 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002970 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002971 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002972 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002973}
2974
Tejun Heo21a2d342014-02-12 09:29:49 -05002975static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002976{
2977 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002978 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002979 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002980 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002981 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002982
Tejun Heo21a2d342014-02-12 09:29:49 -05002983 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04002984 lockdep_assert_held(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002985
Li Zefane8c82d22013-06-18 18:48:37 +08002986 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002987 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002988 struct cgroup *cgrp = css->cgroup;
2989
Li Zefane8c82d22013-06-18 18:48:37 +08002990 if (cgroup_is_dead(cgrp))
2991 continue;
2992
Tejun Heo21a2d342014-02-12 09:29:49 -05002993 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002994 if (ret)
2995 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002996 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002997
2998 if (is_add && !ret)
2999 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003000 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003001}
3002
Tejun Heo2da440a2014-02-11 11:52:48 -05003003static void cgroup_exit_cftypes(struct cftype *cfts)
3004{
3005 struct cftype *cft;
3006
Tejun Heo2bd59d42014-02-11 11:52:49 -05003007 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3008 /* free copy for custom atomic_write_len, see init_cftypes() */
3009 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3010 kfree(cft->kf_ops);
3011 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003012 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003013 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003014}
3015
Tejun Heo2bd59d42014-02-11 11:52:49 -05003016static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003017{
3018 struct cftype *cft;
3019
Tejun Heo2bd59d42014-02-11 11:52:49 -05003020 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3021 struct kernfs_ops *kf_ops;
3022
Tejun Heo0adb0702014-02-12 09:29:48 -05003023 WARN_ON(cft->ss || cft->kf_ops);
3024
Tejun Heo2bd59d42014-02-11 11:52:49 -05003025 if (cft->seq_start)
3026 kf_ops = &cgroup_kf_ops;
3027 else
3028 kf_ops = &cgroup_kf_single_ops;
3029
3030 /*
3031 * Ugh... if @cft wants a custom max_write_len, we need to
3032 * make a copy of kf_ops to set its atomic_write_len.
3033 */
3034 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3035 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3036 if (!kf_ops) {
3037 cgroup_exit_cftypes(cfts);
3038 return -ENOMEM;
3039 }
3040 kf_ops->atomic_write_len = cft->max_write_len;
3041 }
3042
3043 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003044 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003045 }
3046
3047 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003048}
3049
Tejun Heo21a2d342014-02-12 09:29:49 -05003050static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3051{
3052 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04003053 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003054
3055 if (!cfts || !cfts[0].ss)
3056 return -ENOENT;
3057
3058 list_del(&cfts->node);
3059 cgroup_apply_cftypes(cfts, false);
3060 cgroup_exit_cftypes(cfts);
3061 return 0;
3062}
3063
Tejun Heo8e3f6542012-04-01 12:09:55 -07003064/**
Tejun Heo80b13582014-02-12 09:29:48 -05003065 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3066 * @cfts: zero-length name terminated array of cftypes
3067 *
3068 * Unregister @cfts. Files described by @cfts are removed from all
3069 * existing cgroups and all future cgroups won't have them either. This
3070 * function can be called anytime whether @cfts' subsys is attached or not.
3071 *
3072 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3073 * registered.
3074 */
3075int cgroup_rm_cftypes(struct cftype *cfts)
3076{
Tejun Heo21a2d342014-02-12 09:29:49 -05003077 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003078
Tejun Heo21a2d342014-02-12 09:29:49 -05003079 mutex_lock(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04003080 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003081 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003082 mutex_unlock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003083 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003084 return ret;
3085}
3086
3087/**
3088 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3089 * @ss: target cgroup subsystem
3090 * @cfts: zero-length name terminated array of cftypes
3091 *
3092 * Register @cfts to @ss. Files described by @cfts are created for all
3093 * existing cgroups to which @ss is attached and all future cgroups will
3094 * have them too. This function can be called anytime whether @ss is
3095 * attached or not.
3096 *
3097 * Returns 0 on successful registration, -errno on failure. Note that this
3098 * function currently returns 0 as long as @cfts registration is successful
3099 * even if some file creation attempts on existing cgroups fail.
3100 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003101int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003102{
Tejun Heo9ccece82013-06-28 16:24:11 -07003103 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003104
Li Zefandc5736e2014-02-17 10:41:50 +08003105 if (!cfts || cfts[0].name[0] == '\0')
3106 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003107
Tejun Heo2bd59d42014-02-11 11:52:49 -05003108 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003109 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003110 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003111
Tejun Heo21a2d342014-02-12 09:29:49 -05003112 mutex_lock(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04003113 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003114
Tejun Heo0adb0702014-02-12 09:29:48 -05003115 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003116 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003117 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003118 cgroup_rm_cftypes_locked(cfts);
3119
Tejun Heo01f64742014-05-13 12:19:23 -04003120 mutex_unlock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003121 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003122 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003123}
Tejun Heo79578622012-04-01 12:09:56 -07003124
3125/**
Li Zefana043e3b2008-02-23 15:24:09 -08003126 * cgroup_task_count - count the number of tasks in a cgroup.
3127 * @cgrp: the cgroup in question
3128 *
3129 * Return the number of tasks in the cgroup.
3130 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003131static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003132{
3133 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003134 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003135
Tejun Heo96d365e2014-02-13 06:58:40 -05003136 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003137 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3138 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003139 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003140 return count;
3141}
3142
Tejun Heo574bd9f2012-11-09 09:12:29 -08003143/**
Tejun Heo492eb212013-08-08 20:11:25 -04003144 * css_next_child - find the next child of a given css
3145 * @pos_css: the current position (%NULL to initiate traversal)
3146 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003147 *
Tejun Heo492eb212013-08-08 20:11:25 -04003148 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003149 * under either cgroup_mutex or RCU read lock. The only requirement is
3150 * that @parent_css and @pos_css are accessible. The next sibling is
3151 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003152 */
Tejun Heo492eb212013-08-08 20:11:25 -04003153struct cgroup_subsys_state *
3154css_next_child(struct cgroup_subsys_state *pos_css,
3155 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003156{
Tejun Heo492eb212013-08-08 20:11:25 -04003157 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3158 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003159 struct cgroup *next;
3160
Tejun Heoace2bee2014-02-11 11:52:47 -05003161 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003162
3163 /*
3164 * @pos could already have been removed. Once a cgroup is removed,
3165 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003166 * changes. As CGRP_DEAD assertion is serialized and happens
3167 * before the cgroup is taken off the ->sibling list, if we see it
3168 * unasserted, it's guaranteed that the next sibling hasn't
3169 * finished its grace period even if it's already removed, and thus
3170 * safe to dereference from this RCU critical section. If
3171 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3172 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003173 *
3174 * If @pos is dead, its next pointer can't be dereferenced;
3175 * however, as each cgroup is given a monotonically increasing
3176 * unique serial number and always appended to the sibling list,
3177 * the next one can be found by walking the parent's children until
3178 * we see a cgroup with higher serial number than @pos's. While
3179 * this path can be slower, it's taken only when either the current
3180 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003181 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003182 if (!pos) {
3183 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3184 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003185 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003186 } else {
3187 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3188 if (next->serial_nr > pos->serial_nr)
3189 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003190 }
3191
Tejun Heo3b281af2014-04-23 11:13:15 -04003192 /*
3193 * @next, if not pointing to the head, can be dereferenced and is
3194 * the next sibling; however, it might have @ss disabled. If so,
3195 * fast-forward to the next enabled one.
3196 */
3197 while (&next->sibling != &cgrp->children) {
3198 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003199
Tejun Heo3b281af2014-04-23 11:13:15 -04003200 if (next_css)
3201 return next_css;
3202 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3203 }
3204 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003205}
Tejun Heo53fa5262013-05-24 10:55:38 +09003206
3207/**
Tejun Heo492eb212013-08-08 20:11:25 -04003208 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003209 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003210 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003211 *
Tejun Heo492eb212013-08-08 20:11:25 -04003212 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003213 * to visit for pre-order traversal of @root's descendants. @root is
3214 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003215 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003216 * While this function requires cgroup_mutex or RCU read locking, it
3217 * doesn't require the whole traversal to be contained in a single critical
3218 * section. This function will return the correct next descendant as long
3219 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003220 */
Tejun Heo492eb212013-08-08 20:11:25 -04003221struct cgroup_subsys_state *
3222css_next_descendant_pre(struct cgroup_subsys_state *pos,
3223 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003224{
Tejun Heo492eb212013-08-08 20:11:25 -04003225 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003226
Tejun Heoace2bee2014-02-11 11:52:47 -05003227 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003228
Tejun Heobd8815a2013-08-08 20:11:27 -04003229 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003230 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003231 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003232
3233 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003234 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003235 if (next)
3236 return next;
3237
3238 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003239 while (pos != root) {
3240 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003241 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003242 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003243 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003244 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003245
3246 return NULL;
3247}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003248
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003249/**
Tejun Heo492eb212013-08-08 20:11:25 -04003250 * css_rightmost_descendant - return the rightmost descendant of a css
3251 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003252 *
Tejun Heo492eb212013-08-08 20:11:25 -04003253 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3254 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003255 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003256 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003257 * While this function requires cgroup_mutex or RCU read locking, it
3258 * doesn't require the whole traversal to be contained in a single critical
3259 * section. This function will return the correct rightmost descendant as
3260 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003261 */
Tejun Heo492eb212013-08-08 20:11:25 -04003262struct cgroup_subsys_state *
3263css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003264{
Tejun Heo492eb212013-08-08 20:11:25 -04003265 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003266
Tejun Heoace2bee2014-02-11 11:52:47 -05003267 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003268
3269 do {
3270 last = pos;
3271 /* ->prev isn't RCU safe, walk ->next till the end */
3272 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003273 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003274 pos = tmp;
3275 } while (pos);
3276
3277 return last;
3278}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003279
Tejun Heo492eb212013-08-08 20:11:25 -04003280static struct cgroup_subsys_state *
3281css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003282{
Tejun Heo492eb212013-08-08 20:11:25 -04003283 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003284
3285 do {
3286 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003287 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003288 } while (pos);
3289
3290 return last;
3291}
3292
3293/**
Tejun Heo492eb212013-08-08 20:11:25 -04003294 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003295 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003296 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003297 *
Tejun Heo492eb212013-08-08 20:11:25 -04003298 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003299 * to visit for post-order traversal of @root's descendants. @root is
3300 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003301 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003302 * While this function requires cgroup_mutex or RCU read locking, it
3303 * doesn't require the whole traversal to be contained in a single critical
3304 * section. This function will return the correct next descendant as long
3305 * as both @pos and @cgroup are accessible and @pos is a descendant of
3306 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003307 */
Tejun Heo492eb212013-08-08 20:11:25 -04003308struct cgroup_subsys_state *
3309css_next_descendant_post(struct cgroup_subsys_state *pos,
3310 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003311{
Tejun Heo492eb212013-08-08 20:11:25 -04003312 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003313
Tejun Heoace2bee2014-02-11 11:52:47 -05003314 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003315
Tejun Heo58b79a92013-09-06 15:31:08 -04003316 /* if first iteration, visit leftmost descendant which may be @root */
3317 if (!pos)
3318 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003319
Tejun Heobd8815a2013-08-08 20:11:27 -04003320 /* if we visited @root, we're done */
3321 if (pos == root)
3322 return NULL;
3323
Tejun Heo574bd9f2012-11-09 09:12:29 -08003324 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003325 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003326 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003327 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003328
3329 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003330 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003331}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003332
Tejun Heo0942eee2013-08-08 20:11:26 -04003333/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003334 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003335 * @it: the iterator to advance
3336 *
3337 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003338 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003339static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003340{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003341 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003342 struct cgrp_cset_link *link;
3343 struct css_set *cset;
3344
3345 /* Advance to the next non-empty css_set */
3346 do {
3347 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003348 if (l == it->cset_head) {
3349 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003350 return;
3351 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003352
3353 if (it->ss) {
3354 cset = container_of(l, struct css_set,
3355 e_cset_node[it->ss->id]);
3356 } else {
3357 link = list_entry(l, struct cgrp_cset_link, cset_link);
3358 cset = link->cset;
3359 }
Tejun Heoc7561122014-02-25 10:04:01 -05003360 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3361
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003362 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003363
3364 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003365 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003366 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003367 it->task_pos = cset->mg_tasks.next;
3368
3369 it->tasks_head = &cset->tasks;
3370 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003371}
3372
Tejun Heo0942eee2013-08-08 20:11:26 -04003373/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003374 * css_task_iter_start - initiate task iteration
3375 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003376 * @it: the task iterator to use
3377 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003378 * Initiate iteration through the tasks of @css. The caller can call
3379 * css_task_iter_next() to walk through the tasks until the function
3380 * returns NULL. On completion of iteration, css_task_iter_end() must be
3381 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003382 *
3383 * Note that this function acquires a lock which is released when the
3384 * iteration finishes. The caller can't sleep while iteration is in
3385 * progress.
3386 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003387void css_task_iter_start(struct cgroup_subsys_state *css,
3388 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003389 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003390{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003391 /* no one should try to iterate before mounting cgroups */
3392 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003393
Tejun Heo96d365e2014-02-13 06:58:40 -05003394 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003395
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003396 it->ss = css->ss;
3397
3398 if (it->ss)
3399 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3400 else
3401 it->cset_pos = &css->cgroup->cset_links;
3402
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003403 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003404
Tejun Heo72ec7022013-08-08 20:11:26 -04003405 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003406}
3407
Tejun Heo0942eee2013-08-08 20:11:26 -04003408/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003409 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003410 * @it: the task iterator being iterated
3411 *
3412 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003413 * initialized via css_task_iter_start(). Returns NULL when the iteration
3414 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003415 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003416struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003417{
3418 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003419 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003420
3421 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003422 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003423 return NULL;
3424 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003425
3426 /*
3427 * Advance iterator to find next entry. cset->tasks is consumed
3428 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3429 * next cset.
3430 */
Paul Menage817929e2007-10-18 23:39:36 -07003431 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003432
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003433 if (l == it->tasks_head)
3434 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003435
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003436 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003437 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003438 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003439 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003440
Paul Menage817929e2007-10-18 23:39:36 -07003441 return res;
3442}
3443
Tejun Heo0942eee2013-08-08 20:11:26 -04003444/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003445 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003446 * @it: the task iterator to finish
3447 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003448 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003449 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003450void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003451 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003452{
Tejun Heo96d365e2014-02-13 06:58:40 -05003453 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003454}
3455
3456/**
3457 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3458 * @to: cgroup to which the tasks will be moved
3459 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003460 *
3461 * Locking rules between cgroup_post_fork() and the migration path
3462 * guarantee that, if a task is forking while being migrated, the new child
3463 * is guaranteed to be either visible in the source cgroup after the
3464 * parent's migration is complete or put into the target cgroup. No task
3465 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003466 */
3467int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3468{
Tejun Heo952aaa12014-02-25 10:04:03 -05003469 LIST_HEAD(preloaded_csets);
3470 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003471 struct css_task_iter it;
3472 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003473 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003474
Tejun Heo952aaa12014-02-25 10:04:03 -05003475 mutex_lock(&cgroup_mutex);
3476
3477 /* all tasks in @from are being moved, all csets are source */
3478 down_read(&css_set_rwsem);
3479 list_for_each_entry(link, &from->cset_links, cset_link)
3480 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3481 up_read(&css_set_rwsem);
3482
3483 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3484 if (ret)
3485 goto out_err;
3486
3487 /*
3488 * Migrate tasks one-by-one until @form is empty. This fails iff
3489 * ->can_attach() fails.
3490 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003491 do {
3492 css_task_iter_start(&from->dummy_css, &it);
3493 task = css_task_iter_next(&it);
3494 if (task)
3495 get_task_struct(task);
3496 css_task_iter_end(&it);
3497
3498 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003499 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003500 put_task_struct(task);
3501 }
3502 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003503out_err:
3504 cgroup_migrate_finish(&preloaded_csets);
3505 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003506 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003507}
3508
Paul Menage817929e2007-10-18 23:39:36 -07003509/*
Ben Blum102a7752009-09-23 15:56:26 -07003510 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003511 *
3512 * Reading this file can return large amounts of data if a cgroup has
3513 * *lots* of attached tasks. So it may need several calls to read(),
3514 * but we cannot guarantee that the information we produce is correct
3515 * unless we produce it entirely atomically.
3516 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003517 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003518
Li Zefan24528252012-01-20 11:58:43 +08003519/* which pidlist file are we talking about? */
3520enum cgroup_filetype {
3521 CGROUP_FILE_PROCS,
3522 CGROUP_FILE_TASKS,
3523};
3524
3525/*
3526 * A pidlist is a list of pids that virtually represents the contents of one
3527 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3528 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3529 * to the cgroup.
3530 */
3531struct cgroup_pidlist {
3532 /*
3533 * used to find which pidlist is wanted. doesn't change as long as
3534 * this particular list stays in the list.
3535 */
3536 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3537 /* array of xids */
3538 pid_t *list;
3539 /* how many elements the above list has */
3540 int length;
Li Zefan24528252012-01-20 11:58:43 +08003541 /* each of these stored in a list by its cgroup */
3542 struct list_head links;
3543 /* pointer to the cgroup we belong to, for list removal purposes */
3544 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003545 /* for delayed destruction */
3546 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003547};
3548
Paul Menagebbcb81d2007-10-18 23:39:32 -07003549/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003550 * The following two functions "fix" the issue where there are more pids
3551 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3552 * TODO: replace with a kernel-wide solution to this problem
3553 */
3554#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3555static void *pidlist_allocate(int count)
3556{
3557 if (PIDLIST_TOO_LARGE(count))
3558 return vmalloc(count * sizeof(pid_t));
3559 else
3560 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3561}
Tejun Heob1a21362013-11-29 10:42:58 -05003562
Ben Blumd1d9fd32009-09-23 15:56:28 -07003563static void pidlist_free(void *p)
3564{
3565 if (is_vmalloc_addr(p))
3566 vfree(p);
3567 else
3568 kfree(p);
3569}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003570
3571/*
Tejun Heob1a21362013-11-29 10:42:58 -05003572 * Used to destroy all pidlists lingering waiting for destroy timer. None
3573 * should be left afterwards.
3574 */
3575static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3576{
3577 struct cgroup_pidlist *l, *tmp_l;
3578
3579 mutex_lock(&cgrp->pidlist_mutex);
3580 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3581 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3582 mutex_unlock(&cgrp->pidlist_mutex);
3583
3584 flush_workqueue(cgroup_pidlist_destroy_wq);
3585 BUG_ON(!list_empty(&cgrp->pidlists));
3586}
3587
3588static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3589{
3590 struct delayed_work *dwork = to_delayed_work(work);
3591 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3592 destroy_dwork);
3593 struct cgroup_pidlist *tofree = NULL;
3594
3595 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003596
3597 /*
Tejun Heo04502362013-11-29 10:42:59 -05003598 * Destroy iff we didn't get queued again. The state won't change
3599 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003600 */
Tejun Heo04502362013-11-29 10:42:59 -05003601 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003602 list_del(&l->links);
3603 pidlist_free(l->list);
3604 put_pid_ns(l->key.ns);
3605 tofree = l;
3606 }
3607
Tejun Heob1a21362013-11-29 10:42:58 -05003608 mutex_unlock(&l->owner->pidlist_mutex);
3609 kfree(tofree);
3610}
3611
3612/*
Ben Blum102a7752009-09-23 15:56:26 -07003613 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003614 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003615 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003616static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003617{
Ben Blum102a7752009-09-23 15:56:26 -07003618 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003619
3620 /*
3621 * we presume the 0th element is unique, so i starts at 1. trivial
3622 * edge cases first; no work needs to be done for either
3623 */
3624 if (length == 0 || length == 1)
3625 return length;
3626 /* src and dest walk down the list; dest counts unique elements */
3627 for (src = 1; src < length; src++) {
3628 /* find next unique element */
3629 while (list[src] == list[src-1]) {
3630 src++;
3631 if (src == length)
3632 goto after;
3633 }
3634 /* dest always points to where the next unique element goes */
3635 list[dest] = list[src];
3636 dest++;
3637 }
3638after:
Ben Blum102a7752009-09-23 15:56:26 -07003639 return dest;
3640}
3641
Tejun Heoafb2bc12013-11-29 10:42:59 -05003642/*
3643 * The two pid files - task and cgroup.procs - guaranteed that the result
3644 * is sorted, which forced this whole pidlist fiasco. As pid order is
3645 * different per namespace, each namespace needs differently sorted list,
3646 * making it impossible to use, for example, single rbtree of member tasks
3647 * sorted by task pointer. As pidlists can be fairly large, allocating one
3648 * per open file is dangerous, so cgroup had to implement shared pool of
3649 * pidlists keyed by cgroup and namespace.
3650 *
3651 * All this extra complexity was caused by the original implementation
3652 * committing to an entirely unnecessary property. In the long term, we
3653 * want to do away with it. Explicitly scramble sort order if
3654 * sane_behavior so that no such expectation exists in the new interface.
3655 *
3656 * Scrambling is done by swapping every two consecutive bits, which is
3657 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3658 */
3659static pid_t pid_fry(pid_t pid)
3660{
3661 unsigned a = pid & 0x55555555;
3662 unsigned b = pid & 0xAAAAAAAA;
3663
3664 return (a << 1) | (b >> 1);
3665}
3666
3667static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3668{
3669 if (cgroup_sane_behavior(cgrp))
3670 return pid_fry(pid);
3671 else
3672 return pid;
3673}
3674
Ben Blum102a7752009-09-23 15:56:26 -07003675static int cmppid(const void *a, const void *b)
3676{
3677 return *(pid_t *)a - *(pid_t *)b;
3678}
3679
Tejun Heoafb2bc12013-11-29 10:42:59 -05003680static int fried_cmppid(const void *a, const void *b)
3681{
3682 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3683}
3684
Ben Blum72a8cb32009-09-23 15:56:27 -07003685static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3686 enum cgroup_filetype type)
3687{
3688 struct cgroup_pidlist *l;
3689 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003690 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003691
Tejun Heoe6b81712013-11-29 10:42:59 -05003692 lockdep_assert_held(&cgrp->pidlist_mutex);
3693
3694 list_for_each_entry(l, &cgrp->pidlists, links)
3695 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003697 return NULL;
3698}
3699
Ben Blum72a8cb32009-09-23 15:56:27 -07003700/*
3701 * find the appropriate pidlist for our purpose (given procs vs tasks)
3702 * returns with the lock on that pidlist already held, and takes care
3703 * of the use count, or returns NULL with no locks held if we're out of
3704 * memory.
3705 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003706static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3707 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003708{
3709 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003710
Tejun Heoe6b81712013-11-29 10:42:59 -05003711 lockdep_assert_held(&cgrp->pidlist_mutex);
3712
3713 l = cgroup_pidlist_find(cgrp, type);
3714 if (l)
3715 return l;
3716
Ben Blum72a8cb32009-09-23 15:56:27 -07003717 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003718 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003719 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003720 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003721
Tejun Heob1a21362013-11-29 10:42:58 -05003722 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003723 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003724 /* don't need task_nsproxy() if we're looking at ourself */
3725 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003726 l->owner = cgrp;
3727 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003728 return l;
3729}
3730
3731/*
Ben Blum102a7752009-09-23 15:56:26 -07003732 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3733 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003734static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3735 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003736{
3737 pid_t *array;
3738 int length;
3739 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003740 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003741 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003742 struct cgroup_pidlist *l;
3743
Tejun Heo4bac00d2013-11-29 10:42:59 -05003744 lockdep_assert_held(&cgrp->pidlist_mutex);
3745
Ben Blum102a7752009-09-23 15:56:26 -07003746 /*
3747 * If cgroup gets more users after we read count, we won't have
3748 * enough space - tough. This race is indistinguishable to the
3749 * caller from the case that the additional cgroup users didn't
3750 * show up until sometime later on.
3751 */
3752 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003753 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003754 if (!array)
3755 return -ENOMEM;
3756 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003757 css_task_iter_start(&cgrp->dummy_css, &it);
3758 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003759 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003760 break;
Ben Blum102a7752009-09-23 15:56:26 -07003761 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003762 if (type == CGROUP_FILE_PROCS)
3763 pid = task_tgid_vnr(tsk);
3764 else
3765 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003766 if (pid > 0) /* make sure to only use valid results */
3767 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003768 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003769 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003770 length = n;
3771 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003772 if (cgroup_sane_behavior(cgrp))
3773 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3774 else
3775 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003776 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003777 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003778
Tejun Heoe6b81712013-11-29 10:42:59 -05003779 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003780 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003781 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003782 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003783 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003784 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003785
3786 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003787 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003788 l->list = array;
3789 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003790 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003791 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003792}
3793
Balbir Singh846c7bb2007-10-18 23:39:44 -07003794/**
Li Zefana043e3b2008-02-23 15:24:09 -08003795 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003796 * @stats: cgroupstats to fill information into
3797 * @dentry: A dentry entry belonging to the cgroup for which stats have
3798 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003799 *
3800 * Build and fill cgroupstats so that taskstats can export it to user
3801 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003802 */
3803int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3804{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003805 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003806 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003807 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003808 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003809
Tejun Heo2bd59d42014-02-11 11:52:49 -05003810 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3811 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3812 kernfs_type(kn) != KERNFS_DIR)
3813 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003814
Li Zefanbad34662014-02-14 16:54:28 +08003815 mutex_lock(&cgroup_mutex);
3816
Tejun Heo2bd59d42014-02-11 11:52:49 -05003817 /*
3818 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04003819 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05003820 * @kn->priv is RCU safe. Let's do the RCU dancing.
3821 */
3822 rcu_read_lock();
3823 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003824 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003825 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003826 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003827 return -ENOENT;
3828 }
Li Zefanbad34662014-02-14 16:54:28 +08003829 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003830
Tejun Heo72ec7022013-08-08 20:11:26 -04003831 css_task_iter_start(&cgrp->dummy_css, &it);
3832 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003833 switch (tsk->state) {
3834 case TASK_RUNNING:
3835 stats->nr_running++;
3836 break;
3837 case TASK_INTERRUPTIBLE:
3838 stats->nr_sleeping++;
3839 break;
3840 case TASK_UNINTERRUPTIBLE:
3841 stats->nr_uninterruptible++;
3842 break;
3843 case TASK_STOPPED:
3844 stats->nr_stopped++;
3845 break;
3846 default:
3847 if (delayacct_is_task_waiting_on_io(tsk))
3848 stats->nr_io_wait++;
3849 break;
3850 }
3851 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003852 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003853
Li Zefanbad34662014-02-14 16:54:28 +08003854 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003855 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003856}
3857
Paul Menage8f3ff202009-09-23 15:56:25 -07003858
Paul Menagecc31edc2008-10-18 20:28:04 -07003859/*
Ben Blum102a7752009-09-23 15:56:26 -07003860 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003861 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003862 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003863 */
3864
Ben Blum102a7752009-09-23 15:56:26 -07003865static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003866{
3867 /*
3868 * Initially we receive a position value that corresponds to
3869 * one more than the last pid shown (or 0 on the first call or
3870 * after a seek to the start). Use a binary-search to find the
3871 * next pid to display, if any
3872 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003873 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003874 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003875 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003876 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003877 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003878 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003879
Tejun Heo4bac00d2013-11-29 10:42:59 -05003880 mutex_lock(&cgrp->pidlist_mutex);
3881
3882 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003883 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003884 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003885 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003886 * could already have been destroyed.
3887 */
Tejun Heo5d224442013-12-05 12:28:04 -05003888 if (of->priv)
3889 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003890
3891 /*
3892 * Either this is the first start() after open or the matching
3893 * pidlist has been destroyed inbetween. Create a new one.
3894 */
Tejun Heo5d224442013-12-05 12:28:04 -05003895 if (!of->priv) {
3896 ret = pidlist_array_load(cgrp, type,
3897 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003898 if (ret)
3899 return ERR_PTR(ret);
3900 }
Tejun Heo5d224442013-12-05 12:28:04 -05003901 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003902
Paul Menagecc31edc2008-10-18 20:28:04 -07003903 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003904 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003905
Paul Menagecc31edc2008-10-18 20:28:04 -07003906 while (index < end) {
3907 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003908 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003909 index = mid;
3910 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003911 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003912 index = mid + 1;
3913 else
3914 end = mid;
3915 }
3916 }
3917 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003918 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003919 return NULL;
3920 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003921 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003922 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003923 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003924}
3925
Ben Blum102a7752009-09-23 15:56:26 -07003926static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003927{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003928 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003929 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003930
Tejun Heo5d224442013-12-05 12:28:04 -05003931 if (l)
3932 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003933 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003934 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003935}
3936
Ben Blum102a7752009-09-23 15:56:26 -07003937static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003938{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003939 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003940 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003941 pid_t *p = v;
3942 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003943 /*
3944 * Advance to the next pid in the array. If this goes off the
3945 * end, we're done
3946 */
3947 p++;
3948 if (p >= end) {
3949 return NULL;
3950 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003951 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003952 return p;
3953 }
3954}
3955
Ben Blum102a7752009-09-23 15:56:26 -07003956static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003957{
3958 return seq_printf(s, "%d\n", *(int *)v);
3959}
3960
Tejun Heo182446d2013-08-08 20:11:24 -04003961static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3962 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003963{
Tejun Heo182446d2013-08-08 20:11:24 -04003964 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003965}
3966
Tejun Heo182446d2013-08-08 20:11:24 -04003967static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3968 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003969{
Tejun Heo182446d2013-08-08 20:11:24 -04003970 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003971 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003972 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003973 else
Tejun Heo182446d2013-08-08 20:11:24 -04003974 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003975 return 0;
3976}
3977
Tejun Heo182446d2013-08-08 20:11:24 -04003978static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3979 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003980{
Tejun Heo182446d2013-08-08 20:11:24 -04003981 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003982}
3983
Tejun Heo182446d2013-08-08 20:11:24 -04003984static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3985 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003986{
3987 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003988 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003989 else
Tejun Heo182446d2013-08-08 20:11:24 -04003990 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003991 return 0;
3992}
3993
Tejun Heod5c56ce2013-06-03 19:14:34 -07003994static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003995 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003996 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003997 .seq_start = cgroup_pidlist_start,
3998 .seq_next = cgroup_pidlist_next,
3999 .seq_stop = cgroup_pidlist_stop,
4000 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004001 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004002 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004003 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004004 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004005 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004006 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004007 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004008 .read_u64 = cgroup_clone_children_read,
4009 .write_u64 = cgroup_clone_children_write,
4010 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004011 {
Tejun Heo873fe092013-04-14 20:15:26 -07004012 .name = "cgroup.sane_behavior",
4013 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004014 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004015 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004016 {
4017 .name = "cgroup.controllers",
4018 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
4019 .seq_show = cgroup_root_controllers_show,
4020 },
4021 {
4022 .name = "cgroup.controllers",
4023 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4024 .seq_show = cgroup_controllers_show,
4025 },
4026 {
4027 .name = "cgroup.subtree_control",
4028 .flags = CFTYPE_ONLY_ON_DFL,
4029 .seq_show = cgroup_subtree_control_show,
Tejun Heo451af502014-05-13 12:16:21 -04004030 .write = cgroup_subtree_control_write,
Tejun Heof8f22e52014-04-23 11:13:16 -04004031 },
Tejun Heo842b5972014-04-25 18:28:02 -04004032 {
4033 .name = "cgroup.populated",
4034 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
4035 .seq_show = cgroup_populated_show,
4036 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004037
4038 /*
4039 * Historical crazy stuff. These don't have "cgroup." prefix and
4040 * don't exist if sane_behavior. If you're depending on these, be
4041 * prepared to be burned.
4042 */
4043 {
4044 .name = "tasks",
4045 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004046 .seq_start = cgroup_pidlist_start,
4047 .seq_next = cgroup_pidlist_next,
4048 .seq_stop = cgroup_pidlist_stop,
4049 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004050 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004051 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004052 .mode = S_IRUGO | S_IWUSR,
4053 },
4054 {
4055 .name = "notify_on_release",
4056 .flags = CFTYPE_INSANE,
4057 .read_u64 = cgroup_read_notify_on_release,
4058 .write_u64 = cgroup_write_notify_on_release,
4059 },
Tejun Heo873fe092013-04-14 20:15:26 -07004060 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004061 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004062 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004063 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004064 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004065 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004066 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004067 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004068};
4069
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004070/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004071 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004072 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004073 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004074 *
4075 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004076 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004077static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004080 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004081
Tejun Heo8e3f6542012-04-01 12:09:55 -07004082 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004083 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004084 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004085
Tejun Heo69dfa002014-05-04 15:09:13 -04004086 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004087 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004088
Tejun Heo0adb0702014-02-12 09:29:48 -05004089 list_for_each_entry(cfts, &ss->cfts, node) {
4090 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004091 if (ret < 0)
4092 goto err;
4093 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004095 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004096err:
4097 cgroup_clear_dir(cgrp, subsys_mask);
4098 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099}
4100
Tejun Heo0c21ead2013-08-13 20:22:51 -04004101/*
4102 * css destruction is four-stage process.
4103 *
4104 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4105 * Implemented in kill_css().
4106 *
4107 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004108 * and thus css_tryget_online() is guaranteed to fail, the css can be
4109 * offlined by invoking offline_css(). After offlining, the base ref is
4110 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004111 *
4112 * 3. When the percpu_ref reaches zero, the only possible remaining
4113 * accessors are inside RCU read sections. css_release() schedules the
4114 * RCU callback.
4115 *
4116 * 4. After the grace period, the css can be freed. Implemented in
4117 * css_free_work_fn().
4118 *
4119 * It is actually hairier because both step 2 and 4 require process context
4120 * and thus involve punting to css->destroy_work adding two additional
4121 * steps to the already complex sequence.
4122 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004123static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004124{
4125 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004126 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004127 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004128
Tejun Heo0ae78e02013-08-13 11:01:54 -04004129 if (css->parent)
4130 css_put(css->parent);
4131
Tejun Heo0c21ead2013-08-13 20:22:51 -04004132 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004133 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004134}
4135
4136static void css_free_rcu_fn(struct rcu_head *rcu_head)
4137{
4138 struct cgroup_subsys_state *css =
4139 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4140
Tejun Heo0c21ead2013-08-13 20:22:51 -04004141 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004142 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004143}
4144
Tejun Heod3daf282013-06-13 19:39:16 -07004145static void css_release(struct percpu_ref *ref)
4146{
4147 struct cgroup_subsys_state *css =
4148 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo15a4c832014-05-04 15:09:14 -04004149 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004150
Tejun Heo15a4c832014-05-04 15:09:14 -04004151 cgroup_idr_remove(&ss->css_idr, css->id);
4152
Tejun Heo0c21ead2013-08-13 20:22:51 -04004153 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004154}
4155
Tejun Heoddfcada2014-05-04 15:09:14 -04004156static void init_and_link_css(struct cgroup_subsys_state *css,
4157 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158{
Tejun Heoddfcada2014-05-04 15:09:14 -04004159 cgroup_get(cgrp);
4160
Paul Menagebd89aab2007-10-18 23:40:44 -07004161 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004162 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004164
Tejun Heoddfcada2014-05-04 15:09:14 -04004165 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004166 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004167 css_get(css->parent);
4168 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004170 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004171
Tejun Heoca8bdca2013-08-26 18:40:56 -04004172 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173}
4174
Li Zefan2a4ac632013-07-31 16:16:40 +08004175/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004176static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004177{
Tejun Heo623f9262013-08-13 11:01:55 -04004178 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004179 int ret = 0;
4180
Tejun Heoace2bee2014-02-11 11:52:47 -05004181 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004182 lockdep_assert_held(&cgroup_mutex);
4183
Tejun Heo92fb9742012-11-19 08:13:38 -08004184 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004185 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004186 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004187 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004188 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004189 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004190 }
Tejun Heob1929db2012-11-19 08:13:38 -08004191 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004192}
4193
Li Zefan2a4ac632013-07-31 16:16:40 +08004194/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004195static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004196{
Tejun Heo623f9262013-08-13 11:01:55 -04004197 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004198
Tejun Heoace2bee2014-02-11 11:52:47 -05004199 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004200 lockdep_assert_held(&cgroup_mutex);
4201
4202 if (!(css->flags & CSS_ONLINE))
4203 return;
4204
Li Zefand7eeac12013-03-12 15:35:59 -07004205 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004206 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004207
Tejun Heoeb954192013-08-08 20:11:23 -04004208 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004209 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004210 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004211
4212 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004213}
4214
Tejun Heoc81c925a2013-12-06 15:11:56 -05004215/**
4216 * create_css - create a cgroup_subsys_state
4217 * @cgrp: the cgroup new css will be associated with
4218 * @ss: the subsys of new css
4219 *
4220 * Create a new css associated with @cgrp - @ss pair. On success, the new
4221 * css is online and installed in @cgrp with all interface files created.
4222 * Returns 0 on success, -errno on failure.
4223 */
4224static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4225{
4226 struct cgroup *parent = cgrp->parent;
4227 struct cgroup_subsys_state *css;
4228 int err;
4229
Tejun Heoc81c925a2013-12-06 15:11:56 -05004230 lockdep_assert_held(&cgroup_mutex);
4231
4232 css = ss->css_alloc(cgroup_css(parent, ss));
4233 if (IS_ERR(css))
4234 return PTR_ERR(css);
4235
Tejun Heoddfcada2014-05-04 15:09:14 -04004236 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004237
Tejun Heoc81c925a2013-12-06 15:11:56 -05004238 err = percpu_ref_init(&css->refcnt, css_release);
4239 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004240 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004241
Tejun Heo15a4c832014-05-04 15:09:14 -04004242 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4243 if (err < 0)
4244 goto err_free_percpu_ref;
4245 css->id = err;
4246
Tejun Heoaec25022014-02-08 10:36:58 -05004247 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004248 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004249 goto err_free_id;
4250
4251 /* @css is ready to be brought online now, make it visible */
4252 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004253
4254 err = online_css(css);
4255 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004256 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004257
Tejun Heoc81c925a2013-12-06 15:11:56 -05004258 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4259 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004260 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 -04004261 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004262 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004263 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004264 ss->warned_broken_hierarchy = true;
4265 }
4266
4267 return 0;
4268
Li Zefan3eb59ec2014-03-18 17:02:36 +08004269err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004270 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004271err_free_id:
4272 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004273err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004274 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004275err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004276 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004277 return err;
4278}
4279
Tejun Heob3bfd982014-05-13 12:19:22 -04004280static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4281 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004282{
Tejun Heoa9746d82014-05-13 12:19:22 -04004283 struct cgroup *parent, *cgrp;
4284 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004286 struct kernfs_node *kn;
Tejun Heob3bfd982014-05-13 12:19:22 -04004287 int ssid, ret;
Li Zefan65dff752013-03-01 15:01:56 +08004288
Tejun Heoa9746d82014-05-13 12:19:22 -04004289 parent = cgroup_kn_lock_live(parent_kn);
4290 if (!parent)
4291 return -ENODEV;
4292 root = parent->root;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004293
4294 /* allocate the cgroup and its ID, 0 is reserved for the root */
4295 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4296 if (!cgrp) {
4297 ret = -ENOMEM;
4298 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004299 }
4300
4301 /*
4302 * Temporarily set the pointer to NULL, so idr_find() won't return
4303 * a half-baked cgroup.
4304 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004305 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004306 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004307 ret = -ENOMEM;
4308 goto out_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004309 }
4310
Paul Menagecc31edc2008-10-18 20:28:04 -07004311 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004312
Paul Menagebd89aab2007-10-18 23:40:44 -07004313 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004314 cgrp->dummy_css.parent = &parent->dummy_css;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004315 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004316
Li Zefanb6abdb02008-03-04 14:28:19 -08004317 if (notify_on_release(parent))
4318 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4319
Tejun Heo2260e7f2012-11-19 08:13:38 -08004320 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4321 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004322
Tejun Heo2bd59d42014-02-11 11:52:49 -05004323 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004324 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004325 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004326 ret = PTR_ERR(kn);
4327 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004328 }
4329 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330
Tejun Heo6f305582014-02-12 09:29:50 -05004331 /*
4332 * This extra ref will be put in cgroup_free_fn() and guarantees
4333 * that @cgrp->kn is always accessible.
4334 */
4335 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336
Tejun Heo00356bd2013-06-18 11:14:22 -07004337 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004338
Tejun Heo4e139af2012-11-19 08:13:36 -08004339 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004340 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004341 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004342 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004343
Tejun Heo0d802552013-12-06 15:11:56 -05004344 /*
4345 * @cgrp is now fully operational. If something fails after this
4346 * point, it'll be released via the normal destruction path.
4347 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004348 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004349
Tejun Heoba0f4d72014-05-13 12:19:22 -04004350 ret = cgroup_kn_set_ugid(kn);
4351 if (ret)
4352 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004353
Tejun Heoba0f4d72014-05-13 12:19:22 -04004354 ret = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4355 if (ret)
4356 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004357
Tejun Heo9d403e92013-12-06 15:11:56 -05004358 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004359 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004360 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004361 ret = create_css(cgrp, ss);
4362 if (ret)
4363 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004364 }
Tejun Heoa8638032012-11-09 09:12:29 -08004365 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366
Tejun Heobd53d612014-04-23 11:13:16 -04004367 /*
4368 * On the default hierarchy, a child doesn't automatically inherit
4369 * child_subsys_mask from the parent. Each is configured manually.
4370 */
4371 if (!cgroup_on_dfl(cgrp))
4372 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004373
Tejun Heo2bd59d42014-02-11 11:52:49 -05004374 kernfs_activate(kn);
4375
Tejun Heoba0f4d72014-05-13 12:19:22 -04004376 ret = 0;
4377 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378
Tejun Heoba0f4d72014-05-13 12:19:22 -04004379out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004380 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004381out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004382 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004383out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004384 cgroup_kn_unlock(parent_kn);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004385 return ret;
4386
4387out_destroy:
4388 cgroup_destroy_locked(cgrp);
4389 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390}
4391
Tejun Heo223dbc32013-08-13 20:22:50 -04004392/*
4393 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heoec903c02014-05-13 12:11:01 -04004394 * css_tryget_online() is now guaranteed to fail.
Tejun Heo223dbc32013-08-13 20:22:50 -04004395 */
4396static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004397{
Tejun Heo223dbc32013-08-13 20:22:50 -04004398 struct cgroup_subsys_state *css =
4399 container_of(work, struct cgroup_subsys_state, destroy_work);
4400 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004401
Tejun Heoace2bee2014-02-11 11:52:47 -05004402 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004403 mutex_lock(&cgroup_mutex);
4404
4405 /*
Tejun Heoec903c02014-05-13 12:11:01 -04004406 * css_tryget_online() is guaranteed to fail now. Tell subsystems
4407 * to initate destruction.
Tejun Heo09a503ea2013-08-13 20:22:50 -04004408 */
4409 offline_css(css);
4410
4411 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004412 * If @cgrp is marked dead, it's waiting for refs of all css's to
4413 * be disabled before proceeding to the second phase of cgroup
4414 * destruction. If we are the last one, kick it off.
4415 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004416 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004417 cgroup_destroy_css_killed(cgrp);
4418
4419 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004420 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004421
4422 /*
4423 * Put the css refs from kill_css(). Each css holds an extra
4424 * reference to the cgroup's dentry and cgroup removal proceeds
4425 * regardless of css refs. On the last put of each css, whenever
4426 * that may be, the extra dentry ref is put so that dentry
4427 * destruction happens only after all css's are released.
4428 */
4429 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004430}
4431
Tejun Heo223dbc32013-08-13 20:22:50 -04004432/* css kill confirmation processing requires process context, bounce */
4433static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004434{
4435 struct cgroup_subsys_state *css =
4436 container_of(ref, struct cgroup_subsys_state, refcnt);
4437
Tejun Heo223dbc32013-08-13 20:22:50 -04004438 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004439 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004440}
4441
Tejun Heof392e512014-04-23 11:13:14 -04004442/**
4443 * kill_css - destroy a css
4444 * @css: css to destroy
4445 *
4446 * This function initiates destruction of @css by removing cgroup interface
4447 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004448 * asynchronously once css_tryget_online() is guaranteed to fail and when
4449 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004450 */
4451static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004452{
Tejun Heo94419622014-03-19 10:23:54 -04004453 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo01f64742014-05-13 12:19:23 -04004454 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004455
Tejun Heo2bd59d42014-02-11 11:52:49 -05004456 /*
4457 * This must happen before css is disassociated with its cgroup.
4458 * See seq_css() for details.
4459 */
Tejun Heoaec25022014-02-08 10:36:58 -05004460 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004461
Tejun Heoedae0c32013-08-13 20:22:51 -04004462 /*
4463 * Killing would put the base ref, but we need to keep it alive
4464 * until after ->css_offline().
4465 */
4466 css_get(css);
4467
4468 /*
4469 * cgroup core guarantees that, by the time ->css_offline() is
4470 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004471 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004472 * proceed to offlining css's because percpu_ref_kill() doesn't
4473 * guarantee that the ref is seen as killed on all CPUs on return.
4474 *
4475 * Use percpu_ref_kill_and_confirm() to get notifications as each
4476 * css is confirmed to be seen as killed on all CPUs.
4477 */
4478 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004479}
4480
4481/**
4482 * cgroup_destroy_locked - the first stage of cgroup destruction
4483 * @cgrp: cgroup to be destroyed
4484 *
4485 * css's make use of percpu refcnts whose killing latency shouldn't be
4486 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004487 * guarantee that css_tryget_online() won't succeed by the time
4488 * ->css_offline() is invoked. To satisfy all the requirements,
4489 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004490 *
4491 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4492 * userland visible parts and start killing the percpu refcnts of
4493 * css's. Set up so that the next stage will be kicked off once all
4494 * the percpu refcnts are confirmed to be killed.
4495 *
4496 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4497 * rest of destruction. Once all cgroup references are gone, the
4498 * cgroup is RCU-freed.
4499 *
4500 * This function implements s1. After this step, @cgrp is gone as far as
4501 * the userland is concerned and a new cgroup with the same name may be
4502 * created. As cgroup doesn't care about the names internally, this
4503 * doesn't cause any problem.
4504 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004505static int cgroup_destroy_locked(struct cgroup *cgrp)
4506 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004507{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004508 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004509 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004510 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004511 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004512
Tejun Heoace2bee2014-02-11 11:52:47 -05004513 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004514 lockdep_assert_held(&cgroup_mutex);
4515
Tejun Heoddd69142013-06-12 21:04:54 -07004516 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004517 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004518 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004519 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004520 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004521 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004522 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004523 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004524 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004525
Tejun Heo1a90dd52012-11-05 09:16:59 -08004526 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004527 * Make sure there's no live children. We can't test ->children
4528 * emptiness as dead children linger on it while being destroyed;
4529 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4530 */
4531 empty = true;
4532 rcu_read_lock();
4533 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4534 empty = cgroup_is_dead(child);
4535 if (!empty)
4536 break;
4537 }
4538 rcu_read_unlock();
4539 if (!empty)
4540 return -EBUSY;
4541
4542 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004543 * Mark @cgrp dead. This prevents further task migration and child
4544 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004545 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004546 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004547 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004548 */
Tejun Heo54766d42013-06-12 21:04:53 -07004549 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004550
Tejun Heo5d773812014-03-19 10:23:53 -04004551 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004552 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4553 * will be invoked to perform the rest of destruction once the
Tejun Heo01f64742014-05-13 12:19:23 -04004554 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004555 */
4556 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004557 kill_css(css);
4558
Tejun Heo455050d2013-06-13 19:27:41 -07004559 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4560 raw_spin_lock(&release_list_lock);
4561 if (!list_empty(&cgrp->release_list))
4562 list_del_init(&cgrp->release_list);
4563 raw_spin_unlock(&release_list_lock);
4564
4565 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004566 * If @cgrp has css's attached, the second stage of cgroup
4567 * destruction is kicked off from css_killed_work_fn() after the
4568 * refs of all attached css's are killed. If @cgrp doesn't have
4569 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004570 */
Tejun Heof20104d2013-08-13 20:22:50 -04004571 if (!cgrp->nr_css)
4572 cgroup_destroy_css_killed(cgrp);
4573
Tejun Heo01f64742014-05-13 12:19:23 -04004574 /*
4575 * Remove @cgrp directory along with the base files. @cgrp has an
4576 * extra ref on its kn.
4577 */
4578 kernfs_remove(cgrp->kn);
Tejun Heo455050d2013-06-13 19:27:41 -07004579
Tejun Heoea15f8c2013-06-13 19:27:42 -07004580 return 0;
4581};
4582
Tejun Heod3daf282013-06-13 19:39:16 -07004583/**
Tejun Heof20104d2013-08-13 20:22:50 -04004584 * cgroup_destroy_css_killed - the second step of cgroup destruction
Fabian Frederick60106942014-05-05 20:08:13 +02004585 * @cgrp: the cgroup whose csses have just finished offlining
Tejun Heod3daf282013-06-13 19:39:16 -07004586 *
4587 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004588 * destroyed after all css's are offlined and performs the rest of
4589 * destruction. This is the second step of destruction described in the
4590 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004591 */
Tejun Heof20104d2013-08-13 20:22:50 -04004592static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004593{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004594 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004595
Tejun Heoace2bee2014-02-11 11:52:47 -05004596 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004597 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004598
Paul Menage999cd8a2009-01-07 18:08:36 -08004599 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004600 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004601
Tejun Heo59f52962014-02-11 11:52:49 -05004602 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603
Paul Menagebd89aab2007-10-18 23:40:44 -07004604 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004605 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004606}
4607
Tejun Heo2bd59d42014-02-11 11:52:49 -05004608static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004609{
Tejun Heoa9746d82014-05-13 12:19:22 -04004610 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004611 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004612
Tejun Heoa9746d82014-05-13 12:19:22 -04004613 cgrp = cgroup_kn_lock_live(kn);
4614 if (!cgrp)
4615 return 0;
4616 cgroup_get(cgrp); /* for @kn->priv clearing */
Tejun Heo42809dd2012-11-19 08:13:37 -08004617
Tejun Heoa9746d82014-05-13 12:19:22 -04004618 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004619
Tejun Heoa9746d82014-05-13 12:19:22 -04004620 cgroup_kn_unlock(kn);
Tejun Heocfc79d52014-05-13 12:19:22 -04004621
4622 /*
4623 * There are two control paths which try to determine cgroup from
4624 * dentry without going through kernfs - cgroupstats_build() and
4625 * css_tryget_online_from_dir(). Those are supported by RCU
4626 * protecting clearing of cgrp->kn->priv backpointer, which should
4627 * happen after all files under it have been removed.
4628 */
4629 if (!ret)
4630 RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL);
4631
Tejun Heo2bd59d42014-02-11 11:52:49 -05004632 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004633 return ret;
4634}
4635
Tejun Heo2bd59d42014-02-11 11:52:49 -05004636static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4637 .remount_fs = cgroup_remount,
4638 .show_options = cgroup_show_options,
4639 .mkdir = cgroup_mkdir,
4640 .rmdir = cgroup_rmdir,
4641 .rename = cgroup_rename,
4642};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004643
Tejun Heo15a4c832014-05-04 15:09:14 -04004644static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004647
4648 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649
Tejun Heoace2bee2014-02-11 11:52:47 -05004650 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004651 mutex_lock(&cgroup_mutex);
4652
Tejun Heo15a4c832014-05-04 15:09:14 -04004653 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004654 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004655
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004656 /* Create the root cgroup state for this subsystem */
4657 ss->root = &cgrp_dfl_root;
4658 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659 /* We don't handle early failures gracefully */
4660 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004661 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004662 if (early) {
4663 /* idr_alloc() can't be called safely during early init */
4664 css->id = 1;
4665 } else {
4666 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4667 BUG_ON(css->id < 0);
4668 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669
Li Zefane8d55fd2008-04-29 01:00:13 -07004670 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004671 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004672 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004673 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004674 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675
4676 need_forkexit_callback |= ss->fork || ss->exit;
4677
Li Zefane8d55fd2008-04-29 01:00:13 -07004678 /* At system boot, before all subsystems have been
4679 * registered, no tasks have been forked, so we don't
4680 * need to invoke fork callbacks here. */
4681 BUG_ON(!list_empty(&init_task.tasks));
4682
Tejun Heoae7f1642013-08-13 20:22:50 -04004683 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004684
Tejun Heof392e512014-04-23 11:13:14 -04004685 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004686
Ben Blume6a11052010-03-10 15:22:09 -08004687 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004688 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004689}
4690
4691/**
Li Zefana043e3b2008-02-23 15:24:09 -08004692 * cgroup_init_early - cgroup initialization at system boot
4693 *
4694 * Initialize cgroups at system boot, and initialize any
4695 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004696 */
4697int __init cgroup_init_early(void)
4698{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004699 static struct cgroup_sb_opts __initdata opts =
4700 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004701 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004702 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004703
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004704 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004705 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004706
Tejun Heo3ed80a62014-02-08 10:36:58 -05004707 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004708 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004709 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4710 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004711 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004712 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4713 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004714
Tejun Heoaec25022014-02-08 10:36:58 -05004715 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004716 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004717
4718 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004719 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004720 }
4721 return 0;
4722}
4723
4724/**
Li Zefana043e3b2008-02-23 15:24:09 -08004725 * cgroup_init - cgroup initialization
4726 *
4727 * Register cgroup filesystem and /proc file, and initialize
4728 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004729 */
4730int __init cgroup_init(void)
4731{
Tejun Heo30159ec2013-06-25 11:53:37 -07004732 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004733 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004734 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004735
Tejun Heo2bd59d42014-02-11 11:52:49 -05004736 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004737
Tejun Heo985ed672014-03-19 10:23:53 -04004738 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004739 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004740
Tejun Heo82fe9b02013-06-25 11:53:37 -07004741 /* Add init_css_set to the hash table */
4742 key = css_set_hash(init_css_set.subsys);
4743 hash_add(css_set_table, &init_css_set.hlist, key);
4744
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004745 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004746
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004747 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004748 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004749
Tejun Heo172a2c062014-03-19 10:23:53 -04004750 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004751 if (ss->early_init) {
4752 struct cgroup_subsys_state *css =
4753 init_css_set.subsys[ss->id];
4754
4755 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4756 GFP_KERNEL);
4757 BUG_ON(css->id < 0);
4758 } else {
4759 cgroup_init_subsys(ss, false);
4760 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004761
Tejun Heo2d8f2432014-04-23 11:13:15 -04004762 list_add_tail(&init_css_set.e_cset_node[ssid],
4763 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4764
Tejun Heo172a2c062014-03-19 10:23:53 -04004765 /*
4766 * cftype registration needs kmalloc and can't be done
4767 * during early_init. Register base cftypes separately.
4768 */
4769 if (ss->base_cftypes)
4770 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4771 }
Greg KH676db4a2010-08-05 13:53:35 -07004772
Paul Menageddbcc7e2007-10-18 23:39:30 -07004773 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004774 if (!cgroup_kobj)
4775 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004776
4777 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004778 if (err < 0) {
4779 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004780 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004781 }
4782
4783 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004784 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004785}
Paul Menageb4f48b62007-10-18 23:39:33 -07004786
Tejun Heoe5fca242013-11-22 17:14:39 -05004787static int __init cgroup_wq_init(void)
4788{
4789 /*
4790 * There isn't much point in executing destruction path in
4791 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004792 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004793 *
4794 * We would prefer to do this in cgroup_init() above, but that
4795 * is called before init_workqueues(): so leave this until after.
4796 */
Tejun Heo1a115332014-02-12 19:06:19 -05004797 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004798 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004799
4800 /*
4801 * Used to destroy pidlists and separate to serve as flush domain.
4802 * Cap @max_active to 1 too.
4803 */
4804 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4805 0, 1);
4806 BUG_ON(!cgroup_pidlist_destroy_wq);
4807
Tejun Heoe5fca242013-11-22 17:14:39 -05004808 return 0;
4809}
4810core_initcall(cgroup_wq_init);
4811
Paul Menagea4243162007-10-18 23:39:35 -07004812/*
4813 * proc_cgroup_show()
4814 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4815 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004816 */
4817
4818/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004819int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004820{
4821 struct pid *pid;
4822 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004823 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004824 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004825 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004826
4827 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004828 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004829 if (!buf)
4830 goto out;
4831
4832 retval = -ESRCH;
4833 pid = m->private;
4834 tsk = get_pid_task(pid, PIDTYPE_PID);
4835 if (!tsk)
4836 goto out_free;
4837
4838 retval = 0;
4839
4840 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004841 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004842
Tejun Heo985ed672014-03-19 10:23:53 -04004843 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004844 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004845 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004846 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004847
Tejun Heoa2dd4242014-03-19 10:23:55 -04004848 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004849 continue;
4850
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004851 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004852 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004853 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004854 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004855 if (strlen(root->name))
4856 seq_printf(m, "%sname=%s", count ? "," : "",
4857 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004858 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004859 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004860 path = cgroup_path(cgrp, buf, PATH_MAX);
4861 if (!path) {
4862 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004863 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004864 }
4865 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004866 seq_putc(m, '\n');
4867 }
4868
4869out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004870 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004871 mutex_unlock(&cgroup_mutex);
4872 put_task_struct(tsk);
4873out_free:
4874 kfree(buf);
4875out:
4876 return retval;
4877}
4878
Paul Menagea4243162007-10-18 23:39:35 -07004879/* Display information about each subsystem and each hierarchy */
4880static int proc_cgroupstats_show(struct seq_file *m, void *v)
4881{
Tejun Heo30159ec2013-06-25 11:53:37 -07004882 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004883 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004884
Paul Menage8bab8dd2008-04-04 14:29:57 -07004885 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004886 /*
4887 * ideally we don't want subsystems moving around while we do this.
4888 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4889 * subsys/hierarchy state.
4890 */
Paul Menagea4243162007-10-18 23:39:35 -07004891 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004892
4893 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004894 seq_printf(m, "%s\t%d\t%d\t%d\n",
4895 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004896 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004897
Paul Menagea4243162007-10-18 23:39:35 -07004898 mutex_unlock(&cgroup_mutex);
4899 return 0;
4900}
4901
4902static int cgroupstats_open(struct inode *inode, struct file *file)
4903{
Al Viro9dce07f2008-03-29 03:07:28 +00004904 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004905}
4906
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004907static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004908 .open = cgroupstats_open,
4909 .read = seq_read,
4910 .llseek = seq_lseek,
4911 .release = single_release,
4912};
4913
Paul Menageb4f48b62007-10-18 23:39:33 -07004914/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004915 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004916 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004917 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004918 * A task is associated with the init_css_set until cgroup_post_fork()
4919 * attaches it to the parent's css_set. Empty cg_list indicates that
4920 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004921 */
4922void cgroup_fork(struct task_struct *child)
4923{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004924 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004925 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004926}
4927
4928/**
Li Zefana043e3b2008-02-23 15:24:09 -08004929 * cgroup_post_fork - called on a new task after adding it to the task list
4930 * @child: the task in question
4931 *
Tejun Heo5edee612012-10-16 15:03:14 -07004932 * Adds the task to the list running through its css_set if necessary and
4933 * call the subsystem fork() callbacks. Has to be after the task is
4934 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004935 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004936 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004937 */
Paul Menage817929e2007-10-18 23:39:36 -07004938void cgroup_post_fork(struct task_struct *child)
4939{
Tejun Heo30159ec2013-06-25 11:53:37 -07004940 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004941 int i;
4942
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004943 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004944 * This may race against cgroup_enable_task_cg_links(). As that
4945 * function sets use_task_css_set_links before grabbing
4946 * tasklist_lock and we just went through tasklist_lock to add
4947 * @child, it's guaranteed that either we see the set
4948 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4949 * @child during its iteration.
4950 *
4951 * If we won the race, @child is associated with %current's
4952 * css_set. Grabbing css_set_rwsem guarantees both that the
4953 * association is stable, and, on completion of the parent's
4954 * migration, @child is visible in the source of migration or
4955 * already in the destination cgroup. This guarantee is necessary
4956 * when implementing operations which need to migrate all tasks of
4957 * a cgroup to another.
4958 *
4959 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4960 * will remain in init_css_set. This is safe because all tasks are
4961 * in the init_css_set before cg_links is enabled and there's no
4962 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004963 */
Paul Menage817929e2007-10-18 23:39:36 -07004964 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004965 struct css_set *cset;
4966
Tejun Heo96d365e2014-02-13 06:58:40 -05004967 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004968 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004969 if (list_empty(&child->cg_list)) {
4970 rcu_assign_pointer(child->cgroups, cset);
4971 list_add(&child->cg_list, &cset->tasks);
4972 get_css_set(cset);
4973 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004974 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004975 }
Tejun Heo5edee612012-10-16 15:03:14 -07004976
4977 /*
4978 * Call ss->fork(). This must happen after @child is linked on
4979 * css_set; otherwise, @child might change state between ->fork()
4980 * and addition to css_set.
4981 */
4982 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004983 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004984 if (ss->fork)
4985 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004986 }
Paul Menage817929e2007-10-18 23:39:36 -07004987}
Tejun Heo5edee612012-10-16 15:03:14 -07004988
Paul Menage817929e2007-10-18 23:39:36 -07004989/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004990 * cgroup_exit - detach cgroup from exiting task
4991 * @tsk: pointer to task_struct of exiting process
4992 *
4993 * Description: Detach cgroup from @tsk and release it.
4994 *
4995 * Note that cgroups marked notify_on_release force every task in
4996 * them to take the global cgroup_mutex mutex when exiting.
4997 * This could impact scaling on very large systems. Be reluctant to
4998 * use notify_on_release cgroups where very high task exit scaling
4999 * is required on large systems.
5000 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005001 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5002 * call cgroup_exit() while the task is still competent to handle
5003 * notify_on_release(), then leave the task attached to the root cgroup in
5004 * each hierarchy for the remainder of its exit. No need to bother with
5005 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005006 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005007 */
Li Zefan1ec41832014-03-28 15:22:19 +08005008void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005009{
Tejun Heo30159ec2013-06-25 11:53:37 -07005010 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005011 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005012 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005013 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005014
5015 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005016 * Unlink from @tsk from its css_set. As migration path can't race
5017 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005018 */
5019 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005020 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005021 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005022 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005023 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005024 }
5025
Paul Menageb4f48b62007-10-18 23:39:33 -07005026 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005027 cset = task_css_set(tsk);
5028 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005029
Li Zefan1ec41832014-03-28 15:22:19 +08005030 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005031 /* see cgroup_post_fork() for details */
5032 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005033 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005034 struct cgroup_subsys_state *old_css = cset->subsys[i];
5035 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005036
Tejun Heoeb954192013-08-08 20:11:23 -04005037 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005038 }
5039 }
5040 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005041
Tejun Heoeaf797a2014-02-25 10:04:03 -05005042 if (put_cset)
5043 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005044}
Paul Menage697f4162007-10-18 23:39:34 -07005045
Paul Menagebd89aab2007-10-18 23:40:44 -07005046static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047{
Li Zefanf50daa72013-03-01 15:06:07 +08005048 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005049 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005050 /*
5051 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005052 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005053 * it now
5054 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005055 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005056
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005057 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005058 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005059 list_empty(&cgrp->release_list)) {
5060 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005061 need_schedule_work = 1;
5062 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005063 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064 if (need_schedule_work)
5065 schedule_work(&release_agent_work);
5066 }
5067}
5068
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069/*
5070 * Notify userspace when a cgroup is released, by running the
5071 * configured release agent with the name of the cgroup (path
5072 * relative to the root of cgroup file system) as the argument.
5073 *
5074 * Most likely, this user command will try to rmdir this cgroup.
5075 *
5076 * This races with the possibility that some other task will be
5077 * attached to this cgroup before it is removed, or that some other
5078 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5079 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5080 * unused, and this cgroup will be reprieved from its death sentence,
5081 * to continue to serve a useful existence. Next time it's released,
5082 * we will get notified again, if it still has 'notify_on_release' set.
5083 *
5084 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5085 * means only wait until the task is successfully execve()'d. The
5086 * separate release agent task is forked by call_usermodehelper(),
5087 * then control in this thread returns here, without waiting for the
5088 * release agent task. We don't bother to wait because the caller of
5089 * this routine has no use for the exit status of the release agent
5090 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005091 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092static void cgroup_release_agent(struct work_struct *work)
5093{
5094 BUG_ON(work != &release_agent_work);
5095 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005096 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005097 while (!list_empty(&release_list)) {
5098 char *argv[3], *envp[3];
5099 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005100 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005101 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005102 struct cgroup,
5103 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005104 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005105 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005106 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005107 if (!pathbuf)
5108 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005109 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5110 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005111 goto continue_free;
5112 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5113 if (!agentbuf)
5114 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005115
5116 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005117 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005118 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005119 argv[i] = NULL;
5120
5121 i = 0;
5122 /* minimal command environment */
5123 envp[i++] = "HOME=/";
5124 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5125 envp[i] = NULL;
5126
5127 /* Drop the lock while we invoke the usermode helper,
5128 * since the exec could involve hitting disk and hence
5129 * be a slow process */
5130 mutex_unlock(&cgroup_mutex);
5131 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005132 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005133 continue_free:
5134 kfree(pathbuf);
5135 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005136 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005137 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005138 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005139 mutex_unlock(&cgroup_mutex);
5140}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005141
5142static int __init cgroup_disable(char *str)
5143{
Tejun Heo30159ec2013-06-25 11:53:37 -07005144 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005145 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005146 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005147
5148 while ((token = strsep(&str, ",")) != NULL) {
5149 if (!*token)
5150 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005151
Tejun Heo3ed80a62014-02-08 10:36:58 -05005152 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005153 if (!strcmp(token, ss->name)) {
5154 ss->disabled = 1;
5155 printk(KERN_INFO "Disabling %s control group"
5156 " subsystem\n", ss->name);
5157 break;
5158 }
5159 }
5160 }
5161 return 1;
5162}
5163__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005164
Tejun Heob77d7b62013-08-13 11:01:54 -04005165/**
Tejun Heoec903c02014-05-13 12:11:01 -04005166 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005167 * @dentry: directory dentry of interest
5168 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005169 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005170 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5171 * to get the corresponding css and return it. If such css doesn't exist
5172 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005173 */
Tejun Heoec903c02014-05-13 12:11:01 -04005174struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5175 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005176{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005177 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5178 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005179 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005180
Tejun Heo35cf0832013-08-26 18:40:56 -04005181 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005182 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5183 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005184 return ERR_PTR(-EBADF);
5185
Tejun Heo5a17f542014-02-11 11:52:47 -05005186 rcu_read_lock();
5187
Tejun Heo2bd59d42014-02-11 11:52:49 -05005188 /*
5189 * This path doesn't originate from kernfs and @kn could already
5190 * have been or be removed at any point. @kn->priv is RCU
Tejun Heocfc79d52014-05-13 12:19:22 -04005191 * protected for this access. See cgroup_rmdir() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005192 */
5193 cgrp = rcu_dereference(kn->priv);
5194 if (cgrp)
5195 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005196
Tejun Heoec903c02014-05-13 12:11:01 -04005197 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005198 css = ERR_PTR(-ENOENT);
5199
5200 rcu_read_unlock();
5201 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005202}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005203
Li Zefan1cb650b2013-08-19 10:05:24 +08005204/**
5205 * css_from_id - lookup css by id
5206 * @id: the cgroup id
5207 * @ss: cgroup subsys to be looked into
5208 *
5209 * Returns the css if there's valid one with @id, otherwise returns NULL.
5210 * Should be called under rcu_read_lock().
5211 */
5212struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5213{
Tejun Heo6fa49182014-05-04 15:09:13 -04005214 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005215 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005216}
5217
Paul Menagefe693432009-09-23 15:56:20 -07005218#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005219static struct cgroup_subsys_state *
5220debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005221{
5222 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5223
5224 if (!css)
5225 return ERR_PTR(-ENOMEM);
5226
5227 return css;
5228}
5229
Tejun Heoeb954192013-08-08 20:11:23 -04005230static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005231{
Tejun Heoeb954192013-08-08 20:11:23 -04005232 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005233}
5234
Tejun Heo182446d2013-08-08 20:11:24 -04005235static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5236 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005237{
Tejun Heo182446d2013-08-08 20:11:24 -04005238 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005239}
5240
Tejun Heo182446d2013-08-08 20:11:24 -04005241static u64 current_css_set_read(struct cgroup_subsys_state *css,
5242 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005243{
5244 return (u64)(unsigned long)current->cgroups;
5245}
5246
Tejun Heo182446d2013-08-08 20:11:24 -04005247static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005248 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005249{
5250 u64 count;
5251
5252 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005253 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005254 rcu_read_unlock();
5255 return count;
5256}
5257
Tejun Heo2da8ca82013-12-05 12:28:04 -05005258static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005259{
Tejun Heo69d02062013-06-12 21:04:50 -07005260 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005261 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005262 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005263
Tejun Heoe61734c2014-02-12 09:29:50 -05005264 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5265 if (!name_buf)
5266 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005267
Tejun Heo96d365e2014-02-13 06:58:40 -05005268 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005269 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005270 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005271 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005272 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005273
Tejun Heoa2dd4242014-03-19 10:23:55 -04005274 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005275 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005276 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005277 }
5278 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005279 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005280 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005281 return 0;
5282}
5283
5284#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005285static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005286{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005287 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005288 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005289
Tejun Heo96d365e2014-02-13 06:58:40 -05005290 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005291 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005292 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005293 struct task_struct *task;
5294 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005295
Tejun Heo5abb8852013-06-12 21:04:49 -07005296 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005297
Tejun Heo5abb8852013-06-12 21:04:49 -07005298 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005299 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5300 goto overflow;
5301 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005302 }
Tejun Heoc7561122014-02-25 10:04:01 -05005303
5304 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5305 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5306 goto overflow;
5307 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5308 }
5309 continue;
5310 overflow:
5311 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005312 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005313 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005314 return 0;
5315}
5316
Tejun Heo182446d2013-08-08 20:11:24 -04005317static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005318{
Tejun Heo182446d2013-08-08 20:11:24 -04005319 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005320}
5321
5322static struct cftype debug_files[] = {
5323 {
Paul Menagefe693432009-09-23 15:56:20 -07005324 .name = "taskcount",
5325 .read_u64 = debug_taskcount_read,
5326 },
5327
5328 {
5329 .name = "current_css_set",
5330 .read_u64 = current_css_set_read,
5331 },
5332
5333 {
5334 .name = "current_css_set_refcount",
5335 .read_u64 = current_css_set_refcount_read,
5336 },
5337
5338 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005339 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005340 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005341 },
5342
5343 {
5344 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005345 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005346 },
5347
5348 {
Paul Menagefe693432009-09-23 15:56:20 -07005349 .name = "releasable",
5350 .read_u64 = releasable_read,
5351 },
Paul Menagefe693432009-09-23 15:56:20 -07005352
Tejun Heo4baf6e32012-04-01 12:09:55 -07005353 { } /* terminate */
5354};
Paul Menagefe693432009-09-23 15:56:20 -07005355
Tejun Heo073219e2014-02-08 10:36:58 -05005356struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005357 .css_alloc = debug_css_alloc,
5358 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005359 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005360};
5361#endif /* CONFIG_CGROUP_DEBUG */