blob: 2c9eae6ad9704d3278557f6c692d2ef0027b587b [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>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mm.h>
40#include <linux/mutex.h>
41#include <linux/mount.h>
42#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070043#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/rcupdate.h>
45#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050048#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050059#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070060
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070062
Tejun Heoe25e2cb2011-12-12 18:12:21 -080063/*
Tejun Heob1a21362013-11-29 10:42:58 -050064 * pidlists linger the following amount before being destroyed. The goal
65 * is avoiding frequent destruction in the middle of consecutive read calls
66 * Expiring in the middle is a performance problem not a correctness one.
67 * 1 sec should be enough.
68 */
69#define CGROUP_PIDLIST_DESTROY_DELAY HZ
70
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050071#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
72 MAX_CFTYPE_NAME + 2)
73
Tejun Heob1a21362013-11-29 10:42:58 -050074/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080075 * cgroup_mutex is the master lock. Any modification to cgroup or its
76 * hierarchy must be performed while holding it.
77 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050078 * css_set_rwsem protects task->cgroups pointer, the list of css_set
79 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050081 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
82 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 */
Tejun Heo22194492013-04-07 09:29:51 -070084#ifdef CONFIG_PROVE_RCU
85DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050086DECLARE_RWSEM(css_set_rwsem);
87EXPORT_SYMBOL_GPL(cgroup_mutex);
88EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070089#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070090static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050091static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070092#endif
93
Tejun Heo69e943b2014-02-08 10:36:58 -050094/*
Tejun Heo15a4c832014-05-04 15:09:14 -040095 * Protects cgroup_idr and css_idr so that IDs can be released without
96 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040097 */
98static DEFINE_SPINLOCK(cgroup_idr_lock);
99
100/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500101 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
102 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
103 */
104static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700105
Tejun Heo8353da12014-05-13 12:19:23 -0400106#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700107 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
108 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400109 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500110
Ben Blumaae8aab2010-03-10 15:22:07 -0800111/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500112 * cgroup destruction makes heavy use of work items and there can be a lot
113 * of concurrent destructions. Use a separate workqueue so that cgroup
114 * destruction work items don't end up filling up max_active of system_wq
115 * which may lead to deadlock.
116 */
117static struct workqueue_struct *cgroup_destroy_wq;
118
119/*
Tejun Heob1a21362013-11-29 10:42:58 -0500120 * pidlist destructions need to be flushed on cgroup destruction. Use a
121 * separate workqueue as flush domain.
122 */
123static struct workqueue_struct *cgroup_pidlist_destroy_wq;
124
Tejun Heo3ed80a62014-02-08 10:36:58 -0500125/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500126#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500127static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128#include <linux/cgroup_subsys.h>
129};
Tejun Heo073219e2014-02-08 10:36:58 -0500130#undef SUBSYS
131
132/* array of cgroup subsystem names */
133#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
134static const char *cgroup_subsys_name[] = {
135#include <linux/cgroup_subsys.h>
136};
137#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400140 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700141 * unattached - it never has more than a single cgroup, and all tasks are
142 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700143 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400144struct cgroup_root cgrp_dfl_root;
Tejun Heod0ec4232015-08-05 16:03:19 -0400145EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700146
Tejun Heoa2dd4242014-03-19 10:23:55 -0400147/*
148 * The default hierarchy always exists but is hidden until mounted for the
149 * first time. This is for backward compatibility.
150 */
151static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152
Tejun Heoa8ddc822014-07-15 11:05:10 -0400153/*
154 * Set by the boot param of the same name and makes subsystems with NULL
155 * ->dfl_files to use ->legacy_files on the default hierarchy.
156 */
157static bool cgroup_legacy_files_on_dfl;
158
Tejun Heo5533e012014-05-14 19:33:07 -0400159/* some controllers are not supported in the default hierarchy */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000160static unsigned long cgrp_dfl_root_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400161
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162/* The list of hierarchy roots */
163
Tejun Heo9871bf92013-06-24 15:21:47 -0700164static LIST_HEAD(cgroup_roots);
165static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166
Tejun Heo3417ae12014-02-08 10:37:01 -0500167/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700168static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700169
Li Zefan794611a2013-06-18 18:53:53 +0800170/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400171 * Assign a monotonically increasing serial number to csses. It guarantees
172 * cgroups with bigger numbers are newer than those with smaller numbers.
173 * Also, as csses are always appended to the parent's ->children list, it
174 * guarantees that sibling csses are always sorted in the ascending serial
175 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800176 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400177static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800178
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000179/*
180 * These bitmask flags indicate whether tasks in the fork and exit paths have
181 * fork/exit handlers to call. This avoids us having to do extra work in the
182 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000184static unsigned long have_fork_callback __read_mostly;
185static unsigned long have_exit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186
Aleksa Sarai7e476822015-06-09 21:32:09 +1000187/* Ditto for the can_fork callback. */
188static unsigned long have_canfork_callback __read_mostly;
189
Tejun Heoa14c6872014-07-15 11:05:09 -0400190static struct cftype cgroup_dfl_base_files[];
191static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700192
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400193static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000194 unsigned long ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800195static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400196static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
197 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400198static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400199static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400200static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
201 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800202
Tejun Heo6fa49182014-05-04 15:09:13 -0400203/* IDR wrappers which synchronize using cgroup_idr_lock */
204static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
205 gfp_t gfp_mask)
206{
207 int ret;
208
209 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400210 spin_lock_bh(&cgroup_idr_lock);
Vladimir Davydovcf780b72015-08-03 15:32:26 +0300211 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_WAIT);
Tejun Heo54504e92014-05-13 12:10:59 -0400212 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400213 idr_preload_end();
214 return ret;
215}
216
217static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
218{
219 void *ret;
220
Tejun Heo54504e92014-05-13 12:10:59 -0400221 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400222 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400223 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400224 return ret;
225}
226
227static void cgroup_idr_remove(struct idr *idr, int id)
228{
Tejun Heo54504e92014-05-13 12:10:59 -0400229 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400230 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400231 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400232}
233
Tejun Heod51f39b2014-05-16 13:22:48 -0400234static struct cgroup *cgroup_parent(struct cgroup *cgrp)
235{
236 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
237
238 if (parent_css)
239 return container_of(parent_css, struct cgroup, self);
240 return NULL;
241}
242
Tejun Heo95109b62013-08-08 20:11:27 -0400243/**
244 * cgroup_css - obtain a cgroup's css for the specified subsystem
245 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400246 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400247 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400248 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
249 * function must be called either under cgroup_mutex or rcu_read_lock() and
250 * the caller is responsible for pinning the returned css if it wants to
251 * keep accessing it outside the said locks. This function may return
252 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400253 */
254static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400255 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400256{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400257 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500258 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500259 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400260 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400261 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400262}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700263
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400264/**
265 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
266 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400267 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400268 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400269 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400270 * as the matching css of the nearest ancestor including self which has @ss
271 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
272 * function is guaranteed to return non-NULL css.
273 */
274static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
275 struct cgroup_subsys *ss)
276{
277 lockdep_assert_held(&cgroup_mutex);
278
279 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400280 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400281
282 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
283 return NULL;
284
Tejun Heoeeecbd12014-11-18 02:49:52 -0500285 /*
286 * This function is used while updating css associations and thus
287 * can't test the csses directly. Use ->child_subsys_mask.
288 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400289 while (cgroup_parent(cgrp) &&
290 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
291 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400292
293 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700294}
295
Tejun Heoeeecbd12014-11-18 02:49:52 -0500296/**
297 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
298 * @cgrp: the cgroup of interest
299 * @ss: the subsystem of interest
300 *
301 * Find and get the effective css of @cgrp for @ss. The effective css is
302 * defined as the matching css of the nearest ancestor including self which
303 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
304 * the root css is returned, so this function always returns a valid css.
305 * The returned css must be put using css_put().
306 */
307struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
308 struct cgroup_subsys *ss)
309{
310 struct cgroup_subsys_state *css;
311
312 rcu_read_lock();
313
314 do {
315 css = cgroup_css(cgrp, ss);
316
317 if (css && css_tryget_online(css))
318 goto out_unlock;
319 cgrp = cgroup_parent(cgrp);
320 } while (cgrp);
321
322 css = init_css_set.subsys[ss->id];
323 css_get(css);
324out_unlock:
325 rcu_read_unlock();
326 return css;
327}
328
Paul Menageddbcc7e2007-10-18 23:39:30 -0700329/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700330static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700331{
Tejun Heo184faf32014-05-16 13:22:51 -0400332 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700333}
334
Tejun Heob4168642014-05-13 12:16:21 -0400335struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500336{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500337 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400338 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500339
340 /*
341 * This is open and unprotected implementation of cgroup_css().
342 * seq_css() is only called from a kernfs file operation which has
343 * an active reference on the file. Because all the subsystem
344 * files are drained before a css is disassociated with a cgroup,
345 * the matching css from the cgroup's subsys table is guaranteed to
346 * be and stay valid until the enclosing operation is complete.
347 */
348 if (cft->ss)
349 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
350 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400351 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500352}
Tejun Heob4168642014-05-13 12:16:21 -0400353EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500354
Li Zefan78574cf2013-04-08 19:00:38 -0700355/**
356 * cgroup_is_descendant - test ancestry
357 * @cgrp: the cgroup to be tested
358 * @ancestor: possible ancestor of @cgrp
359 *
360 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
361 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
362 * and @ancestor are accessible.
363 */
364bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
365{
366 while (cgrp) {
367 if (cgrp == ancestor)
368 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400369 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700370 }
371 return false;
372}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700373
Adrian Bunke9685a02008-02-07 00:13:46 -0800374static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700375{
Paul Menagebd89aab2007-10-18 23:40:44 -0700376 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700377}
378
Tejun Heo30159ec2013-06-25 11:53:37 -0700379/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500380 * for_each_css - iterate all css's of a cgroup
381 * @css: the iteration cursor
382 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
383 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700384 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400385 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700386 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500387#define for_each_css(css, ssid, cgrp) \
388 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
389 if (!((css) = rcu_dereference_check( \
390 (cgrp)->subsys[(ssid)], \
391 lockdep_is_held(&cgroup_mutex)))) { } \
392 else
393
394/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400395 * for_each_e_css - iterate all effective css's of a cgroup
396 * @css: the iteration cursor
397 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
398 * @cgrp: the target cgroup to iterate css's of
399 *
400 * Should be called under cgroup_[tree_]mutex.
401 */
402#define for_each_e_css(css, ssid, cgrp) \
403 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
404 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
405 ; \
406 else
407
408/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500409 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700410 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500411 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700412 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500413#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500414 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
415 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700416
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000417/**
418 * for_each_subsys_which - filter for_each_subsys with a bitmask
419 * @ss: the iteration cursor
420 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
421 * @ss_maskp: a pointer to the bitmask
422 *
423 * The block will only run for cases where the ssid-th bit (1 << ssid) of
424 * mask is set to 1.
425 */
426#define for_each_subsys_which(ss, ssid, ss_maskp) \
427 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000428 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000429 else \
430 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
431 if (((ss) = cgroup_subsys[ssid]) && false) \
432 break; \
433 else
434
Tejun Heo985ed672014-03-19 10:23:53 -0400435/* iterate across the hierarchies */
436#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700437 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700438
Tejun Heof8f22e52014-04-23 11:13:16 -0400439/* iterate over child cgrps, lock should be held throughout iteration */
440#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400441 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400442 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400443 cgroup_is_dead(child); })) \
444 ; \
445 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700446
Paul Menage81a6a5c2007-10-18 23:39:38 -0700447static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700448static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700449
Tejun Heo69d02062013-06-12 21:04:50 -0700450/*
451 * A cgroup can be associated with multiple css_sets as different tasks may
452 * belong to different cgroups on different hierarchies. In the other
453 * direction, a css_set is naturally associated with multiple cgroups.
454 * This M:N relationship is represented by the following link structure
455 * which exists for each association and allows traversing the associations
456 * from both sides.
457 */
458struct cgrp_cset_link {
459 /* the cgroup and css_set this link associates */
460 struct cgroup *cgrp;
461 struct css_set *cset;
462
463 /* list of cgrp_cset_links anchored at cgrp->cset_links */
464 struct list_head cset_link;
465
466 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
467 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700468};
469
Tejun Heo172a2c062014-03-19 10:23:53 -0400470/*
471 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700472 * hierarchies being mounted. It contains a pointer to the root state
473 * for each subsystem. Also used to anchor the list of css_sets. Not
474 * reference-counted, to improve performance when child cgroups
475 * haven't been created.
476 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400477struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400478 .refcount = ATOMIC_INIT(1),
479 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
480 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
481 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
482 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
483 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
484};
Paul Menage817929e2007-10-18 23:39:36 -0700485
Tejun Heo172a2c062014-03-19 10:23:53 -0400486static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700487
Tejun Heo842b5972014-04-25 18:28:02 -0400488/**
489 * cgroup_update_populated - updated populated count of a cgroup
490 * @cgrp: the target cgroup
491 * @populated: inc or dec populated count
492 *
493 * @cgrp is either getting the first task (css_set) or losing the last.
494 * Update @cgrp->populated_cnt accordingly. The count is propagated
495 * towards root so that a given cgroup's populated_cnt is zero iff the
496 * cgroup and all its descendants are empty.
497 *
498 * @cgrp's interface file "cgroup.populated" is zero if
499 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
500 * changes from or to zero, userland is notified that the content of the
501 * interface file has changed. This can be used to detect when @cgrp and
502 * its descendants become populated or empty.
503 */
504static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
505{
506 lockdep_assert_held(&css_set_rwsem);
507
508 do {
509 bool trigger;
510
511 if (populated)
512 trigger = !cgrp->populated_cnt++;
513 else
514 trigger = !--cgrp->populated_cnt;
515
516 if (!trigger)
517 break;
518
519 if (cgrp->populated_kn)
520 kernfs_notify(cgrp->populated_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400521 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400522 } while (cgrp);
523}
524
Paul Menage7717f7b2009-09-23 15:56:22 -0700525/*
526 * hash table for cgroup groups. This improves the performance to find
527 * an existing css_set. This hash doesn't (currently) take into
528 * account cgroups in empty hierarchies.
529 */
Li Zefan472b1052008-04-29 01:00:11 -0700530#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800531static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700532
Li Zefan0ac801f2013-01-10 11:49:27 +0800533static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700534{
Li Zefan0ac801f2013-01-10 11:49:27 +0800535 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700536 struct cgroup_subsys *ss;
537 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700538
Tejun Heo30159ec2013-06-25 11:53:37 -0700539 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800540 key += (unsigned long)css[i];
541 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700542
Li Zefan0ac801f2013-01-10 11:49:27 +0800543 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700544}
545
Zefan Lia25eb522014-09-19 16:51:00 +0800546static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700547{
Tejun Heo69d02062013-06-12 21:04:50 -0700548 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400549 struct cgroup_subsys *ss;
550 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700551
Tejun Heo89c55092014-02-13 06:58:40 -0500552 lockdep_assert_held(&css_set_rwsem);
553
554 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700555 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700556
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700557 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400558 for_each_subsys(ss, ssid)
559 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700560 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700561 css_set_count--;
562
Tejun Heo69d02062013-06-12 21:04:50 -0700563 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700564 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700565
Tejun Heo69d02062013-06-12 21:04:50 -0700566 list_del(&link->cset_link);
567 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800568
Tejun Heo96d365e2014-02-13 06:58:40 -0500569 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400570 if (list_empty(&cgrp->cset_links)) {
571 cgroup_update_populated(cgrp, false);
Zefan Lia25eb522014-09-19 16:51:00 +0800572 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700573 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700574
575 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700576 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700577
Tejun Heo5abb8852013-06-12 21:04:49 -0700578 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700579}
580
Zefan Lia25eb522014-09-19 16:51:00 +0800581static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500582{
583 /*
584 * Ensure that the refcount doesn't hit zero while any readers
585 * can see it. Similar to atomic_dec_and_lock(), but for an
586 * rwlock
587 */
588 if (atomic_add_unless(&cset->refcount, -1, 1))
589 return;
590
591 down_write(&css_set_rwsem);
Zefan Lia25eb522014-09-19 16:51:00 +0800592 put_css_set_locked(cset);
Tejun Heo89c55092014-02-13 06:58:40 -0500593 up_write(&css_set_rwsem);
594}
595
Paul Menage817929e2007-10-18 23:39:36 -0700596/*
597 * refcounted get/put for css_set objects
598 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700599static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700600{
Tejun Heo5abb8852013-06-12 21:04:49 -0700601 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700602}
603
Tejun Heob326f9d2013-06-24 15:21:48 -0700604/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700605 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700606 * @cset: candidate css_set being tested
607 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700608 * @new_cgrp: cgroup that's being entered by the task
609 * @template: desired set of css pointers in css_set (pre-calculated)
610 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800611 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700612 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
613 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700614static bool compare_css_sets(struct css_set *cset,
615 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700616 struct cgroup *new_cgrp,
617 struct cgroup_subsys_state *template[])
618{
619 struct list_head *l1, *l2;
620
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400621 /*
622 * On the default hierarchy, there can be csets which are
623 * associated with the same set of cgroups but different csses.
624 * Let's first ensure that csses match.
625 */
626 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700627 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700628
629 /*
630 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400631 * different cgroups in hierarchies. As different cgroups may
632 * share the same effective css, this comparison is always
633 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700634 */
Tejun Heo69d02062013-06-12 21:04:50 -0700635 l1 = &cset->cgrp_links;
636 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700637 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700638 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700640
641 l1 = l1->next;
642 l2 = l2->next;
643 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700644 if (l1 == &cset->cgrp_links) {
645 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700646 break;
647 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700648 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700649 }
650 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700651 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
652 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
653 cgrp1 = link1->cgrp;
654 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700655 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700657
658 /*
659 * If this hierarchy is the hierarchy of the cgroup
660 * that's changing, then we need to check that this
661 * css_set points to the new cgroup; if it's any other
662 * hierarchy, then this css_set should point to the
663 * same cgroup as the old css_set.
664 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 if (cgrp1->root == new_cgrp->root) {
666 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700667 return false;
668 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700669 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 return false;
671 }
672 }
673 return true;
674}
675
Tejun Heob326f9d2013-06-24 15:21:48 -0700676/**
677 * find_existing_css_set - init css array and find the matching css_set
678 * @old_cset: the css_set that we're using before the cgroup transition
679 * @cgrp: the cgroup that we're moving into
680 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700681 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700682static struct css_set *find_existing_css_set(struct css_set *old_cset,
683 struct cgroup *cgrp,
684 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700685{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400686 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700687 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800689 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700690 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700691
Ben Blumaae8aab2010-03-10 15:22:07 -0800692 /*
693 * Build the set of subsystem state objects that we want to see in the
694 * new css_set. while subsystems can change globally, the entries here
695 * won't change, so no need for locking.
696 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700697 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400698 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400699 /*
700 * @ss is in this hierarchy, so we want the
701 * effective css from @cgrp.
702 */
703 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700704 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400705 /*
706 * @ss is not in this hierarchy, so we don't want
707 * to change the css.
708 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700710 }
711 }
712
Li Zefan0ac801f2013-01-10 11:49:27 +0800713 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700714 hash_for_each_possible(css_set_table, cset, hlist, key) {
715 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 continue;
717
718 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700720 }
Paul Menage817929e2007-10-18 23:39:36 -0700721
722 /* No existing cgroup group matched */
723 return NULL;
724}
725
Tejun Heo69d02062013-06-12 21:04:50 -0700726static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700727{
Tejun Heo69d02062013-06-12 21:04:50 -0700728 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700729
Tejun Heo69d02062013-06-12 21:04:50 -0700730 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
731 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700732 kfree(link);
733 }
734}
735
Tejun Heo69d02062013-06-12 21:04:50 -0700736/**
737 * allocate_cgrp_cset_links - allocate cgrp_cset_links
738 * @count: the number of links to allocate
739 * @tmp_links: list_head the allocated links are put on
740 *
741 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
742 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700743 */
Tejun Heo69d02062013-06-12 21:04:50 -0700744static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700745{
Tejun Heo69d02062013-06-12 21:04:50 -0700746 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700747 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700748
749 INIT_LIST_HEAD(tmp_links);
750
Li Zefan36553432008-07-29 22:33:19 -0700751 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700752 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700753 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700754 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700755 return -ENOMEM;
756 }
Tejun Heo69d02062013-06-12 21:04:50 -0700757 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700758 }
759 return 0;
760}
761
Li Zefanc12f65d2009-01-07 18:07:42 -0800762/**
763 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700764 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700765 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800766 * @cgrp: the destination cgroup
767 */
Tejun Heo69d02062013-06-12 21:04:50 -0700768static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
769 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800770{
Tejun Heo69d02062013-06-12 21:04:50 -0700771 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800772
Tejun Heo69d02062013-06-12 21:04:50 -0700773 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400774
775 if (cgroup_on_dfl(cgrp))
776 cset->dfl_cgrp = cgrp;
777
Tejun Heo69d02062013-06-12 21:04:50 -0700778 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
779 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700780 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400781
782 if (list_empty(&cgrp->cset_links))
783 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700784 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400785
Paul Menage7717f7b2009-09-23 15:56:22 -0700786 /*
787 * Always add links to the tail of the list so that the list
788 * is sorted by order of hierarchy creation
789 */
Tejun Heo69d02062013-06-12 21:04:50 -0700790 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800791}
792
Tejun Heob326f9d2013-06-24 15:21:48 -0700793/**
794 * find_css_set - return a new css_set with one cgroup updated
795 * @old_cset: the baseline css_set
796 * @cgrp: the cgroup to be updated
797 *
798 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
799 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700800 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700801static struct css_set *find_css_set(struct css_set *old_cset,
802 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700803{
Tejun Heob326f9d2013-06-24 15:21:48 -0700804 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700805 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700806 struct list_head tmp_links;
807 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400808 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800809 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400810 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700811
Tejun Heob326f9d2013-06-24 15:21:48 -0700812 lockdep_assert_held(&cgroup_mutex);
813
Paul Menage817929e2007-10-18 23:39:36 -0700814 /* First see if we already have a cgroup group that matches
815 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500816 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700817 cset = find_existing_css_set(old_cset, cgrp, template);
818 if (cset)
819 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500820 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700821
Tejun Heo5abb8852013-06-12 21:04:49 -0700822 if (cset)
823 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700824
Tejun Heof4f4be22013-06-12 21:04:51 -0700825 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700826 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700827 return NULL;
828
Tejun Heo69d02062013-06-12 21:04:50 -0700829 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700830 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700831 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700832 return NULL;
833 }
834
Tejun Heo5abb8852013-06-12 21:04:49 -0700835 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700836 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700837 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500838 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500839 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500840 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700841 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700842
843 /* Copy the set of subsystem state objects generated in
844 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700845 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700846
Tejun Heo96d365e2014-02-13 06:58:40 -0500847 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700848 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700849 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700850 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700851
Paul Menage7717f7b2009-09-23 15:56:22 -0700852 if (c->root == cgrp->root)
853 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700854 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700855 }
Paul Menage817929e2007-10-18 23:39:36 -0700856
Tejun Heo69d02062013-06-12 21:04:50 -0700857 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700858
Paul Menage817929e2007-10-18 23:39:36 -0700859 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700860
Tejun Heo2d8f2432014-04-23 11:13:15 -0400861 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700862 key = css_set_hash(cset->subsys);
863 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700864
Tejun Heo2d8f2432014-04-23 11:13:15 -0400865 for_each_subsys(ss, ssid)
866 list_add_tail(&cset->e_cset_node[ssid],
867 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
868
Tejun Heo96d365e2014-02-13 06:58:40 -0500869 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700870
Tejun Heo5abb8852013-06-12 21:04:49 -0700871 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700872}
873
Tejun Heo0c986252015-09-16 11:51:12 -0400874void cgroup_threadgroup_change_begin(struct task_struct *tsk)
875{
876 down_read(&tsk->signal->group_rwsem);
877}
878
879void cgroup_threadgroup_change_end(struct task_struct *tsk)
880{
881 up_read(&tsk->signal->group_rwsem);
882}
883
884/**
885 * threadgroup_lock - lock threadgroup
886 * @tsk: member task of the threadgroup to lock
887 *
888 * Lock the threadgroup @tsk belongs to. No new task is allowed to enter
889 * and member tasks aren't allowed to exit (as indicated by PF_EXITING) or
890 * change ->group_leader/pid. This is useful for cases where the threadgroup
891 * needs to stay stable across blockable operations.
892 *
893 * fork and exit explicitly call threadgroup_change_{begin|end}() for
894 * synchronization. While held, no new task will be added to threadgroup
895 * and no existing live task will have its PF_EXITING set.
896 *
897 * de_thread() does threadgroup_change_{begin|end}() when a non-leader
898 * sub-thread becomes a new leader.
899 */
900static void threadgroup_lock(struct task_struct *tsk)
901{
902 down_write(&tsk->signal->group_rwsem);
903}
904
905/**
906 * threadgroup_unlock - unlock threadgroup
907 * @tsk: member task of the threadgroup to unlock
908 *
909 * Reverse threadgroup_lock().
910 */
911static inline void threadgroup_unlock(struct task_struct *tsk)
912{
913 up_write(&tsk->signal->group_rwsem);
914}
915
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400916static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700917{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400918 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500919
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400920 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500921}
922
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400923static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500924{
925 int id;
926
927 lockdep_assert_held(&cgroup_mutex);
928
Tejun Heo985ed672014-03-19 10:23:53 -0400929 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500930 if (id < 0)
931 return id;
932
933 root->hierarchy_id = id;
934 return 0;
935}
936
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400937static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500938{
939 lockdep_assert_held(&cgroup_mutex);
940
941 if (root->hierarchy_id) {
942 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
943 root->hierarchy_id = 0;
944 }
945}
946
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400947static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500948{
949 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400950 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -0500951 WARN_ON_ONCE(root->hierarchy_id);
952
953 idr_destroy(&root->cgroup_idr);
954 kfree(root);
955 }
956}
957
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400958static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500959{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400960 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500961 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500962
Tejun Heo2bd59d42014-02-11 11:52:49 -0500963 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500964
Tejun Heo776f02f2014-02-12 09:29:50 -0500965 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -0400966 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -0500967
Tejun Heof2e85d52014-02-11 11:52:49 -0500968 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400969 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500970
971 /*
972 * Release all the links from cset_links to this hierarchy's
973 * root cgroup
974 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500975 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500976
977 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
978 list_del(&link->cset_link);
979 list_del(&link->cgrp_link);
980 kfree(link);
981 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500982 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500983
984 if (!list_empty(&root->root_list)) {
985 list_del(&root->root_list);
986 cgroup_root_count--;
987 }
988
989 cgroup_exit_root_id(root);
990
991 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500992
Tejun Heo2bd59d42014-02-11 11:52:49 -0500993 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500994 cgroup_free_root(root);
995}
996
Tejun Heoceb6a082014-02-25 10:04:02 -0500997/* look up cgroup associated with given css_set on the specified hierarchy */
998static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400999 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001000{
Paul Menage7717f7b2009-09-23 15:56:22 -07001001 struct cgroup *res = NULL;
1002
Tejun Heo96d365e2014-02-13 06:58:40 -05001003 lockdep_assert_held(&cgroup_mutex);
1004 lockdep_assert_held(&css_set_rwsem);
1005
Tejun Heo5abb8852013-06-12 21:04:49 -07001006 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001007 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001008 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001009 struct cgrp_cset_link *link;
1010
1011 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001012 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001013
Paul Menage7717f7b2009-09-23 15:56:22 -07001014 if (c->root == root) {
1015 res = c;
1016 break;
1017 }
1018 }
1019 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001020
Paul Menage7717f7b2009-09-23 15:56:22 -07001021 BUG_ON(!res);
1022 return res;
1023}
1024
1025/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001026 * Return the cgroup for "task" from the given hierarchy. Must be
1027 * called with cgroup_mutex and css_set_rwsem held.
1028 */
1029static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001030 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001031{
1032 /*
1033 * No need to lock the task - since we hold cgroup_mutex the
1034 * task can't change groups, so the only thing that can happen
1035 * is that it exits and its css is set back to init_css_set.
1036 */
1037 return cset_cgroup_from_root(task_css_set(task), root);
1038}
1039
1040/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 * A task must hold cgroup_mutex to modify cgroups.
1042 *
1043 * Any task can increment and decrement the count field without lock.
1044 * So in general, code holding cgroup_mutex can't rely on the count
1045 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001046 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 * means that no tasks are currently attached, therefore there is no
1048 * way a task attached to that cgroup can fork (the other way to
1049 * increment the count). So code holding cgroup_mutex can safely
1050 * assume that if the count is zero, it will stay zero. Similarly, if
1051 * a task holds cgroup_mutex on a cgroup with zero count, it
1052 * knows that the cgroup won't be removed, as cgroup_rmdir()
1053 * needs that mutex.
1054 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 * A cgroup can only be deleted if both its 'count' of using tasks
1056 * is zero, and its list of 'children' cgroups is empty. Since all
1057 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001058 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001060 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 *
1062 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001063 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 */
1065
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001066static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001067static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001068static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001069
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001070static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1071 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001073 struct cgroup_subsys *ss = cft->ss;
1074
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001075 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1076 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1077 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001078 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1079 cft->name);
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001080 else
1081 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1082 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083}
1084
Tejun Heof2e85d52014-02-11 11:52:49 -05001085/**
1086 * cgroup_file_mode - deduce file mode of a control file
1087 * @cft: the control file in question
1088 *
1089 * returns cft->mode if ->mode is not 0
1090 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1091 * returns S_IRUGO if it has only a read handler
1092 * returns S_IWUSR if it has only a write hander
1093 */
1094static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001095{
Tejun Heof2e85d52014-02-11 11:52:49 -05001096 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001097
Tejun Heof2e85d52014-02-11 11:52:49 -05001098 if (cft->mode)
1099 return cft->mode;
1100
1101 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1102 mode |= S_IRUGO;
1103
Tejun Heo6770c642014-05-13 12:16:21 -04001104 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001105 mode |= S_IWUSR;
1106
1107 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001108}
1109
Tejun Heo59f52962014-02-11 11:52:49 -05001110static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001112 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001113 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114}
1115
Li Zefanaa323622014-09-04 14:43:38 +08001116static bool cgroup_tryget(struct cgroup *cgrp)
1117{
1118 return css_tryget(&cgrp->self);
1119}
1120
Tejun Heo59f52962014-02-11 11:52:49 -05001121static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122{
Tejun Heo9d755d32014-05-14 09:15:02 -04001123 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124}
1125
Tejun Heoa9746d82014-05-13 12:19:22 -04001126/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001127 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001128 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001129 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001130 *
1131 * On the default hierarchy, a subsystem may request other subsystems to be
1132 * enabled together through its ->depends_on mask. In such cases, more
1133 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1134 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001135 * This function calculates which subsystems need to be enabled if
1136 * @subtree_control is to be applied to @cgrp. The returned mask is always
1137 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001138 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001139static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1140 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001141{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001142 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001143 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001144 struct cgroup_subsys *ss;
1145 int ssid;
1146
1147 lockdep_assert_held(&cgroup_mutex);
1148
Tejun Heo0f060de2014-11-18 02:49:50 -05001149 if (!cgroup_on_dfl(cgrp))
1150 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001151
1152 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001153 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001154
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001155 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1156 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001157
1158 /*
1159 * Mask out subsystems which aren't available. This can
1160 * happen only if some depended-upon subsystems were bound
1161 * to non-default hierarchies.
1162 */
1163 if (parent)
1164 new_ss_mask &= parent->child_subsys_mask;
1165 else
1166 new_ss_mask &= cgrp->root->subsys_mask;
1167
1168 if (new_ss_mask == cur_ss_mask)
1169 break;
1170 cur_ss_mask = new_ss_mask;
1171 }
1172
Tejun Heo0f060de2014-11-18 02:49:50 -05001173 return cur_ss_mask;
1174}
1175
1176/**
1177 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1178 * @cgrp: the target cgroup
1179 *
1180 * Update @cgrp->child_subsys_mask according to the current
1181 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1182 */
1183static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1184{
1185 cgrp->child_subsys_mask =
1186 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001187}
1188
Tejun Heoa9746d82014-05-13 12:19:22 -04001189/**
1190 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1191 * @kn: the kernfs_node being serviced
1192 *
1193 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1194 * the method finishes if locking succeeded. Note that once this function
1195 * returns the cgroup returned by cgroup_kn_lock_live() may become
1196 * inaccessible any time. If the caller intends to continue to access the
1197 * cgroup, it should pin it before invoking this function.
1198 */
1199static void cgroup_kn_unlock(struct kernfs_node *kn)
1200{
1201 struct cgroup *cgrp;
1202
1203 if (kernfs_type(kn) == KERNFS_DIR)
1204 cgrp = kn->priv;
1205 else
1206 cgrp = kn->parent->priv;
1207
1208 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001209
1210 kernfs_unbreak_active_protection(kn);
1211 cgroup_put(cgrp);
1212}
1213
1214/**
1215 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1216 * @kn: the kernfs_node being serviced
1217 *
1218 * This helper is to be used by a cgroup kernfs method currently servicing
1219 * @kn. It breaks the active protection, performs cgroup locking and
1220 * verifies that the associated cgroup is alive. Returns the cgroup if
1221 * alive; otherwise, %NULL. A successful return should be undone by a
1222 * matching cgroup_kn_unlock() invocation.
1223 *
1224 * Any cgroup kernfs method implementation which requires locking the
1225 * associated cgroup should use this helper. It avoids nesting cgroup
1226 * locking under kernfs active protection and allows all kernfs operations
1227 * including self-removal.
1228 */
1229static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1230{
1231 struct cgroup *cgrp;
1232
1233 if (kernfs_type(kn) == KERNFS_DIR)
1234 cgrp = kn->priv;
1235 else
1236 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001237
1238 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001239 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001240 * active_ref. cgroup liveliness check alone provides enough
1241 * protection against removal. Ensure @cgrp stays accessible and
1242 * break the active_ref protection.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 */
Li Zefanaa323622014-09-04 14:43:38 +08001244 if (!cgroup_tryget(cgrp))
1245 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001246 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247
Tejun Heoa9746d82014-05-13 12:19:22 -04001248 mutex_lock(&cgroup_mutex);
1249
1250 if (!cgroup_is_dead(cgrp))
1251 return cgrp;
1252
1253 cgroup_kn_unlock(kn);
1254 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255}
1256
Li Zefan2739d3c2013-01-21 18:18:33 +08001257static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001258{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001259 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001260
Tejun Heo01f64742014-05-13 12:19:23 -04001261 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001262 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001263}
1264
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001265/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001266 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001267 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001268 * @subsys_mask: mask of the subsystem ids whose files should be removed
1269 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001270static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001271{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001272 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001273 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001274
Tejun Heob420ba72013-07-12 12:34:02 -07001275 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001276 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001277
Tejun Heo69dfa002014-05-04 15:09:13 -04001278 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001279 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001280 list_for_each_entry(cfts, &ss->cfts, node)
1281 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001282 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283}
1284
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001285static int rebind_subsystems(struct cgroup_root *dst_root,
1286 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287{
Tejun Heo30159ec2013-06-25 11:53:37 -07001288 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001289 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001290 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291
Tejun Heoace2bee2014-02-11 11:52:47 -05001292 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001293
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001294 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001295 /* if @ss has non-root csses attached to it, can't move */
1296 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001297 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298
Tejun Heo5df36032014-03-19 10:23:54 -04001299 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001300 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001301 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 }
1303
Tejun Heo5533e012014-05-14 19:33:07 -04001304 /* skip creating root files on dfl_root for inhibited subsystems */
1305 tmp_ss_mask = ss_mask;
1306 if (dst_root == &cgrp_dfl_root)
1307 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1308
1309 ret = cgroup_populate_dir(&dst_root->cgrp, tmp_ss_mask);
Tejun Heoa2dd4242014-03-19 10:23:55 -04001310 if (ret) {
1311 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001312 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313
Tejun Heoa2dd4242014-03-19 10:23:55 -04001314 /*
1315 * Rebinding back to the default root is not allowed to
1316 * fail. Using both default and non-default roots should
1317 * be rare. Moving subsystems back and forth even more so.
1318 * Just warn about it and continue.
1319 */
1320 if (cgrp_dfl_root_visible) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001321 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001322 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001323 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001324 }
Tejun Heo5df36032014-03-19 10:23:54 -04001325 }
Tejun Heo31261212013-06-28 17:07:30 -07001326
1327 /*
1328 * Nothing can fail from this point on. Remove files for the
1329 * removed subsystems and rebind each subsystem.
1330 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001331 for_each_subsys_which(ss, ssid, &ss_mask)
1332 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001333
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001334 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001335 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001336 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001337 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001338
Tejun Heo5df36032014-03-19 10:23:54 -04001339 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001340 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001341
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001342 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001343
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001344 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1345 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001346 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001347 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001348
Tejun Heo2d8f2432014-04-23 11:13:15 -04001349 down_write(&css_set_rwsem);
1350 hash_for_each(css_set_table, i, cset, hlist)
1351 list_move_tail(&cset->e_cset_node[ss->id],
1352 &dst_root->cgrp.e_csets[ss->id]);
1353 up_write(&css_set_rwsem);
1354
Tejun Heof392e512014-04-23 11:13:14 -04001355 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo667c2492014-07-08 18:02:56 -04001356 src_root->cgrp.subtree_control &= ~(1 << ssid);
1357 cgroup_refresh_child_subsys_mask(&src_root->cgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001358
Tejun Heobd53d612014-04-23 11:13:16 -04001359 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001360 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo667c2492014-07-08 18:02:56 -04001361 if (dst_root != &cgrp_dfl_root) {
1362 dst_root->cgrp.subtree_control |= 1 << ssid;
1363 cgroup_refresh_child_subsys_mask(&dst_root->cgrp);
1364 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001365
Tejun Heo5df36032014-03-19 10:23:54 -04001366 if (ss->bind)
1367 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369
Tejun Heoa2dd4242014-03-19 10:23:55 -04001370 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371 return 0;
1372}
1373
Tejun Heo2bd59d42014-02-11 11:52:49 -05001374static int cgroup_show_options(struct seq_file *seq,
1375 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001377 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001379 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Tejun Heod98817d2015-08-18 13:58:16 -07001381 if (root != &cgrp_dfl_root)
1382 for_each_subsys(ss, ssid)
1383 if (root->subsys_mask & (1 << ssid))
Kees Cook61e57c02015-09-08 14:58:22 -07001384 seq_show_option(seq, ss->legacy_name, NULL);
Tejun Heo93438622013-04-14 20:15:25 -07001385 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001387 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001388 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001389
1390 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001391 if (strlen(root->release_agent_path))
Kees Cooka068acf2015-09-04 15:44:57 -07001392 seq_show_option(seq, "release_agent",
1393 root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001394 spin_unlock(&release_agent_path_lock);
1395
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001396 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001397 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001398 if (strlen(root->name))
Kees Cooka068acf2015-09-04 15:44:57 -07001399 seq_show_option(seq, "name", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400 return 0;
1401}
1402
1403struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001404 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001405 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001406 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001407 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001408 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001409 /* User explicitly requested empty subsystem */
1410 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411};
1412
Ben Blumcf5d5942010-03-10 15:22:09 -08001413static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001415 char *token, *o = data;
1416 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001417 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001418 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001419 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001420 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001421
1422#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001423 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001424#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425
Paul Menagec6d57f32009-09-23 15:56:19 -07001426 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427
1428 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001429 nr_opts++;
1430
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431 if (!*token)
1432 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001433 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001434 /* Explicitly have no subsystems */
1435 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001436 continue;
1437 }
1438 if (!strcmp(token, "all")) {
1439 /* Mutually exclusive option 'all' + subsystem name */
1440 if (one_ss)
1441 return -EINVAL;
1442 all_ss = true;
1443 continue;
1444 }
Tejun Heo873fe092013-04-14 20:15:26 -07001445 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1446 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1447 continue;
1448 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001449 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001450 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001451 continue;
1452 }
1453 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001454 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001455 continue;
1456 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001457 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001458 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001459 continue;
1460 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001461 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001462 /* Specifying two release agents is forbidden */
1463 if (opts->release_agent)
1464 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001465 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001466 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001467 if (!opts->release_agent)
1468 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001469 continue;
1470 }
1471 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 const char *name = token + 5;
1473 /* Can't specify an empty name */
1474 if (!strlen(name))
1475 return -EINVAL;
1476 /* Must match [\w.-]+ */
1477 for (i = 0; i < strlen(name); i++) {
1478 char c = name[i];
1479 if (isalnum(c))
1480 continue;
1481 if ((c == '.') || (c == '-') || (c == '_'))
1482 continue;
1483 return -EINVAL;
1484 }
1485 /* Specifying two names is forbidden */
1486 if (opts->name)
1487 return -EINVAL;
1488 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001489 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 GFP_KERNEL);
1491 if (!opts->name)
1492 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001493
1494 continue;
1495 }
1496
Tejun Heo30159ec2013-06-25 11:53:37 -07001497 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001498 if (strcmp(token, ss->legacy_name))
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001499 continue;
1500 if (ss->disabled)
1501 continue;
1502
1503 /* Mutually exclusive option 'all' + subsystem name */
1504 if (all_ss)
1505 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001506 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001507 one_ss = true;
1508
1509 break;
1510 }
1511 if (i == CGROUP_SUBSYS_COUNT)
1512 return -ENOENT;
1513 }
1514
Tejun Heo873fe092013-04-14 20:15:26 -07001515 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001516 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001517 if (nr_opts != 1) {
1518 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001519 return -EINVAL;
1520 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001521 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001522 }
1523
Li Zefanf9ab5b52009-06-17 16:26:33 -07001524 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001525 * If the 'all' option was specified select all the subsystems,
1526 * otherwise if 'none', 'name=' and a subsystem name options were
1527 * not specified, let's default to 'all'
1528 */
1529 if (all_ss || (!one_ss && !opts->none && !opts->name))
1530 for_each_subsys(ss, i)
1531 if (!ss->disabled)
1532 opts->subsys_mask |= (1 << i);
1533
1534 /*
1535 * We either have to specify by name or by subsystems. (So all
1536 * empty hierarchies must have a name).
1537 */
1538 if (!opts->subsys_mask && !opts->name)
1539 return -EINVAL;
1540
1541 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001542 * Option noprefix was introduced just for backward compatibility
1543 * with the old cpuset, so we allow noprefix only if mounting just
1544 * the cpuset subsystem.
1545 */
Tejun Heo93438622013-04-14 20:15:25 -07001546 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001547 return -EINVAL;
1548
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001549 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001550 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001551 return -EINVAL;
1552
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 return 0;
1554}
1555
Tejun Heo2bd59d42014-02-11 11:52:49 -05001556static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557{
1558 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001559 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001561 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562
Tejun Heoaa6ec292014-07-09 10:08:08 -04001563 if (root == &cgrp_dfl_root) {
1564 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001565 return -EINVAL;
1566 }
1567
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 mutex_lock(&cgroup_mutex);
1569
1570 /* See what subsystems are wanted */
1571 ret = parse_cgroupfs_options(data, &opts);
1572 if (ret)
1573 goto out_unlock;
1574
Tejun Heof392e512014-04-23 11:13:14 -04001575 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001576 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001577 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001578
Tejun Heof392e512014-04-23 11:13:14 -04001579 added_mask = opts.subsys_mask & ~root->subsys_mask;
1580 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001581
Ben Blumcf5d5942010-03-10 15:22:09 -08001582 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001583 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001584 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001585 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001586 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001588 goto out_unlock;
1589 }
1590
Tejun Heof172e672013-06-28 17:07:30 -07001591 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001592 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001593 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001594 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001595 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596
Tejun Heo5df36032014-03-19 10:23:54 -04001597 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001598 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001601 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001602
Tejun Heo69e943b2014-02-08 10:36:58 -05001603 if (opts.release_agent) {
1604 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001605 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001606 spin_unlock(&release_agent_path_lock);
1607 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001609 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001610 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612 return ret;
1613}
1614
Tejun Heoafeb0f92014-02-13 06:58:39 -05001615/*
1616 * To reduce the fork() overhead for systems that are not actually using
1617 * their cgroups capability, we don't maintain the lists running through
1618 * each css_set to its tasks until we see the list actually used - in other
1619 * words after the first mount.
1620 */
1621static bool use_task_css_set_links __read_mostly;
1622
1623static void cgroup_enable_task_cg_lists(void)
1624{
1625 struct task_struct *p, *g;
1626
Tejun Heo96d365e2014-02-13 06:58:40 -05001627 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001628
1629 if (use_task_css_set_links)
1630 goto out_unlock;
1631
1632 use_task_css_set_links = true;
1633
1634 /*
1635 * We need tasklist_lock because RCU is not safe against
1636 * while_each_thread(). Besides, a forking task that has passed
1637 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1638 * is not guaranteed to have its child immediately visible in the
1639 * tasklist if we walk through it with RCU.
1640 */
1641 read_lock(&tasklist_lock);
1642 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001643 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1644 task_css_set(p) != &init_css_set);
1645
1646 /*
1647 * We should check if the process is exiting, otherwise
1648 * it will race with cgroup_exit() in that the list
1649 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001650 * Do it while holding siglock so that we don't end up
1651 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001652 */
Tejun Heof153ad12014-02-25 09:56:49 -05001653 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001654 if (!(p->flags & PF_EXITING)) {
1655 struct css_set *cset = task_css_set(p);
1656
1657 list_add(&p->cg_list, &cset->tasks);
1658 get_css_set(cset);
1659 }
Tejun Heof153ad12014-02-25 09:56:49 -05001660 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001661 } while_each_thread(g, p);
1662 read_unlock(&tasklist_lock);
1663out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001664 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001665}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666
Paul Menagecc31edc2008-10-18 20:28:04 -07001667static void init_cgroup_housekeeping(struct cgroup *cgrp)
1668{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001669 struct cgroup_subsys *ss;
1670 int ssid;
1671
Tejun Heod5c419b2014-05-16 13:22:48 -04001672 INIT_LIST_HEAD(&cgrp->self.sibling);
1673 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001674 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001675 INIT_LIST_HEAD(&cgrp->pidlists);
1676 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001677 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001678 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001679
1680 for_each_subsys(ss, ssid)
1681 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001682
1683 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001684 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001685}
Paul Menagec6d57f32009-09-23 15:56:19 -07001686
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001687static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001688 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001690 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001691
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001693 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001694 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001695 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001696 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697
Paul Menagec6d57f32009-09-23 15:56:19 -07001698 root->flags = opts->flags;
1699 if (opts->release_agent)
1700 strcpy(root->release_agent_path, opts->release_agent);
1701 if (opts->name)
1702 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001703 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001704 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001705}
1706
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001707static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001708{
Tejun Heod427dfe2014-02-11 11:52:48 -05001709 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001710 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heoa14c6872014-07-15 11:05:09 -04001711 struct cftype *base_files;
Tejun Heod427dfe2014-02-11 11:52:48 -05001712 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001713 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001714
Tejun Heod427dfe2014-02-11 11:52:48 -05001715 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001716
Vladimir Davydovcf780b72015-08-03 15:32:26 +03001717 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001718 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001719 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001720 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001721
Tejun Heo2aad2a82014-09-24 13:31:50 -04001722 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1723 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001724 if (ret)
1725 goto out;
1726
Tejun Heod427dfe2014-02-11 11:52:48 -05001727 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001728 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001729 * but that's OK - it can only be increased by someone holding
1730 * cgroup_lock, and that's us. The worst that can happen is that we
1731 * have some link structures left over
1732 */
1733 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001735 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736
Tejun Heo985ed672014-03-19 10:23:53 -04001737 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001738 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001739 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740
Tejun Heo2bd59d42014-02-11 11:52:49 -05001741 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1742 KERNFS_ROOT_CREATE_DEACTIVATED,
1743 root_cgrp);
1744 if (IS_ERR(root->kf_root)) {
1745 ret = PTR_ERR(root->kf_root);
1746 goto exit_root_id;
1747 }
1748 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749
Tejun Heoa14c6872014-07-15 11:05:09 -04001750 if (root == &cgrp_dfl_root)
1751 base_files = cgroup_dfl_base_files;
1752 else
1753 base_files = cgroup_legacy_base_files;
1754
1755 ret = cgroup_addrm_files(root_cgrp, base_files, true);
Tejun Heod427dfe2014-02-11 11:52:48 -05001756 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001757 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758
Tejun Heo5df36032014-03-19 10:23:54 -04001759 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001760 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001761 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001762
Tejun Heod427dfe2014-02-11 11:52:48 -05001763 /*
1764 * There must be no failure case after here, since rebinding takes
1765 * care of subsystems' refcounts, which are explicitly dropped in
1766 * the failure exit path.
1767 */
1768 list_add(&root->root_list, &cgroup_roots);
1769 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770
Tejun Heod427dfe2014-02-11 11:52:48 -05001771 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001772 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001773 * objects.
1774 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001775 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001776 hash_for_each(css_set_table, i, cset, hlist)
1777 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001778 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779
Tejun Heod5c419b2014-05-16 13:22:48 -04001780 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001781 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001782
Tejun Heo2bd59d42014-02-11 11:52:49 -05001783 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001784 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001785 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001786
Tejun Heo2bd59d42014-02-11 11:52:49 -05001787destroy_root:
1788 kernfs_destroy_root(root->kf_root);
1789 root->kf_root = NULL;
1790exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001791 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001792cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001793 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001794out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001795 free_cgrp_cset_links(&tmp_links);
1796 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797}
1798
Al Virof7e83572010-07-26 13:23:11 +04001799static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001801 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001802{
Li Zefan3a32bd72014-06-30 11:50:59 +08001803 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001804 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001805 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001807 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001808 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001809 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001810 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001811
1812 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001813 * The first time anyone tries to mount a cgroup, enable the list
1814 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001815 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001816 if (!use_task_css_set_links)
1817 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001818
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001819 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820
Paul Menageddbcc7e2007-10-18 23:39:30 -07001821 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001824 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001825
Tejun Heo2bd59d42014-02-11 11:52:49 -05001826 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001827 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04001828 cgrp_dfl_root_visible = true;
1829 root = &cgrp_dfl_root;
1830 cgroup_get(&root->cgrp);
1831 ret = 0;
1832 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 }
1834
Li Zefan970317a2014-06-30 11:49:58 +08001835 /*
1836 * Destruction of cgroup root is asynchronous, so subsystems may
1837 * still be dying after the previous unmount. Let's drain the
1838 * dying subsystems. We just need to ensure that the ones
1839 * unmounted previously finish dying and don't care about new ones
1840 * starting. Testing ref liveliness is good enough.
1841 */
1842 for_each_subsys(ss, i) {
1843 if (!(opts.subsys_mask & (1 << i)) ||
1844 ss->root == &cgrp_dfl_root)
1845 continue;
1846
1847 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1848 mutex_unlock(&cgroup_mutex);
1849 msleep(10);
1850 ret = restart_syscall();
1851 goto out_free;
1852 }
1853 cgroup_put(&ss->root->cgrp);
1854 }
1855
Tejun Heo985ed672014-03-19 10:23:53 -04001856 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001857 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001858
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001859 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001860 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001861
Paul Menage817929e2007-10-18 23:39:36 -07001862 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001863 * If we asked for a name then it must match. Also, if
1864 * name matches but sybsys_mask doesn't, we should fail.
1865 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001866 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001867 if (opts.name) {
1868 if (strcmp(opts.name, root->name))
1869 continue;
1870 name_match = true;
1871 }
Tejun Heo31261212013-06-28 17:07:30 -07001872
1873 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001874 * If we asked for subsystems (or explicitly for no
1875 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001876 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001877 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001878 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001879 if (!name_match)
1880 continue;
1881 ret = -EBUSY;
1882 goto out_unlock;
1883 }
Tejun Heo873fe092013-04-14 20:15:26 -07001884
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001885 if (root->flags ^ opts.flags)
1886 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05001887
Tejun Heo776f02f2014-02-12 09:29:50 -05001888 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08001889 * We want to reuse @root whose lifetime is governed by its
1890 * ->cgrp. Let's check whether @root is alive and keep it
1891 * that way. As cgroup_kill_sb() can happen anytime, we
1892 * want to block it by pinning the sb so that @root doesn't
1893 * get killed before mount is complete.
1894 *
1895 * With the sb pinned, tryget_live can reliably indicate
1896 * whether @root can be reused. If it's being killed,
1897 * drain it. We can use wait_queue for the wait but this
1898 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001899 */
Li Zefan3a32bd72014-06-30 11:50:59 +08001900 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1901 if (IS_ERR(pinned_sb) ||
1902 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001903 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08001904 if (!IS_ERR_OR_NULL(pinned_sb))
1905 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05001906 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001907 ret = restart_syscall();
1908 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001909 }
1910
1911 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001912 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001913 }
1914
Tejun Heo172a2c062014-03-19 10:23:53 -04001915 /*
1916 * No such thing, create a new one. name= matching without subsys
1917 * specification is allowed for already existing hierarchies but we
1918 * can't create new one without subsys specification.
1919 */
1920 if (!opts.subsys_mask && !opts.none) {
1921 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001922 goto out_unlock;
1923 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001924
Tejun Heo172a2c062014-03-19 10:23:53 -04001925 root = kzalloc(sizeof(*root), GFP_KERNEL);
1926 if (!root) {
1927 ret = -ENOMEM;
1928 goto out_unlock;
1929 }
1930
1931 init_cgroup_root(root, &opts);
1932
Tejun Heo35585572014-02-13 06:58:38 -05001933 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001934 if (ret)
1935 cgroup_free_root(root);
1936
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001937out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001938 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001939out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001940 kfree(opts.release_agent);
1941 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001942
Tejun Heo2bd59d42014-02-11 11:52:49 -05001943 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001944 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001945
Jianyu Zhanc9482a52014-04-26 15:40:28 +08001946 dentry = kernfs_mount(fs_type, flags, root->kf_root,
1947 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001948 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001949 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08001950
1951 /*
1952 * If @pinned_sb, we're reusing an existing root and holding an
1953 * extra ref on its sb. Mount is complete. Put the extra ref.
1954 */
1955 if (pinned_sb) {
1956 WARN_ON(new_sb);
1957 deactivate_super(pinned_sb);
1958 }
1959
Tejun Heo2bd59d42014-02-11 11:52:49 -05001960 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001961}
1962
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001963static void cgroup_kill_sb(struct super_block *sb)
1964{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001965 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001966 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001967
Tejun Heo9d755d32014-05-14 09:15:02 -04001968 /*
1969 * If @root doesn't have any mounts or children, start killing it.
1970 * This prevents new mounts by disabling percpu_ref_tryget_live().
1971 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08001972 *
1973 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04001974 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05001975 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08001976 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04001977 cgroup_put(&root->cgrp);
1978 else
1979 percpu_ref_kill(&root->cgrp.self.refcnt);
1980
Tejun Heo2bd59d42014-02-11 11:52:49 -05001981 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001982}
1983
1984static struct file_system_type cgroup_fs_type = {
1985 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001986 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001987 .kill_sb = cgroup_kill_sb,
1988};
1989
Li Zefana043e3b2008-02-23 15:24:09 -08001990/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001991 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001992 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001993 * @buf: the buffer to write the path into
1994 * @buflen: the length of the buffer
1995 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001996 * Determine @task's cgroup on the first (the one with the lowest non-zero
1997 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1998 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1999 * cgroup controller callbacks.
2000 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002001 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002002 */
Tejun Heoe61734c2014-02-12 09:29:50 -05002003char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002004{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002005 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002006 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002007 int hierarchy_id = 1;
2008 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07002009
2010 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05002011 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07002012
Tejun Heo913ffdb2013-07-11 16:34:48 -07002013 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2014
Tejun Heo857a2be2013-04-14 20:50:08 -07002015 if (root) {
2016 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05002017 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002018 } else {
2019 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05002020 if (strlcpy(buf, "/", buflen) < buflen)
2021 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07002022 }
2023
Tejun Heo96d365e2014-02-13 06:58:40 -05002024 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07002025 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05002026 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07002027}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002028EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002029
Tejun Heob3dc0942014-02-25 10:04:01 -05002030/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08002031struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05002032 /* the src and dst cset list running through cset->mg_node */
2033 struct list_head src_csets;
2034 struct list_head dst_csets;
2035
2036 /*
2037 * Fields for cgroup_taskset_*() iteration.
2038 *
2039 * Before migration is committed, the target migration tasks are on
2040 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2041 * the csets on ->dst_csets. ->csets point to either ->src_csets
2042 * or ->dst_csets depending on whether migration is committed.
2043 *
2044 * ->cur_csets and ->cur_task point to the current task position
2045 * during iteration.
2046 */
2047 struct list_head *csets;
2048 struct css_set *cur_cset;
2049 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002050};
2051
2052/**
2053 * cgroup_taskset_first - reset taskset and return the first task
2054 * @tset: taskset of interest
2055 *
2056 * @tset iteration is initialized and the first task is returned.
2057 */
2058struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2059{
Tejun Heob3dc0942014-02-25 10:04:01 -05002060 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2061 tset->cur_task = NULL;
2062
2063 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002064}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002065
2066/**
2067 * cgroup_taskset_next - iterate to the next task in taskset
2068 * @tset: taskset of interest
2069 *
2070 * Return the next task in @tset. Iteration must have been initialized
2071 * with cgroup_taskset_first().
2072 */
2073struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2074{
Tejun Heob3dc0942014-02-25 10:04:01 -05002075 struct css_set *cset = tset->cur_cset;
2076 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002077
Tejun Heob3dc0942014-02-25 10:04:01 -05002078 while (&cset->mg_node != tset->csets) {
2079 if (!task)
2080 task = list_first_entry(&cset->mg_tasks,
2081 struct task_struct, cg_list);
2082 else
2083 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002084
Tejun Heob3dc0942014-02-25 10:04:01 -05002085 if (&task->cg_list != &cset->mg_tasks) {
2086 tset->cur_cset = cset;
2087 tset->cur_task = task;
2088 return task;
2089 }
2090
2091 cset = list_next_entry(cset, mg_node);
2092 task = NULL;
2093 }
2094
2095 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002096}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002097
2098/**
Ben Blum74a11662011-05-26 16:25:20 -07002099 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02002100 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002101 * @tsk: the task being migrated
2102 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07002103 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002104 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07002105 */
Tejun Heo5abb8852013-06-12 21:04:49 -07002106static void cgroup_task_migrate(struct cgroup *old_cgrp,
2107 struct task_struct *tsk,
2108 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07002109{
Tejun Heo5abb8852013-06-12 21:04:49 -07002110 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07002111
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002112 lockdep_assert_held(&cgroup_mutex);
2113 lockdep_assert_held(&css_set_rwsem);
2114
Ben Blum74a11662011-05-26 16:25:20 -07002115 /*
Tejun Heo0c986252015-09-16 11:51:12 -04002116 * We are synchronized through threadgroup_lock() against PF_EXITING
2117 * setting such that we can't race against cgroup_exit() changing the
2118 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07002119 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01002120 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002121 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002122
Tejun Heob3dc0942014-02-25 10:04:01 -05002123 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07002124 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002125
Tejun Heo1b9aba42014-03-19 17:43:21 -04002126 /*
2127 * Use move_tail so that cgroup_taskset_first() still returns the
2128 * leader after migration. This works because cgroup_migrate()
2129 * ensures that the dst_cset of the leader is the first on the
2130 * tset's dst_csets list.
2131 */
2132 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07002133
2134 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07002135 * We just gained a reference on old_cset by taking it from the
2136 * task. As trading it for new_cset is protected by cgroup_mutex,
2137 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07002138 */
Zefan Lia25eb522014-09-19 16:51:00 +08002139 put_css_set_locked(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002140}
2141
Li Zefana043e3b2008-02-23 15:24:09 -08002142/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002143 * cgroup_migrate_finish - cleanup after attach
2144 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002145 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002146 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2147 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002148 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002149static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002150{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002151 struct css_set *cset, *tmp_cset;
2152
2153 lockdep_assert_held(&cgroup_mutex);
2154
2155 down_write(&css_set_rwsem);
2156 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2157 cset->mg_src_cgrp = NULL;
2158 cset->mg_dst_cset = NULL;
2159 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002160 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002161 }
2162 up_write(&css_set_rwsem);
2163}
2164
2165/**
2166 * cgroup_migrate_add_src - add a migration source css_set
2167 * @src_cset: the source css_set to add
2168 * @dst_cgrp: the destination cgroup
2169 * @preloaded_csets: list of preloaded css_sets
2170 *
2171 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2172 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2173 * up by cgroup_migrate_finish().
2174 *
Tejun Heo0c986252015-09-16 11:51:12 -04002175 * This function may be called without holding threadgroup_lock even if the
2176 * target is a process. Threads may be created and destroyed but as long
2177 * as cgroup_mutex is not dropped, no new css_set can be put into play and
2178 * the preloaded css_sets are guaranteed to cover all migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002179 */
2180static void cgroup_migrate_add_src(struct css_set *src_cset,
2181 struct cgroup *dst_cgrp,
2182 struct list_head *preloaded_csets)
2183{
2184 struct cgroup *src_cgrp;
2185
2186 lockdep_assert_held(&cgroup_mutex);
2187 lockdep_assert_held(&css_set_rwsem);
2188
2189 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2190
Tejun Heo1958d2d2014-02-25 10:04:03 -05002191 if (!list_empty(&src_cset->mg_preload_node))
2192 return;
2193
2194 WARN_ON(src_cset->mg_src_cgrp);
2195 WARN_ON(!list_empty(&src_cset->mg_tasks));
2196 WARN_ON(!list_empty(&src_cset->mg_node));
2197
2198 src_cset->mg_src_cgrp = src_cgrp;
2199 get_css_set(src_cset);
2200 list_add(&src_cset->mg_preload_node, preloaded_csets);
2201}
2202
2203/**
2204 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002205 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002206 * @preloaded_csets: list of preloaded source css_sets
2207 *
2208 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2209 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002210 * pins all destination css_sets, links each to its source, and append them
2211 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2212 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002213 *
2214 * This function must be called after cgroup_migrate_add_src() has been
2215 * called on each migration source css_set. After migration is performed
2216 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2217 * @preloaded_csets.
2218 */
2219static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2220 struct list_head *preloaded_csets)
2221{
2222 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002223 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002224
2225 lockdep_assert_held(&cgroup_mutex);
2226
Tejun Heof8f22e52014-04-23 11:13:16 -04002227 /*
2228 * Except for the root, child_subsys_mask must be zero for a cgroup
2229 * with tasks so that child cgroups don't compete against tasks.
2230 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002231 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002232 dst_cgrp->child_subsys_mask)
2233 return -EBUSY;
2234
Tejun Heo1958d2d2014-02-25 10:04:03 -05002235 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002236 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002237 struct css_set *dst_cset;
2238
Tejun Heof817de92014-04-23 11:13:16 -04002239 dst_cset = find_css_set(src_cset,
2240 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002241 if (!dst_cset)
2242 goto err;
2243
2244 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002245
2246 /*
2247 * If src cset equals dst, it's noop. Drop the src.
2248 * cgroup_migrate() will skip the cset too. Note that we
2249 * can't handle src == dst as some nodes are used by both.
2250 */
2251 if (src_cset == dst_cset) {
2252 src_cset->mg_src_cgrp = NULL;
2253 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002254 put_css_set(src_cset);
2255 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002256 continue;
2257 }
2258
Tejun Heo1958d2d2014-02-25 10:04:03 -05002259 src_cset->mg_dst_cset = dst_cset;
2260
2261 if (list_empty(&dst_cset->mg_preload_node))
2262 list_add(&dst_cset->mg_preload_node, &csets);
2263 else
Zefan Lia25eb522014-09-19 16:51:00 +08002264 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002265 }
2266
Tejun Heof817de92014-04-23 11:13:16 -04002267 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002268 return 0;
2269err:
2270 cgroup_migrate_finish(&csets);
2271 return -ENOMEM;
2272}
2273
2274/**
2275 * cgroup_migrate - migrate a process or task to a cgroup
2276 * @cgrp: the destination cgroup
2277 * @leader: the leader of the process or the task to migrate
2278 * @threadgroup: whether @leader points to the whole process or a single task
2279 *
2280 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heo0c986252015-09-16 11:51:12 -04002281 * process, the caller must be holding threadgroup_lock of @leader. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002282 * caller is also responsible for invoking cgroup_migrate_add_src() and
2283 * cgroup_migrate_prepare_dst() on the targets before invoking this
2284 * function and following up with cgroup_migrate_finish().
2285 *
2286 * As long as a controller's ->can_attach() doesn't fail, this function is
2287 * guaranteed to succeed. This means that, excluding ->can_attach()
2288 * failure, when migrating multiple targets, the success or failure can be
2289 * decided for all targets by invoking group_migrate_prepare_dst() before
2290 * actually starting migrating.
2291 */
2292static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2293 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002294{
Tejun Heob3dc0942014-02-25 10:04:01 -05002295 struct cgroup_taskset tset = {
2296 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2297 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2298 .csets = &tset.src_csets,
2299 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002300 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002301 struct css_set *cset, *tmp_cset;
2302 struct task_struct *task, *tmp_task;
2303 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002304
2305 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002306 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2307 * already PF_EXITING could be freed from underneath us unless we
2308 * take an rcu_read_lock.
2309 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002310 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002311 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002312 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002313 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002314 /* @task either already exited or can't exit until the end */
2315 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002316 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002317
Tejun Heoeaf797a2014-02-25 10:04:03 -05002318 /* leave @task alone if post_fork() hasn't linked it yet */
2319 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002320 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002321
Tejun Heob3dc0942014-02-25 10:04:01 -05002322 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002323 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002324 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002325
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002326 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002327 * cgroup_taskset_first() must always return the leader.
2328 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002329 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002330 list_move_tail(&task->cg_list, &cset->mg_tasks);
2331 if (list_empty(&cset->mg_node))
2332 list_add_tail(&cset->mg_node, &tset.src_csets);
2333 if (list_empty(&cset->mg_dst_cset->mg_node))
2334 list_move_tail(&cset->mg_dst_cset->mg_node,
2335 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002336 next:
Li Zefan081aa452013-03-13 09:17:09 +08002337 if (!threadgroup)
2338 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002339 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002340 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002341 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002342
Tejun Heo134d3372011-12-12 18:12:21 -08002343 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002344 if (list_empty(&tset.src_csets))
2345 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002346
Tejun Heo1958d2d2014-02-25 10:04:03 -05002347 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002348 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002349 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002350 ret = css->ss->can_attach(css, &tset);
2351 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002352 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002353 goto out_cancel_attach;
2354 }
2355 }
Ben Blum74a11662011-05-26 16:25:20 -07002356 }
2357
2358 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002359 * Now that we're guaranteed success, proceed to move all tasks to
2360 * the new cgroup. There are no failure cases after here, so this
2361 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002362 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002363 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002364 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2365 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2366 cgroup_task_migrate(cset->mg_src_cgrp, task,
2367 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002368 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002369 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002370
2371 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002372 * Migration is committed, all target tasks are now on dst_csets.
2373 * Nothing is sensitive to fork() after this point. Notify
2374 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002375 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002376 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002377
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002378 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002379 if (css->ss->attach)
2380 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002381
Tejun Heo9db8de32014-02-13 06:58:43 -05002382 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002383 goto out_release_tset;
2384
Ben Blum74a11662011-05-26 16:25:20 -07002385out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002386 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002387 if (css == failed_css)
2388 break;
2389 if (css->ss->cancel_attach)
2390 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002391 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002392out_release_tset:
2393 down_write(&css_set_rwsem);
2394 list_splice_init(&tset.dst_csets, &tset.src_csets);
2395 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002396 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002397 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002398 }
2399 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002400 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002401}
2402
Tejun Heo1958d2d2014-02-25 10:04:03 -05002403/**
2404 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2405 * @dst_cgrp: the cgroup to attach to
2406 * @leader: the task or the leader of the threadgroup to be attached
2407 * @threadgroup: attach the whole threadgroup?
2408 *
Tejun Heo0c986252015-09-16 11:51:12 -04002409 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002410 */
2411static int cgroup_attach_task(struct cgroup *dst_cgrp,
2412 struct task_struct *leader, bool threadgroup)
2413{
2414 LIST_HEAD(preloaded_csets);
2415 struct task_struct *task;
2416 int ret;
2417
2418 /* look up all src csets */
2419 down_read(&css_set_rwsem);
2420 rcu_read_lock();
2421 task = leader;
2422 do {
2423 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2424 &preloaded_csets);
2425 if (!threadgroup)
2426 break;
2427 } while_each_thread(leader, task);
2428 rcu_read_unlock();
2429 up_read(&css_set_rwsem);
2430
2431 /* prepare dst csets and commit */
2432 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2433 if (!ret)
2434 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2435
2436 cgroup_migrate_finish(&preloaded_csets);
2437 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002438}
2439
Tejun Heo187fe842015-06-18 16:54:28 -04002440static int cgroup_procs_write_permission(struct task_struct *task,
2441 struct cgroup *dst_cgrp,
2442 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002443{
2444 const struct cred *cred = current_cred();
2445 const struct cred *tcred = get_task_cred(task);
2446 int ret = 0;
2447
2448 /*
2449 * even if we're attaching all tasks in the thread group, we only
2450 * need to check permissions on one of them.
2451 */
2452 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2453 !uid_eq(cred->euid, tcred->uid) &&
2454 !uid_eq(cred->euid, tcred->suid))
2455 ret = -EACCES;
2456
Tejun Heo187fe842015-06-18 16:54:28 -04002457 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2458 struct super_block *sb = of->file->f_path.dentry->d_sb;
2459 struct cgroup *cgrp;
2460 struct inode *inode;
2461
2462 down_read(&css_set_rwsem);
2463 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
2464 up_read(&css_set_rwsem);
2465
2466 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2467 cgrp = cgroup_parent(cgrp);
2468
2469 ret = -ENOMEM;
2470 inode = kernfs_get_inode(sb, cgrp->procs_kn);
2471 if (inode) {
2472 ret = inode_permission(inode, MAY_WRITE);
2473 iput(inode);
2474 }
2475 }
2476
Tejun Heodedf22e2015-06-18 16:54:28 -04002477 put_cred(tcred);
2478 return ret;
2479}
2480
Ben Blum74a11662011-05-26 16:25:20 -07002481/*
2482 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002483 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002484 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002485 */
Tejun Heoacbef752014-05-13 12:16:22 -04002486static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2487 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002488{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002489 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002490 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002491 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002492 int ret;
2493
Tejun Heoacbef752014-05-13 12:16:22 -04002494 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2495 return -EINVAL;
2496
Tejun Heoe76ecae2014-05-13 12:19:23 -04002497 cgrp = cgroup_kn_lock_live(of->kn);
2498 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002499 return -ENODEV;
2500
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002501retry_find_task:
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002502 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002503 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002504 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002505 if (!tsk) {
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002506 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002507 ret = -ESRCH;
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002508 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002509 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002510 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002511 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002512 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002513
2514 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002515 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002516
2517 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002518 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002519 * trapped in a cpuset, or RT worker may be born in a cgroup
2520 * with no rt_runtime allocated. Just say no.
2521 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002522 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002523 ret = -EINVAL;
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002524 rcu_read_unlock();
2525 goto out_unlock_cgroup;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002526 }
2527
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002528 get_task_struct(tsk);
2529 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002530
Tejun Heo0c986252015-09-16 11:51:12 -04002531 threadgroup_lock(tsk);
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002532 if (threadgroup) {
2533 if (!thread_group_leader(tsk)) {
2534 /*
2535 * a race with de_thread from another thread's exec()
2536 * may strip us of our leadership, if this happens,
2537 * there is no choice but to throw this task away and
2538 * try again; this is
2539 * "double-double-toil-and-trouble-check locking".
2540 */
Tejun Heo0c986252015-09-16 11:51:12 -04002541 threadgroup_unlock(tsk);
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002542 put_task_struct(tsk);
2543 goto retry_find_task;
2544 }
2545 }
2546
Tejun Heo187fe842015-06-18 16:54:28 -04002547 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002548 if (!ret)
2549 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002550
Tejun Heo0c986252015-09-16 11:51:12 -04002551 threadgroup_unlock(tsk);
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002552
2553 put_task_struct(tsk);
2554out_unlock_cgroup:
Tejun Heoe76ecae2014-05-13 12:19:23 -04002555 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002556 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002557}
2558
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002559/**
2560 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2561 * @from: attach to all cgroups of a given task
2562 * @tsk: the task to be attached
2563 */
2564int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2565{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002566 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002567 int retval = 0;
2568
Tejun Heo47cfcd02013-04-07 09:29:51 -07002569 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002570 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002571 struct cgroup *from_cgrp;
2572
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002573 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002574 continue;
2575
Tejun Heo96d365e2014-02-13 06:58:40 -05002576 down_read(&css_set_rwsem);
2577 from_cgrp = task_cgroup_from_root(from, root);
2578 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002579
Li Zefan6f4b7e62013-07-31 16:18:36 +08002580 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002581 if (retval)
2582 break;
2583 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002584 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002585
2586 return retval;
2587}
2588EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2589
Tejun Heoacbef752014-05-13 12:16:22 -04002590static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2591 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002592{
Tejun Heoacbef752014-05-13 12:16:22 -04002593 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002594}
2595
Tejun Heoacbef752014-05-13 12:16:22 -04002596static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2597 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002598{
Tejun Heoacbef752014-05-13 12:16:22 -04002599 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002600}
2601
Tejun Heo451af502014-05-13 12:16:21 -04002602static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2603 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e0662008-07-25 01:46:59 -07002604{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002605 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002606
Tejun Heoe76ecae2014-05-13 12:19:23 -04002607 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2608
2609 cgrp = cgroup_kn_lock_live(of->kn);
2610 if (!cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07002611 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002612 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002613 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2614 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002615 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002616 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002617 return nbytes;
Paul Menagee788e0662008-07-25 01:46:59 -07002618}
2619
Tejun Heo2da8ca82013-12-05 12:28:04 -05002620static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002621{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002622 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002623
Tejun Heo46cfeb02014-05-13 12:11:00 -04002624 spin_lock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002625 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002626 spin_unlock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002627 seq_putc(seq, '\n');
Paul Menagee788e0662008-07-25 01:46:59 -07002628 return 0;
2629}
2630
Tejun Heo2da8ca82013-12-05 12:28:04 -05002631static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002632{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002633 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002634 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002635}
2636
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002637static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002638{
2639 struct cgroup_subsys *ss;
2640 bool printed = false;
2641 int ssid;
2642
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002643 for_each_subsys_which(ss, ssid, &ss_mask) {
2644 if (printed)
2645 seq_putc(seq, ' ');
2646 seq_printf(seq, "%s", ss->name);
2647 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002648 }
2649 if (printed)
2650 seq_putc(seq, '\n');
2651}
2652
2653/* show controllers which are currently attached to the default hierarchy */
2654static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2655{
2656 struct cgroup *cgrp = seq_css(seq)->cgroup;
2657
Tejun Heo5533e012014-05-14 19:33:07 -04002658 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2659 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002660 return 0;
2661}
2662
2663/* show controllers which are enabled from the parent */
2664static int cgroup_controllers_show(struct seq_file *seq, void *v)
2665{
2666 struct cgroup *cgrp = seq_css(seq)->cgroup;
2667
Tejun Heo667c2492014-07-08 18:02:56 -04002668 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002669 return 0;
2670}
2671
2672/* show controllers which are enabled for a given cgroup's children */
2673static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2674{
2675 struct cgroup *cgrp = seq_css(seq)->cgroup;
2676
Tejun Heo667c2492014-07-08 18:02:56 -04002677 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002678 return 0;
2679}
2680
2681/**
2682 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2683 * @cgrp: root of the subtree to update csses for
2684 *
2685 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2686 * css associations need to be updated accordingly. This function looks up
2687 * all css_sets which are attached to the subtree, creates the matching
2688 * updated css_sets and migrates the tasks to the new ones.
2689 */
2690static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2691{
2692 LIST_HEAD(preloaded_csets);
2693 struct cgroup_subsys_state *css;
2694 struct css_set *src_cset;
2695 int ret;
2696
Tejun Heof8f22e52014-04-23 11:13:16 -04002697 lockdep_assert_held(&cgroup_mutex);
2698
2699 /* look up all csses currently attached to @cgrp's subtree */
2700 down_read(&css_set_rwsem);
2701 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2702 struct cgrp_cset_link *link;
2703
2704 /* self is not affected by child_subsys_mask change */
2705 if (css->cgroup == cgrp)
2706 continue;
2707
2708 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2709 cgroup_migrate_add_src(link->cset, cgrp,
2710 &preloaded_csets);
2711 }
2712 up_read(&css_set_rwsem);
2713
2714 /* NULL dst indicates self on default hierarchy */
2715 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2716 if (ret)
2717 goto out_finish;
2718
2719 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2720 struct task_struct *last_task = NULL, *task;
2721
2722 /* src_csets precede dst_csets, break on the first dst_cset */
2723 if (!src_cset->mg_src_cgrp)
2724 break;
2725
2726 /*
2727 * All tasks in src_cset need to be migrated to the
2728 * matching dst_cset. Empty it process by process. We
2729 * walk tasks but migrate processes. The leader might even
2730 * belong to a different cset but such src_cset would also
2731 * be among the target src_csets because the default
2732 * hierarchy enforces per-process membership.
2733 */
2734 while (true) {
2735 down_read(&css_set_rwsem);
2736 task = list_first_entry_or_null(&src_cset->tasks,
2737 struct task_struct, cg_list);
2738 if (task) {
2739 task = task->group_leader;
2740 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2741 get_task_struct(task);
2742 }
2743 up_read(&css_set_rwsem);
2744
2745 if (!task)
2746 break;
2747
2748 /* guard against possible infinite loop */
2749 if (WARN(last_task == task,
2750 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2751 goto out_finish;
2752 last_task = task;
2753
Tejun Heo0c986252015-09-16 11:51:12 -04002754 threadgroup_lock(task);
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002755 /* raced against de_thread() from another thread? */
2756 if (!thread_group_leader(task)) {
Tejun Heo0c986252015-09-16 11:51:12 -04002757 threadgroup_unlock(task);
Tejun Heof9f9e7b2015-09-16 11:51:12 -04002758 put_task_struct(task);
2759 continue;
2760 }
2761
Tejun Heof8f22e52014-04-23 11:13:16 -04002762 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2763
Tejun Heo0c986252015-09-16 11:51:12 -04002764 threadgroup_unlock(task);
Tejun Heof8f22e52014-04-23 11:13:16 -04002765 put_task_struct(task);
2766
2767 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2768 goto out_finish;
2769 }
2770 }
2771
2772out_finish:
2773 cgroup_migrate_finish(&preloaded_csets);
2774 return ret;
2775}
2776
2777/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002778static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2779 char *buf, size_t nbytes,
2780 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002781{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002782 unsigned long enable = 0, disable = 0;
2783 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002784 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002785 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002786 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002787 int ssid, ret;
2788
2789 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002790 * Parse input - space separated list of subsystem names prefixed
2791 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002792 */
Tejun Heo451af502014-05-13 12:16:21 -04002793 buf = strstrip(buf);
2794 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002795 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2796
Tejun Heod37167a2014-05-13 12:10:59 -04002797 if (tok[0] == '\0')
2798 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002799 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
2800 if (ss->disabled || strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002801 continue;
2802
2803 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002804 enable |= 1 << ssid;
2805 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002806 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002807 disable |= 1 << ssid;
2808 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002809 } else {
2810 return -EINVAL;
2811 }
2812 break;
2813 }
2814 if (ssid == CGROUP_SUBSYS_COUNT)
2815 return -EINVAL;
2816 }
2817
Tejun Heoa9746d82014-05-13 12:19:22 -04002818 cgrp = cgroup_kn_lock_live(of->kn);
2819 if (!cgrp)
2820 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002821
2822 for_each_subsys(ss, ssid) {
2823 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002824 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002825 enable &= ~(1 << ssid);
2826 continue;
2827 }
2828
Tejun Heoc29adf22014-07-08 18:02:56 -04002829 /* unavailable or not enabled on the parent? */
2830 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2831 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002832 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002833 ret = -ENOENT;
2834 goto out_unlock;
2835 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002836 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002837 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002838 disable &= ~(1 << ssid);
2839 continue;
2840 }
2841
2842 /* a child has it enabled? */
2843 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002844 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002845 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002846 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002847 }
2848 }
2849 }
2850 }
2851
2852 if (!enable && !disable) {
2853 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002854 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002855 }
2856
2857 /*
Tejun Heo667c2492014-07-08 18:02:56 -04002858 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002859 * with tasks so that child cgroups don't compete against tasks.
2860 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002861 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002862 ret = -EBUSY;
2863 goto out_unlock;
2864 }
2865
2866 /*
Tejun Heof63070d2014-07-08 18:02:57 -04002867 * Update subsys masks and calculate what needs to be done. More
2868 * subsystems than specified may need to be enabled or disabled
2869 * depending on subsystem dependencies.
2870 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05002871 old_sc = cgrp->subtree_control;
2872 old_ss = cgrp->child_subsys_mask;
2873 new_sc = (old_sc | enable) & ~disable;
2874 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04002875
Tejun Heo755bf5e2014-11-18 02:49:50 -05002876 css_enable = ~old_ss & new_ss;
2877 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04002878 enable |= css_enable;
2879 disable |= css_disable;
2880
Tejun Heodb6e3052014-11-18 02:49:51 -05002881 /*
2882 * Because css offlining is asynchronous, userland might try to
2883 * re-enable the same controller while the previous instance is
2884 * still around. In such cases, wait till it's gone using
2885 * offline_waitq.
2886 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002887 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05002888 cgroup_for_each_live_child(child, cgrp) {
2889 DEFINE_WAIT(wait);
2890
2891 if (!cgroup_css(child, ss))
2892 continue;
2893
2894 cgroup_get(child);
2895 prepare_to_wait(&child->offline_waitq, &wait,
2896 TASK_UNINTERRUPTIBLE);
2897 cgroup_kn_unlock(of->kn);
2898 schedule();
2899 finish_wait(&child->offline_waitq, &wait);
2900 cgroup_put(child);
2901
2902 return restart_syscall();
2903 }
2904 }
2905
Tejun Heo755bf5e2014-11-18 02:49:50 -05002906 cgrp->subtree_control = new_sc;
2907 cgrp->child_subsys_mask = new_ss;
2908
Tejun Heof63070d2014-07-08 18:02:57 -04002909 /*
2910 * Create new csses or make the existing ones visible. A css is
2911 * created invisible if it's being implicitly enabled through
2912 * dependency. An invisible css is made visible when the userland
2913 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04002914 */
2915 for_each_subsys(ss, ssid) {
2916 if (!(enable & (1 << ssid)))
2917 continue;
2918
2919 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04002920 if (css_enable & (1 << ssid))
2921 ret = create_css(child, ss,
2922 cgrp->subtree_control & (1 << ssid));
2923 else
2924 ret = cgroup_populate_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002925 if (ret)
2926 goto err_undo_css;
2927 }
2928 }
2929
Tejun Heoc29adf22014-07-08 18:02:56 -04002930 /*
2931 * At this point, cgroup_e_css() results reflect the new csses
2932 * making the following cgroup_update_dfl_csses() properly update
2933 * css associations of all tasks in the subtree.
2934 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002935 ret = cgroup_update_dfl_csses(cgrp);
2936 if (ret)
2937 goto err_undo_css;
2938
Tejun Heof63070d2014-07-08 18:02:57 -04002939 /*
2940 * All tasks are migrated out of disabled csses. Kill or hide
2941 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002942 * disabled while other subsystems are still depending on it. The
2943 * css must not actively control resources and be in the vanilla
2944 * state if it's made visible again later. Controllers which may
2945 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04002946 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002947 for_each_subsys(ss, ssid) {
2948 if (!(disable & (1 << ssid)))
2949 continue;
2950
Tejun Heof63070d2014-07-08 18:02:57 -04002951 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002952 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2953
2954 if (css_disable & (1 << ssid)) {
2955 kill_css(css);
2956 } else {
Tejun Heof63070d2014-07-08 18:02:57 -04002957 cgroup_clear_dir(child, 1 << ssid);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002958 if (ss->css_reset)
2959 ss->css_reset(css);
2960 }
Tejun Heof63070d2014-07-08 18:02:57 -04002961 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002962 }
2963
Tejun Heo56c807b2014-11-18 02:49:51 -05002964 /*
2965 * The effective csses of all the descendants (excluding @cgrp) may
2966 * have changed. Subsystems can optionally subscribe to this event
2967 * by implementing ->css_e_css_changed() which is invoked if any of
2968 * the effective csses seen from the css's cgroup may have changed.
2969 */
2970 for_each_subsys(ss, ssid) {
2971 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
2972 struct cgroup_subsys_state *css;
2973
2974 if (!ss->css_e_css_changed || !this_css)
2975 continue;
2976
2977 css_for_each_descendant_pre(css, this_css)
2978 if (css != this_css)
2979 ss->css_e_css_changed(css);
2980 }
2981
Tejun Heof8f22e52014-04-23 11:13:16 -04002982 kernfs_activate(cgrp->kn);
2983 ret = 0;
2984out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002985 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002986 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002987
2988err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05002989 cgrp->subtree_control = old_sc;
2990 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04002991
2992 for_each_subsys(ss, ssid) {
2993 if (!(enable & (1 << ssid)))
2994 continue;
2995
2996 cgroup_for_each_live_child(child, cgrp) {
2997 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04002998
2999 if (!css)
3000 continue;
3001
3002 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04003003 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04003004 else
3005 cgroup_clear_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003006 }
3007 }
3008 goto out_unlock;
3009}
3010
Tejun Heo842b5972014-04-25 18:28:02 -04003011static int cgroup_populated_show(struct seq_file *seq, void *v)
3012{
3013 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
3014 return 0;
3015}
3016
Tejun Heo2bd59d42014-02-11 11:52:49 -05003017static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3018 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003019{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003020 struct cgroup *cgrp = of->kn->parent->priv;
3021 struct cftype *cft = of->kn->priv;
3022 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003023 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003024
Tejun Heob4168642014-05-13 12:16:21 -04003025 if (cft->write)
3026 return cft->write(of, buf, nbytes, off);
3027
Tejun Heo2bd59d42014-02-11 11:52:49 -05003028 /*
3029 * kernfs guarantees that a file isn't deleted with operations in
3030 * flight, which means that the matching css is and stays alive and
3031 * doesn't need to be pinned. The RCU locking is not necessary
3032 * either. It's just for the convenience of using cgroup_css().
3033 */
3034 rcu_read_lock();
3035 css = cgroup_css(cgrp, cft->ss);
3036 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003037
Tejun Heo451af502014-05-13 12:16:21 -04003038 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003039 unsigned long long v;
3040 ret = kstrtoull(buf, 0, &v);
3041 if (!ret)
3042 ret = cft->write_u64(css, cft, v);
3043 } else if (cft->write_s64) {
3044 long long v;
3045 ret = kstrtoll(buf, 0, &v);
3046 if (!ret)
3047 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003048 } else {
3049 ret = -EINVAL;
3050 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003051
Tejun Heoa742c592013-12-05 12:28:03 -05003052 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003053}
3054
Tejun Heo6612f052013-12-05 12:28:04 -05003055static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003056{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003057 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003058}
3059
3060static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3061{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003062 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003063}
3064
3065static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3066{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003067 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003068}
3069
3070static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3071{
Tejun Heo7da11272013-12-05 12:28:04 -05003072 struct cftype *cft = seq_cft(m);
3073 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003074
Tejun Heo2da8ca82013-12-05 12:28:04 -05003075 if (cft->seq_show)
3076 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003077
Tejun Heo896f5192013-12-05 12:28:04 -05003078 if (cft->read_u64)
3079 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3080 else if (cft->read_s64)
3081 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3082 else
3083 return -EINVAL;
3084 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003085}
3086
Tejun Heo2bd59d42014-02-11 11:52:49 -05003087static struct kernfs_ops cgroup_kf_single_ops = {
3088 .atomic_write_len = PAGE_SIZE,
3089 .write = cgroup_file_write,
3090 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003091};
3092
Tejun Heo2bd59d42014-02-11 11:52:49 -05003093static struct kernfs_ops cgroup_kf_ops = {
3094 .atomic_write_len = PAGE_SIZE,
3095 .write = cgroup_file_write,
3096 .seq_start = cgroup_seqfile_start,
3097 .seq_next = cgroup_seqfile_next,
3098 .seq_stop = cgroup_seqfile_stop,
3099 .seq_show = cgroup_seqfile_show,
3100};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003101
3102/*
3103 * cgroup_rename - Only allow simple rename of directories in place.
3104 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003105static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3106 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003107{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003108 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003109 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003110
Tejun Heo2bd59d42014-02-11 11:52:49 -05003111 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003112 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003113 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003114 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003115
Tejun Heo6db8e852013-06-14 11:18:22 -07003116 /*
3117 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003118 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003119 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003120 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003121 return -EPERM;
3122
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003123 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003124 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003125 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003126 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003127 */
3128 kernfs_break_active_protection(new_parent);
3129 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003130
Tejun Heo2bd59d42014-02-11 11:52:49 -05003131 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003132
Tejun Heo2bd59d42014-02-11 11:52:49 -05003133 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003134
Tejun Heo2bd59d42014-02-11 11:52:49 -05003135 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003136
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003137 kernfs_unbreak_active_protection(kn);
3138 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003139 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003140}
3141
Tejun Heo49957f82014-04-07 16:44:47 -04003142/* set uid and gid of cgroup dirs and files to that of the creator */
3143static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3144{
3145 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3146 .ia_uid = current_fsuid(),
3147 .ia_gid = current_fsgid(), };
3148
3149 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3150 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3151 return 0;
3152
3153 return kernfs_setattr(kn, &iattr);
3154}
3155
Tejun Heo2bb566c2013-08-08 20:11:23 -04003156static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003157{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003158 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003159 struct kernfs_node *kn;
3160 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003161 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003162
Tejun Heo2bd59d42014-02-11 11:52:49 -05003163#ifdef CONFIG_DEBUG_LOCK_ALLOC
3164 key = &cft->lockdep_key;
3165#endif
3166 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3167 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003168 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003169 if (IS_ERR(kn))
3170 return PTR_ERR(kn);
3171
3172 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003173 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003174 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003175 return ret;
3176 }
3177
Tejun Heo187fe842015-06-18 16:54:28 -04003178 if (cft->write == cgroup_procs_write)
3179 cgrp->procs_kn = kn;
3180 else if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04003181 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04003182 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003183}
3184
Tejun Heob1f28d32013-06-28 16:24:10 -07003185/**
3186 * cgroup_addrm_files - add or remove files to a cgroup directory
3187 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07003188 * @cfts: array of cftypes to be added
3189 * @is_add: whether to add or remove
3190 *
3191 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04003192 * For removals, this function never fails. If addition fails, this
3193 * function doesn't remove files already added. The caller is responsible
3194 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07003195 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04003196static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
3197 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003198{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003199 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07003200 int ret;
3201
Tejun Heo01f64742014-05-13 12:19:23 -04003202 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003203
3204 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003205 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003206 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003207 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003208 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003209 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003210 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003211 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003212 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003213 continue;
3214
Li Zefan2739d3c2013-01-21 18:18:33 +08003215 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003216 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003217 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003218 pr_warn("%s: failed to add %s, err=%d\n",
3219 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07003220 return ret;
3221 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003222 } else {
3223 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003224 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003225 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003226 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003227}
3228
Tejun Heo21a2d342014-02-12 09:29:49 -05003229static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003230{
3231 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003232 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003233 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003234 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003235 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003236
Tejun Heo01f64742014-05-13 12:19:23 -04003237 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003238
Li Zefane8c82d22013-06-18 18:48:37 +08003239 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003240 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003241 struct cgroup *cgrp = css->cgroup;
3242
Li Zefane8c82d22013-06-18 18:48:37 +08003243 if (cgroup_is_dead(cgrp))
3244 continue;
3245
Tejun Heo21a2d342014-02-12 09:29:49 -05003246 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003247 if (ret)
3248 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003249 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003250
3251 if (is_add && !ret)
3252 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003253 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003254}
3255
Tejun Heo2da440a2014-02-11 11:52:48 -05003256static void cgroup_exit_cftypes(struct cftype *cfts)
3257{
3258 struct cftype *cft;
3259
Tejun Heo2bd59d42014-02-11 11:52:49 -05003260 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3261 /* free copy for custom atomic_write_len, see init_cftypes() */
3262 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3263 kfree(cft->kf_ops);
3264 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003265 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003266
3267 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003268 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003269 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003270}
3271
Tejun Heo2bd59d42014-02-11 11:52:49 -05003272static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003273{
3274 struct cftype *cft;
3275
Tejun Heo2bd59d42014-02-11 11:52:49 -05003276 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3277 struct kernfs_ops *kf_ops;
3278
Tejun Heo0adb0702014-02-12 09:29:48 -05003279 WARN_ON(cft->ss || cft->kf_ops);
3280
Tejun Heo2bd59d42014-02-11 11:52:49 -05003281 if (cft->seq_start)
3282 kf_ops = &cgroup_kf_ops;
3283 else
3284 kf_ops = &cgroup_kf_single_ops;
3285
3286 /*
3287 * Ugh... if @cft wants a custom max_write_len, we need to
3288 * make a copy of kf_ops to set its atomic_write_len.
3289 */
3290 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3291 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3292 if (!kf_ops) {
3293 cgroup_exit_cftypes(cfts);
3294 return -ENOMEM;
3295 }
3296 kf_ops->atomic_write_len = cft->max_write_len;
3297 }
3298
3299 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003300 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003301 }
3302
3303 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003304}
3305
Tejun Heo21a2d342014-02-12 09:29:49 -05003306static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3307{
Tejun Heo01f64742014-05-13 12:19:23 -04003308 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003309
3310 if (!cfts || !cfts[0].ss)
3311 return -ENOENT;
3312
3313 list_del(&cfts->node);
3314 cgroup_apply_cftypes(cfts, false);
3315 cgroup_exit_cftypes(cfts);
3316 return 0;
3317}
3318
Tejun Heo8e3f6542012-04-01 12:09:55 -07003319/**
Tejun Heo80b13582014-02-12 09:29:48 -05003320 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3321 * @cfts: zero-length name terminated array of cftypes
3322 *
3323 * Unregister @cfts. Files described by @cfts are removed from all
3324 * existing cgroups and all future cgroups won't have them either. This
3325 * function can be called anytime whether @cfts' subsys is attached or not.
3326 *
3327 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3328 * registered.
3329 */
3330int cgroup_rm_cftypes(struct cftype *cfts)
3331{
Tejun Heo21a2d342014-02-12 09:29:49 -05003332 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003333
Tejun Heo01f64742014-05-13 12:19:23 -04003334 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003335 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003336 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003337 return ret;
3338}
3339
3340/**
3341 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3342 * @ss: target cgroup subsystem
3343 * @cfts: zero-length name terminated array of cftypes
3344 *
3345 * Register @cfts to @ss. Files described by @cfts are created for all
3346 * existing cgroups to which @ss is attached and all future cgroups will
3347 * have them too. This function can be called anytime whether @ss is
3348 * attached or not.
3349 *
3350 * Returns 0 on successful registration, -errno on failure. Note that this
3351 * function currently returns 0 as long as @cfts registration is successful
3352 * even if some file creation attempts on existing cgroups fail.
3353 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003354static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003355{
Tejun Heo9ccece82013-06-28 16:24:11 -07003356 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003357
Li Zefanc731ae12014-06-05 17:16:30 +08003358 if (ss->disabled)
3359 return 0;
3360
Li Zefandc5736e2014-02-17 10:41:50 +08003361 if (!cfts || cfts[0].name[0] == '\0')
3362 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003363
Tejun Heo2bd59d42014-02-11 11:52:49 -05003364 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003365 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003366 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003367
Tejun Heo01f64742014-05-13 12:19:23 -04003368 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003369
Tejun Heo0adb0702014-02-12 09:29:48 -05003370 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003371 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003372 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003373 cgroup_rm_cftypes_locked(cfts);
3374
Tejun Heo01f64742014-05-13 12:19:23 -04003375 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003376 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003377}
Tejun Heo79578622012-04-01 12:09:56 -07003378
3379/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003380 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3381 * @ss: target cgroup subsystem
3382 * @cfts: zero-length name terminated array of cftypes
3383 *
3384 * Similar to cgroup_add_cftypes() but the added files are only used for
3385 * the default hierarchy.
3386 */
3387int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3388{
3389 struct cftype *cft;
3390
3391 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003392 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003393 return cgroup_add_cftypes(ss, cfts);
3394}
3395
3396/**
3397 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3398 * @ss: target cgroup subsystem
3399 * @cfts: zero-length name terminated array of cftypes
3400 *
3401 * Similar to cgroup_add_cftypes() but the added files are only used for
3402 * the legacy hierarchies.
3403 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003404int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3405{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003406 struct cftype *cft;
3407
Vivek Goyalfa8137b2014-08-08 11:44:03 -04003408 /*
3409 * If legacy_flies_on_dfl, we want to show the legacy files on the
3410 * dfl hierarchy but iff the target subsystem hasn't been updated
3411 * for the dfl hierarchy yet.
3412 */
3413 if (!cgroup_legacy_files_on_dfl ||
3414 ss->dfl_cftypes != ss->legacy_cftypes) {
3415 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3416 cft->flags |= __CFTYPE_NOT_ON_DFL;
3417 }
3418
Tejun Heo2cf669a2014-07-15 11:05:09 -04003419 return cgroup_add_cftypes(ss, cfts);
3420}
3421
Li Zefana043e3b2008-02-23 15:24:09 -08003422/**
3423 * cgroup_task_count - count the number of tasks in a cgroup.
3424 * @cgrp: the cgroup in question
3425 *
3426 * Return the number of tasks in the cgroup.
3427 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003428static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003429{
3430 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003431 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003432
Tejun Heo96d365e2014-02-13 06:58:40 -05003433 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003434 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3435 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003436 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003437 return count;
3438}
3439
Tejun Heo574bd9f2012-11-09 09:12:29 -08003440/**
Tejun Heo492eb212013-08-08 20:11:25 -04003441 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003442 * @pos: the current position (%NULL to initiate traversal)
3443 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003444 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003445 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003446 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003447 * that @parent and @pos are accessible. The next sibling is guaranteed to
3448 * be returned regardless of their states.
3449 *
3450 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3451 * css which finished ->css_online() is guaranteed to be visible in the
3452 * future iterations and will stay visible until the last reference is put.
3453 * A css which hasn't finished ->css_online() or already finished
3454 * ->css_offline() may show up during traversal. It's each subsystem's
3455 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003456 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003457struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3458 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003459{
Tejun Heoc2931b72014-05-16 13:22:51 -04003460 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003461
Tejun Heo8353da12014-05-13 12:19:23 -04003462 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003463
3464 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003465 * @pos could already have been unlinked from the sibling list.
3466 * Once a cgroup is removed, its ->sibling.next is no longer
3467 * updated when its next sibling changes. CSS_RELEASED is set when
3468 * @pos is taken off list, at which time its next pointer is valid,
3469 * and, as releases are serialized, the one pointed to by the next
3470 * pointer is guaranteed to not have started release yet. This
3471 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3472 * critical section, the one pointed to by its next pointer is
3473 * guaranteed to not have finished its RCU grace period even if we
3474 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003475 *
Tejun Heode3f0342014-05-16 13:22:49 -04003476 * If @pos has CSS_RELEASED set, its next pointer can't be
3477 * dereferenced; however, as each css is given a monotonically
3478 * increasing unique serial number and always appended to the
3479 * sibling list, the next one can be found by walking the parent's
3480 * children until the first css with higher serial number than
3481 * @pos's. While this path can be slower, it happens iff iteration
3482 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003483 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003484 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003485 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3486 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3487 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003488 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003489 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003490 if (next->serial_nr > pos->serial_nr)
3491 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003492 }
3493
Tejun Heo3b281af2014-04-23 11:13:15 -04003494 /*
3495 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003496 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003497 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003498 if (&next->sibling != &parent->children)
3499 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003500 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003501}
Tejun Heo53fa5262013-05-24 10:55:38 +09003502
3503/**
Tejun Heo492eb212013-08-08 20:11:25 -04003504 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003505 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003506 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003507 *
Tejun Heo492eb212013-08-08 20:11:25 -04003508 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003509 * to visit for pre-order traversal of @root's descendants. @root is
3510 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003511 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003512 * While this function requires cgroup_mutex or RCU read locking, it
3513 * doesn't require the whole traversal to be contained in a single critical
3514 * section. This function will return the correct next descendant as long
3515 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003516 *
3517 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3518 * css which finished ->css_online() is guaranteed to be visible in the
3519 * future iterations and will stay visible until the last reference is put.
3520 * A css which hasn't finished ->css_online() or already finished
3521 * ->css_offline() may show up during traversal. It's each subsystem's
3522 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003523 */
Tejun Heo492eb212013-08-08 20:11:25 -04003524struct cgroup_subsys_state *
3525css_next_descendant_pre(struct cgroup_subsys_state *pos,
3526 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003527{
Tejun Heo492eb212013-08-08 20:11:25 -04003528 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003529
Tejun Heo8353da12014-05-13 12:19:23 -04003530 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003531
Tejun Heobd8815a2013-08-08 20:11:27 -04003532 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003533 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003534 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003535
3536 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003537 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003538 if (next)
3539 return next;
3540
3541 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003542 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003543 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003544 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003545 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003546 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003547 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003548
3549 return NULL;
3550}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003551
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003552/**
Tejun Heo492eb212013-08-08 20:11:25 -04003553 * css_rightmost_descendant - return the rightmost descendant of a css
3554 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003555 *
Tejun Heo492eb212013-08-08 20:11:25 -04003556 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3557 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003558 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003559 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003560 * While this function requires cgroup_mutex or RCU read locking, it
3561 * doesn't require the whole traversal to be contained in a single critical
3562 * section. This function will return the correct rightmost descendant as
3563 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003564 */
Tejun Heo492eb212013-08-08 20:11:25 -04003565struct cgroup_subsys_state *
3566css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003567{
Tejun Heo492eb212013-08-08 20:11:25 -04003568 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003569
Tejun Heo8353da12014-05-13 12:19:23 -04003570 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003571
3572 do {
3573 last = pos;
3574 /* ->prev isn't RCU safe, walk ->next till the end */
3575 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003576 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003577 pos = tmp;
3578 } while (pos);
3579
3580 return last;
3581}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003582
Tejun Heo492eb212013-08-08 20:11:25 -04003583static struct cgroup_subsys_state *
3584css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003585{
Tejun Heo492eb212013-08-08 20:11:25 -04003586 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003587
3588 do {
3589 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003590 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003591 } while (pos);
3592
3593 return last;
3594}
3595
3596/**
Tejun Heo492eb212013-08-08 20:11:25 -04003597 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003598 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003599 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003600 *
Tejun Heo492eb212013-08-08 20:11:25 -04003601 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003602 * to visit for post-order traversal of @root's descendants. @root is
3603 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003604 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003605 * While this function requires cgroup_mutex or RCU read locking, it
3606 * doesn't require the whole traversal to be contained in a single critical
3607 * section. This function will return the correct next descendant as long
3608 * as both @pos and @cgroup are accessible and @pos is a descendant of
3609 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003610 *
3611 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3612 * css which finished ->css_online() is guaranteed to be visible in the
3613 * future iterations and will stay visible until the last reference is put.
3614 * A css which hasn't finished ->css_online() or already finished
3615 * ->css_offline() may show up during traversal. It's each subsystem's
3616 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003617 */
Tejun Heo492eb212013-08-08 20:11:25 -04003618struct cgroup_subsys_state *
3619css_next_descendant_post(struct cgroup_subsys_state *pos,
3620 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003621{
Tejun Heo492eb212013-08-08 20:11:25 -04003622 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003623
Tejun Heo8353da12014-05-13 12:19:23 -04003624 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003625
Tejun Heo58b79a92013-09-06 15:31:08 -04003626 /* if first iteration, visit leftmost descendant which may be @root */
3627 if (!pos)
3628 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003629
Tejun Heobd8815a2013-08-08 20:11:27 -04003630 /* if we visited @root, we're done */
3631 if (pos == root)
3632 return NULL;
3633
Tejun Heo574bd9f2012-11-09 09:12:29 -08003634 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003635 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003636 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003637 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003638
3639 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003640 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003641}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003642
Tejun Heof3d46502014-05-16 13:22:52 -04003643/**
3644 * css_has_online_children - does a css have online children
3645 * @css: the target css
3646 *
3647 * Returns %true if @css has any online children; otherwise, %false. This
3648 * function can be called from any context but the caller is responsible
3649 * for synchronizing against on/offlining as necessary.
3650 */
3651bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003652{
Tejun Heof3d46502014-05-16 13:22:52 -04003653 struct cgroup_subsys_state *child;
3654 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003655
3656 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003657 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003658 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003659 ret = true;
3660 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003661 }
3662 }
3663 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003664 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003665}
3666
Tejun Heo0942eee2013-08-08 20:11:26 -04003667/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003668 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003669 * @it: the iterator to advance
3670 *
3671 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003672 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003673static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003674{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003675 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003676 struct cgrp_cset_link *link;
3677 struct css_set *cset;
3678
3679 /* Advance to the next non-empty css_set */
3680 do {
3681 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003682 if (l == it->cset_head) {
3683 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003684 return;
3685 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003686
3687 if (it->ss) {
3688 cset = container_of(l, struct css_set,
3689 e_cset_node[it->ss->id]);
3690 } else {
3691 link = list_entry(l, struct cgrp_cset_link, cset_link);
3692 cset = link->cset;
3693 }
Tejun Heoc7561122014-02-25 10:04:01 -05003694 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3695
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003696 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003697
3698 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003699 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003700 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003701 it->task_pos = cset->mg_tasks.next;
3702
3703 it->tasks_head = &cset->tasks;
3704 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003705}
3706
Tejun Heo0942eee2013-08-08 20:11:26 -04003707/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003708 * css_task_iter_start - initiate task iteration
3709 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003710 * @it: the task iterator to use
3711 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003712 * Initiate iteration through the tasks of @css. The caller can call
3713 * css_task_iter_next() to walk through the tasks until the function
3714 * returns NULL. On completion of iteration, css_task_iter_end() must be
3715 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003716 *
3717 * Note that this function acquires a lock which is released when the
3718 * iteration finishes. The caller can't sleep while iteration is in
3719 * progress.
3720 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003721void css_task_iter_start(struct cgroup_subsys_state *css,
3722 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003723 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003724{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003725 /* no one should try to iterate before mounting cgroups */
3726 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003727
Tejun Heo96d365e2014-02-13 06:58:40 -05003728 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003729
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003730 it->ss = css->ss;
3731
3732 if (it->ss)
3733 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3734 else
3735 it->cset_pos = &css->cgroup->cset_links;
3736
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003737 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003738
Tejun Heo72ec7022013-08-08 20:11:26 -04003739 css_advance_task_iter(it);
Paul Menagebd89aab2007-10-18 23:40:44 -07003740}
Paul Menage817929e2007-10-18 23:39:36 -07003741
Tejun Heo0942eee2013-08-08 20:11:26 -04003742/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003743 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003744 * @it: the task iterator being iterated
3745 *
3746 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003747 * initialized via css_task_iter_start(). Returns NULL when the iteration
3748 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003749 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003750struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003751{
3752 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003753 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003754
3755 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003756 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003757 return NULL;
3758 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003759
3760 /*
3761 * Advance iterator to find next entry. cset->tasks is consumed
3762 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3763 * next cset.
3764 */
Paul Menage817929e2007-10-18 23:39:36 -07003765 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003766
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003767 if (l == it->tasks_head)
3768 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003769
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003770 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003771 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003772 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003773 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003774
Paul Menage817929e2007-10-18 23:39:36 -07003775 return res;
3776}
3777
Tejun Heo0942eee2013-08-08 20:11:26 -04003778/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003779 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003780 * @it: the task iterator to finish
3781 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003782 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003783 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003784void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003785 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003786{
Tejun Heo96d365e2014-02-13 06:58:40 -05003787 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003788}
3789
3790/**
3791 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3792 * @to: cgroup to which the tasks will be moved
3793 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003794 *
3795 * Locking rules between cgroup_post_fork() and the migration path
3796 * guarantee that, if a task is forking while being migrated, the new child
3797 * is guaranteed to be either visible in the source cgroup after the
3798 * parent's migration is complete or put into the target cgroup. No task
3799 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003800 */
3801int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3802{
Tejun Heo952aaa12014-02-25 10:04:03 -05003803 LIST_HEAD(preloaded_csets);
3804 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003805 struct css_task_iter it;
3806 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003807 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003808
Tejun Heo952aaa12014-02-25 10:04:03 -05003809 mutex_lock(&cgroup_mutex);
3810
3811 /* all tasks in @from are being moved, all csets are source */
3812 down_read(&css_set_rwsem);
3813 list_for_each_entry(link, &from->cset_links, cset_link)
3814 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3815 up_read(&css_set_rwsem);
3816
3817 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3818 if (ret)
3819 goto out_err;
3820
3821 /*
3822 * Migrate tasks one-by-one until @form is empty. This fails iff
3823 * ->can_attach() fails.
3824 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003825 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003826 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003827 task = css_task_iter_next(&it);
3828 if (task)
3829 get_task_struct(task);
3830 css_task_iter_end(&it);
3831
3832 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003833 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003834 put_task_struct(task);
3835 }
3836 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003837out_err:
3838 cgroup_migrate_finish(&preloaded_csets);
3839 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003840 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003841}
3842
Paul Menage817929e2007-10-18 23:39:36 -07003843/*
Ben Blum102a7752009-09-23 15:56:26 -07003844 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003845 *
3846 * Reading this file can return large amounts of data if a cgroup has
3847 * *lots* of attached tasks. So it may need several calls to read(),
3848 * but we cannot guarantee that the information we produce is correct
3849 * unless we produce it entirely atomically.
3850 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003851 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003852
Li Zefan24528252012-01-20 11:58:43 +08003853/* which pidlist file are we talking about? */
3854enum cgroup_filetype {
3855 CGROUP_FILE_PROCS,
3856 CGROUP_FILE_TASKS,
3857};
3858
3859/*
3860 * A pidlist is a list of pids that virtually represents the contents of one
3861 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3862 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3863 * to the cgroup.
3864 */
3865struct cgroup_pidlist {
3866 /*
3867 * used to find which pidlist is wanted. doesn't change as long as
3868 * this particular list stays in the list.
3869 */
3870 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3871 /* array of xids */
3872 pid_t *list;
3873 /* how many elements the above list has */
3874 int length;
Li Zefan24528252012-01-20 11:58:43 +08003875 /* each of these stored in a list by its cgroup */
3876 struct list_head links;
3877 /* pointer to the cgroup we belong to, for list removal purposes */
3878 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003879 /* for delayed destruction */
3880 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003881};
3882
Paul Menagebbcb81d2007-10-18 23:39:32 -07003883/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003884 * The following two functions "fix" the issue where there are more pids
3885 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3886 * TODO: replace with a kernel-wide solution to this problem
3887 */
3888#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3889static void *pidlist_allocate(int count)
3890{
3891 if (PIDLIST_TOO_LARGE(count))
3892 return vmalloc(count * sizeof(pid_t));
3893 else
3894 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3895}
Tejun Heob1a21362013-11-29 10:42:58 -05003896
Ben Blumd1d9fd32009-09-23 15:56:28 -07003897static void pidlist_free(void *p)
3898{
Bandan Das58794512015-03-02 17:51:10 -05003899 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003900}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003901
3902/*
Tejun Heob1a21362013-11-29 10:42:58 -05003903 * Used to destroy all pidlists lingering waiting for destroy timer. None
3904 * should be left afterwards.
3905 */
3906static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3907{
3908 struct cgroup_pidlist *l, *tmp_l;
3909
3910 mutex_lock(&cgrp->pidlist_mutex);
3911 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3912 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3913 mutex_unlock(&cgrp->pidlist_mutex);
3914
3915 flush_workqueue(cgroup_pidlist_destroy_wq);
3916 BUG_ON(!list_empty(&cgrp->pidlists));
3917}
3918
3919static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3920{
3921 struct delayed_work *dwork = to_delayed_work(work);
3922 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3923 destroy_dwork);
3924 struct cgroup_pidlist *tofree = NULL;
3925
3926 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003927
3928 /*
Tejun Heo04502362013-11-29 10:42:59 -05003929 * Destroy iff we didn't get queued again. The state won't change
3930 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003931 */
Tejun Heo04502362013-11-29 10:42:59 -05003932 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003933 list_del(&l->links);
3934 pidlist_free(l->list);
3935 put_pid_ns(l->key.ns);
3936 tofree = l;
3937 }
3938
Tejun Heob1a21362013-11-29 10:42:58 -05003939 mutex_unlock(&l->owner->pidlist_mutex);
3940 kfree(tofree);
3941}
3942
3943/*
Ben Blum102a7752009-09-23 15:56:26 -07003944 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003945 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003946 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003947static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003948{
Ben Blum102a7752009-09-23 15:56:26 -07003949 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003950
3951 /*
3952 * we presume the 0th element is unique, so i starts at 1. trivial
3953 * edge cases first; no work needs to be done for either
3954 */
3955 if (length == 0 || length == 1)
3956 return length;
3957 /* src and dest walk down the list; dest counts unique elements */
3958 for (src = 1; src < length; src++) {
3959 /* find next unique element */
3960 while (list[src] == list[src-1]) {
3961 src++;
3962 if (src == length)
3963 goto after;
3964 }
3965 /* dest always points to where the next unique element goes */
3966 list[dest] = list[src];
3967 dest++;
3968 }
3969after:
Ben Blum102a7752009-09-23 15:56:26 -07003970 return dest;
3971}
3972
Tejun Heoafb2bc12013-11-29 10:42:59 -05003973/*
3974 * The two pid files - task and cgroup.procs - guaranteed that the result
3975 * is sorted, which forced this whole pidlist fiasco. As pid order is
3976 * different per namespace, each namespace needs differently sorted list,
3977 * making it impossible to use, for example, single rbtree of member tasks
3978 * sorted by task pointer. As pidlists can be fairly large, allocating one
3979 * per open file is dangerous, so cgroup had to implement shared pool of
3980 * pidlists keyed by cgroup and namespace.
3981 *
3982 * All this extra complexity was caused by the original implementation
3983 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04003984 * want to do away with it. Explicitly scramble sort order if on the
3985 * default hierarchy so that no such expectation exists in the new
3986 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05003987 *
3988 * Scrambling is done by swapping every two consecutive bits, which is
3989 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3990 */
3991static pid_t pid_fry(pid_t pid)
3992{
3993 unsigned a = pid & 0x55555555;
3994 unsigned b = pid & 0xAAAAAAAA;
3995
3996 return (a << 1) | (b >> 1);
3997}
3998
3999static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
4000{
Tejun Heoaa6ec292014-07-09 10:08:08 -04004001 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004002 return pid_fry(pid);
4003 else
4004 return pid;
4005}
4006
Ben Blum102a7752009-09-23 15:56:26 -07004007static int cmppid(const void *a, const void *b)
4008{
4009 return *(pid_t *)a - *(pid_t *)b;
4010}
4011
Tejun Heoafb2bc12013-11-29 10:42:59 -05004012static int fried_cmppid(const void *a, const void *b)
4013{
4014 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
4015}
4016
Ben Blum72a8cb32009-09-23 15:56:27 -07004017static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
4018 enum cgroup_filetype type)
4019{
4020 struct cgroup_pidlist *l;
4021 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004022 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08004023
Tejun Heoe6b81712013-11-29 10:42:59 -05004024 lockdep_assert_held(&cgrp->pidlist_mutex);
4025
4026 list_for_each_entry(l, &cgrp->pidlists, links)
4027 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07004028 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004029 return NULL;
4030}
4031
Ben Blum72a8cb32009-09-23 15:56:27 -07004032/*
4033 * find the appropriate pidlist for our purpose (given procs vs tasks)
4034 * returns with the lock on that pidlist already held, and takes care
4035 * of the use count, or returns NULL with no locks held if we're out of
4036 * memory.
4037 */
Tejun Heoe6b81712013-11-29 10:42:59 -05004038static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
4039 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07004040{
4041 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07004042
Tejun Heoe6b81712013-11-29 10:42:59 -05004043 lockdep_assert_held(&cgrp->pidlist_mutex);
4044
4045 l = cgroup_pidlist_find(cgrp, type);
4046 if (l)
4047 return l;
4048
Ben Blum72a8cb32009-09-23 15:56:27 -07004049 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07004050 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05004051 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07004052 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05004053
Tejun Heob1a21362013-11-29 10:42:58 -05004054 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07004055 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05004056 /* don't need task_nsproxy() if we're looking at ourself */
4057 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07004058 l->owner = cgrp;
4059 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07004060 return l;
4061}
4062
4063/*
Ben Blum102a7752009-09-23 15:56:26 -07004064 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4065 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004066static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4067 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004068{
4069 pid_t *array;
4070 int length;
4071 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004072 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004073 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004074 struct cgroup_pidlist *l;
4075
Tejun Heo4bac00d2013-11-29 10:42:59 -05004076 lockdep_assert_held(&cgrp->pidlist_mutex);
4077
Ben Blum102a7752009-09-23 15:56:26 -07004078 /*
4079 * If cgroup gets more users after we read count, we won't have
4080 * enough space - tough. This race is indistinguishable to the
4081 * caller from the case that the additional cgroup users didn't
4082 * show up until sometime later on.
4083 */
4084 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004085 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004086 if (!array)
4087 return -ENOMEM;
4088 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004089 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004090 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004091 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004092 break;
Ben Blum102a7752009-09-23 15:56:26 -07004093 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004094 if (type == CGROUP_FILE_PROCS)
4095 pid = task_tgid_vnr(tsk);
4096 else
4097 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004098 if (pid > 0) /* make sure to only use valid results */
4099 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004100 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004101 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004102 length = n;
4103 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004104 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004105 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4106 else
4107 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004108 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004109 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004110
Tejun Heoe6b81712013-11-29 10:42:59 -05004111 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004112 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004113 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004114 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004115 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004116
4117 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004118 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004119 l->list = array;
4120 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004121 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004122 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004123}
4124
Balbir Singh846c7bb2007-10-18 23:39:44 -07004125/**
Li Zefana043e3b2008-02-23 15:24:09 -08004126 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004127 * @stats: cgroupstats to fill information into
4128 * @dentry: A dentry entry belonging to the cgroup for which stats have
4129 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004130 *
4131 * Build and fill cgroupstats so that taskstats can export it to user
4132 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004133 */
4134int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4135{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004136 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004137 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004138 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004139 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004140
Tejun Heo2bd59d42014-02-11 11:52:49 -05004141 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4142 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4143 kernfs_type(kn) != KERNFS_DIR)
4144 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004145
Li Zefanbad34662014-02-14 16:54:28 +08004146 mutex_lock(&cgroup_mutex);
4147
Tejun Heo2bd59d42014-02-11 11:52:49 -05004148 /*
4149 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004150 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004151 * @kn->priv is RCU safe. Let's do the RCU dancing.
4152 */
4153 rcu_read_lock();
4154 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004155 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004156 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004157 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004158 return -ENOENT;
4159 }
Li Zefanbad34662014-02-14 16:54:28 +08004160 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004161
Tejun Heo9d800df2014-05-14 09:15:00 -04004162 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004163 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004164 switch (tsk->state) {
4165 case TASK_RUNNING:
4166 stats->nr_running++;
4167 break;
4168 case TASK_INTERRUPTIBLE:
4169 stats->nr_sleeping++;
4170 break;
4171 case TASK_UNINTERRUPTIBLE:
4172 stats->nr_uninterruptible++;
4173 break;
4174 case TASK_STOPPED:
4175 stats->nr_stopped++;
4176 break;
4177 default:
4178 if (delayacct_is_task_waiting_on_io(tsk))
4179 stats->nr_io_wait++;
4180 break;
4181 }
4182 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004183 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004184
Li Zefanbad34662014-02-14 16:54:28 +08004185 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004186 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004187}
4188
Paul Menage8f3ff202009-09-23 15:56:25 -07004189
Paul Menagecc31edc2008-10-18 20:28:04 -07004190/*
Ben Blum102a7752009-09-23 15:56:26 -07004191 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004192 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004193 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004194 */
4195
Ben Blum102a7752009-09-23 15:56:26 -07004196static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004197{
4198 /*
4199 * Initially we receive a position value that corresponds to
4200 * one more than the last pid shown (or 0 on the first call or
4201 * after a seek to the start). Use a binary-search to find the
4202 * next pid to display, if any
4203 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004204 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004205 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004206 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004207 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004208 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004209 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004210
Tejun Heo4bac00d2013-11-29 10:42:59 -05004211 mutex_lock(&cgrp->pidlist_mutex);
4212
4213 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004214 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004215 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004216 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004217 * could already have been destroyed.
4218 */
Tejun Heo5d224442013-12-05 12:28:04 -05004219 if (of->priv)
4220 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004221
4222 /*
4223 * Either this is the first start() after open or the matching
4224 * pidlist has been destroyed inbetween. Create a new one.
4225 */
Tejun Heo5d224442013-12-05 12:28:04 -05004226 if (!of->priv) {
4227 ret = pidlist_array_load(cgrp, type,
4228 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004229 if (ret)
4230 return ERR_PTR(ret);
4231 }
Tejun Heo5d224442013-12-05 12:28:04 -05004232 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004233
Paul Menagecc31edc2008-10-18 20:28:04 -07004234 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004235 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004236
Paul Menagecc31edc2008-10-18 20:28:04 -07004237 while (index < end) {
4238 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004239 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004240 index = mid;
4241 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004242 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004243 index = mid + 1;
4244 else
4245 end = mid;
4246 }
4247 }
4248 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004249 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004250 return NULL;
4251 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004252 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004253 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004254 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004255}
4256
Ben Blum102a7752009-09-23 15:56:26 -07004257static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004258{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004259 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004260 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004261
Tejun Heo5d224442013-12-05 12:28:04 -05004262 if (l)
4263 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004264 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004265 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004266}
4267
Ben Blum102a7752009-09-23 15:56:26 -07004268static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004269{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004270 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004271 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004272 pid_t *p = v;
4273 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004274 /*
4275 * Advance to the next pid in the array. If this goes off the
4276 * end, we're done
4277 */
4278 p++;
4279 if (p >= end) {
4280 return NULL;
4281 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004282 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004283 return p;
4284 }
4285}
4286
Ben Blum102a7752009-09-23 15:56:26 -07004287static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004288{
Joe Perches94ff2122015-04-15 16:18:20 -07004289 seq_printf(s, "%d\n", *(int *)v);
4290
4291 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004292}
4293
Tejun Heo182446d2013-08-08 20:11:24 -04004294static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4295 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004296{
Tejun Heo182446d2013-08-08 20:11:24 -04004297 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004298}
4299
Tejun Heo182446d2013-08-08 20:11:24 -04004300static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4301 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004302{
Paul Menage6379c102008-07-25 01:47:01 -07004303 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004304 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004305 else
Tejun Heo182446d2013-08-08 20:11:24 -04004306 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004307 return 0;
4308}
4309
Tejun Heo182446d2013-08-08 20:11:24 -04004310static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4311 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004312{
Tejun Heo182446d2013-08-08 20:11:24 -04004313 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004314}
4315
Tejun Heo182446d2013-08-08 20:11:24 -04004316static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4317 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004318{
4319 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004320 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004321 else
Tejun Heo182446d2013-08-08 20:11:24 -04004322 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004323 return 0;
4324}
4325
Tejun Heoa14c6872014-07-15 11:05:09 -04004326/* cgroup core interface files for the default hierarchy */
4327static struct cftype cgroup_dfl_base_files[] = {
4328 {
4329 .name = "cgroup.procs",
4330 .seq_start = cgroup_pidlist_start,
4331 .seq_next = cgroup_pidlist_next,
4332 .seq_stop = cgroup_pidlist_stop,
4333 .seq_show = cgroup_pidlist_show,
4334 .private = CGROUP_FILE_PROCS,
4335 .write = cgroup_procs_write,
4336 .mode = S_IRUGO | S_IWUSR,
4337 },
4338 {
4339 .name = "cgroup.controllers",
4340 .flags = CFTYPE_ONLY_ON_ROOT,
4341 .seq_show = cgroup_root_controllers_show,
4342 },
4343 {
4344 .name = "cgroup.controllers",
4345 .flags = CFTYPE_NOT_ON_ROOT,
4346 .seq_show = cgroup_controllers_show,
4347 },
4348 {
4349 .name = "cgroup.subtree_control",
4350 .seq_show = cgroup_subtree_control_show,
4351 .write = cgroup_subtree_control_write,
4352 },
4353 {
4354 .name = "cgroup.populated",
4355 .flags = CFTYPE_NOT_ON_ROOT,
4356 .seq_show = cgroup_populated_show,
4357 },
4358 { } /* terminate */
4359};
4360
4361/* cgroup core interface files for the legacy hierarchies */
4362static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004363 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004364 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004365 .seq_start = cgroup_pidlist_start,
4366 .seq_next = cgroup_pidlist_next,
4367 .seq_stop = cgroup_pidlist_stop,
4368 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004369 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004370 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004371 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004372 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004373 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004374 .name = "cgroup.clone_children",
4375 .read_u64 = cgroup_clone_children_read,
4376 .write_u64 = cgroup_clone_children_write,
4377 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004378 {
Tejun Heo873fe092013-04-14 20:15:26 -07004379 .name = "cgroup.sane_behavior",
4380 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004381 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004382 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004383 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004384 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004385 .seq_start = cgroup_pidlist_start,
4386 .seq_next = cgroup_pidlist_next,
4387 .seq_stop = cgroup_pidlist_stop,
4388 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004389 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004390 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004391 .mode = S_IRUGO | S_IWUSR,
4392 },
4393 {
4394 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004395 .read_u64 = cgroup_read_notify_on_release,
4396 .write_u64 = cgroup_write_notify_on_release,
4397 },
Tejun Heo873fe092013-04-14 20:15:26 -07004398 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004399 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004400 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004401 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004402 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004403 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004404 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004405 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004406};
4407
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004408/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004409 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004410 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004411 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004412 *
4413 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004414 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10004415static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004417 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004418 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004419
Tejun Heo8e3f6542012-04-01 12:09:55 -07004420 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004421 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004422 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004423
Tejun Heo69dfa002014-05-04 15:09:13 -04004424 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004425 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004426
Tejun Heo0adb0702014-02-12 09:29:48 -05004427 list_for_each_entry(cfts, &ss->cfts, node) {
4428 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004429 if (ret < 0)
4430 goto err;
4431 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004432 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004433 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004434err:
4435 cgroup_clear_dir(cgrp, subsys_mask);
4436 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004437}
4438
Tejun Heo0c21ead2013-08-13 20:22:51 -04004439/*
4440 * css destruction is four-stage process.
4441 *
4442 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4443 * Implemented in kill_css().
4444 *
4445 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004446 * and thus css_tryget_online() is guaranteed to fail, the css can be
4447 * offlined by invoking offline_css(). After offlining, the base ref is
4448 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004449 *
4450 * 3. When the percpu_ref reaches zero, the only possible remaining
4451 * accessors are inside RCU read sections. css_release() schedules the
4452 * RCU callback.
4453 *
4454 * 4. After the grace period, the css can be freed. Implemented in
4455 * css_free_work_fn().
4456 *
4457 * It is actually hairier because both step 2 and 4 require process context
4458 * and thus involve punting to css->destroy_work adding two additional
4459 * steps to the already complex sequence.
4460 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004461static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004462{
4463 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004464 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004465 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004466 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004467
Tejun Heo9a1049d2014-06-28 08:10:14 -04004468 percpu_ref_exit(&css->refcnt);
4469
Vladimir Davydov01e58652015-02-12 14:59:26 -08004470 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004471 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004472 int id = css->id;
4473
Tejun Heo9d755d32014-05-14 09:15:02 -04004474 if (css->parent)
4475 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004476
Vladimir Davydov01e58652015-02-12 14:59:26 -08004477 ss->css_free(css);
4478 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004479 cgroup_put(cgrp);
4480 } else {
4481 /* cgroup free path */
4482 atomic_dec(&cgrp->root->nr_cgrps);
4483 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004484 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004485
Tejun Heod51f39b2014-05-16 13:22:48 -04004486 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004487 /*
4488 * We get a ref to the parent, and put the ref when
4489 * this cgroup is being freed, so it's guaranteed
4490 * that the parent won't be destroyed before its
4491 * children.
4492 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004493 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004494 kernfs_put(cgrp->kn);
4495 kfree(cgrp);
4496 } else {
4497 /*
4498 * This is root cgroup's refcnt reaching zero,
4499 * which indicates that the root should be
4500 * released.
4501 */
4502 cgroup_destroy_root(cgrp->root);
4503 }
4504 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004505}
4506
4507static void css_free_rcu_fn(struct rcu_head *rcu_head)
4508{
4509 struct cgroup_subsys_state *css =
4510 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4511
Tejun Heo0c21ead2013-08-13 20:22:51 -04004512 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004513 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004514}
4515
Tejun Heo25e15d82014-05-14 09:15:02 -04004516static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004517{
4518 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004519 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004520 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004521 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004522
Tejun Heo1fed1b22014-05-16 13:22:49 -04004523 mutex_lock(&cgroup_mutex);
4524
Tejun Heode3f0342014-05-16 13:22:49 -04004525 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004526 list_del_rcu(&css->sibling);
4527
Tejun Heo9d755d32014-05-14 09:15:02 -04004528 if (ss) {
4529 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004530 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004531 if (ss->css_released)
4532 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004533 } else {
4534 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004535 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4536 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004537
4538 /*
4539 * There are two control paths which try to determine
4540 * cgroup from dentry without going through kernfs -
4541 * cgroupstats_build() and css_tryget_online_from_dir().
4542 * Those are supported by RCU protecting clearing of
4543 * cgrp->kn->priv backpointer.
4544 */
4545 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004546 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004547
Tejun Heo1fed1b22014-05-16 13:22:49 -04004548 mutex_unlock(&cgroup_mutex);
4549
Tejun Heo0c21ead2013-08-13 20:22:51 -04004550 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004551}
4552
Paul Menageddbcc7e2007-10-18 23:39:30 -07004553static void css_release(struct percpu_ref *ref)
4554{
4555 struct cgroup_subsys_state *css =
4556 container_of(ref, struct cgroup_subsys_state, refcnt);
4557
Tejun Heo25e15d82014-05-14 09:15:02 -04004558 INIT_WORK(&css->destroy_work, css_release_work_fn);
4559 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004560}
4561
Tejun Heoddfcada2014-05-04 15:09:14 -04004562static void init_and_link_css(struct cgroup_subsys_state *css,
4563 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004564{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004565 lockdep_assert_held(&cgroup_mutex);
4566
Tejun Heoddfcada2014-05-04 15:09:14 -04004567 cgroup_get(cgrp);
4568
Tejun Heod5c419b2014-05-16 13:22:48 -04004569 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004570 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004571 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004572 INIT_LIST_HEAD(&css->sibling);
4573 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004574 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004575
Tejun Heod51f39b2014-05-16 13:22:48 -04004576 if (cgroup_parent(cgrp)) {
4577 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004578 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004579 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004580
Tejun Heoca8bdca2013-08-26 18:40:56 -04004581 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004582}
4583
Li Zefan2a4ac632013-07-31 16:16:40 +08004584/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004585static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004586{
Tejun Heo623f9262013-08-13 11:01:55 -04004587 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004588 int ret = 0;
4589
Tejun Heoa31f2d32012-11-19 08:13:37 -08004590 lockdep_assert_held(&cgroup_mutex);
4591
Tejun Heo92fb9742012-11-19 08:13:38 -08004592 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004593 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004594 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004595 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004596 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004597 }
Tejun Heob1929db2012-11-19 08:13:38 -08004598 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004599}
4600
Li Zefan2a4ac632013-07-31 16:16:40 +08004601/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004602static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004603{
Tejun Heo623f9262013-08-13 11:01:55 -04004604 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004605
4606 lockdep_assert_held(&cgroup_mutex);
4607
4608 if (!(css->flags & CSS_ONLINE))
4609 return;
4610
Li Zefand7eeac12013-03-12 15:35:59 -07004611 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004612 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004613
Tejun Heoeb954192013-08-08 20:11:23 -04004614 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004615 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004616
4617 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004618}
4619
Tejun Heoc81c925a2013-12-06 15:11:56 -05004620/**
4621 * create_css - create a cgroup_subsys_state
4622 * @cgrp: the cgroup new css will be associated with
4623 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004624 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004625 *
4626 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004627 * css is online and installed in @cgrp with all interface files created if
4628 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004629 */
Tejun Heof63070d2014-07-08 18:02:57 -04004630static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4631 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004632{
Tejun Heod51f39b2014-05-16 13:22:48 -04004633 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004634 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004635 struct cgroup_subsys_state *css;
4636 int err;
4637
Tejun Heoc81c925a2013-12-06 15:11:56 -05004638 lockdep_assert_held(&cgroup_mutex);
4639
Tejun Heo1fed1b22014-05-16 13:22:49 -04004640 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004641 if (IS_ERR(css))
4642 return PTR_ERR(css);
4643
Tejun Heoddfcada2014-05-04 15:09:14 -04004644 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004645
Tejun Heo2aad2a82014-09-24 13:31:50 -04004646 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004647 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004648 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004649
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004650 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04004651 if (err < 0)
4652 goto err_free_percpu_ref;
4653 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004654
Tejun Heof63070d2014-07-08 18:02:57 -04004655 if (visible) {
4656 err = cgroup_populate_dir(cgrp, 1 << ss->id);
4657 if (err)
4658 goto err_free_id;
4659 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004660
4661 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004662 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004663 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004664
4665 err = online_css(css);
4666 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004667 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004668
Tejun Heoc81c925a2013-12-06 15:11:56 -05004669 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004670 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004671 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 -04004672 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004673 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004674 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004675 ss->warned_broken_hierarchy = true;
4676 }
4677
4678 return 0;
4679
Tejun Heo1fed1b22014-05-16 13:22:49 -04004680err_list_del:
4681 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004682 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004683err_free_id:
4684 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004685err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004686 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004687err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004688 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004689 return err;
4690}
4691
Tejun Heob3bfd982014-05-13 12:19:22 -04004692static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4693 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694{
Tejun Heoa9746d82014-05-13 12:19:22 -04004695 struct cgroup *parent, *cgrp;
4696 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004697 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004698 struct kernfs_node *kn;
Tejun Heoa14c6872014-07-15 11:05:09 -04004699 struct cftype *base_files;
Tejun Heob3bfd982014-05-13 12:19:22 -04004700 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004701
Alban Crequy71b1fb52014-08-18 12:20:20 +01004702 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4703 */
4704 if (strchr(name, '\n'))
4705 return -EINVAL;
4706
Tejun Heoa9746d82014-05-13 12:19:22 -04004707 parent = cgroup_kn_lock_live(parent_kn);
4708 if (!parent)
4709 return -ENODEV;
4710 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004711
Tejun Heo0a950f62012-11-19 09:02:12 -08004712 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004713 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004714 if (!cgrp) {
4715 ret = -ENOMEM;
4716 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004717 }
4718
Tejun Heo2aad2a82014-09-24 13:31:50 -04004719 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004720 if (ret)
4721 goto out_free_cgrp;
4722
Li Zefan0ab02ca2014-02-11 16:05:46 +08004723 /*
4724 * Temporarily set the pointer to NULL, so idr_find() won't return
4725 * a half-baked cgroup.
4726 */
Vladimir Davydovcf780b72015-08-03 15:32:26 +03004727 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004728 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004729 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004730 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004731 }
4732
Paul Menagecc31edc2008-10-18 20:28:04 -07004733 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004734
Tejun Heo9d800df2014-05-14 09:15:00 -04004735 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004736 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004737
Li Zefanb6abdb02008-03-04 14:28:19 -08004738 if (notify_on_release(parent))
4739 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4740
Tejun Heo2260e7f2012-11-19 08:13:38 -08004741 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4742 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004743
Tejun Heo2bd59d42014-02-11 11:52:49 -05004744 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004745 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004746 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004747 ret = PTR_ERR(kn);
4748 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004749 }
4750 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004751
Tejun Heo6f305582014-02-12 09:29:50 -05004752 /*
4753 * This extra ref will be put in cgroup_free_fn() and guarantees
4754 * that @cgrp->kn is always accessible.
4755 */
4756 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004757
Tejun Heo0cb51d72014-05-16 13:22:49 -04004758 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004759
Tejun Heo4e139af2012-11-19 08:13:36 -08004760 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004761 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004762 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004763 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004764
Tejun Heo0d802552013-12-06 15:11:56 -05004765 /*
4766 * @cgrp is now fully operational. If something fails after this
4767 * point, it'll be released via the normal destruction path.
4768 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004769 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004770
Tejun Heoba0f4d72014-05-13 12:19:22 -04004771 ret = cgroup_kn_set_ugid(kn);
4772 if (ret)
4773 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004774
Tejun Heoa14c6872014-07-15 11:05:09 -04004775 if (cgroup_on_dfl(cgrp))
4776 base_files = cgroup_dfl_base_files;
4777 else
4778 base_files = cgroup_legacy_base_files;
4779
4780 ret = cgroup_addrm_files(cgrp, base_files, true);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004781 if (ret)
4782 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004783
Tejun Heo9d403e92013-12-06 15:11:56 -05004784 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004785 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004786 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004787 ret = create_css(cgrp, ss,
4788 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004789 if (ret)
4790 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004791 }
Tejun Heoa8638032012-11-09 09:12:29 -08004792 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004793
Tejun Heobd53d612014-04-23 11:13:16 -04004794 /*
4795 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004796 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004797 */
Tejun Heo667c2492014-07-08 18:02:56 -04004798 if (!cgroup_on_dfl(cgrp)) {
4799 cgrp->subtree_control = parent->subtree_control;
4800 cgroup_refresh_child_subsys_mask(cgrp);
4801 }
Tejun Heof392e512014-04-23 11:13:14 -04004802
Tejun Heo2bd59d42014-02-11 11:52:49 -05004803 kernfs_activate(kn);
4804
Tejun Heoba0f4d72014-05-13 12:19:22 -04004805 ret = 0;
4806 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004807
Tejun Heoba0f4d72014-05-13 12:19:22 -04004808out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004809 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004810out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004811 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004812out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004813 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004814out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004815 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004816 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004817
4818out_destroy:
4819 cgroup_destroy_locked(cgrp);
4820 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004821}
4822
Tejun Heo223dbc32013-08-13 20:22:50 -04004823/*
4824 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004825 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4826 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004827 */
4828static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004829{
Tejun Heo223dbc32013-08-13 20:22:50 -04004830 struct cgroup_subsys_state *css =
4831 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004832
Tejun Heof20104d2013-08-13 20:22:50 -04004833 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004834 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004835 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004836
Tejun Heo09a503ea2013-08-13 20:22:50 -04004837 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004838}
4839
Tejun Heo223dbc32013-08-13 20:22:50 -04004840/* css kill confirmation processing requires process context, bounce */
4841static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004842{
4843 struct cgroup_subsys_state *css =
4844 container_of(ref, struct cgroup_subsys_state, refcnt);
4845
Tejun Heo223dbc32013-08-13 20:22:50 -04004846 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004847 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004848}
4849
Tejun Heof392e512014-04-23 11:13:14 -04004850/**
4851 * kill_css - destroy a css
4852 * @css: css to destroy
4853 *
4854 * This function initiates destruction of @css by removing cgroup interface
4855 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004856 * asynchronously once css_tryget_online() is guaranteed to fail and when
4857 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004858 */
4859static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004860{
Tejun Heo01f64742014-05-13 12:19:23 -04004861 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004862
Tejun Heo2bd59d42014-02-11 11:52:49 -05004863 /*
4864 * This must happen before css is disassociated with its cgroup.
4865 * See seq_css() for details.
4866 */
Tejun Heoaec25022014-02-08 10:36:58 -05004867 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004868
Tejun Heoedae0c32013-08-13 20:22:51 -04004869 /*
4870 * Killing would put the base ref, but we need to keep it alive
4871 * until after ->css_offline().
4872 */
4873 css_get(css);
4874
4875 /*
4876 * cgroup core guarantees that, by the time ->css_offline() is
4877 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004878 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004879 * proceed to offlining css's because percpu_ref_kill() doesn't
4880 * guarantee that the ref is seen as killed on all CPUs on return.
4881 *
4882 * Use percpu_ref_kill_and_confirm() to get notifications as each
4883 * css is confirmed to be seen as killed on all CPUs.
4884 */
4885 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004886}
4887
4888/**
4889 * cgroup_destroy_locked - the first stage of cgroup destruction
4890 * @cgrp: cgroup to be destroyed
4891 *
4892 * css's make use of percpu refcnts whose killing latency shouldn't be
4893 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004894 * guarantee that css_tryget_online() won't succeed by the time
4895 * ->css_offline() is invoked. To satisfy all the requirements,
4896 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004897 *
4898 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4899 * userland visible parts and start killing the percpu refcnts of
4900 * css's. Set up so that the next stage will be kicked off once all
4901 * the percpu refcnts are confirmed to be killed.
4902 *
4903 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4904 * rest of destruction. Once all cgroup references are gone, the
4905 * cgroup is RCU-freed.
4906 *
4907 * This function implements s1. After this step, @cgrp is gone as far as
4908 * the userland is concerned and a new cgroup with the same name may be
4909 * created. As cgroup doesn't care about the names internally, this
4910 * doesn't cause any problem.
4911 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004912static int cgroup_destroy_locked(struct cgroup *cgrp)
4913 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004914{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004915 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004916 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004917 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004918
Tejun Heo42809dd2012-11-19 08:13:37 -08004919 lockdep_assert_held(&cgroup_mutex);
4920
Tejun Heoddd69142013-06-12 21:04:54 -07004921 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004922 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004923 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004924 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004925 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004926 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004927 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004928 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004929 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004930
Tejun Heo1a90dd52012-11-05 09:16:59 -08004931 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004932 * Make sure there's no live children. We can't test emptiness of
4933 * ->self.children as dead children linger on it while being
4934 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004935 */
Tejun Heof3d46502014-05-16 13:22:52 -04004936 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004937 return -EBUSY;
4938
4939 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004940 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004941 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004942 */
Tejun Heo184faf32014-05-16 13:22:51 -04004943 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004944
Tejun Heo249f3462014-05-14 09:15:01 -04004945 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004946 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004947 kill_css(css);
4948
Tejun Heo455050d2013-06-13 19:27:41 -07004949 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004950 * Remove @cgrp directory along with the base files. @cgrp has an
4951 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07004952 */
Tejun Heo01f64742014-05-13 12:19:23 -04004953 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04004954
Tejun Heod51f39b2014-05-16 13:22:48 -04004955 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05004956
Tejun Heo249f3462014-05-14 09:15:01 -04004957 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004958 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07004959
Tejun Heoea15f8c2013-06-13 19:27:42 -07004960 return 0;
4961};
4962
Tejun Heo2bd59d42014-02-11 11:52:49 -05004963static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004964{
Tejun Heoa9746d82014-05-13 12:19:22 -04004965 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004966 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004967
Tejun Heoa9746d82014-05-13 12:19:22 -04004968 cgrp = cgroup_kn_lock_live(kn);
4969 if (!cgrp)
4970 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004971
Tejun Heoa9746d82014-05-13 12:19:22 -04004972 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004973
Tejun Heoa9746d82014-05-13 12:19:22 -04004974 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004975 return ret;
4976}
4977
Tejun Heo2bd59d42014-02-11 11:52:49 -05004978static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4979 .remount_fs = cgroup_remount,
4980 .show_options = cgroup_show_options,
4981 .mkdir = cgroup_mkdir,
4982 .rmdir = cgroup_rmdir,
4983 .rename = cgroup_rename,
4984};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004985
Tejun Heo15a4c832014-05-04 15:09:14 -04004986static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004987{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004988 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004989
4990 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004991
Tejun Heo648bb562012-11-19 08:13:36 -08004992 mutex_lock(&cgroup_mutex);
4993
Tejun Heo15a4c832014-05-04 15:09:14 -04004994 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004995 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004996
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004997 /* Create the root cgroup state for this subsystem */
4998 ss->root = &cgrp_dfl_root;
4999 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005000 /* We don't handle early failures gracefully */
5001 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005002 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005003
5004 /*
5005 * Root csses are never destroyed and we can't initialize
5006 * percpu_ref during early init. Disable refcnting.
5007 */
5008 css->flags |= CSS_NO_REF;
5009
Tejun Heo15a4c832014-05-04 15:09:14 -04005010 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005011 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005012 css->id = 1;
5013 } else {
5014 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5015 BUG_ON(css->id < 0);
5016 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005017
Li Zefane8d55fd2008-04-29 01:00:13 -07005018 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005019 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005020 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005021 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005022 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005023
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005024 have_fork_callback |= (bool)ss->fork << ss->id;
5025 have_exit_callback |= (bool)ss->exit << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005026 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005027
Li Zefane8d55fd2008-04-29 01:00:13 -07005028 /* At system boot, before all subsystems have been
5029 * registered, no tasks have been forked, so we don't
5030 * need to invoke fork callbacks here. */
5031 BUG_ON(!list_empty(&init_task.tasks));
5032
Tejun Heoae7f1642013-08-13 20:22:50 -04005033 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005034
Tejun Heo648bb562012-11-19 08:13:36 -08005035 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005036}
5037
5038/**
Li Zefana043e3b2008-02-23 15:24:09 -08005039 * cgroup_init_early - cgroup initialization at system boot
5040 *
5041 * Initialize cgroups at system boot, and initialize any
5042 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005043 */
5044int __init cgroup_init_early(void)
5045{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04005046 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07005047 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005048 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005049
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005050 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04005051 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5052
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005053 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005054
Tejun Heo3ed80a62014-02-08 10:36:58 -05005055 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005056 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05005057 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5058 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005059 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005060 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5061 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005062
Tejun Heoaec25022014-02-08 10:36:58 -05005063 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005064 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005065 if (!ss->legacy_name)
5066 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005067
5068 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005069 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005070 }
5071 return 0;
5072}
5073
5074/**
Li Zefana043e3b2008-02-23 15:24:09 -08005075 * cgroup_init - cgroup initialization
5076 *
5077 * Register cgroup filesystem and /proc file, and initialize
5078 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005079 */
5080int __init cgroup_init(void)
5081{
Tejun Heo30159ec2013-06-25 11:53:37 -07005082 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005083 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04005084 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07005085
Tejun Heoa14c6872014-07-15 11:05:09 -04005086 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5087 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005088
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005089 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005090
Tejun Heo82fe9b02013-06-25 11:53:37 -07005091 /* Add init_css_set to the hash table */
5092 key = css_set_hash(init_css_set.subsys);
5093 hash_add(css_set_table, &init_css_set.hlist, key);
5094
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005095 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005096
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005097 mutex_unlock(&cgroup_mutex);
5098
Tejun Heo172a2c062014-03-19 10:23:53 -04005099 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005100 if (ss->early_init) {
5101 struct cgroup_subsys_state *css =
5102 init_css_set.subsys[ss->id];
5103
5104 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5105 GFP_KERNEL);
5106 BUG_ON(css->id < 0);
5107 } else {
5108 cgroup_init_subsys(ss, false);
5109 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005110
Tejun Heo2d8f2432014-04-23 11:13:15 -04005111 list_add_tail(&init_css_set.e_cset_node[ssid],
5112 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005113
5114 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005115 * Setting dfl_root subsys_mask needs to consider the
5116 * disabled flag and cftype registration needs kmalloc,
5117 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005118 */
Tejun Heoa8ddc822014-07-15 11:05:10 -04005119 if (ss->disabled)
5120 continue;
5121
5122 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5123
5124 if (cgroup_legacy_files_on_dfl && !ss->dfl_cftypes)
5125 ss->dfl_cftypes = ss->legacy_cftypes;
5126
Tejun Heo5de4fa12014-07-15 11:05:10 -04005127 if (!ss->dfl_cftypes)
5128 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5129
Tejun Heoa8ddc822014-07-15 11:05:10 -04005130 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5131 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5132 } else {
5133 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5134 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005135 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005136
5137 if (ss->bind)
5138 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005139 }
Greg KH676db4a2010-08-05 13:53:35 -07005140
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005141 err = sysfs_create_mount_point(fs_kobj, "cgroup");
5142 if (err)
5143 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005144
5145 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005146 if (err < 0) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005147 sysfs_remove_mount_point(fs_kobj, "cgroup");
Tejun Heo2bd59d42014-02-11 11:52:49 -05005148 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005149 }
5150
5151 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005152 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005153}
Paul Menageb4f48b62007-10-18 23:39:33 -07005154
Tejun Heoe5fca242013-11-22 17:14:39 -05005155static int __init cgroup_wq_init(void)
5156{
5157 /*
5158 * There isn't much point in executing destruction path in
5159 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005160 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005161 *
5162 * We would prefer to do this in cgroup_init() above, but that
5163 * is called before init_workqueues(): so leave this until after.
5164 */
Tejun Heo1a115332014-02-12 19:06:19 -05005165 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005166 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005167
5168 /*
5169 * Used to destroy pidlists and separate to serve as flush domain.
5170 * Cap @max_active to 1 too.
5171 */
5172 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5173 0, 1);
5174 BUG_ON(!cgroup_pidlist_destroy_wq);
5175
Tejun Heoe5fca242013-11-22 17:14:39 -05005176 return 0;
5177}
5178core_initcall(cgroup_wq_init);
5179
Paul Menagea4243162007-10-18 23:39:35 -07005180/*
5181 * proc_cgroup_show()
5182 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5183 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005184 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005185int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5186 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005187{
Tejun Heoe61734c2014-02-12 09:29:50 -05005188 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005189 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005190 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005191
5192 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005193 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005194 if (!buf)
5195 goto out;
5196
Paul Menagea4243162007-10-18 23:39:35 -07005197 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05005198 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005199
Tejun Heo985ed672014-03-19 10:23:53 -04005200 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005201 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005202 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005203 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005204
Tejun Heoa2dd4242014-03-19 10:23:55 -04005205 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005206 continue;
5207
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005208 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005209 if (root != &cgrp_dfl_root)
5210 for_each_subsys(ss, ssid)
5211 if (root->subsys_mask & (1 << ssid))
5212 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005213 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005214 if (strlen(root->name))
5215 seq_printf(m, "%sname=%s", count ? "," : "",
5216 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005217 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005218 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05005219 path = cgroup_path(cgrp, buf, PATH_MAX);
5220 if (!path) {
5221 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07005222 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05005223 }
5224 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07005225 seq_putc(m, '\n');
5226 }
5227
Zefan Li006f4ac2014-09-18 16:03:15 +08005228 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005229out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05005230 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005231 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005232 kfree(buf);
5233out:
5234 return retval;
5235}
5236
Paul Menagea4243162007-10-18 23:39:35 -07005237/* Display information about each subsystem and each hierarchy */
5238static int proc_cgroupstats_show(struct seq_file *m, void *v)
5239{
Tejun Heo30159ec2013-06-25 11:53:37 -07005240 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005241 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005242
Paul Menage8bab8dd2008-04-04 14:29:57 -07005243 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005244 /*
5245 * ideally we don't want subsystems moving around while we do this.
5246 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5247 * subsys/hierarchy state.
5248 */
Paul Menagea4243162007-10-18 23:39:35 -07005249 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005250
5251 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005252 seq_printf(m, "%s\t%d\t%d\t%d\n",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005253 ss->legacy_name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05005254 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005255
Paul Menagea4243162007-10-18 23:39:35 -07005256 mutex_unlock(&cgroup_mutex);
5257 return 0;
5258}
5259
5260static int cgroupstats_open(struct inode *inode, struct file *file)
5261{
Al Viro9dce07f2008-03-29 03:07:28 +00005262 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005263}
5264
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005265static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005266 .open = cgroupstats_open,
5267 .read = seq_read,
5268 .llseek = seq_lseek,
5269 .release = single_release,
5270};
5271
Aleksa Sarai7e476822015-06-09 21:32:09 +10005272static void **subsys_canfork_priv_p(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5273{
5274 if (CGROUP_CANFORK_START <= i && i < CGROUP_CANFORK_END)
5275 return &ss_priv[i - CGROUP_CANFORK_START];
5276 return NULL;
5277}
5278
5279static void *subsys_canfork_priv(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
5280{
5281 void **private = subsys_canfork_priv_p(ss_priv, i);
5282 return private ? *private : NULL;
5283}
5284
Paul Menageb4f48b62007-10-18 23:39:33 -07005285/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005286 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005287 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005288 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005289 * A task is associated with the init_css_set until cgroup_post_fork()
5290 * attaches it to the parent's css_set. Empty cg_list indicates that
5291 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005292 */
5293void cgroup_fork(struct task_struct *child)
5294{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005295 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005296 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005297}
5298
5299/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10005300 * cgroup_can_fork - called on a new task before the process is exposed
5301 * @child: the task in question.
5302 *
5303 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5304 * returns an error, the fork aborts with that error code. This allows for
5305 * a cgroup subsystem to conditionally allow or deny new forks.
5306 */
5307int cgroup_can_fork(struct task_struct *child,
5308 void *ss_priv[CGROUP_CANFORK_COUNT])
5309{
5310 struct cgroup_subsys *ss;
5311 int i, j, ret;
5312
5313 for_each_subsys_which(ss, i, &have_canfork_callback) {
5314 ret = ss->can_fork(child, subsys_canfork_priv_p(ss_priv, i));
5315 if (ret)
5316 goto out_revert;
5317 }
5318
5319 return 0;
5320
5321out_revert:
5322 for_each_subsys(ss, j) {
5323 if (j >= i)
5324 break;
5325 if (ss->cancel_fork)
5326 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, j));
5327 }
5328
5329 return ret;
5330}
5331
5332/**
5333 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5334 * @child: the task in question
5335 *
5336 * This calls the cancel_fork() callbacks if a fork failed *after*
5337 * cgroup_can_fork() succeded.
5338 */
5339void cgroup_cancel_fork(struct task_struct *child,
5340 void *ss_priv[CGROUP_CANFORK_COUNT])
5341{
5342 struct cgroup_subsys *ss;
5343 int i;
5344
5345 for_each_subsys(ss, i)
5346 if (ss->cancel_fork)
5347 ss->cancel_fork(child, subsys_canfork_priv(ss_priv, i));
5348}
5349
5350/**
Li Zefana043e3b2008-02-23 15:24:09 -08005351 * cgroup_post_fork - called on a new task after adding it to the task list
5352 * @child: the task in question
5353 *
Tejun Heo5edee612012-10-16 15:03:14 -07005354 * Adds the task to the list running through its css_set if necessary and
5355 * call the subsystem fork() callbacks. Has to be after the task is
5356 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005357 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005358 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005359 */
Aleksa Sarai7e476822015-06-09 21:32:09 +10005360void cgroup_post_fork(struct task_struct *child,
5361 void *old_ss_priv[CGROUP_CANFORK_COUNT])
Paul Menage817929e2007-10-18 23:39:36 -07005362{
Tejun Heo30159ec2013-06-25 11:53:37 -07005363 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005364 int i;
5365
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005366 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005367 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005368 * function sets use_task_css_set_links before grabbing
5369 * tasklist_lock and we just went through tasklist_lock to add
5370 * @child, it's guaranteed that either we see the set
5371 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5372 * @child during its iteration.
5373 *
5374 * If we won the race, @child is associated with %current's
5375 * css_set. Grabbing css_set_rwsem guarantees both that the
5376 * association is stable, and, on completion of the parent's
5377 * migration, @child is visible in the source of migration or
5378 * already in the destination cgroup. This guarantee is necessary
5379 * when implementing operations which need to migrate all tasks of
5380 * a cgroup to another.
5381 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005382 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005383 * will remain in init_css_set. This is safe because all tasks are
5384 * in the init_css_set before cg_links is enabled and there's no
5385 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005386 */
Paul Menage817929e2007-10-18 23:39:36 -07005387 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005388 struct css_set *cset;
5389
Tejun Heo96d365e2014-02-13 06:58:40 -05005390 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005391 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005392 if (list_empty(&child->cg_list)) {
5393 rcu_assign_pointer(child->cgroups, cset);
5394 list_add(&child->cg_list, &cset->tasks);
5395 get_css_set(cset);
5396 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005397 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07005398 }
Tejun Heo5edee612012-10-16 15:03:14 -07005399
5400 /*
5401 * Call ss->fork(). This must happen after @child is linked on
5402 * css_set; otherwise, @child might change state between ->fork()
5403 * and addition to css_set.
5404 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005405 for_each_subsys_which(ss, i, &have_fork_callback)
Aleksa Sarai7e476822015-06-09 21:32:09 +10005406 ss->fork(child, subsys_canfork_priv(old_ss_priv, i));
Paul Menage817929e2007-10-18 23:39:36 -07005407}
Tejun Heo5edee612012-10-16 15:03:14 -07005408
Paul Menage817929e2007-10-18 23:39:36 -07005409/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005410 * cgroup_exit - detach cgroup from exiting task
5411 * @tsk: pointer to task_struct of exiting process
5412 *
5413 * Description: Detach cgroup from @tsk and release it.
5414 *
5415 * Note that cgroups marked notify_on_release force every task in
5416 * them to take the global cgroup_mutex mutex when exiting.
5417 * This could impact scaling on very large systems. Be reluctant to
5418 * use notify_on_release cgroups where very high task exit scaling
5419 * is required on large systems.
5420 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005421 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5422 * call cgroup_exit() while the task is still competent to handle
5423 * notify_on_release(), then leave the task attached to the root cgroup in
5424 * each hierarchy for the remainder of its exit. No need to bother with
5425 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005426 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005427 */
Li Zefan1ec41832014-03-28 15:22:19 +08005428void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005429{
Tejun Heo30159ec2013-06-25 11:53:37 -07005430 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005431 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005432 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005433 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005434
5435 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005436 * Unlink from @tsk from its css_set. As migration path can't race
5437 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005438 */
5439 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005440 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005441 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005442 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005443 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005444 }
5445
Paul Menageb4f48b62007-10-18 23:39:33 -07005446 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005447 cset = task_css_set(tsk);
5448 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005449
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005450 /* see cgroup_post_fork() for details */
5451 for_each_subsys_which(ss, i, &have_exit_callback) {
5452 struct cgroup_subsys_state *old_css = cset->subsys[i];
5453 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005454
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005455 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005456 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005457
Tejun Heoeaf797a2014-02-25 10:04:03 -05005458 if (put_cset)
Zefan Lia25eb522014-09-19 16:51:00 +08005459 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005460}
Paul Menage697f4162007-10-18 23:39:34 -07005461
Paul Menagebd89aab2007-10-18 23:40:44 -07005462static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005463{
Zefan Lia25eb522014-09-19 16:51:00 +08005464 if (notify_on_release(cgrp) && !cgroup_has_tasks(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005465 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5466 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005467}
5468
Paul Menage81a6a5c2007-10-18 23:39:38 -07005469/*
5470 * Notify userspace when a cgroup is released, by running the
5471 * configured release agent with the name of the cgroup (path
5472 * relative to the root of cgroup file system) as the argument.
5473 *
5474 * Most likely, this user command will try to rmdir this cgroup.
5475 *
5476 * This races with the possibility that some other task will be
5477 * attached to this cgroup before it is removed, or that some other
5478 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5479 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5480 * unused, and this cgroup will be reprieved from its death sentence,
5481 * to continue to serve a useful existence. Next time it's released,
5482 * we will get notified again, if it still has 'notify_on_release' set.
5483 *
5484 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5485 * means only wait until the task is successfully execve()'d. The
5486 * separate release agent task is forked by call_usermodehelper(),
5487 * then control in this thread returns here, without waiting for the
5488 * release agent task. We don't bother to wait because the caller of
5489 * this routine has no use for the exit status of the release agent
5490 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005491 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005492static void cgroup_release_agent(struct work_struct *work)
5493{
Zefan Li971ff492014-09-18 16:06:19 +08005494 struct cgroup *cgrp =
5495 container_of(work, struct cgroup, release_agent_work);
5496 char *pathbuf = NULL, *agentbuf = NULL, *path;
5497 char *argv[3], *envp[3];
5498
Paul Menage81a6a5c2007-10-18 23:39:38 -07005499 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005500
Zefan Li971ff492014-09-18 16:06:19 +08005501 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5502 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5503 if (!pathbuf || !agentbuf)
5504 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005505
Zefan Li971ff492014-09-18 16:06:19 +08005506 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5507 if (!path)
5508 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005509
Zefan Li971ff492014-09-18 16:06:19 +08005510 argv[0] = agentbuf;
5511 argv[1] = path;
5512 argv[2] = NULL;
5513
5514 /* minimal command environment */
5515 envp[0] = "HOME=/";
5516 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5517 envp[2] = NULL;
5518
Paul Menage81a6a5c2007-10-18 23:39:38 -07005519 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005520 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005521 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005522out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005523 mutex_unlock(&cgroup_mutex);
5524out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005525 kfree(agentbuf);
5526 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005527}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005528
5529static int __init cgroup_disable(char *str)
5530{
Tejun Heo30159ec2013-06-25 11:53:37 -07005531 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005532 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005533 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005534
5535 while ((token = strsep(&str, ",")) != NULL) {
5536 if (!*token)
5537 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005538
Tejun Heo3ed80a62014-02-08 10:36:58 -05005539 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005540 if (strcmp(token, ss->name) &&
5541 strcmp(token, ss->legacy_name))
5542 continue;
5543
5544 ss->disabled = 1;
5545 printk(KERN_INFO "Disabling %s control group subsystem\n",
5546 ss->name);
5547 break;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005548 }
5549 }
5550 return 1;
5551}
5552__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005553
Tejun Heoa8ddc822014-07-15 11:05:10 -04005554static int __init cgroup_set_legacy_files_on_dfl(char *str)
5555{
5556 printk("cgroup: using legacy files on the default hierarchy\n");
5557 cgroup_legacy_files_on_dfl = true;
5558 return 0;
5559}
5560__setup("cgroup__DEVEL__legacy_files_on_dfl", cgroup_set_legacy_files_on_dfl);
5561
Tejun Heob77d7b62013-08-13 11:01:54 -04005562/**
Tejun Heoec903c02014-05-13 12:11:01 -04005563 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005564 * @dentry: directory dentry of interest
5565 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005566 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005567 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5568 * to get the corresponding css and return it. If such css doesn't exist
5569 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005570 */
Tejun Heoec903c02014-05-13 12:11:01 -04005571struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5572 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005573{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005574 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5575 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005576 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005577
Tejun Heo35cf0832013-08-26 18:40:56 -04005578 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005579 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5580 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005581 return ERR_PTR(-EBADF);
5582
Tejun Heo5a17f542014-02-11 11:52:47 -05005583 rcu_read_lock();
5584
Tejun Heo2bd59d42014-02-11 11:52:49 -05005585 /*
5586 * This path doesn't originate from kernfs and @kn could already
5587 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005588 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005589 */
5590 cgrp = rcu_dereference(kn->priv);
5591 if (cgrp)
5592 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005593
Tejun Heoec903c02014-05-13 12:11:01 -04005594 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005595 css = ERR_PTR(-ENOENT);
5596
5597 rcu_read_unlock();
5598 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005599}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005600
Li Zefan1cb650b2013-08-19 10:05:24 +08005601/**
5602 * css_from_id - lookup css by id
5603 * @id: the cgroup id
5604 * @ss: cgroup subsys to be looked into
5605 *
5606 * Returns the css if there's valid one with @id, otherwise returns NULL.
5607 * Should be called under rcu_read_lock().
5608 */
5609struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5610{
Tejun Heo6fa49182014-05-04 15:09:13 -04005611 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005612 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005613}
5614
Paul Menagefe693432009-09-23 15:56:20 -07005615#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005616static struct cgroup_subsys_state *
5617debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005618{
5619 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5620
5621 if (!css)
5622 return ERR_PTR(-ENOMEM);
5623
5624 return css;
5625}
5626
Tejun Heoeb954192013-08-08 20:11:23 -04005627static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005628{
Tejun Heoeb954192013-08-08 20:11:23 -04005629 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005630}
5631
Tejun Heo182446d2013-08-08 20:11:24 -04005632static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5633 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005634{
Tejun Heo182446d2013-08-08 20:11:24 -04005635 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005636}
5637
Tejun Heo182446d2013-08-08 20:11:24 -04005638static u64 current_css_set_read(struct cgroup_subsys_state *css,
5639 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005640{
5641 return (u64)(unsigned long)current->cgroups;
5642}
5643
Tejun Heo182446d2013-08-08 20:11:24 -04005644static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005645 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005646{
5647 u64 count;
5648
5649 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005650 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005651 rcu_read_unlock();
5652 return count;
5653}
5654
Tejun Heo2da8ca82013-12-05 12:28:04 -05005655static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005656{
Tejun Heo69d02062013-06-12 21:04:50 -07005657 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005658 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005659 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005660
Tejun Heoe61734c2014-02-12 09:29:50 -05005661 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5662 if (!name_buf)
5663 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005664
Tejun Heo96d365e2014-02-13 06:58:40 -05005665 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005666 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005667 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005668 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005669 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005670
Tejun Heoa2dd4242014-03-19 10:23:55 -04005671 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005672 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005673 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005674 }
5675 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005676 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005677 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005678 return 0;
5679}
5680
5681#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005682static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005683{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005684 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005685 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005686
Tejun Heo96d365e2014-02-13 06:58:40 -05005687 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005688 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005689 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005690 struct task_struct *task;
5691 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005692
Tejun Heo5abb8852013-06-12 21:04:49 -07005693 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005694
Tejun Heo5abb8852013-06-12 21:04:49 -07005695 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005696 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5697 goto overflow;
5698 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005699 }
Tejun Heoc7561122014-02-25 10:04:01 -05005700
5701 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5702 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5703 goto overflow;
5704 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5705 }
5706 continue;
5707 overflow:
5708 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005709 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005710 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005711 return 0;
5712}
5713
Tejun Heo182446d2013-08-08 20:11:24 -04005714static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005715{
Zefan Lia25eb522014-09-19 16:51:00 +08005716 return (!cgroup_has_tasks(css->cgroup) &&
5717 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005718}
5719
5720static struct cftype debug_files[] = {
5721 {
Paul Menagefe693432009-09-23 15:56:20 -07005722 .name = "taskcount",
5723 .read_u64 = debug_taskcount_read,
5724 },
5725
5726 {
5727 .name = "current_css_set",
5728 .read_u64 = current_css_set_read,
5729 },
5730
5731 {
5732 .name = "current_css_set_refcount",
5733 .read_u64 = current_css_set_refcount_read,
5734 },
5735
5736 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005737 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005738 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005739 },
5740
5741 {
5742 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005743 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005744 },
5745
5746 {
Paul Menagefe693432009-09-23 15:56:20 -07005747 .name = "releasable",
5748 .read_u64 = releasable_read,
5749 },
Paul Menagefe693432009-09-23 15:56:20 -07005750
Tejun Heo4baf6e32012-04-01 12:09:55 -07005751 { } /* terminate */
5752};
Paul Menagefe693432009-09-23 15:56:20 -07005753
Tejun Heo073219e2014-02-08 10:36:58 -05005754struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005755 .css_alloc = debug_css_alloc,
5756 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005757 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005758};
5759#endif /* CONFIG_CGROUP_DEBUG */