blob: f1c98c527b2d7243e89883626a38787d3ee198b2 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
38#include <linux/mm.h>
39#include <linux/mutex.h>
40#include <linux/mount.h>
41#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070042#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/rcupdate.h>
44#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050047#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070048#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051#include <linux/delayacct.h>
52#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080053#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070054#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070055#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070056#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020057#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050058#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070059
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoe25e2cb2011-12-12 18:12:21 -080062/*
Tejun Heob1a21362013-11-29 10:42:58 -050063 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
67 */
68#define CGROUP_PIDLIST_DESTROY_DELAY HZ
69
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050070#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
71 MAX_CFTYPE_NAME + 2)
72
Tejun Heob1a21362013-11-29 10:42:58 -050073/*
Tejun Heoace2bee2014-02-11 11:52:47 -050074 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
75 * creation/removal and hierarchy changing operations including cgroup
76 * creation, removal, css association and controller rebinding. This outer
77 * lock is needed mainly to resolve the circular dependency between kernfs
78 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
79 */
80static DEFINE_MUTEX(cgroup_tree_mutex);
81
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082/*
83 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
85 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050086 * css_set_rwsem protects task->cgroups pointer, the list of css_set
87 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080088 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050089 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
90 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080091 */
Tejun Heo22194492013-04-07 09:29:51 -070092#ifdef CONFIG_PROVE_RCU
93DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050094DECLARE_RWSEM(css_set_rwsem);
95EXPORT_SYMBOL_GPL(cgroup_mutex);
96EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070097#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070098static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050099static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -0700100#endif
101
Tejun Heo69e943b2014-02-08 10:36:58 -0500102/*
Tejun Heo6fa49182014-05-04 15:09:13 -0400103 * Protects cgroup_idr so that IDs can be released without grabbing
104 * cgroup_mutex.
105 */
106static DEFINE_SPINLOCK(cgroup_idr_lock);
107
108/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500109 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
110 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
111 */
112static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700113
Tejun Heoace2bee2014-02-11 11:52:47 -0500114#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500115 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500116 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500117 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500118 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500119
Ben Blumaae8aab2010-03-10 15:22:07 -0800120/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500121 * cgroup destruction makes heavy use of work items and there can be a lot
122 * of concurrent destructions. Use a separate workqueue so that cgroup
123 * destruction work items don't end up filling up max_active of system_wq
124 * which may lead to deadlock.
125 */
126static struct workqueue_struct *cgroup_destroy_wq;
127
128/*
Tejun Heob1a21362013-11-29 10:42:58 -0500129 * pidlist destructions need to be flushed on cgroup destruction. Use a
130 * separate workqueue as flush domain.
131 */
132static struct workqueue_struct *cgroup_pidlist_destroy_wq;
133
Tejun Heo3ed80a62014-02-08 10:36:58 -0500134/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500135#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500136static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137#include <linux/cgroup_subsys.h>
138};
Tejun Heo073219e2014-02-08 10:36:58 -0500139#undef SUBSYS
140
141/* array of cgroup subsystem names */
142#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
143static const char *cgroup_subsys_name[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Paul Menageddbcc7e2007-10-18 23:39:30 -0700148/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400149 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700150 * unattached - it never has more than a single cgroup, and all tasks are
151 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400153struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700154
Tejun Heoa2dd4242014-03-19 10:23:55 -0400155/*
156 * The default hierarchy always exists but is hidden until mounted for the
157 * first time. This is for backward compatibility.
158 */
159static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160
161/* The list of hierarchy roots */
162
Tejun Heo9871bf92013-06-24 15:21:47 -0700163static LIST_HEAD(cgroup_roots);
164static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165
Tejun Heo3417ae12014-02-08 10:37:01 -0500166/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700167static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700168
Li Zefan794611a2013-06-18 18:53:53 +0800169/*
170 * Assign a monotonically increasing serial number to cgroups. It
171 * guarantees cgroups with bigger numbers are newer than those with smaller
172 * numbers. Also, as cgroups are always appended to the parent's
173 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700174 * the ascending serial number order on the list. Protected by
175 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800176 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700177static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800178
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800180 * check for fork/exit handlers to call. This avoids us having to do
181 * extra work in the fork/exit path if none of the subsystems need to
182 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700184static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185
Tejun Heo628f7cd2013-06-28 16:24:11 -0700186static struct cftype cgroup_base_files[];
187
Tejun Heo59f52962014-02-11 11:52:49 -0500188static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400189static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400190 unsigned int ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400191static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800192static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400193static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
194static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400195static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
196 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500197static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800198
Tejun Heo6fa49182014-05-04 15:09:13 -0400199/* IDR wrappers which synchronize using cgroup_idr_lock */
200static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
201 gfp_t gfp_mask)
202{
203 int ret;
204
205 idr_preload(gfp_mask);
206 spin_lock(&cgroup_idr_lock);
207 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
208 spin_unlock(&cgroup_idr_lock);
209 idr_preload_end();
210 return ret;
211}
212
213static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
214{
215 void *ret;
216
217 spin_lock(&cgroup_idr_lock);
218 ret = idr_replace(idr, ptr, id);
219 spin_unlock(&cgroup_idr_lock);
220 return ret;
221}
222
223static void cgroup_idr_remove(struct idr *idr, int id)
224{
225 spin_lock(&cgroup_idr_lock);
226 idr_remove(idr, id);
227 spin_unlock(&cgroup_idr_lock);
228}
229
Tejun Heo95109b62013-08-08 20:11:27 -0400230/**
231 * cgroup_css - obtain a cgroup's css for the specified subsystem
232 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400233 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400234 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400235 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
236 * function must be called either under cgroup_mutex or rcu_read_lock() and
237 * the caller is responsible for pinning the returned css if it wants to
238 * keep accessing it outside the said locks. This function may return
239 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400240 */
241static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400242 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400243{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400244 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500245 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500246 lockdep_is_held(&cgroup_tree_mutex) ||
247 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400248 else
249 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400250}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700251
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400252/**
253 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
254 * @cgrp: the cgroup of interest
255 * @ss: the subsystem of interest (%NULL returns the dummy_css)
256 *
257 * Similar to cgroup_css() but returns the effctive css, which is defined
258 * as the matching css of the nearest ancestor including self which has @ss
259 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
260 * function is guaranteed to return non-NULL css.
261 */
262static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
263 struct cgroup_subsys *ss)
264{
265 lockdep_assert_held(&cgroup_mutex);
266
267 if (!ss)
268 return &cgrp->dummy_css;
269
270 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
271 return NULL;
272
273 while (cgrp->parent &&
274 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
275 cgrp = cgrp->parent;
276
277 return cgroup_css(cgrp, ss);
278}
279
Paul Menageddbcc7e2007-10-18 23:39:30 -0700280/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700281static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700282{
Tejun Heo54766d42013-06-12 21:04:53 -0700283 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284}
285
Tejun Heo59f52962014-02-11 11:52:49 -0500286struct cgroup_subsys_state *seq_css(struct seq_file *seq)
287{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500288 struct kernfs_open_file *of = seq->private;
289 struct cgroup *cgrp = of->kn->parent->priv;
290 struct cftype *cft = seq_cft(seq);
291
292 /*
293 * This is open and unprotected implementation of cgroup_css().
294 * seq_css() is only called from a kernfs file operation which has
295 * an active reference on the file. Because all the subsystem
296 * files are drained before a css is disassociated with a cgroup,
297 * the matching css from the cgroup's subsys table is guaranteed to
298 * be and stay valid until the enclosing operation is complete.
299 */
300 if (cft->ss)
301 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
302 else
303 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500304}
305EXPORT_SYMBOL_GPL(seq_css);
306
Li Zefan78574cf2013-04-08 19:00:38 -0700307/**
308 * cgroup_is_descendant - test ancestry
309 * @cgrp: the cgroup to be tested
310 * @ancestor: possible ancestor of @cgrp
311 *
312 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
313 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
314 * and @ancestor are accessible.
315 */
316bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
317{
318 while (cgrp) {
319 if (cgrp == ancestor)
320 return true;
321 cgrp = cgrp->parent;
322 }
323 return false;
324}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700325
Adrian Bunke9685a02008-02-07 00:13:46 -0800326static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327{
328 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700329 (1 << CGRP_RELEASABLE) |
330 (1 << CGRP_NOTIFY_ON_RELEASE);
331 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332}
333
Adrian Bunke9685a02008-02-07 00:13:46 -0800334static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335{
Paul Menagebd89aab2007-10-18 23:40:44 -0700336 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337}
338
Tejun Heo30159ec2013-06-25 11:53:37 -0700339/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500340 * for_each_css - iterate all css's of a cgroup
341 * @css: the iteration cursor
342 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
343 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700344 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400345 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700346 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500347#define for_each_css(css, ssid, cgrp) \
348 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
349 if (!((css) = rcu_dereference_check( \
350 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500351 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500352 lockdep_is_held(&cgroup_mutex)))) { } \
353 else
354
355/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400356 * for_each_e_css - iterate all effective css's of a cgroup
357 * @css: the iteration cursor
358 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
359 * @cgrp: the target cgroup to iterate css's of
360 *
361 * Should be called under cgroup_[tree_]mutex.
362 */
363#define for_each_e_css(css, ssid, cgrp) \
364 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
365 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
366 ; \
367 else
368
369/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500370 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700371 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500372 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700373 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500374#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500375 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
376 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700377
Tejun Heo985ed672014-03-19 10:23:53 -0400378/* iterate across the hierarchies */
379#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700380 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700381
Tejun Heof8f22e52014-04-23 11:13:16 -0400382/* iterate over child cgrps, lock should be held throughout iteration */
383#define cgroup_for_each_live_child(child, cgrp) \
384 list_for_each_entry((child), &(cgrp)->children, sibling) \
385 if (({ lockdep_assert_held(&cgroup_tree_mutex); \
386 cgroup_is_dead(child); })) \
387 ; \
388 else
389
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700390/**
391 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
392 * @cgrp: the cgroup to be checked for liveness
393 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700394 * On success, returns true; the mutex should be later unlocked. On
395 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700396 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700397static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700398{
399 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700400 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700401 mutex_unlock(&cgroup_mutex);
402 return false;
403 }
404 return true;
405}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700406
Paul Menage81a6a5c2007-10-18 23:39:38 -0700407/* the list of cgroups eligible for automatic release. Protected by
408 * release_list_lock */
409static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200410static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411static void cgroup_release_agent(struct work_struct *work);
412static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700413static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700414
Tejun Heo69d02062013-06-12 21:04:50 -0700415/*
416 * A cgroup can be associated with multiple css_sets as different tasks may
417 * belong to different cgroups on different hierarchies. In the other
418 * direction, a css_set is naturally associated with multiple cgroups.
419 * This M:N relationship is represented by the following link structure
420 * which exists for each association and allows traversing the associations
421 * from both sides.
422 */
423struct cgrp_cset_link {
424 /* the cgroup and css_set this link associates */
425 struct cgroup *cgrp;
426 struct css_set *cset;
427
428 /* list of cgrp_cset_links anchored at cgrp->cset_links */
429 struct list_head cset_link;
430
431 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
432 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700433};
434
Tejun Heo172a2c062014-03-19 10:23:53 -0400435/*
436 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700437 * hierarchies being mounted. It contains a pointer to the root state
438 * for each subsystem. Also used to anchor the list of css_sets. Not
439 * reference-counted, to improve performance when child cgroups
440 * haven't been created.
441 */
Tejun Heo172a2c062014-03-19 10:23:53 -0400442static struct css_set init_css_set = {
443 .refcount = ATOMIC_INIT(1),
444 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
445 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
446 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
447 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
448 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
449};
Paul Menage817929e2007-10-18 23:39:36 -0700450
Tejun Heo172a2c062014-03-19 10:23:53 -0400451static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700452
Tejun Heo842b5972014-04-25 18:28:02 -0400453/**
454 * cgroup_update_populated - updated populated count of a cgroup
455 * @cgrp: the target cgroup
456 * @populated: inc or dec populated count
457 *
458 * @cgrp is either getting the first task (css_set) or losing the last.
459 * Update @cgrp->populated_cnt accordingly. The count is propagated
460 * towards root so that a given cgroup's populated_cnt is zero iff the
461 * cgroup and all its descendants are empty.
462 *
463 * @cgrp's interface file "cgroup.populated" is zero if
464 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
465 * changes from or to zero, userland is notified that the content of the
466 * interface file has changed. This can be used to detect when @cgrp and
467 * its descendants become populated or empty.
468 */
469static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
470{
471 lockdep_assert_held(&css_set_rwsem);
472
473 do {
474 bool trigger;
475
476 if (populated)
477 trigger = !cgrp->populated_cnt++;
478 else
479 trigger = !--cgrp->populated_cnt;
480
481 if (!trigger)
482 break;
483
484 if (cgrp->populated_kn)
485 kernfs_notify(cgrp->populated_kn);
486 cgrp = cgrp->parent;
487 } while (cgrp);
488}
489
Paul Menage7717f7b2009-09-23 15:56:22 -0700490/*
491 * hash table for cgroup groups. This improves the performance to find
492 * an existing css_set. This hash doesn't (currently) take into
493 * account cgroups in empty hierarchies.
494 */
Li Zefan472b1052008-04-29 01:00:11 -0700495#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800496static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700497
Li Zefan0ac801f2013-01-10 11:49:27 +0800498static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700499{
Li Zefan0ac801f2013-01-10 11:49:27 +0800500 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700501 struct cgroup_subsys *ss;
502 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700503
Tejun Heo30159ec2013-06-25 11:53:37 -0700504 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800505 key += (unsigned long)css[i];
506 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700507
Li Zefan0ac801f2013-01-10 11:49:27 +0800508 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700509}
510
Tejun Heo89c55092014-02-13 06:58:40 -0500511static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700512{
Tejun Heo69d02062013-06-12 21:04:50 -0700513 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400514 struct cgroup_subsys *ss;
515 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700516
Tejun Heo89c55092014-02-13 06:58:40 -0500517 lockdep_assert_held(&css_set_rwsem);
518
519 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700520 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700521
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700522 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400523 for_each_subsys(ss, ssid)
524 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700525 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700526 css_set_count--;
527
Tejun Heo69d02062013-06-12 21:04:50 -0700528 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700529 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700530
Tejun Heo69d02062013-06-12 21:04:50 -0700531 list_del(&link->cset_link);
532 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800533
Tejun Heo96d365e2014-02-13 06:58:40 -0500534 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400535 if (list_empty(&cgrp->cset_links)) {
536 cgroup_update_populated(cgrp, false);
537 if (notify_on_release(cgrp)) {
538 if (taskexit)
539 set_bit(CGRP_RELEASABLE, &cgrp->flags);
540 check_for_release(cgrp);
541 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700542 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700543
544 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700545 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700546
Tejun Heo5abb8852013-06-12 21:04:49 -0700547 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700548}
549
Tejun Heo89c55092014-02-13 06:58:40 -0500550static void put_css_set(struct css_set *cset, bool taskexit)
551{
552 /*
553 * Ensure that the refcount doesn't hit zero while any readers
554 * can see it. Similar to atomic_dec_and_lock(), but for an
555 * rwlock
556 */
557 if (atomic_add_unless(&cset->refcount, -1, 1))
558 return;
559
560 down_write(&css_set_rwsem);
561 put_css_set_locked(cset, taskexit);
562 up_write(&css_set_rwsem);
563}
564
Paul Menage817929e2007-10-18 23:39:36 -0700565/*
566 * refcounted get/put for css_set objects
567 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700568static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700569{
Tejun Heo5abb8852013-06-12 21:04:49 -0700570 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700571}
572
Tejun Heob326f9d2013-06-24 15:21:48 -0700573/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700574 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700575 * @cset: candidate css_set being tested
576 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700577 * @new_cgrp: cgroup that's being entered by the task
578 * @template: desired set of css pointers in css_set (pre-calculated)
579 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800580 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700581 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
582 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700583static bool compare_css_sets(struct css_set *cset,
584 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 struct cgroup *new_cgrp,
586 struct cgroup_subsys_state *template[])
587{
588 struct list_head *l1, *l2;
589
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400590 /*
591 * On the default hierarchy, there can be csets which are
592 * associated with the same set of cgroups but different csses.
593 * Let's first ensure that csses match.
594 */
595 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700596 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700597
598 /*
599 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400600 * different cgroups in hierarchies. As different cgroups may
601 * share the same effective css, this comparison is always
602 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700603 */
Tejun Heo69d02062013-06-12 21:04:50 -0700604 l1 = &cset->cgrp_links;
605 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700606 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700607 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700608 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700609
610 l1 = l1->next;
611 l2 = l2->next;
612 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700613 if (l1 == &cset->cgrp_links) {
614 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700615 break;
616 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700617 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 }
619 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700620 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
621 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
622 cgrp1 = link1->cgrp;
623 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700624 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700625 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700626
627 /*
628 * If this hierarchy is the hierarchy of the cgroup
629 * that's changing, then we need to check that this
630 * css_set points to the new cgroup; if it's any other
631 * hierarchy, then this css_set should point to the
632 * same cgroup as the old css_set.
633 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700634 if (cgrp1->root == new_cgrp->root) {
635 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700636 return false;
637 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700639 return false;
640 }
641 }
642 return true;
643}
644
Tejun Heob326f9d2013-06-24 15:21:48 -0700645/**
646 * find_existing_css_set - init css array and find the matching css_set
647 * @old_cset: the css_set that we're using before the cgroup transition
648 * @cgrp: the cgroup that we're moving into
649 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700650 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700651static struct css_set *find_existing_css_set(struct css_set *old_cset,
652 struct cgroup *cgrp,
653 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700654{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400655 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700656 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800658 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700659 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700660
Ben Blumaae8aab2010-03-10 15:22:07 -0800661 /*
662 * Build the set of subsystem state objects that we want to see in the
663 * new css_set. while subsystems can change globally, the entries here
664 * won't change, so no need for locking.
665 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700666 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400667 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400668 /*
669 * @ss is in this hierarchy, so we want the
670 * effective css from @cgrp.
671 */
672 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700673 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400674 /*
675 * @ss is not in this hierarchy, so we don't want
676 * to change the css.
677 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700679 }
680 }
681
Li Zefan0ac801f2013-01-10 11:49:27 +0800682 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700683 hash_for_each_possible(css_set_table, cset, hlist, key) {
684 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700685 continue;
686
687 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700689 }
Paul Menage817929e2007-10-18 23:39:36 -0700690
691 /* No existing cgroup group matched */
692 return NULL;
693}
694
Tejun Heo69d02062013-06-12 21:04:50 -0700695static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700696{
Tejun Heo69d02062013-06-12 21:04:50 -0700697 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700698
Tejun Heo69d02062013-06-12 21:04:50 -0700699 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
700 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700701 kfree(link);
702 }
703}
704
Tejun Heo69d02062013-06-12 21:04:50 -0700705/**
706 * allocate_cgrp_cset_links - allocate cgrp_cset_links
707 * @count: the number of links to allocate
708 * @tmp_links: list_head the allocated links are put on
709 *
710 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
711 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700712 */
Tejun Heo69d02062013-06-12 21:04:50 -0700713static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700714{
Tejun Heo69d02062013-06-12 21:04:50 -0700715 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700716 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700717
718 INIT_LIST_HEAD(tmp_links);
719
Li Zefan36553432008-07-29 22:33:19 -0700720 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700721 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700722 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700723 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700724 return -ENOMEM;
725 }
Tejun Heo69d02062013-06-12 21:04:50 -0700726 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700727 }
728 return 0;
729}
730
Li Zefanc12f65d2009-01-07 18:07:42 -0800731/**
732 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700733 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700734 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800735 * @cgrp: the destination cgroup
736 */
Tejun Heo69d02062013-06-12 21:04:50 -0700737static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
738 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800739{
Tejun Heo69d02062013-06-12 21:04:50 -0700740 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800741
Tejun Heo69d02062013-06-12 21:04:50 -0700742 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400743
744 if (cgroup_on_dfl(cgrp))
745 cset->dfl_cgrp = cgrp;
746
Tejun Heo69d02062013-06-12 21:04:50 -0700747 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
748 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700749 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400750
751 if (list_empty(&cgrp->cset_links))
752 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700753 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400754
Paul Menage7717f7b2009-09-23 15:56:22 -0700755 /*
756 * Always add links to the tail of the list so that the list
757 * is sorted by order of hierarchy creation
758 */
Tejun Heo69d02062013-06-12 21:04:50 -0700759 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800760}
761
Tejun Heob326f9d2013-06-24 15:21:48 -0700762/**
763 * find_css_set - return a new css_set with one cgroup updated
764 * @old_cset: the baseline css_set
765 * @cgrp: the cgroup to be updated
766 *
767 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
768 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700769 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700770static struct css_set *find_css_set(struct css_set *old_cset,
771 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700772{
Tejun Heob326f9d2013-06-24 15:21:48 -0700773 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700774 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700775 struct list_head tmp_links;
776 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400777 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800778 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400779 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700780
Tejun Heob326f9d2013-06-24 15:21:48 -0700781 lockdep_assert_held(&cgroup_mutex);
782
Paul Menage817929e2007-10-18 23:39:36 -0700783 /* First see if we already have a cgroup group that matches
784 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500785 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700786 cset = find_existing_css_set(old_cset, cgrp, template);
787 if (cset)
788 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500789 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700790
Tejun Heo5abb8852013-06-12 21:04:49 -0700791 if (cset)
792 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700793
Tejun Heof4f4be22013-06-12 21:04:51 -0700794 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700795 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700796 return NULL;
797
Tejun Heo69d02062013-06-12 21:04:50 -0700798 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700799 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700800 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700801 return NULL;
802 }
803
Tejun Heo5abb8852013-06-12 21:04:49 -0700804 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700805 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700806 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500807 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500808 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500809 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700810 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700811
812 /* Copy the set of subsystem state objects generated in
813 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700814 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700815
Tejun Heo96d365e2014-02-13 06:58:40 -0500816 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700817 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700818 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700819 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700820
Paul Menage7717f7b2009-09-23 15:56:22 -0700821 if (c->root == cgrp->root)
822 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700823 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700824 }
Paul Menage817929e2007-10-18 23:39:36 -0700825
Tejun Heo69d02062013-06-12 21:04:50 -0700826 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700827
Paul Menage817929e2007-10-18 23:39:36 -0700828 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700829
Tejun Heo2d8f2432014-04-23 11:13:15 -0400830 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700831 key = css_set_hash(cset->subsys);
832 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700833
Tejun Heo2d8f2432014-04-23 11:13:15 -0400834 for_each_subsys(ss, ssid)
835 list_add_tail(&cset->e_cset_node[ssid],
836 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
837
Tejun Heo96d365e2014-02-13 06:58:40 -0500838 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700839
Tejun Heo5abb8852013-06-12 21:04:49 -0700840 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700841}
842
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400843static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700844{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400845 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500846
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400847 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500848}
849
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400850static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500851{
852 int id;
853
854 lockdep_assert_held(&cgroup_mutex);
855
Tejun Heo985ed672014-03-19 10:23:53 -0400856 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500857 if (id < 0)
858 return id;
859
860 root->hierarchy_id = id;
861 return 0;
862}
863
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400864static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500865{
866 lockdep_assert_held(&cgroup_mutex);
867
868 if (root->hierarchy_id) {
869 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
870 root->hierarchy_id = 0;
871 }
872}
873
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400874static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500875{
876 if (root) {
877 /* hierarhcy ID shoulid already have been released */
878 WARN_ON_ONCE(root->hierarchy_id);
879
880 idr_destroy(&root->cgroup_idr);
881 kfree(root);
882 }
883}
884
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400885static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500886{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400887 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500888 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500889
Tejun Heo2bd59d42014-02-11 11:52:49 -0500890 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500891 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500892
Tejun Heo776f02f2014-02-12 09:29:50 -0500893 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500894 BUG_ON(!list_empty(&cgrp->children));
895
Tejun Heof2e85d52014-02-11 11:52:49 -0500896 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400897 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500898
899 /*
900 * Release all the links from cset_links to this hierarchy's
901 * root cgroup
902 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500903 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500904
905 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
906 list_del(&link->cset_link);
907 list_del(&link->cgrp_link);
908 kfree(link);
909 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500910 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500911
912 if (!list_empty(&root->root_list)) {
913 list_del(&root->root_list);
914 cgroup_root_count--;
915 }
916
917 cgroup_exit_root_id(root);
918
919 mutex_unlock(&cgroup_mutex);
920 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500921
Tejun Heo2bd59d42014-02-11 11:52:49 -0500922 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500923 cgroup_free_root(root);
924}
925
Tejun Heoceb6a082014-02-25 10:04:02 -0500926/* look up cgroup associated with given css_set on the specified hierarchy */
927static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400928 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700929{
Paul Menage7717f7b2009-09-23 15:56:22 -0700930 struct cgroup *res = NULL;
931
Tejun Heo96d365e2014-02-13 06:58:40 -0500932 lockdep_assert_held(&cgroup_mutex);
933 lockdep_assert_held(&css_set_rwsem);
934
Tejun Heo5abb8852013-06-12 21:04:49 -0700935 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400936 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700937 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700938 struct cgrp_cset_link *link;
939
940 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700941 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700942
Paul Menage7717f7b2009-09-23 15:56:22 -0700943 if (c->root == root) {
944 res = c;
945 break;
946 }
947 }
948 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500949
Paul Menage7717f7b2009-09-23 15:56:22 -0700950 BUG_ON(!res);
951 return res;
952}
953
954/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500955 * Return the cgroup for "task" from the given hierarchy. Must be
956 * called with cgroup_mutex and css_set_rwsem held.
957 */
958static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400959 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500960{
961 /*
962 * No need to lock the task - since we hold cgroup_mutex the
963 * task can't change groups, so the only thing that can happen
964 * is that it exits and its css is set back to init_css_set.
965 */
966 return cset_cgroup_from_root(task_css_set(task), root);
967}
968
969/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 * A task must hold cgroup_mutex to modify cgroups.
971 *
972 * Any task can increment and decrement the count field without lock.
973 * So in general, code holding cgroup_mutex can't rely on the count
974 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800975 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 * means that no tasks are currently attached, therefore there is no
977 * way a task attached to that cgroup can fork (the other way to
978 * increment the count). So code holding cgroup_mutex can safely
979 * assume that if the count is zero, it will stay zero. Similarly, if
980 * a task holds cgroup_mutex on a cgroup with zero count, it
981 * knows that the cgroup won't be removed, as cgroup_rmdir()
982 * needs that mutex.
983 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
985 * (usually) take cgroup_mutex. These are the two most performance
986 * critical pieces of code here. The exception occurs on cgroup_exit(),
987 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
988 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800989 * to the release agent with the name of the cgroup (path relative to
990 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 *
992 * A cgroup can only be deleted if both its 'count' of using tasks
993 * is zero, and its list of 'children' cgroups is empty. Since all
994 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400995 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400997 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 *
999 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001000 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001 */
1002
Tejun Heo69dfa002014-05-04 15:09:13 -04001003static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001004static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001005static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001006
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001007static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1008 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001010 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1011 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1012 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1013 cft->ss->name, cft->name);
1014 else
1015 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1016 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017}
1018
Tejun Heof2e85d52014-02-11 11:52:49 -05001019/**
1020 * cgroup_file_mode - deduce file mode of a control file
1021 * @cft: the control file in question
1022 *
1023 * returns cft->mode if ->mode is not 0
1024 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1025 * returns S_IRUGO if it has only a read handler
1026 * returns S_IWUSR if it has only a write hander
1027 */
1028static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001029{
Tejun Heof2e85d52014-02-11 11:52:49 -05001030 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001031
Tejun Heof2e85d52014-02-11 11:52:49 -05001032 if (cft->mode)
1033 return cft->mode;
1034
1035 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1036 mode |= S_IRUGO;
1037
1038 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
1039 cft->trigger)
1040 mode |= S_IWUSR;
1041
1042 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001043}
1044
Li Zefanbe445622013-01-24 14:31:42 +08001045static void cgroup_free_fn(struct work_struct *work)
1046{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001047 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001048
Tejun Heo3c9c8252014-02-12 09:29:50 -05001049 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001050 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001051
Tejun Heo776f02f2014-02-12 09:29:50 -05001052 if (cgrp->parent) {
1053 /*
1054 * We get a ref to the parent, and put the ref when this
1055 * cgroup is being freed, so it's guaranteed that the
1056 * parent won't be destroyed before its children.
1057 */
1058 cgroup_put(cgrp->parent);
1059 kernfs_put(cgrp->kn);
1060 kfree(cgrp);
1061 } else {
1062 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001063 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001064 * indicates that the root should be released.
1065 */
1066 cgroup_destroy_root(cgrp->root);
1067 }
Li Zefanbe445622013-01-24 14:31:42 +08001068}
1069
1070static void cgroup_free_rcu(struct rcu_head *head)
1071{
1072 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1073
Tejun Heoea15f8c2013-06-13 19:27:42 -07001074 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001075 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001076}
1077
Tejun Heo59f52962014-02-11 11:52:49 -05001078static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001080 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1081 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1082 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083}
1084
Tejun Heo59f52962014-02-11 11:52:49 -05001085static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001087 if (!atomic_dec_and_test(&cgrp->refcnt))
1088 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001089 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001090 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091
Tejun Heo2bd59d42014-02-11 11:52:49 -05001092 /*
1093 * XXX: cgrp->id is only used to look up css's. As cgroup and
1094 * css's lifetimes will be decoupled, it should be made
1095 * per-subsystem and moved to css->id so that lookups are
1096 * successful until the target css is released.
1097 */
Tejun Heo6fa49182014-05-04 15:09:13 -04001098 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001099 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100
Tejun Heo2bd59d42014-02-11 11:52:49 -05001101 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102}
1103
Li Zefan2739d3c2013-01-21 18:18:33 +08001104static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001106 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107
Tejun Heoace2bee2014-02-11 11:52:47 -05001108 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001109 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001110}
1111
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001112/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001113 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001114 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001115 * @subsys_mask: mask of the subsystem ids whose files should be removed
1116 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001117static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001118{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001119 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001120 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001121
Tejun Heob420ba72013-07-12 12:34:02 -07001122 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001123 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001124
Tejun Heo69dfa002014-05-04 15:09:13 -04001125 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001126 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001127 list_for_each_entry(cfts, &ss->cfts, node)
1128 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001129 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130}
1131
Tejun Heo69dfa002014-05-04 15:09:13 -04001132static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Tejun Heo30159ec2013-06-25 11:53:37 -07001134 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001135 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136
Tejun Heoace2bee2014-02-11 11:52:47 -05001137 lockdep_assert_held(&cgroup_tree_mutex);
1138 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001139
Tejun Heo5df36032014-03-19 10:23:54 -04001140 for_each_subsys(ss, ssid) {
1141 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001143
Tejun Heo7fd8c562014-04-23 11:13:16 -04001144 /* if @ss has non-root csses attached to it, can't move */
1145 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001146 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147
Tejun Heo5df36032014-03-19 10:23:54 -04001148 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001149 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001150 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151 }
1152
Tejun Heoa2dd4242014-03-19 10:23:55 -04001153 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1154 if (ret) {
1155 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001156 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157
Tejun Heoa2dd4242014-03-19 10:23:55 -04001158 /*
1159 * Rebinding back to the default root is not allowed to
1160 * fail. Using both default and non-default roots should
1161 * be rare. Moving subsystems back and forth even more so.
1162 * Just warn about it and continue.
1163 */
1164 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001165 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001166 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001167 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001168 }
Tejun Heo5df36032014-03-19 10:23:54 -04001169 }
Tejun Heo31261212013-06-28 17:07:30 -07001170
1171 /*
1172 * Nothing can fail from this point on. Remove files for the
1173 * removed subsystems and rebind each subsystem.
1174 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001175 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001176 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001177 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001178 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001179 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001180
Tejun Heo5df36032014-03-19 10:23:54 -04001181 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001182 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001183 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001184 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001185
Tejun Heo5df36032014-03-19 10:23:54 -04001186 if (!(ss_mask & (1 << ssid)))
1187 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001188
Tejun Heo5df36032014-03-19 10:23:54 -04001189 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001190 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001191
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001192 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001193
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001194 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1195 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001196 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001197 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001198
Tejun Heo2d8f2432014-04-23 11:13:15 -04001199 down_write(&css_set_rwsem);
1200 hash_for_each(css_set_table, i, cset, hlist)
1201 list_move_tail(&cset->e_cset_node[ss->id],
1202 &dst_root->cgrp.e_csets[ss->id]);
1203 up_write(&css_set_rwsem);
1204
Tejun Heof392e512014-04-23 11:13:14 -04001205 src_root->subsys_mask &= ~(1 << ssid);
1206 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1207
Tejun Heobd53d612014-04-23 11:13:16 -04001208 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001209 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001210 if (dst_root != &cgrp_dfl_root)
1211 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001212
Tejun Heo5df36032014-03-19 10:23:54 -04001213 if (ss->bind)
1214 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001216
Tejun Heoa2dd4242014-03-19 10:23:55 -04001217 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001218 return 0;
1219}
1220
Tejun Heo2bd59d42014-02-11 11:52:49 -05001221static int cgroup_show_options(struct seq_file *seq,
1222 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001223{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001224 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001225 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001226 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001227
Tejun Heob85d2042013-12-06 15:11:57 -05001228 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001229 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001230 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001231 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1232 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001233 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001234 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001235 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001236 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001237
1238 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001239 if (strlen(root->release_agent_path))
1240 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001241 spin_unlock(&release_agent_path_lock);
1242
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001243 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001244 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001245 if (strlen(root->name))
1246 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 return 0;
1248}
1249
1250struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001251 unsigned int subsys_mask;
1252 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001253 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001254 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001255 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001256 /* User explicitly requested empty subsystem */
1257 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001258};
1259
Ben Blumcf5d5942010-03-10 15:22:09 -08001260static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001262 char *token, *o = data;
1263 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001264 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001265 struct cgroup_subsys *ss;
1266 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001267
1268#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001269 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001270#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271
Paul Menagec6d57f32009-09-23 15:56:19 -07001272 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001273
1274 while ((token = strsep(&o, ",")) != NULL) {
1275 if (!*token)
1276 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001277 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001278 /* Explicitly have no subsystems */
1279 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001280 continue;
1281 }
1282 if (!strcmp(token, "all")) {
1283 /* Mutually exclusive option 'all' + subsystem name */
1284 if (one_ss)
1285 return -EINVAL;
1286 all_ss = true;
1287 continue;
1288 }
Tejun Heo873fe092013-04-14 20:15:26 -07001289 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1290 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1291 continue;
1292 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001293 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001294 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001295 continue;
1296 }
1297 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001298 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001299 continue;
1300 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001301 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001302 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001303 continue;
1304 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001305 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001306 /* Specifying two release agents is forbidden */
1307 if (opts->release_agent)
1308 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001309 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001310 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001311 if (!opts->release_agent)
1312 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001313 continue;
1314 }
1315 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001316 const char *name = token + 5;
1317 /* Can't specify an empty name */
1318 if (!strlen(name))
1319 return -EINVAL;
1320 /* Must match [\w.-]+ */
1321 for (i = 0; i < strlen(name); i++) {
1322 char c = name[i];
1323 if (isalnum(c))
1324 continue;
1325 if ((c == '.') || (c == '-') || (c == '_'))
1326 continue;
1327 return -EINVAL;
1328 }
1329 /* Specifying two names is forbidden */
1330 if (opts->name)
1331 return -EINVAL;
1332 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001333 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001334 GFP_KERNEL);
1335 if (!opts->name)
1336 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001337
1338 continue;
1339 }
1340
Tejun Heo30159ec2013-06-25 11:53:37 -07001341 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001342 if (strcmp(token, ss->name))
1343 continue;
1344 if (ss->disabled)
1345 continue;
1346
1347 /* Mutually exclusive option 'all' + subsystem name */
1348 if (all_ss)
1349 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001350 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001351 one_ss = true;
1352
1353 break;
1354 }
1355 if (i == CGROUP_SUBSYS_COUNT)
1356 return -ENOENT;
1357 }
1358
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001359 /* Consistency checks */
1360
Tejun Heo873fe092013-04-14 20:15:26 -07001361 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001362 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001363
Tejun Heod3ba07c2014-02-13 06:58:38 -05001364 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1365 opts->cpuset_clone_children || opts->release_agent ||
1366 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001367 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001368 return -EINVAL;
1369 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001370 } else {
1371 /*
1372 * If the 'all' option was specified select all the
1373 * subsystems, otherwise if 'none', 'name=' and a subsystem
1374 * name options were not specified, let's default to 'all'
1375 */
1376 if (all_ss || (!one_ss && !opts->none && !opts->name))
1377 for_each_subsys(ss, i)
1378 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001379 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001380
Tejun Heoa2dd4242014-03-19 10:23:55 -04001381 /*
1382 * We either have to specify by name or by subsystems. (So
1383 * all empty hierarchies must have a name).
1384 */
1385 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001386 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001387 }
1388
Li Zefanf9ab5b52009-06-17 16:26:33 -07001389 /*
1390 * Option noprefix was introduced just for backward compatibility
1391 * with the old cpuset, so we allow noprefix only if mounting just
1392 * the cpuset subsystem.
1393 */
Tejun Heo93438622013-04-14 20:15:25 -07001394 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001395 return -EINVAL;
1396
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001397
1398 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001399 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001400 return -EINVAL;
1401
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402 return 0;
1403}
1404
Tejun Heo2bd59d42014-02-11 11:52:49 -05001405static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406{
1407 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001408 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001410 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411
Tejun Heo873fe092013-04-14 20:15:26 -07001412 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001413 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001414 return -EINVAL;
1415 }
1416
Tejun Heoace2bee2014-02-11 11:52:47 -05001417 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418 mutex_lock(&cgroup_mutex);
1419
1420 /* See what subsystems are wanted */
1421 ret = parse_cgroupfs_options(data, &opts);
1422 if (ret)
1423 goto out_unlock;
1424
Tejun Heof392e512014-04-23 11:13:14 -04001425 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001426 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001427 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001428
Tejun Heof392e512014-04-23 11:13:14 -04001429 added_mask = opts.subsys_mask & ~root->subsys_mask;
1430 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001431
Ben Blumcf5d5942010-03-10 15:22:09 -08001432 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001433 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001434 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001435 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001436 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1437 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001439 goto out_unlock;
1440 }
1441
Tejun Heof172e672013-06-28 17:07:30 -07001442 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001443 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001444 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001445 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001446 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447
Tejun Heo5df36032014-03-19 10:23:54 -04001448 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001449 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001452 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001453
Tejun Heo69e943b2014-02-08 10:36:58 -05001454 if (opts.release_agent) {
1455 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001456 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001457 spin_unlock(&release_agent_path_lock);
1458 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001460 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001461 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001463 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464 return ret;
1465}
1466
Tejun Heoafeb0f92014-02-13 06:58:39 -05001467/*
1468 * To reduce the fork() overhead for systems that are not actually using
1469 * their cgroups capability, we don't maintain the lists running through
1470 * each css_set to its tasks until we see the list actually used - in other
1471 * words after the first mount.
1472 */
1473static bool use_task_css_set_links __read_mostly;
1474
1475static void cgroup_enable_task_cg_lists(void)
1476{
1477 struct task_struct *p, *g;
1478
Tejun Heo96d365e2014-02-13 06:58:40 -05001479 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001480
1481 if (use_task_css_set_links)
1482 goto out_unlock;
1483
1484 use_task_css_set_links = true;
1485
1486 /*
1487 * We need tasklist_lock because RCU is not safe against
1488 * while_each_thread(). Besides, a forking task that has passed
1489 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1490 * is not guaranteed to have its child immediately visible in the
1491 * tasklist if we walk through it with RCU.
1492 */
1493 read_lock(&tasklist_lock);
1494 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001495 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1496 task_css_set(p) != &init_css_set);
1497
1498 /*
1499 * We should check if the process is exiting, otherwise
1500 * it will race with cgroup_exit() in that the list
1501 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001502 * Do it while holding siglock so that we don't end up
1503 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001504 */
Tejun Heof153ad12014-02-25 09:56:49 -05001505 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001506 if (!(p->flags & PF_EXITING)) {
1507 struct css_set *cset = task_css_set(p);
1508
1509 list_add(&p->cg_list, &cset->tasks);
1510 get_css_set(cset);
1511 }
Tejun Heof153ad12014-02-25 09:56:49 -05001512 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001513 } while_each_thread(g, p);
1514 read_unlock(&tasklist_lock);
1515out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001516 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001517}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518
Paul Menagecc31edc2008-10-18 20:28:04 -07001519static void init_cgroup_housekeeping(struct cgroup *cgrp)
1520{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001521 struct cgroup_subsys *ss;
1522 int ssid;
1523
Tejun Heo2bd59d42014-02-11 11:52:49 -05001524 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001525 INIT_LIST_HEAD(&cgrp->sibling);
1526 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001527 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001528 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001529 INIT_LIST_HEAD(&cgrp->pidlists);
1530 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001531 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001532
1533 for_each_subsys(ss, ssid)
1534 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001535
1536 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001537}
Paul Menagec6d57f32009-09-23 15:56:19 -07001538
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001539static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001540 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001542 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001543
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001545 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001546 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001547 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001548 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
Paul Menagec6d57f32009-09-23 15:56:19 -07001550 root->flags = opts->flags;
1551 if (opts->release_agent)
1552 strcpy(root->release_agent_path, opts->release_agent);
1553 if (opts->name)
1554 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001555 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001556 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001557}
1558
Tejun Heo69dfa002014-05-04 15:09:13 -04001559static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001560{
Tejun Heod427dfe2014-02-11 11:52:48 -05001561 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001562 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001563 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001564 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001565
Tejun Heod427dfe2014-02-11 11:52:48 -05001566 lockdep_assert_held(&cgroup_tree_mutex);
1567 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001568
Tejun Heo6fa49182014-05-04 15:09:13 -04001569 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001570 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001571 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001572 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001573
Tejun Heod427dfe2014-02-11 11:52:48 -05001574 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001575 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001576 * but that's OK - it can only be increased by someone holding
1577 * cgroup_lock, and that's us. The worst that can happen is that we
1578 * have some link structures left over
1579 */
1580 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001582 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583
Tejun Heo985ed672014-03-19 10:23:53 -04001584 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001585 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001586 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587
Tejun Heo2bd59d42014-02-11 11:52:49 -05001588 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1589 KERNFS_ROOT_CREATE_DEACTIVATED,
1590 root_cgrp);
1591 if (IS_ERR(root->kf_root)) {
1592 ret = PTR_ERR(root->kf_root);
1593 goto exit_root_id;
1594 }
1595 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596
Tejun Heod427dfe2014-02-11 11:52:48 -05001597 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1598 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001599 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600
Tejun Heo5df36032014-03-19 10:23:54 -04001601 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001602 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001603 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001604
Tejun Heod427dfe2014-02-11 11:52:48 -05001605 /*
1606 * There must be no failure case after here, since rebinding takes
1607 * care of subsystems' refcounts, which are explicitly dropped in
1608 * the failure exit path.
1609 */
1610 list_add(&root->root_list, &cgroup_roots);
1611 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
Tejun Heod427dfe2014-02-11 11:52:48 -05001613 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001614 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001615 * objects.
1616 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001617 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001618 hash_for_each(css_set_table, i, cset, hlist)
1619 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001620 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Tejun Heod427dfe2014-02-11 11:52:48 -05001622 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001623 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001624
Tejun Heo2bd59d42014-02-11 11:52:49 -05001625 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001626 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001627 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001628
Tejun Heo2bd59d42014-02-11 11:52:49 -05001629destroy_root:
1630 kernfs_destroy_root(root->kf_root);
1631 root->kf_root = NULL;
1632exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001633 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001634out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001635 free_cgrp_cset_links(&tmp_links);
1636 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637}
1638
Al Virof7e83572010-07-26 13:23:11 +04001639static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001641 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001643 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001645 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001646 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001647 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001648
1649 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001650 * The first time anyone tries to mount a cgroup, enable the list
1651 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001652 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001653 if (!use_task_css_set_links)
1654 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001655
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001656 mutex_lock(&cgroup_tree_mutex);
1657 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001662 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001663retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001664 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001665 if (!opts.subsys_mask && !opts.none && !opts.name) {
1666 cgrp_dfl_root_visible = true;
1667 root = &cgrp_dfl_root;
1668 cgroup_get(&root->cgrp);
1669 ret = 0;
1670 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671 }
1672
Tejun Heo985ed672014-03-19 10:23:53 -04001673 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001674 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001676 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001677 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001678
Paul Menage817929e2007-10-18 23:39:36 -07001679 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001680 * If we asked for a name then it must match. Also, if
1681 * name matches but sybsys_mask doesn't, we should fail.
1682 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001683 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001684 if (opts.name) {
1685 if (strcmp(opts.name, root->name))
1686 continue;
1687 name_match = true;
1688 }
Tejun Heo31261212013-06-28 17:07:30 -07001689
1690 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001691 * If we asked for subsystems (or explicitly for no
1692 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001693 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001694 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001695 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001696 if (!name_match)
1697 continue;
1698 ret = -EBUSY;
1699 goto out_unlock;
1700 }
Tejun Heo873fe092013-04-14 20:15:26 -07001701
Tejun Heoc7ba8282013-06-29 14:06:10 -07001702 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001703 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001704 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001705 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001706 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001707 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001708 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001709 }
Tejun Heo873fe092013-04-14 20:15:26 -07001710 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001711
Tejun Heo776f02f2014-02-12 09:29:50 -05001712 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001713 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001714 * ref indicate that the root is being destroyed. Wait for
1715 * destruction to complete so that the subsystems are free.
1716 * We can use wait_queue for the wait but this path is
1717 * super cold. Let's just sleep for a bit and retry.
1718 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001719 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001720 mutex_unlock(&cgroup_mutex);
1721 mutex_unlock(&cgroup_tree_mutex);
1722 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001723 mutex_lock(&cgroup_tree_mutex);
1724 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001725 goto retry;
1726 }
1727
1728 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001729 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 }
1731
Tejun Heo172a2c062014-03-19 10:23:53 -04001732 /*
1733 * No such thing, create a new one. name= matching without subsys
1734 * specification is allowed for already existing hierarchies but we
1735 * can't create new one without subsys specification.
1736 */
1737 if (!opts.subsys_mask && !opts.none) {
1738 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001739 goto out_unlock;
1740 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
Tejun Heo172a2c062014-03-19 10:23:53 -04001742 root = kzalloc(sizeof(*root), GFP_KERNEL);
1743 if (!root) {
1744 ret = -ENOMEM;
1745 goto out_unlock;
1746 }
1747
1748 init_cgroup_root(root, &opts);
1749
Tejun Heo35585572014-02-13 06:58:38 -05001750 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001751 if (ret)
1752 cgroup_free_root(root);
1753
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001754out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001755 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001756 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001757
Paul Menagec6d57f32009-09-23 15:56:19 -07001758 kfree(opts.release_agent);
1759 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001760
Tejun Heo2bd59d42014-02-11 11:52:49 -05001761 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001762 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001763
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001764 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1765 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001766 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001767 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768}
1769
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001770static void cgroup_kill_sb(struct super_block *sb)
1771{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001772 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001773 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001775 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001776 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777}
1778
1779static struct file_system_type cgroup_fs_type = {
1780 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001781 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 .kill_sb = cgroup_kill_sb,
1783};
1784
Greg KH676db4a2010-08-05 13:53:35 -07001785static struct kobject *cgroup_kobj;
1786
Li Zefana043e3b2008-02-23 15:24:09 -08001787/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001788 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001789 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001790 * @buf: the buffer to write the path into
1791 * @buflen: the length of the buffer
1792 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001793 * Determine @task's cgroup on the first (the one with the lowest non-zero
1794 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1795 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1796 * cgroup controller callbacks.
1797 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001798 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001800char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001801{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001802 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001803 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001804 int hierarchy_id = 1;
1805 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001806
1807 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001808 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001809
Tejun Heo913ffdb2013-07-11 16:34:48 -07001810 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1811
Tejun Heo857a2be2013-04-14 20:50:08 -07001812 if (root) {
1813 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001814 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001815 } else {
1816 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001817 if (strlcpy(buf, "/", buflen) < buflen)
1818 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001819 }
1820
Tejun Heo96d365e2014-02-13 06:58:40 -05001821 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001822 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001823 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001824}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001825EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001826
Tejun Heob3dc0942014-02-25 10:04:01 -05001827/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001828struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001829 /* the src and dst cset list running through cset->mg_node */
1830 struct list_head src_csets;
1831 struct list_head dst_csets;
1832
1833 /*
1834 * Fields for cgroup_taskset_*() iteration.
1835 *
1836 * Before migration is committed, the target migration tasks are on
1837 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1838 * the csets on ->dst_csets. ->csets point to either ->src_csets
1839 * or ->dst_csets depending on whether migration is committed.
1840 *
1841 * ->cur_csets and ->cur_task point to the current task position
1842 * during iteration.
1843 */
1844 struct list_head *csets;
1845 struct css_set *cur_cset;
1846 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001847};
1848
1849/**
1850 * cgroup_taskset_first - reset taskset and return the first task
1851 * @tset: taskset of interest
1852 *
1853 * @tset iteration is initialized and the first task is returned.
1854 */
1855struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1856{
Tejun Heob3dc0942014-02-25 10:04:01 -05001857 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1858 tset->cur_task = NULL;
1859
1860 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001861}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001862
1863/**
1864 * cgroup_taskset_next - iterate to the next task in taskset
1865 * @tset: taskset of interest
1866 *
1867 * Return the next task in @tset. Iteration must have been initialized
1868 * with cgroup_taskset_first().
1869 */
1870struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1871{
Tejun Heob3dc0942014-02-25 10:04:01 -05001872 struct css_set *cset = tset->cur_cset;
1873 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001874
Tejun Heob3dc0942014-02-25 10:04:01 -05001875 while (&cset->mg_node != tset->csets) {
1876 if (!task)
1877 task = list_first_entry(&cset->mg_tasks,
1878 struct task_struct, cg_list);
1879 else
1880 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001881
Tejun Heob3dc0942014-02-25 10:04:01 -05001882 if (&task->cg_list != &cset->mg_tasks) {
1883 tset->cur_cset = cset;
1884 tset->cur_task = task;
1885 return task;
1886 }
1887
1888 cset = list_next_entry(cset, mg_node);
1889 task = NULL;
1890 }
1891
1892 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001893}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001894
1895/**
Ben Blum74a11662011-05-26 16:25:20 -07001896 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001897 * @old_cgrp; the cgroup @tsk is being migrated from
1898 * @tsk: the task being migrated
1899 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001900 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001901 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001902 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001903static void cgroup_task_migrate(struct cgroup *old_cgrp,
1904 struct task_struct *tsk,
1905 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001906{
Tejun Heo5abb8852013-06-12 21:04:49 -07001907 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001908
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001909 lockdep_assert_held(&cgroup_mutex);
1910 lockdep_assert_held(&css_set_rwsem);
1911
Ben Blum74a11662011-05-26 16:25:20 -07001912 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001913 * We are synchronized through threadgroup_lock() against PF_EXITING
1914 * setting such that we can't race against cgroup_exit() changing the
1915 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001916 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001917 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001918 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001919
Tejun Heob3dc0942014-02-25 10:04:01 -05001920 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001921 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001922
Tejun Heo1b9aba42014-03-19 17:43:21 -04001923 /*
1924 * Use move_tail so that cgroup_taskset_first() still returns the
1925 * leader after migration. This works because cgroup_migrate()
1926 * ensures that the dst_cset of the leader is the first on the
1927 * tset's dst_csets list.
1928 */
1929 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001930
1931 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001932 * We just gained a reference on old_cset by taking it from the
1933 * task. As trading it for new_cset is protected by cgroup_mutex,
1934 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001935 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001936 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001937 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001938}
1939
Li Zefana043e3b2008-02-23 15:24:09 -08001940/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001941 * cgroup_migrate_finish - cleanup after attach
1942 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001943 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001944 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1945 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001946 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001947static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001948{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001949 struct css_set *cset, *tmp_cset;
1950
1951 lockdep_assert_held(&cgroup_mutex);
1952
1953 down_write(&css_set_rwsem);
1954 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1955 cset->mg_src_cgrp = NULL;
1956 cset->mg_dst_cset = NULL;
1957 list_del_init(&cset->mg_preload_node);
1958 put_css_set_locked(cset, false);
1959 }
1960 up_write(&css_set_rwsem);
1961}
1962
1963/**
1964 * cgroup_migrate_add_src - add a migration source css_set
1965 * @src_cset: the source css_set to add
1966 * @dst_cgrp: the destination cgroup
1967 * @preloaded_csets: list of preloaded css_sets
1968 *
1969 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1970 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1971 * up by cgroup_migrate_finish().
1972 *
1973 * This function may be called without holding threadgroup_lock even if the
1974 * target is a process. Threads may be created and destroyed but as long
1975 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1976 * the preloaded css_sets are guaranteed to cover all migrations.
1977 */
1978static void cgroup_migrate_add_src(struct css_set *src_cset,
1979 struct cgroup *dst_cgrp,
1980 struct list_head *preloaded_csets)
1981{
1982 struct cgroup *src_cgrp;
1983
1984 lockdep_assert_held(&cgroup_mutex);
1985 lockdep_assert_held(&css_set_rwsem);
1986
1987 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1988
Tejun Heo1958d2d2014-02-25 10:04:03 -05001989 if (!list_empty(&src_cset->mg_preload_node))
1990 return;
1991
1992 WARN_ON(src_cset->mg_src_cgrp);
1993 WARN_ON(!list_empty(&src_cset->mg_tasks));
1994 WARN_ON(!list_empty(&src_cset->mg_node));
1995
1996 src_cset->mg_src_cgrp = src_cgrp;
1997 get_css_set(src_cset);
1998 list_add(&src_cset->mg_preload_node, preloaded_csets);
1999}
2000
2001/**
2002 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04002003 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002004 * @preloaded_csets: list of preloaded source css_sets
2005 *
2006 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2007 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002008 * pins all destination css_sets, links each to its source, and append them
2009 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2010 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002011 *
2012 * This function must be called after cgroup_migrate_add_src() has been
2013 * called on each migration source css_set. After migration is performed
2014 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2015 * @preloaded_csets.
2016 */
2017static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2018 struct list_head *preloaded_csets)
2019{
2020 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002021 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002022
2023 lockdep_assert_held(&cgroup_mutex);
2024
Tejun Heof8f22e52014-04-23 11:13:16 -04002025 /*
2026 * Except for the root, child_subsys_mask must be zero for a cgroup
2027 * with tasks so that child cgroups don't compete against tasks.
2028 */
2029 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2030 dst_cgrp->child_subsys_mask)
2031 return -EBUSY;
2032
Tejun Heo1958d2d2014-02-25 10:04:03 -05002033 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002034 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002035 struct css_set *dst_cset;
2036
Tejun Heof817de92014-04-23 11:13:16 -04002037 dst_cset = find_css_set(src_cset,
2038 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002039 if (!dst_cset)
2040 goto err;
2041
2042 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002043
2044 /*
2045 * If src cset equals dst, it's noop. Drop the src.
2046 * cgroup_migrate() will skip the cset too. Note that we
2047 * can't handle src == dst as some nodes are used by both.
2048 */
2049 if (src_cset == dst_cset) {
2050 src_cset->mg_src_cgrp = NULL;
2051 list_del_init(&src_cset->mg_preload_node);
2052 put_css_set(src_cset, false);
2053 put_css_set(dst_cset, false);
2054 continue;
2055 }
2056
Tejun Heo1958d2d2014-02-25 10:04:03 -05002057 src_cset->mg_dst_cset = dst_cset;
2058
2059 if (list_empty(&dst_cset->mg_preload_node))
2060 list_add(&dst_cset->mg_preload_node, &csets);
2061 else
2062 put_css_set(dst_cset, false);
2063 }
2064
Tejun Heof817de92014-04-23 11:13:16 -04002065 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002066 return 0;
2067err:
2068 cgroup_migrate_finish(&csets);
2069 return -ENOMEM;
2070}
2071
2072/**
2073 * cgroup_migrate - migrate a process or task to a cgroup
2074 * @cgrp: the destination cgroup
2075 * @leader: the leader of the process or the task to migrate
2076 * @threadgroup: whether @leader points to the whole process or a single task
2077 *
2078 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2079 * process, the caller must be holding threadgroup_lock of @leader. The
2080 * caller is also responsible for invoking cgroup_migrate_add_src() and
2081 * cgroup_migrate_prepare_dst() on the targets before invoking this
2082 * function and following up with cgroup_migrate_finish().
2083 *
2084 * As long as a controller's ->can_attach() doesn't fail, this function is
2085 * guaranteed to succeed. This means that, excluding ->can_attach()
2086 * failure, when migrating multiple targets, the success or failure can be
2087 * decided for all targets by invoking group_migrate_prepare_dst() before
2088 * actually starting migrating.
2089 */
2090static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2091 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002092{
Tejun Heob3dc0942014-02-25 10:04:01 -05002093 struct cgroup_taskset tset = {
2094 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2095 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2096 .csets = &tset.src_csets,
2097 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002098 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002099 struct css_set *cset, *tmp_cset;
2100 struct task_struct *task, *tmp_task;
2101 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002102
2103 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002104 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2105 * already PF_EXITING could be freed from underneath us unless we
2106 * take an rcu_read_lock.
2107 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002108 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002109 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002110 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002111 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002112 /* @task either already exited or can't exit until the end */
2113 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002114 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002115
Tejun Heoeaf797a2014-02-25 10:04:03 -05002116 /* leave @task alone if post_fork() hasn't linked it yet */
2117 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002118 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002119
Tejun Heob3dc0942014-02-25 10:04:01 -05002120 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002121 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002122 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002123
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002124 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002125 * cgroup_taskset_first() must always return the leader.
2126 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002127 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002128 list_move_tail(&task->cg_list, &cset->mg_tasks);
2129 if (list_empty(&cset->mg_node))
2130 list_add_tail(&cset->mg_node, &tset.src_csets);
2131 if (list_empty(&cset->mg_dst_cset->mg_node))
2132 list_move_tail(&cset->mg_dst_cset->mg_node,
2133 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002134 next:
Li Zefan081aa452013-03-13 09:17:09 +08002135 if (!threadgroup)
2136 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002137 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002138 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002139 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002140
Tejun Heo134d3372011-12-12 18:12:21 -08002141 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002142 if (list_empty(&tset.src_csets))
2143 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002144
Tejun Heo1958d2d2014-02-25 10:04:03 -05002145 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002146 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002147 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002148 ret = css->ss->can_attach(css, &tset);
2149 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002150 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002151 goto out_cancel_attach;
2152 }
2153 }
Ben Blum74a11662011-05-26 16:25:20 -07002154 }
2155
2156 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002157 * Now that we're guaranteed success, proceed to move all tasks to
2158 * the new cgroup. There are no failure cases after here, so this
2159 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002160 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002161 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002162 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2163 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2164 cgroup_task_migrate(cset->mg_src_cgrp, task,
2165 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002166 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002167 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002168
2169 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002170 * Migration is committed, all target tasks are now on dst_csets.
2171 * Nothing is sensitive to fork() after this point. Notify
2172 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002173 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002174 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002175
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002176 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002177 if (css->ss->attach)
2178 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002179
Tejun Heo9db8de32014-02-13 06:58:43 -05002180 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002181 goto out_release_tset;
2182
Ben Blum74a11662011-05-26 16:25:20 -07002183out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002184 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002185 if (css == failed_css)
2186 break;
2187 if (css->ss->cancel_attach)
2188 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002189 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002190out_release_tset:
2191 down_write(&css_set_rwsem);
2192 list_splice_init(&tset.dst_csets, &tset.src_csets);
2193 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002194 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002195 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002196 }
2197 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002198 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002199}
2200
Tejun Heo1958d2d2014-02-25 10:04:03 -05002201/**
2202 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2203 * @dst_cgrp: the cgroup to attach to
2204 * @leader: the task or the leader of the threadgroup to be attached
2205 * @threadgroup: attach the whole threadgroup?
2206 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002207 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002208 */
2209static int cgroup_attach_task(struct cgroup *dst_cgrp,
2210 struct task_struct *leader, bool threadgroup)
2211{
2212 LIST_HEAD(preloaded_csets);
2213 struct task_struct *task;
2214 int ret;
2215
2216 /* look up all src csets */
2217 down_read(&css_set_rwsem);
2218 rcu_read_lock();
2219 task = leader;
2220 do {
2221 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2222 &preloaded_csets);
2223 if (!threadgroup)
2224 break;
2225 } while_each_thread(leader, task);
2226 rcu_read_unlock();
2227 up_read(&css_set_rwsem);
2228
2229 /* prepare dst csets and commit */
2230 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2231 if (!ret)
2232 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2233
2234 cgroup_migrate_finish(&preloaded_csets);
2235 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002236}
2237
2238/*
2239 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002240 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002241 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002242 */
2243static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002244{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002245 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002246 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002247 int ret;
2248
Ben Blum74a11662011-05-26 16:25:20 -07002249 if (!cgroup_lock_live_group(cgrp))
2250 return -ENODEV;
2251
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002252retry_find_task:
2253 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002254 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002255 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002256 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002257 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002258 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002259 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002260 }
Ben Blum74a11662011-05-26 16:25:20 -07002261 /*
2262 * even if we're attaching all tasks in the thread group, we
2263 * only need to check permissions on one of them.
2264 */
David Howellsc69e8d92008-11-14 10:39:19 +11002265 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002266 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2267 !uid_eq(cred->euid, tcred->uid) &&
2268 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002269 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002270 ret = -EACCES;
2271 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002272 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002273 } else
2274 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002275
2276 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002277 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002278
2279 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002280 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002281 * trapped in a cpuset, or RT worker may be born in a cgroup
2282 * with no rt_runtime allocated. Just say no.
2283 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002284 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002285 ret = -EINVAL;
2286 rcu_read_unlock();
2287 goto out_unlock_cgroup;
2288 }
2289
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002290 get_task_struct(tsk);
2291 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002292
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002293 threadgroup_lock(tsk);
2294 if (threadgroup) {
2295 if (!thread_group_leader(tsk)) {
2296 /*
2297 * a race with de_thread from another thread's exec()
2298 * may strip us of our leadership, if this happens,
2299 * there is no choice but to throw this task away and
2300 * try again; this is
2301 * "double-double-toil-and-trouble-check locking".
2302 */
2303 threadgroup_unlock(tsk);
2304 put_task_struct(tsk);
2305 goto retry_find_task;
2306 }
Li Zefan081aa452013-03-13 09:17:09 +08002307 }
2308
2309 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2310
Tejun Heocd3d0952011-12-12 18:12:21 -08002311 threadgroup_unlock(tsk);
2312
Paul Menagebbcb81d2007-10-18 23:39:32 -07002313 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002314out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002315 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002316 return ret;
2317}
2318
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002319/**
2320 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2321 * @from: attach to all cgroups of a given task
2322 * @tsk: the task to be attached
2323 */
2324int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2325{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002326 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002327 int retval = 0;
2328
Tejun Heo47cfcd02013-04-07 09:29:51 -07002329 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002330 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002331 struct cgroup *from_cgrp;
2332
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002333 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002334 continue;
2335
Tejun Heo96d365e2014-02-13 06:58:40 -05002336 down_read(&css_set_rwsem);
2337 from_cgrp = task_cgroup_from_root(from, root);
2338 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002339
Li Zefan6f4b7e62013-07-31 16:18:36 +08002340 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002341 if (retval)
2342 break;
2343 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002344 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002345
2346 return retval;
2347}
2348EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2349
Tejun Heo182446d2013-08-08 20:11:24 -04002350static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2351 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002352{
Tejun Heo182446d2013-08-08 20:11:24 -04002353 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002354}
2355
Tejun Heo182446d2013-08-08 20:11:24 -04002356static int cgroup_procs_write(struct cgroup_subsys_state *css,
2357 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002358{
Tejun Heo182446d2013-08-08 20:11:24 -04002359 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002360}
2361
Tejun Heo182446d2013-08-08 20:11:24 -04002362static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002363 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002364{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002365 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002366
2367 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002368 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002369 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002370 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002371 strlcpy(root->release_agent_path, buffer,
2372 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002373 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002374 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002375 return 0;
2376}
2377
Tejun Heo2da8ca82013-12-05 12:28:04 -05002378static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002379{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002380 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002381
Paul Menagee788e062008-07-25 01:46:59 -07002382 if (!cgroup_lock_live_group(cgrp))
2383 return -ENODEV;
2384 seq_puts(seq, cgrp->root->release_agent_path);
2385 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002386 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002387 return 0;
2388}
2389
Tejun Heo2da8ca82013-12-05 12:28:04 -05002390static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002391{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002392 struct cgroup *cgrp = seq_css(seq)->cgroup;
2393
2394 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002395 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002396}
2397
Tejun Heof8f22e52014-04-23 11:13:16 -04002398static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2399{
2400 struct cgroup_subsys *ss;
2401 bool printed = false;
2402 int ssid;
2403
2404 for_each_subsys(ss, ssid) {
2405 if (ss_mask & (1 << ssid)) {
2406 if (printed)
2407 seq_putc(seq, ' ');
2408 seq_printf(seq, "%s", ss->name);
2409 printed = true;
2410 }
2411 }
2412 if (printed)
2413 seq_putc(seq, '\n');
2414}
2415
2416/* show controllers which are currently attached to the default hierarchy */
2417static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2418{
2419 struct cgroup *cgrp = seq_css(seq)->cgroup;
2420
2421 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2422 return 0;
2423}
2424
2425/* show controllers which are enabled from the parent */
2426static int cgroup_controllers_show(struct seq_file *seq, void *v)
2427{
2428 struct cgroup *cgrp = seq_css(seq)->cgroup;
2429
2430 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2431 return 0;
2432}
2433
2434/* show controllers which are enabled for a given cgroup's children */
2435static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2436{
2437 struct cgroup *cgrp = seq_css(seq)->cgroup;
2438
2439 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2440 return 0;
2441}
2442
2443/**
2444 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2445 * @cgrp: root of the subtree to update csses for
2446 *
2447 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2448 * css associations need to be updated accordingly. This function looks up
2449 * all css_sets which are attached to the subtree, creates the matching
2450 * updated css_sets and migrates the tasks to the new ones.
2451 */
2452static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2453{
2454 LIST_HEAD(preloaded_csets);
2455 struct cgroup_subsys_state *css;
2456 struct css_set *src_cset;
2457 int ret;
2458
2459 lockdep_assert_held(&cgroup_tree_mutex);
2460 lockdep_assert_held(&cgroup_mutex);
2461
2462 /* look up all csses currently attached to @cgrp's subtree */
2463 down_read(&css_set_rwsem);
2464 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2465 struct cgrp_cset_link *link;
2466
2467 /* self is not affected by child_subsys_mask change */
2468 if (css->cgroup == cgrp)
2469 continue;
2470
2471 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2472 cgroup_migrate_add_src(link->cset, cgrp,
2473 &preloaded_csets);
2474 }
2475 up_read(&css_set_rwsem);
2476
2477 /* NULL dst indicates self on default hierarchy */
2478 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2479 if (ret)
2480 goto out_finish;
2481
2482 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2483 struct task_struct *last_task = NULL, *task;
2484
2485 /* src_csets precede dst_csets, break on the first dst_cset */
2486 if (!src_cset->mg_src_cgrp)
2487 break;
2488
2489 /*
2490 * All tasks in src_cset need to be migrated to the
2491 * matching dst_cset. Empty it process by process. We
2492 * walk tasks but migrate processes. The leader might even
2493 * belong to a different cset but such src_cset would also
2494 * be among the target src_csets because the default
2495 * hierarchy enforces per-process membership.
2496 */
2497 while (true) {
2498 down_read(&css_set_rwsem);
2499 task = list_first_entry_or_null(&src_cset->tasks,
2500 struct task_struct, cg_list);
2501 if (task) {
2502 task = task->group_leader;
2503 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2504 get_task_struct(task);
2505 }
2506 up_read(&css_set_rwsem);
2507
2508 if (!task)
2509 break;
2510
2511 /* guard against possible infinite loop */
2512 if (WARN(last_task == task,
2513 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2514 goto out_finish;
2515 last_task = task;
2516
2517 threadgroup_lock(task);
2518 /* raced against de_thread() from another thread? */
2519 if (!thread_group_leader(task)) {
2520 threadgroup_unlock(task);
2521 put_task_struct(task);
2522 continue;
2523 }
2524
2525 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2526
2527 threadgroup_unlock(task);
2528 put_task_struct(task);
2529
2530 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2531 goto out_finish;
2532 }
2533 }
2534
2535out_finish:
2536 cgroup_migrate_finish(&preloaded_csets);
2537 return ret;
2538}
2539
2540/* change the enabled child controllers for a cgroup in the default hierarchy */
2541static int cgroup_subtree_control_write(struct cgroup_subsys_state *dummy_css,
2542 struct cftype *cft, char *buffer)
2543{
Tejun Heo69dfa002014-05-04 15:09:13 -04002544 unsigned int enable_req = 0, disable_req = 0, enable, disable;
Tejun Heof8f22e52014-04-23 11:13:16 -04002545 struct cgroup *cgrp = dummy_css->cgroup, *child;
2546 struct cgroup_subsys *ss;
2547 char *tok, *p;
2548 int ssid, ret;
2549
2550 /*
2551 * Parse input - white space separated list of subsystem names
2552 * prefixed with either + or -.
2553 */
2554 p = buffer;
2555 while ((tok = strsep(&p, " \t\n"))) {
2556 for_each_subsys(ss, ssid) {
2557 if (ss->disabled || strcmp(tok + 1, ss->name))
2558 continue;
2559
2560 if (*tok == '+') {
2561 enable_req |= 1 << ssid;
2562 disable_req &= ~(1 << ssid);
2563 } else if (*tok == '-') {
2564 disable_req |= 1 << ssid;
2565 enable_req &= ~(1 << ssid);
2566 } else {
2567 return -EINVAL;
2568 }
2569 break;
2570 }
2571 if (ssid == CGROUP_SUBSYS_COUNT)
2572 return -EINVAL;
2573 }
2574
2575 /*
2576 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2577 * active_ref. cgroup_lock_live_group() already provides enough
2578 * protection. Ensure @cgrp stays accessible and break the
2579 * active_ref protection.
2580 */
2581 cgroup_get(cgrp);
2582 kernfs_break_active_protection(cgrp->control_kn);
2583retry:
2584 enable = enable_req;
2585 disable = disable_req;
2586
2587 mutex_lock(&cgroup_tree_mutex);
2588
2589 for_each_subsys(ss, ssid) {
2590 if (enable & (1 << ssid)) {
2591 if (cgrp->child_subsys_mask & (1 << ssid)) {
2592 enable &= ~(1 << ssid);
2593 continue;
2594 }
2595
2596 /*
2597 * Because css offlining is asynchronous, userland
2598 * might try to re-enable the same controller while
2599 * the previous instance is still around. In such
2600 * cases, wait till it's gone using offline_waitq.
2601 */
2602 cgroup_for_each_live_child(child, cgrp) {
2603 wait_queue_t wait;
2604
2605 if (!cgroup_css(child, ss))
2606 continue;
2607
2608 prepare_to_wait(&child->offline_waitq, &wait,
2609 TASK_UNINTERRUPTIBLE);
2610 mutex_unlock(&cgroup_tree_mutex);
2611 schedule();
2612 finish_wait(&child->offline_waitq, &wait);
2613 goto retry;
2614 }
2615
2616 /* unavailable or not enabled on the parent? */
2617 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2618 (cgrp->parent &&
2619 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2620 ret = -ENOENT;
2621 goto out_unlock_tree;
2622 }
2623 } else if (disable & (1 << ssid)) {
2624 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2625 disable &= ~(1 << ssid);
2626 continue;
2627 }
2628
2629 /* a child has it enabled? */
2630 cgroup_for_each_live_child(child, cgrp) {
2631 if (child->child_subsys_mask & (1 << ssid)) {
2632 ret = -EBUSY;
2633 goto out_unlock_tree;
2634 }
2635 }
2636 }
2637 }
2638
2639 if (!enable && !disable) {
2640 ret = 0;
2641 goto out_unlock_tree;
2642 }
2643
2644 if (!cgroup_lock_live_group(cgrp)) {
2645 ret = -ENODEV;
2646 goto out_unlock_tree;
2647 }
2648
2649 /*
2650 * Except for the root, child_subsys_mask must be zero for a cgroup
2651 * with tasks so that child cgroups don't compete against tasks.
2652 */
2653 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2654 ret = -EBUSY;
2655 goto out_unlock;
2656 }
2657
2658 /*
2659 * Create csses for enables and update child_subsys_mask. This
2660 * changes cgroup_e_css() results which in turn makes the
2661 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2662 * subtree to the updated csses.
2663 */
2664 for_each_subsys(ss, ssid) {
2665 if (!(enable & (1 << ssid)))
2666 continue;
2667
2668 cgroup_for_each_live_child(child, cgrp) {
2669 ret = create_css(child, ss);
2670 if (ret)
2671 goto err_undo_css;
2672 }
2673 }
2674
2675 cgrp->child_subsys_mask |= enable;
2676 cgrp->child_subsys_mask &= ~disable;
2677
2678 ret = cgroup_update_dfl_csses(cgrp);
2679 if (ret)
2680 goto err_undo_css;
2681
2682 /* all tasks are now migrated away from the old csses, kill them */
2683 for_each_subsys(ss, ssid) {
2684 if (!(disable & (1 << ssid)))
2685 continue;
2686
2687 cgroup_for_each_live_child(child, cgrp)
2688 kill_css(cgroup_css(child, ss));
2689 }
2690
2691 kernfs_activate(cgrp->kn);
2692 ret = 0;
2693out_unlock:
2694 mutex_unlock(&cgroup_mutex);
2695out_unlock_tree:
2696 mutex_unlock(&cgroup_tree_mutex);
2697 kernfs_unbreak_active_protection(cgrp->control_kn);
2698 cgroup_put(cgrp);
2699 return ret;
2700
2701err_undo_css:
2702 cgrp->child_subsys_mask &= ~enable;
2703 cgrp->child_subsys_mask |= disable;
2704
2705 for_each_subsys(ss, ssid) {
2706 if (!(enable & (1 << ssid)))
2707 continue;
2708
2709 cgroup_for_each_live_child(child, cgrp) {
2710 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2711 if (css)
2712 kill_css(css);
2713 }
2714 }
2715 goto out_unlock;
2716}
2717
Tejun Heo842b5972014-04-25 18:28:02 -04002718static int cgroup_populated_show(struct seq_file *seq, void *v)
2719{
2720 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2721 return 0;
2722}
2723
Tejun Heo2bd59d42014-02-11 11:52:49 -05002724static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2725 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002727 struct cgroup *cgrp = of->kn->parent->priv;
2728 struct cftype *cft = of->kn->priv;
2729 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002730 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731
Tejun Heo2bd59d42014-02-11 11:52:49 -05002732 /*
2733 * kernfs guarantees that a file isn't deleted with operations in
2734 * flight, which means that the matching css is and stays alive and
2735 * doesn't need to be pinned. The RCU locking is not necessary
2736 * either. It's just for the convenience of using cgroup_css().
2737 */
2738 rcu_read_lock();
2739 css = cgroup_css(cgrp, cft->ss);
2740 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741
Tejun Heoa742c592013-12-05 12:28:03 -05002742 if (cft->write_string) {
2743 ret = cft->write_string(css, cft, strstrip(buf));
2744 } else if (cft->write_u64) {
2745 unsigned long long v;
2746 ret = kstrtoull(buf, 0, &v);
2747 if (!ret)
2748 ret = cft->write_u64(css, cft, v);
2749 } else if (cft->write_s64) {
2750 long long v;
2751 ret = kstrtoll(buf, 0, &v);
2752 if (!ret)
2753 ret = cft->write_s64(css, cft, v);
2754 } else if (cft->trigger) {
2755 ret = cft->trigger(css, (unsigned int)cft->private);
2756 } else {
2757 ret = -EINVAL;
2758 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002759
Tejun Heoa742c592013-12-05 12:28:03 -05002760 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002761}
2762
Tejun Heo6612f052013-12-05 12:28:04 -05002763static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002764{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002765 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002766}
2767
2768static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2769{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002770 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002771}
2772
2773static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2774{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002775 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002776}
2777
2778static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2779{
Tejun Heo7da11272013-12-05 12:28:04 -05002780 struct cftype *cft = seq_cft(m);
2781 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002782
Tejun Heo2da8ca82013-12-05 12:28:04 -05002783 if (cft->seq_show)
2784 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002785
Tejun Heo896f5192013-12-05 12:28:04 -05002786 if (cft->read_u64)
2787 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2788 else if (cft->read_s64)
2789 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2790 else
2791 return -EINVAL;
2792 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002793}
2794
Tejun Heo2bd59d42014-02-11 11:52:49 -05002795static struct kernfs_ops cgroup_kf_single_ops = {
2796 .atomic_write_len = PAGE_SIZE,
2797 .write = cgroup_file_write,
2798 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002799};
2800
Tejun Heo2bd59d42014-02-11 11:52:49 -05002801static struct kernfs_ops cgroup_kf_ops = {
2802 .atomic_write_len = PAGE_SIZE,
2803 .write = cgroup_file_write,
2804 .seq_start = cgroup_seqfile_start,
2805 .seq_next = cgroup_seqfile_next,
2806 .seq_stop = cgroup_seqfile_stop,
2807 .seq_show = cgroup_seqfile_show,
2808};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002809
2810/*
2811 * cgroup_rename - Only allow simple rename of directories in place.
2812 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002813static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2814 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002815{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002816 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002817 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002818
Tejun Heo2bd59d42014-02-11 11:52:49 -05002819 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002820 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002821 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002822 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002823
Tejun Heo6db8e852013-06-14 11:18:22 -07002824 /*
2825 * This isn't a proper migration and its usefulness is very
2826 * limited. Disallow if sane_behavior.
2827 */
2828 if (cgroup_sane_behavior(cgrp))
2829 return -EPERM;
2830
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002831 /*
2832 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2833 * active_ref. kernfs_rename() doesn't require active_ref
2834 * protection. Break them before grabbing cgroup_tree_mutex.
2835 */
2836 kernfs_break_active_protection(new_parent);
2837 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002838
Tejun Heo2bd59d42014-02-11 11:52:49 -05002839 mutex_lock(&cgroup_tree_mutex);
2840 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002841
Tejun Heo2bd59d42014-02-11 11:52:49 -05002842 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002843
Tejun Heo2bd59d42014-02-11 11:52:49 -05002844 mutex_unlock(&cgroup_mutex);
2845 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002846
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002847 kernfs_unbreak_active_protection(kn);
2848 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002849 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002850}
2851
Tejun Heo49957f82014-04-07 16:44:47 -04002852/* set uid and gid of cgroup dirs and files to that of the creator */
2853static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2854{
2855 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2856 .ia_uid = current_fsuid(),
2857 .ia_gid = current_fsgid(), };
2858
2859 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2860 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2861 return 0;
2862
2863 return kernfs_setattr(kn, &iattr);
2864}
2865
Tejun Heo2bb566c2013-08-08 20:11:23 -04002866static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002867{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002868 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002869 struct kernfs_node *kn;
2870 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002871 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002872
Tejun Heo2bd59d42014-02-11 11:52:49 -05002873#ifdef CONFIG_DEBUG_LOCK_ALLOC
2874 key = &cft->lockdep_key;
2875#endif
2876 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2877 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2878 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002879 if (IS_ERR(kn))
2880 return PTR_ERR(kn);
2881
2882 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002883 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002884 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002885 return ret;
2886 }
2887
2888 if (cft->seq_show == cgroup_subtree_control_show)
2889 cgrp->control_kn = kn;
Tejun Heo842b5972014-04-25 18:28:02 -04002890 else if (cft->seq_show == cgroup_populated_show)
2891 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002892 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002893}
2894
Tejun Heob1f28d32013-06-28 16:24:10 -07002895/**
2896 * cgroup_addrm_files - add or remove files to a cgroup directory
2897 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002898 * @cfts: array of cftypes to be added
2899 * @is_add: whether to add or remove
2900 *
2901 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002902 * For removals, this function never fails. If addition fails, this
2903 * function doesn't remove files already added. The caller is responsible
2904 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002905 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002906static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2907 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002908{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002909 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002910 int ret;
2911
Tejun Heoace2bee2014-02-11 11:52:47 -05002912 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002913
2914 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002915 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002916 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2917 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002918 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2919 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002920 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2921 continue;
2922 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2923 continue;
2924
Li Zefan2739d3c2013-01-21 18:18:33 +08002925 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002926 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002927 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002928 pr_warn("%s: failed to add %s, err=%d\n",
2929 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002930 return ret;
2931 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002932 } else {
2933 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002934 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002935 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002936 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002937}
2938
Tejun Heo21a2d342014-02-12 09:29:49 -05002939static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002940{
2941 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002942 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002943 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002944 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002945 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002946
Tejun Heo21a2d342014-02-12 09:29:49 -05002947 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002948
Li Zefane8c82d22013-06-18 18:48:37 +08002949 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002950 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002951 struct cgroup *cgrp = css->cgroup;
2952
Li Zefane8c82d22013-06-18 18:48:37 +08002953 if (cgroup_is_dead(cgrp))
2954 continue;
2955
Tejun Heo21a2d342014-02-12 09:29:49 -05002956 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002957 if (ret)
2958 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002959 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002960
2961 if (is_add && !ret)
2962 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002963 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002964}
2965
Tejun Heo2da440a2014-02-11 11:52:48 -05002966static void cgroup_exit_cftypes(struct cftype *cfts)
2967{
2968 struct cftype *cft;
2969
Tejun Heo2bd59d42014-02-11 11:52:49 -05002970 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2971 /* free copy for custom atomic_write_len, see init_cftypes() */
2972 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2973 kfree(cft->kf_ops);
2974 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002975 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002976 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002977}
2978
Tejun Heo2bd59d42014-02-11 11:52:49 -05002979static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002980{
2981 struct cftype *cft;
2982
Tejun Heo2bd59d42014-02-11 11:52:49 -05002983 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2984 struct kernfs_ops *kf_ops;
2985
Tejun Heo0adb0702014-02-12 09:29:48 -05002986 WARN_ON(cft->ss || cft->kf_ops);
2987
Tejun Heo2bd59d42014-02-11 11:52:49 -05002988 if (cft->seq_start)
2989 kf_ops = &cgroup_kf_ops;
2990 else
2991 kf_ops = &cgroup_kf_single_ops;
2992
2993 /*
2994 * Ugh... if @cft wants a custom max_write_len, we need to
2995 * make a copy of kf_ops to set its atomic_write_len.
2996 */
2997 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2998 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2999 if (!kf_ops) {
3000 cgroup_exit_cftypes(cfts);
3001 return -ENOMEM;
3002 }
3003 kf_ops->atomic_write_len = cft->max_write_len;
3004 }
3005
3006 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003007 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003008 }
3009
3010 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003011}
3012
Tejun Heo21a2d342014-02-12 09:29:49 -05003013static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3014{
3015 lockdep_assert_held(&cgroup_tree_mutex);
3016
3017 if (!cfts || !cfts[0].ss)
3018 return -ENOENT;
3019
3020 list_del(&cfts->node);
3021 cgroup_apply_cftypes(cfts, false);
3022 cgroup_exit_cftypes(cfts);
3023 return 0;
3024}
3025
Tejun Heo8e3f6542012-04-01 12:09:55 -07003026/**
Tejun Heo80b13582014-02-12 09:29:48 -05003027 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3028 * @cfts: zero-length name terminated array of cftypes
3029 *
3030 * Unregister @cfts. Files described by @cfts are removed from all
3031 * existing cgroups and all future cgroups won't have them either. This
3032 * function can be called anytime whether @cfts' subsys is attached or not.
3033 *
3034 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3035 * registered.
3036 */
3037int cgroup_rm_cftypes(struct cftype *cfts)
3038{
Tejun Heo21a2d342014-02-12 09:29:49 -05003039 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003040
Tejun Heo21a2d342014-02-12 09:29:49 -05003041 mutex_lock(&cgroup_tree_mutex);
3042 ret = cgroup_rm_cftypes_locked(cfts);
3043 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003044 return ret;
3045}
3046
3047/**
3048 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3049 * @ss: target cgroup subsystem
3050 * @cfts: zero-length name terminated array of cftypes
3051 *
3052 * Register @cfts to @ss. Files described by @cfts are created for all
3053 * existing cgroups to which @ss is attached and all future cgroups will
3054 * have them too. This function can be called anytime whether @ss is
3055 * attached or not.
3056 *
3057 * Returns 0 on successful registration, -errno on failure. Note that this
3058 * function currently returns 0 as long as @cfts registration is successful
3059 * even if some file creation attempts on existing cgroups fail.
3060 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003061int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003062{
Tejun Heo9ccece82013-06-28 16:24:11 -07003063 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003064
Li Zefandc5736e2014-02-17 10:41:50 +08003065 if (!cfts || cfts[0].name[0] == '\0')
3066 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003067
Tejun Heo2bd59d42014-02-11 11:52:49 -05003068 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003069 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003070 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003071
Tejun Heo21a2d342014-02-12 09:29:49 -05003072 mutex_lock(&cgroup_tree_mutex);
3073
Tejun Heo0adb0702014-02-12 09:29:48 -05003074 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003075 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003076 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003077 cgroup_rm_cftypes_locked(cfts);
3078
3079 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003080 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003081}
Tejun Heo79578622012-04-01 12:09:56 -07003082
3083/**
Li Zefana043e3b2008-02-23 15:24:09 -08003084 * cgroup_task_count - count the number of tasks in a cgroup.
3085 * @cgrp: the cgroup in question
3086 *
3087 * Return the number of tasks in the cgroup.
3088 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003089static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003090{
3091 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003092 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003093
Tejun Heo96d365e2014-02-13 06:58:40 -05003094 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003095 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3096 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003097 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003098 return count;
3099}
3100
Tejun Heo574bd9f2012-11-09 09:12:29 -08003101/**
Tejun Heo492eb212013-08-08 20:11:25 -04003102 * css_next_child - find the next child of a given css
3103 * @pos_css: the current position (%NULL to initiate traversal)
3104 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003105 *
Tejun Heo492eb212013-08-08 20:11:25 -04003106 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003107 * under either cgroup_mutex or RCU read lock. The only requirement is
3108 * that @parent_css and @pos_css are accessible. The next sibling is
3109 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003110 */
Tejun Heo492eb212013-08-08 20:11:25 -04003111struct cgroup_subsys_state *
3112css_next_child(struct cgroup_subsys_state *pos_css,
3113 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003114{
Tejun Heo492eb212013-08-08 20:11:25 -04003115 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3116 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003117 struct cgroup *next;
3118
Tejun Heoace2bee2014-02-11 11:52:47 -05003119 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003120
3121 /*
3122 * @pos could already have been removed. Once a cgroup is removed,
3123 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003124 * changes. As CGRP_DEAD assertion is serialized and happens
3125 * before the cgroup is taken off the ->sibling list, if we see it
3126 * unasserted, it's guaranteed that the next sibling hasn't
3127 * finished its grace period even if it's already removed, and thus
3128 * safe to dereference from this RCU critical section. If
3129 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3130 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003131 *
3132 * If @pos is dead, its next pointer can't be dereferenced;
3133 * however, as each cgroup is given a monotonically increasing
3134 * unique serial number and always appended to the sibling list,
3135 * the next one can be found by walking the parent's children until
3136 * we see a cgroup with higher serial number than @pos's. While
3137 * this path can be slower, it's taken only when either the current
3138 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003139 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003140 if (!pos) {
3141 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3142 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003143 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003144 } else {
3145 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3146 if (next->serial_nr > pos->serial_nr)
3147 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003148 }
3149
Tejun Heo3b281af2014-04-23 11:13:15 -04003150 /*
3151 * @next, if not pointing to the head, can be dereferenced and is
3152 * the next sibling; however, it might have @ss disabled. If so,
3153 * fast-forward to the next enabled one.
3154 */
3155 while (&next->sibling != &cgrp->children) {
3156 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003157
Tejun Heo3b281af2014-04-23 11:13:15 -04003158 if (next_css)
3159 return next_css;
3160 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3161 }
3162 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003163}
Tejun Heo53fa5262013-05-24 10:55:38 +09003164
3165/**
Tejun Heo492eb212013-08-08 20:11:25 -04003166 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003167 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003168 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003169 *
Tejun Heo492eb212013-08-08 20:11:25 -04003170 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003171 * to visit for pre-order traversal of @root's descendants. @root is
3172 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003173 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003174 * While this function requires cgroup_mutex or RCU read locking, it
3175 * doesn't require the whole traversal to be contained in a single critical
3176 * section. This function will return the correct next descendant as long
3177 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178 */
Tejun Heo492eb212013-08-08 20:11:25 -04003179struct cgroup_subsys_state *
3180css_next_descendant_pre(struct cgroup_subsys_state *pos,
3181 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182{
Tejun Heo492eb212013-08-08 20:11:25 -04003183 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003184
Tejun Heoace2bee2014-02-11 11:52:47 -05003185 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003186
Tejun Heobd8815a2013-08-08 20:11:27 -04003187 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003188 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003189 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003190
3191 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003192 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193 if (next)
3194 return next;
3195
3196 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003197 while (pos != root) {
3198 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003199 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003200 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003201 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003202 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003203
3204 return NULL;
3205}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003206
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003207/**
Tejun Heo492eb212013-08-08 20:11:25 -04003208 * css_rightmost_descendant - return the rightmost descendant of a css
3209 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003210 *
Tejun Heo492eb212013-08-08 20:11:25 -04003211 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3212 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003213 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003214 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003215 * While this function requires cgroup_mutex or RCU read locking, it
3216 * doesn't require the whole traversal to be contained in a single critical
3217 * section. This function will return the correct rightmost descendant as
3218 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003219 */
Tejun Heo492eb212013-08-08 20:11:25 -04003220struct cgroup_subsys_state *
3221css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003222{
Tejun Heo492eb212013-08-08 20:11:25 -04003223 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003224
Tejun Heoace2bee2014-02-11 11:52:47 -05003225 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003226
3227 do {
3228 last = pos;
3229 /* ->prev isn't RCU safe, walk ->next till the end */
3230 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003231 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003232 pos = tmp;
3233 } while (pos);
3234
3235 return last;
3236}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003237
Tejun Heo492eb212013-08-08 20:11:25 -04003238static struct cgroup_subsys_state *
3239css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003240{
Tejun Heo492eb212013-08-08 20:11:25 -04003241 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003242
3243 do {
3244 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003245 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003246 } while (pos);
3247
3248 return last;
3249}
3250
3251/**
Tejun Heo492eb212013-08-08 20:11:25 -04003252 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003253 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003254 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003255 *
Tejun Heo492eb212013-08-08 20:11:25 -04003256 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003257 * to visit for post-order traversal of @root's descendants. @root is
3258 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003259 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003260 * While this function requires cgroup_mutex or RCU read locking, it
3261 * doesn't require the whole traversal to be contained in a single critical
3262 * section. This function will return the correct next descendant as long
3263 * as both @pos and @cgroup are accessible and @pos is a descendant of
3264 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003265 */
Tejun Heo492eb212013-08-08 20:11:25 -04003266struct cgroup_subsys_state *
3267css_next_descendant_post(struct cgroup_subsys_state *pos,
3268 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003269{
Tejun Heo492eb212013-08-08 20:11:25 -04003270 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003271
Tejun Heoace2bee2014-02-11 11:52:47 -05003272 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003273
Tejun Heo58b79a92013-09-06 15:31:08 -04003274 /* if first iteration, visit leftmost descendant which may be @root */
3275 if (!pos)
3276 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003277
Tejun Heobd8815a2013-08-08 20:11:27 -04003278 /* if we visited @root, we're done */
3279 if (pos == root)
3280 return NULL;
3281
Tejun Heo574bd9f2012-11-09 09:12:29 -08003282 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003283 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003284 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003285 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003286
3287 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003288 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003289}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003290
Tejun Heo0942eee2013-08-08 20:11:26 -04003291/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003292 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003293 * @it: the iterator to advance
3294 *
3295 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003296 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003297static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003298{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003299 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003300 struct cgrp_cset_link *link;
3301 struct css_set *cset;
3302
3303 /* Advance to the next non-empty css_set */
3304 do {
3305 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003306 if (l == it->cset_head) {
3307 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003308 return;
3309 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003310
3311 if (it->ss) {
3312 cset = container_of(l, struct css_set,
3313 e_cset_node[it->ss->id]);
3314 } else {
3315 link = list_entry(l, struct cgrp_cset_link, cset_link);
3316 cset = link->cset;
3317 }
Tejun Heoc7561122014-02-25 10:04:01 -05003318 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3319
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003320 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003321
3322 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003323 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003324 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003325 it->task_pos = cset->mg_tasks.next;
3326
3327 it->tasks_head = &cset->tasks;
3328 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003329}
3330
Tejun Heo0942eee2013-08-08 20:11:26 -04003331/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003332 * css_task_iter_start - initiate task iteration
3333 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003334 * @it: the task iterator to use
3335 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003336 * Initiate iteration through the tasks of @css. The caller can call
3337 * css_task_iter_next() to walk through the tasks until the function
3338 * returns NULL. On completion of iteration, css_task_iter_end() must be
3339 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003340 *
3341 * Note that this function acquires a lock which is released when the
3342 * iteration finishes. The caller can't sleep while iteration is in
3343 * progress.
3344 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003345void css_task_iter_start(struct cgroup_subsys_state *css,
3346 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003347 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003348{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003349 /* no one should try to iterate before mounting cgroups */
3350 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003351
Tejun Heo96d365e2014-02-13 06:58:40 -05003352 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003353
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003354 it->ss = css->ss;
3355
3356 if (it->ss)
3357 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3358 else
3359 it->cset_pos = &css->cgroup->cset_links;
3360
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003361 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003362
Tejun Heo72ec7022013-08-08 20:11:26 -04003363 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003364}
3365
Tejun Heo0942eee2013-08-08 20:11:26 -04003366/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003367 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003368 * @it: the task iterator being iterated
3369 *
3370 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003371 * initialized via css_task_iter_start(). Returns NULL when the iteration
3372 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003373 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003374struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003375{
3376 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003377 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003378
3379 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003380 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003381 return NULL;
3382 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003383
3384 /*
3385 * Advance iterator to find next entry. cset->tasks is consumed
3386 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3387 * next cset.
3388 */
Paul Menage817929e2007-10-18 23:39:36 -07003389 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003390
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003391 if (l == it->tasks_head)
3392 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003393
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003394 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003395 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003396 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003397 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003398
Paul Menage817929e2007-10-18 23:39:36 -07003399 return res;
3400}
3401
Tejun Heo0942eee2013-08-08 20:11:26 -04003402/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003403 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003404 * @it: the task iterator to finish
3405 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003406 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003407 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003408void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003409 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003410{
Tejun Heo96d365e2014-02-13 06:58:40 -05003411 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003412}
3413
3414/**
3415 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3416 * @to: cgroup to which the tasks will be moved
3417 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003418 *
3419 * Locking rules between cgroup_post_fork() and the migration path
3420 * guarantee that, if a task is forking while being migrated, the new child
3421 * is guaranteed to be either visible in the source cgroup after the
3422 * parent's migration is complete or put into the target cgroup. No task
3423 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003424 */
3425int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3426{
Tejun Heo952aaa12014-02-25 10:04:03 -05003427 LIST_HEAD(preloaded_csets);
3428 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003429 struct css_task_iter it;
3430 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003431 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003432
Tejun Heo952aaa12014-02-25 10:04:03 -05003433 mutex_lock(&cgroup_mutex);
3434
3435 /* all tasks in @from are being moved, all csets are source */
3436 down_read(&css_set_rwsem);
3437 list_for_each_entry(link, &from->cset_links, cset_link)
3438 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3439 up_read(&css_set_rwsem);
3440
3441 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3442 if (ret)
3443 goto out_err;
3444
3445 /*
3446 * Migrate tasks one-by-one until @form is empty. This fails iff
3447 * ->can_attach() fails.
3448 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003449 do {
3450 css_task_iter_start(&from->dummy_css, &it);
3451 task = css_task_iter_next(&it);
3452 if (task)
3453 get_task_struct(task);
3454 css_task_iter_end(&it);
3455
3456 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003457 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003458 put_task_struct(task);
3459 }
3460 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003461out_err:
3462 cgroup_migrate_finish(&preloaded_csets);
3463 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003464 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003465}
3466
Paul Menage817929e2007-10-18 23:39:36 -07003467/*
Ben Blum102a7752009-09-23 15:56:26 -07003468 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003469 *
3470 * Reading this file can return large amounts of data if a cgroup has
3471 * *lots* of attached tasks. So it may need several calls to read(),
3472 * but we cannot guarantee that the information we produce is correct
3473 * unless we produce it entirely atomically.
3474 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003475 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003476
Li Zefan24528252012-01-20 11:58:43 +08003477/* which pidlist file are we talking about? */
3478enum cgroup_filetype {
3479 CGROUP_FILE_PROCS,
3480 CGROUP_FILE_TASKS,
3481};
3482
3483/*
3484 * A pidlist is a list of pids that virtually represents the contents of one
3485 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3486 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3487 * to the cgroup.
3488 */
3489struct cgroup_pidlist {
3490 /*
3491 * used to find which pidlist is wanted. doesn't change as long as
3492 * this particular list stays in the list.
3493 */
3494 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3495 /* array of xids */
3496 pid_t *list;
3497 /* how many elements the above list has */
3498 int length;
Li Zefan24528252012-01-20 11:58:43 +08003499 /* each of these stored in a list by its cgroup */
3500 struct list_head links;
3501 /* pointer to the cgroup we belong to, for list removal purposes */
3502 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003503 /* for delayed destruction */
3504 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003505};
3506
Paul Menagebbcb81d2007-10-18 23:39:32 -07003507/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003508 * The following two functions "fix" the issue where there are more pids
3509 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3510 * TODO: replace with a kernel-wide solution to this problem
3511 */
3512#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3513static void *pidlist_allocate(int count)
3514{
3515 if (PIDLIST_TOO_LARGE(count))
3516 return vmalloc(count * sizeof(pid_t));
3517 else
3518 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3519}
Tejun Heob1a21362013-11-29 10:42:58 -05003520
Ben Blumd1d9fd32009-09-23 15:56:28 -07003521static void pidlist_free(void *p)
3522{
3523 if (is_vmalloc_addr(p))
3524 vfree(p);
3525 else
3526 kfree(p);
3527}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003528
3529/*
Tejun Heob1a21362013-11-29 10:42:58 -05003530 * Used to destroy all pidlists lingering waiting for destroy timer. None
3531 * should be left afterwards.
3532 */
3533static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3534{
3535 struct cgroup_pidlist *l, *tmp_l;
3536
3537 mutex_lock(&cgrp->pidlist_mutex);
3538 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3539 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3540 mutex_unlock(&cgrp->pidlist_mutex);
3541
3542 flush_workqueue(cgroup_pidlist_destroy_wq);
3543 BUG_ON(!list_empty(&cgrp->pidlists));
3544}
3545
3546static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3547{
3548 struct delayed_work *dwork = to_delayed_work(work);
3549 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3550 destroy_dwork);
3551 struct cgroup_pidlist *tofree = NULL;
3552
3553 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003554
3555 /*
Tejun Heo04502362013-11-29 10:42:59 -05003556 * Destroy iff we didn't get queued again. The state won't change
3557 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003558 */
Tejun Heo04502362013-11-29 10:42:59 -05003559 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003560 list_del(&l->links);
3561 pidlist_free(l->list);
3562 put_pid_ns(l->key.ns);
3563 tofree = l;
3564 }
3565
Tejun Heob1a21362013-11-29 10:42:58 -05003566 mutex_unlock(&l->owner->pidlist_mutex);
3567 kfree(tofree);
3568}
3569
3570/*
Ben Blum102a7752009-09-23 15:56:26 -07003571 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003572 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003573 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003574static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003575{
Ben Blum102a7752009-09-23 15:56:26 -07003576 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003577
3578 /*
3579 * we presume the 0th element is unique, so i starts at 1. trivial
3580 * edge cases first; no work needs to be done for either
3581 */
3582 if (length == 0 || length == 1)
3583 return length;
3584 /* src and dest walk down the list; dest counts unique elements */
3585 for (src = 1; src < length; src++) {
3586 /* find next unique element */
3587 while (list[src] == list[src-1]) {
3588 src++;
3589 if (src == length)
3590 goto after;
3591 }
3592 /* dest always points to where the next unique element goes */
3593 list[dest] = list[src];
3594 dest++;
3595 }
3596after:
Ben Blum102a7752009-09-23 15:56:26 -07003597 return dest;
3598}
3599
Tejun Heoafb2bc12013-11-29 10:42:59 -05003600/*
3601 * The two pid files - task and cgroup.procs - guaranteed that the result
3602 * is sorted, which forced this whole pidlist fiasco. As pid order is
3603 * different per namespace, each namespace needs differently sorted list,
3604 * making it impossible to use, for example, single rbtree of member tasks
3605 * sorted by task pointer. As pidlists can be fairly large, allocating one
3606 * per open file is dangerous, so cgroup had to implement shared pool of
3607 * pidlists keyed by cgroup and namespace.
3608 *
3609 * All this extra complexity was caused by the original implementation
3610 * committing to an entirely unnecessary property. In the long term, we
3611 * want to do away with it. Explicitly scramble sort order if
3612 * sane_behavior so that no such expectation exists in the new interface.
3613 *
3614 * Scrambling is done by swapping every two consecutive bits, which is
3615 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3616 */
3617static pid_t pid_fry(pid_t pid)
3618{
3619 unsigned a = pid & 0x55555555;
3620 unsigned b = pid & 0xAAAAAAAA;
3621
3622 return (a << 1) | (b >> 1);
3623}
3624
3625static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3626{
3627 if (cgroup_sane_behavior(cgrp))
3628 return pid_fry(pid);
3629 else
3630 return pid;
3631}
3632
Ben Blum102a7752009-09-23 15:56:26 -07003633static int cmppid(const void *a, const void *b)
3634{
3635 return *(pid_t *)a - *(pid_t *)b;
3636}
3637
Tejun Heoafb2bc12013-11-29 10:42:59 -05003638static int fried_cmppid(const void *a, const void *b)
3639{
3640 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3641}
3642
Ben Blum72a8cb32009-09-23 15:56:27 -07003643static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3644 enum cgroup_filetype type)
3645{
3646 struct cgroup_pidlist *l;
3647 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003648 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003649
Tejun Heoe6b81712013-11-29 10:42:59 -05003650 lockdep_assert_held(&cgrp->pidlist_mutex);
3651
3652 list_for_each_entry(l, &cgrp->pidlists, links)
3653 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003655 return NULL;
3656}
3657
Ben Blum72a8cb32009-09-23 15:56:27 -07003658/*
3659 * find the appropriate pidlist for our purpose (given procs vs tasks)
3660 * returns with the lock on that pidlist already held, and takes care
3661 * of the use count, or returns NULL with no locks held if we're out of
3662 * memory.
3663 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003664static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3665 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003666{
3667 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003668
Tejun Heoe6b81712013-11-29 10:42:59 -05003669 lockdep_assert_held(&cgrp->pidlist_mutex);
3670
3671 l = cgroup_pidlist_find(cgrp, type);
3672 if (l)
3673 return l;
3674
Ben Blum72a8cb32009-09-23 15:56:27 -07003675 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003676 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003677 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003678 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003679
Tejun Heob1a21362013-11-29 10:42:58 -05003680 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003681 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003682 /* don't need task_nsproxy() if we're looking at ourself */
3683 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003684 l->owner = cgrp;
3685 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003686 return l;
3687}
3688
3689/*
Ben Blum102a7752009-09-23 15:56:26 -07003690 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3691 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003692static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3693 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003694{
3695 pid_t *array;
3696 int length;
3697 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003698 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003699 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003700 struct cgroup_pidlist *l;
3701
Tejun Heo4bac00d2013-11-29 10:42:59 -05003702 lockdep_assert_held(&cgrp->pidlist_mutex);
3703
Ben Blum102a7752009-09-23 15:56:26 -07003704 /*
3705 * If cgroup gets more users after we read count, we won't have
3706 * enough space - tough. This race is indistinguishable to the
3707 * caller from the case that the additional cgroup users didn't
3708 * show up until sometime later on.
3709 */
3710 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003711 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003712 if (!array)
3713 return -ENOMEM;
3714 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003715 css_task_iter_start(&cgrp->dummy_css, &it);
3716 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003717 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003718 break;
Ben Blum102a7752009-09-23 15:56:26 -07003719 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003720 if (type == CGROUP_FILE_PROCS)
3721 pid = task_tgid_vnr(tsk);
3722 else
3723 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003724 if (pid > 0) /* make sure to only use valid results */
3725 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003726 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003727 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003728 length = n;
3729 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003730 if (cgroup_sane_behavior(cgrp))
3731 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3732 else
3733 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003734 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003735 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003736
Tejun Heoe6b81712013-11-29 10:42:59 -05003737 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003739 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003740 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003741 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003742 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003743
3744 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003745 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003746 l->list = array;
3747 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003748 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003749 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003750}
3751
Balbir Singh846c7bb2007-10-18 23:39:44 -07003752/**
Li Zefana043e3b2008-02-23 15:24:09 -08003753 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003754 * @stats: cgroupstats to fill information into
3755 * @dentry: A dentry entry belonging to the cgroup for which stats have
3756 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003757 *
3758 * Build and fill cgroupstats so that taskstats can export it to user
3759 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003760 */
3761int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3762{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003763 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003764 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003765 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003766 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003767
Tejun Heo2bd59d42014-02-11 11:52:49 -05003768 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3769 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3770 kernfs_type(kn) != KERNFS_DIR)
3771 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003772
Li Zefanbad34662014-02-14 16:54:28 +08003773 mutex_lock(&cgroup_mutex);
3774
Tejun Heo2bd59d42014-02-11 11:52:49 -05003775 /*
3776 * We aren't being called from kernfs and there's no guarantee on
3777 * @kn->priv's validity. For this and css_tryget_from_dir(),
3778 * @kn->priv is RCU safe. Let's do the RCU dancing.
3779 */
3780 rcu_read_lock();
3781 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003782 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003783 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003784 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003785 return -ENOENT;
3786 }
Li Zefanbad34662014-02-14 16:54:28 +08003787 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003788
Tejun Heo72ec7022013-08-08 20:11:26 -04003789 css_task_iter_start(&cgrp->dummy_css, &it);
3790 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003791 switch (tsk->state) {
3792 case TASK_RUNNING:
3793 stats->nr_running++;
3794 break;
3795 case TASK_INTERRUPTIBLE:
3796 stats->nr_sleeping++;
3797 break;
3798 case TASK_UNINTERRUPTIBLE:
3799 stats->nr_uninterruptible++;
3800 break;
3801 case TASK_STOPPED:
3802 stats->nr_stopped++;
3803 break;
3804 default:
3805 if (delayacct_is_task_waiting_on_io(tsk))
3806 stats->nr_io_wait++;
3807 break;
3808 }
3809 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003810 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003811
Li Zefanbad34662014-02-14 16:54:28 +08003812 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003813 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003814}
3815
Paul Menage8f3ff202009-09-23 15:56:25 -07003816
Paul Menagecc31edc2008-10-18 20:28:04 -07003817/*
Ben Blum102a7752009-09-23 15:56:26 -07003818 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003819 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003820 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003821 */
3822
Ben Blum102a7752009-09-23 15:56:26 -07003823static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003824{
3825 /*
3826 * Initially we receive a position value that corresponds to
3827 * one more than the last pid shown (or 0 on the first call or
3828 * after a seek to the start). Use a binary-search to find the
3829 * next pid to display, if any
3830 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003831 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003832 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003833 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003834 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003835 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003836 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003837
Tejun Heo4bac00d2013-11-29 10:42:59 -05003838 mutex_lock(&cgrp->pidlist_mutex);
3839
3840 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003841 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003842 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003843 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003844 * could already have been destroyed.
3845 */
Tejun Heo5d224442013-12-05 12:28:04 -05003846 if (of->priv)
3847 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003848
3849 /*
3850 * Either this is the first start() after open or the matching
3851 * pidlist has been destroyed inbetween. Create a new one.
3852 */
Tejun Heo5d224442013-12-05 12:28:04 -05003853 if (!of->priv) {
3854 ret = pidlist_array_load(cgrp, type,
3855 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003856 if (ret)
3857 return ERR_PTR(ret);
3858 }
Tejun Heo5d224442013-12-05 12:28:04 -05003859 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003860
Paul Menagecc31edc2008-10-18 20:28:04 -07003861 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003862 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003863
Paul Menagecc31edc2008-10-18 20:28:04 -07003864 while (index < end) {
3865 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003866 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003867 index = mid;
3868 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003869 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 index = mid + 1;
3871 else
3872 end = mid;
3873 }
3874 }
3875 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003876 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003877 return NULL;
3878 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003879 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003880 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003881 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003882}
3883
Ben Blum102a7752009-09-23 15:56:26 -07003884static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003885{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003886 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003887 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003888
Tejun Heo5d224442013-12-05 12:28:04 -05003889 if (l)
3890 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003891 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003892 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003893}
3894
Ben Blum102a7752009-09-23 15:56:26 -07003895static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003896{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003897 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003898 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003899 pid_t *p = v;
3900 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003901 /*
3902 * Advance to the next pid in the array. If this goes off the
3903 * end, we're done
3904 */
3905 p++;
3906 if (p >= end) {
3907 return NULL;
3908 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003909 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003910 return p;
3911 }
3912}
3913
Ben Blum102a7752009-09-23 15:56:26 -07003914static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003915{
3916 return seq_printf(s, "%d\n", *(int *)v);
3917}
3918
Tejun Heo182446d2013-08-08 20:11:24 -04003919static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3920 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003921{
Tejun Heo182446d2013-08-08 20:11:24 -04003922 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003923}
3924
Tejun Heo182446d2013-08-08 20:11:24 -04003925static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3926 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003927{
Tejun Heo182446d2013-08-08 20:11:24 -04003928 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003929 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003930 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003931 else
Tejun Heo182446d2013-08-08 20:11:24 -04003932 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003933 return 0;
3934}
3935
Tejun Heo182446d2013-08-08 20:11:24 -04003936static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3937 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003938{
Tejun Heo182446d2013-08-08 20:11:24 -04003939 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003940}
3941
Tejun Heo182446d2013-08-08 20:11:24 -04003942static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3943 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003944{
3945 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003946 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 else
Tejun Heo182446d2013-08-08 20:11:24 -04003948 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003949 return 0;
3950}
3951
Tejun Heod5c56ce2013-06-03 19:14:34 -07003952static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003953 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003954 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003955 .seq_start = cgroup_pidlist_start,
3956 .seq_next = cgroup_pidlist_next,
3957 .seq_stop = cgroup_pidlist_stop,
3958 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003959 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003960 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003961 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003962 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003963 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003964 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003965 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003966 .read_u64 = cgroup_clone_children_read,
3967 .write_u64 = cgroup_clone_children_write,
3968 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003969 {
Tejun Heo873fe092013-04-14 20:15:26 -07003970 .name = "cgroup.sane_behavior",
3971 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003972 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003973 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003974 {
3975 .name = "cgroup.controllers",
3976 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3977 .seq_show = cgroup_root_controllers_show,
3978 },
3979 {
3980 .name = "cgroup.controllers",
3981 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3982 .seq_show = cgroup_controllers_show,
3983 },
3984 {
3985 .name = "cgroup.subtree_control",
3986 .flags = CFTYPE_ONLY_ON_DFL,
3987 .seq_show = cgroup_subtree_control_show,
3988 .write_string = cgroup_subtree_control_write,
3989 },
Tejun Heo842b5972014-04-25 18:28:02 -04003990 {
3991 .name = "cgroup.populated",
3992 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3993 .seq_show = cgroup_populated_show,
3994 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003995
3996 /*
3997 * Historical crazy stuff. These don't have "cgroup." prefix and
3998 * don't exist if sane_behavior. If you're depending on these, be
3999 * prepared to be burned.
4000 */
4001 {
4002 .name = "tasks",
4003 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004004 .seq_start = cgroup_pidlist_start,
4005 .seq_next = cgroup_pidlist_next,
4006 .seq_stop = cgroup_pidlist_stop,
4007 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004008 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004009 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004010 .mode = S_IRUGO | S_IWUSR,
4011 },
4012 {
4013 .name = "notify_on_release",
4014 .flags = CFTYPE_INSANE,
4015 .read_u64 = cgroup_read_notify_on_release,
4016 .write_u64 = cgroup_write_notify_on_release,
4017 },
Tejun Heo873fe092013-04-14 20:15:26 -07004018 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004019 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004020 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004021 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004022 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004023 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004024 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004025 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004026};
4027
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004028/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004029 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004030 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004031 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004032 *
4033 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004034 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004035static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004036{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004037 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004038 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004039
Tejun Heo8e3f6542012-04-01 12:09:55 -07004040 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004041 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004042 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004043
Tejun Heo69dfa002014-05-04 15:09:13 -04004044 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004045 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004046
Tejun Heo0adb0702014-02-12 09:29:48 -05004047 list_for_each_entry(cfts, &ss->cfts, node) {
4048 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004049 if (ret < 0)
4050 goto err;
4051 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004052 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004054err:
4055 cgroup_clear_dir(cgrp, subsys_mask);
4056 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057}
4058
Tejun Heo0c21ead2013-08-13 20:22:51 -04004059/*
4060 * css destruction is four-stage process.
4061 *
4062 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4063 * Implemented in kill_css().
4064 *
4065 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4066 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4067 * by invoking offline_css(). After offlining, the base ref is put.
4068 * Implemented in css_killed_work_fn().
4069 *
4070 * 3. When the percpu_ref reaches zero, the only possible remaining
4071 * accessors are inside RCU read sections. css_release() schedules the
4072 * RCU callback.
4073 *
4074 * 4. After the grace period, the css can be freed. Implemented in
4075 * css_free_work_fn().
4076 *
4077 * It is actually hairier because both step 2 and 4 require process context
4078 * and thus involve punting to css->destroy_work adding two additional
4079 * steps to the already complex sequence.
4080 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004081static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004082{
4083 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004084 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004085 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004086
Tejun Heo0ae78e02013-08-13 11:01:54 -04004087 if (css->parent)
4088 css_put(css->parent);
4089
Tejun Heo0c21ead2013-08-13 20:22:51 -04004090 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004091 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004092}
4093
4094static void css_free_rcu_fn(struct rcu_head *rcu_head)
4095{
4096 struct cgroup_subsys_state *css =
4097 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4098
Tejun Heo0c21ead2013-08-13 20:22:51 -04004099 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004100 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004101}
4102
Tejun Heod3daf282013-06-13 19:39:16 -07004103static void css_release(struct percpu_ref *ref)
4104{
4105 struct cgroup_subsys_state *css =
4106 container_of(ref, struct cgroup_subsys_state, refcnt);
4107
Monam Agarwal01a97142014-03-24 00:17:18 +05304108 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004109 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004110}
4111
Tejun Heoddfcada2014-05-04 15:09:14 -04004112static void init_and_link_css(struct cgroup_subsys_state *css,
4113 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114{
Tejun Heoddfcada2014-05-04 15:09:14 -04004115 cgroup_get(cgrp);
4116
Paul Menagebd89aab2007-10-18 23:40:44 -07004117 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004118 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004119 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004120
Tejun Heoddfcada2014-05-04 15:09:14 -04004121 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004122 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004123 css_get(css->parent);
4124 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004126 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004127
Tejun Heoca8bdca2013-08-26 18:40:56 -04004128 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129}
4130
Li Zefan2a4ac632013-07-31 16:16:40 +08004131/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004132static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004133{
Tejun Heo623f9262013-08-13 11:01:55 -04004134 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004135 int ret = 0;
4136
Tejun Heoace2bee2014-02-11 11:52:47 -05004137 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004138 lockdep_assert_held(&cgroup_mutex);
4139
Tejun Heo92fb9742012-11-19 08:13:38 -08004140 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004141 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004142 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004143 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004144 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004145 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004146 }
Tejun Heob1929db2012-11-19 08:13:38 -08004147 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004148}
4149
Li Zefan2a4ac632013-07-31 16:16:40 +08004150/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004151static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004152{
Tejun Heo623f9262013-08-13 11:01:55 -04004153 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004154
Tejun Heoace2bee2014-02-11 11:52:47 -05004155 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004156 lockdep_assert_held(&cgroup_mutex);
4157
4158 if (!(css->flags & CSS_ONLINE))
4159 return;
4160
Li Zefand7eeac12013-03-12 15:35:59 -07004161 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004162 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004163
Tejun Heoeb954192013-08-08 20:11:23 -04004164 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004165 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004166 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004167
4168 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004169}
4170
Tejun Heoc81c925a2013-12-06 15:11:56 -05004171/**
4172 * create_css - create a cgroup_subsys_state
4173 * @cgrp: the cgroup new css will be associated with
4174 * @ss: the subsys of new css
4175 *
4176 * Create a new css associated with @cgrp - @ss pair. On success, the new
4177 * css is online and installed in @cgrp with all interface files created.
4178 * Returns 0 on success, -errno on failure.
4179 */
4180static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4181{
4182 struct cgroup *parent = cgrp->parent;
4183 struct cgroup_subsys_state *css;
4184 int err;
4185
Tejun Heoc81c925a2013-12-06 15:11:56 -05004186 lockdep_assert_held(&cgroup_mutex);
4187
4188 css = ss->css_alloc(cgroup_css(parent, ss));
4189 if (IS_ERR(css))
4190 return PTR_ERR(css);
4191
Tejun Heoddfcada2014-05-04 15:09:14 -04004192 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004193
Tejun Heoc81c925a2013-12-06 15:11:56 -05004194 err = percpu_ref_init(&css->refcnt, css_release);
4195 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004196 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004197
Tejun Heoaec25022014-02-08 10:36:58 -05004198 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004199 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004200 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004201
4202 err = online_css(css);
4203 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004204 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004205
Tejun Heoc81c925a2013-12-06 15:11:56 -05004206 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4207 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004208 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 -04004209 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004210 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004211 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004212 ss->warned_broken_hierarchy = true;
4213 }
4214
4215 return 0;
4216
Li Zefan3eb59ec2014-03-18 17:02:36 +08004217err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004218 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004219err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004220 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004221err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004222 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004223 return err;
4224}
4225
Tejun Heo2bd59d42014-02-11 11:52:49 -05004226/**
Li Zefana043e3b2008-02-23 15:24:09 -08004227 * cgroup_create - create a cgroup
4228 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05004229 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05004230 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231 */
Tejun Heoe61734c2014-02-12 09:29:50 -05004232static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05004233 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004234{
Paul Menagebd89aab2007-10-18 23:40:44 -07004235 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004236 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004237 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004239 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240
Tejun Heo0a950f62012-11-19 09:02:12 -08004241 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004242 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4243 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 return -ENOMEM;
4245
Tejun Heoace2bee2014-02-11 11:52:47 -05004246 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004247
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004248 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004249 * Only live parents can have children. Note that the liveliness
4250 * check isn't strictly necessary because cgroup_mkdir() and
4251 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4252 * anyway so that locking is contained inside cgroup proper and we
4253 * don't get nasty surprises if we ever grow another caller.
4254 */
4255 if (!cgroup_lock_live_group(parent)) {
4256 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004257 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004258 }
4259
4260 /*
4261 * Temporarily set the pointer to NULL, so idr_find() won't return
4262 * a half-baked cgroup.
4263 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004264 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004265 if (cgrp->id < 0) {
4266 err = -ENOMEM;
4267 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004268 }
4269
Paul Menagecc31edc2008-10-18 20:28:04 -07004270 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271
Paul Menagebd89aab2007-10-18 23:40:44 -07004272 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004273 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004274 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275
Li Zefanb6abdb02008-03-04 14:28:19 -08004276 if (notify_on_release(parent))
4277 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4278
Tejun Heo2260e7f2012-11-19 08:13:38 -08004279 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4280 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004281
Tejun Heo2bd59d42014-02-11 11:52:49 -05004282 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004283 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004284 if (IS_ERR(kn)) {
4285 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004286 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004287 }
4288 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Tejun Heo6f305582014-02-12 09:29:50 -05004290 /*
4291 * This extra ref will be put in cgroup_free_fn() and guarantees
4292 * that @cgrp->kn is always accessible.
4293 */
4294 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004295
Tejun Heo00356bd2013-06-18 11:14:22 -07004296 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004297
Tejun Heo4e139af2012-11-19 08:13:36 -08004298 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004299 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004300 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004301 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004302
Tejun Heo0d802552013-12-06 15:11:56 -05004303 /*
4304 * @cgrp is now fully operational. If something fails after this
4305 * point, it'll be released via the normal destruction path.
4306 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004307 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004308
Tejun Heo49957f82014-04-07 16:44:47 -04004309 err = cgroup_kn_set_ugid(kn);
4310 if (err)
4311 goto err_destroy;
4312
Tejun Heo2bb566c2013-08-08 20:11:23 -04004313 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004314 if (err)
4315 goto err_destroy;
4316
Tejun Heo9d403e92013-12-06 15:11:56 -05004317 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004318 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004319 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05004320 err = create_css(cgrp, ss);
4321 if (err)
4322 goto err_destroy;
4323 }
Tejun Heoa8638032012-11-09 09:12:29 -08004324 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325
Tejun Heobd53d612014-04-23 11:13:16 -04004326 /*
4327 * On the default hierarchy, a child doesn't automatically inherit
4328 * child_subsys_mask from the parent. Each is configured manually.
4329 */
4330 if (!cgroup_on_dfl(cgrp))
4331 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004332
Tejun Heo2bd59d42014-02-11 11:52:49 -05004333 kernfs_activate(kn);
4334
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004336 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004337
4338 return 0;
4339
Tejun Heo0a950f62012-11-19 09:02:12 -08004340err_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004341 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004342err_unlock:
4343 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004344err_unlock_tree:
4345 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004346 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004348
4349err_destroy:
4350 cgroup_destroy_locked(cgrp);
4351 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004352 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004353 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004354}
4355
Tejun Heo2bd59d42014-02-11 11:52:49 -05004356static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4357 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004359 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004360 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004362 /*
4363 * cgroup_create() grabs cgroup_tree_mutex which nests outside
4364 * kernfs active_ref and cgroup_create() already synchronizes
4365 * properly against removal through cgroup_lock_live_group().
4366 * Break it before calling cgroup_create().
4367 */
4368 cgroup_get(parent);
4369 kernfs_break_active_protection(parent_kn);
4370
4371 ret = cgroup_create(parent, name, mode);
4372
4373 kernfs_unbreak_active_protection(parent_kn);
4374 cgroup_put(parent);
4375 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376}
4377
Tejun Heo223dbc32013-08-13 20:22:50 -04004378/*
4379 * This is called when the refcnt of a css is confirmed to be killed.
4380 * css_tryget() is now guaranteed to fail.
4381 */
4382static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004383{
Tejun Heo223dbc32013-08-13 20:22:50 -04004384 struct cgroup_subsys_state *css =
4385 container_of(work, struct cgroup_subsys_state, destroy_work);
4386 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004387
Tejun Heoace2bee2014-02-11 11:52:47 -05004388 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004389 mutex_lock(&cgroup_mutex);
4390
4391 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004392 * css_tryget() is guaranteed to fail now. Tell subsystems to
4393 * initate destruction.
4394 */
4395 offline_css(css);
4396
4397 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004398 * If @cgrp is marked dead, it's waiting for refs of all css's to
4399 * be disabled before proceeding to the second phase of cgroup
4400 * destruction. If we are the last one, kick it off.
4401 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004402 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004403 cgroup_destroy_css_killed(cgrp);
4404
4405 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004406 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004407
4408 /*
4409 * Put the css refs from kill_css(). Each css holds an extra
4410 * reference to the cgroup's dentry and cgroup removal proceeds
4411 * regardless of css refs. On the last put of each css, whenever
4412 * that may be, the extra dentry ref is put so that dentry
4413 * destruction happens only after all css's are released.
4414 */
4415 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004416}
4417
Tejun Heo223dbc32013-08-13 20:22:50 -04004418/* css kill confirmation processing requires process context, bounce */
4419static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004420{
4421 struct cgroup_subsys_state *css =
4422 container_of(ref, struct cgroup_subsys_state, refcnt);
4423
Tejun Heo223dbc32013-08-13 20:22:50 -04004424 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004425 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004426}
4427
Tejun Heof392e512014-04-23 11:13:14 -04004428/**
4429 * kill_css - destroy a css
4430 * @css: css to destroy
4431 *
4432 * This function initiates destruction of @css by removing cgroup interface
4433 * files and putting its base reference. ->css_offline() will be invoked
4434 * asynchronously once css_tryget() is guaranteed to fail and when the
4435 * reference count reaches zero, @css will be released.
4436 */
4437static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004438{
Tejun Heo94419622014-03-19 10:23:54 -04004439 lockdep_assert_held(&cgroup_tree_mutex);
4440
Tejun Heo2bd59d42014-02-11 11:52:49 -05004441 /*
4442 * This must happen before css is disassociated with its cgroup.
4443 * See seq_css() for details.
4444 */
Tejun Heoaec25022014-02-08 10:36:58 -05004445 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004446
Tejun Heoedae0c32013-08-13 20:22:51 -04004447 /*
4448 * Killing would put the base ref, but we need to keep it alive
4449 * until after ->css_offline().
4450 */
4451 css_get(css);
4452
4453 /*
4454 * cgroup core guarantees that, by the time ->css_offline() is
4455 * invoked, no new css reference will be given out via
4456 * css_tryget(). We can't simply call percpu_ref_kill() and
4457 * proceed to offlining css's because percpu_ref_kill() doesn't
4458 * guarantee that the ref is seen as killed on all CPUs on return.
4459 *
4460 * Use percpu_ref_kill_and_confirm() to get notifications as each
4461 * css is confirmed to be seen as killed on all CPUs.
4462 */
4463 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004464}
4465
4466/**
4467 * cgroup_destroy_locked - the first stage of cgroup destruction
4468 * @cgrp: cgroup to be destroyed
4469 *
4470 * css's make use of percpu refcnts whose killing latency shouldn't be
4471 * exposed to userland and are RCU protected. Also, cgroup core needs to
4472 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4473 * invoked. To satisfy all the requirements, destruction is implemented in
4474 * the following two steps.
4475 *
4476 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4477 * userland visible parts and start killing the percpu refcnts of
4478 * css's. Set up so that the next stage will be kicked off once all
4479 * the percpu refcnts are confirmed to be killed.
4480 *
4481 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4482 * rest of destruction. Once all cgroup references are gone, the
4483 * cgroup is RCU-freed.
4484 *
4485 * This function implements s1. After this step, @cgrp is gone as far as
4486 * the userland is concerned and a new cgroup with the same name may be
4487 * created. As cgroup doesn't care about the names internally, this
4488 * doesn't cause any problem.
4489 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004490static int cgroup_destroy_locked(struct cgroup *cgrp)
4491 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004493 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004494 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004495 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004496 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497
Tejun Heoace2bee2014-02-11 11:52:47 -05004498 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004499 lockdep_assert_held(&cgroup_mutex);
4500
Tejun Heoddd69142013-06-12 21:04:54 -07004501 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004502 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004503 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004504 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004505 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004506 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004507 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004508 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004509 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004510
Tejun Heo1a90dd52012-11-05 09:16:59 -08004511 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004512 * Make sure there's no live children. We can't test ->children
4513 * emptiness as dead children linger on it while being destroyed;
4514 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4515 */
4516 empty = true;
4517 rcu_read_lock();
4518 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4519 empty = cgroup_is_dead(child);
4520 if (!empty)
4521 break;
4522 }
4523 rcu_read_unlock();
4524 if (!empty)
4525 return -EBUSY;
4526
4527 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004528 * Mark @cgrp dead. This prevents further task migration and child
4529 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004530 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004531 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004532 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004533 */
Tejun Heo54766d42013-06-12 21:04:53 -07004534 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004535
Tejun Heo5d773812014-03-19 10:23:53 -04004536 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004537 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4538 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004539 * percpu refs of all css's are confirmed to be killed. This
4540 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004541 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004542 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004543 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004544 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004545 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004546
Tejun Heo455050d2013-06-13 19:27:41 -07004547 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4548 raw_spin_lock(&release_list_lock);
4549 if (!list_empty(&cgrp->release_list))
4550 list_del_init(&cgrp->release_list);
4551 raw_spin_unlock(&release_list_lock);
4552
4553 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004554 * If @cgrp has css's attached, the second stage of cgroup
4555 * destruction is kicked off from css_killed_work_fn() after the
4556 * refs of all attached css's are killed. If @cgrp doesn't have
4557 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004558 */
Tejun Heof20104d2013-08-13 20:22:50 -04004559 if (!cgrp->nr_css)
4560 cgroup_destroy_css_killed(cgrp);
4561
Tejun Heo2bd59d42014-02-11 11:52:49 -05004562 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004563 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004564
Tejun Heof20104d2013-08-13 20:22:50 -04004565 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004566 * There are two control paths which try to determine cgroup from
4567 * dentry without going through kernfs - cgroupstats_build() and
4568 * css_tryget_from_dir(). Those are supported by RCU protecting
4569 * clearing of cgrp->kn->priv backpointer, which should happen
4570 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004571 */
Tejun Heo6f305582014-02-12 09:29:50 -05004572 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004573 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004574
Tejun Heo4ac06012014-02-11 11:52:47 -05004575 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004576
Tejun Heoea15f8c2013-06-13 19:27:42 -07004577 return 0;
4578};
4579
Tejun Heod3daf282013-06-13 19:39:16 -07004580/**
Tejun Heof20104d2013-08-13 20:22:50 -04004581 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004582 * @work: cgroup->destroy_free_work
4583 *
4584 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004585 * destroyed after all css's are offlined and performs the rest of
4586 * destruction. This is the second step of destruction described in the
4587 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004588 */
Tejun Heof20104d2013-08-13 20:22:50 -04004589static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004590{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004591 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004592
Tejun Heoace2bee2014-02-11 11:52:47 -05004593 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004594 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004595
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004596 /* delete this cgroup from parent->children */
4597 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004598
Tejun Heo59f52962014-02-11 11:52:49 -05004599 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004600
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004602 check_for_release(parent);
4603}
4604
Tejun Heo2bd59d42014-02-11 11:52:49 -05004605static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004606{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004607 struct cgroup *cgrp = kn->priv;
4608 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004609
Tejun Heo2bd59d42014-02-11 11:52:49 -05004610 /*
4611 * This is self-destruction but @kn can't be removed while this
4612 * callback is in progress. Let's break active protection. Once
4613 * the protection is broken, @cgrp can be destroyed at any point.
4614 * Pin it so that it stays accessible.
4615 */
4616 cgroup_get(cgrp);
4617 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004618
Tejun Heoace2bee2014-02-11 11:52:47 -05004619 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004620 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004621
Tejun Heo2bd59d42014-02-11 11:52:49 -05004622 /*
4623 * @cgrp might already have been destroyed while we're trying to
4624 * grab the mutexes.
4625 */
4626 if (!cgroup_is_dead(cgrp))
4627 ret = cgroup_destroy_locked(cgrp);
4628
Tejun Heo42809dd2012-11-19 08:13:37 -08004629 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004630 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004631
Tejun Heo2bd59d42014-02-11 11:52:49 -05004632 kernfs_unbreak_active_protection(kn);
4633 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004634 return ret;
4635}
4636
Tejun Heo2bd59d42014-02-11 11:52:49 -05004637static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4638 .remount_fs = cgroup_remount,
4639 .show_options = cgroup_show_options,
4640 .mkdir = cgroup_mkdir,
4641 .rmdir = cgroup_rmdir,
4642 .rename = cgroup_rename,
4643};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004644
Li Zefan06a11922008-04-29 01:00:07 -07004645static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004648
4649 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650
Tejun Heoace2bee2014-02-11 11:52:47 -05004651 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004652 mutex_lock(&cgroup_mutex);
4653
Tejun Heo0adb0702014-02-12 09:29:48 -05004654 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004655
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004656 /* Create the root cgroup state for this subsystem */
4657 ss->root = &cgrp_dfl_root;
4658 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659 /* We don't handle early failures gracefully */
4660 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004661 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662
Li Zefane8d55fd2008-04-29 01:00:13 -07004663 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004664 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004665 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004666 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004667 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668
4669 need_forkexit_callback |= ss->fork || ss->exit;
4670
Li Zefane8d55fd2008-04-29 01:00:13 -07004671 /* At system boot, before all subsystems have been
4672 * registered, no tasks have been forked, so we don't
4673 * need to invoke fork callbacks here. */
4674 BUG_ON(!list_empty(&init_task.tasks));
4675
Tejun Heoae7f1642013-08-13 20:22:50 -04004676 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004677
Tejun Heof392e512014-04-23 11:13:14 -04004678 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004679
Ben Blume6a11052010-03-10 15:22:09 -08004680 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004681 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004682}
4683
4684/**
Li Zefana043e3b2008-02-23 15:24:09 -08004685 * cgroup_init_early - cgroup initialization at system boot
4686 *
4687 * Initialize cgroups at system boot, and initialize any
4688 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004689 */
4690int __init cgroup_init_early(void)
4691{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004692 static struct cgroup_sb_opts __initdata opts =
4693 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004694 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004695 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004696
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004697 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004698 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004699
Tejun Heo3ed80a62014-02-08 10:36:58 -05004700 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004701 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004702 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4703 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004704 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004705 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4706 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004707
Tejun Heoaec25022014-02-08 10:36:58 -05004708 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004709 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004710
4711 if (ss->early_init)
4712 cgroup_init_subsys(ss);
4713 }
4714 return 0;
4715}
4716
4717/**
Li Zefana043e3b2008-02-23 15:24:09 -08004718 * cgroup_init - cgroup initialization
4719 *
4720 * Register cgroup filesystem and /proc file, and initialize
4721 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 */
4723int __init cgroup_init(void)
4724{
Tejun Heo30159ec2013-06-25 11:53:37 -07004725 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004726 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004727 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004728
Tejun Heo2bd59d42014-02-11 11:52:49 -05004729 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730
Tejun Heo985ed672014-03-19 10:23:53 -04004731 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004732 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004733
Tejun Heo82fe9b02013-06-25 11:53:37 -07004734 /* Add init_css_set to the hash table */
4735 key = css_set_hash(init_css_set.subsys);
4736 hash_add(css_set_table, &init_css_set.hlist, key);
4737
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004738 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004739
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004740 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004741 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004742
Tejun Heo172a2c062014-03-19 10:23:53 -04004743 for_each_subsys(ss, ssid) {
4744 if (!ss->early_init)
4745 cgroup_init_subsys(ss);
4746
Tejun Heo2d8f2432014-04-23 11:13:15 -04004747 list_add_tail(&init_css_set.e_cset_node[ssid],
4748 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4749
Tejun Heo172a2c062014-03-19 10:23:53 -04004750 /*
4751 * cftype registration needs kmalloc and can't be done
4752 * during early_init. Register base cftypes separately.
4753 */
4754 if (ss->base_cftypes)
4755 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4756 }
Greg KH676db4a2010-08-05 13:53:35 -07004757
Paul Menageddbcc7e2007-10-18 23:39:30 -07004758 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004759 if (!cgroup_kobj)
4760 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004761
4762 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004763 if (err < 0) {
4764 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004765 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004766 }
4767
4768 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004769 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004770}
Paul Menageb4f48b62007-10-18 23:39:33 -07004771
Tejun Heoe5fca242013-11-22 17:14:39 -05004772static int __init cgroup_wq_init(void)
4773{
4774 /*
4775 * There isn't much point in executing destruction path in
4776 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004777 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004778 *
4779 * We would prefer to do this in cgroup_init() above, but that
4780 * is called before init_workqueues(): so leave this until after.
4781 */
Tejun Heo1a115332014-02-12 19:06:19 -05004782 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004783 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004784
4785 /*
4786 * Used to destroy pidlists and separate to serve as flush domain.
4787 * Cap @max_active to 1 too.
4788 */
4789 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4790 0, 1);
4791 BUG_ON(!cgroup_pidlist_destroy_wq);
4792
Tejun Heoe5fca242013-11-22 17:14:39 -05004793 return 0;
4794}
4795core_initcall(cgroup_wq_init);
4796
Paul Menagea4243162007-10-18 23:39:35 -07004797/*
4798 * proc_cgroup_show()
4799 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4800 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004801 */
4802
4803/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004804int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004805{
4806 struct pid *pid;
4807 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004808 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004809 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004810 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004811
4812 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004813 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004814 if (!buf)
4815 goto out;
4816
4817 retval = -ESRCH;
4818 pid = m->private;
4819 tsk = get_pid_task(pid, PIDTYPE_PID);
4820 if (!tsk)
4821 goto out_free;
4822
4823 retval = 0;
4824
4825 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004826 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004827
Tejun Heo985ed672014-03-19 10:23:53 -04004828 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004829 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004830 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004831 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004832
Tejun Heoa2dd4242014-03-19 10:23:55 -04004833 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004834 continue;
4835
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004836 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004837 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004838 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004839 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004840 if (strlen(root->name))
4841 seq_printf(m, "%sname=%s", count ? "," : "",
4842 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004843 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004844 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004845 path = cgroup_path(cgrp, buf, PATH_MAX);
4846 if (!path) {
4847 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004848 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004849 }
4850 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004851 seq_putc(m, '\n');
4852 }
4853
4854out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004855 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004856 mutex_unlock(&cgroup_mutex);
4857 put_task_struct(tsk);
4858out_free:
4859 kfree(buf);
4860out:
4861 return retval;
4862}
4863
Paul Menagea4243162007-10-18 23:39:35 -07004864/* Display information about each subsystem and each hierarchy */
4865static int proc_cgroupstats_show(struct seq_file *m, void *v)
4866{
Tejun Heo30159ec2013-06-25 11:53:37 -07004867 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004868 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004869
Paul Menage8bab8dd2008-04-04 14:29:57 -07004870 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004871 /*
4872 * ideally we don't want subsystems moving around while we do this.
4873 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4874 * subsys/hierarchy state.
4875 */
Paul Menagea4243162007-10-18 23:39:35 -07004876 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004877
4878 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004879 seq_printf(m, "%s\t%d\t%d\t%d\n",
4880 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004881 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004882
Paul Menagea4243162007-10-18 23:39:35 -07004883 mutex_unlock(&cgroup_mutex);
4884 return 0;
4885}
4886
4887static int cgroupstats_open(struct inode *inode, struct file *file)
4888{
Al Viro9dce07f2008-03-29 03:07:28 +00004889 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004890}
4891
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004892static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004893 .open = cgroupstats_open,
4894 .read = seq_read,
4895 .llseek = seq_lseek,
4896 .release = single_release,
4897};
4898
Paul Menageb4f48b62007-10-18 23:39:33 -07004899/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004900 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004901 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004902 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004903 * A task is associated with the init_css_set until cgroup_post_fork()
4904 * attaches it to the parent's css_set. Empty cg_list indicates that
4905 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004906 */
4907void cgroup_fork(struct task_struct *child)
4908{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004909 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004910 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004911}
4912
4913/**
Li Zefana043e3b2008-02-23 15:24:09 -08004914 * cgroup_post_fork - called on a new task after adding it to the task list
4915 * @child: the task in question
4916 *
Tejun Heo5edee612012-10-16 15:03:14 -07004917 * Adds the task to the list running through its css_set if necessary and
4918 * call the subsystem fork() callbacks. Has to be after the task is
4919 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004920 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004921 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004922 */
Paul Menage817929e2007-10-18 23:39:36 -07004923void cgroup_post_fork(struct task_struct *child)
4924{
Tejun Heo30159ec2013-06-25 11:53:37 -07004925 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004926 int i;
4927
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004928 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004929 * This may race against cgroup_enable_task_cg_links(). As that
4930 * function sets use_task_css_set_links before grabbing
4931 * tasklist_lock and we just went through tasklist_lock to add
4932 * @child, it's guaranteed that either we see the set
4933 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4934 * @child during its iteration.
4935 *
4936 * If we won the race, @child is associated with %current's
4937 * css_set. Grabbing css_set_rwsem guarantees both that the
4938 * association is stable, and, on completion of the parent's
4939 * migration, @child is visible in the source of migration or
4940 * already in the destination cgroup. This guarantee is necessary
4941 * when implementing operations which need to migrate all tasks of
4942 * a cgroup to another.
4943 *
4944 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4945 * will remain in init_css_set. This is safe because all tasks are
4946 * in the init_css_set before cg_links is enabled and there's no
4947 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004948 */
Paul Menage817929e2007-10-18 23:39:36 -07004949 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004950 struct css_set *cset;
4951
Tejun Heo96d365e2014-02-13 06:58:40 -05004952 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004953 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004954 if (list_empty(&child->cg_list)) {
4955 rcu_assign_pointer(child->cgroups, cset);
4956 list_add(&child->cg_list, &cset->tasks);
4957 get_css_set(cset);
4958 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004959 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004960 }
Tejun Heo5edee612012-10-16 15:03:14 -07004961
4962 /*
4963 * Call ss->fork(). This must happen after @child is linked on
4964 * css_set; otherwise, @child might change state between ->fork()
4965 * and addition to css_set.
4966 */
4967 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004968 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004969 if (ss->fork)
4970 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004971 }
Paul Menage817929e2007-10-18 23:39:36 -07004972}
Tejun Heo5edee612012-10-16 15:03:14 -07004973
Paul Menage817929e2007-10-18 23:39:36 -07004974/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004975 * cgroup_exit - detach cgroup from exiting task
4976 * @tsk: pointer to task_struct of exiting process
4977 *
4978 * Description: Detach cgroup from @tsk and release it.
4979 *
4980 * Note that cgroups marked notify_on_release force every task in
4981 * them to take the global cgroup_mutex mutex when exiting.
4982 * This could impact scaling on very large systems. Be reluctant to
4983 * use notify_on_release cgroups where very high task exit scaling
4984 * is required on large systems.
4985 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004986 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4987 * call cgroup_exit() while the task is still competent to handle
4988 * notify_on_release(), then leave the task attached to the root cgroup in
4989 * each hierarchy for the remainder of its exit. No need to bother with
4990 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004991 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004992 */
Li Zefan1ec41832014-03-28 15:22:19 +08004993void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004994{
Tejun Heo30159ec2013-06-25 11:53:37 -07004995 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004996 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004997 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004998 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004999
5000 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005001 * Unlink from @tsk from its css_set. As migration path can't race
5002 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005003 */
5004 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005005 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005006 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005007 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005008 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005009 }
5010
Paul Menageb4f48b62007-10-18 23:39:33 -07005011 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005012 cset = task_css_set(tsk);
5013 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005014
Li Zefan1ec41832014-03-28 15:22:19 +08005015 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005016 /* see cgroup_post_fork() for details */
5017 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005018 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005019 struct cgroup_subsys_state *old_css = cset->subsys[i];
5020 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005021
Tejun Heoeb954192013-08-08 20:11:23 -04005022 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005023 }
5024 }
5025 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005026
Tejun Heoeaf797a2014-02-25 10:04:03 -05005027 if (put_cset)
5028 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005029}
Paul Menage697f4162007-10-18 23:39:34 -07005030
Paul Menagebd89aab2007-10-18 23:40:44 -07005031static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005032{
Li Zefanf50daa72013-03-01 15:06:07 +08005033 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005034 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005035 /*
5036 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005037 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005038 * it now
5039 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005041
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005042 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005043 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005044 list_empty(&cgrp->release_list)) {
5045 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046 need_schedule_work = 1;
5047 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005048 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 if (need_schedule_work)
5050 schedule_work(&release_agent_work);
5051 }
5052}
5053
Paul Menage81a6a5c2007-10-18 23:39:38 -07005054/*
5055 * Notify userspace when a cgroup is released, by running the
5056 * configured release agent with the name of the cgroup (path
5057 * relative to the root of cgroup file system) as the argument.
5058 *
5059 * Most likely, this user command will try to rmdir this cgroup.
5060 *
5061 * This races with the possibility that some other task will be
5062 * attached to this cgroup before it is removed, or that some other
5063 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5064 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5065 * unused, and this cgroup will be reprieved from its death sentence,
5066 * to continue to serve a useful existence. Next time it's released,
5067 * we will get notified again, if it still has 'notify_on_release' set.
5068 *
5069 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5070 * means only wait until the task is successfully execve()'d. The
5071 * separate release agent task is forked by call_usermodehelper(),
5072 * then control in this thread returns here, without waiting for the
5073 * release agent task. We don't bother to wait because the caller of
5074 * this routine has no use for the exit status of the release agent
5075 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005077static void cgroup_release_agent(struct work_struct *work)
5078{
5079 BUG_ON(work != &release_agent_work);
5080 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005081 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005082 while (!list_empty(&release_list)) {
5083 char *argv[3], *envp[3];
5084 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005085 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005086 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005087 struct cgroup,
5088 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005089 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005090 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005091 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005092 if (!pathbuf)
5093 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005094 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5095 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005096 goto continue_free;
5097 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5098 if (!agentbuf)
5099 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005100
5101 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005102 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005103 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005104 argv[i] = NULL;
5105
5106 i = 0;
5107 /* minimal command environment */
5108 envp[i++] = "HOME=/";
5109 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5110 envp[i] = NULL;
5111
5112 /* Drop the lock while we invoke the usermode helper,
5113 * since the exec could involve hitting disk and hence
5114 * be a slow process */
5115 mutex_unlock(&cgroup_mutex);
5116 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005117 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005118 continue_free:
5119 kfree(pathbuf);
5120 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005121 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005122 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005123 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005124 mutex_unlock(&cgroup_mutex);
5125}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005126
5127static int __init cgroup_disable(char *str)
5128{
Tejun Heo30159ec2013-06-25 11:53:37 -07005129 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005130 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005131 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005132
5133 while ((token = strsep(&str, ",")) != NULL) {
5134 if (!*token)
5135 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005136
Tejun Heo3ed80a62014-02-08 10:36:58 -05005137 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005138 if (!strcmp(token, ss->name)) {
5139 ss->disabled = 1;
5140 printk(KERN_INFO "Disabling %s control group"
5141 " subsystem\n", ss->name);
5142 break;
5143 }
5144 }
5145 }
5146 return 1;
5147}
5148__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005149
Tejun Heob77d7b62013-08-13 11:01:54 -04005150/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005151 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005152 * @dentry: directory dentry of interest
5153 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005154 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005155 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5156 * to get the corresponding css and return it. If such css doesn't exist
5157 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005158 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005159struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5160 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005161{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005162 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5163 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005164 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005165
Tejun Heo35cf0832013-08-26 18:40:56 -04005166 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005167 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5168 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005169 return ERR_PTR(-EBADF);
5170
Tejun Heo5a17f542014-02-11 11:52:47 -05005171 rcu_read_lock();
5172
Tejun Heo2bd59d42014-02-11 11:52:49 -05005173 /*
5174 * This path doesn't originate from kernfs and @kn could already
5175 * have been or be removed at any point. @kn->priv is RCU
5176 * protected for this access. See destroy_locked() for details.
5177 */
5178 cgrp = rcu_dereference(kn->priv);
5179 if (cgrp)
5180 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005181
5182 if (!css || !css_tryget(css))
5183 css = ERR_PTR(-ENOENT);
5184
5185 rcu_read_unlock();
5186 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005187}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005188
Li Zefan1cb650b2013-08-19 10:05:24 +08005189/**
5190 * css_from_id - lookup css by id
5191 * @id: the cgroup id
5192 * @ss: cgroup subsys to be looked into
5193 *
5194 * Returns the css if there's valid one with @id, otherwise returns NULL.
5195 * Should be called under rcu_read_lock().
5196 */
5197struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5198{
5199 struct cgroup *cgrp;
5200
Tejun Heo6fa49182014-05-04 15:09:13 -04005201 WARN_ON_ONCE(!rcu_read_lock_held());
Li Zefan1cb650b2013-08-19 10:05:24 +08005202
5203 cgrp = idr_find(&ss->root->cgroup_idr, id);
5204 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005205 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005206 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005207}
5208
Paul Menagefe693432009-09-23 15:56:20 -07005209#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005210static struct cgroup_subsys_state *
5211debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005212{
5213 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5214
5215 if (!css)
5216 return ERR_PTR(-ENOMEM);
5217
5218 return css;
5219}
5220
Tejun Heoeb954192013-08-08 20:11:23 -04005221static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005222{
Tejun Heoeb954192013-08-08 20:11:23 -04005223 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005224}
5225
Tejun Heo182446d2013-08-08 20:11:24 -04005226static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5227 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005228{
Tejun Heo182446d2013-08-08 20:11:24 -04005229 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005230}
5231
Tejun Heo182446d2013-08-08 20:11:24 -04005232static u64 current_css_set_read(struct cgroup_subsys_state *css,
5233 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005234{
5235 return (u64)(unsigned long)current->cgroups;
5236}
5237
Tejun Heo182446d2013-08-08 20:11:24 -04005238static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005239 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005240{
5241 u64 count;
5242
5243 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005244 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005245 rcu_read_unlock();
5246 return count;
5247}
5248
Tejun Heo2da8ca82013-12-05 12:28:04 -05005249static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005250{
Tejun Heo69d02062013-06-12 21:04:50 -07005251 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005252 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005253 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005254
Tejun Heoe61734c2014-02-12 09:29:50 -05005255 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5256 if (!name_buf)
5257 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005258
Tejun Heo96d365e2014-02-13 06:58:40 -05005259 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005260 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005261 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005262 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005263 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005264
Tejun Heoa2dd4242014-03-19 10:23:55 -04005265 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005266 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005267 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005268 }
5269 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005270 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005271 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005272 return 0;
5273}
5274
5275#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005276static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005277{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005278 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005279 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005280
Tejun Heo96d365e2014-02-13 06:58:40 -05005281 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005282 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005283 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005284 struct task_struct *task;
5285 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005286
Tejun Heo5abb8852013-06-12 21:04:49 -07005287 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005288
Tejun Heo5abb8852013-06-12 21:04:49 -07005289 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005290 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5291 goto overflow;
5292 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005293 }
Tejun Heoc7561122014-02-25 10:04:01 -05005294
5295 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5296 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5297 goto overflow;
5298 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5299 }
5300 continue;
5301 overflow:
5302 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005303 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005304 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005305 return 0;
5306}
5307
Tejun Heo182446d2013-08-08 20:11:24 -04005308static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005309{
Tejun Heo182446d2013-08-08 20:11:24 -04005310 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005311}
5312
5313static struct cftype debug_files[] = {
5314 {
Paul Menagefe693432009-09-23 15:56:20 -07005315 .name = "taskcount",
5316 .read_u64 = debug_taskcount_read,
5317 },
5318
5319 {
5320 .name = "current_css_set",
5321 .read_u64 = current_css_set_read,
5322 },
5323
5324 {
5325 .name = "current_css_set_refcount",
5326 .read_u64 = current_css_set_refcount_read,
5327 },
5328
5329 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005330 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005331 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005332 },
5333
5334 {
5335 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005336 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005337 },
5338
5339 {
Paul Menagefe693432009-09-23 15:56:20 -07005340 .name = "releasable",
5341 .read_u64 = releasable_read,
5342 },
Paul Menagefe693432009-09-23 15:56:20 -07005343
Tejun Heo4baf6e32012-04-01 12:09:55 -07005344 { } /* terminate */
5345};
Paul Menagefe693432009-09-23 15:56:20 -07005346
Tejun Heo073219e2014-02-08 10:36:58 -05005347struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005348 .css_alloc = debug_css_alloc,
5349 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005350 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005351};
5352#endif /* CONFIG_CGROUP_DEBUG */