blob: 3a973519068ff5fa98989f43608fcd1ff9aa21c1 [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 Menagec6d57f332009-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>
Tejun Heod59cfc02015-05-13 16:35:17 -040049#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070050#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070051#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070052#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080055#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020059#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050060#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070061
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070063
Tejun Heoe25e2cbb2011-12-12 18:12:21 -080064/*
Tejun Heob1a21362013-11-29 10:42:58 -050065 * pidlists linger the following amount before being destroyed. The goal
66 * is avoiding frequent destruction in the middle of consecutive read calls
67 * Expiring in the middle is a performance problem not a correctness one.
68 * 1 sec should be enough.
69 */
70#define CGROUP_PIDLIST_DESTROY_DELAY HZ
71
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050072#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
73 MAX_CFTYPE_NAME + 2)
74
Tejun Heob1a21362013-11-29 10:42:58 -050075/*
Tejun Heoe25e2cbb2011-12-12 18:12:21 -080076 * cgroup_mutex is the master lock. Any modification to cgroup or its
77 * hierarchy must be performed while holding it.
78 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050079 * css_set_rwsem protects task->cgroups pointer, the list of css_set
80 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cbb2011-12-12 18:12:21 -080081 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050082 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
83 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cbb2011-12-12 18:12:21 -080084 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050087DECLARE_RWSEM(css_set_rwsem);
88EXPORT_SYMBOL_GPL(cgroup_mutex);
89EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070090#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070091static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050092static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070093#endif
94
Tejun Heo69e943b2014-02-08 10:36:58 -050095/*
Tejun Heo15a4c832014-05-04 15:09:14 -040096 * Protects cgroup_idr and css_idr so that IDs can be released without
97 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -040098 */
99static DEFINE_SPINLOCK(cgroup_idr_lock);
100
101/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500102 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
103 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
104 */
105static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700106
Tejun Heod59cfc02015-05-13 16:35:17 -0400107struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
108
Tejun Heo8353da12014-05-13 12:19:23 -0400109#define cgroup_assert_mutex_or_rcu_locked() \
Tejun Heo87fb54f12013-12-06 15:11:55 -0500110 rcu_lockdep_assert(rcu_read_lock_held() || \
111 lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400112 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500113
Ben Blumaae8aab2010-03-10 15:22:07 -0800114/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500115 * cgroup destruction makes heavy use of work items and there can be a lot
116 * of concurrent destructions. Use a separate workqueue so that cgroup
117 * destruction work items don't end up filling up max_active of system_wq
118 * which may lead to deadlock.
119 */
120static struct workqueue_struct *cgroup_destroy_wq;
121
122/*
Tejun Heob1a21362013-11-29 10:42:58 -0500123 * pidlist destructions need to be flushed on cgroup destruction. Use a
124 * separate workqueue as flush domain.
125 */
126static struct workqueue_struct *cgroup_pidlist_destroy_wq;
127
Tejun Heo3ed80a62014-02-08 10:36:58 -0500128/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500129#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500130static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131#include <linux/cgroup_subsys.h>
132};
Tejun Heo073219e2014-02-08 10:36:58 -0500133#undef SUBSYS
134
135/* array of cgroup subsystem names */
136#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
137static const char *cgroup_subsys_name[] = {
138#include <linux/cgroup_subsys.h>
139};
140#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400143 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700144 * unattached - it never has more than a single cgroup, and all tasks are
145 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400147struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700148
Tejun Heoa2dd4242014-03-19 10:23:55 -0400149/*
150 * The default hierarchy always exists but is hidden until mounted for the
151 * first time. This is for backward compatibility.
152 */
153static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154
Tejun Heoa8ddc822014-07-15 11:05:10 -0400155/*
156 * Set by the boot param of the same name and makes subsystems with NULL
157 * ->dfl_files to use ->legacy_files on the default hierarchy.
158 */
159static bool cgroup_legacy_files_on_dfl;
160
Tejun Heo5533e012014-05-14 19:33:07 -0400161/* some controllers are not supported in the default hierarchy */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000162static unsigned long cgrp_dfl_root_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400163
Paul Menageddbcc7e2007-10-18 23:39:30 -0700164/* The list of hierarchy roots */
165
Tejun Heo9871bf92013-06-24 15:21:47 -0700166static LIST_HEAD(cgroup_roots);
167static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700168
Tejun Heo3417ae12014-02-08 10:37:01 -0500169/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700170static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700171
Li Zefan794611a2013-06-18 18:53:53 +0800172/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400173 * Assign a monotonically increasing serial number to csses. It guarantees
174 * cgroups with bigger numbers are newer than those with smaller numbers.
175 * Also, as csses are always appended to the parent's ->children list, it
176 * guarantees that sibling csses are always sorted in the ascending serial
177 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800178 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400179static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800180
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000181/*
182 * These bitmask flags indicate whether tasks in the fork and exit paths have
183 * fork/exit handlers to call. This avoids us having to do extra work in the
184 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000186static unsigned long have_fork_callback __read_mostly;
187static unsigned long have_exit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188
Tejun Heoa14c6872014-07-15 11:05:09 -0400189static struct cftype cgroup_dfl_base_files[];
190static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700191
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400192static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000193 unsigned long ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800194static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400195static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
196 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400197static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400198static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400199static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
200 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800201
Tejun Heo6fa49182014-05-04 15:09:13 -0400202/* IDR wrappers which synchronize using cgroup_idr_lock */
203static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
204 gfp_t gfp_mask)
205{
206 int ret;
207
208 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400209 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400210 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400211 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400212 idr_preload_end();
213 return ret;
214}
215
216static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
217{
218 void *ret;
219
Tejun Heo54504e92014-05-13 12:10:59 -0400220 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400221 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400222 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400223 return ret;
224}
225
226static void cgroup_idr_remove(struct idr *idr, int id)
227{
Tejun Heo54504e92014-05-13 12:10:59 -0400228 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400229 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400230 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400231}
232
Tejun Heod51f39b2014-05-16 13:22:48 -0400233static struct cgroup *cgroup_parent(struct cgroup *cgrp)
234{
235 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
236
237 if (parent_css)
238 return container_of(parent_css, struct cgroup, self);
239 return NULL;
240}
241
Tejun Heo95109b62013-08-08 20:11:27 -0400242/**
243 * cgroup_css - obtain a cgroup's css for the specified subsystem
244 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400245 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400246 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400247 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
248 * function must be called either under cgroup_mutex or rcu_read_lock() and
249 * the caller is responsible for pinning the returned css if it wants to
250 * keep accessing it outside the said locks. This function may return
251 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400252 */
253static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400254 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400255{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400256 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500257 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee812014-02-11 11:52:47 -0500258 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400259 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400260 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400261}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700262
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400263/**
264 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
265 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400266 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400267 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400268 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400269 * as the matching css of the nearest ancestor including self which has @ss
270 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
271 * function is guaranteed to return non-NULL css.
272 */
273static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
274 struct cgroup_subsys *ss)
275{
276 lockdep_assert_held(&cgroup_mutex);
277
278 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400279 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400280
281 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
282 return NULL;
283
Tejun Heoeeecbd12014-11-18 02:49:52 -0500284 /*
285 * This function is used while updating css associations and thus
286 * can't test the csses directly. Use ->child_subsys_mask.
287 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400288 while (cgroup_parent(cgrp) &&
289 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
290 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400291
292 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700293}
294
Tejun Heoeeecbd12014-11-18 02:49:52 -0500295/**
296 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
297 * @cgrp: the cgroup of interest
298 * @ss: the subsystem of interest
299 *
300 * Find and get the effective css of @cgrp for @ss. The effective css is
301 * defined as the matching css of the nearest ancestor including self which
302 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
303 * the root css is returned, so this function always returns a valid css.
304 * The returned css must be put using css_put().
305 */
306struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
307 struct cgroup_subsys *ss)
308{
309 struct cgroup_subsys_state *css;
310
311 rcu_read_lock();
312
313 do {
314 css = cgroup_css(cgrp, ss);
315
316 if (css && css_tryget_online(css))
317 goto out_unlock;
318 cgrp = cgroup_parent(cgrp);
319 } while (cgrp);
320
321 css = init_css_set.subsys[ss->id];
322 css_get(css);
323out_unlock:
324 rcu_read_unlock();
325 return css;
326}
327
Paul Menageddbcc7e2007-10-18 23:39:30 -0700328/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700329static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700330{
Tejun Heo184faf32014-05-16 13:22:51 -0400331 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700332}
333
Tejun Heob4168642014-05-13 12:16:21 -0400334struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500335{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500336 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400337 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500338
339 /*
340 * This is open and unprotected implementation of cgroup_css().
341 * seq_css() is only called from a kernfs file operation which has
342 * an active reference on the file. Because all the subsystem
343 * files are drained before a css is disassociated with a cgroup,
344 * the matching css from the cgroup's subsys table is guaranteed to
345 * be and stay valid until the enclosing operation is complete.
346 */
347 if (cft->ss)
348 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
349 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400350 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500351}
Tejun Heob4168642014-05-13 12:16:21 -0400352EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500353
Li Zefan78574cf2013-04-08 19:00:38 -0700354/**
355 * cgroup_is_descendant - test ancestry
356 * @cgrp: the cgroup to be tested
357 * @ancestor: possible ancestor of @cgrp
358 *
359 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
360 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
361 * and @ancestor are accessible.
362 */
363bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
364{
365 while (cgrp) {
366 if (cgrp == ancestor)
367 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400368 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700369 }
370 return false;
371}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700372
Adrian Bunke9685a02008-02-07 00:13:46 -0800373static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700374{
Paul Menagebd89aab2007-10-18 23:40:44 -0700375 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700376}
377
Tejun Heo30159ec2013-06-25 11:53:37 -0700378/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500379 * for_each_css - iterate all css's of a cgroup
380 * @css: the iteration cursor
381 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
382 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700383 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400384 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700385 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500386#define for_each_css(css, ssid, cgrp) \
387 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
388 if (!((css) = rcu_dereference_check( \
389 (cgrp)->subsys[(ssid)], \
390 lockdep_is_held(&cgroup_mutex)))) { } \
391 else
392
393/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400394 * for_each_e_css - iterate all effective css's of a cgroup
395 * @css: the iteration cursor
396 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
397 * @cgrp: the target cgroup to iterate css's of
398 *
399 * Should be called under cgroup_[tree_]mutex.
400 */
401#define for_each_e_css(css, ssid, cgrp) \
402 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
403 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
404 ; \
405 else
406
407/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500408 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700409 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500410 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700411 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500412#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500413 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
414 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700415
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000416/**
417 * for_each_subsys_which - filter for_each_subsys with a bitmask
418 * @ss: the iteration cursor
419 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
420 * @ss_maskp: a pointer to the bitmask
421 *
422 * The block will only run for cases where the ssid-th bit (1 << ssid) of
423 * mask is set to 1.
424 */
425#define for_each_subsys_which(ss, ssid, ss_maskp) \
426 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
427 ; \
428 else \
429 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
430 if (((ss) = cgroup_subsys[ssid]) && false) \
431 break; \
432 else
433
Tejun Heo985ed672014-03-19 10:23:53 -0400434/* iterate across the hierarchies */
435#define for_each_root(root) \
Tejun Heo5549c4972013-06-24 15:21:48 -0700436 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700437
Tejun Heof8f22e52014-04-23 11:13:16 -0400438/* iterate over child cgrps, lock should be held throughout iteration */
439#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400440 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400441 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400442 cgroup_is_dead(child); })) \
443 ; \
444 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700445
Paul Menage81a6a5c2007-10-18 23:39:38 -0700446static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700447static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700448
Tejun Heo69d02062013-06-12 21:04:50 -0700449/*
450 * A cgroup can be associated with multiple css_sets as different tasks may
451 * belong to different cgroups on different hierarchies. In the other
452 * direction, a css_set is naturally associated with multiple cgroups.
453 * This M:N relationship is represented by the following link structure
454 * which exists for each association and allows traversing the associations
455 * from both sides.
456 */
457struct cgrp_cset_link {
458 /* the cgroup and css_set this link associates */
459 struct cgroup *cgrp;
460 struct css_set *cset;
461
462 /* list of cgrp_cset_links anchored at cgrp->cset_links */
463 struct list_head cset_link;
464
465 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
466 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700467};
468
Tejun Heo172a2c062014-03-19 10:23:53 -0400469/*
470 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700471 * hierarchies being mounted. It contains a pointer to the root state
472 * for each subsystem. Also used to anchor the list of css_sets. Not
473 * reference-counted, to improve performance when child cgroups
474 * haven't been created.
475 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400476struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400477 .refcount = ATOMIC_INIT(1),
478 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
479 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
480 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
481 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
482 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
483};
Paul Menage817929e2007-10-18 23:39:36 -0700484
Tejun Heo172a2c062014-03-19 10:23:53 -0400485static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700486
Tejun Heo842b5972014-04-25 18:28:02 -0400487/**
488 * cgroup_update_populated - updated populated count of a cgroup
489 * @cgrp: the target cgroup
490 * @populated: inc or dec populated count
491 *
492 * @cgrp is either getting the first task (css_set) or losing the last.
493 * Update @cgrp->populated_cnt accordingly. The count is propagated
494 * towards root so that a given cgroup's populated_cnt is zero iff the
495 * cgroup and all its descendants are empty.
496 *
497 * @cgrp's interface file "cgroup.populated" is zero if
498 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
499 * changes from or to zero, userland is notified that the content of the
500 * interface file has changed. This can be used to detect when @cgrp and
501 * its descendants become populated or empty.
502 */
503static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
504{
505 lockdep_assert_held(&css_set_rwsem);
506
507 do {
508 bool trigger;
509
510 if (populated)
511 trigger = !cgrp->populated_cnt++;
512 else
513 trigger = !--cgrp->populated_cnt;
514
515 if (!trigger)
516 break;
517
518 if (cgrp->populated_kn)
519 kernfs_notify(cgrp->populated_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400520 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400521 } while (cgrp);
522}
523
Paul Menage7717f7b2009-09-23 15:56:22 -0700524/*
525 * hash table for cgroup groups. This improves the performance to find
526 * an existing css_set. This hash doesn't (currently) take into
527 * account cgroups in empty hierarchies.
528 */
Li Zefan472b1052008-04-29 01:00:11 -0700529#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800530static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700531
Li Zefan0ac801f2013-01-10 11:49:27 +0800532static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700533{
Li Zefan0ac801f2013-01-10 11:49:27 +0800534 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700535 struct cgroup_subsys *ss;
536 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700537
Tejun Heo30159ec2013-06-25 11:53:37 -0700538 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800539 key += (unsigned long)css[i];
540 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700541
Li Zefan0ac801f2013-01-10 11:49:27 +0800542 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700543}
544
Zefan Lia25eb522014-09-19 16:51:00 +0800545static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700546{
Tejun Heo69d02062013-06-12 21:04:50 -0700547 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400548 struct cgroup_subsys *ss;
549 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700550
Tejun Heo89c55092014-02-13 06:58:40 -0500551 lockdep_assert_held(&css_set_rwsem);
552
553 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700554 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700555
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700556 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400557 for_each_subsys(ss, ssid)
558 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700559 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700560 css_set_count--;
561
Tejun Heo69d02062013-06-12 21:04:50 -0700562 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700563 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700564
Tejun Heo69d02062013-06-12 21:04:50 -0700565 list_del(&link->cset_link);
566 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800567
Tejun Heo96d365e2014-02-13 06:58:40 -0500568 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400569 if (list_empty(&cgrp->cset_links)) {
570 cgroup_update_populated(cgrp, false);
Zefan Lia25eb522014-09-19 16:51:00 +0800571 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700572 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700573
574 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700575 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700576
Tejun Heo5abb8852013-06-12 21:04:49 -0700577 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700578}
579
Zefan Lia25eb522014-09-19 16:51:00 +0800580static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500581{
582 /*
583 * Ensure that the refcount doesn't hit zero while any readers
584 * can see it. Similar to atomic_dec_and_lock(), but for an
585 * rwlock
586 */
587 if (atomic_add_unless(&cset->refcount, -1, 1))
588 return;
589
590 down_write(&css_set_rwsem);
Zefan Lia25eb522014-09-19 16:51:00 +0800591 put_css_set_locked(cset);
Tejun Heo89c55092014-02-13 06:58:40 -0500592 up_write(&css_set_rwsem);
593}
594
Paul Menage817929e2007-10-18 23:39:36 -0700595/*
596 * refcounted get/put for css_set objects
597 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700598static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700599{
Tejun Heo5abb8852013-06-12 21:04:49 -0700600 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700601}
602
Tejun Heob326f9d2013-06-24 15:21:48 -0700603/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700604 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700605 * @cset: candidate css_set being tested
606 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700607 * @new_cgrp: cgroup that's being entered by the task
608 * @template: desired set of css pointers in css_set (pre-calculated)
609 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800610 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700611 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
612 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700613static bool compare_css_sets(struct css_set *cset,
614 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700615 struct cgroup *new_cgrp,
616 struct cgroup_subsys_state *template[])
617{
618 struct list_head *l1, *l2;
619
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400620 /*
621 * On the default hierarchy, there can be csets which are
622 * associated with the same set of cgroups but different csses.
623 * Let's first ensure that csses match.
624 */
625 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700626 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700627
628 /*
629 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400630 * different cgroups in hierarchies. As different cgroups may
631 * share the same effective css, this comparison is always
632 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 */
Tejun Heo69d02062013-06-12 21:04:50 -0700634 l1 = &cset->cgrp_links;
635 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700636 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700637 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700639
640 l1 = l1->next;
641 l2 = l2->next;
642 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700643 if (l1 == &cset->cgrp_links) {
644 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700645 break;
646 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700647 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700648 }
649 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700650 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
651 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
652 cgrp1 = link1->cgrp;
653 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700654 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700655 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700656
657 /*
658 * If this hierarchy is the hierarchy of the cgroup
659 * that's changing, then we need to check that this
660 * css_set points to the new cgroup; if it's any other
661 * hierarchy, then this css_set should point to the
662 * same cgroup as the old css_set.
663 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700664 if (cgrp1->root == new_cgrp->root) {
665 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700666 return false;
667 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700668 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700669 return false;
670 }
671 }
672 return true;
673}
674
Tejun Heob326f9d2013-06-24 15:21:48 -0700675/**
676 * find_existing_css_set - init css array and find the matching css_set
677 * @old_cset: the css_set that we're using before the cgroup transition
678 * @cgrp: the cgroup that we're moving into
679 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700680 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700681static struct css_set *find_existing_css_set(struct css_set *old_cset,
682 struct cgroup *cgrp,
683 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700684{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400685 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700686 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800688 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700689 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700690
Ben Blumaae8aab2010-03-10 15:22:07 -0800691 /*
692 * Build the set of subsystem state objects that we want to see in the
693 * new css_set. while subsystems can change globally, the entries here
694 * won't change, so no need for locking.
695 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700696 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400697 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400698 /*
699 * @ss is in this hierarchy, so we want the
700 * effective css from @cgrp.
701 */
702 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700703 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400704 /*
705 * @ss is not in this hierarchy, so we don't want
706 * to change the css.
707 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700708 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700709 }
710 }
711
Li Zefan0ac801f2013-01-10 11:49:27 +0800712 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 hash_for_each_possible(css_set_table, cset, hlist, key) {
714 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700715 continue;
716
717 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700718 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700719 }
Paul Menage817929e2007-10-18 23:39:36 -0700720
721 /* No existing cgroup group matched */
722 return NULL;
723}
724
Tejun Heo69d02062013-06-12 21:04:50 -0700725static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700726{
Tejun Heo69d02062013-06-12 21:04:50 -0700727 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700728
Tejun Heo69d02062013-06-12 21:04:50 -0700729 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
730 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700731 kfree(link);
732 }
733}
734
Tejun Heo69d02062013-06-12 21:04:50 -0700735/**
736 * allocate_cgrp_cset_links - allocate cgrp_cset_links
737 * @count: the number of links to allocate
738 * @tmp_links: list_head the allocated links are put on
739 *
740 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
741 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700742 */
Tejun Heo69d02062013-06-12 21:04:50 -0700743static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700744{
Tejun Heo69d02062013-06-12 21:04:50 -0700745 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700746 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700747
748 INIT_LIST_HEAD(tmp_links);
749
Li Zefan36553432008-07-29 22:33:19 -0700750 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700751 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700752 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700753 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700754 return -ENOMEM;
755 }
Tejun Heo69d02062013-06-12 21:04:50 -0700756 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700757 }
758 return 0;
759}
760
Li Zefanc12f65d2009-01-07 18:07:42 -0800761/**
762 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700763 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700764 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800765 * @cgrp: the destination cgroup
766 */
Tejun Heo69d02062013-06-12 21:04:50 -0700767static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
768 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800769{
Tejun Heo69d02062013-06-12 21:04:50 -0700770 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800771
Tejun Heo69d02062013-06-12 21:04:50 -0700772 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400773
774 if (cgroup_on_dfl(cgrp))
775 cset->dfl_cgrp = cgrp;
776
Tejun Heo69d02062013-06-12 21:04:50 -0700777 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
778 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700779 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400780
781 if (list_empty(&cgrp->cset_links))
782 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700783 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400784
Paul Menage7717f7b2009-09-23 15:56:22 -0700785 /*
786 * Always add links to the tail of the list so that the list
787 * is sorted by order of hierarchy creation
788 */
Tejun Heo69d02062013-06-12 21:04:50 -0700789 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800790}
791
Tejun Heob326f9d2013-06-24 15:21:48 -0700792/**
793 * find_css_set - return a new css_set with one cgroup updated
794 * @old_cset: the baseline css_set
795 * @cgrp: the cgroup to be updated
796 *
797 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
798 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700799 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700800static struct css_set *find_css_set(struct css_set *old_cset,
801 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700802{
Tejun Heob326f9d2013-06-24 15:21:48 -0700803 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700804 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700805 struct list_head tmp_links;
806 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400807 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800808 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400809 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700810
Tejun Heob326f9d2013-06-24 15:21:48 -0700811 lockdep_assert_held(&cgroup_mutex);
812
Paul Menage817929e2007-10-18 23:39:36 -0700813 /* First see if we already have a cgroup group that matches
814 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500815 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700816 cset = find_existing_css_set(old_cset, cgrp, template);
817 if (cset)
818 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500819 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700820
Tejun Heo5abb8852013-06-12 21:04:49 -0700821 if (cset)
822 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700823
Tejun Heof4f4be22013-06-12 21:04:51 -0700824 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700825 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700826 return NULL;
827
Tejun Heo69d02062013-06-12 21:04:50 -0700828 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700829 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700830 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700831 return NULL;
832 }
833
Tejun Heo5abb8852013-06-12 21:04:49 -0700834 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700835 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700836 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500837 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500838 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500839 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700840 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700841
842 /* Copy the set of subsystem state objects generated in
843 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700844 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700845
Tejun Heo96d365e2014-02-13 06:58:40 -0500846 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700847 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700848 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700849 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700850
Paul Menage7717f7b2009-09-23 15:56:22 -0700851 if (c->root == cgrp->root)
852 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700853 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700854 }
Paul Menage817929e2007-10-18 23:39:36 -0700855
Tejun Heo69d02062013-06-12 21:04:50 -0700856 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700857
Paul Menage817929e2007-10-18 23:39:36 -0700858 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700859
Tejun Heo2d8f2432014-04-23 11:13:15 -0400860 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700861 key = css_set_hash(cset->subsys);
862 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700863
Tejun Heo2d8f2432014-04-23 11:13:15 -0400864 for_each_subsys(ss, ssid)
865 list_add_tail(&cset->e_cset_node[ssid],
866 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
867
Tejun Heo96d365e2014-02-13 06:58:40 -0500868 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700869
Tejun Heo5abb8852013-06-12 21:04:49 -0700870 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700871}
872
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400873static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700874{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400875 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500876
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400877 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500878}
879
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400880static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500881{
882 int id;
883
884 lockdep_assert_held(&cgroup_mutex);
885
Tejun Heo985ed672014-03-19 10:23:53 -0400886 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500887 if (id < 0)
888 return id;
889
890 root->hierarchy_id = id;
891 return 0;
892}
893
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400894static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500895{
896 lockdep_assert_held(&cgroup_mutex);
897
898 if (root->hierarchy_id) {
899 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
900 root->hierarchy_id = 0;
901 }
902}
903
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400904static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500905{
906 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400907 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -0500908 WARN_ON_ONCE(root->hierarchy_id);
909
910 idr_destroy(&root->cgroup_idr);
911 kfree(root);
912 }
913}
914
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400915static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500916{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400917 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500918 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500919
Tejun Heo2bd59d42014-02-11 11:52:49 -0500920 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500921
Tejun Heo776f02f2014-02-12 09:29:50 -0500922 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -0400923 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -0500924
Tejun Heof2e85d52014-02-11 11:52:49 -0500925 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400926 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500927
928 /*
929 * Release all the links from cset_links to this hierarchy's
930 * root cgroup
931 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500932 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500933
934 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
935 list_del(&link->cset_link);
936 list_del(&link->cgrp_link);
937 kfree(link);
938 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500939 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500940
941 if (!list_empty(&root->root_list)) {
942 list_del(&root->root_list);
943 cgroup_root_count--;
944 }
945
946 cgroup_exit_root_id(root);
947
948 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500949
Tejun Heo2bd59d42014-02-11 11:52:49 -0500950 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500951 cgroup_free_root(root);
952}
953
Tejun Heoceb6a082014-02-25 10:04:02 -0500954/* look up cgroup associated with given css_set on the specified hierarchy */
955static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400956 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700957{
Paul Menage7717f7b2009-09-23 15:56:22 -0700958 struct cgroup *res = NULL;
959
Tejun Heo96d365e2014-02-13 06:58:40 -0500960 lockdep_assert_held(&cgroup_mutex);
961 lockdep_assert_held(&css_set_rwsem);
962
Tejun Heo5abb8852013-06-12 21:04:49 -0700963 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400964 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700965 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700966 struct cgrp_cset_link *link;
967
968 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700969 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700970
Paul Menage7717f7b2009-09-23 15:56:22 -0700971 if (c->root == root) {
972 res = c;
973 break;
974 }
975 }
976 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500977
Paul Menage7717f7b2009-09-23 15:56:22 -0700978 BUG_ON(!res);
979 return res;
980}
981
982/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500983 * Return the cgroup for "task" from the given hierarchy. Must be
984 * called with cgroup_mutex and css_set_rwsem held.
985 */
986static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400987 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500988{
989 /*
990 * No need to lock the task - since we hold cgroup_mutex the
991 * task can't change groups, so the only thing that can happen
992 * is that it exits and its css is set back to init_css_set.
993 */
994 return cset_cgroup_from_root(task_css_set(task), root);
995}
996
997/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 * A task must hold cgroup_mutex to modify cgroups.
999 *
1000 * Any task can increment and decrement the count field without lock.
1001 * So in general, code holding cgroup_mutex can't rely on the count
1002 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001003 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 * means that no tasks are currently attached, therefore there is no
1005 * way a task attached to that cgroup can fork (the other way to
1006 * increment the count). So code holding cgroup_mutex can safely
1007 * assume that if the count is zero, it will stay zero. Similarly, if
1008 * a task holds cgroup_mutex on a cgroup with zero count, it
1009 * knows that the cgroup won't be removed, as cgroup_rmdir()
1010 * needs that mutex.
1011 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 * A cgroup can only be deleted if both its 'count' of using tasks
1013 * is zero, and its list of 'children' cgroups is empty. Since all
1014 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001015 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001017 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 *
1019 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001020 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021 */
1022
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001023static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001024static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001025static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001026
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001027static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1028 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001030 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1031 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1032 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1033 cft->ss->name, cft->name);
1034 else
1035 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1036 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037}
1038
Tejun Heof2e85d52014-02-11 11:52:49 -05001039/**
1040 * cgroup_file_mode - deduce file mode of a control file
1041 * @cft: the control file in question
1042 *
1043 * returns cft->mode if ->mode is not 0
1044 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1045 * returns S_IRUGO if it has only a read handler
1046 * returns S_IWUSR if it has only a write hander
1047 */
1048static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001049{
Tejun Heof2e85d52014-02-11 11:52:49 -05001050 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001051
Tejun Heof2e85d52014-02-11 11:52:49 -05001052 if (cft->mode)
1053 return cft->mode;
1054
1055 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1056 mode |= S_IRUGO;
1057
Tejun Heo6770c642014-05-13 12:16:21 -04001058 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001059 mode |= S_IWUSR;
1060
1061 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001062}
1063
Tejun Heo59f52962014-02-11 11:52:49 -05001064static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001066 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001067 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068}
1069
Li Zefanaa323622014-09-04 14:43:38 +08001070static bool cgroup_tryget(struct cgroup *cgrp)
1071{
1072 return css_tryget(&cgrp->self);
1073}
1074
Tejun Heo59f52962014-02-11 11:52:49 -05001075static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076{
Tejun Heo9d755d32014-05-14 09:15:02 -04001077 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078}
1079
Tejun Heoa9746d82014-05-13 12:19:22 -04001080/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001081 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001082 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001083 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001084 *
1085 * On the default hierarchy, a subsystem may request other subsystems to be
1086 * enabled together through its ->depends_on mask. In such cases, more
1087 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1088 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001089 * This function calculates which subsystems need to be enabled if
1090 * @subtree_control is to be applied to @cgrp. The returned mask is always
1091 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001092 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001093static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1094 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001095{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001096 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001097 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001098 struct cgroup_subsys *ss;
1099 int ssid;
1100
1101 lockdep_assert_held(&cgroup_mutex);
1102
Tejun Heo0f060de2014-11-18 02:49:50 -05001103 if (!cgroup_on_dfl(cgrp))
1104 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001105
1106 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001107 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001108
1109 for_each_subsys(ss, ssid)
1110 if (cur_ss_mask & (1 << ssid))
1111 new_ss_mask |= ss->depends_on;
1112
1113 /*
1114 * Mask out subsystems which aren't available. This can
1115 * happen only if some depended-upon subsystems were bound
1116 * to non-default hierarchies.
1117 */
1118 if (parent)
1119 new_ss_mask &= parent->child_subsys_mask;
1120 else
1121 new_ss_mask &= cgrp->root->subsys_mask;
1122
1123 if (new_ss_mask == cur_ss_mask)
1124 break;
1125 cur_ss_mask = new_ss_mask;
1126 }
1127
Tejun Heo0f060de2014-11-18 02:49:50 -05001128 return cur_ss_mask;
1129}
1130
1131/**
1132 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1133 * @cgrp: the target cgroup
1134 *
1135 * Update @cgrp->child_subsys_mask according to the current
1136 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1137 */
1138static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
1139{
1140 cgrp->child_subsys_mask =
1141 cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
Tejun Heo667c2492014-07-08 18:02:56 -04001142}
1143
Tejun Heoa9746d82014-05-13 12:19:22 -04001144/**
1145 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1146 * @kn: the kernfs_node being serviced
1147 *
1148 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1149 * the method finishes if locking succeeded. Note that once this function
1150 * returns the cgroup returned by cgroup_kn_lock_live() may become
1151 * inaccessible any time. If the caller intends to continue to access the
1152 * cgroup, it should pin it before invoking this function.
1153 */
1154static void cgroup_kn_unlock(struct kernfs_node *kn)
1155{
1156 struct cgroup *cgrp;
1157
1158 if (kernfs_type(kn) == KERNFS_DIR)
1159 cgrp = kn->priv;
1160 else
1161 cgrp = kn->parent->priv;
1162
1163 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001164
1165 kernfs_unbreak_active_protection(kn);
1166 cgroup_put(cgrp);
1167}
1168
1169/**
1170 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1171 * @kn: the kernfs_node being serviced
1172 *
1173 * This helper is to be used by a cgroup kernfs method currently servicing
1174 * @kn. It breaks the active protection, performs cgroup locking and
1175 * verifies that the associated cgroup is alive. Returns the cgroup if
1176 * alive; otherwise, %NULL. A successful return should be undone by a
1177 * matching cgroup_kn_unlock() invocation.
1178 *
1179 * Any cgroup kernfs method implementation which requires locking the
1180 * associated cgroup should use this helper. It avoids nesting cgroup
1181 * locking under kernfs active protection and allows all kernfs operations
1182 * including self-removal.
1183 */
1184static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
1185{
1186 struct cgroup *cgrp;
1187
1188 if (kernfs_type(kn) == KERNFS_DIR)
1189 cgrp = kn->priv;
1190 else
1191 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192
1193 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001194 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001195 * active_ref. cgroup liveliness check alone provides enough
1196 * protection against removal. Ensure @cgrp stays accessible and
1197 * break the active_ref protection.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001198 */
Li Zefanaa323622014-09-04 14:43:38 +08001199 if (!cgroup_tryget(cgrp))
1200 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001201 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001202
Tejun Heoa9746d82014-05-13 12:19:22 -04001203 mutex_lock(&cgroup_mutex);
1204
1205 if (!cgroup_is_dead(cgrp))
1206 return cgrp;
1207
1208 cgroup_kn_unlock(kn);
1209 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001210}
1211
Li Zefan2739d3c2013-01-21 18:18:33 +08001212static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001213{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001214 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215
Tejun Heo01f64742014-05-13 12:19:23 -04001216 lockdep_assert_held(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001217 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001218}
1219
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001220/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001221 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001222 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001223 * @subsys_mask: mask of the subsystem ids whose files should be removed
1224 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001225static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001226{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001227 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001228 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001229
Tejun Heob420ba72013-07-12 12:34:02 -07001230 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001231 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001232
Tejun Heo69dfa002014-05-04 15:09:13 -04001233 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001234 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001235 list_for_each_entry(cfts, &ss->cfts, node)
1236 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001237 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001238}
1239
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001240static int rebind_subsystems(struct cgroup_root *dst_root,
1241 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242{
Tejun Heo30159ec2013-06-25 11:53:37 -07001243 struct cgroup_subsys *ss;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001244 unsigned long tmp_ss_mask;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001245 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001246
Tejun Heoace2bee812014-02-11 11:52:47 -05001247 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001248
Tejun Heo5df36032014-03-19 10:23:54 -04001249 for_each_subsys(ss, ssid) {
1250 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001251 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001252
Tejun Heo7fd8c562014-04-23 11:13:16 -04001253 /* if @ss has non-root csses attached to it, can't move */
1254 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001255 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001256
Tejun Heo5df36032014-03-19 10:23:54 -04001257 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001258 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001259 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001260 }
1261
Tejun Heo5533e012014-05-14 19:33:07 -04001262 /* skip creating root files on dfl_root for inhibited subsystems */
1263 tmp_ss_mask = ss_mask;
1264 if (dst_root == &cgrp_dfl_root)
1265 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1266
1267 ret = cgroup_populate_dir(&dst_root->cgrp, tmp_ss_mask);
Tejun Heoa2dd4242014-03-19 10:23:55 -04001268 if (ret) {
1269 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001270 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271
Tejun Heoa2dd4242014-03-19 10:23:55 -04001272 /*
1273 * Rebinding back to the default root is not allowed to
1274 * fail. Using both default and non-default roots should
1275 * be rare. Moving subsystems back and forth even more so.
1276 * Just warn about it and continue.
1277 */
1278 if (cgrp_dfl_root_visible) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001279 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001280 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001281 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001282 }
Tejun Heo5df36032014-03-19 10:23:54 -04001283 }
Tejun Heo31261212013-06-28 17:07:30 -07001284
1285 /*
1286 * Nothing can fail from this point on. Remove files for the
1287 * removed subsystems and rebind each subsystem.
1288 */
Tejun Heo5df36032014-03-19 10:23:54 -04001289 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001290 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001291 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001292
Tejun Heo5df36032014-03-19 10:23:54 -04001293 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001294 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001295 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001296 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001297
Tejun Heo5df36032014-03-19 10:23:54 -04001298 if (!(ss_mask & (1 << ssid)))
1299 continue;
Tejun Heoa8a648c42013-06-24 15:21:47 -07001300
Tejun Heo5df36032014-03-19 10:23:54 -04001301 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001302 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001303
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001304 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c42013-06-24 15:21:47 -07001305
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001306 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1307 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001308 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001309 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c42013-06-24 15:21:47 -07001310
Tejun Heo2d8f2432014-04-23 11:13:15 -04001311 down_write(&css_set_rwsem);
1312 hash_for_each(css_set_table, i, cset, hlist)
1313 list_move_tail(&cset->e_cset_node[ss->id],
1314 &dst_root->cgrp.e_csets[ss->id]);
1315 up_write(&css_set_rwsem);
1316
Tejun Heof392e512014-04-23 11:13:14 -04001317 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo667c2492014-07-08 18:02:56 -04001318 src_root->cgrp.subtree_control &= ~(1 << ssid);
1319 cgroup_refresh_child_subsys_mask(&src_root->cgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001320
Tejun Heobd53d612014-04-23 11:13:16 -04001321 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001322 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo667c2492014-07-08 18:02:56 -04001323 if (dst_root != &cgrp_dfl_root) {
1324 dst_root->cgrp.subtree_control |= 1 << ssid;
1325 cgroup_refresh_child_subsys_mask(&dst_root->cgrp);
1326 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001327
Tejun Heo5df36032014-03-19 10:23:54 -04001328 if (ss->bind)
1329 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331
Tejun Heoa2dd4242014-03-19 10:23:55 -04001332 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 return 0;
1334}
1335
Tejun Heo2bd59d42014-02-11 11:52:49 -05001336static int cgroup_show_options(struct seq_file *seq,
1337 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001339 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001341 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
Tejun Heob85d2042013-12-06 15:11:57 -05001343 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001344 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001345 seq_printf(seq, ",%s", ss->name);
Tejun Heo93438622013-04-14 20:15:25 -07001346 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001348 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001349 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001350
1351 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001352 if (strlen(root->release_agent_path))
1353 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001354 spin_unlock(&release_agent_path_lock);
1355
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001356 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001357 seq_puts(seq, ",clone_children");
Paul Menagec6d57f332009-09-23 15:56:19 -07001358 if (strlen(root->name))
1359 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360 return 0;
1361}
1362
1363struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001364 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001365 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001366 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001367 bool cpuset_clone_children;
Paul Menagec6d57f332009-09-23 15:56:19 -07001368 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001369 /* User explicitly requested empty subsystem */
1370 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371};
1372
Ben Blumcf5d5942010-03-10 15:22:09 -08001373static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374{
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001375 char *token, *o = data;
1376 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001377 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001378 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001379 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001380 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001381
1382#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001383 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001384#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385
Paul Menagec6d57f332009-09-23 15:56:19 -07001386 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387
1388 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001389 nr_opts++;
1390
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 if (!*token)
1392 return -EINVAL;
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001393 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001394 /* Explicitly have no subsystems */
1395 opts->none = true;
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001396 continue;
1397 }
1398 if (!strcmp(token, "all")) {
1399 /* Mutually exclusive option 'all' + subsystem name */
1400 if (one_ss)
1401 return -EINVAL;
1402 all_ss = true;
1403 continue;
1404 }
Tejun Heo873fe092013-04-14 20:15:26 -07001405 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1406 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1407 continue;
1408 }
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001409 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001410 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001411 continue;
1412 }
1413 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001414 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001415 continue;
1416 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001417 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001418 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001419 continue;
1420 }
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001421 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001422 /* Specifying two release agents is forbidden */
1423 if (opts->release_agent)
1424 return -EINVAL;
Paul Menagec6d57f332009-09-23 15:56:19 -07001425 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001426 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001427 if (!opts->release_agent)
1428 return -ENOMEM;
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001429 continue;
1430 }
1431 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f332009-09-23 15:56:19 -07001432 const char *name = token + 5;
1433 /* Can't specify an empty name */
1434 if (!strlen(name))
1435 return -EINVAL;
1436 /* Must match [\w.-]+ */
1437 for (i = 0; i < strlen(name); i++) {
1438 char c = name[i];
1439 if (isalnum(c))
1440 continue;
1441 if ((c == '.') || (c == '-') || (c == '_'))
1442 continue;
1443 return -EINVAL;
1444 }
1445 /* Specifying two names is forbidden */
1446 if (opts->name)
1447 return -EINVAL;
1448 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001449 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f332009-09-23 15:56:19 -07001450 GFP_KERNEL);
1451 if (!opts->name)
1452 return -ENOMEM;
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001453
1454 continue;
1455 }
1456
Tejun Heo30159ec2013-06-25 11:53:37 -07001457 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001458 if (strcmp(token, ss->name))
1459 continue;
1460 if (ss->disabled)
1461 continue;
1462
1463 /* Mutually exclusive option 'all' + subsystem name */
1464 if (all_ss)
1465 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001466 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf2352010-10-27 15:33:37 -07001467 one_ss = true;
1468
1469 break;
1470 }
1471 if (i == CGROUP_SUBSYS_COUNT)
1472 return -ENOENT;
1473 }
1474
Tejun Heo873fe092013-04-14 20:15:26 -07001475 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001476 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 -04001477 if (nr_opts != 1) {
1478 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001479 return -EINVAL;
1480 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001481 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001482 }
1483
Li Zefanf9ab5b52009-06-17 16:26:33 -07001484 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001485 * If the 'all' option was specified select all the subsystems,
1486 * otherwise if 'none', 'name=' and a subsystem name options were
1487 * not specified, let's default to 'all'
1488 */
1489 if (all_ss || (!one_ss && !opts->none && !opts->name))
1490 for_each_subsys(ss, i)
1491 if (!ss->disabled)
1492 opts->subsys_mask |= (1 << i);
1493
1494 /*
1495 * We either have to specify by name or by subsystems. (So all
1496 * empty hierarchies must have a name).
1497 */
1498 if (!opts->subsys_mask && !opts->name)
1499 return -EINVAL;
1500
1501 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001502 * Option noprefix was introduced just for backward compatibility
1503 * with the old cpuset, so we allow noprefix only if mounting just
1504 * the cpuset subsystem.
1505 */
Tejun Heo93438622013-04-14 20:15:25 -07001506 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001507 return -EINVAL;
1508
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001509 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001510 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511 return -EINVAL;
1512
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513 return 0;
1514}
1515
Tejun Heo2bd59d42014-02-11 11:52:49 -05001516static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517{
1518 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001519 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001521 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522
Tejun Heoaa6ec292014-07-09 10:08:08 -04001523 if (root == &cgrp_dfl_root) {
1524 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001525 return -EINVAL;
1526 }
1527
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 mutex_lock(&cgroup_mutex);
1529
1530 /* See what subsystems are wanted */
1531 ret = parse_cgroupfs_options(data, &opts);
1532 if (ret)
1533 goto out_unlock;
1534
Tejun Heof392e512014-04-23 11:13:14 -04001535 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001536 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001537 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001538
Tejun Heof392e512014-04-23 11:13:14 -04001539 added_mask = opts.subsys_mask & ~root->subsys_mask;
1540 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001541
Ben Blumcf5d5942010-03-10 15:22:09 -08001542 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001543 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001544 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001545 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001546 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 ret = -EINVAL;
Paul Menagec6d57f332009-09-23 15:56:19 -07001548 goto out_unlock;
1549 }
1550
Tejun Heof172e672013-06-28 17:07:30 -07001551 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001552 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001553 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001554 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001555 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556
Tejun Heo5df36032014-03-19 10:23:54 -04001557 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001558 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001561 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001562
Tejun Heo69e943b2014-02-08 10:36:58 -05001563 if (opts.release_agent) {
1564 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001565 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001566 spin_unlock(&release_agent_path_lock);
1567 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001569 kfree(opts.release_agent);
Paul Menagec6d57f332009-09-23 15:56:19 -07001570 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572 return ret;
1573}
1574
Tejun Heoafeb0f92014-02-13 06:58:39 -05001575/*
1576 * To reduce the fork() overhead for systems that are not actually using
1577 * their cgroups capability, we don't maintain the lists running through
1578 * each css_set to its tasks until we see the list actually used - in other
1579 * words after the first mount.
1580 */
1581static bool use_task_css_set_links __read_mostly;
1582
1583static void cgroup_enable_task_cg_lists(void)
1584{
1585 struct task_struct *p, *g;
1586
Tejun Heo96d365e2014-02-13 06:58:40 -05001587 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001588
1589 if (use_task_css_set_links)
1590 goto out_unlock;
1591
1592 use_task_css_set_links = true;
1593
1594 /*
1595 * We need tasklist_lock because RCU is not safe against
1596 * while_each_thread(). Besides, a forking task that has passed
1597 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1598 * is not guaranteed to have its child immediately visible in the
1599 * tasklist if we walk through it with RCU.
1600 */
1601 read_lock(&tasklist_lock);
1602 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001603 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1604 task_css_set(p) != &init_css_set);
1605
1606 /*
1607 * We should check if the process is exiting, otherwise
1608 * it will race with cgroup_exit() in that the list
1609 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001610 * Do it while holding siglock so that we don't end up
1611 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001612 */
Tejun Heof153ad12014-02-25 09:56:49 -05001613 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001614 if (!(p->flags & PF_EXITING)) {
1615 struct css_set *cset = task_css_set(p);
1616
1617 list_add(&p->cg_list, &cset->tasks);
1618 get_css_set(cset);
1619 }
Tejun Heof153ad12014-02-25 09:56:49 -05001620 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001621 } while_each_thread(g, p);
1622 read_unlock(&tasklist_lock);
1623out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001624 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001625}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Paul Menagecc31edc2008-10-18 20:28:04 -07001627static void init_cgroup_housekeeping(struct cgroup *cgrp)
1628{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001629 struct cgroup_subsys *ss;
1630 int ssid;
1631
Tejun Heod5c419b2014-05-16 13:22:48 -04001632 INIT_LIST_HEAD(&cgrp->self.sibling);
1633 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001634 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001635 INIT_LIST_HEAD(&cgrp->pidlists);
1636 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001637 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001638 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001639
1640 for_each_subsys(ss, ssid)
1641 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001642
1643 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001644 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001645}
Paul Menagec6d57f332009-09-23 15:56:19 -07001646
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001647static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001648 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001650 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001651
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001653 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001654 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001655 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001656 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657
Paul Menagec6d57f332009-09-23 15:56:19 -07001658 root->flags = opts->flags;
1659 if (opts->release_agent)
1660 strcpy(root->release_agent_path, opts->release_agent);
1661 if (opts->name)
1662 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001663 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001664 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f332009-09-23 15:56:19 -07001665}
1666
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001667static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001668{
Tejun Heod427dfe2014-02-11 11:52:48 -05001669 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001670 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heoa14c6872014-07-15 11:05:09 -04001671 struct cftype *base_files;
Tejun Heod427dfe2014-02-11 11:52:48 -05001672 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001673 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001674
Tejun Heod427dfe2014-02-11 11:52:48 -05001675 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001676
Tejun Heo6fa49182014-05-04 15:09:13 -04001677 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001678 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001679 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001680 root_cgrp->id = ret;
Paul Menagec6d57f332009-09-23 15:56:19 -07001681
Tejun Heo2aad2a82014-09-24 13:31:50 -04001682 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1683 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001684 if (ret)
1685 goto out;
1686
Tejun Heod427dfe2014-02-11 11:52:48 -05001687 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001688 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001689 * but that's OK - it can only be increased by someone holding
1690 * cgroup_lock, and that's us. The worst that can happen is that we
1691 * have some link structures left over
1692 */
1693 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001695 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
Tejun Heo985ed672014-03-19 10:23:53 -04001697 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001698 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001699 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700
Tejun Heo2bd59d42014-02-11 11:52:49 -05001701 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1702 KERNFS_ROOT_CREATE_DEACTIVATED,
1703 root_cgrp);
1704 if (IS_ERR(root->kf_root)) {
1705 ret = PTR_ERR(root->kf_root);
1706 goto exit_root_id;
1707 }
1708 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709
Tejun Heoa14c6872014-07-15 11:05:09 -04001710 if (root == &cgrp_dfl_root)
1711 base_files = cgroup_dfl_base_files;
1712 else
1713 base_files = cgroup_legacy_base_files;
1714
1715 ret = cgroup_addrm_files(root_cgrp, base_files, true);
Tejun Heod427dfe2014-02-11 11:52:48 -05001716 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001717 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718
Tejun Heo5df36032014-03-19 10:23:54 -04001719 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001720 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001721 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001722
Tejun Heod427dfe2014-02-11 11:52:48 -05001723 /*
1724 * There must be no failure case after here, since rebinding takes
1725 * care of subsystems' refcounts, which are explicitly dropped in
1726 * the failure exit path.
1727 */
1728 list_add(&root->root_list, &cgroup_roots);
1729 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
Tejun Heod427dfe2014-02-11 11:52:48 -05001731 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001732 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001733 * objects.
1734 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001735 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001736 hash_for_each(css_set_table, i, cset, hlist)
1737 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001738 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739
Tejun Heod5c419b2014-05-16 13:22:48 -04001740 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001741 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001742
Tejun Heo2bd59d42014-02-11 11:52:49 -05001743 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001744 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001745 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001746
Tejun Heo2bd59d42014-02-11 11:52:49 -05001747destroy_root:
1748 kernfs_destroy_root(root->kf_root);
1749 root->kf_root = NULL;
1750exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001751 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001752cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001753 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001754out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001755 free_cgrp_cset_links(&tmp_links);
1756 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757}
1758
Al Virof7e83572010-07-26 13:23:11 +04001759static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001761 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762{
Li Zefan3a32bd72014-06-30 11:50:59 +08001763 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001764 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001765 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001767 struct dentry *dentry;
Tejun Heo8e30e2b82014-02-11 11:52:48 -05001768 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001769 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001770 bool new_sb;
Paul Menagec6d57f332009-09-23 15:56:19 -07001771
1772 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001773 * The first time anyone tries to mount a cgroup, enable the list
1774 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f332009-09-23 15:56:19 -07001775 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001776 if (!use_task_css_set_links)
1777 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001778
Tejun Heo8e30e2b82014-02-11 11:52:48 -05001779 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783 if (ret)
Tejun Heo8e30e2b82014-02-11 11:52:48 -05001784 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001785
Tejun Heo2bd59d42014-02-11 11:52:49 -05001786 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001787 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04001788 cgrp_dfl_root_visible = true;
1789 root = &cgrp_dfl_root;
1790 cgroup_get(&root->cgrp);
1791 ret = 0;
1792 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 }
1794
Li Zefan970317a2014-06-30 11:49:58 +08001795 /*
1796 * Destruction of cgroup root is asynchronous, so subsystems may
1797 * still be dying after the previous unmount. Let's drain the
1798 * dying subsystems. We just need to ensure that the ones
1799 * unmounted previously finish dying and don't care about new ones
1800 * starting. Testing ref liveliness is good enough.
1801 */
1802 for_each_subsys(ss, i) {
1803 if (!(opts.subsys_mask & (1 << i)) ||
1804 ss->root == &cgrp_dfl_root)
1805 continue;
1806
1807 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1808 mutex_unlock(&cgroup_mutex);
1809 msleep(10);
1810 ret = restart_syscall();
1811 goto out_free;
1812 }
1813 cgroup_put(&ss->root->cgrp);
1814 }
1815
Tejun Heo985ed672014-03-19 10:23:53 -04001816 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001817 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001819 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001820 continue;
Paul Menagec6d57f332009-09-23 15:56:19 -07001821
Paul Menage817929e2007-10-18 23:39:36 -07001822 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001823 * If we asked for a name then it must match. Also, if
1824 * name matches but sybsys_mask doesn't, we should fail.
1825 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001826 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001827 if (opts.name) {
1828 if (strcmp(opts.name, root->name))
1829 continue;
1830 name_match = true;
1831 }
Tejun Heo31261212013-06-28 17:07:30 -07001832
1833 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001834 * If we asked for subsystems (or explicitly for no
1835 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001836 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001837 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001838 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001839 if (!name_match)
1840 continue;
1841 ret = -EBUSY;
1842 goto out_unlock;
1843 }
Tejun Heo873fe092013-04-14 20:15:26 -07001844
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001845 if (root->flags ^ opts.flags)
1846 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05001847
Tejun Heo776f02f2014-02-12 09:29:50 -05001848 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08001849 * We want to reuse @root whose lifetime is governed by its
1850 * ->cgrp. Let's check whether @root is alive and keep it
1851 * that way. As cgroup_kill_sb() can happen anytime, we
1852 * want to block it by pinning the sb so that @root doesn't
1853 * get killed before mount is complete.
1854 *
1855 * With the sb pinned, tryget_live can reliably indicate
1856 * whether @root can be reused. If it's being killed,
1857 * drain it. We can use wait_queue for the wait but this
1858 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001859 */
Li Zefan3a32bd72014-06-30 11:50:59 +08001860 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1861 if (IS_ERR(pinned_sb) ||
1862 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001863 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08001864 if (!IS_ERR_OR_NULL(pinned_sb))
1865 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05001866 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001867 ret = restart_syscall();
1868 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001869 }
1870
1871 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001872 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001873 }
1874
Tejun Heo172a2c062014-03-19 10:23:53 -04001875 /*
1876 * No such thing, create a new one. name= matching without subsys
1877 * specification is allowed for already existing hierarchies but we
1878 * can't create new one without subsys specification.
1879 */
1880 if (!opts.subsys_mask && !opts.none) {
1881 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001882 goto out_unlock;
1883 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001884
Tejun Heo172a2c062014-03-19 10:23:53 -04001885 root = kzalloc(sizeof(*root), GFP_KERNEL);
1886 if (!root) {
1887 ret = -ENOMEM;
1888 goto out_unlock;
1889 }
1890
1891 init_cgroup_root(root, &opts);
1892
Tejun Heo35585572014-02-13 06:58:38 -05001893 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001894 if (ret)
1895 cgroup_free_root(root);
1896
Tejun Heo8e30e2b82014-02-11 11:52:48 -05001897out_unlock:
Tejun Heoe25e2cbb2011-12-12 18:12:21 -08001898 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001899out_free:
Paul Menagec6d57f332009-09-23 15:56:19 -07001900 kfree(opts.release_agent);
1901 kfree(opts.name);
Tejun Heo8e30e2b82014-02-11 11:52:48 -05001902
Tejun Heo2bd59d42014-02-11 11:52:49 -05001903 if (ret)
Tejun Heo8e30e2b82014-02-11 11:52:48 -05001904 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001905
Jianyu Zhanc9482a52014-04-26 15:40:28 +08001906 dentry = kernfs_mount(fs_type, flags, root->kf_root,
1907 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001908 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001909 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08001910
1911 /*
1912 * If @pinned_sb, we're reusing an existing root and holding an
1913 * extra ref on its sb. Mount is complete. Put the extra ref.
1914 */
1915 if (pinned_sb) {
1916 WARN_ON(new_sb);
1917 deactivate_super(pinned_sb);
1918 }
1919
Tejun Heo2bd59d42014-02-11 11:52:49 -05001920 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001921}
1922
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001923static void cgroup_kill_sb(struct super_block *sb)
1924{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001925 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001926 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001927
Tejun Heo9d755d32014-05-14 09:15:02 -04001928 /*
1929 * If @root doesn't have any mounts or children, start killing it.
1930 * This prevents new mounts by disabling percpu_ref_tryget_live().
1931 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08001932 *
1933 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04001934 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05001935 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08001936 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04001937 cgroup_put(&root->cgrp);
1938 else
1939 percpu_ref_kill(&root->cgrp.self.refcnt);
1940
Tejun Heo2bd59d42014-02-11 11:52:49 -05001941 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001942}
1943
1944static struct file_system_type cgroup_fs_type = {
1945 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001946 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001947 .kill_sb = cgroup_kill_sb,
1948};
1949
Greg KH676db4a2010-08-05 13:53:35 -07001950static struct kobject *cgroup_kobj;
1951
Li Zefana043e3b2008-02-23 15:24:09 -08001952/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001953 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001954 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001955 * @buf: the buffer to write the path into
1956 * @buflen: the length of the buffer
1957 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001958 * Determine @task's cgroup on the first (the one with the lowest non-zero
1959 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1960 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1961 * cgroup controller callbacks.
1962 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001963 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001964 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001965char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001966{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001967 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001968 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001969 int hierarchy_id = 1;
1970 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001971
1972 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001973 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001974
Tejun Heo913ffdb2013-07-11 16:34:48 -07001975 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1976
Tejun Heo857a2be2013-04-14 20:50:08 -07001977 if (root) {
1978 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001979 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001980 } else {
1981 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001982 if (strlcpy(buf, "/", buflen) < buflen)
1983 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001984 }
1985
Tejun Heo96d365e2014-02-13 06:58:40 -05001986 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001987 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001988 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001989}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001990EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001991
Tejun Heob3dc0942014-02-25 10:04:01 -05001992/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001993struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001994 /* the src and dst cset list running through cset->mg_node */
1995 struct list_head src_csets;
1996 struct list_head dst_csets;
1997
1998 /*
1999 * Fields for cgroup_taskset_*() iteration.
2000 *
2001 * Before migration is committed, the target migration tasks are on
2002 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2003 * the csets on ->dst_csets. ->csets point to either ->src_csets
2004 * or ->dst_csets depending on whether migration is committed.
2005 *
2006 * ->cur_csets and ->cur_task point to the current task position
2007 * during iteration.
2008 */
2009 struct list_head *csets;
2010 struct css_set *cur_cset;
2011 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002012};
2013
2014/**
2015 * cgroup_taskset_first - reset taskset and return the first task
2016 * @tset: taskset of interest
2017 *
2018 * @tset iteration is initialized and the first task is returned.
2019 */
2020struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2021{
Tejun Heob3dc0942014-02-25 10:04:01 -05002022 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2023 tset->cur_task = NULL;
2024
2025 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002026}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002027
2028/**
2029 * cgroup_taskset_next - iterate to the next task in taskset
2030 * @tset: taskset of interest
2031 *
2032 * Return the next task in @tset. Iteration must have been initialized
2033 * with cgroup_taskset_first().
2034 */
2035struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2036{
Tejun Heob3dc0942014-02-25 10:04:01 -05002037 struct css_set *cset = tset->cur_cset;
2038 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002039
Tejun Heob3dc0942014-02-25 10:04:01 -05002040 while (&cset->mg_node != tset->csets) {
2041 if (!task)
2042 task = list_first_entry(&cset->mg_tasks,
2043 struct task_struct, cg_list);
2044 else
2045 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002046
Tejun Heob3dc0942014-02-25 10:04:01 -05002047 if (&task->cg_list != &cset->mg_tasks) {
2048 tset->cur_cset = cset;
2049 tset->cur_task = task;
2050 return task;
2051 }
2052
2053 cset = list_next_entry(cset, mg_node);
2054 task = NULL;
2055 }
2056
2057 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002058}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002059
2060/**
Ben Blum74a11662011-05-26 16:25:20 -07002061 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02002062 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002063 * @tsk: the task being migrated
2064 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07002065 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002066 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07002067 */
Tejun Heo5abb8852013-06-12 21:04:49 -07002068static void cgroup_task_migrate(struct cgroup *old_cgrp,
2069 struct task_struct *tsk,
2070 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07002071{
Tejun Heo5abb8852013-06-12 21:04:49 -07002072 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07002073
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002074 lockdep_assert_held(&cgroup_mutex);
2075 lockdep_assert_held(&css_set_rwsem);
2076
Ben Blum74a11662011-05-26 16:25:20 -07002077 /*
Tejun Heod59cfc02015-05-13 16:35:17 -04002078 * We are synchronized through cgroup_threadgroup_rwsem against
2079 * PF_EXITING setting such that we can't race against cgroup_exit()
2080 * changing the css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07002081 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01002082 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002083 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002084
Tejun Heob3dc0942014-02-25 10:04:01 -05002085 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07002086 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002087
Tejun Heo1b9aba42014-03-19 17:43:21 -04002088 /*
2089 * Use move_tail so that cgroup_taskset_first() still returns the
2090 * leader after migration. This works because cgroup_migrate()
2091 * ensures that the dst_cset of the leader is the first on the
2092 * tset's dst_csets list.
2093 */
2094 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07002095
2096 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07002097 * We just gained a reference on old_cset by taking it from the
2098 * task. As trading it for new_cset is protected by cgroup_mutex,
2099 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07002100 */
Zefan Lia25eb522014-09-19 16:51:00 +08002101 put_css_set_locked(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002102}
2103
Li Zefana043e3b2008-02-23 15:24:09 -08002104/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002105 * cgroup_migrate_finish - cleanup after attach
2106 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002107 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002108 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2109 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002110 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002111static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002112{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002113 struct css_set *cset, *tmp_cset;
2114
2115 lockdep_assert_held(&cgroup_mutex);
2116
2117 down_write(&css_set_rwsem);
2118 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2119 cset->mg_src_cgrp = NULL;
2120 cset->mg_dst_cset = NULL;
2121 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002122 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002123 }
2124 up_write(&css_set_rwsem);
2125}
2126
2127/**
2128 * cgroup_migrate_add_src - add a migration source css_set
2129 * @src_cset: the source css_set to add
2130 * @dst_cgrp: the destination cgroup
2131 * @preloaded_csets: list of preloaded css_sets
2132 *
2133 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2134 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2135 * up by cgroup_migrate_finish().
2136 *
Tejun Heod59cfc02015-05-13 16:35:17 -04002137 * This function may be called without holding cgroup_threadgroup_rwsem
2138 * even if the target is a process. Threads may be created and destroyed
2139 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2140 * into play and the preloaded css_sets are guaranteed to cover all
2141 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002142 */
2143static void cgroup_migrate_add_src(struct css_set *src_cset,
2144 struct cgroup *dst_cgrp,
2145 struct list_head *preloaded_csets)
2146{
2147 struct cgroup *src_cgrp;
2148
2149 lockdep_assert_held(&cgroup_mutex);
2150 lockdep_assert_held(&css_set_rwsem);
2151
2152 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2153
Tejun Heo1958d2d2014-02-25 10:04:03 -05002154 if (!list_empty(&src_cset->mg_preload_node))
2155 return;
2156
2157 WARN_ON(src_cset->mg_src_cgrp);
2158 WARN_ON(!list_empty(&src_cset->mg_tasks));
2159 WARN_ON(!list_empty(&src_cset->mg_node));
2160
2161 src_cset->mg_src_cgrp = src_cgrp;
2162 get_css_set(src_cset);
2163 list_add(&src_cset->mg_preload_node, preloaded_csets);
2164}
2165
2166/**
2167 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002168 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002169 * @preloaded_csets: list of preloaded source css_sets
2170 *
2171 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2172 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002173 * pins all destination css_sets, links each to its source, and append them
2174 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2175 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002176 *
2177 * This function must be called after cgroup_migrate_add_src() has been
2178 * called on each migration source css_set. After migration is performed
2179 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2180 * @preloaded_csets.
2181 */
2182static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2183 struct list_head *preloaded_csets)
2184{
2185 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002186 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002187
2188 lockdep_assert_held(&cgroup_mutex);
2189
Tejun Heof8f22e52014-04-23 11:13:16 -04002190 /*
2191 * Except for the root, child_subsys_mask must be zero for a cgroup
2192 * with tasks so that child cgroups don't compete against tasks.
2193 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002194 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002195 dst_cgrp->child_subsys_mask)
2196 return -EBUSY;
2197
Tejun Heo1958d2d2014-02-25 10:04:03 -05002198 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002199 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002200 struct css_set *dst_cset;
2201
Tejun Heof817de92014-04-23 11:13:16 -04002202 dst_cset = find_css_set(src_cset,
2203 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002204 if (!dst_cset)
2205 goto err;
2206
2207 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002208
2209 /*
2210 * If src cset equals dst, it's noop. Drop the src.
2211 * cgroup_migrate() will skip the cset too. Note that we
2212 * can't handle src == dst as some nodes are used by both.
2213 */
2214 if (src_cset == dst_cset) {
2215 src_cset->mg_src_cgrp = NULL;
2216 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002217 put_css_set(src_cset);
2218 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002219 continue;
2220 }
2221
Tejun Heo1958d2d2014-02-25 10:04:03 -05002222 src_cset->mg_dst_cset = dst_cset;
2223
2224 if (list_empty(&dst_cset->mg_preload_node))
2225 list_add(&dst_cset->mg_preload_node, &csets);
2226 else
Zefan Lia25eb522014-09-19 16:51:00 +08002227 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002228 }
2229
Tejun Heof817de92014-04-23 11:13:16 -04002230 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002231 return 0;
2232err:
2233 cgroup_migrate_finish(&csets);
2234 return -ENOMEM;
2235}
2236
2237/**
2238 * cgroup_migrate - migrate a process or task to a cgroup
2239 * @cgrp: the destination cgroup
2240 * @leader: the leader of the process or the task to migrate
2241 * @threadgroup: whether @leader points to the whole process or a single task
2242 *
2243 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heod59cfc02015-05-13 16:35:17 -04002244 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002245 * caller is also responsible for invoking cgroup_migrate_add_src() and
2246 * cgroup_migrate_prepare_dst() on the targets before invoking this
2247 * function and following up with cgroup_migrate_finish().
2248 *
2249 * As long as a controller's ->can_attach() doesn't fail, this function is
2250 * guaranteed to succeed. This means that, excluding ->can_attach()
2251 * failure, when migrating multiple targets, the success or failure can be
2252 * decided for all targets by invoking group_migrate_prepare_dst() before
2253 * actually starting migrating.
2254 */
2255static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2256 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002257{
Tejun Heob3dc0942014-02-25 10:04:01 -05002258 struct cgroup_taskset tset = {
2259 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2260 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2261 .csets = &tset.src_csets,
2262 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002263 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002264 struct css_set *cset, *tmp_cset;
2265 struct task_struct *task, *tmp_task;
2266 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002267
2268 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002269 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2270 * already PF_EXITING could be freed from underneath us unless we
2271 * take an rcu_read_lock.
2272 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002273 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002274 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002275 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002276 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002277 /* @task either already exited or can't exit until the end */
2278 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002279 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002280
Tejun Heoeaf797a2014-02-25 10:04:03 -05002281 /* leave @task alone if post_fork() hasn't linked it yet */
2282 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002283 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002284
Tejun Heob3dc0942014-02-25 10:04:01 -05002285 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002286 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002287 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002288
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002289 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002290 * cgroup_taskset_first() must always return the leader.
2291 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002292 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002293 list_move_tail(&task->cg_list, &cset->mg_tasks);
2294 if (list_empty(&cset->mg_node))
2295 list_add_tail(&cset->mg_node, &tset.src_csets);
2296 if (list_empty(&cset->mg_dst_cset->mg_node))
2297 list_move_tail(&cset->mg_dst_cset->mg_node,
2298 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002299 next:
Li Zefan081aa452013-03-13 09:17:09 +08002300 if (!threadgroup)
2301 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002302 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002303 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002304 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002305
Tejun Heo134d3372011-12-12 18:12:21 -08002306 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002307 if (list_empty(&tset.src_csets))
2308 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002309
Tejun Heo1958d2d2014-02-25 10:04:03 -05002310 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002311 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002312 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002313 ret = css->ss->can_attach(css, &tset);
2314 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002315 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002316 goto out_cancel_attach;
2317 }
2318 }
Ben Blum74a11662011-05-26 16:25:20 -07002319 }
2320
2321 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002322 * Now that we're guaranteed success, proceed to move all tasks to
2323 * the new cgroup. There are no failure cases after here, so this
2324 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002325 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002326 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002327 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2328 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2329 cgroup_task_migrate(cset->mg_src_cgrp, task,
2330 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002331 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002332 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002333
2334 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002335 * Migration is committed, all target tasks are now on dst_csets.
2336 * Nothing is sensitive to fork() after this point. Notify
2337 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002338 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002339 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002340
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002341 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002342 if (css->ss->attach)
2343 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002344
Tejun Heo9db8de32014-02-13 06:58:43 -05002345 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002346 goto out_release_tset;
2347
Ben Blum74a11662011-05-26 16:25:20 -07002348out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002349 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002350 if (css == failed_css)
2351 break;
2352 if (css->ss->cancel_attach)
2353 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002354 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002355out_release_tset:
2356 down_write(&css_set_rwsem);
2357 list_splice_init(&tset.dst_csets, &tset.src_csets);
2358 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002359 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002360 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002361 }
2362 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002363 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002364}
2365
Tejun Heo1958d2d2014-02-25 10:04:03 -05002366/**
2367 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2368 * @dst_cgrp: the cgroup to attach to
2369 * @leader: the task or the leader of the threadgroup to be attached
2370 * @threadgroup: attach the whole threadgroup?
2371 *
Tejun Heod59cfc02015-05-13 16:35:17 -04002372 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002373 */
2374static int cgroup_attach_task(struct cgroup *dst_cgrp,
2375 struct task_struct *leader, bool threadgroup)
2376{
2377 LIST_HEAD(preloaded_csets);
2378 struct task_struct *task;
2379 int ret;
2380
2381 /* look up all src csets */
2382 down_read(&css_set_rwsem);
2383 rcu_read_lock();
2384 task = leader;
2385 do {
2386 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2387 &preloaded_csets);
2388 if (!threadgroup)
2389 break;
2390 } while_each_thread(leader, task);
2391 rcu_read_unlock();
2392 up_read(&css_set_rwsem);
2393
2394 /* prepare dst csets and commit */
2395 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2396 if (!ret)
2397 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2398
2399 cgroup_migrate_finish(&preloaded_csets);
2400 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002401}
2402
2403/*
2404 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002405 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002406 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002407 */
Tejun Heoacbef752014-05-13 12:16:22 -04002408static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2409 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002410{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002411 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002412 const struct cred *cred = current_cred(), *tcred;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002413 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002414 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002415 int ret;
2416
Tejun Heoacbef752014-05-13 12:16:22 -04002417 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2418 return -EINVAL;
2419
Tejun Heoe76ecae2014-05-13 12:19:23 -04002420 cgrp = cgroup_kn_lock_live(of->kn);
2421 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002422 return -ENODEV;
2423
Tejun Heob5ba75b2015-05-13 16:35:18 -04002424 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002425 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002426 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002427 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002428 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002429 ret = -ESRCH;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002430 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002431 }
Ben Blum74a11662011-05-26 16:25:20 -07002432 /*
2433 * even if we're attaching all tasks in the thread group, we
2434 * only need to check permissions on one of them.
2435 */
David Howellsc69e8d92008-11-14 10:39:19 +11002436 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002437 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2438 !uid_eq(cred->euid, tcred->uid) &&
2439 !uid_eq(cred->euid, tcred->suid)) {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002440 ret = -EACCES;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002441 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002442 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002443 } else
2444 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002445
2446 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002447 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002448
2449 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002450 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002451 * trapped in a cpuset, or RT worker may be born in a cgroup
2452 * with no rt_runtime allocated. Just say no.
2453 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002454 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002455 ret = -EINVAL;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002456 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002457 }
2458
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002459 get_task_struct(tsk);
2460 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002461
Li Zefan081aa452013-03-13 09:17:09 +08002462 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2463
Paul Menagebbcb81d2007-10-18 23:39:32 -07002464 put_task_struct(tsk);
Tejun Heob5ba75b2015-05-13 16:35:18 -04002465 goto out_unlock_threadgroup;
2466
2467out_unlock_rcu:
2468 rcu_read_unlock();
2469out_unlock_threadgroup:
2470 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002471 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002472 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002473}
2474
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002475/**
2476 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2477 * @from: attach to all cgroups of a given task
2478 * @tsk: the task to be attached
2479 */
2480int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2481{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002482 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002483 int retval = 0;
2484
Tejun Heo47cfcd02013-04-07 09:29:51 -07002485 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002486 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002487 struct cgroup *from_cgrp;
2488
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002489 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002490 continue;
2491
Tejun Heo96d365e2014-02-13 06:58:40 -05002492 down_read(&css_set_rwsem);
2493 from_cgrp = task_cgroup_from_root(from, root);
2494 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002495
Li Zefan6f4b7e62013-07-31 16:18:36 +08002496 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002497 if (retval)
2498 break;
2499 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002500 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002501
2502 return retval;
2503}
2504EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2505
Tejun Heoacbef752014-05-13 12:16:22 -04002506static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2507 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002508{
Tejun Heoacbef752014-05-13 12:16:22 -04002509 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002510}
2511
Tejun Heoacbef752014-05-13 12:16:22 -04002512static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2513 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002514{
Tejun Heoacbef752014-05-13 12:16:22 -04002515 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002516}
2517
Tejun Heo451af5042014-05-13 12:16:21 -04002518static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2519 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e0662008-07-25 01:46:59 -07002520{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002521 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002522
Tejun Heoe76ecae2014-05-13 12:19:23 -04002523 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2524
2525 cgrp = cgroup_kn_lock_live(of->kn);
2526 if (!cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07002527 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002528 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002529 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2530 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002531 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002532 cgroup_kn_unlock(of->kn);
Tejun Heo451af5042014-05-13 12:16:21 -04002533 return nbytes;
Paul Menagee788e0662008-07-25 01:46:59 -07002534}
2535
Tejun Heo2da8ca82013-12-05 12:28:04 -05002536static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002537{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002538 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002539
Tejun Heo46cfeb02014-05-13 12:11:00 -04002540 spin_lock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002541 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002542 spin_unlock(&release_agent_path_lock);
Paul Menagee788e0662008-07-25 01:46:59 -07002543 seq_putc(seq, '\n');
Paul Menagee788e0662008-07-25 01:46:59 -07002544 return 0;
2545}
2546
Tejun Heo2da8ca82013-12-05 12:28:04 -05002547static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002548{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002549 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002550 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002551}
2552
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002553static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002554{
2555 struct cgroup_subsys *ss;
2556 bool printed = false;
2557 int ssid;
2558
2559 for_each_subsys(ss, ssid) {
2560 if (ss_mask & (1 << ssid)) {
2561 if (printed)
2562 seq_putc(seq, ' ');
2563 seq_printf(seq, "%s", ss->name);
2564 printed = true;
2565 }
2566 }
2567 if (printed)
2568 seq_putc(seq, '\n');
2569}
2570
2571/* show controllers which are currently attached to the default hierarchy */
2572static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2573{
2574 struct cgroup *cgrp = seq_css(seq)->cgroup;
2575
Tejun Heo5533e012014-05-14 19:33:07 -04002576 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2577 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002578 return 0;
2579}
2580
2581/* show controllers which are enabled from the parent */
2582static int cgroup_controllers_show(struct seq_file *seq, void *v)
2583{
2584 struct cgroup *cgrp = seq_css(seq)->cgroup;
2585
Tejun Heo667c2492014-07-08 18:02:56 -04002586 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002587 return 0;
2588}
2589
2590/* show controllers which are enabled for a given cgroup's children */
2591static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2592{
2593 struct cgroup *cgrp = seq_css(seq)->cgroup;
2594
Tejun Heo667c2492014-07-08 18:02:56 -04002595 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002596 return 0;
2597}
2598
2599/**
2600 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2601 * @cgrp: root of the subtree to update csses for
2602 *
2603 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2604 * css associations need to be updated accordingly. This function looks up
2605 * all css_sets which are attached to the subtree, creates the matching
2606 * updated css_sets and migrates the tasks to the new ones.
2607 */
2608static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2609{
2610 LIST_HEAD(preloaded_csets);
2611 struct cgroup_subsys_state *css;
2612 struct css_set *src_cset;
2613 int ret;
2614
Tejun Heof8f22e52014-04-23 11:13:16 -04002615 lockdep_assert_held(&cgroup_mutex);
2616
Tejun Heob5ba75b2015-05-13 16:35:18 -04002617 percpu_down_write(&cgroup_threadgroup_rwsem);
2618
Tejun Heof8f22e52014-04-23 11:13:16 -04002619 /* look up all csses currently attached to @cgrp's subtree */
2620 down_read(&css_set_rwsem);
2621 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2622 struct cgrp_cset_link *link;
2623
2624 /* self is not affected by child_subsys_mask change */
2625 if (css->cgroup == cgrp)
2626 continue;
2627
2628 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2629 cgroup_migrate_add_src(link->cset, cgrp,
2630 &preloaded_csets);
2631 }
2632 up_read(&css_set_rwsem);
2633
2634 /* NULL dst indicates self on default hierarchy */
2635 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2636 if (ret)
2637 goto out_finish;
2638
2639 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2640 struct task_struct *last_task = NULL, *task;
2641
2642 /* src_csets precede dst_csets, break on the first dst_cset */
2643 if (!src_cset->mg_src_cgrp)
2644 break;
2645
2646 /*
2647 * All tasks in src_cset need to be migrated to the
2648 * matching dst_cset. Empty it process by process. We
2649 * walk tasks but migrate processes. The leader might even
2650 * belong to a different cset but such src_cset would also
2651 * be among the target src_csets because the default
2652 * hierarchy enforces per-process membership.
2653 */
2654 while (true) {
2655 down_read(&css_set_rwsem);
2656 task = list_first_entry_or_null(&src_cset->tasks,
2657 struct task_struct, cg_list);
2658 if (task) {
2659 task = task->group_leader;
2660 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2661 get_task_struct(task);
2662 }
2663 up_read(&css_set_rwsem);
2664
2665 if (!task)
2666 break;
2667
2668 /* guard against possible infinite loop */
2669 if (WARN(last_task == task,
2670 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2671 goto out_finish;
2672 last_task = task;
2673
Tejun Heof8f22e52014-04-23 11:13:16 -04002674 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2675
Tejun Heof8f22e52014-04-23 11:13:16 -04002676 put_task_struct(task);
2677
2678 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2679 goto out_finish;
2680 }
2681 }
2682
2683out_finish:
2684 cgroup_migrate_finish(&preloaded_csets);
Tejun Heob5ba75b2015-05-13 16:35:18 -04002685 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002686 return ret;
2687}
2688
2689/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af5042014-05-13 12:16:21 -04002690static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2691 char *buf, size_t nbytes,
2692 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002693{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002694 unsigned long enable = 0, disable = 0;
2695 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002696 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002697 struct cgroup_subsys *ss;
Tejun Heo451af5042014-05-13 12:16:21 -04002698 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002699 int ssid, ret;
2700
2701 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002702 * Parse input - space separated list of subsystem names prefixed
2703 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002704 */
Tejun Heo451af5042014-05-13 12:16:21 -04002705 buf = strstrip(buf);
2706 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04002707 if (tok[0] == '\0')
2708 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002709 for_each_subsys(ss, ssid) {
Tejun Heo5533e012014-05-14 19:33:07 -04002710 if (ss->disabled || strcmp(tok + 1, ss->name) ||
2711 ((1 << ss->id) & cgrp_dfl_root_inhibit_ss_mask))
Tejun Heof8f22e52014-04-23 11:13:16 -04002712 continue;
2713
2714 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002715 enable |= 1 << ssid;
2716 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002717 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002718 disable |= 1 << ssid;
2719 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002720 } else {
2721 return -EINVAL;
2722 }
2723 break;
2724 }
2725 if (ssid == CGROUP_SUBSYS_COUNT)
2726 return -EINVAL;
2727 }
2728
Tejun Heoa9746d82014-05-13 12:19:22 -04002729 cgrp = cgroup_kn_lock_live(of->kn);
2730 if (!cgrp)
2731 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002732
2733 for_each_subsys(ss, ssid) {
2734 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002735 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002736 enable &= ~(1 << ssid);
2737 continue;
2738 }
2739
Tejun Heoc29adf22014-07-08 18:02:56 -04002740 /* unavailable or not enabled on the parent? */
2741 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2742 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002743 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002744 ret = -ENOENT;
2745 goto out_unlock;
2746 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002747 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002748 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002749 disable &= ~(1 << ssid);
2750 continue;
2751 }
2752
2753 /* a child has it enabled? */
2754 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002755 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002756 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002757 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002758 }
2759 }
2760 }
2761 }
2762
2763 if (!enable && !disable) {
2764 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002765 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002766 }
2767
2768 /*
Tejun Heo667c2492014-07-08 18:02:56 -04002769 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002770 * with tasks so that child cgroups don't compete against tasks.
2771 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002772 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002773 ret = -EBUSY;
2774 goto out_unlock;
2775 }
2776
2777 /*
Tejun Heof63070d2014-07-08 18:02:57 -04002778 * Update subsys masks and calculate what needs to be done. More
2779 * subsystems than specified may need to be enabled or disabled
2780 * depending on subsystem dependencies.
2781 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05002782 old_sc = cgrp->subtree_control;
2783 old_ss = cgrp->child_subsys_mask;
2784 new_sc = (old_sc | enable) & ~disable;
2785 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04002786
Tejun Heo755bf5e2014-11-18 02:49:50 -05002787 css_enable = ~old_ss & new_ss;
2788 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04002789 enable |= css_enable;
2790 disable |= css_disable;
2791
Tejun Heodb6e3052014-11-18 02:49:51 -05002792 /*
2793 * Because css offlining is asynchronous, userland might try to
2794 * re-enable the same controller while the previous instance is
2795 * still around. In such cases, wait till it's gone using
2796 * offline_waitq.
2797 */
2798 for_each_subsys(ss, ssid) {
2799 if (!(css_enable & (1 << ssid)))
2800 continue;
2801
2802 cgroup_for_each_live_child(child, cgrp) {
2803 DEFINE_WAIT(wait);
2804
2805 if (!cgroup_css(child, ss))
2806 continue;
2807
2808 cgroup_get(child);
2809 prepare_to_wait(&child->offline_waitq, &wait,
2810 TASK_UNINTERRUPTIBLE);
2811 cgroup_kn_unlock(of->kn);
2812 schedule();
2813 finish_wait(&child->offline_waitq, &wait);
2814 cgroup_put(child);
2815
2816 return restart_syscall();
2817 }
2818 }
2819
Tejun Heo755bf5e2014-11-18 02:49:50 -05002820 cgrp->subtree_control = new_sc;
2821 cgrp->child_subsys_mask = new_ss;
2822
Tejun Heof63070d2014-07-08 18:02:57 -04002823 /*
2824 * Create new csses or make the existing ones visible. A css is
2825 * created invisible if it's being implicitly enabled through
2826 * dependency. An invisible css is made visible when the userland
2827 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04002828 */
2829 for_each_subsys(ss, ssid) {
2830 if (!(enable & (1 << ssid)))
2831 continue;
2832
2833 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04002834 if (css_enable & (1 << ssid))
2835 ret = create_css(child, ss,
2836 cgrp->subtree_control & (1 << ssid));
2837 else
2838 ret = cgroup_populate_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002839 if (ret)
2840 goto err_undo_css;
2841 }
2842 }
2843
Tejun Heoc29adf22014-07-08 18:02:56 -04002844 /*
2845 * At this point, cgroup_e_css() results reflect the new csses
2846 * making the following cgroup_update_dfl_csses() properly update
2847 * css associations of all tasks in the subtree.
2848 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002849 ret = cgroup_update_dfl_csses(cgrp);
2850 if (ret)
2851 goto err_undo_css;
2852
Tejun Heof63070d2014-07-08 18:02:57 -04002853 /*
2854 * All tasks are migrated out of disabled csses. Kill or hide
2855 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002856 * disabled while other subsystems are still depending on it. The
2857 * css must not actively control resources and be in the vanilla
2858 * state if it's made visible again later. Controllers which may
2859 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04002860 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002861 for_each_subsys(ss, ssid) {
2862 if (!(disable & (1 << ssid)))
2863 continue;
2864
Tejun Heof63070d2014-07-08 18:02:57 -04002865 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002866 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2867
2868 if (css_disable & (1 << ssid)) {
2869 kill_css(css);
2870 } else {
Tejun Heof63070d2014-07-08 18:02:57 -04002871 cgroup_clear_dir(child, 1 << ssid);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002872 if (ss->css_reset)
2873 ss->css_reset(css);
2874 }
Tejun Heof63070d2014-07-08 18:02:57 -04002875 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002876 }
2877
Tejun Heo56c807b2014-11-18 02:49:51 -05002878 /*
2879 * The effective csses of all the descendants (excluding @cgrp) may
2880 * have changed. Subsystems can optionally subscribe to this event
2881 * by implementing ->css_e_css_changed() which is invoked if any of
2882 * the effective csses seen from the css's cgroup may have changed.
2883 */
2884 for_each_subsys(ss, ssid) {
2885 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
2886 struct cgroup_subsys_state *css;
2887
2888 if (!ss->css_e_css_changed || !this_css)
2889 continue;
2890
2891 css_for_each_descendant_pre(css, this_css)
2892 if (css != this_css)
2893 ss->css_e_css_changed(css);
2894 }
2895
Tejun Heof8f22e52014-04-23 11:13:16 -04002896 kernfs_activate(cgrp->kn);
2897 ret = 0;
2898out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002899 cgroup_kn_unlock(of->kn);
Tejun Heo451af5042014-05-13 12:16:21 -04002900 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002901
2902err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05002903 cgrp->subtree_control = old_sc;
2904 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04002905
2906 for_each_subsys(ss, ssid) {
2907 if (!(enable & (1 << ssid)))
2908 continue;
2909
2910 cgroup_for_each_live_child(child, cgrp) {
2911 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04002912
2913 if (!css)
2914 continue;
2915
2916 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04002917 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04002918 else
2919 cgroup_clear_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002920 }
2921 }
2922 goto out_unlock;
2923}
2924
Tejun Heo842b5972014-04-25 18:28:02 -04002925static int cgroup_populated_show(struct seq_file *seq, void *v)
2926{
2927 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2928 return 0;
2929}
2930
Tejun Heo2bd59d42014-02-11 11:52:49 -05002931static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2932 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002933{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002934 struct cgroup *cgrp = of->kn->parent->priv;
2935 struct cftype *cft = of->kn->priv;
2936 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002937 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002938
Tejun Heob4168642014-05-13 12:16:21 -04002939 if (cft->write)
2940 return cft->write(of, buf, nbytes, off);
2941
Tejun Heo2bd59d42014-02-11 11:52:49 -05002942 /*
2943 * kernfs guarantees that a file isn't deleted with operations in
2944 * flight, which means that the matching css is and stays alive and
2945 * doesn't need to be pinned. The RCU locking is not necessary
2946 * either. It's just for the convenience of using cgroup_css().
2947 */
2948 rcu_read_lock();
2949 css = cgroup_css(cgrp, cft->ss);
2950 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002951
Tejun Heo451af5042014-05-13 12:16:21 -04002952 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002953 unsigned long long v;
2954 ret = kstrtoull(buf, 0, &v);
2955 if (!ret)
2956 ret = cft->write_u64(css, cft, v);
2957 } else if (cft->write_s64) {
2958 long long v;
2959 ret = kstrtoll(buf, 0, &v);
2960 if (!ret)
2961 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002962 } else {
2963 ret = -EINVAL;
2964 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002965
Tejun Heoa742c592013-12-05 12:28:03 -05002966 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002967}
2968
Tejun Heo6612f052013-12-05 12:28:04 -05002969static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002970{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002971 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002972}
2973
2974static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2975{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002976 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002977}
2978
2979static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2980{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002981 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002982}
2983
2984static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2985{
Tejun Heo7da11272013-12-05 12:28:04 -05002986 struct cftype *cft = seq_cft(m);
2987 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002988
Tejun Heo2da8ca82013-12-05 12:28:04 -05002989 if (cft->seq_show)
2990 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002991
Tejun Heo896f5192013-12-05 12:28:04 -05002992 if (cft->read_u64)
2993 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2994 else if (cft->read_s64)
2995 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2996 else
2997 return -EINVAL;
2998 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002999}
3000
Tejun Heo2bd59d42014-02-11 11:52:49 -05003001static struct kernfs_ops cgroup_kf_single_ops = {
3002 .atomic_write_len = PAGE_SIZE,
3003 .write = cgroup_file_write,
3004 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003005};
3006
Tejun Heo2bd59d42014-02-11 11:52:49 -05003007static struct kernfs_ops cgroup_kf_ops = {
3008 .atomic_write_len = PAGE_SIZE,
3009 .write = cgroup_file_write,
3010 .seq_start = cgroup_seqfile_start,
3011 .seq_next = cgroup_seqfile_next,
3012 .seq_stop = cgroup_seqfile_stop,
3013 .seq_show = cgroup_seqfile_show,
3014};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003015
3016/*
3017 * cgroup_rename - Only allow simple rename of directories in place.
3018 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003019static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3020 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003021{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003022 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003023 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003024
Tejun Heo2bd59d42014-02-11 11:52:49 -05003025 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003026 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003027 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003028 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003029
Tejun Heo6db8e852013-06-14 11:18:22 -07003030 /*
3031 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003032 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003033 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003034 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003035 return -EPERM;
3036
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003037 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003038 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003039 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003040 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003041 */
3042 kernfs_break_active_protection(new_parent);
3043 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003044
Tejun Heo2bd59d42014-02-11 11:52:49 -05003045 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003046
Tejun Heo2bd59d42014-02-11 11:52:49 -05003047 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003048
Tejun Heo2bd59d42014-02-11 11:52:49 -05003049 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003050
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003051 kernfs_unbreak_active_protection(kn);
3052 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003053 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003054}
3055
Tejun Heo49957f82014-04-07 16:44:47 -04003056/* set uid and gid of cgroup dirs and files to that of the creator */
3057static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3058{
3059 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3060 .ia_uid = current_fsuid(),
3061 .ia_gid = current_fsgid(), };
3062
3063 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3064 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3065 return 0;
3066
3067 return kernfs_setattr(kn, &iattr);
3068}
3069
Tejun Heo2bb566c2013-08-08 20:11:23 -04003070static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003071{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003072 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003073 struct kernfs_node *kn;
3074 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003075 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003076
Tejun Heo2bd59d42014-02-11 11:52:49 -05003077#ifdef CONFIG_DEBUG_LOCK_ALLOC
3078 key = &cft->lockdep_key;
3079#endif
3080 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3081 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003082 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003083 if (IS_ERR(kn))
3084 return PTR_ERR(kn);
3085
3086 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003087 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003088 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003089 return ret;
3090 }
3091
Tejun Heob7fc5ad2014-05-13 12:16:22 -04003092 if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04003093 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04003094 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003095}
3096
Tejun Heob1f28d32013-06-28 16:24:10 -07003097/**
3098 * cgroup_addrm_files - add or remove files to a cgroup directory
3099 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07003100 * @cfts: array of cftypes to be added
3101 * @is_add: whether to add or remove
3102 *
3103 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04003104 * For removals, this function never fails. If addition fails, this
3105 * function doesn't remove files already added. The caller is responsible
3106 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07003107 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04003108static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
3109 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003110{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003111 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07003112 int ret;
3113
Tejun Heo01f64742014-05-13 12:19:23 -04003114 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003115
3116 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003117 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003118 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003119 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003120 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003121 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003122 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003123 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003124 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003125 continue;
3126
Li Zefan2739d3c2013-01-21 18:18:33 +08003127 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003128 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003129 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003130 pr_warn("%s: failed to add %s, err=%d\n",
3131 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07003132 return ret;
3133 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003134 } else {
3135 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003136 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003137 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003138 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003139}
3140
Tejun Heo21a2d342014-02-12 09:29:49 -05003141static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003142{
3143 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003144 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003145 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003146 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003147 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003148
Tejun Heo01f64742014-05-13 12:19:23 -04003149 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003150
Li Zefane8c82d22013-06-18 18:48:37 +08003151 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003152 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003153 struct cgroup *cgrp = css->cgroup;
3154
Li Zefane8c82d22013-06-18 18:48:37 +08003155 if (cgroup_is_dead(cgrp))
3156 continue;
3157
Tejun Heo21a2d342014-02-12 09:29:49 -05003158 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003159 if (ret)
3160 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003161 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003162
3163 if (is_add && !ret)
3164 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003165 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003166}
3167
Tejun Heo2da440a2014-02-11 11:52:48 -05003168static void cgroup_exit_cftypes(struct cftype *cfts)
3169{
3170 struct cftype *cft;
3171
Tejun Heo2bd59d42014-02-11 11:52:49 -05003172 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3173 /* free copy for custom atomic_write_len, see init_cftypes() */
3174 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3175 kfree(cft->kf_ops);
3176 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003177 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003178
3179 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003180 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003181 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003182}
3183
Tejun Heo2bd59d42014-02-11 11:52:49 -05003184static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003185{
3186 struct cftype *cft;
3187
Tejun Heo2bd59d42014-02-11 11:52:49 -05003188 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3189 struct kernfs_ops *kf_ops;
3190
Tejun Heo0adb0702014-02-12 09:29:48 -05003191 WARN_ON(cft->ss || cft->kf_ops);
3192
Tejun Heo2bd59d42014-02-11 11:52:49 -05003193 if (cft->seq_start)
3194 kf_ops = &cgroup_kf_ops;
3195 else
3196 kf_ops = &cgroup_kf_single_ops;
3197
3198 /*
3199 * Ugh... if @cft wants a custom max_write_len, we need to
3200 * make a copy of kf_ops to set its atomic_write_len.
3201 */
3202 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3203 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3204 if (!kf_ops) {
3205 cgroup_exit_cftypes(cfts);
3206 return -ENOMEM;
3207 }
3208 kf_ops->atomic_write_len = cft->max_write_len;
3209 }
3210
3211 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003212 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003213 }
3214
3215 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003216}
3217
Tejun Heo21a2d342014-02-12 09:29:49 -05003218static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3219{
Tejun Heo01f64742014-05-13 12:19:23 -04003220 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003221
3222 if (!cfts || !cfts[0].ss)
3223 return -ENOENT;
3224
3225 list_del(&cfts->node);
3226 cgroup_apply_cftypes(cfts, false);
3227 cgroup_exit_cftypes(cfts);
3228 return 0;
3229}
3230
Tejun Heo8e3f6542012-04-01 12:09:55 -07003231/**
Tejun Heo80b13582014-02-12 09:29:48 -05003232 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3233 * @cfts: zero-length name terminated array of cftypes
3234 *
3235 * Unregister @cfts. Files described by @cfts are removed from all
3236 * existing cgroups and all future cgroups won't have them either. This
3237 * function can be called anytime whether @cfts' subsys is attached or not.
3238 *
3239 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3240 * registered.
3241 */
3242int cgroup_rm_cftypes(struct cftype *cfts)
3243{
Tejun Heo21a2d342014-02-12 09:29:49 -05003244 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003245
Tejun Heo01f64742014-05-13 12:19:23 -04003246 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003247 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003248 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003249 return ret;
3250}
3251
3252/**
3253 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3254 * @ss: target cgroup subsystem
3255 * @cfts: zero-length name terminated array of cftypes
3256 *
3257 * Register @cfts to @ss. Files described by @cfts are created for all
3258 * existing cgroups to which @ss is attached and all future cgroups will
3259 * have them too. This function can be called anytime whether @ss is
3260 * attached or not.
3261 *
3262 * Returns 0 on successful registration, -errno on failure. Note that this
3263 * function currently returns 0 as long as @cfts registration is successful
3264 * even if some file creation attempts on existing cgroups fail.
3265 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003266static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003267{
Tejun Heo9ccece82013-06-28 16:24:11 -07003268 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003269
Li Zefanc731ae12014-06-05 17:16:30 +08003270 if (ss->disabled)
3271 return 0;
3272
Li Zefandc5736e2014-02-17 10:41:50 +08003273 if (!cfts || cfts[0].name[0] == '\0')
3274 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003275
Tejun Heo2bd59d42014-02-11 11:52:49 -05003276 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003277 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003278 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003279
Tejun Heo01f64742014-05-13 12:19:23 -04003280 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003281
Tejun Heo0adb0702014-02-12 09:29:48 -05003282 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003283 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003284 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003285 cgroup_rm_cftypes_locked(cfts);
3286
Tejun Heo01f64742014-05-13 12:19:23 -04003287 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003288 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003289}
Tejun Heo79578622012-04-01 12:09:56 -07003290
3291/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003292 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3293 * @ss: target cgroup subsystem
3294 * @cfts: zero-length name terminated array of cftypes
3295 *
3296 * Similar to cgroup_add_cftypes() but the added files are only used for
3297 * the default hierarchy.
3298 */
3299int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3300{
3301 struct cftype *cft;
3302
3303 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003304 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003305 return cgroup_add_cftypes(ss, cfts);
3306}
3307
3308/**
3309 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3310 * @ss: target cgroup subsystem
3311 * @cfts: zero-length name terminated array of cftypes
3312 *
3313 * Similar to cgroup_add_cftypes() but the added files are only used for
3314 * the legacy hierarchies.
3315 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003316int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3317{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003318 struct cftype *cft;
3319
Vivek Goyalfa8137b2014-08-08 11:44:03 -04003320 /*
3321 * If legacy_flies_on_dfl, we want to show the legacy files on the
3322 * dfl hierarchy but iff the target subsystem hasn't been updated
3323 * for the dfl hierarchy yet.
3324 */
3325 if (!cgroup_legacy_files_on_dfl ||
3326 ss->dfl_cftypes != ss->legacy_cftypes) {
3327 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3328 cft->flags |= __CFTYPE_NOT_ON_DFL;
3329 }
3330
Tejun Heo2cf669a2014-07-15 11:05:09 -04003331 return cgroup_add_cftypes(ss, cfts);
3332}
3333
Li Zefana043e3b2008-02-23 15:24:09 -08003334/**
3335 * cgroup_task_count - count the number of tasks in a cgroup.
3336 * @cgrp: the cgroup in question
3337 *
3338 * Return the number of tasks in the cgroup.
3339 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003340static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003341{
3342 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003343 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003344
Tejun Heo96d365e2014-02-13 06:58:40 -05003345 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003346 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3347 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003348 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003349 return count;
3350}
3351
Tejun Heo574bd9f2012-11-09 09:12:29 -08003352/**
Tejun Heo492eb212013-08-08 20:11:25 -04003353 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003354 * @pos: the current position (%NULL to initiate traversal)
3355 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003356 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003357 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f12013-12-06 15:11:55 -05003358 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003359 * that @parent and @pos are accessible. The next sibling is guaranteed to
3360 * be returned regardless of their states.
3361 *
3362 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3363 * css which finished ->css_online() is guaranteed to be visible in the
3364 * future iterations and will stay visible until the last reference is put.
3365 * A css which hasn't finished ->css_online() or already finished
3366 * ->css_offline() may show up during traversal. It's each subsystem's
3367 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003368 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003369struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3370 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003371{
Tejun Heoc2931b72014-05-16 13:22:51 -04003372 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003373
Tejun Heo8353da12014-05-13 12:19:23 -04003374 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003375
3376 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003377 * @pos could already have been unlinked from the sibling list.
3378 * Once a cgroup is removed, its ->sibling.next is no longer
3379 * updated when its next sibling changes. CSS_RELEASED is set when
3380 * @pos is taken off list, at which time its next pointer is valid,
3381 * and, as releases are serialized, the one pointed to by the next
3382 * pointer is guaranteed to not have started release yet. This
3383 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3384 * critical section, the one pointed to by its next pointer is
3385 * guaranteed to not have finished its RCU grace period even if we
3386 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003387 *
Tejun Heode3f0342014-05-16 13:22:49 -04003388 * If @pos has CSS_RELEASED set, its next pointer can't be
3389 * dereferenced; however, as each css is given a monotonically
3390 * increasing unique serial number and always appended to the
3391 * sibling list, the next one can be found by walking the parent's
3392 * children until the first css with higher serial number than
3393 * @pos's. While this path can be slower, it happens iff iteration
3394 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003395 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003396 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003397 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3398 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3399 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003400 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003401 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003402 if (next->serial_nr > pos->serial_nr)
3403 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003404 }
3405
Tejun Heo3b281af2014-04-23 11:13:15 -04003406 /*
3407 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003408 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003409 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003410 if (&next->sibling != &parent->children)
3411 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003412 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003413}
Tejun Heo53fa5262013-05-24 10:55:38 +09003414
3415/**
Tejun Heo492eb212013-08-08 20:11:25 -04003416 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003417 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003418 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003419 *
Tejun Heo492eb212013-08-08 20:11:25 -04003420 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003421 * to visit for pre-order traversal of @root's descendants. @root is
3422 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003423 *
Tejun Heo87fb54f12013-12-06 15:11:55 -05003424 * While this function requires cgroup_mutex or RCU read locking, it
3425 * doesn't require the whole traversal to be contained in a single critical
3426 * section. This function will return the correct next descendant as long
3427 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003428 *
3429 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3430 * css which finished ->css_online() is guaranteed to be visible in the
3431 * future iterations and will stay visible until the last reference is put.
3432 * A css which hasn't finished ->css_online() or already finished
3433 * ->css_offline() may show up during traversal. It's each subsystem's
3434 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003435 */
Tejun Heo492eb212013-08-08 20:11:25 -04003436struct cgroup_subsys_state *
3437css_next_descendant_pre(struct cgroup_subsys_state *pos,
3438 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003439{
Tejun Heo492eb212013-08-08 20:11:25 -04003440 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003441
Tejun Heo8353da12014-05-13 12:19:23 -04003442 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003443
Tejun Heobd8815a2013-08-08 20:11:27 -04003444 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003445 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003446 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003447
3448 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003449 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003450 if (next)
3451 return next;
3452
3453 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003454 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003455 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003456 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003457 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003458 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003459 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003460
3461 return NULL;
3462}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003463
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003464/**
Tejun Heo492eb212013-08-08 20:11:25 -04003465 * css_rightmost_descendant - return the rightmost descendant of a css
3466 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003467 *
Tejun Heo492eb212013-08-08 20:11:25 -04003468 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3469 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003470 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003471 *
Tejun Heo87fb54f12013-12-06 15:11:55 -05003472 * While this function requires cgroup_mutex or RCU read locking, it
3473 * doesn't require the whole traversal to be contained in a single critical
3474 * section. This function will return the correct rightmost descendant as
3475 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003476 */
Tejun Heo492eb212013-08-08 20:11:25 -04003477struct cgroup_subsys_state *
3478css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003479{
Tejun Heo492eb212013-08-08 20:11:25 -04003480 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003481
Tejun Heo8353da12014-05-13 12:19:23 -04003482 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003483
3484 do {
3485 last = pos;
3486 /* ->prev isn't RCU safe, walk ->next till the end */
3487 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003488 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003489 pos = tmp;
3490 } while (pos);
3491
3492 return last;
3493}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003494
Tejun Heo492eb212013-08-08 20:11:25 -04003495static struct cgroup_subsys_state *
3496css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003497{
Tejun Heo492eb212013-08-08 20:11:25 -04003498 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003499
3500 do {
3501 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003502 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003503 } while (pos);
3504
3505 return last;
3506}
3507
3508/**
Tejun Heo492eb212013-08-08 20:11:25 -04003509 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003510 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003511 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003512 *
Tejun Heo492eb212013-08-08 20:11:25 -04003513 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003514 * to visit for post-order traversal of @root's descendants. @root is
3515 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003516 *
Tejun Heo87fb54f12013-12-06 15:11:55 -05003517 * While this function requires cgroup_mutex or RCU read locking, it
3518 * doesn't require the whole traversal to be contained in a single critical
3519 * section. This function will return the correct next descendant as long
3520 * as both @pos and @cgroup are accessible and @pos is a descendant of
3521 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003522 *
3523 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3524 * css which finished ->css_online() is guaranteed to be visible in the
3525 * future iterations and will stay visible until the last reference is put.
3526 * A css which hasn't finished ->css_online() or already finished
3527 * ->css_offline() may show up during traversal. It's each subsystem's
3528 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003529 */
Tejun Heo492eb212013-08-08 20:11:25 -04003530struct cgroup_subsys_state *
3531css_next_descendant_post(struct cgroup_subsys_state *pos,
3532 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003533{
Tejun Heo492eb212013-08-08 20:11:25 -04003534 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003535
Tejun Heo8353da12014-05-13 12:19:23 -04003536 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003537
Tejun Heo58b79a92013-09-06 15:31:08 -04003538 /* if first iteration, visit leftmost descendant which may be @root */
3539 if (!pos)
3540 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003541
Tejun Heobd8815a2013-08-08 20:11:27 -04003542 /* if we visited @root, we're done */
3543 if (pos == root)
3544 return NULL;
3545
Tejun Heo574bd9f2012-11-09 09:12:29 -08003546 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003547 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003548 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003549 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003550
3551 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003552 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003553}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003554
Tejun Heof3d46502014-05-16 13:22:52 -04003555/**
3556 * css_has_online_children - does a css have online children
3557 * @css: the target css
3558 *
3559 * Returns %true if @css has any online children; otherwise, %false. This
3560 * function can be called from any context but the caller is responsible
3561 * for synchronizing against on/offlining as necessary.
3562 */
3563bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003564{
Tejun Heof3d46502014-05-16 13:22:52 -04003565 struct cgroup_subsys_state *child;
3566 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003567
3568 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003569 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003570 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003571 ret = true;
3572 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003573 }
3574 }
3575 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003576 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003577}
3578
Tejun Heo0942eee2013-08-08 20:11:26 -04003579/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003580 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003581 * @it: the iterator to advance
3582 *
3583 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003584 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003585static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003586{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003587 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003588 struct cgrp_cset_link *link;
3589 struct css_set *cset;
3590
3591 /* Advance to the next non-empty css_set */
3592 do {
3593 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003594 if (l == it->cset_head) {
3595 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003596 return;
3597 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003598
3599 if (it->ss) {
3600 cset = container_of(l, struct css_set,
3601 e_cset_node[it->ss->id]);
3602 } else {
3603 link = list_entry(l, struct cgrp_cset_link, cset_link);
3604 cset = link->cset;
3605 }
Tejun Heoc7561122014-02-25 10:04:01 -05003606 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3607
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003608 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003609
3610 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003611 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003612 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003613 it->task_pos = cset->mg_tasks.next;
3614
3615 it->tasks_head = &cset->tasks;
3616 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003617}
3618
Tejun Heo0942eee2013-08-08 20:11:26 -04003619/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003620 * css_task_iter_start - initiate task iteration
3621 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003622 * @it: the task iterator to use
3623 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003624 * Initiate iteration through the tasks of @css. The caller can call
3625 * css_task_iter_next() to walk through the tasks until the function
3626 * returns NULL. On completion of iteration, css_task_iter_end() must be
3627 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003628 *
3629 * Note that this function acquires a lock which is released when the
3630 * iteration finishes. The caller can't sleep while iteration is in
3631 * progress.
3632 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003633void css_task_iter_start(struct cgroup_subsys_state *css,
3634 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003635 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003636{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003637 /* no one should try to iterate before mounting cgroups */
3638 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003639
Tejun Heo96d365e2014-02-13 06:58:40 -05003640 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003641
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003642 it->ss = css->ss;
3643
3644 if (it->ss)
3645 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3646 else
3647 it->cset_pos = &css->cgroup->cset_links;
3648
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003649 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003650
Tejun Heo72ec7022013-08-08 20:11:26 -04003651 css_advance_task_iter(it);
Paul Menagebd89aab2007-10-18 23:40:44 -07003652}
Paul Menage817929e2007-10-18 23:39:36 -07003653
Tejun Heo0942eee2013-08-08 20:11:26 -04003654/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003655 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003656 * @it: the task iterator being iterated
3657 *
3658 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003659 * initialized via css_task_iter_start(). Returns NULL when the iteration
3660 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003661 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003662struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003663{
3664 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003665 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003666
3667 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003668 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003669 return NULL;
3670 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003671
3672 /*
3673 * Advance iterator to find next entry. cset->tasks is consumed
3674 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3675 * next cset.
3676 */
Paul Menage817929e2007-10-18 23:39:36 -07003677 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003678
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003679 if (l == it->tasks_head)
3680 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003681
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003682 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003683 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003684 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003685 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003686
Paul Menage817929e2007-10-18 23:39:36 -07003687 return res;
3688}
3689
Tejun Heo0942eee2013-08-08 20:11:26 -04003690/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003691 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003692 * @it: the task iterator to finish
3693 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003694 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003695 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003696void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003697 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003698{
Tejun Heo96d365e2014-02-13 06:58:40 -05003699 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003700}
3701
3702/**
3703 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3704 * @to: cgroup to which the tasks will be moved
3705 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003706 *
3707 * Locking rules between cgroup_post_fork() and the migration path
3708 * guarantee that, if a task is forking while being migrated, the new child
3709 * is guaranteed to be either visible in the source cgroup after the
3710 * parent's migration is complete or put into the target cgroup. No task
3711 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003712 */
3713int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3714{
Tejun Heo952aaa12014-02-25 10:04:03 -05003715 LIST_HEAD(preloaded_csets);
3716 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003717 struct css_task_iter it;
3718 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003719 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003720
Tejun Heo952aaa12014-02-25 10:04:03 -05003721 mutex_lock(&cgroup_mutex);
3722
3723 /* all tasks in @from are being moved, all csets are source */
3724 down_read(&css_set_rwsem);
3725 list_for_each_entry(link, &from->cset_links, cset_link)
3726 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3727 up_read(&css_set_rwsem);
3728
3729 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3730 if (ret)
3731 goto out_err;
3732
3733 /*
3734 * Migrate tasks one-by-one until @form is empty. This fails iff
3735 * ->can_attach() fails.
3736 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003737 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003738 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003739 task = css_task_iter_next(&it);
3740 if (task)
3741 get_task_struct(task);
3742 css_task_iter_end(&it);
3743
3744 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003745 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003746 put_task_struct(task);
3747 }
3748 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003749out_err:
3750 cgroup_migrate_finish(&preloaded_csets);
3751 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003752 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003753}
3754
Paul Menage817929e2007-10-18 23:39:36 -07003755/*
Ben Blum102a7752009-09-23 15:56:26 -07003756 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003757 *
3758 * Reading this file can return large amounts of data if a cgroup has
3759 * *lots* of attached tasks. So it may need several calls to read(),
3760 * but we cannot guarantee that the information we produce is correct
3761 * unless we produce it entirely atomically.
3762 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003763 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003764
Li Zefan24528252012-01-20 11:58:43 +08003765/* which pidlist file are we talking about? */
3766enum cgroup_filetype {
3767 CGROUP_FILE_PROCS,
3768 CGROUP_FILE_TASKS,
3769};
3770
3771/*
3772 * A pidlist is a list of pids that virtually represents the contents of one
3773 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3774 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3775 * to the cgroup.
3776 */
3777struct cgroup_pidlist {
3778 /*
3779 * used to find which pidlist is wanted. doesn't change as long as
3780 * this particular list stays in the list.
3781 */
3782 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3783 /* array of xids */
3784 pid_t *list;
3785 /* how many elements the above list has */
3786 int length;
Li Zefan24528252012-01-20 11:58:43 +08003787 /* each of these stored in a list by its cgroup */
3788 struct list_head links;
3789 /* pointer to the cgroup we belong to, for list removal purposes */
3790 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003791 /* for delayed destruction */
3792 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003793};
3794
Paul Menagebbcb81d2007-10-18 23:39:32 -07003795/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003796 * The following two functions "fix" the issue where there are more pids
3797 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3798 * TODO: replace with a kernel-wide solution to this problem
3799 */
3800#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3801static void *pidlist_allocate(int count)
3802{
3803 if (PIDLIST_TOO_LARGE(count))
3804 return vmalloc(count * sizeof(pid_t));
3805 else
3806 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3807}
Tejun Heob1a21362013-11-29 10:42:58 -05003808
Ben Blumd1d9fd32009-09-23 15:56:28 -07003809static void pidlist_free(void *p)
3810{
Bandan Das58794512015-03-02 17:51:10 -05003811 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003812}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003813
3814/*
Tejun Heob1a21362013-11-29 10:42:58 -05003815 * Used to destroy all pidlists lingering waiting for destroy timer. None
3816 * should be left afterwards.
3817 */
3818static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3819{
3820 struct cgroup_pidlist *l, *tmp_l;
3821
3822 mutex_lock(&cgrp->pidlist_mutex);
3823 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3824 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3825 mutex_unlock(&cgrp->pidlist_mutex);
3826
3827 flush_workqueue(cgroup_pidlist_destroy_wq);
3828 BUG_ON(!list_empty(&cgrp->pidlists));
3829}
3830
3831static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3832{
3833 struct delayed_work *dwork = to_delayed_work(work);
3834 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3835 destroy_dwork);
3836 struct cgroup_pidlist *tofree = NULL;
3837
3838 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003839
3840 /*
Tejun Heo04502362013-11-29 10:42:59 -05003841 * Destroy iff we didn't get queued again. The state won't change
3842 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003843 */
Tejun Heo04502362013-11-29 10:42:59 -05003844 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003845 list_del(&l->links);
3846 pidlist_free(l->list);
3847 put_pid_ns(l->key.ns);
3848 tofree = l;
3849 }
3850
Tejun Heob1a21362013-11-29 10:42:58 -05003851 mutex_unlock(&l->owner->pidlist_mutex);
3852 kfree(tofree);
3853}
3854
3855/*
Ben Blum102a7752009-09-23 15:56:26 -07003856 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003857 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003858 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003859static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003860{
Ben Blum102a7752009-09-23 15:56:26 -07003861 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003862
3863 /*
3864 * we presume the 0th element is unique, so i starts at 1. trivial
3865 * edge cases first; no work needs to be done for either
3866 */
3867 if (length == 0 || length == 1)
3868 return length;
3869 /* src and dest walk down the list; dest counts unique elements */
3870 for (src = 1; src < length; src++) {
3871 /* find next unique element */
3872 while (list[src] == list[src-1]) {
3873 src++;
3874 if (src == length)
3875 goto after;
3876 }
3877 /* dest always points to where the next unique element goes */
3878 list[dest] = list[src];
3879 dest++;
3880 }
3881after:
Ben Blum102a7752009-09-23 15:56:26 -07003882 return dest;
3883}
3884
Tejun Heoafb2bc12013-11-29 10:42:59 -05003885/*
3886 * The two pid files - task and cgroup.procs - guaranteed that the result
3887 * is sorted, which forced this whole pidlist fiasco. As pid order is
3888 * different per namespace, each namespace needs differently sorted list,
3889 * making it impossible to use, for example, single rbtree of member tasks
3890 * sorted by task pointer. As pidlists can be fairly large, allocating one
3891 * per open file is dangerous, so cgroup had to implement shared pool of
3892 * pidlists keyed by cgroup and namespace.
3893 *
3894 * All this extra complexity was caused by the original implementation
3895 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04003896 * want to do away with it. Explicitly scramble sort order if on the
3897 * default hierarchy so that no such expectation exists in the new
3898 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05003899 *
3900 * Scrambling is done by swapping every two consecutive bits, which is
3901 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3902 */
3903static pid_t pid_fry(pid_t pid)
3904{
3905 unsigned a = pid & 0x55555555;
3906 unsigned b = pid & 0xAAAAAAAA;
3907
3908 return (a << 1) | (b >> 1);
3909}
3910
3911static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3912{
Tejun Heoaa6ec292014-07-09 10:08:08 -04003913 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05003914 return pid_fry(pid);
3915 else
3916 return pid;
3917}
3918
Ben Blum102a7752009-09-23 15:56:26 -07003919static int cmppid(const void *a, const void *b)
3920{
3921 return *(pid_t *)a - *(pid_t *)b;
3922}
3923
Tejun Heoafb2bc12013-11-29 10:42:59 -05003924static int fried_cmppid(const void *a, const void *b)
3925{
3926 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3927}
3928
Ben Blum72a8cb32009-09-23 15:56:27 -07003929static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3930 enum cgroup_filetype type)
3931{
3932 struct cgroup_pidlist *l;
3933 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003934 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003935
Tejun Heoe6b81712013-11-29 10:42:59 -05003936 lockdep_assert_held(&cgrp->pidlist_mutex);
3937
3938 list_for_each_entry(l, &cgrp->pidlists, links)
3939 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003940 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003941 return NULL;
3942}
3943
Ben Blum72a8cb32009-09-23 15:56:27 -07003944/*
3945 * find the appropriate pidlist for our purpose (given procs vs tasks)
3946 * returns with the lock on that pidlist already held, and takes care
3947 * of the use count, or returns NULL with no locks held if we're out of
3948 * memory.
3949 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003950static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3951 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003952{
3953 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003954
Tejun Heoe6b81712013-11-29 10:42:59 -05003955 lockdep_assert_held(&cgrp->pidlist_mutex);
3956
3957 l = cgroup_pidlist_find(cgrp, type);
3958 if (l)
3959 return l;
3960
Ben Blum72a8cb32009-09-23 15:56:27 -07003961 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003962 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003963 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003964 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003965
Tejun Heob1a21362013-11-29 10:42:58 -05003966 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003967 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003968 /* don't need task_nsproxy() if we're looking at ourself */
3969 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003970 l->owner = cgrp;
3971 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003972 return l;
3973}
3974
3975/*
Ben Blum102a7752009-09-23 15:56:26 -07003976 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3977 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003978static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3979 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003980{
3981 pid_t *array;
3982 int length;
3983 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003984 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003985 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003986 struct cgroup_pidlist *l;
3987
Tejun Heo4bac00d2013-11-29 10:42:59 -05003988 lockdep_assert_held(&cgrp->pidlist_mutex);
3989
Ben Blum102a7752009-09-23 15:56:26 -07003990 /*
3991 * If cgroup gets more users after we read count, we won't have
3992 * enough space - tough. This race is indistinguishable to the
3993 * caller from the case that the additional cgroup users didn't
3994 * show up until sometime later on.
3995 */
3996 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003997 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003998 if (!array)
3999 return -ENOMEM;
4000 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004001 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004002 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004003 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004004 break;
Ben Blum102a7752009-09-23 15:56:26 -07004005 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004006 if (type == CGROUP_FILE_PROCS)
4007 pid = task_tgid_vnr(tsk);
4008 else
4009 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004010 if (pid > 0) /* make sure to only use valid results */
4011 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004012 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004013 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004014 length = n;
4015 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004016 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004017 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4018 else
4019 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004020 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004021 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004022
Tejun Heoe6b81712013-11-29 10:42:59 -05004023 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004024 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004025 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004026 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004027 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004028
4029 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004030 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004031 l->list = array;
4032 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004033 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004034 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004035}
4036
Balbir Singh846c7bb2007-10-18 23:39:44 -07004037/**
Li Zefana043e3b2008-02-23 15:24:09 -08004038 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004039 * @stats: cgroupstats to fill information into
4040 * @dentry: A dentry entry belonging to the cgroup for which stats have
4041 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004042 *
4043 * Build and fill cgroupstats so that taskstats can export it to user
4044 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004045 */
4046int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4047{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004048 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004049 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004050 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004051 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004052
Tejun Heo2bd59d42014-02-11 11:52:49 -05004053 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4054 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4055 kernfs_type(kn) != KERNFS_DIR)
4056 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004057
Li Zefanbad34662014-02-14 16:54:28 +08004058 mutex_lock(&cgroup_mutex);
4059
Tejun Heo2bd59d42014-02-11 11:52:49 -05004060 /*
4061 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004062 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004063 * @kn->priv is RCU safe. Let's do the RCU dancing.
4064 */
4065 rcu_read_lock();
4066 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004067 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004068 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004069 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004070 return -ENOENT;
4071 }
Li Zefanbad34662014-02-14 16:54:28 +08004072 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004073
Tejun Heo9d800df2014-05-14 09:15:00 -04004074 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004075 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004076 switch (tsk->state) {
4077 case TASK_RUNNING:
4078 stats->nr_running++;
4079 break;
4080 case TASK_INTERRUPTIBLE:
4081 stats->nr_sleeping++;
4082 break;
4083 case TASK_UNINTERRUPTIBLE:
4084 stats->nr_uninterruptible++;
4085 break;
4086 case TASK_STOPPED:
4087 stats->nr_stopped++;
4088 break;
4089 default:
4090 if (delayacct_is_task_waiting_on_io(tsk))
4091 stats->nr_io_wait++;
4092 break;
4093 }
4094 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004095 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004096
Li Zefanbad34662014-02-14 16:54:28 +08004097 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004098 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004099}
4100
Paul Menage8f3ff202009-09-23 15:56:25 -07004101
Paul Menagecc31edc2008-10-18 20:28:04 -07004102/*
Ben Blum102a7752009-09-23 15:56:26 -07004103 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004104 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004105 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004106 */
4107
Ben Blum102a7752009-09-23 15:56:26 -07004108static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004109{
4110 /*
4111 * Initially we receive a position value that corresponds to
4112 * one more than the last pid shown (or 0 on the first call or
4113 * after a seek to the start). Use a binary-search to find the
4114 * next pid to display, if any
4115 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004116 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004117 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004118 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004119 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004120 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004121 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004122
Tejun Heo4bac00d2013-11-29 10:42:59 -05004123 mutex_lock(&cgrp->pidlist_mutex);
4124
4125 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004126 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004127 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004128 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004129 * could already have been destroyed.
4130 */
Tejun Heo5d224442013-12-05 12:28:04 -05004131 if (of->priv)
4132 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004133
4134 /*
4135 * Either this is the first start() after open or the matching
4136 * pidlist has been destroyed inbetween. Create a new one.
4137 */
Tejun Heo5d224442013-12-05 12:28:04 -05004138 if (!of->priv) {
4139 ret = pidlist_array_load(cgrp, type,
4140 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004141 if (ret)
4142 return ERR_PTR(ret);
4143 }
Tejun Heo5d224442013-12-05 12:28:04 -05004144 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004145
Paul Menagecc31edc2008-10-18 20:28:04 -07004146 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004147 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004148
Paul Menagecc31edc2008-10-18 20:28:04 -07004149 while (index < end) {
4150 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004151 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004152 index = mid;
4153 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004154 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004155 index = mid + 1;
4156 else
4157 end = mid;
4158 }
4159 }
4160 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004161 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004162 return NULL;
4163 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004164 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004165 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004166 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004167}
4168
Ben Blum102a7752009-09-23 15:56:26 -07004169static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004170{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004171 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004172 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004173
Tejun Heo5d224442013-12-05 12:28:04 -05004174 if (l)
4175 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004176 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004177 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004178}
4179
Ben Blum102a7752009-09-23 15:56:26 -07004180static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004181{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004182 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004183 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004184 pid_t *p = v;
4185 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004186 /*
4187 * Advance to the next pid in the array. If this goes off the
4188 * end, we're done
4189 */
4190 p++;
4191 if (p >= end) {
4192 return NULL;
4193 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004194 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004195 return p;
4196 }
4197}
4198
Ben Blum102a7752009-09-23 15:56:26 -07004199static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004200{
Joe Perches94ff2122015-04-15 16:18:20 -07004201 seq_printf(s, "%d\n", *(int *)v);
4202
4203 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004204}
4205
Tejun Heo182446d2013-08-08 20:11:24 -04004206static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4207 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004208{
Tejun Heo182446d2013-08-08 20:11:24 -04004209 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004210}
4211
Tejun Heo182446d2013-08-08 20:11:24 -04004212static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4213 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004214{
Paul Menage6379c102008-07-25 01:47:01 -07004215 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004216 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004217 else
Tejun Heo182446d2013-08-08 20:11:24 -04004218 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004219 return 0;
4220}
4221
Tejun Heo182446d2013-08-08 20:11:24 -04004222static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4223 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004224{
Tejun Heo182446d2013-08-08 20:11:24 -04004225 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004226}
4227
Tejun Heo182446d2013-08-08 20:11:24 -04004228static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4229 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004230{
4231 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004232 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004233 else
Tejun Heo182446d2013-08-08 20:11:24 -04004234 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004235 return 0;
4236}
4237
Tejun Heoa14c6872014-07-15 11:05:09 -04004238/* cgroup core interface files for the default hierarchy */
4239static struct cftype cgroup_dfl_base_files[] = {
4240 {
4241 .name = "cgroup.procs",
4242 .seq_start = cgroup_pidlist_start,
4243 .seq_next = cgroup_pidlist_next,
4244 .seq_stop = cgroup_pidlist_stop,
4245 .seq_show = cgroup_pidlist_show,
4246 .private = CGROUP_FILE_PROCS,
4247 .write = cgroup_procs_write,
4248 .mode = S_IRUGO | S_IWUSR,
4249 },
4250 {
4251 .name = "cgroup.controllers",
4252 .flags = CFTYPE_ONLY_ON_ROOT,
4253 .seq_show = cgroup_root_controllers_show,
4254 },
4255 {
4256 .name = "cgroup.controllers",
4257 .flags = CFTYPE_NOT_ON_ROOT,
4258 .seq_show = cgroup_controllers_show,
4259 },
4260 {
4261 .name = "cgroup.subtree_control",
4262 .seq_show = cgroup_subtree_control_show,
4263 .write = cgroup_subtree_control_write,
4264 },
4265 {
4266 .name = "cgroup.populated",
4267 .flags = CFTYPE_NOT_ON_ROOT,
4268 .seq_show = cgroup_populated_show,
4269 },
4270 { } /* terminate */
4271};
4272
4273/* cgroup core interface files for the legacy hierarchies */
4274static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004275 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004276 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004277 .seq_start = cgroup_pidlist_start,
4278 .seq_next = cgroup_pidlist_next,
4279 .seq_stop = cgroup_pidlist_stop,
4280 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004281 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004282 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004283 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004284 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004285 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004286 .name = "cgroup.clone_children",
4287 .read_u64 = cgroup_clone_children_read,
4288 .write_u64 = cgroup_clone_children_write,
4289 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004290 {
Tejun Heo873fe092013-04-14 20:15:26 -07004291 .name = "cgroup.sane_behavior",
4292 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004293 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004294 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004295 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004296 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004297 .seq_start = cgroup_pidlist_start,
4298 .seq_next = cgroup_pidlist_next,
4299 .seq_stop = cgroup_pidlist_stop,
4300 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004301 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004302 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004303 .mode = S_IRUGO | S_IWUSR,
4304 },
4305 {
4306 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004307 .read_u64 = cgroup_read_notify_on_release,
4308 .write_u64 = cgroup_write_notify_on_release,
4309 },
Tejun Heo873fe092013-04-14 20:15:26 -07004310 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004311 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004312 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004313 .seq_show = cgroup_release_agent_show,
Tejun Heo451af5042014-05-13 12:16:21 -04004314 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004315 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004316 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004317 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004318};
4319
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004320/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004321 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004322 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004323 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004324 *
4325 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004326 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10004327static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004330 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004331
Tejun Heo8e3f6542012-04-01 12:09:55 -07004332 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004333 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004334 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004335
Tejun Heo69dfa002014-05-04 15:09:13 -04004336 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004337 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004338
Tejun Heo0adb0702014-02-12 09:29:48 -05004339 list_for_each_entry(cfts, &ss->cfts, node) {
4340 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004341 if (ret < 0)
4342 goto err;
4343 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004346err:
4347 cgroup_clear_dir(cgrp, subsys_mask);
4348 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349}
4350
Tejun Heo0c21ead2013-08-13 20:22:51 -04004351/*
4352 * css destruction is four-stage process.
4353 *
4354 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4355 * Implemented in kill_css().
4356 *
4357 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004358 * and thus css_tryget_online() is guaranteed to fail, the css can be
4359 * offlined by invoking offline_css(). After offlining, the base ref is
4360 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004361 *
4362 * 3. When the percpu_ref reaches zero, the only possible remaining
4363 * accessors are inside RCU read sections. css_release() schedules the
4364 * RCU callback.
4365 *
4366 * 4. After the grace period, the css can be freed. Implemented in
4367 * css_free_work_fn().
4368 *
4369 * It is actually hairier because both step 2 and 4 require process context
4370 * and thus involve punting to css->destroy_work adding two additional
4371 * steps to the already complex sequence.
4372 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004373static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004374{
4375 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004376 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004377 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004378 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004379
Tejun Heo9a1049d2014-06-28 08:10:14 -04004380 percpu_ref_exit(&css->refcnt);
4381
Vladimir Davydov01e58652015-02-12 14:59:26 -08004382 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004383 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004384 int id = css->id;
4385
Tejun Heo9d755d32014-05-14 09:15:02 -04004386 if (css->parent)
4387 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004388
Vladimir Davydov01e58652015-02-12 14:59:26 -08004389 ss->css_free(css);
4390 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004391 cgroup_put(cgrp);
4392 } else {
4393 /* cgroup free path */
4394 atomic_dec(&cgrp->root->nr_cgrps);
4395 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004396 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004397
Tejun Heod51f39b2014-05-16 13:22:48 -04004398 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004399 /*
4400 * We get a ref to the parent, and put the ref when
4401 * this cgroup is being freed, so it's guaranteed
4402 * that the parent won't be destroyed before its
4403 * children.
4404 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004405 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004406 kernfs_put(cgrp->kn);
4407 kfree(cgrp);
4408 } else {
4409 /*
4410 * This is root cgroup's refcnt reaching zero,
4411 * which indicates that the root should be
4412 * released.
4413 */
4414 cgroup_destroy_root(cgrp->root);
4415 }
4416 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004417}
4418
4419static void css_free_rcu_fn(struct rcu_head *rcu_head)
4420{
4421 struct cgroup_subsys_state *css =
4422 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4423
Tejun Heo0c21ead2013-08-13 20:22:51 -04004424 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004425 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004426}
4427
Tejun Heo25e15d82014-05-14 09:15:02 -04004428static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004429{
4430 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004431 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004432 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004433 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004434
Tejun Heo1fed1b22014-05-16 13:22:49 -04004435 mutex_lock(&cgroup_mutex);
4436
Tejun Heode3f0342014-05-16 13:22:49 -04004437 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004438 list_del_rcu(&css->sibling);
4439
Tejun Heo9d755d32014-05-14 09:15:02 -04004440 if (ss) {
4441 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004442 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004443 if (ss->css_released)
4444 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004445 } else {
4446 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004447 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4448 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004449
4450 /*
4451 * There are two control paths which try to determine
4452 * cgroup from dentry without going through kernfs -
4453 * cgroupstats_build() and css_tryget_online_from_dir().
4454 * Those are supported by RCU protecting clearing of
4455 * cgrp->kn->priv backpointer.
4456 */
4457 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004458 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004459
Tejun Heo1fed1b22014-05-16 13:22:49 -04004460 mutex_unlock(&cgroup_mutex);
4461
Tejun Heo0c21ead2013-08-13 20:22:51 -04004462 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004463}
4464
Paul Menageddbcc7e2007-10-18 23:39:30 -07004465static void css_release(struct percpu_ref *ref)
4466{
4467 struct cgroup_subsys_state *css =
4468 container_of(ref, struct cgroup_subsys_state, refcnt);
4469
Tejun Heo25e15d82014-05-14 09:15:02 -04004470 INIT_WORK(&css->destroy_work, css_release_work_fn);
4471 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472}
4473
Tejun Heoddfcada2014-05-04 15:09:14 -04004474static void init_and_link_css(struct cgroup_subsys_state *css,
4475 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004476{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004477 lockdep_assert_held(&cgroup_mutex);
4478
Tejun Heoddfcada2014-05-04 15:09:14 -04004479 cgroup_get(cgrp);
4480
Tejun Heod5c419b2014-05-16 13:22:48 -04004481 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004482 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004483 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004484 INIT_LIST_HEAD(&css->sibling);
4485 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004486 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004487
Tejun Heod51f39b2014-05-16 13:22:48 -04004488 if (cgroup_parent(cgrp)) {
4489 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004490 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004491 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004492
Tejun Heoca8bdca2013-08-26 18:40:56 -04004493 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494}
4495
Li Zefan2a4ac632013-07-31 16:16:40 +08004496/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004497static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004498{
Tejun Heo623f9262013-08-13 11:01:55 -04004499 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004500 int ret = 0;
4501
Tejun Heoa31f2d32012-11-19 08:13:37 -08004502 lockdep_assert_held(&cgroup_mutex);
4503
Tejun Heo92fb9742012-11-19 08:13:38 -08004504 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004505 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004506 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004507 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004508 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004509 }
Tejun Heob1929db2012-11-19 08:13:38 -08004510 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004511}
4512
Li Zefan2a4ac632013-07-31 16:16:40 +08004513/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004514static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004515{
Tejun Heo623f9262013-08-13 11:01:55 -04004516 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004517
4518 lockdep_assert_held(&cgroup_mutex);
4519
4520 if (!(css->flags & CSS_ONLINE))
4521 return;
4522
Li Zefand7eeac12013-03-12 15:35:59 -07004523 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004524 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004525
Tejun Heoeb954192013-08-08 20:11:23 -04004526 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004527 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004528
4529 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004530}
4531
Tejun Heoc81c925a2013-12-06 15:11:56 -05004532/**
4533 * create_css - create a cgroup_subsys_state
4534 * @cgrp: the cgroup new css will be associated with
4535 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004536 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004537 *
4538 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004539 * css is online and installed in @cgrp with all interface files created if
4540 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004541 */
Tejun Heof63070d2014-07-08 18:02:57 -04004542static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4543 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004544{
Tejun Heod51f39b2014-05-16 13:22:48 -04004545 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004546 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004547 struct cgroup_subsys_state *css;
4548 int err;
4549
Tejun Heoc81c925a2013-12-06 15:11:56 -05004550 lockdep_assert_held(&cgroup_mutex);
4551
Tejun Heo1fed1b22014-05-16 13:22:49 -04004552 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004553 if (IS_ERR(css))
4554 return PTR_ERR(css);
4555
Tejun Heoddfcada2014-05-04 15:09:14 -04004556 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004557
Tejun Heo2aad2a82014-09-24 13:31:50 -04004558 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004559 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004560 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004561
Tejun Heo15a4c832014-05-04 15:09:14 -04004562 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4563 if (err < 0)
4564 goto err_free_percpu_ref;
4565 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004566
Tejun Heof63070d2014-07-08 18:02:57 -04004567 if (visible) {
4568 err = cgroup_populate_dir(cgrp, 1 << ss->id);
4569 if (err)
4570 goto err_free_id;
4571 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004572
4573 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004574 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004575 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004576
4577 err = online_css(css);
4578 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004579 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004580
Tejun Heoc81c925a2013-12-06 15:11:56 -05004581 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004582 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004583 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 -04004584 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004585 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004586 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004587 ss->warned_broken_hierarchy = true;
4588 }
4589
4590 return 0;
4591
Tejun Heo1fed1b22014-05-16 13:22:49 -04004592err_list_del:
4593 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004594 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004595err_free_id:
4596 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004597err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004598 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004599err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004600 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004601 return err;
4602}
4603
Tejun Heob3bfd982014-05-13 12:19:22 -04004604static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4605 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004606{
Tejun Heoa9746d82014-05-13 12:19:22 -04004607 struct cgroup *parent, *cgrp;
4608 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004610 struct kernfs_node *kn;
Tejun Heoa14c6872014-07-15 11:05:09 -04004611 struct cftype *base_files;
Tejun Heob3bfd982014-05-13 12:19:22 -04004612 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613
Alban Crequy71b1fb52014-08-18 12:20:20 +01004614 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4615 */
4616 if (strchr(name, '\n'))
4617 return -EINVAL;
4618
Tejun Heoa9746d82014-05-13 12:19:22 -04004619 parent = cgroup_kn_lock_live(parent_kn);
4620 if (!parent)
4621 return -ENODEV;
4622 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004623
Tejun Heo0a950f62012-11-19 09:02:12 -08004624 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004625 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004626 if (!cgrp) {
4627 ret = -ENOMEM;
4628 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004629 }
4630
Tejun Heo2aad2a82014-09-24 13:31:50 -04004631 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004632 if (ret)
4633 goto out_free_cgrp;
4634
Li Zefan0ab02ca2014-02-11 16:05:46 +08004635 /*
4636 * Temporarily set the pointer to NULL, so idr_find() won't return
4637 * a half-baked cgroup.
4638 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004639 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004640 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004641 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004642 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004643 }
4644
Paul Menagecc31edc2008-10-18 20:28:04 -07004645 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646
Tejun Heo9d800df2014-05-14 09:15:00 -04004647 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004648 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649
Li Zefanb6abdb02008-03-04 14:28:19 -08004650 if (notify_on_release(parent))
4651 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4652
Tejun Heo2260e7f2012-11-19 08:13:38 -08004653 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4654 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004655
Tejun Heo2bd59d42014-02-11 11:52:49 -05004656 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004657 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004658 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004659 ret = PTR_ERR(kn);
4660 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004661 }
4662 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004663
Tejun Heo6f305582014-02-12 09:29:50 -05004664 /*
4665 * This extra ref will be put in cgroup_free_fn() and guarantees
4666 * that @cgrp->kn is always accessible.
4667 */
4668 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669
Tejun Heo0cb51d72014-05-16 13:22:49 -04004670 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004671
Tejun Heo4e139af2012-11-19 08:13:36 -08004672 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004673 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004674 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004675 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004676
Tejun Heo0d802552013-12-06 15:11:56 -05004677 /*
4678 * @cgrp is now fully operational. If something fails after this
4679 * point, it'll be released via the normal destruction path.
4680 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004681 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004682
Tejun Heoba0f4d72014-05-13 12:19:22 -04004683 ret = cgroup_kn_set_ugid(kn);
4684 if (ret)
4685 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004686
Tejun Heoa14c6872014-07-15 11:05:09 -04004687 if (cgroup_on_dfl(cgrp))
4688 base_files = cgroup_dfl_base_files;
4689 else
4690 base_files = cgroup_legacy_base_files;
4691
4692 ret = cgroup_addrm_files(cgrp, base_files, true);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004693 if (ret)
4694 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004695
Tejun Heo9d403e92013-12-06 15:11:56 -05004696 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004697 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004698 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004699 ret = create_css(cgrp, ss,
4700 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004701 if (ret)
4702 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004703 }
Tejun Heoa8638032012-11-09 09:12:29 -08004704 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004705
Tejun Heobd53d612014-04-23 11:13:16 -04004706 /*
4707 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004708 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004709 */
Tejun Heo667c2492014-07-08 18:02:56 -04004710 if (!cgroup_on_dfl(cgrp)) {
4711 cgrp->subtree_control = parent->subtree_control;
4712 cgroup_refresh_child_subsys_mask(cgrp);
4713 }
Tejun Heof392e512014-04-23 11:13:14 -04004714
Tejun Heo2bd59d42014-02-11 11:52:49 -05004715 kernfs_activate(kn);
4716
Tejun Heoba0f4d72014-05-13 12:19:22 -04004717 ret = 0;
4718 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004719
Tejun Heoba0f4d72014-05-13 12:19:22 -04004720out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004721 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004722out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004723 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004724out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004725 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004726out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004727 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004728 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004729
4730out_destroy:
4731 cgroup_destroy_locked(cgrp);
4732 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733}
4734
Tejun Heo223dbc32013-08-13 20:22:50 -04004735/*
4736 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004737 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4738 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004739 */
4740static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004741{
Tejun Heo223dbc32013-08-13 20:22:50 -04004742 struct cgroup_subsys_state *css =
4743 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004744
Tejun Heof20104d2013-08-13 20:22:50 -04004745 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004746 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004747 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004748
Tejun Heo09a503ea2013-08-13 20:22:50 -04004749 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004750}
4751
Tejun Heo223dbc32013-08-13 20:22:50 -04004752/* css kill confirmation processing requires process context, bounce */
4753static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004754{
4755 struct cgroup_subsys_state *css =
4756 container_of(ref, struct cgroup_subsys_state, refcnt);
4757
Tejun Heo223dbc32013-08-13 20:22:50 -04004758 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004759 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004760}
4761
Tejun Heof392e512014-04-23 11:13:14 -04004762/**
4763 * kill_css - destroy a css
4764 * @css: css to destroy
4765 *
4766 * This function initiates destruction of @css by removing cgroup interface
4767 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004768 * asynchronously once css_tryget_online() is guaranteed to fail and when
4769 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004770 */
4771static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004772{
Tejun Heo01f64742014-05-13 12:19:23 -04004773 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004774
Tejun Heo2bd59d42014-02-11 11:52:49 -05004775 /*
4776 * This must happen before css is disassociated with its cgroup.
4777 * See seq_css() for details.
4778 */
Tejun Heoaec25022014-02-08 10:36:58 -05004779 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004780
Tejun Heoedae0c32013-08-13 20:22:51 -04004781 /*
4782 * Killing would put the base ref, but we need to keep it alive
4783 * until after ->css_offline().
4784 */
4785 css_get(css);
4786
4787 /*
4788 * cgroup core guarantees that, by the time ->css_offline() is
4789 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004790 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004791 * proceed to offlining css's because percpu_ref_kill() doesn't
4792 * guarantee that the ref is seen as killed on all CPUs on return.
4793 *
4794 * Use percpu_ref_kill_and_confirm() to get notifications as each
4795 * css is confirmed to be seen as killed on all CPUs.
4796 */
4797 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004798}
4799
4800/**
4801 * cgroup_destroy_locked - the first stage of cgroup destruction
4802 * @cgrp: cgroup to be destroyed
4803 *
4804 * css's make use of percpu refcnts whose killing latency shouldn't be
4805 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004806 * guarantee that css_tryget_online() won't succeed by the time
4807 * ->css_offline() is invoked. To satisfy all the requirements,
4808 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004809 *
4810 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4811 * userland visible parts and start killing the percpu refcnts of
4812 * css's. Set up so that the next stage will be kicked off once all
4813 * the percpu refcnts are confirmed to be killed.
4814 *
4815 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4816 * rest of destruction. Once all cgroup references are gone, the
4817 * cgroup is RCU-freed.
4818 *
4819 * This function implements s1. After this step, @cgrp is gone as far as
4820 * the userland is concerned and a new cgroup with the same name may be
4821 * created. As cgroup doesn't care about the names internally, this
4822 * doesn't cause any problem.
4823 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004824static int cgroup_destroy_locked(struct cgroup *cgrp)
4825 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004826{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004827 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004828 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004829 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004830
Tejun Heo42809dd2012-11-19 08:13:37 -08004831 lockdep_assert_held(&cgroup_mutex);
4832
Tejun Heoddd69142013-06-12 21:04:54 -07004833 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004834 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004835 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004836 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004837 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004838 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004839 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004840 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004841 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004842
Tejun Heo1a90dd52012-11-05 09:16:59 -08004843 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004844 * Make sure there's no live children. We can't test emptiness of
4845 * ->self.children as dead children linger on it while being
4846 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004847 */
Tejun Heof3d46502014-05-16 13:22:52 -04004848 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004849 return -EBUSY;
4850
4851 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004852 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004853 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004854 */
Tejun Heo184faf32014-05-16 13:22:51 -04004855 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004856
Tejun Heo249f3462014-05-14 09:15:01 -04004857 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004858 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004859 kill_css(css);
4860
Tejun Heo455050d2013-06-13 19:27:41 -07004861 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004862 * Remove @cgrp directory along with the base files. @cgrp has an
4863 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07004864 */
Tejun Heo01f64742014-05-13 12:19:23 -04004865 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04004866
Tejun Heod51f39b2014-05-16 13:22:48 -04004867 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05004868
Tejun Heo249f3462014-05-14 09:15:01 -04004869 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004870 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07004871
Tejun Heoea15f8c2013-06-13 19:27:42 -07004872 return 0;
4873};
4874
Tejun Heo2bd59d42014-02-11 11:52:49 -05004875static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004876{
Tejun Heoa9746d82014-05-13 12:19:22 -04004877 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004878 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004879
Tejun Heoa9746d82014-05-13 12:19:22 -04004880 cgrp = cgroup_kn_lock_live(kn);
4881 if (!cgrp)
4882 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004883
Tejun Heoa9746d82014-05-13 12:19:22 -04004884 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004885
Tejun Heoa9746d82014-05-13 12:19:22 -04004886 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004887 return ret;
4888}
4889
Tejun Heo2bd59d42014-02-11 11:52:49 -05004890static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4891 .remount_fs = cgroup_remount,
4892 .show_options = cgroup_show_options,
4893 .mkdir = cgroup_mkdir,
4894 .rmdir = cgroup_rmdir,
4895 .rename = cgroup_rename,
4896};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004897
Tejun Heo15a4c832014-05-04 15:09:14 -04004898static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004900 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004901
4902 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004903
Tejun Heo648bb562012-11-19 08:13:36 -08004904 mutex_lock(&cgroup_mutex);
4905
Tejun Heo15a4c832014-05-04 15:09:14 -04004906 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004907 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004908
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004909 /* Create the root cgroup state for this subsystem */
4910 ss->root = &cgrp_dfl_root;
4911 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004912 /* We don't handle early failures gracefully */
4913 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004914 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004915
4916 /*
4917 * Root csses are never destroyed and we can't initialize
4918 * percpu_ref during early init. Disable refcnting.
4919 */
4920 css->flags |= CSS_NO_REF;
4921
Tejun Heo15a4c832014-05-04 15:09:14 -04004922 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004923 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004924 css->id = 1;
4925 } else {
4926 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4927 BUG_ON(css->id < 0);
4928 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004929
Li Zefane8d55fd2008-04-29 01:00:13 -07004930 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004931 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004932 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004933 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004934 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004935
Aleksa Saraicb4a3162015-06-06 10:02:14 +10004936 have_fork_callback |= (bool)ss->fork << ss->id;
4937 have_exit_callback |= (bool)ss->exit << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004938
Li Zefane8d55fd2008-04-29 01:00:13 -07004939 /* At system boot, before all subsystems have been
4940 * registered, no tasks have been forked, so we don't
4941 * need to invoke fork callbacks here. */
4942 BUG_ON(!list_empty(&init_task.tasks));
4943
Tejun Heoae7f1642013-08-13 20:22:50 -04004944 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004945
Tejun Heo648bb562012-11-19 08:13:36 -08004946 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004947}
4948
4949/**
Li Zefana043e3b2008-02-23 15:24:09 -08004950 * cgroup_init_early - cgroup initialization at system boot
4951 *
4952 * Initialize cgroups at system boot, and initialize any
4953 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004954 */
4955int __init cgroup_init_early(void)
4956{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04004957 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07004958 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004959 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004960
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004961 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004962 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4963
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004964 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004965
Tejun Heo3ed80a62014-02-08 10:36:58 -05004966 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004967 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004968 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4969 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004970 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004971 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4972 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004973
Tejun Heoaec25022014-02-08 10:36:58 -05004974 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004975 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004976
4977 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004978 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004979 }
4980 return 0;
4981}
4982
4983/**
Li Zefana043e3b2008-02-23 15:24:09 -08004984 * cgroup_init - cgroup initialization
4985 *
4986 * Register cgroup filesystem and /proc file, and initialize
4987 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004988 */
4989int __init cgroup_init(void)
4990{
Tejun Heo30159ec2013-06-25 11:53:37 -07004991 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004992 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004993 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004994
Tejun Heod59cfc02015-05-13 16:35:17 -04004995 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04004996 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
4997 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004998
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004999 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005000
Tejun Heo82fe9b02013-06-25 11:53:37 -07005001 /* Add init_css_set to the hash table */
5002 key = css_set_hash(init_css_set.subsys);
5003 hash_add(css_set_table, &init_css_set.hlist, key);
5004
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005005 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005006
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005007 mutex_unlock(&cgroup_mutex);
5008
Tejun Heo172a2c062014-03-19 10:23:53 -04005009 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005010 if (ss->early_init) {
5011 struct cgroup_subsys_state *css =
5012 init_css_set.subsys[ss->id];
5013
5014 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5015 GFP_KERNEL);
5016 BUG_ON(css->id < 0);
5017 } else {
5018 cgroup_init_subsys(ss, false);
5019 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005020
Tejun Heo2d8f2432014-04-23 11:13:15 -04005021 list_add_tail(&init_css_set.e_cset_node[ssid],
5022 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005023
5024 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005025 * Setting dfl_root subsys_mask needs to consider the
5026 * disabled flag and cftype registration needs kmalloc,
5027 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005028 */
Tejun Heoa8ddc822014-07-15 11:05:10 -04005029 if (ss->disabled)
5030 continue;
5031
5032 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5033
5034 if (cgroup_legacy_files_on_dfl && !ss->dfl_cftypes)
5035 ss->dfl_cftypes = ss->legacy_cftypes;
5036
Tejun Heo5de4fa12014-07-15 11:05:10 -04005037 if (!ss->dfl_cftypes)
5038 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5039
Tejun Heoa8ddc822014-07-15 11:05:10 -04005040 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5041 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5042 } else {
5043 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5044 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005045 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005046
5047 if (ss->bind)
5048 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005049 }
Greg KH676db4a2010-08-05 13:53:35 -07005050
Paul Menageddbcc7e2007-10-18 23:39:30 -07005051 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005052 if (!cgroup_kobj)
5053 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07005054
5055 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005056 if (err < 0) {
5057 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005058 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005059 }
5060
5061 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005062 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005063}
Paul Menageb4f48b62007-10-18 23:39:33 -07005064
Tejun Heoe5fca242013-11-22 17:14:39 -05005065static int __init cgroup_wq_init(void)
5066{
5067 /*
5068 * There isn't much point in executing destruction path in
5069 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005070 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005071 *
5072 * We would prefer to do this in cgroup_init() above, but that
5073 * is called before init_workqueues(): so leave this until after.
5074 */
Tejun Heo1a115332014-02-12 19:06:19 -05005075 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005076 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005077
5078 /*
5079 * Used to destroy pidlists and separate to serve as flush domain.
5080 * Cap @max_active to 1 too.
5081 */
5082 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5083 0, 1);
5084 BUG_ON(!cgroup_pidlist_destroy_wq);
5085
Tejun Heoe5fca242013-11-22 17:14:39 -05005086 return 0;
5087}
5088core_initcall(cgroup_wq_init);
5089
Paul Menagea4243162007-10-18 23:39:35 -07005090/*
5091 * proc_cgroup_show()
5092 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5093 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005094 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005095int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5096 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005097{
Tejun Heoe61734c2014-02-12 09:29:50 -05005098 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005099 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005100 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005101
5102 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005103 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005104 if (!buf)
5105 goto out;
5106
Paul Menagea4243162007-10-18 23:39:35 -07005107 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05005108 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005109
Tejun Heo985ed672014-03-19 10:23:53 -04005110 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005111 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005112 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005113 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005114
Tejun Heoa2dd4242014-03-19 10:23:55 -04005115 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005116 continue;
5117
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005118 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05005119 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04005120 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05005121 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f332009-09-23 15:56:19 -07005122 if (strlen(root->name))
5123 seq_printf(m, "%sname=%s", count ? "," : "",
5124 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005125 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005126 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05005127 path = cgroup_path(cgrp, buf, PATH_MAX);
5128 if (!path) {
5129 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07005130 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05005131 }
5132 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07005133 seq_putc(m, '\n');
5134 }
5135
Zefan Li006f4ac2014-09-18 16:03:15 +08005136 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005137out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05005138 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005139 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005140 kfree(buf);
5141out:
5142 return retval;
5143}
5144
Paul Menagea4243162007-10-18 23:39:35 -07005145/* Display information about each subsystem and each hierarchy */
5146static int proc_cgroupstats_show(struct seq_file *m, void *v)
5147{
Tejun Heo30159ec2013-06-25 11:53:37 -07005148 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005149 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005150
Paul Menage8bab8dd2008-04-04 14:29:57 -07005151 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005152 /*
5153 * ideally we don't want subsystems moving around while we do this.
5154 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5155 * subsys/hierarchy state.
5156 */
Paul Menagea4243162007-10-18 23:39:35 -07005157 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005158
5159 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005160 seq_printf(m, "%s\t%d\t%d\t%d\n",
5161 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05005162 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005163
Paul Menagea4243162007-10-18 23:39:35 -07005164 mutex_unlock(&cgroup_mutex);
5165 return 0;
5166}
5167
5168static int cgroupstats_open(struct inode *inode, struct file *file)
5169{
Al Viro9dce07f2008-03-29 03:07:28 +00005170 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005171}
5172
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005173static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005174 .open = cgroupstats_open,
5175 .read = seq_read,
5176 .llseek = seq_lseek,
5177 .release = single_release,
5178};
5179
Paul Menageb4f48b62007-10-18 23:39:33 -07005180/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005181 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005182 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005183 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005184 * A task is associated with the init_css_set until cgroup_post_fork()
5185 * attaches it to the parent's css_set. Empty cg_list indicates that
5186 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005187 */
5188void cgroup_fork(struct task_struct *child)
5189{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005190 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005191 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005192}
5193
5194/**
Li Zefana043e3b2008-02-23 15:24:09 -08005195 * cgroup_post_fork - called on a new task after adding it to the task list
5196 * @child: the task in question
5197 *
Tejun Heo5edee612012-10-16 15:03:14 -07005198 * Adds the task to the list running through its css_set if necessary and
5199 * call the subsystem fork() callbacks. Has to be after the task is
5200 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005201 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005202 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005203 */
Paul Menage817929e2007-10-18 23:39:36 -07005204void cgroup_post_fork(struct task_struct *child)
5205{
Tejun Heo30159ec2013-06-25 11:53:37 -07005206 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005207 int i;
5208
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005209 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005210 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005211 * function sets use_task_css_set_links before grabbing
5212 * tasklist_lock and we just went through tasklist_lock to add
5213 * @child, it's guaranteed that either we see the set
5214 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5215 * @child during its iteration.
5216 *
5217 * If we won the race, @child is associated with %current's
5218 * css_set. Grabbing css_set_rwsem guarantees both that the
5219 * association is stable, and, on completion of the parent's
5220 * migration, @child is visible in the source of migration or
5221 * already in the destination cgroup. This guarantee is necessary
5222 * when implementing operations which need to migrate all tasks of
5223 * a cgroup to another.
5224 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005225 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005226 * will remain in init_css_set. This is safe because all tasks are
5227 * in the init_css_set before cg_links is enabled and there's no
5228 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005229 */
Paul Menage817929e2007-10-18 23:39:36 -07005230 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005231 struct css_set *cset;
5232
Tejun Heo96d365e2014-02-13 06:58:40 -05005233 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005234 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005235 if (list_empty(&child->cg_list)) {
5236 rcu_assign_pointer(child->cgroups, cset);
5237 list_add(&child->cg_list, &cset->tasks);
5238 get_css_set(cset);
5239 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005240 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07005241 }
Tejun Heo5edee612012-10-16 15:03:14 -07005242
5243 /*
5244 * Call ss->fork(). This must happen after @child is linked on
5245 * css_set; otherwise, @child might change state between ->fork()
5246 * and addition to css_set.
5247 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005248 for_each_subsys_which(ss, i, &have_fork_callback)
5249 ss->fork(child);
Paul Menage817929e2007-10-18 23:39:36 -07005250}
Tejun Heo5edee612012-10-16 15:03:14 -07005251
Paul Menage817929e2007-10-18 23:39:36 -07005252/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005253 * cgroup_exit - detach cgroup from exiting task
5254 * @tsk: pointer to task_struct of exiting process
5255 *
5256 * Description: Detach cgroup from @tsk and release it.
5257 *
5258 * Note that cgroups marked notify_on_release force every task in
5259 * them to take the global cgroup_mutex mutex when exiting.
5260 * This could impact scaling on very large systems. Be reluctant to
5261 * use notify_on_release cgroups where very high task exit scaling
5262 * is required on large systems.
5263 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005264 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5265 * call cgroup_exit() while the task is still competent to handle
5266 * notify_on_release(), then leave the task attached to the root cgroup in
5267 * each hierarchy for the remainder of its exit. No need to bother with
5268 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005269 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005270 */
Li Zefan1ec41832014-03-28 15:22:19 +08005271void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005272{
Tejun Heo30159ec2013-06-25 11:53:37 -07005273 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005274 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005275 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005276 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005277
5278 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005279 * Unlink from @tsk from its css_set. As migration path can't race
5280 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005281 */
5282 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005283 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005284 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005285 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005286 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005287 }
5288
Paul Menageb4f48b62007-10-18 23:39:33 -07005289 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005290 cset = task_css_set(tsk);
5291 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005292
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005293 /* see cgroup_post_fork() for details */
5294 for_each_subsys_which(ss, i, &have_exit_callback) {
5295 struct cgroup_subsys_state *old_css = cset->subsys[i];
5296 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005297
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005298 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005299 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005300
Tejun Heoeaf797a2014-02-25 10:04:03 -05005301 if (put_cset)
Zefan Lia25eb522014-09-19 16:51:00 +08005302 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005303}
Paul Menage697f4162007-10-18 23:39:34 -07005304
Paul Menagebd89aab2007-10-18 23:40:44 -07005305static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005306{
Zefan Lia25eb522014-09-19 16:51:00 +08005307 if (notify_on_release(cgrp) && !cgroup_has_tasks(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005308 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5309 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005310}
5311
Paul Menage81a6a5c2007-10-18 23:39:38 -07005312/*
5313 * Notify userspace when a cgroup is released, by running the
5314 * configured release agent with the name of the cgroup (path
5315 * relative to the root of cgroup file system) as the argument.
5316 *
5317 * Most likely, this user command will try to rmdir this cgroup.
5318 *
5319 * This races with the possibility that some other task will be
5320 * attached to this cgroup before it is removed, or that some other
5321 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5322 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5323 * unused, and this cgroup will be reprieved from its death sentence,
5324 * to continue to serve a useful existence. Next time it's released,
5325 * we will get notified again, if it still has 'notify_on_release' set.
5326 *
5327 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5328 * means only wait until the task is successfully execve()'d. The
5329 * separate release agent task is forked by call_usermodehelper(),
5330 * then control in this thread returns here, without waiting for the
5331 * release agent task. We don't bother to wait because the caller of
5332 * this routine has no use for the exit status of the release agent
5333 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005334 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005335static void cgroup_release_agent(struct work_struct *work)
5336{
Zefan Li971ff492014-09-18 16:06:19 +08005337 struct cgroup *cgrp =
5338 container_of(work, struct cgroup, release_agent_work);
5339 char *pathbuf = NULL, *agentbuf = NULL, *path;
5340 char *argv[3], *envp[3];
5341
Paul Menage81a6a5c2007-10-18 23:39:38 -07005342 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005343
Zefan Li971ff492014-09-18 16:06:19 +08005344 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5345 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5346 if (!pathbuf || !agentbuf)
5347 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005348
Zefan Li971ff492014-09-18 16:06:19 +08005349 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5350 if (!path)
5351 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005352
Zefan Li971ff492014-09-18 16:06:19 +08005353 argv[0] = agentbuf;
5354 argv[1] = path;
5355 argv[2] = NULL;
5356
5357 /* minimal command environment */
5358 envp[0] = "HOME=/";
5359 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5360 envp[2] = NULL;
5361
Paul Menage81a6a5c2007-10-18 23:39:38 -07005362 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005363 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005364 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005365out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005366 mutex_unlock(&cgroup_mutex);
5367out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005368 kfree(agentbuf);
5369 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005370}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005371
5372static int __init cgroup_disable(char *str)
5373{
Tejun Heo30159ec2013-06-25 11:53:37 -07005374 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005375 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005376 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005377
5378 while ((token = strsep(&str, ",")) != NULL) {
5379 if (!*token)
5380 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005381
Tejun Heo3ed80a62014-02-08 10:36:58 -05005382 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005383 if (!strcmp(token, ss->name)) {
5384 ss->disabled = 1;
5385 printk(KERN_INFO "Disabling %s control group"
5386 " subsystem\n", ss->name);
5387 break;
5388 }
5389 }
5390 }
5391 return 1;
5392}
5393__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005394
Tejun Heoa8ddc822014-07-15 11:05:10 -04005395static int __init cgroup_set_legacy_files_on_dfl(char *str)
5396{
5397 printk("cgroup: using legacy files on the default hierarchy\n");
5398 cgroup_legacy_files_on_dfl = true;
5399 return 0;
5400}
5401__setup("cgroup__DEVEL__legacy_files_on_dfl", cgroup_set_legacy_files_on_dfl);
5402
Tejun Heob77d7b62013-08-13 11:01:54 -04005403/**
Tejun Heoec903c02014-05-13 12:11:01 -04005404 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005405 * @dentry: directory dentry of interest
5406 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005407 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005408 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5409 * to get the corresponding css and return it. If such css doesn't exist
5410 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005411 */
Tejun Heoec903c02014-05-13 12:11:01 -04005412struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5413 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005414{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005415 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5416 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005417 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005418
Tejun Heo35cf0832013-08-26 18:40:56 -04005419 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005420 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5421 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005422 return ERR_PTR(-EBADF);
5423
Tejun Heo5a17f542014-02-11 11:52:47 -05005424 rcu_read_lock();
5425
Tejun Heo2bd59d42014-02-11 11:52:49 -05005426 /*
5427 * This path doesn't originate from kernfs and @kn could already
5428 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005429 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005430 */
5431 cgrp = rcu_dereference(kn->priv);
5432 if (cgrp)
5433 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005434
Tejun Heoec903c02014-05-13 12:11:01 -04005435 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005436 css = ERR_PTR(-ENOENT);
5437
5438 rcu_read_unlock();
5439 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005440}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005441
Li Zefan1cb650b2013-08-19 10:05:24 +08005442/**
5443 * css_from_id - lookup css by id
5444 * @id: the cgroup id
5445 * @ss: cgroup subsys to be looked into
5446 *
5447 * Returns the css if there's valid one with @id, otherwise returns NULL.
5448 * Should be called under rcu_read_lock().
5449 */
5450struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5451{
Tejun Heo6fa49182014-05-04 15:09:13 -04005452 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005453 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005454}
5455
Paul Menagefe693432009-09-23 15:56:20 -07005456#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005457static struct cgroup_subsys_state *
5458debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005459{
5460 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5461
5462 if (!css)
5463 return ERR_PTR(-ENOMEM);
5464
5465 return css;
5466}
5467
Tejun Heoeb954192013-08-08 20:11:23 -04005468static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005469{
Tejun Heoeb954192013-08-08 20:11:23 -04005470 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005471}
5472
Tejun Heo182446d2013-08-08 20:11:24 -04005473static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5474 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005475{
Tejun Heo182446d2013-08-08 20:11:24 -04005476 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005477}
5478
Tejun Heo182446d2013-08-08 20:11:24 -04005479static u64 current_css_set_read(struct cgroup_subsys_state *css,
5480 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005481{
5482 return (u64)(unsigned long)current->cgroups;
5483}
5484
Tejun Heo182446d2013-08-08 20:11:24 -04005485static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005486 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005487{
5488 u64 count;
5489
5490 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005491 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005492 rcu_read_unlock();
5493 return count;
5494}
5495
Tejun Heo2da8ca82013-12-05 12:28:04 -05005496static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005497{
Tejun Heo69d02062013-06-12 21:04:50 -07005498 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005499 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005500 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005501
Tejun Heoe61734c2014-02-12 09:29:50 -05005502 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5503 if (!name_buf)
5504 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005505
Tejun Heo96d365e2014-02-13 06:58:40 -05005506 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005507 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005508 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005509 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005510 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005511
Tejun Heoa2dd4242014-03-19 10:23:55 -04005512 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005513 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005514 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005515 }
5516 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005517 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005518 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005519 return 0;
5520}
5521
5522#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005523static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005524{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005525 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005526 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005527
Tejun Heo96d365e2014-02-13 06:58:40 -05005528 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005529 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005530 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005531 struct task_struct *task;
5532 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005533
Tejun Heo5abb8852013-06-12 21:04:49 -07005534 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005535
Tejun Heo5abb8852013-06-12 21:04:49 -07005536 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005537 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5538 goto overflow;
5539 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005540 }
Tejun Heoc7561122014-02-25 10:04:01 -05005541
5542 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5543 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5544 goto overflow;
5545 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5546 }
5547 continue;
5548 overflow:
5549 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005550 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005551 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005552 return 0;
5553}
5554
Tejun Heo182446d2013-08-08 20:11:24 -04005555static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005556{
Zefan Lia25eb522014-09-19 16:51:00 +08005557 return (!cgroup_has_tasks(css->cgroup) &&
5558 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005559}
5560
5561static struct cftype debug_files[] = {
5562 {
Paul Menagefe693432009-09-23 15:56:20 -07005563 .name = "taskcount",
5564 .read_u64 = debug_taskcount_read,
5565 },
5566
5567 {
5568 .name = "current_css_set",
5569 .read_u64 = current_css_set_read,
5570 },
5571
5572 {
5573 .name = "current_css_set_refcount",
5574 .read_u64 = current_css_set_refcount_read,
5575 },
5576
5577 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005578 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005579 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005580 },
5581
5582 {
5583 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005584 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005585 },
5586
5587 {
Paul Menagefe693432009-09-23 15:56:20 -07005588 .name = "releasable",
5589 .read_u64 = releasable_read,
5590 },
Paul Menagefe693432009-09-23 15:56:20 -07005591
Tejun Heo4baf6e32012-04-01 12:09:55 -07005592 { } /* terminate */
5593};
Paul Menagefe693432009-09-23 15:56:20 -07005594
Tejun Heo073219e2014-02-08 10:36:58 -05005595struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005596 .css_alloc = debug_css_alloc,
5597 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005598 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005599};
5600#endif /* CONFIG_CGROUP_DEBUG */