blob: 3f6641632f731ae4ddc9cd731a0905c99057895a [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 Heob85d2042013-12-06 15:11:57 -05001336 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001337 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001338 seq_printf(seq, ",%s", ss->name);
Tejun Heo93438622013-04-14 20:15:25 -07001339 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001341 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001342 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001343
1344 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001345 if (strlen(root->release_agent_path))
1346 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001347 spin_unlock(&release_agent_path_lock);
1348
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001349 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001350 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001351 if (strlen(root->name))
1352 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353 return 0;
1354}
1355
1356struct cgroup_sb_opts {
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001357 unsigned long subsys_mask;
Tejun Heo69dfa002014-05-04 15:09:13 -04001358 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001359 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001360 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001361 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001362 /* User explicitly requested empty subsystem */
1363 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001364};
1365
Ben Blumcf5d5942010-03-10 15:22:09 -08001366static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001368 char *token, *o = data;
1369 bool all_ss = false, one_ss = false;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001370 unsigned long mask = -1UL;
Tejun Heo30159ec2013-06-25 11:53:37 -07001371 struct cgroup_subsys *ss;
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001372 int nr_opts = 0;
Tejun Heo30159ec2013-06-25 11:53:37 -07001373 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001374
1375#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001376 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001377#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378
Paul Menagec6d57f32009-09-23 15:56:19 -07001379 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
1381 while ((token = strsep(&o, ",")) != NULL) {
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001382 nr_opts++;
1383
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384 if (!*token)
1385 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001386 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001387 /* Explicitly have no subsystems */
1388 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001389 continue;
1390 }
1391 if (!strcmp(token, "all")) {
1392 /* Mutually exclusive option 'all' + subsystem name */
1393 if (one_ss)
1394 return -EINVAL;
1395 all_ss = true;
1396 continue;
1397 }
Tejun Heo873fe092013-04-14 20:15:26 -07001398 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1399 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1400 continue;
1401 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001402 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001403 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001404 continue;
1405 }
1406 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001407 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001408 continue;
1409 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001410 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001411 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001412 continue;
1413 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001414 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001415 /* Specifying two release agents is forbidden */
1416 if (opts->release_agent)
1417 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001418 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001419 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001420 if (!opts->release_agent)
1421 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001422 continue;
1423 }
1424 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001425 const char *name = token + 5;
1426 /* Can't specify an empty name */
1427 if (!strlen(name))
1428 return -EINVAL;
1429 /* Must match [\w.-]+ */
1430 for (i = 0; i < strlen(name); i++) {
1431 char c = name[i];
1432 if (isalnum(c))
1433 continue;
1434 if ((c == '.') || (c == '-') || (c == '_'))
1435 continue;
1436 return -EINVAL;
1437 }
1438 /* Specifying two names is forbidden */
1439 if (opts->name)
1440 return -EINVAL;
1441 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001442 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001443 GFP_KERNEL);
1444 if (!opts->name)
1445 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001446
1447 continue;
1448 }
1449
Tejun Heo30159ec2013-06-25 11:53:37 -07001450 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001451 if (strcmp(token, ss->name))
1452 continue;
1453 if (ss->disabled)
1454 continue;
1455
1456 /* Mutually exclusive option 'all' + subsystem name */
1457 if (all_ss)
1458 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001459 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001460 one_ss = true;
1461
1462 break;
1463 }
1464 if (i == CGROUP_SUBSYS_COUNT)
1465 return -ENOENT;
1466 }
1467
Tejun Heo873fe092013-04-14 20:15:26 -07001468 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001469 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 -04001470 if (nr_opts != 1) {
1471 pr_err("sane_behavior: no other mount options allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001472 return -EINVAL;
1473 }
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001474 return 0;
Tejun Heo873fe092013-04-14 20:15:26 -07001475 }
1476
Li Zefanf9ab5b52009-06-17 16:26:33 -07001477 /*
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001478 * If the 'all' option was specified select all the subsystems,
1479 * otherwise if 'none', 'name=' and a subsystem name options were
1480 * not specified, let's default to 'all'
1481 */
1482 if (all_ss || (!one_ss && !opts->none && !opts->name))
1483 for_each_subsys(ss, i)
1484 if (!ss->disabled)
1485 opts->subsys_mask |= (1 << i);
1486
1487 /*
1488 * We either have to specify by name or by subsystems. (So all
1489 * empty hierarchies must have a name).
1490 */
1491 if (!opts->subsys_mask && !opts->name)
1492 return -EINVAL;
1493
1494 /*
Li Zefanf9ab5b52009-06-17 16:26:33 -07001495 * Option noprefix was introduced just for backward compatibility
1496 * with the old cpuset, so we allow noprefix only if mounting just
1497 * the cpuset subsystem.
1498 */
Tejun Heo93438622013-04-14 20:15:25 -07001499 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001500 return -EINVAL;
1501
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001502 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001503 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001504 return -EINVAL;
1505
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 return 0;
1507}
1508
Tejun Heo2bd59d42014-02-11 11:52:49 -05001509static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510{
1511 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001512 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513 struct cgroup_sb_opts opts;
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001514 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515
Tejun Heoaa6ec292014-07-09 10:08:08 -04001516 if (root == &cgrp_dfl_root) {
1517 pr_err("remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001518 return -EINVAL;
1519 }
1520
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521 mutex_lock(&cgroup_mutex);
1522
1523 /* See what subsystems are wanted */
1524 ret = parse_cgroupfs_options(data, &opts);
1525 if (ret)
1526 goto out_unlock;
1527
Tejun Heof392e512014-04-23 11:13:14 -04001528 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001529 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001530 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001531
Tejun Heof392e512014-04-23 11:13:14 -04001532 added_mask = opts.subsys_mask & ~root->subsys_mask;
1533 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001534
Ben Blumcf5d5942010-03-10 15:22:09 -08001535 /* Don't allow flags or name to change at remount */
Tejun Heo7450e902014-07-09 10:08:07 -04001536 if ((opts.flags ^ root->flags) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001537 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001538 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo7450e902014-07-09 10:08:07 -04001539 opts.flags, opts.name ?: "", root->flags, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001541 goto out_unlock;
1542 }
1543
Tejun Heof172e672013-06-28 17:07:30 -07001544 /* remounting is not allowed for populated hierarchies */
Tejun Heod5c419b2014-05-16 13:22:48 -04001545 if (!list_empty(&root->cgrp.self.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001546 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001547 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001548 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
Tejun Heo5df36032014-03-19 10:23:54 -04001550 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001551 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001554 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001555
Tejun Heo69e943b2014-02-08 10:36:58 -05001556 if (opts.release_agent) {
1557 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001558 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001559 spin_unlock(&release_agent_path_lock);
1560 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001562 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 return ret;
1566}
1567
Tejun Heoafeb0f92014-02-13 06:58:39 -05001568/*
1569 * To reduce the fork() overhead for systems that are not actually using
1570 * their cgroups capability, we don't maintain the lists running through
1571 * each css_set to its tasks until we see the list actually used - in other
1572 * words after the first mount.
1573 */
1574static bool use_task_css_set_links __read_mostly;
1575
1576static void cgroup_enable_task_cg_lists(void)
1577{
1578 struct task_struct *p, *g;
1579
Tejun Heo96d365e2014-02-13 06:58:40 -05001580 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001581
1582 if (use_task_css_set_links)
1583 goto out_unlock;
1584
1585 use_task_css_set_links = true;
1586
1587 /*
1588 * We need tasklist_lock because RCU is not safe against
1589 * while_each_thread(). Besides, a forking task that has passed
1590 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1591 * is not guaranteed to have its child immediately visible in the
1592 * tasklist if we walk through it with RCU.
1593 */
1594 read_lock(&tasklist_lock);
1595 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001596 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1597 task_css_set(p) != &init_css_set);
1598
1599 /*
1600 * We should check if the process is exiting, otherwise
1601 * it will race with cgroup_exit() in that the list
1602 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001603 * Do it while holding siglock so that we don't end up
1604 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001605 */
Tejun Heof153ad12014-02-25 09:56:49 -05001606 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001607 if (!(p->flags & PF_EXITING)) {
1608 struct css_set *cset = task_css_set(p);
1609
1610 list_add(&p->cg_list, &cset->tasks);
1611 get_css_set(cset);
1612 }
Tejun Heof153ad12014-02-25 09:56:49 -05001613 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001614 } while_each_thread(g, p);
1615 read_unlock(&tasklist_lock);
1616out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001617 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001618}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
Paul Menagecc31edc2008-10-18 20:28:04 -07001620static void init_cgroup_housekeeping(struct cgroup *cgrp)
1621{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001622 struct cgroup_subsys *ss;
1623 int ssid;
1624
Tejun Heod5c419b2014-05-16 13:22:48 -04001625 INIT_LIST_HEAD(&cgrp->self.sibling);
1626 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001627 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001628 INIT_LIST_HEAD(&cgrp->pidlists);
1629 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001630 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001631 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001632
1633 for_each_subsys(ss, ssid)
1634 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001635
1636 init_waitqueue_head(&cgrp->offline_waitq);
Zefan Li971ff492014-09-18 16:06:19 +08001637 INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001638}
Paul Menagec6d57f32009-09-23 15:56:19 -07001639
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001640static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001641 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001643 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001644
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001646 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001647 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001648 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001649 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650
Paul Menagec6d57f32009-09-23 15:56:19 -07001651 root->flags = opts->flags;
1652 if (opts->release_agent)
1653 strcpy(root->release_agent_path, opts->release_agent);
1654 if (opts->name)
1655 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001656 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001657 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001658}
1659
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10001660static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001661{
Tejun Heod427dfe2014-02-11 11:52:48 -05001662 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001663 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heoa14c6872014-07-15 11:05:09 -04001664 struct cftype *base_files;
Tejun Heod427dfe2014-02-11 11:52:48 -05001665 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001666 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001667
Tejun Heod427dfe2014-02-11 11:52:48 -05001668 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001669
Tejun Heo6fa49182014-05-04 15:09:13 -04001670 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001671 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001672 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001673 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001674
Tejun Heo2aad2a82014-09-24 13:31:50 -04001675 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1676 GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001677 if (ret)
1678 goto out;
1679
Tejun Heod427dfe2014-02-11 11:52:48 -05001680 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001681 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001682 * but that's OK - it can only be increased by someone holding
1683 * cgroup_lock, and that's us. The worst that can happen is that we
1684 * have some link structures left over
1685 */
1686 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001688 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689
Tejun Heo985ed672014-03-19 10:23:53 -04001690 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001691 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04001692 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693
Tejun Heo2bd59d42014-02-11 11:52:49 -05001694 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1695 KERNFS_ROOT_CREATE_DEACTIVATED,
1696 root_cgrp);
1697 if (IS_ERR(root->kf_root)) {
1698 ret = PTR_ERR(root->kf_root);
1699 goto exit_root_id;
1700 }
1701 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702
Tejun Heoa14c6872014-07-15 11:05:09 -04001703 if (root == &cgrp_dfl_root)
1704 base_files = cgroup_dfl_base_files;
1705 else
1706 base_files = cgroup_legacy_base_files;
1707
1708 ret = cgroup_addrm_files(root_cgrp, base_files, true);
Tejun Heod427dfe2014-02-11 11:52:48 -05001709 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001710 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711
Tejun Heo5df36032014-03-19 10:23:54 -04001712 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001713 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001714 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001715
Tejun Heod427dfe2014-02-11 11:52:48 -05001716 /*
1717 * There must be no failure case after here, since rebinding takes
1718 * care of subsystems' refcounts, which are explicitly dropped in
1719 * the failure exit path.
1720 */
1721 list_add(&root->root_list, &cgroup_roots);
1722 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723
Tejun Heod427dfe2014-02-11 11:52:48 -05001724 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001725 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001726 * objects.
1727 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001728 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001729 hash_for_each(css_set_table, i, cset, hlist)
1730 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001731 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
Tejun Heod5c419b2014-05-16 13:22:48 -04001733 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001734 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001735
Tejun Heo2bd59d42014-02-11 11:52:49 -05001736 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001737 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001738 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001739
Tejun Heo2bd59d42014-02-11 11:52:49 -05001740destroy_root:
1741 kernfs_destroy_root(root->kf_root);
1742 root->kf_root = NULL;
1743exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001744 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04001745cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04001746 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001747out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001748 free_cgrp_cset_links(&tmp_links);
1749 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750}
1751
Al Virof7e83572010-07-26 13:23:11 +04001752static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001754 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755{
Li Zefan3a32bd72014-06-30 11:50:59 +08001756 struct super_block *pinned_sb = NULL;
Li Zefan970317a2014-06-30 11:49:58 +08001757 struct cgroup_subsys *ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001758 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001760 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001761 int ret;
Li Zefan970317a2014-06-30 11:49:58 +08001762 int i;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001763 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001764
1765 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001766 * The first time anyone tries to mount a cgroup, enable the list
1767 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001768 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001769 if (!use_task_css_set_links)
1770 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001771
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001772 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001777 goto out_unlock;
Tejun Heoa015edd2014-05-14 09:15:00 -04001778
Tejun Heo2bd59d42014-02-11 11:52:49 -05001779 /* look for a matching existing root */
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001780 if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
Tejun Heoa2dd4242014-03-19 10:23:55 -04001781 cgrp_dfl_root_visible = true;
1782 root = &cgrp_dfl_root;
1783 cgroup_get(&root->cgrp);
1784 ret = 0;
1785 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 }
1787
Li Zefan970317a2014-06-30 11:49:58 +08001788 /*
1789 * Destruction of cgroup root is asynchronous, so subsystems may
1790 * still be dying after the previous unmount. Let's drain the
1791 * dying subsystems. We just need to ensure that the ones
1792 * unmounted previously finish dying and don't care about new ones
1793 * starting. Testing ref liveliness is good enough.
1794 */
1795 for_each_subsys(ss, i) {
1796 if (!(opts.subsys_mask & (1 << i)) ||
1797 ss->root == &cgrp_dfl_root)
1798 continue;
1799
1800 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1801 mutex_unlock(&cgroup_mutex);
1802 msleep(10);
1803 ret = restart_syscall();
1804 goto out_free;
1805 }
1806 cgroup_put(&ss->root->cgrp);
1807 }
1808
Tejun Heo985ed672014-03-19 10:23:53 -04001809 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001810 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001812 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001813 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001814
Paul Menage817929e2007-10-18 23:39:36 -07001815 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001816 * If we asked for a name then it must match. Also, if
1817 * name matches but sybsys_mask doesn't, we should fail.
1818 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001819 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001820 if (opts.name) {
1821 if (strcmp(opts.name, root->name))
1822 continue;
1823 name_match = true;
1824 }
Tejun Heo31261212013-06-28 17:07:30 -07001825
1826 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001827 * If we asked for subsystems (or explicitly for no
1828 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001829 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001830 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001831 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001832 if (!name_match)
1833 continue;
1834 ret = -EBUSY;
1835 goto out_unlock;
1836 }
Tejun Heo873fe092013-04-14 20:15:26 -07001837
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04001838 if (root->flags ^ opts.flags)
1839 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Tejun Heo2bd59d42014-02-11 11:52:49 -05001840
Tejun Heo776f02f2014-02-12 09:29:50 -05001841 /*
Li Zefan3a32bd72014-06-30 11:50:59 +08001842 * We want to reuse @root whose lifetime is governed by its
1843 * ->cgrp. Let's check whether @root is alive and keep it
1844 * that way. As cgroup_kill_sb() can happen anytime, we
1845 * want to block it by pinning the sb so that @root doesn't
1846 * get killed before mount is complete.
1847 *
1848 * With the sb pinned, tryget_live can reliably indicate
1849 * whether @root can be reused. If it's being killed,
1850 * drain it. We can use wait_queue for the wait but this
1851 * path is super cold. Let's just sleep a bit and retry.
Tejun Heo776f02f2014-02-12 09:29:50 -05001852 */
Li Zefan3a32bd72014-06-30 11:50:59 +08001853 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1854 if (IS_ERR(pinned_sb) ||
1855 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001856 mutex_unlock(&cgroup_mutex);
Li Zefan3a32bd72014-06-30 11:50:59 +08001857 if (!IS_ERR_OR_NULL(pinned_sb))
1858 deactivate_super(pinned_sb);
Tejun Heo776f02f2014-02-12 09:29:50 -05001859 msleep(10);
Tejun Heoa015edd2014-05-14 09:15:00 -04001860 ret = restart_syscall();
1861 goto out_free;
Tejun Heo776f02f2014-02-12 09:29:50 -05001862 }
1863
1864 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001865 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001866 }
1867
Tejun Heo172a2c062014-03-19 10:23:53 -04001868 /*
1869 * No such thing, create a new one. name= matching without subsys
1870 * specification is allowed for already existing hierarchies but we
1871 * can't create new one without subsys specification.
1872 */
1873 if (!opts.subsys_mask && !opts.none) {
1874 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001875 goto out_unlock;
1876 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001877
Tejun Heo172a2c062014-03-19 10:23:53 -04001878 root = kzalloc(sizeof(*root), GFP_KERNEL);
1879 if (!root) {
1880 ret = -ENOMEM;
1881 goto out_unlock;
1882 }
1883
1884 init_cgroup_root(root, &opts);
1885
Tejun Heo35585572014-02-13 06:58:38 -05001886 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001887 if (ret)
1888 cgroup_free_root(root);
1889
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001890out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001891 mutex_unlock(&cgroup_mutex);
Tejun Heoa015edd2014-05-14 09:15:00 -04001892out_free:
Paul Menagec6d57f32009-09-23 15:56:19 -07001893 kfree(opts.release_agent);
1894 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001895
Tejun Heo2bd59d42014-02-11 11:52:49 -05001896 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001897 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001898
Jianyu Zhanc9482a52014-04-26 15:40:28 +08001899 dentry = kernfs_mount(fs_type, flags, root->kf_root,
1900 CGROUP_SUPER_MAGIC, &new_sb);
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001901 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001902 cgroup_put(&root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08001903
1904 /*
1905 * If @pinned_sb, we're reusing an existing root and holding an
1906 * extra ref on its sb. Mount is complete. Put the extra ref.
1907 */
1908 if (pinned_sb) {
1909 WARN_ON(new_sb);
1910 deactivate_super(pinned_sb);
1911 }
1912
Tejun Heo2bd59d42014-02-11 11:52:49 -05001913 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001914}
1915
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001916static void cgroup_kill_sb(struct super_block *sb)
1917{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001918 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001919 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001920
Tejun Heo9d755d32014-05-14 09:15:02 -04001921 /*
1922 * If @root doesn't have any mounts or children, start killing it.
1923 * This prevents new mounts by disabling percpu_ref_tryget_live().
1924 * cgroup_mount() may wait for @root's release.
Li Zefan1f779fb2014-06-04 16:48:15 +08001925 *
1926 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04001927 */
Johannes Weiner3c606d32015-01-22 10:19:43 -05001928 if (!list_empty(&root->cgrp.self.children) ||
Li Zefan1f779fb2014-06-04 16:48:15 +08001929 root == &cgrp_dfl_root)
Tejun Heo9d755d32014-05-14 09:15:02 -04001930 cgroup_put(&root->cgrp);
1931 else
1932 percpu_ref_kill(&root->cgrp.self.refcnt);
1933
Tejun Heo2bd59d42014-02-11 11:52:49 -05001934 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001935}
1936
1937static struct file_system_type cgroup_fs_type = {
1938 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001939 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001940 .kill_sb = cgroup_kill_sb,
1941};
1942
Li Zefana043e3b2008-02-23 15:24:09 -08001943/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001944 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001945 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001946 * @buf: the buffer to write the path into
1947 * @buflen: the length of the buffer
1948 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001949 * Determine @task's cgroup on the first (the one with the lowest non-zero
1950 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1951 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1952 * cgroup controller callbacks.
1953 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001954 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001955 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001956char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001957{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001958 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001959 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001960 int hierarchy_id = 1;
1961 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001962
1963 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001964 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001965
Tejun Heo913ffdb2013-07-11 16:34:48 -07001966 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1967
Tejun Heo857a2be2013-04-14 20:50:08 -07001968 if (root) {
1969 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001970 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001971 } else {
1972 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001973 if (strlcpy(buf, "/", buflen) < buflen)
1974 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001975 }
1976
Tejun Heo96d365e2014-02-13 06:58:40 -05001977 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001978 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001979 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001980}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001981EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001982
Tejun Heob3dc0942014-02-25 10:04:01 -05001983/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001984struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001985 /* the src and dst cset list running through cset->mg_node */
1986 struct list_head src_csets;
1987 struct list_head dst_csets;
1988
1989 /*
1990 * Fields for cgroup_taskset_*() iteration.
1991 *
1992 * Before migration is committed, the target migration tasks are on
1993 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1994 * the csets on ->dst_csets. ->csets point to either ->src_csets
1995 * or ->dst_csets depending on whether migration is committed.
1996 *
1997 * ->cur_csets and ->cur_task point to the current task position
1998 * during iteration.
1999 */
2000 struct list_head *csets;
2001 struct css_set *cur_cset;
2002 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002003};
2004
2005/**
2006 * cgroup_taskset_first - reset taskset and return the first task
2007 * @tset: taskset of interest
2008 *
2009 * @tset iteration is initialized and the first task is returned.
2010 */
2011struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
2012{
Tejun Heob3dc0942014-02-25 10:04:01 -05002013 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2014 tset->cur_task = NULL;
2015
2016 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002017}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002018
2019/**
2020 * cgroup_taskset_next - iterate to the next task in taskset
2021 * @tset: taskset of interest
2022 *
2023 * Return the next task in @tset. Iteration must have been initialized
2024 * with cgroup_taskset_first().
2025 */
2026struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
2027{
Tejun Heob3dc0942014-02-25 10:04:01 -05002028 struct css_set *cset = tset->cur_cset;
2029 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002030
Tejun Heob3dc0942014-02-25 10:04:01 -05002031 while (&cset->mg_node != tset->csets) {
2032 if (!task)
2033 task = list_first_entry(&cset->mg_tasks,
2034 struct task_struct, cg_list);
2035 else
2036 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002037
Tejun Heob3dc0942014-02-25 10:04:01 -05002038 if (&task->cg_list != &cset->mg_tasks) {
2039 tset->cur_cset = cset;
2040 tset->cur_task = task;
2041 return task;
2042 }
2043
2044 cset = list_next_entry(cset, mg_node);
2045 task = NULL;
2046 }
2047
2048 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002049}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002050
2051/**
Ben Blum74a11662011-05-26 16:25:20 -07002052 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02002053 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002054 * @tsk: the task being migrated
2055 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07002056 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002057 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07002058 */
Tejun Heo5abb8852013-06-12 21:04:49 -07002059static void cgroup_task_migrate(struct cgroup *old_cgrp,
2060 struct task_struct *tsk,
2061 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07002062{
Tejun Heo5abb8852013-06-12 21:04:49 -07002063 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07002064
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002065 lockdep_assert_held(&cgroup_mutex);
2066 lockdep_assert_held(&css_set_rwsem);
2067
Ben Blum74a11662011-05-26 16:25:20 -07002068 /*
Tejun Heod59cfc02015-05-13 16:35:17 -04002069 * We are synchronized through cgroup_threadgroup_rwsem against
2070 * PF_EXITING setting such that we can't race against cgroup_exit()
2071 * changing the css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07002072 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01002073 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002074 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002075
Tejun Heob3dc0942014-02-25 10:04:01 -05002076 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07002077 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002078
Tejun Heo1b9aba42014-03-19 17:43:21 -04002079 /*
2080 * Use move_tail so that cgroup_taskset_first() still returns the
2081 * leader after migration. This works because cgroup_migrate()
2082 * ensures that the dst_cset of the leader is the first on the
2083 * tset's dst_csets list.
2084 */
2085 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07002086
2087 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07002088 * We just gained a reference on old_cset by taking it from the
2089 * task. As trading it for new_cset is protected by cgroup_mutex,
2090 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07002091 */
Zefan Lia25eb522014-09-19 16:51:00 +08002092 put_css_set_locked(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002093}
2094
Li Zefana043e3b2008-02-23 15:24:09 -08002095/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002096 * cgroup_migrate_finish - cleanup after attach
2097 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07002098 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002099 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2100 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002101 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05002102static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07002103{
Tejun Heo1958d2d2014-02-25 10:04:03 -05002104 struct css_set *cset, *tmp_cset;
2105
2106 lockdep_assert_held(&cgroup_mutex);
2107
2108 down_write(&css_set_rwsem);
2109 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
2110 cset->mg_src_cgrp = NULL;
2111 cset->mg_dst_cset = NULL;
2112 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002113 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002114 }
2115 up_write(&css_set_rwsem);
2116}
2117
2118/**
2119 * cgroup_migrate_add_src - add a migration source css_set
2120 * @src_cset: the source css_set to add
2121 * @dst_cgrp: the destination cgroup
2122 * @preloaded_csets: list of preloaded css_sets
2123 *
2124 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2125 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2126 * up by cgroup_migrate_finish().
2127 *
Tejun Heod59cfc02015-05-13 16:35:17 -04002128 * This function may be called without holding cgroup_threadgroup_rwsem
2129 * even if the target is a process. Threads may be created and destroyed
2130 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2131 * into play and the preloaded css_sets are guaranteed to cover all
2132 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002133 */
2134static void cgroup_migrate_add_src(struct css_set *src_cset,
2135 struct cgroup *dst_cgrp,
2136 struct list_head *preloaded_csets)
2137{
2138 struct cgroup *src_cgrp;
2139
2140 lockdep_assert_held(&cgroup_mutex);
2141 lockdep_assert_held(&css_set_rwsem);
2142
2143 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2144
Tejun Heo1958d2d2014-02-25 10:04:03 -05002145 if (!list_empty(&src_cset->mg_preload_node))
2146 return;
2147
2148 WARN_ON(src_cset->mg_src_cgrp);
2149 WARN_ON(!list_empty(&src_cset->mg_tasks));
2150 WARN_ON(!list_empty(&src_cset->mg_node));
2151
2152 src_cset->mg_src_cgrp = src_cgrp;
2153 get_css_set(src_cset);
2154 list_add(&src_cset->mg_preload_node, preloaded_csets);
2155}
2156
2157/**
2158 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002159 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002160 * @preloaded_csets: list of preloaded source css_sets
2161 *
2162 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2163 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002164 * pins all destination css_sets, links each to its source, and append them
2165 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2166 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002167 *
2168 * This function must be called after cgroup_migrate_add_src() has been
2169 * called on each migration source css_set. After migration is performed
2170 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2171 * @preloaded_csets.
2172 */
2173static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2174 struct list_head *preloaded_csets)
2175{
2176 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002177 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002178
2179 lockdep_assert_held(&cgroup_mutex);
2180
Tejun Heof8f22e52014-04-23 11:13:16 -04002181 /*
2182 * Except for the root, child_subsys_mask must be zero for a cgroup
2183 * with tasks so that child cgroups don't compete against tasks.
2184 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002185 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
Tejun Heof8f22e52014-04-23 11:13:16 -04002186 dst_cgrp->child_subsys_mask)
2187 return -EBUSY;
2188
Tejun Heo1958d2d2014-02-25 10:04:03 -05002189 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002190 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002191 struct css_set *dst_cset;
2192
Tejun Heof817de92014-04-23 11:13:16 -04002193 dst_cset = find_css_set(src_cset,
2194 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002195 if (!dst_cset)
2196 goto err;
2197
2198 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002199
2200 /*
2201 * If src cset equals dst, it's noop. Drop the src.
2202 * cgroup_migrate() will skip the cset too. Note that we
2203 * can't handle src == dst as some nodes are used by both.
2204 */
2205 if (src_cset == dst_cset) {
2206 src_cset->mg_src_cgrp = NULL;
2207 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002208 put_css_set(src_cset);
2209 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002210 continue;
2211 }
2212
Tejun Heo1958d2d2014-02-25 10:04:03 -05002213 src_cset->mg_dst_cset = dst_cset;
2214
2215 if (list_empty(&dst_cset->mg_preload_node))
2216 list_add(&dst_cset->mg_preload_node, &csets);
2217 else
Zefan Lia25eb522014-09-19 16:51:00 +08002218 put_css_set(dst_cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002219 }
2220
Tejun Heof817de92014-04-23 11:13:16 -04002221 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002222 return 0;
2223err:
2224 cgroup_migrate_finish(&csets);
2225 return -ENOMEM;
2226}
2227
2228/**
2229 * cgroup_migrate - migrate a process or task to a cgroup
2230 * @cgrp: the destination cgroup
2231 * @leader: the leader of the process or the task to migrate
2232 * @threadgroup: whether @leader points to the whole process or a single task
2233 *
2234 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
Tejun Heod59cfc02015-05-13 16:35:17 -04002235 * process, the caller must be holding cgroup_threadgroup_rwsem. The
Tejun Heo1958d2d2014-02-25 10:04:03 -05002236 * caller is also responsible for invoking cgroup_migrate_add_src() and
2237 * cgroup_migrate_prepare_dst() on the targets before invoking this
2238 * function and following up with cgroup_migrate_finish().
2239 *
2240 * As long as a controller's ->can_attach() doesn't fail, this function is
2241 * guaranteed to succeed. This means that, excluding ->can_attach()
2242 * failure, when migrating multiple targets, the success or failure can be
2243 * decided for all targets by invoking group_migrate_prepare_dst() before
2244 * actually starting migrating.
2245 */
2246static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2247 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002248{
Tejun Heob3dc0942014-02-25 10:04:01 -05002249 struct cgroup_taskset tset = {
2250 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2251 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2252 .csets = &tset.src_csets,
2253 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002254 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002255 struct css_set *cset, *tmp_cset;
2256 struct task_struct *task, *tmp_task;
2257 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002258
2259 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002260 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2261 * already PF_EXITING could be freed from underneath us unless we
2262 * take an rcu_read_lock.
2263 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002264 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002265 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002266 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002267 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002268 /* @task either already exited or can't exit until the end */
2269 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002270 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002271
Tejun Heoeaf797a2014-02-25 10:04:03 -05002272 /* leave @task alone if post_fork() hasn't linked it yet */
2273 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002274 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002275
Tejun Heob3dc0942014-02-25 10:04:01 -05002276 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002277 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002278 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002279
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002280 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002281 * cgroup_taskset_first() must always return the leader.
2282 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002283 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002284 list_move_tail(&task->cg_list, &cset->mg_tasks);
2285 if (list_empty(&cset->mg_node))
2286 list_add_tail(&cset->mg_node, &tset.src_csets);
2287 if (list_empty(&cset->mg_dst_cset->mg_node))
2288 list_move_tail(&cset->mg_dst_cset->mg_node,
2289 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002290 next:
Li Zefan081aa452013-03-13 09:17:09 +08002291 if (!threadgroup)
2292 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002293 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002294 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002295 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002296
Tejun Heo134d3372011-12-12 18:12:21 -08002297 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002298 if (list_empty(&tset.src_csets))
2299 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002300
Tejun Heo1958d2d2014-02-25 10:04:03 -05002301 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002302 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002303 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002304 ret = css->ss->can_attach(css, &tset);
2305 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002306 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002307 goto out_cancel_attach;
2308 }
2309 }
Ben Blum74a11662011-05-26 16:25:20 -07002310 }
2311
2312 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002313 * Now that we're guaranteed success, proceed to move all tasks to
2314 * the new cgroup. There are no failure cases after here, so this
2315 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002316 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002317 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002318 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2319 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2320 cgroup_task_migrate(cset->mg_src_cgrp, task,
2321 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002322 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002323 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002324
2325 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002326 * Migration is committed, all target tasks are now on dst_csets.
2327 * Nothing is sensitive to fork() after this point. Notify
2328 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002329 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002330 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002331
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002332 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002333 if (css->ss->attach)
2334 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002335
Tejun Heo9db8de32014-02-13 06:58:43 -05002336 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002337 goto out_release_tset;
2338
Ben Blum74a11662011-05-26 16:25:20 -07002339out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002340 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002341 if (css == failed_css)
2342 break;
2343 if (css->ss->cancel_attach)
2344 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002345 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002346out_release_tset:
2347 down_write(&css_set_rwsem);
2348 list_splice_init(&tset.dst_csets, &tset.src_csets);
2349 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002350 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002351 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002352 }
2353 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002354 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002355}
2356
Tejun Heo1958d2d2014-02-25 10:04:03 -05002357/**
2358 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2359 * @dst_cgrp: the cgroup to attach to
2360 * @leader: the task or the leader of the threadgroup to be attached
2361 * @threadgroup: attach the whole threadgroup?
2362 *
Tejun Heod59cfc02015-05-13 16:35:17 -04002363 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002364 */
2365static int cgroup_attach_task(struct cgroup *dst_cgrp,
2366 struct task_struct *leader, bool threadgroup)
2367{
2368 LIST_HEAD(preloaded_csets);
2369 struct task_struct *task;
2370 int ret;
2371
2372 /* look up all src csets */
2373 down_read(&css_set_rwsem);
2374 rcu_read_lock();
2375 task = leader;
2376 do {
2377 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2378 &preloaded_csets);
2379 if (!threadgroup)
2380 break;
2381 } while_each_thread(leader, task);
2382 rcu_read_unlock();
2383 up_read(&css_set_rwsem);
2384
2385 /* prepare dst csets and commit */
2386 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2387 if (!ret)
2388 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2389
2390 cgroup_migrate_finish(&preloaded_csets);
2391 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002392}
2393
Tejun Heo187fe842015-06-18 16:54:28 -04002394static int cgroup_procs_write_permission(struct task_struct *task,
2395 struct cgroup *dst_cgrp,
2396 struct kernfs_open_file *of)
Tejun Heodedf22e2015-06-18 16:54:28 -04002397{
2398 const struct cred *cred = current_cred();
2399 const struct cred *tcred = get_task_cred(task);
2400 int ret = 0;
2401
2402 /*
2403 * even if we're attaching all tasks in the thread group, we only
2404 * need to check permissions on one of them.
2405 */
2406 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2407 !uid_eq(cred->euid, tcred->uid) &&
2408 !uid_eq(cred->euid, tcred->suid))
2409 ret = -EACCES;
2410
Tejun Heo187fe842015-06-18 16:54:28 -04002411 if (!ret && cgroup_on_dfl(dst_cgrp)) {
2412 struct super_block *sb = of->file->f_path.dentry->d_sb;
2413 struct cgroup *cgrp;
2414 struct inode *inode;
2415
2416 down_read(&css_set_rwsem);
2417 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
2418 up_read(&css_set_rwsem);
2419
2420 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2421 cgrp = cgroup_parent(cgrp);
2422
2423 ret = -ENOMEM;
2424 inode = kernfs_get_inode(sb, cgrp->procs_kn);
2425 if (inode) {
2426 ret = inode_permission(inode, MAY_WRITE);
2427 iput(inode);
2428 }
2429 }
2430
Tejun Heodedf22e2015-06-18 16:54:28 -04002431 put_cred(tcred);
2432 return ret;
2433}
2434
Ben Blum74a11662011-05-26 16:25:20 -07002435/*
2436 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002437 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002438 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002439 */
Tejun Heoacbef752014-05-13 12:16:22 -04002440static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2441 size_t nbytes, loff_t off, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002442{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002443 struct task_struct *tsk;
Tejun Heoe76ecae2014-05-13 12:19:23 -04002444 struct cgroup *cgrp;
Tejun Heoacbef752014-05-13 12:16:22 -04002445 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002446 int ret;
2447
Tejun Heoacbef752014-05-13 12:16:22 -04002448 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2449 return -EINVAL;
2450
Tejun Heoe76ecae2014-05-13 12:19:23 -04002451 cgrp = cgroup_kn_lock_live(of->kn);
2452 if (!cgrp)
Ben Blum74a11662011-05-26 16:25:20 -07002453 return -ENODEV;
2454
Tejun Heob5ba75b2015-05-13 16:35:18 -04002455 percpu_down_write(&cgroup_threadgroup_rwsem);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002456 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002457 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002458 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002459 if (!tsk) {
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002460 ret = -ESRCH;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002461 goto out_unlock_rcu;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002462 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002463 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002464 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002465 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002466
2467 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002468 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002469
2470 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002471 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002472 * trapped in a cpuset, or RT worker may be born in a cgroup
2473 * with no rt_runtime allocated. Just say no.
2474 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002475 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002476 ret = -EINVAL;
Tejun Heob5ba75b2015-05-13 16:35:18 -04002477 goto out_unlock_rcu;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002478 }
2479
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002480 get_task_struct(tsk);
2481 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002482
Tejun Heo187fe842015-06-18 16:54:28 -04002483 ret = cgroup_procs_write_permission(tsk, cgrp, of);
Tejun Heodedf22e2015-06-18 16:54:28 -04002484 if (!ret)
2485 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
Li Zefan081aa452013-03-13 09:17:09 +08002486
Paul Menagebbcb81d2007-10-18 23:39:32 -07002487 put_task_struct(tsk);
Tejun Heob5ba75b2015-05-13 16:35:18 -04002488 goto out_unlock_threadgroup;
2489
2490out_unlock_rcu:
2491 rcu_read_unlock();
2492out_unlock_threadgroup:
2493 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002494 cgroup_kn_unlock(of->kn);
Tejun Heoacbef752014-05-13 12:16:22 -04002495 return ret ?: nbytes;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002496}
2497
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002498/**
2499 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2500 * @from: attach to all cgroups of a given task
2501 * @tsk: the task to be attached
2502 */
2503int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2504{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002505 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002506 int retval = 0;
2507
Tejun Heo47cfcd02013-04-07 09:29:51 -07002508 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002509 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002510 struct cgroup *from_cgrp;
2511
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002512 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002513 continue;
2514
Tejun Heo96d365e2014-02-13 06:58:40 -05002515 down_read(&css_set_rwsem);
2516 from_cgrp = task_cgroup_from_root(from, root);
2517 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002518
Li Zefan6f4b7e62013-07-31 16:18:36 +08002519 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002520 if (retval)
2521 break;
2522 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002523 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002524
2525 return retval;
2526}
2527EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2528
Tejun Heoacbef752014-05-13 12:16:22 -04002529static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
2530 char *buf, size_t nbytes, loff_t off)
Paul Menageaf351022008-07-25 01:47:01 -07002531{
Tejun Heoacbef752014-05-13 12:16:22 -04002532 return __cgroup_procs_write(of, buf, nbytes, off, false);
Ben Blum74a11662011-05-26 16:25:20 -07002533}
2534
Tejun Heoacbef752014-05-13 12:16:22 -04002535static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
2536 char *buf, size_t nbytes, loff_t off)
Ben Blum74a11662011-05-26 16:25:20 -07002537{
Tejun Heoacbef752014-05-13 12:16:22 -04002538 return __cgroup_procs_write(of, buf, nbytes, off, true);
Paul Menageaf351022008-07-25 01:47:01 -07002539}
2540
Tejun Heo451af502014-05-13 12:16:21 -04002541static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
2542 char *buf, size_t nbytes, loff_t off)
Paul Menagee788e062008-07-25 01:46:59 -07002543{
Tejun Heoe76ecae2014-05-13 12:19:23 -04002544 struct cgroup *cgrp;
Tejun Heo5f469902014-02-11 11:52:48 -05002545
Tejun Heoe76ecae2014-05-13 12:19:23 -04002546 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2547
2548 cgrp = cgroup_kn_lock_live(of->kn);
2549 if (!cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002550 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002551 spin_lock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002552 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
2553 sizeof(cgrp->root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002554 spin_unlock(&release_agent_path_lock);
Tejun Heoe76ecae2014-05-13 12:19:23 -04002555 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002556 return nbytes;
Paul Menagee788e062008-07-25 01:46:59 -07002557}
2558
Tejun Heo2da8ca82013-12-05 12:28:04 -05002559static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002560{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002561 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002562
Tejun Heo46cfeb02014-05-13 12:11:00 -04002563 spin_lock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002564 seq_puts(seq, cgrp->root->release_agent_path);
Tejun Heo46cfeb02014-05-13 12:11:00 -04002565 spin_unlock(&release_agent_path_lock);
Paul Menagee788e062008-07-25 01:46:59 -07002566 seq_putc(seq, '\n');
Paul Menagee788e062008-07-25 01:46:59 -07002567 return 0;
2568}
2569
Tejun Heo2da8ca82013-12-05 12:28:04 -05002570static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002571{
Tejun Heoc1d5d422014-07-09 10:08:08 -04002572 seq_puts(seq, "0\n");
Paul Menage81a6a5c2007-10-18 23:39:38 -07002573 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002574}
2575
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002576static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002577{
2578 struct cgroup_subsys *ss;
2579 bool printed = false;
2580 int ssid;
2581
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002582 for_each_subsys_which(ss, ssid, &ss_mask) {
2583 if (printed)
2584 seq_putc(seq, ' ');
2585 seq_printf(seq, "%s", ss->name);
2586 printed = true;
Tejun Heof8f22e52014-04-23 11:13:16 -04002587 }
2588 if (printed)
2589 seq_putc(seq, '\n');
2590}
2591
2592/* show controllers which are currently attached to the default hierarchy */
2593static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2594{
2595 struct cgroup *cgrp = seq_css(seq)->cgroup;
2596
Tejun Heo5533e012014-05-14 19:33:07 -04002597 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
2598 ~cgrp_dfl_root_inhibit_ss_mask);
Tejun Heof8f22e52014-04-23 11:13:16 -04002599 return 0;
2600}
2601
2602/* show controllers which are enabled from the parent */
2603static int cgroup_controllers_show(struct seq_file *seq, void *v)
2604{
2605 struct cgroup *cgrp = seq_css(seq)->cgroup;
2606
Tejun Heo667c2492014-07-08 18:02:56 -04002607 cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002608 return 0;
2609}
2610
2611/* show controllers which are enabled for a given cgroup's children */
2612static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2613{
2614 struct cgroup *cgrp = seq_css(seq)->cgroup;
2615
Tejun Heo667c2492014-07-08 18:02:56 -04002616 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002617 return 0;
2618}
2619
2620/**
2621 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2622 * @cgrp: root of the subtree to update csses for
2623 *
2624 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2625 * css associations need to be updated accordingly. This function looks up
2626 * all css_sets which are attached to the subtree, creates the matching
2627 * updated css_sets and migrates the tasks to the new ones.
2628 */
2629static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2630{
2631 LIST_HEAD(preloaded_csets);
2632 struct cgroup_subsys_state *css;
2633 struct css_set *src_cset;
2634 int ret;
2635
Tejun Heof8f22e52014-04-23 11:13:16 -04002636 lockdep_assert_held(&cgroup_mutex);
2637
Tejun Heob5ba75b2015-05-13 16:35:18 -04002638 percpu_down_write(&cgroup_threadgroup_rwsem);
2639
Tejun Heof8f22e52014-04-23 11:13:16 -04002640 /* look up all csses currently attached to @cgrp's subtree */
2641 down_read(&css_set_rwsem);
2642 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2643 struct cgrp_cset_link *link;
2644
2645 /* self is not affected by child_subsys_mask change */
2646 if (css->cgroup == cgrp)
2647 continue;
2648
2649 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2650 cgroup_migrate_add_src(link->cset, cgrp,
2651 &preloaded_csets);
2652 }
2653 up_read(&css_set_rwsem);
2654
2655 /* NULL dst indicates self on default hierarchy */
2656 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2657 if (ret)
2658 goto out_finish;
2659
2660 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2661 struct task_struct *last_task = NULL, *task;
2662
2663 /* src_csets precede dst_csets, break on the first dst_cset */
2664 if (!src_cset->mg_src_cgrp)
2665 break;
2666
2667 /*
2668 * All tasks in src_cset need to be migrated to the
2669 * matching dst_cset. Empty it process by process. We
2670 * walk tasks but migrate processes. The leader might even
2671 * belong to a different cset but such src_cset would also
2672 * be among the target src_csets because the default
2673 * hierarchy enforces per-process membership.
2674 */
2675 while (true) {
2676 down_read(&css_set_rwsem);
2677 task = list_first_entry_or_null(&src_cset->tasks,
2678 struct task_struct, cg_list);
2679 if (task) {
2680 task = task->group_leader;
2681 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2682 get_task_struct(task);
2683 }
2684 up_read(&css_set_rwsem);
2685
2686 if (!task)
2687 break;
2688
2689 /* guard against possible infinite loop */
2690 if (WARN(last_task == task,
2691 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2692 goto out_finish;
2693 last_task = task;
2694
Tejun Heof8f22e52014-04-23 11:13:16 -04002695 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2696
Tejun Heof8f22e52014-04-23 11:13:16 -04002697 put_task_struct(task);
2698
2699 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2700 goto out_finish;
2701 }
2702 }
2703
2704out_finish:
2705 cgroup_migrate_finish(&preloaded_csets);
Tejun Heob5ba75b2015-05-13 16:35:18 -04002706 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002707 return ret;
2708}
2709
2710/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04002711static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2712 char *buf, size_t nbytes,
2713 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04002714{
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10002715 unsigned long enable = 0, disable = 0;
2716 unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
Tejun Heoa9746d82014-05-13 12:19:22 -04002717 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04002718 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04002719 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04002720 int ssid, ret;
2721
2722 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002723 * Parse input - space separated list of subsystem names prefixed
2724 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002725 */
Tejun Heo451af502014-05-13 12:16:21 -04002726 buf = strstrip(buf);
2727 while ((tok = strsep(&buf, " "))) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002728 unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
2729
Tejun Heod37167a2014-05-13 12:10:59 -04002730 if (tok[0] == '\0')
2731 continue;
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002732 for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
2733 if (ss->disabled || strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04002734 continue;
2735
2736 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002737 enable |= 1 << ssid;
2738 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002739 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002740 disable |= 1 << ssid;
2741 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002742 } else {
2743 return -EINVAL;
2744 }
2745 break;
2746 }
2747 if (ssid == CGROUP_SUBSYS_COUNT)
2748 return -EINVAL;
2749 }
2750
Tejun Heoa9746d82014-05-13 12:19:22 -04002751 cgrp = cgroup_kn_lock_live(of->kn);
2752 if (!cgrp)
2753 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04002754
2755 for_each_subsys(ss, ssid) {
2756 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002757 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002758 enable &= ~(1 << ssid);
2759 continue;
2760 }
2761
Tejun Heoc29adf22014-07-08 18:02:56 -04002762 /* unavailable or not enabled on the parent? */
2763 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2764 (cgroup_parent(cgrp) &&
Tejun Heo667c2492014-07-08 18:02:56 -04002765 !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04002766 ret = -ENOENT;
2767 goto out_unlock;
2768 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002769 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04002770 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002771 disable &= ~(1 << ssid);
2772 continue;
2773 }
2774
2775 /* a child has it enabled? */
2776 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04002777 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002778 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04002779 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002780 }
2781 }
2782 }
2783 }
2784
2785 if (!enable && !disable) {
2786 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04002787 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04002788 }
2789
2790 /*
Tejun Heo667c2492014-07-08 18:02:56 -04002791 * Except for the root, subtree_control must be zero for a cgroup
Tejun Heof8f22e52014-04-23 11:13:16 -04002792 * with tasks so that child cgroups don't compete against tasks.
2793 */
Tejun Heod51f39b2014-05-16 13:22:48 -04002794 if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002795 ret = -EBUSY;
2796 goto out_unlock;
2797 }
2798
2799 /*
Tejun Heof63070d2014-07-08 18:02:57 -04002800 * Update subsys masks and calculate what needs to be done. More
2801 * subsystems than specified may need to be enabled or disabled
2802 * depending on subsystem dependencies.
2803 */
Tejun Heo755bf5e2014-11-18 02:49:50 -05002804 old_sc = cgrp->subtree_control;
2805 old_ss = cgrp->child_subsys_mask;
2806 new_sc = (old_sc | enable) & ~disable;
2807 new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
Tejun Heoc29adf22014-07-08 18:02:56 -04002808
Tejun Heo755bf5e2014-11-18 02:49:50 -05002809 css_enable = ~old_ss & new_ss;
2810 css_disable = old_ss & ~new_ss;
Tejun Heof63070d2014-07-08 18:02:57 -04002811 enable |= css_enable;
2812 disable |= css_disable;
2813
Tejun Heodb6e3052014-11-18 02:49:51 -05002814 /*
2815 * Because css offlining is asynchronous, userland might try to
2816 * re-enable the same controller while the previous instance is
2817 * still around. In such cases, wait till it's gone using
2818 * offline_waitq.
2819 */
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002820 for_each_subsys_which(ss, ssid, &css_enable) {
Tejun Heodb6e3052014-11-18 02:49:51 -05002821 cgroup_for_each_live_child(child, cgrp) {
2822 DEFINE_WAIT(wait);
2823
2824 if (!cgroup_css(child, ss))
2825 continue;
2826
2827 cgroup_get(child);
2828 prepare_to_wait(&child->offline_waitq, &wait,
2829 TASK_UNINTERRUPTIBLE);
2830 cgroup_kn_unlock(of->kn);
2831 schedule();
2832 finish_wait(&child->offline_waitq, &wait);
2833 cgroup_put(child);
2834
2835 return restart_syscall();
2836 }
2837 }
2838
Tejun Heo755bf5e2014-11-18 02:49:50 -05002839 cgrp->subtree_control = new_sc;
2840 cgrp->child_subsys_mask = new_ss;
2841
Tejun Heof63070d2014-07-08 18:02:57 -04002842 /*
2843 * Create new csses or make the existing ones visible. A css is
2844 * created invisible if it's being implicitly enabled through
2845 * dependency. An invisible css is made visible when the userland
2846 * explicitly enables it.
Tejun Heof8f22e52014-04-23 11:13:16 -04002847 */
2848 for_each_subsys(ss, ssid) {
2849 if (!(enable & (1 << ssid)))
2850 continue;
2851
2852 cgroup_for_each_live_child(child, cgrp) {
Tejun Heof63070d2014-07-08 18:02:57 -04002853 if (css_enable & (1 << ssid))
2854 ret = create_css(child, ss,
2855 cgrp->subtree_control & (1 << ssid));
2856 else
2857 ret = cgroup_populate_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002858 if (ret)
2859 goto err_undo_css;
2860 }
2861 }
2862
Tejun Heoc29adf22014-07-08 18:02:56 -04002863 /*
2864 * At this point, cgroup_e_css() results reflect the new csses
2865 * making the following cgroup_update_dfl_csses() properly update
2866 * css associations of all tasks in the subtree.
2867 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002868 ret = cgroup_update_dfl_csses(cgrp);
2869 if (ret)
2870 goto err_undo_css;
2871
Tejun Heof63070d2014-07-08 18:02:57 -04002872 /*
2873 * All tasks are migrated out of disabled csses. Kill or hide
2874 * them. A css is hidden when the userland requests it to be
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002875 * disabled while other subsystems are still depending on it. The
2876 * css must not actively control resources and be in the vanilla
2877 * state if it's made visible again later. Controllers which may
2878 * be depended upon should provide ->css_reset() for this purpose.
Tejun Heof63070d2014-07-08 18:02:57 -04002879 */
Tejun Heof8f22e52014-04-23 11:13:16 -04002880 for_each_subsys(ss, ssid) {
2881 if (!(disable & (1 << ssid)))
2882 continue;
2883
Tejun Heof63070d2014-07-08 18:02:57 -04002884 cgroup_for_each_live_child(child, cgrp) {
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002885 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2886
2887 if (css_disable & (1 << ssid)) {
2888 kill_css(css);
2889 } else {
Tejun Heof63070d2014-07-08 18:02:57 -04002890 cgroup_clear_dir(child, 1 << ssid);
Tejun Heob4536f0ca2014-07-08 18:02:57 -04002891 if (ss->css_reset)
2892 ss->css_reset(css);
2893 }
Tejun Heof63070d2014-07-08 18:02:57 -04002894 }
Tejun Heof8f22e52014-04-23 11:13:16 -04002895 }
2896
Tejun Heo56c807b2014-11-18 02:49:51 -05002897 /*
2898 * The effective csses of all the descendants (excluding @cgrp) may
2899 * have changed. Subsystems can optionally subscribe to this event
2900 * by implementing ->css_e_css_changed() which is invoked if any of
2901 * the effective csses seen from the css's cgroup may have changed.
2902 */
2903 for_each_subsys(ss, ssid) {
2904 struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
2905 struct cgroup_subsys_state *css;
2906
2907 if (!ss->css_e_css_changed || !this_css)
2908 continue;
2909
2910 css_for_each_descendant_pre(css, this_css)
2911 if (css != this_css)
2912 ss->css_e_css_changed(css);
2913 }
2914
Tejun Heof8f22e52014-04-23 11:13:16 -04002915 kernfs_activate(cgrp->kn);
2916 ret = 0;
2917out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04002918 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04002919 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04002920
2921err_undo_css:
Tejun Heo755bf5e2014-11-18 02:49:50 -05002922 cgrp->subtree_control = old_sc;
2923 cgrp->child_subsys_mask = old_ss;
Tejun Heof8f22e52014-04-23 11:13:16 -04002924
2925 for_each_subsys(ss, ssid) {
2926 if (!(enable & (1 << ssid)))
2927 continue;
2928
2929 cgroup_for_each_live_child(child, cgrp) {
2930 struct cgroup_subsys_state *css = cgroup_css(child, ss);
Tejun Heof63070d2014-07-08 18:02:57 -04002931
2932 if (!css)
2933 continue;
2934
2935 if (css_enable & (1 << ssid))
Tejun Heof8f22e52014-04-23 11:13:16 -04002936 kill_css(css);
Tejun Heof63070d2014-07-08 18:02:57 -04002937 else
2938 cgroup_clear_dir(child, 1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002939 }
2940 }
2941 goto out_unlock;
2942}
2943
Tejun Heo842b5972014-04-25 18:28:02 -04002944static int cgroup_populated_show(struct seq_file *seq, void *v)
2945{
2946 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2947 return 0;
2948}
2949
Tejun Heo2bd59d42014-02-11 11:52:49 -05002950static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2951 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002952{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002953 struct cgroup *cgrp = of->kn->parent->priv;
2954 struct cftype *cft = of->kn->priv;
2955 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002956 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002957
Tejun Heob4168642014-05-13 12:16:21 -04002958 if (cft->write)
2959 return cft->write(of, buf, nbytes, off);
2960
Tejun Heo2bd59d42014-02-11 11:52:49 -05002961 /*
2962 * kernfs guarantees that a file isn't deleted with operations in
2963 * flight, which means that the matching css is and stays alive and
2964 * doesn't need to be pinned. The RCU locking is not necessary
2965 * either. It's just for the convenience of using cgroup_css().
2966 */
2967 rcu_read_lock();
2968 css = cgroup_css(cgrp, cft->ss);
2969 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002970
Tejun Heo451af502014-05-13 12:16:21 -04002971 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05002972 unsigned long long v;
2973 ret = kstrtoull(buf, 0, &v);
2974 if (!ret)
2975 ret = cft->write_u64(css, cft, v);
2976 } else if (cft->write_s64) {
2977 long long v;
2978 ret = kstrtoll(buf, 0, &v);
2979 if (!ret)
2980 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05002981 } else {
2982 ret = -EINVAL;
2983 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002984
Tejun Heoa742c592013-12-05 12:28:03 -05002985 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002986}
2987
Tejun Heo6612f052013-12-05 12:28:04 -05002988static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002989{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002990 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002991}
2992
2993static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2994{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002995 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002996}
2997
2998static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2999{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003000 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003001}
3002
3003static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3004{
Tejun Heo7da11272013-12-05 12:28:04 -05003005 struct cftype *cft = seq_cft(m);
3006 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003007
Tejun Heo2da8ca82013-12-05 12:28:04 -05003008 if (cft->seq_show)
3009 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003010
Tejun Heo896f5192013-12-05 12:28:04 -05003011 if (cft->read_u64)
3012 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3013 else if (cft->read_s64)
3014 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3015 else
3016 return -EINVAL;
3017 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003018}
3019
Tejun Heo2bd59d42014-02-11 11:52:49 -05003020static struct kernfs_ops cgroup_kf_single_ops = {
3021 .atomic_write_len = PAGE_SIZE,
3022 .write = cgroup_file_write,
3023 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003024};
3025
Tejun Heo2bd59d42014-02-11 11:52:49 -05003026static struct kernfs_ops cgroup_kf_ops = {
3027 .atomic_write_len = PAGE_SIZE,
3028 .write = cgroup_file_write,
3029 .seq_start = cgroup_seqfile_start,
3030 .seq_next = cgroup_seqfile_next,
3031 .seq_stop = cgroup_seqfile_stop,
3032 .seq_show = cgroup_seqfile_show,
3033};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003034
3035/*
3036 * cgroup_rename - Only allow simple rename of directories in place.
3037 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003038static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
3039 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003040{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003041 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08003042 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08003043
Tejun Heo2bd59d42014-02-11 11:52:49 -05003044 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003045 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003046 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003047 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08003048
Tejun Heo6db8e852013-06-14 11:18:22 -07003049 /*
3050 * This isn't a proper migration and its usefulness is very
Tejun Heoaa6ec292014-07-09 10:08:08 -04003051 * limited. Disallow on the default hierarchy.
Tejun Heo6db8e852013-06-14 11:18:22 -07003052 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04003053 if (cgroup_on_dfl(cgrp))
Tejun Heo6db8e852013-06-14 11:18:22 -07003054 return -EPERM;
3055
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003056 /*
Tejun Heo8353da12014-05-13 12:19:23 -04003057 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003058 * active_ref. kernfs_rename() doesn't require active_ref
Tejun Heo8353da12014-05-13 12:19:23 -04003059 * protection. Break them before grabbing cgroup_mutex.
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003060 */
3061 kernfs_break_active_protection(new_parent);
3062 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08003063
Tejun Heo2bd59d42014-02-11 11:52:49 -05003064 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003065
Tejun Heo2bd59d42014-02-11 11:52:49 -05003066 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08003067
Tejun Heo2bd59d42014-02-11 11:52:49 -05003068 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003069
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003070 kernfs_unbreak_active_protection(kn);
3071 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003072 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07003073}
3074
Tejun Heo49957f82014-04-07 16:44:47 -04003075/* set uid and gid of cgroup dirs and files to that of the creator */
3076static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3077{
3078 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3079 .ia_uid = current_fsuid(),
3080 .ia_gid = current_fsgid(), };
3081
3082 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3083 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3084 return 0;
3085
3086 return kernfs_setattr(kn, &iattr);
3087}
3088
Tejun Heo2bb566c2013-08-08 20:11:23 -04003089static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003090{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003091 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003092 struct kernfs_node *kn;
3093 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04003094 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003095
Tejun Heo2bd59d42014-02-11 11:52:49 -05003096#ifdef CONFIG_DEBUG_LOCK_ALLOC
3097 key = &cft->lockdep_key;
3098#endif
3099 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3100 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08003101 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04003102 if (IS_ERR(kn))
3103 return PTR_ERR(kn);
3104
3105 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003106 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04003107 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003108 return ret;
3109 }
3110
Tejun Heo187fe842015-06-18 16:54:28 -04003111 if (cft->write == cgroup_procs_write)
3112 cgrp->procs_kn = kn;
3113 else if (cft->seq_show == cgroup_populated_show)
Tejun Heo842b5972014-04-25 18:28:02 -04003114 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04003115 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003116}
3117
Tejun Heob1f28d32013-06-28 16:24:10 -07003118/**
3119 * cgroup_addrm_files - add or remove files to a cgroup directory
3120 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07003121 * @cfts: array of cftypes to be added
3122 * @is_add: whether to add or remove
3123 *
3124 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04003125 * For removals, this function never fails. If addition fails, this
3126 * function doesn't remove files already added. The caller is responsible
3127 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07003128 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04003129static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
3130 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003131{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003132 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07003133 int ret;
3134
Tejun Heo01f64742014-05-13 12:19:23 -04003135 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07003136
3137 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08003138 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003139 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04003140 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003141 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07003142 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003143 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003144 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04003145 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08003146 continue;
3147
Li Zefan2739d3c2013-01-21 18:18:33 +08003148 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003149 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07003150 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04003151 pr_warn("%s: failed to add %s, err=%d\n",
3152 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07003153 return ret;
3154 }
Li Zefan2739d3c2013-01-21 18:18:33 +08003155 } else {
3156 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07003157 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003158 }
Tejun Heob1f28d32013-06-28 16:24:10 -07003159 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003160}
3161
Tejun Heo21a2d342014-02-12 09:29:49 -05003162static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003163{
3164 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04003165 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003166 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04003167 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07003168 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003169
Tejun Heo01f64742014-05-13 12:19:23 -04003170 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08003171
Li Zefane8c82d22013-06-18 18:48:37 +08003172 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04003173 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04003174 struct cgroup *cgrp = css->cgroup;
3175
Li Zefane8c82d22013-06-18 18:48:37 +08003176 if (cgroup_is_dead(cgrp))
3177 continue;
3178
Tejun Heo21a2d342014-02-12 09:29:49 -05003179 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07003180 if (ret)
3181 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003182 }
Tejun Heo21a2d342014-02-12 09:29:49 -05003183
3184 if (is_add && !ret)
3185 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07003186 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003187}
3188
Tejun Heo2da440a2014-02-11 11:52:48 -05003189static void cgroup_exit_cftypes(struct cftype *cfts)
3190{
3191 struct cftype *cft;
3192
Tejun Heo2bd59d42014-02-11 11:52:49 -05003193 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3194 /* free copy for custom atomic_write_len, see init_cftypes() */
3195 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3196 kfree(cft->kf_ops);
3197 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05003198 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003199
3200 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003201 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003202 }
Tejun Heo2da440a2014-02-11 11:52:48 -05003203}
3204
Tejun Heo2bd59d42014-02-11 11:52:49 -05003205static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05003206{
3207 struct cftype *cft;
3208
Tejun Heo2bd59d42014-02-11 11:52:49 -05003209 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3210 struct kernfs_ops *kf_ops;
3211
Tejun Heo0adb0702014-02-12 09:29:48 -05003212 WARN_ON(cft->ss || cft->kf_ops);
3213
Tejun Heo2bd59d42014-02-11 11:52:49 -05003214 if (cft->seq_start)
3215 kf_ops = &cgroup_kf_ops;
3216 else
3217 kf_ops = &cgroup_kf_single_ops;
3218
3219 /*
3220 * Ugh... if @cft wants a custom max_write_len, we need to
3221 * make a copy of kf_ops to set its atomic_write_len.
3222 */
3223 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3224 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3225 if (!kf_ops) {
3226 cgroup_exit_cftypes(cfts);
3227 return -ENOMEM;
3228 }
3229 kf_ops->atomic_write_len = cft->max_write_len;
3230 }
3231
3232 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003233 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003234 }
3235
3236 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003237}
3238
Tejun Heo21a2d342014-02-12 09:29:49 -05003239static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3240{
Tejun Heo01f64742014-05-13 12:19:23 -04003241 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003242
3243 if (!cfts || !cfts[0].ss)
3244 return -ENOENT;
3245
3246 list_del(&cfts->node);
3247 cgroup_apply_cftypes(cfts, false);
3248 cgroup_exit_cftypes(cfts);
3249 return 0;
3250}
3251
Tejun Heo8e3f6542012-04-01 12:09:55 -07003252/**
Tejun Heo80b13582014-02-12 09:29:48 -05003253 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3254 * @cfts: zero-length name terminated array of cftypes
3255 *
3256 * Unregister @cfts. Files described by @cfts are removed from all
3257 * existing cgroups and all future cgroups won't have them either. This
3258 * function can be called anytime whether @cfts' subsys is attached or not.
3259 *
3260 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3261 * registered.
3262 */
3263int cgroup_rm_cftypes(struct cftype *cfts)
3264{
Tejun Heo21a2d342014-02-12 09:29:49 -05003265 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003266
Tejun Heo01f64742014-05-13 12:19:23 -04003267 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003268 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04003269 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003270 return ret;
3271}
3272
3273/**
3274 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3275 * @ss: target cgroup subsystem
3276 * @cfts: zero-length name terminated array of cftypes
3277 *
3278 * Register @cfts to @ss. Files described by @cfts are created for all
3279 * existing cgroups to which @ss is attached and all future cgroups will
3280 * have them too. This function can be called anytime whether @ss is
3281 * attached or not.
3282 *
3283 * Returns 0 on successful registration, -errno on failure. Note that this
3284 * function currently returns 0 as long as @cfts registration is successful
3285 * even if some file creation attempts on existing cgroups fail.
3286 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003287static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003288{
Tejun Heo9ccece82013-06-28 16:24:11 -07003289 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003290
Li Zefanc731ae12014-06-05 17:16:30 +08003291 if (ss->disabled)
3292 return 0;
3293
Li Zefandc5736e2014-02-17 10:41:50 +08003294 if (!cfts || cfts[0].name[0] == '\0')
3295 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003296
Tejun Heo2bd59d42014-02-11 11:52:49 -05003297 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003298 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003299 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003300
Tejun Heo01f64742014-05-13 12:19:23 -04003301 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05003302
Tejun Heo0adb0702014-02-12 09:29:48 -05003303 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003304 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003305 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003306 cgroup_rm_cftypes_locked(cfts);
3307
Tejun Heo01f64742014-05-13 12:19:23 -04003308 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003309 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003310}
Tejun Heo79578622012-04-01 12:09:56 -07003311
3312/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04003313 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3314 * @ss: target cgroup subsystem
3315 * @cfts: zero-length name terminated array of cftypes
3316 *
3317 * Similar to cgroup_add_cftypes() but the added files are only used for
3318 * the default hierarchy.
3319 */
3320int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3321{
3322 struct cftype *cft;
3323
3324 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04003325 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04003326 return cgroup_add_cftypes(ss, cfts);
3327}
3328
3329/**
3330 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3331 * @ss: target cgroup subsystem
3332 * @cfts: zero-length name terminated array of cftypes
3333 *
3334 * Similar to cgroup_add_cftypes() but the added files are only used for
3335 * the legacy hierarchies.
3336 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04003337int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3338{
Tejun Heoa8ddc822014-07-15 11:05:10 -04003339 struct cftype *cft;
3340
Vivek Goyalfa8137b2014-08-08 11:44:03 -04003341 /*
3342 * If legacy_flies_on_dfl, we want to show the legacy files on the
3343 * dfl hierarchy but iff the target subsystem hasn't been updated
3344 * for the dfl hierarchy yet.
3345 */
3346 if (!cgroup_legacy_files_on_dfl ||
3347 ss->dfl_cftypes != ss->legacy_cftypes) {
3348 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3349 cft->flags |= __CFTYPE_NOT_ON_DFL;
3350 }
3351
Tejun Heo2cf669a2014-07-15 11:05:09 -04003352 return cgroup_add_cftypes(ss, cfts);
3353}
3354
Li Zefana043e3b2008-02-23 15:24:09 -08003355/**
3356 * cgroup_task_count - count the number of tasks in a cgroup.
3357 * @cgrp: the cgroup in question
3358 *
3359 * Return the number of tasks in the cgroup.
3360 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003361static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003362{
3363 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003364 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003365
Tejun Heo96d365e2014-02-13 06:58:40 -05003366 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003367 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3368 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003369 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003370 return count;
3371}
3372
Tejun Heo574bd9f2012-11-09 09:12:29 -08003373/**
Tejun Heo492eb212013-08-08 20:11:25 -04003374 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04003375 * @pos: the current position (%NULL to initiate traversal)
3376 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003377 *
Tejun Heoc2931b72014-05-16 13:22:51 -04003378 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003379 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04003380 * that @parent and @pos are accessible. The next sibling is guaranteed to
3381 * be returned regardless of their states.
3382 *
3383 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3384 * css which finished ->css_online() is guaranteed to be visible in the
3385 * future iterations and will stay visible until the last reference is put.
3386 * A css which hasn't finished ->css_online() or already finished
3387 * ->css_offline() may show up during traversal. It's each subsystem's
3388 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09003389 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003390struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3391 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09003392{
Tejun Heoc2931b72014-05-16 13:22:51 -04003393 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003394
Tejun Heo8353da12014-05-13 12:19:23 -04003395 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003396
3397 /*
Tejun Heode3f0342014-05-16 13:22:49 -04003398 * @pos could already have been unlinked from the sibling list.
3399 * Once a cgroup is removed, its ->sibling.next is no longer
3400 * updated when its next sibling changes. CSS_RELEASED is set when
3401 * @pos is taken off list, at which time its next pointer is valid,
3402 * and, as releases are serialized, the one pointed to by the next
3403 * pointer is guaranteed to not have started release yet. This
3404 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3405 * critical section, the one pointed to by its next pointer is
3406 * guaranteed to not have finished its RCU grace period even if we
3407 * have dropped rcu_read_lock() inbetween iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04003408 *
Tejun Heode3f0342014-05-16 13:22:49 -04003409 * If @pos has CSS_RELEASED set, its next pointer can't be
3410 * dereferenced; however, as each css is given a monotonically
3411 * increasing unique serial number and always appended to the
3412 * sibling list, the next one can be found by walking the parent's
3413 * children until the first css with higher serial number than
3414 * @pos's. While this path can be slower, it happens iff iteration
3415 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09003416 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003417 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04003418 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3419 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3420 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003421 } else {
Tejun Heoc2931b72014-05-16 13:22:51 -04003422 list_for_each_entry_rcu(next, &parent->children, sibling)
Tejun Heo3b287a52013-08-08 20:11:24 -04003423 if (next->serial_nr > pos->serial_nr)
3424 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003425 }
3426
Tejun Heo3b281af2014-04-23 11:13:15 -04003427 /*
3428 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04003429 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04003430 */
Tejun Heoc2931b72014-05-16 13:22:51 -04003431 if (&next->sibling != &parent->children)
3432 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04003433 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003434}
Tejun Heo53fa5262013-05-24 10:55:38 +09003435
3436/**
Tejun Heo492eb212013-08-08 20:11:25 -04003437 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003438 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003439 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003440 *
Tejun Heo492eb212013-08-08 20:11:25 -04003441 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003442 * to visit for pre-order traversal of @root's descendants. @root is
3443 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003444 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003445 * While this function requires cgroup_mutex or RCU read locking, it
3446 * doesn't require the whole traversal to be contained in a single critical
3447 * section. This function will return the correct next descendant as long
3448 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04003449 *
3450 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3451 * css which finished ->css_online() is guaranteed to be visible in the
3452 * future iterations and will stay visible until the last reference is put.
3453 * A css which hasn't finished ->css_online() or already finished
3454 * ->css_offline() may show up during traversal. It's each subsystem's
3455 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003456 */
Tejun Heo492eb212013-08-08 20:11:25 -04003457struct cgroup_subsys_state *
3458css_next_descendant_pre(struct cgroup_subsys_state *pos,
3459 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003460{
Tejun Heo492eb212013-08-08 20:11:25 -04003461 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003462
Tejun Heo8353da12014-05-13 12:19:23 -04003463 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003464
Tejun Heobd8815a2013-08-08 20:11:27 -04003465 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003466 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003467 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003468
3469 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003470 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003471 if (next)
3472 return next;
3473
3474 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003475 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04003476 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003477 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003478 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04003479 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003480 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003481
3482 return NULL;
3483}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003484
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003485/**
Tejun Heo492eb212013-08-08 20:11:25 -04003486 * css_rightmost_descendant - return the rightmost descendant of a css
3487 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003488 *
Tejun Heo492eb212013-08-08 20:11:25 -04003489 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3490 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003491 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003492 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003493 * While this function requires cgroup_mutex or RCU read locking, it
3494 * doesn't require the whole traversal to be contained in a single critical
3495 * section. This function will return the correct rightmost descendant as
3496 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003497 */
Tejun Heo492eb212013-08-08 20:11:25 -04003498struct cgroup_subsys_state *
3499css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003500{
Tejun Heo492eb212013-08-08 20:11:25 -04003501 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003502
Tejun Heo8353da12014-05-13 12:19:23 -04003503 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003504
3505 do {
3506 last = pos;
3507 /* ->prev isn't RCU safe, walk ->next till the end */
3508 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003509 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003510 pos = tmp;
3511 } while (pos);
3512
3513 return last;
3514}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003515
Tejun Heo492eb212013-08-08 20:11:25 -04003516static struct cgroup_subsys_state *
3517css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003518{
Tejun Heo492eb212013-08-08 20:11:25 -04003519 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003520
3521 do {
3522 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003523 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003524 } while (pos);
3525
3526 return last;
3527}
3528
3529/**
Tejun Heo492eb212013-08-08 20:11:25 -04003530 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003531 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003532 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003533 *
Tejun Heo492eb212013-08-08 20:11:25 -04003534 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003535 * to visit for post-order traversal of @root's descendants. @root is
3536 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003537 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003538 * While this function requires cgroup_mutex or RCU read locking, it
3539 * doesn't require the whole traversal to be contained in a single critical
3540 * section. This function will return the correct next descendant as long
3541 * as both @pos and @cgroup are accessible and @pos is a descendant of
3542 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04003543 *
3544 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3545 * css which finished ->css_online() is guaranteed to be visible in the
3546 * future iterations and will stay visible until the last reference is put.
3547 * A css which hasn't finished ->css_online() or already finished
3548 * ->css_offline() may show up during traversal. It's each subsystem's
3549 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003550 */
Tejun Heo492eb212013-08-08 20:11:25 -04003551struct cgroup_subsys_state *
3552css_next_descendant_post(struct cgroup_subsys_state *pos,
3553 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003554{
Tejun Heo492eb212013-08-08 20:11:25 -04003555 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003556
Tejun Heo8353da12014-05-13 12:19:23 -04003557 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003558
Tejun Heo58b79a92013-09-06 15:31:08 -04003559 /* if first iteration, visit leftmost descendant which may be @root */
3560 if (!pos)
3561 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003562
Tejun Heobd8815a2013-08-08 20:11:27 -04003563 /* if we visited @root, we're done */
3564 if (pos == root)
3565 return NULL;
3566
Tejun Heo574bd9f2012-11-09 09:12:29 -08003567 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003568 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003569 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003570 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003571
3572 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04003573 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003574}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003575
Tejun Heof3d46502014-05-16 13:22:52 -04003576/**
3577 * css_has_online_children - does a css have online children
3578 * @css: the target css
3579 *
3580 * Returns %true if @css has any online children; otherwise, %false. This
3581 * function can be called from any context but the caller is responsible
3582 * for synchronizing against on/offlining as necessary.
3583 */
3584bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04003585{
Tejun Heof3d46502014-05-16 13:22:52 -04003586 struct cgroup_subsys_state *child;
3587 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04003588
3589 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04003590 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08003591 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04003592 ret = true;
3593 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04003594 }
3595 }
3596 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04003597 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003598}
3599
Tejun Heo0942eee2013-08-08 20:11:26 -04003600/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003601 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003602 * @it: the iterator to advance
3603 *
3604 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003605 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003606static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003607{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003608 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003609 struct cgrp_cset_link *link;
3610 struct css_set *cset;
3611
3612 /* Advance to the next non-empty css_set */
3613 do {
3614 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003615 if (l == it->cset_head) {
3616 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003617 return;
3618 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003619
3620 if (it->ss) {
3621 cset = container_of(l, struct css_set,
3622 e_cset_node[it->ss->id]);
3623 } else {
3624 link = list_entry(l, struct cgrp_cset_link, cset_link);
3625 cset = link->cset;
3626 }
Tejun Heoc7561122014-02-25 10:04:01 -05003627 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3628
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003629 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003630
3631 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003632 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003633 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003634 it->task_pos = cset->mg_tasks.next;
3635
3636 it->tasks_head = &cset->tasks;
3637 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003638}
3639
Tejun Heo0942eee2013-08-08 20:11:26 -04003640/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003641 * css_task_iter_start - initiate task iteration
3642 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003643 * @it: the task iterator to use
3644 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003645 * Initiate iteration through the tasks of @css. The caller can call
3646 * css_task_iter_next() to walk through the tasks until the function
3647 * returns NULL. On completion of iteration, css_task_iter_end() must be
3648 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003649 *
3650 * Note that this function acquires a lock which is released when the
3651 * iteration finishes. The caller can't sleep while iteration is in
3652 * progress.
3653 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003654void css_task_iter_start(struct cgroup_subsys_state *css,
3655 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003656 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003657{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003658 /* no one should try to iterate before mounting cgroups */
3659 WARN_ON_ONCE(!use_task_css_set_links);
Paul Menage817929e2007-10-18 23:39:36 -07003660
Tejun Heo96d365e2014-02-13 06:58:40 -05003661 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003662
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003663 it->ss = css->ss;
3664
3665 if (it->ss)
3666 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3667 else
3668 it->cset_pos = &css->cgroup->cset_links;
3669
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003670 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003671
Tejun Heo72ec7022013-08-08 20:11:26 -04003672 css_advance_task_iter(it);
Paul Menagebd89aab2007-10-18 23:40:44 -07003673}
Paul Menage817929e2007-10-18 23:39:36 -07003674
Tejun Heo0942eee2013-08-08 20:11:26 -04003675/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003676 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003677 * @it: the task iterator being iterated
3678 *
3679 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003680 * initialized via css_task_iter_start(). Returns NULL when the iteration
3681 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003682 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003683struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003684{
3685 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003686 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003687
3688 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003689 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003690 return NULL;
3691 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003692
3693 /*
3694 * Advance iterator to find next entry. cset->tasks is consumed
3695 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3696 * next cset.
3697 */
Paul Menage817929e2007-10-18 23:39:36 -07003698 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003699
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003700 if (l == it->tasks_head)
3701 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003702
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003703 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003704 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003705 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003706 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003707
Paul Menage817929e2007-10-18 23:39:36 -07003708 return res;
3709}
3710
Tejun Heo0942eee2013-08-08 20:11:26 -04003711/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003712 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003713 * @it: the task iterator to finish
3714 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003715 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003716 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003717void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003718 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003719{
Tejun Heo96d365e2014-02-13 06:58:40 -05003720 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003721}
3722
3723/**
3724 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3725 * @to: cgroup to which the tasks will be moved
3726 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003727 *
3728 * Locking rules between cgroup_post_fork() and the migration path
3729 * guarantee that, if a task is forking while being migrated, the new child
3730 * is guaranteed to be either visible in the source cgroup after the
3731 * parent's migration is complete or put into the target cgroup. No task
3732 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003733 */
3734int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3735{
Tejun Heo952aaa12014-02-25 10:04:03 -05003736 LIST_HEAD(preloaded_csets);
3737 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003738 struct css_task_iter it;
3739 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003740 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003741
Tejun Heo952aaa12014-02-25 10:04:03 -05003742 mutex_lock(&cgroup_mutex);
3743
3744 /* all tasks in @from are being moved, all csets are source */
3745 down_read(&css_set_rwsem);
3746 list_for_each_entry(link, &from->cset_links, cset_link)
3747 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3748 up_read(&css_set_rwsem);
3749
3750 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3751 if (ret)
3752 goto out_err;
3753
3754 /*
3755 * Migrate tasks one-by-one until @form is empty. This fails iff
3756 * ->can_attach() fails.
3757 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003758 do {
Tejun Heo9d800df2014-05-14 09:15:00 -04003759 css_task_iter_start(&from->self, &it);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003760 task = css_task_iter_next(&it);
3761 if (task)
3762 get_task_struct(task);
3763 css_task_iter_end(&it);
3764
3765 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003766 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003767 put_task_struct(task);
3768 }
3769 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003770out_err:
3771 cgroup_migrate_finish(&preloaded_csets);
3772 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003773 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003774}
3775
Paul Menage817929e2007-10-18 23:39:36 -07003776/*
Ben Blum102a7752009-09-23 15:56:26 -07003777 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003778 *
3779 * Reading this file can return large amounts of data if a cgroup has
3780 * *lots* of attached tasks. So it may need several calls to read(),
3781 * but we cannot guarantee that the information we produce is correct
3782 * unless we produce it entirely atomically.
3783 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003784 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003785
Li Zefan24528252012-01-20 11:58:43 +08003786/* which pidlist file are we talking about? */
3787enum cgroup_filetype {
3788 CGROUP_FILE_PROCS,
3789 CGROUP_FILE_TASKS,
3790};
3791
3792/*
3793 * A pidlist is a list of pids that virtually represents the contents of one
3794 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3795 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3796 * to the cgroup.
3797 */
3798struct cgroup_pidlist {
3799 /*
3800 * used to find which pidlist is wanted. doesn't change as long as
3801 * this particular list stays in the list.
3802 */
3803 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3804 /* array of xids */
3805 pid_t *list;
3806 /* how many elements the above list has */
3807 int length;
Li Zefan24528252012-01-20 11:58:43 +08003808 /* each of these stored in a list by its cgroup */
3809 struct list_head links;
3810 /* pointer to the cgroup we belong to, for list removal purposes */
3811 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003812 /* for delayed destruction */
3813 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003814};
3815
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003817 * The following two functions "fix" the issue where there are more pids
3818 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3819 * TODO: replace with a kernel-wide solution to this problem
3820 */
3821#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3822static void *pidlist_allocate(int count)
3823{
3824 if (PIDLIST_TOO_LARGE(count))
3825 return vmalloc(count * sizeof(pid_t));
3826 else
3827 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3828}
Tejun Heob1a21362013-11-29 10:42:58 -05003829
Ben Blumd1d9fd32009-09-23 15:56:28 -07003830static void pidlist_free(void *p)
3831{
Bandan Das58794512015-03-02 17:51:10 -05003832 kvfree(p);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003833}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003834
3835/*
Tejun Heob1a21362013-11-29 10:42:58 -05003836 * Used to destroy all pidlists lingering waiting for destroy timer. None
3837 * should be left afterwards.
3838 */
3839static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3840{
3841 struct cgroup_pidlist *l, *tmp_l;
3842
3843 mutex_lock(&cgrp->pidlist_mutex);
3844 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3845 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3846 mutex_unlock(&cgrp->pidlist_mutex);
3847
3848 flush_workqueue(cgroup_pidlist_destroy_wq);
3849 BUG_ON(!list_empty(&cgrp->pidlists));
3850}
3851
3852static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3853{
3854 struct delayed_work *dwork = to_delayed_work(work);
3855 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3856 destroy_dwork);
3857 struct cgroup_pidlist *tofree = NULL;
3858
3859 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003860
3861 /*
Tejun Heo04502362013-11-29 10:42:59 -05003862 * Destroy iff we didn't get queued again. The state won't change
3863 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003864 */
Tejun Heo04502362013-11-29 10:42:59 -05003865 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003866 list_del(&l->links);
3867 pidlist_free(l->list);
3868 put_pid_ns(l->key.ns);
3869 tofree = l;
3870 }
3871
Tejun Heob1a21362013-11-29 10:42:58 -05003872 mutex_unlock(&l->owner->pidlist_mutex);
3873 kfree(tofree);
3874}
3875
3876/*
Ben Blum102a7752009-09-23 15:56:26 -07003877 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003878 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003879 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003880static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003881{
Ben Blum102a7752009-09-23 15:56:26 -07003882 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003883
3884 /*
3885 * we presume the 0th element is unique, so i starts at 1. trivial
3886 * edge cases first; no work needs to be done for either
3887 */
3888 if (length == 0 || length == 1)
3889 return length;
3890 /* src and dest walk down the list; dest counts unique elements */
3891 for (src = 1; src < length; src++) {
3892 /* find next unique element */
3893 while (list[src] == list[src-1]) {
3894 src++;
3895 if (src == length)
3896 goto after;
3897 }
3898 /* dest always points to where the next unique element goes */
3899 list[dest] = list[src];
3900 dest++;
3901 }
3902after:
Ben Blum102a7752009-09-23 15:56:26 -07003903 return dest;
3904}
3905
Tejun Heoafb2bc12013-11-29 10:42:59 -05003906/*
3907 * The two pid files - task and cgroup.procs - guaranteed that the result
3908 * is sorted, which forced this whole pidlist fiasco. As pid order is
3909 * different per namespace, each namespace needs differently sorted list,
3910 * making it impossible to use, for example, single rbtree of member tasks
3911 * sorted by task pointer. As pidlists can be fairly large, allocating one
3912 * per open file is dangerous, so cgroup had to implement shared pool of
3913 * pidlists keyed by cgroup and namespace.
3914 *
3915 * All this extra complexity was caused by the original implementation
3916 * committing to an entirely unnecessary property. In the long term, we
Tejun Heoaa6ec292014-07-09 10:08:08 -04003917 * want to do away with it. Explicitly scramble sort order if on the
3918 * default hierarchy so that no such expectation exists in the new
3919 * interface.
Tejun Heoafb2bc12013-11-29 10:42:59 -05003920 *
3921 * Scrambling is done by swapping every two consecutive bits, which is
3922 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3923 */
3924static pid_t pid_fry(pid_t pid)
3925{
3926 unsigned a = pid & 0x55555555;
3927 unsigned b = pid & 0xAAAAAAAA;
3928
3929 return (a << 1) | (b >> 1);
3930}
3931
3932static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3933{
Tejun Heoaa6ec292014-07-09 10:08:08 -04003934 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05003935 return pid_fry(pid);
3936 else
3937 return pid;
3938}
3939
Ben Blum102a7752009-09-23 15:56:26 -07003940static int cmppid(const void *a, const void *b)
3941{
3942 return *(pid_t *)a - *(pid_t *)b;
3943}
3944
Tejun Heoafb2bc12013-11-29 10:42:59 -05003945static int fried_cmppid(const void *a, const void *b)
3946{
3947 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3948}
3949
Ben Blum72a8cb32009-09-23 15:56:27 -07003950static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3951 enum cgroup_filetype type)
3952{
3953 struct cgroup_pidlist *l;
3954 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003955 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003956
Tejun Heoe6b81712013-11-29 10:42:59 -05003957 lockdep_assert_held(&cgrp->pidlist_mutex);
3958
3959 list_for_each_entry(l, &cgrp->pidlists, links)
3960 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003961 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003962 return NULL;
3963}
3964
Ben Blum72a8cb32009-09-23 15:56:27 -07003965/*
3966 * find the appropriate pidlist for our purpose (given procs vs tasks)
3967 * returns with the lock on that pidlist already held, and takes care
3968 * of the use count, or returns NULL with no locks held if we're out of
3969 * memory.
3970 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003971static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3972 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003973{
3974 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003975
Tejun Heoe6b81712013-11-29 10:42:59 -05003976 lockdep_assert_held(&cgrp->pidlist_mutex);
3977
3978 l = cgroup_pidlist_find(cgrp, type);
3979 if (l)
3980 return l;
3981
Ben Blum72a8cb32009-09-23 15:56:27 -07003982 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003983 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003984 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003985 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003986
Tejun Heob1a21362013-11-29 10:42:58 -05003987 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003988 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003989 /* don't need task_nsproxy() if we're looking at ourself */
3990 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003991 l->owner = cgrp;
3992 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003993 return l;
3994}
3995
3996/*
Ben Blum102a7752009-09-23 15:56:26 -07003997 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3998 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003999static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
4000 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07004001{
4002 pid_t *array;
4003 int length;
4004 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04004005 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07004006 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07004007 struct cgroup_pidlist *l;
4008
Tejun Heo4bac00d2013-11-29 10:42:59 -05004009 lockdep_assert_held(&cgrp->pidlist_mutex);
4010
Ben Blum102a7752009-09-23 15:56:26 -07004011 /*
4012 * If cgroup gets more users after we read count, we won't have
4013 * enough space - tough. This race is indistinguishable to the
4014 * caller from the case that the additional cgroup users didn't
4015 * show up until sometime later on.
4016 */
4017 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07004018 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07004019 if (!array)
4020 return -ENOMEM;
4021 /* now, populate the array */
Tejun Heo9d800df2014-05-14 09:15:00 -04004022 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004023 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07004024 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07004025 break;
Ben Blum102a7752009-09-23 15:56:26 -07004026 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07004027 if (type == CGROUP_FILE_PROCS)
4028 pid = task_tgid_vnr(tsk);
4029 else
4030 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07004031 if (pid > 0) /* make sure to only use valid results */
4032 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07004033 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004034 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07004035 length = n;
4036 /* now sort & (if procs) strip out duplicates */
Tejun Heoaa6ec292014-07-09 10:08:08 -04004037 if (cgroup_on_dfl(cgrp))
Tejun Heoafb2bc12013-11-29 10:42:59 -05004038 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
4039 else
4040 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07004041 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07004042 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05004043
Tejun Heoe6b81712013-11-29 10:42:59 -05004044 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07004045 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07004046 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07004047 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07004048 }
Tejun Heoe6b81712013-11-29 10:42:59 -05004049
4050 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07004051 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07004052 l->list = array;
4053 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07004054 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07004055 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004056}
4057
Balbir Singh846c7bb2007-10-18 23:39:44 -07004058/**
Li Zefana043e3b2008-02-23 15:24:09 -08004059 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07004060 * @stats: cgroupstats to fill information into
4061 * @dentry: A dentry entry belonging to the cgroup for which stats have
4062 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08004063 *
4064 * Build and fill cgroupstats so that taskstats can export it to user
4065 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07004066 */
4067int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
4068{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004069 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07004070 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04004071 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004072 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08004073
Tejun Heo2bd59d42014-02-11 11:52:49 -05004074 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4075 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4076 kernfs_type(kn) != KERNFS_DIR)
4077 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004078
Li Zefanbad34662014-02-14 16:54:28 +08004079 mutex_lock(&cgroup_mutex);
4080
Tejun Heo2bd59d42014-02-11 11:52:49 -05004081 /*
4082 * We aren't being called from kernfs and there's no guarantee on
Tejun Heoec903c02014-05-13 12:11:01 -04004083 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
Tejun Heo2bd59d42014-02-11 11:52:49 -05004084 * @kn->priv is RCU safe. Let's do the RCU dancing.
4085 */
4086 rcu_read_lock();
4087 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08004088 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05004089 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08004090 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004091 return -ENOENT;
4092 }
Li Zefanbad34662014-02-14 16:54:28 +08004093 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07004094
Tejun Heo9d800df2014-05-14 09:15:00 -04004095 css_task_iter_start(&cgrp->self, &it);
Tejun Heo72ec7022013-08-08 20:11:26 -04004096 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07004097 switch (tsk->state) {
4098 case TASK_RUNNING:
4099 stats->nr_running++;
4100 break;
4101 case TASK_INTERRUPTIBLE:
4102 stats->nr_sleeping++;
4103 break;
4104 case TASK_UNINTERRUPTIBLE:
4105 stats->nr_uninterruptible++;
4106 break;
4107 case TASK_STOPPED:
4108 stats->nr_stopped++;
4109 break;
4110 default:
4111 if (delayacct_is_task_waiting_on_io(tsk))
4112 stats->nr_io_wait++;
4113 break;
4114 }
4115 }
Tejun Heo72ec7022013-08-08 20:11:26 -04004116 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07004117
Li Zefanbad34662014-02-14 16:54:28 +08004118 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004119 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07004120}
4121
Paul Menage8f3ff202009-09-23 15:56:25 -07004122
Paul Menagecc31edc2008-10-18 20:28:04 -07004123/*
Ben Blum102a7752009-09-23 15:56:26 -07004124 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07004125 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07004126 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07004127 */
4128
Ben Blum102a7752009-09-23 15:56:26 -07004129static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004130{
4131 /*
4132 * Initially we receive a position value that corresponds to
4133 * one more than the last pid shown (or 0 on the first call or
4134 * after a seek to the start). Use a binary-search to find the
4135 * next pid to display, if any
4136 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004137 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004138 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004139 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05004140 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07004141 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004142 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07004143
Tejun Heo4bac00d2013-11-29 10:42:59 -05004144 mutex_lock(&cgrp->pidlist_mutex);
4145
4146 /*
Tejun Heo5d224442013-12-05 12:28:04 -05004147 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05004148 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05004149 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05004150 * could already have been destroyed.
4151 */
Tejun Heo5d224442013-12-05 12:28:04 -05004152 if (of->priv)
4153 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004154
4155 /*
4156 * Either this is the first start() after open or the matching
4157 * pidlist has been destroyed inbetween. Create a new one.
4158 */
Tejun Heo5d224442013-12-05 12:28:04 -05004159 if (!of->priv) {
4160 ret = pidlist_array_load(cgrp, type,
4161 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05004162 if (ret)
4163 return ERR_PTR(ret);
4164 }
Tejun Heo5d224442013-12-05 12:28:04 -05004165 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004166
Paul Menagecc31edc2008-10-18 20:28:04 -07004167 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07004168 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11004169
Paul Menagecc31edc2008-10-18 20:28:04 -07004170 while (index < end) {
4171 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004172 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07004173 index = mid;
4174 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004175 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07004176 index = mid + 1;
4177 else
4178 end = mid;
4179 }
4180 }
4181 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07004182 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07004183 return NULL;
4184 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07004185 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05004186 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07004187 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004188}
4189
Ben Blum102a7752009-09-23 15:56:26 -07004190static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004191{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004192 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004193 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05004194
Tejun Heo5d224442013-12-05 12:28:04 -05004195 if (l)
4196 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05004197 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05004198 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07004199}
4200
Ben Blum102a7752009-09-23 15:56:26 -07004201static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07004202{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004203 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05004204 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07004205 pid_t *p = v;
4206 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07004207 /*
4208 * Advance to the next pid in the array. If this goes off the
4209 * end, we're done
4210 */
4211 p++;
4212 if (p >= end) {
4213 return NULL;
4214 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05004215 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07004216 return p;
4217 }
4218}
4219
Ben Blum102a7752009-09-23 15:56:26 -07004220static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004221{
Joe Perches94ff2122015-04-15 16:18:20 -07004222 seq_printf(s, "%d\n", *(int *)v);
4223
4224 return 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07004225}
4226
Tejun Heo182446d2013-08-08 20:11:24 -04004227static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
4228 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004229{
Tejun Heo182446d2013-08-08 20:11:24 -04004230 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004231}
4232
Tejun Heo182446d2013-08-08 20:11:24 -04004233static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
4234 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07004235{
Paul Menage6379c102008-07-25 01:47:01 -07004236 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004237 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004238 else
Tejun Heo182446d2013-08-08 20:11:24 -04004239 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07004240 return 0;
4241}
4242
Tejun Heo182446d2013-08-08 20:11:24 -04004243static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4244 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004245{
Tejun Heo182446d2013-08-08 20:11:24 -04004246 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004247}
4248
Tejun Heo182446d2013-08-08 20:11:24 -04004249static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4250 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004251{
4252 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004253 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004254 else
Tejun Heo182446d2013-08-08 20:11:24 -04004255 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004256 return 0;
4257}
4258
Tejun Heoa14c6872014-07-15 11:05:09 -04004259/* cgroup core interface files for the default hierarchy */
4260static struct cftype cgroup_dfl_base_files[] = {
4261 {
4262 .name = "cgroup.procs",
4263 .seq_start = cgroup_pidlist_start,
4264 .seq_next = cgroup_pidlist_next,
4265 .seq_stop = cgroup_pidlist_stop,
4266 .seq_show = cgroup_pidlist_show,
4267 .private = CGROUP_FILE_PROCS,
4268 .write = cgroup_procs_write,
4269 .mode = S_IRUGO | S_IWUSR,
4270 },
4271 {
4272 .name = "cgroup.controllers",
4273 .flags = CFTYPE_ONLY_ON_ROOT,
4274 .seq_show = cgroup_root_controllers_show,
4275 },
4276 {
4277 .name = "cgroup.controllers",
4278 .flags = CFTYPE_NOT_ON_ROOT,
4279 .seq_show = cgroup_controllers_show,
4280 },
4281 {
4282 .name = "cgroup.subtree_control",
4283 .seq_show = cgroup_subtree_control_show,
4284 .write = cgroup_subtree_control_write,
4285 },
4286 {
4287 .name = "cgroup.populated",
4288 .flags = CFTYPE_NOT_ON_ROOT,
4289 .seq_show = cgroup_populated_show,
4290 },
4291 { } /* terminate */
4292};
4293
4294/* cgroup core interface files for the legacy hierarchies */
4295static struct cftype cgroup_legacy_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004296 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004297 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05004298 .seq_start = cgroup_pidlist_start,
4299 .seq_next = cgroup_pidlist_next,
4300 .seq_stop = cgroup_pidlist_stop,
4301 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004302 .private = CGROUP_FILE_PROCS,
Tejun Heoacbef752014-05-13 12:16:22 -04004303 .write = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07004304 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004305 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004306 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07004307 .name = "cgroup.clone_children",
4308 .read_u64 = cgroup_clone_children_read,
4309 .write_u64 = cgroup_clone_children_write,
4310 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004311 {
Tejun Heo873fe092013-04-14 20:15:26 -07004312 .name = "cgroup.sane_behavior",
4313 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004314 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07004315 },
Tejun Heof8f22e52014-04-23 11:13:16 -04004316 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004317 .name = "tasks",
Tejun Heo6612f052013-12-05 12:28:04 -05004318 .seq_start = cgroup_pidlist_start,
4319 .seq_next = cgroup_pidlist_next,
4320 .seq_stop = cgroup_pidlist_stop,
4321 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004322 .private = CGROUP_FILE_TASKS,
Tejun Heoacbef752014-05-13 12:16:22 -04004323 .write = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004324 .mode = S_IRUGO | S_IWUSR,
4325 },
4326 {
4327 .name = "notify_on_release",
Tejun Heod5c56ce2013-06-03 19:14:34 -07004328 .read_u64 = cgroup_read_notify_on_release,
4329 .write_u64 = cgroup_write_notify_on_release,
4330 },
Tejun Heo873fe092013-04-14 20:15:26 -07004331 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004332 .name = "release_agent",
Tejun Heoa14c6872014-07-15 11:05:09 -04004333 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004334 .seq_show = cgroup_release_agent_show,
Tejun Heo451af502014-05-13 12:16:21 -04004335 .write = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004336 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004337 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004338 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004339};
4340
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004341/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004342 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004343 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004344 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004345 *
4346 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004347 */
Aleksa Sarai8ab456a2015-05-19 00:51:00 +10004348static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004350 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004351 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004352
Tejun Heo8e3f6542012-04-01 12:09:55 -07004353 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004354 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004355 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004356
Tejun Heo69dfa002014-05-04 15:09:13 -04004357 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004358 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004359
Tejun Heo0adb0702014-02-12 09:29:48 -05004360 list_for_each_entry(cfts, &ss->cfts, node) {
4361 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004362 if (ret < 0)
4363 goto err;
4364 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004367err:
4368 cgroup_clear_dir(cgrp, subsys_mask);
4369 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370}
4371
Tejun Heo0c21ead2013-08-13 20:22:51 -04004372/*
4373 * css destruction is four-stage process.
4374 *
4375 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4376 * Implemented in kill_css().
4377 *
4378 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04004379 * and thus css_tryget_online() is guaranteed to fail, the css can be
4380 * offlined by invoking offline_css(). After offlining, the base ref is
4381 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04004382 *
4383 * 3. When the percpu_ref reaches zero, the only possible remaining
4384 * accessors are inside RCU read sections. css_release() schedules the
4385 * RCU callback.
4386 *
4387 * 4. After the grace period, the css can be freed. Implemented in
4388 * css_free_work_fn().
4389 *
4390 * It is actually hairier because both step 2 and 4 require process context
4391 * and thus involve punting to css->destroy_work adding two additional
4392 * steps to the already complex sequence.
4393 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004394static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004395{
4396 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004397 container_of(work, struct cgroup_subsys_state, destroy_work);
Vladimir Davydov01e58652015-02-12 14:59:26 -08004398 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004399 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004400
Tejun Heo9a1049d2014-06-28 08:10:14 -04004401 percpu_ref_exit(&css->refcnt);
4402
Vladimir Davydov01e58652015-02-12 14:59:26 -08004403 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004404 /* css free path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004405 int id = css->id;
4406
Tejun Heo9d755d32014-05-14 09:15:02 -04004407 if (css->parent)
4408 css_put(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004409
Vladimir Davydov01e58652015-02-12 14:59:26 -08004410 ss->css_free(css);
4411 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004412 cgroup_put(cgrp);
4413 } else {
4414 /* cgroup free path */
4415 atomic_dec(&cgrp->root->nr_cgrps);
4416 cgroup_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08004417 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04004418
Tejun Heod51f39b2014-05-16 13:22:48 -04004419 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04004420 /*
4421 * We get a ref to the parent, and put the ref when
4422 * this cgroup is being freed, so it's guaranteed
4423 * that the parent won't be destroyed before its
4424 * children.
4425 */
Tejun Heod51f39b2014-05-16 13:22:48 -04004426 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04004427 kernfs_put(cgrp->kn);
4428 kfree(cgrp);
4429 } else {
4430 /*
4431 * This is root cgroup's refcnt reaching zero,
4432 * which indicates that the root should be
4433 * released.
4434 */
4435 cgroup_destroy_root(cgrp->root);
4436 }
4437 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04004438}
4439
4440static void css_free_rcu_fn(struct rcu_head *rcu_head)
4441{
4442 struct cgroup_subsys_state *css =
4443 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4444
Tejun Heo0c21ead2013-08-13 20:22:51 -04004445 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004446 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004447}
4448
Tejun Heo25e15d82014-05-14 09:15:02 -04004449static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004450{
4451 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04004452 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04004453 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04004454 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004455
Tejun Heo1fed1b22014-05-16 13:22:49 -04004456 mutex_lock(&cgroup_mutex);
4457
Tejun Heode3f0342014-05-16 13:22:49 -04004458 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04004459 list_del_rcu(&css->sibling);
4460
Tejun Heo9d755d32014-05-14 09:15:02 -04004461 if (ss) {
4462 /* css release path */
Vladimir Davydov01e58652015-02-12 14:59:26 -08004463 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05004464 if (ss->css_released)
4465 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04004466 } else {
4467 /* cgroup release path */
Tejun Heo9d755d32014-05-14 09:15:02 -04004468 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4469 cgrp->id = -1;
Li Zefana4189482014-09-04 14:43:07 +08004470
4471 /*
4472 * There are two control paths which try to determine
4473 * cgroup from dentry without going through kernfs -
4474 * cgroupstats_build() and css_tryget_online_from_dir().
4475 * Those are supported by RCU protecting clearing of
4476 * cgrp->kn->priv backpointer.
4477 */
4478 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004479 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004480
Tejun Heo1fed1b22014-05-16 13:22:49 -04004481 mutex_unlock(&cgroup_mutex);
4482
Tejun Heo0c21ead2013-08-13 20:22:51 -04004483 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004484}
4485
Paul Menageddbcc7e2007-10-18 23:39:30 -07004486static void css_release(struct percpu_ref *ref)
4487{
4488 struct cgroup_subsys_state *css =
4489 container_of(ref, struct cgroup_subsys_state, refcnt);
4490
Tejun Heo25e15d82014-05-14 09:15:02 -04004491 INIT_WORK(&css->destroy_work, css_release_work_fn);
4492 queue_work(cgroup_destroy_wq, &css->destroy_work);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004493}
4494
Tejun Heoddfcada2014-05-04 15:09:14 -04004495static void init_and_link_css(struct cgroup_subsys_state *css,
4496 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497{
Tejun Heo0cb51d72014-05-16 13:22:49 -04004498 lockdep_assert_held(&cgroup_mutex);
4499
Tejun Heoddfcada2014-05-04 15:09:14 -04004500 cgroup_get(cgrp);
4501
Tejun Heod5c419b2014-05-16 13:22:48 -04004502 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07004503 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004504 css->ss = ss;
Tejun Heod5c419b2014-05-16 13:22:48 -04004505 INIT_LIST_HEAD(&css->sibling);
4506 INIT_LIST_HEAD(&css->children);
Tejun Heo0cb51d72014-05-16 13:22:49 -04004507 css->serial_nr = css_serial_nr_next++;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004508
Tejun Heod51f39b2014-05-16 13:22:48 -04004509 if (cgroup_parent(cgrp)) {
4510 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004511 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04004512 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004513
Tejun Heoca8bdca2013-08-26 18:40:56 -04004514 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004515}
4516
Li Zefan2a4ac632013-07-31 16:16:40 +08004517/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004518static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004519{
Tejun Heo623f9262013-08-13 11:01:55 -04004520 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004521 int ret = 0;
4522
Tejun Heoa31f2d32012-11-19 08:13:37 -08004523 lockdep_assert_held(&cgroup_mutex);
4524
Tejun Heo92fb9742012-11-19 08:13:38 -08004525 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004526 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004527 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004528 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05004529 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004530 }
Tejun Heob1929db2012-11-19 08:13:38 -08004531 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004532}
4533
Li Zefan2a4ac632013-07-31 16:16:40 +08004534/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004535static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004536{
Tejun Heo623f9262013-08-13 11:01:55 -04004537 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004538
4539 lockdep_assert_held(&cgroup_mutex);
4540
4541 if (!(css->flags & CSS_ONLINE))
4542 return;
4543
Li Zefand7eeac12013-03-12 15:35:59 -07004544 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004545 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004546
Tejun Heoeb954192013-08-08 20:11:23 -04004547 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04004548 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004549
4550 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004551}
4552
Tejun Heoc81c925a2013-12-06 15:11:56 -05004553/**
4554 * create_css - create a cgroup_subsys_state
4555 * @cgrp: the cgroup new css will be associated with
4556 * @ss: the subsys of new css
Tejun Heof63070d2014-07-08 18:02:57 -04004557 * @visible: whether to create control knobs for the new css or not
Tejun Heoc81c925a2013-12-06 15:11:56 -05004558 *
4559 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heof63070d2014-07-08 18:02:57 -04004560 * css is online and installed in @cgrp with all interface files created if
4561 * @visible. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05004562 */
Tejun Heof63070d2014-07-08 18:02:57 -04004563static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
4564 bool visible)
Tejun Heoc81c925a2013-12-06 15:11:56 -05004565{
Tejun Heod51f39b2014-05-16 13:22:48 -04004566 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04004567 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004568 struct cgroup_subsys_state *css;
4569 int err;
4570
Tejun Heoc81c925a2013-12-06 15:11:56 -05004571 lockdep_assert_held(&cgroup_mutex);
4572
Tejun Heo1fed1b22014-05-16 13:22:49 -04004573 css = ss->css_alloc(parent_css);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004574 if (IS_ERR(css))
4575 return PTR_ERR(css);
4576
Tejun Heoddfcada2014-05-04 15:09:14 -04004577 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004578
Tejun Heo2aad2a82014-09-24 13:31:50 -04004579 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004580 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004581 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004582
Tejun Heo15a4c832014-05-04 15:09:14 -04004583 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4584 if (err < 0)
4585 goto err_free_percpu_ref;
4586 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004587
Tejun Heof63070d2014-07-08 18:02:57 -04004588 if (visible) {
4589 err = cgroup_populate_dir(cgrp, 1 << ss->id);
4590 if (err)
4591 goto err_free_id;
4592 }
Tejun Heo15a4c832014-05-04 15:09:14 -04004593
4594 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04004595 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04004596 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004597
4598 err = online_css(css);
4599 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04004600 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04004601
Tejun Heoc81c925a2013-12-06 15:11:56 -05004602 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
Tejun Heod51f39b2014-05-16 13:22:48 -04004603 cgroup_parent(parent)) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004604 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 -04004605 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004606 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004607 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004608 ss->warned_broken_hierarchy = true;
4609 }
4610
4611 return 0;
4612
Tejun Heo1fed1b22014-05-16 13:22:49 -04004613err_list_del:
4614 list_del_rcu(&css->sibling);
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004615 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004616err_free_id:
4617 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004618err_free_percpu_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004619 percpu_ref_exit(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004620err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004621 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004622 return err;
4623}
4624
Tejun Heob3bfd982014-05-13 12:19:22 -04004625static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4626 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627{
Tejun Heoa9746d82014-05-13 12:19:22 -04004628 struct cgroup *parent, *cgrp;
4629 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004630 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004631 struct kernfs_node *kn;
Tejun Heoa14c6872014-07-15 11:05:09 -04004632 struct cftype *base_files;
Tejun Heob3bfd982014-05-13 12:19:22 -04004633 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004634
Alban Crequy71b1fb52014-08-18 12:20:20 +01004635 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
4636 */
4637 if (strchr(name, '\n'))
4638 return -EINVAL;
4639
Tejun Heoa9746d82014-05-13 12:19:22 -04004640 parent = cgroup_kn_lock_live(parent_kn);
4641 if (!parent)
4642 return -ENODEV;
4643 root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644
Tejun Heo0a950f62012-11-19 09:02:12 -08004645 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004646 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004647 if (!cgrp) {
4648 ret = -ENOMEM;
4649 goto out_unlock;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004650 }
4651
Tejun Heo2aad2a82014-09-24 13:31:50 -04004652 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04004653 if (ret)
4654 goto out_free_cgrp;
4655
Li Zefan0ab02ca2014-02-11 16:05:46 +08004656 /*
4657 * Temporarily set the pointer to NULL, so idr_find() won't return
4658 * a half-baked cgroup.
4659 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004660 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004661 if (cgrp->id < 0) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004662 ret = -ENOMEM;
Tejun Heo9d755d32014-05-14 09:15:02 -04004663 goto out_cancel_ref;
Tejun Heo976c06b2012-11-05 09:16:59 -08004664 }
4665
Paul Menagecc31edc2008-10-18 20:28:04 -07004666 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004667
Tejun Heo9d800df2014-05-14 09:15:00 -04004668 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004669 cgrp->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004670
Li Zefanb6abdb02008-03-04 14:28:19 -08004671 if (notify_on_release(parent))
4672 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4673
Tejun Heo2260e7f2012-11-19 08:13:38 -08004674 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4675 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004676
Tejun Heo2bd59d42014-02-11 11:52:49 -05004677 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004678 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004679 if (IS_ERR(kn)) {
Tejun Heoba0f4d72014-05-13 12:19:22 -04004680 ret = PTR_ERR(kn);
4681 goto out_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004682 }
4683 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004684
Tejun Heo6f305582014-02-12 09:29:50 -05004685 /*
4686 * This extra ref will be put in cgroup_free_fn() and guarantees
4687 * that @cgrp->kn is always accessible.
4688 */
4689 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004690
Tejun Heo0cb51d72014-05-16 13:22:49 -04004691 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004692
Tejun Heo4e139af2012-11-19 08:13:36 -08004693 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04004694 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004695 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004696 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004697
Tejun Heo0d802552013-12-06 15:11:56 -05004698 /*
4699 * @cgrp is now fully operational. If something fails after this
4700 * point, it'll be released via the normal destruction path.
4701 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004702 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004703
Tejun Heoba0f4d72014-05-13 12:19:22 -04004704 ret = cgroup_kn_set_ugid(kn);
4705 if (ret)
4706 goto out_destroy;
Tejun Heo49957f82014-04-07 16:44:47 -04004707
Tejun Heoa14c6872014-07-15 11:05:09 -04004708 if (cgroup_on_dfl(cgrp))
4709 base_files = cgroup_dfl_base_files;
4710 else
4711 base_files = cgroup_legacy_base_files;
4712
4713 ret = cgroup_addrm_files(cgrp, base_files, true);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004714 if (ret)
4715 goto out_destroy;
Tejun Heo628f7cd2013-06-28 16:24:11 -07004716
Tejun Heo9d403e92013-12-06 15:11:56 -05004717 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004718 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004719 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heof63070d2014-07-08 18:02:57 -04004720 ret = create_css(cgrp, ss,
4721 parent->subtree_control & (1 << ssid));
Tejun Heoba0f4d72014-05-13 12:19:22 -04004722 if (ret)
4723 goto out_destroy;
Tejun Heob85d2042013-12-06 15:11:57 -05004724 }
Tejun Heoa8638032012-11-09 09:12:29 -08004725 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004726
Tejun Heobd53d612014-04-23 11:13:16 -04004727 /*
4728 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04004729 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04004730 */
Tejun Heo667c2492014-07-08 18:02:56 -04004731 if (!cgroup_on_dfl(cgrp)) {
4732 cgrp->subtree_control = parent->subtree_control;
4733 cgroup_refresh_child_subsys_mask(cgrp);
4734 }
Tejun Heof392e512014-04-23 11:13:14 -04004735
Tejun Heo2bd59d42014-02-11 11:52:49 -05004736 kernfs_activate(kn);
4737
Tejun Heoba0f4d72014-05-13 12:19:22 -04004738 ret = 0;
4739 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004740
Tejun Heoba0f4d72014-05-13 12:19:22 -04004741out_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004742 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Tejun Heo9d755d32014-05-14 09:15:02 -04004743out_cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04004744 percpu_ref_exit(&cgrp->self.refcnt);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004745out_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004746 kfree(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04004747out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04004748 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004749 return ret;
Tejun Heoba0f4d72014-05-13 12:19:22 -04004750
4751out_destroy:
4752 cgroup_destroy_locked(cgrp);
4753 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004754}
4755
Tejun Heo223dbc32013-08-13 20:22:50 -04004756/*
4757 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04004758 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4759 * initate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04004760 */
4761static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004762{
Tejun Heo223dbc32013-08-13 20:22:50 -04004763 struct cgroup_subsys_state *css =
4764 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004765
Tejun Heof20104d2013-08-13 20:22:50 -04004766 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004767 offline_css(css);
Tejun Heof20104d2013-08-13 20:22:50 -04004768 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004769
Tejun Heo09a503ea2013-08-13 20:22:50 -04004770 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004771}
4772
Tejun Heo223dbc32013-08-13 20:22:50 -04004773/* css kill confirmation processing requires process context, bounce */
4774static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004775{
4776 struct cgroup_subsys_state *css =
4777 container_of(ref, struct cgroup_subsys_state, refcnt);
4778
Tejun Heo223dbc32013-08-13 20:22:50 -04004779 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004780 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004781}
4782
Tejun Heof392e512014-04-23 11:13:14 -04004783/**
4784 * kill_css - destroy a css
4785 * @css: css to destroy
4786 *
4787 * This function initiates destruction of @css by removing cgroup interface
4788 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04004789 * asynchronously once css_tryget_online() is guaranteed to fail and when
4790 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04004791 */
4792static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004793{
Tejun Heo01f64742014-05-13 12:19:23 -04004794 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04004795
Tejun Heo2bd59d42014-02-11 11:52:49 -05004796 /*
4797 * This must happen before css is disassociated with its cgroup.
4798 * See seq_css() for details.
4799 */
Tejun Heoaec25022014-02-08 10:36:58 -05004800 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004801
Tejun Heoedae0c32013-08-13 20:22:51 -04004802 /*
4803 * Killing would put the base ref, but we need to keep it alive
4804 * until after ->css_offline().
4805 */
4806 css_get(css);
4807
4808 /*
4809 * cgroup core guarantees that, by the time ->css_offline() is
4810 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04004811 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04004812 * proceed to offlining css's because percpu_ref_kill() doesn't
4813 * guarantee that the ref is seen as killed on all CPUs on return.
4814 *
4815 * Use percpu_ref_kill_and_confirm() to get notifications as each
4816 * css is confirmed to be seen as killed on all CPUs.
4817 */
4818 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004819}
4820
4821/**
4822 * cgroup_destroy_locked - the first stage of cgroup destruction
4823 * @cgrp: cgroup to be destroyed
4824 *
4825 * css's make use of percpu refcnts whose killing latency shouldn't be
4826 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04004827 * guarantee that css_tryget_online() won't succeed by the time
4828 * ->css_offline() is invoked. To satisfy all the requirements,
4829 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07004830 *
4831 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4832 * userland visible parts and start killing the percpu refcnts of
4833 * css's. Set up so that the next stage will be kicked off once all
4834 * the percpu refcnts are confirmed to be killed.
4835 *
4836 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4837 * rest of destruction. Once all cgroup references are gone, the
4838 * cgroup is RCU-freed.
4839 *
4840 * This function implements s1. After this step, @cgrp is gone as far as
4841 * the userland is concerned and a new cgroup with the same name may be
4842 * created. As cgroup doesn't care about the names internally, this
4843 * doesn't cause any problem.
4844 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004845static int cgroup_destroy_locked(struct cgroup *cgrp)
4846 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004847{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004848 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004849 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004850 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004851
Tejun Heo42809dd2012-11-19 08:13:37 -08004852 lockdep_assert_held(&cgroup_mutex);
4853
Tejun Heoddd69142013-06-12 21:04:54 -07004854 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004855 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004856 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004857 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004858 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004859 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004860 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004861 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004862 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004863
Tejun Heo1a90dd52012-11-05 09:16:59 -08004864 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04004865 * Make sure there's no live children. We can't test emptiness of
4866 * ->self.children as dead children linger on it while being
4867 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004868 */
Tejun Heof3d46502014-05-16 13:22:52 -04004869 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004870 return -EBUSY;
4871
4872 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004873 * Mark @cgrp dead. This prevents further task migration and child
Tejun Heode3f0342014-05-16 13:22:49 -04004874 * creation by disabling cgroup_lock_live_group().
Tejun Heo455050d2013-06-13 19:27:41 -07004875 */
Tejun Heo184faf32014-05-16 13:22:51 -04004876 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08004877
Tejun Heo249f3462014-05-14 09:15:01 -04004878 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004879 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004880 kill_css(css);
4881
Tejun Heo455050d2013-06-13 19:27:41 -07004882 /*
Tejun Heo01f64742014-05-13 12:19:23 -04004883 * Remove @cgrp directory along with the base files. @cgrp has an
4884 * extra ref on its kn.
Tejun Heo455050d2013-06-13 19:27:41 -07004885 */
Tejun Heo01f64742014-05-13 12:19:23 -04004886 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04004887
Tejun Heod51f39b2014-05-16 13:22:48 -04004888 check_for_release(cgroup_parent(cgrp));
Tejun Heo2bd59d42014-02-11 11:52:49 -05004889
Tejun Heo249f3462014-05-14 09:15:01 -04004890 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04004891 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07004892
Tejun Heoea15f8c2013-06-13 19:27:42 -07004893 return 0;
4894};
4895
Tejun Heo2bd59d42014-02-11 11:52:49 -05004896static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004897{
Tejun Heoa9746d82014-05-13 12:19:22 -04004898 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004899 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004900
Tejun Heoa9746d82014-05-13 12:19:22 -04004901 cgrp = cgroup_kn_lock_live(kn);
4902 if (!cgrp)
4903 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004904
Tejun Heoa9746d82014-05-13 12:19:22 -04004905 ret = cgroup_destroy_locked(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004906
Tejun Heoa9746d82014-05-13 12:19:22 -04004907 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004908 return ret;
4909}
4910
Tejun Heo2bd59d42014-02-11 11:52:49 -05004911static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4912 .remount_fs = cgroup_remount,
4913 .show_options = cgroup_show_options,
4914 .mkdir = cgroup_mkdir,
4915 .rmdir = cgroup_rmdir,
4916 .rename = cgroup_rename,
4917};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004918
Tejun Heo15a4c832014-05-04 15:09:14 -04004919static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004920{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004922
4923 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004924
Tejun Heo648bb562012-11-19 08:13:36 -08004925 mutex_lock(&cgroup_mutex);
4926
Tejun Heo15a4c832014-05-04 15:09:14 -04004927 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004928 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004929
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004930 /* Create the root cgroup state for this subsystem */
4931 ss->root = &cgrp_dfl_root;
4932 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004933 /* We don't handle early failures gracefully */
4934 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004935 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04004936
4937 /*
4938 * Root csses are never destroyed and we can't initialize
4939 * percpu_ref during early init. Disable refcnting.
4940 */
4941 css->flags |= CSS_NO_REF;
4942
Tejun Heo15a4c832014-05-04 15:09:14 -04004943 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04004944 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04004945 css->id = 1;
4946 } else {
4947 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4948 BUG_ON(css->id < 0);
4949 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950
Li Zefane8d55fd2008-04-29 01:00:13 -07004951 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004952 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004953 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004954 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004955 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004956
Aleksa Saraicb4a3162015-06-06 10:02:14 +10004957 have_fork_callback |= (bool)ss->fork << ss->id;
4958 have_exit_callback |= (bool)ss->exit << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004959
Li Zefane8d55fd2008-04-29 01:00:13 -07004960 /* At system boot, before all subsystems have been
4961 * registered, no tasks have been forked, so we don't
4962 * need to invoke fork callbacks here. */
4963 BUG_ON(!list_empty(&init_task.tasks));
4964
Tejun Heoae7f1642013-08-13 20:22:50 -04004965 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004966
Tejun Heo648bb562012-11-19 08:13:36 -08004967 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004968}
4969
4970/**
Li Zefana043e3b2008-02-23 15:24:09 -08004971 * cgroup_init_early - cgroup initialization at system boot
4972 *
4973 * Initialize cgroups at system boot, and initialize any
4974 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004975 */
4976int __init cgroup_init_early(void)
4977{
Tejun Heo7b9a6ba2014-07-09 10:08:08 -04004978 static struct cgroup_sb_opts __initdata opts;
Tejun Heo30159ec2013-06-25 11:53:37 -07004979 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004980 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004981
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004982 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heo3b514d22014-05-16 13:22:47 -04004983 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4984
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004985 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004986
Tejun Heo3ed80a62014-02-08 10:36:58 -05004987 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004988 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004989 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4990 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004991 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004992 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4993 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004994
Tejun Heoaec25022014-02-08 10:36:58 -05004995 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004996 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004997
4998 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004999 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005000 }
5001 return 0;
5002}
5003
5004/**
Li Zefana043e3b2008-02-23 15:24:09 -08005005 * cgroup_init - cgroup initialization
5006 *
5007 * Register cgroup filesystem and /proc file, and initialize
5008 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005009 */
5010int __init cgroup_init(void)
5011{
Tejun Heo30159ec2013-06-25 11:53:37 -07005012 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005013 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04005014 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07005015
Tejun Heod59cfc02015-05-13 16:35:17 -04005016 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
Tejun Heoa14c6872014-07-15 11:05:09 -04005017 BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
5018 BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005019
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005020 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005021
Tejun Heo82fe9b02013-06-25 11:53:37 -07005022 /* Add init_css_set to the hash table */
5023 key = css_set_hash(init_css_set.subsys);
5024 hash_add(css_set_table, &init_css_set.hlist, key);
5025
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005026 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005027
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005028 mutex_unlock(&cgroup_mutex);
5029
Tejun Heo172a2c062014-03-19 10:23:53 -04005030 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005031 if (ss->early_init) {
5032 struct cgroup_subsys_state *css =
5033 init_css_set.subsys[ss->id];
5034
5035 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5036 GFP_KERNEL);
5037 BUG_ON(css->id < 0);
5038 } else {
5039 cgroup_init_subsys(ss, false);
5040 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005041
Tejun Heo2d8f2432014-04-23 11:13:15 -04005042 list_add_tail(&init_css_set.e_cset_node[ssid],
5043 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005044
5045 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005046 * Setting dfl_root subsys_mask needs to consider the
5047 * disabled flag and cftype registration needs kmalloc,
5048 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005049 */
Tejun Heoa8ddc822014-07-15 11:05:10 -04005050 if (ss->disabled)
5051 continue;
5052
5053 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5054
5055 if (cgroup_legacy_files_on_dfl && !ss->dfl_cftypes)
5056 ss->dfl_cftypes = ss->legacy_cftypes;
5057
Tejun Heo5de4fa12014-07-15 11:05:10 -04005058 if (!ss->dfl_cftypes)
5059 cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
5060
Tejun Heoa8ddc822014-07-15 11:05:10 -04005061 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5062 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5063 } else {
5064 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5065 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005066 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005067
5068 if (ss->bind)
5069 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005070 }
Greg KH676db4a2010-08-05 13:53:35 -07005071
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005072 err = sysfs_create_mount_point(fs_kobj, "cgroup");
5073 if (err)
5074 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005075
5076 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005077 if (err < 0) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05005078 sysfs_remove_mount_point(fs_kobj, "cgroup");
Tejun Heo2bd59d42014-02-11 11:52:49 -05005079 return err;
Paul Menagea4243162007-10-18 23:39:35 -07005080 }
5081
5082 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005083 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005084}
Paul Menageb4f48b62007-10-18 23:39:33 -07005085
Tejun Heoe5fca242013-11-22 17:14:39 -05005086static int __init cgroup_wq_init(void)
5087{
5088 /*
5089 * There isn't much point in executing destruction path in
5090 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005091 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005092 *
5093 * We would prefer to do this in cgroup_init() above, but that
5094 * is called before init_workqueues(): so leave this until after.
5095 */
Tejun Heo1a115332014-02-12 19:06:19 -05005096 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005097 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05005098
5099 /*
5100 * Used to destroy pidlists and separate to serve as flush domain.
5101 * Cap @max_active to 1 too.
5102 */
5103 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
5104 0, 1);
5105 BUG_ON(!cgroup_pidlist_destroy_wq);
5106
Tejun Heoe5fca242013-11-22 17:14:39 -05005107 return 0;
5108}
5109core_initcall(cgroup_wq_init);
5110
Paul Menagea4243162007-10-18 23:39:35 -07005111/*
5112 * proc_cgroup_show()
5113 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5114 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005115 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005116int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5117 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005118{
Tejun Heoe61734c2014-02-12 09:29:50 -05005119 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07005120 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005121 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07005122
5123 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05005124 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07005125 if (!buf)
5126 goto out;
5127
Paul Menagea4243162007-10-18 23:39:35 -07005128 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05005129 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005130
Tejun Heo985ed672014-03-19 10:23:53 -04005131 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005132 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005133 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05005134 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005135
Tejun Heoa2dd4242014-03-19 10:23:55 -04005136 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04005137 continue;
5138
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005139 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05005140 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04005141 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05005142 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005143 if (strlen(root->name))
5144 seq_printf(m, "%sname=%s", count ? "," : "",
5145 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005146 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005147 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05005148 path = cgroup_path(cgrp, buf, PATH_MAX);
5149 if (!path) {
5150 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07005151 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05005152 }
5153 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07005154 seq_putc(m, '\n');
5155 }
5156
Zefan Li006f4ac2014-09-18 16:03:15 +08005157 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07005158out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05005159 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07005160 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005161 kfree(buf);
5162out:
5163 return retval;
5164}
5165
Paul Menagea4243162007-10-18 23:39:35 -07005166/* Display information about each subsystem and each hierarchy */
5167static int proc_cgroupstats_show(struct seq_file *m, void *v)
5168{
Tejun Heo30159ec2013-06-25 11:53:37 -07005169 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005170 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005171
Paul Menage8bab8dd2008-04-04 14:29:57 -07005172 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005173 /*
5174 * ideally we don't want subsystems moving around while we do this.
5175 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5176 * subsys/hierarchy state.
5177 */
Paul Menagea4243162007-10-18 23:39:35 -07005178 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005179
5180 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005181 seq_printf(m, "%s\t%d\t%d\t%d\n",
5182 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05005183 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005184
Paul Menagea4243162007-10-18 23:39:35 -07005185 mutex_unlock(&cgroup_mutex);
5186 return 0;
5187}
5188
5189static int cgroupstats_open(struct inode *inode, struct file *file)
5190{
Al Viro9dce07f2008-03-29 03:07:28 +00005191 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005192}
5193
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005194static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005195 .open = cgroupstats_open,
5196 .read = seq_read,
5197 .llseek = seq_lseek,
5198 .release = single_release,
5199};
5200
Paul Menageb4f48b62007-10-18 23:39:33 -07005201/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05005202 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08005203 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005204 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05005205 * A task is associated with the init_css_set until cgroup_post_fork()
5206 * attaches it to the parent's css_set. Empty cg_list indicates that
5207 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07005208 */
5209void cgroup_fork(struct task_struct *child)
5210{
Tejun Heoeaf797a2014-02-25 10:04:03 -05005211 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005212 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005213}
5214
5215/**
Li Zefana043e3b2008-02-23 15:24:09 -08005216 * cgroup_post_fork - called on a new task after adding it to the task list
5217 * @child: the task in question
5218 *
Tejun Heo5edee612012-10-16 15:03:14 -07005219 * Adds the task to the list running through its css_set if necessary and
5220 * call the subsystem fork() callbacks. Has to be after the task is
5221 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005222 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005223 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005224 */
Paul Menage817929e2007-10-18 23:39:36 -07005225void cgroup_post_fork(struct task_struct *child)
5226{
Tejun Heo30159ec2013-06-25 11:53:37 -07005227 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005228 int i;
5229
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005230 /*
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005231 * This may race against cgroup_enable_task_cg_lists(). As that
Tejun Heoeaf797a2014-02-25 10:04:03 -05005232 * function sets use_task_css_set_links before grabbing
5233 * tasklist_lock and we just went through tasklist_lock to add
5234 * @child, it's guaranteed that either we see the set
5235 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5236 * @child during its iteration.
5237 *
5238 * If we won the race, @child is associated with %current's
5239 * css_set. Grabbing css_set_rwsem guarantees both that the
5240 * association is stable, and, on completion of the parent's
5241 * migration, @child is visible in the source of migration or
5242 * already in the destination cgroup. This guarantee is necessary
5243 * when implementing operations which need to migrate all tasks of
5244 * a cgroup to another.
5245 *
Dongsheng Yang251f8c02014-08-25 19:27:52 +08005246 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
Tejun Heoeaf797a2014-02-25 10:04:03 -05005247 * will remain in init_css_set. This is safe because all tasks are
5248 * in the init_css_set before cg_links is enabled and there's no
5249 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005250 */
Paul Menage817929e2007-10-18 23:39:36 -07005251 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05005252 struct css_set *cset;
5253
Tejun Heo96d365e2014-02-13 06:58:40 -05005254 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005255 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05005256 if (list_empty(&child->cg_list)) {
5257 rcu_assign_pointer(child->cgroups, cset);
5258 list_add(&child->cg_list, &cset->tasks);
5259 get_css_set(cset);
5260 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005261 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07005262 }
Tejun Heo5edee612012-10-16 15:03:14 -07005263
5264 /*
5265 * Call ss->fork(). This must happen after @child is linked on
5266 * css_set; otherwise, @child might change state between ->fork()
5267 * and addition to css_set.
5268 */
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005269 for_each_subsys_which(ss, i, &have_fork_callback)
5270 ss->fork(child);
Paul Menage817929e2007-10-18 23:39:36 -07005271}
Tejun Heo5edee612012-10-16 15:03:14 -07005272
Paul Menage817929e2007-10-18 23:39:36 -07005273/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005274 * cgroup_exit - detach cgroup from exiting task
5275 * @tsk: pointer to task_struct of exiting process
5276 *
5277 * Description: Detach cgroup from @tsk and release it.
5278 *
5279 * Note that cgroups marked notify_on_release force every task in
5280 * them to take the global cgroup_mutex mutex when exiting.
5281 * This could impact scaling on very large systems. Be reluctant to
5282 * use notify_on_release cgroups where very high task exit scaling
5283 * is required on large systems.
5284 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005285 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5286 * call cgroup_exit() while the task is still competent to handle
5287 * notify_on_release(), then leave the task attached to the root cgroup in
5288 * each hierarchy for the remainder of its exit. No need to bother with
5289 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005290 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005291 */
Li Zefan1ec41832014-03-28 15:22:19 +08005292void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005293{
Tejun Heo30159ec2013-06-25 11:53:37 -07005294 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005295 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005296 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005297 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005298
5299 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005300 * Unlink from @tsk from its css_set. As migration path can't race
5301 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005302 */
5303 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005304 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005305 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005306 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005307 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005308 }
5309
Paul Menageb4f48b62007-10-18 23:39:33 -07005310 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005311 cset = task_css_set(tsk);
5312 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005313
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005314 /* see cgroup_post_fork() for details */
5315 for_each_subsys_which(ss, i, &have_exit_callback) {
5316 struct cgroup_subsys_state *old_css = cset->subsys[i];
5317 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005318
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005319 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005320 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005321
Tejun Heoeaf797a2014-02-25 10:04:03 -05005322 if (put_cset)
Zefan Lia25eb522014-09-19 16:51:00 +08005323 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005324}
Paul Menage697f4162007-10-18 23:39:34 -07005325
Paul Menagebd89aab2007-10-18 23:40:44 -07005326static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005327{
Zefan Lia25eb522014-09-19 16:51:00 +08005328 if (notify_on_release(cgrp) && !cgroup_has_tasks(cgrp) &&
Zefan Li971ff492014-09-18 16:06:19 +08005329 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
5330 schedule_work(&cgrp->release_agent_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005331}
5332
Paul Menage81a6a5c2007-10-18 23:39:38 -07005333/*
5334 * Notify userspace when a cgroup is released, by running the
5335 * configured release agent with the name of the cgroup (path
5336 * relative to the root of cgroup file system) as the argument.
5337 *
5338 * Most likely, this user command will try to rmdir this cgroup.
5339 *
5340 * This races with the possibility that some other task will be
5341 * attached to this cgroup before it is removed, or that some other
5342 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5343 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5344 * unused, and this cgroup will be reprieved from its death sentence,
5345 * to continue to serve a useful existence. Next time it's released,
5346 * we will get notified again, if it still has 'notify_on_release' set.
5347 *
5348 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5349 * means only wait until the task is successfully execve()'d. The
5350 * separate release agent task is forked by call_usermodehelper(),
5351 * then control in this thread returns here, without waiting for the
5352 * release agent task. We don't bother to wait because the caller of
5353 * this routine has no use for the exit status of the release agent
5354 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005355 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005356static void cgroup_release_agent(struct work_struct *work)
5357{
Zefan Li971ff492014-09-18 16:06:19 +08005358 struct cgroup *cgrp =
5359 container_of(work, struct cgroup, release_agent_work);
5360 char *pathbuf = NULL, *agentbuf = NULL, *path;
5361 char *argv[3], *envp[3];
5362
Paul Menage81a6a5c2007-10-18 23:39:38 -07005363 mutex_lock(&cgroup_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005364
Zefan Li971ff492014-09-18 16:06:19 +08005365 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
5366 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5367 if (!pathbuf || !agentbuf)
5368 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005369
Zefan Li971ff492014-09-18 16:06:19 +08005370 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5371 if (!path)
5372 goto out;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005373
Zefan Li971ff492014-09-18 16:06:19 +08005374 argv[0] = agentbuf;
5375 argv[1] = path;
5376 argv[2] = NULL;
5377
5378 /* minimal command environment */
5379 envp[0] = "HOME=/";
5380 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5381 envp[2] = NULL;
5382
Paul Menage81a6a5c2007-10-18 23:39:38 -07005383 mutex_unlock(&cgroup_mutex);
Zefan Li971ff492014-09-18 16:06:19 +08005384 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Zefan Li3e2cd912014-09-20 14:35:43 +08005385 goto out_free;
Zefan Li971ff492014-09-18 16:06:19 +08005386out:
Zefan Li3e2cd912014-09-20 14:35:43 +08005387 mutex_unlock(&cgroup_mutex);
5388out_free:
Zefan Li971ff492014-09-18 16:06:19 +08005389 kfree(agentbuf);
5390 kfree(pathbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005391}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005392
5393static int __init cgroup_disable(char *str)
5394{
Tejun Heo30159ec2013-06-25 11:53:37 -07005395 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005396 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005397 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005398
5399 while ((token = strsep(&str, ",")) != NULL) {
5400 if (!*token)
5401 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005402
Tejun Heo3ed80a62014-02-08 10:36:58 -05005403 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005404 if (!strcmp(token, ss->name)) {
5405 ss->disabled = 1;
5406 printk(KERN_INFO "Disabling %s control group"
5407 " subsystem\n", ss->name);
5408 break;
5409 }
5410 }
5411 }
5412 return 1;
5413}
5414__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005415
Tejun Heoa8ddc822014-07-15 11:05:10 -04005416static int __init cgroup_set_legacy_files_on_dfl(char *str)
5417{
5418 printk("cgroup: using legacy files on the default hierarchy\n");
5419 cgroup_legacy_files_on_dfl = true;
5420 return 0;
5421}
5422__setup("cgroup__DEVEL__legacy_files_on_dfl", cgroup_set_legacy_files_on_dfl);
5423
Tejun Heob77d7b62013-08-13 11:01:54 -04005424/**
Tejun Heoec903c02014-05-13 12:11:01 -04005425 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04005426 * @dentry: directory dentry of interest
5427 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005428 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005429 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5430 * to get the corresponding css and return it. If such css doesn't exist
5431 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005432 */
Tejun Heoec903c02014-05-13 12:11:01 -04005433struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5434 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005435{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005436 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5437 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005438 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005439
Tejun Heo35cf0832013-08-26 18:40:56 -04005440 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005441 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5442 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005443 return ERR_PTR(-EBADF);
5444
Tejun Heo5a17f542014-02-11 11:52:47 -05005445 rcu_read_lock();
5446
Tejun Heo2bd59d42014-02-11 11:52:49 -05005447 /*
5448 * This path doesn't originate from kernfs and @kn could already
5449 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08005450 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05005451 */
5452 cgrp = rcu_dereference(kn->priv);
5453 if (cgrp)
5454 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005455
Tejun Heoec903c02014-05-13 12:11:01 -04005456 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05005457 css = ERR_PTR(-ENOENT);
5458
5459 rcu_read_unlock();
5460 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005461}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005462
Li Zefan1cb650b2013-08-19 10:05:24 +08005463/**
5464 * css_from_id - lookup css by id
5465 * @id: the cgroup id
5466 * @ss: cgroup subsys to be looked into
5467 *
5468 * Returns the css if there's valid one with @id, otherwise returns NULL.
5469 * Should be called under rcu_read_lock().
5470 */
5471struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5472{
Tejun Heo6fa49182014-05-04 15:09:13 -04005473 WARN_ON_ONCE(!rcu_read_lock_held());
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005474 return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005475}
5476
Paul Menagefe693432009-09-23 15:56:20 -07005477#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005478static struct cgroup_subsys_state *
5479debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005480{
5481 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5482
5483 if (!css)
5484 return ERR_PTR(-ENOMEM);
5485
5486 return css;
5487}
5488
Tejun Heoeb954192013-08-08 20:11:23 -04005489static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005490{
Tejun Heoeb954192013-08-08 20:11:23 -04005491 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005492}
5493
Tejun Heo182446d2013-08-08 20:11:24 -04005494static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5495 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005496{
Tejun Heo182446d2013-08-08 20:11:24 -04005497 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005498}
5499
Tejun Heo182446d2013-08-08 20:11:24 -04005500static u64 current_css_set_read(struct cgroup_subsys_state *css,
5501 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005502{
5503 return (u64)(unsigned long)current->cgroups;
5504}
5505
Tejun Heo182446d2013-08-08 20:11:24 -04005506static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005507 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005508{
5509 u64 count;
5510
5511 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005512 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005513 rcu_read_unlock();
5514 return count;
5515}
5516
Tejun Heo2da8ca82013-12-05 12:28:04 -05005517static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005518{
Tejun Heo69d02062013-06-12 21:04:50 -07005519 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005520 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005521 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005522
Tejun Heoe61734c2014-02-12 09:29:50 -05005523 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5524 if (!name_buf)
5525 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005526
Tejun Heo96d365e2014-02-13 06:58:40 -05005527 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005528 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005529 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005530 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005531 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005532
Tejun Heoa2dd4242014-03-19 10:23:55 -04005533 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005534 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005535 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005536 }
5537 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005538 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005539 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005540 return 0;
5541}
5542
5543#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005544static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005545{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005546 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005547 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005548
Tejun Heo96d365e2014-02-13 06:58:40 -05005549 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005550 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005551 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005552 struct task_struct *task;
5553 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005554
Tejun Heo5abb8852013-06-12 21:04:49 -07005555 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005556
Tejun Heo5abb8852013-06-12 21:04:49 -07005557 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005558 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5559 goto overflow;
5560 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005561 }
Tejun Heoc7561122014-02-25 10:04:01 -05005562
5563 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5564 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5565 goto overflow;
5566 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5567 }
5568 continue;
5569 overflow:
5570 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005571 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005572 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005573 return 0;
5574}
5575
Tejun Heo182446d2013-08-08 20:11:24 -04005576static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005577{
Zefan Lia25eb522014-09-19 16:51:00 +08005578 return (!cgroup_has_tasks(css->cgroup) &&
5579 !css_has_online_children(&css->cgroup->self));
Paul Menagefe693432009-09-23 15:56:20 -07005580}
5581
5582static struct cftype debug_files[] = {
5583 {
Paul Menagefe693432009-09-23 15:56:20 -07005584 .name = "taskcount",
5585 .read_u64 = debug_taskcount_read,
5586 },
5587
5588 {
5589 .name = "current_css_set",
5590 .read_u64 = current_css_set_read,
5591 },
5592
5593 {
5594 .name = "current_css_set_refcount",
5595 .read_u64 = current_css_set_refcount_read,
5596 },
5597
5598 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005599 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005600 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005601 },
5602
5603 {
5604 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005605 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005606 },
5607
5608 {
Paul Menagefe693432009-09-23 15:56:20 -07005609 .name = "releasable",
5610 .read_u64 = releasable_read,
5611 },
Paul Menagefe693432009-09-23 15:56:20 -07005612
Tejun Heo4baf6e32012-04-01 12:09:55 -07005613 { } /* terminate */
5614};
Paul Menagefe693432009-09-23 15:56:20 -07005615
Tejun Heo073219e2014-02-08 10:36:58 -05005616struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005617 .css_alloc = debug_css_alloc,
5618 .css_free = debug_css_free,
Tejun Heo55779642014-07-15 11:05:09 -04005619 .legacy_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005620};
5621#endif /* CONFIG_CGROUP_DEBUG */