blob: d9f854defa784e3710b80addc5bf81d490bc2ff0 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mm.h>
40#include <linux/mutex.h>
41#include <linux/mount.h>
42#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070043#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/rcupdate.h>
45#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050048#include <linux/rwsem.h>
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 Heoe25e2cb2011-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 Heoe25e2cb2011-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 Heoe25e2cb2011-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 Heoe25e2cb2011-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 Heo87fb54f2013-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 Heod0ec4232015-08-05 16:03:19 -0400148EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700149
Tejun Heoa2dd4242014-03-19 10:23:55 -0400150/*
151 * The default hierarchy always exists but is hidden until mounted for the
152 * first time. This is for backward compatibility.
153 */
154static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155
Tejun Heoa8ddc822014-07-15 11:05:10 -0400156/*
157 * Set by the boot param of the same name and makes subsystems with NULL
158 * ->dfl_files to use ->legacy_files on the default hierarchy.
159 */
160static bool cgroup_legacy_files_on_dfl;
161
Tejun Heo5533e012014-05-14 19:33:07 -0400162/* some controllers are not supported in the default hierarchy */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000163static unsigned long cgrp_dfl_root_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400164
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165/* The list of hierarchy roots */
166
Tejun Heo9871bf92013-06-24 15:21:47 -0700167static LIST_HEAD(cgroup_roots);
168static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169
Tejun Heo3417ae12014-02-08 10:37:01 -0500170/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700171static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700172
Li Zefan794611a2013-06-18 18:53:53 +0800173/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400174 * Assign a monotonically increasing serial number to csses. It guarantees
175 * cgroups with bigger numbers are newer than those with smaller numbers.
176 * Also, as csses are always appended to the parent's ->children list, it
177 * guarantees that sibling csses are always sorted in the ascending serial
178 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800179 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400180static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800181
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000182/*
183 * These bitmask flags indicate whether tasks in the fork and exit paths have
184 * fork/exit handlers to call. This avoids us having to do extra work in the
185 * fork/exit path to check which subsystems have fork/exit callbacks.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000187static unsigned long have_fork_callback __read_mostly;
188static unsigned long have_exit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189
Tejun Heoa14c6872014-07-15 11:05:09 -0400190static struct cftype cgroup_dfl_base_files[];
191static struct cftype cgroup_legacy_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700192
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400193static int rebind_subsystems(struct cgroup_root *dst_root,
Aleksa Sarai8ab456a2015-05-19 00:51:00 +1000194 unsigned long ss_mask);
Tejun Heo42809dd2012-11-19 08:13:37 -0800195static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof63070d2014-07-08 18:02:57 -0400196static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
197 bool visible);
Tejun Heo9d755d32014-05-14 09:15:02 -0400198static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400199static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400200static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
201 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800202
Tejun Heo6fa49182014-05-04 15:09:13 -0400203/* IDR wrappers which synchronize using cgroup_idr_lock */
204static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
205 gfp_t gfp_mask)
206{
207 int ret;
208
209 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400210 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400211 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400212 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400213 idr_preload_end();
214 return ret;
215}
216
217static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
218{
219 void *ret;
220
Tejun Heo54504e92014-05-13 12:10:59 -0400221 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400222 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400223 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400224 return ret;
225}
226
227static void cgroup_idr_remove(struct idr *idr, int id)
228{
Tejun Heo54504e92014-05-13 12:10:59 -0400229 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400230 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400231 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400232}
233
Tejun Heod51f39b2014-05-16 13:22:48 -0400234static struct cgroup *cgroup_parent(struct cgroup *cgrp)
235{
236 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
237
238 if (parent_css)
239 return container_of(parent_css, struct cgroup, self);
240 return NULL;
241}
242
Tejun Heo95109b62013-08-08 20:11:27 -0400243/**
244 * cgroup_css - obtain a cgroup's css for the specified subsystem
245 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400246 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400247 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400248 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
249 * function must be called either under cgroup_mutex or rcu_read_lock() and
250 * the caller is responsible for pinning the returned css if it wants to
251 * keep accessing it outside the said locks. This function may return
252 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400253 */
254static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400255 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400256{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400257 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500258 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500259 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400260 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400261 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400262}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700263
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400264/**
265 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
266 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400267 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400268 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400269 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400270 * as the matching css of the nearest ancestor including self which has @ss
271 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
272 * function is guaranteed to return non-NULL css.
273 */
274static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
275 struct cgroup_subsys *ss)
276{
277 lockdep_assert_held(&cgroup_mutex);
278
279 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400280 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400281
282 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
283 return NULL;
284
Tejun Heoeeecbd12014-11-18 02:49:52 -0500285 /*
286 * This function is used while updating css associations and thus
287 * can't test the csses directly. Use ->child_subsys_mask.
288 */
Tejun Heod51f39b2014-05-16 13:22:48 -0400289 while (cgroup_parent(cgrp) &&
290 !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
291 cgrp = cgroup_parent(cgrp);
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400292
293 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700294}
295
Tejun Heoeeecbd12014-11-18 02:49:52 -0500296/**
297 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
298 * @cgrp: the cgroup of interest
299 * @ss: the subsystem of interest
300 *
301 * Find and get the effective css of @cgrp for @ss. The effective css is
302 * defined as the matching css of the nearest ancestor including self which
303 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
304 * the root css is returned, so this function always returns a valid css.
305 * The returned css must be put using css_put().
306 */
307struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
308 struct cgroup_subsys *ss)
309{
310 struct cgroup_subsys_state *css;
311
312 rcu_read_lock();
313
314 do {
315 css = cgroup_css(cgrp, ss);
316
317 if (css && css_tryget_online(css))
318 goto out_unlock;
319 cgrp = cgroup_parent(cgrp);
320 } while (cgrp);
321
322 css = init_css_set.subsys[ss->id];
323 css_get(css);
324out_unlock:
325 rcu_read_unlock();
326 return css;
327}
328
Paul Menageddbcc7e2007-10-18 23:39:30 -0700329/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700330static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700331{
Tejun Heo184faf32014-05-16 13:22:51 -0400332 return !(cgrp->self.flags & CSS_ONLINE);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700333}
334
Tejun Heob4168642014-05-13 12:16:21 -0400335struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500336{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500337 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400338 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500339
340 /*
341 * This is open and unprotected implementation of cgroup_css().
342 * seq_css() is only called from a kernfs file operation which has
343 * an active reference on the file. Because all the subsystem
344 * files are drained before a css is disassociated with a cgroup,
345 * the matching css from the cgroup's subsys table is guaranteed to
346 * be and stay valid until the enclosing operation is complete.
347 */
348 if (cft->ss)
349 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
350 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400351 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500352}
Tejun Heob4168642014-05-13 12:16:21 -0400353EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500354
Li Zefan78574cf2013-04-08 19:00:38 -0700355/**
356 * cgroup_is_descendant - test ancestry
357 * @cgrp: the cgroup to be tested
358 * @ancestor: possible ancestor of @cgrp
359 *
360 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
361 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
362 * and @ancestor are accessible.
363 */
364bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
365{
366 while (cgrp) {
367 if (cgrp == ancestor)
368 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400369 cgrp = cgroup_parent(cgrp);
Li Zefan78574cf2013-04-08 19:00:38 -0700370 }
371 return false;
372}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700373
Adrian Bunke9685a02008-02-07 00:13:46 -0800374static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700375{
Paul Menagebd89aab2007-10-18 23:40:44 -0700376 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700377}
378
Tejun Heo30159ec2013-06-25 11:53:37 -0700379/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500380 * for_each_css - iterate all css's of a cgroup
381 * @css: the iteration cursor
382 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
383 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700384 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400385 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700386 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500387#define for_each_css(css, ssid, cgrp) \
388 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
389 if (!((css) = rcu_dereference_check( \
390 (cgrp)->subsys[(ssid)], \
391 lockdep_is_held(&cgroup_mutex)))) { } \
392 else
393
394/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400395 * for_each_e_css - iterate all effective css's of a cgroup
396 * @css: the iteration cursor
397 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
398 * @cgrp: the target cgroup to iterate css's of
399 *
400 * Should be called under cgroup_[tree_]mutex.
401 */
402#define for_each_e_css(css, ssid, cgrp) \
403 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
404 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
405 ; \
406 else
407
408/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500409 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700410 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500411 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700412 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500413#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500414 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
415 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700416
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000417/**
418 * for_each_subsys_which - filter for_each_subsys with a bitmask
419 * @ss: the iteration cursor
420 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
421 * @ss_maskp: a pointer to the bitmask
422 *
423 * The block will only run for cases where the ssid-th bit (1 << ssid) of
424 * mask is set to 1.
425 */
426#define for_each_subsys_which(ss, ssid, ss_maskp) \
427 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000428 (ssid) = 0; \
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000429 else \
430 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
431 if (((ss) = cgroup_subsys[ssid]) && false) \
432 break; \
433 else
434
Tejun Heo985ed672014-03-19 10:23:53 -0400435/* iterate across the hierarchies */
436#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700437 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700438
Tejun Heof8f22e52014-04-23 11:13:16 -0400439/* iterate over child cgrps, lock should be held throughout iteration */
440#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400441 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400442 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400443 cgroup_is_dead(child); })) \
444 ; \
445 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700446
Paul Menage81a6a5c2007-10-18 23:39:38 -0700447static void cgroup_release_agent(struct work_struct *work);
Paul Menagebd89aab2007-10-18 23:40:44 -0700448static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700449
Tejun Heo69d02062013-06-12 21:04:50 -0700450/*
451 * A cgroup can be associated with multiple css_sets as different tasks may
452 * belong to different cgroups on different hierarchies. In the other
453 * direction, a css_set is naturally associated with multiple cgroups.
454 * This M:N relationship is represented by the following link structure
455 * which exists for each association and allows traversing the associations
456 * from both sides.
457 */
458struct cgrp_cset_link {
459 /* the cgroup and css_set this link associates */
460 struct cgroup *cgrp;
461 struct css_set *cset;
462
463 /* list of cgrp_cset_links anchored at cgrp->cset_links */
464 struct list_head cset_link;
465
466 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
467 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700468};
469
Tejun Heo172a2c062014-03-19 10:23:53 -0400470/*
471 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700472 * hierarchies being mounted. It contains a pointer to the root state
473 * for each subsystem. Also used to anchor the list of css_sets. Not
474 * reference-counted, to improve performance when child cgroups
475 * haven't been created.
476 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400477struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400478 .refcount = ATOMIC_INIT(1),
479 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
480 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
481 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
482 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
483 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
484};
Paul Menage817929e2007-10-18 23:39:36 -0700485
Tejun Heo172a2c062014-03-19 10:23:53 -0400486static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700487
Tejun Heo842b5972014-04-25 18:28:02 -0400488/**
489 * cgroup_update_populated - updated populated count of a cgroup
490 * @cgrp: the target cgroup
491 * @populated: inc or dec populated count
492 *
493 * @cgrp is either getting the first task (css_set) or losing the last.
494 * Update @cgrp->populated_cnt accordingly. The count is propagated
495 * towards root so that a given cgroup's populated_cnt is zero iff the
496 * cgroup and all its descendants are empty.
497 *
498 * @cgrp's interface file "cgroup.populated" is zero if
499 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
500 * changes from or to zero, userland is notified that the content of the
501 * interface file has changed. This can be used to detect when @cgrp and
502 * its descendants become populated or empty.
503 */
504static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
505{
506 lockdep_assert_held(&css_set_rwsem);
507
508 do {
509 bool trigger;
510
511 if (populated)
512 trigger = !cgrp->populated_cnt++;
513 else
514 trigger = !--cgrp->populated_cnt;
515
516 if (!trigger)
517 break;
518
519 if (cgrp->populated_kn)
520 kernfs_notify(cgrp->populated_kn);
Tejun Heod51f39b2014-05-16 13:22:48 -0400521 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400522 } while (cgrp);
523}
524
Paul Menage7717f7b2009-09-23 15:56:22 -0700525/*
526 * hash table for cgroup groups. This improves the performance to find
527 * an existing css_set. This hash doesn't (currently) take into
528 * account cgroups in empty hierarchies.
529 */
Li Zefan472b1052008-04-29 01:00:11 -0700530#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800531static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700532
Li Zefan0ac801f2013-01-10 11:49:27 +0800533static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700534{
Li Zefan0ac801f2013-01-10 11:49:27 +0800535 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700536 struct cgroup_subsys *ss;
537 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700538
Tejun Heo30159ec2013-06-25 11:53:37 -0700539 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800540 key += (unsigned long)css[i];
541 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700542
Li Zefan0ac801f2013-01-10 11:49:27 +0800543 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700544}
545
Zefan Lia25eb522014-09-19 16:51:00 +0800546static void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700547{
Tejun Heo69d02062013-06-12 21:04:50 -0700548 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400549 struct cgroup_subsys *ss;
550 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700551
Tejun Heo89c55092014-02-13 06:58:40 -0500552 lockdep_assert_held(&css_set_rwsem);
553
554 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700555 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700556
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700557 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400558 for_each_subsys(ss, ssid)
559 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700560 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700561 css_set_count--;
562
Tejun Heo69d02062013-06-12 21:04:50 -0700563 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700564 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700565
Tejun Heo69d02062013-06-12 21:04:50 -0700566 list_del(&link->cset_link);
567 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800568
Tejun Heo96d365e2014-02-13 06:58:40 -0500569 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400570 if (list_empty(&cgrp->cset_links)) {
571 cgroup_update_populated(cgrp, false);
Zefan Lia25eb522014-09-19 16:51:00 +0800572 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700573 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700574
575 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700576 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700577
Tejun Heo5abb8852013-06-12 21:04:49 -0700578 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700579}
580
Zefan Lia25eb522014-09-19 16:51:00 +0800581static void put_css_set(struct css_set *cset)
Tejun Heo89c55092014-02-13 06:58:40 -0500582{
583 /*
584 * Ensure that the refcount doesn't hit zero while any readers
585 * can see it. Similar to atomic_dec_and_lock(), but for an
586 * rwlock
587 */
588 if (atomic_add_unless(&cset->refcount, -1, 1))
589 return;
590
591 down_write(&css_set_rwsem);
Zefan Lia25eb522014-09-19 16:51:00 +0800592 put_css_set_locked(cset);
Tejun Heo89c55092014-02-13 06:58:40 -0500593 up_write(&css_set_rwsem);
594}
595
Paul Menage817929e2007-10-18 23:39:36 -0700596/*
597 * refcounted get/put for css_set objects
598 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700599static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700600{
Tejun Heo5abb8852013-06-12 21:04:49 -0700601 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700602}
603
Tejun Heob326f9d2013-06-24 15:21:48 -0700604/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700605 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700606 * @cset: candidate css_set being tested
607 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700608 * @new_cgrp: cgroup that's being entered by the task
609 * @template: desired set of css pointers in css_set (pre-calculated)
610 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800611 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700612 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
613 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700614static bool compare_css_sets(struct css_set *cset,
615 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700616 struct cgroup *new_cgrp,
617 struct cgroup_subsys_state *template[])
618{
619 struct list_head *l1, *l2;
620
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400621 /*
622 * On the default hierarchy, there can be csets which are
623 * associated with the same set of cgroups but different csses.
624 * Let's first ensure that csses match.
625 */
626 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700627 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700628
629 /*
630 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400631 * different cgroups in hierarchies. As different cgroups may
632 * share the same effective css, this comparison is always
633 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700634 */
Tejun Heo69d02062013-06-12 21:04:50 -0700635 l1 = &cset->cgrp_links;
636 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700637 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700638 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700640
641 l1 = l1->next;
642 l2 = l2->next;
643 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700644 if (l1 == &cset->cgrp_links) {
645 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700646 break;
647 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700648 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700649 }
650 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700651 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
652 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
653 cgrp1 = link1->cgrp;
654 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700655 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700657
658 /*
659 * If this hierarchy is the hierarchy of the cgroup
660 * that's changing, then we need to check that this
661 * css_set points to the new cgroup; if it's any other
662 * hierarchy, then this css_set should point to the
663 * same cgroup as the old css_set.
664 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 if (cgrp1->root == new_cgrp->root) {
666 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700667 return false;
668 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700669 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 return false;
671 }
672 }
673 return true;
674}
675
Tejun Heob326f9d2013-06-24 15:21:48 -0700676/**
677 * find_existing_css_set - init css array and find the matching css_set
678 * @old_cset: the css_set that we're using before the cgroup transition
679 * @cgrp: the cgroup that we're moving into
680 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700681 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700682static struct css_set *find_existing_css_set(struct css_set *old_cset,
683 struct cgroup *cgrp,
684 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700685{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400686 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700687 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800689 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700690 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700691
Ben Blumaae8aab2010-03-10 15:22:07 -0800692 /*
693 * Build the set of subsystem state objects that we want to see in the
694 * new css_set. while subsystems can change globally, the entries here
695 * won't change, so no need for locking.
696 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700697 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400698 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400699 /*
700 * @ss is in this hierarchy, so we want the
701 * effective css from @cgrp.
702 */
703 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700704 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400705 /*
706 * @ss is not in this hierarchy, so we don't want
707 * to change the css.
708 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700710 }
711 }
712
Li Zefan0ac801f2013-01-10 11:49:27 +0800713 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700714 hash_for_each_possible(css_set_table, cset, hlist, key) {
715 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 continue;
717
718 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700720 }
Paul Menage817929e2007-10-18 23:39:36 -0700721
722 /* No existing cgroup group matched */
723 return NULL;
724}
725
Tejun Heo69d02062013-06-12 21:04:50 -0700726static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700727{
Tejun Heo69d02062013-06-12 21:04:50 -0700728 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700729
Tejun Heo69d02062013-06-12 21:04:50 -0700730 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
731 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700732 kfree(link);
733 }
734}
735
Tejun Heo69d02062013-06-12 21:04:50 -0700736/**
737 * allocate_cgrp_cset_links - allocate cgrp_cset_links
738 * @count: the number of links to allocate
739 * @tmp_links: list_head the allocated links are put on
740 *
741 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
742 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700743 */
Tejun Heo69d02062013-06-12 21:04:50 -0700744static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700745{
Tejun Heo69d02062013-06-12 21:04:50 -0700746 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700747 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700748
749 INIT_LIST_HEAD(tmp_links);
750
Li Zefan36553432008-07-29 22:33:19 -0700751 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700752 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700753 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700754 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700755 return -ENOMEM;
756 }
Tejun Heo69d02062013-06-12 21:04:50 -0700757 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700758 }
759 return 0;
760}
761
Li Zefanc12f65d2009-01-07 18:07:42 -0800762/**
763 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700764 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700765 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800766 * @cgrp: the destination cgroup
767 */
Tejun Heo69d02062013-06-12 21:04:50 -0700768static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
769 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800770{
Tejun Heo69d02062013-06-12 21:04:50 -0700771 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800772
Tejun Heo69d02062013-06-12 21:04:50 -0700773 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400774
775 if (cgroup_on_dfl(cgrp))
776 cset->dfl_cgrp = cgrp;
777
Tejun Heo69d02062013-06-12 21:04:50 -0700778 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
779 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700780 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400781
782 if (list_empty(&cgrp->cset_links))
783 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700784 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400785
Paul Menage7717f7b2009-09-23 15:56:22 -0700786 /*
787 * Always add links to the tail of the list so that the list
788 * is sorted by order of hierarchy creation
789 */
Tejun Heo69d02062013-06-12 21:04:50 -0700790 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800791}
792
Tejun Heob326f9d2013-06-24 15:21:48 -0700793/**
794 * find_css_set - return a new css_set with one cgroup updated
795 * @old_cset: the baseline css_set
796 * @cgrp: the cgroup to be updated
797 *
798 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
799 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700800 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700801static struct css_set *find_css_set(struct css_set *old_cset,
802 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700803{
Tejun Heob326f9d2013-06-24 15:21:48 -0700804 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700805 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700806 struct list_head tmp_links;
807 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400808 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800809 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400810 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700811
Tejun Heob326f9d2013-06-24 15:21:48 -0700812 lockdep_assert_held(&cgroup_mutex);
813
Paul Menage817929e2007-10-18 23:39:36 -0700814 /* First see if we already have a cgroup group that matches
815 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500816 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700817 cset = find_existing_css_set(old_cset, cgrp, template);
818 if (cset)
819 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500820 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700821
Tejun Heo5abb8852013-06-12 21:04:49 -0700822 if (cset)
823 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700824
Tejun Heof4f4be22013-06-12 21:04:51 -0700825 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700826 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700827 return NULL;
828
Tejun Heo69d02062013-06-12 21:04:50 -0700829 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700830 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700831 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700832 return NULL;
833 }
834
Tejun Heo5abb8852013-06-12 21:04:49 -0700835 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700836 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700837 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500838 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500839 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500840 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700841 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700842
843 /* Copy the set of subsystem state objects generated in
844 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700845 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700846
Tejun Heo96d365e2014-02-13 06:58:40 -0500847 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700848 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700849 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700850 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700851
Paul Menage7717f7b2009-09-23 15:56:22 -0700852 if (c->root == cgrp->root)
853 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700854 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700855 }
Paul Menage817929e2007-10-18 23:39:36 -0700856
Tejun Heo69d02062013-06-12 21:04:50 -0700857 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700858
Paul Menage817929e2007-10-18 23:39:36 -0700859 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700860
Tejun Heo2d8f2432014-04-23 11:13:15 -0400861 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700862 key = css_set_hash(cset->subsys);
863 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700864
Tejun Heo2d8f2432014-04-23 11:13:15 -0400865 for_each_subsys(ss, ssid)
866 list_add_tail(&cset->e_cset_node[ssid],
867 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
868
Tejun Heo96d365e2014-02-13 06:58:40 -0500869 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700870
Tejun Heo5abb8852013-06-12 21:04:49 -0700871 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700872}
873
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400874static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700875{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400876 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500877
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400878 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500879}
880
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400881static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500882{
883 int id;
884
885 lockdep_assert_held(&cgroup_mutex);
886
Tejun Heo985ed672014-03-19 10:23:53 -0400887 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500888 if (id < 0)
889 return id;
890
891 root->hierarchy_id = id;
892 return 0;
893}
894
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400895static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500896{
897 lockdep_assert_held(&cgroup_mutex);
898
899 if (root->hierarchy_id) {
900 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
901 root->hierarchy_id = 0;
902 }
903}
904
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400905static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500906{
907 if (root) {
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400908 /* hierarchy ID should already have been released */
Tejun Heof2e85d52014-02-11 11:52:49 -0500909 WARN_ON_ONCE(root->hierarchy_id);
910
911 idr_destroy(&root->cgroup_idr);
912 kfree(root);
913 }
914}
915
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400916static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500917{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400918 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500919 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500920
Tejun Heo2bd59d42014-02-11 11:52:49 -0500921 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500922
Tejun Heo776f02f2014-02-12 09:29:50 -0500923 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -0400924 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -0500925
Tejun Heof2e85d52014-02-11 11:52:49 -0500926 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400927 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500928
929 /*
930 * Release all the links from cset_links to this hierarchy's
931 * root cgroup
932 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500933 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500934
935 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
936 list_del(&link->cset_link);
937 list_del(&link->cgrp_link);
938 kfree(link);
939 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500940 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500941
942 if (!list_empty(&root->root_list)) {
943 list_del(&root->root_list);
944 cgroup_root_count--;
945 }
946
947 cgroup_exit_root_id(root);
948
949 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500950
Tejun Heo2bd59d42014-02-11 11:52:49 -0500951 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500952 cgroup_free_root(root);
953}
954
Tejun Heoceb6a082014-02-25 10:04:02 -0500955/* look up cgroup associated with given css_set on the specified hierarchy */
956static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400957 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700958{
Paul Menage7717f7b2009-09-23 15:56:22 -0700959 struct cgroup *res = NULL;
960
Tejun Heo96d365e2014-02-13 06:58:40 -0500961 lockdep_assert_held(&cgroup_mutex);
962 lockdep_assert_held(&css_set_rwsem);
963
Tejun Heo5abb8852013-06-12 21:04:49 -0700964 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400965 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700966 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700967 struct cgrp_cset_link *link;
968
969 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700970 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700971
Paul Menage7717f7b2009-09-23 15:56:22 -0700972 if (c->root == root) {
973 res = c;
974 break;
975 }
976 }
977 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500978
Paul Menage7717f7b2009-09-23 15:56:22 -0700979 BUG_ON(!res);
980 return res;
981}
982
983/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500984 * Return the cgroup for "task" from the given hierarchy. Must be
985 * called with cgroup_mutex and css_set_rwsem held.
986 */
987static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400988 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500989{
990 /*
991 * No need to lock the task - since we hold cgroup_mutex the
992 * task can't change groups, so the only thing that can happen
993 * is that it exits and its css is set back to init_css_set.
994 */
995 return cset_cgroup_from_root(task_css_set(task), root);
996}
997
998/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 * A task must hold cgroup_mutex to modify cgroups.
1000 *
1001 * Any task can increment and decrement the count field without lock.
1002 * So in general, code holding cgroup_mutex can't rely on the count
1003 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001004 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 * means that no tasks are currently attached, therefore there is no
1006 * way a task attached to that cgroup can fork (the other way to
1007 * increment the count). So code holding cgroup_mutex can safely
1008 * assume that if the count is zero, it will stay zero. Similarly, if
1009 * a task holds cgroup_mutex on a cgroup with zero count, it
1010 * knows that the cgroup won't be removed, as cgroup_rmdir()
1011 * needs that mutex.
1012 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 * A cgroup can only be deleted if both its 'count' of using tasks
1014 * is zero, and its list of 'children' cgroups is empty. Since all
1015 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001016 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001018 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 *
1020 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001021 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 */
1023
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001024static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001025static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001026static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001027
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001028static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1029 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001031 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1032 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1033 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1034 cft->ss->name, cft->name);
1035 else
1036 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1037 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038}
1039
Tejun Heof2e85d52014-02-11 11:52:49 -05001040/**
1041 * cgroup_file_mode - deduce file mode of a control file
1042 * @cft: the control file in question
1043 *
1044 * returns cft->mode if ->mode is not 0
1045 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1046 * returns S_IRUGO if it has only a read handler
1047 * returns S_IWUSR if it has only a write hander
1048 */
1049static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001050{
Tejun Heof2e85d52014-02-11 11:52:49 -05001051 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001052
Tejun Heof2e85d52014-02-11 11:52:49 -05001053 if (cft->mode)
1054 return cft->mode;
1055
1056 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1057 mode |= S_IRUGO;
1058
Tejun Heo6770c642014-05-13 12:16:21 -04001059 if (cft->write_u64 || cft->write_s64 || cft->write)
Tejun Heof2e85d52014-02-11 11:52:49 -05001060 mode |= S_IWUSR;
1061
1062 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001063}
1064
Tejun Heo59f52962014-02-11 11:52:49 -05001065static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001067 WARN_ON_ONCE(cgroup_is_dead(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04001068 css_get(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069}
1070
Li Zefanaa323622014-09-04 14:43:38 +08001071static bool cgroup_tryget(struct cgroup *cgrp)
1072{
1073 return css_tryget(&cgrp->self);
1074}
1075
Tejun Heo59f52962014-02-11 11:52:49 -05001076static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077{
Tejun Heo9d755d32014-05-14 09:15:02 -04001078 css_put(&cgrp->self);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079}
1080
Tejun Heoa9746d82014-05-13 12:19:22 -04001081/**
Tejun Heo0f060de2014-11-18 02:49:50 -05001082 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
Tejun Heoaf0ba672014-07-08 18:02:57 -04001083 * @cgrp: the target cgroup
Tejun Heo0f060de2014-11-18 02:49:50 -05001084 * @subtree_control: the new subtree_control mask to consider
Tejun Heoaf0ba672014-07-08 18:02:57 -04001085 *
1086 * On the default hierarchy, a subsystem may request other subsystems to be
1087 * enabled together through its ->depends_on mask. In such cases, more
1088 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1089 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001090 * This function calculates which subsystems need to be enabled if
1091 * @subtree_control is to be applied to @cgrp. The returned mask is always
1092 * a superset of @subtree_control and follows the usual hierarchy rules.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001093 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001094static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
1095 unsigned long subtree_control)
Tejun Heo667c2492014-07-08 18:02:56 -04001096{
Tejun Heoaf0ba672014-07-08 18:02:57 -04001097 struct cgroup *parent = cgroup_parent(cgrp);
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001098 unsigned long cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001099 struct cgroup_subsys *ss;
1100 int ssid;
1101
1102 lockdep_assert_held(&cgroup_mutex);
1103
Tejun Heo0f060de2014-11-18 02:49:50 -05001104 if (!cgroup_on_dfl(cgrp))
1105 return cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001106
1107 while (true) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001108 unsigned long new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001109
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001110 for_each_subsys_which(ss, ssid, &cur_ss_mask)
1111 new_ss_mask |= ss->depends_on;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001112
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 Heoace2bee2014-02-11 11:52:47 -05001247 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001248
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001249 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo7fd8c562014-04-23 11:13:16 -04001250 /* if @ss has non-root csses attached to it, can't move */
1251 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001252 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253
Tejun Heo5df36032014-03-19 10:23:54 -04001254 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001255 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001256 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257 }
1258
Tejun Heo5533e012014-05-14 19:33:07 -04001259 /* skip creating root files on dfl_root for inhibited subsystems */
1260 tmp_ss_mask = ss_mask;
1261 if (dst_root == &cgrp_dfl_root)
1262 tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
1263
1264 ret = cgroup_populate_dir(&dst_root->cgrp, tmp_ss_mask);
Tejun Heoa2dd4242014-03-19 10:23:55 -04001265 if (ret) {
1266 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001267 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001268
Tejun Heoa2dd4242014-03-19 10:23:55 -04001269 /*
1270 * Rebinding back to the default root is not allowed to
1271 * fail. Using both default and non-default roots should
1272 * be rare. Moving subsystems back and forth even more so.
1273 * Just warn about it and continue.
1274 */
1275 if (cgrp_dfl_root_visible) {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001276 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001277 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001278 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001279 }
Tejun Heo5df36032014-03-19 10:23:54 -04001280 }
Tejun Heo31261212013-06-28 17:07:30 -07001281
1282 /*
1283 * Nothing can fail from this point on. Remove files for the
1284 * removed subsystems and rebind each subsystem.
1285 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001286 for_each_subsys_which(ss, ssid, &ss_mask)
1287 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo31261212013-06-28 17:07:30 -07001288
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001289 for_each_subsys_which(ss, ssid, &ss_mask) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001290 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001291 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001292 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001293
Tejun Heo5df36032014-03-19 10:23:54 -04001294 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001295 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001296
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001297 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001298
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001299 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1300 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001301 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001302 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001303
Tejun Heo2d8f2432014-04-23 11:13:15 -04001304 down_write(&css_set_rwsem);
1305 hash_for_each(css_set_table, i, cset, hlist)
1306 list_move_tail(&cset->e_cset_node[ss->id],
1307 &dst_root->cgrp.e_csets[ss->id]);
1308 up_write(&css_set_rwsem);
1309
Tejun Heof392e512014-04-23 11:13:14 -04001310 src_root->subsys_mask &= ~(1 << ssid);
Tejun Heo667c2492014-07-08 18:02:56 -04001311 src_root->cgrp.subtree_control &= ~(1 << ssid);
1312 cgroup_refresh_child_subsys_mask(&src_root->cgrp);
Tejun Heof392e512014-04-23 11:13:14 -04001313
Tejun Heobd53d612014-04-23 11:13:16 -04001314 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001315 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo667c2492014-07-08 18:02:56 -04001316 if (dst_root != &cgrp_dfl_root) {
1317 dst_root->cgrp.subtree_control |= 1 << ssid;
1318 cgroup_refresh_child_subsys_mask(&dst_root->cgrp);
1319 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001320
Tejun Heo5df36032014-03-19 10:23:54 -04001321 if (ss->bind)
1322 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324
Tejun Heoa2dd4242014-03-19 10:23:55 -04001325 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 return 0;
1327}
1328
Tejun Heo2bd59d42014-02-11 11:52:49 -05001329static int cgroup_show_options(struct seq_file *seq,
1330 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001332 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001334 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
Tejun Heod98817d2015-08-18 13:58:16 -07001336 if (root != &cgrp_dfl_root)
1337 for_each_subsys(ss, ssid)
1338 if (root->subsys_mask & (1 << ssid))
1339 seq_printf(seq, ",%s", ss->name);
Tejun Heo93438622013-04-14 20:15:25 -07001340 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001342 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001343 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001344
1345 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001346 if (strlen(root->release_agent_path))
1347 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001348 spin_unlock(&release_agent_path_lock);
1349
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001350 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001351 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001352 if (strlen(root->name))
1353 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001354 return 0;
1355}
1356
1357struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001358 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001359 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001360 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001361 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001362 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001363 /* User explicitly requested empty subsystem */
1364 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365};
1366
Ben Blumcf5d5942010-03-10 15:22:09 -08001367static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001369 char *token, *o = data;
1370 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001371 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001372 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001373 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001374 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001375
1376#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001377 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001378#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379
Paul Menagec6d57f32009-09-23 15:56:19 -07001380 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381
1382 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001383 nr_opts++;
1384
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385 if (!*token)
1386 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001387 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001388 /* Explicitly have no subsystems */
1389 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001390 continue;
1391 }
1392 if (!strcmp(token, "all")) {
1393 /* Mutually exclusive option 'all' + subsystem name */
1394 if (one_ss)
1395 return -EINVAL;
1396 all_ss = true;
1397 continue;
1398 }
Tejun Heo873fe092013-04-14 20:15:26 -07001399 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1400 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1401 continue;
1402 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001403 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001404 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001405 continue;
1406 }
1407 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001408 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001409 continue;
1410 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001411 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001412 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001413 continue;
1414 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001415 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001416 /* Specifying two release agents is forbidden */
1417 if (opts->release_agent)
1418 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001419 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001420 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001421 if (!opts->release_agent)
1422 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001423 continue;
1424 }
1425 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001426 const char *name = token + 5;
1427 /* Can't specify an empty name */
1428 if (!strlen(name))
1429 return -EINVAL;
1430 /* Must match [\w.-]+ */
1431 for (i = 0; i < strlen(name); i++) {
1432 char c = name[i];
1433 if (isalnum(c))
1434 continue;
1435 if ((c == '.') || (c == '-') || (c == '_'))
1436 continue;
1437 return -EINVAL;
1438 }
1439 /* Specifying two names is forbidden */
1440 if (opts->name)
1441 return -EINVAL;
1442 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001443 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001444 GFP_KERNEL);
1445 if (!opts->name)
1446 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001447
1448 continue;
1449 }
1450
Tejun Heo30159ec2013-06-25 11:53:37 -07001451 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001452 if (strcmp(token, ss->name))
1453 continue;
1454 if (ss->disabled)
1455 continue;
1456
1457 /* Mutually exclusive option 'all' + subsystem name */
1458 if (all_ss)
1459 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001460 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001461 one_ss = true;
1462
1463 break;
1464 }
1465 if (i == CGROUP_SUBSYS_COUNT)
1466 return -ENOENT;
1467 }
1468
Tejun Heo873fe092013-04-14 20:15:26 -07001469 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001470 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 -04001471 if (nr_opts != 1) {
1472 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001473 return -EINVAL;
1474 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001475 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001476 }
1477
Li Zefanf9ab5b52009-06-17 16:26:33 -07001478 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001479 * If the 'all' option was specified select all the subsystems,
1480 * otherwise if 'none', 'name=' and a subsystem name options were
1481 * not specified, let's default to 'all'
1482 */
1483 if (all_ss || (!one_ss && !opts->none && !opts->name))
1484 for_each_subsys(ss, i)
1485 if (!ss->disabled)
1486 opts->subsys_mask |= (1 << i);
1487
1488 /*
1489 * We either have to specify by name or by subsystems. (So all
1490 * empty hierarchies must have a name).
1491 */
1492 if (!opts->subsys_mask && !opts->name)
1493 return -EINVAL;
1494
1495 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001496 * Option noprefix was introduced just for backward compatibility
1497 * with the old cpuset, so we allow noprefix only if mounting just
1498 * the cpuset subsystem.
1499 */
Tejun Heo93438622013-04-14 20:15:25 -07001500 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001501 return -EINVAL;
1502
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001503 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001504 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001505 return -EINVAL;
1506
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 return 0;
1508}
1509
Tejun Heo2bd59d42014-02-11 11:52:49 -05001510static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511{
1512 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001513 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001515 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516
Tejun Heoaa6ec292014-07-09 10:08:08 -04001517 if (root == &cgrp_dfl_root) {
1518 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001519 return -EINVAL;
1520 }
1521
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522 mutex_lock(&cgroup_mutex);
1523
1524 /* See what subsystems are wanted */
1525 ret = parse_cgroupfs_options(data, &opts);
1526 if (ret)
1527 goto out_unlock;
1528
Tejun Heof392e512014-04-23 11:13:14 -04001529 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001530 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001531 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001532
Tejun Heof392e512014-04-23 11:13:14 -04001533 added_mask = opts.subsys_mask & ~root->subsys_mask;
1534 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001535
Ben Blumcf5d5942010-03-10 15:22:09 -08001536 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001537 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001538 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001539 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001540 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001542 goto out_unlock;
1543 }
1544
Tejun Heof172e672013-06-28 17:07:30 -07001545 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001546 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001547 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001548 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001549 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550
Tejun Heo5df36032014-03-19 10:23:54 -04001551 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001552 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001555 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001556
Tejun Heo69e943b2014-02-08 10:36:58 -05001557 if (opts.release_agent) {
1558 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001559 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001560 spin_unlock(&release_agent_path_lock);
1561 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001563 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001564 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 return ret;
1567}
1568
Tejun Heoafeb0f92014-02-13 06:58:39 -05001569/*
1570 * To reduce the fork() overhead for systems that are not actually using
1571 * their cgroups capability, we don't maintain the lists running through
1572 * each css_set to its tasks until we see the list actually used - in other
1573 * words after the first mount.
1574 */
1575static bool use_task_css_set_links __read_mostly;
1576
1577static void cgroup_enable_task_cg_lists(void)
1578{
1579 struct task_struct *p, *g;
1580
Tejun Heo96d365e2014-02-13 06:58:40 -05001581 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001582
1583 if (use_task_css_set_links)
1584 goto out_unlock;
1585
1586 use_task_css_set_links = true;
1587
1588 /*
1589 * We need tasklist_lock because RCU is not safe against
1590 * while_each_thread(). Besides, a forking task that has passed
1591 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1592 * is not guaranteed to have its child immediately visible in the
1593 * tasklist if we walk through it with RCU.
1594 */
1595 read_lock(&tasklist_lock);
1596 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001597 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1598 task_css_set(p) != &init_css_set);
1599
1600 /*
1601 * We should check if the process is exiting, otherwise
1602 * it will race with cgroup_exit() in that the list
1603 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001604 * Do it while holding siglock so that we don't end up
1605 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001606 */
Tejun Heof153ad12014-02-25 09:56:49 -05001607 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001608 if (!(p->flags & PF_EXITING)) {
1609 struct css_set *cset = task_css_set(p);
1610
1611 list_add(&p->cg_list, &cset->tasks);
1612 get_css_set(cset);
1613 }
Tejun Heof153ad12014-02-25 09:56:49 -05001614 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001615 } while_each_thread(g, p);
1616 read_unlock(&tasklist_lock);
1617out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001618 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001619}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620
Paul Menagecc31edc2008-10-18 20:28:04 -07001621static void init_cgroup_housekeeping(struct cgroup *cgrp)
1622{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001623 struct cgroup_subsys *ss;
1624 int ssid;
1625
Tejun Heod5c419b2014-05-16 13:22:48 -04001626 INIT_LIST_HEAD(&cgrp->self.sibling);
1627 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001628 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001629 INIT_LIST_HEAD(&cgrp->pidlists);
1630 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001631 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001632 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001633
1634 for_each_subsys(ss, ssid)
1635 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001636
1637 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001638 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001639}
Paul Menagec6d57f32009-09-23 15:56:19 -07001640
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001641static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001642 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001644 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001645
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001647 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001648 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001649 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001650 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651
Paul Menagec6d57f32009-09-23 15:56:19 -07001652 root->flags = opts->flags;
1653 if (opts->release_agent)
1654 strcpy(root->release_agent_path, opts->release_agent);
1655 if (opts->name)
1656 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001657 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001658 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001659}
1660
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001661static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001662{
Tejun Heod427dfe2014-02-11 11:52:48 -05001663 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001664 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heoa14c6872014-07-15 11:05:09 -04001665 struct cftype *base_files;
Tejun Heod427dfe2014-02-11 11:52:48 -05001666 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001667 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001668
Tejun Heod427dfe2014-02-11 11:52:48 -05001669 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001670
Tejun Heo6fa49182014-05-04 15:09:13 -04001671 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001672 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001673 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001674 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001675
Tejun Heo2aad2a82014-09-24 13:31:50 -04001676 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1677 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001678 if (ret)
1679 goto out;
1680
Tejun Heod427dfe2014-02-11 11:52:48 -05001681 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001682 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001683 * but that's OK - it can only be increased by someone holding
1684 * cgroup_lock, and that's us. The worst that can happen is that we
1685 * have some link structures left over
1686 */
1687 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001689 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690
Tejun Heo985ed672014-03-19 10:23:53 -04001691 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001692 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001693 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694
Tejun Heo2bd59d42014-02-11 11:52:49 -05001695 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1696 KERNFS_ROOT_CREATE_DEACTIVATED,
1697 root_cgrp);
1698 if (IS_ERR(root->kf_root)) {
1699 ret = PTR_ERR(root->kf_root);
1700 goto exit_root_id;
1701 }
1702 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703
Tejun Heoa14c6872014-07-15 11:05:09 -04001704 if (root == &cgrp_dfl_root)
1705 base_files = cgroup_dfl_base_files;
1706 else
1707 base_files = cgroup_legacy_base_files;
1708
1709 ret = cgroup_addrm_files(root_cgrp, base_files, true);
Tejun Heod427dfe2014-02-11 11:52:48 -05001710 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001711 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712
Tejun Heo5df36032014-03-19 10:23:54 -04001713 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001714 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001715 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001716
Tejun Heod427dfe2014-02-11 11:52:48 -05001717 /*
1718 * There must be no failure case after here, since rebinding takes
1719 * care of subsystems' refcounts, which are explicitly dropped in
1720 * the failure exit path.
1721 */
1722 list_add(&root->root_list, &cgroup_roots);
1723 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724
Tejun Heod427dfe2014-02-11 11:52:48 -05001725 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001726 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001727 * objects.
1728 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001729 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001730 hash_for_each(css_set_table, i, cset, hlist)
1731 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001732 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733
Tejun Heod5c419b2014-05-16 13:22:48 -04001734 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001735 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001736
Tejun Heo2bd59d42014-02-11 11:52:49 -05001737 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001738 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001739 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001740
Tejun Heo2bd59d42014-02-11 11:52:49 -05001741destroy_root:
1742 kernfs_destroy_root(root->kf_root);
1743 root->kf_root = NULL;
1744exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001745 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001746cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001747 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001748out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001749 free_cgrp_cset_links(&tmp_links);
1750 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751}
1752
Al Virof7e83572010-07-26 13:23:11 +04001753static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001755 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756{
Li Zefan3a32bd72014-06-30 11:50:59 +08001757 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001758 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001759 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001761 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001762 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001763 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001764 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001765
1766 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001767 * The first time anyone tries to mount a cgroup, enable the list
1768 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001769 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001770 if (!use_task_css_set_links)
1771 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001772
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001773 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001778 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001779
Tejun Heo2bd59d42014-02-11 11:52:49 -05001780 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001781 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04001782 cgrp_dfl_root_visible = true;
1783 root = &cgrp_dfl_root;
1784 cgroup_get(&root->cgrp);
1785 ret = 0;
1786 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 }
1788
Li Zefan970317a2014-06-30 11:49:58 +08001789 /*
1790 * Destruction of cgroup root is asynchronous, so subsystems may
1791 * still be dying after the previous unmount. Let's drain the
1792 * dying subsystems. We just need to ensure that the ones
1793 * unmounted previously finish dying and don't care about new ones
1794 * starting. Testing ref liveliness is good enough.
1795 */
1796 for_each_subsys(ss, i) {
1797 if (!(opts.subsys_mask & (1 << i)) ||
1798 ss->root == &cgrp_dfl_root)
1799 continue;
1800
1801 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1802 mutex_unlock(&cgroup_mutex);
1803 msleep(10);
1804 ret = restart_syscall();
1805 goto out_free;
1806 }
1807 cgroup_put(&ss->root->cgrp);
1808 }
1809
Tejun Heo985ed672014-03-19 10:23:53 -04001810 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001811 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001813 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001814 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001815
Paul Menage817929e2007-10-18 23:39:36 -07001816 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001817 * If we asked for a name then it must match. Also, if
1818 * name matches but sybsys_mask doesn't, we should fail.
1819 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001820 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001821 if (opts.name) {
1822 if (strcmp(opts.name, root->name))
1823 continue;
1824 name_match = true;
1825 }
Tejun Heo31261212013-06-28 17:07:30 -07001826
1827 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001828 * If we asked for subsystems (or explicitly for no
1829 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001830 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001831 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001832 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001833 if (!name_match)
1834 continue;
1835 ret = -EBUSY;
1836 goto out_unlock;
1837 }
Tejun Heo873fe092013-04-14 20:15:26 -07001838
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001839 if (root->flags ^ opts.flags)
1840 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05001841
Tejun Heo776f02f2014-02-12 09:29:50 -05001842 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08001843 * We want to reuse @root whose lifetime is governed by its
1844 * ->cgrp. Let's check whether @root is alive and keep it
1845 * that way. As cgroup_kill_sb() can happen anytime, we
1846 * want to block it by pinning the sb so that @root doesn't
1847 * get killed before mount is complete.
1848 *
1849 * With the sb pinned, tryget_live can reliably indicate
1850 * whether @root can be reused. If it's being killed,
1851 * drain it. We can use wait_queue for the wait but this
1852 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001853 */
Li Zefan3a32bd72014-06-30 11:50:59 +08001854 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1855 if (IS_ERR(pinned_sb) ||
1856 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001857 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08001858 if (!IS_ERR_OR_NULL(pinned_sb))
1859 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05001860 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001861 ret = restart_syscall();
1862 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001863 }
1864
1865 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001866 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001867 }
1868
Tejun Heo172a2c062014-03-19 10:23:53 -04001869 /*
1870 * No such thing, create a new one. name= matching without subsys
1871 * specification is allowed for already existing hierarchies but we
1872 * can't create new one without subsys specification.
1873 */
1874 if (!opts.subsys_mask && !opts.none) {
1875 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001876 goto out_unlock;
1877 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001878
Tejun Heo172a2c062014-03-19 10:23:53 -04001879 root = kzalloc(sizeof(*root), GFP_KERNEL);
1880 if (!root) {
1881 ret = -ENOMEM;
1882 goto out_unlock;
1883 }
1884
1885 init_cgroup_root(root, &opts);
1886
Tejun Heo35585572014-02-13 06:58:38 -05001887 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001888 if (ret)
1889 cgroup_free_root(root);
1890
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001891out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001892 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001893out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001894 kfree(opts.release_agent);
1895 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001896
Tejun Heo2bd59d42014-02-11 11:52:49 -05001897 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001898 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001899
Jianyu Zhanc9482a52014-04-26 15:40:28 +08001900 dentry = kernfs_mount(fs_type, flags, root->kf_root,
1901 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001902 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001903 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08001904
1905 /*
1906 * If @pinned_sb, we're reusing an existing root and holding an
1907 * extra ref on its sb. Mount is complete. Put the extra ref.
1908 */
1909 if (pinned_sb) {
1910 WARN_ON(new_sb);
1911 deactivate_super(pinned_sb);
1912 }
1913
Tejun Heo2bd59d42014-02-11 11:52:49 -05001914 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001915}
1916
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001917static void cgroup_kill_sb(struct super_block *sb)
1918{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001919 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001920 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001921
Tejun Heo9d755d32014-05-14 09:15:02 -04001922 /*
1923 * If @root doesn't have any mounts or children, start killing it.
1924 * This prevents new mounts by disabling percpu_ref_tryget_live().
1925 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08001926 *
1927 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04001928 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05001929 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08001930 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04001931 cgroup_put(&root->cgrp);
1932 else
1933 percpu_ref_kill(&root->cgrp.self.refcnt);
1934
Tejun Heo2bd59d42014-02-11 11:52:49 -05001935 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001936}
1937
1938static struct file_system_type cgroup_fs_type = {
1939 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001940 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001941 .kill_sb = cgroup_kill_sb,
1942};
1943
Li Zefana043e3b2008-02-23 15:24:09 -08001944/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001945 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001946 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001947 * @buf: the buffer to write the path into
1948 * @buflen: the length of the buffer
1949 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001950 * Determine @task's cgroup on the first (the one with the lowest non-zero
1951 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1952 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1953 * cgroup controller callbacks.
1954 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001955 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001956 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001957char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001958{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001959 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001960 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001961 int hierarchy_id = 1;
1962 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001963
1964 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001965 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001966
Tejun Heo913ffdb2013-07-11 16:34:48 -07001967 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1968
Tejun Heo857a2be2013-04-14 20:50:08 -07001969 if (root) {
1970 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001971 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001972 } else {
1973 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001974 if (strlcpy(buf, "/", buflen) < buflen)
1975 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001976 }
1977
Tejun Heo96d365e2014-02-13 06:58:40 -05001978 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001979 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001980 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001981}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001982EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001983
Tejun Heob3dc0942014-02-25 10:04:01 -05001984/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001985struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001986 /* the src and dst cset list running through cset->mg_node */
1987 struct list_head src_csets;
1988 struct list_head dst_csets;
1989
1990 /*
1991 * Fields for cgroup_taskset_*() iteration.
1992 *
1993 * Before migration is committed, the target migration tasks are on
1994 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1995 * the csets on ->dst_csets. ->csets point to either ->src_csets
1996 * or ->dst_csets depending on whether migration is committed.
1997 *
1998 * ->cur_csets and ->cur_task point to the current task position
1999 * during iteration.
2000 */
2001 struct list_head *csets;
2002 struct css_set *cur_cset;
2003 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002004};
2005
2006/**
2007 * cgroup_taskset_first - reset taskset and return the first task
2008 * @tset: taskset of interest
2009 *
2010 * @tset iteration is initialized and the first task is returned.
2011 */
2012struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2013{
Tejun Heob3dc0942014-02-25 10:04:01 -05002014 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2015 tset->cur_task = NULL;
2016
2017 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002018}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002019
2020/**
2021 * cgroup_taskset_next - iterate to the next task in taskset
2022 * @tset: taskset of interest
2023 *
2024 * Return the next task in @tset. Iteration must have been initialized
2025 * with cgroup_taskset_first().
2026 */
2027struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2028{
Tejun Heob3dc0942014-02-25 10:04:01 -05002029 struct css_set *cset = tset->cur_cset;
2030 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002031
Tejun Heob3dc0942014-02-25 10:04:01 -05002032 while (&cset->mg_node != tset->csets) {
2033 if (!task)
2034 task = list_first_entry(&cset->mg_tasks,
2035 struct task_struct, cg_list);
2036 else
2037 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002038
Tejun Heob3dc0942014-02-25 10:04:01 -05002039 if (&task->cg_list != &cset->mg_tasks) {
2040 tset->cur_cset = cset;
2041 tset->cur_task = task;
2042 return task;
2043 }
2044
2045 cset = list_next_entry(cset, mg_node);
2046 task = NULL;
2047 }
2048
2049 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002050}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002051
2052/**
Ben Blum74a11662011-05-26 16:25:20 -07002053 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02002054 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002055 * @tsk: the task being migrated
2056 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07002057 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002058 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07002059 */
Tejun Heo5abb8852013-06-12 21:04:49 -07002060static void cgroup_task_migrate(struct cgroup *old_cgrp,
2061 struct task_struct *tsk,
2062 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07002063{
Tejun Heo5abb8852013-06-12 21:04:49 -07002064 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07002065
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002066 lockdep_assert_held(&cgroup_mutex);
2067 lockdep_assert_held(&css_set_rwsem);
2068
Ben Blum74a11662011-05-26 16:25:20 -07002069 /*
Tejun Heod59cfc02015-05-13 16:35:17 -04002070 * We are synchronized through cgroup_threadgroup_rwsem against
2071 * PF_EXITING setting such that we can't race against cgroup_exit()
2072 * changing the css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07002073 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01002074 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002075 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002076
Tejun Heob3dc0942014-02-25 10:04:01 -05002077 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07002078 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002079
Tejun Heo1b9aba42014-03-19 17:43:21 -04002080 /*
2081 * Use move_tail so that cgroup_taskset_first() still returns the
2082 * leader after migration. This works because cgroup_migrate()
2083 * ensures that the dst_cset of the leader is the first on the
2084 * tset's dst_csets list.
2085 */
2086 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07002087
2088 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07002089 * We just gained a reference on old_cset by taking it from the
2090 * task. As trading it for new_cset is protected by cgroup_mutex,
2091 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07002092 */
Zefan Lia25eb522014-09-19 16:51:00 +08002093 put_css_set_locked(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002094}
2095
Li Zefana043e3b2008-02-23 15:24:09 -08002096/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002097 * cgroup_migrate_finish - cleanup after attach
2098 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002099 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002100 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2101 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002102 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002103static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002104{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002105 struct css_set *cset, *tmp_cset;
2106
2107 lockdep_assert_held(&cgroup_mutex);
2108
2109 down_write(&css_set_rwsem);
2110 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2111 cset->mg_src_cgrp = NULL;
2112 cset->mg_dst_cset = NULL;
2113 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002114 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002115 }
2116 up_write(&css_set_rwsem);
2117}
2118
2119/**
2120 * cgroup_migrate_add_src - add a migration source css_set
2121 * @src_cset: the source css_set to add
2122 * @dst_cgrp: the destination cgroup
2123 * @preloaded_csets: list of preloaded css_sets
2124 *
2125 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2126 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2127 * up by cgroup_migrate_finish().
2128 *
Tejun Heod59cfc02015-05-13 16:35:17 -04002129 * This function may be called without holding cgroup_threadgroup_rwsem
2130 * even if the target is a process. Threads may be created and destroyed
2131 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2132 * into play and the preloaded css_sets are guaranteed to cover all
2133 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002134 */
2135static void cgroup_migrate_add_src(struct css_set *src_cset,
2136 struct cgroup *dst_cgrp,
2137 struct list_head *preloaded_csets)
2138{
2139 struct cgroup *src_cgrp;
2140
2141 lockdep_assert_held(&cgroup_mutex);
2142 lockdep_assert_held(&css_set_rwsem);
2143
2144 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2145
Tejun Heo1958d2d2014-02-25 10:04:03 -05002146 if (!list_empty(&src_cset->mg_preload_node))
2147 return;
2148
2149 WARN_ON(src_cset->mg_src_cgrp);
2150 WARN_ON(!list_empty(&src_cset->mg_tasks));
2151 WARN_ON(!list_empty(&src_cset->mg_node));
2152
2153 src_cset->mg_src_cgrp = src_cgrp;
2154 get_css_set(src_cset);
2155 list_add(&src_cset->mg_preload_node, preloaded_csets);
2156}
2157
2158/**
2159 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002160 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002161 * @preloaded_csets: list of preloaded source css_sets
2162 *
2163 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2164 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002165 * pins all destination css_sets, links each to its source, and append them
2166 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2167 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002168 *
2169 * This function must be called after cgroup_migrate_add_src() has been
2170 * called on each migration source css_set. After migration is performed
2171 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2172 * @preloaded_csets.
2173 */
2174static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2175 struct list_head *preloaded_csets)
2176{
2177 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002178 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002179
2180 lockdep_assert_held(&cgroup_mutex);
2181
Tejun Heof8f22e52014-04-23 11:13:16 -04002182 /*
2183 * Except for the root, child_subsys_mask must be zero for a cgroup
2184 * with tasks so that child cgroups don't compete against tasks.
2185 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002186 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002187 dst_cgrp->child_subsys_mask)
2188 return -EBUSY;
2189
Tejun Heo1958d2d2014-02-25 10:04:03 -05002190 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002191 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002192 struct css_set *dst_cset;
2193
Tejun Heof817de92014-04-23 11:13:16 -04002194 dst_cset = find_css_set(src_cset,
2195 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002196 if (!dst_cset)
2197 goto err;
2198
2199 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002200
2201 /*
2202 * If src cset equals dst, it's noop. Drop the src.
2203 * cgroup_migrate() will skip the cset too. Note that we
2204 * can't handle src == dst as some nodes are used by both.
2205 */
2206 if (src_cset == dst_cset) {
2207 src_cset->mg_src_cgrp = NULL;
2208 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002209 put_css_set(src_cset);
2210 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002211 continue;
2212 }
2213
Tejun Heo1958d2d2014-02-25 10:04:03 -05002214 src_cset->mg_dst_cset = dst_cset;
2215
2216 if (list_empty(&dst_cset->mg_preload_node))
2217 list_add(&dst_cset->mg_preload_node, &csets);
2218 else
Zefan Lia25eb522014-09-19 16:51:00 +08002219 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002220 }
2221
Tejun Heof817de92014-04-23 11:13:16 -04002222 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002223 return 0;
2224err:
2225 cgroup_migrate_finish(&csets);
2226 return -ENOMEM;
2227}
2228
2229/**
2230 * cgroup_migrate - migrate a process or task to a cgroup
2231 * @cgrp: the destination cgroup
2232 * @leader: the leader of the process or the task to migrate
2233 * @threadgroup: whether @leader points to the whole process or a single task
2234 *
2235 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heod59cfc02015-05-13 16:35:17 -04002236 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002237 * caller is also responsible for invoking cgroup_migrate_add_src() and
2238 * cgroup_migrate_prepare_dst() on the targets before invoking this
2239 * function and following up with cgroup_migrate_finish().
2240 *
2241 * As long as a controller's ->can_attach() doesn't fail, this function is
2242 * guaranteed to succeed. This means that, excluding ->can_attach()
2243 * failure, when migrating multiple targets, the success or failure can be
2244 * decided for all targets by invoking group_migrate_prepare_dst() before
2245 * actually starting migrating.
2246 */
2247static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2248 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002249{
Tejun Heob3dc0942014-02-25 10:04:01 -05002250 struct cgroup_taskset tset = {
2251 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2252 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2253 .csets = &tset.src_csets,
2254 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002255 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002256 struct css_set *cset, *tmp_cset;
2257 struct task_struct *task, *tmp_task;
2258 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002259
2260 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002261 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2262 * already PF_EXITING could be freed from underneath us unless we
2263 * take an rcu_read_lock.
2264 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002265 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002266 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002267 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002268 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002269 /* @task either already exited or can't exit until the end */
2270 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002271 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002272
Tejun Heoeaf797a2014-02-25 10:04:03 -05002273 /* leave @task alone if post_fork() hasn't linked it yet */
2274 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002275 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002276
Tejun Heob3dc0942014-02-25 10:04:01 -05002277 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002278 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002279 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002280
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002281 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002282 * cgroup_taskset_first() must always return the leader.
2283 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002284 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002285 list_move_tail(&task->cg_list, &cset->mg_tasks);
2286 if (list_empty(&cset->mg_node))
2287 list_add_tail(&cset->mg_node, &tset.src_csets);
2288 if (list_empty(&cset->mg_dst_cset->mg_node))
2289 list_move_tail(&cset->mg_dst_cset->mg_node,
2290 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002291 next:
Li Zefan081aa452013-03-13 09:17:09 +08002292 if (!threadgroup)
2293 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002294 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002295 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002296 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002297
Tejun Heo134d3372011-12-12 18:12:21 -08002298 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002299 if (list_empty(&tset.src_csets))
2300 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002301
Tejun Heo1958d2d2014-02-25 10:04:03 -05002302 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002303 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002304 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002305 ret = css->ss->can_attach(css, &tset);
2306 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002307 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002308 goto out_cancel_attach;
2309 }
2310 }
Ben Blum74a11662011-05-26 16:25:20 -07002311 }
2312
2313 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002314 * Now that we're guaranteed success, proceed to move all tasks to
2315 * the new cgroup. There are no failure cases after here, so this
2316 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002317 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002318 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002319 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2320 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2321 cgroup_task_migrate(cset->mg_src_cgrp, task,
2322 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002323 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002324 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002325
2326 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002327 * Migration is committed, all target tasks are now on dst_csets.
2328 * Nothing is sensitive to fork() after this point. Notify
2329 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002330 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002331 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002332
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002333 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002334 if (css->ss->attach)
2335 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002336
Tejun Heo9db8de32014-02-13 06:58:43 -05002337 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002338 goto out_release_tset;
2339
Ben Blum74a11662011-05-26 16:25:20 -07002340out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002341 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002342 if (css == failed_css)
2343 break;
2344 if (css->ss->cancel_attach)
2345 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002346 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002347out_release_tset:
2348 down_write(&css_set_rwsem);
2349 list_splice_init(&tset.dst_csets, &tset.src_csets);
2350 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002351 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002352 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002353 }
2354 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002355 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002356}
2357
Tejun Heo1958d2d2014-02-25 10:04:03 -05002358/**
2359 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2360 * @dst_cgrp: the cgroup to attach to
2361 * @leader: the task or the leader of the threadgroup to be attached
2362 * @threadgroup: attach the whole threadgroup?
2363 *
Tejun Heod59cfc02015-05-13 16:35:17 -04002364 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002365 */
2366static int cgroup_attach_task(struct cgroup *dst_cgrp,
2367 struct task_struct *leader, bool threadgroup)
2368{
2369 LIST_HEAD(preloaded_csets);
2370 struct task_struct *task;
2371 int ret;
2372
2373 /* look up all src csets */
2374 down_read(&css_set_rwsem);
2375 rcu_read_lock();
2376 task = leader;
2377 do {
2378 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2379 &preloaded_csets);
2380 if (!threadgroup)
2381 break;
2382 } while_each_thread(leader, task);
2383 rcu_read_unlock();
2384 up_read(&css_set_rwsem);
2385
2386 /* prepare dst csets and commit */
2387 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2388 if (!ret)
2389 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2390
2391 cgroup_migrate_finish(&preloaded_csets);
2392 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002393}
2394
Tejun Heo187fe842015-06-18 16:54:28 -04002395static int cgroup_procs_write_permission(struct task_struct *task,
2396 struct cgroup *dst_cgrp,
2397 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002398{
2399 const struct cred *cred = current_cred();
2400 const struct cred *tcred = get_task_cred(task);
2401 int ret = 0;
2402
2403 /*
2404 * even if we're attaching all tasks in the thread group, we only
2405 * need to check permissions on one of them.
2406 */
2407 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2408 !uid_eq(cred->euid, tcred->uid) &&
2409 !uid_eq(cred->euid, tcred->suid))
2410 ret = -EACCES;
2411
Tejun Heo187fe842015-06-18 16:54:28 -04002412 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2413 struct super_block *sb = of->file->f_path.dentry->d_sb;
2414 struct cgroup *cgrp;
2415 struct inode *inode;
2416
2417 down_read(&css_set_rwsem);
2418 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
2419 up_read(&css_set_rwsem);
2420
2421 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2422 cgrp = cgroup_parent(cgrp);
2423
2424 ret = -ENOMEM;
2425 inode = kernfs_get_inode(sb, cgrp->procs_kn);
2426 if (inode) {
2427 ret = inode_permission(inode, MAY_WRITE);
2428 iput(inode);
2429 }
2430 }
2431
Tejun Heodedf22e2015-06-18 16:54:28 -04002432 put_cred(tcred);
2433 return ret;
2434}
2435
Ben Blum74a11662011-05-26 16:25:20 -07002436/*
2437 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002438 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002439 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002440 */
Tejun Heoacbef752014-05-13 12:16:22 -04002441static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2442 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002443{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002444 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002445 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002446 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002447 int ret;
2448
Tejun Heoacbef752014-05-13 12:16:22 -04002449 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2450 return -EINVAL;
2451
Tejun Heoe76ecae2014-05-13 12:19:23 -04002452 cgrp = cgroup_kn_lock_live(of->kn);
2453 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002454 return -ENODEV;
2455
Tejun Heob5ba75b2015-05-13 16:35:18 -04002456 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002457 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002458 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002459 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002460 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002461 ret = -ESRCH;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002462 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002463 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002464 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002465 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002466 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002467
2468 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002469 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002470
2471 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002472 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002473 * trapped in a cpuset, or RT worker may be born in a cgroup
2474 * with no rt_runtime allocated. Just say no.
2475 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002476 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002477 ret = -EINVAL;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002478 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002479 }
2480
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002481 get_task_struct(tsk);
2482 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002483
Tejun Heo187fe842015-06-18 16:54:28 -04002484 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002485 if (!ret)
2486 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002487
Paul Menagebbcb81d2007-10-18 23:39:32 -07002488 put_task_struct(tsk);
Tejun Heob5ba75b2015-05-13 16:35:18 -04002489 goto out_unlock_threadgroup;
2490
2491out_unlock_rcu:
2492 rcu_read_unlock();
2493out_unlock_threadgroup:
2494 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002495 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002496 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002497}
2498
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002499/**
2500 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2501 * @from: attach to all cgroups of a given task
2502 * @tsk: the task to be attached
2503 */
2504int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2505{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002506 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002507 int retval = 0;
2508
Tejun Heo47cfcd02013-04-07 09:29:51 -07002509 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002510 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002511 struct cgroup *from_cgrp;
2512
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002513 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002514 continue;
2515
Tejun Heo96d365e2014-02-13 06:58:40 -05002516 down_read(&css_set_rwsem);
2517 from_cgrp = task_cgroup_from_root(from, root);
2518 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002519
Li Zefan6f4b7e62013-07-31 16:18:36 +08002520 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002521 if (retval)
2522 break;
2523 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002524 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002525
2526 return retval;
2527}
2528EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2529
Tejun Heoacbef752014-05-13 12:16:22 -04002530static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2531 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002532{
Tejun Heoacbef752014-05-13 12:16:22 -04002533 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002534}
2535
Tejun Heoacbef752014-05-13 12:16:22 -04002536static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2537 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002538{
Tejun Heoacbef752014-05-13 12:16:22 -04002539 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002540}
2541
Tejun Heo451af502014-05-13 12:16:21 -04002542static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2543 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002544{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002545 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002546
Tejun Heoe76ecae2014-05-13 12:19:23 -04002547 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2548
2549 cgrp = cgroup_kn_lock_live(of->kn);
2550 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002551 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002552 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002553 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2554 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002555 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002556 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002557 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002558}
2559
Tejun Heo2da8ca82013-12-05 12:28:04 -05002560static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002561{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002562 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002563
Tejun Heo46cfeb02014-05-13 12:11:00 -04002564 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002565 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002566 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002567 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002568 return 0;
2569}
2570
Tejun Heo2da8ca82013-12-05 12:28:04 -05002571static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002572{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002573 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002574 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002575}
2576
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002577static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002578{
2579 struct cgroup_subsys *ss;
2580 bool printed = false;
2581 int ssid;
2582
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002583 for_each_subsys_which(ss, ssid, &ss_mask) {
2584 if (printed)
2585 seq_putc(seq, ' ');
2586 seq_printf(seq, "%s", ss->name);
2587 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002588 }
2589 if (printed)
2590 seq_putc(seq, '\n');
2591}
2592
2593/* show controllers which are currently attached to the default hierarchy */
2594static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2595{
2596 struct cgroup *cgrp = seq_css(seq)->cgroup;
2597
Tejun Heo5533e012014-05-14 19:33:07 -04002598 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2599 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002600 return 0;
2601}
2602
2603/* show controllers which are enabled from the parent */
2604static int cgroup_controllers_show(struct seq_file *seq, void *v)
2605{
2606 struct cgroup *cgrp = seq_css(seq)->cgroup;
2607
Tejun Heo667c2492014-07-08 18:02:56 -04002608 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002609 return 0;
2610}
2611
2612/* show controllers which are enabled for a given cgroup's children */
2613static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2614{
2615 struct cgroup *cgrp = seq_css(seq)->cgroup;
2616
Tejun Heo667c2492014-07-08 18:02:56 -04002617 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002618 return 0;
2619}
2620
2621/**
2622 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2623 * @cgrp: root of the subtree to update csses for
2624 *
2625 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2626 * css associations need to be updated accordingly. This function looks up
2627 * all css_sets which are attached to the subtree, creates the matching
2628 * updated css_sets and migrates the tasks to the new ones.
2629 */
2630static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2631{
2632 LIST_HEAD(preloaded_csets);
2633 struct cgroup_subsys_state *css;
2634 struct css_set *src_cset;
2635 int ret;
2636
Tejun Heof8f22e52014-04-23 11:13:16 -04002637 lockdep_assert_held(&cgroup_mutex);
2638
Tejun Heob5ba75b2015-05-13 16:35:18 -04002639 percpu_down_write(&cgroup_threadgroup_rwsem);
2640
Tejun Heof8f22e52014-04-23 11:13:16 -04002641 /* look up all csses currently attached to @cgrp's subtree */
2642 down_read(&css_set_rwsem);
2643 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2644 struct cgrp_cset_link *link;
2645
2646 /* self is not affected by child_subsys_mask change */
2647 if (css->cgroup == cgrp)
2648 continue;
2649
2650 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2651 cgroup_migrate_add_src(link->cset, cgrp,
2652 &preloaded_csets);
2653 }
2654 up_read(&css_set_rwsem);
2655
2656 /* NULL dst indicates self on default hierarchy */
2657 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2658 if (ret)
2659 goto out_finish;
2660
2661 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2662 struct task_struct *last_task = NULL, *task;
2663
2664 /* src_csets precede dst_csets, break on the first dst_cset */
2665 if (!src_cset->mg_src_cgrp)
2666 break;
2667
2668 /*
2669 * All tasks in src_cset need to be migrated to the
2670 * matching dst_cset. Empty it process by process. We
2671 * walk tasks but migrate processes. The leader might even
2672 * belong to a different cset but such src_cset would also
2673 * be among the target src_csets because the default
2674 * hierarchy enforces per-process membership.
2675 */
2676 while (true) {
2677 down_read(&css_set_rwsem);
2678 task = list_first_entry_or_null(&src_cset->tasks,
2679 struct task_struct, cg_list);
2680 if (task) {
2681 task = task->group_leader;
2682 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2683 get_task_struct(task);
2684 }
2685 up_read(&css_set_rwsem);
2686
2687 if (!task)
2688 break;
2689
2690 /* guard against possible infinite loop */
2691 if (WARN(last_task == task,
2692 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2693 goto out_finish;
2694 last_task = task;
2695
Tejun Heof8f22e52014-04-23 11:13:16 -04002696 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2697
Tejun Heof8f22e52014-04-23 11:13:16 -04002698 put_task_struct(task);
2699
2700 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2701 goto out_finish;
2702 }
2703 }
2704
2705out_finish:
2706 cgroup_migrate_finish(&preloaded_csets);
Tejun Heob5ba75b2015-05-13 16:35:18 -04002707 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002708 return ret;
2709}
2710
2711/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002712static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2713 char *buf, size_t nbytes,
2714 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002715{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002716 unsigned long enable = 0, disable = 0;
2717 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002718 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002719 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002720 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002721 int ssid, ret;
2722
2723 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002724 * Parse input - space separated list of subsystem names prefixed
2725 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002726 */
Tejun Heo451af502014-05-13 12:16:21 -04002727 buf = strstrip(buf);
2728 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002729 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2730
Tejun Heod37167a2014-05-13 12:10:59 -04002731 if (tok[0] == '\0')
2732 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002733 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
2734 if (ss->disabled || strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002735 continue;
2736
2737 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002738 enable |= 1 << ssid;
2739 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002740 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002741 disable |= 1 << ssid;
2742 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002743 } else {
2744 return -EINVAL;
2745 }
2746 break;
2747 }
2748 if (ssid == CGROUP_SUBSYS_COUNT)
2749 return -EINVAL;
2750 }
2751
Tejun Heoa9746d82014-05-13 12:19:22 -04002752 cgrp = cgroup_kn_lock_live(of->kn);
2753 if (!cgrp)
2754 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002755
2756 for_each_subsys(ss, ssid) {
2757 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002758 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002759 enable &= ~(1 << ssid);
2760 continue;
2761 }
2762
Tejun Heoc29adf22014-07-08 18:02:56 -04002763 /* unavailable or not enabled on the parent? */
2764 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2765 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002766 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002767 ret = -ENOENT;
2768 goto out_unlock;
2769 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002770 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002771 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002772 disable &= ~(1 << ssid);
2773 continue;
2774 }
2775
2776 /* a child has it enabled? */
2777 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002778 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002779 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002780 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002781 }
2782 }
2783 }
2784 }
2785
2786 if (!enable && !disable) {
2787 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002788 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002789 }
2790
2791 /*
Tejun Heo667c2492014-07-08 18:02:56 -04002792 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002793 * with tasks so that child cgroups don't compete against tasks.
2794 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002795 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002796 ret = -EBUSY;
2797 goto out_unlock;
2798 }
2799
2800 /*
Tejun Heof63070d2014-07-08 18:02:57 -04002801 * Update subsys masks and calculate what needs to be done. More
2802 * subsystems than specified may need to be enabled or disabled
2803 * depending on subsystem dependencies.
2804 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05002805 old_sc = cgrp->subtree_control;
2806 old_ss = cgrp->child_subsys_mask;
2807 new_sc = (old_sc | enable) & ~disable;
2808 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04002809
Tejun Heo755bf5e2014-11-18 02:49:50 -05002810 css_enable = ~old_ss & new_ss;
2811 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04002812 enable |= css_enable;
2813 disable |= css_disable;
2814
Tejun Heodb6e3052014-11-18 02:49:51 -05002815 /*
2816 * Because css offlining is asynchronous, userland might try to
2817 * re-enable the same controller while the previous instance is
2818 * still around. In such cases, wait till it's gone using
2819 * offline_waitq.
2820 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002821 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05002822 cgroup_for_each_live_child(child, cgrp) {
2823 DEFINE_WAIT(wait);
2824
2825 if (!cgroup_css(child, ss))
2826 continue;
2827
2828 cgroup_get(child);
2829 prepare_to_wait(&child->offline_waitq, &wait,
2830 TASK_UNINTERRUPTIBLE);
2831 cgroup_kn_unlock(of->kn);
2832 schedule();
2833 finish_wait(&child->offline_waitq, &wait);
2834 cgroup_put(child);
2835
2836 return restart_syscall();
2837 }
2838 }
2839
Tejun Heo755bf5e2014-11-18 02:49:50 -05002840 cgrp->subtree_control = new_sc;
2841 cgrp->child_subsys_mask = new_ss;
2842
Tejun Heof63070d2014-07-08 18:02:57 -04002843 /*
2844 * Create new csses or make the existing ones visible. A css is
2845 * created invisible if it's being implicitly enabled through
2846 * dependency. An invisible css is made visible when the userland
2847 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04002848 */
2849 for_each_subsys(ss, ssid) {
2850 if (!(enable & (1 << ssid)))
2851 continue;
2852
2853 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04002854 if (css_enable & (1 << ssid))
2855 ret = create_css(child, ss,
2856 cgrp->subtree_control & (1 << ssid));
2857 else
2858 ret = cgroup_populate_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002859 if (ret)
2860 goto err_undo_css;
2861 }
2862 }
2863
Tejun Heoc29adf22014-07-08 18:02:56 -04002864 /*
2865 * At this point, cgroup_e_css() results reflect the new csses
2866 * making the following cgroup_update_dfl_csses() properly update
2867 * css associations of all tasks in the subtree.
2868 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002869 ret = cgroup_update_dfl_csses(cgrp);
2870 if (ret)
2871 goto err_undo_css;
2872
Tejun Heof63070d2014-07-08 18:02:57 -04002873 /*
2874 * All tasks are migrated out of disabled csses. Kill or hide
2875 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002876 * disabled while other subsystems are still depending on it. The
2877 * css must not actively control resources and be in the vanilla
2878 * state if it's made visible again later. Controllers which may
2879 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04002880 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002881 for_each_subsys(ss, ssid) {
2882 if (!(disable & (1 << ssid)))
2883 continue;
2884
Tejun Heof63070d2014-07-08 18:02:57 -04002885 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002886 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2887
2888 if (css_disable & (1 << ssid)) {
2889 kill_css(css);
2890 } else {
Tejun Heof63070d2014-07-08 18:02:57 -04002891 cgroup_clear_dir(child, 1 << ssid);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002892 if (ss->css_reset)
2893 ss->css_reset(css);
2894 }
Tejun Heof63070d2014-07-08 18:02:57 -04002895 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002896 }
2897
Tejun Heo56c807b2014-11-18 02:49:51 -05002898 /*
2899 * The effective csses of all the descendants (excluding @cgrp) may
2900 * have changed. Subsystems can optionally subscribe to this event
2901 * by implementing ->css_e_css_changed() which is invoked if any of
2902 * the effective csses seen from the css's cgroup may have changed.
2903 */
2904 for_each_subsys(ss, ssid) {
2905 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
2906 struct cgroup_subsys_state *css;
2907
2908 if (!ss->css_e_css_changed || !this_css)
2909 continue;
2910
2911 css_for_each_descendant_pre(css, this_css)
2912 if (css != this_css)
2913 ss->css_e_css_changed(css);
2914 }
2915
Tejun Heof8f22e52014-04-23 11:13:16 -04002916 kernfs_activate(cgrp->kn);
2917 ret = 0;
2918out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002919 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002920 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002921
2922err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05002923 cgrp->subtree_control = old_sc;
2924 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04002925
2926 for_each_subsys(ss, ssid) {
2927 if (!(enable & (1 << ssid)))
2928 continue;
2929
2930 cgroup_for_each_live_child(child, cgrp) {
2931 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04002932
2933 if (!css)
2934 continue;
2935
2936 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04002937 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04002938 else
2939 cgroup_clear_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002940 }
2941 }
2942 goto out_unlock;
2943}
2944
Tejun Heo842b5972014-04-25 18:28:02 -04002945static int cgroup_populated_show(struct seq_file *seq, void *v)
2946{
2947 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2948 return 0;
2949}
2950
Tejun Heo2bd59d42014-02-11 11:52:49 -05002951static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2952 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002953{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002954 struct cgroup *cgrp = of->kn->parent->priv;
2955 struct cftype *cft = of->kn->priv;
2956 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002957 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002958
Tejun Heob4168642014-05-13 12:16:21 -04002959 if (cft->write)
2960 return cft->write(of, buf, nbytes, off);
2961
Tejun Heo2bd59d42014-02-11 11:52:49 -05002962 /*
2963 * kernfs guarantees that a file isn't deleted with operations in
2964 * flight, which means that the matching css is and stays alive and
2965 * doesn't need to be pinned. The RCU locking is not necessary
2966 * either. It's just for the convenience of using cgroup_css().
2967 */
2968 rcu_read_lock();
2969 css = cgroup_css(cgrp, cft->ss);
2970 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002971
Tejun Heo451af502014-05-13 12:16:21 -04002972 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002973 unsigned long long v;
2974 ret = kstrtoull(buf, 0, &v);
2975 if (!ret)
2976 ret = cft->write_u64(css, cft, v);
2977 } else if (cft->write_s64) {
2978 long long v;
2979 ret = kstrtoll(buf, 0, &v);
2980 if (!ret)
2981 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002982 } else {
2983 ret = -EINVAL;
2984 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002985
Tejun Heoa742c592013-12-05 12:28:03 -05002986 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002987}
2988
Tejun Heo6612f052013-12-05 12:28:04 -05002989static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002990{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002991 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002992}
2993
2994static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2995{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002996 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002997}
2998
2999static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3000{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003001 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003002}
3003
3004static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3005{
Tejun Heo7da11272013-12-05 12:28:04 -05003006 struct cftype *cft = seq_cft(m);
3007 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003008
Tejun Heo2da8ca82013-12-05 12:28:04 -05003009 if (cft->seq_show)
3010 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003011
Tejun Heo896f5192013-12-05 12:28:04 -05003012 if (cft->read_u64)
3013 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3014 else if (cft->read_s64)
3015 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3016 else
3017 return -EINVAL;
3018 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003019}
3020
Tejun Heo2bd59d42014-02-11 11:52:49 -05003021static struct kernfs_ops cgroup_kf_single_ops = {
3022 .atomic_write_len = PAGE_SIZE,
3023 .write = cgroup_file_write,
3024 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003025};
3026
Tejun Heo2bd59d42014-02-11 11:52:49 -05003027static struct kernfs_ops cgroup_kf_ops = {
3028 .atomic_write_len = PAGE_SIZE,
3029 .write = cgroup_file_write,
3030 .seq_start = cgroup_seqfile_start,
3031 .seq_next = cgroup_seqfile_next,
3032 .seq_stop = cgroup_seqfile_stop,
3033 .seq_show = cgroup_seqfile_show,
3034};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003035
3036/*
3037 * cgroup_rename - Only allow simple rename of directories in place.
3038 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003039static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3040 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003041{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003042 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003043 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003044
Tejun Heo2bd59d42014-02-11 11:52:49 -05003045 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003046 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003047 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003048 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003049
Tejun Heo6db8e852013-06-14 11:18:22 -07003050 /*
3051 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003052 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003053 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003054 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003055 return -EPERM;
3056
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003057 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003058 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003059 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003060 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003061 */
3062 kernfs_break_active_protection(new_parent);
3063 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003064
Tejun Heo2bd59d42014-02-11 11:52:49 -05003065 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003066
Tejun Heo2bd59d42014-02-11 11:52:49 -05003067 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003068
Tejun Heo2bd59d42014-02-11 11:52:49 -05003069 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003070
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003071 kernfs_unbreak_active_protection(kn);
3072 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003073 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003074}
3075
Tejun Heo49957f82014-04-07 16:44:47 -04003076/* set uid and gid of cgroup dirs and files to that of the creator */
3077static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3078{
3079 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3080 .ia_uid = current_fsuid(),
3081 .ia_gid = current_fsgid(), };
3082
3083 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3084 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3085 return 0;
3086
3087 return kernfs_setattr(kn, &iattr);
3088}
3089
Tejun Heo2bb566c2013-08-08 20:11:23 -04003090static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003091{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003092 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003093 struct kernfs_node *kn;
3094 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003095 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003096
Tejun Heo2bd59d42014-02-11 11:52:49 -05003097#ifdef CONFIG_DEBUG_LOCK_ALLOC
3098 key = &cft->lockdep_key;
3099#endif
3100 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3101 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003102 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003103 if (IS_ERR(kn))
3104 return PTR_ERR(kn);
3105
3106 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003107 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003108 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003109 return ret;
3110 }
3111
Tejun Heo187fe842015-06-18 16:54:28 -04003112 if (cft->write == cgroup_procs_write)
3113 cgrp->procs_kn = kn;
3114 else if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04003115 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04003116 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003117}
3118
Tejun Heob1f28d32013-06-28 16:24:10 -07003119/**
3120 * cgroup_addrm_files - add or remove files to a cgroup directory
3121 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07003122 * @cfts: array of cftypes to be added
3123 * @is_add: whether to add or remove
3124 *
3125 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04003126 * For removals, this function never fails. If addition fails, this
3127 * function doesn't remove files already added. The caller is responsible
3128 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07003129 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04003130static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
3131 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003132{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003133 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07003134 int ret;
3135
Tejun Heo01f64742014-05-13 12:19:23 -04003136 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003137
3138 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003139 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003140 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003141 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003142 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003143 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003144 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003145 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003146 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003147 continue;
3148
Li Zefan2739d3c2013-01-21 18:18:33 +08003149 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003150 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003151 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003152 pr_warn("%s: failed to add %s, err=%d\n",
3153 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07003154 return ret;
3155 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003156 } else {
3157 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003158 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003159 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003160 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003161}
3162
Tejun Heo21a2d342014-02-12 09:29:49 -05003163static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003164{
3165 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003166 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003167 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003168 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003169 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003170
Tejun Heo01f64742014-05-13 12:19:23 -04003171 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003172
Li Zefane8c82d22013-06-18 18:48:37 +08003173 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003174 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003175 struct cgroup *cgrp = css->cgroup;
3176
Li Zefane8c82d22013-06-18 18:48:37 +08003177 if (cgroup_is_dead(cgrp))
3178 continue;
3179
Tejun Heo21a2d342014-02-12 09:29:49 -05003180 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003181 if (ret)
3182 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003183 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003184
3185 if (is_add && !ret)
3186 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003187 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003188}
3189
Tejun Heo2da440a2014-02-11 11:52:48 -05003190static void cgroup_exit_cftypes(struct cftype *cfts)
3191{
3192 struct cftype *cft;
3193
Tejun Heo2bd59d42014-02-11 11:52:49 -05003194 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3195 /* free copy for custom atomic_write_len, see init_cftypes() */
3196 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3197 kfree(cft->kf_ops);
3198 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003199 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003200
3201 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003202 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003203 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003204}
3205
Tejun Heo2bd59d42014-02-11 11:52:49 -05003206static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003207{
3208 struct cftype *cft;
3209
Tejun Heo2bd59d42014-02-11 11:52:49 -05003210 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3211 struct kernfs_ops *kf_ops;
3212
Tejun Heo0adb0702014-02-12 09:29:48 -05003213 WARN_ON(cft->ss || cft->kf_ops);
3214
Tejun Heo2bd59d42014-02-11 11:52:49 -05003215 if (cft->seq_start)
3216 kf_ops = &cgroup_kf_ops;
3217 else
3218 kf_ops = &cgroup_kf_single_ops;
3219
3220 /*
3221 * Ugh... if @cft wants a custom max_write_len, we need to
3222 * make a copy of kf_ops to set its atomic_write_len.
3223 */
3224 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3225 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3226 if (!kf_ops) {
3227 cgroup_exit_cftypes(cfts);
3228 return -ENOMEM;
3229 }
3230 kf_ops->atomic_write_len = cft->max_write_len;
3231 }
3232
3233 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003234 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003235 }
3236
3237 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003238}
3239
Tejun Heo21a2d342014-02-12 09:29:49 -05003240static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3241{
Tejun Heo01f64742014-05-13 12:19:23 -04003242 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003243
3244 if (!cfts || !cfts[0].ss)
3245 return -ENOENT;
3246
3247 list_del(&cfts->node);
3248 cgroup_apply_cftypes(cfts, false);
3249 cgroup_exit_cftypes(cfts);
3250 return 0;
3251}
3252
Tejun Heo8e3f6542012-04-01 12:09:55 -07003253/**
Tejun Heo80b13582014-02-12 09:29:48 -05003254 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3255 * @cfts: zero-length name terminated array of cftypes
3256 *
3257 * Unregister @cfts. Files described by @cfts are removed from all
3258 * existing cgroups and all future cgroups won't have them either. This
3259 * function can be called anytime whether @cfts' subsys is attached or not.
3260 *
3261 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3262 * registered.
3263 */
3264int cgroup_rm_cftypes(struct cftype *cfts)
3265{
Tejun Heo21a2d342014-02-12 09:29:49 -05003266 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003267
Tejun Heo01f64742014-05-13 12:19:23 -04003268 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003269 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003270 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003271 return ret;
3272}
3273
3274/**
3275 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3276 * @ss: target cgroup subsystem
3277 * @cfts: zero-length name terminated array of cftypes
3278 *
3279 * Register @cfts to @ss. Files described by @cfts are created for all
3280 * existing cgroups to which @ss is attached and all future cgroups will
3281 * have them too. This function can be called anytime whether @ss is
3282 * attached or not.
3283 *
3284 * Returns 0 on successful registration, -errno on failure. Note that this
3285 * function currently returns 0 as long as @cfts registration is successful
3286 * even if some file creation attempts on existing cgroups fail.
3287 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003288static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003289{
Tejun Heo9ccece82013-06-28 16:24:11 -07003290 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003291
Li Zefanc731ae12014-06-05 17:16:30 +08003292 if (ss->disabled)
3293 return 0;
3294
Li Zefandc5736e2014-02-17 10:41:50 +08003295 if (!cfts || cfts[0].name[0] == '\0')
3296 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003297
Tejun Heo2bd59d42014-02-11 11:52:49 -05003298 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003299 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003300 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003301
Tejun Heo01f64742014-05-13 12:19:23 -04003302 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003303
Tejun Heo0adb0702014-02-12 09:29:48 -05003304 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003305 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003306 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003307 cgroup_rm_cftypes_locked(cfts);
3308
Tejun Heo01f64742014-05-13 12:19:23 -04003309 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003310 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003311}
Tejun Heo79578622012-04-01 12:09:56 -07003312
3313/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003314 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3315 * @ss: target cgroup subsystem
3316 * @cfts: zero-length name terminated array of cftypes
3317 *
3318 * Similar to cgroup_add_cftypes() but the added files are only used for
3319 * the default hierarchy.
3320 */
3321int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3322{
3323 struct cftype *cft;
3324
3325 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003326 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003327 return cgroup_add_cftypes(ss, cfts);
3328}
3329
3330/**
3331 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3332 * @ss: target cgroup subsystem
3333 * @cfts: zero-length name terminated array of cftypes
3334 *
3335 * Similar to cgroup_add_cftypes() but the added files are only used for
3336 * the legacy hierarchies.
3337 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003338int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3339{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003340 struct cftype *cft;
3341
Vivek Goyalfa8137b2014-08-08 11:44:03 -04003342 /*
3343 * If legacy_flies_on_dfl, we want to show the legacy files on the
3344 * dfl hierarchy but iff the target subsystem hasn't been updated
3345 * for the dfl hierarchy yet.
3346 */
3347 if (!cgroup_legacy_files_on_dfl ||
3348 ss->dfl_cftypes != ss->legacy_cftypes) {
3349 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3350 cft->flags |= __CFTYPE_NOT_ON_DFL;
3351 }
3352
Tejun Heo2cf669a2014-07-15 11:05:09 -04003353 return cgroup_add_cftypes(ss, cfts);
3354}
3355
Li Zefana043e3b2008-02-23 15:24:09 -08003356/**
3357 * cgroup_task_count - count the number of tasks in a cgroup.
3358 * @cgrp: the cgroup in question
3359 *
3360 * Return the number of tasks in the cgroup.
3361 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003362static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003363{
3364 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003365 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003366
Tejun Heo96d365e2014-02-13 06:58:40 -05003367 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003368 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3369 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003370 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003371 return count;
3372}
3373
Tejun Heo574bd9f2012-11-09 09:12:29 -08003374/**
Tejun Heo492eb212013-08-08 20:11:25 -04003375 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003376 * @pos: the current position (%NULL to initiate traversal)
3377 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003378 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003379 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003380 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003381 * that @parent and @pos are accessible. The next sibling is guaranteed to
3382 * be returned regardless of their states.
3383 *
3384 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3385 * css which finished ->css_online() is guaranteed to be visible in the
3386 * future iterations and will stay visible until the last reference is put.
3387 * A css which hasn't finished ->css_online() or already finished
3388 * ->css_offline() may show up during traversal. It's each subsystem's
3389 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003390 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003391struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3392 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003393{
Tejun Heoc2931b72014-05-16 13:22:51 -04003394 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003395
Tejun Heo8353da12014-05-13 12:19:23 -04003396 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003397
3398 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003399 * @pos could already have been unlinked from the sibling list.
3400 * Once a cgroup is removed, its ->sibling.next is no longer
3401 * updated when its next sibling changes. CSS_RELEASED is set when
3402 * @pos is taken off list, at which time its next pointer is valid,
3403 * and, as releases are serialized, the one pointed to by the next
3404 * pointer is guaranteed to not have started release yet. This
3405 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3406 * critical section, the one pointed to by its next pointer is
3407 * guaranteed to not have finished its RCU grace period even if we
3408 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003409 *
Tejun Heode3f0342014-05-16 13:22:49 -04003410 * If @pos has CSS_RELEASED set, its next pointer can't be
3411 * dereferenced; however, as each css is given a monotonically
3412 * increasing unique serial number and always appended to the
3413 * sibling list, the next one can be found by walking the parent's
3414 * children until the first css with higher serial number than
3415 * @pos's. While this path can be slower, it happens iff iteration
3416 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003417 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003418 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003419 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3420 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3421 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003422 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003423 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003424 if (next->serial_nr > pos->serial_nr)
3425 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003426 }
3427
Tejun Heo3b281af2014-04-23 11:13:15 -04003428 /*
3429 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003430 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003431 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003432 if (&next->sibling != &parent->children)
3433 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003434 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003435}
Tejun Heo53fa5262013-05-24 10:55:38 +09003436
3437/**
Tejun Heo492eb212013-08-08 20:11:25 -04003438 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003439 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003440 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003441 *
Tejun Heo492eb212013-08-08 20:11:25 -04003442 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003443 * to visit for pre-order traversal of @root's descendants. @root is
3444 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003445 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003446 * While this function requires cgroup_mutex or RCU read locking, it
3447 * doesn't require the whole traversal to be contained in a single critical
3448 * section. This function will return the correct next descendant as long
3449 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003450 *
3451 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3452 * css which finished ->css_online() is guaranteed to be visible in the
3453 * future iterations and will stay visible until the last reference is put.
3454 * A css which hasn't finished ->css_online() or already finished
3455 * ->css_offline() may show up during traversal. It's each subsystem's
3456 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003457 */
Tejun Heo492eb212013-08-08 20:11:25 -04003458struct cgroup_subsys_state *
3459css_next_descendant_pre(struct cgroup_subsys_state *pos,
3460 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003461{
Tejun Heo492eb212013-08-08 20:11:25 -04003462 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003463
Tejun Heo8353da12014-05-13 12:19:23 -04003464 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003465
Tejun Heobd8815a2013-08-08 20:11:27 -04003466 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003467 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003468 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003469
3470 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003471 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003472 if (next)
3473 return next;
3474
3475 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003476 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003477 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003478 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003479 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003480 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003481 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003482
3483 return NULL;
3484}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003485
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003486/**
Tejun Heo492eb212013-08-08 20:11:25 -04003487 * css_rightmost_descendant - return the rightmost descendant of a css
3488 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003489 *
Tejun Heo492eb212013-08-08 20:11:25 -04003490 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3491 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003492 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003493 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003494 * While this function requires cgroup_mutex or RCU read locking, it
3495 * doesn't require the whole traversal to be contained in a single critical
3496 * section. This function will return the correct rightmost descendant as
3497 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003498 */
Tejun Heo492eb212013-08-08 20:11:25 -04003499struct cgroup_subsys_state *
3500css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003501{
Tejun Heo492eb212013-08-08 20:11:25 -04003502 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003503
Tejun Heo8353da12014-05-13 12:19:23 -04003504 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003505
3506 do {
3507 last = pos;
3508 /* ->prev isn't RCU safe, walk ->next till the end */
3509 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003510 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003511 pos = tmp;
3512 } while (pos);
3513
3514 return last;
3515}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003516
Tejun Heo492eb212013-08-08 20:11:25 -04003517static struct cgroup_subsys_state *
3518css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003519{
Tejun Heo492eb212013-08-08 20:11:25 -04003520 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003521
3522 do {
3523 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003524 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003525 } while (pos);
3526
3527 return last;
3528}
3529
3530/**
Tejun Heo492eb212013-08-08 20:11:25 -04003531 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003532 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003533 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003534 *
Tejun Heo492eb212013-08-08 20:11:25 -04003535 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003536 * to visit for post-order traversal of @root's descendants. @root is
3537 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003538 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003539 * While this function requires cgroup_mutex or RCU read locking, it
3540 * doesn't require the whole traversal to be contained in a single critical
3541 * section. This function will return the correct next descendant as long
3542 * as both @pos and @cgroup are accessible and @pos is a descendant of
3543 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003544 *
3545 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3546 * css which finished ->css_online() is guaranteed to be visible in the
3547 * future iterations and will stay visible until the last reference is put.
3548 * A css which hasn't finished ->css_online() or already finished
3549 * ->css_offline() may show up during traversal. It's each subsystem's
3550 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003551 */
Tejun Heo492eb212013-08-08 20:11:25 -04003552struct cgroup_subsys_state *
3553css_next_descendant_post(struct cgroup_subsys_state *pos,
3554 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003555{
Tejun Heo492eb212013-08-08 20:11:25 -04003556 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003557
Tejun Heo8353da12014-05-13 12:19:23 -04003558 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003559
Tejun Heo58b79a92013-09-06 15:31:08 -04003560 /* if first iteration, visit leftmost descendant which may be @root */
3561 if (!pos)
3562 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003563
Tejun Heobd8815a2013-08-08 20:11:27 -04003564 /* if we visited @root, we're done */
3565 if (pos == root)
3566 return NULL;
3567
Tejun Heo574bd9f2012-11-09 09:12:29 -08003568 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003569 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003570 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003571 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003572
3573 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003574 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003575}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003576
Tejun Heof3d46502014-05-16 13:22:52 -04003577/**
3578 * css_has_online_children - does a css have online children
3579 * @css: the target css
3580 *
3581 * Returns %true if @css has any online children; otherwise, %false. This
3582 * function can be called from any context but the caller is responsible
3583 * for synchronizing against on/offlining as necessary.
3584 */
3585bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003586{
Tejun Heof3d46502014-05-16 13:22:52 -04003587 struct cgroup_subsys_state *child;
3588 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003589
3590 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003591 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003592 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003593 ret = true;
3594 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003595 }
3596 }
3597 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003598 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003599}
3600
Tejun Heo0942eee2013-08-08 20:11:26 -04003601/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003602 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003603 * @it: the iterator to advance
3604 *
3605 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003606 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003607static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003608{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003609 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003610 struct cgrp_cset_link *link;
3611 struct css_set *cset;
3612
3613 /* Advance to the next non-empty css_set */
3614 do {
3615 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003616 if (l == it->cset_head) {
3617 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003618 return;
3619 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003620
3621 if (it->ss) {
3622 cset = container_of(l, struct css_set,
3623 e_cset_node[it->ss->id]);
3624 } else {
3625 link = list_entry(l, struct cgrp_cset_link, cset_link);
3626 cset = link->cset;
3627 }
Tejun Heoc7561122014-02-25 10:04:01 -05003628 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3629
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003630 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003631
3632 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003633 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003634 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003635 it->task_pos = cset->mg_tasks.next;
3636
3637 it->tasks_head = &cset->tasks;
3638 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003639}
3640
Tejun Heo0942eee2013-08-08 20:11:26 -04003641/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003642 * css_task_iter_start - initiate task iteration
3643 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003644 * @it: the task iterator to use
3645 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003646 * Initiate iteration through the tasks of @css. The caller can call
3647 * css_task_iter_next() to walk through the tasks until the function
3648 * returns NULL. On completion of iteration, css_task_iter_end() must be
3649 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003650 *
3651 * Note that this function acquires a lock which is released when the
3652 * iteration finishes. The caller can't sleep while iteration is in
3653 * progress.
3654 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003655void css_task_iter_start(struct cgroup_subsys_state *css,
3656 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003657 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003658{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003659 /* no one should try to iterate before mounting cgroups */
3660 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003661
Tejun Heo96d365e2014-02-13 06:58:40 -05003662 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003663
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003664 it->ss = css->ss;
3665
3666 if (it->ss)
3667 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3668 else
3669 it->cset_pos = &css->cgroup->cset_links;
3670
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003671 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003672
Tejun Heo72ec7022013-08-08 20:11:26 -04003673 css_advance_task_iter(it);
Paul Menagebd89aab2007-10-18 23:40:44 -07003674}
Paul Menage817929e2007-10-18 23:39:36 -07003675
Tejun Heo0942eee2013-08-08 20:11:26 -04003676/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003677 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003678 * @it: the task iterator being iterated
3679 *
3680 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003681 * initialized via css_task_iter_start(). Returns NULL when the iteration
3682 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003683 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003684struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003685{
3686 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003687 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003688
3689 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003690 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003691 return NULL;
3692 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003693
3694 /*
3695 * Advance iterator to find next entry. cset->tasks is consumed
3696 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3697 * next cset.
3698 */
Paul Menage817929e2007-10-18 23:39:36 -07003699 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003700
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003701 if (l == it->tasks_head)
3702 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003703
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003704 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003705 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003706 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003707 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003708
Paul Menage817929e2007-10-18 23:39:36 -07003709 return res;
3710}
3711
Tejun Heo0942eee2013-08-08 20:11:26 -04003712/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003713 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003714 * @it: the task iterator to finish
3715 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003716 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003717 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003718void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003719 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003720{
Tejun Heo96d365e2014-02-13 06:58:40 -05003721 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003722}
3723
3724/**
3725 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3726 * @to: cgroup to which the tasks will be moved
3727 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003728 *
3729 * Locking rules between cgroup_post_fork() and the migration path
3730 * guarantee that, if a task is forking while being migrated, the new child
3731 * is guaranteed to be either visible in the source cgroup after the
3732 * parent's migration is complete or put into the target cgroup. No task
3733 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003734 */
3735int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3736{
Tejun Heo952aaa12014-02-25 10:04:03 -05003737 LIST_HEAD(preloaded_csets);
3738 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003739 struct css_task_iter it;
3740 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003741 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003742
Tejun Heo952aaa12014-02-25 10:04:03 -05003743 mutex_lock(&cgroup_mutex);
3744
3745 /* all tasks in @from are being moved, all csets are source */
3746 down_read(&css_set_rwsem);
3747 list_for_each_entry(link, &from->cset_links, cset_link)
3748 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3749 up_read(&css_set_rwsem);
3750
3751 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3752 if (ret)
3753 goto out_err;
3754
3755 /*
3756 * Migrate tasks one-by-one until @form is empty. This fails iff
3757 * ->can_attach() fails.
3758 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003759 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003760 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003761 task = css_task_iter_next(&it);
3762 if (task)
3763 get_task_struct(task);
3764 css_task_iter_end(&it);
3765
3766 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003767 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003768 put_task_struct(task);
3769 }
3770 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003771out_err:
3772 cgroup_migrate_finish(&preloaded_csets);
3773 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003774 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003775}
3776
Paul Menage817929e2007-10-18 23:39:36 -07003777/*
Ben Blum102a7752009-09-23 15:56:26 -07003778 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003779 *
3780 * Reading this file can return large amounts of data if a cgroup has
3781 * *lots* of attached tasks. So it may need several calls to read(),
3782 * but we cannot guarantee that the information we produce is correct
3783 * unless we produce it entirely atomically.
3784 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003785 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003786
Li Zefan24528252012-01-20 11:58:43 +08003787/* which pidlist file are we talking about? */
3788enum cgroup_filetype {
3789 CGROUP_FILE_PROCS,
3790 CGROUP_FILE_TASKS,
3791};
3792
3793/*
3794 * A pidlist is a list of pids that virtually represents the contents of one
3795 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3796 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3797 * to the cgroup.
3798 */
3799struct cgroup_pidlist {
3800 /*
3801 * used to find which pidlist is wanted. doesn't change as long as
3802 * this particular list stays in the list.
3803 */
3804 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3805 /* array of xids */
3806 pid_t *list;
3807 /* how many elements the above list has */
3808 int length;
Li Zefan24528252012-01-20 11:58:43 +08003809 /* each of these stored in a list by its cgroup */
3810 struct list_head links;
3811 /* pointer to the cgroup we belong to, for list removal purposes */
3812 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003813 /* for delayed destruction */
3814 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003815};
3816
Paul Menagebbcb81d2007-10-18 23:39:32 -07003817/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003818 * The following two functions "fix" the issue where there are more pids
3819 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3820 * TODO: replace with a kernel-wide solution to this problem
3821 */
3822#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3823static void *pidlist_allocate(int count)
3824{
3825 if (PIDLIST_TOO_LARGE(count))
3826 return vmalloc(count * sizeof(pid_t));
3827 else
3828 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3829}
Tejun Heob1a21362013-11-29 10:42:58 -05003830
Ben Blumd1d9fd32009-09-23 15:56:28 -07003831static void pidlist_free(void *p)
3832{
Bandan Das58794512015-03-02 17:51:10 -05003833 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003834}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003835
3836/*
Tejun Heob1a21362013-11-29 10:42:58 -05003837 * Used to destroy all pidlists lingering waiting for destroy timer. None
3838 * should be left afterwards.
3839 */
3840static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3841{
3842 struct cgroup_pidlist *l, *tmp_l;
3843
3844 mutex_lock(&cgrp->pidlist_mutex);
3845 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3846 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3847 mutex_unlock(&cgrp->pidlist_mutex);
3848
3849 flush_workqueue(cgroup_pidlist_destroy_wq);
3850 BUG_ON(!list_empty(&cgrp->pidlists));
3851}
3852
3853static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3854{
3855 struct delayed_work *dwork = to_delayed_work(work);
3856 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3857 destroy_dwork);
3858 struct cgroup_pidlist *tofree = NULL;
3859
3860 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003861
3862 /*
Tejun Heo04502362013-11-29 10:42:59 -05003863 * Destroy iff we didn't get queued again. The state won't change
3864 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003865 */
Tejun Heo04502362013-11-29 10:42:59 -05003866 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003867 list_del(&l->links);
3868 pidlist_free(l->list);
3869 put_pid_ns(l->key.ns);
3870 tofree = l;
3871 }
3872
Tejun Heob1a21362013-11-29 10:42:58 -05003873 mutex_unlock(&l->owner->pidlist_mutex);
3874 kfree(tofree);
3875}
3876
3877/*
Ben Blum102a7752009-09-23 15:56:26 -07003878 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003879 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003880 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003881static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003882{
Ben Blum102a7752009-09-23 15:56:26 -07003883 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003884
3885 /*
3886 * we presume the 0th element is unique, so i starts at 1. trivial
3887 * edge cases first; no work needs to be done for either
3888 */
3889 if (length == 0 || length == 1)
3890 return length;
3891 /* src and dest walk down the list; dest counts unique elements */
3892 for (src = 1; src < length; src++) {
3893 /* find next unique element */
3894 while (list[src] == list[src-1]) {
3895 src++;
3896 if (src == length)
3897 goto after;
3898 }
3899 /* dest always points to where the next unique element goes */
3900 list[dest] = list[src];
3901 dest++;
3902 }
3903after:
Ben Blum102a7752009-09-23 15:56:26 -07003904 return dest;
3905}
3906
Tejun Heoafb2bc12013-11-29 10:42:59 -05003907/*
3908 * The two pid files - task and cgroup.procs - guaranteed that the result
3909 * is sorted, which forced this whole pidlist fiasco. As pid order is
3910 * different per namespace, each namespace needs differently sorted list,
3911 * making it impossible to use, for example, single rbtree of member tasks
3912 * sorted by task pointer. As pidlists can be fairly large, allocating one
3913 * per open file is dangerous, so cgroup had to implement shared pool of
3914 * pidlists keyed by cgroup and namespace.
3915 *
3916 * All this extra complexity was caused by the original implementation
3917 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04003918 * want to do away with it. Explicitly scramble sort order if on the
3919 * default hierarchy so that no such expectation exists in the new
3920 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05003921 *
3922 * Scrambling is done by swapping every two consecutive bits, which is
3923 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3924 */
3925static pid_t pid_fry(pid_t pid)
3926{
3927 unsigned a = pid & 0x55555555;
3928 unsigned b = pid & 0xAAAAAAAA;
3929
3930 return (a << 1) | (b >> 1);
3931}
3932
3933static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3934{
Tejun Heoaa6ec292014-07-09 10:08:08 -04003935 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05003936 return pid_fry(pid);
3937 else
3938 return pid;
3939}
3940
Ben Blum102a7752009-09-23 15:56:26 -07003941static int cmppid(const void *a, const void *b)
3942{
3943 return *(pid_t *)a - *(pid_t *)b;
3944}
3945
Tejun Heoafb2bc12013-11-29 10:42:59 -05003946static int fried_cmppid(const void *a, const void *b)
3947{
3948 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3949}
3950
Ben Blum72a8cb32009-09-23 15:56:27 -07003951static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3952 enum cgroup_filetype type)
3953{
3954 struct cgroup_pidlist *l;
3955 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003956 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003957
Tejun Heoe6b81712013-11-29 10:42:59 -05003958 lockdep_assert_held(&cgrp->pidlist_mutex);
3959
3960 list_for_each_entry(l, &cgrp->pidlists, links)
3961 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003962 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003963 return NULL;
3964}
3965
Ben Blum72a8cb32009-09-23 15:56:27 -07003966/*
3967 * find the appropriate pidlist for our purpose (given procs vs tasks)
3968 * returns with the lock on that pidlist already held, and takes care
3969 * of the use count, or returns NULL with no locks held if we're out of
3970 * memory.
3971 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003972static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3973 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003974{
3975 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003976
Tejun Heoe6b81712013-11-29 10:42:59 -05003977 lockdep_assert_held(&cgrp->pidlist_mutex);
3978
3979 l = cgroup_pidlist_find(cgrp, type);
3980 if (l)
3981 return l;
3982
Ben Blum72a8cb32009-09-23 15:56:27 -07003983 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003984 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003985 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003986 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003987
Tejun Heob1a21362013-11-29 10:42:58 -05003988 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003989 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003990 /* don't need task_nsproxy() if we're looking at ourself */
3991 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003992 l->owner = cgrp;
3993 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003994 return l;
3995}
3996
3997/*
Ben Blum102a7752009-09-23 15:56:26 -07003998 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3999 */
Ben Blum72a8cb32009-09-23 15:56:27 -07004000static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4001 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004002{
4003 pid_t *array;
4004 int length;
4005 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004006 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004007 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004008 struct cgroup_pidlist *l;
4009
Tejun Heo4bac00d2013-11-29 10:42:59 -05004010 lockdep_assert_held(&cgrp->pidlist_mutex);
4011
Ben Blum102a7752009-09-23 15:56:26 -07004012 /*
4013 * If cgroup gets more users after we read count, we won't have
4014 * enough space - tough. This race is indistinguishable to the
4015 * caller from the case that the additional cgroup users didn't
4016 * show up until sometime later on.
4017 */
4018 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004019 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004020 if (!array)
4021 return -ENOMEM;
4022 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004023 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004024 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004025 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004026 break;
Ben Blum102a7752009-09-23 15:56:26 -07004027 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004028 if (type == CGROUP_FILE_PROCS)
4029 pid = task_tgid_vnr(tsk);
4030 else
4031 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004032 if (pid > 0) /* make sure to only use valid results */
4033 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004034 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004035 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004036 length = n;
4037 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004038 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004039 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4040 else
4041 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004042 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004043 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004044
Tejun Heoe6b81712013-11-29 10:42:59 -05004045 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004046 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004047 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004048 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004049 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004050
4051 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004052 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004053 l->list = array;
4054 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004055 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004056 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004057}
4058
Balbir Singh846c7bb2007-10-18 23:39:44 -07004059/**
Li Zefana043e3b2008-02-23 15:24:09 -08004060 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004061 * @stats: cgroupstats to fill information into
4062 * @dentry: A dentry entry belonging to the cgroup for which stats have
4063 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004064 *
4065 * Build and fill cgroupstats so that taskstats can export it to user
4066 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004067 */
4068int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4069{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004070 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004071 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004072 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004073 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004074
Tejun Heo2bd59d42014-02-11 11:52:49 -05004075 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4076 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4077 kernfs_type(kn) != KERNFS_DIR)
4078 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004079
Li Zefanbad34662014-02-14 16:54:28 +08004080 mutex_lock(&cgroup_mutex);
4081
Tejun Heo2bd59d42014-02-11 11:52:49 -05004082 /*
4083 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004084 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004085 * @kn->priv is RCU safe. Let's do the RCU dancing.
4086 */
4087 rcu_read_lock();
4088 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004089 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004090 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004091 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004092 return -ENOENT;
4093 }
Li Zefanbad34662014-02-14 16:54:28 +08004094 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004095
Tejun Heo9d800df2014-05-14 09:15:00 -04004096 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004097 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004098 switch (tsk->state) {
4099 case TASK_RUNNING:
4100 stats->nr_running++;
4101 break;
4102 case TASK_INTERRUPTIBLE:
4103 stats->nr_sleeping++;
4104 break;
4105 case TASK_UNINTERRUPTIBLE:
4106 stats->nr_uninterruptible++;
4107 break;
4108 case TASK_STOPPED:
4109 stats->nr_stopped++;
4110 break;
4111 default:
4112 if (delayacct_is_task_waiting_on_io(tsk))
4113 stats->nr_io_wait++;
4114 break;
4115 }
4116 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004117 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004118
Li Zefanbad34662014-02-14 16:54:28 +08004119 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004120 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004121}
4122
Paul Menage8f3ff202009-09-23 15:56:25 -07004123
Paul Menagecc31edc2008-10-18 20:28:04 -07004124/*
Ben Blum102a7752009-09-23 15:56:26 -07004125 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004126 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004127 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004128 */
4129
Ben Blum102a7752009-09-23 15:56:26 -07004130static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004131{
4132 /*
4133 * Initially we receive a position value that corresponds to
4134 * one more than the last pid shown (or 0 on the first call or
4135 * after a seek to the start). Use a binary-search to find the
4136 * next pid to display, if any
4137 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004138 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004139 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004140 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004141 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004142 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004143 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004144
Tejun Heo4bac00d2013-11-29 10:42:59 -05004145 mutex_lock(&cgrp->pidlist_mutex);
4146
4147 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004148 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004149 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004150 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004151 * could already have been destroyed.
4152 */
Tejun Heo5d224442013-12-05 12:28:04 -05004153 if (of->priv)
4154 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004155
4156 /*
4157 * Either this is the first start() after open or the matching
4158 * pidlist has been destroyed inbetween. Create a new one.
4159 */
Tejun Heo5d224442013-12-05 12:28:04 -05004160 if (!of->priv) {
4161 ret = pidlist_array_load(cgrp, type,
4162 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004163 if (ret)
4164 return ERR_PTR(ret);
4165 }
Tejun Heo5d224442013-12-05 12:28:04 -05004166 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004167
Paul Menagecc31edc2008-10-18 20:28:04 -07004168 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004169 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004170
Paul Menagecc31edc2008-10-18 20:28:04 -07004171 while (index < end) {
4172 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004173 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004174 index = mid;
4175 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004176 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004177 index = mid + 1;
4178 else
4179 end = mid;
4180 }
4181 }
4182 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004183 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004184 return NULL;
4185 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004186 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004187 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004188 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004189}
4190
Ben Blum102a7752009-09-23 15:56:26 -07004191static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004192{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004193 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004194 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004195
Tejun Heo5d224442013-12-05 12:28:04 -05004196 if (l)
4197 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004198 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004199 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004200}
4201
Ben Blum102a7752009-09-23 15:56:26 -07004202static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004203{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004204 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004205 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004206 pid_t *p = v;
4207 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004208 /*
4209 * Advance to the next pid in the array. If this goes off the
4210 * end, we're done
4211 */
4212 p++;
4213 if (p >= end) {
4214 return NULL;
4215 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004216 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004217 return p;
4218 }
4219}
4220
Ben Blum102a7752009-09-23 15:56:26 -07004221static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004222{
Joe Perches94ff2122015-04-15 16:18:20 -07004223 seq_printf(s, "%d\n", *(int *)v);
4224
4225 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004226}
4227
Tejun Heo182446d2013-08-08 20:11:24 -04004228static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4229 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004230{
Tejun Heo182446d2013-08-08 20:11:24 -04004231 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004232}
4233
Tejun Heo182446d2013-08-08 20:11:24 -04004234static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4235 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004236{
Paul Menage6379c102008-07-25 01:47:01 -07004237 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004238 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004239 else
Tejun Heo182446d2013-08-08 20:11:24 -04004240 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004241 return 0;
4242}
4243
Tejun Heo182446d2013-08-08 20:11:24 -04004244static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4245 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004246{
Tejun Heo182446d2013-08-08 20:11:24 -04004247 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004248}
4249
Tejun Heo182446d2013-08-08 20:11:24 -04004250static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4251 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004252{
4253 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004254 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004255 else
Tejun Heo182446d2013-08-08 20:11:24 -04004256 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004257 return 0;
4258}
4259
Tejun Heoa14c6872014-07-15 11:05:09 -04004260/* cgroup core interface files for the default hierarchy */
4261static struct cftype cgroup_dfl_base_files[] = {
4262 {
4263 .name = "cgroup.procs",
4264 .seq_start = cgroup_pidlist_start,
4265 .seq_next = cgroup_pidlist_next,
4266 .seq_stop = cgroup_pidlist_stop,
4267 .seq_show = cgroup_pidlist_show,
4268 .private = CGROUP_FILE_PROCS,
4269 .write = cgroup_procs_write,
4270 .mode = S_IRUGO | S_IWUSR,
4271 },
4272 {
4273 .name = "cgroup.controllers",
4274 .flags = CFTYPE_ONLY_ON_ROOT,
4275 .seq_show = cgroup_root_controllers_show,
4276 },
4277 {
4278 .name = "cgroup.controllers",
4279 .flags = CFTYPE_NOT_ON_ROOT,
4280 .seq_show = cgroup_controllers_show,
4281 },
4282 {
4283 .name = "cgroup.subtree_control",
4284 .seq_show = cgroup_subtree_control_show,
4285 .write = cgroup_subtree_control_write,
4286 },
4287 {
4288 .name = "cgroup.populated",
4289 .flags = CFTYPE_NOT_ON_ROOT,
4290 .seq_show = cgroup_populated_show,
4291 },
4292 { } /* terminate */
4293};
4294
4295/* cgroup core interface files for the legacy hierarchies */
4296static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004297 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004298 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004299 .seq_start = cgroup_pidlist_start,
4300 .seq_next = cgroup_pidlist_next,
4301 .seq_stop = cgroup_pidlist_stop,
4302 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004303 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004304 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004305 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004306 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004307 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004308 .name = "cgroup.clone_children",
4309 .read_u64 = cgroup_clone_children_read,
4310 .write_u64 = cgroup_clone_children_write,
4311 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004312 {
Tejun Heo873fe092013-04-14 20:15:26 -07004313 .name = "cgroup.sane_behavior",
4314 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004315 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004316 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004317 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004318 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004319 .seq_start = cgroup_pidlist_start,
4320 .seq_next = cgroup_pidlist_next,
4321 .seq_stop = cgroup_pidlist_stop,
4322 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004323 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004324 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004325 .mode = S_IRUGO | S_IWUSR,
4326 },
4327 {
4328 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004329 .read_u64 = cgroup_read_notify_on_release,
4330 .write_u64 = cgroup_write_notify_on_release,
4331 },
Tejun Heo873fe092013-04-14 20:15:26 -07004332 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004333 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004334 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004335 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004336 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004337 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004338 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004339 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004340};
4341
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004342/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004343 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004344 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004345 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004346 *
4347 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004348 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10004349static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004350{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004352 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004353
Tejun Heo8e3f6542012-04-01 12:09:55 -07004354 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004355 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004356 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004357
Tejun Heo69dfa002014-05-04 15:09:13 -04004358 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004359 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004360
Tejun Heo0adb0702014-02-12 09:29:48 -05004361 list_for_each_entry(cfts, &ss->cfts, node) {
4362 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004363 if (ret < 0)
4364 goto err;
4365 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004368err:
4369 cgroup_clear_dir(cgrp, subsys_mask);
4370 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371}
4372
Tejun Heo0c21ead2013-08-13 20:22:51 -04004373/*
4374 * css destruction is four-stage process.
4375 *
4376 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4377 * Implemented in kill_css().
4378 *
4379 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004380 * and thus css_tryget_online() is guaranteed to fail, the css can be
4381 * offlined by invoking offline_css(). After offlining, the base ref is
4382 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004383 *
4384 * 3. When the percpu_ref reaches zero, the only possible remaining
4385 * accessors are inside RCU read sections. css_release() schedules the
4386 * RCU callback.
4387 *
4388 * 4. After the grace period, the css can be freed. Implemented in
4389 * css_free_work_fn().
4390 *
4391 * It is actually hairier because both step 2 and 4 require process context
4392 * and thus involve punting to css->destroy_work adding two additional
4393 * steps to the already complex sequence.
4394 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004395static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004396{
4397 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004398 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004399 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004400 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004401
Tejun Heo9a1049d2014-06-28 08:10:14 -04004402 percpu_ref_exit(&css->refcnt);
4403
Vladimir Davydov01e58652015-02-12 14:59:26 -08004404 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004405 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004406 int id = css->id;
4407
Tejun Heo9d755d32014-05-14 09:15:02 -04004408 if (css->parent)
4409 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004410
Vladimir Davydov01e58652015-02-12 14:59:26 -08004411 ss->css_free(css);
4412 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004413 cgroup_put(cgrp);
4414 } else {
4415 /* cgroup free path */
4416 atomic_dec(&cgrp->root->nr_cgrps);
4417 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004418 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004419
Tejun Heod51f39b2014-05-16 13:22:48 -04004420 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004421 /*
4422 * We get a ref to the parent, and put the ref when
4423 * this cgroup is being freed, so it's guaranteed
4424 * that the parent won't be destroyed before its
4425 * children.
4426 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004427 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004428 kernfs_put(cgrp->kn);
4429 kfree(cgrp);
4430 } else {
4431 /*
4432 * This is root cgroup's refcnt reaching zero,
4433 * which indicates that the root should be
4434 * released.
4435 */
4436 cgroup_destroy_root(cgrp->root);
4437 }
4438 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004439}
4440
4441static void css_free_rcu_fn(struct rcu_head *rcu_head)
4442{
4443 struct cgroup_subsys_state *css =
4444 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4445
Tejun Heo0c21ead2013-08-13 20:22:51 -04004446 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004447 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004448}
4449
Tejun Heo25e15d82014-05-14 09:15:02 -04004450static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004451{
4452 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004453 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004454 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004455 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004456
Tejun Heo1fed1b22014-05-16 13:22:49 -04004457 mutex_lock(&cgroup_mutex);
4458
Tejun Heode3f0342014-05-16 13:22:49 -04004459 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004460 list_del_rcu(&css->sibling);
4461
Tejun Heo9d755d32014-05-14 09:15:02 -04004462 if (ss) {
4463 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004464 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004465 if (ss->css_released)
4466 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004467 } else {
4468 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004469 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4470 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004471
4472 /*
4473 * There are two control paths which try to determine
4474 * cgroup from dentry without going through kernfs -
4475 * cgroupstats_build() and css_tryget_online_from_dir().
4476 * Those are supported by RCU protecting clearing of
4477 * cgrp->kn->priv backpointer.
4478 */
4479 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004480 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004481
Tejun Heo1fed1b22014-05-16 13:22:49 -04004482 mutex_unlock(&cgroup_mutex);
4483
Tejun Heo0c21ead2013-08-13 20:22:51 -04004484 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004485}
4486
Paul Menageddbcc7e2007-10-18 23:39:30 -07004487static void css_release(struct percpu_ref *ref)
4488{
4489 struct cgroup_subsys_state *css =
4490 container_of(ref, struct cgroup_subsys_state, refcnt);
4491
Tejun Heo25e15d82014-05-14 09:15:02 -04004492 INIT_WORK(&css->destroy_work, css_release_work_fn);
4493 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494}
4495
Tejun Heoddfcada2014-05-04 15:09:14 -04004496static void init_and_link_css(struct cgroup_subsys_state *css,
4497 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004498{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004499 lockdep_assert_held(&cgroup_mutex);
4500
Tejun Heoddfcada2014-05-04 15:09:14 -04004501 cgroup_get(cgrp);
4502
Tejun Heod5c419b2014-05-16 13:22:48 -04004503 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004504 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004505 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004506 INIT_LIST_HEAD(&css->sibling);
4507 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004508 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004509
Tejun Heod51f39b2014-05-16 13:22:48 -04004510 if (cgroup_parent(cgrp)) {
4511 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004512 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004513 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004514
Tejun Heoca8bdca2013-08-26 18:40:56 -04004515 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004516}
4517
Li Zefan2a4ac632013-07-31 16:16:40 +08004518/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004519static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004520{
Tejun Heo623f9262013-08-13 11:01:55 -04004521 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004522 int ret = 0;
4523
Tejun Heoa31f2d32012-11-19 08:13:37 -08004524 lockdep_assert_held(&cgroup_mutex);
4525
Tejun Heo92fb9742012-11-19 08:13:38 -08004526 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004527 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004528 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004529 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004530 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004531 }
Tejun Heob1929db2012-11-19 08:13:38 -08004532 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004533}
4534
Li Zefan2a4ac632013-07-31 16:16:40 +08004535/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004536static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004537{
Tejun Heo623f9262013-08-13 11:01:55 -04004538 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004539
4540 lockdep_assert_held(&cgroup_mutex);
4541
4542 if (!(css->flags & CSS_ONLINE))
4543 return;
4544
Li Zefand7eeac12013-03-12 15:35:59 -07004545 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004546 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004547
Tejun Heoeb954192013-08-08 20:11:23 -04004548 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004549 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004550
4551 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004552}
4553
Tejun Heoc81c925a2013-12-06 15:11:56 -05004554/**
4555 * create_css - create a cgroup_subsys_state
4556 * @cgrp: the cgroup new css will be associated with
4557 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004558 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004559 *
4560 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004561 * css is online and installed in @cgrp with all interface files created if
4562 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004563 */
Tejun Heof63070d2014-07-08 18:02:57 -04004564static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4565 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004566{
Tejun Heod51f39b2014-05-16 13:22:48 -04004567 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004568 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004569 struct cgroup_subsys_state *css;
4570 int err;
4571
Tejun Heoc81c925a2013-12-06 15:11:56 -05004572 lockdep_assert_held(&cgroup_mutex);
4573
Tejun Heo1fed1b22014-05-16 13:22:49 -04004574 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004575 if (IS_ERR(css))
4576 return PTR_ERR(css);
4577
Tejun Heoddfcada2014-05-04 15:09:14 -04004578 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004579
Tejun Heo2aad2a82014-09-24 13:31:50 -04004580 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004581 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004582 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004583
Tejun Heo15a4c832014-05-04 15:09:14 -04004584 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4585 if (err < 0)
4586 goto err_free_percpu_ref;
4587 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004588
Tejun Heof63070d2014-07-08 18:02:57 -04004589 if (visible) {
4590 err = cgroup_populate_dir(cgrp, 1 << ss->id);
4591 if (err)
4592 goto err_free_id;
4593 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004594
4595 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004596 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004597 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004598
4599 err = online_css(css);
4600 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004601 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004602
Tejun Heoc81c925a2013-12-06 15:11:56 -05004603 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004604 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004605 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 -04004606 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004607 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004608 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004609 ss->warned_broken_hierarchy = true;
4610 }
4611
4612 return 0;
4613
Tejun Heo1fed1b22014-05-16 13:22:49 -04004614err_list_del:
4615 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004616 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004617err_free_id:
4618 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004619err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004620 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004621err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004622 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004623 return err;
4624}
4625
Tejun Heob3bfd982014-05-13 12:19:22 -04004626static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4627 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628{
Tejun Heoa9746d82014-05-13 12:19:22 -04004629 struct cgroup *parent, *cgrp;
4630 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004632 struct kernfs_node *kn;
Tejun Heoa14c6872014-07-15 11:05:09 -04004633 struct cftype *base_files;
Tejun Heob3bfd982014-05-13 12:19:22 -04004634 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635
Alban Crequy71b1fb52014-08-18 12:20:20 +01004636 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4637 */
4638 if (strchr(name, '\n'))
4639 return -EINVAL;
4640
Tejun Heoa9746d82014-05-13 12:19:22 -04004641 parent = cgroup_kn_lock_live(parent_kn);
4642 if (!parent)
4643 return -ENODEV;
4644 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645
Tejun Heo0a950f62012-11-19 09:02:12 -08004646 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004647 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004648 if (!cgrp) {
4649 ret = -ENOMEM;
4650 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004651 }
4652
Tejun Heo2aad2a82014-09-24 13:31:50 -04004653 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004654 if (ret)
4655 goto out_free_cgrp;
4656
Li Zefan0ab02ca2014-02-11 16:05:46 +08004657 /*
4658 * Temporarily set the pointer to NULL, so idr_find() won't return
4659 * a half-baked cgroup.
4660 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004661 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004662 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004663 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004664 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004665 }
4666
Paul Menagecc31edc2008-10-18 20:28:04 -07004667 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668
Tejun Heo9d800df2014-05-14 09:15:00 -04004669 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004670 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671
Li Zefanb6abdb02008-03-04 14:28:19 -08004672 if (notify_on_release(parent))
4673 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4674
Tejun Heo2260e7f2012-11-19 08:13:38 -08004675 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4676 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004677
Tejun Heo2bd59d42014-02-11 11:52:49 -05004678 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004679 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004680 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004681 ret = PTR_ERR(kn);
4682 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004683 }
4684 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685
Tejun Heo6f305582014-02-12 09:29:50 -05004686 /*
4687 * This extra ref will be put in cgroup_free_fn() and guarantees
4688 * that @cgrp->kn is always accessible.
4689 */
4690 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691
Tejun Heo0cb51d72014-05-16 13:22:49 -04004692 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004693
Tejun Heo4e139af2012-11-19 08:13:36 -08004694 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004695 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004696 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004697 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004698
Tejun Heo0d802552013-12-06 15:11:56 -05004699 /*
4700 * @cgrp is now fully operational. If something fails after this
4701 * point, it'll be released via the normal destruction path.
4702 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004703 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004704
Tejun Heoba0f4d72014-05-13 12:19:22 -04004705 ret = cgroup_kn_set_ugid(kn);
4706 if (ret)
4707 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004708
Tejun Heoa14c6872014-07-15 11:05:09 -04004709 if (cgroup_on_dfl(cgrp))
4710 base_files = cgroup_dfl_base_files;
4711 else
4712 base_files = cgroup_legacy_base_files;
4713
4714 ret = cgroup_addrm_files(cgrp, base_files, true);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004715 if (ret)
4716 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004717
Tejun Heo9d403e92013-12-06 15:11:56 -05004718 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004719 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004720 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004721 ret = create_css(cgrp, ss,
4722 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004723 if (ret)
4724 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004725 }
Tejun Heoa8638032012-11-09 09:12:29 -08004726 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727
Tejun Heobd53d612014-04-23 11:13:16 -04004728 /*
4729 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004730 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004731 */
Tejun Heo667c2492014-07-08 18:02:56 -04004732 if (!cgroup_on_dfl(cgrp)) {
4733 cgrp->subtree_control = parent->subtree_control;
4734 cgroup_refresh_child_subsys_mask(cgrp);
4735 }
Tejun Heof392e512014-04-23 11:13:14 -04004736
Tejun Heo2bd59d42014-02-11 11:52:49 -05004737 kernfs_activate(kn);
4738
Tejun Heoba0f4d72014-05-13 12:19:22 -04004739 ret = 0;
4740 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004741
Tejun Heoba0f4d72014-05-13 12:19:22 -04004742out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004743 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004744out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004745 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004746out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004747 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004748out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004749 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004750 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004751
4752out_destroy:
4753 cgroup_destroy_locked(cgrp);
4754 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004755}
4756
Tejun Heo223dbc32013-08-13 20:22:50 -04004757/*
4758 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004759 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4760 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004761 */
4762static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004763{
Tejun Heo223dbc32013-08-13 20:22:50 -04004764 struct cgroup_subsys_state *css =
4765 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004766
Tejun Heof20104d2013-08-13 20:22:50 -04004767 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004768 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004769 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004770
Tejun Heo09a503ea2013-08-13 20:22:50 -04004771 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004772}
4773
Tejun Heo223dbc32013-08-13 20:22:50 -04004774/* css kill confirmation processing requires process context, bounce */
4775static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004776{
4777 struct cgroup_subsys_state *css =
4778 container_of(ref, struct cgroup_subsys_state, refcnt);
4779
Tejun Heo223dbc32013-08-13 20:22:50 -04004780 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004781 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004782}
4783
Tejun Heof392e512014-04-23 11:13:14 -04004784/**
4785 * kill_css - destroy a css
4786 * @css: css to destroy
4787 *
4788 * This function initiates destruction of @css by removing cgroup interface
4789 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004790 * asynchronously once css_tryget_online() is guaranteed to fail and when
4791 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004792 */
4793static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004794{
Tejun Heo01f64742014-05-13 12:19:23 -04004795 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004796
Tejun Heo2bd59d42014-02-11 11:52:49 -05004797 /*
4798 * This must happen before css is disassociated with its cgroup.
4799 * See seq_css() for details.
4800 */
Tejun Heoaec25022014-02-08 10:36:58 -05004801 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004802
Tejun Heoedae0c32013-08-13 20:22:51 -04004803 /*
4804 * Killing would put the base ref, but we need to keep it alive
4805 * until after ->css_offline().
4806 */
4807 css_get(css);
4808
4809 /*
4810 * cgroup core guarantees that, by the time ->css_offline() is
4811 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004812 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004813 * proceed to offlining css's because percpu_ref_kill() doesn't
4814 * guarantee that the ref is seen as killed on all CPUs on return.
4815 *
4816 * Use percpu_ref_kill_and_confirm() to get notifications as each
4817 * css is confirmed to be seen as killed on all CPUs.
4818 */
4819 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004820}
4821
4822/**
4823 * cgroup_destroy_locked - the first stage of cgroup destruction
4824 * @cgrp: cgroup to be destroyed
4825 *
4826 * css's make use of percpu refcnts whose killing latency shouldn't be
4827 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004828 * guarantee that css_tryget_online() won't succeed by the time
4829 * ->css_offline() is invoked. To satisfy all the requirements,
4830 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004831 *
4832 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4833 * userland visible parts and start killing the percpu refcnts of
4834 * css's. Set up so that the next stage will be kicked off once all
4835 * the percpu refcnts are confirmed to be killed.
4836 *
4837 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4838 * rest of destruction. Once all cgroup references are gone, the
4839 * cgroup is RCU-freed.
4840 *
4841 * This function implements s1. After this step, @cgrp is gone as far as
4842 * the userland is concerned and a new cgroup with the same name may be
4843 * created. As cgroup doesn't care about the names internally, this
4844 * doesn't cause any problem.
4845 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004846static int cgroup_destroy_locked(struct cgroup *cgrp)
4847 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004848{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004849 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004850 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004851 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004852
Tejun Heo42809dd2012-11-19 08:13:37 -08004853 lockdep_assert_held(&cgroup_mutex);
4854
Tejun Heoddd69142013-06-12 21:04:54 -07004855 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004856 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004857 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004858 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004859 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004860 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004861 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004862 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004863 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004864
Tejun Heo1a90dd52012-11-05 09:16:59 -08004865 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004866 * Make sure there's no live children. We can't test emptiness of
4867 * ->self.children as dead children linger on it while being
4868 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004869 */
Tejun Heof3d46502014-05-16 13:22:52 -04004870 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004871 return -EBUSY;
4872
4873 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004874 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004875 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004876 */
Tejun Heo184faf32014-05-16 13:22:51 -04004877 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004878
Tejun Heo249f3462014-05-14 09:15:01 -04004879 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004880 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004881 kill_css(css);
4882
Tejun Heo455050d2013-06-13 19:27:41 -07004883 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004884 * Remove @cgrp directory along with the base files. @cgrp has an
4885 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07004886 */
Tejun Heo01f64742014-05-13 12:19:23 -04004887 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04004888
Tejun Heod51f39b2014-05-16 13:22:48 -04004889 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05004890
Tejun Heo249f3462014-05-14 09:15:01 -04004891 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004892 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07004893
Tejun Heoea15f8c2013-06-13 19:27:42 -07004894 return 0;
4895};
4896
Tejun Heo2bd59d42014-02-11 11:52:49 -05004897static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004898{
Tejun Heoa9746d82014-05-13 12:19:22 -04004899 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004900 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004901
Tejun Heoa9746d82014-05-13 12:19:22 -04004902 cgrp = cgroup_kn_lock_live(kn);
4903 if (!cgrp)
4904 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004905
Tejun Heoa9746d82014-05-13 12:19:22 -04004906 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004907
Tejun Heoa9746d82014-05-13 12:19:22 -04004908 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004909 return ret;
4910}
4911
Tejun Heo2bd59d42014-02-11 11:52:49 -05004912static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4913 .remount_fs = cgroup_remount,
4914 .show_options = cgroup_show_options,
4915 .mkdir = cgroup_mkdir,
4916 .rmdir = cgroup_rmdir,
4917 .rename = cgroup_rename,
4918};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004919
Tejun Heo15a4c832014-05-04 15:09:14 -04004920static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004922 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004923
4924 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004925
Tejun Heo648bb562012-11-19 08:13:36 -08004926 mutex_lock(&cgroup_mutex);
4927
Tejun Heo15a4c832014-05-04 15:09:14 -04004928 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004929 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004930
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004931 /* Create the root cgroup state for this subsystem */
4932 ss->root = &cgrp_dfl_root;
4933 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004934 /* We don't handle early failures gracefully */
4935 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004936 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004937
4938 /*
4939 * Root csses are never destroyed and we can't initialize
4940 * percpu_ref during early init. Disable refcnting.
4941 */
4942 css->flags |= CSS_NO_REF;
4943
Tejun Heo15a4c832014-05-04 15:09:14 -04004944 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004945 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004946 css->id = 1;
4947 } else {
4948 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4949 BUG_ON(css->id < 0);
4950 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004951
Li Zefane8d55fd2008-04-29 01:00:13 -07004952 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004953 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004954 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004955 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004956 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004957
Aleksa Saraicb4a3162015-06-06 10:02:14 +10004958 have_fork_callback |= (bool)ss->fork << ss->id;
4959 have_exit_callback |= (bool)ss->exit << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004960
Li Zefane8d55fd2008-04-29 01:00:13 -07004961 /* At system boot, before all subsystems have been
4962 * registered, no tasks have been forked, so we don't
4963 * need to invoke fork callbacks here. */
4964 BUG_ON(!list_empty(&init_task.tasks));
4965
Tejun Heoae7f1642013-08-13 20:22:50 -04004966 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004967
Tejun Heo648bb562012-11-19 08:13:36 -08004968 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004969}
4970
4971/**
Li Zefana043e3b2008-02-23 15:24:09 -08004972 * cgroup_init_early - cgroup initialization at system boot
4973 *
4974 * Initialize cgroups at system boot, and initialize any
4975 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004976 */
4977int __init cgroup_init_early(void)
4978{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04004979 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07004980 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004981 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004982
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004983 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004984 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4985
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004986 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004987
Tejun Heo3ed80a62014-02-08 10:36:58 -05004988 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004989 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004990 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4991 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004992 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004993 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4994 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004995
Tejun Heoaec25022014-02-08 10:36:58 -05004996 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004997 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004998
4999 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005000 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005001 }
5002 return 0;
5003}
5004
5005/**
Li Zefana043e3b2008-02-23 15:24:09 -08005006 * cgroup_init - cgroup initialization
5007 *
5008 * Register cgroup filesystem and /proc file, and initialize
5009 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005010 */
5011int __init cgroup_init(void)
5012{
Tejun Heo30159ec2013-06-25 11:53:37 -07005013 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005014 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04005015 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07005016
Tejun Heod59cfc02015-05-13 16:35:17 -04005017 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005018 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5019 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005020
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005021 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005022
Tejun Heo82fe9b02013-06-25 11:53:37 -07005023 /* Add init_css_set to the hash table */
5024 key = css_set_hash(init_css_set.subsys);
5025 hash_add(css_set_table, &init_css_set.hlist, key);
5026
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005027 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005028
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005029 mutex_unlock(&cgroup_mutex);
5030
Tejun Heo172a2c062014-03-19 10:23:53 -04005031 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005032 if (ss->early_init) {
5033 struct cgroup_subsys_state *css =
5034 init_css_set.subsys[ss->id];
5035
5036 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5037 GFP_KERNEL);
5038 BUG_ON(css->id < 0);
5039 } else {
5040 cgroup_init_subsys(ss, false);
5041 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005042
Tejun Heo2d8f2432014-04-23 11:13:15 -04005043 list_add_tail(&init_css_set.e_cset_node[ssid],
5044 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005045
5046 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005047 * Setting dfl_root subsys_mask needs to consider the
5048 * disabled flag and cftype registration needs kmalloc,
5049 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005050 */
Tejun Heoa8ddc822014-07-15 11:05:10 -04005051 if (ss->disabled)
5052 continue;
5053
5054 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5055
5056 if (cgroup_legacy_files_on_dfl && !ss->dfl_cftypes)
5057 ss->dfl_cftypes = ss->legacy_cftypes;
5058
Tejun Heo5de4fa12014-07-15 11:05:10 -04005059 if (!ss->dfl_cftypes)
5060 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5061
Tejun Heoa8ddc822014-07-15 11:05:10 -04005062 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5063 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5064 } else {
5065 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5066 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005067 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005068
5069 if (ss->bind)
5070 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005071 }
Greg KH676db4a2010-08-05 13:53:35 -07005072
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005073 err = sysfs_create_mount_point(fs_kobj, "cgroup");
5074 if (err)
5075 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005076
5077 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005078 if (err < 0) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005079 sysfs_remove_mount_point(fs_kobj, "cgroup");
Tejun Heo2bd59d42014-02-11 11:52:49 -05005080 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005081 }
5082
5083 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005084 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005085}
Paul Menageb4f48b62007-10-18 23:39:33 -07005086
Tejun Heoe5fca242013-11-22 17:14:39 -05005087static int __init cgroup_wq_init(void)
5088{
5089 /*
5090 * There isn't much point in executing destruction path in
5091 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005092 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005093 *
5094 * We would prefer to do this in cgroup_init() above, but that
5095 * is called before init_workqueues(): so leave this until after.
5096 */
Tejun Heo1a115332014-02-12 19:06:19 -05005097 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005098 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005099
5100 /*
5101 * Used to destroy pidlists and separate to serve as flush domain.
5102 * Cap @max_active to 1 too.
5103 */
5104 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5105 0, 1);
5106 BUG_ON(!cgroup_pidlist_destroy_wq);
5107
Tejun Heoe5fca242013-11-22 17:14:39 -05005108 return 0;
5109}
5110core_initcall(cgroup_wq_init);
5111
Paul Menagea4243162007-10-18 23:39:35 -07005112/*
5113 * proc_cgroup_show()
5114 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5115 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005116 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005117int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5118 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005119{
Tejun Heoe61734c2014-02-12 09:29:50 -05005120 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005121 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005122 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005123
5124 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005125 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005126 if (!buf)
5127 goto out;
5128
Paul Menagea4243162007-10-18 23:39:35 -07005129 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05005130 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005131
Tejun Heo985ed672014-03-19 10:23:53 -04005132 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005133 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005134 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005135 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005136
Tejun Heoa2dd4242014-03-19 10:23:55 -04005137 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005138 continue;
5139
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005140 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07005141 if (root != &cgrp_dfl_root)
5142 for_each_subsys(ss, ssid)
5143 if (root->subsys_mask & (1 << ssid))
5144 seq_printf(m, "%s%s", count++ ? "," : "",
5145 ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005146 if (strlen(root->name))
5147 seq_printf(m, "%sname=%s", count ? "," : "",
5148 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005149 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005150 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05005151 path = cgroup_path(cgrp, buf, PATH_MAX);
5152 if (!path) {
5153 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07005154 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05005155 }
5156 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07005157 seq_putc(m, '\n');
5158 }
5159
Zefan Li006f4ac2014-09-18 16:03:15 +08005160 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005161out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05005162 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005163 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005164 kfree(buf);
5165out:
5166 return retval;
5167}
5168
Paul Menagea4243162007-10-18 23:39:35 -07005169/* Display information about each subsystem and each hierarchy */
5170static int proc_cgroupstats_show(struct seq_file *m, void *v)
5171{
Tejun Heo30159ec2013-06-25 11:53:37 -07005172 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005173 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005174
Paul Menage8bab8dd2008-04-04 14:29:57 -07005175 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005176 /*
5177 * ideally we don't want subsystems moving around while we do this.
5178 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5179 * subsys/hierarchy state.
5180 */
Paul Menagea4243162007-10-18 23:39:35 -07005181 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005182
5183 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005184 seq_printf(m, "%s\t%d\t%d\t%d\n",
5185 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05005186 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005187
Paul Menagea4243162007-10-18 23:39:35 -07005188 mutex_unlock(&cgroup_mutex);
5189 return 0;
5190}
5191
5192static int cgroupstats_open(struct inode *inode, struct file *file)
5193{
Al Viro9dce07f2008-03-29 03:07:28 +00005194 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005195}
5196
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005197static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005198 .open = cgroupstats_open,
5199 .read = seq_read,
5200 .llseek = seq_lseek,
5201 .release = single_release,
5202};
5203
Paul Menageb4f48b62007-10-18 23:39:33 -07005204/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005205 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005206 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005207 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005208 * A task is associated with the init_css_set until cgroup_post_fork()
5209 * attaches it to the parent's css_set. Empty cg_list indicates that
5210 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005211 */
5212void cgroup_fork(struct task_struct *child)
5213{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005214 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005215 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005216}
5217
5218/**
Li Zefana043e3b2008-02-23 15:24:09 -08005219 * cgroup_post_fork - called on a new task after adding it to the task list
5220 * @child: the task in question
5221 *
Tejun Heo5edee612012-10-16 15:03:14 -07005222 * Adds the task to the list running through its css_set if necessary and
5223 * call the subsystem fork() callbacks. Has to be after the task is
5224 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005225 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005226 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005227 */
Paul Menage817929e2007-10-18 23:39:36 -07005228void cgroup_post_fork(struct task_struct *child)
5229{
Tejun Heo30159ec2013-06-25 11:53:37 -07005230 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005231 int i;
5232
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005233 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005234 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005235 * function sets use_task_css_set_links before grabbing
5236 * tasklist_lock and we just went through tasklist_lock to add
5237 * @child, it's guaranteed that either we see the set
5238 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5239 * @child during its iteration.
5240 *
5241 * If we won the race, @child is associated with %current's
5242 * css_set. Grabbing css_set_rwsem guarantees both that the
5243 * association is stable, and, on completion of the parent's
5244 * migration, @child is visible in the source of migration or
5245 * already in the destination cgroup. This guarantee is necessary
5246 * when implementing operations which need to migrate all tasks of
5247 * a cgroup to another.
5248 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005249 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005250 * will remain in init_css_set. This is safe because all tasks are
5251 * in the init_css_set before cg_links is enabled and there's no
5252 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005253 */
Paul Menage817929e2007-10-18 23:39:36 -07005254 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005255 struct css_set *cset;
5256
Tejun Heo96d365e2014-02-13 06:58:40 -05005257 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005258 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005259 if (list_empty(&child->cg_list)) {
5260 rcu_assign_pointer(child->cgroups, cset);
5261 list_add(&child->cg_list, &cset->tasks);
5262 get_css_set(cset);
5263 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005264 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07005265 }
Tejun Heo5edee612012-10-16 15:03:14 -07005266
5267 /*
5268 * Call ss->fork(). This must happen after @child is linked on
5269 * css_set; otherwise, @child might change state between ->fork()
5270 * and addition to css_set.
5271 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005272 for_each_subsys_which(ss, i, &have_fork_callback)
5273 ss->fork(child);
Paul Menage817929e2007-10-18 23:39:36 -07005274}
Tejun Heo5edee612012-10-16 15:03:14 -07005275
Paul Menage817929e2007-10-18 23:39:36 -07005276/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005277 * cgroup_exit - detach cgroup from exiting task
5278 * @tsk: pointer to task_struct of exiting process
5279 *
5280 * Description: Detach cgroup from @tsk and release it.
5281 *
5282 * Note that cgroups marked notify_on_release force every task in
5283 * them to take the global cgroup_mutex mutex when exiting.
5284 * This could impact scaling on very large systems. Be reluctant to
5285 * use notify_on_release cgroups where very high task exit scaling
5286 * is required on large systems.
5287 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005288 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5289 * call cgroup_exit() while the task is still competent to handle
5290 * notify_on_release(), then leave the task attached to the root cgroup in
5291 * each hierarchy for the remainder of its exit. No need to bother with
5292 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005293 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005294 */
Li Zefan1ec41832014-03-28 15:22:19 +08005295void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005296{
Tejun Heo30159ec2013-06-25 11:53:37 -07005297 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005298 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005299 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005300 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005301
5302 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005303 * Unlink from @tsk from its css_set. As migration path can't race
5304 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005305 */
5306 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005307 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005308 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005309 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005310 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005311 }
5312
Paul Menageb4f48b62007-10-18 23:39:33 -07005313 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005314 cset = task_css_set(tsk);
5315 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005316
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005317 /* see cgroup_post_fork() for details */
5318 for_each_subsys_which(ss, i, &have_exit_callback) {
5319 struct cgroup_subsys_state *old_css = cset->subsys[i];
5320 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005321
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005322 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005323 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005324
Tejun Heoeaf797a2014-02-25 10:04:03 -05005325 if (put_cset)
Zefan Lia25eb522014-09-19 16:51:00 +08005326 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005327}
Paul Menage697f4162007-10-18 23:39:34 -07005328
Paul Menagebd89aab2007-10-18 23:40:44 -07005329static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005330{
Zefan Lia25eb522014-09-19 16:51:00 +08005331 if (notify_on_release(cgrp) && !cgroup_has_tasks(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005332 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5333 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005334}
5335
Paul Menage81a6a5c2007-10-18 23:39:38 -07005336/*
5337 * Notify userspace when a cgroup is released, by running the
5338 * configured release agent with the name of the cgroup (path
5339 * relative to the root of cgroup file system) as the argument.
5340 *
5341 * Most likely, this user command will try to rmdir this cgroup.
5342 *
5343 * This races with the possibility that some other task will be
5344 * attached to this cgroup before it is removed, or that some other
5345 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5346 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5347 * unused, and this cgroup will be reprieved from its death sentence,
5348 * to continue to serve a useful existence. Next time it's released,
5349 * we will get notified again, if it still has 'notify_on_release' set.
5350 *
5351 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5352 * means only wait until the task is successfully execve()'d. The
5353 * separate release agent task is forked by call_usermodehelper(),
5354 * then control in this thread returns here, without waiting for the
5355 * release agent task. We don't bother to wait because the caller of
5356 * this routine has no use for the exit status of the release agent
5357 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005358 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005359static void cgroup_release_agent(struct work_struct *work)
5360{
Zefan Li971ff492014-09-18 16:06:19 +08005361 struct cgroup *cgrp =
5362 container_of(work, struct cgroup, release_agent_work);
5363 char *pathbuf = NULL, *agentbuf = NULL, *path;
5364 char *argv[3], *envp[3];
5365
Paul Menage81a6a5c2007-10-18 23:39:38 -07005366 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005367
Zefan Li971ff492014-09-18 16:06:19 +08005368 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5369 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5370 if (!pathbuf || !agentbuf)
5371 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005372
Zefan Li971ff492014-09-18 16:06:19 +08005373 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5374 if (!path)
5375 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005376
Zefan Li971ff492014-09-18 16:06:19 +08005377 argv[0] = agentbuf;
5378 argv[1] = path;
5379 argv[2] = NULL;
5380
5381 /* minimal command environment */
5382 envp[0] = "HOME=/";
5383 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5384 envp[2] = NULL;
5385
Paul Menage81a6a5c2007-10-18 23:39:38 -07005386 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005387 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005388 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005389out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005390 mutex_unlock(&cgroup_mutex);
5391out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005392 kfree(agentbuf);
5393 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005394}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005395
5396static int __init cgroup_disable(char *str)
5397{
Tejun Heo30159ec2013-06-25 11:53:37 -07005398 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005399 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005400 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005401
5402 while ((token = strsep(&str, ",")) != NULL) {
5403 if (!*token)
5404 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005405
Tejun Heo3ed80a62014-02-08 10:36:58 -05005406 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005407 if (!strcmp(token, ss->name)) {
5408 ss->disabled = 1;
5409 printk(KERN_INFO "Disabling %s control group"
5410 " subsystem\n", ss->name);
5411 break;
5412 }
5413 }
5414 }
5415 return 1;
5416}
5417__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418
Tejun Heoa8ddc822014-07-15 11:05:10 -04005419static int __init cgroup_set_legacy_files_on_dfl(char *str)
5420{
5421 printk("cgroup: using legacy files on the default hierarchy\n");
5422 cgroup_legacy_files_on_dfl = true;
5423 return 0;
5424}
5425__setup("cgroup__DEVEL__legacy_files_on_dfl", cgroup_set_legacy_files_on_dfl);
5426
Tejun Heob77d7b62013-08-13 11:01:54 -04005427/**
Tejun Heoec903c02014-05-13 12:11:01 -04005428 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005429 * @dentry: directory dentry of interest
5430 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005431 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005432 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5433 * to get the corresponding css and return it. If such css doesn't exist
5434 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005435 */
Tejun Heoec903c02014-05-13 12:11:01 -04005436struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5437 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005438{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005439 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5440 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005441 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005442
Tejun Heo35cf0832013-08-26 18:40:56 -04005443 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005444 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5445 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005446 return ERR_PTR(-EBADF);
5447
Tejun Heo5a17f542014-02-11 11:52:47 -05005448 rcu_read_lock();
5449
Tejun Heo2bd59d42014-02-11 11:52:49 -05005450 /*
5451 * This path doesn't originate from kernfs and @kn could already
5452 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005453 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005454 */
5455 cgrp = rcu_dereference(kn->priv);
5456 if (cgrp)
5457 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005458
Tejun Heoec903c02014-05-13 12:11:01 -04005459 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005460 css = ERR_PTR(-ENOENT);
5461
5462 rcu_read_unlock();
5463 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005464}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005465
Li Zefan1cb650b2013-08-19 10:05:24 +08005466/**
5467 * css_from_id - lookup css by id
5468 * @id: the cgroup id
5469 * @ss: cgroup subsys to be looked into
5470 *
5471 * Returns the css if there's valid one with @id, otherwise returns NULL.
5472 * Should be called under rcu_read_lock().
5473 */
5474struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5475{
Tejun Heo6fa49182014-05-04 15:09:13 -04005476 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005477 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005478}
5479
Paul Menagefe693432009-09-23 15:56:20 -07005480#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005481static struct cgroup_subsys_state *
5482debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005483{
5484 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5485
5486 if (!css)
5487 return ERR_PTR(-ENOMEM);
5488
5489 return css;
5490}
5491
Tejun Heoeb954192013-08-08 20:11:23 -04005492static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005493{
Tejun Heoeb954192013-08-08 20:11:23 -04005494 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005495}
5496
Tejun Heo182446d2013-08-08 20:11:24 -04005497static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5498 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005499{
Tejun Heo182446d2013-08-08 20:11:24 -04005500 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005501}
5502
Tejun Heo182446d2013-08-08 20:11:24 -04005503static u64 current_css_set_read(struct cgroup_subsys_state *css,
5504 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005505{
5506 return (u64)(unsigned long)current->cgroups;
5507}
5508
Tejun Heo182446d2013-08-08 20:11:24 -04005509static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005510 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005511{
5512 u64 count;
5513
5514 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005515 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005516 rcu_read_unlock();
5517 return count;
5518}
5519
Tejun Heo2da8ca82013-12-05 12:28:04 -05005520static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005521{
Tejun Heo69d02062013-06-12 21:04:50 -07005522 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005523 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005524 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005525
Tejun Heoe61734c2014-02-12 09:29:50 -05005526 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5527 if (!name_buf)
5528 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005529
Tejun Heo96d365e2014-02-13 06:58:40 -05005530 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005531 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005532 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005533 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005534 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005535
Tejun Heoa2dd4242014-03-19 10:23:55 -04005536 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005537 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005538 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005539 }
5540 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005541 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005542 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005543 return 0;
5544}
5545
5546#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005547static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005548{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005549 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005550 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005551
Tejun Heo96d365e2014-02-13 06:58:40 -05005552 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005553 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005554 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005555 struct task_struct *task;
5556 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005557
Tejun Heo5abb8852013-06-12 21:04:49 -07005558 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005559
Tejun Heo5abb8852013-06-12 21:04:49 -07005560 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005561 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5562 goto overflow;
5563 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005564 }
Tejun Heoc7561122014-02-25 10:04:01 -05005565
5566 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5567 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5568 goto overflow;
5569 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5570 }
5571 continue;
5572 overflow:
5573 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005574 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005575 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005576 return 0;
5577}
5578
Tejun Heo182446d2013-08-08 20:11:24 -04005579static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005580{
Zefan Lia25eb522014-09-19 16:51:00 +08005581 return (!cgroup_has_tasks(css->cgroup) &&
5582 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005583}
5584
5585static struct cftype debug_files[] = {
5586 {
Paul Menagefe693432009-09-23 15:56:20 -07005587 .name = "taskcount",
5588 .read_u64 = debug_taskcount_read,
5589 },
5590
5591 {
5592 .name = "current_css_set",
5593 .read_u64 = current_css_set_read,
5594 },
5595
5596 {
5597 .name = "current_css_set_refcount",
5598 .read_u64 = current_css_set_refcount_read,
5599 },
5600
5601 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005602 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005603 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005604 },
5605
5606 {
5607 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005608 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005609 },
5610
5611 {
Paul Menagefe693432009-09-23 15:56:20 -07005612 .name = "releasable",
5613 .read_u64 = releasable_read,
5614 },
Paul Menagefe693432009-09-23 15:56:20 -07005615
Tejun Heo4baf6e32012-04-01 12:09:55 -07005616 { } /* terminate */
5617};
Paul Menagefe693432009-09-23 15:56:20 -07005618
Tejun Heo073219e2014-02-08 10:36:58 -05005619struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005620 .css_alloc = debug_css_alloc,
5621 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005622 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005623};
5624#endif /* CONFIG_CGROUP_DEBUG */